File: | nnc/ccv_nnc_symbolic_graph_io.c |
Warning: | line 670, column 31 Passed-by-value struct argument contains uninitialized data (e.g., field: 'reserved') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | #include "ccv_nnc.h" | ||||
2 | #include "ccv_nnc_easy.h" | ||||
3 | #include "ccv_nnc_internal.h" | ||||
4 | #include "ccv_internal.h" | ||||
5 | #include "_ccv_nnc_symbolic_graph.h" | ||||
6 | #include "3rdparty/sqlite3/sqlite3.h" | ||||
7 | #ifdef HAVE_CUDA1 | ||||
8 | #include "gpu/ccv_nnc_compat.h" | ||||
9 | #endif | ||||
10 | |||||
11 | // MARK - Level-3 API | ||||
12 | |||||
13 | #ifdef NDEBUG | ||||
14 | #define SQLITE_ENFORCE(stmt)((void) sizeof ((stmt) ? 1 : 0), __extension__ ({ if (stmt) ; else __assert_fail ("stmt", "ccv_nnc_symbolic_graph_io.c", 14 , __extension__ __PRETTY_FUNCTION__); })) (void)(stmt) | ||||
15 | #else | ||||
16 | #define SQLITE_ENFORCEassert assert | ||||
17 | #endif | ||||
18 | |||||
19 | static int _ccv_nnc_symbolic_graph_index_in_repo(const ccv_nnc_symbolic_graph_t* const graph, const ccv_array_t* const repo) | ||||
20 | { | ||||
21 | if (!graph) | ||||
22 | return -1; | ||||
23 | int i; | ||||
24 | for (i = 0; i < repo->rnum; i++) | ||||
25 | if (*(ccv_nnc_symbolic_graph_t**)ccv_array_get(repo, i)((void*)(((char*)((repo)->data)) + (size_t)(repo)->rsize * (size_t)(i))) == graph) | ||||
26 | return i; | ||||
27 | return -1; | ||||
28 | } | ||||
29 | |||||
30 | static void _ccv_nnc_symbolic_graph_write(const ccv_nnc_symbolic_graph_t* const graph, const ccv_array_t* const repo, const int graph_idx, sqlite3_stmt* const tensor_symbol_insert_stmt, sqlite3_stmt* const exec_symbol_insert_stmt, sqlite3_stmt* const graph_insert_stmt, ccv_array_t* const ws) | ||||
31 | { | ||||
32 | int i; | ||||
33 | for (i = 0; i < graph->tensor_symbol_info->rnum; i++) | ||||
34 | { | ||||
35 | const ccv_nnc_tensor_symbol_info_t* const symbol_info = (ccv_nnc_tensor_symbol_info_t*)ccv_array_get(graph->tensor_symbol_info, i)((void*)(((char*)((graph->tensor_symbol_info)->data)) + (size_t)(graph->tensor_symbol_info)->rsize * (size_t)( i))); | ||||
36 | sqlite3_bind_int(tensor_symbol_insert_stmt, 1, i); | ||||
37 | sqlite3_bind_int(tensor_symbol_insert_stmt, 2, graph_idx); | ||||
38 | sqlite3_bind_int(tensor_symbol_insert_stmt, 3, symbol_info->assign_ref); | ||||
39 | sqlite3_bind_int(tensor_symbol_insert_stmt, 4, symbol_info->r_assign_ref); | ||||
40 | sqlite3_bind_int(tensor_symbol_insert_stmt, 5, symbol_info->bypass_ref); | ||||
41 | sqlite3_bind_int(tensor_symbol_insert_stmt, 6, symbol_info->r_bypass_ref); | ||||
42 | sqlite3_bind_int(tensor_symbol_insert_stmt, 7, symbol_info->p_ref); | ||||
43 | sqlite3_bind_int(tensor_symbol_insert_stmt, 8, symbol_info->alias_ref); | ||||
44 | sqlite3_bind_int(tensor_symbol_insert_stmt, 9, symbol_info->pair_ref); | ||||
45 | sqlite3_bind_int(tensor_symbol_insert_stmt, 10, symbol_info->flags); | ||||
46 | sqlite3_bind_blob(tensor_symbol_insert_stmt, 11, symbol_info->ofs, sizeof(symbol_info->ofs), 0); | ||||
47 | sqlite3_bind_blob(tensor_symbol_insert_stmt, 12, symbol_info->stride, sizeof(symbol_info->stride), 0); | ||||
48 | if (symbol_info->s_ref) | ||||
49 | sqlite3_bind_blob(tensor_symbol_insert_stmt, 13, ccv_array_get(symbol_info->s_ref, 0)((void*)(((char*)((symbol_info->s_ref)->data)) + (size_t )(symbol_info->s_ref)->rsize * (size_t)(0))), sizeof(int) * symbol_info->s_ref->rnum, 0); | ||||
50 | else | ||||
51 | sqlite3_bind_null(tensor_symbol_insert_stmt, 13); | ||||
52 | if (symbol_info->name) | ||||
53 | sqlite3_bind_text(tensor_symbol_insert_stmt, 14, symbol_info->name, -1, 0); | ||||
54 | else | ||||
55 | sqlite3_bind_null(tensor_symbol_insert_stmt, 14); | ||||
56 | sqlite3_bind_int(tensor_symbol_insert_stmt, 15, symbol_info->info.type); | ||||
57 | sqlite3_bind_int(tensor_symbol_insert_stmt, 16, symbol_info->info.format); | ||||
58 | sqlite3_bind_int(tensor_symbol_insert_stmt, 17, symbol_info->info.datatype); | ||||
59 | sqlite3_bind_blob(tensor_symbol_insert_stmt, 18, symbol_info->info.dim, sizeof(symbol_info->info.dim), 0); | ||||
60 | SQLITE_ENFORCE(SQLITE_DONE == sqlite3_step(tensor_symbol_insert_stmt))((void) sizeof ((101 == sqlite3_step(tensor_symbol_insert_stmt )) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(tensor_symbol_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(tensor_symbol_insert_stmt)" , "ccv_nnc_symbolic_graph_io.c", 60, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
61 | sqlite3_reset(tensor_symbol_insert_stmt); | ||||
62 | sqlite3_clear_bindings(tensor_symbol_insert_stmt); | ||||
63 | } | ||||
64 | for (i = 0; i < graph->exec_symbol_info->rnum; i++) | ||||
65 | { | ||||
66 | const ccv_nnc_graph_exec_symbol_info_t* const symbol_info = (ccv_nnc_graph_exec_symbol_info_t*)ccv_array_get(graph->exec_symbol_info, i)((void*)(((char*)((graph->exec_symbol_info)->data)) + ( size_t)(graph->exec_symbol_info)->rsize * (size_t)(i))); | ||||
67 | sqlite3_bind_int(exec_symbol_insert_stmt, 1, i); | ||||
68 | sqlite3_bind_int(exec_symbol_insert_stmt, 2, graph_idx); | ||||
69 | sqlite3_bind_int(exec_symbol_insert_stmt, 3, symbol_info->input_size); | ||||
70 | sqlite3_bind_int(exec_symbol_insert_stmt, 4, symbol_info->output_size); | ||||
71 | sqlite3_bind_int(exec_symbol_insert_stmt, 5, symbol_info->graph_ref_size); | ||||
72 | sqlite3_bind_int(exec_symbol_insert_stmt, 6, symbol_info->flags); | ||||
73 | sqlite3_bind_int(exec_symbol_insert_stmt, 7, symbol_info->pair_ref); | ||||
74 | if (symbol_info->input_size) | ||||
75 | sqlite3_bind_blob(exec_symbol_insert_stmt, 8, symbol_info->inputs, sizeof(int) * symbol_info->input_size, 0); | ||||
76 | if (symbol_info->output_size) | ||||
77 | sqlite3_bind_blob(exec_symbol_insert_stmt, 9, symbol_info->outputs, sizeof(int) * symbol_info->output_size, 0); | ||||
78 | if (symbol_info->outgoings && symbol_info->outgoings->rnum) | ||||
79 | sqlite3_bind_blob(exec_symbol_insert_stmt, 10, ccv_array_get(symbol_info->outgoings, 0)((void*)(((char*)((symbol_info->outgoings)->data)) + (size_t )(symbol_info->outgoings)->rsize * (size_t)(0))), sizeof(int) * symbol_info->outgoings->rnum, 0); | ||||
80 | if (symbol_info->name) | ||||
81 | sqlite3_bind_text(exec_symbol_insert_stmt, 11, symbol_info->name, -1, 0); | ||||
82 | sqlite3_bind_int(exec_symbol_insert_stmt, 12, symbol_info->cmd.cmd); | ||||
83 | sqlite3_bind_int(exec_symbol_insert_stmt, 13, symbol_info->cmd.backend); | ||||
84 | sqlite3_bind_int(exec_symbol_insert_stmt, 14, symbol_info->cmd.algorithm); | ||||
85 | sqlite3_bind_blob(exec_symbol_insert_stmt, 15, &symbol_info->cmd.info, sizeof(symbol_info->cmd.info), 0); | ||||
86 | sqlite3_bind_blob(exec_symbol_insert_stmt, 16, &symbol_info->hint, sizeof(symbol_info->hint), 0); | ||||
87 | if (symbol_info->graph_ref_size) | ||||
88 | sqlite3_bind_blob(exec_symbol_insert_stmt, 17, CCV_NNC_GRAPH_REF(symbol_info)((symbol_info)->_heap_graph_ref ? (symbol_info)->_heap_graph_ref : (symbol_info)->_inline_graph_ref), sizeof(int) * symbol_info->graph_ref_size, 0); | ||||
89 | if (symbol_info->flags & CCV_NNC_GRAPH_EXEC_CASE_OF) | ||||
90 | { | ||||
91 | sqlite3_bind_int(exec_symbol_insert_stmt, 18, 0); | ||||
92 | sqlite3_bind_int(exec_symbol_insert_stmt, 19, symbol_info->case_of.flags); | ||||
93 | sqlite3_bind_int(exec_symbol_insert_stmt, 20, symbol_info->case_of.argument.offset); | ||||
94 | sqlite3_bind_int(exec_symbol_insert_stmt, 21, symbol_info->case_of.argument.size); | ||||
95 | } | ||||
96 | if (symbol_info->flags & CCV_NNC_GRAPH_EXEC_P_WHILE) | ||||
97 | { | ||||
98 | sqlite3_bind_int(exec_symbol_insert_stmt, 22, 0); | ||||
99 | sqlite3_bind_int(exec_symbol_insert_stmt, 23, symbol_info->p_while.input_size); | ||||
100 | if (symbol_info->p_while.input_size) | ||||
101 | sqlite3_bind_blob(exec_symbol_insert_stmt, 24, symbol_info->p_while.inputs, sizeof(int) * symbol_info->p_while.input_size, 0); | ||||
102 | } | ||||
103 | SQLITE_ENFORCE(SQLITE_DONE == sqlite3_step(exec_symbol_insert_stmt))((void) sizeof ((101 == sqlite3_step(exec_symbol_insert_stmt) ) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(exec_symbol_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(exec_symbol_insert_stmt)" , "ccv_nnc_symbolic_graph_io.c", 103, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
104 | sqlite3_reset(exec_symbol_insert_stmt); | ||||
105 | sqlite3_clear_bindings(exec_symbol_insert_stmt); | ||||
106 | } | ||||
107 | ccv_array_clear(ws); | ||||
108 | sqlite3_bind_int(graph_insert_stmt, 1, graph_idx); | ||||
109 | sqlite3_bind_int(graph_insert_stmt, 2, graph->tensor_symbol_info->rnum); | ||||
110 | sqlite3_bind_int(graph_insert_stmt, 3, graph->exec_symbol_info->rnum); | ||||
111 | if (graph->sources && graph->sources->rnum) | ||||
112 | for (i = 0; i < graph->sources->rnum; i++) | ||||
113 | ccv_array_push(ws, &((ccv_nnc_graph_exec_symbol_t*)ccv_array_get(graph->sources, i)((void*)(((char*)((graph->sources)->data)) + (size_t)(graph ->sources)->rsize * (size_t)(i))))->d); | ||||
114 | if (graph->destinations && graph->destinations->rnum) | ||||
115 | for (i = 0; i < graph->destinations->rnum; i++) | ||||
116 | ccv_array_push(ws, &((ccv_nnc_graph_exec_symbol_t*)ccv_array_get(graph->destinations, i)((void*)(((char*)((graph->destinations)->data)) + (size_t )(graph->destinations)->rsize * (size_t)(i))))->d); | ||||
117 | if (graph->sub_graphs && graph->sub_graphs->rnum) | ||||
118 | for (i = 0; i < graph->sub_graphs->rnum; i++) | ||||
119 | { | ||||
120 | const int sub_graph_idx = _ccv_nnc_symbolic_graph_index_in_repo(*(ccv_nnc_symbolic_graph_t**)ccv_array_get(graph->sub_graphs, i)((void*)(((char*)((graph->sub_graphs)->data)) + (size_t )(graph->sub_graphs)->rsize * (size_t)(i))), repo); | ||||
121 | ccv_array_push(ws, &sub_graph_idx); | ||||
122 | } | ||||
123 | if (graph->breakpoint_size && graph->breakpoints) | ||||
124 | for (i = 0; i < graph->breakpoint_size; i++) | ||||
125 | ccv_array_push(ws, &graph->breakpoints[i].d); | ||||
126 | const int* pos = (int*)ccv_array_get(ws, 0)((void*)(((char*)((ws)->data)) + (size_t)(ws)->rsize * ( size_t)(0))); | ||||
127 | if (graph->sources && graph->sources->rnum) | ||||
128 | { | ||||
129 | sqlite3_bind_blob(graph_insert_stmt, 4, pos, sizeof(int) * graph->sources->rnum, 0); | ||||
130 | pos += graph->sources->rnum; | ||||
131 | } | ||||
132 | if (graph->destinations && graph->destinations->rnum) | ||||
133 | { | ||||
134 | sqlite3_bind_blob(graph_insert_stmt, 5, pos, sizeof(int) * graph->destinations->rnum, 0); | ||||
135 | pos += graph->destinations->rnum; | ||||
136 | } | ||||
137 | if (graph->sub_graphs && graph->sub_graphs->rnum) | ||||
138 | { | ||||
139 | sqlite3_bind_blob(graph_insert_stmt, 6, pos, sizeof(int) * graph->sub_graphs->rnum, 0); | ||||
140 | pos += graph->sub_graphs->rnum; | ||||
141 | } | ||||
142 | sqlite3_bind_int(graph_insert_stmt, 7, _ccv_nnc_symbolic_graph_index_in_repo(graph->pair, repo)); | ||||
143 | sqlite3_bind_int(graph_insert_stmt, 8, _ccv_nnc_symbolic_graph_index_in_repo(graph->p, repo)); | ||||
144 | sqlite3_bind_int(graph_insert_stmt, 9, graph->p_idx); | ||||
145 | sqlite3_bind_int(graph_insert_stmt, 10, graph->exec_idx); | ||||
146 | sqlite3_bind_int(graph_insert_stmt, 11, graph->breakpoint_size); | ||||
147 | if (graph->breakpoint_size && graph->breakpoints) | ||||
148 | sqlite3_bind_blob(graph_insert_stmt, 12, pos, sizeof(int) * graph->breakpoint_size, 0); | ||||
149 | sqlite3_bind_int(graph_insert_stmt, 13, graph->backward.tensor_symbol_size); | ||||
150 | if (graph->backward.tensor_symbol_size) | ||||
151 | sqlite3_bind_blob(graph_insert_stmt, 14, graph->backward.tensor_symbol_idx, sizeof(int) * graph->backward.tensor_symbol_size, 0); | ||||
152 | sqlite3_bind_int(graph_insert_stmt, 15, graph->backward.exec_symbol_size); | ||||
153 | if (graph->backward.exec_symbol_size) | ||||
154 | sqlite3_bind_blob(graph_insert_stmt, 16, graph->backward.exec_symbol_idx, sizeof(int) * graph->backward.exec_symbol_size, 0); | ||||
155 | sqlite3_bind_int(graph_insert_stmt, 17, graph->data_parallel.count); | ||||
156 | sqlite3_bind_int(graph_insert_stmt, 18, graph->data_parallel.tensor_symbol_size); | ||||
157 | if (graph->data_parallel.tensor_symbol_idx) | ||||
158 | sqlite3_bind_blob(graph_insert_stmt, 19, graph->data_parallel.tensor_symbol_idx, sizeof(int) * graph->data_parallel.tensor_symbol_size, 0); | ||||
159 | sqlite3_bind_int(graph_insert_stmt, 20, graph->data_parallel.exec_symbol_size); | ||||
160 | if (graph->data_parallel.exec_symbol_idx) | ||||
161 | sqlite3_bind_blob(graph_insert_stmt, 21, graph->data_parallel.exec_symbol_idx, sizeof(int) * graph->data_parallel.exec_symbol_size, 0); | ||||
162 | SQLITE_ENFORCE(SQLITE_DONE == sqlite3_step(graph_insert_stmt))((void) sizeof ((101 == sqlite3_step(graph_insert_stmt)) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(graph_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(graph_insert_stmt)" , "ccv_nnc_symbolic_graph_io.c", 162, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
163 | sqlite3_reset(graph_insert_stmt); | ||||
164 | sqlite3_clear_bindings(graph_insert_stmt); | ||||
165 | } | ||||
166 | |||||
167 | static void _ccv_nnc_symbolic_graph_push_repo(const ccv_nnc_symbolic_graph_t* const graph, ccv_array_t* const repo) | ||||
168 | { | ||||
169 | ccv_array_push(repo, &graph); | ||||
170 | int i; | ||||
171 | if (graph->sub_graphs && graph->sub_graphs->rnum) | ||||
172 | for (i = 0; i < graph->sub_graphs->rnum; i++) | ||||
173 | { | ||||
174 | const ccv_nnc_symbolic_graph_t* const sub_graph = *(ccv_nnc_symbolic_graph_t**)ccv_array_get(graph->sub_graphs, i)((void*)(((char*)((graph->sub_graphs)->data)) + (size_t )(graph->sub_graphs)->rsize * (size_t)(i))); | ||||
175 | if (sub_graph) | ||||
176 | _ccv_nnc_symbolic_graph_push_repo(sub_graph, repo); | ||||
177 | } | ||||
178 | } | ||||
179 | |||||
180 | void ccv_nnc_symbolic_graph_write(const ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_bind_t* const tensor_binds, const int tensor_bind_size, const char* const fn) | ||||
181 | { | ||||
182 | sqlite3* conn = 0; | ||||
183 | if (SQLITE_OK0 != sqlite3_open(fn, &conn)) | ||||
184 | return; | ||||
185 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, "BEGIN", 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(conn, "BEGIN", 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec(conn, "BEGIN" , 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(conn, \"BEGIN\", 0, 0, 0)" , "ccv_nnc_symbolic_graph_io.c", 185, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
186 | const char tensor_symbol_create_table_qs[] = "CREATE TABLE IF NOT EXISTS tensor_symbol " | ||||
187 | "(id INTEGER, graph INTEGER, assign_ref INTEGER, r_assign_ref INTEGER, " | ||||
188 | "bypass_ref INTEGER, r_bypass_ref INTEGER, p_ref INTEGER, alias_ref INTEGER, pair_ref INTEGER, " | ||||
189 | "flags INTEGER, ofs BLOB, stride BLOB, s_ref BLOB, name TEXT, type INTEGER, format INTEGER, " | ||||
190 | "datatype INTEGER, dim BLOB, PRIMARY KEY (id, graph))"; | ||||
191 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, tensor_symbol_create_table_qs, 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(conn, tensor_symbol_create_table_qs , 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec( conn, tensor_symbol_create_table_qs, 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(conn, tensor_symbol_create_table_qs, 0, 0, 0)" , "ccv_nnc_symbolic_graph_io.c", 191, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
192 | const char tensor_symbol_insert_qs[] = | ||||
193 | "REPLACE INTO tensor_symbol " | ||||
194 | "(id, graph, assign_ref, r_assign_ref, bypass_ref, r_bypass_ref, p_ref, alias_ref, pair_ref, flags, " | ||||
195 | "ofs, stride, s_ref, name, type, format, datatype, dim) VALUES " | ||||
196 | "($id, $graph, $assign_ref, $r_assign_ref, $bypass_ref, $r_bypass_ref, $p_ref, $alias_ref, $pair_ref, " | ||||
197 | "$flags, $ofs, $stride, $s_ref, $name, $type, $format, $datatype, $dim)"; | ||||
198 | sqlite3_stmt* tensor_symbol_insert_stmt = 0; | ||||
199 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, tensor_symbol_insert_qs, sizeof(tensor_symbol_insert_qs), &tensor_symbol_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, tensor_symbol_insert_qs , sizeof(tensor_symbol_insert_qs), &tensor_symbol_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( conn, tensor_symbol_insert_qs, sizeof(tensor_symbol_insert_qs ), &tensor_symbol_insert_stmt, 0)) ; else __assert_fail ( "SQLITE_OK == sqlite3_prepare_v2(conn, tensor_symbol_insert_qs, sizeof(tensor_symbol_insert_qs), &tensor_symbol_insert_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 199, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
200 | |||||
201 | const char exec_symbol_create_table_qs[] = "CREATE TABLE IF NOT EXISTS graph_exec_symbol " | ||||
202 | "(id INTEGER, graph INTEGER, input_size INTEGER, output_size INTEGER, graph_ref_size INTEGER, " | ||||
203 | "flags INTEGER, pair_ref INTEGER, inputs BLOB, outputs BLOB, outgoings BLOB, name TEXT, " | ||||
204 | "cmd_cmd INTEGER, cmd_backend INTEGER, cmd_algorithm INTEGER, cmd_info BLOB, hint BLOB, graph_ref BLOB, " | ||||
205 | "case_of_expr INTEGER, case_of_flags INTEGER, case_of_argument_offset INTEGER, case_of_argument_size INTEGER, " | ||||
206 | "p_while_expr INTEGER, p_while_input_size INTEGER, p_while_inputs BLOB, PRIMARY KEY (id, graph))"; | ||||
207 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, exec_symbol_create_table_qs, 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(conn, exec_symbol_create_table_qs , 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec( conn, exec_symbol_create_table_qs, 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(conn, exec_symbol_create_table_qs, 0, 0, 0)" , "ccv_nnc_symbolic_graph_io.c", 207, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
208 | const char exec_symbol_insert_qs[] = | ||||
209 | "REPLACE INTO graph_exec_symbol " | ||||
210 | "(id, graph, input_size, output_size, graph_ref_size, flags, pair_ref, inputs, outputs, outgoings, " | ||||
211 | "name, cmd_cmd, cmd_backend, cmd_algorithm, cmd_info, hint, graph_ref, case_of_expr, case_of_flags, " | ||||
212 | "case_of_argument_offset, case_of_argument_size, p_while_expr, p_while_input_size, p_while_inputs) " | ||||
213 | "VALUES ($id, $graph, $input_size, $output_size, $graph_ref_size, $flags, $pair_ref, $inputs, $outputs, " | ||||
214 | "$outgoings, $name, $cmd_cmd, $cmd_backend, $cmd_algorithm, $cmd_info, $hint, $graph_ref, $case_of_expr, " | ||||
215 | "$case_of_flags, $case_of_argument_offset, $case_of_argument_size, $p_while_expr, $p_while_input_size, " | ||||
216 | "$p_while_inputs)"; | ||||
217 | sqlite3_stmt* exec_symbol_insert_stmt = 0; | ||||
218 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, exec_symbol_insert_qs, sizeof(exec_symbol_insert_qs), &exec_symbol_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, exec_symbol_insert_qs , sizeof(exec_symbol_insert_qs), &exec_symbol_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( conn, exec_symbol_insert_qs, sizeof(exec_symbol_insert_qs), & exec_symbol_insert_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(conn, exec_symbol_insert_qs, sizeof(exec_symbol_insert_qs), &exec_symbol_insert_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 218, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
219 | |||||
220 | const char graph_create_table_qs[] = "CREATE TABLE IF NOT EXISTS graph " | ||||
221 | "(graph INTEGER PRIMARY KEY, tensor_symbol_size INTEGER, exec_symbol_size INTEGER, sources BLOB, " | ||||
222 | "destinations BLOB, sub_graphs BLOB, pair INTEGER, p INTEGER, p_idx INTEGER, exec_idx INTEGER, " | ||||
223 | "breakpoint_size INTEGER, breakpoints BLOB, backward_tensor_symbol_size INTEGER, " | ||||
224 | "backward_tensor_symbol_idx BLOB, backward_exec_symbol_size INTEGER, backward_exec_symbol_idx BLOB, " | ||||
225 | "parallel_count INTEGER, parallel_tensor_symbol_size INTEGER, parallel_tensor_symbol_idx BLOB, " | ||||
226 | "parallel_exec_symbol_size INTEGER, parallel_exec_symbol_idx BLOB)"; | ||||
227 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, graph_create_table_qs, 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(conn, graph_create_table_qs , 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec( conn, graph_create_table_qs, 0, 0, 0)) ; else __assert_fail ( "SQLITE_OK == sqlite3_exec(conn, graph_create_table_qs, 0, 0, 0)" , "ccv_nnc_symbolic_graph_io.c", 227, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
228 | const char graph_insert_qs[] = | ||||
229 | "REPLACE INTO graph " | ||||
230 | "(graph, tensor_symbol_size, exec_symbol_size, sources, destinations, sub_graphs, pair, p, p_idx, " | ||||
231 | "exec_idx, breakpoint_size, breakpoints, backward_tensor_symbol_size, " | ||||
232 | "backward_tensor_symbol_idx, backward_exec_symbol_size, backward_exec_symbol_idx, " | ||||
233 | "parallel_count, parallel_tensor_symbol_size, parallel_tensor_symbol_idx, " | ||||
234 | "parallel_exec_symbol_size, parallel_exec_symbol_idx) VALUES " | ||||
235 | "($graph, $tensor_symbol_size, $exec_symbol_size, $sources, $destinations, $sub_graphs, $pair, $p, $p_idx, " | ||||
236 | "$exec_idx, $breakpoint_size, $breakpoints, $backward_tensor_symbol_size, " | ||||
237 | "$backward_tensor_symbol_idx, $backward_exec_symbol_size, $backward_exec_symbol_idx, " | ||||
238 | "$parallel_count, $parallel_tensor_symbol_size, $parallel_tensor_symbol_idx, " | ||||
239 | "$parallel_exec_symbol_size, $parallel_exec_symbol_idx)"; | ||||
240 | sqlite3_stmt* graph_insert_stmt = 0; | ||||
241 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, graph_insert_qs, sizeof(graph_insert_qs), &graph_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, graph_insert_qs , sizeof(graph_insert_qs), &graph_insert_stmt, 0)) ? 1 : 0 ), __extension__ ({ if (0 == sqlite3_prepare_v2(conn, graph_insert_qs , sizeof(graph_insert_qs), &graph_insert_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(conn, graph_insert_qs, sizeof(graph_insert_qs), &graph_insert_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 241, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
242 | ccv_array_t* const repo = ccv_array_new(sizeof(ccv_nnc_symbolic_graph_t*), 1, 0); | ||||
243 | _ccv_nnc_symbolic_graph_push_repo(graph, repo); | ||||
244 | ccv_array_t* const ws = ccv_array_new(sizeof(int), 1, 0); | ||||
245 | int i; | ||||
246 | for (i = 0; i < repo->rnum; i++) | ||||
247 | _ccv_nnc_symbolic_graph_write(*(ccv_nnc_symbolic_graph_t**)ccv_array_get(repo, i)((void*)(((char*)((repo)->data)) + (size_t)(repo)->rsize * (size_t)(i))), repo, i, | ||||
248 | tensor_symbol_insert_stmt, exec_symbol_insert_stmt, graph_insert_stmt, ws); | ||||
249 | ccv_array_free(ws); | ||||
250 | sqlite3_finalize(tensor_symbol_insert_stmt); | ||||
251 | sqlite3_finalize(exec_symbol_insert_stmt); | ||||
252 | sqlite3_finalize(graph_insert_stmt); | ||||
253 | // Write tensor binds. | ||||
254 | const char tensor_bind_create_table_qs[] = "CREATE TABLE IF NOT EXISTS tensor_bind " | ||||
255 | "(id INTEGER, graph INTEGER, type INTEGER, format INTEGER, datatype INTEGER, " | ||||
256 | "dim BLOB, data BLOB, PRIMARY KEY (id, graph))"; | ||||
257 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, tensor_bind_create_table_qs, 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(conn, tensor_bind_create_table_qs , 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec( conn, tensor_bind_create_table_qs, 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(conn, tensor_bind_create_table_qs, 0, 0, 0)" , "ccv_nnc_symbolic_graph_io.c", 257, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
258 | // Remove everything in that table. | ||||
259 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, "DELETE FROM tensor_bind", 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(conn, "DELETE FROM tensor_bind" , 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec( conn, "DELETE FROM tensor_bind", 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(conn, \"DELETE FROM tensor_bind\", 0, 0, 0)" , "ccv_nnc_symbolic_graph_io.c", 259, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
260 | const char tensor_bind_insert_qs[] = | ||||
261 | "REPLACE INTO tensor_bind " | ||||
262 | "(id, graph, type, format, datatype, dim, data) VALUES (" | ||||
263 | "$id, $graph, $type, $format, $datatype, $dim, $data)"; | ||||
264 | sqlite3_stmt* tensor_bind_insert_stmt = 0; | ||||
265 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, tensor_bind_insert_qs, sizeof(tensor_bind_insert_qs), &tensor_bind_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, tensor_bind_insert_qs , sizeof(tensor_bind_insert_qs), &tensor_bind_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( conn, tensor_bind_insert_qs, sizeof(tensor_bind_insert_qs), & tensor_bind_insert_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(conn, tensor_bind_insert_qs, sizeof(tensor_bind_insert_qs), &tensor_bind_insert_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 265, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
266 | #ifdef HAVE_CUDA1 | ||||
267 | size_t workspace_size = 0; | ||||
268 | void* workspace = 0; | ||||
269 | #endif | ||||
270 | for (i = 0; i < tensor_bind_size; i++) | ||||
271 | { | ||||
272 | const int graph_idx = _ccv_nnc_symbolic_graph_index_in_repo(tensor_binds[i].symbol.graph, repo); | ||||
273 | if (graph_idx < 0) | ||||
274 | continue; | ||||
275 | sqlite3_bind_int(tensor_bind_insert_stmt, 1, tensor_binds[i].symbol.d); | ||||
276 | sqlite3_bind_int(tensor_bind_insert_stmt, 2, graph_idx); | ||||
277 | if (tensor_binds[i].tensor) | ||||
278 | { | ||||
279 | const ccv_nnc_tensor_t* const tensor = tensor_binds[i].tensor; | ||||
280 | assert(!CCV_IS_TENSOR_VIEW(tensor))((void) sizeof ((!((*(int*)(tensor)) & CCV_TENSOR_VIEW)) ? 1 : 0), __extension__ ({ if (!((*(int*)(tensor)) & CCV_TENSOR_VIEW )) ; else __assert_fail ("!CCV_IS_TENSOR_VIEW(tensor)", "ccv_nnc_symbolic_graph_io.c" , 280, __extension__ __PRETTY_FUNCTION__); })); | ||||
281 | sqlite3_bind_int(tensor_bind_insert_stmt, 3, tensor->info.type); | ||||
282 | sqlite3_bind_int(tensor_bind_insert_stmt, 4, tensor->info.format); | ||||
283 | sqlite3_bind_int(tensor_bind_insert_stmt, 5, tensor->info.datatype); | ||||
284 | sqlite3_bind_blob(tensor_bind_insert_stmt, 6, tensor->info.dim, sizeof(tensor->info.dim), 0); | ||||
285 | const size_t data_size = ccv_nnc_tensor_data_size(tensor->info); | ||||
286 | #ifdef HAVE_CUDA1 | ||||
287 | if (CCV_TENSOR_GET_MEMORY(tensor->info.type)((tensor->info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY) | ||||
288 | { | ||||
289 | if (!workspace) | ||||
290 | { | ||||
291 | workspace = ccmallocmalloc(data_size); | ||||
292 | workspace_size = data_size; | ||||
293 | } else if (data_size > workspace_size) { | ||||
294 | workspace = ccreallocrealloc(workspace, data_size); | ||||
295 | workspace_size = data_size; | ||||
296 | } | ||||
297 | cumemcpy(workspace, CCV_TENSOR_CPU_MEMORY, tensor->data.u8, tensor->info.type, data_size); | ||||
298 | sqlite3_bind_blob(tensor_bind_insert_stmt, 7, workspace, data_size, 0); | ||||
299 | } else | ||||
300 | sqlite3_bind_blob(tensor_bind_insert_stmt, 7, tensor->data.u8, data_size, 0); | ||||
301 | #else | ||||
302 | sqlite3_bind_blob(tensor_bind_insert_stmt, 7, tensor->data.u8, data_size, 0); | ||||
303 | #endif | ||||
304 | } else { | ||||
305 | assert(tensor_binds[i].symbol.d >= 0)((void) sizeof ((tensor_binds[i].symbol.d >= 0) ? 1 : 0), __extension__ ({ if (tensor_binds[i].symbol.d >= 0) ; else __assert_fail ("tensor_binds[i].symbol.d >= 0", "ccv_nnc_symbolic_graph_io.c" , 305, __extension__ __PRETTY_FUNCTION__); })); | ||||
306 | const ccv_nnc_tensor_symbol_info_t* const symbol_info = (ccv_nnc_tensor_symbol_info_t*)ccv_array_get(graph->tensor_symbol_info, tensor_binds[i].symbol.d)((void*)(((char*)((graph->tensor_symbol_info)->data)) + (size_t)(graph->tensor_symbol_info)->rsize * (size_t)( tensor_binds[i].symbol.d))); | ||||
307 | sqlite3_bind_int(tensor_bind_insert_stmt, 3, symbol_info->info.type); | ||||
308 | sqlite3_bind_int(tensor_bind_insert_stmt, 4, symbol_info->info.format); | ||||
309 | sqlite3_bind_int(tensor_bind_insert_stmt, 5, symbol_info->info.datatype); | ||||
310 | sqlite3_bind_blob(tensor_bind_insert_stmt, 6, symbol_info->info.dim, sizeof(symbol_info->info.dim), 0); | ||||
311 | } | ||||
312 | sqlite3_step(tensor_bind_insert_stmt); | ||||
313 | sqlite3_reset(tensor_bind_insert_stmt); | ||||
314 | sqlite3_clear_bindings(tensor_bind_insert_stmt); | ||||
315 | } | ||||
316 | sqlite3_finalize(tensor_bind_insert_stmt); | ||||
317 | #ifdef HAVE_CUDA1 | ||||
318 | if (workspace) | ||||
319 | ccfreefree(workspace); | ||||
320 | #endif | ||||
321 | ccv_array_free(repo); | ||||
322 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, "COMMIT", 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(conn, "COMMIT", 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec(conn, "COMMIT" , 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(conn, \"COMMIT\", 0, 0, 0)" , "ccv_nnc_symbolic_graph_io.c", 322, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
323 | sqlite3_close(conn); | ||||
324 | } | ||||
325 | |||||
326 | static ccv_nnc_symbolic_graph_t* _ccv_nnc_symbolic_graph_get(const ccv_array_t* const repo, const ccv_nnc_symbolic_graph_t* const pos) | ||||
327 | { | ||||
328 | const int idx = (uintptr_t)pos >> 1; | ||||
329 | assert(idx < repo->rnum)((void) sizeof ((idx < repo->rnum) ? 1 : 0), __extension__ ({ if (idx < repo->rnum) ; else __assert_fail ("idx < repo->rnum" , "ccv_nnc_symbolic_graph_io.c", 329, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
330 | return *(ccv_nnc_symbolic_graph_t**)ccv_array_get(repo, idx)((void*)(((char*)((repo)->data)) + (size_t)(repo)->rsize * (size_t)(idx))); | ||||
331 | } | ||||
332 | |||||
333 | #define CCV_NNC_IS_SYMBOLIC_GRAPH_POS(ptr)((uintptr_t)(ptr) & 1) ((uintptr_t)(ptr) & 1) | ||||
334 | |||||
335 | static ccv_nnc_symbolic_graph_t* _ccv_nnc_symbolic_graph_pos(const int idx) | ||||
336 | { | ||||
337 | if (idx < 0) | ||||
338 | return 0; // This is nil. | ||||
339 | return (ccv_nnc_symbolic_graph_t*)(((uintptr_t)idx << 1) + 1); | ||||
340 | } | ||||
341 | |||||
342 | static void _ccv_nnc_symbolic_graph_read(const int graph_idx, sqlite3_stmt* const graph_select_stmt, sqlite3_stmt* const tensor_symbol_select_stmt, sqlite3_stmt* const exec_symbol_select_stmt, ccv_nnc_symbolic_graph_t* const graph) | ||||
343 | { | ||||
344 | int i, j; | ||||
345 | ccv_array_resize(graph->tensor_symbol_info, sqlite3_column_int(graph_select_stmt, 1)); | ||||
346 | ccv_array_resize(graph->exec_symbol_info, sqlite3_column_int(graph_select_stmt, 2)); | ||||
347 | if (sqlite3_column_blob(graph_select_stmt, 3)) | ||||
348 | { | ||||
349 | const int* const sources = sqlite3_column_blob(graph_select_stmt, 3); | ||||
350 | const int count = sqlite3_column_bytes(graph_select_stmt, 3) / sizeof(int); | ||||
351 | graph->sources = ccv_array_new(sizeof(ccv_nnc_graph_exec_symbol_t), count, 0); | ||||
352 | for (i = 0; i < count; i++) | ||||
353 | { | ||||
354 | const ccv_nnc_graph_exec_symbol_t symbol = { | ||||
355 | .graph = graph, | ||||
356 | .d = sources[i] | ||||
357 | }; | ||||
358 | ccv_array_push(graph->sources, &symbol); | ||||
359 | } | ||||
360 | } | ||||
361 | if (sqlite3_column_blob(graph_select_stmt, 4)) | ||||
362 | { | ||||
363 | const int* const destinations = sqlite3_column_blob(graph_select_stmt, 4); | ||||
364 | const int count = sqlite3_column_bytes(graph_select_stmt, 4) / sizeof(int); | ||||
365 | graph->destinations = ccv_array_new(sizeof(ccv_nnc_graph_exec_symbol_t), count, 0); | ||||
366 | for (i = 0; i < count; i++) | ||||
367 | { | ||||
368 | const ccv_nnc_graph_exec_symbol_t symbol = { | ||||
369 | .graph = graph, | ||||
370 | .d = destinations[i] | ||||
371 | }; | ||||
372 | ccv_array_push(graph->destinations, &symbol); | ||||
373 | } | ||||
374 | } | ||||
375 | if (sqlite3_column_blob(graph_select_stmt, 5)) | ||||
376 | { | ||||
377 | const int* const sub_graphs = sqlite3_column_blob(graph_select_stmt, 5); | ||||
378 | const int count = sqlite3_column_bytes(graph_select_stmt, 5) / sizeof(int); | ||||
379 | graph->sub_graphs = ccv_array_new(sizeof(ccv_nnc_symbolic_graph_t*), count, 0); | ||||
380 | for (i = 0; i < count; i++) | ||||
381 | { | ||||
382 | const ccv_nnc_symbolic_graph_t* const sub_graph = _ccv_nnc_symbolic_graph_pos(sub_graphs[i]); | ||||
383 | ccv_array_push(graph->sub_graphs, &sub_graph); | ||||
384 | } | ||||
385 | } | ||||
386 | graph->pair = _ccv_nnc_symbolic_graph_pos(sqlite3_column_int(graph_select_stmt, 6)); | ||||
387 | graph->p = _ccv_nnc_symbolic_graph_pos(sqlite3_column_int(graph_select_stmt, 7)); | ||||
388 | graph->p_idx = sqlite3_column_int(graph_select_stmt, 8); | ||||
389 | graph->exec_idx = sqlite3_column_int(graph_select_stmt, 9); | ||||
390 | graph->breakpoint_size = sqlite3_column_int(graph_select_stmt, 10); | ||||
391 | if (graph->breakpoint_size) | ||||
392 | { | ||||
393 | graph->breakpoints = (ccv_nnc_graph_exec_symbol_t*)ccmallocmalloc(sizeof(ccv_nnc_graph_exec_symbol_t) * graph->breakpoint_size); | ||||
394 | assert(sizeof(int) * graph->breakpoint_size == sqlite3_column_bytes(graph_select_stmt, 11))((void) sizeof ((sizeof(int) * graph->breakpoint_size == sqlite3_column_bytes (graph_select_stmt, 11)) ? 1 : 0), __extension__ ({ if (sizeof (int) * graph->breakpoint_size == sqlite3_column_bytes(graph_select_stmt , 11)) ; else __assert_fail ("sizeof(int) * graph->breakpoint_size == sqlite3_column_bytes(graph_select_stmt, 11)" , "ccv_nnc_symbolic_graph_io.c", 394, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
395 | const int* const breakpoints = sqlite3_column_blob(graph_select_stmt, 11); | ||||
396 | for (i = 0; i < graph->breakpoint_size; i++) | ||||
397 | graph->breakpoints[i] = (ccv_nnc_graph_exec_symbol_t){ | ||||
398 | .d = breakpoints[i], | ||||
399 | .graph = graph | ||||
400 | }; | ||||
401 | } | ||||
402 | graph->backward.tensor_symbol_size = sqlite3_column_int(graph_select_stmt, 12); | ||||
403 | graph->backward.exec_symbol_size = sqlite3_column_int(graph_select_stmt, 14); | ||||
404 | if (graph->backward.tensor_symbol_size || graph->backward.exec_symbol_size) | ||||
405 | graph->backward.tensor_symbol_idx = (int*)ccmallocmalloc(sizeof(int) * (graph->backward.tensor_symbol_size + graph->backward.exec_symbol_size)); | ||||
406 | if (graph->backward.tensor_symbol_size) | ||||
407 | { | ||||
408 | assert(sizeof(int) * graph->backward.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 13))((void) sizeof ((sizeof(int) * graph->backward.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 13)) ? 1 : 0), __extension__ ({ if (sizeof(int) * graph->backward.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 13)) ; else __assert_fail ("sizeof(int) * graph->backward.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 13)" , "ccv_nnc_symbolic_graph_io.c", 408, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
409 | const int* const backward_tensor_symbol_idx = sqlite3_column_blob(graph_select_stmt, 13); | ||||
410 | memcpy(graph->backward.tensor_symbol_idx, backward_tensor_symbol_idx, sizeof(int) * graph->backward.tensor_symbol_size); | ||||
411 | } | ||||
412 | if (graph->backward.exec_symbol_size) | ||||
413 | { | ||||
414 | graph->backward.exec_symbol_idx = graph->backward.tensor_symbol_idx + graph->backward.tensor_symbol_size; | ||||
415 | assert(sizeof(int) * graph->backward.exec_symbol_size == sqlite3_column_bytes(graph_select_stmt, 15))((void) sizeof ((sizeof(int) * graph->backward.exec_symbol_size == sqlite3_column_bytes(graph_select_stmt, 15)) ? 1 : 0), __extension__ ({ if (sizeof(int) * graph->backward.exec_symbol_size == sqlite3_column_bytes (graph_select_stmt, 15)) ; else __assert_fail ("sizeof(int) * graph->backward.exec_symbol_size == sqlite3_column_bytes(graph_select_stmt, 15)" , "ccv_nnc_symbolic_graph_io.c", 415, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
416 | const int* const backward_exec_symbol_idx = sqlite3_column_blob(graph_select_stmt, 15); | ||||
417 | memcpy(graph->backward.exec_symbol_idx, backward_exec_symbol_idx, sizeof(int) * graph->backward.exec_symbol_size); | ||||
418 | } | ||||
419 | graph->data_parallel.count = sqlite3_column_int(graph_select_stmt, 16); | ||||
420 | graph->data_parallel.tensor_symbol_size = sqlite3_column_int(graph_select_stmt, 17); | ||||
421 | if (graph->data_parallel.tensor_symbol_size) | ||||
422 | { | ||||
423 | graph->data_parallel.tensor_symbol_idx = (int*)ccmallocmalloc(sizeof(int) * graph->data_parallel.tensor_symbol_size); | ||||
424 | assert(sizeof(int) * graph->data_parallel.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 18))((void) sizeof ((sizeof(int) * graph->data_parallel.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 18)) ? 1 : 0), __extension__ ({ if (sizeof(int) * graph->data_parallel.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 18)) ; else __assert_fail ("sizeof(int) * graph->data_parallel.tensor_symbol_size == sqlite3_column_bytes(graph_select_stmt, 18)" , "ccv_nnc_symbolic_graph_io.c", 424, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
425 | const int* const parallel_tensor_symbol_idx = sqlite3_column_blob(graph_select_stmt, 18); | ||||
426 | memcpy(graph->data_parallel.tensor_symbol_idx, parallel_tensor_symbol_idx, sizeof(int) * graph->data_parallel.tensor_symbol_size); | ||||
427 | } | ||||
428 | graph->data_parallel.exec_symbol_size = sqlite3_column_int(graph_select_stmt, 19); | ||||
429 | if (graph->data_parallel.exec_symbol_size) | ||||
430 | { | ||||
431 | graph->data_parallel.exec_symbol_idx = (int*)ccmallocmalloc(sizeof(int) * graph->data_parallel.exec_symbol_size); | ||||
432 | assert(sizeof(int) * graph->data_parallel.exec_symbol_size == sqlite3_column_bytes(graph_select_stmt, 20))((void) sizeof ((sizeof(int) * graph->data_parallel.exec_symbol_size == sqlite3_column_bytes(graph_select_stmt, 20)) ? 1 : 0), __extension__ ({ if (sizeof(int) * graph->data_parallel.exec_symbol_size == sqlite3_column_bytes(graph_select_stmt, 20)) ; else __assert_fail ("sizeof(int) * graph->data_parallel.exec_symbol_size == sqlite3_column_bytes(graph_select_stmt, 20)" , "ccv_nnc_symbolic_graph_io.c", 432, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
433 | const int* const parallel_exec_symbol_idx = sqlite3_column_blob(graph_select_stmt, 20); | ||||
434 | memcpy(graph->data_parallel.exec_symbol_idx, parallel_exec_symbol_idx, sizeof(int) * graph->data_parallel.exec_symbol_size); | ||||
435 | } | ||||
436 | sqlite3_bind_int(tensor_symbol_select_stmt, 1, graph_idx); | ||||
437 | for (i = 0; SQLITE_ROW100 == sqlite3_step(tensor_symbol_select_stmt); i++) | ||||
438 | { | ||||
439 | assert(sqlite3_column_int(tensor_symbol_select_stmt, 0) == i)((void) sizeof ((sqlite3_column_int(tensor_symbol_select_stmt , 0) == i) ? 1 : 0), __extension__ ({ if (sqlite3_column_int( tensor_symbol_select_stmt, 0) == i) ; else __assert_fail ("sqlite3_column_int(tensor_symbol_select_stmt, 0) == i" , "ccv_nnc_symbolic_graph_io.c", 439, __extension__ __PRETTY_FUNCTION__ ); })); // id should match. | ||||
440 | assert(i < graph->tensor_symbol_info->rnum)((void) sizeof ((i < graph->tensor_symbol_info->rnum ) ? 1 : 0), __extension__ ({ if (i < graph->tensor_symbol_info ->rnum) ; else __assert_fail ("i < graph->tensor_symbol_info->rnum" , "ccv_nnc_symbolic_graph_io.c", 440, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
441 | assert(sqlite3_column_int(tensor_symbol_select_stmt, 0) == i)((void) sizeof ((sqlite3_column_int(tensor_symbol_select_stmt , 0) == i) ? 1 : 0), __extension__ ({ if (sqlite3_column_int( tensor_symbol_select_stmt, 0) == i) ; else __assert_fail ("sqlite3_column_int(tensor_symbol_select_stmt, 0) == i" , "ccv_nnc_symbolic_graph_io.c", 441, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
442 | ccv_nnc_tensor_symbol_info_t* const symbol_info = (ccv_nnc_tensor_symbol_info_t*)ccv_array_get(graph->tensor_symbol_info, i)((void*)(((char*)((graph->tensor_symbol_info)->data)) + (size_t)(graph->tensor_symbol_info)->rsize * (size_t)( i))); | ||||
443 | symbol_info->assign_ref = sqlite3_column_int(tensor_symbol_select_stmt, 1); | ||||
444 | symbol_info->r_assign_ref = sqlite3_column_int(tensor_symbol_select_stmt, 2); | ||||
445 | symbol_info->bypass_ref = sqlite3_column_int(tensor_symbol_select_stmt, 3); | ||||
446 | symbol_info->r_bypass_ref = sqlite3_column_int(tensor_symbol_select_stmt, 4); | ||||
447 | symbol_info->p_ref = sqlite3_column_int(tensor_symbol_select_stmt, 5); | ||||
448 | symbol_info->alias_ref = sqlite3_column_int(tensor_symbol_select_stmt, 6); | ||||
449 | symbol_info->pair_ref = sqlite3_column_int(tensor_symbol_select_stmt, 7); | ||||
450 | symbol_info->flags = sqlite3_column_int(tensor_symbol_select_stmt, 8); | ||||
451 | memset(symbol_info->ofs, 0, sizeof(symbol_info->ofs)); | ||||
452 | const int* const ofs = sqlite3_column_blob(tensor_symbol_select_stmt, 9); | ||||
453 | if (ofs) | ||||
454 | memcpy(symbol_info->ofs, ofs, ccv_min(sqlite3_column_bytes(tensor_symbol_select_stmt, 8), sizeof(symbol_info->ofs))({ typeof (sqlite3_column_bytes(tensor_symbol_select_stmt, 8) ) _a = (sqlite3_column_bytes(tensor_symbol_select_stmt, 8)); typeof (sizeof(symbol_info->ofs)) _b = (sizeof(symbol_info->ofs )); (_a < _b) ? _a : _b; })); | ||||
455 | memset(symbol_info->stride, 0, sizeof(symbol_info->stride)); | ||||
456 | const int* const stride = sqlite3_column_blob(tensor_symbol_select_stmt, 10); | ||||
457 | if (stride) | ||||
458 | memcpy(symbol_info->stride, stride, ccv_min(sqlite3_column_bytes(tensor_symbol_select_stmt, 9), sizeof(symbol_info->stride))({ typeof (sqlite3_column_bytes(tensor_symbol_select_stmt, 9) ) _a = (sqlite3_column_bytes(tensor_symbol_select_stmt, 9)); typeof (sizeof(symbol_info->stride)) _b = (sizeof(symbol_info-> stride)); (_a < _b) ? _a : _b; })); | ||||
459 | const int* const s_ref = sqlite3_column_blob(tensor_symbol_select_stmt, 11); | ||||
460 | if (s_ref) | ||||
461 | { | ||||
462 | const int count = sqlite3_column_bytes(tensor_symbol_select_stmt, 11) / sizeof(int); | ||||
463 | symbol_info->s_ref = ccv_array_new(sizeof(int), count, 0); | ||||
464 | ccv_array_resize(symbol_info->s_ref, count); | ||||
465 | memcpy(ccv_array_get(symbol_info->s_ref, 0)((void*)(((char*)((symbol_info->s_ref)->data)) + (size_t )(symbol_info->s_ref)->rsize * (size_t)(0))), s_ref, sizeof(int) * count); | ||||
466 | } else | ||||
467 | symbol_info->s_ref = 0; | ||||
468 | const char* const name = (char*)sqlite3_column_text(tensor_symbol_select_stmt, 12); | ||||
469 | if (name) | ||||
470 | { | ||||
471 | const int count = sqlite3_column_bytes(tensor_symbol_select_stmt, 12); | ||||
472 | symbol_info->name = (char*)ccmallocmalloc(sizeof(char) * (count + 1)); | ||||
473 | memcpy(symbol_info->name, name, count); | ||||
474 | symbol_info->name[count] = 0; // null terminator | ||||
475 | } else | ||||
476 | symbol_info->name = 0; | ||||
477 | symbol_info->info.type = sqlite3_column_int(tensor_symbol_select_stmt, 13); | ||||
478 | symbol_info->info.format = sqlite3_column_int(tensor_symbol_select_stmt, 14); | ||||
479 | symbol_info->info.datatype = sqlite3_column_int(tensor_symbol_select_stmt, 15); | ||||
480 | memset(symbol_info->info.dim, 0, sizeof(symbol_info->info.dim)); | ||||
481 | const int* const dim = sqlite3_column_blob(tensor_symbol_select_stmt, 16); | ||||
482 | if (dim) | ||||
483 | memcpy(symbol_info->info.dim, dim, ccv_min(sqlite3_column_bytes(tensor_symbol_select_stmt, 16), sizeof(symbol_info->info.dim))({ typeof (sqlite3_column_bytes(tensor_symbol_select_stmt, 16 )) _a = (sqlite3_column_bytes(tensor_symbol_select_stmt, 16)) ; typeof (sizeof(symbol_info->info.dim)) _b = (sizeof(symbol_info ->info.dim)); (_a < _b) ? _a : _b; })); | ||||
484 | if (CCV_NNC_TENSOR_SYMBOL_IS_DEAD(symbol_info->flags)((symbol_info->flags) & CCV_NNC_TENSOR_SYMBOL_DEAD) && graph->reuse.tensor < 0) | ||||
485 | graph->reuse.tensor = i; | ||||
486 | } | ||||
487 | sqlite3_reset(tensor_symbol_select_stmt); | ||||
488 | sqlite3_clear_bindings(tensor_symbol_select_stmt); | ||||
489 | sqlite3_bind_int(exec_symbol_select_stmt, 1, graph_idx); | ||||
490 | for (i = 0; SQLITE_ROW100 == sqlite3_step(exec_symbol_select_stmt); i++) | ||||
491 | { | ||||
492 | assert(sqlite3_column_int(exec_symbol_select_stmt, 0) == i)((void) sizeof ((sqlite3_column_int(exec_symbol_select_stmt, 0 ) == i) ? 1 : 0), __extension__ ({ if (sqlite3_column_int(exec_symbol_select_stmt , 0) == i) ; else __assert_fail ("sqlite3_column_int(exec_symbol_select_stmt, 0) == i" , "ccv_nnc_symbolic_graph_io.c", 492, __extension__ __PRETTY_FUNCTION__ ); })); // id should match. | ||||
493 | assert(i < graph->exec_symbol_info->rnum)((void) sizeof ((i < graph->exec_symbol_info->rnum) ? 1 : 0), __extension__ ({ if (i < graph->exec_symbol_info ->rnum) ; else __assert_fail ("i < graph->exec_symbol_info->rnum" , "ccv_nnc_symbolic_graph_io.c", 493, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
494 | assert(sqlite3_column_int(exec_symbol_select_stmt, 0) == i)((void) sizeof ((sqlite3_column_int(exec_symbol_select_stmt, 0 ) == i) ? 1 : 0), __extension__ ({ if (sqlite3_column_int(exec_symbol_select_stmt , 0) == i) ; else __assert_fail ("sqlite3_column_int(exec_symbol_select_stmt, 0) == i" , "ccv_nnc_symbolic_graph_io.c", 494, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
495 | ccv_nnc_graph_exec_symbol_info_t* const symbol_info = (ccv_nnc_graph_exec_symbol_info_t*)ccv_array_get(graph->exec_symbol_info, i)((void*)(((char*)((graph->exec_symbol_info)->data)) + ( size_t)(graph->exec_symbol_info)->rsize * (size_t)(i))); | ||||
496 | memset(symbol_info, 0, sizeof(ccv_nnc_graph_exec_symbol_info_t)); | ||||
497 | symbol_info->input_size = sqlite3_column_int(exec_symbol_select_stmt, 1); | ||||
498 | symbol_info->output_size = sqlite3_column_int(exec_symbol_select_stmt, 2); | ||||
499 | symbol_info->graph_ref_size = sqlite3_column_int(exec_symbol_select_stmt, 3); | ||||
500 | symbol_info->flags = sqlite3_column_int(exec_symbol_select_stmt, 4); | ||||
501 | symbol_info->pair_ref = sqlite3_column_int(exec_symbol_select_stmt, 5); | ||||
502 | if (symbol_info->input_size > 0 || symbol_info->output_size > 0) | ||||
503 | { | ||||
504 | symbol_info->inputs = (int*)ccmallocmalloc(sizeof(int) * (symbol_info->input_size + symbol_info->output_size)); | ||||
505 | for (j = 0; j < symbol_info->input_size; j++) | ||||
506 | symbol_info->inputs[j] = CCV_NNC_NO_TENSOR_SYMBOL; | ||||
507 | symbol_info->outputs = symbol_info->inputs + symbol_info->input_size; | ||||
508 | for (j = 0; j < symbol_info->output_size; j++) | ||||
509 | symbol_info->outputs[j] = CCV_NNC_NO_TENSOR_SYMBOL; | ||||
510 | } | ||||
511 | if (symbol_info->input_size) | ||||
512 | { | ||||
513 | const int* const inputs = sqlite3_column_blob(exec_symbol_select_stmt, 6); | ||||
514 | if (inputs) | ||||
515 | memcpy(symbol_info->inputs, inputs, ccv_min(sizeof(int) * symbol_info->input_size, sqlite3_column_bytes(exec_symbol_select_stmt, 6))({ typeof (sizeof(int) * symbol_info->input_size) _a = (sizeof (int) * symbol_info->input_size); typeof (sqlite3_column_bytes (exec_symbol_select_stmt, 6)) _b = (sqlite3_column_bytes(exec_symbol_select_stmt , 6)); (_a < _b) ? _a : _b; })); | ||||
516 | } | ||||
517 | if (symbol_info->output_size) | ||||
518 | { | ||||
519 | const int* const outputs = sqlite3_column_blob(exec_symbol_select_stmt, 7); | ||||
520 | if (outputs) | ||||
521 | memcpy(symbol_info->outputs, outputs, ccv_min(sizeof(int) * symbol_info->output_size, sqlite3_column_bytes(exec_symbol_select_stmt, 7))({ typeof (sizeof(int) * symbol_info->output_size) _a = (sizeof (int) * symbol_info->output_size); typeof (sqlite3_column_bytes (exec_symbol_select_stmt, 7)) _b = (sqlite3_column_bytes(exec_symbol_select_stmt , 7)); (_a < _b) ? _a : _b; })); | ||||
522 | } | ||||
523 | const int* const outgoings = sqlite3_column_blob(exec_symbol_select_stmt, 8); | ||||
524 | if (outgoings) | ||||
525 | { | ||||
526 | const int count = sqlite3_column_bytes(exec_symbol_select_stmt, 8) / sizeof(int); | ||||
527 | symbol_info->outgoings = ccv_array_new(sizeof(int), count, 0); | ||||
528 | ccv_array_resize(symbol_info->outgoings, count); | ||||
529 | memcpy(ccv_array_get(symbol_info->outgoings, 0)((void*)(((char*)((symbol_info->outgoings)->data)) + (size_t )(symbol_info->outgoings)->rsize * (size_t)(0))), outgoings, sizeof(int) * count); | ||||
530 | } | ||||
531 | const char* const name = (char*)sqlite3_column_text(exec_symbol_select_stmt, 9); | ||||
532 | if (name) | ||||
533 | { | ||||
534 | const int count = sqlite3_column_bytes(exec_symbol_select_stmt, 9); | ||||
535 | symbol_info->name = (char*)ccmallocmalloc(sizeof(char) * (count + 1)); | ||||
536 | memcpy(symbol_info->name, name, count); | ||||
537 | symbol_info->name[count] = 0; // null terminator | ||||
538 | } | ||||
539 | symbol_info->cmd.cmd = sqlite3_column_int(exec_symbol_select_stmt, 10); | ||||
540 | symbol_info->cmd.backend = sqlite3_column_int(exec_symbol_select_stmt, 11); | ||||
541 | symbol_info->cmd.algorithm = sqlite3_column_int(exec_symbol_select_stmt, 12); | ||||
542 | const void* const cmd_info = sqlite3_column_blob(exec_symbol_select_stmt, 13); | ||||
543 | if (cmd_info) | ||||
544 | memcpy(&symbol_info->cmd.info, cmd_info, ccv_min(sizeof(symbol_info->cmd.info), sqlite3_column_bytes(exec_symbol_select_stmt, 13))({ typeof (sizeof(symbol_info->cmd.info)) _a = (sizeof(symbol_info ->cmd.info)); typeof (sqlite3_column_bytes(exec_symbol_select_stmt , 13)) _b = (sqlite3_column_bytes(exec_symbol_select_stmt, 13 )); (_a < _b) ? _a : _b; })); | ||||
545 | const void* const hint = sqlite3_column_blob(exec_symbol_select_stmt, 14); | ||||
546 | if (hint) | ||||
547 | memcpy(&symbol_info->hint, hint, ccv_min(sizeof(symbol_info->hint), sqlite3_column_bytes(exec_symbol_select_stmt, 14))({ typeof (sizeof(symbol_info->hint)) _a = (sizeof(symbol_info ->hint)); typeof (sqlite3_column_bytes(exec_symbol_select_stmt , 14)) _b = (sqlite3_column_bytes(exec_symbol_select_stmt, 14 )); (_a < _b) ? _a : _b; })); | ||||
548 | if (symbol_info->graph_ref_size) | ||||
549 | { | ||||
550 | const int* const graph_ref = sqlite3_column_blob(exec_symbol_select_stmt, 15); | ||||
551 | if (symbol_info->graph_ref_size > sizeof(symbol_info->_inline_graph_ref) / sizeof(symbol_info->_inline_graph_ref[0])) | ||||
552 | symbol_info->_heap_graph_ref = (int*)cccalloccalloc(symbol_info->graph_ref_size, sizeof(int)); | ||||
553 | if (graph_ref) | ||||
554 | memcpy(CCV_NNC_GRAPH_REF(symbol_info)((symbol_info)->_heap_graph_ref ? (symbol_info)->_heap_graph_ref : (symbol_info)->_inline_graph_ref), graph_ref, ccv_min(sizeof(int) * symbol_info->graph_ref_size, sqlite3_column_bytes(exec_symbol_select_stmt, 15))({ typeof (sizeof(int) * symbol_info->graph_ref_size) _a = (sizeof(int) * symbol_info->graph_ref_size); typeof (sqlite3_column_bytes (exec_symbol_select_stmt, 15)) _b = (sqlite3_column_bytes(exec_symbol_select_stmt , 15)); (_a < _b) ? _a : _b; })); | ||||
555 | } | ||||
556 | if (symbol_info->flags & CCV_NNC_GRAPH_EXEC_CASE_OF) | ||||
557 | { | ||||
558 | symbol_info->case_of.flags = sqlite3_column_int(exec_symbol_select_stmt, 17); | ||||
559 | symbol_info->case_of.argument.offset = sqlite3_column_int(exec_symbol_select_stmt, 18); | ||||
560 | symbol_info->case_of.argument.size = sqlite3_column_int(exec_symbol_select_stmt, 19); | ||||
561 | } else if (symbol_info->flags & CCV_NNC_GRAPH_EXEC_P_WHILE) { | ||||
562 | symbol_info->p_while.input_size = sqlite3_column_int(exec_symbol_select_stmt, 21); | ||||
563 | if (symbol_info->p_while.input_size) | ||||
564 | { | ||||
565 | symbol_info->p_while.inputs = (int*)cccalloccalloc(symbol_info->p_while.input_size, sizeof(int)); | ||||
566 | const int* const inputs = sqlite3_column_blob(exec_symbol_select_stmt, 22); | ||||
567 | if (inputs) | ||||
568 | memcpy(symbol_info->p_while.inputs, inputs, ccv_min(sizeof(int) * symbol_info->p_while.input_size, sqlite3_column_bytes(exec_symbol_select_stmt, 22))({ typeof (sizeof(int) * symbol_info->p_while.input_size) _a = (sizeof(int) * symbol_info->p_while.input_size); typeof (sqlite3_column_bytes(exec_symbol_select_stmt, 22)) _b = (sqlite3_column_bytes (exec_symbol_select_stmt, 22)); (_a < _b) ? _a : _b; })); | ||||
569 | } | ||||
570 | } | ||||
571 | if (CCV_NNC_GRAPH_EXEC_IS_DEAD(symbol_info->flags)((symbol_info->flags) & CCV_NNC_GRAPH_EXEC_DEAD) && graph->reuse.exec < 0) | ||||
572 | graph->reuse.exec = i; | ||||
573 | } | ||||
574 | sqlite3_reset(exec_symbol_select_stmt); | ||||
575 | sqlite3_clear_bindings(exec_symbol_select_stmt); | ||||
576 | } | ||||
577 | |||||
578 | static void _ccv_nnc_symbolic_graph_rewire(const ccv_array_t* const repo, ccv_nnc_symbolic_graph_t* const graph) | ||||
579 | { | ||||
580 | if (graph->p && CCV_NNC_IS_SYMBOLIC_GRAPH_POS(graph->p)((uintptr_t)(graph->p) & 1)) | ||||
581 | graph->p = _ccv_nnc_symbolic_graph_get(repo, graph->p); | ||||
582 | if (graph->pair && CCV_NNC_IS_SYMBOLIC_GRAPH_POS(graph->pair)((uintptr_t)(graph->pair) & 1)) | ||||
583 | graph->pair = _ccv_nnc_symbolic_graph_get(repo, graph->pair); | ||||
584 | int i; | ||||
585 | if (graph->sub_graphs) | ||||
586 | for (i = 0; i < graph->sub_graphs->rnum; i++) | ||||
587 | { | ||||
588 | ccv_nnc_symbolic_graph_t* const sub_graph = *(ccv_nnc_symbolic_graph_t**)ccv_array_get(graph->sub_graphs, i)((void*)(((char*)((graph->sub_graphs)->data)) + (size_t )(graph->sub_graphs)->rsize * (size_t)(i))); | ||||
589 | if (sub_graph && CCV_NNC_IS_SYMBOLIC_GRAPH_POS(sub_graph)((uintptr_t)(sub_graph) & 1)) | ||||
590 | *(ccv_nnc_symbolic_graph_t**)ccv_array_get(graph->sub_graphs, i)((void*)(((char*)((graph->sub_graphs)->data)) + (size_t )(graph->sub_graphs)->rsize * (size_t)(i))) = _ccv_nnc_symbolic_graph_get(repo, sub_graph); | ||||
591 | } | ||||
592 | } | ||||
593 | |||||
594 | void ccv_nnc_symbolic_graph_read(const char* const fn, ccv_nnc_symbolic_graph_t** const graph_ref, ccv_nnc_tensor_bind_t** const tensor_binds_ref, int* const tensor_bind_size_ref) | ||||
595 | { | ||||
596 | sqlite3* conn = 0; | ||||
597 | if (SQLITE_OK0 != sqlite3_open(fn, &conn)) | ||||
| |||||
598 | return; | ||||
599 | ccv_array_t* const repo = ccv_array_new(sizeof(ccv_nnc_symbolic_graph_t*), 1, 0); | ||||
600 | const char graph_select_qs[] = | ||||
601 | "SELECT graph, tensor_symbol_size, exec_symbol_size, sources, destinations, sub_graphs, pair, p, p_idx, " | ||||
602 | "exec_idx, breakpoint_size, breakpoints, backward_tensor_symbol_size, " | ||||
603 | "backward_tensor_symbol_idx, backward_exec_symbol_size, backward_exec_symbol_idx, " | ||||
604 | "parallel_count, parallel_tensor_symbol_size, parallel_tensor_symbol_idx, " | ||||
605 | "parallel_exec_symbol_size, parallel_exec_symbol_idx FROM graph ORDER BY graph"; | ||||
606 | sqlite3_stmt* graph_select_stmt = 0; | ||||
607 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, graph_select_qs, sizeof(graph_select_qs), &graph_select_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, graph_select_qs , sizeof(graph_select_qs), &graph_select_stmt, 0)) ? 1 : 0 ), __extension__ ({ if (0 == sqlite3_prepare_v2(conn, graph_select_qs , sizeof(graph_select_qs), &graph_select_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(conn, graph_select_qs, sizeof(graph_select_qs), &graph_select_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 607, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
608 | sqlite3_stmt* tensor_symbol_select_stmt = 0; | ||||
609 | const char tensor_symbol_select_qs[] = | ||||
610 | "SELECT id, assign_ref, r_assign_ref, bypass_ref, r_bypass_ref, p_ref, alias_ref, pair_ref, flags, ofs, stride, " | ||||
611 | "s_ref, name, type, format, datatype, dim FROM tensor_symbol WHERE graph=$graph ORDER BY id"; | ||||
612 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, tensor_symbol_select_qs, sizeof(tensor_symbol_select_qs), &tensor_symbol_select_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, tensor_symbol_select_qs , sizeof(tensor_symbol_select_qs), &tensor_symbol_select_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( conn, tensor_symbol_select_qs, sizeof(tensor_symbol_select_qs ), &tensor_symbol_select_stmt, 0)) ; else __assert_fail ( "SQLITE_OK == sqlite3_prepare_v2(conn, tensor_symbol_select_qs, sizeof(tensor_symbol_select_qs), &tensor_symbol_select_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 612, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
613 | const char exec_symbol_select_qs[] = | ||||
614 | "SELECT id, input_size, output_size, graph_ref_size, flags, pair_ref, inputs, outputs, outgoings, " | ||||
615 | "name, cmd_cmd, cmd_backend, cmd_algorithm, cmd_info, hint, graph_ref, case_of_expr, case_of_flags, " | ||||
616 | "case_of_argument_offset, case_of_argument_size, p_while_expr, p_while_input_size, p_while_inputs " | ||||
617 | "FROM graph_exec_symbol WHERE graph=$graph ORDER BY id"; | ||||
618 | sqlite3_stmt* exec_symbol_select_stmt = 0; | ||||
619 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, exec_symbol_select_qs, sizeof(exec_symbol_select_qs), &exec_symbol_select_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, exec_symbol_select_qs , sizeof(exec_symbol_select_qs), &exec_symbol_select_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( conn, exec_symbol_select_qs, sizeof(exec_symbol_select_qs), & exec_symbol_select_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(conn, exec_symbol_select_qs, sizeof(exec_symbol_select_qs), &exec_symbol_select_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 619, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
620 | while (SQLITE_ROW100 == sqlite3_step(graph_select_stmt)) | ||||
621 | { | ||||
622 | ccv_nnc_symbolic_graph_t* const graph = ccv_nnc_symbolic_graph_new(); | ||||
623 | const int graph_idx = sqlite3_column_int(graph_select_stmt, 0); | ||||
624 | assert(graph_idx == repo->rnum)((void) sizeof ((graph_idx == repo->rnum) ? 1 : 0), __extension__ ({ if (graph_idx == repo->rnum) ; else __assert_fail ("graph_idx == repo->rnum" , "ccv_nnc_symbolic_graph_io.c", 624, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
625 | ccv_array_push(repo, &graph); | ||||
626 | _ccv_nnc_symbolic_graph_read(graph_idx, graph_select_stmt, tensor_symbol_select_stmt, exec_symbol_select_stmt, graph); | ||||
627 | } | ||||
628 | int i; | ||||
629 | for (i = 0; i < repo->rnum; i++) | ||||
630 | _ccv_nnc_symbolic_graph_rewire(repo, *(ccv_nnc_symbolic_graph_t**)ccv_array_get(repo, i)((void*)(((char*)((repo)->data)) + (size_t)(repo)->rsize * (size_t)(i)))); | ||||
631 | *graph_ref = (repo->rnum
* (size_t)(0))) : 0; | ||||
632 | assert((tensor_bind_size_ref && tensor_binds_ref) || (!tensor_bind_size_ref && !tensor_binds_ref))((void) sizeof (((tensor_bind_size_ref && tensor_binds_ref ) || (!tensor_bind_size_ref && !tensor_binds_ref)) ? 1 : 0), __extension__ ({ if ((tensor_bind_size_ref && tensor_binds_ref ) || (!tensor_bind_size_ref && !tensor_binds_ref)) ; else __assert_fail ("(tensor_bind_size_ref && tensor_binds_ref) || (!tensor_bind_size_ref && !tensor_binds_ref)" , "ccv_nnc_symbolic_graph_io.c", 632, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
633 | if (tensor_bind_size_ref
| ||||
634 | { | ||||
635 | const char tensor_bind_count_qs[] = | ||||
636 | "SELECT COUNT(*) FROM tensor_bind"; | ||||
637 | sqlite3_stmt* tensor_bind_count_stmt = 0; | ||||
638 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, tensor_bind_count_qs, sizeof(tensor_bind_count_qs), &tensor_bind_count_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, tensor_bind_count_qs , sizeof(tensor_bind_count_qs), &tensor_bind_count_stmt, 0 )) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2(conn , tensor_bind_count_qs, sizeof(tensor_bind_count_qs), &tensor_bind_count_stmt , 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(conn, tensor_bind_count_qs, sizeof(tensor_bind_count_qs), &tensor_bind_count_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 638, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
639 | sqlite3_step(tensor_bind_count_stmt); | ||||
640 | const int tensor_bind_size = *tensor_bind_size_ref = sqlite3_column_int(tensor_bind_count_stmt, 0); | ||||
641 | sqlite3_finalize(tensor_bind_count_stmt); | ||||
642 | // Respect the insert order (rowid). | ||||
643 | if (!tensor_bind_size) | ||||
644 | *tensor_binds_ref = 0; | ||||
645 | else { | ||||
646 | const char tensor_bind_select_qs[] = | ||||
647 | "SELECT id, graph, type, format, datatype, dim, data FROM tensor_bind"; | ||||
648 | sqlite3_stmt* tensor_bind_select_stmt = 0; | ||||
649 | ccv_nnc_tensor_bind_t* const tensor_binds = *tensor_binds_ref = (ccv_nnc_tensor_bind_t*)ccmallocmalloc(sizeof(ccv_nnc_tensor_bind_t) * tensor_bind_size); | ||||
650 | SQLITE_ENFORCE(SQLITE_OK == sqlite3_prepare_v2(conn, tensor_bind_select_qs, sizeof(tensor_bind_select_qs), &tensor_bind_select_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(conn, tensor_bind_select_qs , sizeof(tensor_bind_select_qs), &tensor_bind_select_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( conn, tensor_bind_select_qs, sizeof(tensor_bind_select_qs), & tensor_bind_select_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(conn, tensor_bind_select_qs, sizeof(tensor_bind_select_qs), &tensor_bind_select_stmt, 0)" , "ccv_nnc_symbolic_graph_io.c", 650, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
651 | for (i = 0; SQLITE_ROW100 == sqlite3_step(tensor_bind_select_stmt); i++) | ||||
652 | { | ||||
653 | assert(i < tensor_bind_size)((void) sizeof ((i < tensor_bind_size) ? 1 : 0), __extension__ ({ if (i < tensor_bind_size) ; else __assert_fail ("i < tensor_bind_size" , "ccv_nnc_symbolic_graph_io.c", 653, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
654 | tensor_binds[i].symbol.d = sqlite3_column_int(tensor_bind_select_stmt, 0); | ||||
655 | const int graph_idx = sqlite3_column_int(tensor_bind_select_stmt, 1); | ||||
656 | assert(graph_idx < repo->rnum)((void) sizeof ((graph_idx < repo->rnum) ? 1 : 0), __extension__ ({ if (graph_idx < repo->rnum) ; else __assert_fail ("graph_idx < repo->rnum" , "ccv_nnc_symbolic_graph_io.c", 656, __extension__ __PRETTY_FUNCTION__ ); })); | ||||
657 | tensor_binds[i].symbol.graph = (graph_idx >= 0) ? *(ccv_nnc_symbolic_graph_t**)ccv_array_get(repo, graph_idx)((void*)(((char*)((repo)->data)) + (size_t)(repo)->rsize * (size_t)(graph_idx))) : 0; | ||||
658 | ccv_nnc_tensor_param_t info; | ||||
659 | info.type = sqlite3_column_int(tensor_bind_select_stmt, 2); | ||||
660 | info.format = sqlite3_column_int(tensor_bind_select_stmt, 3); | ||||
661 | info.datatype = sqlite3_column_int(tensor_bind_select_stmt, 4); | ||||
662 | const int* const dim = sqlite3_column_blob(tensor_bind_select_stmt, 5); | ||||
663 | memset(info.dim, 0, sizeof(info.dim)); | ||||
664 | if (dim) | ||||
665 | memcpy(info.dim, dim, ccv_min(sizeof(info.dim), sqlite3_column_bytes(tensor_bind_select_stmt, 5))({ typeof (sizeof(info.dim)) _a = (sizeof(info.dim)); typeof ( sqlite3_column_bytes(tensor_bind_select_stmt, 5)) _b = (sqlite3_column_bytes (tensor_bind_select_stmt, 5)); (_a < _b) ? _a : _b; })); | ||||
666 | const void* const data = sqlite3_column_blob(tensor_bind_select_stmt, 6); | ||||
667 | if (!data) | ||||
668 | tensor_binds[i].tensor = 0; | ||||
669 | else { | ||||
670 | tensor_binds[i].tensor = ccv_nnc_tensor_new(0, info, 0); | ||||
| |||||
671 | size_t data_size = ccv_nnc_tensor_data_size(info); | ||||
672 | #ifdef HAVE_CUDA1 | ||||
673 | if (CCV_TENSOR_GET_MEMORY(info.type)((info.type) & 0x3) == CCV_TENSOR_GPU_MEMORY) | ||||
674 | cumemcpy(tensor_binds[i].tensor->data.u8, info.type, data, CCV_TENSOR_CPU_MEMORY, ccv_min(data_size, sqlite3_column_bytes(tensor_bind_select_stmt, 6))({ typeof (data_size) _a = (data_size); typeof (sqlite3_column_bytes (tensor_bind_select_stmt, 6)) _b = (sqlite3_column_bytes(tensor_bind_select_stmt , 6)); (_a < _b) ? _a : _b; })); | ||||
675 | else | ||||
676 | memcpy(tensor_binds[i].tensor->data.u8, data, ccv_min(data_size, sqlite3_column_bytes(tensor_bind_select_stmt, 6))({ typeof (data_size) _a = (data_size); typeof (sqlite3_column_bytes (tensor_bind_select_stmt, 6)) _b = (sqlite3_column_bytes(tensor_bind_select_stmt , 6)); (_a < _b) ? _a : _b; })); | ||||
677 | #else | ||||
678 | memcpy(tensor_binds[i].tensor->data.u8, data, ccv_min(data_size, sqlite3_column_bytes(tensor_bind_select_stmt, 6))({ typeof (data_size) _a = (data_size); typeof (sqlite3_column_bytes (tensor_bind_select_stmt, 6)) _b = (sqlite3_column_bytes(tensor_bind_select_stmt , 6)); (_a < _b) ? _a : _b; })); | ||||
679 | #endif | ||||
680 | } | ||||
681 | } | ||||
682 | for (; i < tensor_bind_size; i++) | ||||
683 | { | ||||
684 | tensor_binds[i].symbol.d = CCV_NNC_NO_TENSOR_SYMBOL; | ||||
685 | tensor_binds[i].symbol.graph = 0; | ||||
686 | tensor_binds[i].tensor = 0; | ||||
687 | } | ||||
688 | sqlite3_finalize(tensor_bind_select_stmt); | ||||
689 | } | ||||
690 | } | ||||
691 | ccv_array_free(repo); | ||||
692 | sqlite3_finalize(graph_select_stmt); | ||||
693 | sqlite3_finalize(tensor_symbol_select_stmt); | ||||
694 | sqlite3_finalize(exec_symbol_select_stmt); | ||||
695 | sqlite3_close(conn); | ||||
696 | } |