Skip to content

Commit bf2d3dc

Browse files
committed
Merge NNX logic into checkpoint_conversion/inspect_checkpoint.py
1 parent 99fcce5 commit bf2d3dc

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

src/maxtext/checkpoint_conversion/inspect_checkpoint.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
import absl
6969
from maxtext.inference.inference_utils import str2bool
7070
from maxtext.checkpoint_conversion.utils.utils import print_peak_memory
71+
from maxtext.utils.model_creation_utils import create_nnx_abstract_model
72+
from flax import nnx
7173

7274

7375
def natural_sort_key(s: str):
@@ -238,15 +240,19 @@ def inspect_maxtext(args, remaining_args):
238240
print(argv)
239241
config = pyconfig.initialize(argv)
240242

241-
print(f"\n--- Inspecting MaxText Architecture: {config.model_name} (Scan: {config.scan_layers}) ---")
243+
print(
244+
f"\n--- Inspecting MaxText Architecture: {config.model_name} "
245+
f"(scan_layers: {config.scan_layers}, enable_nnx: {config.enable_nnx}) ---"
246+
)
242247
devices_array = maxtext_utils.create_device_mesh(config)
243248
mesh = jax.sharding.Mesh(devices_array, config.mesh_axes)
244-
quant = quantizations.configure_quantization(config)
245-
model = Transformer(config, mesh=mesh, quant=quant)
246-
247-
# Extract abstract parameters. This returns a PyTree of `ShapeDtypeStruct`
248-
# objects, without materializing weights.
249-
abstract_param = maxtext_utils.get_abstract_param(model, config)
249+
if config.enable_nnx:
250+
_, abstract_model = create_nnx_abstract_model(config, mesh=mesh)
251+
_, abstract_param, _ = nnx.split(abstract_model, nnx.Param, ...)
252+
else:
253+
quant = quantizations.configure_quantization(config)
254+
model = Transformer(config, mesh=mesh, quant=quant)
255+
abstract_param = maxtext_utils.get_abstract_param(model, config)
250256

251257
# Calculate and display the total parameter count based purely on abstract shapes.
252258
num_params = max_utils.calculate_num_params_from_pytree(abstract_param)
@@ -261,8 +267,9 @@ def inspect_maxtext(args, remaining_args):
261267
for path_tuple, abstract_leaf_value in abstract_params_flat:
262268
key_parts = param_key_parts_from_path(path_tuple)
263269

264-
# Construct a MaxText-style parameter key (e.g., "params.params.layer.weight").
265-
param_key = "params." + ".".join(key_parts)
270+
# Construct a MaxText-style parameter key.
271+
key_str = ".".join(key_parts)
272+
param_key = "params." + key_str
266273

267274
shape = abstract_leaf_value.shape
268275
param_dict[param_key] = f"shape: {shape}"
@@ -294,6 +301,7 @@ def inspect_orbax(args):
294301
# Defer imports to avoid overhead when running in other modes.
295302
import orbax.checkpoint as ocp
296303
from etils import epath
304+
from maxtext.checkpoint_conversion.utils.utils import param_key_parts_from_path
297305

298306
path = epath.Path(args.path)
299307

@@ -309,9 +317,12 @@ def inspect_orbax(args):
309317
# Filter strictly for parameter keys and format them.
310318
param_dict = {}
311319
for k, v in dictionary.items():
312-
# `k` is a tuple representing the path hierarchy; join it into a single string.
313-
# `v` is a metadata object containing `.shape` and `.dtype`.
314-
param_key = ".".join(k)
320+
# `k` is a tuple representing the path hierarchy.
321+
# Pass it through param_key_parts_from_path to normalize NNX structures
322+
# (folding list indices like "0" into "layers_0" and dropping "value").
323+
key_parts = param_key_parts_from_path(k)
324+
param_key = ".".join(key_parts)
325+
315326
if not param_key.startswith("params"):
316327
continue
317328

0 commit comments

Comments
 (0)