Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"files.associations": {
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
}
}
46 changes: 25 additions & 21 deletions include/factory/layer_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@
#include "ops/op.hpp"
#include "layer/layer.hpp"

namespace kuiper_infer {
class LayerRegisterer {
public:
typedef std::shared_ptr<Layer> (*Creator)(const std::shared_ptr<Operator> &op);

typedef std::map<OpType, Creator> CreateRegistry;

static void RegisterCreator(OpType op_type, const Creator &creator);

static std::shared_ptr<Layer> CreateLayer(const std::shared_ptr<Operator> &op);

static CreateRegistry &Registry();
};

class LayerRegistererWrapper {
public:
LayerRegistererWrapper(OpType op_type, const LayerRegisterer::Creator &creator) {
LayerRegisterer::RegisterCreator(op_type, creator);
}
};
namespace kuiper_infer
{
class LayerRegisterer
{
public:
// 这是一个函数指针,输入参数为const std::shared_ptr<Operator> &op,输出参数为std::shared_ptr<Layer>
typedef std::shared_ptr<Layer> (*Creator)(const std::shared_ptr<Operator> &op);
// 这是注册表类型的别名CreateRegistry
typedef std::map<OpType, Creator> CreateRegistry;
// 注册一下算子,将算子的信息塞到map中
static void RegisterCreator(OpType op_type, const Creator &creator);
// 创建对应的layer并返回
static std::shared_ptr<Layer> CreateLayer(const std::shared_ptr<Operator> &op);
static CreateRegistry &Registry();
};

class LayerRegistererWrapper // 这个是为了实现算子的注册
{
public:
LayerRegistererWrapper(OpType op_type, const LayerRegisterer::Creator &creator)
{
LayerRegisterer::RegisterCreator(op_type, creator);
}
};

}
#endif //KUIPER_COURSE_INCLUDE_FACTORY_LAYER_FACTORY_HPP_
#endif // KUIPER_COURSE_INCLUDE_FACTORY_LAYER_FACTORY_HPP_
29 changes: 15 additions & 14 deletions include/layer/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
#define KUIPER_COURSE_INCLUDE_LAYER_LAYER_HPP_
#include <string>
#include "data/tensor.hpp"
namespace kuiper_infer {
class Layer {
public:
explicit Layer(const std::string &layer_name);
namespace kuiper_infer
{
class Layer
{
public:
explicit Layer(const std::string &layer_name);
// 算子执行接口
virtual void Forwards(const std::vector<std::shared_ptr<Tensor<float>>> &inputs,
std::vector<std::shared_ptr<Tensor<float>>> &outputs);
virtual ~Layer() = default;

virtual void Forwards(const std::vector<std::shared_ptr<Tensor<float>>> &inputs,
std::vector<std::shared_ptr<Tensor<float>>> &outputs);
// reluLayer中 inputs 等于 x , outputs 等于 y= x,if x>0
// 计算得到的结果放在y当中,x是输入,放在inputs中

virtual ~Layer() = default;
private:
std::string layer_name_; //relu layer "relu"
};
private:
// 存储算子类型
std::string layer_name_; // relu layer "relu"
};
}
#endif //KUIPER_COURSE_INCLUDE_LAYER_LAYER_HPP_
#endif // KUIPER_COURSE_INCLUDE_LAYER_LAYER_HPP_
35 changes: 17 additions & 18 deletions include/layer/relu_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
#include "layer.hpp"
#include "ops/relu_op.hpp"

namespace kuiper_infer {
class ReluLayer : public Layer {
public:
~ReluLayer() override = default;
namespace kuiper_infer
{
class ReluLayer : public Layer
{
public:
~ReluLayer() override = default;
// 此处使用了算子基类的指针指向各种算子
explicit ReluLayer(const std::shared_ptr<Operator> &op);
// 执行relu 操作的具体函数Forwards,这个函数必须要重写
void Forwards(const std::vector<std::shared_ptr<Tensor<float>>> &inputs,
std::vector<std::shared_ptr<Tensor<float>>> &outputs) override;
// 根据op创建相应的layer
static std::shared_ptr<Layer> CreateInstance(const std::shared_ptr<Operator> &op);

// 通过这里,把relu_op中的thresh告知给relu layer, 因为计算的时候要用到
explicit ReluLayer(const std::shared_ptr<Operator> &op);

// 执行relu 操作的具体函数Forwards
void Forwards(const std::vector<std::shared_ptr<Tensor<float>>> &inputs,
std::vector<std::shared_ptr<Tensor<float>>> &outputs) override;

// 下节的内容,不用管
static std::shared_ptr<Layer> CreateInstance(const std::shared_ptr<Operator> &op);

private:
std::unique_ptr<ReluOperator> op_;
};
private:
std::unique_ptr<ReluOperator> op_;
};
}
#endif //KUIPER_COURSE_INCLUDE_LAYER_RELU_LAYER_HPP_
#endif // KUIPER_COURSE_INCLUDE_LAYER_RELU_LAYER_HPP_
33 changes: 17 additions & 16 deletions include/ops/op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

#ifndef KUIPER_COURSE_INCLUDE_OPS_OP_HPP_
#define KUIPER_COURSE_INCLUDE_OPS_OP_HPP_
namespace kuiper_infer {
enum class OpType {
kOperatorUnknown = -1,
kOperatorRelu = 0,
};

class Operator {
public:
OpType op_type_ = OpType::kOperatorUnknown; //不是一个具体节点 制定为unknown

virtual ~Operator() = default; //

explicit Operator(OpType op_type);
};

namespace kuiper_infer
{
// 算子类型枚举
enum class OpType
{
kOperatorUnknown = -1,
kOperatorRelu = 0,
};

class Operator
{
public:
// 存储算子的类型
OpType op_type_ = OpType::kOperatorUnknown; // 不是一个具体节点 制定为unknown
virtual ~Operator() = default; // 指定为虚函数是为了让Operator成为一个抽象类
explicit Operator(OpType op_type);
};
}
#endif //KUIPER_COURSE_INCLUDE_OPS_OP_HPP_
#endif // KUIPER_COURSE_INCLUDE_OPS_OP_HPP_
36 changes: 14 additions & 22 deletions include/ops/relu_op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@
#ifndef KUIPER_COURSE_INCLUDE_OPS_RELU_OP_HPP_
#define KUIPER_COURSE_INCLUDE_OPS_RELU_OP_HPP_
#include "op.hpp"
namespace kuiper_infer {
class ReluOperator : public Operator {
public:
~ReluOperator() override = default;
namespace kuiper_infer
{
class ReluOperator : public Operator
{
public:
// override是为了保证重写的函数,与基类有相同的签名
~ReluOperator() override = default;
explicit ReluOperator(float thresh);
void set_thresh(float thresh);
float get_thresh() const;

explicit ReluOperator(float thresh);

void set_thresh(float thresh);

float get_thresh() const;

private:
// 需要传递到reluLayer中,怎么传递?
float thresh_ = 0.f; // 用于过滤tensor<float>值当中大于thresh的部分
// relu存的变量只有thresh
// stride padding kernel_size 这些是到时候convOperator需要的
// operator起到了属性存储、变量的作用
// operator所有子类不负责具体运算
// 具体运算由另外一个类Layer类负责
// y =x , if x >=0 y = 0 if x < 0

};
private:
float thresh_ = 0.f; // 存储阈值
};
}
#endif //KUIPER_COURSE_INCLUDE_OPS_RELU_OP_HPP_
#endif // KUIPER_COURSE_INCLUDE_OPS_RELU_OP_HPP_
49 changes: 27 additions & 22 deletions source/factory/layer_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@
#include "factory/layer_factory.hpp"
#include <glog/logging.h>

namespace kuiper_infer {
void LayerRegisterer::RegisterCreator(OpType op_type, const Creator &creator) {
CHECK(creator != nullptr) << "Layer creator is empty";
CreateRegistry &registry = Registry();
CHECK_EQ(registry.count(op_type), 0) << "Layer type: " << int(op_type) << " has already registered!";
registry.insert({op_type, creator});
}
namespace kuiper_infer
{
void LayerRegisterer::RegisterCreator(OpType op_type, const Creator &creator)
{
CHECK(creator != nullptr) << "Layer creator is empty";
CreateRegistry &registry = Registry();
CHECK_EQ(registry.count(op_type), 0) << "Layer type: " << int(op_type) << " has already registered!";
registry.insert({op_type, creator});
}

std::shared_ptr<Layer> LayerRegisterer::CreateLayer(const std::shared_ptr<Operator> &op) {
CreateRegistry &registry = Registry();
const OpType op_type = op->op_type_;
std::shared_ptr<Layer> LayerRegisterer::CreateLayer(const std::shared_ptr<Operator> &op)
{
CreateRegistry &registry = Registry();
const OpType op_type = op->op_type_;

LOG_IF(FATAL, registry.count(op_type) <= 0) << "Can not find the layer type: " << int(op_type);
const auto &creator = registry.find(op_type)->second;
LOG_IF(FATAL, registry.count(op_type) <= 0) << "Can not find the layer type: " << int(op_type);
const auto &creator = registry.find(op_type)->second;

LOG_IF(FATAL, !creator) << "Layer creator is empty!";
std::shared_ptr<Layer> layer = creator(op);
LOG_IF(FATAL, !layer) << "Layer init failed!";
return layer;
}
LOG_IF(FATAL, !creator) << "Layer creator is empty!";
std::shared_ptr<Layer> layer = creator(op);
LOG_IF(FATAL, !layer) << "Layer init failed!";
return layer;
}

LayerRegisterer::CreateRegistry &LayerRegisterer::Registry() {
static CreateRegistry *kRegistry = new CreateRegistry();
CHECK(kRegistry != nullptr) << "Global layer register init failed!";
return *kRegistry;
}
LayerRegisterer::CreateRegistry &LayerRegisterer::Registry()
{
// 实现单例 static创建的CreateRegistry是全局变量,系统运行过程中是全局唯一的
static CreateRegistry *kRegistry = new CreateRegistry();
CHECK(kRegistry != nullptr) << "Global layer register init failed!";
return *kRegistry;
}
}
19 changes: 10 additions & 9 deletions source/layer/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
//
#include "layer/layer.hpp"
#include <glog/logging.h>
namespace kuiper_infer {
Layer::Layer(const std::string &layer_name) : layer_name_(layer_name) {

}

void Layer::Forwards(const std::vector<std::shared_ptr<Tensor<float>>> &inputs,
std::vector<std::shared_ptr<Tensor<float>>> &outputs) {
LOG(FATAL) << "The layer " << this->layer_name_ << " not implement yet!";
}
namespace kuiper_infer
{
Layer::Layer(const std::string &layer_name) : layer_name_(layer_name)
{
}
void Layer::Forwards(const std::vector<std::shared_ptr<Tensor<float>>> &inputs,
std::vector<std::shared_ptr<Tensor<float>>> &outputs)
{
LOG(FATAL) << "The layer " << this->layer_name_ << " not implement yet!";
}
}
Loading