File: | nnc/ccv_cnnp_model_core.c |
Warning: | line 356, column 1 Assigned value is garbage or undefined |
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_cnnp_model.h" |
6 | #include "3rdparty/khash/khash.h" |
7 | |
8 | // MARK - Baisc Layers |
9 | |
10 | static const ccv_cnnp_model_vtab_t ccv_cnnp_input_isa; |
11 | |
12 | #define CCV_CNNP_IS_MODEL_INPUT(x)((x)->isa == &ccv_cnnp_input_isa) ((x)->isa == &ccv_cnnp_input_isa) |
13 | |
14 | #define CCV_CNNP_IS_MODEL_PARAMETER(x)((x)->param_ref != 0 || (x)->param_sel != 0) ((x)->param_ref != 0 || (x)->param_sel != 0) |
15 | |
16 | typedef struct { |
17 | ccv_cnnp_model_t super; |
18 | int sequence_size; |
19 | ccv_cnnp_model_t* sequence[1]; |
20 | } ccv_cnnp_sequential_model_t; |
21 | |
22 | static void _ccv_cnnp_sequential_model_deinit(ccv_cnnp_model_t* const super) |
23 | { |
24 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
25 | int i, j; |
26 | for (i = 0; i < self->sequence_size; i++) |
27 | { |
28 | ccv_cnnp_model_t* const model = self->sequence[i]; |
29 | if (!model) |
30 | continue; |
31 | ccv_cnnp_model_free(model); |
32 | for (j = i + 1; j < self->sequence_size; j++) |
33 | if (self->sequence[j] == model) |
34 | self->sequence[j] = 0; |
35 | } |
36 | } |
37 | |
38 | static void _ccv_cnnp_sequential_model_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size) |
39 | { |
40 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
41 | PRINT(CCV_CLI_VERBOSE, "[cnnp_sequential_model_build] 1. %p, sequence_size: %d\n", self, self->sequence_size)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels())) { printf("[cnnp_sequential_model_build] 1. %p, sequence_size: %d\n" , self, self->sequence_size); fflush(stdout); } } while (0 ); |
42 | ccv_cnnp_model_t* const sub_model = self->sequence[0]; |
43 | // Go through each sub model to build the graph. |
44 | ccv_nnc_tensor_symbol_t input; |
45 | sub_model->data = self->super.data; |
46 | ccv_cnnp_model_build(sub_model, graph, inputs, input_size, &input, 1); |
47 | sub_model->data = 0; |
48 | int i; |
49 | for (i = 1; i < self->sequence_size; i++) |
50 | { |
51 | ccv_nnc_tensor_symbol_t output; |
52 | ccv_cnnp_model_t* const sub_model = self->sequence[i]; |
53 | // Go through each sub model to build the graph. |
54 | sub_model->data = self->super.data; |
55 | ccv_cnnp_model_build(sub_model, graph, &input, 1, &output, 1); |
56 | sub_model->data = 0; |
57 | input = output; |
58 | } |
59 | outputs[0] = input; |
60 | PRINT(CCV_CLI_VERBOSE, "[cnnp_sequential_model_build] 2. %p\n", self)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels())) { printf("[cnnp_sequential_model_build] 2. %p\n", self); fflush (stdout); } } while (0); |
61 | } |
62 | |
63 | static void _ccv_cnnp_sequential_model_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context) |
64 | { |
65 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
66 | int i; |
67 | for (i = 0; i < self->sequence_size; i++) |
68 | ccv_cnnp_model_init_states(self->sequence[i], graph, initializer, context); |
69 | } |
70 | |
71 | static void _ccv_cnnp_sequential_model_set_is_test(ccv_cnnp_model_t* const super, const int is_test, const ccv_cnnp_cmd_updater_f updater, void* const context) |
72 | { |
73 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
74 | int i; |
75 | for (i = 0; i < self->sequence_size; i++) |
76 | ccv_cnnp_model_set_is_test(self->sequence[i], is_test, updater, context); |
77 | } |
78 | |
79 | static ccv_cnnp_model_t* _ccv_cnnp_sequential_model_copy(const ccv_cnnp_model_t* const super, void* const context); |
80 | |
81 | static void _ccv_cnnp_sequential_model_add_to_parameter_indices(ccv_cnnp_model_t* const super, const int index, ccv_array_t* const parameter_indices) |
82 | { |
83 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
84 | int i; |
85 | for (i = 0; i < self->sequence_size; i++) |
86 | ccv_cnnp_model_add_to_parameter_indices(self->sequence[i], index, parameter_indices); |
87 | } |
88 | |
89 | static void _ccv_cnnp_sequential_model_notify(const ccv_cnnp_model_t* const super, const int tag, void* const payload) |
90 | { |
91 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
92 | int i; |
93 | for (i = 0; i < self->sequence_size; i++) |
94 | ccv_cnnp_model_notify(self->sequence[i], tag, payload); |
95 | } |
96 | |
97 | static const ccv_cnnp_model_vtab_t ccv_cnnp_sequential_model_isa = { |
98 | .deinit = _ccv_cnnp_sequential_model_deinit, |
99 | .build = _ccv_cnnp_sequential_model_build, |
100 | .init_states = _ccv_cnnp_sequential_model_init_states, |
101 | .copy = _ccv_cnnp_sequential_model_copy, |
102 | .set_is_test = _ccv_cnnp_sequential_model_set_is_test, |
103 | .add_to_parameter_indices = _ccv_cnnp_sequential_model_add_to_parameter_indices, |
104 | .notify = _ccv_cnnp_sequential_model_notify, |
105 | }; |
106 | |
107 | KHASH_MAP_INIT_INT64(model, ccv_cnnp_model_t*)typedef struct kh_model_s { khint_t n_buckets, size, n_occupied , upper_bound; khint32_t *flags; khint64_t *keys; ccv_cnnp_model_t * *vals; } kh_model_t; static inline __attribute__ ((__unused__ )) kh_model_t *kh_init_model(void) { return (kh_model_t*)calloc (1,sizeof(kh_model_t)); } static inline __attribute__ ((__unused__ )) void kh_destroy_model(kh_model_t *h) { if (h) { free((void *)h->keys); free(h->flags); free((void *)h->vals); free (h); } } static inline __attribute__ ((__unused__)) void kh_clear_model (kh_model_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_model (const kh_model_t *h, khint64_t key) { if (h->n_buckets) { khint_t k, i, last, mask, step = 0; mask = h->n_buckets - 1; k = (khint32_t)((key)>>33^(key)^(key)<<11); 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_model (kh_model_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) { khint64_t *new_keys = (khint64_t*)realloc((void *)h->keys,new_n_buckets * sizeof (khint64_t)); if (!new_keys) { free(new_flags); return -1; } h ->keys = new_keys; if (1) { ccv_cnnp_model_t* *new_vals = ( ccv_cnnp_model_t**)realloc((void *)h->vals,new_n_buckets * sizeof(ccv_cnnp_model_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) { khint64_t key = h->keys[j]; ccv_cnnp_model_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)>>33^(key )^(key)<<11); 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) { { khint64_t tmp = h->keys[i]; h->keys[i] = key ; key = tmp; } if (1) { ccv_cnnp_model_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 = (khint64_t*)realloc((void *)h->keys,new_n_buckets * sizeof(khint64_t)); if (1) h-> vals = (ccv_cnnp_model_t**)realloc((void *)h->vals,new_n_buckets * sizeof(ccv_cnnp_model_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_model(kh_model_t *h, khint64_t key, int *ret) { khint_t x; if (h->n_occupied >= h-> upper_bound) { if (h->n_buckets > (h->size<<1) ) { if (kh_resize_model(h, h->n_buckets - 1) < 0) { *ret = -1; return h->n_buckets; } } else if (kh_resize_model(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)>> 33^(key)^(key)<<11); 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_model(kh_model_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; } } |
108 | |
109 | static ccv_cnnp_model_t* _ccv_cnnp_sequential_model_copy(const ccv_cnnp_model_t* const super, void* const context) |
110 | { |
111 | const ccv_cnnp_sequential_model_t* const self = (const ccv_cnnp_sequential_model_t*)super; |
112 | ccv_cnnp_sequential_model_t* const sequential_model = (ccv_cnnp_sequential_model_t*)cccalloccalloc(1, sizeof(ccv_cnnp_sequential_model_t) + sizeof(ccv_cnnp_model_t*) * (self->sequence_size - 1) + sizeof(ccv_nnc_tensor_symbol_t)); |
113 | sequential_model->super.isa = &ccv_cnnp_sequential_model_isa; |
114 | sequential_model->super.input_size = 1; |
115 | sequential_model->super.outputs = (ccv_nnc_tensor_symbol_t*)(sequential_model->sequence + self->sequence_size); |
116 | sequential_model->super.output_size = 1; |
117 | ccv_cnnp_model_copy_name(&sequential_model->super, self->super.name); |
118 | sequential_model->sequence_size = self->sequence_size; |
119 | int i; |
120 | khash_t(model)kh_model_t* model_map = context ? (khash_t(model)kh_model_t*)context : kh_init(model)kh_init_model(); |
121 | for (i = 0; i < self->sequence_size; i++) |
122 | { |
123 | ccv_cnnp_model_t* const sub_model = self->sequence[i]; |
124 | int ret; |
125 | khiter_t k = kh_put(model, model_map, (uint64_t)(uintptr_t)sub_model, &ret)kh_put_model(model_map, (uint64_t)(uintptr_t)sub_model, & ret); |
126 | ccv_cnnp_model_t* model_copy; |
127 | if (ret != 0) |
128 | model_copy = kh_val(model_map, k)((model_map)->vals[k]) = _ccv_cnnp_model_copy(sub_model, model_map); |
129 | else |
130 | model_copy = kh_val(model_map, k)((model_map)->vals[k]); |
131 | sequential_model->sequence[i] = model_copy; |
132 | } |
133 | if (!context) |
134 | kh_destroy(model, model_map)kh_destroy_model(model_map); |
135 | return (ccv_cnnp_model_t*)sequential_model; |
136 | } |
137 | |
138 | ccv_cnnp_model_t* ccv_cnnp_sequential_new(ccv_cnnp_model_t* const* const models, const int model_size, const int is_trainable, const char* const name) |
139 | { |
140 | assert(model_size > 0)((void) sizeof ((model_size > 0) ? 1 : 0), __extension__ ( { if (model_size > 0) ; else __assert_fail ("model_size > 0" , "ccv_cnnp_model_core.c", 140, __extension__ __PRETTY_FUNCTION__ ); })); |
141 | ccv_cnnp_sequential_model_t* const sequential_model = (ccv_cnnp_sequential_model_t*)cccalloccalloc(1, sizeof(ccv_cnnp_sequential_model_t) + sizeof(ccv_cnnp_model_t*) * (model_size - 1) + sizeof(ccv_nnc_tensor_symbol_t)); |
142 | sequential_model->super.isa = &ccv_cnnp_sequential_model_isa; |
143 | sequential_model->super.input_size = models[0]->input_size; |
144 | sequential_model->super.outputs = (ccv_nnc_tensor_symbol_t*)(sequential_model->sequence + model_size); |
145 | sequential_model->super.output_size = 1; |
146 | sequential_model->super.is_trainable = is_trainable; |
147 | ccv_cnnp_model_copy_name(&sequential_model->super, name); |
148 | sequential_model->sequence_size = model_size; |
149 | memcpy(sequential_model->sequence, models, sizeof(ccv_cnnp_model_t*) * model_size); |
150 | return (ccv_cnnp_model_t*)sequential_model; |
151 | } |
152 | |
153 | typedef struct { |
154 | ccv_cnnp_model_t super; |
155 | // The model's outputs, it is different from super.output_size, as latter is for actual tensor symbols. |
156 | int model_output_size; |
157 | // The name is similar to sequential model, but it is just topological sorted models. |
158 | int sequence_size; |
159 | int* model_outputs; // Which model, as in sequences, have some outputs. |
160 | ccv_cnnp_model_io_t sequence[1]; |
161 | } ccv_cnnp_functional_model_t; |
162 | |
163 | static void _ccv_cnnp_functional_model_deinit(ccv_cnnp_model_t* const super) |
164 | { |
165 | ccv_cnnp_functional_model_t* const self = (ccv_cnnp_functional_model_t*)super; |
166 | int i, j = 0, k; |
167 | for (i = 0; i < self->sequence_size; i++) |
168 | { |
169 | ccv_cnnp_model_t* const model = self->sequence[i]->model; |
170 | if (!model) |
171 | continue; |
172 | self->sequence[j++] = (ccv_cnnp_model_io_t)model; |
173 | // Go through all their IO to remove itself as model. |
174 | assert(model->io)((void) sizeof ((model->io) ? 1 : 0), __extension__ ({ if ( model->io) ; else __assert_fail ("model->io", "ccv_cnnp_model_core.c" , 174, __extension__ __PRETTY_FUNCTION__); })); |
175 | for (k = 0; k < model->io->rnum; k++) |
176 | { |
177 | ccv_cnnp_model_io_t model_io = *(ccv_cnnp_model_io_t*)ccv_array_get(model->io, k)((void*)(((char*)((model->io)->data)) + (size_t)(model-> io)->rsize * (size_t)(k))); |
178 | model_io->model = 0; |
179 | } |
180 | } |
181 | for (i = 0; i < j; i++) |
182 | ccv_cnnp_model_free((ccv_cnnp_model_t*)self->sequence[i]); |
183 | } |
184 | |
185 | KHASH_MAP_INIT_INT64(io_node, ccv_array_t*)typedef struct kh_io_node_s { khint_t n_buckets, size, n_occupied , upper_bound; khint32_t *flags; khint64_t *keys; ccv_array_t * *vals; } kh_io_node_t; static inline __attribute__ ((__unused__ )) kh_io_node_t *kh_init_io_node(void) { return (kh_io_node_t *)calloc(1,sizeof(kh_io_node_t)); } static inline __attribute__ ((__unused__)) void kh_destroy_io_node(kh_io_node_t *h) { if (h) { free((void *)h->keys); free(h->flags); free((void *)h->vals); free(h); } } static inline __attribute__ ((__unused__ )) void kh_clear_io_node(kh_io_node_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_io_node(const kh_io_node_t *h, khint64_t key) { if (h->n_buckets) { khint_t k, i, last, mask, step = 0; mask = h->n_buckets - 1; k = (khint32_t)((key)>> 33^(key)^(key)<<11); 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_io_node(kh_io_node_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) { khint64_t *new_keys = (khint64_t*)realloc((void *)h->keys,new_n_buckets * sizeof (khint64_t)); if (!new_keys) { free(new_flags); return -1; } h ->keys = new_keys; if (1) { ccv_array_t* *new_vals = (ccv_array_t **)realloc((void *)h->vals,new_n_buckets * sizeof(ccv_array_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) { khint64_t key = h->keys[j]; ccv_array_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)>>33^(key)^(key)<<11); 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) { { khint64_t tmp = h-> keys[i]; h->keys[i] = key; key = tmp; } if (1) { ccv_array_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 = ( khint64_t*)realloc((void *)h->keys,new_n_buckets * sizeof( khint64_t)); if (1) h->vals = (ccv_array_t**)realloc((void *)h->vals,new_n_buckets * sizeof(ccv_array_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_io_node(kh_io_node_t *h, khint64_t key, int *ret) { khint_t x; if (h->n_occupied >= h->upper_bound) { if (h->n_buckets > (h->size <<1)) { if (kh_resize_io_node(h, h->n_buckets - 1) < 0) { *ret = -1; return h->n_buckets; } } else if (kh_resize_io_node (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)>> 33^(key)^(key)<<11); 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_io_node(kh_io_node_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; } } |
186 | |
187 | typedef struct { |
188 | ccv_array_t* nodes; |
189 | ccv_nnc_graph_exec_symbol_new_hook_f previous_func; |
190 | void* previous_context; |
191 | } ccv_functional_model_build_node_hook_t; |
192 | |
193 | static void _ccv_cnnp_functional_model_build_node_new(void* context, const ccv_nnc_graph_exec_symbol_t symbol, const ccv_nnc_cmd_t cmd, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, const ccv_nnc_tensor_symbol_t* const outputs, const int output_size, const char* const name) |
194 | { |
195 | ccv_functional_model_build_node_hook_t* const hook = (ccv_functional_model_build_node_hook_t*)context; |
196 | ccv_array_push(hook->nodes, &symbol); |
197 | if (hook->previous_func) |
198 | hook->previous_func(hook->previous_context, symbol, cmd, inputs, input_size, outputs, output_size, name); |
199 | } |
200 | |
201 | static void _ccv_cnnp_functional_model_build(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t* const inputs, const int input_size, ccv_nnc_tensor_symbol_t* const outputs, const int output_size) |
202 | { |
203 | ccv_cnnp_functional_model_t* const self = (ccv_cnnp_functional_model_t*)super; |
204 | PRINT(CCV_CLI_VERBOSE, "[cnnp_functional_model_build] 1. %p, input_size: %d, output_size: %d\n", self, input_size, output_size)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels())) { printf("[cnnp_functional_model_build] 1. %p, input_size: %d, output_size: %d\n" , self, input_size, output_size); fflush(stdout); } } while ( 0); |
205 | assert(self->super.input_size == input_size)((void) sizeof ((self->super.input_size == input_size) ? 1 : 0), __extension__ ({ if (self->super.input_size == input_size ) ; else __assert_fail ("self->super.input_size == input_size" , "ccv_cnnp_model_core.c", 205, __extension__ __PRETTY_FUNCTION__ ); })); |
206 | assert(self->super.output_size == output_size)((void) sizeof ((self->super.output_size == output_size) ? 1 : 0), __extension__ ({ if (self->super.output_size == output_size ) ; else __assert_fail ("self->super.output_size == output_size" , "ccv_cnnp_model_core.c", 206, __extension__ __PRETTY_FUNCTION__ ); })); |
207 | int i, j, k; |
208 | for (i = 0; i < self->super.input_size; i++) |
209 | self->sequence[i]->outputs[0] = self->sequence[i]->model->outputs[0] = inputs[i]; // Assigning the output symbol of input layer to be the input symbol. |
210 | ccv_array_t* input_symbols = ccv_array_new(sizeof(ccv_nnc_tensor_symbol_t), 1, 0); |
211 | ccv_array_t* parameter_indices = 0; |
212 | khash_t(io_node)kh_io_node_t* io_node_map = kh_init(io_node)kh_init_io_node(); |
213 | for (i = self->super.input_size; i < self->sequence_size; i++) |
214 | { |
215 | ccv_cnnp_model_t* const sub_model = self->sequence[i]->model; |
216 | ccv_array_clear(input_symbols); |
217 | const ccv_array_t* const incomings = self->sequence[i]->incomings; |
218 | if (incomings) |
219 | for (j = 0; j < incomings->rnum; j++) |
220 | { |
221 | const ccv_cnnp_model_io_t input = *(ccv_cnnp_model_io_t*)ccv_array_get(incomings, j)((void*)(((char*)((incomings)->data)) + (size_t)(incomings )->rsize * (size_t)(j))); |
222 | if (CCV_CNNP_IS_MODEL_PARAMETER(input)((input)->param_ref != 0 || (input)->param_sel != 0)) |
223 | { |
224 | if (!parameter_indices) |
225 | parameter_indices = ccv_array_new(sizeof(int), 0, 0); |
226 | else |
227 | ccv_array_clear(parameter_indices); |
228 | const int param_sel = input->param_sel > 0 ? input->param_sel - 1 : input->param_sel; |
229 | assert(input->param_sel != 0)((void) sizeof ((input->param_sel != 0) ? 1 : 0), __extension__ ({ if (input->param_sel != 0) ; else __assert_fail ("input->param_sel != 0" , "ccv_cnnp_model_core.c", 229, __extension__ __PRETTY_FUNCTION__ ); })); |
230 | ccv_cnnp_model_add_to_parameter_indices(input->model, param_sel, parameter_indices); |
231 | assert(parameter_indices->rnum > 0)((void) sizeof ((parameter_indices->rnum > 0) ? 1 : 0), __extension__ ({ if (parameter_indices->rnum > 0) ; else __assert_fail ("parameter_indices->rnum > 0", "ccv_cnnp_model_core.c" , 231, __extension__ __PRETTY_FUNCTION__); })); |
232 | const int param_ref = input->param_ref > 0 ? input->param_ref - 1 : input->param_ref; |
233 | assert(input->param_ref != 0)((void) sizeof ((input->param_ref != 0) ? 1 : 0), __extension__ ({ if (input->param_ref != 0) ; else __assert_fail ("input->param_ref != 0" , "ccv_cnnp_model_core.c", 233, __extension__ __PRETTY_FUNCTION__ ); })); |
234 | if (param_ref >= 0) |
235 | { |
236 | assert(param_ref < parameter_indices->rnum)((void) sizeof ((param_ref < parameter_indices->rnum) ? 1 : 0), __extension__ ({ if (param_ref < parameter_indices ->rnum) ; else __assert_fail ("param_ref < parameter_indices->rnum" , "ccv_cnnp_model_core.c", 236, __extension__ __PRETTY_FUNCTION__ ); })); |
237 | const ccv_nnc_tensor_symbol_t parameter = ccv_cnnp_parameter_from_indice(super, *(int*)ccv_array_get(parameter_indices, param_ref)((void*)(((char*)((parameter_indices)->data)) + (size_t)(parameter_indices )->rsize * (size_t)(param_ref)))); |
238 | ccv_array_push(input_symbols, ¶meter); |
239 | } else // Otherwise, all of them. |
240 | for (k = 0; k < parameter_indices->rnum; k++) |
241 | { |
242 | const ccv_nnc_tensor_symbol_t parameter = ccv_cnnp_parameter_from_indice(super, *(int*)ccv_array_get(parameter_indices, k)((void*)(((char*)((parameter_indices)->data)) + (size_t)(parameter_indices )->rsize * (size_t)(k)))); |
243 | ccv_array_push(input_symbols, ¶meter); |
244 | } |
245 | } else { |
246 | for (k = 0; k < input->model->output_size; k++) |
247 | ccv_array_push(input_symbols, &input->outputs[k]); |
248 | } |
249 | } |
250 | // Go through each sub model to build the graph. |
251 | ccv_array_t* nodes; |
252 | ccv_functional_model_build_node_hook_t hook; |
253 | const ccv_array_t* const dependencies = self->sequence[i]->dependencies; |
254 | if ((dependencies && dependencies->rnum > 0) || self->sequence[i]->dependents > 0) |
255 | { |
256 | int ret; |
257 | khiter_t k = kh_put(io_node, io_node_map, (uint64_t)(uintptr_t)self->sequence[i], &ret)kh_put_io_node(io_node_map, (uint64_t)(uintptr_t)self->sequence [i], &ret); |
258 | if (ret != 0) |
259 | nodes = kh_val(io_node_map, k)((io_node_map)->vals[k]) = ccv_array_new(sizeof(ccv_nnc_graph_exec_symbol_t), 1, 0); |
260 | else |
261 | nodes = kh_val(io_node_map, k)((io_node_map)->vals[k]); |
262 | hook.nodes = nodes; |
263 | hook.previous_context = ccv_nnc_graph_exec_symbol_new_hook(graph, _ccv_cnnp_functional_model_build_node_new, &hook, &hook.previous_func); |
264 | } |
265 | sub_model->data = self->super.data; |
266 | ccv_cnnp_model_build(sub_model, graph, (ccv_nnc_tensor_symbol_t*)ccv_array_get(input_symbols, 0)((void*)(((char*)((input_symbols)->data)) + (size_t)(input_symbols )->rsize * (size_t)(0))), input_symbols->rnum, self->sequence[i]->outputs, sub_model->output_size); |
267 | if ((dependencies && dependencies->rnum > 0) || self->sequence[i]->dependents > 0) |
268 | { |
269 | ccv_nnc_graph_exec_symbol_new_hook(graph, hook.previous_func, hook.previous_context, 0); |
270 | if (dependencies) |
271 | for (j = 0; j < dependencies->rnum; j++) |
272 | { |
273 | const ccv_cnnp_model_io_t dependency = *(ccv_cnnp_model_io_t*)ccv_array_get(dependencies, j)((void*)(((char*)((dependencies)->data)) + (size_t)(dependencies )->rsize * (size_t)(j))); |
274 | khiter_t k = kh_get(io_node, io_node_map, (uint64_t)(uintptr_t)dependency)kh_get_io_node(io_node_map, (uint64_t)(uintptr_t)dependency); |
275 | if (k == kh_end(io_node_map)((io_node_map)->n_buckets)) |
276 | continue; |
277 | const ccv_array_t* const dependency_nodes = kh_val(io_node_map, k)((io_node_map)->vals[k]); |
278 | int x, y; |
279 | for (y = 0; y < dependency_nodes->rnum; y++) |
280 | for (x = 0; x < nodes->rnum; x++) |
281 | ccv_nnc_graph_exec_symbol_concat(graph, *(ccv_nnc_graph_exec_symbol_t*)ccv_array_get(dependency_nodes, y)((void*)(((char*)((dependency_nodes)->data)) + (size_t)(dependency_nodes )->rsize * (size_t)(y))), *(ccv_nnc_graph_exec_symbol_t*)ccv_array_get(nodes, x)((void*)(((char*)((nodes)->data)) + (size_t)(nodes)->rsize * (size_t)(x)))); |
282 | } |
283 | } |
284 | sub_model->data = 0; |
285 | } |
286 | khiter_t it; |
287 | for (it = kh_begin(io_node_map)(khint_t)(0); it != kh_end(io_node_map)((io_node_map)->n_buckets); ++it) |
288 | { |
289 | if (!kh_exist(io_node_map, it)(!(((io_node_map)->flags[(it)>>4]>>(((it)& 0xfU)<<1))&3))) |
290 | continue; |
291 | ccv_array_t* const nodes = kh_val(io_node_map, it)((io_node_map)->vals[it]); |
292 | ccv_array_free(nodes); |
293 | } |
294 | kh_destroy(io_node, io_node_map)kh_destroy_io_node(io_node_map); |
295 | ccv_array_free(input_symbols); |
296 | if (parameter_indices) |
297 | ccv_array_free(parameter_indices); |
298 | for (i = 0, k = 0; k < self->model_output_size; k++) |
299 | { |
300 | ccv_cnnp_model_t* const sub_model = self->sequence[self->model_outputs[k]]->model; |
301 | for (j = 0; j < sub_model->output_size; j++) |
302 | outputs[i + j] = self->sequence[self->model_outputs[k]]->outputs[j]; |
303 | i += sub_model->output_size; |
304 | } |
305 | assert(i == output_size)((void) sizeof ((i == output_size) ? 1 : 0), __extension__ ({ if (i == output_size) ; else __assert_fail ("i == output_size" , "ccv_cnnp_model_core.c", 305, __extension__ __PRETTY_FUNCTION__ ); })); |
306 | PRINT(CCV_CLI_VERBOSE, "[cnnp_functional_model_build] 2. %p\n", self)do { if ((CCV_CLI_VERBOSE & ccv_cli_get_output_levels())) { printf("[cnnp_functional_model_build] 2. %p\n", self); fflush (stdout); } } while (0); |
307 | } |
308 | |
309 | static void _ccv_cnnp_functional_model_init_states(ccv_cnnp_model_t* const super, ccv_nnc_symbolic_graph_t* const graph, const ccv_cnnp_state_initializer_f initializer, void* const context) |
310 | { |
311 | ccv_cnnp_functional_model_t* const self = (ccv_cnnp_functional_model_t*)super; |
312 | int i; |
313 | for (i = self->super.input_size; i < self->sequence_size; i++) |
314 | ccv_cnnp_model_init_states(self->sequence[i]->model, graph, initializer, context); |
315 | } |
316 | |
317 | static void _ccv_cnnp_functional_model_set_is_test(ccv_cnnp_model_t* const super, const int is_test, const ccv_cnnp_cmd_updater_f updater, void* const context) |
318 | { |
319 | ccv_cnnp_functional_model_t* const self = (ccv_cnnp_functional_model_t*)super; |
320 | int i; |
321 | for (i = self->super.input_size; i < self->sequence_size; i++) |
322 | ccv_cnnp_model_set_is_test(self->sequence[i]->model, is_test, updater, context); |
323 | } |
324 | |
325 | static void _ccv_cnnp_functional_model_add_to_parameter_indices(ccv_cnnp_model_t* const super, const int index, ccv_array_t* const parameter_indices) |
326 | { |
327 | ccv_cnnp_functional_model_t* const self = (ccv_cnnp_functional_model_t*)super; |
328 | int i; |
329 | for (i = self->super.input_size; i < self->sequence_size; i++) |
330 | ccv_cnnp_model_add_to_parameter_indices(self->sequence[i]->model, index, parameter_indices); |
331 | } |
332 | |
333 | static void _ccv_cnnp_functional_model_notify(const ccv_cnnp_model_t* const super, const int tag, void* const payload) |
334 | { |
335 | ccv_cnnp_functional_model_t* const self = (ccv_cnnp_functional_model_t*)super; |
336 | int i; |
337 | for (i = 0; i < self->sequence_size; i++) |
338 | { |
339 | const ccv_cnnp_model_t* const model = self->sequence[i]->model; |
340 | ccv_cnnp_model_notify(model, tag, payload); |
341 | } |
342 | } |
343 | |
344 | static ccv_cnnp_model_t* _ccv_cnnp_functional_model_copy(const ccv_cnnp_model_t* const super, void* const context); |
345 | |
346 | static const ccv_cnnp_model_vtab_t ccv_cnnp_functional_model_isa = { |
347 | .deinit = _ccv_cnnp_functional_model_deinit, |
348 | .build = _ccv_cnnp_functional_model_build, |
349 | .init_states = _ccv_cnnp_functional_model_init_states, |
350 | .copy = _ccv_cnnp_functional_model_copy, |
351 | .set_is_test = _ccv_cnnp_functional_model_set_is_test, |
352 | .add_to_parameter_indices = _ccv_cnnp_functional_model_add_to_parameter_indices, |
353 | .notify = _ccv_cnnp_functional_model_notify, |
354 | }; |
355 | |
356 |