乐于分享
好东西不私藏

TensorRT-LLM 0.5.0 源码之十六

TensorRT-LLM 0.5.0 源码之十六

ammo.py

try:    import ammo.torch.quantization as atq    from ammo.torch.export import export_model_configexcept ImportError:    raise ImportError("AMMO toolkit is not installed. Please install it first.")
def _quantize_model(model: torch.nn.Module,                    qformat: Literal['fp8', 'int8_sq', 'int4_awq'],                    calib_dataloader: DataLoader,                    quant_cfg_dict: Optional[Dict] = None) -> torch.nn.Module:    assert qformat in ['fp8', 'int8_sq', 'int4_awq'], \        f'Got unsupported AMMO quantization format, {qformat} '    if qformat == "fp8":        quant_cfg = atq.FP8_DEFAULT_CFG        if quant_cfg_dict:            for name, cfg in quant_cfg_dict.items():                quant_cfg['quant_cfg'][name] = cfg    elif qformat == "int8_sq":        quant_cfg = atq.INT8_SMOOTHQUANT_CFG    elif qformat == "int4_awq":        quant_cfg = atq.INT4_AWQ_CFG    else:        raise ValueError(f"Unsupported quantization format: {qformat}")    def calibrate_loop():        """Adjusts weights and scaling factors based on selected algorithms."""        for idx, data in enumerate(calib_dataloader):            logger.debug(f"Calibrating batch {idx}")            model(data)    logger.debug("Starting quantization...")    atq.quantize(model, quant_cfg, forward_loop=calibrate_loop)    logger.debug("Quantization done")    return model
def quantize_and_export(model: torch.nn.Module,                        qformat: Literal['fp8', 'int8_sq', 'int4_awq'],                        calib_dataloader: DataLoader,                        export_path: Optional[Union[str, Path]] = None,                        tensor_parallel_size: int = 1) -> torch.nn.Module:    model_cls_name = type(model).__name__    if "Llama" in model_cls_name:        model_type = "llama"    elif "GPTJ" in model_cls_name:        model_type = "gptj"    elif "GPT2" in model_cls_name:        model_type = "gpt2"    elif "Falcon" in model_cls_name or "RW" in model_cls_name:        model_type = "falcon"    else:        raise NotImplementedError(            f"Deploying quantized model {model_cls_name} is not supported")    model = _quantize_model(model,                            qformat=qformat,                            calib_dataloader=calib_dataloader)    if export_path:        with torch.inference_mode():            if qformat == "int4_awq":                torch.save(model.state_dict(), export_path)            else:                export_model_config(                    model,                    model_type,                    torch.float16,                    quantization=qformat,                    export_dir=export_path,                    inference_tensor_parallel=tensor_parallel_size,                )        logger.info(f"Quantized model exported to :{export_path}")    return model

quant.py

# isort: offfrom ...quantization.layers import (    SmoothQuantAttention, SmoothQuantGatedMLP, SmoothQuantLayerNorm,    SmoothQuantMLP, SmoothQuantRmsNorm, WeightOnlyGroupwiseQuantColumnLinear,    WeightOnlyGroupwiseQuantRowLinear, WeightOnlyQuantColumnLinear,    WeightOnlyQuantRowLinear)# isort: on

smooth_quantize

