1717from jax import numpy as jnp
1818from jax .sharding import Mesh
1919
20- from flax import linen as nn
21-
20+ from flax import nnx
2221from MaxText .common_types import Config
23- from MaxText .layers import quantizations
22+ from MaxText .layers import quantizations , nnx_wrappers
23+ from MaxText .layers .initializers import variable_to_logically_partitioned
24+
2425
26+ from typing import Optional
2527# pytype: disable=attribute-error
2628
2729
28- class SimpleDecoderLayer (nn .Module ):
30+ class SimpleDecoderLayer (nnx .Module ):
2931 """Decoder layer consisting of a single [embed, embed] weight matrix."""
3032
31- config : Config
32- mesh : Mesh
33- model_mode : str
34- quant : None | quantizations .AqtQuantization = None
33+ def __init__ (
34+ self ,
35+ config : Config ,
36+ mesh : Mesh ,
37+ model_mode : str ,
38+ rngs : nnx .Rngs ,
39+ quant : Optional [quantizations .AqtQuantization ] = None ,
40+ ) -> None :
41+
42+ self .config = config
43+ self .mesh = mesh
44+ self .model_mode = model_mode
45+ self .rngs = rngs
46+ self .quant = quant
3547
36- def setup (self ):
37- self .weight_mat = self .param (
38- "weights" ,
39- nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("embed" , "mlp" )),
40- (self .config .emb_dim , self .config .emb_dim ),
48+ init_fn = nnx .with_partitioning (nnx .initializers .lecun_normal (), sharding = ("embed" , "mlp" ), mesh = self .mesh )
49+
50+ self .weights = nnx .Param (
51+ init_fn (self .rngs .params (), (self .config .emb_dim , self .config .emb_dim )),
4152 )
4253
4354 def __call__ (
4455 self , inputs : jnp .ndarray , positions , segmentation , deterministic , model_mode , previous_chunk = None , page_state = None
4556 ):
4657 if self .config .scan_layers :
47- return inputs @ self .weight_mat .astype (inputs .dtype ), None
48- else :
49- return inputs @ self .weight_mat .astype (inputs .dtype )
58+ return inputs @ self .weights .astype (inputs .dtype ), None
59+ return inputs @ self .weights .astype (inputs .dtype )
60+
61+
62+ SimpleDecoderLayerToLinen = nnx_wrappers .to_linen_class (
63+ SimpleDecoderLayer ,
64+ base_metadata_fn = variable_to_logically_partitioned ,
65+ )
5066
5167
52- class SimpleMlpDecoderLayer (nn .Module ):
68+ class SimpleMlpDecoderLayer (nnx .Module ):
5369 """Decoder layer consisting of [embed,mlp] followed by an [mlp,embed] matmul."""
5470
55- config : Config
56- mesh : Mesh
57- model_mode : str
58- quant : None | quantizations .AqtQuantization = None
71+ def __init__ (
72+ self ,
73+ config : Config ,
74+ mesh : Mesh ,
75+ model_mode : str ,
76+ rngs : nnx .Rngs ,
77+ quant : Optional [quantizations .AqtQuantization ] = None ,
78+ ) -> None :
79+
80+ self .config = config
81+ self .mesh = mesh
82+ self .model_mode = model_mode
83+ self .rngs = rngs
84+ self .quant = quant
5985
60- def setup (self ):
61- self .ff_1 = self .param (
62- "ff_1" ,
63- nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("embed" , "mlp" )),
64- (self .config .emb_dim , self .config .mlp_dim ),
86+ init_ff1_fn = nnx .with_partitioning (nnx .initializers .lecun_normal (), sharding = ("embed" , "mlp" ), mesh = self .mesh )
87+
88+ self .ff_1 = nnx .Param (
89+ init_ff1_fn (self .rngs .params (), (self .config .emb_dim , self .config .mlp_dim )),
6590 )
66- self .ff_2 = self .param (
67- "ff_2" ,
68- nn .with_logical_partitioning (nn .initializers .lecun_normal (), ("mlp" , "embed" )),
69- (self .config .mlp_dim , self .config .emb_dim ),
91+
92+ init_ff2_fn = nnx .with_partitioning (nnx .initializers .lecun_normal (), sharding = ("mlp" , "embed" ), mesh = self .mesh )
93+
94+ self .ff_2 = nnx .Param (
95+ init_ff2_fn (self .rngs .params (), (self .config .mlp_dim , self .config .emb_dim )),
7096 )
7197
7298 def __call__ (
@@ -84,5 +110,10 @@ def __call__(
84110 output = intermediate @ self .ff_2 .astype (inputs .dtype )
85111 if self .config .scan_layers :
86112 return output , None
87- else :
88- return output
113+ return output
114+
115+
116+ SimpleMlpDecoderLayerToLinen = nnx_wrappers .to_linen_class (
117+ SimpleMlpDecoderLayer ,
118+ base_metadata_fn = variable_to_logically_partitioned ,
119+ )
0 commit comments