| 273 | } |
| 274 | |
| 275 | LoadOutputTypes mlx_load_helper( |
| 276 | nb::object file, |
| 277 | std::optional<std::string> format, |
| 278 | bool return_metadata, |
| 279 | mx::StreamOrDevice s) { |
| 280 | if (!format.has_value()) { |
| 281 | std::string fname; |
| 282 | if (is_str_or_path(file)) { |
| 283 | fname = nb::cast<std::string>(nb::str(file)); |
| 284 | } else if (is_istream_object(file)) { |
| 285 | fname = nb::cast<std::string>(file.attr("name")); |
| 286 | } else { |
| 287 | throw std::invalid_argument( |
| 288 | "[load] Input must be a file-like object opened in binary mode, or string"); |
| 289 | } |
| 290 | size_t ext = fname.find_last_of('.'); |
| 291 | if (ext == std::string::npos) { |
| 292 | throw std::invalid_argument( |
| 293 | "[load] Could not infer file format from extension"); |
| 294 | } |
| 295 | format.emplace(fname.substr(ext + 1)); |
| 296 | } |
| 297 | |
| 298 | if (return_metadata && (format.value() == "npy" || format.value() == "npz")) { |
| 299 | throw std::invalid_argument( |
| 300 | "[load] metadata not supported for format " + format.value()); |
| 301 | } |
| 302 | if (format.value() == "safetensors") { |
| 303 | auto [dict, metadata] = mlx_load_safetensor_helper(file, s); |
| 304 | if (return_metadata) { |
| 305 | return std::make_pair(dict, metadata); |
| 306 | } |
| 307 | return dict; |
| 308 | } else if (format.value() == "npz") { |
| 309 | return mlx_load_npz_helper(file, s); |
| 310 | } else if (format.value() == "npy") { |
| 311 | return mlx_load_npy_helper(file, s); |
| 312 | } else if (format.value() == "gguf") { |
| 313 | auto [weights, metadata] = mlx_load_gguf_helper(file, s); |
| 314 | if (return_metadata) { |
| 315 | return std::make_pair(weights, metadata); |
| 316 | } else { |
| 317 | return weights; |
| 318 | } |
| 319 | } else { |
| 320 | throw std::invalid_argument("[load] Unknown file format " + format.value()); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /////////////////////////////////////////////////////////////////////////////// |
| 325 | // Saving |
nothing calls this directly
no test coverage detected