def _smooth_quantize_llama(model, quant_mode):    assert quant_mode.has_act_and_weight_quant()    for layer in model.layers:        assert hasattr(layer,                       "input_layernorm"), "The layer has no input_layernorm"        layer.input_layernorm = SmoothQuantRmsNorm(            normalized_shape=layer.hidden_size,            dtype=layer.dtype,            quant_mode=quant_mode)        assert hasattr(layer, "attention"), "The layer has no attention"        layer.attention = SmoothQuantAttention(            layer.hidden_size,            num_attention_heads=layer.num_attention_heads,            num_kv_heads=layer.num_kv_heads,            max_position_embeddings=layer.max_position_embeddings,            num_layers=model.num_layers,            dtype=layer.dtype,            attention_mask_type=layer.attention_mask_type,            position_embedding_type=layer.position_embedding_type,            tp_group=layer.tp_group,            tp_size=layer.tp_size,            quant_mode=quant_mode,            bias=False)        assert hasattr(layer, "mlp"), "The layer has no mlp"        layer.mlp = SmoothQuantGatedMLP(hidden_size=model.hidden_size,                                        ffn_hidden_size=layer.mlp_hidden_size,                                        hidden_act=layer.hidden_act,                                        dtype=layer.dtype,                                        tp_group=layer.tp_group,                                        tp_size=layer.tp_size,                                        quant_mode=quant_mode,                                        bias=False)        assert hasattr(            layer,            "post_layernorm"), "The layer has no post_rmspost_layernormnorm"        layer.post_layernorm = SmoothQuantRmsNorm(            normalized_shape=layer.hidden_size,            dtype=layer.dtype,            quant_mode=quant_mode)    setattr(model, 'quant_mode', quant_mode)    return modeldef smooth_quantize(model, quant_mode):    assert isinstance(model, GPTLMHeadModel) or isinstance(model, LLaMAForCausalLM) \            or isinstance(model, BloomForCausalLM),\            "Only GPTLMHeadModel, LLaMAForCausalLM and BloomForCausalLM are well tested now"    if isinstance(model, LLaMAForCausalLM):        return _smooth_quantize_llama(model, quant_mode)    else:        assert False, f"Model {type(model).__name__} is not supported by SmoothQuant yet"

weight_only_quantize

def weight_only_quantize(model,                         quant_mode,                         exclude_modules=None,                         current_key_name=None):    assert quant_mode.is_weight_only()    exclude_modules = ['lm_head'                       ] if exclude_modules is None else exclude_modules    for name, module in model.named_children():        if current_key_name is None:            current_key_name = []        current_key_name.append(name)        if len(list(module.children())) > 0:            weight_only_quantize(module, quant_mode, exclude_modules,                                 current_key_name)        if isinstance(module, ColumnLinear) and name not in exclude_modules:            if not any(key in '.'.join(current_key_name)                       for key in exclude_modules):                model._modules[name] = WeightOnlyQuantColumnLinear(                    in_features=module.in_features,                    out_features=module.out_features * module.tp_size,                    bias=module.bias is not None,                    dtype=module.dtype,                    tp_group=module.tp_group,                    tp_size=module.tp_size,                    gather_output=module.gather_output,                    quant_mode=quant_mode)        elif isinstance(module, RowLinear) and name not in exclude_modules:            if not any(key in '.'.join(current_key_name)                       for key in exclude_modules):                model._modules[name] = WeightOnlyQuantRowLinear(                    in_features=module.in_features * module.tp_size,                    out_features=module.out_features,                    bias=module.bias is not None,                    dtype=module.dtype,                    tp_group=module.tp_group,                    tp_size=module.tp_size,                    quant_mode=quant_mode)        current_key_name.pop(-1)    setattr(model, 'quant_mode', quant_mode)    return model

weight_only_groupwise_quantize

