Skip to content

Commit 5c5d7e1

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

1 file changed

Lines changed: 53 additions & 16 deletions

File tree

src/maxtext/checkpoint_conversion/inspect_checkpoint.py

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,21 @@ def inspect_maxtext(args, remaining_args):
238238
print(argv)
239239
config = pyconfig.initialize(argv)
240240

241-
print(f"\n--- Inspecting MaxText Architecture: {config.model_name} (Scan: {config.scan_layers}) ---")
241+
print(
242+
f"\n--- Inspecting MaxText Architecture: {config.model_name} (Scan: {config.scan_layers}) ---"
243+
)
242244
devices_array = maxtext_utils.create_device_mesh(config)
243245
mesh = jax.sharding.Mesh(devices_array, config.mesh_axes)
244-
quant = quantizations.configure_quantization(config)
245-
model = Transformer(config, mesh=mesh, quant=quant)
246+
if getattr(config, "enable_nnx", False):
247+
from maxtext.utils.model_creation_utils import create_nnx_abstract_model
248+
from flax import nnx
246249

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)
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)
@@ -262,7 +268,11 @@ def inspect_maxtext(args, remaining_args):
262268
key_parts = param_key_parts_from_path(path_tuple)
263269

264270
# Construct a MaxText-style parameter key (e.g., "params.params.layer.weight").
265-
param_key = "params." + ".".join(key_parts)
271+
key_str = ".".join(key_parts)
272+
if getattr(config, "enable_nnx", False) and not key_str.startswith("params"):
273+
param_key = "params.params." + key_str
274+
else:
275+
param_key = "params." + key_str
266276

267277
shape = abstract_leaf_value.shape
268278
param_dict[param_key] = f"shape: {shape}"
@@ -334,19 +344,35 @@ def main():
334344
# Shared parser for arguments common across all modes.
335345
shared_parser = argparse.ArgumentParser(add_help=False)
336346
shared_parser.add_argument(
337-
"--check_dtype", type=str2bool, required=False, default=False, help="Whether to append dtype info to the output"
347+
"--check_dtype",
348+
type=str2bool,
349+
required=False,
350+
default=False,
351+
help="Whether to append dtype info to the output",
338352
)
339353
shared_parser.add_argument(
340-
"--output_file", type=str, required=False, default="", help="Path to save the output structure"
354+
"--output_file",
355+
type=str,
356+
required=False,
357+
default="",
358+
help="Path to save the output structure",
341359
)
342360

343361
# Main parser and sub-parsers for distinct inspection modes.
344-
parser = argparse.ArgumentParser(description="Consolidated Model Checkpoint Inspector")
345-
subparsers = parser.add_subparsers(dest="mode", required=True, help="Inspection mode: hf, maxtext, orbax")
362+
parser = argparse.ArgumentParser(
363+
description="Consolidated Model Checkpoint Inspector"
364+
)
365+
subparsers = parser.add_subparsers(
366+
dest="mode", required=True, help="Inspection mode: hf, maxtext, orbax"
367+
)
346368

347369
# Mode 1: HuggingFace
348-
parser_hf = subparsers.add_parser("hf", parents=[shared_parser], help="Inspect .safetensors or .pth files")
349-
parser_hf.add_argument("--path", type=str, required=True, help="Directory containing checkpoint files")
370+
parser_hf = subparsers.add_parser(
371+
"hf", parents=[shared_parser], help="Inspect .safetensors or .pth files"
372+
)
373+
parser_hf.add_argument(
374+
"--path", type=str, required=True, help="Directory containing checkpoint files"
375+
)
350376
parser_hf.add_argument(
351377
"--format",
352378
type=str,
@@ -357,11 +383,22 @@ def main():
357383
)
358384

359385
# Mode 2: MaxText Architecture
360-
subparsers.add_parser("maxtext", parents=[shared_parser], help="Inspect MaxText theoretical architecture")
386+
subparsers.add_parser(
387+
"maxtext",
388+
parents=[shared_parser],
389+
help="Inspect MaxText theoretical architecture",
390+
)
361391

362392
# Mode 3: Orbax
363-
parser_orbax = subparsers.add_parser("orbax", parents=[shared_parser], help="Inspect saved Orbax checkpoint metadata")
364-
parser_orbax.add_argument("--path", type=str, required=True, help="Path to checkpoint items (local or GCS)")
393+
parser_orbax = subparsers.add_parser(
394+
"orbax", parents=[shared_parser], help="Inspect saved Orbax checkpoint metadata"
395+
)
396+
parser_orbax.add_argument(
397+
"--path",
398+
type=str,
399+
required=True,
400+
help="Path to checkpoint items (local or GCS)",
401+
)
365402

366403
args, remaining_args = parser.parse_known_args()
367404

0 commit comments

Comments
 (0)