Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/test/unit/nnc/tensor.bind.tests.c
Line
Count
Source
1
2
#include "case.h"
3
#include "ccv_case.h"
4
#include "ccv_nnc_case.h"
5
#include <ccv.h>
6
#include <nnc/ccv_nnc.h>
7
#include <nnc/ccv_nnc_easy.h>
8
#include "3rdparty/dsfmt/dSFMT.h"
9
10
TEST_SETUP()
11
{
12
  ccv_nnc_init();
13
}
14
15
static int while_4(ccv_nnc_tensor_t* const* const inputs, const int input_size, const void* const data)
16
5
{
17
5
  return inputs[0]->data.i64[0] < 4;
18
5
}
19
20
TEST_CASE("while z = a * x + b (x <- z) compiled a and b binded to a tensor")
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, CPU_TENSOR_NHWC(32F, 1), "x");
24
1
  ccv_nnc_symbolic_graph_t* while_graph = ccv_nnc_symbolic_graph_new();
25
1
  ccv_nnc_symbolic_graph_while(symbolic_graph, CCV_NNC_GRAPH_FORWARD, while_graph, "while");
26
1
  ccv_nnc_tensor_symbol_t a = ccv_nnc_tensor_symbol_new(while_graph, CPU_TENSOR_NHWC(32F, 1), "a");
27
1
  ccv_nnc_tensor_symbol_t b = ccv_nnc_tensor_symbol_new(while_graph, CPU_TENSOR_NHWC(32F, 1), "b");
28
1
  ccv_nnc_tensor_symbol_t y = ccv_nnc_tensor_symbol_new(while_graph, CPU_TENSOR_NHWC(32F, 1), "y");
29
1
  ccv_nnc_tensor_symbol_t z = ccv_nnc_tensor_symbol_new(while_graph, CPU_TENSOR_NHWC(32F, 1), "z");
30
1
  ccv_nnc_graph_exec_symbol_new(while_graph, CMD_EWPROD_FORWARD(), TENSOR_SYMBOL_LIST(a, x), TENSOR_SYMBOL_LIST(y), "prod");
31
1
  ccv_nnc_graph_exec_symbol_t sum = ccv_nnc_graph_exec_symbol_new(while_graph, CMD_EWSUM_FORWARD(), TENSOR_SYMBOL_LIST(y, b), TENSOR_SYMBOL_LIST(z), "sum");
32
1
  ccv_nnc_graph_exec_symbol_t noop = ccv_nnc_graph_exec_symbol_new(while_graph, CMD_NOOP(), 0, 0, 0, 0, "noop");
33
1
  ccv_nnc_graph_exec_symbol_concat(while_graph, sum, noop);
34
1
  ccv_nnc_graph_exec_symbol_autogen(while_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
35
1
  ccv_nnc_symbolic_graph_set_while_expr(while_graph, while_4, 0, TENSOR_SYMBOL_LIST(ccv_nnc_tensor_symbol_for_while_count(while_graph)), GRAPH_EXEC_SYMBOL_LIST(noop));
36
1
  ccv_nnc_symbolic_graph_set_carry_overs(while_graph, TENSOR_SYMBOL_MAP(KV(z, x)));
37
1
  ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
38
1
  SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
39
1
  ccv_nnc_graph_t* graph = 0;
40
1
  ccv_nnc_tensor_arena_t* tensor_arena = 0;
41
1
  ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
42
1
  ccv_nnc_tensor_t* a_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 1), 0);
43
1
  a_tensor->data.f32[0] = 0.3;
44
1
  ccv_nnc_tensor_t* b_tensor = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 1), 0);
45
1
  b_tensor->data.f32[0] = 1.1;
46
1
  ccv_nnc_symbolic_graph_compile(symbolic_graph,
47
1
    TENSOR_BIND_MAP(KV(a, a_tensor), KV(b, b_tensor)), // Binding the tensors.
48
1
    0, 0,
49
1
    SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph),
50
1
    &graph, &tensor_arena, &graph_exec_arena);
51
1
  GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
52
1
  ccv_nnc_tensor_t* x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x);
53
1
  x_tensor->data.f32[0] = 0.88;
54
1
  ccv_nnc_tensor_t* z_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, z);
55
1
  ccv_nnc_graph_exec_t source = ccv_nnc_graph_exec_source(graph_exec_arena);
56
1
  ccv_nnc_graph_exec_t destination = ccv_nnc_graph_exec_destination(graph_exec_arena);
57
1
  ccv_nnc_graph_run(graph, 0, 0, 0, &source, 1, &destination, 1);
58
1
  int i;
59
1
  float z_val = 0.88;
60
6
  for (i = 0; i < 5; 
i++5
)
61
5
    z_val = 0.3 * z_val + 1.1;
62
1
  REQUIRE_EQ_WITH_TOLERANCE(z_tensor->data.f32[0], z_val, 1e-6, "z should be equal to a * x + b (5)");
63
1
  ccv_nnc_tensor_free(a_tensor);
64
1
  ccv_nnc_tensor_free(b_tensor);
65
1
  ccv_nnc_symbolic_graph_free(symbolic_graph);
66
1
  ccv_nnc_graph_exec_arena_free(graph_exec_arena);
67
1
  ccv_nnc_tensor_arena_free(tensor_arena);
68
1
  ccv_nnc_graph_free(graph);
69
1
}
70
71
#include "case_main.h"