Bug Summary

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