Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion emlx/c_src/emlx_nif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ NIF(to_blob) {
}

uint64_t elem_count(std::vector<int> shape) {
return std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<>{});
return std::accumulate(shape.begin(), shape.end(), uint64_t{1}, std::multiplies<uint64_t>{});
Comment thread
edenamram marked this conversation as resolved.
}

NIF(from_blob) {
Expand Down
16 changes: 16 additions & 0 deletions emlx/test/emlx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,20 @@ 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.
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
Loading