Coverage Report

Created: 2019-07-03 22:50

/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 peer_ref; // Reference to its peer. 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 peer_ref; // Reference to its peer. 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* peer; // The peer 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
  // This is the allocated non-continuous buffers.
136
  int buffer_size;
137
  struct {
138
    int type; // The type from tensor blocks.
139
    int pin_mem; // Whether this memory is pinned.
140
    uint64_t size;
141
    uint8_t* ptr;
142
  }* buffers;
143
  // Real allocated tensor header metadata (this is a mixed pool of ccv_tensor_t, ccv_tensor_view_t,
144
  // ccv_tensor_multiview_t, thus, it is aligned to a 16-byte boundary).
145
  ccv_array_t* tensor_metadata;
146
  ccv_array_t* m_tensor_idx; // The index into multi-view tensors in tensor_metadata.
147
};
148
149
struct ccv_nnc_graph_exec_arena_s {
150
  intptr_t graph_ref; // A value contains the pointer name of the graph.
151
  int sub_arena_size;
152
  struct ccv_nnc_graph_exec_arena_s** sub_arenas; // Corresponding to sub graphs.
153
  ccv_nnc_graph_exec_t source;
154
  ccv_nnc_graph_exec_t destination;
155
  int graph_exec_size;
156
  ccv_nnc_graph_exec_t graph_execs[1];
157
};
158
159
1
#define CCV_NNC_ENCODE_WHILE_COUNT_SYMBOL(d) ((int)((~(uint32_t)d) << 4 | 0xe))
160
40
#define CCV_NNC_DECODE_WHILE_COUNT_SYMBOL(symbol) ((~(uint32_t)(symbol)) >> 4)
161
162
inline static void ccv_array_replace_unique_int(ccv_array_t* ints, const int idx, const int outgoing)
163
2.08k
{
164
2.08k
  int i;
165
2.08k
  int flag = 0;
166
4.18k
  for (i = 0; i < ints->rnum;)
167
2.10k
  {
168
2.10k
    if (*(int*)ccv_array_get(ints, i) == idx)
169
1.03k
    {
170
1.03k
      if (flag)
171
4
      {
172
4
        if (i < ints->rnum - 1)
173
4
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
174
4
        --ints->rnum;
175
4
        continue;
176
4
      }
177
1.03k
      *(int*)ccv_array_get(ints, i) = outgoing;
178
1.03k
      flag = 1;
179
1.06k
    } else if (*(int*)ccv_array_get(ints, i) == outgoing) {
180
1.01k
      // Remove this from the list.
181
1.01k
      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
1.01k
      flag = 1;
189
1.01k
    }
190
2.10k
    ++i;
191
2.09k
  }
192
2.08k
  if (!flag)
193
38
    ccv_array_push(ints, &outgoing);
194
2.08k
}
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
163
2.07k
{
164
2.07k
  int i;
165
2.07k
  int flag = 0;
166
4.12k
  for (i = 0; i < ints->rnum;)
167
2.05k
  {
168
2.05k
    if (*(int*)ccv_array_get(ints, i) == idx)
169
1.02k
    {
170
1.02k
      if (flag)
171
0
      {
172
0
        if (i < ints->rnum - 1)
173
0
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
174
0
        --ints->rnum;
175
0
        continue;
176
0
      }
177
1.02k
      *(int*)ccv_array_get(ints, i) = outgoing;
178
1.02k
      flag = 1;
179
1.03k
    } else if (*(int*)ccv_array_get(ints, i) == outgoing) {
180
1.01k
      // Remove this from the list.
181
1.01k
      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
1.01k
      flag = 1;
189
1.01k
    }
190
2.05k
    ++i;
191
2.05k
  }
192
2.07k
  if (!flag)
193
34
    ccv_array_push(ints, &outgoing);
194
2.07k
}
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
163
16
{
164
16
  int i;
165
16
  int flag = 0;
166
60
  for (i = 0; i < ints->rnum;)
167
44
  {
168
44
    if (*(int*)ccv_array_get(ints, i) == idx)
169
8
    {
170
8
      if (flag)
171
4
      {
172
4
        if (i < ints->rnum - 1)
173
4
          *(int*)ccv_array_get(ints, i) = *(int*)ccv_array_get(ints, ints->rnum - 1);
174
4
        --ints->rnum;
175
4
        continue;
176
4
      }
177
4
      *(int*)ccv_array_get(ints, i) = outgoing;
178
4
      flag = 1;
179
36
    } else if (*(int*)ccv_array_get(ints, i) == outgoing) {
180
8
      // Remove this from the list.
181
8
      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
8
      flag = 1;
189
8
    }
190
44
    ++i;
191
40
  }
192
16
  if (!flag)
193
4
    ccv_array_push(ints, &outgoing);
194
16
}
Unexecuted instantiation: ccv_nnc_symbolic_graph_simplify.c:ccv_array_replace_unique_int
195
196
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);
197
198
void ccv_nnc_symbolic_graph_add_source(ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_graph_exec_symbol_t source);
199
void ccv_nnc_symbolic_graph_add_destination(ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_graph_exec_symbol_t destination);
200
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);
201
int ccv_nnc_tensor_symbol_map_raw(ccv_nnc_symbolic_graph_t* const graph, const ccv_nnc_tensor_symbol_t symbol);
202
203
#endif
204