Coverage Report

Created: 2021-04-14 04:30

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/cmd/softmax_loss/ccv_nnc_softmax_crossentropy.c
Line
Count
Source (jump to first uncovered line)
1
#include "ccv.h"
2
#include "nnc/ccv_nnc.h"
3
#include "nnc/ccv_nnc_easy.h"
4
#include "nnc/ccv_nnc_internal.h"
5
6
static int _ccv_nnc_softmax_crossentropy_forw_bitmask(const int input_size, const int output_size, const uint64_t* const input_bitmasks, const int input_bitmask_size, const uint64_t* const output_bitmasks, const int output_bitmask_size)
7
6
{
8
6
  // input: activation, label
9
6
  // output: [loss], softmax
10
6
  if ((input_bitmasks[0] & 3u) == 3u && 
output_bitmasks[0] == 3u2
)
11
1
    return 1;
12
5
  if ((input_bitmasks[0] & 3u) == 3u && 
output_bitmasks[0] == 2u1
)
13
1
    return 1;
14
4
  return 0;
15
4
}
16
17
static int _ccv_nnc_softmax_crossentropy_allow_inplace_forw(const int input_idx, const int input_size, const int output_idx, const int output_size)
18
58
{
19
58
  return (input_idx == 0 && 
output_idx == 136
);
20
58
}
21
22
static int _ccv_nnc_softmax_crossentropy_back_bitmask(const int input_size, const int output_size, const uint64_t* const input_bitmasks, const int input_bitmask_size, const uint64_t* const output_bitmasks, const int output_bitmask_size)
23
1.69k
{
24
1.69k
  // input: [gradient of loss], [gradient of softmax], [activation], label, [loss], softmax
25
1.69k
  // output: w.r.t activation, [label]
26
1.69k
  if ((input_bitmasks[0] & 40u) == 40u && 
(output_bitmasks[0] & 1u) == 1u855
)
27
855
    return 1;
28
844
  return 0;
29
844
}
30
31
static int _ccv_nnc_softmax_crossentropy_allow_inplace_back(const int input_idx, const int input_size, const int output_idx, const int output_size)
32
66
{
33
66
  if (input_idx == 1 && 
output_idx == 00
)
34
0
    return 1;
35
66
  if (input_idx == 2 && 
output_idx == 00
)
36
0
    return 1;
37
66
  else if (input_idx == 5 && 
output_idx == 018
)
38
9
    return 1;
39
57
  return 0;
40
57
}
41
42
static void _ccv_nnc_softmax_crossentropy_tensor_auto_forw(const ccv_nnc_cmd_param_t cmd, const ccv_nnc_tensor_param_t* const inputs, const int input_size, const ccv_nnc_hint_t hint, ccv_nnc_tensor_param_t* const outputs, const int output_size)
43
440
{
44
440
  assert(input_size == 2);
45
440
  assert(output_size >= 1);
46
440
  outputs[0] = inputs[0];
47
440
  // The output should have the same dimentionality of the label data.
48
440
  memcpy(outputs[0].dim, inputs[1].dim, sizeof(outputs[0].dim));
49
440
  const int nd = ccv_nnc_tensor_nd(outputs[0].dim);
50
440
  // Set channel to 1 if it is not..
51
440
  if (nd > 1 && 
ccv_nnc_tensor_get_c(outputs[0]) > 1420
)
52
417
    ccv_nnc_tensor_set_c(&outputs[0], nd, 1);
53
440
  if (output_size > 1)
54
440
    outputs[1] = inputs[0];
55
440
}
56
57
static void _ccv_nnc_softmax_crossentropy_tensor_auto_back(const ccv_nnc_cmd_param_t cmd, const ccv_nnc_tensor_param_t* const inputs, const int input_size, const ccv_nnc_hint_t hint, ccv_nnc_tensor_param_t* const outputs, const int output_size)
58
223
{
59
223
  assert(input_size >= 6);
60
223
  assert(output_size >= 1);
61
223
  outputs[0] = inputs[5];
62
223
  if (output_size > 1)
63
223
    outputs[1] = inputs[3];
64
223
}
65
66
REGISTER_COMMAND(CCV_NNC_SOFTMAX_CROSSENTROPY_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
67
  FIND_BACKEND(ccv_nnc_softmax_crossentropy_cpu_ref.c, gpu/ccv_nnc_softmax_crossentropy_gpu_cudnn.cu)
68
1
{
69
1
  registry->bitmask = _ccv_nnc_softmax_crossentropy_forw_bitmask;
70
1
  registry->tensor_auto = _ccv_nnc_softmax_crossentropy_tensor_auto_forw;
71
1
  registry->allow_inplace = _ccv_nnc_softmax_crossentropy_allow_inplace_forw;
72
1
}
73
74
REGISTER_COMMAND(CCV_NNC_SOFTMAX_CROSSENTROPY_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
75
  FIND_BACKEND(ccv_nnc_softmax_crossentropy_cpu_ref.c, gpu/ccv_nnc_softmax_crossentropy_gpu_cudnn.cu)
76
1
{
77
1
  registry->flags = CCV_NNC_CMD_ATTR_NULL_IS_ONES;
78
1
  registry->bitmask = _ccv_nnc_softmax_crossentropy_back_bitmask;
79
1
  registry->tensor_auto = _ccv_nnc_softmax_crossentropy_tensor_auto_back;
80
1
  registry->allow_inplace = _ccv_nnc_softmax_crossentropy_allow_inplace_back;
81
1
}
82
83
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SOFTMAX_CROSSENTROPY_FORWARD)
84
#define CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_0() ccv_nnc_cmd(CCV_NNC_SOFTMAX_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=0,.trim1=1}}), 0)
85
#define CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 2 parameters for CMD_SOFTMAX_CROSSENTROPY_FORWARD")
86
#define CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_2(_trim0, _trim1) ccv_nnc_cmd(CCV_NNC_SOFTMAX_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=_trim0,.trim1=_trim1}}), 0)
87
#define CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_SEL(_0, _1, _2, _FX, ...) _FX
88
#define CMD_SOFTMAX_CROSSENTROPY_FORWARD(...) CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_SEL(CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_F, ##__VA_ARGS__, CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_2, CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_F, CMD_SOFTMAX_CROSSENTROPY_FORWARD_X_0)(__VA_ARGS__)
89
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SOFTMAX_CROSSENTROPY_BACKWARD)
90
#define CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_0() ccv_nnc_cmd(CCV_NNC_SOFTMAX_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=0,.trim1=1}}), 0)
91
#define CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 2 parameters for CMD_SOFTMAX_CROSSENTROPY_BACKWARD")
92
#define CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_2(_trim0, _trim1) ccv_nnc_cmd(CCV_NNC_SOFTMAX_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.label_smoothing={.trim0=_trim0,.trim1=_trim1}}), 0)
93
#define CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_SEL(_0, _1, _2, _FX, ...) _FX
94
#define CMD_SOFTMAX_CROSSENTROPY_BACKWARD(...) CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_SEL(CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_F, ##__VA_ARGS__, CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_2, CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_F, CMD_SOFTMAX_CROSSENTROPY_BACKWARD_X_0)(__VA_ARGS__)