From d351eff50bcd508cb1517a55e8870a26b0879d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?George=20Guimar=C3=A3es?= Date: Mon, 2 Nov 2026 13:51:14 -0300 Subject: [PATCH] fix: Pass rtol and atol of Nx.all_close/3 to mx::allclose in their order The eager AllClose block passed atol as rtol and rtol as atol, so it tested |a - b| <= rtol + atol * |b|. For large values it said that close tensors were not close. It now passes rtol and atol in the order of EMLX.allclose/5. mx::allclose takes abs(b) in the type of b, which overflows at the smallest signed integer. The swap hid this case in the doctest of Nx.all_close/3 with s8 values. Two integer tensors now go to the default function, which widens them first, as Nx does. --- emlx/lib/emlx/backend.ex | 12 ++++++++++-- emlx/test/emlx/native/expr_test.exs | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/emlx/lib/emlx/backend.ex b/emlx/lib/emlx/backend.ex index f647092..80f0321 100644 --- a/emlx/lib/emlx/backend.ex +++ b/emlx/lib/emlx/backend.ex @@ -2164,11 +2164,19 @@ defmodule EMLX.Backend do |> to_nx(out) end + # mx::allclose takes abs(b) in the type of b, which overflows at the smallest signed integer. + # Two integer tensors go to the default function, which widens them first. @impl true - def block(%Nx.Block.AllClose{atol: atol, rtol: rtol, equal_nan: equal_nan}, out, [a, b], _fun) do + def block( + %Nx.Block.AllClose{atol: atol, rtol: rtol, equal_nan: equal_nan}, + out, + [%T{type: {kind_a, _}} = a, %T{type: {kind_b, _}} = b], + _fun + ) + when kind_a not in [:s, :u] or kind_b not in [:s, :u] do a |> from_nx() - |> EMLX.allclose(from_nx(b), atol || 1.0e-4, rtol || 1.0e-8, equal_nan == true) + |> EMLX.allclose(from_nx(b), rtol, atol, equal_nan == true) |> to_nx(out) end diff --git a/emlx/test/emlx/native/expr_test.exs b/emlx/test/emlx/native/expr_test.exs index 89008e2..e6f8c90 100644 --- a/emlx/test/emlx/native/expr_test.exs +++ b/emlx/test/emlx/native/expr_test.exs @@ -2828,6 +2828,15 @@ defmodule EMLX.Native.ExprTest do assert Nx.to_number(native) == 1 end + test "all_close: rtol scales with b and atol does not, eager and compiled" do + # |a - b| = 0.005 is within rtol * |b| = 0.01, but not within atol = 1.0e-8. + a = Nx.tensor([1000.0], type: :f32, backend: EMLX.Backend) + b = Nx.tensor([1000.005], type: :f32, backend: EMLX.Backend) + + assert Nx.to_number(Nx.all_close(a, b)) == 1 + assert Nx.to_number(Nx.Defn.jit(&all_close_defn/2, compiler: EMLX).(a, b)) == 1 + end + test "all_close: not-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, 4.0], type: :f32, backend: EMLX.Backend)