ó
    6êbiŠ  ã                   óN   • S r SSKJs  Jr  SSKJr  \" S/ S9 " S S5      5       rg)z&Input dataset creator for `model.fit`.é    N)Úkeras_exportz'keras.utils.experimental.DatasetCreator)Úv1c                   ó(   • \ rS rSrSrSS jrS rSrg)ÚDatasetCreatoré   a×	  Object that returns a `tf.data.Dataset` upon invoking.

`tf.keras.utils.experimental.DatasetCreator` is designated as a supported
type for `x`, or the input, in `tf.keras.Model.fit`. Pass an instance of
this class to `fit` when using a callable (with a `input_context` argument)
that returns a `tf.data.Dataset`.

```python
model = tf.keras.Sequential([tf.keras.layers.Dense(10)])
model.compile(tf.keras.optimizers.SGD(), loss="mse")

def dataset_fn(input_context):
  global_batch_size = 64
  batch_size = input_context.get_per_replica_batch_size(global_batch_size)
  dataset = tf.data.Dataset.from_tensors(([1.], [1.])).repeat()
  dataset = dataset.shard(
      input_context.num_input_pipelines, input_context.input_pipeline_id)
  dataset = dataset.batch(batch_size)
  dataset = dataset.prefetch(2)
  return dataset

input_options = tf.distribute.InputOptions(
    experimental_fetch_to_device=True,
    experimental_per_replica_buffer_size=2)
model.fit(tf.keras.utils.experimental.DatasetCreator(
    dataset_fn, input_options=input_options), epochs=10, steps_per_epoch=10)
```

`Model.fit` usage with `DatasetCreator` is intended to work across all
`tf.distribute.Strategy`s, as long as `Strategy.scope` is used at model
creation:

```python
strategy = tf.distribute.experimental.ParameterServerStrategy(
    cluster_resolver)
with strategy.scope():
  model = tf.keras.Sequential([tf.keras.layers.Dense(10)])
model.compile(tf.keras.optimizers.SGD(), loss="mse")

def dataset_fn(input_context):
  ...

input_options = ...
model.fit(tf.keras.utils.experimental.DatasetCreator(
    dataset_fn, input_options=input_options), epochs=10, steps_per_epoch=10)
```

Note: When using `DatasetCreator`, `steps_per_epoch` argument in `Model.fit`
must be provided as the cardinality of such input cannot be inferred.

Args:
  dataset_fn: A callable that takes a single argument of type
    `tf.distribute.InputContext`, which is used for batch size calculation
    and cross-worker input pipeline sharding (if neither is needed, the
    `InputContext` parameter can be ignored in the `dataset_fn`), and
    returns a `tf.data.Dataset`.
  input_options: Optional `tf.distribute.InputOptions`, used for specific
    options when used with distribution, for example, whether to prefetch
    dataset elements to accelerator device memory or host device memory, and
    prefetch buffer size in the replica device memory. No effect if not used
    with distributed training. See `tf.distribute.InputOptions` for more
    information.
Nc                 óÔ   • [        U5      (       d  [        SU 35      eU(       a7  [        U[        R                  R
                  5      (       d  [        SU 35      eXl        X l        g )NzB`dataset_fn` for `DatasetCreator` must be a `callable`. Received: zW`input_options` for `DatasetCreator` must be a `tf.distribute.InputOptions`. Received: )ÚcallableÚ	TypeErrorÚ
isinstanceÚtfÚ
distributeÚInputOptionsÚ
dataset_fnÚinput_options)Úselfr   r   s      Ú\/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/utils/dataset_creator.pyÚ__init__ÚDatasetCreator.__init__Z   so   € Ü˜
×#Ñ#ÜðØ'˜Lð*óð ö Ü˜=¬"¯-©-×*DÑ*D×EÑEäð;Ø;H¸/ðKóð ð
 %ŒØ*Õó    c                 óš   • U R                   " U0 UD6n[        U[        R                  R                  5      (       d  [        SU S35      eU$ )NzOThe `callable` provided to `DatasetCreator` must return a Dataset. It returns "Ú")r   r   r   ÚdataÚDatasetr
   )r   ÚargsÚkwargsÚdatasets       r   Ú__call__ÚDatasetCreator.__call__k   sQ   € ð —/’/ 4Ð2¨6Ñ2ˆÜ˜'¤2§7¡7§?¡?×3Ñ3Üð*Ø*1¨°!ð5óð ð ˆr   )r   r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   Ú__static_attributes__© r   r   r   r      s   † ñ>ô@+õ"	r   r   )r#   Útensorflow.compat.v2ÚcompatÚv2r   Ú tensorflow.python.util.tf_exportr   r   r%   r   r   Ú<module>r*      s:   ðñ  -ç !Ð !õ :ñ Ð7¸BÑ?÷[ð [ó @ñ[r   