Bug Summary

File:nnc/ccv_nnc_symbolic_graph_backward.c
Warning:line 718, column 4
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ccv_nnc_symbolic_graph_backward.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-feature +sse2 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -resource-dir /usr/local/lib/clang/8.0.0 -I ../ -I /usr/local/cuda/include -D HAVE_CBLAS -D HAVE_LIBPNG -D HAVE_LIBJPEG -D HAVE_FFTW3 -D HAVE_PTHREAD -D HAVE_UCONTEXT -D HAVE_LIBLINEAR -D HAVE_TESSERACT -D HAVE_AVCODEC -D HAVE_AVFORMAT -D HAVE_AVUTIL -D HAVE_SWSCALE -D USE_DISPATCH -D HAVE_SSE2 -D HAVE_GSL -D HAVE_CUDA -D HAVE_CUDNN -D HAVE_NCCL -I /usr/local/include -internal-isystem /usr/local/include -internal-isystem /usr/local/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -fdebug-compilation-dir /home/liu/buildslave/linux-x64-runtests/build/lib/nnc -ferror-limit 19 -fmessage-length 0 -fblocks -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -o /home/liu/buildslave/public_html/analyze/2019-05-04-163002-105371-1 -x c ccv_nnc_symbolic_graph_backward.c -faddrsig
1#include "ccv_nnc.h"
2#include "ccv_nnc_easy.h"
3#include "ccv_nnc_internal.h"
4#include "ccv_internal.h"
5#include "_ccv_nnc_symbolic_graph.h"
6
7#pragma mark - Level-3.5 API
8
9typedef struct {
10 int f_wrt; // Check if both f_symbols and wrt_symbols flow through this node.
11 ccv_array_t* outgoings; // backward traverse nodes.
12 uint64_t* input_bitmasks;
13 int input_bitmask_size;
14 uint64_t* output_bitmasks;
15 int output_bitmask_size;
16} ccv_nnc_graph_backward_info_t;
17
18typedef struct {
19 int input_size;
20 int* inputs;
21 int output;
22 ccv_array_t* outgoings;
23 float value;
24 ccv_nnc_graph_exec_symbol_t symbol;
25} ccv_nnc_sum_or_set_graph_exec_symbol_t;
26
27typedef struct {
28 int input_size;
29 int output_size;
30 int* inputs;
31 int* outputs;
32 ccv_array_t* outgoings;
33 ccv_nnc_cmd_t cmd;
34 ccv_nnc_graph_exec_symbol_t symbol;
35} ccv_nnc_autograd_graph_exec_symbol_t;
36
37typedef struct {
38 int d; // The pointer to the forward level object.
39 int alias_ref; // The alias ref to itself (autograd_tensor_symbols array).
40 int flags; // Flags for this symbol.
41 ccv_nnc_tensor_symbol_t symbol;
42} ccv_nnc_autograd_tensor_symbol_t;
43
44typedef struct {
45 int d; // The tensor symbol ref.
46 int x; // The exec symbol ref.
47 ccv_array_t* exec_registry; // Additional exec symbol refs, similar to x, only useful for aliasing.
48 ccv_array_t* alias_registry; // int point to all the alias (if this is not an alias). The alias is the object in autograd_tensor_symbols, you need another level of indirection to get the actual forward level alias.
49} ccv_nnc_tensor_ref_t;
50
51typedef struct {
52 int c; // The start non-accumulated version.
53 ccv_array_t* ref_version; // tensor ref point to the reverse tensor symbol.
54} ccv_nnc_autograd_tensor_version_t;
55
56typedef struct {
57 int d;
58 int alias_ref;
59} ccv_nnc_sum_variable_t;
60
61// This method tries to figure out if a set of aliases can cover the whole tensor dim.
62// This is not a precise implementation though. The requirement is to answer this question
63// with a given memory constraint, therefore, only allow up to 65536 different tensor locations.
64// If you have more than that, it will assume that it doesn't have fully assigned aliases,
65// and will return 0.
66
67// Return 1 if inserted successfully.
68static inline int _ccv_nnc_try_mix(int* const md, const int ins, const int c)
69{
70 if (!c)
71 {
72 md[0] = ins;
73 return 1;
74 }
75 int ll = 0, uu = c - 1;
76 int mm;
77 do {
78 mm = ll + ((uu - ll) >> 1);
79 if (ins == md[mm])
80 return 0;
81 else if (ins < md[mm])
82 uu = mm - 1;
83 else if (ins > md[mm])
84 ll = mm + 1;
85 } while (ll <= uu);
86 if (ll < c)
87 memmove(md + ll + 1, md + ll, sizeof(int) * (c - ll));
88 md[ll] = ins;
89 return 1;
90}
91
92static inline int _ccv_nnc_mix_idx(const int* const md, const int ins, const int c)
93{
94 if (c <= 1)
95 return 0;
96 int ll = 0, uu = c - 1;
97 int mm;
98 do {
99 mm = ll + ((uu - ll) >> 1);
100 if (ins == md[mm])
101 return mm;
102 else if (ins < md[mm])
103 uu = mm - 1;
104 else if (ins > md[mm])
105 ll = mm + 1;
106 } while (ll <= uu);
107 assert(0 && "Shouldn't reach here")((void) sizeof ((0 && "Shouldn't reach here") ? 1 : 0
), __extension__ ({ if (0 && "Shouldn't reach here") ;
else __assert_fail ("0 && \"Shouldn't reach here\"",
"ccv_nnc_symbolic_graph_backward.c", 107, __extension__ __PRETTY_FUNCTION__
); }))
;
108 return -1;
109}
110
111static inline void _ccv_nnc_try_set_pix_0(const int* const ofs, const int* const dim, const int* const tensor_dim, int* const* const scmd, const int* const cube_dim, const int* const cube_step, uint32_t* const cube, int offset)
112{
113 const int s = (ofs[0] == 0) ? 0 : _ccv_nnc_mix_idx(scmd[0], ofs[0], cube_dim[0]) + 1;
114 const int d = ((ofs[0] + dim[0] == tensor_dim[0]) ? cube_dim[0] : _ccv_nnc_mix_idx(scmd[0], ofs[0] + ccv_max(1, dim[0])({ typeof (1) _a = (1); typeof (dim[0]) _b = (dim[0]); (_a >
_b) ? _a : _b; })
, cube_dim[0])) + 1;
115 assert(s >= 0 && d > s)((void) sizeof ((s >= 0 && d > s) ? 1 : 0), __extension__
({ if (s >= 0 && d > s) ; else __assert_fail (
"s >= 0 && d > s", "ccv_nnc_symbolic_graph_backward.c"
, 115, __extension__ __PRETTY_FUNCTION__); }))
;
116 int i;
117 for (i = s; i < d; i++)
118 // Fill this pix. I can make this faster by loop through full ones (divided by 8), but too lazy.
119 cube[(offset + i) >> 5] |= (1u << ((offset + i) & 0x1f));
120}
121
122static inline void _ccv_nnc_try_set_pix_1(const int* const ofs, const int* const dim, const int* const tensor_dim, int* const* const scmd, const int* const cube_dim, const int* const cube_step, uint32_t* const cube, int offset)
123{
124 const int s0 = (ofs[0] == 0) ? 0 : _ccv_nnc_mix_idx(scmd[0], ofs[0], cube_dim[0]) + 1;
125 const int d0 = ((ofs[0] + dim[0] == tensor_dim[0]) ? cube_dim[0] : _ccv_nnc_mix_idx(scmd[0], ofs[0] + ccv_max(1, dim[0])({ typeof (1) _a = (1); typeof (dim[0]) _b = (dim[0]); (_a >
_b) ? _a : _b; })
, cube_dim[0])) + 1;
126 assert(s0 >= 0 && d0 > s0)((void) sizeof ((s0 >= 0 && d0 > s0) ? 1 : 0), __extension__
({ if (s0 >= 0 && d0 > s0) ; else __assert_fail
("s0 >= 0 && d0 > s0", "ccv_nnc_symbolic_graph_backward.c"
, 126, __extension__ __PRETTY_FUNCTION__); }))
;
127 const int s1 = (ofs[1] == 0) ? 0 : _ccv_nnc_mix_idx(scmd[1], ofs[1], cube_dim[1]) + 1;
128 const int d1 = ((ofs[1] + dim[1] == tensor_dim[1]) ? cube_dim[1] : _ccv_nnc_mix_idx(scmd[1], ofs[1] + ccv_max(1, dim[1])({ typeof (1) _a = (1); typeof (dim[1]) _b = (dim[1]); (_a >
_b) ? _a : _b; })
, cube_dim[1])) + 1;
129 assert(s1 >= 0 && d1 > s1)((void) sizeof ((s1 >= 0 && d1 > s1) ? 1 : 0), __extension__
({ if (s1 >= 0 && d1 > s1) ; else __assert_fail
("s1 >= 0 && d1 > s1", "ccv_nnc_symbolic_graph_backward.c"
, 129, __extension__ __PRETTY_FUNCTION__); }))
;
130 int i, j;
131 const int step1 = cube_step[1];
132 if (step1 == d0 - s0)
133 {
134 // Faster one, we can simply loop through.
135 for (i = s1 * step1; i < d1 * step1; i++)
136 cube[(offset + i) >> 5] |= (1u << ((offset + i) & 0x1f));
137 } else {
138 offset += s1 * step1;
139 // There are gaps, slow one.
140 for (i = s1; i < d1; i++, offset += step1)
141 for (j = s0; j < d0; j++)
142 cube[(offset + j) >> 5] |= (1u << ((offset + j) & 0x1f));
143 }
144}
145
146static inline void _ccv_nnc_try_set_pix(const int* const ofs, const int* const dim, const int* const tensor_dim, int* const* const scmd, const int* const cube_dim, const int* const cube_step, uint32_t* const cube, int offset, const int dim_idx)
147{
148 switch (dim_idx)
149 {
150 case 1:
151 _ccv_nnc_try_set_pix_1(ofs, dim, tensor_dim, scmd, cube_dim, cube_step, cube, offset);
152 return;
153 case 0:
154 _ccv_nnc_try_set_pix_0(ofs, dim, tensor_dim, scmd, cube_dim, cube_step, cube, offset);
155 return;
156 }
157 int i;
158 const int s = (ofs[dim_idx] == 0) ? 0 : _ccv_nnc_mix_idx(scmd[dim_idx], ofs[dim_idx], cube_dim[dim_idx]) + 1;
159 const int d = ((ofs[dim_idx] + dim[dim_idx] == tensor_dim[dim_idx]) ? cube_dim[dim_idx] : _ccv_nnc_mix_idx(scmd[dim_idx], ofs[dim_idx] + ccv_max(1, dim[dim_idx])({ typeof (1) _a = (1); typeof (dim[dim_idx]) _b = (dim[dim_idx
]); (_a > _b) ? _a : _b; })
, cube_dim[dim_idx])) + 1;
160 assert(s >= 0 && d > s)((void) sizeof ((s >= 0 && d > s) ? 1 : 0), __extension__
({ if (s >= 0 && d > s) ; else __assert_fail (
"s >= 0 && d > s", "ccv_nnc_symbolic_graph_backward.c"
, 160, __extension__ __PRETTY_FUNCTION__); }))
;
161 for (i = s; i < d; i++)
162 _ccv_nnc_try_set_pix(ofs, dim, tensor_dim, scmd, cube_dim, cube_step, cube, offset + i * cube_step[dim_idx], dim_idx - 1);
163}
164
165static int _ccv_nnc_tensor_ref_fully_assigned_with_aliases(const ccv_nnc_tensor_ref_t* const tensor_ref, const ccv_array_t* const autograd_tensor_symbols, const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info)
166{
167 // Only work with tensor_ref of aliases.
168 assert(tensor_ref->alias_registry)((void) sizeof ((tensor_ref->alias_registry) ? 1 : 0), __extension__
({ if (tensor_ref->alias_registry) ; else __assert_fail (
"tensor_ref->alias_registry", "ccv_nnc_symbolic_graph_backward.c"
, 168, __extension__ __PRETTY_FUNCTION__); }))
;
169 const ccv_nnc_autograd_tensor_symbol_t* autograd = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
170 assert(tensor_symbol_info[autograd->d].alias_ref == 0)((void) sizeof ((tensor_symbol_info[autograd->d].alias_ref
== 0) ? 1 : 0), __extension__ ({ if (tensor_symbol_info[autograd
->d].alias_ref == 0) ; else __assert_fail ("tensor_symbol_info[autograd->d].alias_ref == 0"
, "ccv_nnc_symbolic_graph_backward.c", 170, __extension__ __PRETTY_FUNCTION__
); }))
;
171 const int* tensor_dim = tensor_symbol_info[autograd->d].info.dim;
172 int i, j;
173 for (i = 0; i < tensor_ref->alias_registry->rnum; i++)
174 {
175 const int d = *(int*)ccv_array_get(tensor_ref->alias_registry, i)((void*)(((char*)((tensor_ref->alias_registry)->data)) +
(size_t)(tensor_ref->alias_registry)->rsize * (size_t)
(i)))
;
176 assert(d < autograd_tensor_symbols->rnum)((void) sizeof ((d < autograd_tensor_symbols->rnum) ? 1
: 0), __extension__ ({ if (d < autograd_tensor_symbols->
rnum) ; else __assert_fail ("d < autograd_tensor_symbols->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 176, __extension__ __PRETTY_FUNCTION__
); }))
;
177 const ccv_nnc_autograd_tensor_symbol_t* autograd = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(d)))
;
178 assert(tensor_symbol_info[autograd->d].alias_ref)((void) sizeof ((tensor_symbol_info[autograd->d].alias_ref
) ? 1 : 0), __extension__ ({ if (tensor_symbol_info[autograd->
d].alias_ref) ; else __assert_fail ("tensor_symbol_info[autograd->d].alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 178, __extension__ __PRETTY_FUNCTION__
); }))
;
179 const int* inc = tensor_symbol_info[autograd->d].inc;
180 if (memcmp(inc, tensor_dim, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(8)) != 0)
181 return 0;
182 }
183 /* We need a solid cube (potentially hyper dimensional) to compute if there are overlaps.
184 * To make this cube as small as possible, we need to map the actual tensor dimension
185 * (therefore, we don't actually allocate the whole tensor to compute overlaps) to a smaller
186 * cube given the ofs and dim size of its aliases.
187 *
188 * The following code generated the dimension mapping (using scratch space) with binary search + insertion
189 * and then we fill the cube with a given tensor alias's dimensional information (ofs, dim).
190 * Afterwards, we simply need to check if the cube is totally filled up to know if this tensor
191 * is fully assigned with its aliases (if that is the case, we can skip zeroing for this tensor).
192 *
193 * There are several restrictions though to make this faster: 1). I cannot handle any cube that all side
194 * lengths combined larger than 1023 (scm only have 1024 scratch space). 2). I cannot handle any cube
195 * that the total volume is larger than 2048 * 8 (I only allocate 2K on stack for this).
196 * */
197 int scm[1024]; // Having 1024 int scratch space for mapping dimensions. (Or sparse coordinate mapping).
198 int cube_dim[CCV_NNC_MAX_DIM_ALLOC(8)] = {}; // Mapping dimension size.
199 int cube_size = 1;
200 int* scmptr = scm;
201 for (i = 0; i < CCV_NNC_MAX_DIM_ALLOC(8) && tensor_dim[i]; i++)
202 {
203 int head = 0, tail = 0; // Note that we touched both the head and tail (otherwise this dimension is not fully covered).
204 int len = 0;
205 for (j = 0; j < tensor_ref->alias_registry->rnum; j++)
206 {
207 const int d = *(int*)ccv_array_get(tensor_ref->alias_registry, j)((void*)(((char*)((tensor_ref->alias_registry)->data)) +
(size_t)(tensor_ref->alias_registry)->rsize * (size_t)
(j)))
;
208 assert(d < autograd_tensor_symbols->rnum)((void) sizeof ((d < autograd_tensor_symbols->rnum) ? 1
: 0), __extension__ ({ if (d < autograd_tensor_symbols->
rnum) ; else __assert_fail ("d < autograd_tensor_symbols->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 208, __extension__ __PRETTY_FUNCTION__
); }))
;
209 const ccv_nnc_autograd_tensor_symbol_t* autograd = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(d)))
;
210 assert(tensor_symbol_info[autograd->d].alias_ref)((void) sizeof ((tensor_symbol_info[autograd->d].alias_ref
) ? 1 : 0), __extension__ ({ if (tensor_symbol_info[autograd->
d].alias_ref) ; else __assert_fail ("tensor_symbol_info[autograd->d].alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 210, __extension__ __PRETTY_FUNCTION__
); }))
;
211 const int* ofs = tensor_symbol_info[autograd->d].ofs;
212 const int* dim = tensor_symbol_info[autograd->d].info.dim;
213 head = head || (ofs[i] == 0);
214 tail = tail || (ofs[i] + ccv_max(1, dim[i])({ typeof (1) _a = (1); typeof (dim[i]) _b = (dim[i]); (_a >
_b) ? _a : _b; })
== tensor_dim[i]);
215 if (ofs[i] != 0)
216 len += _ccv_nnc_try_mix(scmptr, ofs[i], len);
217 if (scmptr - scm + len >= 1024) // Cannot handle that much, abort.
218 return 0;
219 if (ofs[i] + ccv_max(1, dim[i])({ typeof (1) _a = (1); typeof (dim[i]) _b = (dim[i]); (_a >
_b) ? _a : _b; })
< tensor_dim[i])
220 len += _ccv_nnc_try_mix(scmptr, ofs[i] + ccv_max(1, dim[i])({ typeof (1) _a = (1); typeof (dim[i]) _b = (dim[i]); (_a >
_b) ? _a : _b; })
, len);
221 if (scmptr - scm + len >= 1024) // Cannot handle that much, abort.
222 return 0;
223 }
224 if (!head || !tail)
225 return 0;
226 cube_size *= (len + 1);
227 cube_dim[i] = len;
228 scmptr += len; // Moving to next level.
229 }
230 // The cube map is too large, cannot do the computation, assume it is not fully assigned.
231 if (cube_size > 2048 * 8)
232 return 0;
233 // binary map to see if it fills up.
234 uint32_t cube[(cube_size + 31) >> 5];
235 memset(cube, 0, sizeof(uint32_t) * ((cube_size + 31) >> 5));
236 int* scmd[CCV_NNC_MAX_DIM_ALLOC(8)] = {}; // Sparse coordinate map at dimension x.
237 int cube_step[CCV_NNC_MAX_DIM_ALLOC(8)] = {};
238 for (i = 0; i < CCV_NNC_MAX_DIM_ALLOC(8) && tensor_dim[i]; i++)
239 {
240 cube_step[i] = (i > 0) ? cube_step[i - 1] * (cube_dim[i - 1] + 1) : 1;
241 scmd[i] = (i > 0) ? scmd[i - 1] + cube_dim[i - 1] : scm;
242 }
243 const int max_dim = i;
244 for (i = 0; i < tensor_ref->alias_registry->rnum; i++)
245 {
246 const int d = *(int*)ccv_array_get(tensor_ref->alias_registry, i)((void*)(((char*)((tensor_ref->alias_registry)->data)) +
(size_t)(tensor_ref->alias_registry)->rsize * (size_t)
(i)))
;
247 assert(d < autograd_tensor_symbols->rnum)((void) sizeof ((d < autograd_tensor_symbols->rnum) ? 1
: 0), __extension__ ({ if (d < autograd_tensor_symbols->
rnum) ; else __assert_fail ("d < autograd_tensor_symbols->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 247, __extension__ __PRETTY_FUNCTION__
); }))
;
248 const ccv_nnc_autograd_tensor_symbol_t* autograd = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(d)))
;
249 assert(tensor_symbol_info[autograd->d].alias_ref)((void) sizeof ((tensor_symbol_info[autograd->d].alias_ref
) ? 1 : 0), __extension__ ({ if (tensor_symbol_info[autograd->
d].alias_ref) ; else __assert_fail ("tensor_symbol_info[autograd->d].alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 249, __extension__ __PRETTY_FUNCTION__
); }))
;
250 const int* ofs = tensor_symbol_info[autograd->d].ofs;
251 const int* dim = tensor_symbol_info[autograd->d].info.dim;
252 _ccv_nnc_try_set_pix(ofs, dim, tensor_dim, scmd, cube_dim, cube_step, cube, 0, max_dim - 1);
253 }
254 // Compare to see now if the binary map filled up. If it filled up, we know it is fully assigned.
255 for (i = 0; i < (cube_size >> 5); i++)
256 if (cube[i] < 0xffffffff)
257 return 0;
258 if ((cube_size & 0x1f) > 0)
259 {
260 // Fetch the rest.
261 uint32_t r = 0;
262 for (i = 0; i < (cube_size & 0x1f); i++)
263 r |= (1u << i);
264 assert(cube[((cube_size + 31) >> 5) - 1] <= r)((void) sizeof ((cube[((cube_size + 31) >> 5) - 1] <=
r) ? 1 : 0), __extension__ ({ if (cube[((cube_size + 31) >>
5) - 1] <= r) ; else __assert_fail ("cube[((cube_size + 31) >> 5) - 1] <= r"
, "ccv_nnc_symbolic_graph_backward.c", 264, __extension__ __PRETTY_FUNCTION__
); }))
;
265 if (cube[((cube_size + 31) >> 5) - 1] < r)
266 return 0;
267 }
268 return 1;
269}
270
271static int _ccv_nnc_tensor_ref_version_find_init(const ccv_nnc_autograd_tensor_version_t* const tensor_ver)
272{
273 int i;
274 for (i = 0; i < tensor_ver->ref_version->rnum; i++)
275 if (((ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(i)))
)->x < 0)
276 return i;
277 return -1;
278}
279
280static void _ccv_nnc_graph_sum_autograd_tensor_versions(const int idx, const int d, const int exec_symbol_info_size, const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info, ccv_nnc_autograd_tensor_version_t* const tensor_ver, ccv_nnc_autograd_graph_exec_symbol_t* const autograd_execs, ccv_array_t* const autograd_tensor_symbols, ccv_array_t* const sum_or_set_execs)
281{
282 int i, j;
283 assert(tensor_ver->c < tensor_ver->ref_version->rnum)((void) sizeof ((tensor_ver->c < tensor_ver->ref_version
->rnum) ? 1 : 0), __extension__ ({ if (tensor_ver->c <
tensor_ver->ref_version->rnum) ; else __assert_fail ("tensor_ver->c < tensor_ver->ref_version->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 283, __extension__ __PRETTY_FUNCTION__
); }))
;
284 const int input_size = tensor_ver->ref_version->rnum - tensor_ver->c;
285 int* inputs = (int*)ccmallocmalloc(sizeof(int) * input_size);
286 for (i = tensor_ver->c; i < tensor_ver->ref_version->rnum; i++)
287 inputs[i] = ((ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(i)))
)->d;
288 const ccv_nnc_autograd_tensor_symbol_t tensor_sym = {
289 .d = d
290 };
291 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
292 ccv_nnc_sum_or_set_graph_exec_symbol_t sum_exec = {
293 .input_size = input_size,
294 .inputs = inputs,
295 .output = autograd_tensor_symbols->rnum - 1
296 };
297 if (idx >= 0)
298 {
299 sum_exec.outgoings = ccv_array_new(sizeof(int), 1, 0);
300 ccv_array_push(sum_exec.outgoings, &idx);
301 }
302 ccv_array_push(sum_or_set_execs, &sum_exec);
303 const int outgoing = exec_symbol_info_size + sum_or_set_execs->rnum - 1;
304 for (i = tensor_ver->c; i < tensor_ver->ref_version->rnum; i++)
305 {
306 const ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(i)))
;
307 const int x = tensor_ref->x;
308 if (x < 0) /* This is initialization tensor, it has to be occurred before the execution anyway. */
309 {
310 // No alias.
311 assert(!tensor_ref->alias_registry)((void) sizeof ((!tensor_ref->alias_registry) ? 1 : 0), __extension__
({ if (!tensor_ref->alias_registry) ; else __assert_fail (
"!tensor_ref->alias_registry", "ccv_nnc_symbolic_graph_backward.c"
, 311, __extension__ __PRETTY_FUNCTION__); }))
;
312 // No associated additional execs.
313 assert(!tensor_ref->exec_registry)((void) sizeof ((!tensor_ref->exec_registry) ? 1 : 0), __extension__
({ if (!tensor_ref->exec_registry) ; else __assert_fail (
"!tensor_ref->exec_registry", "ccv_nnc_symbolic_graph_backward.c"
, 313, __extension__ __PRETTY_FUNCTION__); }))
;
314 continue;
315 }
316 if (x < exec_symbol_info_size)
317 {
318 ccv_nnc_autograd_graph_exec_symbol_t* back_exec = autograd_execs + x;
319 if (!back_exec->outgoings)
320 back_exec->outgoings = ccv_array_new(sizeof(int), 1, 0);
321 ccv_array_replace_unique_int(back_exec->outgoings, idx, outgoing);
322 } else {
323 // This tensor_ref is generated by the sum operation.
324 ccv_nnc_sum_or_set_graph_exec_symbol_t* sum_or_set = (ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, x - exec_symbol_info_size)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(x - exec_symbol_info_size)))
;
325 ccv_array_replace_unique_int(sum_or_set->outgoings, idx, outgoing);
326 }
327 // If this tensor have associated alias, we need to init it to zeros when it is allocated (we only need to set a flag here)
328 // it is handled at compilation phase.
329 if (tensor_ref->alias_registry &&
330 // Loop over to see if this tensor is fully occupied to avoid extra zero step.
331 !_ccv_nnc_tensor_ref_fully_assigned_with_aliases(tensor_ref, autograd_tensor_symbols, tensor_symbol_info))
332 {
333 ccv_nnc_autograd_tensor_symbol_t* tensor_sym = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
334 // By having alias_registry, what this symbol represents must not by an alias.
335 assert(tensor_sym->alias_ref == 0)((void) sizeof ((tensor_sym->alias_ref == 0) ? 1 : 0), __extension__
({ if (tensor_sym->alias_ref == 0) ; else __assert_fail (
"tensor_sym->alias_ref == 0", "ccv_nnc_symbolic_graph_backward.c"
, 335, __extension__ __PRETTY_FUNCTION__); }))
;
336 tensor_sym->flags = CCV_NNC_TENSOR_SYMBOL_INIT_ZEROS;
337 }
338 if (tensor_ref->exec_registry)
339 for (j = 0; j < tensor_ref->exec_registry->rnum; j++)
340 {
341 const int x = *(int*)ccv_array_get(tensor_ref->exec_registry, j)((void*)(((char*)((tensor_ref->exec_registry)->data)) +
(size_t)(tensor_ref->exec_registry)->rsize * (size_t)(
j)))
;
342 assert(x >= 0)((void) sizeof ((x >= 0) ? 1 : 0), __extension__ ({ if (x >=
0) ; else __assert_fail ("x >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 342, __extension__ __PRETTY_FUNCTION__); }))
;
343 // The exec_registry can only be generated by alias registry, therefore, it cannot reference to a sum operation.
344 assert(x < exec_symbol_info_size)((void) sizeof ((x < exec_symbol_info_size) ? 1 : 0), __extension__
({ if (x < exec_symbol_info_size) ; else __assert_fail ("x < exec_symbol_info_size"
, "ccv_nnc_symbolic_graph_backward.c", 344, __extension__ __PRETTY_FUNCTION__
); }))
;
345 ccv_nnc_autograd_graph_exec_symbol_t* back_exec = autograd_execs + x;
346 if (!back_exec->outgoings)
347 back_exec->outgoings = ccv_array_new(sizeof(int), 1, 0);
348 ccv_array_replace_unique_int(back_exec->outgoings, idx, outgoing);
349 }
350 }
351 const ccv_nnc_tensor_ref_t tensor_ref = {
352 .d = autograd_tensor_symbols->rnum - 1,
353 .x = outgoing
354 };
355 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
356 /* Move the c pointer up to the latest summed result. */
357 tensor_ver->c = tensor_ver->ref_version->rnum - 1;
358}
359
360static int _ccv_nnc_tensor_ref_version_involve_alias(const ccv_nnc_tensor_ref_t* const tensor_ref, const ccv_array_t* const autograd_tensor_symbols, const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info, const ccv_nnc_tensor_symbol_info_t* const alias)
361{
362 assert(alias->alias_ref > 0)((void) sizeof ((alias->alias_ref > 0) ? 1 : 0), __extension__
({ if (alias->alias_ref > 0) ; else __assert_fail ("alias->alias_ref > 0"
, "ccv_nnc_symbolic_graph_backward.c", 362, __extension__ __PRETTY_FUNCTION__
); }))
;
363 // No alias_registry, must conflict (owns the whole band).
364 if (!tensor_ref->alias_registry)
365 return 1;
366 int i;
367 for (i = 0; i < tensor_ref->alias_registry->rnum; i++)
368 {
369 const int d = *(int*)ccv_array_get(tensor_ref->alias_registry, i)((void*)(((char*)((tensor_ref->alias_registry)->data)) +
(size_t)(tensor_ref->alias_registry)->rsize * (size_t)
(i)))
;
370 assert(d < autograd_tensor_symbols->rnum)((void) sizeof ((d < autograd_tensor_symbols->rnum) ? 1
: 0), __extension__ ({ if (d < autograd_tensor_symbols->
rnum) ; else __assert_fail ("d < autograd_tensor_symbols->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 370, __extension__ __PRETTY_FUNCTION__
); }))
;
371 ccv_nnc_autograd_tensor_symbol_t* autograd = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(d)))
;
372 if (ccv_nnc_over_tensor_symbol_aliases(tensor_symbol_info + autograd->d, alias))
373 return 1;
374 }
375 // All aliases referenced by this ref_version doesn't overlap with the provided one, thus, there is no conflict at all.
376 return 0;
377}
378
379static int _ccv_nnc_tensor_ref_version_find_alias(const ccv_nnc_tensor_ref_t* const tensor_ref, const ccv_array_t* const autograd_tensor_symbols, const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info, const ccv_nnc_tensor_symbol_info_t* const alias)
380{
381 assert(alias->alias_ref > 0)((void) sizeof ((alias->alias_ref > 0) ? 1 : 0), __extension__
({ if (alias->alias_ref > 0) ; else __assert_fail ("alias->alias_ref > 0"
, "ccv_nnc_symbolic_graph_backward.c", 381, __extension__ __PRETTY_FUNCTION__
); }))
;
382 // No alias_registry, thus, cannot find the exact matched alias.
383 if (!tensor_ref->alias_registry)
384 return -1;
385 int i;
386 for (i = 0; i < tensor_ref->alias_registry->rnum; i++)
387 {
388 const int d = *(int*)ccv_array_get(tensor_ref->alias_registry, i)((void*)(((char*)((tensor_ref->alias_registry)->data)) +
(size_t)(tensor_ref->alias_registry)->rsize * (size_t)
(i)))
;
389 assert(d < autograd_tensor_symbols->rnum)((void) sizeof ((d < autograd_tensor_symbols->rnum) ? 1
: 0), __extension__ ({ if (d < autograd_tensor_symbols->
rnum) ; else __assert_fail ("d < autograd_tensor_symbols->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 389, __extension__ __PRETTY_FUNCTION__
); }))
;
390 ccv_nnc_autograd_tensor_symbol_t* autograd = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(d)))
;
391 // This must reference to an alias.
392 assert(tensor_symbol_info[autograd->d].alias_ref)((void) sizeof ((tensor_symbol_info[autograd->d].alias_ref
) ? 1 : 0), __extension__ ({ if (tensor_symbol_info[autograd->
d].alias_ref) ; else __assert_fail ("tensor_symbol_info[autograd->d].alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 392, __extension__ __PRETTY_FUNCTION__
); }))
;
393 const int* inc = tensor_symbol_info[autograd->d].inc;
394 const int* ofs = tensor_symbol_info[autograd->d].ofs;
395 const int* dim = tensor_symbol_info[autograd->d].info.dim;
396 // If everything matches, this is the required alias.
397 if (memcmp(inc, alias->inc, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(8)) == 0 &&
398 memcmp(ofs, alias->ofs, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(8)) == 0 &&
399 memcmp(dim, alias->info.dim, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(8)) == 0)
400 return d;
401 }
402 return -1;
403}
404
405static int _ccv_nnc_tensor_ref_version_has_this_alias_exclusively(const ccv_nnc_tensor_ref_t* const tensor_ref, const ccv_array_t* const autograd_tensor_symbols, const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info, const ccv_nnc_tensor_symbol_info_t* const alias)
406{
407 assert(alias->alias_ref > 0)((void) sizeof ((alias->alias_ref > 0) ? 1 : 0), __extension__
({ if (alias->alias_ref > 0) ; else __assert_fail ("alias->alias_ref > 0"
, "ccv_nnc_symbolic_graph_backward.c", 407, __extension__ __PRETTY_FUNCTION__
); }))
;
408 // No alias_registry, thus, cannot find the exact matched alias.
409 if (!tensor_ref->alias_registry)
410 return 0;
411 int i;
412 for (i = 0; i < tensor_ref->alias_registry->rnum; i++)
413 {
414 const int d = *(int*)ccv_array_get(tensor_ref->alias_registry, i)((void*)(((char*)((tensor_ref->alias_registry)->data)) +
(size_t)(tensor_ref->alias_registry)->rsize * (size_t)
(i)))
;
415 assert(d < autograd_tensor_symbols->rnum)((void) sizeof ((d < autograd_tensor_symbols->rnum) ? 1
: 0), __extension__ ({ if (d < autograd_tensor_symbols->
rnum) ; else __assert_fail ("d < autograd_tensor_symbols->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 415, __extension__ __PRETTY_FUNCTION__
); }))
;
416 ccv_nnc_autograd_tensor_symbol_t* autograd = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(d)))
;
417 // This must reference to an alias.
418 assert(tensor_symbol_info[autograd->d].alias_ref)((void) sizeof ((tensor_symbol_info[autograd->d].alias_ref
) ? 1 : 0), __extension__ ({ if (tensor_symbol_info[autograd->
d].alias_ref) ; else __assert_fail ("tensor_symbol_info[autograd->d].alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 418, __extension__ __PRETTY_FUNCTION__
); }))
;
419 const int* inc = tensor_symbol_info[autograd->d].inc;
420 const int* ofs = tensor_symbol_info[autograd->d].ofs;
421 const int* dim = tensor_symbol_info[autograd->d].info.dim;
422 if (memcmp(inc, alias->inc, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(8)) != 0 ||
423 memcmp(ofs, alias->ofs, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(8)) != 0 ||
424 memcmp(dim, alias->info.dim, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(8)) != 0)
425 return 0;
426 }
427 // If everything matches for every alias in registry, we can use any of the alias directly.
428 return 1;
429}
430
431static int _ccv_nnc_graph_sum_autograd_tensor_versions_alias(const int idx, const int d, const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info, const int exec_symbol_info_size, const ccv_nnc_tensor_symbol_info_t* const alias, ccv_nnc_autograd_tensor_version_t* const tensor_ver, ccv_nnc_autograd_graph_exec_symbol_t* const autograd_execs, ccv_array_t* const autograd_tensor_symbols, ccv_array_t* const sum_or_set_execs)
432{
433 assert(tensor_ver->c < tensor_ver->ref_version->rnum)((void) sizeof ((tensor_ver->c < tensor_ver->ref_version
->rnum) ? 1 : 0), __extension__ ({ if (tensor_ver->c <
tensor_ver->ref_version->rnum) ; else __assert_fail ("tensor_ver->c < tensor_ver->ref_version->rnum"
, "ccv_nnc_symbolic_graph_backward.c", 433, __extension__ __PRETTY_FUNCTION__
); }))
;
434 int i, j = 0;
435 struct {
436 int k;
437 int i;
438 } kd[tensor_ver->ref_version->rnum - tensor_ver->c];
439 for (i = tensor_ver->c; i < tensor_ver->ref_version->rnum; i++)
440 {
441 ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(i)))
;
442 const int k = _ccv_nnc_tensor_ref_version_find_alias(tensor_ref, autograd_tensor_symbols, tensor_symbol_info, alias);
443 if (k >= 0)
444 kd[j++] = (typeof(kd[0])){
445 .k = k, .i = i
446 };
447 else if (_ccv_nnc_tensor_ref_version_involve_alias(tensor_ref, autograd_tensor_symbols, tensor_symbol_info, alias))
448 kd[j++] = (typeof(kd[0])) {
449 .k = -1, .i = i // It has dependency to the original tensor (non-alias) now, label this with highest bit.
450 };
451 }
452 // Can only find one. This is the easy case, we can simply return that symbol (or its alias).
453 if (j == 1)
454 {
455 if (kd[0].k >= 0)
456 return kd[0].k; // Only can find one alias, that is the one.
457 // Otherwise, need to create a new alias.
458 ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, kd[0].i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(kd[0
].i)))
;
459 ccv_nnc_autograd_tensor_symbol_t* ref = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
460 // Since we create new alias, we need to set the referenced one to be allocated with 0s.
461 if (ref->alias_ref) // If this is an alias, it has to be zero initialized.
462 {
463 ref = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, ref->alias_ref - 1)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(ref->alias_ref
- 1)))
;
464 assert(ref->alias_ref == 0)((void) sizeof ((ref->alias_ref == 0) ? 1 : 0), __extension__
({ if (ref->alias_ref == 0) ; else __assert_fail ("ref->alias_ref == 0"
, "ccv_nnc_symbolic_graph_backward.c", 464, __extension__ __PRETTY_FUNCTION__
); }))
; // This is original.
465 ref->flags = CCV_NNC_TENSOR_SYMBOL_INIT_ZEROS;
466 } else if (tensor_ref->alias_registry && // Otherwise, to see if this symbol is fully occupied.
467 // Loop over to see if this tensor is fully occupied to avoid extra zero step.
468 !_ccv_nnc_tensor_ref_fully_assigned_with_aliases(tensor_ref, autograd_tensor_symbols, tensor_symbol_info)) {
469 ref->flags = CCV_NNC_TENSOR_SYMBOL_INIT_ZEROS;
470 }
471 ccv_nnc_autograd_tensor_symbol_t tensor_sym = {
472 .d = d,
473 .alias_ref = tensor_ref->d + 1
474 };
475 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
476 const int ad = autograd_tensor_symbols->rnum - 1;
477 if (tensor_ref->alias_registry) // Only push this when it has an alias registry (otherwise it already conflict with everyone).
478 ccv_array_push(tensor_ref->alias_registry, &ad);
479 // The newly inserted tensor symbol.
480 return ad;
481 }
482 // Otherwise, we need to create the sum operation out of these.
483 const int input_size = j;
484 int has_this_alias_exclusively = 1;
485 int* inputs = input_size > 0 ? (int*)ccmallocmalloc(sizeof(int) * input_size) : 0;
486 for (i = 0; i < input_size; i++)
487 {
488 ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, kd[i].i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(kd[i
].i)))
;
489 // Can take a fast path if every ref involved has the same alias, our sum operation can be faster (using alias directly).
490 if (has_this_alias_exclusively && kd[i].k >= 0 && _ccv_nnc_tensor_ref_version_has_this_alias_exclusively(tensor_ref, autograd_tensor_symbols, tensor_symbol_info, alias))
491 inputs[i] = *(int*)ccv_array_get(tensor_ref->alias_registry, 0)((void*)(((char*)((tensor_ref->alias_registry)->data)) +
(size_t)(tensor_ref->alias_registry)->rsize * (size_t)
(0)))
; // Assigning the alias.
492 else {
493 if (has_this_alias_exclusively)
494 {
495 has_this_alias_exclusively = 0;
496 for (j = 0; j < i; j++)
497 inputs[j] = ((ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, kd[j].i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(kd[j
].i)))
)->d;
498 }
499 inputs[i] = tensor_ref->d;
500 }
501 }
502 ccv_nnc_autograd_tensor_symbol_t tensor_sym = {
503 .d = alias->alias_ref - 1
504 };
505 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
506 const int tensor_ref_d = autograd_tensor_symbols->rnum - 1;
507 tensor_sym.d = d;
508 tensor_sym.alias_ref = tensor_ref_d + 1;
509 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
510 const int ad = autograd_tensor_symbols->rnum - 1;
511 ccv_nnc_sum_or_set_graph_exec_symbol_t sum_exec = {
512 .input_size = input_size,
513 .inputs = inputs,
514 .output = has_this_alias_exclusively ? ad : tensor_ref_d /* If has this alias exclusively, the output should be alias as well. Otherwise the output is the real tensor. */
515 };
516 if (idx >= 0)
517 {
518 sum_exec.outgoings = ccv_array_new(sizeof(int), 1, 0);
519 ccv_array_push(sum_exec.outgoings, &idx);
520 }
521 ccv_array_push(sum_or_set_execs, &sum_exec);
522 const int outgoing = exec_symbol_info_size + sum_or_set_execs->rnum - 1;
523 int no_alias_registry = 0;
524 for (i = 0; i < input_size; i++)
525 {
526 ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, kd[i].i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(kd[i
].i)))
;
527 if (!has_this_alias_exclusively)
528 {
529 // If the sum operation is not operating on one alias. I need to zero this tensor out when it is first
530 // allocated (see discussions around the flags I use).
531 ccv_nnc_autograd_tensor_symbol_t* tensor_sym = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
532 if (tensor_sym->alias_ref)
533 {
534 // Find the original tensor_sym and set its flags (I prefer to set flags on its original).
535 ccv_nnc_autograd_tensor_symbol_t* ref = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_sym->alias_ref - 1)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_sym->
alias_ref - 1)))
;
536 assert(ref->alias_ref == 0)((void) sizeof ((ref->alias_ref == 0) ? 1 : 0), __extension__
({ if (ref->alias_ref == 0) ; else __assert_fail ("ref->alias_ref == 0"
, "ccv_nnc_symbolic_graph_backward.c", 536, __extension__ __PRETTY_FUNCTION__
); }))
; // This is original.
537 ref->flags = CCV_NNC_TENSOR_SYMBOL_INIT_ZEROS;
538 } else if (tensor_ref->alias_registry && // Otherwise, to see if this symbol is fully occupied.
539 // Loop over to see if this tensor is fully occupied to avoid extra zero step.
540 !_ccv_nnc_tensor_ref_fully_assigned_with_aliases(tensor_ref, autograd_tensor_symbols, tensor_symbol_info)) {
541 tensor_sym->flags = CCV_NNC_TENSOR_SYMBOL_INIT_ZEROS;
542 }
543 }
544 // Check to see if any of these tensors doesn't have alias.
545 no_alias_registry |= (!tensor_ref->alias_registry);
546 const int x = tensor_ref->x;
547 assert(x >= 0)((void) sizeof ((x >= 0) ? 1 : 0), __extension__ ({ if (x >=
0) ; else __assert_fail ("x >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 547, __extension__ __PRETTY_FUNCTION__); }))
; /* Otherwise, this is initialization tensor, which is impossible to be summed up by. */
548 if (x < exec_symbol_info_size)
549 {
550 ccv_nnc_autograd_graph_exec_symbol_t* back_exec = autograd_execs + x;
551 if (!back_exec->outgoings)
552 back_exec->outgoings = ccv_array_new(sizeof(int), 1, 0);
553 ccv_array_push(back_exec->outgoings, &outgoing);
554 } else {
555 ccv_nnc_sum_or_set_graph_exec_symbol_t* sum_or_set = (ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, x - exec_symbol_info_size)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(x - exec_symbol_info_size)))
;
556 ccv_array_push(sum_or_set->outgoings, &outgoing);
557 }
558 if (tensor_ref->exec_registry)
559 for (j = 0; j < tensor_ref->exec_registry->rnum; j++)
560 {
561 const int x = *(int*)ccv_array_get(tensor_ref->exec_registry, j)((void*)(((char*)((tensor_ref->exec_registry)->data)) +
(size_t)(tensor_ref->exec_registry)->rsize * (size_t)(
j)))
;
562 assert(x >= 0)((void) sizeof ((x >= 0) ? 1 : 0), __extension__ ({ if (x >=
0) ; else __assert_fail ("x >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 562, __extension__ __PRETTY_FUNCTION__); }))
; /* Otherwise, this is initialization tensor, which is impossible to be summed up by. */
563 assert(x < exec_symbol_info_size)((void) sizeof ((x < exec_symbol_info_size) ? 1 : 0), __extension__
({ if (x < exec_symbol_info_size) ; else __assert_fail ("x < exec_symbol_info_size"
, "ccv_nnc_symbolic_graph_backward.c", 563, __extension__ __PRETTY_FUNCTION__
); }))
; // exec_registry is only used by alias_registry, it simply cannot reference to a sum operation.
564 ccv_nnc_autograd_graph_exec_symbol_t* back_exec = autograd_execs + x;
565 if (!back_exec->outgoings)
566 back_exec->outgoings = ccv_array_new(sizeof(int), 1, 0);
567 ccv_array_push(back_exec->outgoings, &outgoing);
568 }
569 }
570 const ccv_nnc_tensor_ref_t tensor_ref = {
571 .d = tensor_ref_d,
572 .x = outgoing,
573 .exec_registry = 0, // I don't need to take execution dependencies because this tensor is generated by sum, therefore, we already take that dependency.
574 .alias_registry = !no_alias_registry || has_this_alias_exclusively ? ccv_array_new(sizeof(int), 1, 0) : 0
575 };
576 // If there is no alias registry, then we take the whole tensor ref as one.
577 if (!no_alias_registry || has_this_alias_exclusively)
578 {
579 // If this tensor ref contains multiple different types of alias, have to add them together (otherwise
580 // the computation for if there is an empty slot in this tensor ref is not correct without all the
581 // occupancy availability information).
582 if (!has_this_alias_exclusively)
583 for (i = 0; i < input_size; i++)
584 {
585 ccv_nnc_tensor_ref_t* ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, kd[i].i)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(kd[i
].i)))
;
586 assert(ref->alias_registry)((void) sizeof ((ref->alias_registry) ? 1 : 0), __extension__
({ if (ref->alias_registry) ; else __assert_fail ("ref->alias_registry"
, "ccv_nnc_symbolic_graph_backward.c", 586, __extension__ __PRETTY_FUNCTION__
); }))
;
587 // It may get duplicates. But whatever, won't matter the computation.
588 for (j = 0; j < ref->alias_registry->rnum; j++)
589 ccv_array_push(tensor_ref.alias_registry, ccv_array_get(ref->alias_registry, j)((void*)(((char*)((ref->alias_registry)->data)) + (size_t
)(ref->alias_registry)->rsize * (size_t)(j)))
);
590 }
591 ccv_array_push(tensor_ref.alias_registry, &ad);
592 }
593 assert(input_size <= tensor_ver->ref_version->rnum - tensor_ver->c)((void) sizeof ((input_size <= tensor_ver->ref_version->
rnum - tensor_ver->c) ? 1 : 0), __extension__ ({ if (input_size
<= tensor_ver->ref_version->rnum - tensor_ver->c
) ; else __assert_fail ("input_size <= tensor_ver->ref_version->rnum - tensor_ver->c"
, "ccv_nnc_symbolic_graph_backward.c", 593, __extension__ __PRETTY_FUNCTION__
); }))
;
594 ccv_nnc_tensor_ref_t x;
595 for (i = 0; i < input_size; i++)
596 // If the current one (i + tensor_ver->c) is smaller than the one referenced to, exchange.
597 if (kd[i].i > i + tensor_ver->c)
598 CCV_SWAP(*(ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, i + tensor_ver->c), *(ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, kd[i].i), x)((x) = (*(ccv_nnc_tensor_ref_t*)((void*)(((char*)((tensor_ver
->ref_version)->data)) + (size_t)(tensor_ver->ref_version
)->rsize * (size_t)(i + tensor_ver->c)))), (*(ccv_nnc_tensor_ref_t
*)((void*)(((char*)((tensor_ver->ref_version)->data)) +
(size_t)(tensor_ver->ref_version)->rsize * (size_t)(i +
tensor_ver->c)))) = (*(ccv_nnc_tensor_ref_t*)((void*)(((char
*)((tensor_ver->ref_version)->data)) + (size_t)(tensor_ver
->ref_version)->rsize * (size_t)(kd[i].i)))), (*(ccv_nnc_tensor_ref_t
*)((void*)(((char*)((tensor_ver->ref_version)->data)) +
(size_t)(tensor_ver->ref_version)->rsize * (size_t)(kd
[i].i)))) = (x))
;
599 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
600 // We've consumed input_size tensor refs, now move c up to the pointer of non-consumed tensors.
601 tensor_ver->c += input_size;
602 return ad;
603}
604
605typedef struct ccv_nnc_symbolic_graph_backward_prep_s {
606 int exec_symbol_info_size; // Number of graph exec symbols before adding any new symbols related to automatic differentiation.
607 int tensor_symbol_info_size; // Number of tensor symbols before adding anything new.
608 int sub_prep_size;
609 ccv_nnc_graph_exec_symbol_info_t* exec_symbol_info;
610 ccv_nnc_tensor_symbol_info_t* tensor_symbol_info;
611 ccv_nnc_graph_backward_info_t* backward_info; // Corresponding to forward graph exec symbol info, it is exactly in reverse.
612 ccv_nnc_graph_visit_t* forward_visit; // The visitor structure (top sorted index) when doing traversal.
613 ccv_nnc_graph_visit_t* backward_visit; // The visitor structure (top sorted index) when doing reverse traversal.
614 ccv_nnc_autograd_graph_exec_symbol_t* autograd_execs; // The graph exec symbols we need for automatic differentiation. This is a 1:1 mapping for forward graph exec symbols, however, unlike backward_info, its outgoings may be more complex (may contain outgoing flows to sum nodes).
615 ccv_nnc_autograd_tensor_version_t* autograd_tensor_versions; // Corresponding to forward tensor symbols, each may contain multiple versions (due to multi-write).
616 ccv_array_t* autograd_tensor_symbols; // The tensor symbols we need for automatic differentiation (it may not be 1:1 mapping).
617 ccv_array_t* sum_or_set_execs; // The sum nodes, because in reverse mode, a tensor could have multiple versions, we need to sum them up before use.
618 struct ccv_nnc_symbolic_graph_backward_prep_s* sub_preps; // The preps of its sub-graphs.
619 // Pointers not managed by this struct
620 ccv_nnc_symbolic_graph_t* graph;
621} ccv_nnc_symbolic_graph_backward_prep_t;
622
623static ccv_nnc_symbolic_graph_backward_prep_t _ccv_nnc_symbolic_graph_backward_prep(const ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_graph_exec_symbol_t* const sources, const int source_size, const ccv_nnc_graph_exec_symbol_t* const destinations, const int destination_size)
624{
625 const int exec_symbol_info_size = graph->exec_symbol_info->rnum;
626 assert(exec_symbol_info_size > 0)((void) sizeof ((exec_symbol_info_size > 0) ? 1 : 0), __extension__
({ if (exec_symbol_info_size > 0) ; else __assert_fail ("exec_symbol_info_size > 0"
, "ccv_nnc_symbolic_graph_backward.c", 626, __extension__ __PRETTY_FUNCTION__
); }))
;
627 const int tensor_symbol_info_size = graph->tensor_symbol_info->rnum;
628 assert(tensor_symbol_info_size > 0)((void) sizeof ((tensor_symbol_info_size > 0) ? 1 : 0), __extension__
({ if (tensor_symbol_info_size > 0) ; else __assert_fail (
"tensor_symbol_info_size > 0", "ccv_nnc_symbolic_graph_backward.c"
, 628, __extension__ __PRETTY_FUNCTION__); }))
;
629 ccv_nnc_graph_exec_symbol_info_t* exec_symbol_info = (ccv_nnc_graph_exec_symbol_info_t*)ccmallocmalloc(sizeof(ccv_nnc_graph_exec_symbol_info_t) * exec_symbol_info_size);
630 ccv_nnc_tensor_symbol_info_t* tensor_symbol_info = (ccv_nnc_tensor_symbol_info_t*)ccmallocmalloc(sizeof(ccv_nnc_tensor_symbol_info_t) * tensor_symbol_info_size);
631 ccv_nnc_graph_visit_t* forward_visit = ccv_nnc_graph_visit_new(graph, (ccv_nnc_graph_exec_symbol_info_t*)ccv_array_get(graph->exec_symbol_info, 0), exec_symbol_info_size, sources, source_size, destinations, destination_size, 0)({ ccv_nnc_graph_visit_t* _visit_ = (ccv_nnc_graph_visit_t*)malloc
(sizeof(ccv_nnc_graph_visit_t) + sizeof(_visit_->node[0]) *
((exec_symbol_info_size) - 1)); _visit_->size = 0; do { typedef
struct { int8_t d; int8_t r; uint16_t c; } ccv_nnc_incoming_t
; const int _heap_mem_ = (exec_symbol_info_size > 1024); int
_i_, _j_; ccv_nnc_incoming_t* _incomings_; if (_heap_mem_) _incomings_
= (ccv_nnc_incoming_t*)malloc(sizeof(ccv_nnc_incoming_t) * (
exec_symbol_info_size) + sizeof(int32_t) * (exec_symbol_info_size
) * 2); else _incomings_ = (ccv_nnc_incoming_t*)__builtin_alloca
(sizeof(ccv_nnc_incoming_t) * (exec_symbol_info_size) + sizeof
(int32_t) * (exec_symbol_info_size) * 2); memset(_incomings_,
0, sizeof(ccv_nnc_incoming_t) * (exec_symbol_info_size)); for
(_i_ = 0; _i_ < (exec_symbol_info_size); _i_++) _incomings_
[_i_].r = 1; int32_t* _exists_[2] = { (int32_t*)(_incomings_ +
(exec_symbol_info_size)), (int32_t*)(_incomings_ + (exec_symbol_info_size
)) + (exec_symbol_info_size), }; for (_i_ = 0; _i_ < (source_size
); _i_++) { ((void) sizeof (((sources)[_i_].graph == graph) ?
1 : 0), __extension__ ({ if ((sources)[_i_].graph == graph) ;
else __assert_fail ("(sources)[_i_].graph == graph", "ccv_nnc_symbolic_graph_backward.c"
, 631, __extension__ __PRETTY_FUNCTION__); })); _exists_[0][_i_
] = (sources)[_i_].d; } int _exist_size_[2] = { (source_size)
, 0, }; int _p_ = 0, _q_ = 1; while (_exist_size_[_p_] > 0
) { _exist_size_[_q_] = 0; for (_i_ = 0; _i_ < _exist_size_
[_p_]; _i_++) { const int32_t _idx_ = _exists_[_p_][_i_]; if (
!_incomings_[_idx_].r) continue; _incomings_[_idx_].r = 0; if
(((ccv_nnc_graph_exec_symbol_info_t*)((void*)(((char*)((graph
->exec_symbol_info)->data)) + (size_t)(graph->exec_symbol_info
)->rsize * (size_t)(0))))[_idx_].outgoings) for (_j_ = 0; _j_
< ((ccv_nnc_graph_exec_symbol_info_t*)((void*)(((char*)((
graph->exec_symbol_info)->data)) + (size_t)(graph->exec_symbol_info
)->rsize * (size_t)(0))))[_idx_].outgoings->rnum; _j_++
) { const int d = *(int*)((void*)(((char*)((((ccv_nnc_graph_exec_symbol_info_t
*)((void*)(((char*)((graph->exec_symbol_info)->data)) +
(size_t)(graph->exec_symbol_info)->rsize * (size_t)(0)
)))[_idx_].outgoings)->data)) + (size_t)(((ccv_nnc_graph_exec_symbol_info_t
*)((void*)(((char*)((graph->exec_symbol_info)->data)) +
(size_t)(graph->exec_symbol_info)->rsize * (size_t)(0)
)))[_idx_].outgoings)->rsize * (size_t)(_j_))); ++_incomings_
[d].c; _exists_[_q_][_exist_size_[_q_]] = d; ++_exist_size_[_q_
]; } } ((_i_) = (_p_), (_p_) = (_q_), (_q_) = (_i_)); } for (
_i_ = 0; _i_ < (destination_size); _i_++) { ((void) sizeof
(((destinations)[_i_].graph == graph) ? 1 : 0), __extension__
({ if ((destinations)[_i_].graph == graph) ; else __assert_fail
("(destinations)[_i_].graph == graph", "ccv_nnc_symbolic_graph_backward.c"
, 631, __extension__ __PRETTY_FUNCTION__); })); _incomings_[(
destinations)[_i_].d].d = 1; } for (_i_ = 0; _i_ < (source_size
); _i_++) { ((void) sizeof (((sources)[_i_].graph == graph) ?
1 : 0), __extension__ ({ if ((sources)[_i_].graph == graph) ;
else __assert_fail ("(sources)[_i_].graph == graph", "ccv_nnc_symbolic_graph_backward.c"
, 631, __extension__ __PRETTY_FUNCTION__); })); _exists_[0][_i_
] = (sources)[_i_].d; } _p_ = 0; _q_ = 1; _exist_size_[0] = (
source_size); _exist_size_[1] = 0; int _d_ = 0; while (_exist_size_
[_p_] > 0) { _exist_size_[_q_] = 0; for (_i_ = 0; _i_ <
_exist_size_[_p_];) { const int32_t _idx_ = _exists_[_p_][_i_
]; _visit_->node[_visit_->size].index = ((_idx_)); _visit_
->node[_visit_->size].term = ((_incomings_[_idx_].d)); ++
_visit_->size;; if (_incomings_[_idx_].d) { ++_d_; _incomings_
[_idx_].r = 1; } if (((ccv_nnc_graph_exec_symbol_info_t*)((void
*)(((char*)((graph->exec_symbol_info)->data)) + (size_t
)(graph->exec_symbol_info)->rsize * (size_t)(0))))[_idx_
].outgoings) { if (((ccv_nnc_graph_exec_symbol_info_t*)((void
*)(((char*)((graph->exec_symbol_info)->data)) + (size_t
)(graph->exec_symbol_info)->rsize * (size_t)(0))))[_idx_
].outgoings->rnum == 1) { const int d = *(int*)((void*)(((
char*)((((ccv_nnc_graph_exec_symbol_info_t*)((void*)(((char*)
((graph->exec_symbol_info)->data)) + (size_t)(graph->
exec_symbol_info)->rsize * (size_t)(0))))[_idx_].outgoings
)->data)) + (size_t)(((ccv_nnc_graph_exec_symbol_info_t*)(
(void*)(((char*)((graph->exec_symbol_info)->data)) + (size_t
)(graph->exec_symbol_info)->rsize * (size_t)(0))))[_idx_
].outgoings)->rsize * (size_t)(0))); --_incomings_[d].c; if
(_incomings_[d].c == 0 && _d_ < (destination_size
)) { _exists_[_p_][_i_] = d; continue; } } else for (_j_ = 0;
_j_ < ((ccv_nnc_graph_exec_symbol_info_t*)((void*)(((char
*)((graph->exec_symbol_info)->data)) + (size_t)(graph->
exec_symbol_info)->rsize * (size_t)(0))))[_idx_].outgoings
->rnum; _j_++) { const int d = *(int*)((void*)(((char*)(((
(ccv_nnc_graph_exec_symbol_info_t*)((void*)(((char*)((graph->
exec_symbol_info)->data)) + (size_t)(graph->exec_symbol_info
)->rsize * (size_t)(0))))[_idx_].outgoings)->data)) + (
size_t)(((ccv_nnc_graph_exec_symbol_info_t*)((void*)(((char*)
((graph->exec_symbol_info)->data)) + (size_t)(graph->
exec_symbol_info)->rsize * (size_t)(0))))[_idx_].outgoings
)->rsize * (size_t)(_j_))); --_incomings_[d].c; if (_incomings_
[d].c == 0 && _d_ < (destination_size)) { _exists_
[_q_][_exist_size_[_q_]] = d; ++_exist_size_[_q_]; } } } ++_i_
; } ((_i_) = (_p_), (_p_) = (_q_), (_q_) = (_i_)); } for (_i_
= 0; _i_ < (destination_size); _i_++) { ((void) sizeof ((
(destinations)[_i_].graph == graph) ? 1 : 0), __extension__ (
{ if ((destinations)[_i_].graph == graph) ; else __assert_fail
("(destinations)[_i_].graph == graph", "ccv_nnc_symbolic_graph_backward.c"
, 631, __extension__ __PRETTY_FUNCTION__); })); if (_incomings_
[(destinations)[_i_].d].r) continue; if (!(0)) { ((void) sizeof
((_incomings_[(destinations)[_i_].d].c == 0) ? 1 : 0), __extension__
({ if (_incomings_[(destinations)[_i_].d].c == 0) ; else __assert_fail
("_incomings_[(destinations)[_i_].d].c == 0", "ccv_nnc_symbolic_graph_backward.c"
, 631, __extension__ __PRETTY_FUNCTION__); })); } else if (_incomings_
[(destinations)[_i_].d].c > 0) continue; _visit_->node[
_visit_->size].index = (((destinations)[_i_].d)); _visit_->
node[_visit_->size].term = ((_incomings_[(destinations)[_i_
].d].d)); ++_visit_->size;; } if (_heap_mem_) free(_incomings_
); } while (0);; ((void) sizeof ((_visit_->size <= (exec_symbol_info_size
)) ? 1 : 0), __extension__ ({ if (_visit_->size <= (exec_symbol_info_size
)) ; else __assert_fail ("_visit_->size <= (exec_symbol_info_size)"
, "ccv_nnc_symbolic_graph_backward.c", 631, __extension__ __PRETTY_FUNCTION__
); })); _visit_; })
;
632 ccv_nnc_symbolic_graph_symbol_infer(graph, forward_visit, sources, source_size, destinations, destination_size, 0, 0, tensor_symbol_info, exec_symbol_info);
633 int i;
634 // Now, for each one of these, find a reverse graph.
635 ccv_nnc_graph_backward_info_t* backward_info = (ccv_nnc_graph_backward_info_t*)cccalloccalloc(exec_symbol_info_size, sizeof(ccv_nnc_graph_backward_info_t));
636 ccv_nnc_graph_visit_for(forward_visit, exec_symbol_info, node, idx){ int _i_; for (_i_ = 0; _i_ < (forward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (forward_visit)
->node[_i_].index; const int _node_unused_ __attribute__((
unused)) = (forward_visit)->node[_i_].term; typeof ((exec_symbol_info
)) const node __attribute__((unused)) = (exec_symbol_info) + idx
;
{
637 assert(ccv_nnc_cmd_is_forward(node->cmd) || node->cmd.cmd == CCV_NNC_NOOP)((void) sizeof ((ccv_nnc_cmd_is_forward(node->cmd) || node
->cmd.cmd == CCV_NNC_NOOP) ? 1 : 0), __extension__ ({ if (
ccv_nnc_cmd_is_forward(node->cmd) || node->cmd.cmd == CCV_NNC_NOOP
) ; else __assert_fail ("ccv_nnc_cmd_is_forward(node->cmd) || node->cmd.cmd == CCV_NNC_NOOP"
, "ccv_nnc_symbolic_graph_backward.c", 637, __extension__ __PRETTY_FUNCTION__
); }))
;
638 if (node->outgoings)
639 for (i = 0; i < node->outgoings->rnum; i++)
640 {
641 int d = *(int*)ccv_array_get(node->outgoings, i)((void*)(((char*)((node->outgoings)->data)) + (size_t)(
node->outgoings)->rsize * (size_t)(i)))
;
642 if (backward_info[d].outgoings == 0)
643 backward_info[d].outgoings = ccv_array_new(sizeof(int32_t), 1, 0);
644 ccv_array_push(backward_info[d].outgoings, &idx);
645 }
646 } ccv_nnc_graph_visit_endfor} }
647 // Also mark only the output bits that we use.
648 for (i = 0; i < exec_symbol_info_size; i++)
649 {
650 backward_info[i].input_bitmask_size = ((exec_symbol_info[i].output_size * 2 + exec_symbol_info[i].input_size + 63) >> 6);
651 backward_info[i].output_bitmask_size = ((exec_symbol_info[i].input_size + 63) >> 6);
652 // Allocate input / output bitmasks
653 if (backward_info[i].input_bitmask_size + backward_info[i].output_bitmask_size > 0)
654 {
655 backward_info[i].input_bitmasks = (uint64_t*)cccalloccalloc(backward_info[i].input_bitmask_size + backward_info[i].output_bitmask_size, sizeof(uint64_t));
656 if (backward_info[i].output_bitmask_size)
657 backward_info[i].output_bitmasks = backward_info[i].input_bitmasks + backward_info[i].input_bitmask_size;
658 }
659 }
660 ccv_nnc_graph_visit_t* backward_visit = ccv_nnc_graph_visit_new(graph, backward_info, exec_symbol_info_size, destinations, destination_size, sources, source_size, 0)({ ccv_nnc_graph_visit_t* _visit_ = (ccv_nnc_graph_visit_t*)malloc
(sizeof(ccv_nnc_graph_visit_t) + sizeof(_visit_->node[0]) *
((exec_symbol_info_size) - 1)); _visit_->size = 0; do { typedef
struct { int8_t d; int8_t r; uint16_t c; } ccv_nnc_incoming_t
; const int _heap_mem_ = (exec_symbol_info_size > 1024); int
_i_, _j_; ccv_nnc_incoming_t* _incomings_; if (_heap_mem_) _incomings_
= (ccv_nnc_incoming_t*)malloc(sizeof(ccv_nnc_incoming_t) * (
exec_symbol_info_size) + sizeof(int32_t) * (exec_symbol_info_size
) * 2); else _incomings_ = (ccv_nnc_incoming_t*)__builtin_alloca
(sizeof(ccv_nnc_incoming_t) * (exec_symbol_info_size) + sizeof
(int32_t) * (exec_symbol_info_size) * 2); memset(_incomings_,
0, sizeof(ccv_nnc_incoming_t) * (exec_symbol_info_size)); for
(_i_ = 0; _i_ < (exec_symbol_info_size); _i_++) _incomings_
[_i_].r = 1; int32_t* _exists_[2] = { (int32_t*)(_incomings_ +
(exec_symbol_info_size)), (int32_t*)(_incomings_ + (exec_symbol_info_size
)) + (exec_symbol_info_size), }; for (_i_ = 0; _i_ < (destination_size
); _i_++) { ((void) sizeof (((destinations)[_i_].graph == graph
) ? 1 : 0), __extension__ ({ if ((destinations)[_i_].graph ==
graph) ; else __assert_fail ("(destinations)[_i_].graph == graph"
, "ccv_nnc_symbolic_graph_backward.c", 660, __extension__ __PRETTY_FUNCTION__
); })); _exists_[0][_i_] = (destinations)[_i_].d; } int _exist_size_
[2] = { (destination_size), 0, }; int _p_ = 0, _q_ = 1; while
(_exist_size_[_p_] > 0) { _exist_size_[_q_] = 0; for (_i_
= 0; _i_ < _exist_size_[_p_]; _i_++) { const int32_t _idx_
= _exists_[_p_][_i_]; if (!_incomings_[_idx_].r) continue; _incomings_
[_idx_].r = 0; if ((backward_info)[_idx_].outgoings) for (_j_
= 0; _j_ < (backward_info)[_idx_].outgoings->rnum; _j_
++) { const int d = *(int*)((void*)(((char*)(((backward_info)
[_idx_].outgoings)->data)) + (size_t)((backward_info)[_idx_
].outgoings)->rsize * (size_t)(_j_))); ++_incomings_[d].c;
_exists_[_q_][_exist_size_[_q_]] = d; ++_exist_size_[_q_]; }
} ((_i_) = (_p_), (_p_) = (_q_), (_q_) = (_i_)); } for (_i_ =
0; _i_ < (source_size); _i_++) { ((void) sizeof (((sources
)[_i_].graph == graph) ? 1 : 0), __extension__ ({ if ((sources
)[_i_].graph == graph) ; else __assert_fail ("(sources)[_i_].graph == graph"
, "ccv_nnc_symbolic_graph_backward.c", 660, __extension__ __PRETTY_FUNCTION__
); })); _incomings_[(sources)[_i_].d].d = 1; } for (_i_ = 0; _i_
< (destination_size); _i_++) { ((void) sizeof (((destinations
)[_i_].graph == graph) ? 1 : 0), __extension__ ({ if ((destinations
)[_i_].graph == graph) ; else __assert_fail ("(destinations)[_i_].graph == graph"
, "ccv_nnc_symbolic_graph_backward.c", 660, __extension__ __PRETTY_FUNCTION__
); })); _exists_[0][_i_] = (destinations)[_i_].d; } _p_ = 0; _q_
= 1; _exist_size_[0] = (destination_size); _exist_size_[1] =
0; int _d_ = 0; while (_exist_size_[_p_] > 0) { _exist_size_
[_q_] = 0; for (_i_ = 0; _i_ < _exist_size_[_p_];) { const
int32_t _idx_ = _exists_[_p_][_i_]; _visit_->node[_visit_
->size].index = ((_idx_)); _visit_->node[_visit_->size
].term = ((_incomings_[_idx_].d)); ++_visit_->size;; if (_incomings_
[_idx_].d) { ++_d_; _incomings_[_idx_].r = 1; } if ((backward_info
)[_idx_].outgoings) { if ((backward_info)[_idx_].outgoings->
rnum == 1) { const int d = *(int*)((void*)(((char*)(((backward_info
)[_idx_].outgoings)->data)) + (size_t)((backward_info)[_idx_
].outgoings)->rsize * (size_t)(0))); --_incomings_[d].c; if
(_incomings_[d].c == 0 && _d_ < (source_size)) { _exists_
[_p_][_i_] = d; continue; } } else for (_j_ = 0; _j_ < (backward_info
)[_idx_].outgoings->rnum; _j_++) { const int d = *(int*)((
void*)(((char*)(((backward_info)[_idx_].outgoings)->data))
+ (size_t)((backward_info)[_idx_].outgoings)->rsize * (size_t
)(_j_))); --_incomings_[d].c; if (_incomings_[d].c == 0 &&
_d_ < (source_size)) { _exists_[_q_][_exist_size_[_q_]] =
d; ++_exist_size_[_q_]; } } } ++_i_; } ((_i_) = (_p_), (_p_)
= (_q_), (_q_) = (_i_)); } for (_i_ = 0; _i_ < (source_size
); _i_++) { ((void) sizeof (((sources)[_i_].graph == graph) ?
1 : 0), __extension__ ({ if ((sources)[_i_].graph == graph) ;
else __assert_fail ("(sources)[_i_].graph == graph", "ccv_nnc_symbolic_graph_backward.c"
, 660, __extension__ __PRETTY_FUNCTION__); })); if (_incomings_
[(sources)[_i_].d].r) continue; if (!(0)) { ((void) sizeof ((
_incomings_[(sources)[_i_].d].c == 0) ? 1 : 0), __extension__
({ if (_incomings_[(sources)[_i_].d].c == 0) ; else __assert_fail
("_incomings_[(sources)[_i_].d].c == 0", "ccv_nnc_symbolic_graph_backward.c"
, 660, __extension__ __PRETTY_FUNCTION__); })); } else if (_incomings_
[(sources)[_i_].d].c > 0) continue; _visit_->node[_visit_
->size].index = (((sources)[_i_].d)); _visit_->node[_visit_
->size].term = ((_incomings_[(sources)[_i_].d].d)); ++_visit_
->size;; } if (_heap_mem_) free(_incomings_); } while (0);
; ((void) sizeof ((_visit_->size <= (exec_symbol_info_size
)) ? 1 : 0), __extension__ ({ if (_visit_->size <= (exec_symbol_info_size
)) ; else __assert_fail ("_visit_->size <= (exec_symbol_info_size)"
, "ccv_nnc_symbolic_graph_backward.c", 660, __extension__ __PRETTY_FUNCTION__
); })); _visit_; })
;
661 const int sub_prep_size = graph->sub_graphs ? graph->sub_graphs->rnum : 0;
662 ccv_nnc_symbolic_graph_backward_prep_t* sub_preps = sub_prep_size > 0 ? (ccv_nnc_symbolic_graph_backward_prep_t*)cccalloccalloc(sub_prep_size, sizeof(ccv_nnc_symbolic_graph_backward_prep_t)) : 0;
663 for (i = 0; i < sub_prep_size; i++)
664 {
665 const ccv_nnc_symbolic_graph_t* const sub_graph = *(ccv_nnc_symbolic_graph_t**)ccv_array_get(graph->sub_graphs, i)((void*)(((char*)((graph->sub_graphs)->data)) + (size_t
)(graph->sub_graphs)->rsize * (size_t)(i)))
;
666 sub_preps[i] = _ccv_nnc_symbolic_graph_backward_prep(sub_graph, ccv_nnc_symbolic_graph_sources(sub_graph), ccv_nnc_symbolic_graph_source_size(sub_graph), ccv_nnc_symbolic_graph_destinations(sub_graph), ccv_nnc_symbolic_graph_destination_size(sub_graph));
667 }
668 return (ccv_nnc_symbolic_graph_backward_prep_t){
669 .exec_symbol_info_size = exec_symbol_info_size,
670 .tensor_symbol_info_size = tensor_symbol_info_size,
671 .sub_prep_size = sub_prep_size,
672 .exec_symbol_info = exec_symbol_info,
673 .tensor_symbol_info = tensor_symbol_info,
674 .backward_info = backward_info,
675 .forward_visit = forward_visit,
676 .backward_visit = backward_visit,
677 .sub_preps = sub_preps,
678 .graph = (ccv_nnc_symbolic_graph_t*)graph,
679 };
680}
681
682static void _ccv_nnc_symbolic_graph_backward_exec_io(const ccv_nnc_graph_exec_symbol_info_t* const node, int** const back_input_map, int** const back_output_map, int* const back_input_size, int* const back_output_size)
683{
684 int i;
685 if (node->flags & CCV_NNC_GRAPH_EXEC_CASE_OF)
686 {
687 *back_input_map = node->outputs;
688 *back_input_size = node->output_size;
689 for (i = 0; i < node->case_of.argument.offset; i++)
690 (*back_output_map)[i] = node->inputs[i];
691 const int argument_offset = node->case_of.argument.offset;
692 const int argument_size = node->case_of.argument.size;
693 // Skip the argument range.
694 for (i = argument_offset + argument_size; i < node->input_size; i++)
695 (*back_output_map)[i - argument_size] = node->inputs[i];
696 *back_output_size = node->input_size - node->case_of.argument.size;
697 } else { // if (node->flags & CCV_NNC_GRAPH_EXEC_P_WHILE) {
698 *back_input_map = node->outputs;
699 *back_input_size = node->output_size;
700 *back_output_map = node->inputs;
701 *back_output_size = node->input_size;
702 }
703}
704
705static void _ccv_nnc_symbolic_graph_backward_prep_sub_f_wrt_symbols(const ccv_nnc_graph_exec_symbol_info_t* const forw_exec, const ccv_nnc_symbolic_graph_t* const sub_graph, const int graph_ref, const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info, const uint64_t* const input_bitmasks, const uint64_t* const output_bitmasks, ccv_array_t* const sub_f_symbols, ccv_array_t* const sub_wrt_symbols)
706{
707 int i, j;
708 ccv_array_clear(sub_wrt_symbols);
709 int forw_outputs[ccv_max(1, forw_exec->output_size)({ typeof (1) _a = (1); typeof (forw_exec->output_size) _b
= (forw_exec->output_size); (_a > _b) ? _a : _b; })
];
19
Assuming '_a' is <= '_b'
20
'?' condition is false
710 int forw_inputs[ccv_max(1, forw_exec->input_size)({ typeof (1) _a = (1); typeof (forw_exec->input_size) _b =
(forw_exec->input_size); (_a > _b) ? _a : _b; })
];
21
Assuming '_a' is <= '_b'
22
'?' condition is false
711 int* back_input_map = forw_outputs;
712 int* back_output_map = forw_inputs;
713 int back_input_size, back_output_size;
714 _ccv_nnc_symbolic_graph_backward_exec_io(forw_exec, &back_input_map, &back_output_map, &back_input_size, &back_output_size);
715 for (i = 0; i < back_output_size; i++)
23
Assuming 'i' is < 'back_output_size'
24
Loop condition is true. Entering loop body
716 if (output_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63)))
25
Assuming the condition is true
26
Taking true branch
717 {
718 const int d = back_output_map[i];
27
Assigned value is garbage or undefined
719 const ccv_array_t* const s_refs = tensor_symbol_info[d].s_ref;
720 const int s_ref = s_refs && s_refs->rnum > graph_ref ? *(int*)ccv_array_get(s_refs, graph_ref)((void*)(((char*)((s_refs)->data)) + (size_t)(s_refs)->
rsize * (size_t)(graph_ref)))
- 1 : -1;
721 if (s_ref >= 0)
722 {
723 ccv_nnc_tensor_symbol_t sub_wrt_symbol = {
724 .d = s_ref,
725 .graph = sub_graph,
726 };
727 ccv_array_push(sub_wrt_symbols, &sub_wrt_symbol);
728 } else
729 ccv_array_push(sub_wrt_symbols, &NO_TENSOR_SYMBOL(ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL});
730 }
731 ccv_array_clear(sub_f_symbols);
732 for (i = 0; i < back_input_size; i++)
733 if (input_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63)))
734 {
735 const int d = back_input_map[i];
736 ccv_nnc_tensor_symbol_t sub_f_symbol = {
737 .d = *(int*)ccv_array_get(tensor_symbol_info[d].s_ref, graph_ref)((void*)(((char*)((tensor_symbol_info[d].s_ref)->data)) + (
size_t)(tensor_symbol_info[d].s_ref)->rsize * (size_t)(graph_ref
)))
- 1,
738 .graph = sub_graph,
739 };
740 ccv_array_push(sub_f_symbols, &sub_f_symbol);
741 }
742 // Go through all its assignments (parameterized loop), making them either wrt or f.
743 // The reason is these must flow through the graph, otherwise we cannot form a full
744 // enclosed loop. Also because they are the additional f / wrt symbols, there is
745 // no case that we cannot find their corresponding gradients in the backward sub graphs
746 // (these gradients have to be parameterized to form an enclosed loop as well).
747 for (i = 0; i < sub_graph->tensor_symbol_info->rnum; i++)
748 {
749 const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info = (ccv_nnc_tensor_symbol_info_t*)ccv_array_get(sub_graph->tensor_symbol_info, i)((void*)(((char*)((sub_graph->tensor_symbol_info)->data
)) + (size_t)(sub_graph->tensor_symbol_info)->rsize * (
size_t)(i)))
;
750 if (tensor_symbol_info->assign_ref)
751 {
752 const int assign_ref = tensor_symbol_info->assign_ref - 1;
753 // i is the wrt, assign_ref is the f.
754 int flag = 0;
755 for (j = 0; !flag && j < sub_wrt_symbols->rnum; j++)
756 flag = (((ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, j)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(j)))
)->d == i);
757 if (!flag)
758 {
759 ccv_nnc_tensor_symbol_t sub_wrt_symbol = {
760 .d = i,
761 .graph = sub_graph,
762 };
763 ccv_array_push(sub_wrt_symbols, &sub_wrt_symbol);
764 }
765 flag = 0;
766 for (j = 0; !flag && j < sub_f_symbols->rnum; j++)
767 flag = (((ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_f_symbols, j)((void*)(((char*)((sub_f_symbols)->data)) + (size_t)(sub_f_symbols
)->rsize * (size_t)(j)))
)->d == assign_ref);
768 if (!flag)
769 {
770 ccv_nnc_tensor_symbol_t sub_f_symbol = {
771 .d = assign_ref,
772 .graph = sub_graph,
773 };
774 ccv_array_push(sub_f_symbols, &sub_f_symbol);
775 }
776 }
777 }
778}
779
780// Check whether for a given f_symbol, we can compute wrt_symbols at all, if we can, tag the minimal io and ops (some ops can be replaced with noop) required to do so.
781static int _ccv_nnc_symbolic_graph_backward_prep_prune_ops(const ccv_nnc_symbolic_graph_backward_prep_t* const backward_prep, const ccv_nnc_tensor_symbol_t* const f_symbols, const int f_symbol_size, const ccv_nnc_tensor_symbol_t* const wrt_symbols, const int wrt_symbol_size, const ccv_nnc_graph_exec_symbol_t* const sources, const int source_size, const ccv_nnc_graph_exec_symbol_t* const destinations, const int destination_size)
782{
783 int i, j, p;
784 const int tensor_symbol_info_size = backward_prep->tensor_symbol_info_size;
785 const ccv_nnc_graph_exec_symbol_info_t* const exec_symbol_info = backward_prep->exec_symbol_info;
786 const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info =backward_prep->tensor_symbol_info;
787 const ccv_nnc_graph_visit_t* const forward_visit = backward_prep->forward_visit;
788 // Now, for each one of these, find a reverse graph.
789 ccv_nnc_graph_backward_info_t* const backward_info = backward_prep->backward_info;
790 const ccv_nnc_graph_visit_t* const backward_visit = backward_prep->backward_visit;
791 // Find the f_symbols, and tag its flows.
792 ccv_nnc_graph_visit_for(backward_visit, backward_info, node, idx){ int _i_; for (_i_ = 0; _i_ < (backward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (backward_visit
)->node[_i_].index; const int _node_unused_ __attribute__(
(unused)) = (backward_visit)->node[_i_].term; typeof ((backward_info
)) const node __attribute__((unused)) = (backward_info) + idx
;
{
793 int f = node->f_wrt & 0x1;
794 for (i = 0; i < exec_symbol_info[idx].output_size && !f; i++)
795 {
796 int d = exec_symbol_info[idx].outputs[i];
797 if (d < 0)
798 continue;
799 while (tensor_symbol_info[d].alias_ref)
800 d = tensor_symbol_info[d].alias_ref - 1;
801 for (j = 0; j < f_symbol_size && !f; j++)
802 if (d == f_symbols[j].d)
803 f = 1;
804 }
805 if (f)
806 {
807 node->f_wrt |= f;
808 if (node->outgoings)
809 for (i = 0; i < node->outgoings->rnum; i++)
810 {
811 int d = *(int*)ccv_array_get(node->outgoings, i)((void*)(((char*)((node->outgoings)->data)) + (size_t)(
node->outgoings)->rsize * (size_t)(i)))
;
812 backward_info[d].f_wrt |= f;
813 }
814 }
815 } ccv_nnc_graph_visit_endfor} }
816 // Find the wrt_symbols, and tag its flows.
817 ccv_nnc_graph_visit_for(forward_visit, exec_symbol_info, node, idx){ int _i_; for (_i_ = 0; _i_ < (forward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (forward_visit)
->node[_i_].index; const int _node_unused_ __attribute__((
unused)) = (forward_visit)->node[_i_].term; typeof ((exec_symbol_info
)) const node __attribute__((unused)) = (exec_symbol_info) + idx
;
{
818 int wrt = backward_info[idx].f_wrt & 0x2;
819 for (i = 0; i < node->input_size && !wrt; i++)
820 {
821 int d = node->inputs[i];
822 if (d < 0)
823 continue;
824 while (tensor_symbol_info[d].alias_ref)
825 d = tensor_symbol_info[d].alias_ref - 1;
826 for (j = 0; j < wrt_symbol_size && !wrt; j++)
827 if (d == wrt_symbols[j].d)
828 wrt = 0x2;
829 }
830 if (wrt)
831 {
832 backward_info[idx].f_wrt |= wrt;
833 if (node->outgoings)
834 for (i = 0; i < node->outgoings->rnum; i++)
835 {
836 int d = *(int*)ccv_array_get(node->outgoings, i)((void*)(((char*)((node->outgoings)->data)) + (size_t)(
node->outgoings)->rsize * (size_t)(i)))
;
837 backward_info[d].f_wrt |= wrt;
838 }
839 }
840 } ccv_nnc_graph_visit_endfor} }
841 enum {
842 WRT_SYMBOL_USE = 1,
843 F_SYMBOL_USE = 2
844 };
845 uint8_t* used_grad = (uint8_t*)cccalloccalloc(tensor_symbol_info_size, sizeof(uint8_t));
846 // First, all f_symbols and wrt_symbols are used.
847 for (i = 0; i < f_symbol_size; i++)
848 if (f_symbols[i].d >= 0)
849 used_grad[tensor_symbol_info[f_symbols[i].d].alias_ref ? tensor_symbol_info[f_symbols[i].d].alias_ref - 1 : f_symbols[i].d] |= F_SYMBOL_USE;
850 for (i = 0; i < wrt_symbol_size; i++)
851 if (wrt_symbols[i].d >= 0)
852 used_grad[tensor_symbol_info[wrt_symbols[i].d].alias_ref ? tensor_symbol_info[wrt_symbols[i].d].alias_ref - 1 : wrt_symbols[i].d] |= WRT_SYMBOL_USE;
853 // Do optimistic assumption, and then compute used_grad
854 ccv_nnc_graph_visit_for(forward_visit, exec_symbol_info, _, idx){ int _i_; for (_i_ = 0; _i_ < (forward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (forward_visit)
->node[_i_].index; const int _node_unused_ __attribute__((
unused)) = (forward_visit)->node[_i_].term; typeof ((exec_symbol_info
)) const _ __attribute__((unused)) = (exec_symbol_info) + idx
;
{
855 ccv_nnc_graph_backward_info_t* node = backward_info + idx;
856 /* Only interested in the ones on the f / wrt flow */
857 if ((node->f_wrt & 0x3) == 0x3)
858 {
859 const ccv_nnc_graph_exec_symbol_info_t* forw_exec = exec_symbol_info + idx;
860 ccv_nnc_cmd_t cmd = forw_exec->cmd;
861 if (cmd.cmd != CCV_NNC_NOOP)
862 cmd.cmd += 1; /* Backward command is the one after forward command. */
863 assert(ccv_nnc_cmd_is_backward(cmd) || cmd.cmd == CCV_NNC_NOOP)((void) sizeof ((ccv_nnc_cmd_is_backward(cmd) || cmd.cmd == CCV_NNC_NOOP
) ? 1 : 0), __extension__ ({ if (ccv_nnc_cmd_is_backward(cmd)
|| cmd.cmd == CCV_NNC_NOOP) ; else __assert_fail ("ccv_nnc_cmd_is_backward(cmd) || cmd.cmd == CCV_NNC_NOOP"
, "ccv_nnc_symbolic_graph_backward.c", 863, __extension__ __PRETTY_FUNCTION__
); }))
;
864 for (i = 0; i < forw_exec->output_size * 2 + forw_exec->input_size; i++)
865 if (!(i >= forw_exec->output_size && i < forw_exec->output_size + forw_exec->input_size &&
866 forw_exec->inputs[i - forw_exec->output_size] < 0) && // If the input is empty, no need.
867 !(i >= forw_exec->output_size + forw_exec->input_size && i < forw_exec->output_size * 2 + forw_exec->input_size &&
868 forw_exec->outputs[i - forw_exec->output_size - forw_exec->input_size] < 0) && // If the output is empty, no need.
869 !(i < forw_exec->output_size && forw_exec->outputs[i] < 0)) // If the output is empty for gradient, no need.
870 node->input_bitmasks[i >> 6] |= ((uint64_t)1 << (i & 63));
871 for (i = 0; i < forw_exec->input_size; i++)
872 if (!(forw_exec->inputs[i] < 0)) // If the inputs is empty, no need.
873 node->output_bitmasks[i >> 6] |= ((uint64_t)1 << (i & 63));
874 int maybe_noop = 1;
875 for (i = 0; i < forw_exec->input_size; i++)
876 /* See if it is used as wrt, if not, no need to run this node at all. */
877 if (forw_exec->inputs[i] >= 0 && used_grad[tensor_symbol_info[forw_exec->inputs[i]].alias_ref ? tensor_symbol_info[forw_exec->inputs[i]].alias_ref - 1 : forw_exec->inputs[i]] & WRT_SYMBOL_USE)
878 {
879 maybe_noop = 0;
880 break;
881 }
882 if (maybe_noop)
883 {
884 for (i = 0; i < node->input_bitmask_size; i++)
885 node->input_bitmasks[i] = 0;
886 for (i = 0; i < node->output_bitmask_size; i++)
887 node->output_bitmasks[i] = 0;
888 node->output_bitmask_size = 0;
889 } else if (cmd.cmd == CCV_NNC_GRAPH_FORWARD || cmd.cmd == CCV_NNC_GRAPH_BACKWARD) {
890 // Clear out all potential outputs if we think it is not a wrt symbols.
891 for (i = 0; i < forw_exec->input_size; i++)
892 if ((node->output_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63))) &&
893 !(used_grad[tensor_symbol_info[forw_exec->inputs[i]].alias_ref ? tensor_symbol_info[forw_exec->inputs[i]].alias_ref - 1 : forw_exec->inputs[i]] & WRT_SYMBOL_USE))
894 node->output_bitmasks[i >> 6] &= ~((uint64_t)1 << (i & 63));
895 // But for now, assuming we need all input gradients.
896 // Clear out all inputs / outputs from forward op.
897 for (i = forw_exec->output_size; i < forw_exec->output_size * 2 + forw_exec->input_size; i++)
898 node->input_bitmasks[i >> 6] &= ~((uint64_t)1 << (i & 63));
899 } else if (ccv_nnc_cmd_bitmask(cmd, forw_exec->output_size * 2 + forw_exec->input_size, forw_exec->input_size, node->input_bitmasks, node->input_bitmask_size, node->output_bitmasks, node->output_bitmask_size)) {
900 int flag; /* Only continue if it changed */
901 do {
902 flag = 0;
903 /* Check if the output first */
904 for (i = 0; i < forw_exec->input_size; i++)
905 /* Only try to eliminate the one that is not used. */
906 if ((node->output_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63))) &&
907 !(used_grad[tensor_symbol_info[forw_exec->inputs[i]].alias_ref ? tensor_symbol_info[forw_exec->inputs[i]].alias_ref - 1 : forw_exec->inputs[i]] & WRT_SYMBOL_USE))
908 {
909 node->output_bitmasks[i >> 6] &= ~((uint64_t)1 << (i & 63));
910 /* If it worked, mark it as flagged. */
911 if (ccv_nnc_cmd_bitmask(cmd, forw_exec->output_size * 2 + forw_exec->input_size, forw_exec->input_size, node->input_bitmasks, node->input_bitmask_size, node->output_bitmasks, node->output_bitmask_size))
912 flag = 1;
913 else /* Refit this with the bit back again. */
914 node->output_bitmasks[i >> 6] |= ((uint64_t)1 << (i & 63));
915 }
916 for (i = 0; i < forw_exec->output_size * 2 + forw_exec->input_size; i++)
917 if ((node->input_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63))) &&
918 (i >= forw_exec->output_size ||
919 !(used_grad[tensor_symbol_info[forw_exec->outputs[i]].alias_ref ? tensor_symbol_info[forw_exec->outputs[i]].alias_ref - 1 : forw_exec->outputs[i]] & F_SYMBOL_USE)))
920 { /* Try to eliminate one of the input. */
921 node->input_bitmasks[i >> 6] &= ~((uint64_t)1 << (i & 63));
922 /* If it worked, mark it as flagged. */
923 if (ccv_nnc_cmd_bitmask(cmd, forw_exec->output_size * 2 + forw_exec->input_size, forw_exec->input_size, node->input_bitmasks, node->input_bitmask_size, node->output_bitmasks, node->output_bitmask_size))
924 flag = 1;
925 else /* Refit this with the bit back again. */
926 node->input_bitmasks[i >> 6] |= ((uint64_t)1 << (i & 63));
927 }
928 } while (flag);
929 }
930 for (i = 0; i < forw_exec->output_size; i++)
931 if (node->input_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63)))
932 /* Mark it is used as wrt. */
933 used_grad[tensor_symbol_info[forw_exec->outputs[i]].alias_ref ? tensor_symbol_info[forw_exec->outputs[i]].alias_ref - 1 : forw_exec->outputs[i]] |= WRT_SYMBOL_USE;
934 for (i = 0; i < forw_exec->input_size; i++)
935 /* Mark it is used as f. */
936 if (node->output_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63)))
937 used_grad[tensor_symbol_info[forw_exec->inputs[i]].alias_ref ? tensor_symbol_info[forw_exec->inputs[i]].alias_ref - 1 : forw_exec->inputs[i]] |= F_SYMBOL_USE;
938 }
939 } ccv_nnc_graph_visit_endfor} }
940 ccv_array_t* sub_f_symbols = 0;
941 ccv_array_t* sub_wrt_symbols = 0;
942 ccv_nnc_graph_visit_for(forward_visit, exec_symbol_info, _, idx){ int _i_; for (_i_ = 0; _i_ < (forward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (forward_visit)
->node[_i_].index; const int _node_unused_ __attribute__((
unused)) = (forward_visit)->node[_i_].term; typeof ((exec_symbol_info
)) const _ __attribute__((unused)) = (exec_symbol_info) + idx
;
{
943 ccv_nnc_graph_backward_info_t* node = backward_info + idx;
944 const ccv_nnc_graph_exec_symbol_info_t* forw_exec = exec_symbol_info + idx;
945 /* Only interested in the ones on the f / wrt flow */
946 if ((node->f_wrt & 0x3) == 0x3 && forw_exec->graph_ref_size > 0)
947 {
948 uint64_t stack_input_bitmasks1[node->input_bitmask_size];
949 uint64_t stack_input_bitmasks2[node->input_bitmask_size];
950 uint64_t* const input_bitmasks = forw_exec->graph_ref_size > 1 ? stack_input_bitmasks1 : node->input_bitmasks;
951 // We collect input masks into this location.
952 if (forw_exec->graph_ref_size > 1)
953 memset(stack_input_bitmasks2, 0, sizeof(uint64_t) * node->input_bitmask_size);
954 for (p = 0; p < forw_exec->graph_ref_size; p++)
955 {
956 // Reset the stack input bitmasks.
957 if (forw_exec->graph_ref_size > 1)
958 memcpy(stack_input_bitmasks1, node->input_bitmasks, sizeof(uint64_t) * node->input_bitmask_size);
959 // Now calling it recursively until we are sure no f_symbols can be removed.
960 const int graph_ref = CCV_NNC_GRAPH_REF(forw_exec)((forw_exec)->_heap_graph_ref ? (forw_exec)->_heap_graph_ref
: (forw_exec)->_inline_graph_ref)
[p] - 1;
961 ccv_nnc_symbolic_graph_backward_prep_t* const sub_prep = backward_prep->sub_preps + graph_ref;
962 if (!sub_wrt_symbols)
963 sub_wrt_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
964 else
965 ccv_array_clear(sub_wrt_symbols);
966 for (i = 0; i < forw_exec->input_size; i++)
967 if (node->output_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63)))
968 {
969 const ccv_array_t* const s_refs = tensor_symbol_info[forw_exec->inputs[i]].s_ref;
970 const int s_ref = s_refs && s_refs->rnum > graph_ref ? *(int*)ccv_array_get(s_refs, graph_ref)((void*)(((char*)((s_refs)->data)) + (size_t)(s_refs)->
rsize * (size_t)(graph_ref)))
- 1 : -1;
971 if (s_ref >= 0)
972 {
973 ccv_nnc_tensor_symbol_t sub_wrt_symbol = {
974 .d = s_ref,
975 .graph = sub_prep->graph,
976 };
977 ccv_array_push(sub_wrt_symbols, &sub_wrt_symbol);
978 }
979 }
980 int flag; // Only continue if it changed */
981 do {
982 flag = 0;
983 for (i = 0; i < forw_exec->output_size; i++)
984 // Try to reduce number of inputs for the backward graph. If it is not tagged as F_SYMBOL_USE, we can reduce it.
985 // It is reducible because this sub graph may have multiple computation paths, therefore, some of these may not
986 // involve our wrt symbols at all.
987 if (!(used_grad[tensor_symbol_info[forw_exec->outputs[i]].alias_ref ? tensor_symbol_info[forw_exec->outputs[i]].alias_ref - 1 : forw_exec->outputs[i]] & F_SYMBOL_USE) &&
988 input_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63)))
989 { /* Try to eliminate one of the input. */
990 input_bitmasks[i >> 6] &= ~((uint64_t)1 << (i & 63));
991 if (!sub_f_symbols)
992 sub_f_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
993 else
994 ccv_array_clear(sub_f_symbols);
995 for (j = 0; j < forw_exec->output_size; j++)
996 if (node->input_bitmasks[j >> 6] & ((uint64_t)1 << (j & 63)))
997 {
998 const int s_ref = *(int*)ccv_array_get(tensor_symbol_info[forw_exec->outputs[j]].s_ref, graph_ref)((void*)(((char*)((tensor_symbol_info[forw_exec->outputs[j
]].s_ref)->data)) + (size_t)(tensor_symbol_info[forw_exec->
outputs[j]].s_ref)->rsize * (size_t)(graph_ref)))
- 1;
999 assert(s_ref >= 0)((void) sizeof ((s_ref >= 0) ? 1 : 0), __extension__ ({ if
(s_ref >= 0) ; else __assert_fail ("s_ref >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 999, __extension__ __PRETTY_FUNCTION__); }))
;
1000 ccv_nnc_tensor_symbol_t sub_f_symbol = {
1001 .d = s_ref,
1002 .graph = sub_prep->graph,
1003 };
1004 ccv_array_push(sub_f_symbols, &sub_f_symbol);
1005 }
1006 if (_ccv_nnc_symbolic_graph_backward_prep_prune_ops(sub_prep, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_f_symbols, 0)((void*)(((char*)((sub_f_symbols)->data)) + (size_t)(sub_f_symbols
)->rsize * (size_t)(0)))
, sub_f_symbols->rnum, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, 0)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(0)))
, sub_wrt_symbols->rnum, ccv_nnc_symbolic_graph_sources(sub_prep->graph), ccv_nnc_symbolic_graph_source_size(sub_prep->graph), ccv_nnc_symbolic_graph_destinations(sub_prep->graph), ccv_nnc_symbolic_graph_destination_size(sub_prep->graph)))
1007 flag = 1;
1008 else /* Refit this with the bit back again. */
1009 input_bitmasks[i >> 6] |= ((uint64_t)1 << (i & 63));
1010 }
1011 } while (flag);
1012 // I am done, need to redo above for sub_prep, and it has to be successful now.
1013 if (!sub_f_symbols)
1014 sub_f_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1015 else
1016 ccv_array_clear(sub_f_symbols);
1017 for (i = 0; i < forw_exec->output_size; i++)
1018 if (input_bitmasks[i >> 6] & ((uint64_t)1 << (i & 63)))
1019 {
1020 const int s_ref = *(int*)ccv_array_get(tensor_symbol_info[forw_exec->outputs[i]].s_ref, graph_ref)((void*)(((char*)((tensor_symbol_info[forw_exec->outputs[i
]].s_ref)->data)) + (size_t)(tensor_symbol_info[forw_exec->
outputs[i]].s_ref)->rsize * (size_t)(graph_ref)))
- 1;
1021 assert(s_ref >= 0)((void) sizeof ((s_ref >= 0) ? 1 : 0), __extension__ ({ if
(s_ref >= 0) ; else __assert_fail ("s_ref >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 1021, __extension__ __PRETTY_FUNCTION__); }))
;
1022 ccv_nnc_tensor_symbol_t sub_f_symbol = {
1023 .d = s_ref,
1024 .graph = sub_prep->graph,
1025 };
1026 ccv_array_push(sub_f_symbols, &sub_f_symbol);
1027 }
1028 _ccv_nnc_symbolic_graph_backward_prep_prune_ops(sub_prep, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_f_symbols, 0)((void*)(((char*)((sub_f_symbols)->data)) + (size_t)(sub_f_symbols
)->rsize * (size_t)(0)))
, sub_f_symbols->rnum, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, 0)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(0)))
, sub_wrt_symbols->rnum, ccv_nnc_symbolic_graph_sources(sub_prep->graph), ccv_nnc_symbolic_graph_source_size(sub_prep->graph), ccv_nnc_symbolic_graph_destinations(sub_prep->graph), ccv_nnc_symbolic_graph_destination_size(sub_prep->graph));
1029 if (forw_exec->graph_ref_size > 1)
1030 for (i = 0; i < node->input_bitmask_size; i++)
1031 stack_input_bitmasks2[i] |= input_bitmasks[i];
1032 }
1033 if (forw_exec->graph_ref_size > 1)
1034 memcpy(node->input_bitmasks, stack_input_bitmasks2, sizeof(uint64_t) * node->input_bitmask_size);
1035 }
1036 } ccv_nnc_graph_visit_endfor} }
1037 if (sub_f_symbols)
1038 ccv_array_free(sub_f_symbols);
1039 if (sub_wrt_symbols)
1040 ccv_array_free(sub_wrt_symbols);
1041 int flag = 1;
1042 for (i = 0; i < f_symbol_size && flag; i++)
1043 flag = (used_grad[tensor_symbol_info[f_symbols[i].d].alias_ref ? tensor_symbol_info[f_symbols[i].d].alias_ref - 1 : f_symbols[i].d] & WRT_SYMBOL_USE);
1044 ccfreefree(used_grad);
1045 return flag;
1046}
1047
1048static void _ccv_nnc_symbolic_graph_backward_prep_gen(ccv_nnc_symbolic_graph_backward_prep_t* const backward_prep, const ccv_nnc_tensor_symbol_t* const f_symbols, const int f_symbol_size, const ccv_nnc_tensor_symbol_t* const wrt_symbols, const int wrt_symbol_size, const int is_while, const ccv_nnc_graph_exec_symbol_t* const sources, const int source_size, const ccv_nnc_graph_exec_symbol_t* const destinations, const int destination_size)
1049{
1050 const int exec_symbol_info_size = backward_prep->exec_symbol_info_size;
1051 const int tensor_symbol_info_size = backward_prep->tensor_symbol_info_size;
1052 const ccv_nnc_graph_exec_symbol_info_t* const exec_symbol_info = backward_prep->exec_symbol_info;
1053 const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info =backward_prep->tensor_symbol_info;
1054 const ccv_nnc_graph_visit_t* const forward_visit = backward_prep->forward_visit;
1055 // Now, for each one of these, find a reverse graph.
1056 ccv_nnc_graph_backward_info_t* const backward_info = backward_prep->backward_info;
1057 const ccv_nnc_graph_visit_t* const backward_visit = backward_prep->backward_visit;
1058 int i, j;
1059 // Now, only the flow from f_symbols back to wrt_symbols are interested to us.
1060 // Visit the graph in reverse order, build the AD nodes.
1061 ccv_nnc_autograd_graph_exec_symbol_t* const autograd_execs = (ccv_nnc_autograd_graph_exec_symbol_t*)cccalloccalloc(exec_symbol_info_size, sizeof(ccv_nnc_autograd_graph_exec_symbol_t));
1062 int max_forw_input_size = 0, max_forw_output_size = 0;
1063 for (i = 0; i < exec_symbol_info_size; i++)
1064 if ((backward_info[i].f_wrt & 0x3) == 0x3)
1065 {
1066 max_forw_input_size = ccv_max(max_forw_input_size, exec_symbol_info[i].input_size)({ typeof (max_forw_input_size) _a = (max_forw_input_size); typeof
(exec_symbol_info[i].input_size) _b = (exec_symbol_info[i].input_size
); (_a > _b) ? _a : _b; })
;
1067 max_forw_output_size = ccv_max(max_forw_output_size, exec_symbol_info[i].output_size)({ typeof (max_forw_output_size) _a = (max_forw_output_size);
typeof (exec_symbol_info[i].output_size) _b = (exec_symbol_info
[i].output_size); (_a > _b) ? _a : _b; })
;
1068 if (backward_info[i].outgoings)
1069 {
1070 // Copy over the outgoing bits.
1071 autograd_execs[i].outgoings = ccv_array_new(sizeof(int), backward_info[i].outgoings->rnum, 0);
1072 for (j = 0; j < backward_info[i].outgoings->rnum; j++)
1073 {
1074 const int d = *(int*)ccv_array_get(backward_info[i].outgoings, j)((void*)(((char*)((backward_info[i].outgoings)->data)) + (
size_t)(backward_info[i].outgoings)->rsize * (size_t)(j)))
;
1075 // Only push the outgoing node if it is in the f_wrt path.
1076 if ((backward_info[d].f_wrt & 0x3) == 0x3)
1077 ccv_array_push(autograd_execs[i].outgoings, &d);
1078 }
1079 }
1080 }
1081 int max_forw_inputs[ccv_max(1, max_forw_input_size)({ typeof (1) _a = (1); typeof (max_forw_input_size) _b = (max_forw_input_size
); (_a > _b) ? _a : _b; })
];
1082 int max_forw_outputs[ccv_max(1, max_forw_output_size)({ typeof (1) _a = (1); typeof (max_forw_output_size) _b = (max_forw_output_size
); (_a > _b) ? _a : _b; })
];
1083 ccv_nnc_autograd_tensor_version_t* const autograd_tensor_versions = (ccv_nnc_autograd_tensor_version_t*)cccalloccalloc(tensor_symbol_info_size, sizeof(ccv_nnc_autograd_tensor_version_t));
1084 ccv_array_t* autograd_tensor_symbols = ccv_array_new(sizeof(ccv_nnc_autograd_tensor_symbol_t), tensor_symbol_info_size, 0);
1085 ccv_array_t* sum_or_set_execs = ccv_array_new(sizeof(ccv_nnc_sum_or_set_graph_exec_symbol_t), 0, 0);
1086 ccv_nnc_graph_visit_for(backward_visit, backward_info, back_info_node, idx){ int _i_; for (_i_ = 0; _i_ < (backward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (backward_visit
)->node[_i_].index; const int _node_unused_ __attribute__(
(unused)) = (backward_visit)->node[_i_].term; typeof ((backward_info
)) const back_info_node __attribute__((unused)) = (backward_info
) + idx;
{
1087 /* This is required by both f flow and wrt flow, therefore, an interest to us */
1088 if ((back_info_node->f_wrt & 0x3) == 0x3)
1089 {
1090 const ccv_nnc_graph_exec_symbol_info_t* forw_exec = exec_symbol_info + idx;
1091 ccv_nnc_autograd_graph_exec_symbol_t* back_exec = autograd_execs + idx;
1092 back_exec->cmd = forw_exec->cmd;
1093 if (back_exec->cmd.cmd != CCV_NNC_NOOP)
1094 back_exec->cmd.cmd += 1; /* Backward command is the one after forward command. */
1095 assert(ccv_nnc_cmd_is_backward(back_exec->cmd) || back_exec->cmd.cmd == CCV_NNC_NOOP)((void) sizeof ((ccv_nnc_cmd_is_backward(back_exec->cmd) ||
back_exec->cmd.cmd == CCV_NNC_NOOP) ? 1 : 0), __extension__
({ if (ccv_nnc_cmd_is_backward(back_exec->cmd) || back_exec
->cmd.cmd == CCV_NNC_NOOP) ; else __assert_fail ("ccv_nnc_cmd_is_backward(back_exec->cmd) || back_exec->cmd.cmd == CCV_NNC_NOOP"
, "ccv_nnc_symbolic_graph_backward.c", 1095, __extension__ __PRETTY_FUNCTION__
); }))
;
1096 if (!back_info_node->output_bitmask_size) /* This has no output, can be a noop. */
1097 back_exec->cmd.cmd = CCV_NNC_NOOP;
1098 else {
1099 int* back_input_map = max_forw_outputs;
1100 int* back_output_map = max_forw_inputs;
1101 _ccv_nnc_symbolic_graph_backward_exec_io(forw_exec, &back_input_map, &back_output_map, &back_exec->input_size, &back_exec->output_size);
1102 back_exec->inputs = ccmallocmalloc(sizeof(int) * (back_exec->input_size + back_exec->output_size));
1103 back_exec->outputs = back_exec->inputs + back_exec->input_size;
1104 /* Need to compute input before we compute output */
1105 for (i = 0; i < back_exec->input_size; i++)
1106 {
1107 /* If we can skip this input, do that. */
1108 if (!(back_info_node->input_bitmasks[i >> 6] & ((uint64_t)1 << i)))
1109 continue;
1110 const int d = back_input_map[i];
1111 const int alias_ref = tensor_symbol_info[d].alias_ref;
1112 ccv_nnc_autograd_tensor_version_t* tensor_ver = alias_ref ? autograd_tensor_versions + (alias_ref - 1) : autograd_tensor_versions + d;
1113 /* Initialization tensor, should corresponding to f symbols */
1114 if (!tensor_ver->ref_version)
1115 {
1116 ccv_nnc_autograd_tensor_symbol_t tensor_sym = {};
1117 if (!alias_ref)
1118 {
1119 tensor_sym.d = d;
1120 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1121 const ccv_nnc_tensor_ref_t tensor_ref = {
1122 .d = autograd_tensor_symbols->rnum - 1,
1123 .x = idx,
1124 .alias_registry = 0
1125 };
1126 tensor_ver->ref_version = ccv_array_new(sizeof(ccv_nnc_tensor_ref_t), 1, 0);
1127 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
1128 } else {
1129 tensor_sym.d = alias_ref - 1;
1130 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1131 const ccv_nnc_tensor_ref_t tensor_ref = {
1132 .d = autograd_tensor_symbols->rnum - 1,
1133 .x = idx,
1134 .alias_registry = ccv_array_new(sizeof(int), 1, 0)
1135 };
1136 tensor_ver->ref_version = ccv_array_new(sizeof(ccv_nnc_tensor_ref_t), 1, 0);
1137 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
1138 tensor_sym.d = d; /* set back */
1139 tensor_sym.alias_ref = tensor_ref.d + 1;
1140 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1141 const int ad = autograd_tensor_symbols->rnum - 1;
1142 ccv_array_push(tensor_ref.alias_registry, &ad);
1143 }
1144 }
1145 /* The simplest case (most common), it is not an alias. */
1146 if (!alias_ref)
1147 {
1148 /* Even simpler, this only have one reference tensor, thus, pass this as input. */
1149 if (tensor_ver->c == tensor_ver->ref_version->rnum - 1)
1150 {
1151 ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, tensor_ver->c)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(tensor_ver
->c)))
;
1152 /* There are alias associated with this tensor ref, zero it out when this tensor is allocated. */
1153 /* This is is required. Consider the case that we have an alias of this tensor used somehwere */
1154 /* on forward pass, when we compute backward, we have that alias computed first, however, its */
1155 /* underlying tensor is not zero initialized, and we will end up with garbage values here. */
1156 if (tensor_ref->alias_registry &&
1157 /* Loop over to see if this tensor is fully occupied to avoid extra zero step. */
1158 !_ccv_nnc_tensor_ref_fully_assigned_with_aliases(tensor_ref, autograd_tensor_symbols, tensor_symbol_info))
1159 {
1160 ccv_nnc_autograd_tensor_symbol_t* tensor_sym = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
1161 assert(tensor_sym->alias_ref == 0)((void) sizeof ((tensor_sym->alias_ref == 0) ? 1 : 0), __extension__
({ if (tensor_sym->alias_ref == 0) ; else __assert_fail (
"tensor_sym->alias_ref == 0", "ccv_nnc_symbolic_graph_backward.c"
, 1161, __extension__ __PRETTY_FUNCTION__); }))
;
1162 tensor_sym->flags = CCV_NNC_TENSOR_SYMBOL_INIT_ZEROS;
1163 }
1164 back_exec->inputs[i] = tensor_ref->d;
1165 } else {
1166 /* Otherwise, we need to sum them up, and then pass the summed result to the computation. */
1167 _ccv_nnc_graph_sum_autograd_tensor_versions(idx, d, exec_symbol_info_size, tensor_symbol_info, tensor_ver, autograd_execs, autograd_tensor_symbols, sum_or_set_execs);
1168 ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, tensor_ver->c)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(tensor_ver
->c)))
;
1169 back_exec->inputs[i] = tensor_ref->d;
1170 }
1171 } else
1172 /* If this is an alias, go through all available tensor ref versions */
1173 back_exec->inputs[i] = _ccv_nnc_graph_sum_autograd_tensor_versions_alias(idx, d, tensor_symbol_info, exec_symbol_info_size, tensor_symbol_info + d, tensor_ver, autograd_execs, autograd_tensor_symbols, sum_or_set_execs);
1174 }
1175 for (i = 0; i < back_exec->output_size; i++)
1176 {
1177 /* If we can skip this output, do that. */
1178 if (!(back_info_node->output_bitmasks[i >> 6] & ((uint64_t)1 << i)))
1179 continue;
1180 const int d = back_output_map[i];
1181 const int alias_ref = tensor_symbol_info[d].alias_ref;
1182 ccv_nnc_autograd_tensor_symbol_t tensor_sym = {
1183 .d = d
1184 };
1185 /* The simplest case (most common), it is not an alias. */
1186 if (!alias_ref)
1187 {
1188 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1189 const ccv_nnc_tensor_ref_t tensor_ref = {
1190 .d = autograd_tensor_symbols->rnum - 1,
1191 .x = idx,
1192 .exec_registry = 0,
1193 .alias_registry = 0
1194 };
1195 ccv_nnc_autograd_tensor_version_t* tensor_ver = autograd_tensor_versions + d;
1196 if (!tensor_ver->ref_version)
1197 tensor_ver->ref_version = ccv_array_new(sizeof(ccv_nnc_tensor_ref_t), 1, 0);
1198 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
1199 back_exec->outputs[i] = tensor_ref.d;
1200 } else {
1201 /* Otherwise, in case that this is an alias, we try to find the existing one (in tensor_ver
1202 * see if can meet the need (thus, for the tensor info / ofs, it fits). */
1203 ccv_nnc_autograd_tensor_version_t* tensor_ver = autograd_tensor_versions + (alias_ref - 1);
1204 if (!tensor_ver->ref_version)
1205 tensor_ver->ref_version = ccv_array_new(sizeof(ccv_nnc_tensor_ref_t), 1, 0);
1206 /* If already exists a ref version, check if any of these not-sealed tensors have free space. */
1207 int found = 0;
1208 for (j = tensor_ver->c; !found && j < tensor_ver->ref_version->rnum; j++)
1209 {
1210 ccv_nnc_tensor_ref_t* tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, j)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(j)))
;
1211 if (!_ccv_nnc_tensor_ref_version_involve_alias(tensor_ref, autograd_tensor_symbols, tensor_symbol_info, tensor_symbol_info + d))
1212 {
1213 tensor_sym.alias_ref = tensor_ref->d + 1;
1214 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1215 const int ad = autograd_tensor_symbols->rnum - 1;
1216 ccv_array_push(tensor_ref->alias_registry, &ad);
1217 if (!tensor_ref->exec_registry)
1218 tensor_ref->exec_registry = ccv_array_new(sizeof(int), 1, 0);
1219 ccv_array_push(tensor_ref->exec_registry, &idx);
1220 back_exec->outputs[i] = ad;
1221 found = 1;
1222 }
1223 }
1224 if (!found) /* Cannot find an tensor ref to insert, create one first */
1225 {
1226 tensor_sym.d = alias_ref - 1; /* Reference back to the non-alias. */
1227 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1228 const ccv_nnc_tensor_ref_t tensor_ref = {
1229 .d = autograd_tensor_symbols->rnum - 1,
1230 .x = idx,
1231 .exec_registry = 0,
1232 .alias_registry = ccv_array_new(sizeof(int), 1, 0)
1233 };
1234 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
1235 tensor_sym.d = d; /* set back */
1236 tensor_sym.alias_ref = tensor_ref.d + 1;
1237 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1238 const int ad = autograd_tensor_symbols->rnum - 1;
1239 ccv_array_push(tensor_ref.alias_registry, &ad);
1240 back_exec->outputs[i] = ad;
1241 }
1242 }
1243 }
1244 }
1245 }
1246 } ccv_nnc_graph_visit_endfor} }
1247 // Find all relevant wrt symbols, generate sum for them if needed.
1248 for (i = 0; i < wrt_symbol_size; i++)
1249 {
1250 const int d = wrt_symbols[i].d;
1251 if (d < 0)
1252 continue;
1253 const int ref_d = (!tensor_symbol_info[d].alias_ref) ? d : tensor_symbol_info[d].alias_ref - 1;
1254 ccv_nnc_autograd_tensor_version_t* tensor_ver = autograd_tensor_versions + ref_d;
1255 if (!tensor_ver->ref_version)
1256 {
1257 // This wrt symbol is not available at all, for this case, we set its flag to init zero.
1258 const ccv_nnc_autograd_tensor_symbol_t tensor_sym = {
1259 .d = ref_d
1260 };
1261 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1262 ccv_nnc_sum_or_set_graph_exec_symbol_t set_exec = {
1263 .value = 0,
1264 .output = autograd_tensor_symbols->rnum - 1,
1265 };
1266 ccv_array_push(sum_or_set_execs, &set_exec);
1267 // Insert the one to be set to zero.
1268 const ccv_nnc_tensor_ref_t tensor_ref = {
1269 .d = autograd_tensor_symbols->rnum - 1,
1270 .x = exec_symbol_info_size + sum_or_set_execs->rnum - 1,
1271 };
1272 tensor_ver->ref_version = ccv_array_new(sizeof(ccv_nnc_tensor_ref_t), 1, 0);
1273 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
1274 continue;
1275 }
1276 // If it is a while loop, we need to insert an accumulator to the graph (this is expressed as a initialization tensor summed with existing results).
1277 // First, insert the initialization tensor if this wrt results is not used directly in next while loop (thus, it participates the computation, therefore, no need to accumulate).
1278 if (is_while && !tensor_symbol_info[ref_d].assign_ref &&
1279 _ccv_nnc_tensor_ref_version_find_init(tensor_ver) < 0) // If the initialization tensor is not inserted yet.
1280 {
1281 const ccv_nnc_autograd_tensor_symbol_t tensor_sym = {
1282 .d = ref_d
1283 };
1284 ccv_array_push(autograd_tensor_symbols, &tensor_sym);
1285 // Insert the one to be summed.
1286 const ccv_nnc_tensor_ref_t tensor_ref = {
1287 .d = autograd_tensor_symbols->rnum - 1,
1288 .x = -1, // This denotes it is an initialization vector.
1289 };
1290 ccv_array_push(tensor_ver->ref_version, &tensor_ref);
1291 }
1292 // If there are more than one tensor in the list, it is possible to sum them up.
1293 if (tensor_ver->c < tensor_ver->ref_version->rnum - 1)
1294 _ccv_nnc_graph_sum_autograd_tensor_versions(-1, ref_d, exec_symbol_info_size, tensor_symbol_info, tensor_ver, autograd_execs, autograd_tensor_symbols, sum_or_set_execs);
1295 // The tensor version should have ref_version, and only one now (after sum up).
1296 assert(tensor_ver->c == tensor_ver->ref_version->rnum - 1)((void) sizeof ((tensor_ver->c == tensor_ver->ref_version
->rnum - 1) ? 1 : 0), __extension__ ({ if (tensor_ver->
c == tensor_ver->ref_version->rnum - 1) ; else __assert_fail
("tensor_ver->c == tensor_ver->ref_version->rnum - 1"
, "ccv_nnc_symbolic_graph_backward.c", 1296, __extension__ __PRETTY_FUNCTION__
); }))
;
1297 }
1298 // Adding additional fields to backward_prep now.
1299 backward_prep->autograd_execs = autograd_execs;
1300 backward_prep->autograd_tensor_versions = autograd_tensor_versions;
1301 backward_prep->autograd_tensor_symbols = autograd_tensor_symbols;
1302 backward_prep->sum_or_set_execs = sum_or_set_execs;
1303 ccv_array_t* sub_f_symbols = 0;
1304 ccv_array_t* sub_wrt_symbols = 0;
1305 ccv_nnc_graph_visit_for(forward_visit, exec_symbol_info, _, idx){ int _i_; for (_i_ = 0; _i_ < (forward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (forward_visit)
->node[_i_].index; const int _node_unused_ __attribute__((
unused)) = (forward_visit)->node[_i_].term; typeof ((exec_symbol_info
)) const _ __attribute__((unused)) = (exec_symbol_info) + idx
;
{
1306 ccv_nnc_graph_backward_info_t* node = backward_info + idx;
1307 const ccv_nnc_graph_exec_symbol_info_t* forw_exec = exec_symbol_info + idx;
1308 /* Only interested in the ones on the f / wrt flow */
1309 if ((node->f_wrt & 0x3) == 0x3)
1310 {
1311 const int is_while = (forw_exec->flags & CCV_NNC_GRAPH_EXEC_P_WHILE);
1312 for (i = 0; i < forw_exec->graph_ref_size; i++)
1313 {
1314 // Now calling it recursively until we are sure no f_symbols can be removed.
1315 const int graph_ref = CCV_NNC_GRAPH_REF(forw_exec)((forw_exec)->_heap_graph_ref ? (forw_exec)->_heap_graph_ref
: (forw_exec)->_inline_graph_ref)
[i] - 1;
1316 ccv_nnc_symbolic_graph_backward_prep_t* const sub_prep = backward_prep->sub_preps + graph_ref;
1317 if (!sub_wrt_symbols)
1318 sub_wrt_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1319 if (!sub_f_symbols)
1320 sub_f_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1321 _ccv_nnc_symbolic_graph_backward_prep_sub_f_wrt_symbols(forw_exec, sub_prep->graph, graph_ref, tensor_symbol_info, node->input_bitmasks, node->output_bitmasks, sub_f_symbols, sub_wrt_symbols);
1322 _ccv_nnc_symbolic_graph_backward_prep_gen(sub_prep, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_f_symbols, 0)((void*)(((char*)((sub_f_symbols)->data)) + (size_t)(sub_f_symbols
)->rsize * (size_t)(0)))
, sub_f_symbols->rnum, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, 0)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(0)))
, sub_wrt_symbols->rnum, is_while, ccv_nnc_symbolic_graph_sources(sub_prep->graph), ccv_nnc_symbolic_graph_source_size(sub_prep->graph), ccv_nnc_symbolic_graph_destinations(sub_prep->graph), ccv_nnc_symbolic_graph_destination_size(sub_prep->graph));
1323 }
1324 }
1325 } ccv_nnc_graph_visit_endfor} }
1326 if (sub_f_symbols)
1327 ccv_array_free(sub_f_symbols);
1328 if (sub_wrt_symbols)
1329 ccv_array_free(sub_wrt_symbols);
1330}
1331
1332static void _ccv_nnc_symbolic_graph_backward_prep_free(const ccv_nnc_symbolic_graph_backward_prep_t backward_prep)
1333{
1334 int i, j;
1335 const int exec_symbol_info_size = backward_prep.exec_symbol_info_size;
1336 const int tensor_symbol_info_size = backward_prep.tensor_symbol_info_size;
1337 ccv_nnc_autograd_graph_exec_symbol_t* const autograd_execs = backward_prep.autograd_execs;
1338 if (autograd_execs)
1339 {
1340 for (i = 0; i < exec_symbol_info_size; i++)
1341 {
1342 if (autograd_execs[i].inputs)
1343 ccfreefree(autograd_execs[i].inputs);
1344 if (autograd_execs[i].outgoings)
1345 ccv_array_free(autograd_execs[i].outgoings);
1346 }
1347 ccfreefree(autograd_execs);
1348 }
1349 ccv_nnc_autograd_tensor_version_t* const autograd_tensor_versions = backward_prep.autograd_tensor_versions;
1350 if (autograd_tensor_versions)
1351 {
1352 for (i = 0; i < tensor_symbol_info_size; i++)
1353 {
1354 if (autograd_tensor_versions[i].ref_version)
1355 {
1356 for (j = 0; j < autograd_tensor_versions[i].ref_version->rnum; j++)
1357 {
1358 ccv_nnc_tensor_ref_t* ref_version = (ccv_nnc_tensor_ref_t*)ccv_array_get(autograd_tensor_versions[i].ref_version, j)((void*)(((char*)((autograd_tensor_versions[i].ref_version)->
data)) + (size_t)(autograd_tensor_versions[i].ref_version)->
rsize * (size_t)(j)))
;
1359 if (ref_version->exec_registry)
1360 ccv_array_free(ref_version->exec_registry);
1361 if (ref_version->alias_registry)
1362 ccv_array_free(ref_version->alias_registry);
1363 }
1364 ccv_array_free(autograd_tensor_versions[i].ref_version);
1365 }
1366 }
1367 ccfreefree(autograd_tensor_versions);
1368 }
1369 if (backward_prep.autograd_tensor_symbols)
1370 ccv_array_free(backward_prep.autograd_tensor_symbols);
1371 ccv_array_t* const sum_or_set_execs = backward_prep.sum_or_set_execs;
1372 if (sum_or_set_execs)
1373 {
1374 for (i = 0; i < sum_or_set_execs->rnum; i++)
1375 {
1376 ccv_nnc_sum_or_set_graph_exec_symbol_t* sum_or_set = (ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, i)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(i)))
;
1377 if (sum_or_set->inputs)
1378 ccfreefree(sum_or_set->inputs);
1379 if (sum_or_set->outgoings)
1380 ccv_array_free(sum_or_set->outgoings);
1381 }
1382 ccv_array_free(sum_or_set_execs);
1383 }
1384 // Now afterwards, these are mandatory.
1385 ccv_nnc_graph_backward_info_t* const backward_info = backward_prep.backward_info;
1386 for (i = 0; i < exec_symbol_info_size; i++)
1387 {
1388 if (backward_info[i].outgoings)
1389 ccv_array_free(backward_info[i].outgoings);
1390 if (backward_info[i].input_bitmasks)
1391 ccfreefree(backward_info[i].input_bitmasks);
1392 }
1393 ccfreefree(backward_info);
1394 ccv_nnc_graph_visit_free(backward_prep.backward_visit);
1395 ccv_nnc_graph_visit_free(backward_prep.forward_visit);
1396 ccfreefree(backward_prep.exec_symbol_info);
1397 ccfreefree(backward_prep.tensor_symbol_info);
1398 for (i = 0; i < backward_prep.sub_prep_size; i++)
1399 _ccv_nnc_symbolic_graph_backward_prep_free(backward_prep.sub_preps[i]);
1400 if (backward_prep.sub_preps)
1401 ccfreefree(backward_prep.sub_preps);
1402}
1403
1404static void _ccv_nnc_add_backward_breakpoint_for_symbol(const ccv_nnc_symbolic_graph_backward_prep_t* const backward_prep, const ccv_nnc_graph_exec_symbol_t breakpoint, ccv_nnc_symbolic_graph_t* const graph, ccv_array_t* const sub_breakpoints)
1405{
1406 const ccv_nnc_graph_exec_symbol_t noop = ccv_nnc_graph_exec_symbol_new(graph, ccv_nnc_cmd(CCV_NNC_NOOP, 0, CMD_GENERIC()((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}}}), 0), 0, 0, 0, 0, 0);
1407 ccv_array_push(sub_breakpoints, &noop);
1408 // Now need to hook this up to the graph.
1409 const ccv_nnc_graph_exec_symbol_info_t* const exec_symbol_info = backward_prep->exec_symbol_info;
1410 const ccv_nnc_graph_visit_t* const forward_visit = backward_prep->forward_visit;
1411 // Now, for each one of these, find a reverse graph.
1412 ccv_nnc_graph_backward_info_t* const backward_info = backward_prep->backward_info;
1413 int i;
1414 // Clean up the high bit.
1415 for (i = 0; i < backward_prep->exec_symbol_info_size; i++)
1416 backward_info[i].f_wrt &= ~0x4;
1417 assert((backward_info[breakpoint.d].f_wrt & 0x3) != 0x3)((void) sizeof (((backward_info[breakpoint.d].f_wrt & 0x3
) != 0x3) ? 1 : 0), __extension__ ({ if ((backward_info[breakpoint
.d].f_wrt & 0x3) != 0x3) ; else __assert_fail ("(backward_info[breakpoint.d].f_wrt & 0x3) != 0x3"
, "ccv_nnc_symbolic_graph_backward.c", 1417, __extension__ __PRETTY_FUNCTION__
); }))
;
1418 backward_info[breakpoint.d].f_wrt |= 0x4;
1419 const ccv_nnc_graph_visit_t* const backward_visit = backward_prep->backward_visit;
1420 const ccv_nnc_autograd_graph_exec_symbol_t* const autograd_execs = backward_prep->autograd_execs;
1421 // Going forward to find whether this breakpoint is a source node to some f_wrt nodes.
1422 ccv_nnc_graph_visit_for(forward_visit, exec_symbol_info, forw_exec, idx){ int _i_; for (_i_ = 0; _i_ < (forward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (forward_visit)
->node[_i_].index; const int _node_unused_ __attribute__((
unused)) = (forward_visit)->node[_i_].term; typeof ((exec_symbol_info
)) const forw_exec __attribute__((unused)) = (exec_symbol_info
) + idx;
{
1423 ccv_nnc_graph_backward_info_t* const node = backward_info + idx;
1424 // If it is tagged on breakpoint flow, but not as both f or wrt, flow through it.
1425 if ((node->f_wrt & 0x4) && (node->f_wrt & 0x3) != 0x3)
1426 for (i = 0; forw_exec->outgoings && i < forw_exec->outgoings->rnum; i++)
1427 {
1428 const int outgoing_idx = *(int*)ccv_array_get(forw_exec->outgoings, i)((void*)(((char*)((forw_exec->outgoings)->data)) + (size_t
)(forw_exec->outgoings)->rsize * (size_t)(i)))
;
1429 ccv_nnc_graph_backward_info_t* const outgoing_node = backward_info + outgoing_idx;
1430 // If this is a f_wrt node. Concatenate.
1431 if (!(outgoing_node->f_wrt & 0x4) && (outgoing_node->f_wrt & 0x3) == 0x3)
1432 ccv_nnc_graph_exec_symbol_concat(graph, autograd_execs[outgoing_idx].symbol, noop);
1433 outgoing_node->f_wrt |= 0x4;
1434 }
1435 } ccv_nnc_graph_visit_endfor} }
1436 // Going backward to find whether this breakpoint is a destination node for some f_wrt_nodes.
1437 ccv_nnc_graph_visit_for(backward_visit, backward_info, node, idx){ int _i_; for (_i_ = 0; _i_ < (backward_visit)->size; _i_
++) { const int idx __attribute__((unused)) = (backward_visit
)->node[_i_].index; const int _node_unused_ __attribute__(
(unused)) = (backward_visit)->node[_i_].term; typeof ((backward_info
)) const node __attribute__((unused)) = (backward_info) + idx
;
{
1438 if ((node->f_wrt & 0x4) && (node->f_wrt & 0x3) != 0x3)
1439 for (i = 0; node->outgoings && i < node->outgoings->rnum; i++)
1440 {
1441 const int outgoing_idx = *(int*)ccv_array_get(node->outgoings, i)((void*)(((char*)((node->outgoings)->data)) + (size_t)(
node->outgoings)->rsize * (size_t)(i)))
;
1442 ccv_nnc_graph_backward_info_t* const outgoing_node = backward_info + outgoing_idx;
1443 // If this is a f_wrt node. Concatenate.
1444 if (!(outgoing_node->f_wrt & 0x4) && (outgoing_node->f_wrt & 0x3) == 0x3)
1445 ccv_nnc_graph_exec_symbol_concat(graph, noop, autograd_execs[outgoing_idx].symbol);
1446 outgoing_node->f_wrt |= 0x4;
1447 }
1448 } ccv_nnc_graph_visit_endfor} }
1449}
1450
1451static ccv_nnc_autograd_tensor_symbol_t* _ccv_nnc_autograd_tensor_symbol_from_tensor_version(ccv_array_t* const autograd_tensor_symbols, const ccv_nnc_autograd_tensor_version_t* const tensor_ver)
1452{
1453 assert(tensor_ver->ref_version)((void) sizeof ((tensor_ver->ref_version) ? 1 : 0), __extension__
({ if (tensor_ver->ref_version) ; else __assert_fail ("tensor_ver->ref_version"
, "ccv_nnc_symbolic_graph_backward.c", 1453, __extension__ __PRETTY_FUNCTION__
); }))
;
1454 const ccv_nnc_tensor_ref_t* const tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, tensor_ver->c)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(tensor_ver
->c)))
;
1455 return (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
1456}
1457
1458static void _ccv_nnc_symbolic_graph_set_backward_carry_overs(const ccv_nnc_symbolic_graph_backward_prep_t* const backward_prep, const ccv_nnc_tensor_symbol_t* const wrt_symbols, const int wrt_symbol_size, ccv_nnc_symbolic_graph_t* const graph)
1459{
1460 int i;
1461 for (i = 0; i < backward_prep->graph->tensor_symbol_info->rnum; i++)
1462 {
1463 const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info = backward_prep->tensor_symbol_info + i;
1464 if (tensor_symbol_info->assign_ref)
1465 {
1466 const int assign_ref = tensor_symbol_info->assign_ref - 1;
1467 ccv_nnc_autograd_tensor_symbol_t* const destination_autograd_symbol = _ccv_nnc_autograd_tensor_symbol_from_tensor_version(backward_prep->autograd_tensor_symbols, backward_prep->autograd_tensor_versions + assign_ref);
1468 ccv_nnc_autograd_tensor_symbol_t* const source_autograd_symbol = _ccv_nnc_autograd_tensor_symbol_from_tensor_version(backward_prep->autograd_tensor_symbols, backward_prep->autograd_tensor_versions + i);
1469 ccv_nnc_symbolic_graph_set_carry_overs(graph, (ccv_nnc_tensor_symbol_map_t []){
1470 { .source = source_autograd_symbol->symbol, .destination = destination_autograd_symbol->symbol }
1471 }, 1);
1472 }
1473 }
1474 for (i = 0; i < wrt_symbol_size; i++)
1475 {
1476 const int d = wrt_symbols[i].d;
1477 if (d < 0)
1478 continue;
1479 const int ref_d = (!backward_prep->tensor_symbol_info[d].alias_ref) ? d : backward_prep->tensor_symbol_info[d].alias_ref - 1;
1480 const ccv_nnc_autograd_tensor_version_t* const tensor_ver = backward_prep->autograd_tensor_versions + ref_d;
1481 const int init_ref_ver = _ccv_nnc_tensor_ref_version_find_init(tensor_ver);
1482 if (init_ref_ver >= 0)
1483 {
1484 const int init_d = ((ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, init_ref_ver)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(init_ref_ver
)))
)->d;
1485 ccv_nnc_autograd_tensor_symbol_t* const destination_autograd_symbol = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(backward_prep->autograd_tensor_symbols, init_d)((void*)(((char*)((backward_prep->autograd_tensor_symbols)
->data)) + (size_t)(backward_prep->autograd_tensor_symbols
)->rsize * (size_t)(init_d)))
;
1486 ccv_nnc_autograd_tensor_symbol_t* const source_autograd_symbol = _ccv_nnc_autograd_tensor_symbol_from_tensor_version(backward_prep->autograd_tensor_symbols, backward_prep->autograd_tensor_versions + ref_d);
1487 ccv_nnc_symbolic_graph_set_carry_overs(graph, (ccv_nnc_tensor_symbol_map_t []){
1488 { .source = source_autograd_symbol->symbol, .destination = destination_autograd_symbol->symbol }
1489 }, 1);
1490 }
1491 }
1492}
1493
1494static void _ccv_nnc_symbolic_graph_add_init_zeros(const ccv_nnc_symbolic_graph_backward_prep_t* const sub_prep, const ccv_nnc_tensor_symbol_t* const wrt_symbols, const int wrt_symbol_size, ccv_nnc_symbolic_graph_t* const graph, ccv_nnc_symbolic_graph_t* const sub_graph, ccv_array_t* const symbols)
1495{
1496 int i;
1497 for (i = 0; i < wrt_symbol_size; i++)
1498 {
1499 const int d = wrt_symbols[i].d;
1500 if (d < 0)
1501 continue;
1502 const int ref_d = (!sub_prep->tensor_symbol_info[d].alias_ref) ? d : sub_prep->tensor_symbol_info[d].alias_ref - 1;
1503 const ccv_nnc_autograd_tensor_version_t* const tensor_ver = sub_prep->autograd_tensor_versions + ref_d;
1504 const int init_ref_ver = _ccv_nnc_tensor_ref_version_find_init(tensor_ver);
1505 if (init_ref_ver >= 0)
1506 {
1507 // Need de-dup logic.
1508 const int init_d = ((ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, init_ref_ver)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(init_ref_ver
)))
)->d;
1509 ccv_nnc_autograd_tensor_symbol_t* const init_autograd_symbol = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(sub_prep->autograd_tensor_symbols, init_d)((void*)(((char*)((sub_prep->autograd_tensor_symbols)->
data)) + (size_t)(sub_prep->autograd_tensor_symbols)->rsize
* (size_t)(init_d)))
;
1510 const ccv_nnc_tensor_symbol_info_t* const sub_init_symbol_info = (ccv_nnc_tensor_symbol_info_t*)ccv_array_get(sub_graph->tensor_symbol_info, init_autograd_symbol->symbol.d)((void*)(((char*)((sub_graph->tensor_symbol_info)->data
)) + (size_t)(sub_graph->tensor_symbol_info)->rsize * (
size_t)(init_autograd_symbol->symbol.d)))
;
1511 // If it doesn't have a parent ref yet, create one.
1512 if (!sub_init_symbol_info->p_ref)
1513 {
1514 ccv_nnc_tensor_symbol_t new_symbol = ccv_nnc_tensor_symbol_new(graph, sub_prep->tensor_symbol_info[ref_d].info, 0);
1515 ccv_nnc_tensor_symbol_set_flags(graph, new_symbol, CCV_NNC_TENSOR_SYMBOL_INIT_ZEROS);
1516 ccv_array_push(symbols, &new_symbol);
1517 ccv_nnc_tensor_symbol_hookup(graph, sub_graph, new_symbol, init_autograd_symbol->symbol);
1518 }
1519 }
1520 }
1521}
1522
1523static void _ccv_nnc_symbolic_graph_add_tape_vars(const ccv_nnc_symbolic_graph_backward_prep_t* const sub_prep, ccv_nnc_symbolic_graph_t* const root, ccv_nnc_symbolic_graph_t* const graph, ccv_nnc_symbolic_graph_t* const sub_graph, ccv_array_t* const symbols)
1524{
1525 int i;
1526 for (i = 0; i < sub_graph->tensor_symbol_info->rnum; i++)
1527 {
1528 const ccv_nnc_tensor_symbol_info_t* const symbol_info = (ccv_nnc_tensor_symbol_info_t*)ccv_array_get(sub_graph->tensor_symbol_info, i)((void*)(((char*)((sub_graph->tensor_symbol_info)->data
)) + (size_t)(sub_graph->tensor_symbol_info)->rsize * (
size_t)(i)))
;
1529 if ((symbol_info->flags & CCV_NNC_TENSOR_SYMBOL_TAPE_VAR) && symbol_info->peer_ref)
1530 {
1531 const int peer_ref = symbol_info->peer_ref - 1;
1532 const ccv_nnc_tensor_symbol_t root_symbol = ccv_nnc_tensor_symbol_resolve(root, (ccv_nnc_tensor_symbol_t){
1533 .d = peer_ref,
1534 .graph = sub_prep->graph,
1535 });
1536 if (root_symbol.d >= 0)
1537 {
1538 ccv_nnc_tensor_symbol_hookup(root, sub_graph, root_symbol, (ccv_nnc_tensor_symbol_t){
1539 .d = i,
1540 .graph = sub_graph,
1541 });
1542 if (symbols)
1543 {
1544 const ccv_nnc_tensor_symbol_t p_symbol = ccv_nnc_tensor_symbol_resolve(graph, (ccv_nnc_tensor_symbol_t){
1545 .d = i,
1546 .graph = sub_graph,
1547 });
1548 ccv_array_push(symbols, &p_symbol);
1549 }
1550 }
1551 }
1552 }
1553}
1554
1555static void _ccv_nnc_symbolic_graph_backward_gen(const ccv_nnc_symbolic_graph_backward_prep_t* const backward_prep, const ccv_nnc_tensor_symbol_t* const f_symbols, const int f_symbol_size, const ccv_nnc_tensor_symbol_t* const wrt_symbols, const int wrt_symbol_size, ccv_nnc_symbolic_graph_t* const graph, ccv_nnc_symbolic_graph_t* const root)
1556{
1557 assert(graph == backward_prep->graph || graph->peer == backward_prep->graph)((void) sizeof ((graph == backward_prep->graph || graph->
peer == backward_prep->graph) ? 1 : 0), __extension__ ({ if
(graph == backward_prep->graph || graph->peer == backward_prep
->graph) ; else __assert_fail ("graph == backward_prep->graph || graph->peer == backward_prep->graph"
, "ccv_nnc_symbolic_graph_backward.c", 1557, __extension__ __PRETTY_FUNCTION__
); }))
;
1
Assuming the condition is false
2
Assuming the condition is true
3
Taking true branch
1558 const int exec_symbol_info_size = backward_prep->exec_symbol_info_size;
1559 const int tensor_symbol_info_size = backward_prep->tensor_symbol_info_size;
1560 const ccv_nnc_graph_exec_symbol_info_t* const exec_symbol_info = backward_prep->exec_symbol_info;
1561 const ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info = backward_prep->tensor_symbol_info;
1562 int i, j, k, p;
1563 ccv_array_t* const autograd_tensor_symbols = backward_prep->autograd_tensor_symbols;
1564 // Generate required symbols based on the information gathered above.
1565 for (i = 0; i < autograd_tensor_symbols->rnum; i++)
4
Assuming the condition is false
5
Loop condition is false. Execution continues on line 1584
1566 {
1567 ccv_nnc_autograd_tensor_symbol_t* symbol = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, i)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(i)))
;
1568 assert(symbol->d >= 0)((void) sizeof ((symbol->d >= 0) ? 1 : 0), __extension__
({ if (symbol->d >= 0) ; else __assert_fail ("symbol->d >= 0"
, "ccv_nnc_symbolic_graph_backward.c", 1568, __extension__ __PRETTY_FUNCTION__
); }))
;
1569 assert(symbol->d < tensor_symbol_info_size)((void) sizeof ((symbol->d < tensor_symbol_info_size) ?
1 : 0), __extension__ ({ if (symbol->d < tensor_symbol_info_size
) ; else __assert_fail ("symbol->d < tensor_symbol_info_size"
, "ccv_nnc_symbolic_graph_backward.c", 1569, __extension__ __PRETTY_FUNCTION__
); }))
;
1570 const ccv_nnc_tensor_symbol_info_t* const forw_symbol = tensor_symbol_info + symbol->d;
1571 if (!symbol->alias_ref)
1572 {
1573 assert(!forw_symbol->alias_ref)((void) sizeof ((!forw_symbol->alias_ref) ? 1 : 0), __extension__
({ if (!forw_symbol->alias_ref) ; else __assert_fail ("!forw_symbol->alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 1573, __extension__ __PRETTY_FUNCTION__
); }))
;
1574 symbol->symbol = ccv_nnc_tensor_symbol_new(graph, forw_symbol->info, 0);
1575 ccv_nnc_tensor_symbol_set_flags(graph, symbol->symbol, symbol->flags);
1576 } else {
1577 assert(forw_symbol->alias_ref)((void) sizeof ((forw_symbol->alias_ref) ? 1 : 0), __extension__
({ if (forw_symbol->alias_ref) ; else __assert_fail ("forw_symbol->alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 1577, __extension__ __PRETTY_FUNCTION__
); }))
;
1578 assert(symbol->flags == 0)((void) sizeof ((symbol->flags == 0) ? 1 : 0), __extension__
({ if (symbol->flags == 0) ; else __assert_fail ("symbol->flags == 0"
, "ccv_nnc_symbolic_graph_backward.c", 1578, __extension__ __PRETTY_FUNCTION__
); }))
; // We don't set flags on alias.
1579 // Due to our generation order, this must be after the original symbol is created.
1580 ccv_nnc_autograd_tensor_symbol_t* ref = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, symbol->alias_ref - 1)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(symbol->alias_ref
- 1)))
;
1581 symbol->symbol = ccv_nnc_tensor_symbol_alias_new(graph, ref->symbol, forw_symbol->ofs, forw_symbol->inc, forw_symbol->info, 0);
1582 }
1583 }
1584 ccv_nnc_graph_backward_info_t* const backward_info = backward_prep->backward_info;
1585 ccv_nnc_autograd_graph_exec_symbol_t* const autograd_execs = backward_prep->autograd_execs;
1586 ccv_array_t* symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1587 ccv_array_t* symbol_map = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_map_t), 0, 0);
1588 ccv_array_t* sub_f_symbols = 0;
1589 ccv_array_t* sub_wrt_symbols = 0;
1590 ccv_array_t* sub_execs = 0;
1591 for (i = 0; i < exec_symbol_info_size; i++)
6
Assuming 'i' is < 'exec_symbol_info_size'
7
Loop condition is true. Entering loop body
1592 {
1593 // This is not going to be an interesting node. Skip.
1594 if ((backward_info[i].f_wrt & 0x3) != 0x3)
8
Assuming the condition is false
9
Taking false branch
1595 continue;
1596 ccv_nnc_graph_backward_info_t* const back_info = backward_info + i;
1597 ccv_nnc_autograd_graph_exec_symbol_t* const back_exec = autograd_execs + i;
1598 if (back_exec->cmd.cmd == CCV_NNC_NOOP)
10
Assuming the condition is false
11
Taking false branch
1599 {
1600 back_exec->symbol = ccv_nnc_graph_exec_symbol_new(graph, back_exec->cmd, 0, 0, 0, 0, 0);
1601 continue;
1602 }
1603 const ccv_nnc_graph_exec_symbol_info_t* const forw_exec = exec_symbol_info + i;
1604 if (forw_exec->flags & CCV_NNC_GRAPH_EXEC_P_WHILE)
12
Assuming the condition is true
13
Taking true branch
1605 {
1606 ccv_array_clear(symbols);
1607 const int graph_ref = CCV_NNC_GRAPH_REF(forw_exec)((forw_exec)->_heap_graph_ref ? (forw_exec)->_heap_graph_ref
: (forw_exec)->_inline_graph_ref)
[0] - 1;
14
Assuming the condition is false
15
'?' condition is false
1608 ccv_nnc_symbolic_graph_backward_prep_t* sub_prep = backward_prep->sub_preps + graph_ref;
1609 ccv_nnc_symbolic_graph_t* sub_graph = ccv_nnc_symbolic_graph_new();
1610 sub_graph->peer = sub_prep->graph;
1611 if (!sub_wrt_symbols)
16
Taking true branch
1612 sub_wrt_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1613 // I am done, need to redo above for sub_prep, and it has to be successful now.
1614 if (!sub_f_symbols)
17
Taking true branch
1615 sub_f_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1616 _ccv_nnc_symbolic_graph_backward_prep_sub_f_wrt_symbols(forw_exec, sub_prep->graph, graph_ref, tensor_symbol_info, back_info->input_bitmasks, back_info->output_bitmasks, sub_f_symbols, sub_wrt_symbols);
18
Calling '_ccv_nnc_symbolic_graph_backward_prep_sub_f_wrt_symbols'
1617 _ccv_nnc_symbolic_graph_backward_gen(sub_prep, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_f_symbols, 0)((void*)(((char*)((sub_f_symbols)->data)) + (size_t)(sub_f_symbols
)->rsize * (size_t)(0)))
, sub_f_symbols->rnum, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, 0)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(0)))
, sub_wrt_symbols->rnum, sub_graph, root);
1618 back_exec->symbol = ccv_nnc_symbolic_graph_while(graph, back_exec->cmd.cmd, sub_graph, forw_exec->name);
1619 if (!sub_execs)
1620 sub_execs = ccv_array_new(sizeof(ccv_nnc_graph_exec_symbol_t), 0, 0);
1621 ccv_array_clear(sub_execs);
1622 // Find the breakpoints in forward graph, creating the reverse one.
1623 for (j = 0; j < sub_prep->graph->breakpoint_size; j++)
1624 {
1625 const int d = sub_prep->graph->breakpoints[j].d;
1626 if (sub_prep->autograd_execs[d].symbol.graph)
1627 ccv_array_push(sub_execs, &sub_prep->autograd_execs[d].symbol);
1628 else
1629 _ccv_nnc_add_backward_breakpoint_for_symbol(sub_prep, sub_prep->graph->breakpoints[j], sub_graph, sub_execs);
1630 }
1631 ccv_nnc_symbolic_graph_set_while_expr(sub_graph, NOOP_GRAPH_WHILE_EXPR(ccv_nnc_graph_while_f)(1), 0, 0, 0, (ccv_nnc_graph_exec_symbol_t*)ccv_array_get(sub_execs, 0)((void*)(((char*)((sub_execs)->data)) + (size_t)(sub_execs
)->rsize * (size_t)(0)))
, sub_execs->rnum);
1632 ccv_nnc_graph_exec_symbol_autogen(sub_graph, 0, 0, CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
1633 _ccv_nnc_symbolic_graph_set_backward_carry_overs(sub_prep, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, 0)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(0)))
, sub_wrt_symbols->rnum, sub_graph);
1634 for (j = 0; j < back_exec->input_size; j++)
1635 if (back_info->input_bitmasks[j >> 6] & ((uint64_t)1 << j))
1636 ccv_array_push(symbols, &(((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->inputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
inputs[j])))
)->symbol));
1637 // Find whether in the wrt symbols, anything we need to init to zero, if there are, these need to be inputs here too.
1638 _ccv_nnc_symbolic_graph_add_init_zeros(sub_prep, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, 0)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(0)))
, sub_wrt_symbols->rnum, graph, sub_graph, symbols);
1639 _ccv_nnc_symbolic_graph_add_tape_vars(sub_prep, root, graph, sub_graph, symbols);
1640 // input_size at this point, may be different from the back_exec->input_size, the reason is because we may added zeroing tensors as input tensors.
1641 const int input_size = symbols->rnum;
1642 for (j = 0; j < back_exec->output_size; j++)
1643 if (back_info->output_bitmasks[j >> 6] & ((uint64_t)1 << j))
1644 ccv_array_push(symbols, &(((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->outputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
outputs[j])))
)->symbol));
1645 const int output_size = symbols->rnum - input_size;
1646 const int p_idx = sub_prep->graph->p_idx - 1;
1647 assert(back_exec->input_size == forw_exec->output_size)((void) sizeof ((back_exec->input_size == forw_exec->output_size
) ? 1 : 0), __extension__ ({ if (back_exec->input_size == forw_exec
->output_size) ; else __assert_fail ("back_exec->input_size == forw_exec->output_size"
, "ccv_nnc_symbolic_graph_backward.c", 1647, __extension__ __PRETTY_FUNCTION__
); }))
;
1648 k = 0;
1649 for (j = 0; j < back_exec->input_size; j++)
1650 if (back_info->input_bitmasks[j >> 6] & ((uint64_t)1 << j))
1651 {
1652 const ccv_nnc_tensor_symbol_info_t* const info = tensor_symbol_info + forw_exec->outputs[j];
1653 const int s_idx = *(int*)ccv_array_get(info->s_ref, p_idx)((void*)(((char*)((info->s_ref)->data)) + (size_t)(info
->s_ref)->rsize * (size_t)(p_idx)))
- 1;
1654 assert(s_idx >= 0)((void) sizeof ((s_idx >= 0) ? 1 : 0), __extension__ ({ if
(s_idx >= 0) ; else __assert_fail ("s_idx >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 1654, __extension__ __PRETTY_FUNCTION__); }))
;
1655 const ccv_nnc_autograd_tensor_symbol_t* const autograd_symbol = _ccv_nnc_autograd_tensor_symbol_from_tensor_version(sub_prep->autograd_tensor_symbols, sub_prep->autograd_tensor_versions + s_idx);
1656 ccv_nnc_tensor_symbol_hookup(graph, sub_graph, *(ccv_nnc_tensor_symbol_t*)ccv_array_get(symbols, k)((void*)(((char*)((symbols)->data)) + (size_t)(symbols)->
rsize * (size_t)(k)))
, autograd_symbol->symbol);
1657 ++k;
1658 }
1659 k = input_size; // Reset k, the symbol pass already set up by add_init_zeros.
1660 assert(back_exec->output_size == forw_exec->input_size)((void) sizeof ((back_exec->output_size == forw_exec->input_size
) ? 1 : 0), __extension__ ({ if (back_exec->output_size ==
forw_exec->input_size) ; else __assert_fail ("back_exec->output_size == forw_exec->input_size"
, "ccv_nnc_symbolic_graph_backward.c", 1660, __extension__ __PRETTY_FUNCTION__
); }))
;
1661 for (j = 0; j < back_exec->output_size; j++)
1662 if (back_info->output_bitmasks[j >> 6] & ((uint64_t)1 << j))
1663 {
1664 const ccv_nnc_tensor_symbol_info_t* const info = tensor_symbol_info + forw_exec->inputs[j];
1665 const int s_idx = *(int*)ccv_array_get(info->s_ref, p_idx)((void*)(((char*)((info->s_ref)->data)) + (size_t)(info
->s_ref)->rsize * (size_t)(p_idx)))
- 1;
1666 assert(s_idx >= 0)((void) sizeof ((s_idx >= 0) ? 1 : 0), __extension__ ({ if
(s_idx >= 0) ; else __assert_fail ("s_idx >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 1666, __extension__ __PRETTY_FUNCTION__); }))
;
1667 const ccv_nnc_autograd_tensor_symbol_t* const autograd_symbol = _ccv_nnc_autograd_tensor_symbol_from_tensor_version(sub_prep->autograd_tensor_symbols, sub_prep->autograd_tensor_versions + s_idx);
1668 ccv_nnc_tensor_symbol_hookup(graph, sub_graph, *(ccv_nnc_tensor_symbol_t*)ccv_array_get(symbols, k)((void*)(((char*)((symbols)->data)) + (size_t)(symbols)->
rsize * (size_t)(k)))
, autograd_symbol->symbol);
1669 ++k;
1670 }
1671 ccv_nnc_graph_exec_symbol_set_io(graph, back_exec->symbol, ccv_array_get(symbols, 0)((void*)(((char*)((symbols)->data)) + (size_t)(symbols)->
rsize * (size_t)(0)))
, input_size, ccv_array_get(symbols, input_size)((void*)(((char*)((symbols)->data)) + (size_t)(symbols)->
rsize * (size_t)(input_size)))
, output_size);
1672 } else if (forw_exec->flags & CCV_NNC_GRAPH_EXEC_CASE_OF) {
1673 ccv_array_clear(symbol_map);
1674 for (j = 0; j < back_exec->output_size; j++)
1675 if (back_info->output_bitmasks[j >> 6] & ((uint64_t)1 << j))
1676 {
1677 ccv_nnc_tensor_symbol_map_t symbol = {
1678 .source = ((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->inputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
inputs[j])))
)->symbol,
1679 .destination = ((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->outputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
outputs[j])))
)->symbol,
1680 };
1681 ccv_array_push(symbol_map, &symbol);
1682 }
1683 const int symbol_map_size = symbol_map->rnum;
1684 back_exec->symbol = ccv_nnc_symbolic_graph_case_of_new(graph, back_exec->cmd.cmd, 0, 0, ccv_array_get(symbol_map, 0)((void*)(((char*)((symbol_map)->data)) + (size_t)(symbol_map
)->rsize * (size_t)(0)))
, symbol_map_size, forw_exec->name);
1685 ccv_nnc_symbolic_graph_set_case_of_expr(graph, back_exec->symbol, NOOP_GRAPH_CASE_OF_EXPR(ccv_nnc_graph_case_of_f)(1), 0);
1686 for (p = 0; p < forw_exec->graph_ref_size; p++)
1687 {
1688 const int graph_ref = CCV_NNC_GRAPH_REF(forw_exec)((forw_exec)->_heap_graph_ref ? (forw_exec)->_heap_graph_ref
: (forw_exec)->_inline_graph_ref)
[p] - 1;
1689 ccv_nnc_symbolic_graph_backward_prep_t* sub_prep = backward_prep->sub_preps + graph_ref;
1690 ccv_nnc_symbolic_graph_t* sub_graph = ccv_nnc_symbolic_graph_new();
1691 sub_graph->peer = sub_prep->graph;
1692 if (!sub_wrt_symbols)
1693 sub_wrt_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1694 // I am done, need to redo above for sub_prep, and it has to be successful now.
1695 if (!sub_f_symbols)
1696 sub_f_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 0, 0);
1697 _ccv_nnc_symbolic_graph_backward_prep_sub_f_wrt_symbols(forw_exec, sub_prep->graph, graph_ref, tensor_symbol_info, back_info->input_bitmasks, back_info->output_bitmasks, sub_f_symbols, sub_wrt_symbols);
1698 _ccv_nnc_symbolic_graph_backward_gen(sub_prep, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_f_symbols, 0)((void*)(((char*)((sub_f_symbols)->data)) + (size_t)(sub_f_symbols
)->rsize * (size_t)(0)))
, sub_f_symbols->rnum, (ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, 0)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(0)))
, sub_wrt_symbols->rnum, sub_graph, root);
1699 ccv_array_clear(symbol_map);
1700 k = 0;
1701 for (j = 0; j < back_exec->output_size; j++)
1702 if (back_info->output_bitmasks[j >> 6] & ((uint64_t)1 << j))
1703 {
1704 const int d = ((ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_wrt_symbols, k)((void*)(((char*)((sub_wrt_symbols)->data)) + (size_t)(sub_wrt_symbols
)->rsize * (size_t)(k)))
)->d;
1705 if (d >= 0)
1706 {
1707 const ccv_nnc_autograd_tensor_symbol_t* const autograd_symbol = _ccv_nnc_autograd_tensor_symbol_from_tensor_version(sub_prep->autograd_tensor_symbols, sub_prep->autograd_tensor_versions + d);
1708 ccv_nnc_tensor_symbol_map_t symbol = {
1709 .source = autograd_symbol->symbol,
1710 .destination = ((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->outputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
outputs[j])))
)->symbol,
1711 };
1712 ccv_array_push(symbol_map, &symbol);
1713 } else {
1714 // Create a new tensor in sub-graph and set it to be 0.
1715 const ccv_nnc_cmd_t cmd = ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, CMD_BLAS(0)((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.blas={.a={0}}}), 0);
1716 const ccv_nnc_autograd_tensor_symbol_t* const autograd_symbol = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->outputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
outputs[j])))
;
1717 // autograd_symbol->d points to the corresponding forward tensor.
1718 ccv_nnc_tensor_symbol_t zero_symbol = ccv_nnc_tensor_symbol_new(sub_graph, tensor_symbol_info[autograd_symbol->d].info, 0);
1719 ccv_nnc_graph_exec_symbol_new(sub_graph, cmd, 0, 0, &zero_symbol, 1, 0);
1720 ccv_nnc_tensor_symbol_map_t symbol = {
1721 .source = zero_symbol,
1722 .destination = autograd_symbol->symbol,
1723 };
1724 ccv_array_push(symbol_map, &symbol);
1725 }
1726 ++k;
1727 }
1728 ccv_nnc_graph_exec_symbol_autogen(sub_graph, 0, 0, CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
1729 const int symbol_map_size = symbol_map->rnum;
1730 ccv_nnc_symbolic_graph_set_case_of(graph, back_exec->symbol, sub_graph, p, ccv_array_get(symbol_map, 0)((void*)(((char*)((symbol_map)->data)) + (size_t)(symbol_map
)->rsize * (size_t)(0)))
, symbol_map_size);
1731 // Hookup input only after this becomes a sub graph of the graph.
1732 k = 0;
1733 for (j = 0; j < back_exec->input_size; j++)
1734 if (back_info->input_bitmasks[j >> 6] & ((uint64_t)1 << j))
1735 {
1736 const int d = ((ccv_nnc_tensor_symbol_t*)ccv_array_get(sub_f_symbols, k)((void*)(((char*)((sub_f_symbols)->data)) + (size_t)(sub_f_symbols
)->rsize * (size_t)(k)))
)->d;
1737 assert(d >= 0)((void) sizeof ((d >= 0) ? 1 : 0), __extension__ ({ if (d >=
0) ; else __assert_fail ("d >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 1737, __extension__ __PRETTY_FUNCTION__); }))
;
1738 // No corresponding sub tensors allocated. Skip.
1739 if (!sub_prep->autograd_tensor_versions[d].ref_version ||
1740 !sub_prep->autograd_tensor_versions[d].ref_version->rnum)
1741 continue;
1742 const ccv_nnc_autograd_tensor_symbol_t* const autograd_symbol = _ccv_nnc_autograd_tensor_symbol_from_tensor_version(sub_prep->autograd_tensor_symbols, sub_prep->autograd_tensor_versions + d);
1743 ccv_nnc_tensor_symbol_hookup(graph, sub_graph, ((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->inputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
inputs[j])))
)->symbol, autograd_symbol->symbol);
1744 ++k;
1745 }
1746 // Need to make sure tape vars are hooked up.
1747 _ccv_nnc_symbolic_graph_add_tape_vars(sub_prep, root, graph, sub_graph, 0);
1748 }
1749 } else {
1750 ccv_array_clear(symbols);
1751 // Gradient inputs.
1752 for (j = 0; j < back_exec->input_size; j++)
1753 if (back_info->input_bitmasks[j >> 6] & ((uint64_t)1 << j))
1754 ccv_array_push(symbols, &(((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->inputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
inputs[j])))
)->symbol));
1755 else
1756 ccv_array_push(symbols, &NO_TENSOR_SYMBOL(ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL});
1757 // Inputs from forward function.
1758 for (j = 0; j < forw_exec->input_size; j++)
1759 if (!(back_info->input_bitmasks[(j + back_exec->input_size) >> 6] & ((uint64_t)1 << (j + back_exec->input_size))))
1760 ccv_array_push(symbols, &NO_TENSOR_SYMBOL(ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL});
1761 else {
1762 const ccv_nnc_tensor_symbol_t symbol = {
1763 .d = forw_exec->inputs[j],
1764 .graph = backward_prep->graph
1765 };
1766 if (graph == backward_prep->graph)
1767 ccv_array_push(symbols, &symbol);
1768 else { // Otherwise, create a new symbol, and set its peer to the old symbol.
1769 const ccv_nnc_tensor_symbol_t new_symbol = ccv_nnc_tensor_symbol_new(graph, tensor_symbol_info[forw_exec->inputs[j]].info, tensor_symbol_info[forw_exec->inputs[j]].name);
1770 ccv_nnc_tensor_symbol_set_peer(graph, new_symbol, symbol);
1771 const int flags = ccv_nnc_tensor_symbol_flags(backward_prep->graph, symbol) | CCV_NNC_TENSOR_SYMBOL_TAPE_VAR;
1772 ccv_nnc_tensor_symbol_set_flags(graph, new_symbol, flags);
1773 ccv_nnc_tensor_symbol_set_flags(backward_prep->graph, symbol, flags);
1774 ccv_array_push(symbols, &new_symbol);
1775 }
1776 }
1777 // Outputs from forward function.
1778 for (j = 0; j < forw_exec->output_size; j++)
1779 if (!(back_info->input_bitmasks[(j + back_exec->input_size + forw_exec->input_size) >> 6] & ((uint64_t)1 << (j + back_exec->input_size + forw_exec->input_size))))
1780 ccv_array_push(symbols, &NO_TENSOR_SYMBOL(ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL});
1781 else {
1782 const ccv_nnc_tensor_symbol_t symbol = {
1783 .d = forw_exec->outputs[j],
1784 .graph = backward_prep->graph
1785 };
1786 if (graph == backward_prep->graph)
1787 ccv_array_push(symbols, &symbol);
1788 else { // Otherwise, create a new symbol, and set its peer to the old symbol.
1789 const ccv_nnc_tensor_symbol_t new_symbol = ccv_nnc_tensor_symbol_new(graph, tensor_symbol_info[forw_exec->outputs[j]].info, tensor_symbol_info[forw_exec->outputs[j]].name);
1790 ccv_nnc_tensor_symbol_set_peer(graph, new_symbol, symbol);
1791 const int flags = ccv_nnc_tensor_symbol_flags(backward_prep->graph, symbol) | CCV_NNC_TENSOR_SYMBOL_TAPE_VAR;
1792 ccv_nnc_tensor_symbol_set_flags(graph, new_symbol, flags);
1793 ccv_nnc_tensor_symbol_set_flags(backward_prep->graph, symbol, flags);
1794 ccv_array_push(symbols, &new_symbol);
1795 }
1796 }
1797 for (j = 0; j < back_exec->output_size; j++)
1798 if (back_info->output_bitmasks[j >> 6] & ((uint64_t)1 << j))
1799 ccv_array_push(symbols, &(((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, back_exec->outputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(back_exec->
outputs[j])))
)->symbol));
1800 else
1801 ccv_array_push(symbols, &NO_TENSOR_SYMBOL(ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL});
1802 back_exec->symbol = ccv_nnc_graph_exec_symbol_new(graph, back_exec->cmd, ccv_array_get(symbols, 0)((void*)(((char*)((symbols)->data)) + (size_t)(symbols)->
rsize * (size_t)(0)))
, back_exec->input_size + forw_exec->input_size + forw_exec->output_size, ccv_array_get(symbols, back_exec->input_size + forw_exec->input_size + forw_exec->output_size)((void*)(((char*)((symbols)->data)) + (size_t)(symbols)->
rsize * (size_t)(back_exec->input_size + forw_exec->input_size
+ forw_exec->output_size)))
, back_exec->output_size, 0);
1803 ccv_nnc_graph_exec_symbol_set_hint(graph, back_exec->symbol, exec_symbol_info[i].hint);
1804 ccv_nnc_graph_exec_symbol_set_peer(graph, back_exec->symbol, (ccv_nnc_graph_exec_symbol_t){
1805 .d = i,
1806 .graph = backward_prep->graph,
1807 });
1808 }
1809 }
1810 if (sub_f_symbols)
1811 ccv_array_free(sub_f_symbols);
1812 if (sub_wrt_symbols)
1813 ccv_array_free(sub_wrt_symbols);
1814 if (sub_execs)
1815 ccv_array_free(sub_execs);
1816 ccv_array_t* const sum_or_set_execs = backward_prep->sum_or_set_execs;
1817 for (i = 0; i < sum_or_set_execs->rnum; i++)
1818 {
1819 ccv_nnc_sum_or_set_graph_exec_symbol_t* sum_or_set_exec = (ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, i)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(i)))
;
1820 // It is sum, set don't have inputs.
1821 if (sum_or_set_exec->input_size)
1822 {
1823 ccv_array_clear(symbols);
1824 // This is to sum.
1825 for (j = 0; j < sum_or_set_exec->input_size; j++)
1826 ccv_array_push(symbols, &(((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, sum_or_set_exec->inputs[j])((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(sum_or_set_exec
->inputs[j])))
)->symbol));
1827 ccv_nnc_cmd_t cmd = ccv_nnc_cmd(CCV_NNC_EWSUM_FORWARD, 0, CMD_GENERIC()((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}}}), 0);
1828 sum_or_set_exec->symbol = ccv_nnc_graph_exec_symbol_new(graph, cmd, ccv_array_get(symbols, 0)((void*)(((char*)((symbols)->data)) + (size_t)(symbols)->
rsize * (size_t)(0)))
, sum_or_set_exec->input_size, &(((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, sum_or_set_exec->output)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(sum_or_set_exec
->output)))
)->symbol), 1, 0);
1829 } else {
1830 ccv_nnc_cmd_t cmd = ccv_nnc_cmd(CCV_NNC_SET_FORWARD, 0, CMD_BLAS(sum_or_set_exec->value)((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.blas={.a={sum_or_set_exec
->value}}})
, 0);
1831 sum_or_set_exec->symbol = ccv_nnc_graph_exec_symbol_new(graph, cmd, 0, 0, &(((ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, sum_or_set_exec->output)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(sum_or_set_exec
->output)))
)->symbol), 1, 0);
1832 }
1833 }
1834 ccv_array_free(symbol_map);
1835 ccv_array_free(symbols);
1836 for (i = 0; i < exec_symbol_info_size; i++)
1837 {
1838 // This is not going to be an interesting node. Skip.
1839 if ((backward_info[i].f_wrt & 0x3) != 0x3)
1840 continue;
1841 ccv_nnc_autograd_graph_exec_symbol_t* const back_exec = autograd_execs + i;
1842 // If on the same graph, we cannot decide whether it is before or after the forw_exec, enforcing it is after forw_exec.
1843 if (graph == backward_prep->graph)
1844 ccv_nnc_graph_exec_symbol_concat(graph, (ccv_nnc_graph_exec_symbol_t){
1845 .d = i,
1846 .graph = graph
1847 }, back_exec->symbol);
1848 if (back_exec->outgoings)
1849 for (j = 0; j < back_exec->outgoings->rnum; j++)
1850 {
1851 int d = *(int*)ccv_array_get(back_exec->outgoings, j)((void*)(((char*)((back_exec->outgoings)->data)) + (size_t
)(back_exec->outgoings)->rsize * (size_t)(j)))
;
1852 if (d < exec_symbol_info_size)
1853 ccv_nnc_graph_exec_symbol_concat(graph, back_exec->symbol, autograd_execs[d].symbol);
1854 else
1855 ccv_nnc_graph_exec_symbol_concat(graph, back_exec->symbol, ((ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, d - exec_symbol_info_size)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(d - exec_symbol_info_size)))
)->symbol);
1856 }
1857 }
1858 for (i = 0; i < sum_or_set_execs->rnum; i++)
1859 {
1860 ccv_nnc_sum_or_set_graph_exec_symbol_t* exec = (ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, i)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(i)))
;
1861 if (exec->outgoings)
1862 for (j = 0; j < exec->outgoings->rnum; j++)
1863 {
1864 int d = *(int*)ccv_array_get(exec->outgoings, j)((void*)(((char*)((exec->outgoings)->data)) + (size_t)(
exec->outgoings)->rsize * (size_t)(j)))
;
1865 if (d < exec_symbol_info_size)
1866 ccv_nnc_graph_exec_symbol_concat(graph, exec->symbol, autograd_execs[d].symbol);
1867 else
1868 ccv_nnc_graph_exec_symbol_concat(graph, exec->symbol, ((ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, d - exec_symbol_info_size)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(d - exec_symbol_info_size)))
)->symbol);
1869 }
1870 }
1871 // Now, everything is done, set the metadata on graph so that we can lookup later for backward symbols
1872 if (graph->backward.tensor_symbol_idx)
1873 graph->backward.tensor_symbol_idx = (int*)ccreallocrealloc(graph->backward.tensor_symbol_idx, sizeof(int) * (graph->tensor_symbol_info->rnum + tensor_symbol_info_size));
1874 else
1875 graph->backward.tensor_symbol_idx = (int*)ccmallocmalloc(sizeof(int) * (graph->tensor_symbol_info->rnum + tensor_symbol_info_size));
1876 graph->backward.tensor_symbol_size = tensor_symbol_info_size;
1877 graph->backward.exec_symbol_idx = graph->backward.tensor_symbol_idx + tensor_symbol_info_size;
1878 graph->backward.exec_symbol_size = graph->tensor_symbol_info->rnum;
1879 for (i = 0; i < tensor_symbol_info_size; i++)
1880 graph->backward.tensor_symbol_idx[i] = -1;
1881 for (i = 0; i < graph->backward.exec_symbol_size; i++)
1882 graph->backward.exec_symbol_idx[i] = -1;
1883 ccv_nnc_autograd_tensor_version_t* const autograd_tensor_versions = backward_prep->autograd_tensor_versions;
1884 // Assigning for wrt symbols.
1885 for (i = 0; i < wrt_symbol_size; i++)
1886 {
1887 const int d = wrt_symbols[i].d;
1888 if (d < 0)
1889 continue;
1890 assert(d < tensor_symbol_info_size)((void) sizeof ((d < tensor_symbol_info_size) ? 1 : 0), __extension__
({ if (d < tensor_symbol_info_size) ; else __assert_fail (
"d < tensor_symbol_info_size", "ccv_nnc_symbolic_graph_backward.c"
, 1890, __extension__ __PRETTY_FUNCTION__); }))
;
1891 // If this wrt symbol is an alias, create extra alias for this.
1892 ccv_nnc_autograd_tensor_version_t* const tensor_ver = autograd_tensor_versions + d;
1893 assert(tensor_ver->ref_version)((void) sizeof ((tensor_ver->ref_version) ? 1 : 0), __extension__
({ if (tensor_ver->ref_version) ; else __assert_fail ("tensor_ver->ref_version"
, "ccv_nnc_symbolic_graph_backward.c", 1893, __extension__ __PRETTY_FUNCTION__
); }))
;
1894 ccv_nnc_tensor_ref_t* const tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, tensor_ver->c)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(tensor_ver
->c)))
;
1895 ccv_nnc_autograd_tensor_symbol_t* autograd_symbol = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
1896 graph->backward.tensor_symbol_idx[d] = autograd_symbol->symbol.d;
1897 const int dd = autograd_symbol->symbol.d;
1898 const int x = tensor_ref->x;
1899 if (tensor_ref->exec_registry && tensor_ref->exec_registry->rnum) // Create no-op node.
1900 {
1901 ccv_nnc_graph_exec_symbol_t noop = ccv_nnc_graph_exec_symbol_new(graph, ccv_nnc_cmd(CCV_NNC_NOOP, 0, CMD_GENERIC()((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}}}), 0), 0, 0, 0, 0, 0);
1902 if (x < exec_symbol_info_size)
1903 ccv_nnc_graph_exec_symbol_concat(graph, autograd_execs[x].symbol, noop);
1904 else
1905 ccv_nnc_graph_exec_symbol_concat(graph, ((ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, x - exec_symbol_info_size)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(x - exec_symbol_info_size)))
)->symbol, noop);
1906 for (j = 0; j < tensor_ref->exec_registry->rnum; j++)
1907 {
1908 const int x = *(int*)ccv_array_get(tensor_ref->exec_registry, j)((void*)(((char*)((tensor_ref->exec_registry)->data)) +
(size_t)(tensor_ref->exec_registry)->rsize * (size_t)(
j)))
;
1909 assert(x >= 0)((void) sizeof ((x >= 0) ? 1 : 0), __extension__ ({ if (x >=
0) ; else __assert_fail ("x >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 1909, __extension__ __PRETTY_FUNCTION__); }))
; /* Otherwise, this is initialization tensor, which is impossible to be summed up by. */
1910 assert(x < exec_symbol_info_size)((void) sizeof ((x < exec_symbol_info_size) ? 1 : 0), __extension__
({ if (x < exec_symbol_info_size) ; else __assert_fail ("x < exec_symbol_info_size"
, "ccv_nnc_symbolic_graph_backward.c", 1910, __extension__ __PRETTY_FUNCTION__
); }))
; // exec_registry is only used by alias_registry, it simply cannot reference to a sum operation.
1911 ccv_nnc_graph_exec_symbol_concat(graph, autograd_execs[x].symbol, noop);
1912 }
1913 graph->backward.exec_symbol_idx[dd] = noop.d;
1914 } else {
1915 if (x < exec_symbol_info_size)
1916 graph->backward.exec_symbol_idx[dd] = autograd_execs[x].symbol.d;
1917 else
1918 graph->backward.exec_symbol_idx[dd] = ((ccv_nnc_sum_or_set_graph_exec_symbol_t*)ccv_array_get(sum_or_set_execs, x - exec_symbol_info_size)((void*)(((char*)((sum_or_set_execs)->data)) + (size_t)(sum_or_set_execs
)->rsize * (size_t)(x - exec_symbol_info_size)))
)->symbol.d;
1919 }
1920 }
1921 // Assigning for f symbols.
1922 for (i = 0; i < f_symbol_size; i++)
1923 {
1924 const int d = f_symbols[i].d;
1925 assert(d >= 0)((void) sizeof ((d >= 0) ? 1 : 0), __extension__ ({ if (d >=
0) ; else __assert_fail ("d >= 0", "ccv_nnc_symbolic_graph_backward.c"
, 1925, __extension__ __PRETTY_FUNCTION__); }))
;
1926 assert(d < tensor_symbol_info_size)((void) sizeof ((d < tensor_symbol_info_size) ? 1 : 0), __extension__
({ if (d < tensor_symbol_info_size) ; else __assert_fail (
"d < tensor_symbol_info_size", "ccv_nnc_symbolic_graph_backward.c"
, 1926, __extension__ __PRETTY_FUNCTION__); }))
;
1927 const ccv_nnc_autograd_tensor_version_t* const tensor_ver = autograd_tensor_versions + d;
1928 if (tensor_ver->ref_version)
1929 {
1930 // We don't use _ccv_nnc_autograd_tensor_symbol_from_tensor_version because that select the last version, but for us, we need the first version.
1931 const ccv_nnc_tensor_ref_t* const tensor_ref = (ccv_nnc_tensor_ref_t*)ccv_array_get(tensor_ver->ref_version, 0)((void*)(((char*)((tensor_ver->ref_version)->data)) + (
size_t)(tensor_ver->ref_version)->rsize * (size_t)(0)))
;
1932 const ccv_nnc_autograd_tensor_symbol_t* const autograd_symbol = (ccv_nnc_autograd_tensor_symbol_t*)ccv_array_get(autograd_tensor_symbols, tensor_ref->d)((void*)(((char*)((autograd_tensor_symbols)->data)) + (size_t
)(autograd_tensor_symbols)->rsize * (size_t)(tensor_ref->
d)))
;
1933 graph->backward.tensor_symbol_idx[d] = autograd_symbol->symbol.d;
1934 // Cannot find relevant backward exec symbols for f, it could be many.
1935 }
1936 }
1937}
1938
1939void ccv_nnc_symbolic_graph_backward(ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const f_symbols, const int f_symbol_size, const ccv_nnc_tensor_symbol_t* const wrt_symbols, const int wrt_symbol_size, const ccv_nnc_graph_exec_symbol_t* const sources, const int source_size, const ccv_nnc_graph_exec_symbol_t* const destinations, const int destination_size)
1940{
1941 int i;
1942 // TODO: f symbols cannot be alias yet.
1943 for (i = 0; i < f_symbol_size; i++)
1944 {
1945 assert(f_symbols[i].graph == graph)((void) sizeof ((f_symbols[i].graph == graph) ? 1 : 0), __extension__
({ if (f_symbols[i].graph == graph) ; else __assert_fail ("f_symbols[i].graph == graph"
, "ccv_nnc_symbolic_graph_backward.c", 1945, __extension__ __PRETTY_FUNCTION__
); }))
; // f symbol has to be in the current graph.
1946 assert(!((ccv_nnc_tensor_symbol_info_t*)ccv_array_get(graph->tensor_symbol_info, f_symbols[i].d))->alias_ref)((void) sizeof ((!((ccv_nnc_tensor_symbol_info_t*)((void*)(((
char*)((graph->tensor_symbol_info)->data)) + (size_t)(graph
->tensor_symbol_info)->rsize * (size_t)(f_symbols[i].d)
)))->alias_ref) ? 1 : 0), __extension__ ({ if (!((ccv_nnc_tensor_symbol_info_t
*)((void*)(((char*)((graph->tensor_symbol_info)->data))
+ (size_t)(graph->tensor_symbol_info)->rsize * (size_t
)(f_symbols[i].d))))->alias_ref) ; else __assert_fail ("!((ccv_nnc_tensor_symbol_info_t*)ccv_array_get(graph->tensor_symbol_info, f_symbols[i].d))->alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 1946, __extension__ __PRETTY_FUNCTION__
); }))
;
1947 }
1948 // TODO: wrt symbols cannot be alias yet.
1949 for (i = 0; i < wrt_symbol_size; i++)
1950 {
1951 assert(wrt_symbols[i].graph == graph)((void) sizeof ((wrt_symbols[i].graph == graph) ? 1 : 0), __extension__
({ if (wrt_symbols[i].graph == graph) ; else __assert_fail (
"wrt_symbols[i].graph == graph", "ccv_nnc_symbolic_graph_backward.c"
, 1951, __extension__ __PRETTY_FUNCTION__); }))
;
1952 assert(!((ccv_nnc_tensor_symbol_info_t*)ccv_array_get(graph->tensor_symbol_info, wrt_symbols[i].d))->alias_ref)((void) sizeof ((!((ccv_nnc_tensor_symbol_info_t*)((void*)(((
char*)((graph->tensor_symbol_info)->data)) + (size_t)(graph
->tensor_symbol_info)->rsize * (size_t)(wrt_symbols[i].
d))))->alias_ref) ? 1 : 0), __extension__ ({ if (!((ccv_nnc_tensor_symbol_info_t
*)((void*)(((char*)((graph->tensor_symbol_info)->data))
+ (size_t)(graph->tensor_symbol_info)->rsize * (size_t
)(wrt_symbols[i].d))))->alias_ref) ; else __assert_fail ("!((ccv_nnc_tensor_symbol_info_t*)ccv_array_get(graph->tensor_symbol_info, wrt_symbols[i].d))->alias_ref"
, "ccv_nnc_symbolic_graph_backward.c", 1952, __extension__ __PRETTY_FUNCTION__
); }))
;
1953 }
1954 const int exec_symbol_info_size = graph->exec_symbol_info->rnum;
1955 const int tensor_symbol_info_size = graph->tensor_symbol_info->rnum;
1956 assert(exec_symbol_info_size > 0)((void) sizeof ((exec_symbol_info_size > 0) ? 1 : 0), __extension__
({ if (exec_symbol_info_size > 0) ; else __assert_fail ("exec_symbol_info_size > 0"
, "ccv_nnc_symbolic_graph_backward.c", 1956, __extension__ __PRETTY_FUNCTION__
); }))
;
1957 assert(tensor_symbol_info_size > 0)((void) sizeof ((tensor_symbol_info_size > 0) ? 1 : 0), __extension__
({ if (tensor_symbol_info_size > 0) ; else __assert_fail (
"tensor_symbol_info_size > 0", "ccv_nnc_symbolic_graph_backward.c"
, 1957, __extension__ __PRETTY_FUNCTION__); }))
;
1958 ccv_nnc_symbolic_graph_backward_prep_t backward_prep = _ccv_nnc_symbolic_graph_backward_prep(graph, sources, source_size, destinations, destination_size);
1959 _ccv_nnc_symbolic_graph_backward_prep_prune_ops(&backward_prep, f_symbols, f_symbol_size, wrt_symbols, wrt_symbol_size, sources, source_size, destinations, destination_size);
1960 _ccv_nnc_symbolic_graph_backward_prep_gen(&backward_prep, f_symbols, f_symbol_size, wrt_symbols, wrt_symbol_size, 0, sources, source_size, destinations, destination_size);
1961 _ccv_nnc_symbolic_graph_backward_gen(&backward_prep, f_symbols, f_symbol_size, wrt_symbols, wrt_symbol_size, graph, graph);
1962 _ccv_nnc_symbolic_graph_backward_prep_free(backward_prep);
1963}
1964
1965ccv_nnc_tensor_symbol_t ccv_nnc_tensor_symbol_for_backward(const ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t symbol)
1966{
1967 assert(symbol.d >= 0)((void) sizeof ((symbol.d >= 0) ? 1 : 0), __extension__ ({
if (symbol.d >= 0) ; else __assert_fail ("symbol.d >= 0"
, "ccv_nnc_symbolic_graph_backward.c", 1967, __extension__ __PRETTY_FUNCTION__
); }))
;
1968 assert(symbol.d < graph->backward.tensor_symbol_size)((void) sizeof ((symbol.d < graph->backward.tensor_symbol_size
) ? 1 : 0), __extension__ ({ if (symbol.d < graph->backward
.tensor_symbol_size) ; else __assert_fail ("symbol.d < graph->backward.tensor_symbol_size"
, "ccv_nnc_symbolic_graph_backward.c", 1968, __extension__ __PRETTY_FUNCTION__
); }))
;
1969 if (graph->backward.tensor_symbol_idx[symbol.d] < 0)
1970 return NO_TENSOR_SYMBOL(ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL};
1971 ccv_nnc_tensor_symbol_t tensor = {
1972 .d = graph->backward.tensor_symbol_idx[symbol.d],
1973 .graph = graph,
1974 };
1975 return tensor;
1976}
1977
1978ccv_nnc_graph_exec_symbol_t ccv_nnc_graph_exec_symbol_for_backward(const ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t symbol)
1979{
1980 assert(symbol.d >= 0)((void) sizeof ((symbol.d >= 0) ? 1 : 0), __extension__ ({
if (symbol.d >= 0) ; else __assert_fail ("symbol.d >= 0"
, "ccv_nnc_symbolic_graph_backward.c", 1980, __extension__ __PRETTY_FUNCTION__
); }))
;
1981 assert(symbol.d < graph->backward.exec_symbol_size)((void) sizeof ((symbol.d < graph->backward.exec_symbol_size
) ? 1 : 0), __extension__ ({ if (symbol.d < graph->backward
.exec_symbol_size) ; else __assert_fail ("symbol.d < graph->backward.exec_symbol_size"
, "ccv_nnc_symbolic_graph_backward.c", 1981, __extension__ __PRETTY_FUNCTION__
); }))
;
1982 const int dd = symbol.d;
1983 assert(graph->backward.exec_symbol_idx[dd] >= 0)((void) sizeof ((graph->backward.exec_symbol_idx[dd] >=
0) ? 1 : 0), __extension__ ({ if (graph->backward.exec_symbol_idx
[dd] >= 0) ; else __assert_fail ("graph->backward.exec_symbol_idx[dd] >= 0"
, "ccv_nnc_symbolic_graph_backward.c", 1983, __extension__ __PRETTY_FUNCTION__
); }))
;
1984 ccv_nnc_graph_exec_symbol_t exec = {
1985 .d = graph->backward.exec_symbol_idx[dd],
1986 .graph = graph
1987 };
1988 return exec;
1989}