Coverage Report

Created: 2017-11-12 13:27

/home/liu/buildslave/linux-x64-runtests/build/test/unit/nnc/tape.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 <nnc/_ccv_nnc_graph.h>
8
#include <3rdparty/dsfmt/dSFMT.h>
9
10
TEST_SETUP()
11
{
12
  ccv_nnc_init();
13
}
14
15
static int while_5(ccv_nnc_tensor_t* const* const commons, const int common_size, ccv_nnc_tensor_t* const* const inputs, const int input_size, ccv_nnc_tensor_t* const* const outputs, const int output_size, const void* const data)
16
0
{
17
0
  return commons[0]->data.i64[0] < 5;
18
0
}
19
20
TEST_CASE("new tape from a graph")
21
1
{
22
1
  ccv_nnc_symbolic_graph_t* symbolic_graph = ccv_nnc_symbolic_graph_new();
23
1
  ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, ONE_CPU_TENSOR(1), "x");
24
1
  ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(symbolic_graph, ONE_CPU_TENSOR(1), "y");
25
1
  ccv_nnc_tensor_symbol_t z0 = ccv_nnc_tensor_symbol_new(symbolic_graph, ONE_CPU_TENSOR(1), "z0");
26
1
  ccv_nnc_symbolic_graph_t* while_symbolic_graph = ccv_nnc_symbolic_graph_new();
27
1
  ccv_nnc_graph_exec_symbol_t while_symbol = ccv_nnc_symbolic_graph_while(symbolic_graph, while_symbolic_graph, "for 1..5");
28
1
  ccv_nnc_tensor_symbol_t z1 = ccv_nnc_tensor_symbol_new(while_symbolic_graph, ONE_CPU_TENSOR(1), "z1");
29
1
  ccv_nnc_graph_exec_symbol_t noop = ccv_nnc_graph_exec_symbol_new(while_symbolic_graph, ccv_nnc_cmd(CCV_NNC_NOOP, 0, CMD_GENERIC(), 0), 0, 0, 0, 0, "noop");
30
1
  ccv_nnc_graph_exec_symbol_t prod0 = ccv_nnc_graph_exec_symbol_new(while_symbolic_graph, ccv_nnc_cmd(CCV_NNC_EWPROD_FORWARD, 0, 
CMD_GENERIC1
(), 0),
TENSOR_SYMBOL_LIST1
(z0, x),
TENSOR_SYMBOL_LIST1
(z1), "prod0");
31
1
  ccv_nnc_graph_exec_symbol_concat(while_symbolic_graph, noop, prod0);
32
1
  ccv_nnc_tensor_symbol_t z2 = ccv_nnc_tensor_symbol_new(symbolic_graph, ONE_CPU_TENSOR(1), "z2");
33
1
  ccv_nnc_tensor_symbol_t z3 = ccv_nnc_tensor_symbol_new(symbolic_graph, ONE_CPU_TENSOR(1), "z3");
34
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, ccv_nnc_cmd(CCV_NNC_EWPROD_FORWARD, 0, 
CMD_GENERIC1
(), 0),
TENSOR_SYMBOL_LIST1
(z1, y),
TENSOR_SYMBOL_LIST1
(z2), "prod1");
35
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, ccv_nnc_cmd(CCV_NNC_EWSUM_FORWARD, 0, 
CMD_GENERIC1
(), 0),
TENSOR_SYMBOL_LIST1
(z2, z0),
TENSOR_SYMBOL_LIST1
(z3), "sum");
36
1
  ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
37
1
  ccv_nnc_symbolic_graph_set_while_expr(while_symbolic_graph, while_5, 0, GRAPH_EXEC_SYMBOL_LIST(noop));
38
1
  ccv_nnc_symbolic_graph_set_while_params(while_symbolic_graph, TENSOR_SYMBOL_MAP(KV(z1, z0)));
39
1
  ccv_nnc_symbolic_graph_set_sources(while_symbolic_graph, GRAPH_EXEC_SYMBOL_LIST(noop));
40
1
  ccv_nnc_symbolic_graph_set_destinations(while_symbolic_graph, GRAPH_EXEC_SYMBOL_LIST(prod0));
41
1
  ccv_nnc_graph_t* graph = 0;
42
1
  ccv_nnc_tensor_arena_t* tensor_arena = 0;
43
1
  ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
44
1
  ccv_nnc_symbolic_graph_compile(symbolic_graph, 0, 0, ccv_nnc_symbolic_graph_sources(symbolic_graph), ccv_nnc_symbolic_graph_source_size(symbolic_graph), ccv_nnc_symbolic_graph_destinations(symbolic_graph), ccv_nnc_symbolic_graph_destination_size(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena);
45
1
  SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
46
1
  GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
47
1
  ccv_nnc_symbolic_graph_free(symbolic_graph);
48
1
  ccv_nnc_graph_exec_t while_exec = ccv_nnc_graph_exec_from_symbol(graph_exec_arena, while_symbol);
49
1
  ccv_nnc_graph_t* while_graph = ccv_nnc_graph_from_graph_exec(graph, while_exec);
50
1
  ccv_nnc_tensor_tape_t* tape = ccv_nnc_tensor_tape_new();
51
1
  ccv_nnc_tensor_t* tensor = ccv_nnc_tensor_new(0, ONE_CPU_TENSOR(1), 0);
52
1
  ccv_numeric_data_t data = tensor->data; // Preserve the data field.
53
1
  tensor->type |= CCV_TAPE_ALLOC;
54
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(),
TENSOR_LIST1
(tensor));
55
1
  
