Repository navigation
Open
Labels
component: api [Python]Issues re: Python APIcomponent: dynamoIssues relating to the `torch.compile` or `torch._dynamo.export` pathsfeature requestNew feature or request
Milestone
Description
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; withsource=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 onmeta, maps its weights, and gives thecompilesettings that lower memory aslow_mem.compile_settings.release_weights_after_build: Optional[bool] = None(feat(dynamo): release model weights once their last engine is built #4853):Nonedrops a mapped weight's pages once its last engine is built,Truealso frees ordinary weights,Falsereleases nothing.
2.16 work
- One recipe instead of the boilerplate. Today a user enters
low_memory, exportslow_mem.model, and passes**low_mem.compile_settingstocompile(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_mappingconverted from a checkpoint in other dtypes. For the second case,low_memorycould 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_DTYPESmaps the dtype names a safetensors header stores ("F32","BF16","F8_E4M3","U16", ...) to torch dtypes, and_SAFETENSORS_DTYPE_NAMESmaps them back.torch_tensorrt.dtypeconverts 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, thesafetensorspackage's own mapping, would makesafetensorsa dependency ofmap_weights; today onlycheckpoint_sourceneeds it. - The per-engine size and engines on disk in the recipe.
low_memoryleft 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
RuntimeWarningsays so). torch.compile: nothing is released, since parameters arrive as graph inputs;release_weights_after_build=Trueis turned off with a warning.MutableTorchTensorRTModule: usesrelease_weights_after_build=False, since it refits and recompiles from the source module's weights.- No value checks:
map_weightschecks 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
Labels
component: api [Python]Issues re: Python APIcomponent: dynamoIssues relating to the `torch.compile` or `torch._dynamo.export` pathsfeature requestNew feature or request
Activity
feat(dynamo): single-block low_memory with map_weights, meta paramete…
feat(dynamo): single-block low_memory with map_weights, meta paramete…
feat(dynamo): single-block low_memory with map_weights, model_weights…
feat(dynamo): single-block low_memory with map_weights, model_weights…
feat(dynamo): single-block low_memory with map_weights, model_weights…
feat(dynamo): single-block low_memory with map_weights, model_weights…
feat(dynamo): single-block low_memory with map_weights, model_weights…