| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
refactor: extract shared NPU example helpers Co-authored-by: pll123pll123<penglianglong1@huawei.com> # message auto-generated for no-merge-commit merge: !57 merge example/common into main refactor: extract shared NPU example helpers Created-by: ll-pen Commit-by: pll123pll123 Merged-by: ascend-robot Description: # 【合入来源】 - [x] 需求:为五个量化 example 提供公共辅助脚本 # 【修改方案】 公共层只处理模型无关的加载参数、回调测量、状态保存恢复和参数采样。量化目标筛选、Qwen 模型类及专家后端选择、decoder 层路径、FSDP2 放置、loss/forward、训练更新和 logits 检查均由各样例负责。五个样例 PR #50~#54 和原始 MXFP8 样例适配 PR #58 依赖本 PR。 examples/common/__init__.py 仅声明 Python 包,没有包级 API。 examples/common/model_utils.py 只提供本地 Transformers 权重加载,不包含 Dense/MoE 分支。 | API | 职责与边界 | | --- | --- | | load_model(model_path, *, model_cls=None, dtype=torch.bfloat16, attn_implementation=None, use_cache=None, **model_kwargs) | 调用指定模型类的 from_pretrained,默认使用 AutoModelForCausalLM。显式传入本地加载和 dtype 参数;attention/cache 为 None 时保留模型默认。额外参数传给模型加载器。模型类、专家后端及设备放置由调用方决定。 | python from common.model_utils import load_model from transformers import Qwen3_5MoeForCausalLM # Dense:训练样例自行决定关闭 cache、使用 eager attention。 model = load_model(model_path, dtype=torch.bfloat16, attn_implementation="eager", use_cache=False) # MoE:模型类型和专家计算策略由具体样例选择。 moe = load_model(model_path, model_cls=Qwen3_5MoeForCausalLM, dtype=torch.bfloat16, attn_implementation="eager", use_cache=False, experts_implementation="grouped_mm") # 沿用模型默认的 attention/cache。 model = load_model(model_path, attn_implementation=None, use_cache=None) examples/common/runtime_utils.py 提供以下公共工具,不读取模型的 loss/logits,不执行模型模式切换、backward 或 optimizer.step。 | API | 职责与边界 | | --- | --- | | measure(step, *, device_api, warmup, iterations, tokens_per_step, before_measure=None) | 执行调用方提供的 step,预热后同步、计时、记录逐 rank 显存并汇总最慢 rank 耗时。设备同步/显存 API 由调用方传入,before_measure 用于预热后清理瞬态状态。tokens_per_step 是参与 rank 每步处理的总唯一 token 数,公共层不根据 world_size 推断并行语义。 | | training_checkpoint(model, optimizer, directory, load=False) | 通过 DCP 保存/恢复模型及优化器状态;optimizer 可为 None。调用者须先建立兼容的量化模型和优化器。未包含 scheduler、随机数和数据读取进度。 | | model_checkpoint(model, directory, load=False) | 仅保存/恢复给定模型的状态,不重建量化结构或推断量化配方。 | | weight_update_views(parameters) | 从调用方指定、有非零本地梯度的参数中选取梯度绝对值最大位置的标量视图,跳过空分片。由样例决定检查哪些参数及如何判定训练成功,采样不代表所有专家均有更新。 | python from common.runtime_utils import measure model.eval() def step(): with torch.no_grad(): model(input_ids=input_ids, use_cache=False) metrics = measure( step, device_api=torch.npu, warmup=3, iterations=10, tokens_per_step=input_ids.numel(), # 单卡;分布式由样例计算总唯一 token 数。 before_measure=lambda: model.zero_grad(set_to_none=True), ) 上述 device_api 参数只解除公共计量代码的 NPU 硬编码;五个样例入口仍显式使用 torch.npu,本 PR 不声明已验证 GPU 样例。模型不兼容的 forward、路由损失、生成、图捕获或 TP/EP 编排应由各自样例实现,不在公共层添加模型分支。 ### 公共 API 参数速查 #### load_model | 参数 | 候选项、默认值与填写方式 | | --- | --- | | model_path | 必填,本地 Transformers 模型目录,例如 "/models/Qwen2.5-0.5B-Instruct"。 | | model_cls | 关键字参数。省略或填 None 使用 AutoModelForCausalLM。要指定架构,传具有 from_pretrained 的模型类本身,例如 Qwen3_5MoeForCausalLM。 | | dtype | 关键字参数,默认 torch.bfloat16。传加载器和设备支持的 PyTorch dtype,例如 torch.bfloat16、torch.float16、torch.float32。五个样例使用 BF16。 | | attn_implementation | 关键字参数,默认 None,沿用模型选择。示例显式填 "eager";其他后端名称须由相应 Transformers 模型和环境支持。 | | use_cache | 关键字参数,默认 None,保留模型设置。False 或 True 分别在加载后关闭或开启 model.config.use_cache。 | | **model_kwargs | 可省略。以关键字形式透传给所选模型的 from_pretrained,例如 experts_implementation="grouped_mm"。可选键由该模型类决定。 | #### measure | 参数 | 候选项、默认值与填写方式 | | --- | --- | | step | 必填,无参可调用对象。传 step 函数本身,函数内完成一次待测操作;返回值会被丢弃。测前反向时把 backward() 放在回调中。 | | device_api | 必填关键字。本例填 torch.npu,对象须实现 synchronize、empty_cache、memory_allocated、reset_peak_memory_stats、max_memory_allocated、max_memory_reserved。 | | warmup、iterations | 必填关键字。公共函数允许 warmup >= 0、iterations >= 1,均填整数,如 warmup=3, iterations=10。五个工作流入口额外要求预热至少 1 次。 | | tokens_per_step | 必填关键字,填每步所有参与 rank 的唯一 token 总数,允许 0。每 rank 处理 64 个不同 token 的八卡数据并行填 512;八个 rank 共同处理同一批 64 个 token 填 64。 | | before_measure | 可选关键字,默认 None;或传无参回调,例如 lambda: model.zero_grad(set_to_none=True),预热结束后、计时开始前执行一次。 | #### training_checkpoint、model_checkpoint 和 weight_update_views | API 参数 | 候选项、默认值与填写方式 | | --- | --- | | 两个 checkpoint API 的 model | 必填,传模型实例;恢复时先按相同量化结构创建目标模型。 | | training_checkpoint 的 optimizer | 必填,传与该模型关联的优化器实例;只处理模型权重时显式传 None。保存和恢复使用匹配的状态结构。 | | 两个 checkpoint API 的 directory、load | 目录必填,可用字符串或 Path。分布式各 rank 使用同一个可访问目录。load=False(默认)保存;load=True 从该目录恢复。 | | weight_update_views 的 parameters | 必填,传参数迭代器,如 model.parameters(),或传目标参数列表,如 [linear.weight]。在反向之后、清梯度之前迭代。每个有非零本地梯度的参数产生一个标量视图;空结果表示此次没有可采样的位置。 | python # loss.backward() 已完成;model、optimizer 已建立。 samples = [(v, v.clone()) for v in weight_update_views(model.parameters())] optimizer.step() changed = any(not torch.equal(view, before) for view, before in samples) training_checkpoint(model, optimizer, "./ckpt/train") training_checkpoint(model, optimizer, "./ckpt/train", load=True) model_checkpoint(quantized_model, "./ckpt/infer") model_checkpoint(quantized_model, "./ckpt/infer", load=True) # 【资料变更】 模块及函数注释说明职责,调用方式见各 example README。 # 【接口变更】 仅调整 example 内部 API,不修改生产接口。删除公共 select/build_model/train/logits 和模型绑定的测量接口,调用方改用上述显式参数和 step 回调。load_model 的 attention/cache 默认保留模型配置,训练样例显式传入 eager/False。 # 【功能验证】 以下为使用公共 helper 的五个样例此前在 A5 单卡运行完整 Qwen2.5-0.5B-Instruct 的记录。FP8 训练的四种粒度组合均通过;FP8 QAT 和 FP8 推理各有两种同粒度组合通过。INT4 QAT 完成训练、恢复续训、转换和生成,INT4 推理完成量化、保存恢复和生成。以下每个样例摘录一组运行日志。 单卡运行记录(pretrained-results.json 摘录,exit_code 为整个样例进程的退出码): text {"case": "fp8_training_w_tensorwise_a_tensorwise", "exit_code": 0, "seconds": 32.25628113746643, "device": 1} {"case": "fp8_qat_w_tensorwise_a_tensorwise", "exit_code": 0, "seconds": 45.522090435028076, "device": 2} {"case": "fp8_inference_w_tensorwise_a_tensorwise", "exit_code": 0, "seconds": 24.144482612609863, "device": 3} {"case": "int4_weight_only_qat", "exit_code": 0, "seconds": 33.85707879066467, "device": 4} {"case": "int4_weight_only_inference", "exit_code": 0, "seconds": 24.244094133377075, "device": 5} # 【CheckList】 - [x] 本地静态和 CPU 辅助验证通过 - [x] A5 单卡验证结果已记录 - [ ] CI 通过 See merge request: Ascend/torchao-ascend!57 | 3 天前 | |
feat: add int4 weight only inference example Co-authored-by: pll123pll123<penglianglong1@huawei.com> # message auto-generated for no-merge-commit merge: !54 merge example/int4-weight-only-inference into main feat: add int4 weight only inference example Created-by: ll-pen Commit-by: pll123pll123 Merged-by: ascend-robot Description: # 【合入来源】 - [x] 需求:提供完整的 int4_weight_only_inference NPU 示例 - [ ] 问题单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 融合专家转换通过样例配置 QwenInt4ExpertConfig 分发。样例使用 register_quantize_module_handler 注册转换函数,调用 quantize_ 选择对应专家,执行模块负责已有的量化计算。 **设计原因。** 单卡样例的输入是 Qwen2.5-0.5B-Instruct 的 BF16 预训练权重。为了用这份权重验证 INT4 推理,样例在加载后先执行训练后量化(PTQ):通过 TorchAO 的 Int4WeightOnlyConfig 和 quantize_ 将目标 Linear 的权重转换为打包的 INT4 表示,激活保持 BF16。转换在构建量化模型时完成,后续前向和逐 token 生成复用该模型的 INT4 权重及量化参数。若输入已是与当前 TorchAO/NPU 表示兼容的 INT4 checkpoint,则可在恢复相应的量化模型结构和参数后直接推理。 1. 在 examples/qwen2_5/ 下新增 qwen2_5_int4_weight_only_inference.py、int4_weight_only_inference.sh 和对应配置;MoE 配置为 examples/qwen3_5/qwen3_5_int4_weight_only_inference_moe_config.yaml。 2. 单卡配置使用完整 Qwen2.5-0.5B-Instruct;8 卡 FSDP2 配置使用完整 Qwen2.5-72B-Instruct 或 Qwen3.5-35B-A3B 文本模型,默认使用完整层数。 3. 以 BF16 为对照,测量量化推理的耗时和逐卡显存。保存和恢复量化 checkpoint,对同一输入比较 logits,再执行四个 token 的贪心生成。 4. MoE 将两组三维专家权重按 K 轴每 128 个值做 INT4 量化,保存每组的 scale 和 offset。沿用原有 router 的 top-k 选择和权重,在样例内执行两次 npu_grouped_matmul 并合并输出。 5. 公共 helper PR #57 仅提供通用加载、step 回调测量、DCP 状态读写和权重采样。当前样例自行实现量化层筛选、Qwen3.5 模型及 grouped_mm 后端适配、block/FSDP2 编排、forward/loss/logits 和适用的训练检查;测量显式传入 torch.npu 及数据并行 token 数。生成和量化步骤由本样例编排。 ### 启动接口与填写示例 在仓库根目录运行以下命令,将模型目录替换为本机已下载的权重和 tokenizer 所在目录。 bash bash examples/qwen2_5/int4_weight_only_inference.sh single --model-path /models/Qwen2.5-0.5B-Instruct | 入口参数 | 可选值与填写方式 | | --- | --- | | Shell 第一个位置参数 | 省略时为 single(Qwen2.5-0.5B 单卡)。dense 选择 Qwen2.5-72B 的八进程配置,moe 选择 Qwen3.5-35B-A3B 的八进程配置。 | | --config PATH | Python 入口读取的 YAML 文件。直接运行 python examples/qwen2_5/qwen2_5_int4_weight_only_inference.py 时默认读取同目录的 qwen2_5_int4_weight_only_inference_config.yaml。使用自定义配置可在启动模式后传 --config ./my_config.yaml。 | | --model-path PATH | 可选,覆盖 YAML 的 model_path。填写启动模式所对应模型的本地目录。 | 下列单卡 YAML 给出了所有对外配置字段的一组填写值。 yaml model_path: /path/to/Qwen2.5-0.5B-Instruct model_kind: dense world_size: 1 packing: plain_int32 batch_size: 1 sequence_length: 64 seed: 42 warmup: 3 iterations: 10 output: ./results/int4_weight_only_inference_single | YAML 字段 | 类型、候选值与用途 | | --- | --- | | model_path、model_kind、world_size | 路径填本地目录。model_kind 填 dense 或 moe。world_size 填实际启动的进程数,随附配置的三组分别为 dense/1、dense/8、moe/8。 | | batch_size、sequence_length | 每个 rank 的样本数和输入 token 长度;填写正整数。训练或计算 causal loss 时长度至少为 2,并在模型上下文范围内。 | | seed、warmup、iterations | 随机种子填整数;预热和正式计时次数分别填正整数。 | | output | 可写结果目录,接受绝对路径或相对启动工作目录的路径。 | | packing | 在 YAML 中设置。随附 Dense/MoE 的 A5 配置均填 plain_int32,MoE 强制使用该值。Dense 将字符串传给 TorchAO;其 v0.18 枚举另有 plain、preshuffled、tile_packed_to_4d。这些名称不代表对应内核已在本示例 A5 环境验证,plain 路径依赖 MSLK。 | ### Python 量化 API 与对应参数 以下为该工作流实际传给框架的参数。修改 CLI 或 YAML 即可改变上表开放的选项;表中标为固定的值由 Python 配方提供。 | API | 参数如何填写 | | --- | --- | | quantize_(model, config, filter_fn=select) | 第一个参数填待转换的模型或子模块,第二个参数按下表所述阶段构造。普通层使用 select(module, name),其中 module 是当前模块,name 是模块路径,返回布尔值;选中 Linear 并排除以 lm_head、shared_expert_gate、.gate 结尾的名称。 | | Int4WeightOnlyConfig(group_size=128, int4_packing_format=..., version=2) | int4_packing_format 取 YAML 的 packing。上游 group size 可选 32、64、128、256;本例的打包和专家 scale 布局固定用 128,版本固定为 2。激活保留 BF16。 | | QwenInt4ExpertConfig() | 无需填写参数。将实例传给 quantize_(model, QwenInt4ExpertConfig(), filter_fn=...),筛选回调只匹配 Qwen3_5MoeExperts。 | | Int4PlainInt32Tensor.from_hp(...)、npu_grouped_matmul(...) | 专家打包传展平的二维浮点权重和块大小 [1, 128]。专家前向传单元素输入列表、转置后的打包权重列表、每组 antiquant_scale、antiquant_offset 及每专家 token 数 group_list。本例固定 group_list_type=1, group_type=0, split_item=2;各张量由模型产生。 | **加载、测量和恢复接口。** load_model(model_path, *, model_cls, dtype, attn_implementation, use_cache, **model_kwargs) 接受本地路径。Dense 传 AutoModelForCausalLM,MoE 传 Qwen3_5MoeForCausalLM 和 experts_implementation="grouped_mm"。本例使用 torch.bfloat16、"eager"、False;公共接口也接受 attn_implementation=None 或 use_cache=None 来保留模型默认值。 measure(step, *, device_api, warmup, iterations, tokens_per_step, before_measure=None) 的 step 填无参执行回调,device_api=torch.npu,次数取 YAML;tokens_per_step 填单 rank 输入 token 数乘数据并行 rank 数。before_measure 可省略或传无参清理回调。 model_checkpoint(model, directory, load=False) 传模型和 checkpoint 目录;False 保存,True 恢复到已重建的量化模型。 # 【资料变更】 新增 examples/qwen2_5/int4_weight_only_inference.md,说明三种启动模式、依赖、配置、测量范围及资源要求。每个 rank 当前在 CPU 加载完整模型,须考虑主机内存开销。 # 【接口变更】 不修改生产 API,新增 example 命令行入口。 # 【功能验证】 ### Qwen2.5-0.5B-Instruct · A5 单卡 以下为此前使用完整预训练模型和配套 tokenizer 运行样例的记录。使用 plain_int32 打包,完成 INT4 权重量化推理、量化模型保存恢复和生成。 bash bash examples/qwen2_5/int4_weight_only_inference.sh single --model-path /models/Qwen2.5-0.5B-Instruct 单卡运行记录(pretrained-results.json 摘录,exit_code 为整个样例进程的退出码): text {"case": "int4_weight_only_inference", "exit_code": 0, "seconds": 24.244094133377075, "device": 5} 量化阶段的原始输出(节选一组配置): text int4_weight_only_inference_single/metrics.jsonl {"stage": "quantized", "per_rank": [{"seconds": 0.2954106900142506, "resident_bytes": 467816960, "peak_allocated_bytes": 502233600, "peak_reserved_bytes": 557842432}], "ms_per_step": 29.541069001425058, "tokens_per_second": 2166.4754243291823, "speedup": 0.5470009226447372, "max_rank_peak_memory_saving_fraction": 0.5125899057154569} ### Qwen3.5-35B-A3B · 完整 40 层单卡(此前运行) 完整 40 层,A5 单卡,batch=1、序列长度 32、预热和计时各一次。正式推理入口完成 BF16 对照、量化推理、checkpoint 保存及恢复、logits 对比和 4 token 生成。以下为 summary.json 指标摘录,单次计时仅用于功能验证。 text {"stage": "bf16", "ms_per_step": 524.4496699888259, "peak_allocated_bytes": [69355939328]} {"stage": "quantized", "ms_per_step": 194.90870996378362, "peak_allocated_bytes": [19974686208]} ### Qwen3.5-35B-A3B · 两层单卡(提交 12279988) Qwen3.5-35B-A3B 原始层 0、3 的真实权重,单卡,batch 1,序列长 32,预热和计时各 1 次。推理样例完成 BF16 对照、量化推理、checkpoint 恢复、logits 比对和 4 token 生成。以下摘录本次 summary.json 的计时字段,单位为毫秒,仅用于识别已完成的阶段。 text exit_code=0 bf16: {"ms_per_step": 20.226960070431232} quantized: {"ms_per_step": 10.83251996897161} 完整 40 层 MoE 的八卡 FSDP2 配置尚未完成端到端验证。 # 【CheckList】 - [x] 已核对单卡预训练模型各配置的结果 - [ ] CI 通过 See merge request: Ascend/torchao-ascend!54 | 3 天前 | |
feat: add int4 weight only inference example Co-authored-by: pll123pll123<penglianglong1@huawei.com> # message auto-generated for no-merge-commit merge: !54 merge example/int4-weight-only-inference into main feat: add int4 weight only inference example Created-by: ll-pen Commit-by: pll123pll123 Merged-by: ascend-robot Description: # 【合入来源】 - [x] 需求:提供完整的 int4_weight_only_inference NPU 示例 - [ ] 问题单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 融合专家转换通过样例配置 QwenInt4ExpertConfig 分发。样例使用 register_quantize_module_handler 注册转换函数,调用 quantize_ 选择对应专家,执行模块负责已有的量化计算。 **设计原因。** 单卡样例的输入是 Qwen2.5-0.5B-Instruct 的 BF16 预训练权重。为了用这份权重验证 INT4 推理,样例在加载后先执行训练后量化(PTQ):通过 TorchAO 的 Int4WeightOnlyConfig 和 quantize_ 将目标 Linear 的权重转换为打包的 INT4 表示,激活保持 BF16。转换在构建量化模型时完成,后续前向和逐 token 生成复用该模型的 INT4 权重及量化参数。若输入已是与当前 TorchAO/NPU 表示兼容的 INT4 checkpoint,则可在恢复相应的量化模型结构和参数后直接推理。 1. 在 examples/qwen2_5/ 下新增 qwen2_5_int4_weight_only_inference.py、int4_weight_only_inference.sh 和对应配置;MoE 配置为 examples/qwen3_5/qwen3_5_int4_weight_only_inference_moe_config.yaml。 2. 单卡配置使用完整 Qwen2.5-0.5B-Instruct;8 卡 FSDP2 配置使用完整 Qwen2.5-72B-Instruct 或 Qwen3.5-35B-A3B 文本模型,默认使用完整层数。 3. 以 BF16 为对照,测量量化推理的耗时和逐卡显存。保存和恢复量化 checkpoint,对同一输入比较 logits,再执行四个 token 的贪心生成。 4. MoE 将两组三维专家权重按 K 轴每 128 个值做 INT4 量化,保存每组的 scale 和 offset。沿用原有 router 的 top-k 选择和权重,在样例内执行两次 npu_grouped_matmul 并合并输出。 5. 公共 helper PR #57 仅提供通用加载、step 回调测量、DCP 状态读写和权重采样。当前样例自行实现量化层筛选、Qwen3.5 模型及 grouped_mm 后端适配、block/FSDP2 编排、forward/loss/logits 和适用的训练检查;测量显式传入 torch.npu 及数据并行 token 数。生成和量化步骤由本样例编排。 ### 启动接口与填写示例 在仓库根目录运行以下命令,将模型目录替换为本机已下载的权重和 tokenizer 所在目录。 bash bash examples/qwen2_5/int4_weight_only_inference.sh single --model-path /models/Qwen2.5-0.5B-Instruct | 入口参数 | 可选值与填写方式 | | --- | --- | | Shell 第一个位置参数 | 省略时为 single(Qwen2.5-0.5B 单卡)。dense 选择 Qwen2.5-72B 的八进程配置,moe 选择 Qwen3.5-35B-A3B 的八进程配置。 | | --config PATH | Python 入口读取的 YAML 文件。直接运行 python examples/qwen2_5/qwen2_5_int4_weight_only_inference.py 时默认读取同目录的 qwen2_5_int4_weight_only_inference_config.yaml。使用自定义配置可在启动模式后传 --config ./my_config.yaml。 | | --model-path PATH | 可选,覆盖 YAML 的 model_path。填写启动模式所对应模型的本地目录。 | 下列单卡 YAML 给出了所有对外配置字段的一组填写值。 yaml model_path: /path/to/Qwen2.5-0.5B-Instruct model_kind: dense world_size: 1 packing: plain_int32 batch_size: 1 sequence_length: 64 seed: 42 warmup: 3 iterations: 10 output: ./results/int4_weight_only_inference_single | YAML 字段 | 类型、候选值与用途 | | --- | --- | | model_path、model_kind、world_size | 路径填本地目录。model_kind 填 dense 或 moe。world_size 填实际启动的进程数,随附配置的三组分别为 dense/1、dense/8、moe/8。 | | batch_size、sequence_length | 每个 rank 的样本数和输入 token 长度;填写正整数。训练或计算 causal loss 时长度至少为 2,并在模型上下文范围内。 | | seed、warmup、iterations | 随机种子填整数;预热和正式计时次数分别填正整数。 | | output | 可写结果目录,接受绝对路径或相对启动工作目录的路径。 | | packing | 在 YAML 中设置。随附 Dense/MoE 的 A5 配置均填 plain_int32,MoE 强制使用该值。Dense 将字符串传给 TorchAO;其 v0.18 枚举另有 plain、preshuffled、tile_packed_to_4d。这些名称不代表对应内核已在本示例 A5 环境验证,plain 路径依赖 MSLK。 | ### Python 量化 API 与对应参数 以下为该工作流实际传给框架的参数。修改 CLI 或 YAML 即可改变上表开放的选项;表中标为固定的值由 Python 配方提供。 | API | 参数如何填写 | | --- | --- | | quantize_(model, config, filter_fn=select) | 第一个参数填待转换的模型或子模块,第二个参数按下表所述阶段构造。普通层使用 select(module, name),其中 module 是当前模块,name 是模块路径,返回布尔值;选中 Linear 并排除以 lm_head、shared_expert_gate、.gate 结尾的名称。 | | Int4WeightOnlyConfig(group_size=128, int4_packing_format=..., version=2) | int4_packing_format 取 YAML 的 packing。上游 group size 可选 32、64、128、256;本例的打包和专家 scale 布局固定用 128,版本固定为 2。激活保留 BF16。 | | QwenInt4ExpertConfig() | 无需填写参数。将实例传给 quantize_(model, QwenInt4ExpertConfig(), filter_fn=...),筛选回调只匹配 Qwen3_5MoeExperts。 | | Int4PlainInt32Tensor.from_hp(...)、npu_grouped_matmul(...) | 专家打包传展平的二维浮点权重和块大小 [1, 128]。专家前向传单元素输入列表、转置后的打包权重列表、每组 antiquant_scale、antiquant_offset 及每专家 token 数 group_list。本例固定 group_list_type=1, group_type=0, split_item=2;各张量由模型产生。 | **加载、测量和恢复接口。** load_model(model_path, *, model_cls, dtype, attn_implementation, use_cache, **model_kwargs) 接受本地路径。Dense 传 AutoModelForCausalLM,MoE 传 Qwen3_5MoeForCausalLM 和 experts_implementation="grouped_mm"。本例使用 torch.bfloat16、"eager"、False;公共接口也接受 attn_implementation=None 或 use_cache=None 来保留模型默认值。 measure(step, *, device_api, warmup, iterations, tokens_per_step, before_measure=None) 的 step 填无参执行回调,device_api=torch.npu,次数取 YAML;tokens_per_step 填单 rank 输入 token 数乘数据并行 rank 数。before_measure 可省略或传无参清理回调。 model_checkpoint(model, directory, load=False) 传模型和 checkpoint 目录;False 保存,True 恢复到已重建的量化模型。 # 【资料变更】 新增 examples/qwen2_5/int4_weight_only_inference.md,说明三种启动模式、依赖、配置、测量范围及资源要求。每个 rank 当前在 CPU 加载完整模型,须考虑主机内存开销。 # 【接口变更】 不修改生产 API,新增 example 命令行入口。 # 【功能验证】 ### Qwen2.5-0.5B-Instruct · A5 单卡 以下为此前使用完整预训练模型和配套 tokenizer 运行样例的记录。使用 plain_int32 打包,完成 INT4 权重量化推理、量化模型保存恢复和生成。 bash bash examples/qwen2_5/int4_weight_only_inference.sh single --model-path /models/Qwen2.5-0.5B-Instruct 单卡运行记录(pretrained-results.json 摘录,exit_code 为整个样例进程的退出码): text {"case": "int4_weight_only_inference", "exit_code": 0, "seconds": 24.244094133377075, "device": 5} 量化阶段的原始输出(节选一组配置): text int4_weight_only_inference_single/metrics.jsonl {"stage": "quantized", "per_rank": [{"seconds": 0.2954106900142506, "resident_bytes": 467816960, "peak_allocated_bytes": 502233600, "peak_reserved_bytes": 557842432}], "ms_per_step": 29.541069001425058, "tokens_per_second": 2166.4754243291823, "speedup": 0.5470009226447372, "max_rank_peak_memory_saving_fraction": 0.5125899057154569} ### Qwen3.5-35B-A3B · 完整 40 层单卡(此前运行) 完整 40 层,A5 单卡,batch=1、序列长度 32、预热和计时各一次。正式推理入口完成 BF16 对照、量化推理、checkpoint 保存及恢复、logits 对比和 4 token 生成。以下为 summary.json 指标摘录,单次计时仅用于功能验证。 text {"stage": "bf16", "ms_per_step": 524.4496699888259, "peak_allocated_bytes": [69355939328]} {"stage": "quantized", "ms_per_step": 194.90870996378362, "peak_allocated_bytes": [19974686208]} ### Qwen3.5-35B-A3B · 两层单卡(提交 12279988) Qwen3.5-35B-A3B 原始层 0、3 的真实权重,单卡,batch 1,序列长 32,预热和计时各 1 次。推理样例完成 BF16 对照、量化推理、checkpoint 恢复、logits 比对和 4 token 生成。以下摘录本次 summary.json 的计时字段,单位为毫秒,仅用于识别已完成的阶段。 text exit_code=0 bf16: {"ms_per_step": 20.226960070431232} quantized: {"ms_per_step": 10.83251996897161} 完整 40 层 MoE 的八卡 FSDP2 配置尚未完成端到端验证。 # 【CheckList】 - [x] 已核对单卡预训练模型各配置的结果 - [ ] CI 通过 See merge request: Ascend/torchao-ascend!54 | 3 天前 | |
adapt torchao_npu to torchao0.18.0 Co-authored-by: GotFusion<5ge@live.com> | 2 个月前 |