Skip to content

[New features] Add Fleet<->HF config export/import bridge (hf_export) - #1769

Merged
changeyoung98 merged 3 commits into
PaddlePaddle:developfrom
changeyoung98:czy-config
Aug 19, 2026
Merged

[New features] Add Fleet<->HF config export/import bridge (hf_export)#1769
changeyoung98 merged 3 commits into
PaddlePaddle:developfrom
changeyoung98:czy-config

Conversation

@changeyoung98

Copy link
Copy Markdown
Contributor

PR Category

Execute Infrastructure

PR Types

New features

Description

Single source of truth for the Fleet<->open-source(HF) config.json bridge: naming map (FLEET_HF_FIELD_MAPPING / ROPE_SCALING_KEYMAP -> HF_EXPORT_RULES / HF_IMPORT_RULES) plus model-fact helpers (structural YaRN detection, rope_scaling pack/unpack, SWA/CSA window semantics, MTP-layer trimming, mHC injection).

Adds tests/single_card_tests/transformer/test_hf_export.py.

是否引起精度变化

Single source of truth for the Fleet<->open-source(HF) config.json bridge:
naming map (FLEET_HF_FIELD_MAPPING / ROPE_SCALING_KEYMAP -> HF_EXPORT_RULES /
HF_IMPORT_RULES) plus model-fact helpers (structural YaRN detection,
rope_scaling pack/unpack, SWA/CSA window semantics, MTP-layer trimming, mHC
injection). Pure Python, no paddle dependency; sibling of
TransformerConfig.transform_rules. Consumed by the erniebot HF config export
engine, which keeps only the export strategy (whitelist/blacklist) and I/O.

Adds tests/single_card_tests/transformer/test_hf_export.py (30 cases).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

序号 位置 优先级 状态
1 hf_export.py:36 P1
2 hf_export.py:59 P1
3 hf_export.py:66 P1
4 hf_export.py:271 P1
5 hf_export.py:301 P1
6 PR 标题 P3
7 hf_export.py:180 P1
Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.


# rope_scaling structural map: HF nested key -> Fleet flat field name.
ROPE_SCALING_KEYMAP = {
"type": "rope_type",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 这里仅识别旧版 type,而当前 HuggingFace 的 canonical 字段是 rope_typetype 只作为兼容别名)。因此 unpack_rope_scaling({"rope_type": "yarn", "factor": 4}) 会丢掉 YaRN 类型,只保留 factor,后续 Fleet 配置会回落到 plain RoPE,改变位置编码。请同时兼容 rope_type(可保留 type 兼容),并补充当前 HF 格式的导入测试。

@risemeup1111 risemeup1111 Aug 18, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前提交已同时兼容 canonical rope_type 和旧版 type,并覆盖两者并存时的优先级,问题已解决。

"params_dtype",
"torch_dtype",
lambda v: str(v).replace("paddle.", ""),
lambda v: f"paddle.{v}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 反向转换会把 HF 的 "bfloat16" 变成字符串 "paddle.bfloat16"。但 TransformerConfig.params_dtype 会直接传给 create_parameter(dtype=...),Paddle 接受的是 "bfloat16" 或实际 dtype 对象,不接受带 paddle. 前缀的字符串,正常 HF 配置会在建模时失败。请返回规范 dtype 字符串或解析为实际 dtype,并覆盖 import converter。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前提交已将导入 dtype 规范化为 Paddle 可接受的不带前缀字符串,并补充对应测试,问题已解决。

(
"hidden_act",
"hidden_act",
lambda v: v.__name__ if callable(v) else v,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 TransformerConfig._process_attribute 会把 HF 的 gelu_pytorch_tanh 转成 functools.partial(F.gelu, approximate=True),Kimi K3 provider 也使用同一形式;partial 虽然 callable,却没有 __name__,此处导出会直接抛 AttributeError。请保留原始字符串或显式处理该 partial,并补充往返测试。

@risemeup1111 risemeup1111 Aug 18, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前提交已显式识别 gelu_pytorch_tanhfunctools.partial 形式,并覆盖导出规则,问题已解决。

def is_swa_config(hf_config):
"""Whether an HF config declares SWA-specific fields."""
return any(
key in SWA_MARKER_HF_KEYS or key.startswith("swa_") for key in hf_config

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 这里只靠 swa_* 伴随字段识别 SWA,会漏掉标准 HF 配置。例如 Mistral 只有 sliding_window=4096,没有任何 swa_* 键,于是规则仍将其导入成 csa_window_size,原生 sliding_window 丢失,非 DSv4 模型的 SWA 被关闭。建议仅在存在 compress_ratios/DSv4 标记时判定 CSA,其余 sliding_window 保持原名,并补充无伴随字段的 SWA 用例。

@risemeup1111 risemeup1111 Aug 18, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前提交已改为仅在 DSv4/压缩比例标记存在时按 CSA 重命名,标准 HF sliding_window 会保持原名,问题已解决。

MTP_TRIM_KEYS = (
"window_attn_skip_freq",
"csa_compress_ratios",
"compress_ratios",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 这里列了 compress_ratios 的 HF 名,却漏了 window_attn_skip_freq 对应的 HF 名 hybrid_layer_pattern。对 HF 输入执行裁剪时会清零 num_nextn_predict_layers 并裁掉 compress_ratios,但 pattern 仍保留 MTP 尾项;反向映射后 TransformerConfig 会以 MTP=0 校验过长列表并失败。请加入 hybrid_layer_pattern,并用 HF 侧字段补测。

@risemeup1111 risemeup1111 Aug 18, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前提交已将 hybrid_layer_pattern 加入 MTP 裁剪键,并补充 HF 字段测试,问题已解决。

Comment thread src/paddlefleet/transformer/hf_export.py Outdated
For dsv4_hybrid the RoPE mode is decided per layer, so a global
rope_type="yarn" must not short-circuit -- HCA/CSA follow their
per-type overrides (default YaRN unless forced to "rope"), MLA (-2)
layers follow the global rope_type, and window layers stay plain
RoPE. Add regression tests for the override / MLA / window combos.
@changeyoung98 changeyoung98 changed the title Add Fleet<->HF config export/import bridge (hf_export) [New features] Add Fleet<->HF config export/import bridge (hf_export) Aug 18, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.10345% with 8 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@d09fdbb). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/paddlefleet/transformer/hf_export.py 93.10% 5 Missing and 3 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             develop    #1769   +/-   ##
==========================================
  Coverage           ?   93.10%           
==========================================
  Files              ?        1           
  Lines              ?      116           
  Branches           ?       33           
==========================================
  Hits               ?      108           
  Misses             ?        5           
  Partials           ?        3           
Flag Coverage Δ
coverage_combine 93.10% <93.10%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/paddlefleet/transformer/hf_export.py 93.10% <93.10%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@changeyoung98
changeyoung98 merged commit 74ae462 into PaddlePaddle:develop Aug 19, 2026
35 of 37 checks passed
@changeyoung98
changeyoung98 deleted the czy-config branch August 19, 2026 02:48
@risemeup1111

Copy link
Copy Markdown
Contributor

✅ Cherry-pick successful! Created PR: #1782

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants