Skip to content

Commit afc03c5

Browse files
authored
Fix GPU pad for outputs larger than 2^31 elements (ml-explore#4744)
1 parent 53c1870 commit afc03c5

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎mlx/backend/gpu/slicing.cpp‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ void pad_gpu(
2525
// Fill output with val
2626
fill_gpu(val, out, s);
2727

28-
// Find offset for start of input values
29-
size_t data_offset = 0;
28+
// Find start of input values
29+
Shape start(out.ndim(), 0);
3030
for (int i = 0; i < axes.size(); i++) {
3131
auto ax = axes[i] < 0 ? out.ndim() + axes[i] : axes[i];
32-
data_offset += out.strides()[ax] * low_pad_size[i];
32+
start[ax] += low_pad_size[i];
3333
}
3434

3535
// Extract slice from output where input will be pasted
3636
array out_slice(in.shape(), out.dtype(), nullptr, {});
37-
out_slice.copy_shared_buffer(
38-
out, out.strides(), out.flags(), out_slice.size(), data_offset);
37+
slice(out, out_slice, start, Shape(out.ndim(), 1));
3938

4039
// Copy input values into the slice
4140
copy_gpu_inplace(in, out_slice, CopyType::GeneralGeneral, s);

‎python/tests/test_ops.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,13 @@ def f(x):
25792579
_, df = mx.vjp(f, [a_fwd], [a_bwd])
25802580
self.assertTrue(mx.allclose(a_bwd[4:-2, 2:-4], df[0]).item())
25812581

2582+
def test_pad_large_offsets(self):
2583+
# Row 2**27 of the output starts at offset 2**31
2584+
rows = 2**27 + 1
2585+
x = mx.broadcast_to(mx.array([[1, 2, 3]], mx.uint8), (rows, 3))
2586+
y = mx.pad(x, ((0, 0), (0, 13)))
2587+
self.assertEqual(y[-1].tolist(), [1, 2, 3] + [0] * 13)
2588+
25822589
def test_where(self):
25832590
self.assertCmpNumpy([True, mx.array([[1, 2], [3, 4]]), 1], mx.where, np.where)
25842591
self.assertCmpNumpy([True, 1, mx.array([[1, 2], [3, 4]])], mx.where, np.where)

0 commit comments

Comments
 (0)