8000 Support gradients for multi-dimensional indexed arrays by aneangel · Pull Request #1 · aneangel/warp · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions changelog/+indexedarray-multidim-grads.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add gradient support for multi-dimensional `wp.indexedarray` kernel inputs. Gradients previously worked only for 1-D
indexed arrays (`NotImplementedError` for higher ranks); the adjoint now follows the per-dimension gather indirection
for 2-D, 3-D, and 4-D indexed arrays, including views that mix indexed and passthrough dimensions.
7 changes: 0 additions & 7 deletions warp/_src/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10469,13 +10469,6 @@ def pack_arg(kernel, arg_type, arg_name, value, device, adjoint=False):
# wrapped in an indexedarray_t with no indices; the adjoint kernel
# accumulates at indices already remapped through the forward argument.
if adjoint and warp._src.types.matches_array_class(arg_type, warp.indexedarray):
# match indexedarray.grad: without dedicated multi-dimensional adjoints
# the adjoint kernel would match the no-op generic adj_address and
# silently produce zero gradients
if arg_type.ndim > 1:
raise NotImplementedError(
"Gradient propagation through indexed arrays is only supported for 1-D indexed arrays"
)
if isinstance(value, warp.array):
return warp._src.types.indexedarray_t(value, [None] * value.ndim, value.shape)

Expand Down
4 changes: 0 additions & 4 deletions warp/_src/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5354,10 +5354,6 @@ def __ctype__(self):
def grad(self):
if self.data is None or self.data.grad is None:
return None
if self.ndim > 1:
raise NotImplementedError(
"Gradient propagation through indexed arrays is only supported for 1-D indexed arrays"
)
return indexedarray(self.data.grad, self.indices[: self.ndim], ndim=self.ndim)

