From ef56506a4fd507240f2b4944003a62e7f6bf2a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?George=20Guimar=C3=A3es?= Date: Mon, 2 Nov 2026 13:49:21 -0300 Subject: [PATCH] fix: Map each parameter of a block default expression to the argument at its position expand_block_via_default/4 zipped the parameters that the default expression reaches with all block arguments. When the default function ignores an argument, every parameter after it read the argument before its own. Each parameter now reads the argument at its position. The option lists after the tensor arguments are not arguments of the program. --- emlx/lib/emlx/native/expr.ex | 30 +++++++++++++---------------- emlx/test/emlx/native/expr_test.exs | 19 ++++++++++++++++++ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/emlx/lib/emlx/native/expr.ex b/emlx/lib/emlx/native/expr.ex index 9fd6401..7d8b235 100644 --- a/emlx/lib/emlx/native/expr.ex +++ b/emlx/lib/emlx/native/expr.ex @@ -2492,26 +2492,22 @@ defmodule EMLX.Native.Expr do defp expand_block_via_default(id, in_args, default_expr, state) do inner_ordered = EMLX.Defn.Tree.post_order(default_expr, &scope_dependencies/1) - # Collect inner :parameter nodes; sort by position (args[0]). - inner_params = - inner_ordered - |> Enum.filter(&(&1.data.op == :parameter)) - |> Enum.sort_by(fn t -> hd(t.data.args) end) - - # Map each inner param id → the corresponding parent-scope arg ref. - parent_arg_refs = Enum.map(in_args, &Map.fetch!(state.node_to_ref, &1.data.id)) + # The inner :parameter nodes that the default expression reaches. A parameter can be + # unused, so each one maps to the parent-scope arg at its own position (args[0]), not + # to the next arg in order. The tensor args come before the option lists. + inner_params = Enum.filter(inner_ordered, &(&1.data.op == :parameter)) + + parent_arg_refs = + in_args + |> Enum.take_while(&(not is_list(&1))) + |> Enum.map(&Map.fetch!(state.node_to_ref, &1.data.id)) - inner_param_id_set = - inner_params - |> Enum.zip(parent_arg_refs) - |> Enum.reduce(MapSet.new(), fn {param, _}, acc -> - MapSet.put(acc, param.data.id) - end) + inner_param_id_set = MapSet.new(inner_params, & &1.data.id) inner_param_ref_map = - inner_params - |> Enum.zip(parent_arg_refs) - |> Map.new(fn {param, ref} -> {param.data.id, ref} end) + Map.new(inner_params, fn %T{data: %Nx.Defn.Expr{id: id, args: [pos | _]}} -> + {id, Enum.at(parent_arg_refs, pos)} + end) # Extend node_to_ref with inner param → parent ref mappings. merged_node_to_ref = Map.merge(state.node_to_ref, inner_param_ref_map) diff --git a/emlx/test/emlx/native/expr_test.exs b/emlx/test/emlx/native/expr_test.exs index 89008e2..059b8b4 100644 --- a/emlx/test/emlx/native/expr_test.exs +++ b/emlx/test/emlx/native/expr_test.exs @@ -24,6 +24,12 @@ defmodule EMLX.Native.ExprTest.NestedWhileAst do end end +defmodule EMLX.Native.ExprTest.IgnoredArgBlock do + @moduledoc false + # A block struct that no lowering recognizes, so it lowers through its default expression. + defstruct [] +end + defmodule EMLX.Native.ExprTest do @moduledoc """ Tests for the EMLX native defn compiler: @@ -2818,6 +2824,19 @@ defmodule EMLX.Native.ExprTest do end describe "block-descent completeness (AllClose/Phase/TopK)" do + test "a default function that ignores an argument reads each argument at its position" do + a = Nx.tensor([1.0, 2.0], type: :f32, backend: EMLX.Backend) + b = Nx.tensor([10.0, 20.0], type: :f32, backend: EMLX.Backend) + + fun = fn a, b -> + Nx.block(%EMLX.Native.ExprTest.IgnoredArgBlock{}, [a, b], b, fn _struct, _a, b -> + Nx.multiply(b, 2) + end) + end + + assert Nx.to_flat_list(Nx.Defn.jit(fun, compiler: EMLX).(a, b)) == [20.0, 40.0] + end + test "all_close: close tensors vs EMLX.Backend" do a = Nx.tensor([1.0, 2.0, 3.0], type: :f32, backend: EMLX.Backend) b = Nx.tensor([1.0, 2.0, 3.0 + 1.0e-7], type: :f32, backend: EMLX.Backend)