{"record":{"id":"6b138a30850a0329","repo":"huggingface/transformers","slug":"worker-process-information-is-not-available-for-se","errorCode":null,"errorMessage":"Worker process information is not available for seeding the generator. This may be because you are using multiprocessing without using a PyTorch DataLoader. The `seed` parameter can only be used when using multiprocessing with a PyTorch DataLoader. Please either use a single process or use a PyTorch DataLoader with multiple workers.","messagePattern":"Worker process information is not available for seeding the generator\\. This may be because you are using multiprocessing without using a PyTorch DataLoader\\. The `seed` parameter can only be used when using multiprocessing with a PyTorch DataLoader\\. Please either use a single process or use a PyTorch DataLoader with multiple workers\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/data/data_collator.py","lineNumber":761,"sourceCode":"        if mp.current_process().name == \"MainProcess\":\n            # If we are in the main process, we create a generator object with the seed\n            self.generator = self.get_generator(self.seed)\n        else:\n            # If we are in a worker process (i.e using multiprocessing), we need to set a unique seed for each\n            # worker's generator, generated as the main seed + the worker's ID.\n            # (https://pytorch.org/docs/stable/data.html#randomness-in-multi-process-data-loading)\n            # Only PyTorch DataLoader allows us to access the worker ID, and so we check for this.\n            import torch\n\n            worker_info = torch.utils.data.get_worker_info()\n            if worker_info is None:\n                error_string = (\n                    \"Worker process information is not available for seeding the generator. This may be because\",\n                    \"you are using multiprocessing without using a PyTorch DataLoader. The `seed` parameter can\",\n                    \"only be used when using multiprocessing with a PyTorch DataLoader. Please either use a\",\n                    \"single process or use a PyTorch DataLoader with multiple workers.\",\n                )\n                raise ValueError(error_string)\n\n            self.generator = self.get_generator(self.seed + worker_info.id)\n\n    def torch_call(self, examples: list[list[int] | Any | dict[str, Any]]) -> dict[str, Any]:\n        # Handle dict or lists with proper padding and conversion to tensor.\n\n        if self.seed and self.generator is None:\n            # If we have a seed, we need to create a generator object. Subsequent calls to this function will use the same generator.\n            # If no seed supplied, we will use the global RNG\n            self.create_rng()\n\n        if isinstance(examples[0], Mapping):\n            batch = pad_without_fast_tokenizer_warning(\n                self.tokenizer, examples, return_tensors=\"pt\", pad_to_multiple_of=self.pad_to_multiple_of\n            )\n        else:\n            batch = {\n                \"input_ids\": _torch_collate_batch(examples, self.tokenizer, pad_to_multiple_of=self.pad_to_multiple_of)","sourceCodeStart":743,"sourceCodeEnd":779,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/data/data_collator.py#L743-L779","documentation":"Raised by DataCollatorForLanguageModeling.create_rng when a seed was supplied but torch.utils.data.get_worker_info() returns None at call time. The per-worker deterministic seeding scheme (seed + worker_id) only works inside PyTorch DataLoader worker processes; outside them there is no worker id to derive a distinct stream, so the collator raises instead of silently producing collisions.","triggerScenarios":"Passing seed=... to DataCollatorForLanguageModeling and then calling the collator (directly or via a plain multiprocessing pool, a HF Trainer without a PyTorch DataLoader worker context, or a manual training loop) where get_worker_info() is None.","commonSituations":"Using the collator with num_workers=0 in a DataLoader (main-process collation), a custom multiprocessing.DataLoader replacement, or unit tests that call collator(batch) directly; upgrading transformers versions where seed support was newly added.","solutions":["Serve the collator through a PyTorch DataLoader with num_workers > 0 so worker info exists.","If single-process training is intended, drop the seed argument and seed the global RNG yourself (torch.manual_seed / np.random.seed).","If you need a seeded generator without DataLoader workers, subclass the collator and override create_rng to build a generator from self.seed alone."],"exampleFix":"# before\ncollator = DataCollatorForLanguageModeling(tokenizer=tok, mlm=True, seed=42)\nbatch = collator(samples)  # raises: no worker info in main process\n\n# after (single process: use global RNG seeding)\ntorch.manual_seed(42)\ncollator = DataCollatorForLanguageModeling(tokenizer=tok, mlm=True)\nbatch = collator(samples)\n\n# after (deterministic per-worker seeding)\nloader = DataLoader(ds, batch_size=8, num_workers=4, collate_fn=collator)","handlingStrategy":"validation","validationCode":"import torch\n\ndef make_collator(tokenizer, seed, use_loader_workers):\n    if seed is not None and not use_loader_workers:\n        # no worker info will exist; seed globally instead\n        torch.manual_seed(seed)\n        return DataCollatorForLanguageModeling(tokenizer=tokenizer)\n    return DataCollatorForLanguageModeling(tokenizer=tokenizer, seed=seed)","typeGuard":null,"tryCatchPattern":"try:\n    batch = collator(features)\nexcept ValueError as e:\n    if 'Worker process information' in str(e):\n        # fall back to global RNG / single-process determinism\n        torch.manual_seed(42)\n        batch = collator_unseeded(features)\n    else:\n        raise","preventionTips":["Only pass seed when the collator is used as a DataLoader collate_fn with num_workers > 0.","In single-process training, rely on torch.manual_seed / np.random.seed for reproducibility.","Unit-test the collator without the seed argument to avoid depending on worker info."],"tags":["data-collator","dataloader","multiprocessing","randomness","seed"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}