Feat: add named parameters - #194
Conversation
15630b4 to
8a20374
Compare
8a20374 to
6efc27e
Compare
6efc27e to
5a75ab8
Compare
5a75ab8 to
9dd31c7
Compare
| std::unordered_set<const Tensor *> visited; | ||
|
|
||
| std::function<void(const Module &, const std::string &)> collect | ||
| = [&](const Module &module, const std::string &module_prefix) { |
There was a problem hiding this comment.
parameters_ 和 modules_ 都是 unordered_map,不保序,同一个共享参数保存成哪个的 key 是不稳定的,现有 NamedModules() 是按名称排序 child 后遍历,这里能不能直接用NamedModules() 方法获取 modules_ 再保序遍历 parameters_ (parameters_ 数量太大的话排序不知道有没有性能问题)
There was a problem hiding this comment.
这里保证一下顺序,先调用NamedModules保证 module 顺序,再在遍历 parameters_后进行排序,保证整体参数顺序稳定
There was a problem hiding this comment.
在头文件里补充注释说明下:
InfiniTrain 的 NamedParameters 按 full parameter name 字典序返回,而不是 PyTorch registration order,且共享参数情况下保留名称字典序靠前的参数。
之后再看是否有必要与 PyTorch 语义完全对齐。
0b8f151 to
9d6aa81
Compare
| // 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); |
There was a problem hiding this comment.
之前没注意,这里 NamedModules() 非 const,返回 shared_ptr,要调用的话引入了 const_cast、const_pointer_cast 和 shared_from_this(),感觉有点危险。而且如果后面param排序的话,module就不需要保序了?要不还是恢复局部递归 collect吧
There was a problem hiding this comment.
收集完成后按照完整参数名排序,再进行共享参数去重,保证保留的参数名稳定
| std::unordered_set<const Tensor *> visited; | ||
|
|
||
| std::function<void(const Module &, const std::string &)> collect | ||
| = [&](const Module &module, const std::string &module_prefix) { |
There was a problem hiding this comment.
在头文件里补充注释说明下:
InfiniTrain 的 NamedParameters 按 full parameter name 字典序返回,而不是 PyTorch registration order,且共享参数情况下保留名称字典序靠前的参数。
之后再看是否有必要与 PyTorch 语义完全对齐。
2a64eef to
77b4a88
Compare
77b4a88 to
ed83c8b
Compare


1. 主要修改