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

JYMiracle305 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

精度对比

性能对比

@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
Comment thread infini_train/include/optimizer.h Outdated
Comment thread infini_train/src/nn/modules/module.cc Outdated
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_ 数量太大的话排序不知道有没有性能问题)

@JYMiracle305 JYMiracle305 Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

这里保证一下顺序,先调用NamedModules保证 module 顺序,再在遍历 parameters_后进行排序,保证整体参数顺序稳定

Comment thread example/llama3/main.cc Outdated
Comment thread example/gpt2/main.cc Outdated
@JYMiracle305
JYMiracle305 force-pushed the feat/named-parameters branch 2 times, most recently from 0b8f151 to 9d6aa81 Compare August 11, 2026 08:11
// NamedModules only reads the hierarchy and provides its stable, name-sorted traversal order. Keep all module
// aliases here so parameter-level deduplication deterministically selects the first full parameter name.
named_modules
= const_cast<Module *>(this)->NamedModules(/*memory=*/nullptr, prefix, /*remove_duplicate=*/false);

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.

之前没注意,这里 NamedModules() 非 const,返回 shared_ptr,要调用的话引入了 const_cast、const_pointer_cast 和 shared_from_this(),感觉有点危险。而且如果后面param排序的话,module就不需要保序了?要不还是恢复局部递归 collect吧

namespace infini_train::nn::parallel {
DistributedOptimizer::DistributedOptimizer(OptimizerCreator creator,
const std::vector<std::shared_ptr<Tensor>> &full_params,
const NamedParameterList &named_parameters,

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.

同步更新一下docs\lora_usage_guide.md:33

Optimizer::Optimizer(const std::vector<std::shared_ptr<Tensor>> &params, float learning_rate)
: params_(params), learning_rate_(learning_rate) {}
Optimizer::Optimizer(const std::vector<std::shared_ptr<Tensor>> &params, float learning_rate,
const NamedParameterList &named_parameters)

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基类的named_parameters会用于创建成员变量parameter_names_,Adam 使用 parameter_names_ 生成 state key,但DistributedOptimizer其实没用parameter_names_,用的是base_optimizer_->StateDict()。所以这里named_parameters可以不传,默认空值直接返回,少一次遍历构造和成员变量保存


void DistributedOptimizer::BuildShardParamsAndBindGrads() {
shard_params_.clear();
shard_named_parameters_.clear();

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.

既然这里两个成员变量都clear了,也没有其他地方用了,那shard_params_和shard_named_parameters_能不能不作为成员变量,只作为普通变量?同理还有parameter_name_by_tensor_其实也没其他地方用,可以把获取的地方从构造函数挪到这个函数里来,避免作为成员变量一直保存,占用显存。

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