From e91452d86cf62c666b005aa0cc7f9a4bca9d3ce8 Mon Sep 17 00:00:00 2001 From: chenhao66 Date: Fri, 19 Apr 2024 07:34:53 +0000 Subject: [PATCH] fix swish op bug && fix benchmark_bin print bug, test=develop --- lite/api/tools/benchmark/benchmark.cc | 90 ++++++++++++++++----------- lite/operators/activation_ops.cc | 6 +- 2 files changed, 59 insertions(+), 37 deletions(-) diff --git a/lite/api/tools/benchmark/benchmark.cc b/lite/api/tools/benchmark/benchmark.cc index 64efc525300..b545fa270ea 100644 --- a/lite/api/tools/benchmark/benchmark.cc +++ b/lite/api/tools/benchmark/benchmark.cc @@ -105,6 +105,47 @@ void RunImpl(std::shared_ptr predictor, perf_data->set_post_process_time(timer.Stop()); } #endif +template +void outputIdxTensor(const Tensor* output_tensor, + std::stringstream& out_ss, + size_t tidx) { + auto out_shape = output_tensor->shape(); + auto out_data = output_tensor->data(); + auto ele_num = lite::ShapeProduction(out_shape); + auto out_mean = lite::compute_mean(out_data, ele_num); + auto out_std_dev = + lite::compute_standard_deviation(out_data, ele_num, true, out_mean); + + out_ss << "output shape(NCHW): " << lite::ShapePrint(out_shape) << std::endl; + out_ss << "output tensor " << tidx << " elem num: " << ele_num << std::endl; + out_ss << "output tensor " << tidx << " mean value: " << out_mean + << std::endl; + out_ss << "output tensor " << tidx << " standard deviation: " << out_std_dev + << std::endl; + + if (FLAGS_show_output_elem) { + for (int i = 0; i < ele_num; ++i) { + out_ss << "out[" << tidx << "][" << i + << "]:" << output_tensor->data()[i] << std::endl; + } + } + + // TODO(sprouteer): Only support float for now, add more types if needed. + if (!FLAGS_output_data_path.empty()) { + std::stringstream out_data; + auto output_path = lite::Split(FLAGS_output_data_path, ":"); + if (output_path.size() <= tidx) { + std::cerr << "Fail to write output tensor to file, tensor_output_path " + "not matching output tensor number. " + << std::endl; + } else { + for (int i = 0; i < ele_num; ++i) { + out_data << output_tensor->data()[i] << std::endl; + } + StoreOutputTensor(out_data, output_path[tidx]); + } + } +} void Run(const std::string& model_file, const std::vector>& input_shapes) { @@ -225,42 +266,19 @@ void Run(const std::string& model_file, for (size_t tidx = 0; tidx < output_tensor_num; ++tidx) { std::unique_ptr output_tensor = predictor->GetOutput(tidx); out_ss << "\n--- output tensor " << tidx << " ---\n"; - auto out_shape = output_tensor->shape(); - auto out_data = output_tensor->data(); - auto ele_num = lite::ShapeProduction(out_shape); - auto out_mean = lite::compute_mean(out_data, ele_num); - auto out_std_dev = lite::compute_standard_deviation( - out_data, ele_num, true, out_mean); - - out_ss << "output shape(NCHW): " << lite::ShapePrint(out_shape) - << std::endl; - out_ss << "output tensor " << tidx << " elem num: " << ele_num << std::endl; - out_ss << "output tensor " << tidx << " mean value: " << out_mean - << std::endl; - out_ss << "output tensor " << tidx << " standard deviation: " << out_std_dev - << std::endl; - - if (FLAGS_show_output_elem) { - for (int i = 0; i < ele_num; ++i) { - out_ss << "out[" << tidx << "][" << i - << "]:" << output_tensor->data()[i] << std::endl; - } - } - - // TODO(sprouteer): Only support float for now, add more types if needed. - if (!FLAGS_output_data_path.empty()) { - std::stringstream out_data; - auto output_path = lite::Split(FLAGS_output_data_path, ":"); - if (output_path.size() <= tidx) { - std::cerr << "Fail to write output tensor to file, tensor_output_path " - "not matching output tensor number. " - << std::endl; - } else { - for (int i = 0; i < ele_num; ++i) { - out_data << output_tensor->data()[i] << std::endl; - } - StoreOutputTensor(out_data, output_path[tidx]); - } + switch (output_tensor->precision()) { + case PRECISION(kFloat): + outputIdxTensor(output_tensor.get(), out_ss, tidx); + break; + case PRECISION(kInt32): + outputIdxTensor(output_tensor.get(), out_ss, tidx); + break; + case PRECISION(kInt64): + outputIdxTensor(output_tensor.get(), out_ss, tidx); + break; + default: + LOG(FATAL) << "outputIdxTensor unsupported precision type: " + << static_cast(output_tensor->precision()); } } diff --git a/lite/operators/activation_ops.cc b/lite/operators/activation_ops.cc index 1cf89afba07..44b5c9986d9 100644 --- a/lite/operators/activation_ops.cc +++ b/lite/operators/activation_ops.cc @@ -51,7 +51,11 @@ bool ActivationOp::AttachImpl(const cpp::OpDesc& opdesc, lite::Scope* scope) { scope->FindVar(prelu_alpha_name)->GetMutable(); param_.active_type = lite_api::ActivationType::kPRelu; } else if (opdesc.Type() == "swish") { - param_.Swish_beta = opdesc.GetAttr("beta"); + if (opdesc.HasAttr("beta")) { + param_.Swish_beta = opdesc.GetAttr("beta"); + } else { + param_.Swish_beta = 1.0f; + } param_.active_type = lite_api::ActivationType::kSwish; } else if (opdesc.Type() == "hard_sigmoid") { param_.active_type = lite_api::ActivationType::kHardSigmoid;