Skip to content

Low-memory compile API (map_weights, low_memory): 2.16 second pass and follow-ups #5001

Open

Description

@cehongwang

Summary

#4853 and #4854 let Torch-TensorRT compile a model from its weights file without loading the weights into memory first. In 2.15 the feature ships as experimental: the docs and the release notes say its API will change in 2.16, as asked in #4854's review. This issue collects the 2.16 work: what the reviewers asked for, what #4854 left out until other PRs landed, and the 2.15 limitations to keep or lift in the redesign.

Part of #4867.

The 2.15 API

  • torch_tensorrt.dynamo.tools.map_weights(module, path, *, strict=True, prefix="") points a module's parameters and buffers at zero-copy views of a safetensors file.
  • save_weights_for_mapping(module, path, *, source=None) writes such a file in the model's dtypes; with source=checkpoint_source(path, key=None, *, chunk_bytes=64 << 20) it converts a checkpoint without loading it.
  • torch_tensorrt.dynamo.low_memory(model_fn, weights, *, offload_module_to_cpu=None, enable_resource_partitioning=None, cpu_memory_budget=None, release_weights_after_build=None) builds the model on meta, maps its weights, and gives the compile settings that lower memory as low_mem.compile_settings.
  • release_weights_after_build: Optional[bool] = None (feat(dynamo): release model weights once their last engine is built #4853): None drops a mapped weight's pages once its last engine is built, True also frees ordinary weights, False releases nothing.

2.16 work

  • One recipe instead of the boilerplate. Today a user enters low_memory, exports low_mem.model, and passes **low_mem.compile_settings to compile (see the resource management guide). Fold these steps into a default recipe, as for the other weight-optimized APIs. Asked by Naren: simplify this boilerplate, fold it into a default recipe, a second pass after 2.15.
  • An optional weights path, defaulting to our cache directory (Naren). The path names the file to map: the model's own checkpoint when it is stored in the model's dtypes, or a file save_weights_for_mapping converted from a checkpoint in other dtypes. For the second case, low_memory could take the source checkpoint, convert it into _utils._cache_root() on first use, and map that file from then on. That needs a key that finds the file again (the checkpoint and the model's dtypes) and eviction, since the files are as large as the model's weights (6.7 GiB for pi0.5).
  • safetensors dtypes in torch_tensorrt.dtype (Naren). weight_mapping._SAFETENSORS_DTYPES maps the dtype names a safetensors header stores ("F32", "BF16", "F8_E4M3", "U16", ...) to torch dtypes, and _SAFETENSORS_DTYPE_NAMES maps them back. torch_tensorrt.dtype converts between torch, numpy and TensorRT types but doesn't know these names. Teach it to parse and produce them, and drop both tables. The alternative, the safetensors package's own mapping, would make safetensors a dependency of map_weights; today only checkpoint_source needs it.
  • The per-engine size and engines on disk in the recipe. low_memory left out two settings until their PRs landed: a fixed per-engine size for the resource partitioner (feat(dynamo): max_partition_size - a fixed per-engine size for the resource partitioner #4855, max_partition_size, open) and keeping built engines on disk until the save (feat: compile_and_save - compile and save with the engines kept on disk #4863, compile_and_save, merged). With mapping, they bound both the weights and the engines that compilation holds.
  • Re-measure feat(dynamo): release model weights once their last engine is built #4853's pi0.5 numbers. Its description's host + GPU peak (25.7 → 18.9 GiB with the setting) was measured before the review changes, which keep tensors the forward reads from outside the model and free each build's folded constants right after it.

Known limitations in 2.15

To keep or lift in the redesign:

  • Windows: a mapped weight's pages can't be dropped, so released weights stay resident until their file is unmapped (a RuntimeWarning says so).
  • torch.compile: nothing is released, since parameters arrive as graph inputs; release_weights_after_build=True is turned off with a warning.
  • MutableTorchTensorRTModule: uses release_weights_after_build=False, since it refits and recompiles from the source module's weights.
  • No value checks: map_weights checks the file's layout from its header (a truncated file, extra bytes, overlapping or gapped tensors, sizes that don't match a dtype and shape), not the bytes themselves, so a same-shaped wrong checkpoint maps without an error. On pi0.5's 13.47 GiB fp32 checkpoint (32 threads), a NaN/Inf scan took 6.4 s cold (3.8 s warm), crc32 5.6 s and sha256 10.3 s.

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: api [Python]component: dynamofeature request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions