Skip to content

Commit

Permalink
修改状态码命名
Browse files Browse the repository at this point in the history
  • Loading branch information
shenshen.fu committed Mar 10, 2024
1 parent aeec18e commit 975dcde
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 33 deletions.
41 changes: 41 additions & 0 deletions bench/bench_rmsnorm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <benchmark/benchmark.h>
#include <cstring>
#include <ctime>
#include <memory>
//
// Created by fss on 24-2-15.
//
#include "../demos/llama2/llama_chat.hpp"
#include "../source/layer/details/rms_norm.hpp"
#include "data/tensor.hpp"

static void BM_RMSNorm(benchmark::State& state) {
using namespace kuiper_infer;
int input_size = state.range(0);

std::vector<std::shared_ptr<Tensor<float>>> input_tensors;
std::vector<std::shared_ptr<Tensor<float>>> output_tensors;

std::shared_ptr<Tensor<float>> input_tensor = std::make_shared<Tensor<float>>(input_size);
input_tensor->RandN();
input_tensors.push_back(input_tensor);

std::vector<sftensor> weight_tensors;
weight_tensors.push_back(input_tensor);

std::vector<std::shared_ptr<Tensor<float>>> outputs(input_size);

std::shared_ptr<Tensor<float>> output_tensor = std::make_shared<Tensor<float>>(input_size);
output_tensors.push_back(output_tensor);

RMSNormLayer rms;
rms.set_weights(weight_tensors);
for (auto _ : state) {
rms.Forward(input_tensors, output_tensors);
}
}

BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(128)->Iterations(3);
BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(512)->Iterations(3);
BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(1024)->Iterations(3);
BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(4096)->Iterations(3);
2 changes: 1 addition & 1 deletion bench/bench_unet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ static void BM_Unet_Batch1_512x512(benchmark::State& state) {
}
}

