Bug Summary

File:nnc/./_ccv_nnc_stream.h
Warning:line 64, column 1
Array access (via field 'flags') results in a null pointer dereference

Annotated Source Code

Press '?' to see keyboard shortcuts

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

./_ccv_nnc_stream.h

1/**********************************************************
2 * C-based/Cached/Core Computer Vision Library
3 * Liu Liu, 2010-02-01
4 **********************************************************/
5
6/**********************************************************
7 * CCV - Neural Network Collection
8 **********************************************************/
9
10#ifndef GUARD_ccv_nnc_stream_internal_h
11#define GUARD_ccv_nnc_stream_internal_h
12
13#include "ccv_nnc.h"
14#include "co.h"
15#include "3rdparty/sfmt/SFMT.h"
16#include "3rdparty/khash/khash.h"
17
18struct ccv_nnc_stream_signal_s {
19 int type;
20 ccv_nnc_stream_context_t* emit_context;
21};
22
23typedef struct {
24 // Empty, this will hold things such as NCCL communicator in subclass.
25 // Putting dummy because C / C++ treats empty struct differently.
26 int dummy;
27} ccv_nnc_stream_resource_container_t;
28
29struct ccv_nnc_stream_context_s {
30 int type;
31 // For resource container
32 ccv_nnc_stream_resource_container_t* _inline_container[1];
33 ccv_nnc_stream_resource_container_t** resource_container;
34 // For scheduler
35 co_routine_t* main; // main task.
36 co_scheduler_t* scheduler;
37 // For neighbor discovery
38 ccv_nnc_stream_context_neighbor_discovery_f neighbor_discovery;
39 void* neighbor_discovery_context;
40 // For hooks
41 ccv_array_t* destructor_hooks;
42 int reuse_destructor_hook;
43 ccv_nnc_stream_signal_t* event;
44 // For random number generator.
45 sfmt_t* sfmt;
46};
47
48// Return the scheduler from a stream (if not created, create one).
49CCV_WARN_UNUSED(co_scheduler_t*)co_scheduler_t* __attribute__((warn_unused_result)) ccv_nnc_stream_context_get_scheduler(ccv_nnc_stream_context_t* const stream_context);
50
51#define co_stream_await(_stream)do { if (!_co_stream_await(_self_, _stream)) { return (co_state_t
){ 51, 0 }; } case 51: ; } while (0)
do { if (!_co_stream_await(_self_, _stream)) { return (co_state_t){ __LINE__51, 0 }; } case __LINE__51: ; } while (0)
52int _co_stream_await(co_routine_t* const self, ccv_nnc_stream_context_t* const stream);
53
54typedef struct ccv_nnc_async_callback_s {
55 ccv_nnc_callback_f fn;
56 void* callback_context;
57} ccv_nnc_async_callback_t;
58
59#ifndef GUARD_ccv_nnc_async_callback_f
60#define GUARD_ccv_nnc_async_callback_f
61typedef void(*ccv_nnc_async_callback_f)(ccv_nnc_async_callback_t* const async);
62#endif
63
64KHASH_MAP_INIT_INT(stream_map, ccv_nnc_stream_context_t*)typedef struct kh_stream_map_s { khint_t n_buckets, size, n_occupied
, upper_bound; khint32_t *flags; khint32_t *keys; ccv_nnc_stream_context_t
* *vals; } kh_stream_map_t; static inline __attribute__ ((__unused__
)) kh_stream_map_t *kh_init_stream_map(void) { return (kh_stream_map_t
*)calloc(1,sizeof(kh_stream_map_t)); } static inline __attribute__
((__unused__)) void kh_destroy_stream_map(kh_stream_map_t *h
) { if (h) { free((void *)h->keys); free(h->flags); free
((void *)h->vals); free(h); } } static inline __attribute__
((__unused__)) void kh_clear_stream_map(kh_stream_map_t *h) {
if (h && h->flags) { memset(h->flags, 0xaa, ((
h->n_buckets) < 16? 1 : (h->n_buckets)>>4) * sizeof
(khint32_t)); h->size = h->n_occupied = 0; } } static inline
__attribute__ ((__unused__)) khint_t kh_get_stream_map(const
kh_stream_map_t *h, khint32_t key) { if (h->n_buckets) { khint_t
k, i, last, mask, step = 0; mask = h->n_buckets - 1; k = (
khint32_t)(key); i = k & mask; last = i; while (!((h->
flags[i>>4]>>((i&0xfU)<<1))&2) &&
(((h->flags[i>>4]>>((i&0xfU)<<1))&
1) || !((h->keys[i]) == (key)))) { i = (i + (++step)) &
mask; if (i == last) return h->n_buckets; } return ((h->
flags[i>>4]>>((i&0xfU)<<1))&3)? h->
n_buckets : i; } else return 0; } static inline __attribute__
((__unused__)) int kh_resize_stream_map(kh_stream_map_t *h, khint_t
new_n_buckets) { khint32_t *new_flags = 0; khint_t j = 1; { (
--(new_n_buckets), (new_n_buckets)|=(new_n_buckets)>>1,
(new_n_buckets)|=(new_n_buckets)>>2, (new_n_buckets)|=
(new_n_buckets)>>4, (new_n_buckets)|=(new_n_buckets)>>
8, (new_n_buckets)|=(new_n_buckets)>>16, ++(new_n_buckets
)); if (new_n_buckets < 4) new_n_buckets = 4; if (h->size
>= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0
; else { new_flags = (khint32_t*)malloc(((new_n_buckets) <
16? 1 : (new_n_buckets)>>4) * sizeof(khint32_t)); if (
!new_flags) return -1; memset(new_flags, 0xaa, ((new_n_buckets
) < 16? 1 : (new_n_buckets)>>4) * sizeof(khint32_t))
; if (h->n_buckets < new_n_buckets) { khint32_t *new_keys
= (khint32_t*)realloc((void *)h->keys,new_n_buckets * sizeof
(khint32_t)); if (!new_keys) { free(new_flags); return -1; } h
->keys = new_keys; if (1) { ccv_nnc_stream_context_t* *new_vals
= (ccv_nnc_stream_context_t**)realloc((void *)h->vals,new_n_buckets
* sizeof(ccv_nnc_stream_context_t*)); if (!new_vals) { free(
new_flags); return -1; } h->vals = new_vals; } } } } if (j
) { for (j = 0; j != h->n_buckets; ++j) { if (((h->flags
[j>>4]>>((j&0xfU)<<1))&3) == 0) { khint32_t
key = h->keys[j]; ccv_nnc_stream_context_t* val; khint_t new_mask
; new_mask = new_n_buckets - 1; if (1) val = h->vals[j]; (
h->flags[j>>4]|=1ul<<((j&0xfU)<<1));
while (1) { khint_t k, i, step = 0; k = (khint32_t)(key); i =
k & new_mask; while (!((new_flags[i>>4]>>((i
&0xfU)<<1))&2)) i = (i + (++step)) & new_mask
; (new_flags[i>>4]&=~(2ul<<((i&0xfU)<<
1))); if (i < h->n_buckets && ((h->flags[i>>
4]>>((i&0xfU)<<1))&3) == 0) { { khint32_t
tmp = h->keys[i]; h->keys[i] = key; key = tmp; } if (1
) { ccv_nnc_stream_context_t* tmp = h->vals[i]; h->vals
[i] = val; val = tmp; } (h->flags[i>>4]|=1ul<<
((i&0xfU)<<1)); } else { h->keys[i] = key; if (1
) h->vals[i] = val; break; } } } } if (h->n_buckets >
new_n_buckets) { h->keys = (khint32_t*)realloc((void *)h->
keys,new_n_buckets * sizeof(khint32_t)); if (1) h->vals = (
ccv_nnc_stream_context_t**)realloc((void *)h->vals,new_n_buckets
* sizeof(ccv_nnc_stream_context_t*)); } free(h->flags); h
->flags = new_flags; h->n_buckets = new_n_buckets; h->
n_occupied = h->size; h->upper_bound = (khint_t)(h->
n_buckets * __ac_HASH_UPPER + 0.5); } return 0; } static inline
__attribute__ ((__unused__)) khint_t kh_put_stream_map(kh_stream_map_t
*h, khint32_t key, int *ret) { khint_t x; if (h->n_occupied
>= h->upper_bound) { if (h->n_buckets > (h->size
<<1)) { if (kh_resize_stream_map(h, h->n_buckets - 1
) < 0) { *ret = -1; return h->n_buckets; } } else if (kh_resize_stream_map
(h, h->n_buckets + 1) < 0) { *ret = -1; return h->n_buckets
; } } { khint_t k, i, site, last, mask = h->n_buckets - 1,
step = 0; x = site = h->n_buckets; k = (khint32_t)(key); i
= k & mask; if (((h->flags[i>>4]>>((i&
0xfU)<<1))&2)) x = i; else { last = i; while (!((h->
flags[i>>4]>>((i&0xfU)<<1))&2) &&
(((h->flags[i>>4]>>((i&0xfU)<<1))&
1) || !((h->keys[i]) == (key)))) { if (((h->flags[i>>
4]>>((i&0xfU)<<1))&1)) site = i; i = (i +
(++step)) & mask; if (i == last) { x = site; break; } } if
(x == h->n_buckets) { if (((h->flags[i>>4]>>
((i&0xfU)<<1))&2) && site != h->n_buckets
) x = site; else x = i; } } } if (((h->flags[x>>4]>>
((x&0xfU)<<1))&2)) { h->keys[x] = key; (h->
flags[x>>4]&=~(3ul<<((x&0xfU)<<1)))
; ++h->size; ++h->n_occupied; *ret = 1; } else if (((h->
flags[x>>4]>>((x&0xfU)<<1))&1)) { h
->keys[x] = key; (h->flags[x>>4]&=~(3ul<<
((x&0xfU)<<1))); ++h->size; *ret = 2; } else *ret
= 0; return x; } static inline __attribute__ ((__unused__)) void
kh_del_stream_map(kh_stream_map_t *h, khint_t x) { if (x != h
->n_buckets && !((h->flags[x>>4]>>(
(x&0xfU)<<1))&3)) { (h->flags[x>>4]|=1ul
<<((x&0xfU)<<1)); --h->size; } }
;
45
Null pointer value stored to field 'flags'
48
Taking true branch
49
Taking false branch
50
Calling 'kh_resize_stream_map'
51
Taking true branch
52
Assuming the condition is true
53
Taking true branch
54
Taking false branch
55
Returning without writing to 'h->flags'
56
Returning from 'kh_resize_stream_map'
57
Taking false branch
58
Array access (via field 'flags') results in a null pointer dereference
65
66#endif