REQUIRE1
(tensor->data.u8 != data.u8, "Tensor should assigned a different memory region with (0, 0).")
;1
56
1
  tensor->data.f32[0] = 0.32;
57
1
  while_graph->while_count = 2;
58
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(),
TENSOR_LIST1
(tensor));
59
1
  
REQUIRE1
(tensor->data.u8 != data.u8, "Tensor should assigned a different memory region with (0, 2).")
;1
60
1
  tensor->data.f32[0] = 0.11;
61
1
  graph->while_count = 1;
62
1
  while_graph->while_count = 1;
63
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(),
TENSOR_LIST1
(tensor));
64
1
  
REQUIRE1
(tensor->data.u8 != data.u8, "Tensor should assigned a different memory region with (1, 1).")
;1
65
1
  tensor->data.f32[0] = 0.58;
66
1
  while_graph->while_count = 2;
67
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(),
TENSOR_LIST1
(tensor));
68
1
  
REQUIRE1
(tensor->data.u8 != data.u8, "Tensor should assigned a different memory region with (1, 2).")
;1
69
1
  tensor->data.f32[0] = 0.18;
70
1
  graph->while_count = 2;
71
1
  while_graph->while_count = 3;
72
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(),
TENSOR_LIST1
(tensor));
73
1
  
REQUIRE1
(tensor->data.u8 != data.u8, "Tensor should assigned a different memory region with (2, 3).")
;1
74
1
  tensor->data.f32[0] = 1.29;
75
1
  while_graph->while_count = 2;
76
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(),
TENSOR_LIST1
(tensor));
77
1
  
REQUIRE1
(tensor->data.u8 != data.u8, "Tensor should assigned a different memory region with (2, 2).")
;1
78
1
  tensor->data.f32[0] = 0.02;
79
1
  graph->while_count = 0;
80
1
  while_graph->while_count = 2;
81
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(tensor),
TENSOR_LIST1
());
82
1
  
REQUIRE_EQ_WITH_TOLERANCE1
(tensor->data.f32[0], 0.11, 1e-5, "Tensor should retain 0.11 with (0, 2).")
;1
83
1
  graph->while_count = 1;
84
1
  while_graph->while_count = 1;
85
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(tensor),
TENSOR_LIST1
());
86
1
  
REQUIRE_EQ_WITH_TOLERANCE1
(tensor->data.f32[0], 0.58, 1e-5, "Tensor should retain 0.58 with (1, 1).")
;1
87
1
  graph->while_count = 0;
88
1
  while_graph->while_count = 0;
89
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(tensor),
TENSOR_LIST1
());
90
1
  
REQUIRE_EQ_WITH_TOLERANCE1
(tensor->data.f32[0], 0.32, 1e-5, "Tensor should retain 0.32 with (0, 0).")
;1
91
1
  graph->while_count = 2;
92
1
  while_graph->while_count = 3;
93
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(tensor),
TENSOR_LIST1
());
94
1
  
REQUIRE_EQ_WITH_TOLERANCE1
(tensor->data.f32[0], 1.29, 1e-5, "Tensor should retain 1.29 with (2, 3).")
;1
95
1
  graph->while_count = 2;
96
1
  while_graph->while_count = 2;
97
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(tensor),
TENSOR_LIST1
());
98
1
  
REQUIRE_EQ_WITH_TOLERANCE1
(tensor->data.f32[0], 0.02, 1e-5, "Tensor should retain 0.02 with (2, 2).")
;1
99
1
  graph->while_count = 1;
100
1
  while_graph->while_count = 2;
101
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(tensor),
TENSOR_LIST1
());
102
1
  
REQUIRE_EQ_WITH_TOLERANCE1
(tensor->data.f32[0], 0.18, 1e-5, "Tensor should retain 0.02 with (1, 2).")
;1
103
1
  // In next loop, try to access data (it will return the data from previous loop).
104
1
  graph->while_count = 2;
105
1
  while_graph->while_count = 4;
106
1
  ccv_nnc_tensor_tape_io(tape, while_graph, 
TENSOR_LIST1
(tensor),
TENSOR_LIST1
());
107
1
  
REQUIRE_EQ_WITH_TOLERANCE1
(tensor->data.f32[0], 1.29, 1e-5, "Tensor should retain 1.29 with (2, 4).")
;1
108
1
  tensor->data = data;
109
1
  ccv_nnc_tensor_free(tensor);
110
1
  ccv_nnc_tensor_tape_free(tape);
111
1
  ccv_nnc_tensor_arena_free(tensor_arena);
112
1
  ccv_nnc_graph_exec_arena_free(graph_exec_arena);
113
1
  ccv_nnc_graph_free(graph);
114
1
}
115
116
#include "case_main.h"