Coverage Report

Created: 2021-09-30 20:21

/home/liu/buildslave/linux-x64-runtests/build/test/int/nnc/random.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("random normal distribution")
15
1
{
16
1
  ccv_nnc_symbolic_graph_t* symbolic_graph = ccv_nnc_symbolic_graph_new();
17
1
  const ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 100000), "x");
18
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_RANDOM_NORMAL_FORWARD(1, 0), TENSOR_SYMBOL_LIST(), TENSOR_SYMBOL_LIST(x), "random uniform");
19
1
  ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
20
1
  SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
21
1
  ccv_nnc_graph_t* graph = 0;
22
1
  ccv_nnc_tensor_arena_t* tensor_arena = 0;
23
1
  ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
24
1
  ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, 0, 0, 0, 0, SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena);
25
1
  GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
26
1
  ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0);
27
1
  ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x);
28
1
  ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 100000), 0);
29
1
  ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(xt), 0);
30
1
  int i;
31
1
  double mean = 0;
32
100k
  for (i = 0; i < 100000; 
i++100k
)
33
100k
    mean += xt->data.f32[i];
34
1
  mean = mean / 100000.0;
35
1
  double std = 0;
36
100k
  for (i = 0; i < 100000; 
i++100k
)
37
100k
    std += (xt->data.f32[i] - mean) * (xt->data.f32[i] - mean);
38
1
  std = sqrt(std / 100000.0);
39
1
  REQUIRE_EQ_WITH_TOLERANCE(mean, 0, 1e-2, "mean should be 0");
40
1
  REQUIRE_EQ_WITH_TOLERANCE(std, 1, 1e-2, "std should be 1");
41
1
  ccv_nnc_tensor_free(xt);
42
1
  ccv_nnc_graph_free(graph);
43
1
  ccv_nnc_tensor_arena_free(tensor_arena);
44
1
  ccv_nnc_graph_exec_arena_free(graph_exec_arena);
45
1
  ccv_nnc_symbolic_graph_free(symbolic_graph);
46
1
}
47
48
TEST_CASE("random uniform distribution")
49
1
{
50
1
  ccv_nnc_symbolic_graph_t* symbolic_graph = ccv_nnc_symbolic_graph_new();
51
1
  const ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 32F, 100000), "x");
52
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_RANDOM_UNIFORM_FORWARD(-8, 4), TENSOR_SYMBOL_LIST(), TENSOR_SYMBOL_LIST(x), "random uniform");
53
1
  ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
54
1
  SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
55
1
  ccv_nnc_graph_t* graph = 0;
56
1
  ccv_nnc_tensor_arena_t* tensor_arena = 0;
57
1
  ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
58
1
  ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, 0, 0, 0, 0, SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena);
59
1
  GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
60
1
  ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0);
61
1
  ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x);
62
1
  ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 100000), 0);
63
1
  ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(xt), 0);
64
1
  int i;
65
1
  int h[4 + 8] = {};
66
100k
  for (i = 0; i < 100000; 
i++100k
)
67
100k
  {
68
100k
    REQUIRE(xt->data.f32[i] > -8 - 1e-5, "it must be bigger than lower bound");
69
100k
    REQUIRE(xt->data.f32[i] < 4 + 1e-5, "and smaller than upper bound");
70
100k
    int b = (int)roundf(xt->data.f32[i] - 0.5) + 8;
71
100k
    b = ccv_max(ccv_min(b, 11), 0);
72
100k
    ++h[b];
73
100k
  }
74
1
  const int count = (int)roundf(100000. / (4 + 8));
75
13
  for (i = 0; i < 12; 
i++12
)
76
12
    { REQUIRE(h[i] >= count - 1000 && h[i] <= count + 1000, "uniform distribution"); }
77
1
  ccv_nnc_tensor_free(xt);
78
1
  ccv_nnc_graph_free(graph);
79
1
  ccv_nnc_tensor_arena_free(tensor_arena);
80
1
  ccv_nnc_graph_exec_arena_free(graph_exec_arena);
