feat: convert SimpleDecoderLayer and SimpleMlpDecoderLayer to use nnx - #1974
Conversation
38a123e to
89de7e2
Compare
SimpleDecodeLayer and SimpleMlpDecodeLayer into nnx SimpleDecoderLayer and SimpleMlpDecoderLayer into nnx
4476b54 to
eb99fd8
Compare
8a6291b to
260271c
Compare
SimpleDecoderLayer and SimpleMlpDecoderLayer into nnx SimpleDecoderLayer and SimpleMlpDecoderLayer to use nnx
1606b5a to
ece793f
Compare
ed83621 to
ead2980
Compare
6b913a3 to
5f3e877
Compare
86b4205 to
e3d58ff
Compare
e3d58ff to
aa2cd98
Compare
aa2cd98 to
7da2050
Compare
7da2050 to
c0f964c
Compare
d579507 to
0f6b007
Compare
| "weights", | ||
| nn.with_logical_partitioning(nn.initializers.lecun_normal(), ("embed", "mlp")), | ||
| (self.config.emb_dim, self.config.emb_dim), | ||
| init_fn = nnx.with_partitioning(nnx.initializers.lecun_normal(), sharding=("embed", "mlp"), mesh=self.mesh) |
There was a problem hiding this comment.
Is this needed? Not seeing it in the previous Linen version
There was a problem hiding this comment.
Thank you for asking this part. We followed the previous Linen version as a reference to current change. For example:
self.weight_mat = self.param(
"weights",
nn.with_logical_partitioning(nn.initializers.lecun_normal(), ("embed", "mlp")),
(self.config.emb_dim, self.config.emb_dim),
)Since self.param should now be replaced with nnx.Param in NNX, we updated the implementation accordingly.
We also replaced nn.with_logical_partitioning from Linen with nnx.with_partitioning from NNX. And for clarity, we moved the nnx.with_partitioning initializer wrapper ( in current case -- init_fn) as an independent variable.
If I am correct, we also have similar implemntation for self.param at different places, such as gpt3.py
Feel free to tell me if the change needs to be updated, thank you
| "ff_1", | ||
| nn.with_logical_partitioning(nn.initializers.lecun_normal(), ("embed", "mlp")), | ||
| (self.config.emb_dim, self.config.mlp_dim), | ||
| init_ff1_fn = nnx.with_partitioning(nnx.initializers.lecun_normal(), sharding=("embed", "mlp"), mesh=self.mesh) |
There was a problem hiding this comment.
Why is this and the nnx.with_partitioning below added?
There was a problem hiding this comment.
similar with answer above, thank you
NuojCheng
left a comment
There was a problem hiding this comment.
Overall LGTM, the memory usages match for both cases.
7562255 to
d2bc8f4
Compare
| rngs = nnx.Rngs(params=0) | ||
| single_pipeline_stage = simple_layer.SimpleDecoderLayerToLinen( | ||
| config=config, mesh=mesh, model_mode=model_mode, rngs=rngs | ||
| ) |
There was a problem hiding this comment.
since we update the implementation from Linen to NNX, we also need to update its related unit test
| rngs = nnx.Rngs(params=0) | ||
| single_pipeline_stage = simple_layer.SimpleDecoderLayerToLinen( | ||
| config=config, mesh=mesh, model_mode=model_mode, rngs=rngs | ||
| ) |
There was a problem hiding this comment.
since we update the implementation from Linen to NNX, we also need to update its related unit test


Description
Use NNX for the
SimpleDecodeLayerandSimpleMlpDecodeLayermodules.We also implement a function to return class of the linen instead of an instantiated Linen object.
Tests
It should work.
Environment
Machine type :
v5litepod-8TPU VMSoftware machine :
tpu-ubuntu2204-basePython Version :
3.10.12Changes
SimpleDecodeLayerBefore change to
NNXlog : https://paste.googleplex.com/4892995450830848After change to
NNXlog (train from scratch) : https://paste.googleplex.com/6179580754526208After change to
NNXlog (continue train from main branch) : https://paste.googleplex.com/6264063394578432Run command :
SimpleMlpDecodeLayerBefore change to
NNXlog : https://paste.googleplex.com/5737594159366144After change to
NNXlog (train from scratch) : https://paste.googleplex.com/5264591793422336After change to
NNXlog (continue train from main branch) : https://paste.googleplex.com/4805767982743552Run command :
Checklist
Before submitting this PR, please make sure (put X in square brackets):