BENCHMARK(BM_Unet_Batch1_512x512)->Unit(benchmark::kMillisecond)->Iterations(kIterationNum);
//BENCHMARK(BM_Unet_Batch1_512x512)->Unit(benchmark::kMillisecond)->Iterations(kIterationNum);
2 changes: 1 addition & 1 deletion include/status_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum class StatusCode {
kInferInputsEmpty = 1,
kInferOutputsEmpty = 2,
kInferParameterError = 3,
kInferInOutShapeMismatch = 4,
kInferDimMismatch = 4,

kFunctionNotImplement = 5,
kParseWeightError = 6,
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ StatusCode ActivationForward(ActivationType type,
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the " + activation_type +
" layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

const uint32_t batch_size = inputs.size();
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/adaptive_avgpooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ StatusCode AdaptiveAveragePoolingLayer::Forward(
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the adaptive "
"pooling layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

if (!output_h_ || !output_w_) {
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/base_convolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ StatusCode BaseConvolutionLayer::Forward(const std::vector<std::shared_ptr<Tenso
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the convolution "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

if (weights_.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/batchnorm2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ StatusCode BatchNorm2dLayer::Forward(const std::vector<std::shared_ptr<Tensor<fl
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the batchnorm2d "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

const uint32_t mean_value_size = this->weights_.size();
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/cat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ StatusCode CatLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>>>&
const uint32_t output_size = outputs.size();
if (inputs.size() % output_size != 0) {
LOG(ERROR) << "The input and output tensor array size of cat layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

const uint32_t packet_size = inputs.size() / output_size;
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/flatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ StatusCode FlattenLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the flatten "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

int32_t start_dim = start_dim_;
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ StatusCode LinearLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>>
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the linear "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

if (this->weights_.empty()) {
Expand Down
31 changes: 13 additions & 18 deletions source/layer/details/matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ void LLamaMatmulLayer::set_weights(const std::vector<float>& weights) {

void LLamaMatmulLayer::set_weights(const std::vector<std::shared_ptr<Tensor<float>>>& weights) {
CHECK(weights.size() == weights_.size());
for (uint32_t i = 0; i < weights.size(); ++i) {
if (this->weights_.at(i) != nullptr) {
CHECK(this->weights_.at(i)->rows() == weights.at(i)->rows());
CHECK(this->weights_.at(i)->cols() == weights.at(i)->cols());
CHECK(this->weights_.at(i)->channels() == weights.at(i)->channels());
}
}
this->weights_ = weights;
}

Expand All @@ -60,7 +53,7 @@ StatusCode LLamaMatmulLayer::Forward(const std::vector<std::shared_ptr<Tensor<fl
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the matmul "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

if (this->weights_.empty()) {
Expand All @@ -75,15 +68,13 @@ StatusCode LLamaMatmulLayer::Forward(const std::vector<std::shared_ptr<Tensor<fl

// w @ x
uint32_t batch = inputs.size();
const std::shared_ptr<Tensor<float>>& weight = weights_.front();
arma::fmat weight_data(weight->raw_ptr(), weight_dim1_, weight_dim0_, false, true); // wt
#pragma omp parallel for if (batch > 1) num_threads(batch)
for (uint32_t i = 0; i < batch; ++i) {
std::shared_ptr<Tensor<float>> input = inputs.at(i);
CHECK(input != nullptr && !input->empty())
<< "The input tensor array in the matmul layer has an empty tensor " << i << " th";
const std::vector<uint32_t>& input_shapes = input->raw_shapes();
CHECK(input_shapes.size() <= 2);
CHECK(!input_shapes.empty() && input_shapes.size() <= 2);

uint32_t input_dim0 = 1;
uint32_t input_dim1 = 1;
Expand All @@ -97,6 +88,7 @@ StatusCode LLamaMatmulLayer::Forward(const std::vector<std::shared_ptr<Tensor<fl

// xt
arma::fmat input_vec(input->raw_ptr(), input_dim1, input_dim0, false, true);
const std::shared_ptr<Tensor<float>>& weight = weights_.front();
std::shared_ptr<Tensor<float>> output = outputs.at(i);
if (output == nullptr || output->empty()) {
output = std::make_shared<Tensor<float>>(1, input_dim1, weight_dim0_);
Expand All @@ -115,16 +107,19 @@ StatusCode LLamaMatmulLayer::Forward(const std::vector<std::shared_ptr<Tensor<fl
float* output_ptr = output->raw_ptr();
float* weight_ptr = weight->raw_ptr();
#pragma omp parallel for
for (int j = 0; j < weight_dim0_; ++j) {
for (int32_t j = 0; j < weight_dim0_; ++j) {
arma::fmat sub_weight(weight_ptr + j * weight_dim1_, weight_dim1_, 1, false, true);
*(output_ptr + j) = as_scalar(input_vec * sub_weight);
*(output_ptr + j) = arma::as_scalar(input_vec * sub_weight);
}
} else if (weight_dim0_ == 1) {
arma::fmat output_mat(output->raw_ptr(), input_dim1, weight_dim0_, false, true);
output_mat = input_vec * weight_data;
} else {
arma::fmat output_mat(output->raw_ptr(), weight_dim0_, input_dim1, false, true);
output_mat = (input_vec * weight_data).t();
arma::fmat weight_data(weight->raw_ptr(), weight_dim1_, weight_dim0_, false, true); // wt
if (weight_dim0_ == 1) {
arma::fmat output_mat(output->raw_ptr(), input_dim1, weight_dim0_, false, true);
output_mat = input_vec * weight_data;
} else {
arma::fmat output_mat(output->raw_ptr(), weight_dim0_, input_dim1, false, true);
output_mat = (input_vec * weight_data).t();
}
}
}
return StatusCode::kSuccess;
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/maxpooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ StatusCode MaxPoolingLayer::Forward(const std::vector<std::shared_ptr<Tensor<flo
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the maxpooling "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

if (!pooling_size_h_ || !pooling_size_w_) {
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/rms_norm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ StatusCode RMSNormLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the rmsnorm "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

if (weights_.empty() || weights_.front()->empty()) {
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/softmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ StatusCode SoftmaxLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the softmax "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

const uint32_t batch_size = inputs.size();
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/upsample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ StatusCode UpSampleLayer::Forward(const std::vector<std::shared_ptr<Tensor<float
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the upsample "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}
uint32_t scale_w = static_cast<uint32_t>(scale_w_);
uint32_t scale_h = static_cast<uint32_t>(scale_h_);
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ StatusCode ViewLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>>>&
if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the view "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

const uint32_t batch_size = inputs.size();
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/yolo_detect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ StatusCode YoloDetectLayer::Forward(const std::vector<std::shared_ptr<Tensor<flo
if (input_size / batch_size != stages_ || input_size % batch_size != 0) {
LOG(ERROR) << "The input and output tensor array size of the yolo detect "
"layer do not match";
return StatusCode::kInferInOutShapeMismatch;
return StatusCode::kInferDimMismatch;
}

CHECK(!this->conv_layers_.empty() && this->conv_layers_.size() == stages)
Expand Down

0 comments on commit 975dcde

Please sign in to comment.