diff --git a/.github/workflows/emlx.yml b/.github/workflows/emlx.yml index 6e50dad..8fd1392 100644 --- a/.github/workflows/emlx.yml +++ b/.github/workflows/emlx.yml @@ -72,7 +72,7 @@ jobs: mix test --warnings-as-errors macos: - name: macOS (${{ matrix.job.elixir }}, ${{ matrix.job.otp }}) + name: macOS ${{ matrix.job.gpu && 'gpu' || 'cpu' }} (${{ matrix.job.elixir }}, ${{ matrix.job.otp }}) runs-on: macos-26 strategy: fail-fast: false diff --git a/emlx/c_src/emlx_nif.cpp b/emlx/c_src/emlx_nif.cpp index b62948d..f18a408 100644 --- a/emlx/c_src/emlx_nif.cpp +++ b/emlx/c_src/emlx_nif.cpp @@ -218,7 +218,7 @@ NIF(to_blob) { } uint64_t elem_count(std::vector shape) { - return std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<>{}); + return std::accumulate(shape.begin(), shape.end(), uint64_t{1}, std::multiplies{}); } NIF(from_blob) { diff --git a/emlx/test/emlx_test.exs b/emlx/test/emlx_test.exs index 53cc1ae..8d2a33a 100644 --- a/emlx/test/emlx_test.exs +++ b/emlx/test/emlx_test.exs @@ -170,4 +170,21 @@ defmodule EMLXTest do end end end + + # Regression: elem_count overflowed int32 for shapes whose element count + # exceeds INT32_MAX, causing "Binary size is too small" on valid binaries. + @tag :large_memory + test "from_binary accepts shape whose element count exceeds INT32_MAX" do + # Reshape on BinaryBackend first — Nx.from_binary creates a flat 1D tensor + # whose single dimension would also exceed INT32_MAX. + int_32_max = 2 ** 31 + binary = String.duplicate(<<7>>, int_32_max) + shape = {2, div(int_32_max, 2)} + + out = Nx.template(shape, :u8) + t = EMLX.Backend.from_binary(out, binary, backend: EMLX.Backend) + + assert Nx.shape(t) == shape + assert_equal(Nx.all(Nx.equal(t, 7)), 1) + end end diff --git a/emlx/test/test_helper.exs b/emlx/test/test_helper.exs index 4cf03a2..a6ed859 100644 --- a/emlx/test/test_helper.exs +++ b/emlx/test/test_helper.exs @@ -51,7 +51,7 @@ distributed_exclude = gpu_exclude = case EMLX.NIF.command_queue_new(:gpu) do - {:ok, _} -> [] + {:ok, _} -> [:large_memory] {:error, _} -> [:metal] end