LMMC
Lamina Mathematic core
Toggle main menu visibility
载入中...
搜索中...
未找到
tensor3_internal.h
浏览该文件的文档.
1
6
#ifndef LMMC_TENSOR3_INTERNAL_H
7
#define LMMC_TENSOR3_INTERNAL_H
8
9
#include "
internal.h
"
10
#include "
lmmc/tensor3.h
"
11
12
static
inline
int
lmmc_tensor3_descriptor_fields_are_valid(
13
const
lmmc_tensor3_t
* tensor) {
14
if
(tensor == NULL) {
15
return
0;
16
}
17
if
(tensor->
data
== NULL) {
18
return
0;
19
}
20
if
(tensor->
dim0
== 0 || tensor->
dim1
== 0 || tensor->
dim2
== 0) {
21
return
0;
22
}
23
return
tensor->
stride0
!= 0 && tensor->
stride1
!= 0 &&
24
tensor->
stride2
!= 0;
25
}
26
27
static
inline
int
lmmc_tensor3_logical_size_is_valid(
28
const
lmmc_tensor3_t
* tensor) {
29
size_t
total;
30
size_t
bytes;
31
if
(!lmmc_safe_mul_size(tensor->
dim0
, tensor->
dim1
, &total)) {
32
return
0;
33
}
34
if
(!lmmc_safe_mul_size(total, tensor->
dim2
, &total)) {
35
return
0;
36
}
37
return
lmmc_safe_mul_size(total,
sizeof
(
lmmc_real_t
), &bytes);
38
}
39
40
static
inline
int
lmmc_tensor3_add_axis_span(
41
size_t
dim,
size_t
stride,
size_t
* max_offset) {
42
size_t
axis_offset;
43
if
(!lmmc_safe_mul_size(dim - 1, stride, &axis_offset)) {
44
return
0;
45
}
46
return
lmmc_safe_add_size(*max_offset, axis_offset, max_offset);
47
}
48
49
static
inline
int
lmmc_tensor3_span_is_valid(
const
lmmc_tensor3_t
* tensor) {
50
size_t
max_offset = 0;
51
size_t
elements;
52
size_t
bytes;
53
if
(!lmmc_tensor3_add_axis_span(
54
tensor->
dim0
, tensor->
stride0
, &max_offset)) {
55
return
0;
56
}
57
if
(!lmmc_tensor3_add_axis_span(
58
tensor->
dim1
, tensor->
stride1
, &max_offset)) {
59
return
0;
60
}
61
if
(!lmmc_tensor3_add_axis_span(
62
tensor->
dim2
, tensor->
stride2
, &max_offset)) {
63
return
0;
64
}
65
if
(!lmmc_safe_add_size(max_offset, 1, &elements)) {
66
return
0;
67
}
68
if
(!lmmc_safe_mul_size(elements,
sizeof
(
lmmc_real_t
), &bytes)) {
69
return
0;
70
}
71
return
(uintptr_t)bytes <= UINTPTR_MAX - (uintptr_t)tensor->
data
;
72
}
73
74
static
inline
lmmc_status_t
lmmc_tensor3_validate(
75
const
lmmc_tensor3_t
* tensor) {
76
if
(!lmmc_tensor3_descriptor_fields_are_valid(tensor)) {
77
return
LMMC_STATUS_INVALID_ARGUMENT
;
78
}
79
if
(!lmmc_tensor3_logical_size_is_valid(tensor)) {
80
return
LMMC_STATUS_INVALID_ARGUMENT
;
81
}
82
if
(!lmmc_tensor3_span_is_valid(tensor)) {
83
return
LMMC_STATUS_INVALID_ARGUMENT
;
84
}
85
return
LMMC_STATUS_OK
;
86
}
87
88
89
static
inline
int
lmmc_tensor3_same_shape(
const
lmmc_tensor3_t
* a,
const
lmmc_tensor3_t
* b) {
90
return
a->
dim0
== b->
dim0
&& a->
dim1
== b->
dim1
&& a->
dim2
== b->
dim2
;
91
}
92
93
static
inline
lmmc_status_t
lmmc_tensor3_validate_binary(
94
const
lmmc_tensor3_t
* a,
95
const
lmmc_tensor3_t
* b,
96
const
lmmc_tensor3_t
* out_tensor
97
) {
98
if
(lmmc_tensor3_validate(a) !=
LMMC_STATUS_OK
||
99
lmmc_tensor3_validate(b) !=
LMMC_STATUS_OK
||
100
lmmc_tensor3_validate(out_tensor) !=
LMMC_STATUS_OK
) {
101
return
LMMC_STATUS_INVALID_ARGUMENT
;
102
}
103
if
(!lmmc_tensor3_same_shape(a, b) || !lmmc_tensor3_same_shape(a, out_tensor)) {
104
return
LMMC_STATUS_DIMENSION_MISMATCH
;
105
}
106
return
LMMC_STATUS_OK
;
107
}
108
109
#endif
lmmc_real_t
double lmmc_real_t
Public and internal LMMC real type.
定义
config.h:28
internal.h
LMMC 内部使用的公共工具(仅源文件可见).
lmmc_status_t
lmmc_status_t
LMMC 库统一返回的状态码.
定义
status.h:22
LMMC_STATUS_OK
@ LMMC_STATUS_OK
定义
status.h:23
LMMC_STATUS_DIMENSION_MISMATCH
@ LMMC_STATUS_DIMENSION_MISMATCH
定义
status.h:25
LMMC_STATUS_INVALID_ARGUMENT
@ LMMC_STATUS_INVALID_ARGUMENT
定义
status.h:24
tensor3.h
三阶稠密张量数据结构、运算与视图。
lmmc_tensor3_t::dim0
size_t dim0
定义
tensor3.h:23
lmmc_tensor3_t::stride1
size_t stride1
定义
tensor3.h:27
lmmc_tensor3_t::stride0
size_t stride0
定义
tensor3.h:26
lmmc_tensor3_t::dim2
size_t dim2
定义
tensor3.h:25
lmmc_tensor3_t::stride2
size_t stride2
定义
tensor3.h:28
lmmc_tensor3_t::dim1
size_t dim1
定义
tensor3.h:24
lmmc_tensor3_t::data
lmmc_real_t * data
定义
tensor3.h:29
lmmc_tensor3_t
三阶张量结构。
定义
tensor3.h:22
src
internal
tensor3_internal.h
生成于
, 为 LMMC使用
1.18.0