Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/test/unit/nnc/minimize.tests.c
Line
Count
Source
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 "3rdparty/dsfmt/dSFMT.h"
8
9
TEST_SETUP()
10
{
11
  ccv_nnc_init();
12
}
13
14
TEST_CASE("solve least square sum with stochastic gradient descent on symbolic graph")
15
1
{
16
1
  ccv_nnc_symbolic_graph_t* const symbolic_graph = ccv_nnc_symbolic_graph_new();
17
1
  ccv_nnc_tensor_symbol_t a = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 2, 2), "a");
18
1
  ccv_nnc_tensor_symbol_t w = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 2, 2), "w");
19
1
  ccv_nnc_tensor_symbol_t bias = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 2), "bias");
20
1
  ccv_nnc_tensor_symbol_t b = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 2, 2), "b");
21
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_GEMM_FORWARD(2), TENSOR_SYMBOL_LIST(a, w, bias), TENSOR_SYMBOL_LIST(b), "gemm");
22
1
  ccv_nnc_tensor_symbol_t c = ccv_nnc_tensor_symbol_new(symbolic_graph, CPU_TENSOR_NHWC(32F, 2, 2), "c");
23
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_EWPROD_FORWARD(), TENSOR_SYMBOL_LIST(b, b), TENSOR_SYMBOL_LIST(c), "square");
24
1
  ccv_nnc_tensor_symbol_t s = ccv_nnc_tensor_symbol_new(symbolic_graph, ccv_nnc_tensor_auto, "s");
25
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_REDUCE_SUM_FORWARD(0, 1), TENSOR_SYMBOL_LIST(c), TENSOR_SYMBOL_LIST(s), "sum");
26
1
  ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
27
1
  ccv_nnc_tensor_symbol_t updates[1];
28
1
  ccv_nnc_tensor_symbol_map_t aux[1];
29
1
  ccv_nnc_graph_exec_symbol_t update_execs[1];
30
1
  ccv_nnc_symbolic_graph_minimize(symbolic_graph, CMD_SGD_FORWARD(0, 0.001, 1, 0.995, 0.9, 0.9), TENSOR_SYMBOL_LIST(s), TENSOR_SYMBOL_LIST(w), 0, 0, SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), 0, updates, aux, update_execs);
31
1
  SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
32
1
  ccv_nnc_graph_t* graph;
33
1
  ccv_nnc_tensor_arena_t* tensor_arena;
34
1
  ccv_nnc_graph_exec_arena_t* graph_exec_arena;
35
1
  ccv_nnc_symbolic_graph_compile(symbolic_graph, 0, 0, 0, 0, SYMBOLIC_GRAPH_SOURCES(symbolic_graph), update_execs, 1, &graph, &tensor_arena, &graph_exec_arena);
36
1
  GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
37
1
  // Relies on the inplace ops for SGD set on both updated w / bias, and momentum.
38
1
  ccv_nnc_tensor_t* const w_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, w);
39
1
  ccv_nnc_cmd_exec(CMD_RANDOM_UNIFORM_FORWARD(-0.5, 0.5), ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(w_tensor), 0);
40
1
  ccv_nnc_tensor_t* const bias_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, bias);
41
1
  int i;
42
2
  for (i = 0; i < 1; 
i++1
)
43
1
  {
44
1
    ccv_nnc_tensor_t* const aux_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, aux[i].source);
45
1
    ccv_nnc_cmd_exec(CMD_SET_FORWARD(0), ccv_nnc_no_hint, 0, 0, 0, TENSOR_LIST(aux_tensor), 0);
46
1
  }
47
1
  ccv_nnc_tensor_t* const a_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, a);
48
1
  ccv_nnc_tensor_t* const f_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, ccv_nnc_tensor_symbol_for_backward(symbolic_graph, s));
49
1
  ccv_nnc_graph_exec_t sgd = ccv_nnc_graph_exec_from_symbol(graph_exec_arena, update_execs[0]);
