From 2cd60770f45de048517322a34ee0cfac8a6395b0 Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 3 Nov 2026 13:48:38 +0100 Subject: [PATCH 1/2] fix: Support named hooks without caching runtime callbacks Apply named hook overrides and defaults, including inside native while loops Keep callback-bearing executables out of global caches Safely abandon runtime calls when callers exit and reject late replies --- emlx/c_src/emlx_runtime_call_bridge.hpp | 53 ++- emlx/lib/emlx.ex | 90 +++-- emlx/lib/emlx/compile_cache.ex | 9 +- emlx/lib/emlx/native/expr.ex | 51 ++- emlx/test/emlx/named_hooks_test.exs | 328 ++++++++++++++++++ emlx/test/emlx/native/expr_test.exs | 8 +- emlx/test/fixtures/runtime_call_lifecycle.exs | 127 +++++++ 7 files changed, 601 insertions(+), 65 deletions(-) create mode 100644 emlx/test/emlx/named_hooks_test.exs create mode 100644 emlx/test/fixtures/runtime_call_lifecycle.exs diff --git a/emlx/c_src/emlx_runtime_call_bridge.hpp b/emlx/c_src/emlx_runtime_call_bridge.hpp index 1f1cca7..e77e3dc 100644 --- a/emlx/c_src/emlx_runtime_call_bridge.hpp +++ b/emlx/c_src/emlx_runtime_call_bridge.hpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,28 @@ inline void invoke_runtime_call(int64_t callback_index, } new (pending) PendingRuntimeCall(); + // The worker must own a reference while waiting: the recipient may die or + // discard the message at any point. Also invalidate destinations on every + // exit path, so a delayed/duplicate reply can never write freed output memory. + auto release_pending = [](PendingRuntimeCall *p) { + { + std::lock_guard lock(p->mu); + if (!p->done) { + p->done = true; + p->ok = false; + p->error = "runtime_call abandoned"; + } + p->outputs.clear(); + } + p->cv.notify_one(); + enif_release_resource(p); + }; + std::unique_ptr owner(pending, release_pending); + std::unique_ptr alive_env(enif_alloc_env(), enif_free_env); + if (!alive_env) { + throw std::runtime_error("emlx::native: failed to allocate liveness env"); + } + if (!alias_inputs) { // Pre-allocate every output buffer now so the destination pointers are // stable before we hand the request to the Elixir side. @@ -154,7 +177,6 @@ inline void invoke_runtime_call(int64_t callback_index, ErlNifEnv *msg_env = enif_alloc_env(); if (msg_env == nullptr) { - enif_release_resource(pending); throw std::runtime_error("emlx::native: failed to allocate msg env for runtime_call"); } @@ -174,7 +196,6 @@ inline void invoke_runtime_call(int64_t callback_index, size_t nbytes = in.nbytes(); ErlNifBinary bin; if (!enif_alloc_binary(nbytes, &bin)) { - enif_release_resource(pending); enif_free_env(msg_env); throw std::runtime_error( "emlx::native: failed to allocate binary for runtime_call arg"); @@ -195,11 +216,7 @@ inline void invoke_runtime_call(int64_t callback_index, } ERL_NIF_TERM pending_term = enif_make_resource(msg_env, pending); - // enif_make_resource/enif_make_copy below keep their own references; drop - // ours now — the resource stays alive via the message term (and later, - // whatever term resolve_runtime_call's caller decodes it into) until its - // own destructor runs. - enif_release_resource(pending); + // Keep the worker-owned reference until this invocation has unwound. ERL_NIF_TERM args_list = enif_make_list_from_array( msg_env, arg_terms.data(), static_cast(arg_terms.size())); @@ -218,8 +235,14 @@ inline void invoke_runtime_call(int64_t callback_index, "emlx::native: failed to send runtime_call request (target process not alive?)"); } - auto is_done = [pending] { + auto is_done = [pending, &alive_env, &caller_pid] { std::lock_guard lock(pending->mu); + if (!pending->done && !enif_is_process_alive(alive_env.get(), &caller_pid)) { + pending->done = true; + pending->ok = false; + pending->error = "runtime_call caller exited"; + pending->outputs.clear(); + } return pending->done; }; @@ -233,8 +256,10 @@ inline void invoke_runtime_call(int64_t callback_index, if (Worker *worker = g_current_worker) { worker->pump_until(is_done); } else { - std::unique_lock lock(pending->mu); - pending->cv.wait(lock, [pending] { return pending->done; }); + while (!is_done()) { + std::unique_lock lock(pending->mu); + pending->cv.wait_for(lock, std::chrono::milliseconds(5)); + } } std::lock_guard lock(pending->mu); @@ -244,8 +269,8 @@ inline void invoke_runtime_call(int64_t callback_index, if (alias_inputs) { size_t passthrough_start = inputs.size() - outputs.size(); - for (size_t i = passthrough_start; i < outputs.size(); i++) { - outputs[i].copy_shared_buffer(inputs[i]); + for (size_t i = 0; i < outputs.size(); i++) { + outputs[i].copy_shared_buffer(inputs[passthrough_start + i]); } } } @@ -280,6 +305,10 @@ inline ERL_NIF_TERM resolve_runtime_call(ErlNifEnv *env, int argc, { std::lock_guard lock(pending->mu); + if (pending->done) { + return nx::nif::error(env, "runtime_call already resolved or abandoned"); + } + if (is_ok) { unsigned n; if (!enif_get_list_length(env, argv[2], &n)) { diff --git a/emlx/lib/emlx.ex b/emlx/lib/emlx.ex index 6453e88..1949681 100644 --- a/emlx/lib/emlx.ex +++ b/emlx/lib/emlx.ex @@ -146,11 +146,11 @@ defmodule EMLX do ## Compile once, replay many times - `Nx.Defn.jit/2` and `Nx.Defn.jit_apply/3` still retrace through Nx on every + For callback-free functions, `Nx.Defn.jit/2` and `Nx.Defn.jit_apply/3` retrace through Nx on every call, but `EMLX.__compile__/4` caches the resulting eval closure in `EMLX.CompileCache`, keyed by function identity, input templates, and device. Repeated jit of the same `&Mod.f/a` with the same shapes hits that - cache and skips `native_compile/3`. + cache and skips native compilation. For a hot loop (e.g. a decode step), prefer `Nx.Defn.compile/3`, which traces and lowers **once** and returns a plain closure the caller holds — no ETS @@ -172,6 +172,23 @@ defmodule EMLX do Cache bounds are documented on `EMLX.CompileCache`. A caller that keeps the `Nx.Defn.compile/3` closure can pass `cache: false` to bypass the cache mechanism. + ## Named hooks + + The compiler's `:hooks` map overrides named `hook` callbacks on Nx 0.13 and + named `io_call` callbacks on Nx 1.x. Missing, nil, or false overrides use the + inline default; a name without a default is a no-op. Matching overrides must + be functions of arity one. Hooks in native while bodies run per iteration. + + Compilations with nonempty `:hooks` and executables containing runtime + callbacks bypass global caches, including the structural program cache. + This keeps request-specific callback closures and receiver aliases out of + process-lifetime storage. A caller-held `Nx.Defn.compile/3` executable still + supports repeated invocation with its own callbacks. Side effects inside + conditional branches remain unsupported. + + If a callback caller dies, the native bridge abandons its pending callback + wait and rejects late replies. This does not preempt a running MLX kernel. + ## Compile-time debug flags Several development-only checks are gated by `Application.compile_env/3` @@ -1805,6 +1822,8 @@ defmodule EMLX do end) rescue e -> {:error, Exception.format(:error, e, __STACKTRACE__)} + catch + kind, reason -> {:error, Exception.format(kind, reason, __STACKTRACE__)} end case reply do @@ -1849,7 +1868,7 @@ defmodule EMLX do @valid_compiler_keys [:device, :max_concurrency, :command_queue, :hooks, :cache] - # Structural program cache for `get_or_compile_program/6`. + # Structural program cache for callback-free programs. @native_dispatch_cache_table :emlx_native_dispatch_cache # Expr-id cache in front of `dispatch_key/3`. @@ -1873,26 +1892,17 @@ defmodule EMLX do hooks = Keyword.get(opts, :hooks, %{}) - case hooks do - empty when empty == %{} -> - :ok - - _ -> - raise ArgumentError, - "EMLX does not support the :hooks named-override map (got callbacks for " <> - "#{inspect(Map.keys(hooks))}) — :hook/:io_call expr nodes lower natively " <> - "with their own inline default callback, but swapping in a different " <> - "callback per hook name via compiler opts isn't wired through" - end + unless is_map(hooks), do: raise(ArgumentError, ":hooks option must be a map") queue = Keyword.get(opts, :command_queue) device = Keyword.get(opts, :device, default_device()) eval_fn = - if Keyword.get(opts, :cache, true) do + if Keyword.get(opts, :cache, true) and hooks == %{} do cached_compile(compile_cache_key(key, vars, hooks, device), vars, fun, device) else - native_compile(vars, fun, device) + {built, _cacheable?} = native_compile(vars, fun, device, hooks) + built end wrap_with_queue(queue, eval_fn) @@ -1904,16 +1914,16 @@ defmodule EMLX do cached {:error, :cache_miss} -> - built = native_compile(vars, fun, device) - EMLX.CompileCache.put(cache_key, built) + {built, cacheable?} = native_compile(vars, fun, device, %{}) + if cacheable?, do: EMLX.CompileCache.put(cache_key, built), else: built end end # Attempts to lower `fun.(vars)` to an `EMLX.Native.Expr` program and build a - # compiled program resource, returning the resulting eval closure. Single-mode: + # compiled program resource, returning the eval closure and cache eligibility. Single-mode: # any op not yet implemented raises `ArgumentError` ("does not yet lower op # ...") straight through to the caller — there is no whole-`defn` fallback lane. - defp native_compile(vars, fun, device) do + defp native_compile(vars, fun, device, hooks) do output_expr = fun.(vars) {worker, effective_device} = resolve_worker(device) # `vars`' flattened leaf count is the true call arity — passed through so @@ -1921,7 +1931,11 @@ defmodule EMLX do # reference every parameter position (see EMLX.Native.Expr.lower/2 doc). num_inputs = vars |> Nx.Defn.Composite.flatten_list() |> length() - base_key = dispatch_key(output_expr, num_inputs, effective_device) + program = EMLX.Native.Expr.lower(output_expr, num_inputs, %{}, hooks) + + base_key = + if program.runtime_calls == [] and hooks == %{}, + do: dispatch_key(output_expr, num_inputs, effective_device) {resource, runtime_calls} = get_or_compile_program( @@ -1930,7 +1944,9 @@ defmodule EMLX do fn -> output_expr end, num_inputs, worker, - effective_device + effective_device, + hooks, + program ) output_template = Nx.Defn.Composite.traverse(output_expr, &Nx.to_template/1) @@ -1981,7 +1997,7 @@ defmodule EMLX do nil end) - fn [params] -> + eval_fn = fn [params] -> {worker, dev} = resolve_worker(effective_device) tensors = materialise_input_tensors(params, dev) quant_signature = quant_signature(tensors, quantizable_positions) @@ -1990,7 +2006,7 @@ defmodule EMLX do # (and cached under `{base_key, %{}}`) by our caller, so the common # case skips a redundant cache round-trip; a quantized signature is # looked up (or compiled once and cached) here — see - # get_or_compile_program/6. `fn -> fun.(vars) end` is only forced on + # get_or_compile_program. For callback-free programs, `fn -> fun.(vars) end` is only forced on # that cache's own miss (once per distinct `{base_key, quant_signature}` # while it stays in the cache) — never eagerly, so a cache *hit* here # costs nothing beyond the lookup itself. @@ -2004,7 +2020,8 @@ defmodule EMLX do fn -> fun.(vars) end, num_inputs, worker, - dev + dev, + hooks ) end @@ -2085,6 +2102,10 @@ defmodule EMLX do [output_container] end + + # Callbacks may capture request aliases or other short-lived state. Their + # lifetime belongs to this returned executable, never to a global cache. + {eval_fn, runtime_calls == []} end # Reassembles the 2-3 physical refs `EMLX.Native.Expr.lower/2` emitted for @@ -2276,19 +2297,30 @@ defmodule EMLX do output_expr_thunk, num_inputs, worker, - dev + dev, + hooks, + program \\ nil ) do cache_key = {base_key, quant_signature} table = dispatch_cache_table() - case :ets.lookup(table, cache_key) do + case if(base_key, do: :ets.lookup(table, cache_key), else: []) do [{_key, compiled, _inserted_ms, _index}] -> compiled [] -> - program = EMLX.Native.Expr.lower(output_expr_thunk.(), num_inputs, quant_signature) + program = + program || + EMLX.Native.Expr.lower(output_expr_thunk.(), num_inputs, quant_signature, hooks) + resource = compile_native_program(worker, dev, program) - EMLX.CompileCache.put_new(table, cache_key, {resource, program.runtime_calls}) + compiled = {resource, program.runtime_calls} + + if base_key != nil and program.runtime_calls == [] do + EMLX.CompileCache.put_new(table, cache_key, compiled) + else + compiled + end end end diff --git a/emlx/lib/emlx/compile_cache.ex b/emlx/lib/emlx/compile_cache.ex index 1120176..b730159 100644 --- a/emlx/lib/emlx/compile_cache.ex +++ b/emlx/lib/emlx/compile_cache.ex @@ -1,9 +1,14 @@ defmodule EMLX.CompileCache do @moduledoc """ - Bounded cache of `native_compile/3` eval closures and compiled programs. + Configurable cache of callback-free eval closures and compiled programs. `EMLX.__compile__/4` stores one closure per `{fun, templates, hooks, device}` - so repeated `Nx.Defn.jit/2` of the same call site skips retrace. The + so repeated `Nx.Defn.jit/2` of the same call site skips retrace. Executables + with runtime callbacks (including inline/default hooks) and compilations + with nonempty `:hooks` options bypass these caches: callback closures may + capture request-specific aliases and must live only as long as the returned + executable. Keep a `Nx.Defn.compile/3` result to reuse such an executable. + The structural program table `:emlx_native_dispatch_cache` and its key table `:emlx_dispatch_key_by_id` are shared across call sites. Each compiled program holds device memory, for example its captured constants. All three diff --git a/emlx/lib/emlx/native/expr.ex b/emlx/lib/emlx/native/expr.ex index 21d1344..9fd6401 100644 --- a/emlx/lib/emlx/native/expr.ex +++ b/emlx/lib/emlx/native/expr.ex @@ -52,7 +52,7 @@ defmodule EMLX.Native.Expr do # ── lowering ────────────────────────────────────────────────────────────── @doc false - def lower(output, num_inputs \\ nil, quant_signature \\ %{}) do + def lower(output, num_inputs \\ nil, quant_signature \\ %{}, hooks \\ %{}) do ordered = EMLX.Defn.Tree.post_order(output, &scope_dependencies/1) top_scope_ids = top_scope_ids(output, ordered) @@ -64,6 +64,7 @@ defmodule EMLX.Native.Expr do instructions: [], node_to_ref: %{}, runtime_calls: [], + hooks: hooks, top_scope_ids: top_scope_ids, quant_signature: quant_signature, while_nesting_depth: 0, @@ -2006,17 +2007,11 @@ defmodule EMLX.Native.Expr do end case callback_spec do - {:named, _name, nil} -> - %{ - state - | node_to_ref: Map.put(state.node_to_ref, id, container_node_ref(tensor_expr, state)) - } - {:token_hook, hooked_expr, inner_spec} -> # Prefix hooked leaves as callback-only operands; trailing tensor # leaves are aliased to this node's outputs so evaluating the # pass-through forces the side effect (no keepalive chain). - case hook_callback(inner_spec) do + case hook_callback(inner_spec, state.hooks) do nil -> %{ state @@ -2032,12 +2027,18 @@ defmodule EMLX.Native.Expr do end spec -> - callback = - hook_callback(spec) || - raise ArgumentError, "invalid #{op} callback_spec: #{inspect(spec)}" + case hook_callback(spec, state.hooks) do + nil -> + %{ + state + | node_to_ref: + Map.put(state.node_to_ref, id, container_node_ref(tensor_expr, state)) + } - {value_ref, state} = emit_io_call_refs(nil, tensor_expr, callback, state) - %{state | node_to_ref: Map.put(state.node_to_ref, id, value_ref)} + callback -> + {value_ref, state} = emit_io_call_refs(nil, tensor_expr, callback, state) + %{state | node_to_ref: Map.put(state.node_to_ref, id, value_ref)} + end end end @@ -2287,11 +2288,25 @@ defmodule EMLX.Native.Expr do {result_refs, state} end - defp hook_callback({:fn, callback}) when is_function(callback, 1), do: callback - defp hook_callback({:named, _name, callback}) when is_function(callback, 1), do: callback - defp hook_callback({:named, _name, nil}), do: nil - defp hook_callback({:token_hook, _hooked_expr, inner_spec}), do: hook_callback(inner_spec) - defp hook_callback(_), do: nil + defp hook_callback({:fn, callback}, _hooks) when is_function(callback, 1), do: callback + + defp hook_callback({:named, name, default}, hooks) do + # Nx treats nil/false overrides as absent. Unused map entries are ignored. + case hooks[name] || default do + nil -> + nil + + callback when is_function(callback, 1) -> + callback + + callback -> + raise ArgumentError, + "invalid callback for hook #{inspect(name)}: expected a function of arity 1, got: #{inspect(callback)}" + end + end + + defp hook_callback(spec, _hooks), + do: raise(ArgumentError, "invalid hook callback_spec: #{inspect(spec)}") # Flat leaf refs for a tensor/container already lowered into `node_to_ref` # -- single leaf unwraps to a bare ref (matching `:runtime_call` / diff --git a/emlx/test/emlx/named_hooks_test.exs b/emlx/test/emlx/named_hooks_test.exs new file mode 100644 index 0000000..4b87846 --- /dev/null +++ b/emlx/test/emlx/named_hooks_test.exs @@ -0,0 +1,328 @@ +defmodule EMLX.NamedHooksTest do + use ExUnit.Case, async: false + import Nx.Defn + + # Exercise the same compiler contract on Nx 0.13 (hook) and Nx 1 (io_call). + deftransform named(value, name) do + op = if function_exported?(Nx.Defn.Kernel, :io_call, 2), do: :io_call, else: :hook + apply(Nx.Defn.Kernel, op, [value, name]) + end + + deftransform with_default(value) do + op = if function_exported?(Nx.Defn.Kernel, :io_call, 3), do: :io_call, else: :hook + + apply(Nx.Defn.Kernel, op, [ + value, + :value, + fn t -> send(self(), {:default, Nx.to_number(t)}) end + ]) + end + + deftransform token_payload(value, passthrough) do + token = apply(Nx.Defn.Kernel, :create_token, []) + {token, _} = apply(Nx.Defn.Kernel, :hook_token, [token, value, :token]) + apply(Nx.Defn.Kernel, :attach_token, [token, passthrough]) + end + + defn single(x), do: named(x + 1, :value) * 2 + defn defaulted(x), do: with_default(x + 1) * 2 + + defn nested(x) do + {x, _} = + while {x, i = 0}, i < 2 do + {x, _} = + while {x, j = 0}, j < 2 do + {named(x + 1, :value), j + 1} + end + + {x, i + 1} + end + + x + end + + defn token_loop(x) do + {x, _} = + while {x, i = 0}, i < 3 do + {token_payload({x + 10, x + 20}, x + 1), i + 1} + end + + x + end + + defn token_tuple(x), do: token_payload(x + 10, {x + 1, x + 2}) + + defn nested_token(x) do + {x, _} = + while {x, i = 0}, i < 2 do + {x, _} = + while {x, j = 0}, j < 2 do + {token_payload(with_default(x + 10), x + 1), j + 1} + end + + {x, i + 1} + end + + x + end + + defp tensor(x), do: Nx.tensor(x, backend: EMLX.Backend) + defp callback(tag), do: fn t -> send(self(), {tag, Nx.to_number(t)}) end + + defp cache_sizes do + closure_table = :sys.get_state(EMLX.CompileCache).table + + Enum.map( + [:emlx_native_dispatch_cache, :emlx_dispatch_key_by_id, closure_table], + &:ets.info(&1, :size) + ) + end + + test "named overrides, defaults, absent and nil overrides agree with Nx" do + for compiler <- [EMLX, Nx.Defn.Evaluator] do + assert Nx.to_number(Nx.Defn.jit(&single/1, compiler: compiler).(tensor(3))) == 8 + + assert Nx.to_number(Nx.Defn.jit(&defaulted/1, compiler: compiler).(tensor(3))) == 8 + assert_receive {:default, 4} + + for hooks <- [%{value: nil}, %{value: false}, %{unrelated: :unused}] do + Nx.Defn.jit(&defaulted/1, compiler: compiler, hooks: hooks).(tensor(3)) + assert_receive {:default, 4} + end + + Nx.Defn.jit(&defaulted/1, compiler: compiler, hooks: %{value: callback(:override)}).( + tensor(3) + ) + + assert_receive {:override, 4} + refute_receive {:default, _}, 0 + refute_receive {:override, _}, 0 + end + end + + test "invalid maps and matching callbacks fail explicitly" do + assert_raise ArgumentError, ~r/:hooks option must be a map/, fn -> + Nx.Defn.jit(&single/1, compiler: EMLX, hooks: []).(tensor(1)) + end + + for invalid <- [:bad, fn -> :ok end, fn _, _ -> :ok end] do + assert_raise ArgumentError, ~r/invalid callback for hook :value/, fn -> + Nx.Defn.jit(&single/1, compiler: EMLX, hooks: %{value: invalid}).(tensor(1)) + end + end + end + + test "overrides inside nested native while fire exactly once per iteration" do + for compiler <- [EMLX, Nx.Defn.Evaluator] do + result = + Nx.Defn.jit(&nested/1, compiler: compiler, hooks: %{value: callback(:iteration)}).( + tensor(0) + ) + + assert Nx.to_number(result) == 4 + + values = + for _ <- 1..4 do + assert_receive {:iteration, value} + value + end + + assert values == [1, 2, 3, 4] + refute_receive {:iteration, _}, 0 + end + end + + test "repeated and concurrent executables retain their own receiver aliases" do + parent = self() + + executables = + for n <- 1..12 do + receiver = :erlang.alias() + cb = fn t -> send(receiver, {:hook, n, Nx.to_number(t)}) end + + {receiver, + Nx.Defn.compile(&single/1, [Nx.template({}, :s32)], compiler: EMLX, hooks: %{value: cb})} + end + + for _ <- 1..2 do + tasks = + for {{_alias, executable}, n} <- Enum.with_index(executables, 1) do + Task.async(fn -> + result = executable.(tensor(n)) + send(parent, {:result, n, Nx.to_number(result)}) + end) + end + + Enum.each(tasks, &Task.await(&1, 30_000)) + + for n <- 1..12 do + expected = n + 1 + assert_receive {:hook, ^n, ^expected} + result = expected * 2 + assert_receive {:result, ^n, ^result} + end + + refute_receive {:hook, _, _}, 0 + end + + for {receiver, _} <- executables, do: :erlang.unalias(receiver) + + # Also race compilation itself, not just invocation of existing executables. + tasks = + for n <- 1..12 do + receiver = :erlang.alias() + + task = + Task.async(fn -> + cb = fn t -> send(receiver, {:compiled_concurrently, n, Nx.to_number(t)}) end + + Nx.Defn.jit(&single/1, compiler: EMLX, hooks: %{value: cb}).(tensor(n)) + |> Nx.to_number() + end) + + {n, receiver, task} + end + + for {n, receiver, task} <- tasks do + expected = n + 1 + assert Task.await(task, 30_000) == expected * 2 + assert_receive {:compiled_concurrently, ^n, ^expected} + :erlang.unalias(receiver) + end + + refute_receive {:compiled_concurrently, _, _}, 0 + end + + test "callback-bearing compilation adds no entries to global caches" do + # Initialize all three tables before measuring; cache bounds are deliberately + # left at their default infinity so eviction cannot disguise retention. + Nx.Defn.jit(&single/1, compiler: EMLX).(tensor(0)) + before = cache_sizes() + + for n <- 1..30 do + receiver = :erlang.alias() + cb = fn t -> send(receiver, {:retention, n, Nx.to_number(t)}) end + Nx.Defn.jit(&single/1, compiler: EMLX, hooks: %{value: cb}).(tensor(n)) + expected = n + 1 + assert_receive {:retention, ^n, ^expected} + :erlang.unalias(receiver) + end + + assert cache_sizes() == before + end + + test "inline default callbacks also bypass global executable caches" do + before = cache_sizes() + + for n <- 1..3 do + receiver = :erlang.alias() + + fun = fn x -> + op = if function_exported?(Nx.Defn.Kernel, :io_call, 3), do: :io_call, else: :hook + + apply(Nx.Defn.Kernel, op, [ + x, + :inline, + fn t -> send(receiver, {:inline, n, Nx.to_number(t)}) end + ]) + end + + Nx.Defn.jit(fun, compiler: EMLX).(tensor(n)) + assert_receive {:inline, ^n, ^n} + :erlang.unalias(receiver) + end + + refute_receive {:inline, _, _}, 0 + assert cache_sizes() == before + end + + test "raising, throwing and exiting callbacks release the native waiter" do + for cb <- [ + fn _ -> raise "hook failure" end, + fn _ -> throw(:hook_failure) end, + fn _ -> exit(:hook_failure) end + ] do + assert_raise EMLX.NIFError, ~r/hook.failure/, fn -> + Nx.Defn.jit(&single/1, compiler: EMLX, hooks: %{value: cb}).(tensor(1)) + end + + assert Nx.to_number(Nx.Defn.jit(&single/1, compiler: EMLX).(tensor(1))) == 4 + end + end + + @tag timeout: 45_000 + test "caller death and late replies are safe in a disposable BEAM" do + paths = + :code.get_path() + |> Enum.map(&List.to_string/1) + |> Enum.filter(&(Path.basename(&1) in ["ebin", "consolidated"])) + |> Enum.reverse() + |> Enum.flat_map(&["-pa", &1]) + + script = Path.expand("../fixtures/runtime_call_lifecycle.exs", __DIR__) + # Python owns and kills the disposable process on timeout, including if a + # native callback blocks VM shutdown. No hanging native work enters ExUnit. + runner = """ + import subprocess, sys + try: + p = subprocess.run(sys.argv[1:], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=30) + sys.stdout.buffer.write(p.stdout) + sys.exit(p.returncode) + except subprocess.TimeoutExpired as e: + sys.stdout.buffer.write(e.stdout or b'') + print('disposable BEAM timed out') + sys.exit(124) + """ + + args = + ["-c", runner, System.find_executable("elixir"), "--erl", "+S 2:2"] ++ paths ++ [script] + + {output, status} = System.cmd("python3", args, stderr_to_stdout: true) + assert status == 0, output + assert output =~ "runtime-call lifecycle OK" + end + + @tag skip: not function_exported?(Nx.Defn.Kernel, :create_token, 0) + test "Nx 0.13 token hooks in native while alias the trailing outputs" do + cb = fn {a, b} -> send(self(), {:token, Nx.to_number(a), Nx.to_number(b)}) end + + for compiler <- [EMLX, Nx.Defn.Evaluator] do + result = Nx.Defn.jit(&token_loop/1, compiler: compiler, hooks: %{token: cb}).(tensor(0)) + assert Nx.to_number(result) == 3 + + for i <- 0..2 do + assert_receive {:token, a, b} + assert {a, b} == {i + 10, i + 20} + end + + refute_receive {:token, _, _}, 0 + end + + {a, b} = + Nx.Defn.jit(&token_tuple/1, compiler: EMLX, hooks: %{token: callback(:tuple_token)}).( + tensor(5) + ) + + assert {Nx.to_number(a), Nx.to_number(b)} == {6, 7} + assert_receive {:tuple_token, 15} + refute_receive {:tuple_token, _}, 0 + + for compiler <- [EMLX, Nx.Defn.Evaluator], override? <- [false, true] do + hooks = %{token: callback(:nested_token)} + hooks = if override?, do: Map.put(hooks, :value, callback(:barrier)), else: hooks + tag = if override?, do: :barrier, else: :default + result = Nx.Defn.jit(&nested_token/1, compiler: compiler, hooks: hooks).(tensor(0)) + assert Nx.to_number(result) == 4 + + for i <- 10..13 do + assert_receive {first_tag, first_value} + assert {first_tag, first_value} == {tag, i} + assert_receive {second_tag, second_value} + assert {second_tag, second_value} == {:nested_token, i} + end + + refute_receive {^tag, _}, 0 + refute_receive {:nested_token, _}, 0 + end + end +end diff --git a/emlx/test/emlx/native/expr_test.exs b/emlx/test/emlx/native/expr_test.exs index 192ed38..89008e2 100644 --- a/emlx/test/emlx/native/expr_test.exs +++ b/emlx/test/emlx/native/expr_test.exs @@ -4234,8 +4234,8 @@ defmodule EMLX.Native.ExprTest do :emlx_native_dispatch_cache |> :ets.tab2list() |> MapSet.new(&elem(&1, 0)) end - describe "runtime_call split-point dispatch cache (compile once, reuse)" do - test "calling the same runtime_call-split defn twice compiles its flat stages once" do + describe "runtime_call executable ownership" do + test "calling a runtime_call defn twice does not retain its callbacks globally" do shape = {19, 128} w1 = Nx.iota(shape, type: :f32) |> Nx.divide(10) |> Nx.backend_transfer(EMLX.Backend) w2 = Nx.iota(shape, type: :f32) |> Nx.divide(7) |> Nx.backend_transfer(EMLX.Backend) @@ -4248,7 +4248,7 @@ defmodule EMLX.Native.ExprTest do Nx.Defn.jit(&dequant_surrounded/2, compiler: EMLX).(x, qw1) end) - assert first > 0 + assert first == 0 {native2, second} = new_dispatch_cache_entries(fn -> @@ -4263,7 +4263,7 @@ defmodule EMLX.Native.ExprTest do assert_all_close(native2, eager2) end - test "two structurally-identical but distinct call sites share one cache entry" do + test "structurally-identical callback-bearing call sites do not populate the cache" do shape = {23, 192} w1 = Nx.iota(shape, type: :f32) |> Nx.divide(10) |> Nx.backend_transfer(EMLX.Backend) w2 = Nx.iota(shape, type: :f32) |> Nx.divide(7) |> Nx.backend_transfer(EMLX.Backend) diff --git a/emlx/test/fixtures/runtime_call_lifecycle.exs b/emlx/test/fixtures/runtime_call_lifecycle.exs new file mode 100644 index 0000000..6981302 --- /dev/null +++ b/emlx/test/fixtures/runtime_call_lifecycle.exs @@ -0,0 +1,127 @@ +# Run only in a disposable BEAM: a broken native wait or resource lifetime must +# not hang/crash the ExUnit VM. The parent enforces an OS-process timeout. +:os.set_signal(:sigchld, :default) +Application.put_env(:emlx, :default_device, :cpu) +Application.put_env(:nx, :default_backend, EMLX.Backend) +{:ok, _} = Application.ensure_all_started(:emlx) + +defmodule RuntimeCallLifecycle do + def run do + {worker, :cpu} = EMLX.resolve_worker(:cpu) + input = Nx.tensor(7, type: :s32, backend: EMLX.Backend) + {:cpu, input_ref} = input.data.ref + + for kind <- [:hook, :runtime_call] do + fun = + case kind do + :hook -> + fn x -> + op = if function_exported?(Nx.Defn.Kernel, :io_call, 2), do: :io_call, else: :hook + apply(Nx.Defn.Kernel, op, [x, :value]) + end + + :runtime_call -> + fn x -> Nx.runtime_call(Nx.template({}, :s32), x, fn t, _ -> t end) end + end + + expr = Nx.Defn.debug_expr_apply(fun, [Nx.template({}, :s32)]) + program = EMLX.Native.Expr.lower(expr, 1, %{}, %{value: fn _ -> :ok end}) + {:ok, compile_job} = EMLX.NIF.compile_program(worker, EMLX.Native.Expr.to_native(program)) + resource = EMLX.await_worker(compile_job) + reply = if kind == :hook, do: [], else: [<<7::signed-native-32>>] + + # Successful reply, then duplicate reply after the worker released its + # destination buffers. The resource itself remains reachable here. + {caller, monitor, pending} = start_call(worker, resource, input_ref) + :ok = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + + receive do + {:finished, ^caller, {:ok, [_]}} -> :ok + after + 5_000 -> raise "normal callback did not finish" + end + + await_down(caller, monitor) + {:error, _} = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + + # Callback recipient dies after receiving the pending resource. It never + # sends a reply. The worker's bounded liveness check must abandon it. + for _ <- 1..8 do + {caller, monitor, pending} = start_call(worker, resource, input_ref) + Process.exit(caller, :kill) + await_down(caller, monitor) + # Allow the 5ms worker heartbeat to observe death. This is not a claim + # that a running MLX kernel is preempted on BEAM process death. + receive do + after + 100 -> :ok + end + + {:error, _} = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + end + + # Exercise the send-failure/message-discard path: no pending resource is + # retained by BEAM after recipient death. The worker reference protects it. + for _ <- 1..8 do + {caller, monitor} = + spawn_monitor(fn -> + {:ok, _} = EMLX.NIF.eval_program(worker, resource, [input_ref]) + end) + + await_down(caller, monitor) + end + + # The same worker and executable remain usable following abandoned calls. + {caller, monitor, pending} = start_call(worker, resource, input_ref) + :ok = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + + receive do + {:finished, ^caller, {:ok, [_]}} -> :ok + after + 5_000 -> raise "worker did not recover after caller death" + end + + await_down(caller, monitor) + end + + IO.puts("runtime-call lifecycle OK") + end + + defp start_call(worker, resource, input_ref) do + parent = self() + + {caller, monitor} = + spawn_monitor(fn -> + {:ok, job} = EMLX.NIF.eval_program(worker, resource, [input_ref]) + + receive do + {:emlx_runtime_call, pending, 0, [_]} -> + send(parent, {:pending, self(), pending}) + after + 5_000 -> raise "callback request not received" + end + + receive do + {^job, result} -> send(parent, {:finished, self(), result}) + after + 5_000 -> raise "callback result not received" + end + end) + + receive do + {:pending, ^caller, pending} -> {caller, monitor, pending} + after + 5_000 -> raise "pending handle not received" + end + end + + defp await_down(caller, monitor) do + receive do + {:DOWN, ^monitor, :process, ^caller, _reason} -> :ok + after + 5_000 -> raise "caller did not exit" + end + end +end + +RuntimeCallLifecycle.run() From 237fe81a55a50b62013efa6aea128a8ca75c9f45 Mon Sep 17 00:00:00 2001 From: Julien Date: Fri, 6 Nov 2026 09:40:23 +0100 Subject: [PATCH 2/2] test: use ExUnit and peers for hook callback lifecycle coverage --- emlx/c_src/emlx_runtime_call_bridge.hpp | 2 + emlx/lib/emlx.ex | 20 +-- emlx/test/emlx/named_hooks_test.exs | 138 +----------------- .../test/emlx/runtime_call_lifecycle_test.exs | 34 +++++ emlx/test/fixtures/runtime_call_lifecycle.exs | 127 ---------------- emlx/test/support/runtime_call_helpers.ex | 71 +++++++++ 6 files changed, 120 insertions(+), 272 deletions(-) create mode 100644 emlx/test/emlx/runtime_call_lifecycle_test.exs delete mode 100644 emlx/test/fixtures/runtime_call_lifecycle.exs create mode 100644 emlx/test/support/runtime_call_helpers.ex diff --git a/emlx/c_src/emlx_runtime_call_bridge.hpp b/emlx/c_src/emlx_runtime_call_bridge.hpp index e77e3dc..93cde64 100644 --- a/emlx/c_src/emlx_runtime_call_bridge.hpp +++ b/emlx/c_src/emlx_runtime_call_bridge.hpp @@ -256,6 +256,8 @@ inline void invoke_runtime_call(int64_t callback_index, if (Worker *worker = g_current_worker) { worker->pump_until(is_done); } else { + // A dead caller cannot reply or notify this condition variable. Wake + // periodically to check its liveness, just as pump_until does above. while (!is_done()) { std::unique_lock lock(pending->mu); pending->cv.wait_for(lock, std::chrono::milliseconds(5)); diff --git a/emlx/lib/emlx.ex b/emlx/lib/emlx.ex index 1949681..9e2f5bf 100644 --- a/emlx/lib/emlx.ex +++ b/emlx/lib/emlx.ex @@ -174,20 +174,11 @@ defmodule EMLX do ## Named hooks - The compiler's `:hooks` map overrides named `hook` callbacks on Nx 0.13 and - named `io_call` callbacks on Nx 1.x. Missing, nil, or false overrides use the - inline default; a name without a default is a no-op. Matching overrides must - be functions of arity one. Hooks in native while bodies run per iteration. + Use the compiler's `:hooks` option to override named `io_call` callbacks. + Otherwise, the inline callback runs, if provided. - Compilations with nonempty `:hooks` and executables containing runtime - callbacks bypass global caches, including the structural program cache. - This keeps request-specific callback closures and receiver aliases out of - process-lifetime storage. A caller-held `Nx.Defn.compile/3` executable still - supports repeated invocation with its own callbacks. Side effects inside - conditional branches remain unsupported. - - If a callback caller dies, the native bridge abandons its pending callback - wait and rejects late replies. This does not preempt a running MLX kernel. + Executables with callbacks bypass global caches; keep the function returned + by `Nx.Defn.compile/3` to reuse them. ## Compile-time debug flags @@ -2303,8 +2294,9 @@ defmodule EMLX do ) do cache_key = {base_key, quant_signature} table = dispatch_cache_table() + cached = if base_key, do: :ets.lookup(table, cache_key), else: [] - case if(base_key, do: :ets.lookup(table, cache_key), else: []) do + case cached do [{_key, compiled, _inserted_ms, _index}] -> compiled diff --git a/emlx/test/emlx/named_hooks_test.exs b/emlx/test/emlx/named_hooks_test.exs index 4b87846..bc30d38 100644 --- a/emlx/test/emlx/named_hooks_test.exs +++ b/emlx/test/emlx/named_hooks_test.exs @@ -2,62 +2,18 @@ defmodule EMLX.NamedHooksTest do use ExUnit.Case, async: false import Nx.Defn - # Exercise the same compiler contract on Nx 0.13 (hook) and Nx 1 (io_call). - deftransform named(value, name) do - op = if function_exported?(Nx.Defn.Kernel, :io_call, 2), do: :io_call, else: :hook - apply(Nx.Defn.Kernel, op, [value, name]) - end - - deftransform with_default(value) do - op = if function_exported?(Nx.Defn.Kernel, :io_call, 3), do: :io_call, else: :hook - - apply(Nx.Defn.Kernel, op, [ - value, - :value, - fn t -> send(self(), {:default, Nx.to_number(t)}) end - ]) - end + defn single(x), do: io_call(x + 1, :value) * 2 - deftransform token_payload(value, passthrough) do - token = apply(Nx.Defn.Kernel, :create_token, []) - {token, _} = apply(Nx.Defn.Kernel, :hook_token, [token, value, :token]) - apply(Nx.Defn.Kernel, :attach_token, [token, passthrough]) + defn defaulted(x) do + io_call(x + 1, :value, fn t -> send(self(), {:default, Nx.to_number(t)}) end) * 2 end - defn single(x), do: named(x + 1, :value) * 2 - defn defaulted(x), do: with_default(x + 1) * 2 - defn nested(x) do {x, _} = while {x, i = 0}, i < 2 do {x, _} = while {x, j = 0}, j < 2 do - {named(x + 1, :value), j + 1} - end - - {x, i + 1} - end - - x - end - - defn token_loop(x) do - {x, _} = - while {x, i = 0}, i < 3 do - {token_payload({x + 10, x + 20}, x + 1), i + 1} - end - - x - end - - defn token_tuple(x), do: token_payload(x + 10, {x + 1, x + 2}) - - defn nested_token(x) do - {x, _} = - while {x, i = 0}, i < 2 do - {x, _} = - while {x, j = 0}, j < 2 do - {token_payload(with_default(x + 10), x + 1), j + 1} + {io_call(x + 1, :value), j + 1} end {x, i + 1} @@ -218,13 +174,9 @@ defmodule EMLX.NamedHooksTest do receiver = :erlang.alias() fun = fn x -> - op = if function_exported?(Nx.Defn.Kernel, :io_call, 3), do: :io_call, else: :hook - - apply(Nx.Defn.Kernel, op, [ - x, - :inline, - fn t -> send(receiver, {:inline, n, Nx.to_number(t)}) end - ]) + Nx.Defn.Kernel.io_call(x, :inline, fn t -> + send(receiver, {:inline, n, Nx.to_number(t)}) + end) end Nx.Defn.jit(fun, compiler: EMLX).(tensor(n)) @@ -249,80 +201,4 @@ defmodule EMLX.NamedHooksTest do assert Nx.to_number(Nx.Defn.jit(&single/1, compiler: EMLX).(tensor(1))) == 4 end end - - @tag timeout: 45_000 - test "caller death and late replies are safe in a disposable BEAM" do - paths = - :code.get_path() - |> Enum.map(&List.to_string/1) - |> Enum.filter(&(Path.basename(&1) in ["ebin", "consolidated"])) - |> Enum.reverse() - |> Enum.flat_map(&["-pa", &1]) - - script = Path.expand("../fixtures/runtime_call_lifecycle.exs", __DIR__) - # Python owns and kills the disposable process on timeout, including if a - # native callback blocks VM shutdown. No hanging native work enters ExUnit. - runner = """ - import subprocess, sys - try: - p = subprocess.run(sys.argv[1:], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=30) - sys.stdout.buffer.write(p.stdout) - sys.exit(p.returncode) - except subprocess.TimeoutExpired as e: - sys.stdout.buffer.write(e.stdout or b'') - print('disposable BEAM timed out') - sys.exit(124) - """ - - args = - ["-c", runner, System.find_executable("elixir"), "--erl", "+S 2:2"] ++ paths ++ [script] - - {output, status} = System.cmd("python3", args, stderr_to_stdout: true) - assert status == 0, output - assert output =~ "runtime-call lifecycle OK" - end - - @tag skip: not function_exported?(Nx.Defn.Kernel, :create_token, 0) - test "Nx 0.13 token hooks in native while alias the trailing outputs" do - cb = fn {a, b} -> send(self(), {:token, Nx.to_number(a), Nx.to_number(b)}) end - - for compiler <- [EMLX, Nx.Defn.Evaluator] do - result = Nx.Defn.jit(&token_loop/1, compiler: compiler, hooks: %{token: cb}).(tensor(0)) - assert Nx.to_number(result) == 3 - - for i <- 0..2 do - assert_receive {:token, a, b} - assert {a, b} == {i + 10, i + 20} - end - - refute_receive {:token, _, _}, 0 - end - - {a, b} = - Nx.Defn.jit(&token_tuple/1, compiler: EMLX, hooks: %{token: callback(:tuple_token)}).( - tensor(5) - ) - - assert {Nx.to_number(a), Nx.to_number(b)} == {6, 7} - assert_receive {:tuple_token, 15} - refute_receive {:tuple_token, _}, 0 - - for compiler <- [EMLX, Nx.Defn.Evaluator], override? <- [false, true] do - hooks = %{token: callback(:nested_token)} - hooks = if override?, do: Map.put(hooks, :value, callback(:barrier)), else: hooks - tag = if override?, do: :barrier, else: :default - result = Nx.Defn.jit(&nested_token/1, compiler: compiler, hooks: hooks).(tensor(0)) - assert Nx.to_number(result) == 4 - - for i <- 10..13 do - assert_receive {first_tag, first_value} - assert {first_tag, first_value} == {tag, i} - assert_receive {second_tag, second_value} - assert {second_tag, second_value} == {:nested_token, i} - end - - refute_receive {^tag, _}, 0 - refute_receive {:nested_token, _}, 0 - end - end end diff --git a/emlx/test/emlx/runtime_call_lifecycle_test.exs b/emlx/test/emlx/runtime_call_lifecycle_test.exs new file mode 100644 index 0000000..1e946a7 --- /dev/null +++ b/emlx/test/emlx/runtime_call_lifecycle_test.exs @@ -0,0 +1,34 @@ +defmodule EMLX.RuntimeCallLifecycleTest do + use ExUnit.Case, async: false + alias EMLX.RuntimeCallHelpers + + for kind <- [:io_call, :runtime_call] do + test "#{kind} completes and rejects duplicate replies" do + {worker, resource, input_ref, reply} = RuntimeCallHelpers.program(unquote(kind)) + {caller, monitor, pending} = RuntimeCallHelpers.start_call(worker, resource, input_ref) + assert :ok = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + assert_receive {:finished, ^caller, {:ok, [_]}}, 5_000 + assert_receive {:DOWN, ^monitor, :process, ^caller, :normal}, 5_000 + assert {:error, _} = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + end + + @tag :distributed + @tag timeout: 30_000 + test "#{kind} abandons dead callers and rejects late replies on a peer" do + {:ok, peer, node} = + :peer.start(%{ + name: :peer.random_name(~c"emlx_callbacks"), + connection: :standard_io, + args: [~c"+S", ~c"2:2"], + shutdown: :halt + }) + + on_exit(fn -> if Process.alive?(peer), do: :peer.stop(peer) end) + assert true = :erpc.call(node, :code, :set_path, [:code.get_path()]) + :ok = :erpc.call(node, :os, :set_signal, [:sigchld, :default]) + :ok = :erpc.call(node, Application, :put_env, [:emlx, :default_device, :cpu]) + assert {:ok, _} = :erpc.call(node, Application, :ensure_all_started, [:emlx]) + assert :ok = :erpc.call(node, RuntimeCallHelpers, :caller_death, [unquote(kind)], 15_000) + end + end +end diff --git a/emlx/test/fixtures/runtime_call_lifecycle.exs b/emlx/test/fixtures/runtime_call_lifecycle.exs deleted file mode 100644 index 6981302..0000000 --- a/emlx/test/fixtures/runtime_call_lifecycle.exs +++ /dev/null @@ -1,127 +0,0 @@ -# Run only in a disposable BEAM: a broken native wait or resource lifetime must -# not hang/crash the ExUnit VM. The parent enforces an OS-process timeout. -:os.set_signal(:sigchld, :default) -Application.put_env(:emlx, :default_device, :cpu) -Application.put_env(:nx, :default_backend, EMLX.Backend) -{:ok, _} = Application.ensure_all_started(:emlx) - -defmodule RuntimeCallLifecycle do - def run do - {worker, :cpu} = EMLX.resolve_worker(:cpu) - input = Nx.tensor(7, type: :s32, backend: EMLX.Backend) - {:cpu, input_ref} = input.data.ref - - for kind <- [:hook, :runtime_call] do - fun = - case kind do - :hook -> - fn x -> - op = if function_exported?(Nx.Defn.Kernel, :io_call, 2), do: :io_call, else: :hook - apply(Nx.Defn.Kernel, op, [x, :value]) - end - - :runtime_call -> - fn x -> Nx.runtime_call(Nx.template({}, :s32), x, fn t, _ -> t end) end - end - - expr = Nx.Defn.debug_expr_apply(fun, [Nx.template({}, :s32)]) - program = EMLX.Native.Expr.lower(expr, 1, %{}, %{value: fn _ -> :ok end}) - {:ok, compile_job} = EMLX.NIF.compile_program(worker, EMLX.Native.Expr.to_native(program)) - resource = EMLX.await_worker(compile_job) - reply = if kind == :hook, do: [], else: [<<7::signed-native-32>>] - - # Successful reply, then duplicate reply after the worker released its - # destination buffers. The resource itself remains reachable here. - {caller, monitor, pending} = start_call(worker, resource, input_ref) - :ok = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) - - receive do - {:finished, ^caller, {:ok, [_]}} -> :ok - after - 5_000 -> raise "normal callback did not finish" - end - - await_down(caller, monitor) - {:error, _} = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) - - # Callback recipient dies after receiving the pending resource. It never - # sends a reply. The worker's bounded liveness check must abandon it. - for _ <- 1..8 do - {caller, monitor, pending} = start_call(worker, resource, input_ref) - Process.exit(caller, :kill) - await_down(caller, monitor) - # Allow the 5ms worker heartbeat to observe death. This is not a claim - # that a running MLX kernel is preempted on BEAM process death. - receive do - after - 100 -> :ok - end - - {:error, _} = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) - end - - # Exercise the send-failure/message-discard path: no pending resource is - # retained by BEAM after recipient death. The worker reference protects it. - for _ <- 1..8 do - {caller, monitor} = - spawn_monitor(fn -> - {:ok, _} = EMLX.NIF.eval_program(worker, resource, [input_ref]) - end) - - await_down(caller, monitor) - end - - # The same worker and executable remain usable following abandoned calls. - {caller, monitor, pending} = start_call(worker, resource, input_ref) - :ok = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) - - receive do - {:finished, ^caller, {:ok, [_]}} -> :ok - after - 5_000 -> raise "worker did not recover after caller death" - end - - await_down(caller, monitor) - end - - IO.puts("runtime-call lifecycle OK") - end - - defp start_call(worker, resource, input_ref) do - parent = self() - - {caller, monitor} = - spawn_monitor(fn -> - {:ok, job} = EMLX.NIF.eval_program(worker, resource, [input_ref]) - - receive do - {:emlx_runtime_call, pending, 0, [_]} -> - send(parent, {:pending, self(), pending}) - after - 5_000 -> raise "callback request not received" - end - - receive do - {^job, result} -> send(parent, {:finished, self(), result}) - after - 5_000 -> raise "callback result not received" - end - end) - - receive do - {:pending, ^caller, pending} -> {caller, monitor, pending} - after - 5_000 -> raise "pending handle not received" - end - end - - defp await_down(caller, monitor) do - receive do - {:DOWN, ^monitor, :process, ^caller, _reason} -> :ok - after - 5_000 -> raise "caller did not exit" - end - end -end - -RuntimeCallLifecycle.run() diff --git a/emlx/test/support/runtime_call_helpers.ex b/emlx/test/support/runtime_call_helpers.ex new file mode 100644 index 0000000..f9b8edf --- /dev/null +++ b/emlx/test/support/runtime_call_helpers.ex @@ -0,0 +1,71 @@ +defmodule EMLX.RuntimeCallHelpers do + @moduledoc false + import ExUnit.Assertions + + def program(kind) do + {worker, :cpu} = EMLX.resolve_worker(:cpu) + input = Nx.tensor(7, type: :s32, backend: EMLX.Backend) + {:cpu, input_ref} = input.data.ref + + fun = + case kind do + :io_call -> fn x -> Nx.Defn.Kernel.io_call(x, :value) end + :runtime_call -> fn x -> Nx.runtime_call(Nx.template({}, :s32), x, fn t, _ -> t end) end + end + + expr = Nx.Defn.debug_expr_apply(fun, [Nx.template({}, :s32)]) + program = EMLX.Native.Expr.lower(expr, 1, %{}, %{value: fn _ -> :ok end}) + {:ok, job} = EMLX.NIF.compile_program(worker, EMLX.Native.Expr.to_native(program)) + resource = EMLX.await_worker(job) + reply = if kind == :io_call, do: [], else: [<<7::signed-native-32>>] + {worker, resource, input_ref, reply} + end + + def start_call(worker, resource, input_ref) do + parent = self() + + {caller, monitor} = + spawn_monitor(fn -> + assert {:ok, job} = EMLX.NIF.eval_program(worker, resource, [input_ref]) + assert_receive {:emlx_runtime_call, pending, 0, [_]}, 5_000 + send(parent, {:pending, self(), pending}) + assert_receive {^job, result}, 5_000 + send(parent, {:finished, self(), result}) + end) + + assert_receive {:pending, ^caller, pending}, 5_000 + {caller, monitor, pending} + end + + # Run on an isolated peer: an ExUnit timeout cannot interrupt a native wait. + def caller_death(kind) do + {worker, resource, input_ref, reply} = program(kind) + + for _ <- 1..8 do + {caller, monitor, pending} = start_call(worker, resource, input_ref) + Process.exit(caller, :kill) + assert_receive {:DOWN, ^monitor, :process, ^caller, :killed}, 5_000 + + # DOWN does not preempt native computation. Allow the worker's 5ms + # liveness check to abandon the callback before delivering a late reply. + Process.sleep(100) + assert {:error, _} = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + end + + # Drop the recipient before it can retain the pending message/resource. + for _ <- 1..8 do + {caller, monitor} = + spawn_monitor(fn -> + assert {:ok, _} = EMLX.NIF.eval_program(worker, resource, [input_ref]) + end) + + assert_receive {:DOWN, ^monitor, :process, ^caller, :normal}, 5_000 + end + + {caller, monitor, pending} = start_call(worker, resource, input_ref) + assert :ok = EMLX.NIF.resolve_runtime_call(pending, :ok, reply) + assert_receive {:finished, ^caller, {:ok, [_]}}, 5_000 + assert_receive {:DOWN, ^monitor, :process, ^caller, :normal}, 5_000 + :ok + end +end