Skip to content

Feat: add named parameters - #194

Open
JYMiracle305 wants to merge 2 commits into
masterfrom
feat/named-parameters
Open

Feat: add named parameters#194
JYMiracle305 wants to merge 2 commits into
masterfrom
feat/named-parameters

Conversation

@JYMiracle305

Copy link
Copy Markdown
Contributor

1. 主要修改

  • 为 Module 新增 NamedParameters(prefix, recurse, remove_duplicate),支持参数名前缀、递归遍历和共享参数去重。
  • 为 Optimizer 新增 parameter_names_ 及设置、读取接口。
  • Adam 的 StateDict() 和 LoadStateDict() 使用参数名生成状态 key。
  • 未设置参数名时保留数字下标 key,兼容不使用命名参数接口的调用方式。
  • GPT-2 和 LLaMA3 入口根据 Tensor 指针为 optimizer 参数绑定模型参数名。
  • 测试使用现有 Linear 和 Sequential,覆盖 prefix、递归、共享参数去重和空子模块。

@JYMiracle305

Copy link
Copy Markdown
Contributor Author

精度对比

image

性能对比

image

@JYMiracle305
JYMiracle305 force-pushed the feat/named-parameters branch from 15630b4 to 8a20374 Compare August 6, 2026 08:12
@JYMiracle305
JYMiracle305 changed the base branch from master to feat/checkpoint-optimizer-state-control August 6, 2026 08:14
@JYMiracle305
JYMiracle305 changed the base branch from feat/checkpoint-optimizer-state-control to master August 6, 2026 08:16
@JYMiracle305
JYMiracle305 changed the base branch from master to feat/checkpoint-optimizer-state-control August 6, 2026 08:17
@JYMiracle305
JYMiracle305 force-pushed the feat/named-parameters branch from 8a20374 to 6efc27e Compare August 6, 2026 08:44
@JYMiracle305
JYMiracle305 force-pushed the feat/named-parameters branch from 6efc27e to 5a75ab8 Compare August 6, 2026 09:47
Base automatically changed from feat/checkpoint-optimizer-state-control to master August 7, 2026 02:18
@kilinchange
kilinchange force-pushed the feat/named-parameters branch from 5a75ab8 to 9dd31c7 Compare August 7, 2026 02:18

void set_parameter_names(const std::vector<std::string> &names);

const std::vector<std::string> &parameter_names() const;

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.

这个函数没有用到

std::unordered_set<const Tensor *> visited;

std::function<void(const Module &, const std::string &)> collect
= [&](const Module &module, const std::string &module_prefix) {

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.

parameters_ 和 modules_ 都是 unordered_map,不保序,同一个共享参数保存成哪个的 key 是不稳定的,现有 NamedModules() 是按名称排序 child 后遍历,这里能不能直接用NamedModules() 方法获取 modules_ 再保序遍历 parameters_ (parameters_ 数量太大的话排序不知道有没有性能问题)

Comment thread example/llama3/main.cc
model_chunks, ddp_world_size, ddp_rank);
} else {
optimizer = optimizer_creator(params_to_optimize);
std::unordered_map<const Tensor *, std::string> parameter_name_by_tensor;

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.

这个逻辑只在 else 分支调用,分布式场景是暂时没有适配吗

Comment thread example/gpt2/main.cc
model_chunks, ddp_world_size, ddp_rank);
} else {
optimizer = optimizer_creator(params_to_optimize);
std::unordered_map<const Tensor *, std::string> parameter_name_by_tensor;

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.

这个逻辑能不能挪到 optimizer 构造时,不要在训练入口这里拼

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.

2 participants