乐于分享
好东西不私藏

TensorRT-LLM 0.5.0 源码之三十一

TensorRT-LLM 0.5.0 源码之三十一

quantization

class QuantMode
{
    // [WARNING] KEEP BELOW DEFINITION IN SYNC WITH tensorrt_llm/quantization/mode.py

public
:
    using
 BaseType = std::uint32_t;

explicit constexpr QuantMode(BaseType value) noexcept
        : mValue{
value}
    {
    }

    QuantMode
() noexcept = default;

constexpr QuantMode(QuantMode const&) noexcept
= default;

    constexpr
 QuantMode& operator=(const QuantMode& other) noexcept = default;

static constexpr QuantMode none() noexcept
{
        return
 QuantMode(BaseType(0));
    }

static constexpr QuantMode int4Weights() noexcept
{
        return
 QuantMode(BaseType(1u) << 0);
    }

static constexpr QuantMode int8Weights() noexcept
{
        return
 QuantMode(BaseType(1u) << 1);
    }

static constexpr QuantMode activations() noexcept
{
        return
 QuantMode(BaseType(1u) << 2);
    }

static constexpr QuantMode perChannelScaling() noexcept
{
        return
 QuantMode(BaseType(1u) << 3);
    }

static constexpr QuantMode perTokenScaling() noexcept
{
        return
 QuantMode(BaseType(1u) << 4);
    }

static constexpr QuantMode perGroupScaling() noexcept
{
        return
 QuantMode(BaseType(1u) << 5);
    }

static constexpr QuantMode int8KvCache() noexcept
{
        return
 QuantMode(BaseType(1u) << 6);
    }

static constexpr QuantMode fp8KvCache() noexcept
{
        return
 QuantMode(BaseType(1u) << 7);
    }

static constexpr QuantMode fp8Qdq() noexcept
{
        return
 QuantMode(BaseType(1u) << 8);
    }

     ////////////////////////////////////////////////////////////

constexpr BaseType value() const noexcept
{
        return
 mValue;
    }

constexpr bool isSet(QuantMode const& mode) const noexcept
{
        return
 (mValue & mode.value()) == mode.value();
    }

constexpr bool hasInt4Weights() const noexcept
{
        return
 isSet(int4Weights());
    }

constexpr bool hasInt8Weights() const noexcept
{
        return
 isSet(int8Weights());
    }

constexpr bool hasActivations() const noexcept
{
        return
 isSet(activations());
    }

constexpr bool hasPerChannelScaling() const noexcept
{
        return
 isSet(perChannelScaling());
    }

constexpr bool hasPerTokenScaling() const noexcept
{
        return
 isSet(perTokenScaling());
    }

constexpr bool hasPerGroupScaling() const noexcept
{
        return
 isSet(perGroupScaling());
    }

constexpr bool hasStaticActivationScaling() const noexcept
{
        return
 !hasPerTokenScaling();
    }

constexpr bool hasInt8KvCache() const noexcept
{
        return
 isSet(int8KvCache());
    }

constexpr bool hasFp8KvCache() const noexcept
{
        return
 isSet(fp8KvCache());
    }

constexpr bool hasFp8Qdq() const noexcept
{
        return
 isSet(fp8Qdq());
    }

constexpr bool hasKvCacheQuant() const noexcept
{
        return
 hasInt8KvCache() || hasFp8KvCache();
    }

      //////////////////////////////////////////////////////

static constexpr QuantMode fromDescription(bool quantizeWeights = false, bool quantizeActivations = false,
        bool
 perToken = false, bool perChannel = false, bool useInt4Weights = false, bool useInt8KvCache = false,
        bool
 useFp8KvCache = false, bool useFp8Qdq = false)

{
        QuantMode quantMode{};
        if
 (quantizeWeights)
        {
            if
 (useInt4Weights)
                quantMode += int4Weights();
            else

                quantMode += int8Weights();
        }

        if
 (quantizeActivations)
        {
            quantMode += activations();
        }

        if
 (perChannel)
        {
            quantMode += QuantMode::perChannelScaling();
        }
        if
 (perToken)
        {
            quantMode += QuantMode::perTokenScaling();
        }

        if
 (useInt8KvCache)
        {
            quantMode += int8KvCache();
        }

        if
 (useFp8KvCache)
        {
            quantMode += fp8KvCache();
        }

        if
 (useFp8Qdq)
        {
            quantMode += fp8Qdq();
        }

        return
 quantMode;
    }

      //////////////////////////////////////////////////////////

    constexpr
 QuantMode operator+(const QuantMode& other) noexcept
    {
        return
 QuantMode(mValue | other.mValue);
    }

    constexpr
 QuantMode& operator+=(const QuantMode& other) noexcept
    {
        return
 *this = *this + other;
    }

    constexpr
 QuantMode operator-(const QuantMode& other) noexcept
    {
        return
 QuantMode(mValue & ~other.mValue);
    }

    constexpr
 QuantMode& operator-=(const QuantMode& other) noexcept
    {
        return
 *this = *this - other;
    }

    constexpr
 bool operator==(const QuantMode& other) const noexcept
    {
        return
 mValue == other.mValue;
    }

    constexpr
 bool operator!=(const QuantMode& other) const noexcept
    {
        return
 !(*this == other);
    }

private
:
    BaseType mValue{0};
};

参考文献

  • • https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/cpp/tensorrt_llm/common/quantization.h
点个「赞」+「在看」❤️
让我们知道这份文字有温暖到你,也是我们持续创作的最大动力!
推荐
TensorRT-LLM 0.5.0 源码之三十
TensorRT-LLM 0.5.0 源码之二十九
TensorRT-LLM 0.5.0 源码之二十八
信息差的消失
Agent 数据生产与训练
基准测试设计:选择开放模型测试与封闭模型测试
gRPC 使用建议
flashinfer.sampling 实现四
AI 时代的组织变更
TensorRT-LLM 0.5.0 源码之二十七
flashinfer.sampling 实现三
什么是循环工程?AI 编码智能体的新范式
Trae IDE 实战指南:核心AI功能、Skill运用与项目开发全流程
Small matrix multiplication - Triton
从空文件夹到生成图表:Claude Code 实战教程
Claude Code 入门:研究者配置指南
Claude Code 究竟擅长什么?一次实测验证
GLM-5.2:面向长时序任务打造
Anthropic Fable 5
循环工程(Loop Engineering)
Agent SFT 标准数据格式 + Loss Mask 完整实现
4D Parallelism
TileLang与OpenAI Triton的核心区别
TensorRT-LLM 0.5.0 源码之二十六
Agent SFT 数据
Claude Code 的上限,就是你的上限
如何用Claude Code提升软件工程工作效率、改善生活
LLM推理优化的核心技术:深入理解KV缓存与分页注意力机制
MiMo-V2-Flash技术报告
AI原生开发中的MCP与CLI对比
Qwen3-TTS 技术报告
PagedAttention
如何让AI听懂你的“话外音”?GOAT-SLM模型实现更懂情感的语言交互
FlashAttention与PagedAttention详解:拯救GPU显存,让大模型飞起来的核心技术
LM-as-a-judge:LLM评估指南
LLM Sequence Packing
深入了解SmoothQuant:大模型高效量化背后的数学原理
语音合成(TTS)分句生成拼接时的响度一致性问题:现状、成因与对策
当扩散模型遇上流匹配:原来是一回事儿
语音合成中的“一对多”问题主流模型解决方案分析
使用LoRA对LLM进行微调的实用技巧
语音合成(TTS)中文自然度:问题、成因、解决方案
语音合成(TTS)跳跃与重复问题的解析:成因、机制及解决方案
最新!SpeechLLM 综述:架构、能力、挑战与未来全揭秘