Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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