# Behavior traces — pytorch/pytorch

> Canonical page: https://codevetter.com/unpack/pytorch/behavior-traces

# Behavior traces — pytorch/pytorch

Scanned commit 5711f1a4586cb0a3dbb3d67538df814db48f94f4 (2026-09-22). Generated by codevetter codevetter 1.12.0 on 2026-09-22.

Traced flows: (1) import torch walks native-library preloading before any Python API exists; (2) torch.compile goes Python wrapper -> dynamo OptimizeContext -> C++ frame hook -> FX graph -> inductor -> aot_autograd -> Triton codegen -> guard-cached bytecode; (3) torch.save/load round-trips through a pickle-metadata + raw-storage zip format with the restricted unpickler on the read side; (4) backward() descends from torch.autograd into the C++ Engine GraphTask scheduler; (5) init_process_group does store rendezvous then backend construction; (6) the test runner shards test files and invokes pytest per module.
- import torch order: _rocm_init.initialize() if present -> (Windows) _load_dll_libraries / (POSIX) _load_global_deps ctypes.CDLL(libtorch_global_deps, RTLD_GLOBAL) + optional _preload_cuda_deps -> `from torch._C import *` -> _C._initExtension(torch_shm_manager path) -> copy _C._VariableFunctions into globals -> import subpackages (cpu/cuda/xpu/mps/mtia before multiprocessing) -> _C._init_names(storage classes) -> attach docstrings -> bind torch.ops/torch.classes namespaces. — [torch/__init__.py#L181-187](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/__init__.py#L181-L187) [torch/__init__.py#L402-505](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/__init__.py#L402-L505) [torch/__init__.py#L2606](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/__init__.py#L2606) [torch/__init__.py#L2715-2800](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/__init__.py#L2715-L2800)
- torch.compile call sequence: compile() logs API usage, rejects Python >=3.16 / free-threaded <3.13.3, resolves backend via get_default_backend, wraps inductor in _TorchCompileInductorWrapper, then torch._dynamo.optimize -> _optimize -> check_if_dynamo_supported -> (TORCHDYNAMO_DISABLE/justknobs check) -> optimize_assert if nopython else _optimize_catch_errors(convert_frame.convert_frame(backend,...)) -> OptimizeContext; per-call the C++ eval-frame hook runs guards then either executes cached compiled code or triggers compilation. — [torch/__init__.py#L3227-3338](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/__init__.py#L3227-L3338) [torch/_dynamo/eval_frame.py#L1786-1925](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/_dynamo/eval_frame.py#L1786-L1925) [torch/_dynamo/eval_frame.py#L1393-1456](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/_dynamo/eval_frame.py#L1393-L1456)
- Inside a dynamo compile: InstructionTranslator in symbolic_convert.py executes bytecode into VariableTrackers; OutputGraph owns the FX graph/guards; compile_subgraph calls the backend; PyCodegen emits replacement bytecode; resume_execution.py builds continuation functions for graph breaks; CheckFunctionManager builds a C++ GuardManager tree for fast recheck. — [torch/_dynamo/CLAUDE.md](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/_dynamo/CLAUDE.md)
- torch.save sequence: save() -> _open_zipfile_writer -> _save() -> PyTorchPickler(persistent_id) dumps obj to data.pkl -> write .format_version/.storage_alignment/byteorder records -> for each serialized storage, move to CPU (pinned-memory D2H if configured) -> zip_file.write_record('data/<key>', storage, num_bytes). — [torch/serialization.py#L945](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/serialization.py#L945) [torch/serialization.py#L1184-1313](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/serialization.py#L1184-L1313)
- torch.load sequence: torch._C._log_api_usage_once('torch.load') -> default weights_only=True -> _is_zipfile -> _open_zipfile_reader -> _load(): read byteorder/format_version/alignment -> Unpickler (weights-only restricted unpickler or full pickle module) resolves 'storage' persistent ids via load_tensor -> restore_location applies map_location -> storage rebuilt from data/<key> record (or lazily via mmap). — [torch/serialization.py#L1316-1445](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/serialization.py#L1316-L1445) [torch/serialization.py#L1999-2128](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/serialization.py#L1999-L2128)
- loss.backward() sequence: torch.autograd.backward -> functorch guard -> grad_variables deprecation shim -> __torch_function__ override check -> _tensor_or_tensors_to_tuple + _make_grads -> retain_graph defaults to create_graph -> _engine_run_backward (TLS context stash, optional logging hooks) -> Variable._execution_engine.run_backward -> C++ Engine::execute -> validate_outputs -> GraphTask scheduled on worker threads -> .grad accumulated on leaves (accumulate_grad=True). — [torch/autograd/__init__.py#L255-403](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/autograd/__init__.py#L255-L403) [torch/autograd/graph.py#L1100-1123](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/autograd/graph.py#L1100-L1123) [torch/csrc/autograd/engine.cpp#L1428-1460](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/csrc/autograd/engine.cpp#L1428-L1460)
- init_process_group sequence: reject double init (GroupMember.WORLD check) -> set_pytorch_distributed_envs_from_justknobs -> Backend(backend) validation/defaulting -> store resolution via explicit store or rendezvous(init_method, default env://) -> _new_process_group_helper builds the backend ProcessGroup (eager ncclCommInit* when device_id is passed for NCCL). — [torch/distributed/distributed_c10d.py#L2370-2626](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/distributed/distributed_c10d.py#L2370-L2626)
- Test execution: python test/run_test.py -> main() -> parse_args + TESTS_TO_INCLUDE env merge -> get_selected_tests (TEST groups like INDUCTOR_TESTS/DISTRIBUTED_TESTS/DYNAMO_CORE_TESTS) -> target-determination amend_tests -> do_sharding -> run_tests splits serial vs parallel files -> run_test_module invokes pytest per file with -m marker expressions; each test file uses run_tests() from common_utils which can spawn per-test-case subprocesses with a 15-min timeout and one retry. — [test/run_test.py#L432-457](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/test/run_test.py#L432-L457) [test/run_test.py#L2115-2214](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/test/run_test.py#L2115-L2214) [test/run_test.py#L2309-2388](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/test/run_test.py#L2309-L2388) [torch/testing/_internal/common_utils.py#L1473-1552](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/testing/_internal/common_utils.py#L1473-L1552)
- Codegen flow at build: cmake/Codegen.cmake runs `python -m torchgen.gen` -> main() parses native_functions.yaml + tags.yaml from aten/src/ATen -> get_custom_build_selector -> emits ATen headers/sources, dispatcher registrations, and the AOTInductor C shim into build/aten/src/ATen and torch/csrc/inductor/aoti_torch/generated. — [torchgen/gen.py#L2866-3025](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torchgen/gen.py#L2866-L3025) [CMakeLists.txt#L25-30](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/CMakeLists.txt#L25-L30)

Every claim links to its cited files at the pinned commit. Citations identify source evidence; they do not establish runtime correctness. Independent CodeVetter scan — not reviewed by upstream maintainers.

## Public product links

- [CodeVetter](https://codevetter.com/)
- [Download](https://codevetter.com/download)
- [Documentation](https://codevetter.com/docs/)
- [Source](https://github.com/Codevetter/codevetter)