50
1.00k
  for (i = 0; i < 1000; 
i++1.00k
)
51
1.00k
  {
52
1.00k
    a_tensor->data.f32[0] = 10;
53
1.00k
    a_tensor->data.f32[1] = 1;
54
1.00k
    a_tensor->data.f32[2] = 3;
55
1.00k
    a_tensor->data.f32[3] = 5;
56
1.00k
    f_tensor->data.f32[0] = 1;
57
1.00k
    bias_tensor->data.f32[0] = 1;
58
1.00k
    bias_tensor->data.f32[1] = -1;
59
1.00k
    if (i == 750)
60
1
      ccv_nnc_graph_exec_set(graph, sgd, CMD_SGD_FORWARD(0, 0.000001, 1, 0.995, 0.9, 0.9));
61
999
    else if (i == 500)
62
1
      ccv_nnc_graph_exec_set(graph, sgd, CMD_SGD_FORWARD(0, 0.00001, 1, 0.995, 0.9, 0.9));
63
998
    else if (i == 250)
64
1
      ccv_nnc_graph_exec_set(graph, sgd, CMD_SGD_FORWARD(0, 0.0001, 1, 0.995, 0.9, 0.9));
65
1.00k
    ccv_nnc_graph_run(graph, 0, 0, 0, TRAVERSE_FULL);
66
1.00k
  }
67
1
  REQUIRE_EQ_WITH_TOLERANCE(a_tensor->data.f32[0] * w_tensor->data.f32[0] + a_tensor->data.f32[1] * w_tensor->data.f32[1], -1, 1e-3, "converge for vector 1");
68
1
  REQUIRE_EQ_WITH_TOLERANCE(a_tensor->data.f32[0] * w_tensor->data.f32[2] + a_tensor->data.f32[1] * w_tensor->data.f32[3], 1, 1e-3, "converge for vector 1");
69
1
  REQUIRE_EQ_WITH_TOLERANCE(a_tensor->data.f32[2] * w_tensor->data.f32[0] + a_tensor->data.f32[3] * w_tensor->data.f32[1], -1, 1e-1, "converge for vector 2");
70
1
  REQUIRE_EQ_WITH_TOLERANCE(a_tensor->data.f32[2] * w_tensor->data.f32[2] + a_tensor->data.f32[3] * w_tensor->data.f32[3], 1, 1e-1, "converge for vector 2");
71
1
  ccv_nnc_symbolic_graph_free(symbolic_graph);
72
1
  ccv_nnc_graph_free(graph);
73
1
  ccv_nnc_tensor_arena_free(tensor_arena);
74
1
  ccv_nnc_graph_exec_arena_free(graph_exec_arena);
