1313
1414from .config import StreamingConfig
1515from .layer_loader import RollingWindowLoader
16+ from .split_model import ensure_streaming_layout
1617from .wrapper import StreamingModelWrapper
1718
1819
@@ -44,7 +45,8 @@ def load_streaming(
4445 streaming_config : Optional [StreamingConfig ] = None ,
4546 tokenizer_config : Optional [dict ] = None ,
4647 revision : Optional [str ] = None ,
47- ) -> Tuple [StreamingModelWrapper , TokenizerWrapper , Dict ]:
48+ load_tokenizer : bool = True ,
49+ ) -> Tuple [StreamingModelWrapper , Optional [TokenizerWrapper ], Dict ]:
4850 """
4951 Load a model for layer-streaming inference.
5052
@@ -67,22 +69,18 @@ def load_streaming(
6769 model_path = _download (path_or_hf_repo , revision = revision )
6870 config = load_config (model_path )
6971
72+ ensure_streaming_layout (model_path , verbose = streaming_config .verbose )
73+
7074 model_class , model_args_class = _get_classes (config )
7175 model_args = model_args_class .from_dict (config )
7276 model = model_class (model_args )
7377
7478 fixed_file = model_path / "fixed_weights.safetensors"
75- if fixed_file .exists ():
76- fixed_weights = mx .load (str (fixed_file ))
77- if hasattr (model , "sanitize" ):
78- fixed_weights = model .sanitize (fixed_weights )
79- model .load_weights (list (fixed_weights .items ()), strict = False )
80- mx .eval (model .parameters ())
81- else :
82- raise FileNotFoundError (
83- f"{ fixed_file } not found. Run: python -m mlx_lm.streaming.split_model "
84- f"<model.safetensors> { model_path } "
85- )
79+ fixed_weights = mx .load (str (fixed_file ))
80+ if hasattr (model , "sanitize" ):
81+ fixed_weights = model .sanitize (fixed_weights )
82+ model .load_weights (list (fixed_weights .items ()), strict = False )
83+ mx .eval (model .parameters ())
8684
8785 layer_size = streaming_config .estimate_layer_memory (
8886 hidden_size = config ["hidden_size" ],
@@ -106,5 +104,7 @@ def load_streaming(
106104 wrapped = StreamingModelWrapper (model , loader )
107105 wrapped .eval ()
108106
109- tokenizer = load_tokenizer (model_path , tokenizer_config )
107+ tokenizer = (
108+ load_tokenizer (model_path , tokenizer_config ) if load_tokenizer else None
109+ )
110110 return wrapped , tokenizer , config
0 commit comments