From 0f8c96695e992a6421bf3843b39ff580bd9c0c68 Mon Sep 17 00:00:00 2001 From: Reid Morrison Date: Sun, 15 Nov 2026 23:20:47 -0500 Subject: [PATCH 1/2] Implement stack/3 in Ortex.Backend Since Nx 0.8, Nx.stack/2 calls the backend's stack/3 instead of using new_axis plus concatenate, so stacking Ortex tensors on Nx 0.8 and later (including 1.0) raised "operation stack is not yet supported on Ortex.Backend". Implement it the way Nx did before: give each tensor the new axis with the backend's reshape, then join them with its concatenate. --- lib/ortex/backend.ex | 17 +++++++++++++++++ test/shape/stack_test.exs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 test/shape/stack_test.exs diff --git a/lib/ortex/backend.ex b/lib/ortex/backend.ex index 379330e..ab11d07 100644 --- a/lib/ortex/backend.ex +++ b/lib/ortex/backend.ex @@ -124,6 +124,23 @@ defmodule Ortex.Backend do %{out | data: %B{ref: Ortex.Native.concatenate(tensor_refs, type, axis)}} end + @impl true + def stack(out, tensors, axis) do + # Give each tensor a new axis of size 1 at `axis`, then join them along it. + tensors = + Enum.map(tensors, fn %T{shape: shape, names: names} = t -> + new_axis = %{ + t + | shape: Tuple.insert_at(shape, axis, 1), + names: List.insert_at(names, axis, nil) + } + + reshape(new_axis, t) + end) + + concatenate(out, tensors, axis) + end + if Application.compile_env(:ortex, :add_backend_on_inspect, true) do defp maybe_add_signature(result, %T{data: %B{ref: _mat_ref}}) do Inspect.Algebra.concat([ diff --git a/test/shape/stack_test.exs b/test/shape/stack_test.exs new file mode 100644 index 0000000..b9bf57a --- /dev/null +++ b/test/shape/stack_test.exs @@ -0,0 +1,30 @@ +defmodule Ortex.TestStack do + use ExUnit.Case + + defp to_ortex(tensor), do: Nx.backend_transfer(tensor, Ortex.Backend) + + test "stack 1d tensors" do + t1 = Nx.tensor([1, 2, 3]) + t2 = Nx.tensor([4, 5, 6]) + + stacked = Nx.stack([to_ortex(t1), to_ortex(t2)]) |> Nx.backend_transfer() + assert stacked == Nx.stack([t1, t2]) + end + + test "stack 2d tensors on each axis" do + t1 = Nx.iota({2, 3}, type: :f32) + t2 = Nx.iota({2, 3}, type: :f32) |> Nx.add(10) + + for axis <- 0..2 do + stacked = Nx.stack([to_ortex(t1), to_ortex(t2)], axis: axis) |> Nx.backend_transfer() + assert stacked == Nx.stack([t1, t2], axis: axis) + end + end + + test "stack three tensors" do + tensors = for i <- 1..3, do: Nx.iota({2, 2}, type: :s64) |> Nx.multiply(i) + + stacked = tensors |> Enum.map(&to_ortex/1) |> Nx.stack(axis: 1) |> Nx.backend_transfer() + assert stacked == Nx.stack(tensors, axis: 1) + end +end From 4185800e4e6b61516b39bb062d489a980b393a62 Mon Sep 17 00:00:00 2001 From: Reid Morrison Date: Sun, 15 Nov 2026 23:28:01 -0500 Subject: [PATCH 2/2] Mark stack/3 as a callback only on Nx versions that define it Nx 0.6, the version in mix.lock, has no stack/3 backend callback, so "@impl true" produced a warning and failed "mix compile --warnings-as-errors" in CI. Derive @impl from Nx.Backend.behaviour_info/1, which the module already uses for its unimplemented callbacks. --- lib/ortex/backend.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ortex/backend.ex b/lib/ortex/backend.ex index ab11d07..e77859f 100644 --- a/lib/ortex/backend.ex +++ b/lib/ortex/backend.ex @@ -124,7 +124,8 @@ defmodule Ortex.Backend do %{out | data: %B{ref: Ortex.Native.concatenate(tensor_refs, type, axis)}} end - @impl true + # stack/3 is a backend callback since Nx 0.8; older Nx stacks with reshape and concatenate. + @impl {:stack, 3} in Nx.Backend.behaviour_info(:callbacks) def stack(out, tensors, axis) do # Give each tensor a new axis of size 1 at `axis`, then join them along it. tensors =