75
1
}
76
77
TEST_CASE("solve least square sum with stochastic gradient descent on dynamic graph")
78
1
{
79
1
  ccv_nnc_dynamic_graph_t* const graph = ccv_nnc_dynamic_graph_new();
80
1
  ccv_nnc_tensor_variable_t w = ccv_nnc_tensor_variable_new(graph, CPU_TENSOR_NHWC(32F, 2, 2));
81
1
  ccv_nnc_tensor_variable_t aux = ccv_nnc_tensor_variable_new(graph, CPU_TENSOR_NHWC(32F, 2, 2));
82
1
  ccv_nnc_dynamic_graph_exec(graph, CMD_SET_FORWARD(0), ccv_nnc_no_hint, 0, 0, 0, TENSOR_VARIABLE_LIST(aux));
83
1
  ccv_nnc_dynamic_graph_exec(graph, CMD_RANDOM_UNIFORM_FORWARD(-0.5, 0.5), ccv_nnc_no_hint, 0, 0, 0, TENSOR_VARIABLE_LIST(w));
84
1
  int i;
85
1.00k
  for (i = 0; i < 1000; 
i++1.00k
)
86
1.00k
  {
87
1.00k
    ccv_nnc_tensor_variable_t a = ccv_nnc_tensor_variable_new(graph, CPU_TENSOR_NHWC(32F, 2, 2));
88
1.00k
    ccv_nnc_tensor_t* const a_tensor = ccv_nnc_tensor_from_variable(graph, a);
89
1.00k
    a_tensor->data.f32[0] = 10;
90
1.00k
    a_tensor->data.f32[1] = 1;
91
1.00k
    a_tensor->data.f32[2] = 3;
92
1.00k
    a_tensor->data.f32[3] = 5;
93
1.00k
    ccv_nnc_tensor_variable_t bias = ccv_nnc_tensor_variable_new(graph, CPU_TENSOR_NHWC(32F, 2));
94
1.00k
    ccv_nnc_tensor_t* const bias_tensor = ccv_nnc_tensor_from_variable(graph, bias);
95
1.00k
    bias_tensor->data.f32[0] = 1;
96
1.00k
    bias_tensor->data.f32[1] = -1;
97
1.00k
    ccv_nnc_tensor_variable_t b = ccv_nnc_tensor_variable_new(graph);
98
1.00k
    ccv_nnc_dynamic_graph_exec(graph, CMD_GEMM_FORWARD(2), ccv_nnc_no_hint, 0, TENSOR_VARIABLE_LIST(a, w, bias), TENSOR_VARIABLE_LIST(b));
99
1.00k
    ccv_nnc_tensor_variable_t c = ccv_nnc_tensor_variable_new(graph);
100
1.00k
    ccv_nnc_dynamic_graph_exec(graph, CMD_EWPROD_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_VARIABLE_LIST(b, b), TENSOR_VARIABLE_LIST(c));
101
1.00k
    ccv_nnc_tensor_variable_t s = ccv_nnc_tensor_variable_new(graph);
102
1.00k
    ccv_nnc_dynamic_graph_exec(graph, CMD_REDUCE_SUM_FORWARD(0, 1), ccv_nnc_no_hint, 0, TENSOR_VARIABLE_LIST(c), TENSOR_VARIABLE_LIST(s));
103
1.00k
    ccv_nnc_dynamic_graph_minimize(graph, CMD_SGD_FORWARD(0, 0.001, 1, 0.995, 0.9, 0.9), TENSOR_VARIABLE_LIST(s), 0, TENSOR_VARIABLE_LIST(w), &aux);
104
1.00k
    ccv_nnc_tensor_variable_free(graph, a);
105
1.00k
    ccv_nnc_tensor_variable_free(graph, b);
106
1.00k
    ccv_nnc_tensor_variable_free(graph, bias);
107
1.00k
    ccv_nnc_tensor_variable_free(graph, c);
108
1.00k
    ccv_nnc_tensor_variable_free(graph, s);
109
1.00k
  }
110
1
  DYNAMIC_GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
111
1
  ccv_nnc_tensor_t* const w_tensor = ccv_nnc_tensor_from_variable(graph, w);
112
1
  REQUIRE_EQ_WITH_TOLERANCE(10 * w_tensor->data.f32[0] + 1 * w_tensor->data.f32[1], -1, 1e-3, "converge for vector 1");
113
1
  REQUIRE_EQ_WITH_TOLERANCE(10 * w_tensor->data.f32[2] + 1 * w_tensor->data.f32[3], 1, 1e-3, "converge for vector 1");
114
1
  REQUIRE_EQ_WITH_TOLERANCE(3 * w_tensor->data.f32[0] + 5 * w_tensor->data.f32[1], -1, 1e-1, "converge for vector 2");
115
1
  REQUIRE_EQ_WITH_TOLERANCE(3 * w_tensor->data.f32[2] + 5 * w_tensor->data.f32[3], 1, 1e-1, "converge for vector 2");
116
1
  ccv_nnc_dynamic_graph_free(graph);
117
1
}
118
119
#include "case_main.h"