Repository navigation
[Module] Refactor init_weights to config-based param_init system - #2633
Conversation
[ghstack-poisoned]
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. ghstack-source-id: 024899c Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. ghstack-source-id: 11120b6 Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. ghstack-source-id: 6871155 Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. ghstack-source-id: 803f279 Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: e6704e8 Pull-Request: #2633
[ghstack-poisoned]
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: 440e49d Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: 1bcce2b Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: 884689c Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: dee7497 Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: ee41de8 Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: ee41de8 Pull-Request: #2633
[ghstack-poisoned]
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: e7a02cb Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: 3f77f2f Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: 3f77f2f Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: aa6d367 Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: 1937d1e Pull-Request: #2633
**NOT READY TO REVIEW** **NOT READY TO REVIEW** **Motiviation and Design decision** Parameter initialization should be configurable without modifying module code. Users reuse provided helpers (e.g., `init_trunc_normal`, `init_zeros`) to customize how parameters are initialized. Parameters are identified by their FQNs, and `init_by_regex` lets users map regex patterns to initializers — so a single declarative dict at the model config level controls all parameter init. In theory, we could just apply param_init at the root module and be done — no recursion needed. So why does `init_states` recurse? The main motivation is that buffer initialization is usually inherently coupled with module internals and not user-configurable. Since buffers require per-module logic via `init_self_buffers`, we provide a recursive init_states that handles both: it walks the module tree, initializing parameters via the configurable param_init and buffers via module-specific overrides, in a single pass. **Summary** - Remove `init_weights`. - `init_states` auto-recurses the module tree, then calls `init_self_parameters` and `init_self_buffers` on each module. - `param_init` (a NamedInitializer callable on Module.Config) uses regex-matched FQN patterns to map parameters to initializers. Child modules without their own `param_init` delegate up the parent chain, so init logic is defined once at the model config level. - `make_decoder_param_init provides` shared patterns for decoder-based model, reused by Llama3, Llama4, Qwen3, and DeepSeek V3. Model-specific extensions (Flux DiT-style, GPT-OSS MoE biases) are composed via dict merge. - Removes init_mean/init_std fields from Linear.Config and Embedding.Config — init parameters are now expressed entirely through param_init patterns. **Verification** with loss_compare.py ┌─────────────┬───────┬───────────────────┐ │ Model │ Steps │ Result │ ├─────────────┼───────┼───────────────────┤ │ Llama3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ Qwen3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ DeepSeek V3 │ 10 │ Bitwise identical │ ├─────────────┼───────┼───────────────────┤ │ GPT-OSS │ 10 │ Bitwise identical │ └─────────────┴───────┴───────────────────┘ ghstack-source-id: ef797d7 Pull-Request: #2633
tianyu-l
left a comment
There was a problem hiding this comment.
I have two major feedback
- rename
DeferredCallableto justFunctionto make it sounds generic. - about when to do config expansion and per-layer
param_initassignment.
If 2 is not trivial to address, we can land first and address later.
| m.to_empty(device=init_device) | ||
| with torch.no_grad(): | ||
| cast(Decoder, m).init_weights(buffer_device=buffer_device) | ||
| # TODO: Change this back to init_weights once |
There was a problem hiding this comment.
what does this TODO mean? It's already init_weights.
| # handling, see below. | ||
| rope: RoPE.Config | ||
| layer: TransformerBlock.Config # required, no default | ||
| layer: TransformerBlock.Config |
There was a problem hiding this comment.
I had thought we don't need to put this in Decoder.Config. It's a pure config time thing, which evaporates after config construction.
| hidden_dim: int | ||
| linear_bias: bool = False | ||
| w1: Linear.Config | ||
| w2w3: Linear.Config |
There was a problem hiding this comment.
uh is this because w2w3 share the same param init fn? It sounds unusual to couple them together, but we can revisit this later.
| R = TypeVar("R") | ||
|
|
||
|
|
||
| class DeferredCallable(Generic[R], Configurable): |
There was a problem hiding this comment.
DeferredCallable sounds a bit contrived. I don't have a good name, but maybe we can just call it Function, in the sense that it's a generic configurable function, just like Module is a generic configurable Module.
There was a problem hiding this comment.
Callable is not a good name because it conflicts with typing.Callable. So this is the name suggested by Claude.
| cfg = replace(cfg, feed_forward=None) | ||
| else: | ||
| cfg = replace(cfg, moe=None) | ||
| resolve_deferred(cfg, layer_id) |
There was a problem hiding this comment.
Oh I think I understood the design.
Right now you are doing:
- At initial config construction time:
- create a single layer config (pending expansion) INSIDE the original model config
- assign (deferred) init function
_LINEAR_DEPTH_INITto each module during
- call
expand_layer_configswhich- expand single layer config to multi-layer
- calls
resolve_deferredto resolve the init function onto each layer's module
I think it has two minor issues:
- the single-layer config gets exposed into final official config, which is unnecessary.
- the deferred init is unnecessarily indirect, and seems overfitting torchtitan's existing depth-init paradigm.
- I don't know if this "depth init" is a good practice or not, it was introduced by someone and never changed, so we probably shouldn't overfit.
- If users want something different, not based on layer-id, but e.g. based on if this transformer block has MoE or not (I made this up), then
resolve_deferredbecomes useless to them and they have to changeexpand_layer_configs-- not that they shouldn't, but I think we don't have to program this way.
Concretely I would suggest the following:
- Move config.layer out from
Decoder.Config. We can first constructLlama4TransformerBlockas the thing to be expanded (if that's more convenient than passing dict around), send it toexpand_layer_configsto construct alayersstacked transformer block, and use the result to constructLlama4Model. - Don't assign
param_initon transformer blocks before layer expansion -- e.g. we can hardcodeparam_initfor each module inside the for-loop of layer expansion, as we are anyways providing one expansion function for each model.
| top_k=1, | ||
| num_shared_experts=1, | ||
| ) | ||
| return Llama4Model.Config( |
There was a problem hiding this comment.
nit: if you are already doing "first define the shared args, then init the actual config", then it may make sense to move 2nd step out so that all models can share the Llama4Model.Config(...) code. But yeah, that's a step further into "config-of-config" land.
|
Claude finished @tianyu-l's task in 5m 40s —— View job Review of Latest Commit & Evaluation of @tianyu-l's Comments
Latest Commit ReviewThe latest commit ( Evaluation of @tianyu-l's Latest Review Comments1. Rename
|
|
Yes, I don't like the idea of some configurations becoming obsolete later. However, this make the config easier to read in I'll change |
…orch#2633) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2785 * pytorch#2780 * __->__ pytorch#2633 ## Motivation Parameter initialization should be configurable without modifying module code. Previously, each module defined its own `init_weights` method with hardcoded init logic (e.g., `nn.init.trunc_normal_` with a fixed std). This couples initialization strategy to module definitions, making it impossible to change init behavior without editing model code. ## Design - **Expand the config to be per-module config** - `param_init` is a `dict[str, Callable]` on `Module.Config`, mapping local parameter names to init functions. It is set on every sub-config in the model config registry. - `init_states` auto-recurses the module tree (children first, then self), calling `_init_self_parameters` and `_init_self_buffers` on each module. Buffer initialization remains module-specific (e.g., RoPE cache, MoE counters) via `_init_self_buffers` overrides. - `DeferredCallable.Config` wraps init functions that vary by layer depth (e.g., depth-scaled std). `resolve_deferred` walks the config tree and resolves these markers during per-layer config expansion. - `expand_layer_configs` (per model) deep-copies the layer template N times, resolves deferred markers, and handles model-specific layer interleaving (MoE vs dense, iRoPE, sliding attention). ## Summary of changes - Remove all `init_weights` methods from modules (Linear, Embedding, RMSNorm, FeedForward, GQAttention, MoE, GroupedExperts, TokenChoiceTopKRouter, RoPE, Decoder). - Add `param_init` field to `Module.Config`; `Config.build()` propagates it to the instance as `_param_init`. - Add `init_states` with auto-recursion, `_init_self_parameters` (dict-lookup init), and `_init_self_buffers` (module-specific buffer init). - Add `DeferredCallable` (in `torchtitan/config/`) for depth-varying init. - Add `param_init.py` (in `torchtitan/models/common/`) with `resolve_deferred`, `skip_param_init`, and `depth_scaled_std` utilities. - Remove `init_mean`/`init_std` fields from `Linear.Config` and `Embedding.Config` — init is now expressed entirely through `param_init` dicts. - Add `layers` list and `expand_layer_configs()` pattern to all model registries, replacing per-layer config construction in `Decoder.__init__`. - Add `w1`/`w2w3` fields to `FeedForward.Config` and `wqkv`/`wo` fields to `GQAttention.Config` (replacing `linear_bias: bool`), so each linear sub-module gets its own `param_init`. ## Verification Bitwise identical loss against `origin/main` with `--debug.seed=42 --debug.deterministic`: | Model | Steps | Result | |--------|-------|-------------------| | Llama3 | 10 | Bitwise identical |
…orch#2633) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * pytorch#2785 * pytorch#2780 * __->__ pytorch#2633 ## Motivation Parameter initialization should be configurable without modifying module code. Previously, each module defined its own `init_weights` method with hardcoded init logic (e.g., `nn.init.trunc_normal_` with a fixed std). This couples initialization strategy to module definitions, making it impossible to change init behavior without editing model code. ## Design - **Expand the config to be per-module config** - `param_init` is a `dict[str, Callable]` on `Module.Config`, mapping local parameter names to init functions. It is set on every sub-config in the model config registry. - `init_states` auto-recurses the module tree (children first, then self), calling `_init_self_parameters` and `_init_self_buffers` on each module. Buffer initialization remains module-specific (e.g., RoPE cache, MoE counters) via `_init_self_buffers` overrides. - `DeferredCallable.Config` wraps init functions that vary by layer depth (e.g., depth-scaled std). `resolve_deferred` walks the config tree and resolves these markers during per-layer config expansion. - `expand_layer_configs` (per model) deep-copies the layer template N times, resolves deferred markers, and handles model-specific layer interleaving (MoE vs dense, iRoPE, sliding attention). ## Summary of changes - Remove all `init_weights` methods from modules (Linear, Embedding, RMSNorm, FeedForward, GQAttention, MoE, GroupedExperts, TokenChoiceTopKRouter, RoPE, Decoder). - Add `param_init` field to `Module.Config`; `Config.build()` propagates it to the instance as `_param_init`. - Add `init_states` with auto-recursion, `_init_self_parameters` (dict-lookup init), and `_init_self_buffers` (module-specific buffer init). - Add `DeferredCallable` (in `torchtitan/config/`) for depth-varying init. - Add `param_init.py` (in `torchtitan/models/common/`) with `resolve_deferred`, `skip_param_init`, and `depth_scaled_std` utilities. - Remove `init_mean`/`init_std` fields from `Linear.Config` and `Embedding.Config` — init is now expressed entirely through `param_init` dicts. - Add `layers` list and `expand_layer_configs()` pattern to all model registries, replacing per-layer config construction in `Decoder.__init__`. - Add `w1`/`w2w3` fields to `FeedForward.Config` and `wqkv`/`wo` fields to `GQAttention.Config` (replacing `linear_bias: bool`), so each linear sub-module gets its own `param_init`. ## Verification Bitwise identical loss against `origin/main` with `--debug.seed=42 --debug.deterministic`: | Model | Steps | Result | |--------|-------|-------------------| | Llama3 | 10 | Bitwise identical |
Stack from ghstack (oldest at bottom):
Motivation
Parameter initialization should be configurable without modifying module code. Previously, each module defined its own
init_weightsmethod with hardcoded init logic (e.g.,nn.init.trunc_normal_with a fixed std). This couples initialization strategy to module definitions, making it impossible to change init behavior without editing model code.Design
param_initis adict[str, Callable]onModule.Config, mapping local parameter names to init functions. It is set on every sub-config in the model config registry.init_statesauto-recurses the module tree (children first, then self), calling_init_self_parametersand_init_self_bufferson each module. Buffer initialization remains module-specific (e.g., RoPE cache, MoE counters) via_init_self_buffersoverrides.DeferredCallable.Configwraps init functions that vary by layer depth (e.g., depth-scaled std).resolve_deferredwalks the config tree and resolves these markers during per-layer config expansion.expand_layer_configs(per model) deep-copies the layer template N times, resolves deferred markers, and handles model-specific layer interleaving (MoE vs dense, iRoPE, sliding attention).Summary of changes
init_weightsmethods from modules (Linear, Embedding, RMSNorm, FeedForward, GQAttention, MoE, GroupedExperts, TokenChoiceTopKRouter, RoPE, Decoder).param_initfield toModule.Config;Config.build()propagates it to the instance as_param_init.init_stateswith auto-recursion,_init_self_parameters(dict-lookup init), and_init_self_buffers(module-specific buffer init).DeferredCallable(intorchtitan/config/) for depth-varying init.param_init.py(intorchtitan/models/common/) withresolve_deferred,skip_param_init, anddepth_scaled_stdutilities.init_mean/init_stdfields fromLinear.ConfigandEmbedding.Config— init is now expressed entirely throughparam_initdicts.layerslist andexpand_layer_configs()pattern to all model registries, replacing per-layer config construction inDecoder.__init__.w1/w2w3fields toFeedForward.Configandwqkv/wofields toGQAttention.Config(replacinglinear_bias: bool), so each linear sub-module gets its ownparam_init.Verification
Bitwise identical loss against
origin/mainwith--debug.seed=42 --debug.deterministic: