Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/test/int/nnc/symbolic.graph.vgg.d.tests.c
Line
Count
Source (jump to first uncovered line)
1
#include "case.h"
2
#include "ccv_case.h"
3
#include "ccv_nnc_case.h"
4
#include <ccv.h>
5
#include <nnc/ccv_nnc.h>
6
#include <nnc/ccv_nnc_easy.h>
7
#include <inc/ccv_convnet_internal.h>
8
9
TEST_SETUP()
10
{
11
  ccv_nnc_init();
12
}
13
14
static ccv_nnc_symbolic_graph_t* ccv_nnc_simple_symbolic_graph(ccv_convnet_t* convnet, ccv_nnc_tensor_t* input, ccv_nnc_tensor_t* output, ccv_nnc_graph_exec_symbol_t* source_symbol, ccv_nnc_graph_exec_symbol_t* dest_symbol, ccv_nnc_tensor_symbol_t* input_symbol_ref, ccv_nnc_tensor_symbol_t* output_symbol_ref, ccv_nnc_tensor_symbol_t* w_symbols, ccv_nnc_tensor_symbol_t* bias_symbols)
15
1
{
16
1
  int i;
17
1
  // We only create the graph compute to the last fc layer.
18
1
  ccv_nnc_symbolic_graph_t* symbolic_vgg = ccv_nnc_symbolic_graph_new();
19
1
  ccv_nnc_tensor_param_t input_info = input->info;
20
1
  ccv_nnc_tensor_symbol_t input_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, input->info, 0);
21
1
  *input_symbol_ref = input_symbol;
22
1
  ccv_nnc_tensor_symbol_t output_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, output->info, 0);
23
1
  *output_symbol_ref = output_symbol;
24
1
  ccv_nnc_graph_exec_symbol_t previous_exec_symbol;
25
22
  for (i = 0; i < convnet->count; 
i++21
)
26
21
  {
27
21
    ccv_convnet_layer_t* layer = convnet->layers + i;
28
21
    int rows, cols, partition;
29
21
    ccv_convnet_make_output(layer, layer->input.matrix.rows, layer->input.matrix.cols, &rows, &cols, &partition);
30
21
    ccv_nnc_tensor_param_t tensor_info = output->info;
31
21
    ccv_nnc_tensor_symbol_t tensor_symbol = output_symbol;
32
21
    if (i < convnet->count - 1)
33
20
    {
34
20
      if (layer->type == CCV_CONVNET_FULL_CONNECT)
35
2
        tensor_info = CPU_TENSOR_NHWC(32F, rows * cols * partition);
36
20
      else
37
20
        
tensor_info = 18
CPU_TENSOR_NHWC18
(32F, rows, cols, (layer->type == CCV_CONVNET_CONVOLUTIONAL ? layer->net.convolutional.count : layer->input.matrix.channels));
38
20
      tensor_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, tensor_info, 0);
39
20
    }
40
21
    ccv_nnc_graph_exec_symbol_t exec_symbol = {0};
41
21
    if (layer->type == CCV_CONVNET_CONVOLUTIONAL)
42
13
    {
43
13
      ccv_nnc_tensor_symbol_t w_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, CPU_TENSOR_NHWC(32F, layer->net.convolutional.count, layer->net.convolutional.rows, layer->net.convolutional.cols, layer->net.convolutional.channels), 0);
44
13
      w_symbols[i] = w_symbol;
45
13
      ccv_nnc_tensor_symbol_t bias_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, CPU_TENSOR_NHWC(32F, layer->net.convolutional.count), 0);
46
13
      bias_symbols[i] = bias_symbol;
47
13
      ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_FORWARD(1, layer->net.convolutional.count, layer->net.convolutional.rows, layer->net.convolutional.cols, layer->net.convolutional.channels);
48
13
      exec_symbol = ccv_nnc_graph_exec_symbol_new(symbolic_vgg, cmd, TENSOR_SYMBOL_LIST(input_symbol, w_symbol, bias_symbol), TENSOR_SYMBOL_LIST(tensor_symbol), 0);
49
13
    } else 
if (8
layer->type == CCV_CONVNET_MAX_POOL8
) {
50
5
      ccv_nnc_cmd_t cmd = CMD_MAX_POOL_FORWARD(layer->net.pool.size, layer->net.pool.size);
51
5
      exec_symbol = ccv_nnc_graph_exec_symbol_new(symbolic_vgg, cmd, TENSOR_SYMBOL_LIST(input_symbol), TENSOR_SYMBOL_LIST(tensor_symbol), 0);
52
5
    } else 
if (3
layer->type == CCV_CONVNET_FULL_CONNECT3
) {
53
3
      ccv_nnc_tensor_symbol_t w_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, CPU_TENSOR_NHWC(32F, layer->net.full_connect.count, layer->input.node.count), 0);
54
3
      w_symbols[i] = w_symbol;
55
3
      ccv_nnc_tensor_symbol_t bias_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, CPU_TENSOR_NHWC(32F, layer->net.full_connect.count), 0);
