MCPcopy Index your code
hub / github.com/ml-explore/mlx-examples / load

Function load

lora/utils.py:129–172  ·  view source on GitHub ↗
(path_or_hf_repo: str, tokenizer_config={})

Source from the content-addressed store, hash-verified

127
128
129def load(path_or_hf_repo: str, tokenizer_config={}):
130 # If the path exists, it will try to load model form it
131 # otherwise download and cache from the hf_repo and cache
132 model_path = Path(path_or_hf_repo)
133 if not model_path.exists():
134 model_path = Path(
135 snapshot_download(
136 repo_id=path_or_hf_repo,
137 allow_patterns=["*.json", "*.safetensors", "tokenizer.model"],
138 )
139 )
140
141 with open(model_path / "config.json", "r") as f:
142 config = json.loads(f.read())
143 quantization = config.get("quantization", None)
144
145 weight_files = glob.glob(str(model_path / "*.safetensors"))
146 if len(weight_files) == 0:
147 raise FileNotFoundError("No safetensors found in {}".format(model_path))
148
149 weights = {}
150 for wf in weight_files:
151 weights.update(mx.load(wf).items())
152
153 model_args = models.ModelArgs.from_dict(config)
154 model = models.Model(model_args)
155 if quantization is not None:
156 class_predicate = (
157 lambda p, m: isinstance(m, (nn.Linear, nn.Embedding))
158 and f"{p}.scales" in weights
159 )
160 nn.quantize(
161 model,
162 **quantization,
163 class_predicate=class_predicate,
164 )
165
166 model.load_weights(list(weights.items()))
167
168 mx.eval(model.parameters())
169 tokenizer = transformers.AutoTokenizer.from_pretrained(
170 model_path, **tokenizer_config
171 )
172 return model, tokenizer, config
173
174
175def generate(

Callers

nothing calls this directly

Calls 5

itemsMethod · 0.80
updateMethod · 0.45
from_dictMethod · 0.45
quantizeMethod · 0.45
from_pretrainedMethod · 0.45

Tested by

no test coverage detected