Sub topic 2
How environments drift and change results
Assume you know basic Python and pip, can use a command line, have trained or run a model with PyTorch or scikit-learn, and know basic Git. This chapter explains why the same repository can produce different results on two machines.
An ML environment is everything around your source code that affects execution:
- The Python version and installed packages
- Native system libraries, such as BLAS for matrix operations
- The operating system and CPU instruction set
- GPU hardware, drivers, and CUDA libraries
- Environment variables and random-number settings
Environment drift happens when these parts become different over time or differ between machines. A dependency may be upgraded, a system library may change, or one machine may use a GPU kernel that another machine does not have.
A result can change without a code change
Suppose a training script installs scikit-learn without a version. On Monday, pip may install version 1.5. On Friday, a new release may be selected. The same function can then have different defaults, numerical behavior, or warnings.
The package itself can also depend on other packages. Installing scikit-learn creates a dependency tree, not just one package. If that tree is resolved at different times, two environments can contain different versions.
Hardware can change results too. Floating-point arithmetic has limited precision. CPUs and GPUs may perform operations in a different order. When many small values are added, changing the order can change the final rounding. During training, a small difference can grow into a different model.
Find differences before debugging the model
Record the environment with the result. At minimum, collect the Git commit, Python version, installed packages, operating system, hardware, and relevant configuration.
git rev-parse HEAD
python --version
python -m pip freeze
python -c "import platform; print(platform.platform())"Compare this record between a successful run and a failing run. If the package list differs, the problem may be environment drift rather than model logic. If the lists match but the hardware or driver differs, numerical or compatibility differences may remain.
A package list is useful evidence, but it is not a complete guarantee. It may omit system libraries, the exact package artifact, or the operating system layer. Later chapters use lock files and containers to cover more of these inputs.
Common debugging mistakes
- Comparing only the top-level packages instead of the full dependency tree
- Assuming a version tag identifies one immutable package artifact
- Ignoring Python, operating-system, or GPU-driver versions
- Treating one matching metric as proof that two environments are identical
- Changing several environment components at once, which hides the cause
Exercise
A model matches on your laptop but loses accuracy in CI. What should you compare first?
Show answer
Compare the Git commit, data and preprocessing, Python version, full dependency set, operating system, hardware, driver, configuration, and random-state settings.