feat(benchmark): huggingface runner 接入 5 个 LLM 真实形状条目(DistillGPT2/XLNet/gemma-2-2b/gemma-3-4b-it /T5Small文本主干)

Co-authored-by: huyuchao<huyuchao2@huawei.com>



# message auto-generated for no-merge-commit merge:
!46686 merge huyuchao/hf-llm-runner-entries into master

feat(benchmark): huggingface runner 接入 5 个 LLM 真实形状条目(DistillGPT2/XLNet/gemma-2-2b/gemma-3-4b-it /T5Small文本主干)

Created-by: huyuchao
Commit-by: huyuchao
Merged-by: ascend-robot
Description: ## 内容(5 提交,3 文件 +153/-9,自洽交付——单独 checkout 本分支即可复现全部结论)

| commit | 内容 |
|---|---|
| bfcadd70 | **四 LLM EXTRA_MODELS 真实形状条目**(config 随机初始化、无权重下载)+ gemma 版本守卫 + 显式 bs + seq=512 |
| 70cfb9ce | 五条目 config **`use_cache=False`** + NPU eager 非确定**豁免覆写** |
| 769e97c2 | common.py **恢复上游 eager 非确定豁免机制**(基类空集,默认零行为变化) |
| d2c83ad5 | **gemma_3_4b_it 入 `SKIP_ACCURACY_CHECK_MODELS`**(上游既有类别) |
| be15d0e3 | **恢复上游 T5Small 条目**(急切 config 写法)+ 豁免集/use_cache 补齐 |

### 五条目明细

| 模型 | 承载 | 关键超参 | 性质 |
|---|---|---|---|
| DistillGPT2 | GPT2LMHeadModel | 6L/768d/12h, vocab 50257 | 修复死条目(transformers 无同名类,stock 必 AttributeError) |
| XLNetLMHeadModel | XLNetLMHeadModel | xlnet-base 真实形状 12L/768d/12h/64hd, mem_len=null | 覆盖 XLNetConfig() 玩具形状(d_model=32) |
| T5Small | T5ForConditionalGeneration | 6L/512d/8h/64d_kv/2048d_ff, vocab 32128 | **恢复上游既有条目**(上游 AutoConfig 懒加载,改为急切 config 避开 import 期网络依赖) |
| gemma_2_2b | Gemma2ForCausalLM | 26L/2304d/8h-4kv/256hd, vocab 256000, sw 4096, softcap 50/30 | 新增 |
| gemma_3_4b_it | Gemma3ForCausalLM(文本主干) | 34L/2560d/8h-4kv/256hd, vocab 262208, sw 1024/pattern 6, rope linear×8 | 新增;vision tower 不在文本 runner 范围 |

全部 config 随机初始化(无权重下载,runner 惯例);gemma 需 transformers>=4.50(ImportError 守卫:旧版条目自动缺席 + 启动版本 warning,其余套件不受影响);T5 无版本要求。

## 五个精度堵点与对应修复(全部实测到代码/对象级)

