MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT-LLM / weight_only_quantize_dict

Function weight_only_quantize_dict

tensorrt_llm/models/convert_utils.py:129–153  ·  view source on GitHub ↗
(weights: Dict[str, torch.Tensor],
                              quant_algo: str,
                              quant_weights=[
                                  'qkv.weight', 'dense.weight', 'fc.weight',
                                  'proj.weight', 'gate.weight'
                              ],
                              exclude_modules=None,
                              plugin: bool = True)

Source from the content-addressed store, hash-verified

127
128
129def weight_only_quantize_dict(weights: Dict[str, torch.Tensor],
130 quant_algo: str,
131 quant_weights=[
132 'qkv.weight', 'dense.weight', 'fc.weight',
133 'proj.weight', 'gate.weight'
134 ],
135 exclude_modules=None,
136 plugin: bool = True):
137 if quant_algo not in [QuantAlgo.W4A16, QuantAlgo.W8A16]:
138 return weights
139 if exclude_modules is None:
140 exclude_modules = ['*shared_expert_gate.weight']
141 for name in list(weights):
142 is_excluded = False
143 for exclude_module in exclude_modules:
144 if fnmatch.fnmatchcase(name, exclude_module):
145 is_excluded = True
146 break
147 if not is_excluded and any([_name in name for _name in quant_weights
148 ]) and weights[name].dtype != torch.int8:
149 quant_weight, quant_scale = weight_only_quantize(
150 weight=weights[name], quant_algo=quant_algo, plugin=plugin)
151 weights[name] = quant_weight
152 weights[name.replace('.weight', '.per_channel_scale')] = quant_scale
153 return weights
154
155
156def load_state_dict(

Calls 2

replaceMethod · 0.80
weight_only_quantizeFunction · 0.70

Tested by

no test coverage detected