Skip to content

Commit 0a822bd

Browse files
committed
Remove MLX runtime fallback and debug logging
1 parent 5f455a2 commit 0a822bd

2 files changed

Lines changed: 1 addition & 91 deletions

File tree

backends/mlx/examples/llm/run_llm_hf.py

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@
2121
"""
2222

2323
import argparse
24-
import ctypes
2524
import logging
26-
import os
27-
import shutil
2825
import time
29-
from pathlib import Path
3026

3127
import torch
3228
from executorch.runtime import Runtime, Verification
@@ -37,73 +33,6 @@
3733
logger = logging.getLogger(__name__)
3834

3935

40-
def _iter_mlx_backend_candidates():
41-
env_path = os.environ.get("ET_MLX_BACKEND_DYLIB")
42-
if env_path:
43-
yield Path(env_path)
44-
45-
for parent in Path(__file__).resolve().parents:
46-
pip_out = parent / "pip-out"
47-
if pip_out.exists():
48-
yield from sorted(
49-
pip_out.glob(
50-
"temp.*/cmake-out/backends/mlx/libmlxdelegate_runtime.dylib"
51-
)
52-
)
53-
yield from sorted(
54-
pip_out.glob("temp.*/cmake-out/backends/mlx/libmlxdelegate.dylib")
55-
)
56-
break
57-
58-
59-
def _ensure_mlx_metallib(dylib_path: Path) -> None:
60-
metallib_path = dylib_path.with_name("mlx.metallib")
61-
if metallib_path.exists():
62-
return
63-
64-
for parent in Path(__file__).resolve().parents:
65-
pip_out = parent / "pip-out"
66-
if not pip_out.exists():
67-
continue
68-
matches = sorted(
69-
pip_out.glob(
70-
"temp.*/cmake-out/backends/mlx/mlx/mlx/backend/metal/kernels/mlx.metallib"
71-
)
72-
)
73-
if not matches:
74-
continue
75-
shutil.copyfile(matches[0], metallib_path)
76-
logger.info(f"Copied MLX metallib next to runtime library: {metallib_path}")
77-
return
78-
79-
80-
def _ensure_mlx_backend_registered() -> Runtime:
81-
runtime = Runtime.get()
82-
if runtime.backend_registry.is_available("MLXBackend"):
83-
return runtime
84-
85-
for candidate in _iter_mlx_backend_candidates():
86-
if not candidate.is_file():
87-
continue
88-
try:
89-
_ensure_mlx_metallib(candidate)
90-
ctypes.CDLL(str(candidate), mode=ctypes.RTLD_GLOBAL)
91-
except OSError as exc:
92-
logger.info(f"Failed to load MLX backend library {candidate}: {exc}")
93-
continue
94-
95-
runtime = Runtime.get()
96-
if runtime.backend_registry.is_available("MLXBackend"):
97-
logger.info(f"Loaded MLX backend runtime library: {candidate}")
98-
return runtime
99-
100-
logger.warning(
101-
"MLXBackend is not registered. If you built mlxdelegate locally, "
102-
"set ET_MLX_BACKEND_DYLIB to the path of libmlxdelegate_runtime.dylib."
103-
)
104-
return runtime
105-
106-
10736
def _get_max_input_seq_len(program) -> int:
10837
"""Inspect the .pte program metadata to determine the max input_ids seq len.
10938
@@ -172,7 +101,7 @@ def run_inference(
172101
text_processor, uses_processor = _load_text_processor(model_id)
173102

174103
logger.info(f"Loading model from {pte_path}...")
175-
et_runtime = _ensure_mlx_backend_registered()
104+
et_runtime = Runtime.get()
176105
program = et_runtime.load_program(pte_path, verification=Verification.Minimal)
177106

178107
max_seq_len = _get_max_input_seq_len(program)

backends/mlx/runtime/MLXBackend.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <mlx/mlx.h>
2020

2121
#include <cstring>
22-
#include <iostream>
2322
#include <limits>
2423
#include <memory>
2524
#include <mutex>
@@ -209,18 +208,14 @@ class MLXBackend final : public ::executorch::runtime::BackendInterface {
209208
}
210209

211210
try {
212-
std::cerr << "MLX init: constructing handle" << std::endl;
213211
new (handle) MLXHandle();
214-
std::cerr << "MLX init: handle constructed" << std::endl;
215212

216213
if (!processed || !processed->data() || processed->size() == 0) {
217214
throw std::runtime_error("init: null or empty delegate payload");
218215
}
219216

220-
std::cerr << "MLX init: parsing delegate payload" << std::endl;
221217
handle->program = loader::load_program(
222218
static_cast<const uint8_t*>(processed->data()), processed->size());
223-
std::cerr << "MLX init: delegate payload parsed" << std::endl;
224219

225220
// Validate schema version
226221
int schema_version = 1;
@@ -248,34 +243,27 @@ class MLXBackend final : public ::executorch::runtime::BackendInterface {
248243
// runtime
249244
const runtime::NamedDataMap* named_data_map =
250245
context.get_named_data_map();
251-
std::cerr << "MLX init: loading constants" << std::endl;
252246
load_constants(
253247
handle->program,
254248
named_data_map,
255249
handle->constants,
256250
handle->constant_buffers);
257-
std::cerr << "MLX init: constants loaded" << std::endl;
258251

259252
// Delegate payload no longer needed after constants are loaded
260253
processed->Free();
261254
processed = nullptr;
262255

263256
// Load mutable buffers (e.g., KV cache)
264-
std::cerr << "MLX init: loading mutable buffers" << std::endl;
265257
load_mutable_buffers(handle->program, handle->mutable_buffers);
266-
std::cerr << "MLX init: mutable buffers loaded" << std::endl;
267258

268259
// Bind execution state (reused across execute() calls)
269-
std::cerr << "MLX init: binding execution state" << std::endl;
270260
handle->state.bind(
271261
handle->program, handle->constants, handle->mutable_buffers);
272-
std::cerr << "MLX init: execution state bound" << std::endl;
273262

274263
// Run init chain if present.
275264
// SAFETY: The >= 0 check ensures init_chain_idx is non-negative, so the
276265
// static_cast<uint32_t> cannot produce UINT32_MAX from a -1 sentinel.
277266
if (handle->program.init_chain_idx >= 0) {
278-
std::cerr << "MLX init: running init chain" << std::endl;
279267
handle->state.is_init_chain = true;
280268
handle->interpreter.run_chain(
281269
handle->program,
@@ -287,21 +275,17 @@ class MLXBackend final : public ::executorch::runtime::BackendInterface {
287275
// Evaluate any constants written by the init chain so the first
288276
// execute() doesn't pay the cost of materializing them.
289277
eval(handle->constants.tensors);
290-
std::cerr << "MLX init: init chain complete" << std::endl;
291278
}
292279

293280
} catch (const std::exception& e) {
294281
ET_LOG(Error, "Failed to load MLX program: %s", e.what());
295-
std::cerr << "Failed to load MLX program: " << e.what() << std::endl;
296282
handle->~MLXHandle();
297283
if (processed != nullptr) {
298284
processed->Free();
299285
}
300286
return Error::InvalidProgram;
301287
} catch (...) {
302288
ET_LOG(Error, "Failed to load MLX program: unknown non-std exception");
303-
std::cerr << "Failed to load MLX program: unknown non-std exception"
304-
<< std::endl;
305289
handle->~MLXHandle();
306290
if (processed != nullptr) {
307291
processed->Free();
@@ -437,12 +421,9 @@ class MLXBackend final : public ::executorch::runtime::BackendInterface {
437421
return Error::Ok;
438422
} catch (const std::exception& e) {
439423
ET_LOG(Error, "MLX execute failed: %s", e.what());
440-
std::cerr << "MLX execute failed: " << e.what() << std::endl;
441424
return Error::Internal;
442425
} catch (...) {
443426
ET_LOG(Error, "MLX execute failed: unknown non-std exception");
444-
std::cerr << "MLX execute failed: unknown non-std exception"
445-
<< std::endl;
446427
return Error::Internal;
447428
}
448429
}

0 commit comments

Comments
 (0)