Environment Variables
Purpose and Scope
Environment variables are process-level settings read by a Python run before or during library initialization. In Transformers, they are most useful when a behavior should apply to an entire script, notebook, service, or distributed job rather than to a single function call. The environment-variable reference in this source set focuses on model-weight loading performance: it documents how to enable parallel loading for Torch and Safetensors checkpoint files and how to tune the number of worker threads. Sources: docs/source/en/reference/environment_variables.md
The practical reason to use these settings is reproducibility. A team may run the same model-loading code in a notebook, a command-line script, a container, or an Accelerate-launched training job. Setting the environment before the program starts makes the intended runtime behavior visible at the boundary of the process. That differs from ordinary API arguments, which are chosen at the call site and usually affect one model, tokenizer, pipeline, or trainer instance. Sources: docs/source/en/reference/environment_variables.md, docs/source/ar/accelerate.md
Timing matters. If a setting affects model initialization, put it before importing high-level helpers or before calling the code that constructs the model. The repository reference examples set the variables first, then import the pipeline helper, then instantiate a text-generation pipeline with automatic device mapping. That ordering is a useful convention for scripts, notebooks, and launchers because it avoids ambiguity about whether some import or object construction has already observed the old process environment. Sources: docs/source/en/reference/environment_variables.md
Relevant Source Files
- docs/source/en/reference/environment_variables.md - English reference documentation for the Transformers-specific parallel model-loading environment variables, including defaults, accepted values, examples, and profiling guidance.
- docs/source/_config.py - Shared documentation configuration that injects a first notebook cell installing transformers, datasets, evaluate, and accelerate for generated documentation examples.
- docs/source/ar/_config.py - Arabic documentation configuration that mirrors the shared notebook installation cell and placeholder formatting rules.
- docs/source/ar/_toctree.yml - Arabic documentation navigation showing how getting started, tutorials, preprocessing, training, Accelerate, PEFT, model sharing, generation, and chat pages are organized.
- docs/source/ar/accelerate.md - Localized Accelerate tutorial that demonstrates distributed-training setup, accelerator preparation, backward replacement, script launching, and notebook launching.
Core Runtime Variables
HF_ENABLE_PARALLEL_LOADING is the main documented switch. By default, the option is disabled. When set to the string true, Transformers can load Torch and Safetensors weight files in parallel during model initialization. The reference presents it as a startup-time optimization for large, multi-shard models, not as a universal acceleration flag. It cites a large OPT checkpoint on an AWS instance where loading can drop from roughly fifty-five seconds to roughly thirty seconds, while also warning that smaller models may not speed up. Sources: docs/source/en/reference/environment_variables.md
HF_PARALLEL_LOADING_WORKERS controls the number of worker threads used after parallel loading is enabled. Its documented default is eight, but the effective count is bounded by the number of files being loaded. If a checkpoint has only two files and the setting asks for eight workers, only two workers are spawned. That detail is important when interpreting benchmarks: the setting gives the loader permission to use more concurrency, but it cannot create file-level parallelism that the checkpoint layout does not provide. Sources: docs/source/en/reference/environment_variables.md
import os
os.environ["HF_ENABLE_PARALLEL_LOADING"] = "true"
os.environ["HF_PARALLEL_LOADING_WORKERS"] = "4"
from transformers import pipeline
model = pipeline(task="text-generation", model="facebook/opt-30b", device_map="auto")A good tuning workflow begins with the default worker count, records cold-start loading time, and changes only one variable at a time. Increasing workers can help when the model is sharded, the storage system can sustain concurrent reads, and deserialization is not blocked elsewhere. It can also be neutral if startup is dominated by device placement, memory pressure, or a small number of weight files. Treat the setting as an experiment to validate on the exact checkpoint format, host type, filesystem, and deployment launcher you plan to use. Sources: docs/source/en/reference/environment_variables.md
Logging, Noise Control, and Diagnostics
Transformers also exposes logging controls in the public documentation. TRANSFORMERS_VERBOSITY overrides the default library logging level, which is warning in the official logging guide, and accepts levels such as debug, info, warning, error, critical, and fatal. TRANSFORMERS_NO_ADVISORY_WARNINGS suppresses advisory warnings when set to a true value. These variables do not change model parameters, preprocessing, generation, or training math. They change what the process reports while the same workload is running, which makes them operational controls rather than modeling controls.
Use verbosity deliberately during debugging. A failing install, unexpected checkpoint load, surprising device placement, or distributed launch issue is usually easier to diagnose with more output at first. After the behavior is understood, reducing the level can keep production logs readable. Advisory-warning suppression should come after review, not before it, because advisory messages often explain recommended usage or upcoming changes. In other words, quiet logs are useful once a run is stable, but they should not hide the signals needed to make that run stable.
Documentation and Launch Context
The documentation build configuration shows a related runtime pattern: generated notebooks begin with an installation cell that installs transformers together with datasets, evaluate, and accelerate, with an alternate commented command for installing Transformers from the GitHub repository. That installation cell is not an environment variable, but it explains the assumed package baseline for many examples. Environment variables should be interpreted in that same setup phase: first establish the environment and dependencies, then run the tutorial, training loop, or inference code. Sources: docs/source/_config.py, docs/source/ar/_config.py
The Arabic documentation tree places installation, quick tour, pipeline inference, AutoClass usage, preprocessing, training, script-based training, Accelerate, PEFT, model sharing, LLM generation, and chat guidance in a coherent learning path. That structure is useful when deciding where environment variables belong in a workflow. They are not a replacement for learning model APIs or training abstractions; they are launch-time controls that sit beside installation, notebooks, training scripts, and distributed execution. Sources: docs/source/ar/_toctree.yml
Accelerate makes this launch-time perspective concrete. The tutorial installs accelerate, creates an Accelerator, prepares dataloaders, the model, and the optimizer, replaces ordinary backward propagation with the accelerator backward call, and then runs code through either a script launcher or notebook launcher. Environment variables should be set before those launched processes import Transformers and construct model objects. In distributed jobs, each worker is a process with its own environment, so the safest mental model is that every worker must receive the same settings before model initialization begins. Sources: docs/source/ar/accelerate.md
Cache, Offline, and Runtime Discipline
Cache and offline behavior require the same discipline as parallel loading even when the variable names are managed elsewhere in a deployment. Decide before launch where model artifacts, tokenizer files, processor assets, and downloaded resources will live. On shared clusters, ephemeral containers, and notebook runtimes, this decision affects whether a later load can find the files it needs. For offline execution, run a small smoke test that loads the same model, tokenizer, or processor from the intended local location before starting a long training or inference job.
For production services, keep environment choices close to the launcher rather than scattered across application code. A shell script, container specification, job template, or service manager can document the intended parallel-loading behavior, worker count, logging level, and cache preparation in one place. That makes rollbacks simpler: change the launch environment and restart, rather than editing model-loading code. For notebooks, put the environment setup in the first executable cell, matching the repository’s notebook-first-cell convention for installation setup. Sources: docs/source/_config.py, docs/source/ar/_config.py
Compact Reference
| Variable | Purpose | Values or default | Best fit |
|---|---|---|---|
| HF_ENABLE_PARALLEL_LOADING | Enables parallel loading of Torch and Safetensors weight files during model initialization. | String true or false; disabled by default. | Large, multi-shard checkpoints where startup time is a real bottleneck. |
| HF_PARALLEL_LOADING_WORKERS | Sets the maximum worker-thread count used by parallel loading. | Default is 8; effective count is capped by the number of files. | Tuning after parallel loading is enabled and benchmarked. |
| TRANSFORMERS_VERBOSITY | Overrides the library logging verbosity from the process environment. | debug, info, warning, error, critical, or fatal. | Debugging or reducing runtime log volume. |
| TRANSFORMERS_NO_ADVISORY_WARNINGS | Suppresses advisory warnings emitted through advisory logging helpers. | True-like values such as 1. | Quieting known advisory messages after they have been reviewed. |
Execution Flow
A typical inference startup flow begins by installing the expected packages, setting environment variables, importing Transformers, and loading the model or pipeline. If the checkpoint is large and sharded, enable parallel loading and measure the cold-start time. If logs are too noisy or too quiet, adjust the logging environment for the next run. If the workload is launched through Accelerate or another process manager, move the settings into the launcher so child processes inherit them consistently. Sources: docs/source/en/reference/environment_variables.md, docs/source/ar/accelerate.md
For training, apply the same sequence but be more careful about multiple processes. Configure the environment before accelerate launch, then let the training script create its Accelerator, prepare objects, and enter the loop. Avoid setting worker-specific values inside only one branch of the script unless that difference is intentional and documented. For notebooks, restart the runtime after changing process-level settings if there is any doubt about whether imports or model construction have already happened.
Next Steps
Read this page with the installation, quickstart, Auto Classes and Model Loading, Accelerate Integration, Distributed and Parallel Training, Inference Optimization, and Troubleshooting pages. For model startup work, combine parallel loading with checkpoint-format, device-map, and quantization decisions. For training work, combine launch-time settings with the Accelerate launcher and distributed configuration. For debugging, first increase visibility, capture the exact launch command and package state, and only then reduce verbosity or suppress advisory messages once the behavior is understood.