def weight_only_groupwise_quantize(model,                                   quant_mode,                                   group_size=128,                                   pre_quant_scale=False,                                   zero=False,                                   exclude_modules=None,                                   current_key_name=None):    exclude_modules = ['lm_head'                       ] if exclude_modules is None else exclude_modules    for name, module in model.named_children():        if current_key_name is None:            current_key_name = []        current_key_name.append(name)        if len(list(module.children())) > 0:            weight_only_groupwise_quantize(module, quant_mode, group_size,                                           pre_quant_scale, zero,                                           exclude_modules, current_key_name)        if isinstance(module, ColumnLinear) and name not in exclude_modules:            if not any(key in '.'.join(current_key_name)                       for key in exclude_modules):                model._modules[name] = WeightOnlyGroupwiseQuantColumnLinear(                    in_features=module.in_features,                    out_features=module.out_features * module.tp_size,                    group_size=group_size,                    pre_quant_scale=pre_quant_scale,                    zero=zero,                    bias=module.bias is not None,                    dtype=module.dtype,                    tp_group=module.tp_group,                    tp_size=module.tp_size,                    gather_output=module.gather_output)        elif isinstance(module, RowLinear) and name not in exclude_modules:            if not any(key in '.'.join(current_key_name)                       for key in exclude_modules):                model._modules[name] = WeightOnlyGroupwiseQuantRowLinear(                    in_features=module.in_features * module.tp_size,                    out_features=module.out_features,                    group_size=group_size,                    pre_quant_scale=pre_quant_scale,                    zero=zero,                    bias=module.bias is not None,                    dtype=module.dtype,                    tp_group=module.tp_group,                    tp_size=module.tp_size)        current_key_name.pop(-1)    setattr(model, 'quant_mode', quant_mode)    return model

others

def get_dummy_quant_scales(num_layers):    return {        'lm_head_act': 0.99,        'lm_head_weights': 0.99,        'fc_act': [0.99 for _ in range(num_layers)],        'fc_weights': [0.99 for _ in range(num_layers)],        'gate_act': [0.99 for _ in range(num_layers)],        'gate_weights': [0.99 for _ in range(num_layers)],        'proj_act': [0.99 for _ in range(num_layers)],        'proj_weights': [0.99 for _ in range(num_layers)],        'qkv_act': [0.99 for _ in range(num_layers)],        'qkv_weights': [0.99 for _ in range(num_layers)],        'qkv_output': [5.0 for _ in range(num_layers)],        'dense_act': [0.99 for _ in range(num_layers)],        'dense_weights': [0.99 for _ in range(num_layers)],    }
def _quantize_layer(layer, layer_idx, quant_mode, quant_scales):    assert hasattr(layer, "mlp"), "The layer has no mlp"    fake_fp8_sf_dt = np.float32    assert isinstance(layer.mlp.fc, (FP8Linear, FP8RowLinear))    assert isinstance(layer.mlp.proj, (FP8Linear, FP8RowLinear))    layer.mlp.fc.activation_scaling_factor.value = np.array(        [quant_scales['fc_act'][layer_idx]], dtype=fake_fp8_sf_dt)    layer.mlp.fc.weights_scaling_factor.value = np.array(        [quant_scales['fc_weights'][layer_idx]], dtype=fake_fp8_sf_dt)    layer.mlp.proj.activation_scaling_factor.value = np.array(        [quant_scales['proj_act'][layer_idx]], dtype=fake_fp8_sf_dt)    layer.mlp.proj.weights_scaling_factor.value = np.array(        [quant_scales['proj_weights'][layer_idx]], dtype=fake_fp8_sf_dt)    if hasattr(layer.mlp, 'gate'):        assert isinstance(layer.mlp.gate, (FP8Linear, FP8RowLinear))        layer.mlp.gate.activation_scaling_factor.value = np.array(            [quant_scales['gate_act'][layer_idx]], dtype=fake_fp8_sf_dt)        layer.mlp.gate.weights_scaling_factor.value = np.array(            [quant_scales['gate_weights'][layer_idx]], dtype=fake_fp8_sf_dt)    assert hasattr(layer, "attention"), "The layer has no attention"    assert isinstance(layer.attention.qkv, (FP8Linear, FP8RowLinear))    assert isinstance(layer.attention.dense, (FP8Linear, FP8RowLinear))    layer.attention.qkv.activation_scaling_factor.value = np.array(        [quant_scales['qkv_act'][layer_idx]], dtype=fake_fp8_sf_dt)    layer.attention.qkv.weights_scaling_factor.value = np.array(        [quant_scales['qkv_weights'][layer_idx]], dtype=fake_fp8_sf_dt)    if quant_mode.has_fp8_kv_cache():        layer.attention.kv_orig_quant_scale.value = np.array(            [quant_scales['qkv_output'][layer_idx]], dtype=fake_fp8_sf_dt)        layer.attention.kv_quant_orig_scale.value = np.array(            [1.0 / quant_scales['qkv_output'][layer_idx]], dtype=fake_fp8_sf_dt)    layer.attention.dense.activation_scaling_factor.value = np.array(        [quant_scales['dense_act'][layer_idx]], dtype=fake_fp8_sf_dt)    layer.attention.dense.weights_scaling_factor.value = np.array(        [quant_scales['dense_weights'][layer_idx]], dtype=fake_fp8_sf_dt)    return layerdef _default_fp8_quantize(model: Union[GPTLMHeadModel, LLaMAForCausalLM,                                       GPTJForCausalLM],                          quant_mode: QuantMode,                          quant_scales: dict = None):    """    Quantize all linear layers (i.e., MLP, Attention QKV/Dense) and KV cache IO with dummy scales    This is used by benchmark script and therefore is intentionally decoupled from AMMO toolkit    """    if quant_scales is None:        num_layers = getattr(model, '_num_layers',                             getattr(model, 'num_layers', None))        assert num_layers is not None        quant_scales = get_dummy_quant_scales(num_layers)    assert model.quant_mode == quant_mode, "Quant setting not consistent with model init setting"    use_fp8_qdq = quant_mode.has_fp8_qdq()    assert use_fp8_qdq    for layer_idx, layer in enumerate(model.layers):        layer = _quantize_layer(layer, layer_idx, quant_mode, quant_scales)    # TODO: add lm_head    return modeldef fp8_quantize(model, quant_mode: QuantMode, quant_scales: dict = None):    if isinstance(            model,        (FalconForCausalLM, GPTJForCausalLM, GPTLMHeadModel, LLaMAForCausalLM)):        return _default_fp8_quantize(model, quant_mode, quant_scales)    raise NotImplementedError(        f"Model {model} is not implemented by fp8_quantize yet")