81
1
  ccv_nnc_symbolic_graph_free(symbolic_graph);
82
1
}
83
84
TEST_CASE("random uniform distribution in half precision")
85
1
{
86
1
  ccv_nnc_symbolic_graph_t* symbolic_graph = ccv_nnc_symbolic_graph_new();
87
1
  const ccv_nnc_tensor_symbol_t x = ccv_nnc_tensor_symbol_new(symbolic_graph, GPU_TENSOR_NHWC(000, 16F, 100000), "x");
88
1
  ccv_nnc_graph_exec_symbol_new(symbolic_graph, CMD_RANDOM_UNIFORM_FORWARD(-8, 4), TENSOR_SYMBOL_LIST(), TENSOR_SYMBOL_LIST(x), "random uniform");
89
1
  ccv_nnc_graph_exec_symbol_autogen(symbolic_graph, 0, 0, CCV_NNC_AUTOGEN_ALL_EXECS | CCV_NNC_AUTOGEN_SOURCES_AND_DESTINATIONS);
90
1
  SYMBOLIC_GRAPH_GEN(symbolic_graph, CCV_NNC_LONG_DOT_GRAPH);
91
1
  ccv_nnc_graph_t* graph = 0;
92
1
  ccv_nnc_tensor_arena_t* tensor_arena = 0;
93
1
  ccv_nnc_graph_exec_arena_t* graph_exec_arena = 0;
94
1
  ccv_nnc_symbolic_graph_compile(symbolic_graph, ccv_nnc_default_compile_params, 0, 0, 0, 0, SYMBOLIC_GRAPH_SOURCES(symbolic_graph), SYMBOLIC_GRAPH_DESTINATIONS(symbolic_graph), &graph, &tensor_arena, &graph_exec_arena);
95
1
  GRAPH_GEN(graph, CCV_NNC_LONG_DOT_GRAPH);
96
1
  ccv_nnc_graph_run(graph, 0, TRAVERSE_FULL, 0, 0);
97
1
  ccv_nnc_tensor_t* const x_tensor = ccv_nnc_tensor_from_symbol(tensor_arena, x);
98
1
  ccv_nnc_tensor_t* const x16t = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(16F, 100000), 0);
99
1
  ccv_nnc_tensor_t* const xt = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 100000), 0);
100
1
  ccv_nnc_cmd_exec(CMD_DATA_TRANSFER_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x_tensor), TENSOR_LIST(x16t), 0);
101
1
  ccv_nnc_cmd_exec(CMD_DATATYPE_CONVERSION_FORWARD(), ccv_nnc_no_hint, 0, TENSOR_LIST(x16t), TENSOR_LIST(xt), 0);
102
1
  int i;
103
1
  int h[4 + 8] = {};
104
100k
  for (i = 0; i < 100000; 
i++100k
)
105
100k
  {
106
100k
    REQUIRE(xt->data.f32[i] > -8 - 1e-5, "it must be bigger than lower bound");
107
100k
    REQUIRE(xt->data.f32[i] < 4 + 1e-5, "and smaller than upper bound");
108
100k
    int b = (int)roundf(xt->data.f32[i] - 0.5) + 8;
109
100k
    b = ccv_max(ccv_min(b, 11), 0);
110
100k
    ++h[b];
111
100k
  }
112
1
  const int count = (int)roundf(100000. / (4 + 8));
113
13
  for (i = 0; i < 12; 
i++12
)
114
12
    { REQUIRE(h[i] >= count - 1000 && h[i] <= count + 1000, "uniform distribution"); }
115
1
  ccv_nnc_tensor_free(xt);
116
1
  ccv_nnc_tensor_free(x16t);
117
1
  ccv_nnc_graph_free(graph);
118
1
  ccv_nnc_tensor_arena_free(tensor_arena);
119
1
  ccv_nnc_graph_exec_arena_free(graph_exec_arena);
120
1
  ccv_nnc_symbolic_graph_free(symbolic_graph);
121
1
}
122
123
#include "case_main.h"