forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autograd.cpp
209 lines (186 loc) · 6.89 KB
/
autograd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "taichi/ir/ir_builder.h"
#include "taichi/ir/statements.h"
#include "taichi/program/program.h"
void autograd() {
/*
import taichi as ti, numpy as np
ti.init()
n = 10
a = ti.field(ti.f32, n, needs_grad=True)
b = ti.field(ti.f32, n, needs_grad=True)
c = ti.field(ti.f32, n, needs_grad=True)
energy = ti.field(ti.f32, [], needs_grad=True)
@ti.kernel
def init():
for i in range(n):
a[i] = i
b[i] = i + 1
@ti.kernel
def cal():
for i in a:
c[i] += a[i] + (b[i] + b[i])
@ti.kernel
def support(): # this function will not appear in CHI Builder code
for i in a:
energy += c[i]
init()
with ti.ad.Tape(energy):
cal()
support()
print(a.grad)
print(b.grad)
print(c.to_numpy())
*/
using namespace taichi;
using namespace lang;
auto program = Program(Arch::x64);
int n = 10;
program.materialize_runtime();
auto *root = new SNode(0, SNodeType::root);
auto get_snode_grad = [&]() -> SNode * {
class GradInfoPrimal final : public SNode::GradInfoProvider {
public:
SNode *snode;
GradInfoPrimal(SNode *_snode) : snode(_snode) {
}
bool is_primal() const override {
return true;
}
SNodeGradType get_snode_grad_type() const override {
return SNodeGradType::kPrimal;
}
SNode *adjoint_snode() const override {
return snode;
}
SNode *dual_snode() const override {
return snode;
}
SNode *adjoint_checkbit_snode() const override {
return nullptr;
}
};
class GradInfoAdjoint final : public SNode::GradInfoProvider {
public:
GradInfoAdjoint() {
}
bool is_primal() const override {
return false;
}
SNodeGradType get_snode_grad_type() const override {
return SNodeGradType::kAdjoint;
}
SNode *adjoint_snode() const override {
return nullptr;
}
SNode *dual_snode() const override {
return nullptr;
}
SNode *adjoint_checkbit_snode() const override {
return nullptr;
}
};
auto *snode =
&root->dense(Axis(0), n, false).insert_children(SNodeType::place);
snode->dt = PrimitiveType::f32;
snode->grad_info = std::make_unique<GradInfoPrimal>(
&root->dense(Axis(0), n, false).insert_children(SNodeType::place));
snode->get_adjoint()->dt = PrimitiveType::f32;
snode->get_adjoint()->grad_info = std::make_unique<GradInfoAdjoint>();
return snode;
};
auto *a = get_snode_grad(), *b = get_snode_grad(), *c = get_snode_grad();
program.add_snode_tree(std::unique_ptr<SNode>(root), /*compile_only=*/false);
std::unique_ptr<Kernel> kernel_init, kernel_forward, kernel_backward,
kernel_ext;
{
IRBuilder builder;
auto *zero = builder.get_int32(0);
auto *one = builder.get_int32(1);
auto *n_stmt = builder.get_int32(n);
auto *loop = builder.create_range_for(zero, n_stmt, 0, 4);
{
auto _ = builder.get_loop_guard(loop);
auto *i = builder.get_loop_index(loop);
builder.create_global_store(builder.create_global_ptr(a, {i}), i);
builder.create_global_store(builder.create_global_ptr(b, {i}),
builder.create_add(i, one));
builder.create_global_store(builder.create_global_ptr(c, {i}), zero);
builder.create_global_store(
builder.create_global_ptr(a->get_adjoint(), {i}), zero);
builder.create_global_store(
builder.create_global_ptr(b->get_adjoint(), {i}), zero);
builder.create_global_store(
builder.create_global_ptr(c->get_adjoint(), {i}), one);
}
kernel_init =
std::make_unique<Kernel>(program, builder.extract_ir(), "init");
}
auto get_kernel_cal = [&](AutodiffMode autodiff_mode) -> Kernel * {
IRBuilder builder;
auto *loop = builder.create_struct_for(a, 0, 4);
{
auto _ = builder.get_loop_guard(loop);
auto *i = builder.get_loop_index(loop);
auto *a_i = builder.create_global_load(builder.create_global_ptr(a, {i}));
auto *b_i = builder.create_global_load(builder.create_global_ptr(b, {i}));
auto *val = builder.create_add(a_i, builder.create_mul(b_i, i));
auto *c_i = builder.create_global_ptr(c, {i});
builder.insert(
std::make_unique<AtomicOpStmt>(AtomicOpType::add, c_i, val));
}
return new Kernel(program, builder.extract_ir(), "cal", autodiff_mode);
};
kernel_forward = std::unique_ptr<Kernel>(get_kernel_cal(AutodiffMode::kNone));
kernel_backward =
std::unique_ptr<Kernel>(get_kernel_cal(AutodiffMode::kReverse));
{
IRBuilder builder;
auto *loop = builder.create_struct_for(a, 0, 4);
{
auto _ = builder.get_loop_guard(loop);
auto *i = builder.get_loop_index(loop);
auto *ext_a = builder.create_external_ptr(
builder.create_arg_load(0, PrimitiveType::f32, true), {i});
auto *a_grad_i = builder.create_global_load(
builder.create_global_ptr(a->get_adjoint(), {i}));
builder.create_global_store(ext_a, a_grad_i);
auto *ext_b = builder.create_external_ptr(
builder.create_arg_load(1, PrimitiveType::f32, true), {i});
auto *b_grad_i = builder.create_global_load(
builder.create_global_ptr(b->get_adjoint(), {i}));
builder.create_global_store(ext_b, b_grad_i);
auto *ext_c = builder.create_external_ptr(
builder.create_arg_load(2, PrimitiveType::f32, true), {i});
auto *c_i = builder.create_global_load(builder.create_global_ptr(c, {i}));
builder.create_global_store(ext_c, c_i);
}
kernel_ext = std::make_unique<Kernel>(program, builder.extract_ir(), "ext");
kernel_ext->insert_arr_arg(get_data_type<int>(), /*total_dim=*/1, {n});
kernel_ext->insert_arr_arg(get_data_type<int>(), /*total_dim=*/1, {n});
kernel_ext->insert_arr_arg(get_data_type<int>(), /*total_dim=*/1, {n});
}
auto ctx_init = kernel_init->make_launch_context();
auto ctx_forward = kernel_forward->make_launch_context();
auto ctx_backward = kernel_backward->make_launch_context();
auto ctx_ext = kernel_ext->make_launch_context();
std::vector<float> ext_a(n), ext_b(n), ext_c(n);
ctx_ext.set_arg_external_array_with_shape(0, taichi::uint64(ext_a.data()), n,
{n});
ctx_ext.set_arg_external_array_with_shape(1, taichi::uint64(ext_b.data()), n,
{n});
ctx_ext.set_arg_external_array_with_shape(2, taichi::uint64(ext_c.data()), n,
{n});
(*kernel_init)(ctx_init);
(*kernel_forward)(ctx_forward);
(*kernel_backward)(ctx_backward);
(*kernel_ext)(ctx_ext);
for (int i = 0; i < n; i++)
std::cout << ext_a[i] << " ";
std::cout << std::endl;
for (int i = 0; i < n; i++)
std::cout << ext_b[i] << " ";
std::cout << std::endl;
for (int i = 0; i < n; i++)
std::cout << ext_c[i] << " ";
std::cout << std::endl;
}