Repository: Project-MONAI/MONAI
File: monai/data/dataset.py (Python)

Code before:
                defaults to `monai.data.utils.pickle_hashing`.
            pickle_module: string representing the module used for pickling metadata and objects,
                default to `"pickle"`. due to the pickle limitation in multi-processing of Dataloader,
                we can't use `pickle` as arg directly, so here we use a string name instead.
                if want to use other pickle module at runtime, just register like:
                >>> from monai.data import utils
                >>> utils.SUPPORTED_PICKLE_MOD["test"] = other_pickle
                this arg is used by `torch.save`, for more details, please check:
                https://pytorch.org/docs/stable/generated/torch.save.html#torch.save,
                and ``monai.data.utils.SUPPORTED_PICKLE_MOD``.
            pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
                Defaults to torch.serialization.DEFAULT_PROTOCOL. For more details, please check:
                https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
            hash_transform: a callable to compute hash from the transform information when caching.
                This may reduce errors due to transforms changing during experiments. Default to None (no hash).
                Other options are `pickle_hashing` and `json_hashing` functions from `monai.data.utils`.
            reset_ops_id: whether to set `TraceKeys.ID` to ``Tracekys.NONE``, defaults to ``True``.
                When this is enabled, the traced transform instance IDs will be removed from the cached MetaTensors.
                This is useful for skipping the transform instance checks when inverting applied operations
                using the cached content and with re-created transform instances.
            track_meta: whether to track the meta information, if `True`, will convert to `MetaTensor`.
                default to `False`. Cannot be used with `weights_only=True`.
            weights_only: keyword argument passed to `torch.load` when reading cached files.
                default to `True`. When set to `True`, `torch.load` restricts loading to tensors and
                other safe objects. Setting this to `False` is required for loading `MetaTensor`
                objects saved with `track_meta=True`.

        Raises:
            ValueError: When both `track_meta=True` and `weights_only=True`, since this combination
                prevents cached MetaTensors from being reloaded and causes perpetual cache regeneration.
        """
        super().__init__(data=data, transform=transform)
        self.cache_dir = Path(cache_dir) if cache_dir is not None else None
        self.hash_func = hash_func
        self.pickle_module = pickle_module
        self.pickle_protocol = pickle_protocol
        if self.cache_dir is not None:
            if not self.cache_dir.exists():
                self.cache_dir.mkdir(parents=True, exist_ok=True)
            if not self.cache_dir.is_dir():
                raise ValueError("cache_dir must be a directory.")
        self.transform_hash: str = ""
        if hash_transform is not None:
            self.set_transform_hash(hash_transform)
        self.reset_ops_id = reset_ops_id
        if track_meta and weights_only:
            raise ValueError(
                "Invalid argument combination: `track_meta=True` cannot be used with `weights_only=True`. "
                "To cache and reload MetaTensors, set `track_meta=True` and `weights_only=False`."
            )
        self.track_meta = track_meta

Review comment (security):
```suggestion
                other safe objects. Setting this to `False` is required for loading `MetaTensor`
                objects saved with `track_meta=True`, however this creates the possibility of remote
                code execution through `torch.load` so be aware of the security implications of doing so.
```

I'd like to explicitly state the security concern here that motivated the previous changes to the datasets.

Code after:
                defaults to `monai.data.utils.pickle_hashing`.
            pickle_module: string representing the module used for pickling metadata and objects,
                default to `"pickle"`. due to the pickle limitation in multi-processing of Dataloader,
                we can't use `pickle` as arg directly, so here we use a string name instead.
                if want to use other pickle module at runtime, just register like:
                >>> from monai.data import utils
                >>> utils.SUPPORTED_PICKLE_MOD["test"] = other_pickle
                this arg is used by `torch.save`, for more details, please check:
                https://pytorch.org/docs/stable/generated/torch.save.html#torch.save,
                and ``monai.data.utils.SUPPORTED_PICKLE_MOD``.
            pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
                Defaults to torch.serialization.DEFAULT_PROTOCOL. For more details, please check:
                https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
            hash_transform: a callable to compute hash from the transform information when caching.
                This may reduce errors due to transforms changing during experiments. Default to None (no hash).
                Other options are `pickle_hashing` and `json_hashing` functions from `monai.data.utils`.
            reset_ops_id: whether to set `TraceKeys.ID` to ``Tracekys.NONE``, defaults to ``True``.
                When this is enabled, the traced transform instance IDs will be removed from the cached MetaTensors.
                This is useful for skipping the transform instance checks when inverting applied operations
                using the cached content and with re-created transform instances.
            track_meta: whether to track the meta information, if `True`, will convert to `MetaTensor`.
                default to `False`. Cannot be used with `weights_only=True`.
            weights_only: keyword argument passed to `torch.load` when reading cached files.
                default to `True`. When set to `True`, `torch.load` restricts loading to tensors and
                other safe objects. Setting this to `False` is required for loading `MetaTensor`
                objects saved with `track_meta=True`, however this creates the possibility of remote
                code execution through `torch.load` so be aware of the security implications of doing so.

        Raises:
            ValueError: When both `track_meta=True` and `weights_only=True`, since this combination
                prevents cached MetaTensors from being reloaded and causes perpetual cache regeneration.
        """
        super().__init__(data=data, transform=transform)
        self.cache_dir = Path(cache_dir) if cache_dir is not None else None
        self.hash_func = hash_func
        self.pickle_module = pickle_module
        self.pickle_protocol = pickle_protocol
        if self.cache_dir is not None:
            if not self.cache_dir.exists():
                self.cache_dir.mkdir(parents=True, exist_ok=True)
            if not self.cache_dir.is_dir():
                raise ValueError("cache_dir must be a directory.")
        self.transform_hash: str = ""
        if hash_transform is not None:
            self.set_transform_hash(hash_transform)
        self.reset_ops_id = reset_ops_id
        if track_meta and weights_only:
            raise ValueError(
                "Invalid argument combination: `track_meta=True` cannot be used with `weights_only=True`. "
                "To cache and reload MetaTensors, set `track_meta=True` and `weights_only=False`."
            )