Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions emlx/lib/emlx/native/expr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions emlx/test/emlx/native/expr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
Loading