Bug Summary

File:nnc/ccv_nnc_dynamic_graph.c
Warning:line 539, 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-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -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 -fcoverage-compilation-dir=/home/liu/buildslave/linux-x64-runtests/build/lib/nnc -resource-dir /usr/local/lib/clang/14.0.0 -I ../ -I /usr/local/cuda/include -D HAVE_CBLAS -D HAVE_LIBPNG -D HAVE_LIBJPEG -D HAVE_FFTW3 -D HAVE_PTHREAD -D HAVE_LIBLINEAR -D HAVE_TESSERACT -D HAVE_AVCODEC -D HAVE_AVFORMAT -D HAVE_AVUTIL -D HAVE_SWSCALE -D USE_DISPATCH -D HAVE_SSE2 -D HAVE_GSL -D HAVE_CUDA -D HAVE_CUDNN -D HAVE_NCCL -D USE_SYSTEM_CUB -I /usr/local/include -internal-isystem /usr/local/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -fdebug-compilation-dir=/home/liu/buildslave/linux-x64-runtests/build/lib/nnc -ferror-limit 19 -fblocks -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/liu/buildslave/public_html/analyze/2022-06-24-190241-827281-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)
43 ccv_nnc_xpu_free(&graph->xpu_alloc, tensor_variable->tensor_view->data.ptr);
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)
83 ccv_nnc_xpu_free(&graph->xpu_alloc, bind->tensor_view->data.ptr);
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->destructor_hook.func = 0;
162 tensor_variable->destructor_hook.context = 0;
163 tensor_variable->info = info;
164 tensor_variable->symbol = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
165 tensor_variable->tensor_view = 0;
166 if (graph->reuse_var >= 0)
167 {
168 const int reuse_var = graph->reuse_var;
169 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", 169, __extension__ __PRETTY_FUNCTION__
); }))
;
170 tensor_variable->index = reuse_var;
171 *(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;
172 int i;
173 graph->reuse_var = -1;
174 for (i = reuse_var + 1; i < graph->vars->rnum && graph->reuse_var < 0; i++)
175 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)
176 graph->reuse_var = i;
177 } else {
178 tensor_variable->index = graph->vars->rnum;
179 ccv_array_push(graph->vars, &tensor_variable);
180 }
181}
182
183ccv_nnc_tensor_variable_t ccv_nnc_tensor_variable_new_impl(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_param_t info)
184{
185 ccv_nnc_tensor_variable_t tensor_variable = ccmallocmalloc(sizeof(struct ccv_nnc_tensor_variable_s));
186 tensor_variable->type = CCV_NNC_TENSOR_VARIABLE;
187 _ccv_nnc_tensor_variable_init(graph, tensor_variable, info);
188 return tensor_variable;
189}
190
191ccv_nnc_tensor_variable_t ccv_nnc_tensor_constant_new_impl(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_param_t info)
192{
193 ccv_nnc_tensor_variable_t tensor_variable = ccmallocmalloc(sizeof(struct ccv_nnc_tensor_variable_s));
194 tensor_variable->type = CCV_NNC_TENSOR_CONSTANT;
195 _ccv_nnc_tensor_variable_init(graph, tensor_variable, info);
196 return tensor_variable;
197}
198
199int ccv_nnc_tensor_variable_is_constant(const ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
200{
201 return tensor_variable->type == CCV_NNC_TENSOR_CONSTANT;
202}
203
204ccv_nnc_tensor_param_t ccv_nnc_tensor_variable_params(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
205{
206 return tensor_variable->info;
207}
208
209ccv_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 inc[CCV_NNC_MAX_DIM_ALLOC(12)], const ccv_nnc_tensor_param_t info)
210{
211 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", 211, __extension__ __PRETTY_FUNCTION__
); }))
;
212 ccv_nnc_tensor_variable_t variable_alias = ccmallocmalloc(sizeof(struct ccv_nnc_tensor_variable_s));
213 variable_alias->type = tensor_variable->type;
214 variable_alias->alias_index_ref = tensor_variable->index + 1;
215 variable_alias->info = info;
216 variable_alias->symbol = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
217 variable_alias->destructor_hook.func = 0;
218 variable_alias->destructor_hook.context = 0;
219 variable_alias->tensor_view = 0;
220 memcpy(variable_alias->ofs, ofs, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
221 memcpy(variable_alias->inc, inc, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12));
222 if (graph->reuse_var >= 0)
223 {
224 const int reuse_var = graph->reuse_var;
225 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", 225, __extension__ __PRETTY_FUNCTION__
); }))
;
226 variable_alias->index = reuse_var;
227 *(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;
228 int i;
229 graph->reuse_var = -1;
230 for (i = reuse_var + 1; i < graph->vars->rnum && graph->reuse_var < 0; i++)
231 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)
232 graph->reuse_var = i;
233 } else {
234 variable_alias->index = graph->vars->rnum;
235 ccv_array_push(graph->vars, &variable_alias);
236 }
237 return variable_alias;
238}
239
240ccv_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)
241{
242 if (tensor_variable->tensor_view)
243 {
244 if (tensor_variable->alias_index_ref)
245 {
246 const int alias_index = tensor_variable->alias_index_ref - 1;
247 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", 247, __extension__ __PRETTY_FUNCTION__
); }))
;
248 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)))
;
249 if (CCV_IS_TENSOR_VIEW(tensor_variable->tensor_view)((*(int*)(tensor_variable->tensor_view)) & CCV_TENSOR_VIEW
)
)
250 {
251 ccv_nnc_tensor_view_t* const tv = tensor_variable->tensor_view;
252 // We cannot have an alias with custom set tensor, otherwise the pointer update is invalid.
253 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", 253, __extension__ __PRETTY_FUNCTION__
); }))
;
254 // Update the tensor_view pointer every time access it, because the underlying variable it alias to have changed.
255 tv->data.u8 = 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;
256 } else {
257 ccv_nnc_tensor_t* const tv = (ccv_nnc_tensor_t*)tensor_variable->tensor_view;
258 // We cannot have an alias with custom set tensor, otherwise the pointer update is invalid.
259 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", 259, __extension__ __PRETTY_FUNCTION__
); }))
;
260 // Update the tensor_view pointer every time access it, because the underlying variable it alias to have changed.
261 tv->data.u8 = CCV_NNC_TENSOR_VIEW(variable_to->tensor_view)((ccv_nnc_tensor_view_t*)((uintptr_t)(variable_to->tensor_view
) & ~(uintptr_t)1))
->data.u8;
262 }
263 }
264 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))
;
265 }
266 if (!tensor_variable->alias_index_ref)
267 {
268 // If we haven't allocated tensor_variable, we cannot allocate them now (because no shape specified), return 0.
269 if (ccv_nnc_is_tensor_auto(tensor_variable->info))
270 return 0;
271 void* ptr = 0;
272 if (CCV_TENSOR_GET_MEMORY(tensor_variable->info.type)((tensor_variable->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY)
273 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, ccv_nnc_tensor_data_size(tensor_variable->info));
274 tensor_variable->tensor_view = (ccv_nnc_tensor_view_t*)ccv_nnc_tensor_new(ptr, tensor_variable->info, 0);
275 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", 275, __extension__ __PRETTY_FUNCTION__
); }))
;
276 return (ccv_nnc_tensor_t*)tensor_variable->tensor_view;
277 }
278 const int alias_index = tensor_variable->alias_index_ref - 1;
279 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", 279, __extension__ __PRETTY_FUNCTION__
); }))
;
280 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)))
;
281 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"
, 281, __extension__ __PRETTY_FUNCTION__); }))
;
282 if (!variable_to->tensor_view)
283 {
284 // If we haven't allocated variable_to, we cannot allocate them now (because no shape specified), return 0.
285 if (ccv_nnc_is_tensor_auto(variable_to->info))
286 return 0;
287 void* ptr = 0;
288 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", 288, __extension__ __PRETTY_FUNCTION__
); }))
;
289 if (CCV_TENSOR_GET_MEMORY(variable_to->info.type)((variable_to->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY)
290 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, ccv_nnc_tensor_data_size(variable_to->info));
291 variable_to->tensor_view = (ccv_nnc_tensor_view_t*)ccv_nnc_tensor_new(ptr, variable_to->info, 0);
292 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", 292, __extension__ __PRETTY_FUNCTION__
); }))
;
293 }
294 int i;
295 int no_ofs = 1;
296 for (i = 0; no_ofs && i < CCV_NNC_MAX_DIM_ALLOC(12); i++)
297 no_ofs = (tensor_variable->ofs[i] == 0);
298 int no_inc = 1;
299 for (i = 0; no_inc && i < CCV_NNC_MAX_DIM_ALLOC(12); i++)
300 no_inc = (tensor_variable->inc[i] == 0);
301 if (!no_inc)
302 no_inc = (memcmp(tensor_variable->inc, tensor_variable->info.dim, sizeof(int) * CCV_NNC_MAX_DIM_ALLOC(12)) == 0);
303 assert(ccv_nnc_tensor_count(tensor_variable->info) <= ccv_nnc_tensor_count(variable_to->info))((void) sizeof ((ccv_nnc_tensor_count(tensor_variable->info
) <= ccv_nnc_tensor_count(variable_to->info)) ? 1 : 0),
__extension__ ({ if (ccv_nnc_tensor_count(tensor_variable->
info) <= ccv_nnc_tensor_count(variable_to->info)) ; else
__assert_fail ("ccv_nnc_tensor_count(tensor_variable->info) <= ccv_nnc_tensor_count(variable_to->info)"
, "ccv_nnc_dynamic_graph.c", 303, __extension__ __PRETTY_FUNCTION__
); }))
;
304 if (no_ofs && !no_inc)
305 {
306 // Allowing vector type to be normal tensor, rather than a tensor view. We cannot have any offset though.
307 const int nd = ccv_nnc_tensor_nd(tensor_variable->info.dim);
308 int first_none_one_dim_idx = -1;
309 for (i = 0; first_none_one_dim_idx < 0 && i < nd; i++)
310 if (tensor_variable->info.dim[i] > 1)
311 first_none_one_dim_idx = i;
312 // Check if from 0 to last_none_one_dim_idx, it is either full or 1, and ofs is either something (1) or nothing (full).
313 if (first_none_one_dim_idx >= 0)
314 {
315 no_inc = 1;
316 assert(tensor_variable->ofs[first_none_one_dim_idx] + tensor_variable->info.dim[first_none_one_dim_idx] <= ccv_max(tensor_variable->inc[first_none_one_dim_idx], tensor_variable->info.dim[first_none_one_dim_idx]))((void) sizeof ((tensor_variable->ofs[first_none_one_dim_idx
] + tensor_variable->info.dim[first_none_one_dim_idx] <=
({ typeof (tensor_variable->inc[first_none_one_dim_idx]) _a
= (tensor_variable->inc[first_none_one_dim_idx]); typeof (
tensor_variable->info.dim[first_none_one_dim_idx]) _b = (tensor_variable
->info.dim[first_none_one_dim_idx]); (_a > _b) ? _a : _b
; })) ? 1 : 0), __extension__ ({ if (tensor_variable->ofs[
first_none_one_dim_idx] + tensor_variable->info.dim[first_none_one_dim_idx
] <= ({ typeof (tensor_variable->inc[first_none_one_dim_idx
]) _a = (tensor_variable->inc[first_none_one_dim_idx]); typeof
(tensor_variable->info.dim[first_none_one_dim_idx]) _b = (
tensor_variable->info.dim[first_none_one_dim_idx]); (_a >
_b) ? _a : _b; })) ; else __assert_fail ("tensor_variable->ofs[first_none_one_dim_idx] + tensor_variable->info.dim[first_none_one_dim_idx] <= ccv_max(tensor_variable->inc[first_none_one_dim_idx], tensor_variable->info.dim[first_none_one_dim_idx])"
, "ccv_nnc_dynamic_graph.c", 316, __extension__ __PRETTY_FUNCTION__
); }))
;
317 if (first_none_one_dim_idx < CCV_NNC_MAX_DIM_ALLOC(12))
318 no_inc = (memcmp(tensor_variable->inc + first_none_one_dim_idx + 1, tensor_variable->info.dim + first_none_one_dim_idx + 1, sizeof(int) * (CCV_NNC_MAX_DIM_ALLOC(12) - first_none_one_dim_idx - 1)) == 0);
319 }
320 }
321 if (no_ofs && no_inc)
322 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);
323 else
324 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, no_inc ? tensor_variable->info.dim : tensor_variable->inc);
325 return (ccv_nnc_tensor_t*)tensor_variable->tensor_view;
326}
327
328static 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)
329{
330 if (symbol.d >= graph->binds->rnum)
331 {
332 const int rnum = graph->binds->rnum;
333 ccv_array_resize(graph->binds, symbol.d + 1);
334 int i;
335 for (i = rnum; i < graph->binds->rnum; i++)
336 ((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;
337 }
338 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)))
;
339 bind->type = tensor_variable->type;
340 bind->index = tensor_variable->index;
341 if (tensor_variable->alias_index_ref)
342 {
343 const ccv_nnc_tensor_symbol_t alias_to = ccv_nnc_tensor_symbol_alias_to(graph->tape, (ccv_nnc_tensor_symbol_t){
344 .d = symbol.d,
345 .graph = graph->tape
346 });
347 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", 347, __extension__ __PRETTY_FUNCTION__
); }))
;
348 bind->alias_ref = alias_to.d + 1;
349 } else
350 bind->alias_ref = 0;
351 if (bind->sources)
352 ccv_array_free(bind->sources);
353 bind->sources = 0;
354 if (bind->destinations)
355 ccv_array_free(bind->destinations);
356 bind->destinations = 0;
357 bind->destructor_hook.func = 0;
358 bind->destructor_hook.context = 0;
359 bind->tensor_view = 0;
360}
361
362static 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)
363{
364 if (tensor_variable->symbol.d >= 0)
365 return tensor_variable->symbol;
366 if (!tensor_variable->alias_index_ref)
367 {
368 const ccv_nnc_tensor_symbol_t symbol = tensor_variable->symbol = ccv_nnc_tensor_symbol_new(graph->tape, tensor_variable->info, 0);
369 _ccv_nnc_tensor_symbol_extra_new(graph, tensor_variable, symbol);
370 return symbol;
371 }
372 const int alias_index = tensor_variable->alias_index_ref - 1;
373 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", 373, __extension__ __PRETTY_FUNCTION__
); }))
;
374 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)))
;
375 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"
, 375, __extension__ __PRETTY_FUNCTION__); }))
;
376 int no_inc = 1;
377 int i;
378 for (i = 0; no_inc && i < CCV_NNC_MAX_DIM_ALLOC(12); i++)
379 no_inc = (tensor_variable->inc[i] == 0);
380 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, no_inc ? tensor_variable->info.dim : tensor_variable->inc, tensor_variable->info, 0);
381 _ccv_nnc_tensor_symbol_extra_new(graph, tensor_variable, symbol);
382 return symbol;
383}
384
385// Return the tensor variable that is old (the provided tensor variable will have a new setting).
386ccv_nnc_tensor_variable_t ccv_nnc_tensor_variable_exchange_new(ccv_nnc_dynamic_graph_t* const graph, ccv_nnc_tensor_variable_t tensor_variable)
387{
388 struct ccv_nnc_tensor_variable_s x = *tensor_variable;
389 ccv_nnc_tensor_variable_t new_variable;
390 // Need to handle alias.
391 if (x.alias_index_ref)
392 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.inc, x.info);
393 else
394 new_variable = ccv_nnc_tensor_variable_new(graph, x.info)ccv_nnc_tensor_variable_new_impl(graph, x.info);
395 *tensor_variable = *new_variable;
396 *new_variable = x;
397 // The index should be the same though.
398 const int index = new_variable->index;
399 new_variable->index = tensor_variable->index;
400 if (new_variable->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL)
401 {
402 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)))
;
403 bind->index = new_variable->index;
404 }
405 tensor_variable->index = index;
406 return new_variable;
407}
408
409void ccv_nnc_dynamic_graph_set_no_grad(ccv_nnc_dynamic_graph_t* const dynamic_graph, const int no_grad)
410{
411 dynamic_graph->no_grad = no_grad;
412}
413
414static ccv_nnc_stream_context_t* _ccv_nnc_dynamic_graph_get_stream(ccv_nnc_dynamic_graph_t* const graph, const int type)
415{
416 if (!graph->stream_map)
417 graph->stream_map = kh_init(stream_map)kh_init_stream_map();
418 int ret = 0;
419 khiter_t k = kh_put(stream_map, graph->stream_map, type, &ret)kh_put_stream_map(graph->stream_map, type, &ret);
420 assert(ret >= 0)((void) sizeof ((ret >= 0) ? 1 : 0), __extension__ ({ if (
ret >= 0) ; else __assert_fail ("ret >= 0", "ccv_nnc_dynamic_graph.c"
, 420, __extension__ __PRETTY_FUNCTION__); }))
;
421 ccv_nnc_stream_context_t* stream = kh_val(graph->stream_map, k)((graph->stream_map)->vals[k]);
422 // If ret == 0, the key already exist, we can return directly, otherwise, create and return.
423 if (ret != 0)
424 {
425 stream = ccv_nnc_stream_context_new(type);
426 kh_val(graph->stream_map, k)((graph->stream_map)->vals[k]) = stream;
427 }
428 return stream;
429}
430
431typedef struct {
432 ccv_nnc_dynamic_graph_t* graph;
433 int stream_type;
434} ccv_nnc_dynamic_graph_neighbor_context_discovery_t;
435
436static ccv_nnc_stream_context_t* _ccv_nnc_dynamic_graph_neighbor_context_discovery(const int device_id, void* const context)
437{
438 ccv_nnc_dynamic_graph_neighbor_context_discovery_t* const discovery = (ccv_nnc_dynamic_graph_neighbor_context_discovery_t*)context;
439 int type = discovery->stream_type;
440 CCV_STREAM_SET_DEVICE_ID(type, device_id)(type) = (((type) & ~0xfff00) | (((device_id) & 0xfff
) << 8))
;
441 return _ccv_nnc_dynamic_graph_get_stream(discovery->graph, type);
442}
443
444void 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)
445{
446 int i, j;
447 for (i = 0; i < input_size; i++)
1
Assuming 'i' is >= 'input_size'
2
Loop condition is false. Execution continues on line 450
448 if (inputs[i] && !inputs[i]->alias_index_ref)
449 { 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", 449, __extension__ __PRETTY_FUNCTION__
); }))
; }
450 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
451 for (i = 0; i
3.1
'i' is >= 'input_size'
< input_size; i++)
4
Loop condition is false. Execution continues on line 453
452 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;
453 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
454 for (i = 0; i
5.1
'i' is >= 'input_size'
< input_size; i++)
6
Loop condition is false. Execution continues on line 456
455 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
}
;
456 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
457 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
458 for (i = 0; i
8.1
'i' is >= 'input_size'
< input_size; i++)
9
Loop condition is false. Execution continues on line 470
459 {
460 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;
461 if (inputs[i] && inputs[i]->alias_index_ref)
462 {
463 const int alias_index_ref = inputs[i]->alias_index_ref - 1;
464 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", 464, __extension__ __PRETTY_FUNCTION__
); }))
;
465 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)))
;
466 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;
467 } else
468 input_alias_sources[i] = 0;
469 }
470 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
471 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"
, 471, __extension__ __PRETTY_FUNCTION__); }))
;
12
Assuming the condition is true
13
Taking true branch
472 const int per_input_size = input_size / parallel_count;
473 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"
, 473, __extension__ __PRETTY_FUNCTION__); }))
;
14
Assuming the condition is true
15
Taking true branch
474 const int per_output_size = output_size / parallel_count;
475 int output_auto = 0;
476 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 479
477 output_auto = outputs[i] ? ccv_nnc_is_tensor_auto(outputs[i]->info) : 0;
478 // One extra step, infer the parameters for outputs.
479 if (output_auto
17.1
'output_auto' is 0
)
18
Taking false branch
480 {
481 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; })
];
482 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; })
];
483 for (i = 0; i < parallel_count; i++)
484 {
485 for (j = 0; j < per_input_size; j++)
486 input_params[j] = inputs[j + i * per_input_size] ? inputs[j + i * per_input_size]->info : ccv_nnc_tensor_auto;
487 for (j = 0; j < per_output_size; j++)
488 output_params[j] = outputs[j + i * per_output_size] ? outputs[j + i * per_output_size]->info : ccv_nnc_tensor_auto;
489 ccv_nnc_hint_tensor_auto(cmd, input_params, per_input_size, hint, output_params, per_output_size);
490 for (j = 0; j < per_output_size; j++)
491 if (outputs[j + i * per_output_size])
492 outputs[j + i * per_output_size]->info = output_params[j];
493 }
494 }
495 int freeable_size = 0;
496 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
497 // Refresh the symbol if it is binded to an existing exec. Otherwise we cannot keep the SSA guarantee.
498 for (i = 0; i
19.1
'i' is >= 'output_size'
< output_size; i++)
20
Loop condition is false. Execution continues on line 525
499 {
500 // First, go over to see whether there is enforce inplace.
501 int enforce_idx = -1;
502 for (j = 0; enforce_idx < 0 && j < input_size; j++)
503 if (inputs[j] && ccv_nnc_cmd_enforce_inplace(cmd, j, input_size, i, output_size))
504 enforce_idx = j;
505 if (enforce_idx >= 0)
506 { 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", 506, __extension__ __PRETTY_FUNCTION__
); }))
; }
507 // We don't allow or check "allow inplace" yet. That logic will be at odds with backward logic.
508 if (outputs[i] && outputs[i]->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL)
509 {
510 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)))
;
511 if (enforce_idx >= 0)
512 { 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", 512, __extension__ __PRETTY_FUNCTION__
); }))
; }
513 if (bind->sources && bind->sources->rnum > 0)
514 {
515 const ccv_nnc_tensor_variable_t old_var = freeables[freeable_size++] = ccv_nnc_tensor_variable_exchange_new(graph, outputs[i]);
516 // If this is enforce output, make sure the tensor view is taken by the output.
517 if (enforce_idx >= 0)
518 {
519 outputs[i]->tensor_view = old_var->tensor_view; // Make sure the tensor view is taken over by the output.
520 old_var->tensor_view = 0;
521 }
522 }
523 }
524 }
525 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
526 if (parallel_count > 1)
23
Assuming 'parallel_count' is > 1
24
Taking true branch
527 {
528 const int max_device_id_size = per_input_size + per_output_size;
529 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", 529, __extension__ __PRETTY_FUNCTION__
); }))
;
25
Assuming 'max_device_id_size' is > 0
26
Taking true branch
530 int device_ids[max_device_id_size];
531 ccv_nnc_stream_context_t* streams[parallel_count];
532 ccv_nnc_stream_signal_t* signal;
533 if (stream_context)
27
Assuming 'stream_context' is null
28
Taking false branch
534 signal = ccv_nnc_stream_context_emit_signal_new(stream_context);
535 for (i = 0; i
28.1
'i' is < 'parallel_count'
< parallel_count; i++)
29
Loop condition is true. Entering loop body
536 {
537 int flag = 0;
538 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
539 if (input_tensors[i * per_input_size + j])
32
Branch condition evaluates to a garbage value
540 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);
541 for (j = 0; j < per_output_size; j++)
542 {
543 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;
544 if (output_tensors[j] && !flag)
545 flag = (CCV_TENSOR_GET_MEMORY(output_tensors[j]->info.type)((output_tensors[j]->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY);
546 }
547 const int stream_type = flag ? CCV_STREAM_CONTEXT_GPU : CCV_STREAM_CONTEXT_CPU;
548 const int tensor_type = flag ? CCV_TENSOR_GPU_MEMORY : CCV_TENSOR_CPU_MEMORY;
549 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);
550 ccv_nnc_stream_context_t* stream_0 = 0;
551 for (j = 0; j < device_id_size; j++)
552 {
553 int type = stream_type;
554 CCV_STREAM_SET_DEVICE_ID(type, device_ids[j])(type) = (((type) & ~0xfff00) | (((device_ids[j]) & 0xfff
) << 8))
;
555 ccv_nnc_stream_context_t* const stream = _ccv_nnc_dynamic_graph_get_stream(graph, type);
556 if (!stream_0)
557 stream_0 = stream;
558 }
559 // Wait signal to finish.
560 if (stream_context)
561 {
562 if (stream_0)
563 ccv_nnc_stream_context_wait_signal(stream_0, signal);
564 else
565 ccv_nnc_stream_context_wait(stream_context);
566 }
567 if (stream_0)
568 {
569 ccv_nnc_dynamic_graph_neighbor_context_discovery_t discovery = {
570 .graph = graph,
571 .stream_type = stream_type
572 };
573 ccv_nnc_stream_context_set_neighbor_discovery(stream_0, _ccv_nnc_dynamic_graph_neighbor_context_discovery, &discovery);
574 }
575 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)
;
576 int k;
577 for (k = 0; k < per_input_size; k++)
578 {
579 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)
;
580 if (input_tensors[k + i * per_input_size] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
581 ccv_nnc_print_tensor_info(input_tensors[k + i * per_input_size]);
582 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
583 }
584 ccv_nnc_cmd_exec(cmd, hint, flags, input_tensors + i * per_input_size, per_input_size, output_tensors, per_output_size, stream_0);
585 for (k = 0; k < per_output_size; k++)
586 {
587 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)
;
588 if (output_tensors[k] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
589 ccv_nnc_print_tensor_info(output_tensors[k]);
590 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
591 }
592 if (stream_context && stream_0)
593 {
594 ccv_nnc_stream_signal_t* const signal = ccv_nnc_stream_context_emit_signal_new(stream_0);
595 ccv_nnc_stream_context_wait_signal(stream_context, signal);
596 }
597 streams[i] = stream_0;
598 }
599 if (!stream_context)
600 for (i = 0; i < parallel_count; i++)
601 if (streams[i])
602 ccv_nnc_stream_context_wait(streams[i]);
603 } else {
604 for (i = 0; i < per_output_size; i++)
605 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;
606 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)
;
607 for (i = 0; i < per_input_size; i++)
608 {
609 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)
;
610 if (input_tensors[i] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
611 ccv_nnc_print_tensor_info(input_tensors[i]);
612 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
613 }
614 ccv_nnc_cmd_exec(cmd, hint, flags, input_tensors, per_input_size, output_tensors, per_output_size, stream_context);
615 for (i = 0; i < per_output_size; i++)
616 {
617 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)
;
618 if (output_tensors[i] && CCV_CLI_OUTPUT_LEVEL_IS(CCV_CLI_INFO)(CCV_CLI_INFO & ccv_cli_get_output_levels()))
619 ccv_nnc_print_tensor_info(output_tensors[i]);
620 PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf
("\n"); fflush(stdout); } } while (0)
;
621 }
622 }
623 int inputs_are_constants = 1;
624 for (i = 0; inputs_are_constants && i < input_size; i++)
625 if (inputs[i] && inputs[i]->type != CCV_NNC_TENSOR_CONSTANT)
626 inputs_are_constants = 0;
627 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.
628 {
629 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; })
];
630 for (i = 0; i < output_size; i++)
631 if (outputs[i])
632 {
633 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", 633, __extension__ __PRETTY_FUNCTION__
); }))
;
634 output_symbols[i] = _ccv_nnc_tensor_symbol_from_variable(graph, outputs[i]);
635 } else
636 output_symbols[i] = NO_TENSOR_SYMBOL(const ccv_nnc_tensor_symbol_t){.d = CCV_NNC_NO_TENSOR_SYMBOL
}
;
637 int t;
638 for (t = 0; t < parallel_count; t++)
639 {
640 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);
641 if (graph_execs)
642 graph_execs[t] = graph_exec;
643 // This needs to be done before we set the new sources on the outputs.
644 for (i = 0; i < per_input_size; i++)
645 {
646 ccv_array_t* const input_source = input_sources[i + t * per_input_size];
647 if (input_source)
648 for (j = 0; j < input_source->rnum; j++)
649 ccv_nnc_graph_exec_symbol_concat(graph->tape, (ccv_nnc_graph_exec_symbol_t){
650 .d = *(int*)ccv_array_get(input_source, j)((void*)(((char*)((input_source)->data)) + (size_t)(input_source
)->rsize * (size_t)(j)))
,
651 .graph = graph->tape
652 }, graph_exec);
653 ccv_array_t* const input_alias_source = input_alias_sources[i + t * per_input_size];
654 if (input_alias_source)
655 for (j = 0; j < input_alias_source->rnum; j++)
656 ccv_nnc_graph_exec_symbol_concat(graph->tape, (ccv_nnc_graph_exec_symbol_t){
657 .d = *(int*)ccv_array_get(input_alias_source, j)((void*)(((char*)((input_alias_source)->data)) + (size_t)(
input_alias_source)->rsize * (size_t)(j)))
,
658 .graph = graph->tape
659 }, graph_exec);
660 }
661 for (i = 0; i < per_input_size; i++)
662 {
663 ccv_nnc_tensor_variable_t const input = inputs[i + t * per_input_size];
664 if (!input || input->type == CCV_NNC_TENSOR_CONSTANT)
665 continue;
666 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)))
;
667 if (!bind->destinations)
668 bind->destinations = ccv_array_new(sizeof(int), 1, 0);
669 ccv_array_add_unique_int(bind->destinations, graph_exec.d);
670 if (input->alias_index_ref)
671 {
672 const int alias_index = input->alias_index_ref - 1;
673 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", 673, __extension__ __PRETTY_FUNCTION__
); }))
;
674 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)))
;
675 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)))
;
676 if (!root_bind->destinations)
677 root_bind->destinations = ccv_array_new(sizeof(int), 1, 0);
678 ccv_array_add_unique_int(root_bind->destinations, graph_exec.d);
679 }
680 }
681 for (i = 0; i < per_output_size; i++)
682 {
683 ccv_nnc_tensor_variable_t const output = outputs[i + t * per_output_size];
684 if (!output)
685 continue;
686 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)))
;
687 assert(!bind->sources)((void) sizeof ((!bind->sources) ? 1 : 0), __extension__ (
{ if (!bind->sources) ; else __assert_fail ("!bind->sources"
, "ccv_nnc_dynamic_graph.c", 687, __extension__ __PRETTY_FUNCTION__
); }))
; // This is a new symbol, therefore, no binded sources associated yet.
688 bind->sources = ccv_array_new(sizeof(int), 1, 0);
689 ccv_array_add_unique_int(bind->sources, graph_exec.d);
690 if (output->alias_index_ref)
691 {
692 const int alias_index = output->alias_index_ref - 1;
693 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", 693, __extension__ __PRETTY_FUNCTION__
); }))
;
694 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)))
;
695 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)))
;
696 if (!root_bind->sources)
697 root_bind->sources = ccv_array_new(sizeof(int), 1, 0);
698 ccv_array_add_unique_int(root_bind->sources, graph_exec.d);
699 }
700 }
701 }
702 }
703 // Now, able to free some of the reused outputs.
704 for (i = 0; i < freeable_size; i++)
705 ccv_nnc_tensor_variable_free(graph, freeables[i]);
706}
707
708int 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)
709{
710 ccv_nnc_dynamic_graph_exec_ret(graph, cmd, hint, flags, inputs, input_size, outputs, output_size, parallel, stream_context, 0);
711 return CCV_NNC_EXEC_SUCCESS;
712}
713
714static 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)
715{
716 if (bind->alias_ref)
717 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)))
;
718 if (!bind->sources || bind->sources->rnum == 0)
719 return 1;
720 int i;
721 for (i = 0; i < bind->sources->rnum; i++)
722 {
723 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)))
;
724 const ccv_nnc_graph_exec_symbol_t exec_symbol = {
725 .d = exec_symbol_d,
726 .graph = graph->tape
727 };
728 const int* outputs; int output_size;
729 ccv_nnc_graph_exec_symbol_io(graph->tape, exec_symbol, 0, 0, &outputs, &output_size);
730 int j;
731 for (j = 0; j < output_size; j++)
732 if (outputs[j] >= 0 && outputs[j] != symbol_d) // If output is me, it is the only output.
733 {
734 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", 734, __extension__ __PRETTY_FUNCTION__
); }))
;
735 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])))
;
736 // This is in use and is it not a constant symbol.
737 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
738 return 0;
739 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
740 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
)))
;
741 if (other_bind->destinations && other_bind->destinations->rnum > 0)
742 return 0;
743 }
744 }
745 return 1;
746}
747
748static 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)
749{
750 int i;
751 if (bind->destinations)
752 {
753 int flag = 0;
754 for (i = 0; !flag && i < bind->destinations->rnum; i++)
755 {
756 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)))
;
757 if (exec_symbol_d == freed_exec_symbol_d)
758 {
759 if (i < bind->destinations->rnum - 1)
760 *(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)))
;
761 --bind->destinations->rnum;
762 flag = 1;
763 }
764 }
765 // This symbol can be freed.
766 if (flag && bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED)
767 {
768 ccv_nnc_tensor_variable_graph_bind_t* root_bind = bind;
769 if (bind->alias_ref)
770 {
771 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)))
;
772 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
773 root_bind = bind;
774 }
775 // If the alias_ref is not freed, we cannot free this, unless it is very clear there is no reference to this any more.
776 // It is possible because exec will be freed already, thus, it is safe to remove this alias out.
777 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED &&
778 ((!root_bind->sources || root_bind->sources->rnum == 0) || _ccv_nnc_tensor_variable_is_only_output(graph, bind, tensor_index)) &&
779 root_bind->destinations->rnum == 0)
780 {
781 if (root_bind->sources)
782 for (i = 0; i < root_bind->sources->rnum; i++)
783 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)))
);
784 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
785 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
786 .d = tensor_index,
787 .graph = graph->tape
788 });
789 } 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.
790 bind->alias_ref && (!bind->sources || bind->sources->rnum == 0) && (!bind->destinations || bind->destinations->rnum == 0)) {
791 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
792 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
793 .d = tensor_index,
794 .graph = graph->tape
795 });
796 }
797 }
798 }
799}
800
801static 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)
802{
803 int i;
804 if (bind->sources)
805 {
806 int flag = 0;
807 for (i = 0; !flag && i < bind->sources->rnum; i++)
808 {
809 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)))
;
810 if (exec_symbol_d == freed_exec_symbol_d)
811 {
812 if (i < bind->sources->rnum - 1)
813 *(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)))
;
814 --bind->sources->rnum;
815 flag = 1;
816 }
817 }
818 // This symbol can be freed.
819 if (flag && bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED)
820 {
821 ccv_nnc_tensor_variable_graph_bind_t* root_bind = bind;
822 if (bind->alias_ref)
823 {
824 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)))
;
825 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
826 root_bind = bind;
827 }
828 // If the alias_ref is not freed, we cannot free this, unless it is very clear there is no reference to this any more.
829 // It is possible because exec will be freed already, thus, it is safe to remove this alias out.
830 if (root_bind->index == CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED &&
831 (root_bind->sources->rnum == 0 || _ccv_nnc_tensor_variable_is_only_output(graph, bind, tensor_index)) &&
832 (!root_bind->destinations || root_bind->destinations->rnum == 0))
833 {
834 for (i = 0; i < root_bind->sources->rnum; i++)
835 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)))
);
836 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
837 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
838 .d = tensor_index,
839 .graph = graph->tape
840 });
841 } 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.
842 bind->alias_ref && (!bind->sources || bind->sources->rnum == 0) && (!bind->destinations || bind->destinations->rnum == 0)) {
843 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
844 ccv_nnc_tensor_symbol_free(graph->tape, (ccv_nnc_tensor_symbol_t){
845 .d = tensor_index,
846 .graph = graph->tape
847 });
848 }
849 }
850 }
851}
852
853static 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)
854{
855 int i;
856 for (i = 0; i < input_size; i++)
857 if (inputs[i] >= 0 && inputs[i] < binds->rnum)
858 {
859 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])))
;
860 if (bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
861 continue;
862 if (bind->alias_ref)
863 {
864 const int alias_to = bind->alias_ref - 1;
865 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)))
;
866 if (root_bind && root_bind->index != CCV_NNC_TENSOR_NO_VARIABLE)
867 _ccv_nnc_update_bind_destinations_when_free(graph, freed_exec_symbol_d, binds, root_bind, alias_to, ws);
868 }
869 _ccv_nnc_update_bind_destinations_when_free(graph, freed_exec_symbol_d, binds, bind, inputs[i], ws);
870 }
871 // Note that this works because there is no overlap of inputs / outputs. (What about alias?).
872 for (i = 0; i < output_size; i++)
873 if (outputs[i] >= 0 && outputs[i] < binds->rnum)
874 {
875 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])))
;
876 if (bind->index == CCV_NNC_TENSOR_NO_VARIABLE)
877 continue;
878 if (bind->alias_ref)
879 {
880 const int alias_to = bind->alias_ref - 1;
881 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)))
;
882 if (root_bind && root_bind->index != CCV_NNC_TENSOR_NO_VARIABLE)
883 _ccv_nnc_update_bind_sources_when_free(graph, freed_exec_symbol_d, binds, root_bind, alias_to, ws);
884 }
885 _ccv_nnc_update_bind_sources_when_free(graph, freed_exec_symbol_d, binds, bind, outputs[i], ws);
886 }
887}
888
889static void _ccv_nnc_stateful_exec_free_if_possible(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_graph_exec_symbol_t symbol)
890{
891 if (!graph->stateful_execs)
892 return;
893 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", 893, __extension__ __PRETTY_FUNCTION__
); }))
;
894 ccv_array_t* const stateful_execs = graph->stateful_execs;
895 ccv_nnc_cmd_t cmd = ccv_nnc_graph_exec_symbol_cmd(graph->tape, symbol);
896 ccv_nnc_stateful_exec_t* const stateful_exec = (ccv_nnc_stateful_exec_t*)cmd.data;
897 if (!stateful_exec)
898 return;
899 // If there is no backward, no need to apply gradients.
900 // Otherwise, if we applied gradients, we can free it as well.
901 // We don't free this stateful exec because apply gradients doesn't require any variables alive.
902 if (!stateful_exec->did_backward_but_not_apply_gradients)
903 {
904 const int index = stateful_exec->index;
905 ccfreefree(stateful_exec);
906 if (index < graph->reuse_stateful_exec || graph->reuse_stateful_exec < 0)
907 graph->reuse_stateful_exec = index;
908 *(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;
909 } else
910 stateful_exec->should_free = 1;
911}
912
913void ccv_nnc_tensor_variable_free(ccv_nnc_dynamic_graph_t* const graph, const ccv_nnc_tensor_variable_t tensor_variable)
914{
915 // If it contains a symbol, this tensor variable is not a free variable. It is either used as input or output.
916 if (tensor_variable->symbol.d != CCV_NNC_NO_TENSOR_SYMBOL)
917 {
918 // If it is not a free variable, when can we free the symbol and the underlying variable?
919 // 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;
920 // 2. The destinations (the commands that uses this tensor) should have no other inputs, or the other inputs has no binded sources as well.
921 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
)))
;
922 // There should be no source associated with it no more.
923 int free_symbol = 0;
924 // 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
925 // compute gradient because I am the only variable it can compute gradient for).
926 ccv_nnc_tensor_variable_graph_bind_t* root_bind = bind;
927 if (bind->alias_ref)
928 {
929 const int alias_to = bind->alias_ref - 1;
930 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)))
;
931 }
932 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);
933 if (!root_bind->sources || root_bind->sources->rnum == 0 || sources_and_is_only_output)
934 {
935 int i, j;
936 free_symbol = 1; // Assume we can free this symbol.
937 if (!graph->ws)
938 graph->ws = ccv_array_new(sizeof(int), root_bind->destinations ? root_bind->destinations->rnum : 0, 0);
939 ccv_array_t* const ws = graph->ws;
940 ccv_array_clear(ws);
941 if (root_bind->destinations)
942 for (i = 0; i < root_bind->destinations->rnum; i++)
943 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)))
);
944 const int ws_init_size = ws->rnum;
945 // Add all sources from root_bind, in case it has been freed (during update bind sources / destinations when free.
946 if (root_bind->sources)
947 for (i = 0; i < root_bind->sources->rnum; i++)
948 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)))
);
949 // If we cannot loop over any exec symbols (this is not in use). It is simple to determine whether we want
950 // to free it or not: if this is an alias and the origin is not freed, we cannot free this symbol.
951 if (ws_init_size == 0)
952 free_symbol = (!bind->alias_ref || root_bind->index < 0);
953 // Go through all the exec symbols use this tensor, to see whether they have inputs that has other sources.
954 for (i = 0; i < ws_init_size; i++)
955 {
956 const int exec_symbol_d = *(int*)ccv_array_get(ws, i)((void*)(((char*)((ws)->data)) + (size_t)(ws)->rsize * (
size_t)(i)))
;
957 const ccv_nnc_graph_exec_symbol_t symbol = {
958 .d = exec_symbol_d,
959 .graph = graph->tape
960 };
961 const int* inputs; int input_size;
962 const int* outputs; int output_size;
963 ccv_nnc_graph_exec_symbol_io(graph->tape, symbol, &inputs, &input_size, &outputs, &output_size);
964 int flag = 0; // flag denotes whether there are cases to keep this exec symbol.
965 if (sources_and_is_only_output)
966 {
967 // If there are sources, check whether we have outputs or not. If we do, we cannot free this.
968 for (j = 0; !flag && j < output_size; j++)
969 if (outputs[j] >= 0 && outputs[j] < graph->binds->rnum)
970 {
971 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])))
;
972 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
973 flag = 1;
974 else {
975 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
976 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
)))
;
977 flag = (other_bind->destinations && other_bind->destinations->rnum > 0);
978 }
979 }
980 } else {
981 // If there is no sources, check if other sources can depend on this exec, if they do, we cannot free this.
982 for (j = 0; !flag && j < input_size; j++)
983 if (inputs[j] >= 0 && inputs[j] < graph->binds->rnum && inputs[j] != tensor_variable->symbol.d)
984 {
985 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])))
;
986 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
987 flag = 1;
988 else {
989 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
990 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
)))
;
991 flag = (other_bind->sources && other_bind->sources->rnum > 0);
992 }
993 }
994 }
995 // This exec can be freed if there is no input required or there is no output required.
996 free_symbol = (free_symbol && !flag);
997 if (!flag)
998 {
999 // Go over inputs and remove all references from binded destinations.
1000 // and go over outputs remove all references from binded sources.
1001 _ccv_nnc_update_bind_sources_destinations_when_free(graph, exec_symbol_d, graph->binds, inputs, input_size, outputs, output_size, ws);
1002 const int* outgoings; int outgoing_size;
1003 ccv_nnc_graph_exec_symbol_to(graph->tape, symbol, &outgoings, &outgoing_size);
1004 for (j = 0; j < outgoing_size; j++)
1005 ccv_array_add_unique_int(ws, outgoings[j]);
1006 _ccv_nnc_stateful_exec_free_if_possible(graph, symbol);
1007 ccv_nnc_graph_exec_symbol_free(graph->tape, symbol);
1008 }
1009 }
1010 if (free_symbol)
1011 {
1012 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
1013 ccv_nnc_tensor_symbol_free(graph->tape, tensor_variable->symbol);
1014 // Now, go over the outgoings, if it is removed, add more to it. Note that the ws array can grow while iterating over.
1015 for (i = ws_init_size; i < ws->rnum; i++)
1016 {
1017 const int exec_symbol_d = *(int*)ccv_array_get(ws, i)((void*)(((char*)((ws)->data)) + (size_t)(ws)->rsize * (
size_t)(i)))
;
1018 const ccv_nnc_graph_exec_symbol_t symbol = {
1019 .d = exec_symbol_d,
1020 .graph = graph->tape
1021 };
1022 const int* inputs; int input_size;
1023 const int* outputs; int output_size;
1024 ccv_nnc_graph_exec_symbol_io(graph->tape, symbol, &inputs, &input_size, &outputs, &output_size);
1025 int flag = 0;
1026 for (j = 0; !flag && j < input_size; j++)
1027 if (inputs[j] >= 0 && inputs[j] < graph->binds->rnum)
1028 {
1029 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])))
;
1030 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
1031 flag = 1;
1032 else {
1033 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
1034 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
)))
;
1035 flag = (other_bind->sources && other_bind->sources->rnum > 0);
1036 }
1037 }
1038 if (flag) // If any inputs make free this destination impossible. Check whether all its outputs are done.
1039 {
1040 int output_flag = 0;
1041 for (j = 0; !output_flag && j < output_size; j++)
1042 if (outputs[j] >= 0 && outputs[j] < graph->binds->rnum)
1043 {
1044 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])))
;
1045 if (other_bind->index >= 0 && other_bind->type != CCV_NNC_TENSOR_CONSTANT)
1046 output_flag = 1;
1047 else {
1048 if (other_bind->alias_ref) // If this is alias, use its original's destinations.
1049 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
)))
;
1050 output_flag = (other_bind->destinations && other_bind->destinations->rnum > 0);
1051 }
1052 }
1053 if (!output_flag) // If no output is used (used means it has a tensor variable, or it has a destination).
1054 flag = 0;
1055 }
1056 // Went over all the inputs, it turns out no more inputs has other references, safe to remove.
1057 if (!flag)
1058 {
1059 _ccv_nnc_update_bind_sources_destinations_when_free(graph, exec_symbol_d, graph->binds, inputs, input_size, outputs, output_size, ws);
1060 const int* outgoings; int outgoing_size;
1061 ccv_nnc_graph_exec_symbol_to(graph->tape, symbol, &outgoings, &outgoing_size);
1062 // It it has outgoings, add that for further inspection.
1063 for (j = 0; j < outgoing_size; j++)
1064 ccv_array_add_unique_int(ws, outgoings[j]);
1065 _ccv_nnc_stateful_exec_free_if_possible(graph, symbol);
1066 ccv_nnc_graph_exec_symbol_free(graph->tape, symbol);
1067 }
1068 }
1069 }
1070 }
1071 // If this symbol is not freed, move the tensor view to the bind.
1072 if (!free_symbol)
1073 {
1074 // If current bind is an alias, and it doesn't have any sources or destinations. We cannot find this alias
1075 // through any exec. This is not only safe to delete, but has to be deleted. We don't need to handle this
1076 // if free_symbol is true, because when that happens, root_bind will be deleted, and we will clean up the
1077 // alias in that process.
1078 if (bind->alias_ref && (!bind->sources || bind->sources->rnum == 0) && (!bind->destinations || bind->destinations->rnum == 0))
1079 {
1080 _ccv_nnc_tensor_variable_graph_bind_free(graph, bind, 1);
1081 ccv_nnc_tensor_symbol_free(graph->tape, tensor_variable->symbol);
1082 } else {
1083 bind->index = CCV_NNC_TENSOR_NO_VARIABLE_BUT_USED; // This tensor variable will be freed, but this symbol extra will continue exists.
1084 bind->destructor_hook.func = tensor_variable->destructor_hook.func; // Transfer the destructor callback.
1085 bind->destructor_hook.context = tensor_variable->destructor_hook.context; // Transfer the destructor callback context.
1086 bind->tensor_view = tensor_variable->tensor_view; // Transfer the ownership to the bind.
1087 tensor_variable->tensor_view = 0;
1088 }
1089 }
1090 }
1091 _ccv_nnc_tensor_variable_free(graph, tensor_variable, 1);
1092}
1093
1094void 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)
1095{
1096 int i, j;
1097 ccv_array_t* const sources_destinations = ccv_array_new(sizeof(ccv_nnc_graph_exec_symbol_t), source_variable_size + destination_variable_size, 0);
1098 for (i = 0; i < source_variable_size; i++)
1099 {
1100 if (source_variables[i]->symbol.d < 0)
1101 continue;
1102 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)))
;
1103 if (bind->destinations && bind->destinations->rnum > 0)
1104 for (j = 0; j < bind->destinations->rnum; j++)
1105 {
1106 // It is ok to have duplicate symbols.
1107 const int d = *(int*)ccv_array_get(bind->destinations, j)((void*)(((char*)((bind->destinations)->data)) + (size_t
)(bind->destinations)->rsize * (size_t)(j)))
;
1108 ccv_nnc_graph_exec_symbol_t symbol = {
1109 .d = d,
1110 .graph = graph->tape
1111 };
1112 ccv_array_push(sources_destinations, &symbol);
1113 }
1114 }
1115 const int source_size = sources_destinations->rnum;
1116 for (i = 0; i < destination_variable_size; i++)
1117 {
1118 if (destination_variables[i]->symbol.d < 0)
1119 continue;
1120 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)))
;
1121 if (bind->sources && bind->sources->rnum > 0)
1122 for (j = 0; j < bind->sources->rnum; j++)
1123 {
1124 // It is ok to have duplicate symbols.
1125 const int d = *(int*)ccv_array_get(bind->sources, j)((void*)(((char*)((bind->sources)->data)) + (size_t)(bind
->sources)->rsize * (size_t)(j)))
;
1126 ccv_nnc_graph_exec_symbol_t symbol = {
1127 .d = d,
1128 .graph = graph->tape
1129 };
1130 ccv_array_push(sources_destinations, &symbol);
1131 }
1132 }
1133 const int destination_size = sources_destinations->rnum - source_size;
1134 if (source_size == 0 || destination_size == 0)
1135 {
1136 ccv_array_free(sources_destinations);
1137 return;
1138 }
1139 const int bitmask_size = ((source_size + 63) >> 6);
1140 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", 1140, __extension__ __PRETTY_FUNCTION__
); }))
;
1141 uint64_t exec_bitmask[bitmask_size];
1142 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);
1143 int k = 0;
1144 for (i = 0; i < source_variable_size; i++)
1145 {
1146 if (source_variables[i]->symbol.d < 0)
1147 {
1148 bitmask[i >> 6] &= ~((uint64_t)1 << (i & 63));
1149 continue;
1150 }
1151 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)))
;
1152 int flag = 0;
1153 if (bind->destinations && bind->destinations->rnum > 0)
1154 {
1155 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", 1155, __extension__ __PRETTY_FUNCTION__
); }))
;
1156 for (j = 0; !flag && j < bind->destinations->rnum; j++)
1157 flag = (((uint64_t)1 << ((k + j) & 63)) & exec_bitmask[(k + j) >> 6]);
1158 k += bind->destinations->rnum;
1159 }
1160 if (flag)
1161 bitmask[i >> 6] |= ((uint64_t)1 << (i & 63));
1162 else
1163 bitmask[i >> 6] &= ~((uint64_t)1 << (i & 63));
1164 }
1165 ccv_array_free(sources_destinations);
1166}
1167
1168int ccv_nnc_dynamic_graph_bookkeeping_count(const ccv_nnc_dynamic_graph_t* const graph, const int type)
1169{
1170 return ccv_nnc_symbolic_graph_active_symbol_count(graph->tape, type);
1171}
1172
1173void ccv_nnc_dynamic_graph_dot(const ccv_nnc_dynamic_graph_t* const graph, const int flags, FILE* out)
1174{
1175 ccv_nnc_symbolic_graph_dot(graph->tape, flags, out);
1176}