@property
Expand Down
141 changes: 141 additions & 0 deletions warp/native/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,147 @@ adj_address(const indexedarray_t<T>& buf, int i, const array_t<T>& adj_buf, int
else if (buf.arr.grad)
adj_atomic_add(&index_grad(buf.arr, i), adj_output);
}
// 2-D/3-D/4-D indexedarray adjoints: same indexedarray_t/array_t CPU-CUDA codegen
// split as the 1-D overloads above, with each dimension's negative coordinates
// and optional index remap resolved independently
template <typename T>
inline CUDA_CALLABLE void adj_address(
const indexedarray_t<T>& buf, int i, int j, const indexedarray_t<T>& adj_buf, int adj_i, int adj 8000 _j, const T& adj_output
)
{
if (i < 0)
i += buf.shape[0];
if (j < 0)
j += buf.shape[1];
if (buf.indices[0])
i = buf.indices[0][i];
if (buf.indices[1])
j = buf.indices[1][j];

if (adj_buf.arr.data)
adj_atomic_add(&index(adj_buf.arr, i, j), adj_output);
else if (buf.arr.grad)
adj_atomic_add(&index_grad(buf.arr, i, j), adj_output);
}
template <typename T>
inline CUDA_CALLABLE void adj_address(
const indexedarray_t<T>& buf, int i, int j, const array_t<T>& adj_buf, int adj_i, int adj_j, const T& adj_output
)
{
if (i < 0)
i += buf.shape[0];
if (j < 0)
j += buf.shape[1];
if (buf.indices[0])
i = buf.indices[0][i];
if (buf.indices[1])
j = buf.indices[1][j];

if (adj_buf.data)
adj_atomic_add(&index(adj_buf, i, j), adj_output);
else if (buf.arr.grad)
adj_atomic_add(&index_grad(buf.arr, i, j), adj_output);
}
template <typename T>
inline CUDA_CALLABLE void adj_address(
const indexedarray_t<T>& buf, int i, int j, int k, const indexedarray_t<T>& adj_buf, int adj_i, int adj_j, int adj_k, const T& adj_output
)
{
if (i < 0)
i += buf.shape[0];
if (j < 0)
j += buf.shape[1];
if (k < 0)
k += buf.shape[2];
if (buf.indices[0])
i = buf.indices[0][i];
if (buf.indices[1])
j = buf.indices[1][j];
if (buf.indices[2])
k = buf.indices[2][k];

if (adj_buf.arr.data)
adj_atomic_add(&index(adj_buf.arr, i, j, k), adj_output);
else if (buf.arr.grad)
adj_atomic_add(&index_grad(buf.arr, i, j, k), adj_output);
}
template <typename T>
inline CUDA_CALLABLE void adj_address(
const indexedarray_t<T>& buf, int i, int j, int k, const array_t<T>& adj_buf, int adj_i, int adj_j, int adj_k, const T& adj_output
)
{
if (i < 0)
i += buf.shape[0];
if (j < 0)
j += buf.shape[1];
if (k < 0)
k += buf.shape[2];
if (buf.indices[0])
i = buf.indices[0][i];
if (buf.indices[1])
j = buf.indices[1][j];
if (buf.indices[2])
k = buf.indices[2][k];

if (adj_buf.data)
adj_atomic_add(&index(adj_buf, i, j, k), adj_output);
else if (buf.arr.grad)
adj_atomic_add(&index_grad(buf.arr, i, j, k), adj_output);
}
template <typename T>
inline CUDA_CALLABLE void adj_address(
const indexedarray_t<T>& buf, int i, int j, int k, int l, const indexedarray_t<T>& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, const T& adj_output
)
{
if (i < 0)
i += buf.shape[0];
if (j < 0)
j += buf.shape[1];
if (k < 0)
k += buf.shape[2];
if (l < 0)
l += buf.shape[3];
if (buf.indices[0])
i = buf.indices[0][i];
if (buf.indices[1])
j = buf.indices[1][j];
if (buf.indices[2])
k = buf.indices[2][k];
if (buf.indices[3])
l = buf.indices[3][l];

if (adj_buf.arr.data)
adj_atomic_add(&index(adj_buf.arr, i, j, k, l), adj_output);
else if (buf.arr.grad)
adj_atomic_add(&index_grad(buf.arr, i, j, k, l), adj_output);
}
template <typename T>
inline CUDA_CALLABLE void adj_address(
const indexedarray_t<T>& buf, int i, int j, int k, int l, const array_t<T>& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, const T& adj_output
)
{
if (i < 0)
i += buf.shape[0];
if (j < 0)
j += buf.shape[1];
if (k < 0)
k += buf.shape[2];
if (l < 0)
l += buf.shape[3];
if (buf.indices[0])
i = buf.indices[0][i];
if (buf.indices[1])
j = buf.indices[1][j];
if (buf.indices[2])
k = buf.indices[2][k];
if (buf.indices[3])
l = buf.indices[3][l];

if (adj_buf.data)
adj_atomic_add(&index(adj_buf, i, j, k, l), adj_output);
else if (buf.arr.grad)
adj_atomic_add(&index_grad(buf.arr, i, j, k, l), adj_output);
}
template <typename T>
inline CUDA_CALLABLE void
adj_address(const array_t<T>& buf, int i, int j, const array_t<T>& adj_buf, int adj_i, int adj_j, const T& adj_output)
Expand Down
157 changes: 139 additions & 18 deletions warp/tests/test_indexedarray.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -1392,11 +1392,9 @@ def kernel_indexedarray_grad_2d(samples: wp.indexedarray2d(dtype=float), total:
wp.atomic_add(total, 0, samples[i, j])


def test_indexedarray_grad_2d_not_implemented(test, device):
# gradients of multi-dimensional indexed arrays are not implemented: without
# dedicated adjoints the backward kernel would match the no-op generic
# adj_address and silently produce zero gradients, so both the tape and the
# manual adjoint launch must raise instead
def test_indexedarray_grad_2d(test, device):
# gradients must flow back through 2-D indexed views, remapping each indexed
# dimension and passing unindexed dimensions through
base = wp.array(np.arange(16, dtype=np.float32).reshape(4, 4), dtype=float, device=device, requires_grad=True)
rows = wp.array([1, 3], dtype=int, device=device)
samples = wp.indexedarray2d(base, [rows, None])
Expand All @@ -1406,22 +1404,142 @@ def test_indexedarray_grad_2d_not_implemented(test, device):
with tape:
wp.launch(kernel_indexedarray_grad_2d, dim=samples.shape, inputs=[samples], outputs=[total], device=device)

with test.assertRaisesRegex(NotImplementedError, "only supported for 1-D indexed arrays"):
tape.backward(loss=total)
assert_np_equal(total.numpy(), np.array([76.0], dtype=np.float32), tol=1e-6)

tape.backward(loss=total)

# d(total)/d(base[r, c]) is 1 on the gathered rows, zero elsewhere
expected = np.zeros((4, 4), dtype=np.float32)
expected[[1, 3], :] = 1.0
assert_np_equal(base.grad.numpy(), expected, tol=1e-6)

# the manual adjoint launch accepts the base array's gradient as a plain array
base.grad.zero_()
total.grad.fill_(1.0)
with test.assertRaisesRegex(NotImplementedError, "only supported for 1-D indexed arrays"):
wp.launch(
kernel_indexedarray_grad_2d,
dim=samples.shape,
inputs=[samples],
outputs=[total],
adj_inputs=[base.grad],
adj_outputs=[total.grad],
adjoint=True,
device=device,
)
assert_np_equal(base.grad.numpy(), expected, tol=1e-6)


@wp.kernel
def kernel_indexedarray_grad_3d(samples: wp.indexedarray3d(dtype=float), total: wp.array(dtype=float)):
i, j, k = wp.tid()
wp.atomic_add(total, 0, samples[i, j, k])


def test_indexedarray_grad_3d(test, device):
# index arrays on the first and last dimensions, passthrough in the middle
base = wp.array(np.arange(27, dtype=np.float32).reshape(3, 3, 3), dtype=float, device=device, requires_grad=True)
ids0 = wp.array([0, 2], dtype=int, device=device)
ids2 = wp.array([1], dtype=int, device=device)
samples = wp.indexedarray3d(base, [ids0, None, ids2])
total = wp.zeros(1, dtype=float, device=device, requires_grad=True)

tape = wp.Tape()
with tape:
wp.launch(kernel_indexedarray_grad_3d, dim=samples.shape, inputs=[samples], outputs=[total], device=device)

tape.backward(loss=total)

expected = np.zeros((3, 3, 3), dtype=np.float32)
expected[[0, 2], :, 1] = 1.0
assert_np_equal(base.grad.numpy(), expected, tol=1e-6)

# the manual adjoint launch accepts the base array's gradient as a plain array
base.grad.zero_()
total.grad.fill_(1.0)
wp.launch(
kernel_indexedarray_grad_3d,
dim=samples.shape,
inputs=[samples],
outputs=[total],
adj_inputs=[base.grad],
adj_outputs=[total.grad],
adjoint=True,
device=device,
)
assert_np_equal(base.grad.numpy(), expected, tol=1e-6)


@wp.kernel
def kernel_indexedarray_grad_4d(samples: wp.indexedarray4d(dtype=float), total: wp.array(dtype=float)):
i, j, k, l = wp.tid()
wp.atomic_add(total, 0, samples[i, j, k, l])


def test_indexedarray_grad_4d(test, device):
base = wp.array(
np.arange(16, dtype=np.float32).reshape(2, 2, 2, 2), dtype=float, device=device, requires_grad=True
)
ids0 = wp.array([1], dtype=int, device=device)
samples = wp.indexedarray4d(base, [ids0, None, None, None])
total = wp.zeros(1, dtype=float, device=device, requires_grad=True)

tape = wp.Tape()
with tape:
wp.launch(kernel_indexedarray_grad_4d, dim=samples.shape, inputs=[samples], outputs=[total], device=device)

tape.backward(loss=total)

expected = np.zeros((2, 2, 2, 2), dtype=np.float32)
expected[1, :, :, :] = 1.0
assert_np_equal(base.grad.numpy(), expected, tol=1e-6)

# the manual adjoint launch accepts the base array's gradient as a plain array
base.grad.zero_()
total.grad.fill_(1.0)
wp.launch(
kernel_indexedarray_grad_4d,
dim=samples.shape,
inputs=[samples],
outputs=[total],
adj_inputs=[base.grad],
adj_outputs=[total.grad],
adjoint=True,
device=device,
)
assert_np_equal(base.grad.numpy(), expected, tol=1e-6)


@wp.kernel
def kernel_indexedarray_grad_2d_negative(samples: wp.indexedarray2d(dtype=float), total: wp.array(dtype=float)):
i, j = wp.tid()
# negative view coordinate on the gathered dimension wraps around before the remap
wp.atomic_add(total, 0, samples[i - 2, j])


def test_indexedarray_grad_2d_negative_indices(test, device):
# each new 2-D/3-D/4-D adjoint overload hand-codes its own per-dimension wrap-around,
# so verify it against the forward read: a negative view coordinate wraps before the
# per-dimension index remap, same as the 1-D case in test_indexedarray_grad_1d_negative_indices
base = wp.array(np.arange(16, dtype=np.float32).reshape(4, 4), dtype=float, device=device, requires_grad=True)
rows = wp.array([1, 3], dtype=int, device=device)
samples = wp.indexedarray2d(base, [rows, None])
total = wp.zeros(1, dtype=float, device=device, requires_grad=True)

tape = wp.Tape()
with tape:
wp.launch(
kernel_indexedarray_grad_2d,
dim=samples.shape,
inputs=[samples],
outputs=[total],
adj_inputs=[base.grad],
adj_outputs=[total.grad],
adjoint=True,
device=device,
kernel_indexedarray_grad_2d_negative, dim=samples.shape, inputs=[samples], outputs=[total], device=device
)

# samples[i - 2, j] wraps to samples[i, j], so the result matches test_indexedarray_grad_2d
assert_np_equal(total.numpy(), np.array([76.0], dtype=np.float32), tol=1e-6)

tape.backward(loss=total)

expected = np.zeros((4, 4), dtype=np.float32)
expected[[1, 3], :] = 1.0
assert_np_equal(base.grad.numpy(), expected, tol=1e-6)


devices = get_test_devices()

Expand All @@ -1447,12 +1565,15 @@ class TestIndexedArray(unittest.TestCase):
test_indexedarray_grad_1d_negative_indices,
devices=devices,
)
add_function_test(TestIndexedArray, "test_indexedarray_grad_2d", test_indexedarray_grad_2d, devices=devices)
add_function_test(
TestIndexedArray,
"test_indexedarray_grad_2d_not_implemented",
test_indexedarray_grad_2d_not_implemented,
"test_indexedarray_grad_2d_negative_indices",
test_indexedarray_grad_2d_negative_indices,
devices=devices,
)
add_function_test(TestIndexedArray, "test_indexedarray_grad_3d", test_indexedarray_grad_3d, devices=devices)
add_function_test(TestIndexedArray, "test_indexedarray_grad_4d", test_indexedarray_grad_4d, devices=devices)
add_function_test(TestIndexedArray, "test_indexedarray_2d", test_indexedarray_2d, devices=devices)
add_function_test(TestIndexedArray, "test_indexedarray_3d", test_indexedarray_3d, devices=devices)
add_function_test(TestIndexedArray, "test_indexedarray_4d", test_indexedarray_4d, devices=devices)
Expand Down
0