LMMC
Lamina Mathematic core
载入中...
搜索中...
未找到
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
12static 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
27static 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
40static 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
49static 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
74static inline lmmc_status_t lmmc_tensor3_validate(
75 const lmmc_tensor3_t* tensor) {
76 if (!lmmc_tensor3_descriptor_fields_are_valid(tensor)) {
78 }
79 if (!lmmc_tensor3_logical_size_is_valid(tensor)) {
81 }
82 if (!lmmc_tensor3_span_is_valid(tensor)) {
84 }
85 return LMMC_STATUS_OK;
86}
87
88
89static 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
93static 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) {
102 }
103 if (!lmmc_tensor3_same_shape(a, b) || !lmmc_tensor3_same_shape(a, out_tensor)) {
105 }
106 return LMMC_STATUS_OK;
107}
108
109#endif
double lmmc_real_t
Public and internal LMMC real type.
LMMC 内部使用的公共工具(仅源文件可见).
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_OK
@ LMMC_STATUS_DIMENSION_MISMATCH
@ LMMC_STATUS_INVALID_ARGUMENT
三阶稠密张量数据结构、运算与视图。
lmmc_real_t * data
三阶张量结构。