56
3
      bias_symbols[i] = bias_symbol;
57
3
      ccv_nnc_cmd_t cmd = CMD_GEMM_FORWARD(layer->net.full_connect.count);
58
3
      // If the input is not what I expected (array), reshape it.
59
3
      if (input_info.dim[0] != ccv_nnc_tensor_count(input_info))
60
1
        input_symbol = ccv_nnc_tensor_symbol_alias_new(symbolic_vgg, input_symbol, ccv_nnc_no_ofs, CPU_TENSOR_NHWC(32F, ccv_nnc_tensor_count(input_info)).dim, CPU_TENSOR_NHWC(32F, ccv_nnc_tensor_count(input_info)), 0);
61
3
      exec_symbol = ccv_nnc_graph_exec_symbol_new(symbolic_vgg, cmd, TENSOR_SYMBOL_LIST(input_symbol, w_symbol, bias_symbol), TENSOR_SYMBOL_LIST(tensor_symbol), 0);
62
3
    } else {
63
0
      assert("unreachable");
64
0
    }
65
21
    if (i != 0)
66
20
      ccv_nnc_graph_exec_symbol_concat(symbolic_vgg, previous_exec_symbol, exec_symbol);
67
21
    previous_exec_symbol = exec_symbol;
68
21
    if (i == 0)
69
1
      *source_symbol = exec_symbol;
70
21
    if (i < convnet->count - 1 &&
71
21
      
(20
layer->type == CCV_CONVNET_CONVOLUTIONAL20
||
layer->type == CCV_CONVNET_FULL_CONNECT7
))
72
15
    {
73
15
      // Create the ReLU layer.
74
15
      ccv_nnc_cmd_t cmd = CMD_RELU_FORWARD();
75
15
      ccv_nnc_tensor_symbol_t next_symbol = ccv_nnc_tensor_symbol_new(symbolic_vgg, tensor_info, 0);
76
15
      exec_symbol = ccv_nnc_graph_exec_symbol_new(symbolic_vgg, cmd, TENSOR_SYMBOL_LIST(tensor_symbol), TENSOR_SYMBOL_LIST(next_symbol), 0);
77
15
      ccv_nnc_graph_exec_symbol_concat(symbolic_vgg, previous_exec_symbol, exec_symbol);
78
15
      tensor_symbol = next_symbol;
79
15
      previous_exec_symbol = exec_symbol;
80
15
    }
81
21
    if (i == convnet->count - 1)
82
1
      *dest_symbol = exec_symbol;
83
21
    // This is the input of next layer.
84
21
    input_symbol = tensor_symbol;
85
21
    input_info = tensor_info;
86
21
  }
87
1
  return symbolic_vgg;
88
1
}
89
90
TEST_CASE("run vgg-d graph from its symbolic representation")
91
1
{
92
1
  ccv_convnet_t* convnet = ccv_convnet_read(0, "../../../samples/image-net-2012-vgg-d.sqlite3");
93
1
  ccv_dense_matrix_t* image = 0;
94
1
  ccv_read("../../../samples/dex.png", &image, CCV_IO_ANY_FILE | CCV_IO_RGB_COLOR);
95
1
  ccv_dense_matrix_t* input = 0;
96
1
  ccv_convnet_input_formation(convnet->input, image, &input);
97
1
  ccv_matrix_free(image);
98
1
  ccv_dense_matrix_t* sliced = 0;
99
1
  ccv_slice(input, (ccv_matrix_t**)&sliced, 0, (input->rows - 225) / 2, (input->cols - 225) / 2, 225, 225);
100
1
  ccv_matrix_free(input);
101
1
  ccv_dense_matrix_t* b = 0;
102
1
  ccv_convnet_encode(convnet, &sliced, &b, 1);
103
1
  ccv_nnc_tensor_t* c = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 1000), 0);
104
1
  ccv_nnc_tensor_symbol_t* w_symbols = ccmalloc(sizeof(ccv_nnc_tensor_symbol_t) * convnet->count);
105
1
  ccv_nnc_tensor_symbol_t* bias_symbols = ccmalloc(sizeof(ccv_nnc_tensor_symbol_t) * convnet->count);
106
1
  ccv_nnc_graph_exec_symbol_t source_symbol, dest_symbol;
107
1
  ccv_nnc_tensor_symbol_t input_symbol, output_symbol;
108
1
  ccv_nnc_symbolic_graph_t* graph = ccv_nnc_simple_symbolic_graph(convnet, (ccv_nnc_tensor_t*)sliced, c, &source_symbol, &dest_symbol, &input_symbol, &output_symbol, w_symbols, bias_symbols);
109
1
  ccv_nnc_graph_t* run_graph = 0;
110
1
  ccv_nnc_tensor_arena_t* tensor_arena = 0;
111
1
  ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
112
1
  SYMBOLIC_GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
