forked from AI-Hypercomputer/maxtext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_creation_utils.py
More file actions
214 lines (176 loc) · 7.66 KB
/
Copy pathmodel_creation_utils.py
File metadata and controls
214 lines (176 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 2023–2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=bare-except, consider-using-generator
""" Utils that are only interesting for creating a model in MaxText. """
from collections.abc import Sequence
from typing import overload
from flax import nnx
import flax.linen as nn
import jax
from jax.sharding import Mesh, AxisType
from MaxText import maxtext_utils
from MaxText import pyconfig
from MaxText.layers import quantizations
from MaxText.common_types import MODEL_MODE_TRAIN, ShardMode
from MaxText.layers import models
from orbax import checkpoint as ocp
from functools import partial
from etils import epath
@overload
def from_config(
config: pyconfig.HyperParameters,
devices: Sequence[jax.Device] | None = None,
*,
model_mode: str = MODEL_MODE_TRAIN,
) -> nn.Module:
...
@overload
def from_config(
config: pyconfig.HyperParameters,
devices: Sequence[jax.Device] | None = None,
*,
model_mode: str = MODEL_MODE_TRAIN,
rngs: nnx.Rngs,
) -> models.Transformer:
...
def from_config(
config: pyconfig.HyperParameters,
devices: Sequence[jax.Device] | None = None,
*,
model_mode: str = MODEL_MODE_TRAIN,
rngs: nnx.Rngs | None = None,
) -> nn.Module | models.Transformer:
"""Load a pretrained MaxText model from checkpoint.
This function loads a model from a checkpoint.
Args:
config: Config object.
devices: Sequence of devices to use for the model. If None, use all
available devices.
Returns:
Transformer: The loaded model instance (only the model)
Example:
model = from_config(config)
"""
rngs = rngs or nnx.Rngs(params=jax.random.PRNGKey(config.init_weights_seed), dropout=1)
devices_array = maxtext_utils.create_device_mesh(config, devices)
if config.shard_mode == ShardMode.EXPLICIT:
axis_types = tuple([AxisType.Explicit] * len(config.mesh_axes))
else:
axis_types = tuple([AxisType.Auto] * len(config.mesh_axes))
mesh = Mesh(devices_array, config.mesh_axes, axis_types=axis_types)
model = create_model(config, mesh, model_mode=model_mode, rngs=rngs)
# Return only the model
return model
def get_transformer_model(
config, mesh, quant, rngs: nnx.Rngs | None = None, model_mode: str = MODEL_MODE_TRAIN
) -> nn.Module:
"""Returns the transformer model based on the configuration."""
# TODO: use nnx model instead of flax linen model
if config.model_fsdp_ag_once:
if rngs is not None:
return models.ZeroOneTransformer(config, mesh, quant=quant, model_mode=model_mode, rngs=rngs)
return models.zero_one_transformer_as_linen(config, mesh, quant=quant, model_mode=model_mode, rngs=rngs)
else:
if rngs is not None:
return models.Transformer(config, mesh, quant=quant, model_mode=model_mode, rngs=rngs)
return models.transformer_as_linen(config, mesh, quant=quant, model_mode=model_mode, rngs=rngs)
def create_model(config, mesh, model_mode: str = MODEL_MODE_TRAIN, rngs: nnx.Rngs | None = None):
"""Instantiates and returns the model object, sharded across the mesh."""
# Model definition
quant = quantizations.configure_quantization(config)
model = get_transformer_model(config, mesh, quant, model_mode=model_mode, rngs=rngs)
model = quantizations.maybe_quantize_model(model, config)
return model
def create_nnx_model(config, devices=None):
"""Creates a NNX model with sharded parameters, possibly loading from a checkpoint."""
def _create_model():
init_rng = jax.random.PRNGKey(config.init_weights_seed)
return from_config(config, devices, rngs=nnx.Rngs(params=init_rng, dropout=1))
abstract_model = nnx.eval_shape(_create_model)
graphdef, abstract_state = nnx.split(abstract_model)
specs = nnx.get_partition_spec(abstract_state)
mesh = abstract_model.mesh
# JIT a function that creates the model state with proper sharding from the start.
# By providing out_shardings, we instruct JAX to produce sharded output directly,
# avoiding a large intermediate allocation on a single device.
with nn.logical_axis_rules(config.logical_axis_rules):
out_shardings = nn.logical_to_mesh_sharding(specs, mesh)
@partial(jax.jit, out_shardings=out_shardings)
def create_sharded_state():
# This will be JIT-compiled. JAX knows the output sharding and can
# initialize the parameters directly on the target devices in a sharded way.
model = _create_model()
return nnx.state(model)
with mesh:
# Create the model with sharded parameters.
sharded_state = create_sharded_state()
model = nnx.merge(graphdef, sharded_state)
if config.load_parameters_path:
try:
ckptr = ocp.Checkpointer(
ocp.PyTreeCheckpointHandler(
restore_concurrent_gb=config.checkpoint_storage_concurrent_gb,
save_concurrent_gb=config.checkpoint_storage_concurrent_gb,
use_ocdbt=config.checkpoint_storage_use_ocdbt,
use_zarr3=config.checkpoint_storage_use_zarr3,
)
)
# This is a memory optimization. We don't want to restore the entire checkpoint - only the params.
# Rather than passing the entire abstract state, which could unnecessarily restore opt_state and
# waste memory, we instead restore the params field of the checkpoint (which itself may be a dictionary
# containing a key named 'params').
# Get the structure of checkpoint in `config.load_parameters_path`
metadata = ckptr.metadata(config.load_parameters_path)
is_nnx_checkpoint = True
if (
"params" in metadata.item_metadata.tree.keys()
and "params" in metadata.item_metadata.tree.get("params", {}).keys()
):
# structure of linen checkpoint: {'params': {'params': {'decoder': ...}}}
is_nnx_checkpoint = False
target_for_restore = jax.tree.map(
lambda v: v.value,
sharded_state,
is_leaf=lambda n: hasattr(n, "value"),
)
item_to_restore = {"params": {"params": target_for_restore}}
restore_args = {"params": {"params": ocp.checkpoint_utils.construct_restore_args(target_for_restore)}}
else:
# structure of nnx checkpoint: {'decoder': {'value': ...}}
target_for_restore = jax.tree.map(
lambda v: {"value": v.value},
sharded_state,
is_leaf=lambda n: isinstance(n, nnx.Variable),
)
item_to_restore = target_for_restore
restore_args = ocp.checkpoint_utils.construct_restore_args(target_for_restore)
restored = ckptr.restore(
epath.Path(config.load_parameters_path),
item=item_to_restore,
transforms={},
restore_args=restore_args,
)
if is_nnx_checkpoint:
checkpoint = jax.tree.map(
lambda v: v["value"],
restored,
is_leaf=lambda x: isinstance(x, dict) and "value" in x and not isinstance(x.get("value"), dict),
)
else:
checkpoint = restored["params"]["params"]
if checkpoint:
nnx.update(model, checkpoint)
except Exception as e:
raise ValueError(f"Checkpoint loading failed: {e}") from e
return model, mesh