Coverage Report

Created: 2021-04-12 01:11

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/_ccv_nnc_symbolic_graph.h
Line
Count
Source (jump to first uncovered line)
1
/**********************************************************
2
 * C-based/Cached/Core Computer Vision Library
3
 * Liu Liu, 2010-02-01
4
 **********************************************************/
5
6
/**********************************************************
7
 * CCV - Neural Network Collection
8
 **********************************************************/
9
10
#ifndef GUARD_ccv_nnc_symbolic_graph_internal_h
11
#define GUARD_ccv_nnc_symbolic_graph_internal_h
12
13
#include "ccv_nnc.h"
14
#include "ccv_nnc_internal.h"
15
16
typedef struct {
17
  // Start for while loop handling
18
  int assign_ref; // Reference to the tensor that the value will be copied from (for parameter passing). Starts at 1.
19
  int r_assign_ref; // It is a reverse of the assign_ref. Starts at 1.
20
  int bypass_ref; // Some exec may not generate output for this tensor. In that case, use the content from tensor bypass (typical case for case..of). Starts at 1.
21
  int r_bypass_ref; // It is a reverse of the bypass_ref. Starts at 1.
22
  int p_ref; // Reference to the tensor number in its parent graph. Starts at 1.
23
  // End of while loop handling.
24
  int alias_ref; // Reference to the tensor. Starts at 1.
25
  int pair_ref; // Reference to its pair. Starts at 1.
26
  int flags;
27
  int ofs[CCV_NNC_MAX_DIM_ALLOC];
28
  int inc[CCV_NNC_MAX_DIM_ALLOC];
29
  ccv_array_t* s_ref; // Reference to the tensor number in its sub graphs, Starts at 1.
30
  char* name;
31
  ccv_nnc_tensor_param_t info;
32
} ccv_nnc_tensor_symbol_info_t;
33
34
enum {
35
  CCV_NNC_GRAPH_EXEC_CASE_OF_NO_BYPASS = 0x01, // If this flag is set, this case..of won't have any bypass.
36
};
37
38
typedef struct {
39
  int input_size;
40
  int output_size;
41
  int graph_ref_size;
42
  int flags; // Mark this node as dead.
43
  int pair_ref; // Reference to its pair. Starts at 1.
44
  int* inputs;
45
  int* outputs;
46
  ccv_array_t* outgoings; // Outgoing nodes
47
  char* name;
48
  ccv_nnc_cmd_t cmd;
49
  ccv_nnc_hint_t hint;
50
  // Below are only relevant to sub-graph nodes (case_of, while).
51
  int _inline_graph_ref[2]; // Reference to the sub-graph. Starts at 1.
52
  int* _heap_graph_ref;
53
  union {
54
    struct {
55
      ccv_nnc_graph_case_of_f expr;
56
      const void* data;
57
      int flags;
58
      struct {
59
        int offset;
60
        int size;
61
      } argument; // range for which data as inputs from input section.
62
    } case_of;
63
    struct {
64
      ccv_nnc_graph_while_f expr;
65
      const void* data;
66
      int* inputs;
67
      int input_size;
68
    } p_while;
69
  };
70
} ccv_nnc_graph_exec_symbol_info_t;
71
72
struct ccv_nnc_symbolic_graph_s {
73
  ccv_array_t* tensor_symbol_info; // A lit of info for tensor symbols.
74
  ccv_array_t* exec_symbol_info; // A list of info for exec symbols.
75
  // I think that I can be more explicit about which are sources and which are destinations.
76
  ccv_array_t* sources;
77
  ccv_array_t* destinations;
78
  // Some extra information piggy-back on symbolic graph struct.
79
  // Start for while loop handling
80
  ccv_array_t* sub_graphs; // A list of its sub-graphs (for while loop).
81
  struct ccv_nnc_symbolic_graph_s* pair; // The pair graph (only useful for backward prop graph).
82
  struct ccv_nnc_symbolic_graph_s* p; // The parent graph (if current one is a sub-graph).
83
  int p_idx; // Reference to the index in its parent graph's sub-graph array, Starts at 1.
84
  int exec_idx; // Reference to the index in its parent graph's exec (the graph exec), Starts at 1.
85
  // Why some of these I choose to be flat int* array, some of these I choose to be ccv_array_t?
86
  // for flat int* array, these are not going to be modified until next time call ccv_nnc_symbolic_graph_backward
87
  // for ccv_array_t, we can continue to modify what's inside.
88
  int breakpoint_size;
89
  ccv_nnc_graph_exec_symbol_t* breakpoints;
90
  // End of while loop handling.
91
  struct {
92
    int tensor;
93
    int exec;
94
  } reuse; // The reuse slot for tensor or graph exec symbols.
95
  // Start for backward (automatic differentiation) handling
96
  struct {
97
    int tensor_symbol_size;
98
    int* tensor_symbol_idx;
99
    int exec_symbol_size;
100
    int* exec_symbol_idx;
101
  } backward;
102
  // End of backward (automatic differentiation) handling.
103
  // For parallel, get duplicated tensors.
104
  struct {
105
    int count;
106
    int tensor_symbol_size;
107
    int* tensor_symbol_idx;
108
    int exec_symbol_size;
109
    int* exec_symbol_idx;
110
  } data_parallel;
111
  // Hooks
112
  struct {
113
    struct {
114
      ccv_nnc_tensor_symbol_new_hook_f func;
115
      void* context;
116
    } tensor_symbol_new;
117
    struct {
118
      ccv_nnc_tensor_symbol_alias_new_hook_f func;
119
      void* context;
120
    } tensor_symbol_alias_new;
121
    struct {
122
      ccv_nnc_graph_exec_symbol_new_hook_f func;
123
      void* context;
124
    } graph_exec_symbol_new;
125
  } hooks;
126
};
127
128
struct ccv_nnc_tensor_arena_s {
129
  intptr_t graph_ref; // A value contains the pointer name of the graph.
130
  int sub_arena_size;
131
  struct ccv_nnc_tensor_arena_s** sub_arenas; // Corresponding to sub graphs.
132
  // This is a table of tensor references to real allocated tensors.
133
  int vt_tensor_size;
134
  ccv_nnc_tensor_t** vt_tensors;
135
  int* vt_alias_refs; // reference to which it is alias to.
136
  int* vt_alias_r_refs_p; // pointer to record which tensor this is aliased to.
137
  int* vt_alias_r_refs; // array to record which tensor this is aliased to. This is first created when bind a symbol.
138
  size_t* vt_sizes; // The size of each tensor.
139
  ccv_numeric_data_t* pb_vt_tensors; // pre-bindings for this vt_tensors, this is optional.
140
  struct {
141
    const ccv_nnc_symbolic_graph_compile_allocator_vtab_t* isa;
142
    struct {
143
      void* free;
144
    } context;
145
  } allocator;
146
  // This is the allocated non-continuous buffers.
147
  int buffer_size;
148
  struct {
149
    int type; // The type from tensor blocks.
150
    int pin_mem; // Whether this memory is pinned.
151
    uint64_t size;
152
    uint8_t* ptr;
153
  }* buffers;
154
  // Real allocated tensor header metadata (this is a mixed pool of ccv_tensor_t, ccv_tensor_view_t,
155
  // ccv_tensor_multiview_t, thus, it is aligned to a 16-byte boundary).
156
  ccv_array_t* tensor_metadata;
157
  ccv_array_t* m_tensor_idx; // The index into multi-view tensors in tensor_metadata.
158
};
159
160
struct ccv_nnc_graph_exec_arena_s {
161
  intptr_t graph_ref; // A value contains the pointer name of the graph.
162
  int sub_arena_size;
163
  struct ccv_nnc_graph_exec_arena_s** sub_arenas; // Corresponding to sub graphs.
164
  ccv_nnc_graph_exec_t source;
165
  ccv_nnc_graph_exec_t destination;
166
  int graph_exec_size;
167
  ccv_nnc_graph_exec_t graph_execs[1];
168
};
169
170
1
#define CCV_NNC_ENCODE_WHILE_COUNT_SYMBOL(d) ((int)((~(uint32_t)d) << 4 | 0xe))
171
40
#define CCV_NNC_DECODE_WHILE_COUNT_SYMBOL(symbol) ((~(uint32_t)(symbol)) >> 4)
172
173
inline static void ccv_array_replace_unique_int(ccv_array_t* ints, const int idx, const int outgoing)
174
8.57k
{
175
8.57k
  int i;
176
8.57k
  int flag = 0;
177
17.1k
  for (i = 0; i < ints->rnum;)
178
8.59k
  {
179
8.59k
    if (*(int*)ccv_array_get(ints, i) == idx)
180
4.28k
    {
181
4.28k
      if (flag)
182
4
      {
183
4
        if (i < ints->rnum - 1)
184
4
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
185
4
        --ints->rnum;
186
4
        continue;
187
4
      }
188
4.28k
      *(int*)ccv_array_get(ints, i) = outgoing;
189
4.28k
      flag = 1;
190
4.30k
    } else if (*(int*)ccv_array_get(ints, i) == outgoing) {
191
4.23k
      // Remove this from the list.
192
4.23k
      if (flag)
193
0
      {
194
0
        if (i < ints->rnum - 1)
195
0
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
196
0
        --ints->rnum;
197
0
        continue;
198
0
      }
199
4.23k
      flag = 1;
200
4.23k
    }
201
8.59k
    ++i;
202
8.58k
  }
203
8.57k
  if (!flag)
204
54
    ccv_array_push(ints, &outgoing);
205
8.57k
}
Unexecuted instantiation: ccv_nnc_tensor_io.c:ccv_array_replace_unique_int
Unexecuted instantiation: ccv_nnc_symbolic_graph.c:ccv_array_replace_unique_int
Unexecuted instantiation: ccv_nnc_symbolic_graph_io.c:ccv_array_replace_unique_int
Unexecuted instantiation: ccv_nnc_symbolic_graph_compile.c:ccv_array_replace_unique_int
ccv_nnc_symbolic_graph_backward.c:ccv_array_replace_unique_int
Line
Count
Source
174
8.55k
{
175
8.55k
  int i;
176
8.55k
  int flag = 0;
177
17.1k
  for (i = 0; i < ints->rnum;)
178
8.54k
  {
179
8.54k
    if (*(int*)ccv_array_get(ints, i) == idx)
180
4.28k
    {
181
4.28k
      if (flag)
182
0
      {
183
0
        if (i < ints->rnum - 1)
184
0
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
185
0
        --ints->rnum;
186
0
        continue;
187
0
      }
188
4.28k
      *(int*)ccv_array_get(ints, i) = outgoing;
189
4.28k
      flag = 1;
190
4.28k
    } else 
if (4.26k
*(int*)4.26k
ccv_array_get4.26k
(ints, i) == outgoing) {
191
4.22k
      // Remove this from the list.
192
4.22k
      if (flag)
193
0
      {
194
0
        if (i < ints->rnum - 1)
195
0
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
196
0
        --ints->rnum;
197
0
        continue;
198
0
      }
199
4.22k
      flag = 1;
200
4.22k
    }
201
8.54k
    ++i;
202
8.54k
  }
203
8.55k
  if (!flag)
204
50
    ccv_array_push(ints, &outgoing);
205
8.55k
}
Unexecuted instantiation: ccv_nnc_symbolic_graph_while.c:ccv_array_replace_unique_int
Unexecuted instantiation: ccv_nnc_symbolic_graph_case_of.c:ccv_array_replace_unique_int
Unexecuted instantiation: ccv_nnc_symbolic_graph_minimize.c:ccv_array_replace_unique_int
ccv_nnc_symbolic_graph_parallel.c:ccv_array_replace_unique_int
Line
Count
Source
174
16
{
175
16
  int i;
176
16
  int flag = 0;
177
60
  for (i = 0; i < ints->rnum;)
178
44
  {
179
44
    if (*(int*)ccv_array_get(ints, i) == idx)
180
8
    {
181
8
      if (flag)
182
4
      {
183
4
        if (i < ints->rnum - 1)
184
4
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
185
4
        --ints->rnum;
186
4
        continue;
187
4
      }
188
4
      *(int*)ccv_array_get(ints, i) = outgoing;
189
4
      flag = 1;
190
36
    } else if (*(int*)ccv_array_get(ints, i) == outgoing) {
191
8
      // Remove this from the list.
192
8
      if (flag)
193
0
      {
194
0
        if (i < ints->rnum - 1)
195
0
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
196
0
        --ints->rnum;
197
0
        continue;
198
0
      }
199
8
      flag = 1;
200
8
    }
201
44
    ++i;
202
40
  }
203
16
  if (!flag)
204
4
    ccv_array_push(ints, &outgoing);
205
16
}
Unexecuted instantiation: ccv_nnc_symbolic_graph_simplify.c:ccv_array_replace_unique_int
Unexecuted instantiation: ccv_nnc_symbolic_graph_memory_compression.c:ccv_array_replace_unique_int
206
207
void ccv_nnc_symbolic_graph_symbol_infer(const ccv_nnc_symbolic_graph_t* const symbolic_graph, const ccv_nnc_graph_visit_t* const visit, const ccv_nnc_graph_exec_symbol_t* const sources, const int source_size, const ccv_nnc_graph_exec_symbol_t* const destinations, const int destination_size, const ccv_nnc_tensor_symbol_info_t* const p_tensor_symbol_info, const int p_tensor_symbol_info_size, ccv_nnc_tensor_symbol_info_t* const tensor_symbol_info, ccv_nnc_graph_exec_symbol_info_t* const exec_symbol_info);
208
209
int ccv_nnc_over_tensor_symbol_aliases(const ccv_nnc_tensor_symbol_info_t* const tensor_a, const ccv_nnc_tensor_symbol_info_t* const tensor_b);
210
int ccv_nnc_tensor_symbol_map_raw(ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t symbol);
211
212
#endif
213