Commit faaf9f4
Skip ComfyUI safetensors post-processing unless opted in (fix sharded FLUX export) (#1794)
### What does this PR do?
**Type of change:** Bug fix
Fixes diffusers HuggingFace export (`export_hf_checkpoint`) failing with
`NotImplementedError: Post-processing sharded safetensors is not
supported` for large quantized diffusion models (e.g. the FP8 FLUX.1-dev
transformer, ~12 GB, which exceeds the default 10 GB `max_shard_size`
and is split into multiple `.safetensors` shards).
**Root cause:** `_postprocess_safetensors` runs for every quantized
diffusers component. It exists only to build single-file deployment
checkpoints (e.g. ComfyUI) — merging with a base checkpoint, NVFP4
padding/swizzling, and embedding quant metadata in the safetensors
header — none of which ModelOpt reads back (the diffusers reload uses
`config.json`). But it embedded the header metadata **by default**
(`enable_layerwise_quant_metadata=True`), and that default-on path
hard-raised for any sharded checkpoint, so a plain `--format fp8` FLUX
export failed at export time. (Introduced in #1195 / #911; not a
transformers/diffusers API change.)
**Fix — make the post-processing opt-in:** `_postprocess_safetensors`
now returns immediately (no-op) unless the caller opts into one of
`merged_base_safetensor_path` / `padding_strategy` /
`enable_swizzle_layout` / `enable_layerwise_quant_metadata` (the last
default flipped `True → False`). A plain quantized diffusers export —
including a sharded one — is left untouched and no longer fails;
`config.json` still carries `quantization_config` for the
diffusers-native reload. Behavior for callers that opt in is unchanged
(sharded + merge/metadata remains unsupported, which is out of scope for
this fix).
### Usage
```bash
# Default export — now succeeds (no ComfyUI post-processing):
python examples/diffusers/quantization/quantize.py --model flux-dev --format fp8 \
--quantized-torch-ckpt-save-path flux-dev-fp8.pt --hf-ckpt-dir flux-dev-fp8 \
--collect-method default --calib-size 128 --quantize-mha \
--model-dtype BFloat16 --trt-high-precision-dtype BFloat16
# Opt into the ComfyUI single-file header metadata:
# ... --extra-param enable_layerwise_quant_metadata=true
```
### Testing
- CPU unit tests (`tests/unit/torch/export/test_export_diffusers.py`):
- `test_postprocess_default_is_noop` — default (incl. a sharded
checkpoint) injects nothing and does not raise.
- `test_postprocess_single_file_metadata_when_opted_in` — opt-in
single-file injection still works.
- `test_postprocess_sharded_opt_in_raises` — an explicit opt-in on a
sharded checkpoint still raises (documents the existing, out-of-scope
limitation).
- GPU test
(`tests/gpu/torch/export/test_export_diffusers.py::test_export_diffusers_sharded_default_no_header_metadata`)
— real FP8 tiny-FLUX, forced sharding, default → succeeds with a clean
header.
- Verified on a GB200 in the dev container: 10/10 unit + 11/11 GPU
diffusers export tests pass; the regression test fails on the original
code (`NotImplementedError`) and passes with the fix. `ruff check` /
`ruff format` clean.
### Before your PR is "*Ready for review*"
- Is this change backward compatible?: ✅ for the diffusers-native reload
(quant config still in `config.json`). 1 parent bb53066 commit faaf9f4
3 files changed
Lines changed: 172 additions & 7 deletions
File tree
- modelopt/torch/export
- tests
- gpu/torch/export
- unit/torch/export
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
171 | 177 | | |
172 | 178 | | |
173 | 179 | | |
| |||
181 | 187 | | |
182 | 188 | | |
183 | 189 | | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
189 | 195 | | |
190 | 196 | | |
191 | 197 | | |
| |||
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
201 | | - | |
| 207 | + | |
202 | 208 | | |
203 | 209 | | |
204 | 210 | | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
205 | 224 | | |
206 | 225 | | |
207 | 226 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
31 | 39 | | |
32 | 40 | | |
33 | 41 | | |
| |||
78 | 86 | | |
79 | 87 | | |
80 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
32 | | - | |
| 35 | + | |
33 | 36 | | |
34 | 37 | | |
35 | 38 | | |
36 | 39 | | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
40 | 69 | | |
41 | 70 | | |
42 | 71 | | |
| |||
117 | 146 | | |
118 | 147 | | |
119 | 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 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
0 commit comments