forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize_state_flow_graph.py
165 lines (120 loc) · 2.73 KB
/
visualize_state_flow_graph.py
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
import taichi as ti
def test_fusion_range():
ti.init(arch=ti.cpu,
async_mode=True,
async_opt_fusion=False,
async_opt_intermediate_file="fusion_range")
x = ti.field(ti.i32)
y = ti.field(ti.i32)
z = ti.field(ti.i32)
n = 128
block = ti.root.dense(ti.i, n)
block.place(x, y, z)
@ti.kernel
def foo():
for i in range(n):
y[i] = x[i] + 1
@ti.kernel
def bar():
for i in range(n):
z[i] = y[i] + 1
foo()
bar()
foo()
ti.sync()
def test_fusion():
ti.init(arch=ti.cpu,
async_mode=True,
async_opt_intermediate_file="fusion",
async_opt_fusion=False)
x = ti.field(ti.i32)
y = ti.field(ti.i32)
z = ti.field(ti.i32)
num_dense_layers = 1
block = ti.root.pointer(ti.i, 128)
for i in range(num_dense_layers):
block = block.dense(ti.i, 2)
block.place(x, y, z)
@ti.kernel
def foo():
for i in x:
y[i] = x[i] + 1
@ti.kernel
def bar():
for i in y:
z[i] = y[i] + 1
foo()
bar()
ti.sync()
def test_write_after_read():
ti.init(arch=ti.cpu, async_mode=True, async_opt_intermediate_file="war")
x = ti.field(ti.i32, shape=16)
@ti.kernel
def p():
print(x[ti.random(ti.i32) % 16])
@ti.kernel
def s():
x[ti.random(ti.i32) % 16] = 3
p()
p()
p()
s()
p()
p()
s()
s()
s()
ti.sync()
def test_multiple_listgens():
ti.init(arch=ti.cpu,
async_mode=True,
async_opt_fusion=False,
async_opt_intermediate_file="multiple_listgens")
x = ti.field(ti.i32)
y = ti.field(ti.i32)
z = ti.field(ti.i32)
ti.root.pointer(ti.i, 32).dense(ti.i, 2).place(x, y, z)
@ti.kernel
def foo():
for i in x:
y[i] = x[i] + 1
@ti.kernel
def bar():
for i in x:
z[i] = y[i] + 1
@ti.kernel
def hello():
for i in x:
x[i] = z[i] + 1
foo()
bar()
hello()
@ti.kernel
def fill():
x[0] = 10
fill()
foo()
bar()
hello()
ti.sync()
def test_activation_demotion():
ti.init(arch=ti.cpu,
async_mode=True,
async_opt_fusion=False,
async_opt_intermediate_file="act")
x = ti.field(ti.i32)
y = ti.field(ti.i32)
ti.root.pointer(ti.i, 32).dense(ti.i, 2).place(x)
ti.root.pointer(ti.i, 32).dense(ti.i, 2).place(y)
@ti.kernel
def restrict():
for i in x:
y[i // 2] = 1
restrict()
restrict()
ti.sync()
test_fusion()
test_fusion_range()
test_write_after_read()
test_multiple_listgens()
test_activation_demotion()