2026-01-28 06:16:04 +00:00

99 lines
2.9 KiB
C

//
// Created by xiangzhou on 11/9/23.
//
// Note : As this file could be used by C or C++ compiler, so it can't use reference, func name overloading etc
#pragma once
#define MAX_NUM_COUNT 128 // hate_speech has 50 inputs
#define MAX_NUM_AXIS 20 // The real max rank is 10 here. Some attribute may have size of 2*AXIS like pads, that's why MAX_NUM_AXIS is defined as 20
#define MAX_ATTR_COUNT 20 // used by Mystery Op to define max number of attributes
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
// This is the same int as defined as in TensorProto_DataType in onnx
enum Data_Type {
FLOAT32_T = 1,
UINT8_T = 2,
INT8_T = 3 ,
UINT16_T = 4,
INT16_T =5,
INT32_T = 6,
INT64_T =7,
STRING_T =8,
};
struct C_Shape {
int32_t dim[MAX_NUM_AXIS];
size_t axes;
};
typedef struct C_Shape C_Shape;
struct C_Tensor {
float* data;
enum Data_Type data_type;
C_Shape shape;
};
typedef struct C_Tensor C_Tensor;
struct C_Tensors {
C_Tensor tensor[MAX_NUM_COUNT];
size_t counts;
};
typedef struct C_Tensors C_Tensors;
struct Attribute {
const char* name_;
};
typedef struct Attribute Attribute;
bool cmpShape(const C_Shape* left, const C_Shape* right);
float* getData(const C_Tensors* tensors, size_t i);
enum Data_Type getDataType(const C_Tensors* tensors, size_t i);
size_t getSize(const C_Tensors* tensors, size_t i);
size_t getSizeFmDim(const C_Tensors* tensors, size_t i, size_t start);
size_t getSizeToDim(const C_Tensors* tensors, size_t i, size_t end);
size_t getSizeFmDim_(const C_Shape* shape, size_t start);
size_t getSizeToDim_(const C_Shape* shape, size_t end);
C_Shape getShape(const C_Tensors* tensors, size_t i);
// The following will consider inputs, weights together
float* getInputData(const C_Tensors* in_tensors, const C_Tensors* weight_tensors, size_t i);
size_t getInputSize(const C_Tensors* in_tensors, const C_Tensors* weight_tensors, size_t i);
C_Shape getInputShape(const C_Tensors* in_tensors, const C_Tensors* weight_tensors, size_t i);
void is_equal_or_exit(const char* name, size_t input_size, size_t output_size);
void is_axes_larger_than_4_or_exit(const char* name, const char* pos, size_t axes);
void is_axis_in_range_or_exit(const char* name, int axis, int low, int high) ;
#define CREATE_DECL_SETUP_FUN(name) \
void name##Operator_Setup(const C_Tensors* inputs, const C_Tensors* weights, const C_Tensors* outputs, name##Attribute* attr);
#define CREATE_DECL_RUN_FUN(name) \
void name##Operator_Run(const C_Tensors* inputs, const C_Tensors* weights, C_Tensors* outputs, const name##Attribute* attr);
#define CREATE_DECL_RUN_FUN2(name) \
void name##Operator_Run(const C_Tensors* inputs, const C_Tensors* weights, C_Tensors* outputs, name##Attribute* attr);
#ifdef __cplusplus
}
void is_equal_or_exit(const char* name, const C_Shape& , const C_Shape&);
#endif