class_docstr: A dataset of DTensors.
  
  DTensorDataset encapsulates a `tf.data.Dataset` whose elements are
  automatically packed and returned as DTensors based on a given mesh and
  layouts.
class_docstr_tok: ["A","dataset","of","DTensors",".","DTensorDataset","encapsulates","a","`","tf.data.Dataset","`","whose","elements","are","automatically","packed","and","returned","as","DTensors","based","on","a","given","mesh","and","layouts","."]
code: def __iter__(self):
      datasets: List[Tuple[int, data_types.DatasetV2]] = []
  
      # Start with the batched the dataset.
      local_dataset = self._batched_dataset
  
      if self._batch_dim is not None:
          if self._num_clients_per_replica > 1:
              # If a replica is split over multiple clients then each batch needs to
              # be repeated before distribution as many times as there are clients
              # corresponding to that replica.
              local_dataset = self._repeat_batch(
                  local_dataset, self._num_clients_per_replica
              )
              sharding_policy = data_service_ops.ShardingPolicy.DATA
          else:
              # Replicas are unique to each client, so FILE based sharding can be used
              # which is more performant since each worker does not need to read the
              # entire dataset.
              sharding_policy = data_service_ops.ShardingPolicy.FILE
      else:
          # No batch dimension sharding specified so disable dataset sharding during
          # the distribute step.
          sharding_policy = data_service_ops.ShardingPolicy.OFF
  
      # Apply distribution here (if specified) so all remaining transformations
      # are executed locally.
      if self._tf_data_service_config is not None:
          local_dataset = local_dataset.apply(
              data_service_ops.distribute(
                  processing_mode=sharding_policy,
                  service=self._tf_data_service_config.dispatcher_address,
                  job_name=f"{self._tf_data_service_config.job_name}_{config.client_id()}",
                  target_workers="LOCAL",
              )
          )
  
      for local_replica_idx, replica_id in enumerate(
          self._local_replica_ids
      ):
          # Select the shard for the corresponding replica.
          dataset = distribute._AutoShardDataset(
              local_dataset,
              num_workers=self._num_local_replicas,
              index=local_replica_idx,
              num_replicas=self.num_global_replicas,
          )
  
          # Repeat each batch for each local device in the replica.
          dataset = self._repeat_batch(
              dataset, self._num_local_devices_per_replica
          )
  
          # Slice each shard further for all non-batch dim shards. If there is no
          # non-batch dim sharding, this slice is essentially a no-op.
          dataset = self._partition(dataset)
  
          # Apply prefetch as the last step. Since each batch is repeated, the
          # number of elements to prefetch has to be scaled by the same size.
          if self._prefetch is not None:
              dataset = dataset.prefetch(
                  self._prefetch * self._num_local_devices_per_replica
              )
  
          datasets.append((replica_id, dataset))
  
      # Convert the datasets into iterators placed on the host.
      d_iterator_resource = _pack_iterator_resource_dtensor(
          datasets=datasets,
          layouts=self._layouts,
          mesh=self._mesh,
          num_local_devices_per_replica=self._num_local_devices_per_replica,
      )
  
      return _DTensorIterator(
          dtensor_components=(d_iterator_resource,),
          global_element_spec=self._global_element_spec,
          layouts=self._layouts,
      )
  
imports: import dataclasses
  import operator
  from typing import Any, List, Optional, Sequence, Tuple
  from tensorflow.dtensor.python import api
  from tensorflow.dtensor.python import config
  from tensorflow.dtensor.python import layout
  from tensorflow.python.data.experimental.ops import data_service_ops
  from tensorflow.python.data.experimental.ops import distribute
  from tensorflow.python.data.ops import dataset_ops
  from tensorflow.python.data.ops import iterator_ops
  from tensorflow.python.eager import context
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import errors
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor
  from tensorflow.python.framework import tensor_shape
  from tensorflow.python.framework import tensor_spec
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.types import data
  from tensorflow.python.util import nest
  from tensorflow.python.util.tf_export import tf_export
label_desc: Use image node_id 2 for calling the DTensorDataset obj's underlying member method code with example usage: obj.__iter__() and returns: _DTensorIterator