| 堵点 | 根因(实证) | 修复 |
|---|---|---|
| DistillGPT2/gemma_2/T5Small 被 `eager_two_runs_differ` 门拦 | 910B2 loss 路径 eager 偶发 1~2 ulp 非位确定(设备级、与后端无关,probe 实证);**上游对此类模型有官方豁免通道而本仓 common.py 丢失**(dynamobench/common.py:2401 有 `skip_accuracy_check_as_eager_non_deterministic`,fork 继承了门丢了豁免) | 769e97c2 恢复机制 + 70cfb9ce/be15d0e3 套件覆写 `{DistillGPT2, gemma_2_2b, T5Small}`(扩到 inference——上游仅 training 开)。豁免只跳过 tol=0 参考系自检门,**不跳过任何数值比对** |
| 豁免后 `fail_accuracy` | 模型输出含 `past_key_values(DynamicCache)`(CausalLM 与 Seq2SeqLM 输出均如此)→ `same()` 对该结构**结构性崩**(探针进程级异常;runner 被 except 吞成 False)。XLNet 无 cache 对象通过,自洽 | 五 config `use_cache=False`(benchmark 推理本不需要 KV cache) |
| gemma_3 `eager_1st_run_OOM` | accuracy 协议存活集(orig + maybe_cast 副本 + fp64 副本闭包残留)峰值 ~58GB > 60GB 卡实际余量;**协议语义本身仅需 ~33GB**(probe 复刻验证) | d2c83ad5 入 SKIP 名单(与上游 `llama_v2_7b_16h`/`hf_GPT2_large` 同类别——上游按"40GB 机器放不下"标定跳过)。runner 内存纪律修复(fp64 副本闭包释放 + maybe_cast 同 dtype 免拷贝)后可移出 |
| T5Small 死键 | fork 演化时丢了 EXTRA 条目、仅剩 `BATCH_SIZE_DIVISORS` 残键(benchmark 仓历史 2475580 实证上游原有) | be15d0e3 按急刻 config 写法恢复(超参抄自官方 config.json)+ bs=8 |
| (附带)import 期网络依赖 | 上游 T5 用 `AutoConfig.from_pretrained` 懒加载,需 HF Hub 可达 | 急切 config 构造:import 期零网络/零缓存(CI、内网必需) |

## 验证(910B2,torch 2.13.0+cpu,triton-ascend 3.2.2,最新 master b29d5da)

### performance(eager vs triton_experimental,最新 master b29d5da + 并行编译,同场冷缓存)

| 模型 | bs | eager ms/iter | compiled ms/iter | speedup | 编译墙钟 |
|---|---|---|---|---|---|
| DistillGPT2 | 8 | 15.38 | 14.53 | **1.058x** | 67s |
| XLNetLMHeadModel | 8 | 103.68 | 82.71 | **1.254x** | 95s |
| T5Small | 4 | 21.18 | 15.06 | **1.407x** | 79s |
| gemma_2_2b | 2 | 87.54 | 79.59 | **1.100x** | 148s |
| gemma_3_4b_it | 2 | 135.57 | 128.76 | **1.053x** | 154s |

五模型全部编译通过全部加速(1.05~1.41x);gemma 两模型整图零断点,DistillGPT2 2 图 2 断。绝对 ms/speedup 随 master 前进与机况浮动(另一场四 LLM 复核:1.141/1.082/1.097/1.061x,eager 15.8/46.6/88.0/134.9ms),以同场比值为准。

### accuracy(本 PR 五提交全量)

| 模型 | 修复前 | 修复后 |
|---|---|---|
| DistillGPT2 | 门拦 → fail_accuracy | **pass_accuracy** |
| XLNetLMHeadModel | pass_accuracy | **pass_accuracy**(回归无恙,不在豁免集) |
| T5Small | (条目丢失不可测) | **pass_accuracy** |
| gemma_2_2b | 门拦 → fail_accuracy | **pass_accuracy** |
| gemma_3_4b_it | `eager_1st_run_OOM`(58.68GB) | **SKIP**(上游既有类别;协议语义需求 33GB,runner 内存纪律修复后可移出恢复完整检查) |

数值佐证:compiled-vs-eager 相对误差 1.2~1.8e-06、cos≈1.0(与设备自身 1~2 ulp 抖动同阶=fp32 累加序噪声,距比对容差 1e-3 三个数量级);compiled 自身位确定。

## 环境要求

