!27518 [MS][LITE][develop] fix matmul resize and refactor ut

Merge pull request !27518 from sunsuodong/fp16_matmul_resize
This commit is contained in:
i-robot 2021-12-13 09:04:08 +00:00 committed by Gitee
commit 647cde2e82
25 changed files with 387 additions and 420 deletions

View File

@ -147,6 +147,7 @@ set(LITE_SRC
${LITE_DIR}/src/schema_tensor_wrapper.cc
${LITE_DIR}/src/tensorlist.cc
${LITE_DIR}/src/tensor.cc
${LITE_DIR}/src/tensor_category.cc
${LITE_DIR}/src/weight_decoder.cc
${LITE_DIR}/src/huffman_decode.cc
${LITE_DIR}/src/common/log.cc

View File

@ -92,7 +92,7 @@ std::string EnumMicroTensorFormat(mindspore::Format format) {
case mindspore::NC4HW4:
return "Format_NC4HW4";
default:
MS_LOG(ERROR) << "unsupported format: " << schema::EnumNameFormat(static_cast<schema::Format>(format));
MS_LOG(ERROR) << "unsupported format: " << format;
return "Format_NUM_OF_FORMAT";
}
}

View File

@ -106,6 +106,7 @@ set(LITE_SRC
${CMAKE_CURRENT_SOURCE_DIR}/runtime/infer_manager.cc
${CMAKE_CURRENT_SOURCE_DIR}/schema_tensor_wrapper.cc
${CMAKE_CURRENT_SOURCE_DIR}/tensor.cc
${CMAKE_CURRENT_SOURCE_DIR}/tensor_category.cc
${CMAKE_CURRENT_SOURCE_DIR}/ms_tensor.cc
${CMAKE_CURRENT_SOURCE_DIR}/executor.cc
${CMAKE_CURRENT_SOURCE_DIR}/inner_context.cc

View File

@ -18,6 +18,7 @@
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <fstream>
#include <climits>
#include "include/ms_tensor.h"

View File

@ -16,6 +16,7 @@
#include "src/runtime/kernel/arm/base/layout_transform.h"
#include "src/common/log_adapter.h"
#include "schema/ops_types_generated.h"
namespace mindspore::kernel {
LayoutConvertor LayoutTransformFp32(mindspore::Format src_format, mindspore::Format dst_format) {

View File

@ -16,6 +16,7 @@
#include "src/runtime/kernel/arm/fp16/layout_transform_fp16.h"
#include "nnacl/fp16/pack_fp16.h"
#include "src/common/log_adapter.h"
#include "schema/ops_types_generated.h"
namespace mindspore::kernel {
LayoutConvertor LayoutTransformFp16(mindspore::Format src_format, mindspore::Format dst_format) {

View File

@ -55,14 +55,14 @@ int MatmulFp32BaseCPUKernel::InitBufferA() {
if (a_pack_ptr_ != nullptr) {
return RET_OK;
}
if (!op_parameter_->is_train_session_) {
if (vec_matmul_) {
a_pack_ptr_ = reinterpret_cast<float *>(in_tensors().at(0)->data());
if (vec_matmul_) {
a_pack_ptr_ = reinterpret_cast<float *>(in_tensors().at(0)->data());
} else {
if (op_parameter_->is_train_session_) {
a_pack_ptr_ = reinterpret_cast<float *>(workspace());
} else {
a_pack_ptr_ = reinterpret_cast<float *>(ms_context_->allocator->Malloc(matrix_a_pack_size_ * sizeof(float)));
}
} else {
a_pack_ptr_ = reinterpret_cast<float *>(workspace());
}
if (a_pack_ptr_ == nullptr) {
MS_LOG(ERROR) << "malloc a_pack_ptr_ failed";
@ -185,25 +185,17 @@ void MatmulFp32BaseCPUKernel::FreeBiasBuf() {
}
void MatmulFp32BaseCPUKernel::FreeResizeBufA() {
if (!op_parameter_->is_train_session_ && !vec_matmul_) {
if (a_pack_ptr_ != nullptr) {
ms_context_->allocator->Free(a_pack_ptr_);
a_pack_ptr_ = nullptr;
}
} else {
a_pack_ptr_ = nullptr;
if (!vec_matmul_ && !op_parameter_->is_train_session_ && a_pack_ptr_ != nullptr) {
ms_context_->allocator->Free(a_pack_ptr_);
}
a_pack_ptr_ = nullptr;
}
void MatmulFp32BaseCPUKernel::FreeResizeBufB() {
if (!op_parameter_->is_train_session_) {
if (b_pack_ptr_ != nullptr) {
ms_context_->allocator->Free(b_pack_ptr_);
b_pack_ptr_ = nullptr;
}
} else {
b_pack_ptr_ = nullptr;
if (!op_parameter_->is_train_session_ && b_pack_ptr_ != nullptr) {
ms_context_->allocator->Free(b_pack_ptr_);
}
b_pack_ptr_ = nullptr;
}
int MatmulFp32BaseCPUKernel::ParallelRunByBatch(int task_id) const {

View File

@ -19,6 +19,7 @@
#include <string>
#include <utility>
#include <algorithm>
#include "schema/ops_types_generated.h"
#include "securec/include/securec.h"
#include "include/errorcode.h"

View File

@ -28,7 +28,6 @@
#include "include/api/format.h"
#include "src/runtime/inner_allocator.h"
#include "src/common/log_adapter.h"
#include "schema/model_generated.h"
#include "src/common/utils.h"
#include "src/tensor_category.h"

View File

@ -0,0 +1,34 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/tensor_category.h"
#include "src/common/utils.h"
#include "schema/model_generated.h"
namespace mindspore {
namespace lite {
Category TensorCategory(const int node_type, const size_t shape_num, const TypeId data_type, const size_t data_size) {
return (node_type == NodeType_ValueNode)
? (shape_num == 0 && data_size == DataTypeSize(data_type) ? Category::CONST_SCALAR : Category::CONST_TENSOR)
: Category::VAR;
}
Category TensorCategory(const schema::Tensor &tensor) {
auto shape_num = tensor.dims() == nullptr ? 0 : tensor.dims()->size();
auto data_size = tensor.data() == nullptr ? 0 : tensor.data()->size();
return TensorCategory(tensor.nodeType(), shape_num, TypeId(tensor.dataType()), data_size);
}
} // namespace lite
} // namespace mindspore

View File

@ -13,14 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_TENSOR_CATEGORY_H_
#define MINDSPORE_LITE_SRC_TENSOR_CATEGORY_H_
#include "src/common/utils.h"
#include "schema/model_generated.h"
#include <cstddef>
#include "ir/dtype/type_id.h"
namespace mindspore {
namespace schema {
struct Tensor;
}
namespace lite {
enum Category {
CONST_TENSOR, // weight tensor
@ -30,18 +32,8 @@ enum Category {
GRAPH_OUTPUT,
};
inline Category TensorCategory(const int node_type, const size_t shape_num, const TypeId data_type,
const size_t data_size) {
return (node_type == NodeType_ValueNode)
? (shape_num == 0 && data_size == DataTypeSize(data_type) ? Category::CONST_SCALAR : Category::CONST_TENSOR)
: Category::VAR;
}
inline Category TensorCategory(const schema::Tensor &tensor) {
auto shape_num = tensor.dims() == nullptr ? 0 : tensor.dims()->size();
auto data_size = tensor.data() == nullptr ? 0 : tensor.data()->size();
return TensorCategory(tensor.nodeType(), shape_num, TypeId(tensor.dataType()), data_size);
}
Category TensorCategory(const int node_type, const size_t shape_num, const TypeId data_type, const size_t data_size);
Category TensorCategory(const schema::Tensor &tensor);
} // namespace lite
} // namespace mindspore
#endif // MINDSPORE_LITE_SRC_TENSOR_CATEGORY_H_

View File

@ -14,7 +14,9 @@
* limitations under the License.
*/
#include "common/common_test.h"
#include "src/common/file_utils.h"
#include "src/common/log_adapter.h"
#include "src/tensor.h"
#ifdef __cplusplus
#if __cplusplus
@ -23,15 +25,82 @@ extern "C" {
#endif
namespace mindspore {
void CommonTest::SetUpTestCase() {}
void CommonTest::TearDownTestCase() {}
void CommonTest::SetUp() {}
void CommonTest::TearDown() {}
lite::Tensor *CommonTest::CreateTensor(TypeId dtype, std::vector<int> shape, const Format &format,
lite::Category category) {
return new (std::nothrow) lite::Tensor(dtype, shape, format, category);
}
void CommonTest::DestroyTensors(std::vector<lite::Tensor *> tensors) {
for (auto &tensor : tensors) {
delete tensor;
}
}
void CommonTest::CompareOutputInt8(int8_t *output_data, int8_t *correct_data, int size, float err_percent) {
int bias_count = 0;
for (int i = 0; i < size; i++) {
int8_t diff = abs(output_data[i] - correct_data[i]);
ASSERT_LE(diff, 1);
if (diff == 1) {
bias_count++;
}
}
float bias_percent = static_cast<float>(bias_count) / static_cast<float>(size);
ASSERT_LE(bias_percent, err_percent);
}
int CommonTest::CompareOutput(const float *output_data, size_t output_num, const std::string &file_path) {
size_t ground_truth_size = 0;
auto ground_truth = reinterpret_cast<float *>(lite::ReadFile(file_path.c_str(), &ground_truth_size));
size_t ground_truth_num = ground_truth_size / sizeof(float);
printf("ground truth num : %zu\n", ground_truth_num);
int res = CompareOutputData(output_data, ground_truth, ground_truth_num);
delete[] ground_truth;
return res;
}
float CommonTest::CompareOutputRelativeData(const float *output_data, const float *correct_data, int data_size) {
float error = 0;
// relative error
float diffSum = 0.0f;
float sum = 0.0f;
for (int i = 0; i < data_size; i++) {
sum += std::abs(correct_data[i]);
}
for (int i = 0; i < data_size; i++) {
float diff = std::abs(output_data[i] - correct_data[i]);
diffSum += diff;
}
error = diffSum / sum;
return error;
}
int CommonTest::CompareRelativeOutput(const float *output_data, const std::string &file_path) {
size_t output_size;
auto ground_truth = reinterpret_cast<float *>(mindspore::lite::ReadFile(file_path.c_str(), &output_size));
if (ground_truth == nullptr) {
return 1;
}
size_t output_num = output_size / sizeof(float);
float error = CompareOutputRelativeData(output_data, ground_truth, output_num);
delete[] ground_truth;
if (error > 1e-4) {
return 1;
}
return 0;
}
void *CommonTest::TensorMutabData(lite::Tensor *tensor) { return tensor->MutableData(); }
size_t CommonTest::TensorSize(lite::Tensor *tensor) { return tensor->Size(); }
int CommonTest::TensorMallocData(lite::Tensor *tensor) { return tensor->MallocData(); }
} // namespace mindspore
#ifdef __cplusplus

View File

@ -23,9 +23,13 @@
#include <algorithm>
#include <vector>
#include "gtest/gtest.h"
#include "src/common/file_utils.h"
#include "include/api/format.h"
#include "src/tensor_category.h"
namespace mindspore {
namespace lite {
class Tensor;
}
class CommonTest : public testing::Test {
public:
// TestCase only enter once
@ -42,6 +46,22 @@ class CommonTest : public testing::Test {
}
}
template <typename T>
lite::Tensor *CreateTensor(TypeId dtype, std::vector<int> shape, std::vector<T> data, const Format &format = NHWC,
lite::Category category = lite::Category::VAR) {
auto tensor = CreateTensor(dtype, shape, format, category);
if (tensor != nullptr) {
if (!data.empty()) {
memcpy(TensorMutabData(tensor), data.data(), TensorSize(tensor));
} else {
(void)TensorMallocData(tensor);
}
}
return tensor;
}
void DestroyTensors(std::vector<lite::Tensor *> tensors);
template <typename T>
void PrintData(const std::string &name, T *output_data, int size) {
std::cout << "The " << name << " is as follows:" << std::endl;
@ -67,87 +87,20 @@ class CommonTest : public testing::Test {
return 0;
}
static void CompareOutputInt8(int8_t *output_data, int8_t *correct_data, int size, float err_percent) {
int bias_count = 0;
for (int i = 0; i < size; i++) {
int8_t diff = abs(output_data[i] - correct_data[i]);
ASSERT_LE(diff, 1);
if (diff == 1) {
bias_count++;
}
}
float bias_percent = static_cast<float>(bias_count) / static_cast<float>(size);
ASSERT_LE(bias_percent, err_percent);
}
void CompareOutputInt8(int8_t *output_data, int8_t *correct_data, int size, float err_percent);
static int CompareOutput(const float *output_data, size_t output_num, const std::string &file_path) {
size_t ground_truth_size = 0;
auto ground_truth = reinterpret_cast<float *>(lite::ReadFile(file_path.c_str(), &ground_truth_size));
size_t ground_truth_num = ground_truth_size / sizeof(float);
printf("ground truth num : %zu\n", ground_truth_num);
int res = CompareOutputData(output_data, ground_truth, ground_truth_num);
delete[] ground_truth;
return res;
}
int CompareOutput(const float *output_data, size_t output_num, const std::string &file_path);
static float CompareOutputRelativeData(const float *output_data, const float *correct_data, int data_size) {
float error = 0;
float CompareOutputRelativeData(const float *output_data, const float *correct_data, int data_size);
// relative error
float diffSum = 0.0f;
float sum = 0.0f;
for (int i = 0; i < data_size; i++) {
sum += std::abs(correct_data[i]);
}
for (int i = 0; i < data_size; i++) {
float diff = std::abs(output_data[i] - correct_data[i]);
diffSum += diff;
}
error = diffSum / sum;
return error;
}
int CompareRelativeOutput(const float *output_data, const std::string &file_path);
static int CompareRelativeOutput(const float *output_data, const std::string &file_path) {
size_t output_size;
auto ground_truth = reinterpret_cast<float *>(mindspore::lite::ReadFile(file_path.c_str(), &output_size));
if (ground_truth == nullptr) {
return 1;
}
size_t output_num = output_size / sizeof(float);
float error = CompareOutputRelativeData(output_data, ground_truth, output_num);
delete[] ground_truth;
if (error > 1e-4) {
return 1;
}
return 0;
}
static float RelativeOutputError(const float *output_data, const std::string &file_path) {
size_t output_size = 0;
auto ground_truth = reinterpret_cast<float *>(mindspore::lite::ReadFile(file_path.c_str(), &output_size));
size_t output_num = output_size / sizeof(float);
float error = CompareOutputRelativeData(output_data, ground_truth, output_num);
delete[] ground_truth;
return error;
}
static void ReadFile(const char *file, size_t *size, char **buf) {
ASSERT_NE(nullptr, file);
ASSERT_NE(nullptr, size);
ASSERT_NE(nullptr, buf);
std::string path = std::string(file);
std::ifstream ifs(path);
ASSERT_EQ(true, ifs.good());
ASSERT_EQ(true, ifs.is_open());
ifs.seekg(0, std::ios::end);
*size = ifs.tellg();
*buf = new char[*size];
ifs.seekg(0, std::ios::beg);
ifs.read(*buf, *size);
ifs.close();
}
private:
lite::Tensor *CreateTensor(TypeId dtype, std::vector<int> shape, const Format &format = NHWC,
lite::Category category = lite::Category::VAR);
void *TensorMutabData(lite::Tensor *tensor);
size_t TensorSize(lite::Tensor *tensor);
int TensorMallocData(lite::Tensor *tensor);
};
} // namespace mindspore
#endif // MINDSPORE_LITE_TEST_COMMON_COMMON_TEST_H_

View File

@ -17,6 +17,7 @@
#include <string>
#include "common/common_test.h"
#include "tools/benchmark/run_benchmark.h"
#include "include/errorcode.h"
namespace mindspore {
namespace lite {

View File

@ -21,6 +21,7 @@
#include "include/api/types.h"
#include "include/api/delegate.h"
#include "include/api/model.h"
#include "src/common/file_utils.h"
namespace mindspore {
class DelegateTest : public mindspore::CommonTest {

View File

@ -20,6 +20,7 @@
#include "tools/converter/converter.h"
#include "src/lite_session.h"
#include "src/lite_kernel.h"
#include "src/common/file_utils.h"
#include "include/api/types.h"
#include "include/api/graph.h"
#include "include/api/model.h"

View File

@ -22,6 +22,7 @@
#include "src/mindrt_executor.h"
#include "src/lite_session.h"
#include "src/lite_kernel.h"
#include "src/common/file_utils.h"
namespace mindspore {
class MindrtParallelTest : public mindspore::CommonTest {

View File

@ -19,9 +19,6 @@
#include "include/errorcode.h"
#include "src/common/config_file.h"
#include "schema/inner/model_generated.h"
#include "schema/inner/ops_generated.h"
#include "schema/ops_generated.h"
#include "schema/model_generated.h"
#include "src/lite_kernel.h"
#include "src/lite_session.h"
#include "include/api/model.h"

View File

@ -24,6 +24,7 @@
#include "include/model.h"
#include "include/errorcode.h"
#include "src/common/log_adapter.h"
#include "src/common/file_utils.h"
#include "src/lite_session.h"
#include "tools/common/meta_graph_serializer.h"
#include "include/version.h"

View File

@ -24,6 +24,7 @@
#include "include/errorcode.h"
#include "src/common/log_adapter.h"
#include "src/lite_session.h"
#include "src/common/file_utils.h"
namespace mindspore {
class InferTest : public mindspore::CommonTest {
@ -68,18 +69,14 @@ TEST_F(InferTest, TestConvNode) {
weight->dataType = TypeId::kNumberTypeFloat32;
weight->dims = {32, 3, 3, 3};
auto buf = new char *[1];
//================================================================
size_t weight_size;
std::string weight_path = "./test_data/conv/convfp32_weight_32_3_3_3.bin";
ReadFile(weight_path.c_str(), &weight_size, buf);
ASSERT_NE(nullptr, buf[0]);
auto weight_data_temp = reinterpret_cast<float *>(buf[0]);
ASSERT_NE(nullptr, weight_data_temp);
size_t weight_size = 0;
char *buff = lite::ReadFile("./test_data/conv/convfp32_weight_32_3_3_3.bin", &weight_size);
ASSERT_NE(nullptr, buff);
weight->data.resize(sizeof(float) * 32 * 3 * 3 * 3);
//================================================================
memcpy(weight->data.data(), weight_data_temp, weight_size);
memcpy(weight->data.data(), buff, weight_size);
weight->offset = -1;
meta_graph->allTensors.emplace_back(std::move(weight));
@ -118,14 +115,12 @@ TEST_F(InferTest, TestConvNode) {
auto data = inTensor->MutableData();
//===================================================
size_t input_size;
std::string input_path = "./test_data/conv/convfp32_input_1_28_28_3.bin";
ReadFile(input_path.c_str(), &input_size, buf);
ASSERT_NE(nullptr, buf[0]);
auto input_data = reinterpret_cast<float *>(buf[0]);
ASSERT_NE(nullptr, input_data);
buff = lite::ReadFile("./test_data/conv/convfp32_input_1_28_28_3.bin", &input_size);
ASSERT_NE(nullptr, buff);
//===================================================
ASSERT_EQ(input_size, inTensor->Size());
memcpy(data, input_data, input_size);
memcpy(data, buff, input_size);
ret = session->RunGraph();
ASSERT_EQ(lite::RET_OK, ret);
auto outputs = session->GetOutputs();
@ -138,11 +133,9 @@ TEST_F(InferTest, TestConvNode) {
ASSERT_NE(nullptr, outData);
//===================================================
size_t output_size;
std::string output_path = "./test_data/conv/convfp32_out_1_28_28_32.bin";
ReadFile(output_path.c_str(), &output_size, buf);
ASSERT_NE(nullptr, buf[0]);
auto output_data = reinterpret_cast<float *>(buf[0]);
ASSERT_NE(nullptr, output_data);
buff = lite::ReadFile("./test_data/conv/convfp32_out_1_28_28_32.bin", &output_size);
ASSERT_NE(nullptr, buff);
auto output_data = reinterpret_cast<float *>(buff);
//===================================================
ASSERT_EQ(output_size, outTensor->Size());
for (int i = 0; i < outTensor->ElementsNum(); i++) {
@ -233,15 +226,12 @@ TEST_F(InferTest, TestAddNode) {
}
TEST_F(InferTest, TestModel) {
auto buf = new char *[1];
size_t model_size;
std::string model_path = "./models/model_hebing_3branch.ms";
ReadFile(model_path.c_str(), &model_size, buf);
ASSERT_NE(nullptr, buf[0]);
auto model = lite::Model::Import(buf[0], model_size);
char *buff = lite::ReadFile("./models/model_hebing_3branch.ms", &model_size);
ASSERT_NE(nullptr, buff);
auto model = lite::Model::Import(buff, model_size);
ASSERT_NE(nullptr, model);
delete[] buf[0];
delete[] buff;
auto context = new lite::InnerContext;
context->device_list_[0].device_info_.cpu_device_info_.cpu_bind_mode_ = lite::NO_BIND;
context->thread_num_ = 4;

View File

@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/time.h>
#include <iostream>
#include <memory>
@ -24,6 +23,7 @@
#include "src/common/log_adapter.h"
#include "src/runtime/kernel/arm/fp32/fullconnection_fp32.h"
#include "src/runtime/infer_manager.h"
#include "src/kernel_registry.h"
namespace mindspore {
using mindspore::lite::Tensor;
@ -33,249 +33,168 @@ class TestFcFp32 : public mindspore::CommonTest {
TestFcFp32() {}
};
int FcTestInit1(std::vector<lite::Tensor *> *inputs_, std::vector<lite::Tensor *> *outputs_,
MatMulParameter *matmal_param, float **correct) {
auto *in_t = new Tensor(kNumberTypeFloat, {2, 2, 2, 2}, mindspore::NHWC, lite::Category::CONST_TENSOR);
in_t->MallocData();
float in[] = {-3.2366564, -4.7733846, -7.8329225, 16.146885, 5.060793, -6.1471, -1.7680453, -6.5721383,
17.87506, -5.1192183, 10.742863, 1.4536934, 19.693445, 19.45783, 5.063163, 0.5234792};
memcpy(in_t->MutableData(), in, sizeof(float) * in_t->ElementsNum());
inputs_->push_back(in_t);
auto *weight_t = new Tensor(kNumberTypeFloat, {3, 8}, mindspore::NHWC, lite::Category::CONST_TENSOR);
weight_t->MallocData();
float weight[] = {-0.0024438887, 0.0006738146, -0.008169129, 0.0021510671, -0.012470592, -0.0053063435,
0.006050155, 0.008656233, 0.012911413, -0.0028635843, -0.00034080597, -0.0010622552,
-0.012254699, -0.01312836, 0.0025241964, -0.004706142, 0.002451482, -0.009558459,
0.004481974, 0.0033251503, -0.011705584, -0.001720293, -0.0039410214, -0.0073637343};
memcpy(weight_t->MutableData(), weight, sizeof(float) * weight_t->ElementsNum());
inputs_->push_back(weight_t);
auto *bias_t = new Tensor(kNumberTypeFloat, {3}, mindspore::NHWC, lite::Category::CONST_TENSOR);
bias_t->MallocData();
float bias[] = {1.6103756, -0.9872417, 0.546849};
memcpy(bias_t->MutableData(), bias, sizeof(float) * bias_t->ElementsNum());
inputs_->push_back(bias_t);
auto *out_t = new Tensor(kNumberTypeFloat, {2, 3}, mindspore::NHWC, lite::Category::CONST_TENSOR);
out_t->MallocData();
outputs_->push_back(out_t);
*correct = reinterpret_cast<float *>(malloc(out_t->ElementsNum() * sizeof(float)));
float nchw_co[] = {1.6157111, -0.98469573, 0.6098231, 1.1649342, -1.2334653, 0.404779};
memcpy(*correct, nchw_co, out_t->ElementsNum() * sizeof(float));
matmal_param->b_transpose_ = true;
matmal_param->a_transpose_ = false;
matmal_param->has_bias_ = true;
matmal_param->act_type_ = ActType_No;
matmal_param->op_parameter_.type_ = 67;
matmal_param->op_parameter_.is_train_session_ = false;
KernelInferShape(*inputs_, *outputs_, reinterpret_cast<OpParameter *>(matmal_param));
return out_t->ElementsNum();
}
TEST_F(TestFcFp32, FcTest1) {
std::vector<lite::Tensor *> inputs_;
std::vector<lite::Tensor *> outputs_;
auto matmul_param = new MatMulParameter();
float *correct;
int total_size = FcTestInit1(&inputs_, &outputs_, matmul_param, &correct);
auto *ctx = new lite::InnerContext;
std::vector<lite::Tensor *> inputs;
std::vector<float> in = {-3.2366564, -4.7733846, -7.8329225, 16.146885, 5.060793, -6.1471, -1.7680453, -6.5721383,
17.87506, -5.1192183, 10.742863, 1.4536934, 19.693445, 19.45783, 5.063163, 0.5234792};
inputs.push_back(
CreateTensor<float>(kNumberTypeFloat32, {2, 2, 2, 2}, in, mindspore::NHWC, lite::Category::CONST_TENSOR));
std::vector<float> weight = {-0.0024438887, 0.0006738146, -0.008169129, 0.0021510671, -0.012470592, -0.0053063435,
0.006050155, 0.008656233, 0.012911413, -0.0028635843, -0.00034080597, -0.0010622552,
-0.012254699, -0.01312836, 0.0025241964, -0.004706142, 0.002451482, -0.009558459,
0.004481974, 0.0033251503, -0.011705584, -0.001720293, -0.0039410214, -0.0073637343};
inputs.push_back(
CreateTensor<float>(kNumberTypeFloat32, {3, 8}, weight, mindspore::NHWC, lite::Category::CONST_TENSOR));
inputs.push_back(CreateTensor<float>(kNumberTypeFloat32, {3}, {1.6103756, -0.9872417, 0.546849}, mindspore::NHWC,
lite::Category::CONST_TENSOR));
std::vector<lite::Tensor *> outputs;
outputs.push_back(CreateTensor<float>(kNumberTypeFloat32, {2, 3}, {}));
auto param = static_cast<MatMulParameter *>(malloc(sizeof(MatMulParameter)));
memset(param, 0, sizeof(MatMulParameter));
param->b_transpose_ = true;
param->a_transpose_ = false;
param->has_bias_ = true;
param->act_type_ = ActType_No;
param->op_parameter_.type_ = 67;
param->op_parameter_.is_train_session_ = false;
KernelInferShape(inputs, outputs, reinterpret_cast<OpParameter *>(param));
auto ctx = std::make_shared<lite::InnerContext>();
ctx->thread_num_ = 2;
matmul_param->op_parameter_.thread_num_ = 2;
ASSERT_EQ(lite::RET_OK, ctx->Init());
auto *fc = new kernel::FullconnectionCPUKernel(reinterpret_cast<OpParameter *>(matmul_param), inputs_, outputs_, ctx);
fc->Prepare();
ASSERT_EQ(ctx->Init(), RET_OK);
param->op_parameter_.thread_num_ = ctx->thread_num_;
kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_FullConnection};
auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc);
ASSERT_NE(creator, nullptr);
auto *kernel = creator(inputs, outputs, reinterpret_cast<OpParameter *>(param), ctx.get(), desc);
ASSERT_NE(kernel, nullptr);
ASSERT_EQ(kernel->Prepare(), RET_OK);
#ifdef SUPPORT_TRAIN
fc->AllocWorkspace();
kernel->AllocWorkspace();
#endif
fc->Run();
ASSERT_EQ(0, CompareOutputData(reinterpret_cast<float *>(outputs_[0]->MutableData()), correct, total_size, 0.0001));
delete fc;
delete ctx;
for (unsigned int i = 0; i < inputs_.size(); i++) {
delete inputs_[i];
}
for (unsigned int i = 0; i < outputs_.size(); i++) {
delete outputs_[i];
}
}
ASSERT_EQ(kernel->Run(), RET_OK);
int FcTestInit2(std::vector<lite::Tensor *> *inputs_, std::vector<lite::Tensor *> *outputs_,
MatMulParameter *matmal_param, float **correct) {
size_t buffer_size;
auto *in_t = new Tensor(kNumberTypeFloat, {20, 4, 2, 10}, mindspore::NCHW, lite::Category::CONST_TENSOR);
in_t->MallocData();
std::string in_path = "./matmul/FcFp32_input1.bin";
auto in_data = mindspore::lite::ReadFile(in_path.c_str(), &buffer_size);
memcpy(in_t->MutableData(), in_data, buffer_size);
inputs_->push_back(in_t);
auto *weight_t = new Tensor(kNumberTypeFloat, {30, 80}, mindspore::NCHW, lite::Category::CONST_TENSOR);
weight_t->MallocData();
std::string weight_path = "./matmul/FcFp32_weight1.bin";
auto w_data = mindspore::lite::ReadFile(weight_path.c_str(), &buffer_size);
memcpy(weight_t->MutableData(), w_data, buffer_size);
inputs_->push_back(weight_t);
auto *bias_t = new Tensor(kNumberTypeFloat, {30}, mindspore::NCHW, lite::Category::CONST_TENSOR);
bias_t->MallocData();
std::string bias_path = "./matmul/FcFp32_bias1.bin";
auto bias_data = mindspore::lite::ReadFile(bias_path.c_str(), &buffer_size);
memcpy(bias_t->MutableData(), bias_data, buffer_size);
inputs_->push_back(bias_t);
auto *out_t = new Tensor(kNumberTypeFloat, {20, 30}, mindspore::NCHW, lite::Category::CONST_TENSOR);
out_t->MallocData();
outputs_->push_back(out_t);
*correct = reinterpret_cast<float *>(malloc(out_t->ElementsNum() * sizeof(float)));
std::string out_path = "./matmul/FcFp32_output1.bin";
auto out_data = mindspore::lite::ReadFile(out_path.c_str(), &buffer_size);
memcpy(*correct, out_data, out_t->ElementsNum() * sizeof(float));
matmal_param->b_transpose_ = true;
matmal_param->a_transpose_ = false;
matmal_param->has_bias_ = true;
matmal_param->act_type_ = ActType_No;
matmal_param->op_parameter_.type_ = 67;
KernelInferShape(*inputs_, *outputs_, reinterpret_cast<OpParameter *>(matmal_param));
return out_t->ElementsNum();
std::vector<float> except_result = {1.6157111, -0.98469573, 0.6098231, 1.1649342, -1.2334653, 0.404779};
ASSERT_EQ(0, CompareOutputData(static_cast<float *>(outputs[0]->data()), except_result.data(),
outputs[0]->ElementsNum(), 0.000001));
delete kernel;
DestroyTensors(inputs);
DestroyTensors(outputs);
}
TEST_F(TestFcFp32, FcTest2) {
std::vector<lite::Tensor *> inputs_;
std::vector<lite::Tensor *> outputs_;
auto matmul_param = new MatMulParameter();
float *correct;
int total_size = FcTestInit2(&inputs_, &outputs_, matmul_param, &correct);
auto *ctx = new lite::InnerContext;
std::vector<lite::Tensor *> inputs;
size_t buffer_size;
auto in_data = mindspore::lite::ReadFile("./matmul/FcFp32_input1.bin", &buffer_size);
std::vector<char> in(in_data, in_data + buffer_size);
inputs.push_back(
CreateTensor<char>(kNumberTypeFloat32, {20, 4, 2, 10}, in, mindspore::NCHW, lite::Category::CONST_TENSOR));
auto w_data = mindspore::lite::ReadFile("./matmul/FcFp32_weight1.bin", &buffer_size);
std::vector<char> weight(w_data, w_data + buffer_size);
inputs.push_back(
CreateTensor<char>(kNumberTypeFloat32, {30, 80}, weight, mindspore::NCHW, lite::Category::CONST_TENSOR));
auto bias_data = mindspore::lite::ReadFile("./matmul/FcFp32_bias1.bin", &buffer_size);
std::vector<char> bias(bias_data, bias_data + buffer_size);
inputs.push_back(CreateTensor<char>(kNumberTypeFloat32, {30}, bias, mindspore::NCHW, lite::Category::CONST_TENSOR));
std::vector<lite::Tensor *> outputs;
outputs.push_back(CreateTensor<float>(kNumberTypeFloat32, {20, 30}, {}));
auto param = static_cast<MatMulParameter *>(malloc(sizeof(MatMulParameter)));
memset(param, 0, sizeof(MatMulParameter));
param->b_transpose_ = true;
param->a_transpose_ = false;
param->has_bias_ = true;
param->act_type_ = ActType_No;
param->op_parameter_.type_ = 67;
KernelInferShape(inputs, outputs, reinterpret_cast<OpParameter *>(param));
auto ctx = std::make_shared<lite::InnerContext>();
ctx->thread_num_ = 1;
matmul_param->op_parameter_.thread_num_ = 1;
ASSERT_EQ(lite::RET_OK, ctx->Init());
auto *fc = new kernel::FullconnectionCPUKernel(reinterpret_cast<OpParameter *>(matmul_param), inputs_, outputs_, ctx);
fc->Prepare();
ASSERT_EQ(ctx->Init(), RET_OK);
param->op_parameter_.thread_num_ = ctx->thread_num_;
kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_FullConnection};
auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc);
ASSERT_NE(creator, nullptr);
auto *kernel = creator(inputs, outputs, reinterpret_cast<OpParameter *>(param), ctx.get(), desc);
ASSERT_NE(kernel, nullptr);
ASSERT_EQ(kernel->Prepare(), RET_OK);
#ifdef SUPPORT_TRAIN
fc->AllocWorkspace();
kernel->AllocWorkspace();
#endif
fc->Run();
ASSERT_EQ(0, CompareOutputData(reinterpret_cast<float *>(outputs_[0]->MutableData()), correct, total_size, 0.0001));
for (unsigned int i = 0; i < inputs_.size(); i++) {
delete inputs_[i];
}
for (unsigned int i = 0; i < outputs_.size(); i++) {
delete outputs_[i];
}
delete fc;
delete ctx;
}
ASSERT_EQ(kernel->Run(), RET_OK);
void FcTestInit3(std::vector<lite::Tensor *> *inputs_, std::vector<lite::Tensor *> *outputs_,
MatMulParameter *matmal_param, float **correct) {
auto *in_t = new Tensor(kNumberTypeFloat, {1, 1, 1, 20}, mindspore::NHWC, lite::Category::CONST_TENSOR);
in_t->MallocData();
float in[] = {1, 0, 3, 0, 4, 5, 2, 5, 2, 5, 1, 5, 0, 1, 2, 0, 2, 1, 0, 5};
memcpy(in_t->MutableData(), in, sizeof(float) * in_t->ElementsNum());
inputs_->push_back(in_t);
auto *weight_t = new Tensor(kNumberTypeFloat, {16, 20}, mindspore::NHWC, lite::Category::CONST_TENSOR);
weight_t->MallocData();
float weight[] = {0, 5, 5, 3, 0, 5, 3, 1, 0, 1, 3, 0, 5, 5, 2, 4, 0, 1, 1, 2, 3, 0, 5, 5, 4, 4, 1, 4, 1, 1, 5, 3,
3, 1, 0, 3, 1, 2, 4, 5, 3, 4, 4, 0, 3, 5, 0, 3, 4, 1, 0, 1, 3, 4, 0, 5, 2, 5, 0, 4, 2, 2, 2, 2,
4, 4, 5, 2, 1, 1, 5, 1, 4, 4, 5, 1, 2, 4, 0, 3, 1, 1, 0, 2, 1, 5, 2, 0, 1, 1, 5, 5, 4, 0, 0, 4,
2, 3, 2, 1, 4, 0, 5, 0, 2, 3, 1, 2, 1, 2, 1, 4, 2, 3, 5, 5, 4, 5, 2, 0, 3, 0, 2, 0, 1, 3, 0, 4,
1, 5, 2, 5, 4, 2, 5, 1, 4, 5, 3, 1, 0, 4, 4, 4, 1, 3, 4, 2, 2, 4, 1, 4, 0, 1, 0, 2, 4, 5, 2, 1,
0, 3, 5, 2, 4, 2, 1, 4, 2, 0, 1, 0, 2, 3, 0, 3, 2, 5, 5, 4, 3, 0, 0, 2, 0, 1, 5, 2, 2, 1, 3, 0,
3, 0, 5, 3, 3, 3, 5, 5, 3, 4, 0, 1, 2, 1, 2, 4, 3, 5, 4, 3, 0, 0, 4, 4, 2, 3, 5, 4, 3, 5, 1, 2,
1, 5, 0, 5, 1, 1, 5, 5, 0, 0, 1, 3, 2, 2, 2, 3, 4, 2, 2, 3, 2, 4, 3, 0, 2, 0, 3, 2, 1, 5, 2, 4,
4, 5, 2, 5, 0, 5, 3, 3, 0, 3, 2, 5, 5, 1, 1, 0, 2, 3, 0, 1, 1, 2, 4, 1, 3, 3, 5, 5, 0, 1, 0, 0,
1, 2, 3, 3, 5, 2, 2, 5, 1, 4, 3, 3, 0, 2, 5, 4, 3, 1, 2, 4, 0, 2, 1, 3, 1, 2, 1, 0, 5, 5, 4, 5};
memcpy(weight_t->MutableData(), weight, sizeof(float) * weight_t->ElementsNum());
inputs_->push_back(weight_t);
auto *out_t = new Tensor(kNumberTypeFloat, {1, 16}, mindspore::NHWC, lite::Category::CONST_TENSOR);
out_t->MallocData();
outputs_->push_back(out_t);
matmal_param->b_transpose_ = true;
matmal_param->a_transpose_ = false;
matmal_param->has_bias_ = false;
matmal_param->act_type_ = ActType_No;
auto out_data = mindspore::lite::ReadFile("./matmul/FcFp32_output1.bin", &buffer_size);
std::vector<char> except_result(out_data, out_data + buffer_size);
ASSERT_EQ(0, CompareOutputData(static_cast<float *>(outputs[0]->data()),
reinterpret_cast<float *>(except_result.data()), outputs[0]->ElementsNum(), 0.000001));
delete kernel;
DestroyTensors(inputs);
DestroyTensors(outputs);
}
TEST_F(TestFcFp32, FcTest3) {
std::vector<lite::Tensor *> inputs_;
std::vector<lite::Tensor *> outputs_;
auto matmul_param = new MatMulParameter();
float *correct;
FcTestInit3(&inputs_, &outputs_, matmul_param, &correct);
auto *ctx = new lite::InnerContext;
ctx->thread_num_ = 1;
matmul_param->op_parameter_.thread_num_ = 1;
ASSERT_EQ(lite::RET_OK, ctx->Init());
auto *fc = new kernel::FullconnectionCPUKernel(reinterpret_cast<OpParameter *>(matmul_param), inputs_, outputs_, ctx);
fc->Prepare();
std::vector<lite::Tensor *> inputs;
std::vector<float> in = {1, 0, 3, 0, 4, 5, 2, 5, 2, 5, 1, 5, 0, 1, 2, 0, 2, 1, 0, 5};
inputs.push_back(
CreateTensor<float>(kNumberTypeFloat32, {1, 1, 1, 20}, in, mindspore::NHWC, lite::Category::CONST_TENSOR));
std::vector<float> weight = {
0, 5, 5, 3, 0, 5, 3, 1, 0, 1, 3, 0, 5, 5, 2, 4, 0, 1, 1, 2, 3, 0, 5, 5, 4, 4, 1, 4, 1, 1, 5, 3, 3, 1, 0, 3,
1, 2, 4, 5, 3, 4, 4, 0, 3, 5, 0, 3, 4, 1, 0, 1, 3, 4, 0, 5, 2, 5, 0, 4, 2, 2, 2, 2, 4, 4, 5, 2, 1, 1, 5, 1,
4, 4, 5, 1, 2, 4, 0, 3, 1, 1, 0, 2, 1, 5, 2, 0, 1, 1, 5, 5, 4, 0, 0, 4, 2, 3, 2, 1, 4, 0, 5, 0, 2, 3, 1, 2,
1, 2, 1, 4, 2, 3, 5, 5, 4, 5, 2, 0, 3, 0, 2, 0, 1, 3, 0, 4, 1, 5, 2, 5, 4, 2, 5, 1, 4, 5, 3, 1, 0, 4, 4, 4,
1, 3, 4, 2, 2, 4, 1, 4, 0, 1, 0, 2, 4, 5, 2, 1, 0, 3, 5, 2, 4, 2, 1, 4, 2, 0, 1, 0, 2, 3, 0, 3, 2, 5, 5, 4,
3, 0, 0, 2, 0, 1, 5, 2, 2, 1, 3, 0, 3, 0, 5, 3, 3, 3, 5, 5, 3, 4, 0, 1, 2, 1, 2, 4, 3, 5, 4, 3, 0, 0, 4, 4,
2, 3, 5, 4, 3, 5, 1, 2, 1, 5, 0, 5, 1, 1, 5, 5, 0, 0, 1, 3, 2, 2, 2, 3, 4, 2, 2, 3, 2, 4, 3, 0, 2, 0, 3, 2,
1, 5, 2, 4, 4, 5, 2, 5, 0, 5, 3, 3, 0, 3, 2, 5, 5, 1, 1, 0, 2, 3, 0, 1, 1, 2, 4, 1, 3, 3, 5, 5, 0, 1, 0, 0,
1, 2, 3, 3, 5, 2, 2, 5, 1, 4, 3, 3, 0, 2, 5, 4, 3, 1, 2, 4, 0, 2, 1, 3, 1, 2, 1, 0, 5, 5, 4, 5};
inputs.push_back(
CreateTensor<float>(kNumberTypeFloat32, {16, 20}, weight, mindspore::NHWC, lite::Category::CONST_TENSOR));
std::vector<lite::Tensor *> outputs;
outputs.push_back(CreateTensor<float>(kNumberTypeFloat32, {1, 16}, {}));
auto param = static_cast<MatMulParameter *>(malloc(sizeof(MatMulParameter)));
memset(param, 0, sizeof(MatMulParameter));
param->b_transpose_ = true;
param->a_transpose_ = false;
param->has_bias_ = false;
param->act_type_ = ActType_No;
param->op_parameter_.type_ = 67;
KernelInferShape(inputs, outputs, reinterpret_cast<OpParameter *>(param));
auto ctx = std::make_shared<lite::InnerContext>();
ctx->thread_num_ = 2;
ASSERT_EQ(ctx->Init(), RET_OK);
param->op_parameter_.thread_num_ = ctx->thread_num_;
kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_FullConnection};
auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc);
ASSERT_NE(creator, nullptr);
auto *kernel = creator(inputs, outputs, reinterpret_cast<OpParameter *>(param), ctx.get(), desc);
ASSERT_NE(kernel, nullptr);
ASSERT_EQ(kernel->Prepare(), RET_OK);
#ifdef SUPPORT_TRAIN
fc->AllocWorkspace();
kernel->AllocWorkspace();
#endif
struct timeval start, end;
gettimeofday(&start, nullptr);
for (int i = 0; i < 100000; ++i) fc->Run();
for (int i = 0; i < 100000; ++i) {
kernel->Run();
}
gettimeofday(&end, nullptr);
// printf("## elapsed: %llu\n", 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - end.tv_usec);
for (unsigned int i = 0; i < inputs_.size(); i++) {
delete inputs_[i];
}
for (unsigned int i = 0; i < outputs_.size(); i++) {
delete outputs_[i];
}
delete fc;
delete ctx;
printf("## elapsed: %lu\n", 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - end.tv_usec);
delete kernel;
DestroyTensors(inputs);
DestroyTensors(outputs);
}
int FcTest4_Init(std::vector<lite::Tensor *> *inputs, std::vector<lite::Tensor *> *outputs, lite::InnerContext *context,
MatMulParameter *param, float **correct) {
auto *in_t = new Tensor(kNumberTypeFloat, {1, 4}, mindspore::NHWC);
in_t->MallocData();
float in[] = {1, 2, 3, 4};
memcpy(in_t->MutableData(), in, sizeof(float) * in_t->ElementsNum());
inputs->push_back(in_t);
auto *weight_t = new Tensor(kNumberTypeFloat, {10, 4}, mindspore::NHWC, lite::Category::CONST_TENSOR);
weight_t->MallocData();
float weight[] = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 1, 2, 3, 4};
memcpy(weight_t->MutableData(), weight, sizeof(float) * weight_t->ElementsNum());
inputs->push_back(weight_t);
auto bias_t = new Tensor(kNumberTypeFloat32, {10}, mindspore::NHWC, lite::Category::CONST_TENSOR);
bias_t->MallocData();
float bias_data[] = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
memcpy(bias_t->MutableData(), bias_data, sizeof(float) * bias_t->ElementsNum());
inputs->push_back(bias_t);
auto *out_t = new Tensor(kNumberTypeFloat, {1, 10}, mindspore::NHWC, lite::Category::CONST_TENSOR);
out_t->MallocData();
outputs->push_back(out_t);
*correct = new float[out_t->ElementsNum()];
float nchw_co[] = {11, 21, 31, 41, 51, 62, 72, 82, 92, 32};
memcpy(*correct, nchw_co, out_t->ElementsNum() * sizeof(float));
param->a_transpose_ = false;
param->b_transpose_ = true;
param->has_bias_ = false;
param->act_type_ = ActType_No;
param->op_parameter_.thread_num_ = 1;
context->thread_num_ = 1;
EXPECT_EQ(lite::RET_OK, context->Init());
return out_t->ElementsNum();
}
int FcTest4_Resize(std::vector<lite::Tensor *> *inputs, std::vector<lite::Tensor *> *outputs, float **correct) {
void FcTest4_Resize(std::vector<lite::Tensor *> *inputs, std::vector<lite::Tensor *> *outputs) {
auto &in_tensor = inputs->at(0);
in_tensor->FreeData();
in_tensor->set_shape({2, 4});
@ -286,39 +205,67 @@ int FcTest4_Resize(std::vector<lite::Tensor *> *inputs, std::vector<lite::Tensor
out_tensor->FreeData();
out_tensor->set_shape({2, 10});
out_tensor->MallocData();
*correct = new float[out_tensor->ElementsNum()];
float nchw_co[] = {11, 21, 31, 41, 51, 62, 72, 82, 92, 32, 9, 17, 25, 33, 41, 50, 58, 66, 74, 21};
memcpy(*correct, nchw_co, sizeof(nchw_co));
return out_tensor->ElementsNum();
}
TEST_F(TestFcFp32, FcTest4_Vec2Batch) {
std::vector<lite::Tensor *> inputs;
inputs.push_back(CreateTensor<float>(kNumberTypeFloat32, {1, 4}, {1, 2, 3, 4}));
std::vector<float> weight = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 1, 2, 3, 4};
inputs.push_back(
CreateTensor<float>(kNumberTypeFloat32, {10, 4}, weight, mindspore::NHWC, lite::Category::CONST_TENSOR));
inputs.push_back(CreateTensor<float>(kNumberTypeFloat32, {10}, {1, 1, 1, 1, 1, 2, 2, 2, 2, 2}, mindspore::NHWC,
lite::Category::CONST_TENSOR));
std::vector<lite::Tensor *> outputs;
lite::InnerContext context;
MatMulParameter *param = static_cast<MatMulParameter *>(malloc(sizeof(MatMulParameter)));
float *correct;
int total_size = FcTest4_Init(&inputs, &outputs, &context, param, &correct);
auto *kernel = new kernel::FullconnectionCPUKernel(reinterpret_cast<OpParameter *>(param), inputs, outputs, &context);
kernel->Prepare();
outputs.push_back(CreateTensor<float>(kNumberTypeFloat32, {1, 10}, {}));
auto param = static_cast<MatMulParameter *>(malloc(sizeof(MatMulParameter)));
memset(param, 0, sizeof(MatMulParameter));
#ifdef SUPPORT_TRAIN
param->op_parameter_.is_train_session_ = true;
#else
param->op_parameter_.is_train_session_ = false;
#endif
param->a_transpose_ = false;
param->b_transpose_ = true;
param->has_bias_ = true;
param->act_type_ = ActType_No;
auto ctx = std::make_shared<lite::InnerContext>();
ctx->thread_num_ = 2;
ASSERT_EQ(ctx->Init(), RET_OK);
param->op_parameter_.thread_num_ = ctx->thread_num_;
kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_FullConnection};
auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc);
ASSERT_NE(creator, nullptr);
auto *kernel = creator(inputs, outputs, reinterpret_cast<OpParameter *>(param), ctx.get(), desc);
ASSERT_NE(kernel, nullptr);
ASSERT_EQ(kernel->Prepare(), RET_OK);
#ifdef SUPPORT_TRAIN
kernel->AllocWorkspace();
#endif
kernel->Run();
ASSERT_EQ(0, CompareOutputData(reinterpret_cast<float *>(outputs[0]->MutableData()), correct, total_size, 0.0001));
ASSERT_EQ(kernel->Run(), RET_OK);
total_size = FcTest4_Resize(&inputs, &outputs, &correct);
kernel->ReSize();
kernel->Run();
ASSERT_EQ(0, CompareOutputData(reinterpret_cast<float *>(outputs[0]->MutableData()), correct, total_size, 0.0001));
delete[] correct;
for (auto &input : inputs) {
delete input;
}
for (auto &output : outputs) {
delete output;
}
std::vector<float> except_result = {11, 21, 31, 41, 51, 62, 72, 82, 92, 32};
ASSERT_EQ(0, CompareOutputData(static_cast<float *>(outputs[0]->data()), except_result.data(),
outputs[0]->ElementsNum(), 0.000001));
FcTest4_Resize(&inputs, &outputs);
ASSERT_EQ(kernel->ReSize(), RET_OK);
#ifdef SUPPORT_TRAIN
kernel->FreeWorkspace();
kernel->AllocWorkspace();
#endif
ASSERT_EQ(kernel->Run(), RET_OK);
except_result = {11, 21, 31, 41, 51, 62, 72, 82, 92, 32, 9, 17, 25, 33, 41, 50, 58, 66, 74, 21};
ASSERT_EQ(0, CompareOutputData(static_cast<float *>(outputs[0]->data()), except_result.data(),
outputs[0]->ElementsNum(), 0.000001));
#ifdef SUPPORT_TRAIN
kernel->FreeWorkspace();
#endif
delete kernel;
DestroyTensors(inputs);
DestroyTensors(outputs);
}
} // namespace mindspore

View File

@ -28,21 +28,6 @@ namespace mindspore {
class TestSparseToDenseFp32 : public mindspore::CommonTest {
public:
TestSparseToDenseFp32() {}
template <typename T>
lite::Tensor *CreateTensor(TypeId dtype, std::vector<int> shape, std::vector<T> data) {
auto tensor = new (std::nothrow) lite::Tensor(dtype, shape);
if (!data.empty()) {
memcpy(tensor->MutableData(), data.data(), tensor->Size());
} else {
(void)tensor->MallocData();
}
return tensor;
}
void DestroyTensors(std::vector<lite::Tensor *> tensors) {
for (auto &tensor : tensors) {
delete tensor;
}
}
};
TEST_F(TestSparseToDenseFp32, SparseToDense_test1) {

View File

@ -39,11 +39,10 @@ namespace mindspore {
class NetworkTest : public mindspore::CommonTest {
public:
NetworkTest() {}
int32_t runNet(mindspore::session::LiteSession *session, const std::string &in, const std::string &out,
const char *tensor_name, bool debug = false);
};
int32_t runNet(mindspore::session::LiteSession *session, const std::string &in, const std::string &out,
const char *tensor_name, bool debug = false);
int32_t fileIterator(mindspore::session::LiteSession *session, const std::string &path,
std::function<int32_t(mindspore::session::LiteSession *session, const std::string &)> cb) {
int32_t res = 0;
@ -60,8 +59,8 @@ int32_t fileIterator(mindspore::session::LiteSession *session, const std::string
}
void replaceExt(const std::string &src, std::string *dst) { *dst = src.substr(0, src.find_last_of('.')) + ".emb"; }
int32_t runNet(mindspore::session::LiteSession *session, const std::string &in, const std::string &out,
const char *tensor_name, bool debug) {
int32_t NetworkTest::runNet(mindspore::session::LiteSession *session, const std::string &in, const std::string &out,
const char *tensor_name, bool debug) {
// setup input
auto inputs = session->GetInputs();
auto inTensor = inputs.at(0);
@ -115,13 +114,10 @@ TEST_F(NetworkTest, efficient_net) {
}
TEST_F(NetworkTest, mobileface_net) {
char *buf = nullptr;
size_t net_size = 0;
std::string net = "./nets/mobilefacenet0924.ms";
ReadFile(net.c_str(), &net_size, &buf);
auto model = lite::Model::Import(buf, net_size);
delete[] buf;
size_t size = 0;
char *buff = lite::ReadFile("./nets/mobilefacenet0924.ms", &size);
auto model = lite::Model::Import(buff, size);
delete[] buff;
auto context = new lite::Context;
ASSERT_NE(context, nullptr);
context->device_list_[0].device_info_.cpu_device_info_.cpu_bind_mode_ = lite::NO_BIND;

View File

@ -29,6 +29,7 @@
#include "src/tensor_category.h"
#include "src/common/file_utils.h"
#include "common/common_test.h"
#include "schema/ops_generated.h"
using Tensor = mindspore::lite::Tensor;
using ArgsTuple = std::tuple<std::vector<int>, void *, mindspore::lite::Category>;

View File

@ -100,6 +100,7 @@ set(LITE_SRC ${API_SRC}
${SRC_DIR}/runtime/runtime_pass.cc
${SRC_DIR}/inner_context.cc
${SRC_DIR}/tensor.cc
${SRC_DIR}/tensor_category.cc
${SRC_DIR}/schema_tensor_wrapper.cc
${SRC_DIR}/ms_tensor.cc
${SRC_DIR}/tensorlist.cc