File: | nnc/ccv_cnnp_model_core.c |
Warning: | line 107, 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 |