113
1
  ccv_nnc_symbolic_graph_compile(graph, TENSOR_BIND_MAP(KV(input_symbol, (ccv_nnc_tensor_t*)sliced), KV(output_symbol, c)), 0, 0, GRAPH_EXEC_SYMBOL_LIST(source_symbol), GRAPH_EXEC_SYMBOL_LIST(dest_symbol), &run_graph, &tensor_arena, &graph_exec_arena);
114
1
  GRAPH_GEN(run_graph, CCV_NNC_LONG_DOT_GRAPH);
115
1
  REQUIRE(ccv_nnc_tensor_arena_size(tensor_arena) <= 471513504, "the allocated size should be smaller than what we previously got");
116
1
  int i;
117
22
  for (i = 0; i < convnet->count; 
i++21
)
118
21
  {
119
21
    ccv_convnet_layer_t* layer = convnet->layers + i;
120
21
    if (layer->type == CCV_CONVNET_CONVOLUTIONAL)
121
13
    {
122
13
      ccv_nnc_tensor_t* w = ccv_nnc_tensor_from_symbol(tensor_arena, w_symbols[i]);
123
13
      memcpy(w->data.f32, layer->w, layer->wnum * sizeof(float));
124
13
      ccv_nnc_tensor_t* bias = ccv_nnc_tensor_from_symbol(tensor_arena, bias_symbols[i]);
125
13
      memcpy(bias->data.f32, layer->bias, layer->net.convolutional.count * sizeof(float));
126
13
    } else 
if (8
layer->type == CCV_CONVNET_FULL_CONNECT8
) {
127
3
      ccv_nnc_tensor_t* w = ccv_nnc_tensor_from_symbol(tensor_arena, w_symbols[i]);
128
3
      memcpy(w->data.f32, layer->w, layer->wnum * sizeof(float));
129
3
      ccv_nnc_tensor_t* bias = ccv_nnc_tensor_from_symbol(tensor_arena, bias_symbols[i]);
130
3
      memcpy(bias->data.f32, layer->bias, layer->net.full_connect.count * sizeof(float));
131
3
    }
132
21
  }
133
1
  ccv_nnc_graph_autotune(run_graph, 0, 0, GRAPH_EXEC_LIST(ccv_nnc_graph_exec_from_symbol(graph_exec_arena, source_symbol)), GRAPH_EXEC_LIST(ccv_nnc_graph_exec_from_symbol(graph_exec_arena, dest_symbol)));
134
1
  // Repopulate the weight. Since the weight tensor may be reused, need to repopulate every time run.
135
1
  // To avoid this, bind the weights directly or declare as const.
136
22
  for (i = 0; i < convnet->count; 
i++21
)
137
21
  {
138
21
    ccv_convnet_layer_t* layer = convnet->layers + i;
139
21
    if (layer->type == CCV_CONVNET_CONVOLUTIONAL)
140
13
    {
141
13
      ccv_nnc_tensor_t* w = ccv_nnc_tensor_from_symbol(tensor_arena, w_symbols[i]);
142
13
      memcpy(w->data.f32, layer->w, layer->wnum * sizeof(float));
143
13
      ccv_nnc_tensor_t* bias = ccv_nnc_tensor_from_symbol(tensor_arena, bias_symbols[i]);
144
13
      memcpy(bias->data.f32, layer->bias, layer->net.convolutional.count * sizeof(float));
145
13
    } else 
if (8
layer->type == CCV_CONVNET_FULL_CONNECT8
) {
146
3
      ccv_nnc_tensor_t* w = ccv_nnc_tensor_from_symbol(tensor_arena, w_symbols[i]);
147
3
      memcpy(w->data.f32, layer->w, layer->wnum * sizeof(float));
148
3
      ccv_nnc_tensor_t* bias = ccv_nnc_tensor_from_symbol(tensor_arena, bias_symbols[i]);
149
3
      memcpy(bias->data.f32, layer->bias, layer->net.full_connect.count * sizeof(float));
150
3
    }
151
21
  }
152
1
  ccv_nnc_graph_run(run_graph, 0, 0, 0, GRAPH_EXEC_LIST(ccv_nnc_graph_exec_from_symbol(graph_exec_arena, source_symbol)), GRAPH_EXEC_LIST(ccv_nnc_graph_exec_from_symbol(graph_exec_arena, dest_symbol)));
153
1
  REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, b->data.f32, c->data.f32, 1000, 1e-4, "output should be the same from convnet and from the symbolic graph");
154
1
  ccv_nnc_tensor_free(c);
155
1
  ccv_matrix_free(sliced);
156
1
  ccv_matrix_free(b);
157
1
  ccv_nnc_symbolic_graph_free(graph);
158
1
  ccv_nnc_graph_free(run_graph);
159
1
  ccv_nnc_tensor_arena_free(tensor_arena);
160
1
  ccv_nnc_graph_exec_arena_free(graph_exec_arena);
161
1
  ccfree(w_symbols);
162
1
  ccfree(bias_symbols);
163
1
  ccv_convnet_free(convnet);
164
1
}
165
166
#include "case_main.h"