Skip to content

Commit

Permalink
修改get_instance为create instance. 修改convolution.cpp中的逻辑流程
Browse files Browse the repository at this point in the history
  • Loading branch information
zjhellofss committed Aug 13, 2023
1 parent 5e12234 commit b82f217
Show file tree
Hide file tree
Showing 36 changed files with 118 additions and 97 deletions.
12 changes: 9 additions & 3 deletions bench/bench_yolo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ static void BM_Yolov5s_Batch8_640x640(benchmark::State& state) {
}
}

BENCHMARK(BM_Yolov5nano_Batch4_320x320)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_Yolov5s_Batch4_640x640)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_Yolov5s_Batch8_640x640)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_Yolov5nano_Batch4_320x320)
->Unit(benchmark::kMillisecond)
->Iterations(5);
BENCHMARK(BM_Yolov5s_Batch4_640x640)
->Unit(benchmark::kMillisecond)
->Iterations(5);
BENCHMARK(BM_Yolov5s_Batch8_640x640)
->Unit(benchmark::kMillisecond)
->Iterations(5);
6 changes: 3 additions & 3 deletions include/utils/math/fmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ struct C {
static const LogVar<LOG_N> logVar;
static const ExpdVar<EXPD_N> expdVar;
#ifdef FMATH_USE_XBYAK
static const ExpCode& getInstance() {
static const ExpCode& CreateInstance() {
static const ExpCode expCode(&expVar);
return expCode;
}
Expand Down Expand Up @@ -835,8 +835,8 @@ class PowGenerator {

// for Xbyak version
#ifdef FMATH_USE_XBYAK
float (*const exp)(float) = local::C<>::getInstance().exp_;
__m128 (*const exp_ps)(__m128) = local::C<>::getInstance().exp_ps_;
float (*const exp)(float) = local::C<>::CreateInstance().exp_;
__m128 (*const exp_ps)(__m128) = local::C<>::CreateInstance().exp_ps_;
#endif

// exp2(x) = pow(2, x)
Expand Down
6 changes: 3 additions & 3 deletions source/layer/details/adaptive_avgpooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ InferStatus AdaptiveAveragePoolingLayer::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus AdaptiveAveragePoolingLayer::GetInstance(
ParseParameterAttrStatus AdaptiveAveragePoolingLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& avg_layer) {
CHECK(op != nullptr) << "Adaptive pooling operator is nullptr";
Expand All @@ -160,7 +160,7 @@ ParseParameterAttrStatus AdaptiveAveragePoolingLayer::GetInstance(
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kAdaptiveAvgpoolingGetInstance(
"nn.AdaptiveAvgPool2d", AdaptiveAveragePoolingLayer::GetInstance);
LayerRegistererWrapper kAdaptiveAvgpoolingCreateInstance(
"nn.AdaptiveAvgPool2d", AdaptiveAveragePoolingLayer::CreateInstance);

} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/adaptive_avgpooling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AdaptiveAveragePoolingLayer : public NonParamLayer {
const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& avg_layer);

Expand Down
6 changes: 3 additions & 3 deletions source/layer/details/batchnorm2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ InferStatus BatchNorm2dLayer::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus BatchNorm2dLayer::GetInstance(
ParseParameterAttrStatus BatchNorm2dLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& batch_layer) {
CHECK(op != nullptr) << "BatchNorm get instance failed, operator is nullptr";
Expand Down Expand Up @@ -206,7 +206,7 @@ BatchNorm2dLayer::BatchNorm2dLayer(uint32_t num_features, float eps,
// }
}

LayerRegistererWrapper kBatchNorm2dGetInstance("nn.BatchNorm2d",
BatchNorm2dLayer::GetInstance);
LayerRegistererWrapper kBatchNorm2dCreateInstance("nn.BatchNorm2d",
BatchNorm2dLayer::CreateInstance);

} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/batchnorm2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BatchNorm2dLayer : public ParamLayer {
const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& batch_layer);

Expand Down
4 changes: 2 additions & 2 deletions source/layer/details/cat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ InferStatus CatLayer::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus CatLayer::GetInstance(
ParseParameterAttrStatus CatLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& cat_layer) {
CHECK(op != nullptr) << "Cat operator is nullptr";
Expand All @@ -138,5 +138,5 @@ ParseParameterAttrStatus CatLayer::GetInstance(
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kCatGetInstance("torch.cat", CatLayer::GetInstance);
LayerRegistererWrapper kCatCreateInstance("torch.cat", CatLayer::CreateInstance);
} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/cat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CatLayer : public NonParamLayer {
const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& cat_layer);

Expand Down
66 changes: 38 additions & 28 deletions source/layer/details/convolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ InferStatus ConvolutionLayer::Forward(
}

const uint32_t batch_size = inputs.size();

const uint32_t kernel_count_group = kernel_count / groups_;

if (kernel_matrix_arr_.empty()) {
Expand All @@ -195,34 +194,24 @@ InferStatus ConvolutionLayer::Forward(
<< i << " th";

const uint32_t input_c = input->channels();
const uint32_t input_padded_h = input->rows() + 2 * padding_h_;
const uint32_t input_padded_w = input->cols() + 2 * padding_w_;
const uint32_t input_h = input->rows();
const uint32_t input_w = input->cols();

const uint32_t input_padded_h = input_h + 2 * padding_h_;
const uint32_t input_padded_w = input_w + 2 * padding_w_;

CHECK(input_padded_h >= kernel_h && input_padded_w >= kernel_w);
uint32_t output_h = 0;
uint32_t output_w = 0;
uint32_t col_len = 0;
uint32_t input_h = input->rows();
uint32_t input_w = input->cols();
CHECK(input_h > 0 && input_w > 0);

if (conv_type_ == ConvType::OpConv) {
CHECK(input_padded_h >= kernel_h && input_padded_w >= kernel_w);
output_h = (input_padded_h - kernel_h) / stride_h_ + 1;
output_w = (input_padded_w - kernel_w) / stride_w_ + 1;
col_len = output_h * output_w;
} else {
CHECK(conv_type_ == ConvType::OpDeconv);
output_h = (input_h - 1) * stride_h_ + kernel_h + output_padding_h_;
output_w = (input_w - 1) * stride_w_ + kernel_w + output_padding_w_;
CHECK(output_h > 2 * padding_h_ && output_w > 2 * padding_w_);
output_h -= 2 * padding_h_;
output_w -= 2 * padding_w_;
}
CHECK(input_h > 0 && input_w > 0);
const auto [output_h, output_w] = CalcOutputSize(
conv_type_ == ConvType ::OpConv ? input_padded_h : input_h,
conv_type_ == ConvType ::OpConv ? input_padded_w : input_w, kernel_h,
kernel_w);

CHECK(output_h > 0 && output_w > 0)
<< "The size of the output tensor should be greater than zero " << i
<< " th";

#pragma omp parallel for if (groups_ > 1)
for (uint32_t g = 0; g < groups_; ++g) {
std::shared_ptr<Tensor<float>> output_tensor = outputs.at(i);
Expand Down Expand Up @@ -251,7 +240,7 @@ InferStatus ConvolutionLayer::Forward(
if (conv_type_ == ConvType::OpConv) {
const arma::fmat& input_matrix =
ConvIm2Col(input, kernel_h, kernel_w, input_h, input_w,
input_c_group, g, row_len, col_len);
input_c_group, g, row_len, output_h * output_w);
#pragma omp parallel for
for (uint32_t k = 0; k < kernel_count_group; ++k) {
ConvGemmBias(input_matrix, output_tensor, g, k, kernel_count_group,
Expand Down Expand Up @@ -461,7 +450,28 @@ void ConvolutionLayer::InitIm2ColWeight() {
this->kernel_matrix_arr_ = std::move(kernel_matrix_arr);
}

ParseParameterAttrStatus ConvolutionLayer::GetInstance(
std::pair<uint32_t, uint32_t> ConvolutionLayer::CalcOutputSize(
const uint32_t input_h, const uint32_t input_w, const uint32_t kernel_h,
const uint32_t kernel_w) {
uint32_t output_h = 0;
uint32_t output_w = 0;

if (conv_type_ == ConvType::OpConv) {
CHECK(input_h >= kernel_h && input_w >= kernel_w);
output_h = (input_h - kernel_h) / stride_h_ + 1;
output_w = (input_w - kernel_w) / stride_w_ + 1;
} else {
CHECK(conv_type_ == ConvType::OpDeconv);
output_h = (input_h - 1) * stride_h_ + kernel_h + output_padding_h_;
output_w = (input_w - 1) * stride_w_ + kernel_w + output_padding_w_;
CHECK(output_h > 2 * padding_h_ && output_w > 2 * padding_w_);
output_h -= 2 * padding_h_;
output_w -= 2 * padding_w_;
}
return {output_h, output_w};
}

ParseParameterAttrStatus ConvolutionLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& conv_layer) {
CHECK(op != nullptr) << "Convolution operator is nullptr";
Expand Down Expand Up @@ -676,9 +686,9 @@ ParseParameterAttrStatus ConvolutionLayer::GetInstance(
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kConvGetInstance("nn.Conv2d",
ConvolutionLayer::GetInstance);
LayerRegistererWrapper kConvCreateInstance("nn.Conv2d",
ConvolutionLayer::CreateInstance);

LayerRegistererWrapper kDeConvGetInstance("nn.ConvTranspose2d",
ConvolutionLayer::GetInstance);
LayerRegistererWrapper kDeConvCreateInstance("nn.ConvTranspose2d",
ConvolutionLayer::CreateInstance);
} // namespace kuiper_infer
7 changes: 6 additions & 1 deletion source/layer/details/convolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ConvolutionLayer : public ParamLayer {
bool use_bias = true, uint32_t output_padding_h = 0,
uint32_t output_padding_w = 0);

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& conv_layer);

Expand All @@ -60,6 +60,11 @@ class ConvolutionLayer : public ParamLayer {
void InitIm2ColWeight();

private:
std::pair<uint32_t, uint32_t> CalcOutputSize(const uint32_t input_h,
const uint32_t input_w,
const uint32_t kernel_h,
const uint32_t kernel_w);

void ConvGemmBias(const arma::fmat& input_matrix, sftensor output_tensor,
uint32_t group, uint32_t kernel_index,
uint32_t kernel_count_group, uint32_t output_h,
Expand Down
6 changes: 3 additions & 3 deletions source/layer/details/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ InferStatus ExpressionLayer::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus ExpressionLayer::GetInstance(
ParseParameterAttrStatus ExpressionLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& expression_layer) {
CHECK(op != nullptr) << "Expression operator is nullptr";
Expand All @@ -166,6 +166,6 @@ ParseParameterAttrStatus ExpressionLayer::GetInstance(
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kExpressionGetInstance("pnnx.Expression",
ExpressionLayer::GetInstance);
LayerRegistererWrapper kExpressionCreateInstance("pnnx.Expression",
ExpressionLayer::CreateInstance);
} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ExpressionLayer : public NonParamLayer {
const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& expression_layer);

Expand Down
6 changes: 3 additions & 3 deletions source/layer/details/flatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ InferStatus FlattenLayer::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus FlattenLayer::GetInstance(
ParseParameterAttrStatus FlattenLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& flatten_layer) {
CHECK(op != nullptr) << "Flatten operator is nullptr";
Expand Down Expand Up @@ -130,7 +130,7 @@ ParseParameterAttrStatus FlattenLayer::GetInstance(
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kFlattenGetInstance("torch.flatten",
FlattenLayer::GetInstance);
LayerRegistererWrapper kFlattenCreateInstance("torch.flatten",
FlattenLayer::CreateInstance);

} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/flatten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FlattenLayer : public NonParamLayer {
const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& flatten_layer);

Expand Down
6 changes: 3 additions & 3 deletions source/layer/details/hardsigmoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ InferStatus HardSigmoid::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus HardSigmoid::GetInstance(
ParseParameterAttrStatus HardSigmoid::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& hardsigmoid_layer) {
CHECK(op != nullptr) << "HardSigmoid operator is nullptr";
hardsigmoid_layer = std::make_shared<HardSigmoid>();
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kHardSigmoidGetInstance("nn.Hardsigmoid",
HardSigmoid::GetInstance);
LayerRegistererWrapper kHardSigmoidCreateInstance("nn.Hardsigmoid",
HardSigmoid::CreateInstance);

} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/hardsigmoid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class HardSigmoid : public NonParamLayer {
const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& hardsigmoid_layer);
};
Expand Down
6 changes: 3 additions & 3 deletions source/layer/details/hardswish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ InferStatus HardSwishLayer::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus HardSwishLayer::GetInstance(
ParseParameterAttrStatus HardSwishLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& hardswish_layer) {
CHECK(op != nullptr) << "HardSwishLayer operator is nullptr";
hardswish_layer = std::make_shared<HardSwishLayer>();
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kHardSwishGetInstance("nn.Hardswish",
HardSwishLayer::GetInstance);
LayerRegistererWrapper kHardSwishCreateInstance("nn.Hardswish",
HardSwishLayer::CreateInstance);

} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/hardswish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class HardSwishLayer : public NonParamLayer {
const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

static ParseParameterAttrStatus GetInstance(
static ParseParameterAttrStatus CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& hardswish_layer);
};
Expand Down
6 changes: 3 additions & 3 deletions source/layer/details/linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ InferStatus LinearLayer::Forward(
return InferStatus::kInferSuccess;
}

ParseParameterAttrStatus LinearLayer::GetInstance(
ParseParameterAttrStatus LinearLayer::CreateInstance(
const std::shared_ptr<RuntimeOperator>& op,
std::shared_ptr<Layer>& linear_layer) {
CHECK(op != nullptr) << "Linear operator is nullptr";
Expand Down Expand Up @@ -199,7 +199,7 @@ ParseParameterAttrStatus LinearLayer::GetInstance(
return ParseParameterAttrStatus::kParameterAttrParseSuccess;
}

LayerRegistererWrapper kLinearGetInstance("nn.Linear",
LinearLayer::GetInstance);
LayerRegistererWrapper kLinearCreateInstance("nn.Linear",
LinearLayer::CreateInstance);

} // namespace kuiper_infer
2 changes: 1 addition & 1 deletion source/layer/details/linear.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LinearLayer : public ParamLayer {
InferStatus Forward(const std::vector<std::shared_ptr<Tensor<float>>> &inputs,
std::vector<std::shared_ptr<Tensor<float>>> &outputs) override;

static ParseParameterAttrStatus GetInstance(const std::shared_ptr<RuntimeOperator> &op,
static ParseParameterAttrStatus CreateInstance(const std::shared_ptr<RuntimeOperator> &op,
std::shared_ptr<Layer> &linear_layer);
private:
int32_t in_features_ = 0;
Expand Down
Loading

0 comments on commit b82f217

Please sign in to comment.