diff --git a/emlx/c_src/emlx_runtime_call_bridge.hpp b/emlx/c_src/emlx_runtime_call_bridge.hpp index 1f1cca7..93cde64 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,12 @@ 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; }); + // 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)); + } } std::lock_guard lock(pending->mu); @@ -244,8 +271,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 +307,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 a3a1fd1..5b873f2 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,14 @@ 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 + + Use the compiler's `:hooks` option to override named `io_call` callbacks. + Otherwise, the inline callback runs, if provided. + + Executables with callbacks bypass global caches; keep the function returned + by `Nx.Defn.compile/3` to reuse them. + ## Compile-time debug flags Several development-only checks are gated by `Application.compile_env/3` @@ -1854,6 +1862,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 @@ -1898,7 +1908,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`. @@ -1922,26 +1932,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) @@ -1953,16 +1954,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 @@ -1970,7 +1971,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( @@ -1979,7 +1984,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) @@ -2030,7 +2037,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) @@ -2039,7 +2046,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. @@ -2053,7 +2060,8 @@ defmodule EMLX do fn -> fun.(vars) end, num_inputs, worker, - dev + dev, + hooks ) end @@ -2134,6 +2142,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 @@ -2325,19 +2337,31 @@ 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() + cached = if base_key, do: :ets.lookup(table, cache_key), else: [] - case :ets.lookup(table, cache_key) do + case cached 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..bc30d38 --- /dev/null +++ b/emlx/test/emlx/named_hooks_test.exs @@ -0,0 +1,204 @@ +defmodule EMLX.NamedHooksTest do + use ExUnit.Case, async: false + import Nx.Defn + + defn single(x), do: io_call(x + 1, :value) * 2 + + defn defaulted(x) do + io_call(x + 1, :value, fn t -> send(self(), {:default, Nx.to_number(t)}) end) * 2 + end + + defn nested(x) do + {x, _} = + while {x, i = 0}, i < 2 do + {x, _} = + while {x, j = 0}, j < 2 do + {io_call(x + 1, :value), 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 -> + 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)) + 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 +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/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/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