Uh oh!
There was an error while loading. Please reload this page.
[Quantization] Add TRT-ModelOpt as a Backend - #11173
Conversation
ishan-modi
commented
Apr 1, 2025
@sayakpaul, would you mind giving a quick look and suggestions |
sayakpaul
commented
Apr 8, 2025
Thanks for getting started on this. I guess there is a problem here: NVIDIA/Model-Optimizer#165? Additionally, the API should have a TRTConfig in place of just a dict being the quantization config. |
ishan-modi
commented
Apr 8, 2025
I think the problem has been fixed the newest release, I just need to bump it up in diffusers requirements, also we can do the following for passing Config class by TRTConfig did you mean including the config classes from ModelOptimizer here ? |
sayakpaul
commented
Apr 8, 2025
We use namings like So, in this case, we should be using |
sayakpaul
commented
Apr 8, 2025
Alright, let's try with the latest fixes then. |
ishan-modi
commented
Apr 8, 2025
The newer version wasn't backward compatible hence the issues, I have fixed it. Related to naming, package name is |
sayakpaul
commented
Apr 8, 2025
Doesn't it have any reliance on tensorrt? |
ishan-modi
commented
Apr 8, 2025
No it doesn't, we can use TRT to compile the quantized model |
sayakpaul
commented
Apr 9, 2025
Could you elaborate what you mean by this? |
sayakpaul
left a comment
There was a problem hiding this comment.
This looks nice. Could you demonstrate some memory savings and any speedups when using modelopt, please? We can then add tests, docs, etc.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ishan-modi
commented
Apr 10, 2025
Yeah, so for quantizing the model we dont use tensorRT, but once the model is quantized we can compile the model using tensorrt. |
💾 Model & Inference Memory (in MB)
Following is the codeimporttorchfromtqdmimporttqdmfromdiffusersimportSanaTransformer2DModel, SD3Transformer2DModel, FluxTransformer2DModelfromdiffusers.quantizers.quantization_configimportNVIDIAModelOptConfigcheckpoint="Efficient-Large-Model/Sana_600M_1024px_diffusers"model_cls=SanaTransformer2DModel# checkpoint = "stabilityai/stable-diffusion-3-medium-diffusers"# model_cls = SD3Transformer2DModel# checkpoint = "black-forest-labs/FLUX.1-dev"# model_cls = FluxTransformer2DModelinput=lambda: (torch.randn((2, 32, 32, 32), dtype=torch.bfloat16).to('cuda'), torch.randn((2,10,300,2304), dtype=torch.bfloat16).to('cuda'), torch.Tensor([0,0]).to('cuda'))
# input = lambda: (torch.randn((1,16,96,96), dtype=torch.bfloat16).to('cuda'), torch.randn((1,300,4096), dtype=torch.bfloat16).to('cuda'), torch.randn((1, 2048), dtype=torch.bfloat16).to('cuda'), torch.Tensor([0]).to('cuda'))# input = lambda: (torch.randn((1,1024, 64), dtype=torch.bfloat16).to('cuda'), torch.randn((1,300,4096), dtype=torch.bfloat16).to('cuda'), torch.randn((1, 768), dtype=torch.bfloat16).to('cuda'), torch.Tensor([0]).to('cuda'), torch.randn((300, 3)).to('cuda'), torch.randn((1024, 3)).to('cuda'), torch.Tensor([0]).to('cuda'))quant_config_fp8= {"quant_type": "FP8", "quant_method": "modelopt"}
quant_config_int4= {"quant_type": "INT4", "quant_method": "modelopt", "block_quantize": 128, "channel_quantize": -1}
quant_config_nvfp4= {"quant_type": "NVFP4", "quant_method": "modelopt", "block_quantize": 128, "channel_quantize": -1, 'modules_to_not_convert' : ['conv']}
deftest_quantization(config, checkpoint, model_cls):
quant_config=NVIDIAModelOptConfig(**config)
print(quant_config.get_config_from_quant_type())
quant_model=model_cls.from_pretrained(checkpoint, subfolder="transformer", quantization_config=quant_config, torch_dtype=torch.bfloat16, device_map="balanced").to('cuda')
print(f"Quant {config['quant_type']} Model Memory Footprint: ", quant_model.get_memory_footprint() /1e6)
returnquant_modeldeftest_quant_inference(model, input, iter=10):
torch.cuda.empty_cache()
torch.cuda.reset_max_memory_allocated()
inference_memory=0for_intqdm(range(iter)):
withtorch.no_grad():
output=model(*input())
inference_memory+=torch.cuda.max_memory_allocated()
inference_memory/=iterprint("Inference Memory: ", inference_memory/1e6)
test_quant_inference(test_quantization(quant_config_fp8, checkpoint, model_cls), input)
# test_quant_inference(test_quantization(quant_config_int4, checkpoint, model_cls), input)# test_quant_inference(test_quantization(quant_config_nvfp4, checkpoint, model_cls), input)# test_quant_inference(model_cls.from_pretrained(checkpoint, subfolder="transformer", torch_dtype=torch.bfloat16).to('cuda'), input)Speed UpsThere is no significant speedup between the different quantizations because internally modelopt still uses high precision arithmetic (float32). Sorry for being a bit late on this, @sayakpaul let me know next steps ! | |||||||||||||||||||||||||||||||||||||||||
sayakpaul
commented
Apr 25, 2025
@ishan-modi let us know if this is ready to be reviewed. |
@sayakpaul, I think it is ready for preliminary review, on-the-fly quantization works fine. But loading pre-quantized models errors out and will be fixed in next release here (early may) by NVIDIA team. @jingyu-ml, just so that you are in the loop |
There was a problem hiding this comment.
Looking good so far!
Could you also demonstrate some memory and timing numbers with the modelopt toolkit and some visual results?
No need, just saw #11173 (comment). But it doesn't measure the inference memory which is usually done via torch.cuda.max_memory_allocated(). Could we also see those numbers? Would it be possible to make it clear in the PR description that
on-the-fly quantization works fine. But loading pre-quantized models errors out and will be fixed in next release NVIDIA/Model-Optimizer#185 (early may) by NVIDIA team.
@jingyu-ml is it expected to not see any speedups in latency?
Uh oh!
There was an error while loading. Please reload this page.
SunMarc
left a comment
There was a problem hiding this comment.
Thanks for this Just some nits, it could be nice to add this quantization scheme to transformers after this gets merged !
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
sayakpaul
commented
Aug 27, 2025
@ishan-modi just a quick question. Do we know if the |
ishan-modi
commented
Aug 27, 2025
@sayakpaul, yes modelopt does support SVDQuant, but in this integration we support only |
sayakpaul
commented
Aug 27, 2025
That's fine. I wanted to because I think if we can support |
sayakpaul
commented
Aug 30, 2025
Will merge after @DN6 has had a chance to review. @ishan-modi can we also include a note in the docs that just performing the conversion step with @realAsma@jingyu-ml after this PR is merged, we could plan writing a post/guide on how to take a |
DN6
left a comment
There was a problem hiding this comment.
Excellent work @ishan-modi 👍🏽 Thank you 🙏🏽
sayakpaul
commented
Sep 1, 2025
@ishan-modi can we fix the remaining CI problems and then we should be good to go. |
ishan-modi
commented
Sep 1, 2025
@sayakpaul, should be fixed now. |
Uh oh!
There was an error while loading. Please reload this page.
sayakpaul
commented
Sep 3, 2025
Congratulations on shipping this thing, @ishan-modi! Thank you! Let's maybe now focus on the following things to maximize the potential impact:
Happy to help. |
sayakpaul
commented
Dec 23, 2025
@ishan-modi I hope things are well on your end. I think we never got around to showing how to deploy / use the TRT-serialized models through an engine to realize actual speedups? @kevalmorabia97 / @jingyu-ml do you think we could collaborate on that? |
What does this PR do?
WIP, aimed at adding new backend for quantization #11032. For now, this PR just works for on-the-fly quantization. Loading pre-quantized models errors out and it is to be fixed by NVIDIA team in next release early may
Depends on
this to support latest diffusersthis to enable INT8 quantizationthis to enable NF4 quantizationCode
Following is a discussion on speedups while using real_quant with NVIDIA team here