- gemma 两模型需 transformers>=4.50(守卫缺席+版本 warning);DistillGPT2/XLNet/T5Small 无额外要求
- perf 依赖 TE 后端编译通道修复(PR #46616 或 #46891 任一)

关联 issue: [#4981](https://gitcode.com/Ascend/pytorch/issues/4981)、[#5062](https://gitcode.com/Ascend/pytorch/issues/5062)

See merge request: Ascend/pytorch!46686
This commit is contained in:
huyuchao
2026-09-29 09:52:30 +08:00
committed by ascend-robot
parent 2edb78f70d
commit 4af251d33a
3 changed files with 153 additions and 9 deletions
+27 -7
View File
@@ -1012,6 +1012,14 @@ class BenchmarkRunner:
model = DDP(model, find_unused_parameters=True)
return model
@property
def skip_accuracy_check_as_eager_non_deterministic(self):
# Upstream pytorch/benchmark contract (dynamobench/common.py): models
# whose eager execution is known non-bitwise-reproducible on this
# device are exempt from the tol=0 eager-two-runs gate; suites
# override this set. The downstream numerical comparisons still run.
return set()
def check_accuracy(
self, name, model, example_inputs, optimize_ctx, experiment, tag
):
@@ -1054,6 +1062,7 @@ class BenchmarkRunner:
# Collect the fp64 reference outputs to be used later for accuracy checking.
fp64_outputs = None
model_fp64 = inputs_fp64 = None
try:
model_fp64, inputs_fp64 = cast_to_fp64(
self.deepcopy_and_maybe_ddp(model),
@@ -1073,6 +1082,13 @@ class BenchmarkRunner:
if torch.npu.is_available():
torch.npu.empty_cache()
except Exception:
# Release the copies on the failure path too: the try-tail del
# never ran, and a leaked fp64-sized model OOMs the later eager
# deepcopy. Assign None instead of del: the try-tail del already
# unbound the names, and del-ing them again trips F821.
model_fp64 = inputs_fp64 = None
if torch.npu.is_available():
torch.npu.empty_cache()
log.warning(
"fp64 golden ref were not generated for %s. Setting accuracy check to cosine",
name,
@@ -1132,13 +1148,17 @@ class BenchmarkRunner:
# Two eager runs should have exactly same result
is_same = True
try:
if not same(
correct_result,
correct_rerun_result,
fp64_ref=None,
cos_similarity=False,
tol=0,
equal_nan=self.equal_nan,
if (
name
not in self.skip_accuracy_check_as_eager_non_deterministic
and not same(
correct_result,
correct_rerun_result,
fp64_ref=None,
cos_similarity=False,
tol=0,
equal_nan=self.equal_nan,
)
):
is_same = False
except Exception:
+124 -1
View File
@@ -230,6 +230,7 @@ SKIP_ACCURACY_CHECK_MODELS = {
# even for 40 GB machine.
"DebertaV2ForMaskedLM",
"BlenderbotForCausalLM",
"gemma_3_4b_it",
}
DECODER_INPUT_MODEL_CLASS_NAMES = {
@@ -329,7 +330,14 @@ def get_sequence_length(model_cls, model_name):
"Bert",
"Roberta",
)
) or model_name in ("DistillGPT2", "GoogleFnet", "YituTechConvBert", "CamemBert"):
) or model_name in (
"DistillGPT2",
"GoogleFnet",
"YituTechConvBert",
"CamemBert",
"gemma_2_2b",
"gemma_3_4b_it",
):
seq_length = 512
elif model_name in ("TrOCRForCausalLM"):
seq_length = 256
@@ -473,6 +481,112 @@ EXTRA_MODELS = {
),
}
# Real-shape LLM entries (random-init configs, no weight download; eager
# config objects are compatible with the lazy-config convention above --
# non-callables are used as-is):
# - DistillGPT2: legacy name with no same-named transformers class (distilgpt2
# is a 6-layer GPT-2); carried by GPT2LMHeadModel at its real shape.
# - XLNetLMHeadModel: overrides the default toy XLNetConfig() (d_model=32)
# with the real xlnet-base shape.
# - gemma_2_2b / gemma_3_4b_it: real hyperparams from the official
# config.json; gemma-3 uses the text backbone (vision tower is out of
# scope for the text runner). Gemma classes need transformers>=4.50;
# older versions skip the entries via the guard below.
from transformers import ( # noqa: E402
GPT2Config as _GPT2Config,
GPT2LMHeadModel as _GPT2LMHeadModel,
T5Config as _T5Config,
T5ForConditionalGeneration as _T5ForConditionalGeneration,
XLNetConfig as _XLNetConfig,
XLNetLMHeadModel as _XLNetLMHeadModel,
)
try:
from transformers import ( # noqa: E402
Gemma2Config as _Gemma2Config,
Gemma2ForCausalLM as _Gemma2ForCausalLM,
Gemma3ForCausalLM as _Gemma3ForCausalLM,
Gemma3TextConfig as _Gemma3TextConfig,
)
_HAS_GEMMA = True
except ImportError:
_HAS_GEMMA = False
# Warn instead of silently missing: make "--only gemma_* selects nothing" visible.
log.warning(
"gemma model entries disabled: transformers>=4.50 required, current %s",
getattr(importlib.import_module("transformers"), "__version__", "unknown"),
)
EXTRA_MODELS["DistillGPT2"] = (
_GPT2Config(vocab_size=50257, n_embd=768, n_layer=6, n_head=12, use_cache=False),
_GPT2LMHeadModel,
)
# T5Small: restore the upstream entry (upstream lazy-loads
# AutoConfig.from_pretrained("t5-small")); use an eager config instead to
# avoid network/cache access at import time. Hyperparams from the
# official config.json.
EXTRA_MODELS["T5Small"] = (
_T5Config(d_model=512, d_kv=64, d_ff=2048, num_layers=6, num_heads=8,
vocab_size=32128, use_cache=False),
_T5ForConditionalGeneration,
)
EXTRA_MODELS["XLNetLMHeadModel"] = (
_XLNetConfig(
vocab_size=32000,
d_model=768,
n_head=12,
d_head=64,
n_layer=12,
d_inner=3072,
mem_len=None,
untie_r=True,
),
_XLNetLMHeadModel,
)
if _HAS_GEMMA:
EXTRA_MODELS["gemma_2_2b"] = (
_Gemma2Config(
vocab_size=256000,
hidden_size=2304,
intermediate_size=9216,
num_hidden_layers=26,
num_attention_heads=8,
num_key_value_heads=4,
head_dim=256,
sliding_window=4096,
attn_logit_softcapping=50.0,
final_logit_softcapping=30.0,
query_pre_attn_scalar=256,
rope_theta=10000.0,
use_cache=False,
),
_Gemma2ForCausalLM,
)
EXTRA_MODELS["gemma_3_4b_it"] = (
_Gemma3TextConfig(
vocab_size=262208,
hidden_size=2560,
intermediate_size=10240,
num_hidden_layers=34,
num_attention_heads=8,
num_key_value_heads=4,
head_dim=256,
sliding_window=1024,
sliding_window_pattern=6,
rope_scaling={"rope_type": "linear", "factor": 8.0},
rope_theta=1000000.0,
rope_local_base_freq=10000.0,
query_pre_attn_scalar=256,
use_cache=False,
),
_Gemma3ForCausalLM,
)
# 2b/4b-class models OOM at the default bs=16 in fp32; explicit batch
# size, same source as huggingface_models_list.txt.
BATCH_SIZE_KNOWN_MODELS["gemma_2_2b"] = 2
BATCH_SIZE_KNOWN_MODELS["gemma_3_4b_it"] = 2
NPU_REQUIRE_LEARNING_RATE = {
"OPTForCausalLM",
"AlbertForQuestionAnswering",
@@ -509,6 +623,15 @@ class HuggingfaceRunner(BenchmarkRunner):
def fp32_only_models(self):
return FP32_ONLY_MODELS.union(NPU_FP32_ONLY_MODELS)
@property
def skip_accuracy_check_as_eager_non_deterministic(self):
# NPU eager loss-path kernels are sporadically 1-2 ulp
# non-bitwise-reproducible (device-level, backend-independent).
# Upstream exempts known-non-deterministic models from the tol=0
# eager gate but only in training mode; extend to inference here.
# Downstream numerical comparisons still run.
return {"DistillGPT2", "gemma_2_2b", "T5Small"}
def _get_model_cls_and_config(self, model_name):
if model_name not in EXTRA_MODELS:
model_cls = get_module_cls_by_model_name(model_name)
@@ -35,4 +35,5 @@ RobertaForQuestionAnswering,32
Speech2Text2ForCausalLM,1024
T5ForConditionalGeneration,8
TrOCRForCausalLM,64
XGLMForCausalLM,32
XGLMForCausalLM,32
T5Small,8