| 204 | } |
| 205 | |
| 206 | Expect<WASINN::ErrNo> load(WASINN::WasiNNEnvironment &Env, |
| 207 | Span<const Span<uint8_t>> Builders, WASINN::Device, |
| 208 | uint32_t &GraphId) noexcept { |
| 209 | // Add a new graph. |
| 210 | uint32_t GId = Env.newGraph(Backend::MLX); |
| 211 | auto &GraphRef = Env.NNGraph[GId].get<Graph>(); |
| 212 | if (GraphRef.EnableDebugLog) { |
| 213 | spdlog::info("[WASI-NN] MLX backend: Load."sv); |
| 214 | } |
| 215 | std::string TokenizerPath; |
| 216 | // Parse metadata. |
| 217 | if (Builders.size() <= 1) { |
| 218 | spdlog::error( |
| 219 | "[WASI-NN] MLX backend: Lack model weight or required metadata (model_type)."sv); |
| 220 | Env.deleteGraph(GId); |
| 221 | return ErrNo::InvalidArgument; |
| 222 | } |
| 223 | const std::string Metadata = std::string( |
| 224 | reinterpret_cast<char *>(Builders.back().data()), Builders.back().size()); |
| 225 | simdjson::dom::parser Parser; |
| 226 | simdjson::dom::element Doc; |
| 227 | auto ParseError = Parser.parse(Metadata).get(Doc); |
| 228 | if (ParseError) { |
| 229 | spdlog::error("[WASI-NN] MLX backend: Parse metadata error"sv); |
| 230 | Env.deleteGraph(GId); |
| 231 | return ErrNo::InvalidEncoding; |
| 232 | } |
| 233 | if (Doc.at_key("model_type").error() == simdjson::SUCCESS) { |
| 234 | std::string_view ModelType; |
| 235 | auto Err = Doc["model_type"].get<std::string_view>().get(ModelType); |
| 236 | if (Err) { |
| 237 | spdlog::error( |
| 238 | "[WASI-NN] MLX backend: Unable to retrieve the model_type option."sv); |
| 239 | Env.deleteGraph(GId); |
| 240 | return ErrNo::InvalidArgument; |
| 241 | } |
| 242 | GraphRef.ModelType = ModelType; |
| 243 | } else { |
| 244 | spdlog::error( |
| 245 | "[WASI-NN] MLX backend: Unable to retrieve the model_type option."sv); |
| 246 | Env.deleteGraph(GId); |
| 247 | return ErrNo::InvalidArgument; |
| 248 | } |
| 249 | if (Doc.at_key("enable_debug_log").error() == simdjson::SUCCESS) { |
| 250 | bool EnableDebugLog; |
| 251 | auto Err = Doc["enable_debug_log"].get<bool>().get(EnableDebugLog); |
| 252 | if (Err) { |
| 253 | spdlog::error( |
| 254 | "[WASI-NN] MLX backend: Unable to retrieve the enable_debug_log option."sv); |
| 255 | Env.deleteGraph(GId); |
| 256 | return ErrNo::InvalidArgument; |
| 257 | } |
| 258 | GraphRef.EnableDebugLog = EnableDebugLog; |
| 259 | } |
| 260 | if (Doc.at_key("tokenizer").error() == simdjson::SUCCESS) { |
| 261 | std::string_view TokenizerPathView; |
| 262 | auto Err = Doc["tokenizer"].get<std::string_view>().get(TokenizerPathView); |
| 263 | if (Err) { |
nothing calls this directly
no test coverage detected