参考文献

  • • https://github.com/NVIDIA/TensorRT-LLM/blob/v0.5.0/tensorrt_llm/models/quantized/ammo.py
  • • https://github.com/NVIDIA/TensorRT-LLM/blob/v0.5.0/tensorrt_llm/models/quantized/quant.py
点个「赞」+「在看」❤️
让我们知道这份文字有温暖到你,也是我们持续创作的最大动力!
推荐
Lock-Free 队列实现原理
Share Memory 的 Bank Conflict
告别高成本!TensorRT-LLM实战:如何将LLM推理速度提升数倍
使用LoRA对LLM进行微调的实用技巧
强化学习小白必看:PTX Loss 到底是个啥?
GPT-5 Prompt Migration and Improvement Using the New Optimizer
Task 异步流 coroutine 实现
C++ corotine 介绍
搭建 VSCode 离线开发环境
nlohmann/json 库简介
Intro to C++ Coroutines: Concept
Hugging Face BPE Tokenizer 的资源文件
移动语义 std::move 和完美转发 std::forward
ACEBench: Who Wins the Match Point in Tool Usage?
什么是 GN
RULER: Relative Universal LLM-Elicited Rewards
SFT和RFT的区别
CosyVoice 3: 面向真实场景的大规模零样本语音生成模型
CosyVoice 3: Towards In-the-wild Speech Generation
语音合成(TTS)中文自然度:问题、成因、解决方案
上下文工程如何实现
上下文工程(Context Engineering)
新手必看!LangGraph 101:手把手教你搭一个深度研究 Agent
LangGraph 简介
SFT 泛化新解读:强化学习 + 奖励修正,一文读懂
程序员狂喜!Self-Instruct 框架全解析:无限生成高质量指令集,从此告别标注噩梦!
Evol-Instruct 竟能精准生成领域专属数据?实操技巧速看!
指令微调数据-少即是多
LLM generate 参数怎么用?
语音合成(TTS)跳跃与重复问题的解析:成因、机制及解决方案
大模型训练新思路:GEPA 靠 “反思” 赢过 RL,看完秒懂
F5-TTS:用 Flow Matching 玩转语音,流畅度和真实感都 “拉满” 了
E2 TTS:令人尴尬地简单、完全非自回归、零样本的语音合成技术
Voicebox:大规模文本引导的多语言通用语音生成技术
为什么都在聊 Kimi K2?Open Agentic Intelligence 藏着哪些新惊喜
Step-Audio-AQAA 端到端音频模型
DPO、PPO、GRPO的原理,区别与联系
OPENCSG 中文语料库:一系列高质量的中文数据集,用于语言模型训练
什么是 Classifier-Free Guidance?
Conditional Flow Matching : 连续标准流 Continuous Normalizing Flow
CFM 与 OT-CFM:条件流匹配与最优传输的碰撞
DPO损失实现
Conditional Flow Matching : 常微分方程ODE、欧拉方法和Neural ODE
当 Normalizing flow 遇上语音生成:AI 说话变 “真人” 的秘密在这里!
深度剖析:Kimi - Audio 中 BigVGAN 的神奇作用
为什么说分布变换是 Normalizing flow 的「灵魂操作」?
MATCHA-TTS 来了!条件流匹配让文本转语音效率飙升
从知识增长的角度提升RAG上下文的质量
MiniMax-Speech,零样本语音合成新突破,32 种语言轻松拿捏!
手把手教你创建 evol-instruct 数据集!附完整流程~
社交类聊天的 Query 分析与应答策略
SFT 中指令选择和响应选择哪个更重要?
角色扮演大模型技术分享2-超拟人模型的困境
最新!SpeechLLM 综述:架构、能力、挑战与未来全揭秘
如何低成本生成高质量指令微调数据?
从数量到质量:通过自引导数据选择来提升语言模型性能以实现指令调优
Kimi-Audio:开源音频基础模型全面解析
Kimi-Audio 的 TTS 效果如何?
Qwen 的训练数据是怎么做的?
GeForce RTX 3090, 4090, A10, A40, A100, A800, L20, L40 显卡性能对比
如何低成本生成高质量指令微调数据?
掌握RAG:投入生产前要评估的8个场景
掌握RAG:如何评估RAG的LLM
掌握RAG:如何在部署后观察您的RAG
掌握RAG:如何选择嵌入模型
基础模型中的新范式:为什么o1是不同的,以及它将如何改变LLM应用
Semantic token和连续特征在SLLM下的对比
从数量到质量:通过自引导数据选择来提升语言模型性能以实现指令调优
RLHF及其变体:进展和实际工程见解
Freeze-Omni: 低延迟语音对话模型
Fully Sharded Data Parallelism (FSDP)
什么是置信度?置信度模型怎么做?
晦涩难懂的 Flow matching!图形化理解
中文指令微调数据,质量就是一切!
基于 LLM 的文本泛化
CosyVoice 2:基于大型语言模型的可扩展流式语音合成技术
Mini-Omni2: with Vision, Speech and Duplex Capabilities
FSQ的原理与VQ-VAE的区别和联系
大模型并行训练的一些知识——极简版
亲测有效!如何用 Address Sanitizer 精准定位内存漏洞?附保姆级操作指南
要用 AI 裁员 50% 的千亿独角兽,公开认错,重启招聘!
single codebook和dual codebook在LLM中向量量化上有什么区别?
一些文档去重算法
最佳的指令数据应当是什么样的?
Prefill-Decode分离
亲测有效!如何用 Address Sanitizer 精准定位内存漏洞?附保姆级操作指南
Simhash-文档去重算法简介
RLHF 入门,高手勿进!
最佳的指令数据应当是什么样的?
CosyVoice:一种基于监督式语义标记的可扩展多语言 Zero-Shot 语音合成器
Model Context Protocol (MCP)
MCP(模型上下文协议)是什么以及它是如何运作的
压力测试LLMs——大海捞针实现