Skip to main content

Python class

WeightPathParser

WeightPathParser

class max.pipelines.lib.WeightPathParser

source

Bases: object

Parses and validates weight paths for model configuration.

parse()

static parse(model_path, weight_path)

source

Parses weight paths and extracts any weights repo ID.

For example:

>>> WeightPathParser.parse("org/model", "path/to/weights.safetensors")
(["path/to/weights.safetensors"], None)

>>> WeightPathParser.parse("org/model", ["path/to/weights1.safetensors", "path/to/weights2.safetensors"])
(["path/to/weights1.safetensors", "path/to/weights2.safetensors"], None)

>>> WeightPathParser.parse("org/model", Path("path/to/weights.safetensors"))
([Path("path/to/weights.safetensors")], None)

>>> WeightPathParser.parse("org/model", "org/model/weights.safetensors")
([Path("weights.safetensors")], "org/model")

>>> WeightPathParser.parse("org/model", ["local_weights.safetensors", "other_org/other_model/remote_weights.safetensors"])
([Path("local_weights.safetensors"), Path("remote_weights.safetensors")], "other_org/other_model")

When the weight path does not have a Hugging Face prefix, file_exists returns False and the whole path is treated as a local path:

>>> WeightPathParser.parse("org/model", "very/nested/subfolder/another_nested/weights.safetensors")
([Path("very/nested/subfolder/another_nested/weights.safetensors")], None)

If model_path is empty and the repo ID cannot be derived, a ValueError is raised.

Parameters:

  • model_path (str) – The model path to use for parsing the weight path(s).
  • weight_path (list[Path] | list[str] | tuple[Path, ...] | tuple[str, ...] | Path | str) – The weight path(s) to parse. Can be a single path, tuple, or list.

Returns:

A tuple of (processed_weight_paths, weights_repo_id).

Raises:

ValueError – If weight paths are invalid or cannot be processed.

Return type:

tuple[list[Path], str | None]