Skip to content

Commit

Permalink
修改状态码,增加check实现
Browse files Browse the repository at this point in the history
  • Loading branch information
zjhellofss committed May 31, 2024
1 parent 2fbe102 commit 844eeb3
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 58 deletions.
6 changes: 3 additions & 3 deletions demos/llama2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include "llama_chat.hpp"
int main(int argc, char* argv[]) {
// default parameters
char* checkpoint_path = "tmp/llama2/llama2_7b.bin"; // e.g. out/model.bin
char* tokenizer_path = "tmp/llama2/tokenizer.bin";
char* checkpoint_path = "/weight/llama2_7b.bin"; // e.g. out/model.bin
char* tokenizer_path = "/weight/tokenizer.bin";
float temperature = 1.0f; // 0.0 = greedy deterministic. 1.0 = original. don't set higher
float topp = 0.9f; // top-p in nucleus sampling. 1.0 = off. 0.9 works well, but slower
int steps = 256; // number of steps to run for
int steps = 8; // number of steps to run for
char* prompt = NULL; // prompt string
unsigned long long rng_seed = 0; // seed rng with time by default
char* mode = "generate"; // generate|chat
Expand Down
2 changes: 1 addition & 1 deletion include/status_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum class StatusCode {

kInferInputsEmpty = 1,
kInferOutputsEmpty = 2,
kInferParameterError = 3,
kInferInternalError = 3,
kInferDimMismatch = 4,

kFunctionNotImplement = 5,
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 @@ -54,7 +54,7 @@ StatusCode AdaptiveAveragePoolingLayer::Forward(
if (!output_h_ || !output_w_) {
LOG(ERROR) << "The output_h and output_w in the adaptive pooling layer should be "
"greater than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

const uint32_t batch = inputs.size();
Expand Down
16 changes: 8 additions & 8 deletions source/layer/details/base_convolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,44 +402,44 @@ StatusCode BaseConvolutionLayer::Check(const std::vector<sftensor>& inputs,
if (weights_.empty()) {
LOG(ERROR) << "The number of kernel matrix in the convolution layer should "
"be greater than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (this->use_bias_ && this->bias_.size() != this->weights_.size()) {
LOG(ERROR) << "The number of kernel matrix and bias matrix do not match";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (!stride_h_ || !stride_w_) {
LOG(ERROR) << "The stride in the convolution layer should be greater "
"than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (!dilation_h_ || !dilation_w_) {
LOG(ERROR) << "The dilation in the convolution layer should be greater "
"than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (!groups_) {
LOG(ERROR) << "The group number in the convolution layer should be "
"greater than zero ";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (conv_type_ == ConvType::kOpConv) {
if (output_padding_h_ != 0 || output_padding_w_ != 0) {
LOG(ERROR) << "The output padding in the convolution layer should be zero ";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}
}

const uint32_t kernel_count = this->weights_.size();
if (!kernel_count) {
LOG(ERROR) << "The size of kernel matrix in the convolution layer should be greater "
"than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

const uint32_t kernel_h = this->weights_.at(0)->rows();
Expand All @@ -449,7 +449,7 @@ StatusCode BaseConvolutionLayer::Check(const std::vector<sftensor>& inputs,
if (!kernel_h || !kernel_w || !kernel_channel) {
LOG(ERROR) << "The size of kernel matrix in the convolution layer should be greater "
"than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

for (uint32_t k = 0; k < kernel_count; ++k) {
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 @@ -49,19 +49,19 @@ StatusCode BatchNorm2dLayer::Forward(const std::vector<std::shared_ptr<Tensor<fl
if (mean_value_size != bias_value_size) {
LOG(ERROR) << "The batchnorm2d layer do not have the same number of mean "
"values and bias values";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (this->affine_weight_.size() != this->weights().size()) {
LOG(ERROR) << "The batchnorm2d layer do not have the same number of mean "
"values and affine weight";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (this->affine_bias_.size() != this->affine_weight_.size()) {
LOG(ERROR) << "The batchnorm2d layer do not have the same number of affine "
"weight and affine bias";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}
const uint32_t batch_size = inputs.size();
#pragma omp parallel for num_threads(batch_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 @@ -111,7 +111,7 @@ StatusCode CatLayer::Check(const std::vector<sftensor>& inputs,

if (dim_ != 1 && dim_ != -3) {
LOG(ERROR) << "The dimension parameter of cat layer is error";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

const uint32_t output_size = outputs.size();
Expand Down
4 changes: 2 additions & 2 deletions source/layer/details/flatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ StatusCode FlattenLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>

if (end_dim <= start_dim) {
LOG(ERROR) << "The end dim must greater than start dim";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (end_dim > 3 || start_dim < 1) {
LOG(ERROR) << "The end dim must less than two and start dim must greater than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

const uint32_t batch_size = inputs.size();
Expand Down
8 changes: 4 additions & 4 deletions source/layer/details/linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,22 @@ StatusCode LinearLayer::Check(const std::vector<sftensor>& inputs,

if (this->weights_.empty()) {
LOG(ERROR) << "The weight tensor in the linear layer is empty";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
} else {
if (this->use_bias_ && this->weights_.size() != this->bias_.size()) {
LOG(ERROR) << "The size of the weight and bias tensor do not match";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}
}

if (weights_.size() != 1) {
LOG(ERROR) << "Need one weight tensor in the linear layer";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (use_bias_ && this->bias_.size() != 1) {
LOG(ERROR) << "Need one bias tensor in the linear layer";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}
return StatusCode::kSuccess;
}
Expand Down
4 changes: 2 additions & 2 deletions source/layer/details/matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ StatusCode LLamaMatmulLayer::Forward(const std::vector<std::shared_ptr<Tensor<fl

if (this->weights_.empty()) {
LOG(ERROR) << "The weight tensor in the matmul layer is empty";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (weights_.size() != 1) {
LOG(ERROR) << "Need one weight tensor in the matmul layer";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

// w @ x
Expand Down
4 changes: 2 additions & 2 deletions source/layer/details/maxpooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ StatusCode MaxPoolingLayer::Check(const std::vector<sftensor>& inputs,
if (!pooling_size_h_ || !pooling_size_w_) {
LOG(ERROR) << "The pooling size in the maxpooling layer should be greater "
"than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (!stride_w_ || !stride_h_) {
LOG(ERROR) << "The stride in the maxpooling layer should be greater "
"than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}
return StatusCode::kSuccess;
}
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 @@ -56,7 +56,7 @@ StatusCode RMSNormLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>

if (weights_.empty() || weights_.front()->empty()) {
LOG(ERROR) << "The weight for the rmsnorm layer is missing.";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

std::shared_ptr<Tensor<float>> weight = this->weight(0);
Expand Down
65 changes: 38 additions & 27 deletions source/layer/details/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,12 @@ namespace kuiper_infer {

StatusCode ViewLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) {
if (inputs.empty()) {
LOG(ERROR) << "The input tensor array in the view layer is empty";
return StatusCode::kInferInputsEmpty;
}

if (outputs.empty()) {
LOG(ERROR) << "The output tensor array in the view layer is empty";
return StatusCode::kInferOutputsEmpty;
}

if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the view "
"layer do not match";
return StatusCode::kInferDimMismatch;
StatusCode status_code = Check(inputs, outputs);
if (status_code != StatusCode::kSuccess) {
return status_code;
}

const uint32_t batch_size = inputs.size();
if (shapes_.empty() || (shapes_.front() != -1 && shapes_.front() != batch_size)) {
LOG(ERROR) << "The shape parameter in the view layer has an incorrectly size! ";
return StatusCode::kInferParameterError;
}

for (uint32_t i = 0; i < batch_size; ++i) {
const std::shared_ptr<Tensor<float>>& input_data = inputs.at(i);
CHECK(input_data != nullptr && !input_data->empty())
Expand All @@ -74,19 +58,20 @@ StatusCode ViewLayer::Forward(const std::vector<std::shared_ptr<Tensor<float>>>&
}
}

CHECK(dynamic_index == -1 || dynamic_index == shapes_.size() - 1)
<< "-1 appears in the wrong dimension, it can only be on the last "
"dimension";
if (dynamic_index != -1) {
CHECK(total_size >= current_size);
shapes.push_back(uint32_t(total_size / current_size));
if (dynamic_index != -1 && dynamic_index != shapes_.size() - 1) {
LOG(ERROR) << "-1 appears in the wrong dimension, it can only be on the last "
"dimension";
return StatusCode::kInferInternalError;
} else {
if (dynamic_index != -1) {
CHECK(total_size >= current_size);
shapes.push_back(uint32_t(total_size / current_size));
}
}

std::shared_ptr<Tensor<float>> output_data = outputs.at(i);
output_data = TensorClone(input_data);
CHECK(input_data->size() == output_data->size());
outputs.at(i) = output_data;

output_data->Reshape(shapes, true);
}
return StatusCode::kSuccess;
Expand Down Expand Up @@ -122,6 +107,32 @@ StatusCode ViewLayer::CreateInstance(const std::shared_ptr<RuntimeOperator>& op,
ViewLayer::ViewLayer(std::vector<int32_t> shapes)
: NonParamLayer("view"), shapes_(std::move(shapes)) {}

StatusCode ViewLayer::Check(const std::vector<sftensor>& inputs,
const std::vector<sftensor>& outputs) {
if (inputs.empty()) {
LOG(ERROR) << "The input tensor array in the view layer is empty";
return StatusCode::kInferInputsEmpty;
}

if (outputs.empty()) {
LOG(ERROR) << "The output tensor array in the view layer is empty";
return StatusCode::kInferOutputsEmpty;
}

if (inputs.size() != outputs.size()) {
LOG(ERROR) << "The input and output tensor array size of the view "
"layer do not match";
return StatusCode::kInferDimMismatch;
}

const uint32_t batch_size = inputs.size();
if (shapes_.empty() || (shapes_.front() != -1 && shapes_.front() != batch_size)) {
LOG(ERROR) << "The shape parameter in the view layer has an incorrectly size! ";
return StatusCode::kInferInternalError;
}
return StatusCode::kSuccess;
}

LayerRegistererWrapper kViewCreateInstance(ViewLayer::CreateInstance, "Tensor.view");

} // namespace kuiper_infer
3 changes: 3 additions & 0 deletions source/layer/details/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ViewLayer : public NonParamLayer {
public:
explicit ViewLayer(std::vector<int32_t> shapes);

StatusCode Check(const std::vector<sftensor>& inputs,
const std::vector<sftensor>& outputs) override;

StatusCode Forward(const std::vector<std::shared_ptr<Tensor<float>>>& inputs,
std::vector<std::shared_ptr<Tensor<float>>>& outputs) override;

Expand Down
6 changes: 3 additions & 3 deletions test/test_layer/test_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ StatusCode Convolution(const std::vector<std::shared_ptr<Tensor<float>>>& inputs
}
if (weights_.empty()) {
LOG(ERROR) << "Weight parameters is empty";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

const uint32_t batch_size = inputs.size();
Expand All @@ -67,7 +67,7 @@ StatusCode Convolution(const std::vector<std::shared_ptr<Tensor<float>>>& inputs
uint32_t output_w = uint32_t(std::floor((input_w - kernel_w) / stride_w_ + 1));
if (output_h <= 0 || output_w <= 0) {
LOG(ERROR) << "The size of the output feature map is less than zero";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}

if (!output_data) {
Expand All @@ -77,7 +77,7 @@ StatusCode Convolution(const std::vector<std::shared_ptr<Tensor<float>>>& inputs

if (kernel->channels() != input_c) {
LOG(ERROR) << "The channel of the weight and input is not adapting";
return StatusCode::kInferParameterError;
return StatusCode::kInferInternalError;
}
arma::fmat& output_channel = output_data->slice(k);
for (uint32_t ic = 0; ic < input_c; ++ic) {
Expand Down

0 comments on commit 844eeb3

Please sign in to comment.