-
Notifications
You must be signed in to change notification settings - Fork 473
Fix gradient propagation for in-place array assignments #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
aff4bb9
ae918a6
ebab221
d195b7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
@@ -3139,7 +3151,7 @@ def strip_indices(indices, count): | |
|
|
||
| return indices | ||
|
|
||
| def recurse_subscript(adj, node, indices): | ||
| def recurse_subscript(adj, node, indices, allow_partial=True): | ||
| if isinstance(node, ast.Name): | ||
| target = adj.eval(node) | ||
| return target, indices | ||
|
|
@@ -3161,10 +3173,10 @@ def recurse_subscript(adj, node, indices): | |
|
|
||
| indices = [ij, *indices] # prepend | ||
|
|
||
| target, indices = adj.recurse_subscript(node.value, indices) | ||
| target, indices = adj.recurse_subscript(node.value, indices, allow_partial) | ||
|
|
||
| target_type = strip_reference(target.type) | ||
| if is_array(target_type): | ||
| if is_array(target_type) and allow_partial: | ||
| flat_indices = [i for ij in indices for i in ij] | ||
| if len(flat_indices) > target_type.ndim: | ||
| target = adj.emit_indexing(target, flat_indices[: target_type.ndim]) | ||
|
|
@@ -3176,8 +3188,8 @@ def recurse_subscript(adj, node, indices): | |
| return target, indices | ||
|
|
||
| # returns the object being indexed, and the list of indices | ||
| def eval_subscript(adj, node): | ||
| target, indices = adj.recurse_subscript(node, []) | ||
| def eval_subscript(adj, node, allow_partial=True): | ||
| target, indices = adj.recurse_subscript(node, [], allow_partial) | ||
| flat_indices = [i for ij in indices for i in ij] | ||
| return target, flat_indices | ||
|
|
||
|
|
@@ -3211,7 +3223,7 @@ def emit_Assign(adj, node): | |
| # more generally in `adj.eval()`. | ||
| if isinstance(node.value, ast.List): | ||
| raise WarpCodegenError( | ||
| "List constructs are not supported in kernels. Use vectors like `wp.vec3()` for small fixed-size collections, or `wp.zeros(shape=N, dtype=...)` for stack-allocated arrays." | ||
| "List constructs are not supported in kernels. Use vectors like `wp.vec3()` for small collections instead." | ||
| ) | ||
|
|
||
| lhs = node.targets[0] | ||
|
|
@@ -3273,22 +3285,45 @@ def emit_Assign(adj, node): | |
| adj.add_forward(f"{var.emit()} = {rhs.emit()};") | ||
| return | ||
|
|
||
| target, indices = adj.eval_subscript(lhs) | ||
| target, indices = adj.eval_subscript(lhs, allow_partial=False) | ||
|
|
||
| target_type = strip_reference(target.type) | ||
| indices = adj.eval_indices(target_type, indices) | ||
|
|
||
| if is_array(target_type): | ||
| adj.add_builtin_call("array_store", [target, *indices, rhs]) | ||
| if len(indices) > target_type.ndim: | ||
| # Array vector/matrix component assignment | ||
| array_indices = indices[: target_type.ndim] | ||
| vec_indices = indices[target_type.ndim :] | ||
|
|
||
| if warp.config.verify_autograd_array_access: | ||
| kernel_name = adj.fun_name | ||
| filename = adj.filename | ||
| lineno = adj.lineno + adj.fun_lineno | ||
| array_indices = adj.eval_indices(target_type, array_indices) | ||
|
|
||
| target.mark_write(kernel_name=kernel_name, filename=filename, lineno=lineno) | ||
| vec_target = adj.emit_indexing(target, array_indices) | ||
| vec_target_type = strip_reference(vec_target.type) | ||
|
|
||
| vec_indices = adj.eval_indices(vec_target_type, vec_indices) | ||
|
|
||
| new_vec = adj.add_builtin_call("assign_copy", [vec_target, *vec_indices, rhs]) | ||
| adj.add_builtin_call("array_store", [target, *array_indices, new_vec]) | ||
|
|
||
| if warp.config.verify_autograd_array_access: | ||
| kernel_name = adj.fun_name | ||
| filename = adj.filename | ||
| lineno = adj.lineno + adj.fun_lineno | ||
| target.mark_write(kernel_name=kernel_name, filename=filename, lineno=lineno) | ||
| return | ||
| else: | ||
| indices = adj.eval_indices(target_type, indices) | ||
| adj.add_builtin_call("array_store", [target, *indices, rhs]) | ||
|
|
||
| elif is_tile(target_type): | ||
| if warp.config.verify_autograd_array_access: | ||
| kernel_name = adj.fun_name | ||
| filename = adj.filename | ||
| lineno = adj.lineno + adj.fun_lineno | ||
| target.mark_write(kernel_name=kernel_name, filename=filename, lineno=lineno) | ||
| return | ||
|
|
||
| indices = adj.eval_indices(target_type, indices) | ||
| if is_tile(target_type): | ||
| adj.add_builtin_call("assign", [target, *indices, rhs]) | ||
|
|
||
| elif ( | ||
|
|
@@ -3307,7 +3342,6 @@ def emit_Assign(adj, node): | |
| adj.add_builtin_call("store", [attr, rhs]) | ||
| return | ||
|
|
||
| # TODO: array vec component case | ||
| if is_reference(target.type): | ||
| attr = adj.add_builtin_call("indexref", [target, *indices]) | ||
| adj.add_builtin_call("store", [attr, rhs]) | ||
|
|
@@ -3502,8 +3536,6 @@ def make_new_assign_statement(): | |
| new_node = ast.Assign(targets=[lhs], value=ast.BinOp(lhs, node.op, node.value)) | ||
| adj.eval(new_node) | ||
|
|
||
| rhs = adj.eval(node.value) | ||
|
|
||
| if isinstance(lhs, ast.Subscript): | ||
| # wp.adjoint[var] appears in custom grad functions, and does not require | ||
| # special consideration in the AugAssign case | ||
|
|
@@ -3512,8 +3544,34 @@ def make_new_assign_statement(): | |
| return | ||
|
|
||
| target, indices = adj.eval_subscript(lhs) | ||
|
|
||
| target_type = strip_reference(target.type) | ||
|
|
||
| if is_reference(target.type): | ||
| if is_array(target_type) and len(indices) > target_type.ndim: | ||
| rhs = adj.eval(node.value) | ||
|
|
||
| array_indices = indices[: target_type.ndim] | ||
| vec_indices = indices[target_type.ndim :] | ||
|
|
||
| array_indices = adj.eval_indices(target_type, array_indices) | ||
| vec_target = adj.emit_indexing(target, array_indices) | ||
| vec_target_type = strip_reference(vec_target.type) | ||
|
|
||
| vec_indices = adj.eval_indices(vec_target_type, vec_indices) | ||
| old_val = adj.emit_indexing(vec_target, vec_indices) | ||
|
|
||
| op_name = builtin_operators[type(node.op)] | ||
| new_val = adj.add_builtin_call(op_name, [old_val, rhs]) | ||
|
|
||
| new_vec = adj.add_builtin_call("assign_copy", [vec_target, *vec_indices, new_val]) | ||
| adj.add_builtin_call("array_store", [target, *array_indices, new_vec]) | ||
| return | ||
|
Comment on lines
+3549
to
+3568
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
At line 3553, The correct approach is to split the indices first (as is done later for the write-back path at lines 3557–3567) and index step by step: if is_array(target_type) and len(indices) > target_type.ndim:
rhs = adj.eval(node.value)
array_indices = indices[:target_type.ndim]
vec_indices = indices[target_type.ndim:]
array_indices = adj.eval_indices(target_type, array_indices)
vec_target = adj.emit_indexing(target, array_indices)
vec_target_type = strip_reference(vec_target.type)
vec_indices = adj.eval_indices(vec_target_type, vec_indices)
old_val = adj.emit_indexing(vec_target, vec_indices)
op_name = builtin_operators[type(node.op)]
new_val = adj.add_builtin_call(op_name, [old_val, rhs])
new_vec = adj.add_builtin_call("assign_copy", [vec_target, *vec_indices, new_val])
adj.add_builtin_call("array_store", [target, *array_indices, new_vec])
returnNote: while in practice |
||
| else: | ||
| make_new_assign_statement() | ||
| return | ||
|
|
||
| rhs = adj.eval(node.value) | ||
|
|
||
| indices = adj.eval_indices(target_type, indices) | ||
|
|
||
| if is_array(target_type): | ||
|
|
@@ -3659,6 +3717,9 @@ def emit_Pass(adj, node): | |
| } | ||
|
|
||
| def eval(adj, node): | ||
| if isinstance(node, Var): | ||
| return node | ||
|
|
||
| if hasattr(node, "lineno"): | ||
| adj.set_lineno(node.lineno - 1) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.