llmcompressor.transformers.compression.compressed_tensors_utils
Functions:
-
modify_save_pretrained–Overrides a PreTrainedModel's save_pretrained() method with a wrapped version that
get_model_compressor
get_model_compressor(
model: Module,
sparsity_config: SparsityCompressionConfig
| None = None,
quantization_format: str | None = None,
save_compressed: bool = True,
skip_sparsity_compression_stats: bool = True,
disable_sparse_compression: bool = False,
)
Obtain the compressor based on the config and the quantization_format
Parameters:
-
model(Module) –torch model
-
sparsify_config–Sparsity Compression config
-
quantization_format(str | None, default:None) –Format that the model was quantized to. if not provivided, will be extrapolated from
infer_quantization_format -
save_compressed(bool, default:True) –boolean representing to save in a compressed format
-
skip_sparsity_compression_stats(bool, default:True) –bool allowing compression stats on std out
-
disable_sparse_compression(bool, default:False) –bool to skip sparse compression
Source code in src/llmcompressor/transformers/compression/compressed_tensors_utils.py
modify_save_pretrained
Overrides a PreTrainedModel's save_pretrained() method with a wrapped version that supports compression. The new save_pretrained function performs the following saving operations:
- Saves the model state, potentially in a compressed format
- Saves the recipe, appending any current recipes to existing recipe files
- Copies any necessary python files from the model cache
Source code in src/llmcompressor/transformers/compression/compressed_tensors_utils.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
resave_config
Overwrite the config saved at save_dir with the original model config,
patched with fields that llmcompressor has modified.
Transformers regenerates config.json on save, which can introduce
differences from the original model config (reordered/dropped/renamed
fields). vLLM always supports loading the original model config, but only
sometimes supports loading the transformers-serialized one. To stay
compatible, this loads the original config file referenced by
config._name_or_path, overwrites only the fields that llmcompressor has
changed (currently the expert-count fields in
:data:NUM_EXPERTS_CONFIG_KEYS, which change under REAP expert pruning),
and writes the result to save_dir/config.json.
Parameters:
-
config(PretrainedConfig) –the (possibly modified) model config, used both to locate the original config file and to source the patched field values
-
save_dir(str) –directory containing the
config.jsonto overwrite
Source code in src/llmcompressor/transformers/compression/compressed_tensors_utils.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
suspend_distributed_timeout
suspend_distributed_timeout(
timeout: timedelta = datetime.timedelta(hours=3),
current_group: ProcessGroup | None = None,
)
Context manager that extends the timeout for distributed operations.
Creates a temporary process group with an extended timeout to prevent timeout errors during long-running operations (e.g., model saving) in distributed training environments. The context manager synchronizes all processes before and after the operation using barriers.
Parameters:
-
timeout(timedelta, default:timedelta(hours=3)) –The extended timeout for the temporary process group. Defaults to 3 hours
-
current_group(ProcessGroup | None, default:None) –The current process group to synchronize. If None, defaults to dist.group.WORLD
Source code in src/llmcompressor/transformers/compression/compressed_tensors_utils.py
update_and_save_recipe
Save a recipe ontop of any existing recipe files located at model_stub
Parameters:
-
model_stub(str) –path to existing model or model stub which may contain an existing recipe
-
save_directory(str) –path to save combined existing recipe and current recipe