Skip to content

Commit 85b0f36

Browse files
fix: SelectiveScan NIF test shape mismatch on EXLA backend
- Use explicit {_, tensor} pattern matching instead of |> elem(1) for Nx.Random.uniform (EXLA may return key/tensor differently in JIT context) - Transfer all tensors to BinaryBackend before NIF calls (NIF uses Nx.to_binary) - Remove @tag :slow to prevent --include slow from pulling in NIF tests (module already gated by @moduletag :nif and :cuda) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent dcbbc63 commit 85b0f36

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

test/exphil/native/selective_scan_test.exs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ defmodule ExPhil.Native.SelectiveScanTest do
171171
end
172172

173173
@tag :requires_nif
174-
@tag :slow
175174
test "backward gradient numerical check" do
176175
# Numerical gradient check: compare backward kernel to finite differences
177176
skip_unless_nif_available()
@@ -182,11 +181,17 @@ defmodule ExPhil.Native.SelectiveScanTest do
182181
state = 2
183182
eps = 1.0e-4
184183

185-
x = Nx.Random.uniform(Nx.Random.key(42), shape: {batch, seq_len, hidden}, type: :f32) |> elem(1)
186-
dt = Nx.broadcast(0.05, {batch, seq_len, hidden}) |> Nx.as_type(:f32)
187-
a = Nx.broadcast(-1.0, {hidden, state}) |> Nx.as_type(:f32)
188-
b = Nx.Random.uniform(Nx.Random.key(43), shape: {batch, seq_len, state}, type: :f32) |> elem(1)
189-
c = Nx.Random.uniform(Nx.Random.key(44), shape: {batch, seq_len, state}, type: :f32) |> elem(1)
184+
# Use explicit pattern matching (not |> elem(1)) and transfer to BinaryBackend
185+
# because: 1) EXLA may return {key, tensor} differently in JIT context
186+
# 2) NIF calls Nx.to_binary() which needs CPU tensors
187+
{_, x} = Nx.Random.uniform(Nx.Random.key(42), shape: {batch, seq_len, hidden}, type: :f32)
188+
x = Nx.backend_transfer(x, Nx.BinaryBackend)
189+
dt = Nx.broadcast(0.05, {batch, seq_len, hidden}) |> Nx.as_type(:f32) |> Nx.backend_transfer(Nx.BinaryBackend)
190+
a = Nx.broadcast(-1.0, {hidden, state}) |> Nx.as_type(:f32) |> Nx.backend_transfer(Nx.BinaryBackend)
191+
{_, b} = Nx.Random.uniform(Nx.Random.key(43), shape: {batch, seq_len, state}, type: :f32)
192+
b = Nx.backend_transfer(b, Nx.BinaryBackend)
193+
{_, c} = Nx.Random.uniform(Nx.Random.key(44), shape: {batch, seq_len, state}, type: :f32)
194+
c = Nx.backend_transfer(c, Nx.BinaryBackend)
190195

191196
# Compute analytical gradient
192197
{out, h_all} = SelectiveScan.scan_with_states(x, dt, a, b, c)

0 commit comments

Comments
 (0)