|
| 1 | +defmodule Emily.Backend.FallbacksTest do |
| 2 | + @moduledoc """ |
| 3 | + Tests that exercise every `via_binary` fallback path in |
| 4 | + `Emily.Backend`. These are backend callbacks with no single MLX |
| 5 | + primitive — we transfer inputs to `Nx.BinaryBackend`, run the |
| 6 | + reference op there, and transfer the result back. |
| 7 | +
|
| 8 | + Because the fallback dispatches *to* BinaryBackend, comparing its |
| 9 | + output against a direct BinaryBackend call is tautological. These |
| 10 | + aren't correctness tests — they're a coverage harness to make sure |
| 11 | + every via_binary branch compiles, transfers, and rewraps without |
| 12 | + error, and that the dtype/shape of the result matches the Nx |
| 13 | + contract for the op. |
| 14 | +
|
| 15 | + The cost of these fallbacks (a full round-trip to CPU per call) is |
| 16 | + why the ops they guard are on the roadmap for native translation; |
| 17 | + keeping the smoke coverage here lets us delete each test as the |
| 18 | + corresponding native path lands. |
| 19 | + """ |
| 20 | + |
| 21 | + use ExUnit.Case, async: true |
| 22 | + |
| 23 | + defp emily(list, type \\ {:f, 32}) do |
| 24 | + Nx.tensor(list, type: type, backend: Emily.Backend) |
| 25 | + end |
| 26 | + |
| 27 | + defp flat(t), do: Nx.to_flat_list(t) |
| 28 | + |
| 29 | + describe "indexing fallbacks" do |
| 30 | + test "put_slice routes through BinaryBackend" do |
| 31 | + t = emily([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) |
| 32 | + u = emily([[10.0, 20.0]]) |
| 33 | + |
| 34 | + result = Nx.put_slice(t, [0, 1], u) |
| 35 | + |
| 36 | + assert Nx.shape(result) == {2, 3} |
| 37 | + assert flat(result) == [1.0, 10.0, 20.0, 4.0, 5.0, 6.0] |
| 38 | + end |
| 39 | + |
| 40 | + test "gather with multi-axis indices routes through BinaryBackend" do |
| 41 | + t = emily([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) |
| 42 | + # Multi-axis index: each index selects across axes [0, 1]. |
| 43 | + idx = Nx.tensor([[0, 0], [1, 1], [2, 0]], backend: Emily.Backend) |
| 44 | + |
| 45 | + result = Nx.gather(t, idx, axes: [0, 1]) |
| 46 | + |
| 47 | + assert flat(result) == [1.0, 4.0, 5.0] |
| 48 | + end |
| 49 | + |
| 50 | + test "indexed_add routes through BinaryBackend" do |
| 51 | + t = emily([1.0, 2.0, 3.0, 4.0]) |
| 52 | + idx = Nx.tensor([[0], [2]], backend: Emily.Backend) |
| 53 | + upd = emily([10.0, 100.0]) |
| 54 | + |
| 55 | + result = Nx.indexed_add(t, idx, upd) |
| 56 | + |
| 57 | + assert flat(result) == [11.0, 2.0, 103.0, 4.0] |
| 58 | + end |
| 59 | + |
| 60 | + test "indexed_put routes through BinaryBackend" do |
| 61 | + t = emily([1.0, 2.0, 3.0, 4.0]) |
| 62 | + idx = Nx.tensor([[0], [2]], backend: Emily.Backend) |
| 63 | + upd = emily([99.0, 77.0]) |
| 64 | + |
| 65 | + result = Nx.indexed_put(t, idx, upd) |
| 66 | + |
| 67 | + assert flat(result) == [99.0, 2.0, 77.0, 4.0] |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe "convolution fallback" do |
| 72 | + test "conv routes through BinaryBackend" do |
| 73 | + # {batch=1, channels=1, height=3, width=3} input, {out=1, in=1, 2, 2} kernel. |
| 74 | + input = Nx.iota({1, 1, 3, 3}, type: {:f, 32}, backend: Emily.Backend) |
| 75 | + kernel = emily([[[[1.0, 0.0], [0.0, 1.0]]]]) |
| 76 | + |
| 77 | + result = Nx.conv(input, kernel) |
| 78 | + |
| 79 | + assert Nx.shape(result) == {1, 1, 2, 2} |
| 80 | + # Diagonal kernel: 0+4=4, 1+5=6, 3+7=10, 4+8=12. |
| 81 | + assert flat(result) == [4.0, 6.0, 10.0, 12.0] |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe "reduce fallbacks" do |
| 86 | + test "reduce with a custom accumulator function" do |
| 87 | + t = emily([1.0, 2.0, 3.0, 4.0]) |
| 88 | + acc = Nx.tensor(0.0, backend: Emily.Backend) |
| 89 | + |
| 90 | + result = Nx.reduce(t, acc, fn x, a -> Nx.add(x, a) end) |
| 91 | + |
| 92 | + assert Nx.to_number(result) == 10.0 |
| 93 | + end |
| 94 | + |
| 95 | + test "window_reduce with a custom accumulator function" do |
| 96 | + t = emily([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) |
| 97 | + acc = Nx.tensor(0.0, backend: Emily.Backend) |
| 98 | + |
| 99 | + result = |
| 100 | + Nx.window_reduce(t, acc, {1, 2}, [strides: [1, 1]], fn x, a -> Nx.max(x, a) end) |
| 101 | + |
| 102 | + assert Nx.shape(result) == {2, 2} |
| 103 | + assert flat(result) == [2.0, 3.0, 5.0, 6.0] |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe "window reductions" do |
| 108 | + setup do |
| 109 | + %{tensor: emily([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]])} |
| 110 | + end |
| 111 | + |
| 112 | + test "window_sum", %{tensor: t} do |
| 113 | + result = Nx.window_sum(t, {1, 2}) |
| 114 | + assert flat(result) == [3.0, 5.0, 7.0, 11.0, 13.0, 15.0] |
| 115 | + end |
| 116 | + |
| 117 | + test "window_product", %{tensor: t} do |
| 118 | + result = Nx.window_product(t, {1, 2}) |
| 119 | + assert flat(result) == [2.0, 6.0, 12.0, 30.0, 42.0, 56.0] |
| 120 | + end |
| 121 | + |
| 122 | + test "window_max", %{tensor: t} do |
| 123 | + result = Nx.window_max(t, {1, 2}) |
| 124 | + assert flat(result) == [2.0, 3.0, 4.0, 6.0, 7.0, 8.0] |
| 125 | + end |
| 126 | + |
| 127 | + test "window_min", %{tensor: t} do |
| 128 | + result = Nx.window_min(t, {1, 2}) |
| 129 | + assert flat(result) == [1.0, 2.0, 3.0, 5.0, 6.0, 7.0] |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + describe "window scatter" do |
| 134 | + # Scatter selects the argmax/argmin within each window and scatters |
| 135 | + # the corresponding `source` entry into the output. Small inputs; |
| 136 | + # we're only checking the via_binary path runs clean. |
| 137 | + test "window_scatter_max" do |
| 138 | + t = emily([[1.0, 2.0], [3.0, 4.0]]) |
| 139 | + source = emily([[5.0]]) |
| 140 | + init = Nx.tensor(0.0, backend: Emily.Backend) |
| 141 | + |
| 142 | + result = Nx.window_scatter_max(t, source, init, {2, 2}, strides: [1, 1]) |
| 143 | + |
| 144 | + assert Nx.shape(result) == {2, 2} |
| 145 | + # Max element is 4.0 (bottom-right); that position receives 5.0. |
| 146 | + assert flat(result) == [0.0, 0.0, 0.0, 5.0] |
| 147 | + end |
| 148 | + |
| 149 | + test "window_scatter_min" do |
| 150 | + t = emily([[1.0, 2.0], [3.0, 4.0]]) |
| 151 | + source = emily([[5.0]]) |
| 152 | + init = Nx.tensor(0.0, backend: Emily.Backend) |
| 153 | + |
| 154 | + result = Nx.window_scatter_min(t, source, init, {2, 2}, strides: [1, 1]) |
| 155 | + |
| 156 | + # Min element is 1.0 (top-left). |
| 157 | + assert flat(result) == [5.0, 0.0, 0.0, 0.0] |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + describe "linear algebra fallbacks" do |
| 162 | + test "lu returns (p, l, u) tuple via BinaryBackend" do |
| 163 | + t = emily([[2.0, 1.0], [1.0, 3.0]]) |
| 164 | + {p, l, u} = Nx.LinAlg.lu(t) |
| 165 | + |
| 166 | + assert Nx.shape(p) == {2, 2} |
| 167 | + assert Nx.shape(l) == {2, 2} |
| 168 | + assert Nx.shape(u) == {2, 2} |
| 169 | + |
| 170 | + # Round-trip check: P * L * U ≈ original (within f32 tolerance). |
| 171 | + reconstructed = p |> Nx.dot(l) |> Nx.dot(u) |
| 172 | + assert_in_delta Nx.to_number(reconstructed[0][0]), 2.0, 1.0e-4 |
| 173 | + assert_in_delta Nx.to_number(reconstructed[1][1]), 3.0, 1.0e-4 |
| 174 | + end |
| 175 | + |
| 176 | + test "svd returns (u, s, vt) tuple via BinaryBackend" do |
| 177 | + t = emily([[3.0, 0.0], [0.0, 4.0]]) |
| 178 | + {u, s, vt} = Nx.LinAlg.svd(t) |
| 179 | + |
| 180 | + assert Nx.shape(u) == {2, 2} |
| 181 | + assert Nx.shape(s) == {2} |
| 182 | + assert Nx.shape(vt) == {2, 2} |
| 183 | + |
| 184 | + # Singular values of a positive diagonal are its entries, sorted. |
| 185 | + [s0, s1] = flat(s) |
| 186 | + assert_in_delta max(s0, s1), 4.0, 1.0e-4 |
| 187 | + assert_in_delta min(s0, s1), 3.0, 1.0e-4 |
| 188 | + end |
| 189 | + |
| 190 | + test "triangular_solve routes through BinaryBackend" do |
| 191 | + # L x = b with L lower-triangular; x should be [1, 1]. |
| 192 | + l = emily([[2.0, 0.0], [1.0, 3.0]]) |
| 193 | + b = emily([2.0, 4.0]) |
| 194 | + |
| 195 | + x = Nx.LinAlg.triangular_solve(l, b) |
| 196 | + |
| 197 | + assert_in_delta Nx.to_number(x[0]), 1.0, 1.0e-4 |
| 198 | + assert_in_delta Nx.to_number(x[1]), 1.0, 1.0e-4 |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + describe "forced fallback branches" do |
| 203 | + # Batched dot with integer operands: MLX matmul is float-only, so |
| 204 | + # `Emily.Backend.dot/7` routes integer-batched calls through |
| 205 | + # BinaryBackend. Float-batched path is covered by the property |
| 206 | + # tests in backend_test.exs. |
| 207 | + test "batched dot with s32 operands falls back" do |
| 208 | + a = Nx.iota({2, 3, 4}, type: {:s, 32}, backend: Emily.Backend) |
| 209 | + b = Nx.iota({2, 4, 5}, type: {:s, 32}, backend: Emily.Backend) |
| 210 | + |
| 211 | + emily = Nx.dot(a, [2], [0], b, [1], [0]) |
| 212 | + |
| 213 | + ref_a = Nx.iota({2, 3, 4}, type: {:s, 32}, backend: Nx.BinaryBackend) |
| 214 | + ref_b = Nx.iota({2, 4, 5}, type: {:s, 32}, backend: Nx.BinaryBackend) |
| 215 | + ref = Nx.dot(ref_a, [2], [0], ref_b, [1], [0]) |
| 216 | + |
| 217 | + assert Nx.shape(emily) == {2, 3, 5} |
| 218 | + assert flat(emily) == Nx.to_flat_list(ref) |
| 219 | + end |
| 220 | + |
| 221 | + # Interior-axis cumulative: MLX's cumulative kernels raise on |
| 222 | + # some 4-D+ shape factorings, so the backend routes interior-axis |
| 223 | + # cumulation through BinaryBackend. Last-axis stays native. |
| 224 | + test "cumulative_sum on an interior axis falls back" do |
| 225 | + t = Nx.iota({2, 3, 4}, type: {:f, 32}, backend: Emily.Backend) |
| 226 | + result = Nx.cumulative_sum(t, axis: 1) |
| 227 | + |
| 228 | + ref = Nx.iota({2, 3, 4}, type: {:f, 32}, backend: Nx.BinaryBackend) |
| 229 | + ref_result = Nx.cumulative_sum(ref, axis: 1) |
| 230 | + |
| 231 | + assert flat(result) == Nx.to_flat_list(ref_result) |
| 232 | + end |
| 233 | + |
| 234 | + test "cumulative_product on an interior axis falls back" do |
| 235 | + t = emily([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]) |
| 236 | + result = Nx.cumulative_product(t, axis: 1) |
| 237 | + assert Nx.shape(result) == {2, 2, 2} |
| 238 | + end |
| 239 | + |
| 240 | + test "cumulative_max on an interior axis falls back" do |
| 241 | + t = emily([[[1.0, 4.0], [3.0, 2.0]], [[5.0, 8.0], [7.0, 6.0]]]) |
| 242 | + result = Nx.cumulative_max(t, axis: 1) |
| 243 | + assert Nx.shape(result) == {2, 2, 2} |
| 244 | + end |
| 245 | + |
| 246 | + test "cumulative_min on an interior axis falls back" do |
| 247 | + t = emily([[[1.0, 4.0], [3.0, 2.0]], [[5.0, 8.0], [7.0, 6.0]]]) |
| 248 | + result = Nx.cumulative_min(t, axis: 1) |
| 249 | + assert Nx.shape(result) == {2, 2, 2} |
| 250 | + end |
| 251 | + end |
| 252 | +end |
0 commit comments