添加新Tensor函数
添加Op包含如下步骤:
- 添加对应的Backend的Tensor函数实现(CPU)
- 添加Tensor函数的类型枚举
- 在对应的Backend注册实现的Tensor函数
- 添加Tensor函数的前端表示
添加对应的Backend的Tensor函数实现
CPU
在CPU目录下添加CPUTensorFunction.hpp中添加TensorFunction的子类。
示例如下:
#ifndef CPUTENSORFUNCTION_HPP#define CPUTENSORFUNCTION_HPP ... ...
namespace mllm {class Tensor;
class CPUabcFunction: public TensorFunction {public: void setup(vector<Tensor*> outputs, vector<Tensor*> inputs, vector<float> args) override { ... ... } void execute(vector<Tensor*> outputs, vector<Tensor*> inputs, vector<float> args) override { ... ... }};
} // namespace mllm#endif // CPUTENSORFUNCTION_HPP
添加Tensor函数的类型枚举
在OpDefined.hpp文件中添加Tensor函数的类型枚举。
#ifndef MLLM_OPDEFINED_H#define MLLM_OPDEFINED_H ... ...
namespace mllm { ... ...
enum TensorFuncType { FUNC_ADD, FUNC_SUB, ... ... FUNC_ABC, //<==在此添加};
}#endif
在对应的Backend注册实现的Tensor函数
在对应的Backend.cpp文件中添加CPUabcFunction
... ...
void CPUBackend::registerFuncs() { map_function_[TensorFuncType::FUNC_ADD] = new CPUaddFunction(); map_function_[TensorFuncType::FUNC_SUB] = new CPUsubFunction(); ... ... map_function_[TensorFuncType::FUNC_ABC] = new CPUabcFunction();//<==在此添加}
添加Tensor函数的前端表示
- 在Tensor.hpp中注册对应的Tensor函数的前端表示。
Tensor abc(... ...);
- 在Tensor.cpp中实现对应的Tensor函数的前端表示。
Tensor Tensor::abc(... ...);{ ... ...};