Skip to content
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

[autodiff] Skip store forwarding to keep the GlobalLoadStmt alive #5315

Merged
merged 7 commits into from
Jul 5, 2022
Merged
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
16 changes: 15 additions & 1 deletion taichi/ir/control_flow_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,21 @@ bool CFGNode::store_to_load_forwarding(bool after_lower_access) {
}
} else if (auto global_load = stmt->cast<GlobalLoadStmt>()) {
if (!after_lower_access) {
result = get_store_forwarding_data(global_load->src, i);
bool store_forwarding = true;
if (auto global_ptr = global_load->src->cast<GlobalPtrStmt>()) {
TI_ASSERT(global_ptr->width() == 1);
auto &snodes = global_ptr->snodes;
if (snodes[0]->has_adjoint()) {
// Has adjoint SNode. Skip the store forwarding
// to keep the global load chain,
// so that the grad of intermidiate variable can be computed
// by GlobalLoadStmt
store_forwarding = false;
}
}
if (store_forwarding) {
result = get_store_forwarding_data(global_load->src, i);
}
}
}
if (result) {
Expand Down
2 changes: 1 addition & 1 deletion taichi/ir/snode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void SNode::end_shared_exp_placement() {
}

bool SNode::is_primal() const {
return grad_info->is_primal();
return grad_info && grad_info->is_primal();
}

bool SNode::has_adjoint() const {
Expand Down
28 changes: 28 additions & 0 deletions tests/python/test_ad_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,31 @@ def calc_loss(input_field: ti.template(), loss: ti.template()):
expected = np.modf(randoms)[0] * 2
for i in range(n):
assert grads[i] == test_utils.approx(expected[i], rel=1e-4)


@test_utils.test()
def test_ad_global_store_forwarding():
x = ti.field(dtype=ti.f32, shape=(), needs_grad=True)
a = ti.field(dtype=ti.f32, shape=(), needs_grad=True)
b = ti.field(dtype=ti.f32, shape=(), needs_grad=True)
c = ti.field(dtype=ti.f32, shape=(), needs_grad=True)
d = ti.field(dtype=ti.f32, shape=(), needs_grad=True)
e = ti.field(dtype=ti.f32, shape=(), needs_grad=True)

@ti.kernel
def func():
a[None] = x[None]
b[None] = a[None] * 2
c[None] = b[None] * 3
d[None] = c[None] * 4
e[None] = d[None] * 5

x[None] = 1

with ti.ad.Tape(loss=e):
func()
assert x.grad[None] == 120.0
assert a.grad[None] == 120.0
assert b.grad[None] == 60.0
assert c.grad[None] == 20.0
assert d.grad[None] == 5.0