Repository: Project-MONAI/MONAI
File: monai/engines/workflow.py (Python)

Code before:
        metric_cmp_fn: Callable = default_metric_cmp_fn,
        handlers: Sequence | None = None,
        amp: bool = False,
        event_names: list[str | EventEnum | type[EventEnum]] | None = None,
        event_to_attr: dict | None = None,
        decollate: bool = True,
        to_kwargs: dict | None = None,
        amp_kwargs: dict | None = None,
    ) -> None:
        super().__init__(self._iteration if iteration_update is None else iteration_update)

        if isinstance(data_loader, DataLoader):
            sampler = getattr(data_loader, "sampler", None)

            # set the epoch value for DistributedSampler objects when an epoch starts
            if isinstance(sampler, DistributedSampler):

                @self.on(Events.EPOCH_STARTED)
                def set_sampler_epoch(engine: Engine) -> None:
                    sampler.set_epoch(engine.state.epoch)

            # if the epoch_length isn't given, attempt to get it from the length of the data loader
            if epoch_length is None:
                try:
                    epoch_length = len(data_loader)
                except TypeError:  # raised when data_loader is given an iterable dataset which has no length
                    pass  # deliberately leave epoch_length as None

        # set all sharable data for the workflow based on Ignite engine.state
        self.state: Any = State(
            rank=dist.get_rank() if dist.is_available() and dist.is_initialized() else 0,
            seed=0,
            iteration=0,
            epoch=0,
            max_epochs=max_epochs,
            epoch_length=epoch_length,  # None when the dataset is iterable and so has no length
            output=None,
            batch=None,
            metrics={},
            metric_details={},
            dataloader=None,
            device=device if isinstance(device, torch.device) or device is None else torch.device(device),
            key_metric_name=None,  # we can set many metrics, only use key_metric to compare and save the best model
            best_metric=-1,
            best_metric_epoch=-1,
        )
        self.data_loader = data_loader
        self.non_blocking = non_blocking
        self.prepare_batch = prepare_batch
        self.metric_cmp_fn = metric_cmp_fn
        self.amp = amp

Review comment (suggestion):
Do you think we should add a warning here or validate the dataset instance in the `data_loader`?
```
isinstance(data_loader.dataset, Iterable)
```

Code after:
        metric_cmp_fn: Callable = default_metric_cmp_fn,
        handlers: Sequence | None = None,
        amp: bool = False,
        event_names: list[str | EventEnum | type[EventEnum]] | None = None,
        event_to_attr: dict | None = None,
        decollate: bool = True,
        to_kwargs: dict | None = None,
        amp_kwargs: dict | None = None,
    ) -> None:
        super().__init__(self._iteration if iteration_update is None else iteration_update)

        if isinstance(data_loader, DataLoader):
            sampler = getattr(data_loader, "sampler", None)

            # set the epoch value for DistributedSampler objects when an epoch starts
            if isinstance(sampler, DistributedSampler):

                @self.on(Events.EPOCH_STARTED)
                def set_sampler_epoch(engine: Engine) -> None:
                    sampler.set_epoch(engine.state.epoch)

        # if the epoch_length isn't given, attempt to get it from the length of the data loader
        if epoch_length is None and isinstance(data_loader, Sized):
            try:
                epoch_length = len(data_loader)
            except TypeError:  # raised when data_loader has an iterable dataset with no length, or is some other type
                pass  # deliberately leave epoch_length as None

        # set all sharable data for the workflow based on Ignite engine.state
        self.state: Any = State(
            rank=dist.get_rank() if dist.is_available() and dist.is_initialized() else 0,
            seed=0,
            iteration=0,
            epoch=0,
            max_epochs=max_epochs,
            epoch_length=epoch_length,  # None when the dataset is iterable and so has no length
            output=None,
            batch=None,
            metrics={},
            metric_details={},
            dataloader=None,
            device=device if isinstance(device, torch.device) or device is None else torch.device(device),
            key_metric_name=None,  # we can set many metrics, only use key_metric to compare and save the best model
            best_metric=-1,
            best_metric_epoch=-1,
        )
        self.data_loader = data_loader
        self.non_blocking = non_blocking
        self.prepare_batch = prepare_batch
        self.metric_cmp_fn = metric_cmp_fn
        self.amp = amp