Coverage Report

Created: 2024-08-18 16:21

/home/liu/actions-runner/_work/ccv/ccv/lib/nnc/ccv_nnc_tfb.h
Line
Count
Source
1
/**********************************************************
2
 * C-based/Cached/Core Computer Vision Library
3
 * Liu Liu, 2010-02-01
4
 **********************************************************/
5
6
/**********************************************************
7
 * CCV - Neural Network Collection
8
 **********************************************************/
9
10
/**
11
 * This header is included into main ccv.h
12
 * such that enables toll-free bridging between
13
 * ccv_nnc_tensor_t and ccv_dense_matrix_t
14
 * In effect, ccv_dense_matrix_t will be a specialized
15
 * version of ccv_nnc_tensor_t. We are taking some penalties
16
 * from this change though, namely, the size of ccv_dense_matrix_t
17
 * will be bigger now.
18
 */
19
20
#ifndef GUARD_ccv_nnc_tfb_h
21
#define GUARD_ccv_nnc_tfb_h
22
23
#include <unistd.h>
24
#include <stdint.h>
25
26
enum {
27
  CCV_TENSOR_FORMAT_NCHW = 0x01,
28
  CCV_TENSOR_FORMAT_NHWC = 0x02,
29
  CCV_TENSOR_FORMAT_CHWN = 0x04,
30
};
31
32
enum {
33
  CCV_TENSOR_CPU_MEMORY = 0x1,
34
  CCV_TENSOR_GPU_MEMORY = 0x2,
35
};
36
37
enum {
38
  CCV_COMPUTE_DEVICE_000 = 0x00000,
39
  CCV_COMPUTE_DEVICE_001 = 0x00100,
40
  CCV_COMPUTE_DEVICE_002 = 0x00200,
41
  CCV_COMPUTE_DEVICE_003 = 0x00300,
42
  CCV_COMPUTE_DEVICE_004 = 0x00400,
43
  CCV_COMPUTE_DEVICE_005 = 0x00500,
44
  CCV_COMPUTE_DEVICE_006 = 0x00600,
45
  CCV_COMPUTE_DEVICE_007 = 0x00700,
46
  CCV_COMPUTE_DEVICE_ANY = 0xfff00, // The optimal allocation will be found by the algorithm.
47
};
48
49
1.81M
#define CCV_TENSOR_GET_MEMORY(type) ((type) & 0x3)
50
487k
#define CCV_TENSOR_GET_DEVICE(type) ((
type308
) & 0xfff00)
51
122k
#define CCV_TENSOR_GET_DEVICE_ID(type) (CCV_TENSOR_GET_DEVICE(type) >> 8)
52
31.6k
#define CCV_TENSOR_SET_DEVICE_ID(type, device_id) (type) = (((type) & ~0xfff00) | (((device_id) & 0xfff) << 8))
53
54
enum {
55
  CCV_TENSOR_VIEW       = 0x01000000,
56
  CCV_TENSOR_MULTIVIEW  = 0x02000000,
57
  CCV_TENSOR_PINNED_MEM = 0x04000000, // tensor is pinned in CUDA. This matches CCV_PINNED_MEM.
58
};
59
60
typedef struct {
61
  short v;
62
} ccv_float16_t; // In this way, you cannot perform any operations on float16.
63
64
typedef union ccv_numeric_data_u {
65
  char* i8;
66
  unsigned char* u8;
67
  int* i32;
68
  ccv_float16_t* f16;
69
  float* f32;
70
  int64_t* i64;
71
  uint64_t* u64;
72
  double* f64;
73
  void* ptr;
74
} ccv_numeric_data_t;
75
76
11.1M
#define CCV_NNC_MAX_DIM_ALLOC (12)
77
266G
#define CCV_NNC_MAX_DIM (2)
78
79
typedef struct {
80
  int type;
81
  int format;
82
  int datatype;
83
  int reserved; // This is reserved, for quantized tensor, this might be used in some ways.
84
  int dim[CCV_NNC_MAX_DIM_ALLOC];
85
} ccv_nnc_tensor_param_t;
86
87
typedef struct {
88
  int type;
89
  int refcount;
90
  ccv_numeric_data_t data;
91
  off_t dataof; // Offset.
92
  uintptr_t alias_ref;
93
  uint64_t data_size;
94
  uint64_t sig;
95
  ccv_nnc_tensor_param_t info;
96
} ccv_nnc_tensor_t;
97
98
typedef struct {
99
  int type;
100
  int refcount;
101
  ccv_numeric_data_t data;
102
  off_t dataof; // Offset for the data.
103
  uintptr_t alias_ref;
104
  uint64_t data_size;
105
  uint64_t sig;
106
  ccv_nnc_tensor_param_t info;
107
  /* tensor view and tensor shares the same data structure besides the following. */
108
  int contiguous; // If we have other flags later, we can make this a generic flag.
109
  off_t off; // Offset from the original tensor. This could be different from dataof.
110
  int stride[CCV_NNC_MAX_DIM_ALLOC]; /**< "stride" or, length */
111
} ccv_nnc_tensor_view_t;
112
113
1.60M
#define CCV_IS_TENSOR_VIEW(x) ((*(int*)(x)) & CCV_TENSOR_VIEW)
114
590k
#define CCV_IS_TENSOR_MULTIVIEW(x) ((*(int*)(
x928
)) & CCV_TENSOR_MULTIVIEW)
115
22.7k
#define CCV_IS_TENSOR_CONTIGUOUS(x) (!CCV_IS_TENSOR_VIEW(x) || 
(((ccv_nnc_tensor_view_t*)x)->contiguous == 1)6
)
116
117
#if CCV_NNC_TENSOR_TFB
118
691
#define CCV_TENSOR_IS_DENSE_MATRIX(x) (((x) & 0xFFF) > 0) // has channel components
119
typedef struct {
120
  int type;
121
  int refcount;
122
  ccv_numeric_data_t data;
123
  off_t reserved0;
124
  uintptr_t reserved1;
125
  uint64_t reserved2;
126
  uint64_t sig;
127
  // This is used for toll-free bridging between ccv_dense_matrix_t and ccv_nnc_tensor_t
128
  // Note that this is bigger than it is needed, we carefully structured this
129
  // such that bit is reused as much, but still some wasted spaces.
130
  union {
131
    struct {
132
      int resides;
133
      int format;
134
      int datatype;
135
      int reserved3;
136
      int rows;
137
      int cols;
138
      int channels;
139
      int reserved4; /* This reserved bit need to be zero'ed such that later dim is not cared. */
140
      int step;
141
      union {
142
        unsigned char u8;
143
        int i32;
144
        ccv_float16_t f16;
145
        float f32;
146
        int64_t i64;
147
        double f64;
148
        void* p;
149
      } tb;
150
    };
151
    ccv_nnc_tensor_param_t info;
152
  }; 
153
} ccv_dense_matrix_t;
154
#else
155
#define CCV_TENSOR_IS_DENSE_MATRIX(x) (0)
156
typedef struct {
157
  int type;
158
  int refcount;
159
  uint64_t sig;
160
  int cols;
161
  int rows;
162
  int step;
163
  union {
164
    unsigned char u8;
165
    int i32;
166
    ccv_float16_t f16;
167
    float f32;
168
    int64_t i64;
169
    double f64;
170
    void* p;
171
  } tb;
172
  ccv_numeric_data_t data;
173
} ccv_dense_matrix_t;
174
#endif
175
176
#endif