Coverage Report

Created: 2024-08-19 11:27

/home/liu/actions-runner/_work/ccv/ccv/lib/nnc/cmd/sigmoid_loss/ccv_nnc_sigmoid_binary_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_sigmoid_binary_crossentropy_forw_bitmask(const ccv_nnc_cmd_param_t cmd, 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
0
{
8
  // input: activation, label
9
  // output: [loss], sigmoid
10
0
  if ((input_bitmasks[0] & 3u) == 3u && output_bitmasks[0] == 3u)
11
0
    return 1;
12
0
  if ((input_bitmasks[0] & 3u) == 3u && output_bitmasks[0] == 2u)
13
0
    return 1;
14
0
  return 0;
15
0
}
16
17
static int _ccv_nnc_sigmoid_binary_crossentropy_allow_inplace_forw(const ccv_nnc_cmd_param_t cmd, const int input_idx, const int input_size, const int output_idx, const int output_size)
18
0
{
19
0
  return (input_idx == 0 && output_idx == 1);
20
0
}
21
22
static int _ccv_nnc_sigmoid_binary_crossentropy_back_bitmask(const ccv_nnc_cmd_param_t cmd, 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
66
{
24
  // input: [gradient of loss], [gradient of sigmoid], [activation], label, [loss], sigmoid
25
  // output: w.r.t activation, [label]
26
66
  if ((input_bitmasks[0] & 40u) == 40u && 
(output_bitmasks[0] & 1u) == 1u34
)
27
34
    return 1;
28
32
  return 0;
29
66
}
30
31
static int _ccv_nnc_sigmoid_binary_crossentropy_allow_inplace_back(const ccv_nnc_cmd_param_t cmd, const int input_idx, const int input_size, const int output_idx, const int output_size)
32
8
{
33
8
  if (input_idx == 1 && 
output_idx == 00
)
34
0
    return 1;
35
8
  if (input_idx == 2 && 
output_idx == 00
)
36
0
    return 1;
37
8
  else if (input_idx == 5 && 
output_idx == 04
)
38
2
    return 1;
39
6
  return 0;
40
8
}
41
42
static void _ccv_nnc_sigmoid_binary_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
18
{
44
18
  assert(input_size == 2);
45
18
  assert(output_size == 2);
46
18
  outputs[0] = inputs[0];
47
  // The output should have the same dimentionality of the label data.
48
18
  memcpy(outputs[0].dim, inputs[1].dim, sizeof(outputs[0].dim));
49
18
  const int nd = ccv_nnc_tensor_nd(outputs[0].dim);
50
  // Set channel to 1 if it is not..
51
18
  if (nd > 1 && ccv_nnc_tensor_get_c(outputs[0]) > 1)
52
6
    ccv_nnc_tensor_set_c(&outputs[0], nd, 1);
53
18
  outputs[1] = inputs[0];
54
18
}
55
56
static void _ccv_nnc_sigmoid_binary_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)
57
8
{
58
8
  assert(input_size >= 6);
59
8
  assert(output_size >= 1);
60
8
  outputs[0] = inputs[5];
61
8
  if (output_size > 1)
62
8
    outputs[1] = inputs[3];
63
8
}
64
65
REGISTER_COMMAND(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_FORWARD)(ccv_nnc_cmd_registry_t* const registry)
66
  FIND_BACKEND(ccv_nnc_sigmoid_binary_crossentropy_cpu_ref.c, gpu/ccv_nnc_sigmoid_binary_crossentropy_gpu_ref.cu)
67
1
{
68
1
  registry->bitmask = _ccv_nnc_sigmoid_binary_crossentropy_forw_bitmask;
69
1
  registry->tensor_auto = _ccv_nnc_sigmoid_binary_crossentropy_tensor_auto_forw;
70
1
  registry->allow_inplace = _ccv_nnc_sigmoid_binary_crossentropy_allow_inplace_forw;
71
1
}
72
73
REGISTER_COMMAND(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_BACKWARD)(ccv_nnc_cmd_registry_t* const registry)
74
  FIND_BACKEND(ccv_nnc_sigmoid_binary_crossentropy_cpu_ref.c, gpu/ccv_nnc_sigmoid_binary_crossentropy_gpu_ref.cu)
75
1
{
76
1
  registry->flags = CCV_NNC_CMD_ATTR_NULL_IS_ONES;
77
1
  registry->bitmask = _ccv_nnc_sigmoid_binary_crossentropy_back_bitmask;
78
1
  registry->tensor_auto = _ccv_nnc_sigmoid_binary_crossentropy_tensor_auto_back;
79
1
  registry->allow_inplace = _ccv_nnc_sigmoid_binary_crossentropy_allow_inplace_back;
80
1
}
81
82
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_FORWARD)
83
#define CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_0() ccv_nnc_cmd(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=1}}), 0)
84
#define CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 1 parameters for CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD")
85
#define CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_1(_pos_weight) ccv_nnc_cmd(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_FORWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=_pos_weight}}), 0)
86
#define CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_SEL(_0, _1, _FX, ...) _FX
87
#define CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD(...) CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_SEL(CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_F, ##__VA_ARGS__, CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_1, CMD_SIGMOID_BINARY_CROSSENTROPY_FORWARD_X_0)(__VA_ARGS__)
88
//@REGISTER_EASY_COMMAND_MACRO(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_BACKWARD)
89
#define CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_0() ccv_nnc_cmd(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=1}}), 0)
90
#define CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_F(...) ("This should not be used, you should have either 0 parameter or 2 parameters for CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD")
91
#define CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_1(_pos_weight) ccv_nnc_cmd(CCV_NNC_SIGMOID_BINARY_CROSSENTROPY_BACKWARD, 0, ((ccv_nnc_cmd_param_t){.size={.dim={1,1,1}},.binary_crossentropy={.pos_weight=_pos_weight}}), 0)
92
#define CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_SEL(_0, _1, _FX, ...) _FX
93
#define CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD(...) CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_SEL(CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_F, ##__VA_ARGS__, CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_1, CMD_SIGMOID_BINARY_CROSSENTROPY_BACKWARD_X_0)(__VA_ARGS__)