File: | nnc/ccv_cnnp_model_core.c |
Warning: | line 115, 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 = 0; |
26 | for (i = 0; i < self->sequence_size; i++) |
27 | { |
28 | ccv_cnnp_model_t* const model = self->sequence[i]; |
29 | if (model->deinit_state) |
30 | continue; |
31 | ccv_cnnp_model_deinit(model); |
32 | self->sequence[j++] = model; |
33 | } |
34 | self->sequence_size = j; |
35 | } |
36 | |
37 | static void _ccv_cnnp_sequential_model_dealloc(ccv_cnnp_model_t* const super) |
38 | { |
39 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
40 | int i; |
41 | for (i = 0; i < self->sequence_size; i++) |
42 | ccv_cnnp_model_free(self->sequence[i]); |
43 | } |
44 | |
45 | 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) |
46 | { |
47 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
48 | 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 ); |
49 | ccv_cnnp_model_t* const sub_model = self->sequence[0]; |
50 | // Go through each sub model to build the graph. |
51 | ccv_nnc_tensor_symbol_t input; |
52 | sub_model->data = self->super.data; |
53 | ccv_cnnp_model_build(sub_model, graph, inputs, input_size, &input, 1); |
54 | sub_model->data = 0; |
55 | int i; |
56 | for (i = 1; i < self->sequence_size; i++) |
57 | { |
58 | ccv_nnc_tensor_symbol_t output; |
59 | ccv_cnnp_model_t* const sub_model = self->sequence[i]; |
60 | // Go through each sub model to build the graph. |
61 | sub_model->data = self->super.data; |
62 | ccv_cnnp_model_build(sub_model, graph, &input, 1, &output, 1); |
63 | sub_model->data = 0; |
64 | input = output; |
65 | } |
66 | outputs[0] = input; |
67 | 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); |
68 | } |
69 | |
70 | 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) |
71 | { |
72 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
73 | int i; |
74 | for (i = 0; i < self->sequence_size; i++) |
75 | ccv_cnnp_model_init_states(self->sequence[i], graph, initializer, context); |
76 | } |
77 | |
78 | 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) |
79 | { |
80 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
81 | int i; |
82 | for (i = 0; i < self->sequence_size; i++) |
83 | ccv_cnnp_model_set_is_test(self->sequence[i], is_test, updater, context); |
84 | } |
85 | |
86 | static ccv_cnnp_model_t* _ccv_cnnp_sequential_model_copy(const ccv_cnnp_model_t* const super, void* const context); |
87 | |
88 | 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) |
89 | { |
90 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
91 | int i; |
92 | for (i = 0; i < self->sequence_size; i++) |
93 | ccv_cnnp_model_add_to_parameter_indices(self->sequence[i], index, parameter_indices); |
94 | } |
95 | |
96 | static void _ccv_cnnp_sequential_model_notify(const ccv_cnnp_model_t* const super, const int tag, void* const payload) |
97 | { |
98 | ccv_cnnp_sequential_model_t* const self = (ccv_cnnp_sequential_model_t*)super; |
99 | int i; |
100 | for (i = 0; i < self->sequence_size; i++) |
101 | ccv_cnnp_model_notify(self->sequence[i], tag, payload); |
102 | } |
103 | |
104 | static const ccv_cnnp_model_vtab_t ccv_cnnp_sequential_model_isa = { |
105 | .deinit = _ccv_cnnp_sequential_model_deinit, |
106 | .dealloc = _ccv_cnnp_sequential_model_dealloc, |
107 | .build = _ccv_cnnp_sequential_model_build, |
108 | .init_states = _ccv_cnnp_sequential_model_init_states, |
109 | .copy = _ccv_cnnp_sequential_model_copy, |
110 | .set_is_test = _ccv_cnnp_sequential_model_set_is_test, |
111 | .add_to_parameter_indices = _ccv_cnnp_sequential_model_add_to_parameter_indices, |
112 | .notify = _ccv_cnnp_sequential_model_notify, |
113 | }; |
114 | |
115 |