Bug Summary

File:nnc/ccv_nnc_dynamic_graph.c
Warning:line 593, column 9
Branch condition evaluates to a garbage value

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_dynamic_graph.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/19 -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 -I /usr/local/include -internal-isystem /usr/local/lib/clang/19/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/2026-04-05-102634-1298463-1 -x c ccv_nnc_dynamic_graph.c
1#include "ccv_nnc.h"
2#include "ccv_nnc_easy.h"
3#include "ccv_nnc_internal.h"
4#include "ccv_nnc_easy.h"
5#include "ccv_internal.h"
6#include "_ccv_nnc_dynamic_graph.h"
7
8// MARK - Level-4 API
9
10ccv_nnc_dynamic_graph_t* ccv_nnc_dynamic_graph_new(void)
11{
12 ccv_nnc_dynamic_graph_t* graph = ccmallocmalloc(sizeof(ccv_nnc_dynamic_graph_t));
13 graph->no_grad = 0;
14 graph->reuse_var = -1;
15 graph->vars = ccv_array_new(sizeof(ccv_nnc_tensor_variable_t), 1, 0);
16 graph->binds = ccv_array_new(sizeof(ccv_nnc_tensor_variable_graph_bind_t), 1, 0);
17 graph->tape = ccv_nnc_symbolic_graph_new();
18 graph->xpu_alloc.mp_hdr = -1;
19 graph->xpu_alloc.freed = kh_init(dy_str)kh_init_dy_str();
20 graph->xpu_alloc.allocd = kh_init(dy_alloc)kh_init_dy_alloc();
21 // These may not be used as frequent, init as needed.
22 graph->stateful_execs = 0;
23 graph->reuse_stateful_exec = -1;
24 graph->stream_map = 0;
25 graph->ws = 0;
26 return graph;
27}
28
29static void _ccv_nnc_tensor_variable_free(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, const int zeroing)
30{
31 const int index = tensor_variable->index;
32 if (tensor_variable->tensor_view)
33 {
34 if (tensor_variable->destructor_hook.func)
35 tensor_variable->destructor_hook.func(graph, (ccv_nnc_tensor_t*)tensor_variable->tensor_view, tensor_variable->destructor_hook.context);
36 if (!CCV_NNC_IS_EXTERN_TENSOR_VIEW(tensor_variable->tensor_view)((uintptr_t)(tensor_variable->tensor_view) & 1))
37 {
38 if (CCV_IS_TENSOR_VIEW(tensor_variable->tensor_view)((*(int*)(tensor_variable->tensor_view)) & CCV_TENSOR_VIEW
)
)
39 ccv_nnc_tensor_view_free(tensor_variable->tensor_view);
40 else {
41 if (!tensor_variable->alias_index_ref && // Return this memory to the graph.
42 CCV_TENSOR_GET_MEMORY(tensor_variable->tensor_view->info.type)((tensor_variable->tensor_view->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY && tensor_variable->tensor_view->data.u8)
43 ccv_nnc_xpu_free(&graph->xpu_alloc, tensor_variable->tensor_view->data.u8);
44 ccv_nnc_tensor_free((ccv_nnc_tensor_t*)tensor_variable->tensor_view);
45 }
46 }
47 }
48 ccfreefree(tensor_variable);
49 if (zeroing)
50 *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, index)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(index)))
= 0;
51 int i;
52 for (i = graph->vars->rnum - 1; i >= 0; i--)
53 if (*(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, i)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(i)))
!= 0)
54 {
55 graph->vars->rnum = i + 1;
56 break;
57 }
58 if (index < graph->vars->rnum &&
59 (index < graph->reuse_var || graph->reuse_var < 0))
60 graph->reuse_var = index;
61 else if (graph->reuse_var >= graph->vars->rnum)
62 graph->reuse_var = -1;
63}
64
65static void _ccv_nnc_tensor_variable_graph_bind_free(ccv_nnc_dynamic_graph_t* const graph, ccv_nnc_tensor_variable_graph_bind_t* const bind, const int zeroing)
66{
67 bind->index = CCV_NNC_TENSOR_NO_VARIABLE;
68 if (bind->sources)
69 ccv_array_free(bind->sources);
70 if (bind->destinations)
71 ccv_array_free(bind->destinations);
72 if (bind->tensor_view)
73 {
74 if (bind->destructor_hook.func)
75 bind->destructor_hook.func(graph, (ccv_nnc_tensor_t*)bind->tensor_view, bind->destructor_hook.context);
76 if (!CCV_NNC_IS_EXTERN_TENSOR_VIEW(bind->tensor_view)((uintptr_t)(bind->tensor_view) & 1))
77 {
78 if (CCV_IS_TENSOR_VIEW(bind->tensor_view)((*(int*)(bind->tensor_view)) & CCV_TENSOR_VIEW))
79 ccv_nnc_tensor_view_free(bind->tensor_view);
80 else {
81 if (!bind->alias_ref && // Return this memory to the graph.
82 CCV_TENSOR_GET_MEMORY(bind->tensor_view->info.type)((bind->tensor_view->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY && bind->tensor_view->data.u8)
83 ccv_nnc_xpu_free(&graph->xpu_alloc, bind->tensor_view->data.u8);
84 ccv_nnc_tensor_free((ccv_nnc_tensor_t*)bind->tensor_view);
85 }
86 }
87 }
88 if (zeroing)
89 {
90 bind->sources = 0;
91 bind->destinations = 0;
92 bind->tensor_view = 0;
93 bind->destructor_hook.func = 0;
94 bind->destructor_hook.context = 0;
95 }
96}
97
98void ccv_nnc_dynamic_graph_free(ccv_nnc_dynamic_graph_t* const graph)
99{
100 int i;
101 for (i = 0; i < graph->vars->rnum; i++)
102 {
103 ccv_nnc_tensor_variable_t tensor_variable = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, i)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(i)))
;
104 if (tensor_variable)
105 _ccv_nnc_tensor_variable_free(graph, tensor_variable, 0);
106 }
107 ccv_array_free(graph->vars);
108 for (i = 0; i < graph->binds->rnum; i++)
109 _ccv_nnc_tensor_variable_graph_bind_free(graph, (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, i)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(i)))
, 0);
110 ccv_array_free(graph->binds);
111 ccv_nnc_symbolic_graph_free(graph->tape);
112 if (graph->ws)
113 ccv_array_free(graph->ws);
114 if (graph->stateful_execs)
115 {
116 for (i = 0; i < graph->stateful_execs->rnum; i++)
117 {
118 ccv_nnc_stateful_exec_t* const stateful_exec = *(ccv_nnc_stateful_exec_t**)ccv_array_get(graph->stateful_execs, i)((void*)(((char*)((graph->stateful_execs)->data)) + (size_t
)(graph->stateful_execs)->rsize * (size_t)(i)))
;
119 if (stateful_exec)
120 ccfreefree(stateful_exec);
121 }
122 ccv_array_free(graph->stateful_execs);
123 }
124 if (graph->stream_map)
125 {
126 khiter_t k;
127 for (k = kh_begin(graph->stream_map)(khint_t)(0); k != kh_end(graph->stream_map)((graph->stream_map)->n_buckets); ++k)
128 {
129 if (!kh_exist(graph->stream_map, k)(!(((graph->stream_map)->flags[(k)>>4]>>(((
k)&0xfU)<<1))&3))
)
130 continue;
131 ccv_nnc_stream_context_t* const stream = kh_val(graph->stream_map, k)((graph->stream_map)->vals[k]);
132 ccv_nnc_stream_context_free(stream);
133 }
134 kh_destroy(stream_map, graph->stream_map)kh_destroy_stream_map(graph->stream_map);
135 }
136 ccv_nnc_xpu_alloc_destroy(&graph->xpu_alloc);
137 ccfreefree(graph);
138}
139
140void ccv_nnc_tensor_variable_set(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, ccv_nnc_tensor_t* const tensor)
141{
142 assert(!tensor_variable->alias_index_ref)((void) sizeof ((!tensor_variable->alias_index_ref) ? 1 : 0
), __extension__ ({ if (!tensor_variable->alias_index_ref)
; else __assert_fail ("!tensor_variable->alias_index_ref"
, "ccv_nnc_dynamic_graph.c", 142, __extension__ __PRETTY_FUNCTION__
); }))
;
143 if (tensor_variable->tensor_view && !CCV_NNC_IS_EXTERN_TENSOR_VIEW(tensor_variable->tensor_view)((uintptr_t)(tensor_variable->tensor_view) & 1))
144 {
145 assert(!CCV_IS_TENSOR_VIEW(tensor_variable->tensor_view))((void) sizeof ((!((*(int*)(tensor_variable->tensor_view))
& CCV_TENSOR_VIEW)) ? 1 : 0), __extension__ ({ if (!((*(
int*)(tensor_variable->tensor_view)) & CCV_TENSOR_VIEW
)) ; else __assert_fail ("!CCV_IS_TENSOR_VIEW(tensor_variable->tensor_view)"
, "ccv_nnc_dynamic_graph.c", 145, __extension__ __PRETTY_FUNCTION__
); }))
;
146 ccv_nnc_tensor_free((ccv_nnc_tensor_t*)tensor_variable->tensor_view);
147 }
148 tensor_variable->info = tensor->info;
149 tensor_variable->tensor_view = (ccv_nnc_tensor_view_t*)((uintptr_t)tensor | 1);
150}
151
152void ccv_nnc_tensor_variable_destructor_hook(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, ccv_nnc_tensor_variable_destructor_f func, void* const context)
153{
154 tensor_variable->destructor_hook.func = func;
155 tensor_variable->destructor_hook.context = context;
156}
157
158inline static void _ccv_nnc_tensor_variable_init(ccv_nnc_dynamic_graph_t* const graph, ccv_nnc_tensor_variable_t tensor_variable, const ccv_nnc_tensor_param_t info)
159{
160 tensor_variable->alias_index_ref = 0;
161 tensor_variable->alias_off = 0;
162 tensor_variable->destructor_hook.func = 0;
163 tensor_variable->destructor_hook.context = 0;
164 tensor_variable->info = info;
165 tensor_variable->symbol = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
166 tensor_variable->tensor_view = 0;
167 if (graph->reuse_var >= 0)
168 {
169 const int reuse_var = graph->reuse_var;
170 assert(reuse_var < graph->vars->rnum)((void) sizeof ((reuse_var < graph->vars->rnum) ? 1 :
0), __extension__ ({ if (reuse_var < graph->vars->rnum
) ; else __assert_fail ("reuse_var < graph->vars->rnum"
, "ccv_nnc_dynamic_graph.c", 170, __extension__ __PRETTY_FUNCTION__
); }))
;
171 tensor_variable->index = reuse_var;
172 *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, reuse_var)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(reuse_var)))
= tensor_variable;
173 int i;
174 graph->reuse_var = -1;
175 for (i = reuse_var + 1; i < graph->vars->rnum && graph->reuse_var < 0; i++)
176 if (*(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, i)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(i)))
== 0)
177 graph->reuse_var = i;
178 } else {
179 tensor_variable->index = graph->vars->rnum;
180 ccv_array_push(graph->vars, &tensor_variable);
181 }
182}
183
184ccv_nnc_tensor_variable_t ccv_nnc_tensor_variable_new_impl(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_param_t info)
185{
186 ccv_nnc_tensor_variable_t tensor_variable = ccmallocmalloc(sizeof(struct ccv_nnc_tensor_variable_s));
187 tensor_variable->type = CCV_NNC_TENSOR_VARIABLE;
188 _ccv_nnc_tensor_variable_init(graph, tensor_variable, info);
189 return tensor_variable;
190}
191
192ccv_nnc_tensor_variable_t ccv_nnc_tensor_constant_new_impl(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_param_t info)
193{
194 ccv_nnc_tensor_variable_t tensor_variable = ccmallocmalloc(sizeof(struct ccv_nnc_tensor_variable_s));
195 tensor_variable->type = CCV_NNC_TENSOR_CONSTANT;
196 _ccv_nnc_tensor_variable_init(graph, tensor_variable, info);
197 return tensor_variable;
198}
199
200int ccv_nnc_tensor_variable_is_constant(const ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
201{
202 return tensor_variable->type == CCV_NNC_TENSOR_CONSTANT;
203}
204
205ccv_nnc_tensor_param_t ccv_nnc_tensor_variable_params(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
206{
207 return tensor_variable->info;
208}
209
210ccv_nnc_tensor_variable_t ccv_nnc_tensor_variable_alias_new(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, const int ofs[CCV_NNC_MAX_DIM_ALLOC(12)], const int stride[CCV_NNC_MAX_DIM_ALLOC(12)], const ccv_nnc_tensor_param_t info)
211{
212 ccv_nnc_tensor_variable_t variable_alias = ccmallocmalloc(sizeof(struct ccv_nnc_tensor_variable_s));
213 variable_alias->type = tensor_variable->type;
214 // If the tensor variable is an alias itself, we point directly to its original.
215 if (tensor_variable->alias_index_ref)
216 {
217 variable_alias->alias_index_ref = tensor_variable->alias_index_ref;
218 // The tensor variable need to be fully specified if I am doing alias an alias.
219 assert(!ccv_nnc_is_tensor_auto(tensor_variable->info))((void) sizeof ((!ccv_nnc_is_tensor_auto(tensor_variable->
info)) ? 1 : 0), __extension__ ({ if (!ccv_nnc_is_tensor_auto
(tensor_variable->info)) ; else __assert_fail ("!ccv_nnc_is_tensor_auto(tensor_variable->info)"
, "ccv_nnc_dynamic_graph.c", 219, __extension__ __PRETTY_FUNCTION__
); }))
;
220 int i;
221 int no_stride = 1;
222 for (i = 0; no_stride && i < CCV_NNC_MAX_DIM_ALLOC(12); i++)
223 no_stride = (tensor_variable->stride[i] == 0);
224 int stride_from_dim[CCV_NNC_MAX_DIM_ALLOC(12)];
225 int* to_stride;
226 if (no_stride)
227 {
228 ccv_nnc_tensor_get_stride(tensor_variable->info.dim, stride_from_dim);
229 to_stride = stride_from_dim;
230 } else
231 to_stride = tensor_variable->stride;
232 // If we provide stride, or reshape to a different size, assert the tensor variable itself is contiguous (otherwise we cannot satisfy the reshape requirements).
233 const int different_dim = ccv_nnc_tensor_nd(info.dim) != ccv_nnc_tensor_nd(tensor_variable->info.dim);
234 if (different_dim || (stride[0] != 0 && memcmp(stride, to_stride, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12)) != 0))
235 { assert(ccv_nnc_tensor_view_is_contiguous(tensor_variable->info.dim, to_stride))((void) sizeof ((ccv_nnc_tensor_view_is_contiguous(tensor_variable
->info.dim, to_stride)) ? 1 : 0), __extension__ ({ if (ccv_nnc_tensor_view_is_contiguous
(tensor_variable->info.dim, to_stride)) ; else __assert_fail
("ccv_nnc_tensor_view_is_contiguous(tensor_variable->info.dim, to_stride)"
, "ccv_nnc_dynamic_graph.c", 235, __extension__ __PRETTY_FUNCTION__
); }))
; }
236 // Need to compute alias off, that is the alias off of the tensor variable plus its ofs.
237 const off_t off = ccv_nnc_tensor_view_offset(tensor_variable->info.datatype, to_stride, tensor_variable->ofs);
238 variable_alias->alias_off = tensor_variable->alias_off + off;
239 // If we don't provide stride, copy the stride from previous variable.
240 if (stride[0] == 0)
241 {
242 if (different_dim)
243 ccv_nnc_tensor_get_stride(info.dim, variable_alias->stride);
244 else
245 memcpy(variable_alias->stride, to_stride, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
246 } else
247 memcpy(variable_alias->stride, stride, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
248 } else {
249 variable_alias->alias_index_ref = tensor_variable->index + 1;
250 variable_alias->alias_off = 0;
251 memcpy(variable_alias->stride, stride, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
252 }
253 variable_alias->info = info;
254 variable_alias->symbol = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
255 variable_alias->destructor_hook.func = 0;
256 variable_alias->destructor_hook.context = 0;
257 variable_alias->tensor_view = 0;
258 memcpy(variable_alias->ofs, ofs, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
259 if (graph->reuse_var >= 0)
260 {
261 const int reuse_var = graph->reuse_var;
262 assert(reuse_var < graph->vars->rnum)((void) sizeof ((reuse_var < graph->vars->rnum) ? 1 :
0), __extension__ ({ if (reuse_var < graph->vars->rnum
) ; else __assert_fail ("reuse_var < graph->vars->rnum"
, "ccv_nnc_dynamic_graph.c", 262, __extension__ __PRETTY_FUNCTION__
); }))
;
263 variable_alias->index = reuse_var;
264 *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, reuse_var)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(reuse_var)))
= variable_alias;
265 int i;
266 graph->reuse_var = -1;
267 for (i = reuse_var + 1; i < graph->vars->rnum && graph->reuse_var < 0; i++)
268 if (*(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, i)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(i)))
== 0)
269 graph->reuse_var = i;
270 } else {
271 variable_alias->index = graph->vars->rnum;
272 ccv_array_push(graph->vars, &variable_alias);
273 }
274 return variable_alias;
275}
276
277int ccv_nnc_tensor_variable_alias_params(const ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, int ofs[CCV_NNC_MAX_DIM_ALLOC(12)], int stride[CCV_NNC_MAX_DIM_ALLOC(12)])
278{
279 if (!tensor_variable->alias_index_ref)
280 return -1;
281 if (ofs)
282 memcpy(ofs, tensor_variable->ofs, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
283 if (stride)
284 memcpy(stride, tensor_variable->stride, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
285 return 0;
286}
287
288ccv_nnc_tensor_t* ccv_nnc_tensor_from_variable_impl(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, ccv_nnc_stream_context_t* const stream_context)
289{
290 if (tensor_variable->tensor_view)
291 {
292 if (tensor_variable->alias_index_ref)
293 {
294 const int alias_index = tensor_variable->alias_index_ref - 1;
295 assert(alias_index >= 0)((void) sizeof ((alias_index >= 0) ? 1 : 0), __extension__
({ if (alias_index >= 0) ; else __assert_fail ("alias_index >= 0"
, "ccv_nnc_dynamic_graph.c", 295, __extension__ __PRETTY_FUNCTION__
); }))
;
296 ccv_nnc_tensor_variable_t variable_to = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, alias_index)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(alias_index)))
;
297 if (CCV_IS_TENSOR_VIEW(tensor_variable->tensor_view)((*(int*)(tensor_variable->tensor_view)) & CCV_TENSOR_VIEW
)
)
298 {
299 ccv_nnc_tensor_view_t* const tv = tensor_variable->tensor_view;
300 // We cannot have an alias with custom set tensor, otherwise the pointer update is invalid.
301 assert(!CCV_NNC_IS_EXTERN_TENSOR_VIEW(tv))((void) sizeof ((!((uintptr_t)(tv) & 1)) ? 1 : 0), __extension__
({ if (!((uintptr_t)(tv) & 1)) ; else __assert_fail ("!CCV_NNC_IS_EXTERN_TENSOR_VIEW(tv)"
, "ccv_nnc_dynamic_graph.c", 301, __extension__ __PRETTY_FUNCTION__
); }))
;
302 // Update the tensor_view pointer every time access it, because the underlying variable it alias to have changed.
303 ccv_nnc_tensor_data(CCV_NNC_TENSOR_VIEW(variable_to->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(variable_to->tensor_view
) & ~(uintptr_t)1))
->info, CCV_NNC_TENSOR_VIEW(variable_to->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(variable_to->tensor_view
) & ~(uintptr_t)1))
->data.u8, tv->off + tensor_variable->alias_off, &tv->data, &tv->dataof);
304 } else {
305 ccv_nnc_tensor_t* const tv = (ccv_nnc_tensor_t*)tensor_variable->tensor_view;
306 // We cannot have an alias with custom set tensor, otherwise the pointer update is invalid.
307 assert(!CCV_NNC_IS_EXTERN_TENSOR_VIEW(tv))((void) sizeof ((!((uintptr_t)(tv) & 1)) ? 1 : 0), __extension__
({ if (!((uintptr_t)(tv) & 1)) ; else __assert_fail ("!CCV_NNC_IS_EXTERN_TENSOR_VIEW(tv)"
, "ccv_nnc_dynamic_graph.c", 307, __extension__ __PRETTY_FUNCTION__
); }))
;
308 // Update the tensor_view pointer every time access it, because the underlying variable it alias to have changed.
309 ccv_nnc_tensor_data(CCV_NNC_TENSOR_VIEW(variable_to->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(variable_to->tensor_view
) & ~(uintptr_t)1))
->info, CCV_NNC_TENSOR_VIEW(variable_to->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(variable_to->tensor_view
) & ~(uintptr_t)1))
->data.u8, tensor_variable->alias_off, &tv->data, &tv->dataof);
310 }
311 }
312 return (ccv_nnc_tensor_t*)CCV_NNC_TENSOR_VIEW(tensor_variable->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(tensor_variable->tensor_view
) & ~(uintptr_t)1))
;
313 }
314 if (!tensor_variable->alias_index_ref)
315 {
316 // If we haven't allocated tensor_variable, we cannot allocate them now (because no shape specified), return 0.
317 if (ccv_nnc_is_tensor_auto(tensor_variable->info))
318 return 0;
319 void* ptr = 0;
320 const size_t data_size = ccv_nnc_tensor_data_size(tensor_variable->info);
321 if (CCV_TENSOR_GET_MEMORY(tensor_variable->info.type)((tensor_variable->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY && data_size > 0)
322 ptr = ccv_nnc_xpu_alloc(&graph->xpu_alloc, CCV_TENSOR_GET_DEVICE_ID(tensor_variable->info.type)(((tensor_variable->info.type) & 0xfff00) >> 8), stream_context, data_size);
323 tensor_variable->tensor_view = (ccv_nnc_tensor_view_t*)ccv_nnc_tensor_new(ptr, tensor_variable->info, 0);
324 if (tensor_variable->info.dim[0] > 0)
325 { assert(tensor_variable->tensor_view->data.u8)((void) sizeof ((tensor_variable->tensor_view->data.u8)
? 1 : 0), __extension__ ({ if (tensor_variable->tensor_view
->data.u8) ; else __assert_fail ("tensor_variable->tensor_view->data.u8"
, "ccv_nnc_dynamic_graph.c", 325, __extension__ __PRETTY_FUNCTION__
); }))
; }
326 return (ccv_nnc_tensor_t*)tensor_variable->tensor_view;
327 }
328 const int alias_index = tensor_variable->alias_index_ref - 1;
329 assert(alias_index >= 0)((void) sizeof ((alias_index >= 0) ? 1 : 0), __extension__
({ if (alias_index >= 0) ; else __assert_fail ("alias_index >= 0"
, "ccv_nnc_dynamic_graph.c", 329, __extension__ __PRETTY_FUNCTION__
); }))
;
330 ccv_nnc_tensor_variable_t variable_to = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, alias_index)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(alias_index)))
;
331 assert(!variable_to->alias_index_ref)((void) sizeof ((!variable_to->alias_index_ref) ? 1 : 0), __extension__
({ if (!variable_to->alias_index_ref) ; else __assert_fail
("!variable_to->alias_index_ref", "ccv_nnc_dynamic_graph.c"
, 331, __extension__ __PRETTY_FUNCTION__); }))
;
332 if (!variable_to->tensor_view)
333 {
334 // If we haven't allocated variable_to, we cannot allocate them now (because no shape specified), return 0.
335 if (ccv_nnc_is_tensor_auto(variable_to->info))
336 return 0;
337 void* ptr = 0;
338 assert(variable_to->info.type == tensor_variable->info.type)((void) sizeof ((variable_to->info.type == tensor_variable
->info.type) ? 1 : 0), __extension__ ({ if (variable_to->
info.type == tensor_variable->info.type) ; else __assert_fail
("variable_to->info.type == tensor_variable->info.type"
, "ccv_nnc_dynamic_graph.c", 338, __extension__ __PRETTY_FUNCTION__
); }))
;
339 const size_t data_size = ccv_nnc_tensor_data_size(variable_to->info);
340 if (CCV_TENSOR_GET_MEMORY(variable_to->info.type)((variable_to->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY && data_size > 0)
341 ptr = ccv_nnc_xpu_alloc(&graph->xpu_alloc, CCV_TENSOR_GET_DEVICE_ID(variable_to->info.type)(((variable_to->info.type) & 0xfff00) >> 8), stream_context, data_size);
342 variable_to->tensor_view = (ccv_nnc_tensor_view_t*)ccv_nnc_tensor_new(ptr, variable_to->info, 0);
343 assert(variable_to->tensor_view->data.u8)((void) sizeof ((variable_to->tensor_view->data.u8) ? 1
: 0), __extension__ ({ if (variable_to->tensor_view->data
.u8) ; else __assert_fail ("variable_to->tensor_view->data.u8"
, "ccv_nnc_dynamic_graph.c", 343, __extension__ __PRETTY_FUNCTION__
); }))
;
344 }
345 int i;
346 int no_ofs = 1;
347 for (i = 0; no_ofs && i < CCV_NNC_MAX_DIM_ALLOC(12); i++)
348 no_ofs = (tensor_variable->ofs[i] == 0);
349 int no_stride = 1;
350 for (i = 0; no_stride && i < CCV_NNC_MAX_DIM_ALLOC(12); i++)
351 no_stride = (tensor_variable->stride[i] == 0);
352 int stride_is_packed = no_stride;
353 if (!no_stride) // We have stride, now if it is packed.
354 stride_is_packed = ccv_nnc_is_tensor_stride_packed(tensor_variable->stride, tensor_variable->info.dim);
355 assert(CCV_GET_DATA_TYPE_SIZE(tensor_variable->info.datatype) * ccv_nnc_tensor_count(tensor_variable->info) + tensor_variable->alias_off <= CCV_GET_DATA_TYPE_SIZE(variable_to->info.datatype) * ccv_nnc_tensor_count(variable_to->info))((void) sizeof ((_ccv_get_data_type_size[((tensor_variable->
info.datatype) & 0xFF000) >> 12] * ccv_nnc_tensor_count
(tensor_variable->info) + tensor_variable->alias_off <=
_ccv_get_data_type_size[((variable_to->info.datatype) &
0xFF000) >> 12] * ccv_nnc_tensor_count(variable_to->
info)) ? 1 : 0), __extension__ ({ if (_ccv_get_data_type_size
[((tensor_variable->info.datatype) & 0xFF000) >>
12] * ccv_nnc_tensor_count(tensor_variable->info) + tensor_variable
->alias_off <= _ccv_get_data_type_size[((variable_to->
info.datatype) & 0xFF000) >> 12] * ccv_nnc_tensor_count
(variable_to->info)) ; else __assert_fail ("CCV_GET_DATA_TYPE_SIZE(tensor_variable->info.datatype) * ccv_nnc_tensor_count(tensor_variable->info) + tensor_variable->alias_off <= CCV_GET_DATA_TYPE_SIZE(variable_to->info.datatype) * ccv_nnc_tensor_count(variable_to->info)"
, "ccv_nnc_dynamic_graph.c", 355, __extension__ __PRETTY_FUNCTION__
); }))
;
356 // Allowing vector type to be normal tensor, rather than a tensor view. We cannot have any offset though.
357 if (no_ofs && !stride_is_packed)
358 stride_is_packed = ccv_nnc_tensor_view_is_contiguous(tensor_variable->info.dim, tensor_variable->stride);
359 if (no_ofs && stride_is_packed)
360 tensor_variable->tensor_view = (ccv_nnc_tensor_view_t*)ccv_nnc_tensor_new(CCV_NNC_TENSOR_VIEW(variable_to->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(variable_to->tensor_view
) & ~(uintptr_t)1))
->data.u8, tensor_variable->info, 0);
361 else {
362 if (no_stride)
363 ccv_nnc_tensor_get_stride(tensor_variable->info.dim, tensor_variable->stride);
364 tensor_variable->tensor_view = ccv_nnc_tensor_view_new((ccv_nnc_tensor_t*)CCV_NNC_TENSOR_VIEW(variable_to->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(variable_to->tensor_view
) & ~(uintptr_t)1))
, tensor_variable->info, tensor_variable->ofs, tensor_variable->stride);
365 }
366 if (tensor_variable->alias_off)
367 ccv_nnc_tensor_data_add(tensor_variable->tensor_view->info, tensor_variable->alias_off, &tensor_variable->tensor_view->data, &tensor_variable->tensor_view->dataof);
368 return (ccv_nnc_tensor_t*)tensor_variable->tensor_view;
369}
370
371static void _ccv_nnc_tensor_symbol_extra_new(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, const ccv_nnc_tensor_symbol_t symbol)
372{
373 if (symbol.d >= graph->binds->rnum)
374 {
375 const int rnum = graph->binds->rnum;
376 ccv_array_resize(graph->binds, symbol.d + 1);
377 int i;
378 for (i = rnum; i < graph->binds->rnum; i++)
379 ((ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, i)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(i)))
)->index = CCV_NNC_TENSOR_NO_VARIABLE;
380 }
381 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(symbol.d)))
;
382 bind->type = tensor_variable->type;
383 bind->index = tensor_variable->index;
384 if (tensor_variable->alias_index_ref)
385 {
386 const ccv_nnc_tensor_symbol_t alias_to = ccv_nnc_tensor_symbol_alias_to(graph->tape, (ccv_nnc_tensor_symbol_t){
387 .d = symbol.d,
388 .graph = graph->tape
389 });
390 assert(alias_to.d >= 0 && alias_to.d < graph->binds->rnum)((void) sizeof ((alias_to.d >= 0 && alias_to.d <
graph->binds->rnum) ? 1 : 0), __extension__ ({ if (alias_to
.d >= 0 && alias_to.d < graph->binds->rnum
) ; else __assert_fail ("alias_to.d >= 0 && alias_to.d < graph->binds->rnum"
, "ccv_nnc_dynamic_graph.c", 390, __extension__ __PRETTY_FUNCTION__
); }))
;
391 bind->alias_ref = alias_to.d + 1;
392 } else
393 bind->alias_ref = 0;
394 if (bind->sources)
395 ccv_array_free(bind->sources);
396 bind->sources = 0;
397 if (bind->destinations)
398 ccv_array_free(bind->destinations);
399 bind->destinations = 0;
400 bind->destructor_hook.func = 0;
401 bind->destructor_hook.context = 0;
402 bind->tensor_view = 0;
403}
404
405static ccv_nnc_tensor_symbol_t _ccv_nnc_tensor_symbol_from_variable(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
406{
407 if (tensor_variable->symbol.d >= 0)
408 return tensor_variable->symbol;
409 if (!tensor_variable->alias_index_ref)
410 {
411 const ccv_nnc_tensor_symbol_t symbol = tensor_variable->symbol = ccv_nnc_tensor_symbol_new(graph->tape, tensor_variable->info, 0);
412 _ccv_nnc_tensor_symbol_extra_new(graph, tensor_variable, symbol);
413 return symbol;
414 }
415 const int alias_index = tensor_variable->alias_index_ref - 1;
416 assert(alias_index >= 0)((void) sizeof ((alias_index >= 0) ? 1 : 0), __extension__
({ if (alias_index >= 0) ; else __assert_fail ("alias_index >= 0"
, "ccv_nnc_dynamic_graph.c", 416, __extension__ __PRETTY_FUNCTION__
); }))
;
417 ccv_nnc_tensor_variable_t variable_to = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, alias_index)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(alias_index)))
;
418 assert(!variable_to->alias_index_ref)((void) sizeof ((!variable_to->alias_index_ref) ? 1 : 0), __extension__
({ if (!variable_to->alias_index_ref) ; else __assert_fail
("!variable_to->alias_index_ref", "ccv_nnc_dynamic_graph.c"
, 418, __extension__ __PRETTY_FUNCTION__); }))
;
419 int no_stride = 1;
420 int i;
421 for (i = 0; no_stride && i < CCV_NNC_MAX_DIM_ALLOC(12); i++)
422 no_stride = (tensor_variable->stride[i] == 0);
423 if (no_stride)
424 ccv_nnc_tensor_get_stride(tensor_variable->info.dim, tensor_variable->stride);
425 const ccv_nnc_tensor_symbol_t symbol = tensor_variable->symbol = ccv_nnc_tensor_symbol_alias_new(graph->tape, _ccv_nnc_tensor_symbol_from_variable(graph, variable_to), tensor_variable->ofs, tensor_variable->stride, tensor_variable->info, 0);
426 _ccv_nnc_tensor_symbol_extra_new(graph, tensor_variable, symbol);
427 return symbol;
428}
429
430// Return the tensor variable that is old (the provided tensor variable will have a new setting).
431ccv_nnc_tensor_variable_t ccv_nnc_tensor_variable_exchange_new(ccv_nnc_dynamic_graph_t* const graph, ccv_nnc_tensor_variable_t tensor_variable)
432{
433 struct ccv_nnc_tensor_variable_s x = *tensor_variable;
434 ccv_nnc_tensor_variable_t new_variable;
435 // Need to handle alias.
436 if (x.alias_index_ref)
437 new_variable = ccv_nnc_tensor_variable_alias_new(graph, *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, x.alias_index_ref - 1)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(x.alias_index_ref - 1)))
, x.ofs, x.stride, x.info);
438 else
439 new_variable = ccv_nnc_tensor_variable_new(graph, x.info)ccv_nnc_tensor_variable_new_impl(graph, x.info);
440 *tensor_variable = *new_variable;
441 *new_variable = x;
442 // The index should be the same though.
443 const int index = new_variable->index;
444 new_variable->index = tensor_variable->index;
445 if (new_variable->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL)
446 {
447 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, new_variable->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(new_variable->symbol.d)))
;
448 bind->index = new_variable->index;
449 }
450 tensor_variable->index = index;
451 return new_variable;
452}
453
454void ccv_nnc_dynamic_graph_set_max_concurrency(ccv_nnc_dynamic_graph_t* const dynamic_graph, const int max_stream_count)
455{
456 dynamic_graph->max_stream_count = max_stream_count;
457}
458
459int ccv_nnc_dynamic_graph_set_no_grad(ccv_nnc_dynamic_graph_t* const dynamic_graph, const int no_grad)
460{
461 if (dynamic_graph->no_grad == no_grad)
462 return -1;
463 dynamic_graph->no_grad = no_grad;
464 return 0;
465}
466
467static ccv_nnc_stream_context_t* _ccv_nnc_dynamic_graph_get_stream(ccv_nnc_dynamic_graph_t* const graph, const int type)
468{
469 if (!graph->stream_map)
470 graph->stream_map = kh_init(stream_map)kh_init_stream_map();
471 int ret = 0;
472 khiter_t k = kh_put(stream_map, graph->stream_map, type, &ret)kh_put_stream_map(graph->stream_map, type, &ret);
473 assert(ret >= 0)((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (
ret >= 0) ; else __assert_fail ("ret >= 0", "ccv_nnc_dynamic_graph.c"
, 473, __extension__ __PRETTY_FUNCTION__); }))
;
474 ccv_nnc_stream_context_t* stream = kh_val(graph->stream_map, k)((graph->stream_map)->vals[k]);
475 // If ret == 0, the key already exist, we can return directly, otherwise, create and return.
476 if (ret != 0)
477 {
478 stream = ccv_nnc_stream_context_new(type);
479 kh_val(graph->stream_map, k)((graph->stream_map)->vals[k]) = stream;
480 }
481 return stream;
482}
483
484typedef struct {
485 ccv_nnc_dynamic_graph_t* graph;
486 int stream_type;
487} ccv_nnc_dynamic_graph_neighbor_context_discovery_t;
488
489static ccv_nnc_stream_context_t* _ccv_nnc_dynamic_graph_neighbor_context_discovery(const int device_id, void* const context)
490{
491 ccv_nnc_dynamic_graph_neighbor_context_discovery_t* const discovery = (ccv_nnc_dynamic_graph_neighbor_context_discovery_t*)context;
492 int type = discovery->stream_type;
493 CCV_STREAM_SET_DEVICE_ID(type, device_id)(type) = (((type) & ~0xfff00) | (((device_id) & 0xfff
) << 8))
;
494 return _ccv_nnc_dynamic_graph_get_stream(discovery->graph, type);
495}
496
497void ccv_nnc_dynamic_graph_exec_ret(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, const ccv_nnc_tensor_variable_t* const inputs, const int input_size, ccv_nnc_tensor_variable_t* const outputs, const int output_size, const int parallel, ccv_nnc_stream_context_t* const stream_context, ccv_nnc_graph_exec_symbol_t* const graph_execs)
498{
499 int i, j;
500 for (i = 0; i < input_size; i++)
1
Assuming 'i' is >= 'input_size'
2
Loop condition is false. Execution continues on line 503
501 if (inputs[i] && !inputs[i]->alias_index_ref)
502 { assert(inputs[i]->tensor_view)((void) sizeof ((inputs[i]->tensor_view) ? 1 : 0), __extension__
({ if (inputs[i]->tensor_view) ; else __assert_fail ("inputs[i]->tensor_view"
, "ccv_nnc_dynamic_graph.c", 502, __extension__ __PRETTY_FUNCTION__
); }))
; }
503 ccv_nnc_tensor_t* input_tensors[ccv_max(1, input_size)({ typeof (1) _a = (1); typeof (input_size) _b = (input_size)
; (_a > _b) ? _a : _b; })
];
3
'?' condition is true
504 for (i = 0; i
3.1
'i' is >= 'input_size'
< input_size; i++)
4
Loop condition is false. Execution continues on line 506
505 input_tensors[i] = inputs[i] ? ccv_nnc_tensor_from_variable(graph, inputs[i], stream_context)ccv_nnc_tensor_from_variable_impl(graph, inputs[i], stream_context
)
: 0;
506 ccv_nnc_tensor_symbol_t input_symbols[ccv_max(1, input_size)({ typeof (1) _a = (1); typeof (input_size) _b = (input_size)
; (_a > _b) ? _a : _b; })
];
5
'?' condition is true
507 for (i = 0; i
5.1
'i' is >= 'input_size'
< input_size; i++)
6
Loop condition is false. Execution continues on line 509
508 input_symbols[i] = inputs[i] ? _ccv_nnc_tensor_symbol_from_variable(graph, inputs[i]) : NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
509 ccv_array_t* input_sources[ccv_max(1, input_size)({ typeof (1) _a = (1); typeof (input_size) _b = (input_size)
; (_a > _b) ? _a : _b; })
];
7
'?' condition is true
510 ccv_array_t* input_alias_sources[ccv_max(1, input_size)({ typeof (1) _a = (1); typeof (input_size) _b = (input_size)
; (_a > _b) ? _a : _b; })
];
8
'?' condition is true
511 for (i = 0; i
8.1
'i' is >= 'input_size'
< input_size; i++)
9
Loop condition is false. Execution continues on line 523
512 {
513 input_sources[i] = input_symbols[i].d != CCV_NNC_NO_TENSOR_SYMBOL ? ((ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, input_symbols[i].d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(input_symbols[i].d)))
)->sources : 0;
514 if (inputs[i] && inputs[i]->alias_index_ref)
515 {
516 const int alias_index_ref = inputs[i]->alias_index_ref - 1;
517 assert(alias_index_ref >= 0)((void) sizeof ((alias_index_ref >= 0) ? 1 : 0), __extension__
({ if (alias_index_ref >= 0) ; else __assert_fail ("alias_index_ref >= 0"
, "ccv_nnc_dynamic_graph.c", 517, __extension__ __PRETTY_FUNCTION__
); }))
;
518 ccv_nnc_tensor_variable_t variable_to = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, alias_index_ref)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(alias_index_ref)))
;
519 input_alias_sources[i] = ((ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, variable_to->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(variable_to->symbol.d)))
)->sources;
520 } else
521 input_alias_sources[i] = 0;
522 }
523 const int parallel_count = ccv_max(1, parallel)({ typeof (1) _a = (1); typeof (parallel) _b = (parallel); (_a
> _b) ? _a : _b; })
;
10
Assuming '_a' is <= '_b'
11
'?' condition is false
524 assert(input_size % parallel_count == 0)((void) sizeof ((input_size % parallel_count == 0) ? 1 : 0), __extension__
({ if (input_size % parallel_count == 0) ; else __assert_fail
("input_size % parallel_count == 0", "ccv_nnc_dynamic_graph.c"
, 524, __extension__ __PRETTY_FUNCTION__); }))
;
12
Assuming the condition is true
13
Taking true branch
525 const int per_input_size = input_size / parallel_count;
526 assert(output_size % parallel_count == 0)((void) sizeof ((output_size % parallel_count == 0) ? 1 : 0),
__extension__ ({ if (output_size % parallel_count == 0) ; else
__assert_fail ("output_size % parallel_count == 0", "ccv_nnc_dynamic_graph.c"
, 526, __extension__ __PRETTY_FUNCTION__); }))
;
14
Assuming the condition is true
15
Taking true branch
527 const int per_output_size = output_size / parallel_count;
528 int output_auto = 0;
529 for (i = 0; !output_auto
15.1
'output_auto' is 0
&& i < output_size; i++)
16
Assuming 'i' is >= 'output_size'
17
Loop condition is false. Execution continues on line 532
530 output_auto = outputs[i] ? ccv_nnc_is_tensor_auto(outputs[i]->info) : 0;
531 // One extra step, infer the parameters for outputs.
532 if (output_auto
17.1
'output_auto' is 0
)
18
Taking false branch
533 {
534 ccv_nnc_tensor_param_t input_params[ccv_max(1, per_input_size)({ typeof (1) _a = (1); typeof (per_input_size) _b = (per_input_size
); (_a > _b) ? _a : _b; })
];
535 ccv_nnc_tensor_param_t output_params[ccv_max(1, per_output_size)({ typeof (1) _a = (1); typeof (per_output_size) _b = (per_output_size
); (_a > _b) ? _a : _b; })
];
536 for (i = 0; i < parallel_count; i++)
537 {
538 for (j = 0; j < per_input_size; j++)
539 input_params[j] = inputs[j + i * per_input_size] ? inputs[j + i * per_input_size]->info : ccv_nnc_tensor_auto;
540 for (j = 0; j < per_output_size; j++)
541 output_params[j] = outputs[j + i * per_output_size] ? outputs[j + i * per_output_size]->info : ccv_nnc_tensor_auto;
542 ccv_nnc_hint_tensor_auto(cmd, input_params, per_input_size, hint, output_params, per_output_size);
543 for (j = 0; j < per_output_size; j++)
544 if (outputs[j + i * per_output_size])
545 outputs[j + i * per_output_size]->info = output_params[j];
546 }
547 }
548 int freeable_size = 0;
549 ccv_nnc_tensor_variable_t freeables[ccv_max(1, output_size)({ typeof (1) _a = (1); typeof (output_size) _b = (output_size
); (_a > _b) ? _a : _b; })
];
19
'?' condition is true
550 // Refresh the symbol if it is binded to an existing exec. Otherwise we cannot keep the SSA guarantee.
551 for (i = 0; i
19.1
'i' is >= 'output_size'
< output_size; i++)
20
Loop condition is false. Execution continues on line 579
552 {
553 // First, go over to see whether there is enforce inplace.
554 int enforce_idx = -1;
555 for (j = 0; enforce_idx < 0 && j < input_size; j++)
556 if (inputs[j] && ccv_nnc_cmd_enforce_inplace(cmd, j, input_size, i, output_size))
557 enforce_idx = j;
558 if (enforce_idx >= 0)
559 { assert(outputs[i] == inputs[enforce_idx] && outputs[i]->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL)((void) sizeof ((outputs[i] == inputs[enforce_idx] &&
outputs[i]->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL) ? 1 : 0
), __extension__ ({ if (outputs[i] == inputs[enforce_idx] &&
outputs[i]->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL) ; else __assert_fail
("outputs[i] == inputs[enforce_idx] && outputs[i]->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL"
, "ccv_nnc_dynamic_graph.c", 559, __extension__ __PRETTY_FUNCTION__
); }))
; }
560 // We don't allow or check "allow inplace" yet. That logic will be at odds with backward logic.
561 if (outputs[i] && outputs[i]->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL)
562 {
563 const ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, outputs[i]->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(outputs[i]->symbol.d)))
;
564 if (enforce_idx >= 0)
565 { assert(!bind->destinations || bind->destinations->rnum == 0)((void) sizeof ((!bind->destinations || bind->destinations
->rnum == 0) ? 1 : 0), __extension__ ({ if (!bind->destinations
|| bind->destinations->rnum == 0) ; else __assert_fail
("!bind->destinations || bind->destinations->rnum == 0"
, "ccv_nnc_dynamic_graph.c", 565, __extension__ __PRETTY_FUNCTION__
); }))
; }
566 if (bind->sources && bind->sources->rnum > 0)
567 {
568 const ccv_nnc_tensor_variable_t old_var = freeables[freeable_size++] = ccv_nnc_tensor_variable_exchange_new(graph, outputs[i]);
569 // If this is enforce output, make sure the tensor view is taken by the output.
570 if (enforce_idx >= 0)
571 {
572 outputs[i]->destructor_hook = old_var->destructor_hook;
573 outputs[i]->tensor_view = old_var->tensor_view; // Make sure the tensor view is taken over by the output.
574 old_var->tensor_view = 0;
575 }
576 }
577 }
578 }
579 ccv_nnc_tensor_t* output_tensors[ccv_max(1, per_output_size)({ typeof (1) _a = (1); typeof (per_output_size) _b = (per_output_size
); (_a > _b) ? _a : _b; })
];
21
Assuming '_a' is <= '_b'
22
'?' condition is false
580 if (parallel_count > 1)
23
Assuming 'parallel_count' is > 1
24
Taking true branch
581 {
582 const int max_device_id_size = per_input_size + per_output_size;
583 assert(max_device_id_size > 0)((void) sizeof ((max_device_id_size > 0) ? 1 : 0), __extension__
({ if (max_device_id_size > 0) ; else __assert_fail ("max_device_id_size > 0"
, "ccv_nnc_dynamic_graph.c", 583, __extension__ __PRETTY_FUNCTION__
); }))
;
25
Assuming 'max_device_id_size' is > 0
26
Taking true branch
584 int device_ids[max_device_id_size];
585 ccv_nnc_stream_context_t* streams[parallel_count];
586 ccv_nnc_stream_signal_t* signal;
587 if (stream_context)
27
Assuming 'stream_context' is null
28
Taking false branch
588 signal = ccv_nnc_stream_context_emit_signal_new(stream_context);
589 for (i = 0; i
28.1
'i' is < 'parallel_count'
< parallel_count; i++)
29
Loop condition is true. Entering loop body
590 {
591 int flag = 0;
592 for (j = 0; !flag
29.1
'flag' is 0
&& j < per_input_size; j++)
30
Assuming 'j' is < 'per_input_size'
31
Loop condition is true. Entering loop body
593 if (input_tensors[i * per_input_size + j])
32
Branch condition evaluates to a garbage value
594 flag = (CCV_TENSOR_GET_MEMORY(input_tensors[i * per_input_size + j]->info.type)((input_tensors[i * per_input_size + j]->info.type) & 0x3
)
== CCV_TENSOR_GPU_MEMORY);
595 for (j = 0; j < per_output_size; j++)
596 {
597 output_tensors[j] = outputs[j + i * per_output_size] ? ccv_nnc_tensor_from_variable(graph, outputs[j + i * per_output_size], stream_context)ccv_nnc_tensor_from_variable_impl(graph, outputs[j + i * per_output_size
], stream_context)
: 0;
598 if (output_tensors[j] && !flag)
599 flag = (CCV_TENSOR_GET_MEMORY(output_tensors[j]->info.type)((output_tensors[j]->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY);
600 }
601 const int stream_type = flag ? CCV_STREAM_CONTEXT_GPU : CCV_STREAM_CONTEXT_CPU;
602 const int tensor_type = flag ? CCV_TENSOR_GPU_MEMORY : CCV_TENSOR_CPU_MEMORY;
603 const int device_id_size = ccv_nnc_device_ids_for_io(input_tensors + i * per_input_size, per_input_size, output_tensors, per_output_size, tensor_type, device_ids, max_device_id_size);
604 ccv_nnc_stream_context_t* stream_0 = 0;
605 for (j = 0; j < device_id_size; j++)
606 {
607 int type = stream_type;
608 CCV_STREAM_SET_DEVICE_ID(type, device_ids[j])(type) = (((type) & ~0xfff00) | (((device_ids[j]) & 0xfff
) << 8))
;
609 ccv_nnc_stream_context_t* const stream = _ccv_nnc_dynamic_graph_get_stream(graph, type);
610 if (!stream_0)
611 stream_0 = stream;
612 }
613 // Wait signal to finish.
614 if (stream_context)
615 {
616 if (stream_0)
617 ccv_nnc_stream_context_wait_signal(stream_0, signal);
618 else
619 ccv_nnc_stream_context_wait(stream_context);
620 }
621 if (stream_0)
622 {
623 ccv_nnc_dynamic_graph_neighbor_context_discovery_t discovery = {
624 .graph = graph,
625 .stream_type = stream_type
626 };
627 ccv_nnc_stream_context_set_neighbor_discovery(stream_0, _ccv_nnc_dynamic_graph_neighbor_context_discovery, &discovery);
628 }
629 PRINT(CCV_CLI_INFO, "%s: [%d] -> [%d]\n", ccv_nnc_cmd_name(cmd.cmd), per_input_size, per_output_size)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("%s: [%d] -> [%d]\n", ccv_nnc_cmd_name(cmd.cmd), per_input_size
, per_output_size); fflush(stdout); } } while (0)
;
630 int k;
631 for (k = 0; k < per_input_size; k++)
632 {
633 PRINT(CCV_CLI_INFO, "|-> %d. %p (%p:%d)", k + 1, input_tensors[k + i * per_input_size], (input_tensors[k + i * per_input_size] ? input_tensors[k + i * per_input_size]->data.u8 : 0), (input_tensors[k + i * per_input_size] ? CCV_TENSOR_GET_DEVICE_ID(input_tensors[k + i * per_input_size]->info.type) : -1))do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("|-> %d. %p (%p:%d)", k + 1, input_tensors[k + i * per_input_size
], (input_tensors[k + i * per_input_size] ? input_tensors[k +
i * per_input_size]->data.u8 : 0), (input_tensors[k + i *
per_input_size] ? (((input_tensors[k + i * per_input_size]->
info.type) & 0xfff00) >> 8) : -1)); fflush(stdout);
} } while (0)
;
634 if (input_tensors[k + i * per_input_size] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
635 ccv_nnc_print_tensor_info(input_tensors[k + i * per_input_size]);
636 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
637 }
638 for (k = 0; k < per_output_size; k++)
639 {
640 PRINT(CCV_CLI_INFO, "|<- %d. %p (%p:%d)", k + 1, output_tensors[k], (output_tensors[k] ? output_tensors[k]->data.u8 : 0), (output_tensors[k] ? CCV_TENSOR_GET_DEVICE_ID(output_tensors[k]->info.type) : -1))do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("|<- %d. %p (%p:%d)", k + 1, output_tensors[k], (output_tensors
[k] ? output_tensors[k]->data.u8 : 0), (output_tensors[k] ?
(((output_tensors[k]->info.type) & 0xfff00) >> 8
) : -1)); fflush(stdout); } } while (0)
;
641 if (output_tensors[k] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
642 ccv_nnc_print_tensor_shape(output_tensors[k]);
643 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
644 }
645 const int status = ccv_nnc_cmd_exec(cmd, hint, flags, input_tensors + i * per_input_size, per_input_size, output_tensors, per_output_size, stream_0);
646 if (status != 0)
647 PRINT(CCV_CLI_INFO, "Invalid Status: %d\n", status)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("Invalid Status: %d\n", status); fflush(stdout); } } while (
0)
;
648 if (CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_VERBOSE)(CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
649 {
650 for (k = 0; k < per_output_size; k++)
651 {
652 PRINT(CCV_CLI_VERBOSE, "POST: |<- %d. %p (%p:%d)", k + 1, output_tensors[k], (output_tensors[k] ? output_tensors[k]->data.u8 : 0), (output_tensors[k] ? CCV_TENSOR_GET_DEVICE_ID(output_tensors[k]->info.type) : -1))do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("POST: |<- %d. %p (%p:%d)", k + 1, output_tensors
[k], (output_tensors[k] ? output_tensors[k]->data.u8 : 0),
(output_tensors[k] ? (((output_tensors[k]->info.type) &
0xfff00) >> 8) : -1)); fflush(stdout); } } while (0)
;
653 if (output_tensors[k])
654 ccv_nnc_print_tensor_info(output_tensors[k]);
655 PRINT(CCV_CLI_VERBOSE, "\n")do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels()))
{ printf("\n"); fflush(stdout); } } while (0)
;
656 }
657 }
658 if (stream_context && stream_0)
659 {
660 ccv_nnc_stream_signal_t* const signal = ccv_nnc_stream_context_emit_signal_new(stream_0);
661 ccv_nnc_stream_context_wait_signal(stream_context, signal);
662 }
663 streams[i] = stream_0;
664 }
665 if (!stream_context)
666 for (i = 0; i < parallel_count; i++)
667 if (streams[i])
668 ccv_nnc_stream_context_wait(streams[i]);
669 } else {
670 for (i = 0; i < per_output_size; i++)
671 output_tensors[i] = outputs[i] ? ccv_nnc_tensor_from_variable(graph, outputs[i], stream_context)ccv_nnc_tensor_from_variable_impl(graph, outputs[i], stream_context
)
: 0;
672 PRINT(CCV_CLI_INFO, "%s: [%d] -> [%d]\n", ccv_nnc_cmd_name(cmd.cmd), per_input_size, per_output_size)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("%s: [%d] -> [%d]\n", ccv_nnc_cmd_name(cmd.cmd), per_input_size
, per_output_size); fflush(stdout); } } while (0)
;
673 for (i = 0; i < per_input_size; i++)
674 {
675 PRINT(CCV_CLI_INFO, "|-> %d. %p (%p:%d)", i + 1, input_tensors[i], (input_tensors[i] ? input_tensors[i]->data.u8 : 0), (input_tensors[i] ? CCV_TENSOR_GET_DEVICE_ID(input_tensors[i]->info.type) : -1))do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("|-> %d. %p (%p:%d)", i + 1, input_tensors[i], (input_tensors
[i] ? input_tensors[i]->data.u8 : 0), (input_tensors[i] ? (
((input_tensors[i]->info.type) & 0xfff00) >> 8) :
-1)); fflush(stdout); } } while (0)
;
676 if (input_tensors[i] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
677 ccv_nnc_print_tensor_info(input_tensors[i]);
678 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
679 }
680 ccv_nnc_cmd_exec(cmd, hint, flags, input_tensors, per_input_size, output_tensors, per_output_size, stream_context);
681 for (i = 0; i < per_output_size; i++)
682 {
683 PRINT(CCV_CLI_INFO, "|<- %d. %p (%p:%d)", i + 1, output_tensors[i], (output_tensors[i] ? output_tensors[i]->data.u8 : 0), (output_tensors[i] ? CCV_TENSOR_GET_DEVICE_ID(output_tensors[i]->info.type) : -1))do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("|<- %d. %p (%p:%d)", i + 1, output_tensors[i], (output_tensors
[i] ? output_tensors[i]->data.u8 : 0), (output_tensors[i] ?
(((output_tensors[i]->info.type) & 0xfff00) >> 8
) : -1)); fflush(stdout); } } while (0)
;
684 if (output_tensors[i] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
685 ccv_nnc_print_tensor_info(output_tensors[i]);
686 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
687 }
688 }
689 int inputs_are_constants = 1;
690 for (i = 0; inputs_are_constants && i < input_size; i++)
691 if (inputs[i] && inputs[i]->type != CCV_NNC_TENSOR_CONSTANT)
692 inputs_are_constants = 0;
693 if (input_size > 0 && !inputs_are_constants && !graph->no_grad) // No need to record the execution if there is no input or we disabled gradient computation.
694 {
695 ccv_nnc_tensor_symbol_t output_symbols[ccv_max(1, output_size)({ typeof (1) _a = (1); typeof (output_size) _b = (output_size
); (_a > _b) ? _a : _b; })
];
696 for (i = 0; i < output_size; i++)
697 if (outputs[i])
698 {
699 assert(outputs[i]->type != CCV_NNC_TENSOR_CONSTANT)((void) sizeof ((outputs[i]->type != CCV_NNC_TENSOR_CONSTANT
) ? 1 : 0), __extension__ ({ if (outputs[i]->type != CCV_NNC_TENSOR_CONSTANT
) ; else __assert_fail ("outputs[i]->type != CCV_NNC_TENSOR_CONSTANT"
, "ccv_nnc_dynamic_graph.c", 699, __extension__ __PRETTY_FUNCTION__
); }))
;
700 output_symbols[i] = _ccv_nnc_tensor_symbol_from_variable(graph, outputs[i]);
701 } else
702 output_symbols[i] = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
703 int t;
704 for (t = 0; t < parallel_count; t++)
705 {
706 ccv_nnc_graph_exec_symbol_t graph_exec = ccv_nnc_graph_exec_symbol_new(graph->tape, cmd, input_symbols + t * per_input_size, per_input_size, output_symbols + t * per_output_size, per_output_size, 0);
707 if (graph_execs)
708 graph_execs[t] = graph_exec;
709 // This needs to be done before we set the new sources on the outputs.
710 for (i = 0; i < per_input_size; i++)
711 {
712 ccv_array_t* const input_source = input_sources[i + t * per_input_size];
713 if (input_source)
714 for (j = 0; j < input_source->rnum; j++)
715 ccv_nnc_graph_exec_symbol_concat(graph->tape, (ccv_nnc_graph_exec_symbol_t){
716 .d = *(int*)ccv_array_get(input_source, j)((void*)(((char*)((input_source)->data)) + (size_t)(input_source
)->rsize * (size_t)(j)))
,
717 .graph = graph->tape
718 }, graph_exec);
719 ccv_array_t* const input_alias_source = input_alias_sources[i + t * per_input_size];
720 if (input_alias_source)
721 for (j = 0; j < input_alias_source->rnum; j++)
722 ccv_nnc_graph_exec_symbol_concat(graph->tape, (ccv_nnc_graph_exec_symbol_t){
723 .d = *(int*)ccv_array_get(input_alias_source, j)((void*)(((char*)((input_alias_source)->data)) + (size_t)(
input_alias_source)->rsize * (size_t)(j)))
,
724 .graph = graph->tape
725 }, graph_exec);
726 }
727 for (i = 0; i < per_input_size; i++)
728 {
729 ccv_nnc_tensor_variable_t const input = inputs[i + t * per_input_size];
730 if (!input || input_symbols[i + t * per_input_size].d == CCV_NNC_NO_TENSOR_SYMBOL)
731 continue;
732 // Constant inputs still need lifetime tracking while this exec is alive because
733 // backward may read their concrete tensor buffers even though they do not require
734 // gradients themselves.
735 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, input_symbols[i + t * per_input_size].d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(input_symbols[i + t * per_input_size
].d)))
;
736 if (!bind->destinations)
737 bind->destinations = ccv_array_new(sizeof(int), 1, 0);
738 ccv_array_add_unique_int(bind->destinations, graph_exec.d);
739 if (input->alias_index_ref)
740 {
741 const int alias_index = input->alias_index_ref - 1;
742 assert(alias_index >= 0)((void) sizeof ((alias_index >= 0) ? 1 : 0), __extension__
({ if (alias_index >= 0) ; else __assert_fail ("alias_index >= 0"
, "ccv_nnc_dynamic_graph.c", 742, __extension__ __PRETTY_FUNCTION__
); }))
;
743 ccv_nnc_tensor_variable_t variable_to = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, alias_index)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(alias_index)))
;
744 ccv_nnc_tensor_variable_graph_bind_t* const root_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, variable_to->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(variable_to->symbol.d)))
;
745 if (!root_bind->destinations)
746 root_bind->destinations = ccv_array_new(sizeof(int), 1, 0);
747 ccv_array_add_unique_int(root_bind->destinations, graph_exec.d);
748 }
749 }
750 for (i = 0; i < per_output_size; i++)
751 {
752 ccv_nnc_tensor_variable_t const output = outputs[i + t * per_output_size];
753 if (!output)
754 continue;
755 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, output_symbols[i + t * per_output_size].d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(output_symbols[i + t * per_output_size
].d)))
;
756 assert(!bind->sources)((void) sizeof ((!bind->sources) ? 1 : 0), __extension__ (
{ if (!bind->sources) ; else __assert_fail ("!bind->sources"
, "ccv_nnc_dynamic_graph.c", 756, __extension__ __PRETTY_FUNCTION__
); }))
; // This is a new symbol, therefore, no binded sources associated yet.
757 bind->sources = ccv_array_new(sizeof(int), 1, 0);
758 ccv_array_add_unique_int(bind->sources, graph_exec.d);
759 if (output->alias_index_ref)
760 {
761 const int alias_index = output->alias_index_ref - 1;
762 assert(alias_index >= 0)((void) sizeof ((alias_index >= 0) ? 1 : 0), __extension__
({ if (alias_index >= 0) ; else __assert_fail ("alias_index >= 0"
, "ccv_nnc_dynamic_graph.c", 762, __extension__ __PRETTY_FUNCTION__
); }))
;
763 ccv_nnc_tensor_variable_t variable_to = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, alias_index)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(alias_index)))
;
764 ccv_nnc_tensor_variable_graph_bind_t* const root_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, variable_to->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(variable_to->symbol.d)))
;
765 if (!root_bind->sources)
766 root_bind->sources = ccv_array_new(sizeof(int), 1, 0);
767 ccv_array_add_unique_int(root_bind->sources, graph_exec.d);
768 }
769 }
770 }
771 }
772 // Now, able to free some of the reused outputs.
773 for (i = 0; i < freeable_size; i++)
774 ccv_nnc_tensor_variable_free(graph, freeables[i]);
775}
776
777int ccv_nnc_dynamic_graph_exec(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_t hint, const int flags, const ccv_nnc_tensor_variable_t* const inputs, const int input_size, ccv_nnc_tensor_variable_t* const outputs, const int output_size, const int parallel, ccv_nnc_stream_context_t* const stream_context)
778{
779 ccv_nnc_dynamic_graph_exec_ret(graph, cmd, hint, flags, inputs, input_size, outputs, output_size, parallel, stream_context, 0);
780 return CCV_NNC_EXEC_SUCCESS;
781}
782
783static int _ccv_nnc_tensor_variable_is_only_output(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_graph_bind_t* bind, const int symbol_d)
784{
785 if (bind->alias_ref)
786 bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, bind->alias_ref - 1)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(bind->alias_ref - 1)))
;
787 if (!bind->sources || bind->sources->rnum == 0)
788 return 1;
789 int i;
790 for (i = 0; i < bind->sources->rnum; i++)
791 {
792 const int exec_symbol_d = *(int*)ccv_array_get(bind->sources, i)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(i)))
;
793 const ccv_nnc_graph_exec_symbol_t exec_symbol = {
794 .d = exec_symbol_d,
795 .graph = graph->tape
796 };
797 const int* outputs; int output_size;
798 ccv_nnc_graph_exec_symbol_io(graph->tape, exec_symbol, 0, 0, &outputs, &output_size);
799 int j;
800 for (j = 0; j < output_size; j++)
801 if (outputs[j] >= 0 && outputs[j] != symbol_d) // If output is me, it is the only output.
802 {
803 assert(outputs[j] < graph->binds->rnum)((void) sizeof ((outputs[j] < graph->binds->rnum) ? 1
: 0), __extension__ ({ if (outputs[j] < graph->binds->
rnum) ; else __assert_fail ("outputs[j] < graph->binds->rnum"
, "ccv_nnc_dynamic_graph.c", 803, __extension__ __PRETTY_FUNCTION__
); }))
;
804 const ccv_nnc_tensor_variable_graph_bind_t* other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, outputs[j])((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(outputs[j])))
;
805 // This is in use and is it not a constant symbol.
806 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
807 return 0;
808 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
809 other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, other_bind->alias_ref - 1)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(other_bind->alias_ref - 1
)))
;
810 // The original is in use and is it not a constant symbol.
811 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
812 return 0;
813 if (other_bind->destinations && other_bind->destinations->rnum > 0)
814 return 0;
815 }
816 }
817 return 1;
818}
819
820static void _ccv_nnc_update_bind_destinations_when_free(ccv_nnc_dynamic_graph_t* const graph, const int freed_exec_symbol_d, ccv_array_t* const binds, ccv_nnc_tensor_variable_graph_bind_t* const bind, const int tensor_index, ccv_array_t* const ws)
821{
822 int i;
823 if (bind->destinations)
824 {
825 int flag = 0;
826 for (i = 0; !flag && i < bind->destinations->rnum; i++)
827 {
828 const int exec_symbol_d = *(int*)ccv_array_get(bind->destinations, i)((void*)(((char*)((bind->destinations)->data)) + (size_t
)(bind->destinations)->rsize * (size_t)(i)))
;
829 if (exec_symbol_d == freed_exec_symbol_d)
830 {
831 if (i < bind->destinations->rnum - 1)
832 *(int*)ccv_array_get(bind->destinations, i)((void*)(((char*)((bind->destinations)->data)) + (size_t
)(bind->destinations)->rsize * (size_t)(i)))
= *(int*)ccv_array_get(bind->destinations, bind->destinations->rnum - 1)((void*)(((char*)((bind->destinations)->data)) + (size_t
)(bind->destinations)->rsize * (size_t)(bind->destinations
->rnum - 1)))
;
833 --bind->destinations->rnum;
834 flag = 1;
835 }
836 }
837 // This symbol can be freed.
838 if (flag && bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED)
839 {
840 ccv_nnc_tensor_variable_graph_bind_t* root_bind = bind;
841 if (bind->alias_ref)
842 {
843 root_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(binds, bind->alias_ref - 1)((void*)(((char*)((binds)->data)) + (size_t)(binds)->rsize
* (size_t)(bind->alias_ref - 1)))
;
844 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
845 root_bind = bind;
846 }
847 // If the alias_ref is not freed, we cannot free this, unless it is very clear there is no reference to this any more.
848 // It is possible because exec will be freed already, thus, it is safe to remove this alias out.
849 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED &&
850 ((!root_bind->sources || root_bind->sources->rnum == 0) || _ccv_nnc_tensor_variable_is_only_output(graph, bind, tensor_index)) &&
851 root_bind->destinations->rnum == 0)
852 {
853 if (root_bind->sources)
854 for (i = 0; i < root_bind->sources->rnum; i++)
855 ccv_array_add_unique_int(ws, *(int*)ccv_array_get(root_bind->sources, i)((void*)(((char*)((root_bind->sources)->data)) + (size_t
)(root_bind->sources)->rsize * (size_t)(i)))
);
856 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
857 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
858 .d = tensor_index,
859 .graph = graph->tape
860 });
861 } else if (bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED && // Handle the case the bind is already freed, and it doesn't have any sources or destinations.
862 bind->alias_ref && (!bind->sources || bind->sources->rnum == 0) && (!bind->destinations || bind->destinations->rnum == 0)) {
863 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
864 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
865 .d = tensor_index,
866 .graph = graph->tape
867 });
868 }
869 }
870 }
871}
872
873static void _ccv_nnc_update_bind_sources_when_free(ccv_nnc_dynamic_graph_t* const graph, const int freed_exec_symbol_d, ccv_array_t* const binds, ccv_nnc_tensor_variable_graph_bind_t* const bind, const int tensor_index, ccv_array_t* const ws)
874{
875 int i;
876 if (bind->sources)
877 {
878 int flag = 0;
879 for (i = 0; !flag && i < bind->sources->rnum; i++)
880 {
881 const int exec_symbol_d = *(int*)ccv_array_get(bind->sources, i)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(i)))
;
882 if (exec_symbol_d == freed_exec_symbol_d)
883 {
884 if (i < bind->sources->rnum - 1)
885 *(int*)ccv_array_get(bind->sources, i)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(i)))
= *(int*)ccv_array_get(bind->sources, bind->sources->rnum - 1)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(bind->sources->rnum -
1)))
;
886 --bind->sources->rnum;
887 flag = 1;
888 }
889 }
890 if (flag && !bind->alias_ref && bind->index >= 0 && bind->type == CCV_NNC_TENSOR_CONSTANT && // If it is detached (constant but previously has sources). Now can check again.
891 (bind->sources->rnum == 0 || _ccv_nnc_tensor_variable_is_only_output(graph, bind, tensor_index)) &&
892 (!bind->destinations || bind->destinations->rnum == 0))
893 {
894 // If this is constant, set it to be no symbol again.
895 ccv_nnc_tensor_variable_t tv = *(ccv_nnc_tensor_variable_t*)ccv_array_get(graph->vars, bind->index)((void*)(((char*)((graph->vars)->data)) + (size_t)(graph
->vars)->rsize * (size_t)(bind->index)))
;
896 tv->symbol = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
897 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
898 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
899 .d = tensor_index,
900 .graph = graph->tape
901 });
902 } else if (flag && bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED) {
903 // This symbol can be freed.
904 ccv_nnc_tensor_variable_graph_bind_t* root_bind = bind;
905 if (bind->alias_ref)
906 {
907 root_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(binds, bind->alias_ref - 1)((void*)(((char*)((binds)->data)) + (size_t)(binds)->rsize
* (size_t)(bind->alias_ref - 1)))
;
908 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
909 root_bind = bind;
910 }
911 // If the alias_ref is not freed, we cannot free this, unless it is very clear there is no reference to this any more.
912 // It is possible because exec will be freed already, thus, it is safe to remove this alias out.
913 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED &&
914 (root_bind->sources->rnum == 0 || _ccv_nnc_tensor_variable_is_only_output(graph, bind, tensor_index)) &&
915 (!root_bind->destinations || root_bind->destinations->rnum == 0))
916 {
917 for (i = 0; i < root_bind->sources->rnum; i++)
918 ccv_array_add_unique_int(ws, *(int*)ccv_array_get(root_bind->sources, i)((void*)(((char*)((root_bind->sources)->data)) + (size_t
)(root_bind->sources)->rsize * (size_t)(i)))
);
919 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
920 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
921 .d = tensor_index,
922 .graph = graph->tape
923 });
924 } else if (bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED && // Handle the case the bind is already freed, and it doesn't have any sources or destinations.
925 bind->alias_ref && (!bind->sources || bind->sources->rnum == 0) && (!bind->destinations || bind->destinations->rnum == 0)) {
926 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
927 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
928 .d = tensor_index,
929 .graph = graph->tape
930 });
931 }
932 }
933 }
934}
935
936static void _ccv_nnc_update_bind_sources_destinations_when_free(ccv_nnc_dynamic_graph_t* const graph, const int freed_exec_symbol_d, ccv_array_t* const binds, const int* const inputs, const int input_size, const int* const outputs, const int output_size, ccv_array_t* const ws)
937{
938 int i;
939 for (i = 0; i < input_size; i++)
940 if (inputs[i] >= 0 && inputs[i] < binds->rnum)
941 {
942 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(binds, inputs[i])((void*)(((char*)((binds)->data)) + (size_t)(binds)->rsize
* (size_t)(inputs[i])))
;
943 if (bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
944 continue;
945 if (bind->alias_ref)
946 {
947 const int alias_to = bind->alias_ref - 1;
948 ccv_nnc_tensor_variable_graph_bind_t* const root_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(binds, alias_to)((void*)(((char*)((binds)->data)) + (size_t)(binds)->rsize
* (size_t)(alias_to)))
;
949 if (root_bind && root_bind->index != CCV_NNC_TENSOR_NO_VARIABLE)
950 _ccv_nnc_update_bind_destinations_when_free(graph, freed_exec_symbol_d, binds, root_bind, alias_to, ws);
951 }
952 _ccv_nnc_update_bind_destinations_when_free(graph, freed_exec_symbol_d, binds, bind, inputs[i], ws);
953 }
954 // Note that this works because there is no overlap of inputs / outputs. (What about alias?).
955 for (i = 0; i < output_size; i++)
956 if (outputs[i] >= 0 && outputs[i] < binds->rnum)
957 {
958 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(binds, outputs[i])((void*)(((char*)((binds)->data)) + (size_t)(binds)->rsize
* (size_t)(outputs[i])))
;
959 if (bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
960 continue;
961 if (bind->alias_ref)
962 {
963 const int alias_to = bind->alias_ref - 1;
964 ccv_nnc_tensor_variable_graph_bind_t* const root_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(binds, alias_to)((void*)(((char*)((binds)->data)) + (size_t)(binds)->rsize
* (size_t)(alias_to)))
;
965 if (root_bind && root_bind->index != CCV_NNC_TENSOR_NO_VARIABLE)
966 _ccv_nnc_update_bind_sources_when_free(graph, freed_exec_symbol_d, binds, root_bind, alias_to, ws);
967 }
968 _ccv_nnc_update_bind_sources_when_free(graph, freed_exec_symbol_d, binds, bind, outputs[i], ws);
969 }
970}
971
972static void _ccv_nnc_stateful_exec_free_if_possible(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_graph_exec_symbol_t symbol)
973{
974 if (!graph->stateful_execs)
975 return;
976 assert(symbol.d >= 0)((void) sizeof ((symbol.d >= 0) ? 1 : 0), __extension__ ({
if (symbol.d >= 0) ; else __assert_fail ("symbol.d >= 0"
, "ccv_nnc_dynamic_graph.c", 976, __extension__ __PRETTY_FUNCTION__
); }))
;
977 ccv_array_t* const stateful_execs = graph->stateful_execs;
978 ccv_nnc_cmd_t cmd = ccv_nnc_graph_exec_symbol_cmd(graph->tape, symbol);
979 ccv_nnc_stateful_exec_t* const stateful_exec = (ccv_nnc_stateful_exec_t*)cmd.data;
980 if (!stateful_exec)
981 return;
982 // If there is no backward, no need to apply gradients.
983 // Otherwise, if we applied gradients, we can free it as well.
984 // We don't free this stateful exec because apply gradients doesn't require any variables alive.
985 if (!stateful_exec->did_backward_but_not_apply_gradients)
986 {
987 const int index = stateful_exec->index;
988 ccfreefree(stateful_exec);
989 if (index < graph->reuse_stateful_exec || graph->reuse_stateful_exec < 0)
990 graph->reuse_stateful_exec = index;
991 *(ccv_nnc_stateful_exec_t**)ccv_array_get(stateful_execs, index)((void*)(((char*)((stateful_execs)->data)) + (size_t)(stateful_execs
)->rsize * (size_t)(index)))
= 0;
992 } else
993 stateful_exec->should_free = 1;
994}
995
996static int _ccv_nnc_tensor_bind_trace_forward_to_free(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable, ccv_nnc_tensor_variable_graph_bind_t* const bind, ccv_nnc_tensor_variable_graph_bind_t* const root_bind, int* const ws_start, const int assuming_no_source) // assuming_no_source means we are going to remove sources if possible, thus, it is irrelevant.
997{
998 int can_free_symbol = 0;
999 const int sources_and_is_only_output = (root_bind->sources && root_bind->sources->rnum > 0) && _ccv_nnc_tensor_variable_is_only_output(graph, bind, tensor_variable->symbol.d);
1000 if (!root_bind->sources || root_bind->sources->rnum == 0 || sources_and_is_only_output || assuming_no_source)
1001 {
1002 int i, j;
1003 can_free_symbol = 1; // Assume we can free this symbol.
1004 if (!graph->ws)
1005 graph->ws = ccv_array_new(sizeof(int), root_bind->destinations ? root_bind->destinations->rnum : 0, 0);
1006 ccv_array_t* const ws = graph->ws;
1007 ccv_array_clear(ws);
1008 if (root_bind->destinations)
1009 for (i = 0; i < root_bind->destinations->rnum; i++)
1010 ccv_array_add_unique_int(ws, *(int*)ccv_array_get(root_bind->destinations, i)((void*)(((char*)((root_bind->destinations)->data)) + (
size_t)(root_bind->destinations)->rsize * (size_t)(i)))
);
1011 const int ws_init_size = ws->rnum;
1012 *ws_start = ws_init_size;
1013 // Add all sources from root_bind, in case it has been freed (during update bind sources / destinations when free.
1014 if (root_bind->sources)
1015 for (i = 0; i < root_bind->sources->rnum; i++)
1016 ccv_array_add_unique_int(ws, *(int*)ccv_array_get(root_bind->sources, i)((void*)(((char*)((root_bind->sources)->data)) + (size_t
)(root_bind->sources)->rsize * (size_t)(i)))
);
1017 // If we cannot loop over any exec symbols (this is not in use). It is simple to determine whether we want
1018 // to free it or not: if this is an alias and the origin is not freed, we cannot free this symbol.
1019 if (ws_init_size == 0)
1020 can_free_symbol = (!bind->alias_ref || root_bind->index < 0);
1021 // Go through all the exec symbols use this tensor, to see whether they have inputs that has other sources.
1022 for (i = 0; i < ws_init_size; i++)
1023 {
1024 const int exec_symbol_d = *(int*)ccv_array_get(ws, i)((void*)(((char*)((ws)->data)) + (size_t)(ws)->rsize * (
size_t)(i)))
;
1025 const ccv_nnc_graph_exec_symbol_t symbol = {
1026 .d = exec_symbol_d,
1027 .graph = graph->tape
1028 };
1029 const int* inputs; int input_size;
1030 const int* outputs; int output_size;
1031 ccv_nnc_graph_exec_symbol_io(graph->tape, symbol, &inputs, &input_size, &outputs, &output_size);
1032 int flag = 0; // flag denotes whether there are cases to keep this exec symbol.
1033 if (!root_bind->sources || root_bind->sources->rnum == 0 || assuming_no_source)
1034 {
1035 // If there is no sources, check if other sources can depend on this exec, if they do, we cannot free this.
1036 for (j = 0; !flag && j < input_size; j++)
1037 if (inputs[j] >= 0 && inputs[j] < graph->binds->rnum && inputs[j] != tensor_variable->symbol.d)
1038 {
1039 ccv_nnc_tensor_variable_graph_bind_t* other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, inputs[j])((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(inputs[j])))
;
1040 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
1041 flag = 1;
1042 else {
1043 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
1044 other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, other_bind->alias_ref - 1)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(other_bind->alias_ref - 1
)))
;
1045 flag = (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT) || (other_bind->type != CCV_NNC_TENSOR_CONSTANT && other_bind->sources && other_bind->sources->rnum > 0); // Constant should have no source, or it is detached.
1046 }
1047 }
1048 } else {
1049 // If there are sources, check whether we have outputs or not. If we do, we cannot free this.
1050 for (j = 0; !flag && j < output_size; j++)
1051 if (outputs[j] >= 0 && outputs[j] < graph->binds->rnum)
1052 {
1053 ccv_nnc_tensor_variable_graph_bind_t* other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, outputs[j])((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(outputs[j])))
;
1054 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
1055 flag = 1;
1056 else {
1057 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
1058 other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, other_bind->alias_ref - 1)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(other_bind->alias_ref - 1
)))
;
1059 flag = (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT) || (other_bind->destinations && other_bind->destinations->rnum > 0);
1060 }
1061 }
1062 }
1063 // This exec can be freed if there is no input required or there is no output required.
1064 can_free_symbol = (can_free_symbol && !flag);
1065 if (!flag)
1066 {
1067 // Go over inputs and remove all references from binded destinations.
1068 // and go over outputs remove all references from binded sources.
1069 _ccv_nnc_update_bind_sources_destinations_when_free(graph, exec_symbol_d, graph->binds, inputs, input_size, outputs, output_size, ws);
1070 const int* outgoings; int outgoing_size;
1071 ccv_nnc_graph_exec_symbol_to(graph->tape, symbol, &outgoings, &outgoing_size);
1072 for (j = 0; j < outgoing_size; j++)
1073 ccv_array_add_unique_int(ws, outgoings[j]);
1074 _ccv_nnc_stateful_exec_free_if_possible(graph, symbol);
1075 ccv_nnc_graph_exec_symbol_free(graph->tape, symbol);
1076 }
1077 }
1078 }
1079 return can_free_symbol;
1080}
1081
1082static void _ccv_nnc_tensor_bind_trace_backward_to_free(ccv_nnc_dynamic_graph_t* const graph, ccv_array_t* const ws, const int ws_start)
1083{
1084 int i, j;
1085 // Now, go over the outgoings, if it is removed, add more to it. Note that the ws array can grow while iterating over.
1086 for (i = ws_start; i < ws->rnum; i++)
1087 {
1088 const int exec_symbol_d = *(int*)ccv_array_get(ws, i)((void*)(((char*)((ws)->data)) + (size_t)(ws)->rsize * (
size_t)(i)))
;
1089 const ccv_nnc_graph_exec_symbol_t symbol = {
1090 .d = exec_symbol_d,
1091 .graph = graph->tape
1092 };
1093 const int* inputs; int input_size;
1094 const int* outputs; int output_size;
1095 ccv_nnc_graph_exec_symbol_io(graph->tape, symbol, &inputs, &input_size, &outputs, &output_size);
1096 int flag = 0;
1097 for (j = 0; !flag && j < input_size; j++)
1098 if (inputs[j] >= 0 && inputs[j] < graph->binds->rnum)
1099 {
1100 ccv_nnc_tensor_variable_graph_bind_t* other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, inputs[j])((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(inputs[j])))
;
1101 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
1102 flag = 1;
1103 else {
1104 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
1105 other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, other_bind->alias_ref - 1)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(other_bind->alias_ref - 1
)))
;
1106 flag = (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT) || (other_bind->type != CCV_NNC_TENSOR_CONSTANT && other_bind->sources && other_bind->sources->rnum > 0);
1107 }
1108 }
1109 if (flag) // If any inputs make free this destination impossible. Check whether all its outputs are done.
1110 {
1111 int output_flag = 0;
1112 for (j = 0; !output_flag && j < output_size; j++)
1113 if (outputs[j] >= 0 && outputs[j] < graph->binds->rnum)
1114 {
1115 ccv_nnc_tensor_variable_graph_bind_t* other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, outputs[j])((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(outputs[j])))
;
1116 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
1117 output_flag = 1;
1118 else {
1119 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
1120 other_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, other_bind->alias_ref - 1)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(other_bind->alias_ref - 1
)))
;
1121 output_flag = (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT) || (other_bind->destinations && other_bind->destinations->rnum > 0);
1122 }
1123 }
1124 if (!output_flag) // If no output is used (used means it has a tensor variable, or it has a destination).
1125 flag = 0;
1126 }
1127 // Went over all the inputs, it turns out no more inputs has other references, safe to remove.
1128 if (!flag)
1129 {
1130 _ccv_nnc_update_bind_sources_destinations_when_free(graph, exec_symbol_d, graph->binds, inputs, input_size, outputs, output_size, ws);
1131 const int* outgoings; int outgoing_size;
1132 ccv_nnc_graph_exec_symbol_to(graph->tape, symbol, &outgoings, &outgoing_size);
1133 // It it has outgoings, add that for further inspection.
1134 for (j = 0; j < outgoing_size; j++)
1135 ccv_array_add_unique_int(ws, outgoings[j]);
1136 _ccv_nnc_stateful_exec_free_if_possible(graph, symbol);
1137 ccv_nnc_graph_exec_symbol_free(graph->tape, symbol);
1138 }
1139 }
1140}
1141
1142void ccv_nnc_tensor_variable_free(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
1143{
1144 // If it contains a symbol, this tensor variable is not a free variable. It is either used as input or output.
1145 if (tensor_variable->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL)
1146 {
1147 // If it is not a free variable, when can we free the symbol and the underlying variable?
1148 // 1. There should be no sources (the command generate this tensor should be freed) or the output of these sources is only the current one;
1149 // 2. The destinations (the commands that uses this tensor) should have no other inputs, or the other inputs has no binded sources as well.
1150 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, tensor_variable->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(tensor_variable->symbol.d
)))
;
1151 // There should be no source associated with it no more.
1152 // I am free if no exec symbol is producing me or the symbol producing me can only producing me (thus, it is not required to
1153 // compute gradient because I am the only variable it can compute gradient for).
1154 ccv_nnc_tensor_variable_graph_bind_t* root_bind = bind;
1155 if (bind->alias_ref)
1156 {
1157 const int alias_to = bind->alias_ref - 1;
1158 root_bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, alias_to)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(alias_to)))
;
1159 }
1160 int ws_start;
1161 const int can_free_symbol = _ccv_nnc_tensor_bind_trace_forward_to_free(graph, tensor_variable, bind, root_bind, &ws_start, 0);
1162 if (can_free_symbol)
1163 {
1164 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
1165 ccv_nnc_tensor_symbol_free(graph->tape, tensor_variable->symbol);
1166 _ccv_nnc_tensor_bind_trace_backward_to_free(graph, graph->ws, ws_start);
1167 } else { // If this symbol is not freed, move the tensor view to the bind.
1168 // If current bind is an alias, and it doesn't have any sources or destinations. We cannot find this alias
1169 // through any exec. This is not only safe to delete, but has to be deleted. We don't need to handle this
1170 // if free_symbol is true, because when that happens, root_bind will be deleted, and we will clean up the
1171 // alias in that process.
1172 if (bind->alias_ref && (!bind->sources || bind->sources->rnum == 0) && (!bind->destinations || bind->destinations->rnum == 0))
1173 {
1174 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
1175 ccv_nnc_tensor_symbol_free(graph->tape, tensor_variable->symbol);
1176 } else {
1177 bind->index = CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED; // This tensor variable will be freed, but this symbol extra will continue exists.
1178 bind->destructor_hook.func = tensor_variable->destructor_hook.func; // Transfer the destructor callback.
1179 bind->destructor_hook.context = tensor_variable->destructor_hook.context; // Transfer the destructor callback context.
1180 bind->tensor_view = tensor_variable->tensor_view; // Transfer the ownership to the bind.
1181 tensor_variable->tensor_view = 0;
1182 }
1183 }
1184 }
1185 _ccv_nnc_tensor_variable_free(graph, tensor_variable, 1);
1186}
1187
1188void ccv_nnc_tensor_variable_detach(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
1189{
1190 // This cannot be an alias.
1191 assert(!tensor_variable->alias_index_ref)((void) sizeof ((!tensor_variable->alias_index_ref) ? 1 : 0
), __extension__ ({ if (!tensor_variable->alias_index_ref)
; else __assert_fail ("!tensor_variable->alias_index_ref"
, "ccv_nnc_dynamic_graph.c", 1191, __extension__ __PRETTY_FUNCTION__
); }))
;
1192 // If no computation done yet, mark this as constant.
1193 if (tensor_variable->symbol.d == CCV_NNC_NO_TENSOR_SYMBOL)
1194 {
1195 tensor_variable->type = CCV_NNC_TENSOR_CONSTANT;
1196 return;
1197 }
1198 // Otherwise, we need to do some book keeping updates to make sure it doesn't participate gradient computation any more.
1199 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, tensor_variable->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(tensor_variable->symbol.d
)))
;
1200 // Because tensor variable cannot be alias, its bind cannot have alias pointer.
1201 assert(!bind->alias_ref)((void) sizeof ((!bind->alias_ref) ? 1 : 0), __extension__
({ if (!bind->alias_ref) ; else __assert_fail ("!bind->alias_ref"
, "ccv_nnc_dynamic_graph.c", 1201, __extension__ __PRETTY_FUNCTION__
); }))
;
1202 // Go through to break ties between sources and destinations.
1203 int i, j;
1204 if (bind->sources && bind->destinations)
1205 {
1206 for (i = 0; i < bind->sources->rnum; i++)
1207 {
1208 const int s = *(int*)ccv_array_get(bind->sources, i)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(i)))
;
1209 const int* outputs; int output_size;
1210 const ccv_nnc_graph_exec_symbol_t s_symbol = {
1211 .d = s,
1212 .graph = graph->tape
1213 };
1214 ccv_nnc_graph_exec_symbol_io(graph->tape, s_symbol, 0, 0, &outputs, &output_size);
1215 for (j = 0; j < bind->destinations->rnum; j++)
1216 {
1217 const int d = *(int*)ccv_array_get(bind->destinations, j)((void*)(((char*)((bind->destinations)->data)) + (size_t
)(bind->destinations)->rsize * (size_t)(j)))
;
1218 const ccv_nnc_graph_exec_symbol_t d_symbol = {
1219 .d = d,
1220 .graph = graph->tape
1221 };
1222 const int* inputs; int input_size;
1223 ccv_nnc_graph_exec_symbol_io(graph->tape, d_symbol, &inputs, &input_size, 0, 0);
1224 int x, y;
1225 int flag = 0; // Whether we find a symbol that connects source and destination but not the current one we detach. If found, we cannot break the tie between s_symbol and d_symbol.
1226 for (x = 0; !flag && x < output_size; x++)
1227 {
1228 ccv_nnc_tensor_symbol_t x_symbol = ccv_nnc_tensor_symbol_alias_to(graph->tape, (ccv_nnc_tensor_symbol_t){
1229 .d = outputs[x],
1230 .graph = graph->tape
1231 });
1232 if (x_symbol.d == CCV_NNC_NO_TENSOR_SYMBOL)
1233 {
1234 x_symbol.d = outputs[x];
1235 x_symbol.graph = graph->tape;
1236 }
1237 if (x_symbol.d == tensor_variable->symbol.d || x_symbol.d == CCV_NNC_NO_TENSOR_SYMBOL)
1238 continue;
1239 for (y = 0; !flag && y < input_size; y++)
1240 {
1241 ccv_nnc_tensor_symbol_t y_symbol = ccv_nnc_tensor_symbol_alias_to(graph->tape, (ccv_nnc_tensor_symbol_t){
1242 .d = inputs[y],
1243 .graph = graph->tape
1244 });
1245 if (y_symbol.d == CCV_NNC_NO_TENSOR_SYMBOL)
1246 {
1247 y_symbol.d = inputs[y];
1248 y_symbol.graph = graph->tape;
1249 }
1250 if (y_symbol.d == tensor_variable->symbol.d || y_symbol.d == CCV_NNC_NO_TENSOR_SYMBOL)
1251 continue;
1252 flag = (x_symbol.d == y_symbol.d);
1253 }
1254 }
1255 if (!flag)
1256 ccv_nnc_graph_exec_symbol_disjoin(graph->tape, s_symbol, d_symbol);
1257 }
1258 }
1259 }
1260 const int sources_and_is_only_output = (bind->sources && bind->sources->rnum > 0) && _ccv_nnc_tensor_variable_is_only_output(graph, bind, tensor_variable->symbol.d);
1261 if (!bind->sources || bind->sources->rnum == 0 || sources_and_is_only_output)
1262 {
1263 int ws_start = -1;
1264 _ccv_nnc_tensor_bind_trace_forward_to_free(graph, tensor_variable, bind, bind, &ws_start, 1);
1265 // Because we are detaching from the graph, there is no need to forward trace to see if it is not used and
1266 // then to remove the source execs. We can remove them right now, breaking the graph in two. That is why
1267 // we called trace backward to free regardless the outcome of the forward to free.
1268 if (ws_start == -1)
1269 {
1270 if (!graph->ws)
1271 graph->ws = ccv_array_new(sizeof(int), bind->destinations ? bind->destinations->rnum : 0, 0);
1272 ccv_array_t* const ws = graph->ws;
1273 ccv_array_clear(ws);
1274 if (bind->sources)
1275 for (i = 0; i < bind->sources->rnum; i++)
1276 ccv_array_add_unique_int(ws, *(int*)ccv_array_get(bind->sources, i)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(i)))
);
1277 ws_start = 0;
1278 }
1279 _ccv_nnc_tensor_bind_trace_backward_to_free(graph, graph->ws, ws_start);
1280 }
1281 // If now bind has no relevant sources or destinations, we can safely free the underlying tensor symbol.
1282 if ((!bind->sources || bind->sources->rnum == 0) && (!bind->destinations || bind->destinations->rnum == 0))
1283 {
1284 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
1285 ccv_nnc_tensor_symbol_free(graph->tape, tensor_variable->symbol);
1286 tensor_variable->type = CCV_NNC_TENSOR_CONSTANT;
1287 tensor_variable->symbol = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
1288 return;
1289 }
1290 // Mark both as constant, such that even if it cannot be freed now, it can be freed as soon as possible later.
1291 bind->type = CCV_NNC_TENSOR_CONSTANT;
1292 tensor_variable->type = CCV_NNC_TENSOR_CONSTANT;
1293}
1294
1295void ccv_nnc_dynamic_graph_has_effect_to_tensor_variables(const ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t* const source_variables, const int source_variable_size, const ccv_nnc_tensor_variable_t* const destination_variables, const int destination_variable_size, uint64_t* const bitmask)
1296{
1297 int i, j;
1298 ccv_array_t* const sources_destinations = ccv_array_new(sizeof(ccv_nnc_graph_exec_symbol_t), source_variable_size + destination_variable_size, 0);
1299 for (i = 0; i < source_variable_size; i++)
1300 {
1301 if (source_variables[i]->symbol.d < 0)
1302 continue;
1303 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, source_variables[i]->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(source_variables[i]->symbol
.d)))
;
1304 if (bind->destinations && bind->destinations->rnum > 0)
1305 for (j = 0; j < bind->destinations->rnum; j++)
1306 {
1307 // It is ok to have duplicate symbols.
1308 const int d = *(int*)ccv_array_get(bind->destinations, j)((void*)(((char*)((bind->destinations)->data)) + (size_t
)(bind->destinations)->rsize * (size_t)(j)))
;
1309 ccv_nnc_graph_exec_symbol_t symbol = {
1310 .d = d,
1311 .graph = graph->tape
1312 };
1313 ccv_array_push(sources_destinations, &symbol);
1314 }
1315 }
1316 const int source_size = sources_destinations->rnum;
1317 for (i = 0; i < destination_variable_size; i++)
1318 {
1319 if (destination_variables[i]->symbol.d < 0)
1320 continue;
1321 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, destination_variables[i]->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(destination_variables[i]->
symbol.d)))
;
1322 if (bind->sources && bind->sources->rnum > 0)
1323 for (j = 0; j < bind->sources->rnum; j++)
1324 {
1325 // It is ok to have duplicate symbols.
1326 const int d = *(int*)ccv_array_get(bind->sources, j)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(j)))
;
1327 ccv_nnc_graph_exec_symbol_t symbol = {
1328 .d = d,
1329 .graph = graph->tape
1330 };
1331 ccv_array_push(sources_destinations, &symbol);
1332 }
1333 }
1334 const int destination_size = sources_destinations->rnum - source_size;
1335 if (source_size == 0 || destination_size == 0)
1336 {
1337 ccv_array_free(sources_destinations);
1338 return;
1339 }
1340 const int bitmask_size = ((source_size + 63) >> 6);
1341 assert(bitmask_size < 256)((void) sizeof ((bitmask_size < 256) ? 1 : 0), __extension__
({ if (bitmask_size < 256) ; else __assert_fail ("bitmask_size < 256"
, "ccv_nnc_dynamic_graph.c", 1341, __extension__ __PRETTY_FUNCTION__
); }))
;
1342 uint64_t exec_bitmask[bitmask_size];
1343 ccv_nnc_symbolic_graph_sources_to_destinations(graph->tape, (ccv_nnc_graph_exec_symbol_t*)ccv_array_get(sources_destinations, 0)((void*)(((char*)((sources_destinations)->data)) + (size_t
)(sources_destinations)->rsize * (size_t)(0)))
, source_size, (ccv_nnc_graph_exec_symbol_t*)ccv_array_get(sources_destinations, source_size)((void*)(((char*)((sources_destinations)->data)) + (size_t
)(sources_destinations)->rsize * (size_t)(source_size)))
, destination_size, exec_bitmask);
1344 int k = 0;
1345 for (i = 0; i < source_variable_size; i++)
1346 {
1347 if (source_variables[i]->symbol.d < 0)
1348 {
1349 bitmask[i >> 6] &= ~((uint64_t)1 << (i & 63));
1350 continue;
1351 }
1352 ccv_nnc_tensor_variable_graph_bind_t* const bind = (ccv_nnc_tensor_variable_graph_bind_t*)ccv_array_get(graph->binds, source_variables[i]->symbol.d)((void*)(((char*)((graph->binds)->data)) + (size_t)(graph
->binds)->rsize * (size_t)(source_variables[i]->symbol
.d)))
;
1353 int flag = 0;
1354 if (bind->destinations && bind->destinations->rnum > 0)
1355 {
1356 assert(k <= source_size - bind->destinations->rnum)((void) sizeof ((k <= source_size - bind->destinations->
rnum) ? 1 : 0), __extension__ ({ if (k <= source_size - bind
->destinations->rnum) ; else __assert_fail ("k <= source_size - bind->destinations->rnum"
, "ccv_nnc_dynamic_graph.c", 1356, __extension__ __PRETTY_FUNCTION__
); }))
;
1357 for (j = 0; !flag && j < bind->destinations->rnum; j++)
1358 flag = (((uint64_t)1 << ((k + j) & 63)) & exec_bitmask[(k + j) >> 6]);
1359 k += bind->destinations->rnum;
1360 }
1361 if (flag)
1362 bitmask[i >> 6] |= ((uint64_t)1 << (i & 63));
1363 else
1364 bitmask[i >> 6] &= ~((uint64_t)1 << (i & 63));
1365 }
1366 ccv_array_free(sources_destinations);
1367}
1368
1369int ccv_nnc_dynamic_graph_bookkeeping_count(const ccv_nnc_dynamic_graph_t* const graph, const int type)
1370{
1371 return ccv_nnc_symbolic_graph_active_symbol_count(graph->tape, type);
1372}
1373
1374void ccv_nnc_dynamic_graph_dot(const ccv_nnc_dynamic_graph_t* const graph, const int flags, FILE* out)
1375{
1376 ccv_nnc_symbolic_graph_dot(graph->tape, flags, out);
1377}
1378
1379void ccv_nnc_dynamic_graph_format(const ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_symbolic_graph_format_f format_fn, void* const context)
1380{
1381 ccv_nnc_symbolic_graph_format(graph->tape, 0, 0, 0, 0, format_fn, context);
1382}