parent
c8dec08070
commit
65014d0988
@ -0,0 +1,23 @@
|
||||
include_directories(${DDK_PATH})
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/kernel)
|
||||
file(GLOB_RECURSE NPU_RUNTIME_SRC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/*.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../kernel/npu/*.cc
|
||||
)
|
||||
add_library(hiai SHARED IMPORTED)
|
||||
set_target_properties(hiai PROPERTIES IMPORTED_LOCATION
|
||||
${DDK_LIB_PATH}/libhiai.so)
|
||||
add_library(hiai_ir SHARED IMPORTED)
|
||||
set_target_properties(hiai_ir PROPERTIES IMPORTED_LOCATION
|
||||
${DDK_LIB_PATH}/libhiai_ir.so)
|
||||
add_library(hiai_ir_build SHARED IMPORTED)
|
||||
set_target_properties(hiai_ir_build PROPERTIES IMPORTED_LOCATION
|
||||
${DDK_LIB_PATH}/libhiai_ir_build.so)
|
||||
add_library(npu_kernel_mid OBJECT ${NPU_RUNTIME_SRC})
|
||||
target_link_libraries(
|
||||
npu_kernel_mid
|
||||
hiai
|
||||
hiai_ir
|
||||
hiai_ir_build
|
||||
)
|
@ -0,0 +1,160 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/agent/npu/npu_converter_utils.h"
|
||||
namespace mindspore::lite {
|
||||
ge::Shape ConverterToNPUShape(const std::vector<int> &src_shape) {
|
||||
vector<int64_t> shapes;
|
||||
shapes.reserve(src_shape.size());
|
||||
for (int i = 0; i < src_shape.size(); i++) {
|
||||
shapes.push_back(src_shape[i]);
|
||||
}
|
||||
return ge::Shape({shapes});
|
||||
}
|
||||
ge::Format ConverterToNPUFormat(schema::Format format) {
|
||||
ge::Format ge_format;
|
||||
switch (format) {
|
||||
case schema::Format_NCHW:
|
||||
ge_format = ge::FORMAT_NCHW;
|
||||
break;
|
||||
case schema::Format_NHWC:
|
||||
ge_format = ge::FORMAT_NHWC;
|
||||
break;
|
||||
default:
|
||||
MS_LOG(ERROR) << "Unsupported format:" << format;
|
||||
// use unused format to indicate errors.
|
||||
ge_format = ge::FORMAT_ND;
|
||||
break;
|
||||
}
|
||||
return ge_format;
|
||||
}
|
||||
|
||||
ge::DataType ConverterToNPUDataType(TypeId type_id) {
|
||||
ge::DataType data_type;
|
||||
switch (type_id) {
|
||||
case kNumberTypeFloat:
|
||||
case kNumberTypeFloat32:
|
||||
data_type = ge::DT_FLOAT;
|
||||
break;
|
||||
case kNumberTypeFloat16:
|
||||
data_type = ge::DT_FLOAT16;
|
||||
break;
|
||||
case kNumberTypeInt8:
|
||||
data_type = ge::DT_INT8;
|
||||
break;
|
||||
case kNumberTypeUInt8:
|
||||
data_type = ge::DT_UINT8;
|
||||
break;
|
||||
case kNumberTypeInt16:
|
||||
data_type = ge::DT_INT16;
|
||||
break;
|
||||
case kNumberTypeInt32:
|
||||
data_type = ge::DT_INT32;
|
||||
break;
|
||||
case kNumberTypeUInt32:
|
||||
data_type = ge::DT_UINT32;
|
||||
break;
|
||||
default:
|
||||
data_type = ge::DT_UNDEFINED;
|
||||
break;
|
||||
}
|
||||
return data_type;
|
||||
}
|
||||
hiai::op::Data *ConverterToNPUData(Tensor *src, const std::string &name) {
|
||||
auto data = new (std::nothrow) hiai::op::Data(name);
|
||||
if (data == nullptr) {
|
||||
MS_LOG(ERROR) << "new data failed.";
|
||||
return data;
|
||||
}
|
||||
ge::TensorDesc tensor_desc(ConverterToNPUShape(src->shape()), ConverterToNPUFormat(src->format()),
|
||||
ConverterToNPUDataType(src->data_type()));
|
||||
data->update_input_desc_x(tensor_desc);
|
||||
return data;
|
||||
}
|
||||
|
||||
std::shared_ptr<ge::Tensor> ConverterToNPUTensor(Tensor *src) {
|
||||
std::shared_ptr<ge::Tensor> ge_tensor = std::shared_ptr<ge::Tensor>(new (std::nothrow) ge::Tensor());
|
||||
if (ge_tensor == nullptr) {
|
||||
MS_LOG(ERROR) << "new ge_tensor failed.";
|
||||
return ge_tensor;
|
||||
}
|
||||
ge::TensorDesc tensor_desc(ConverterToNPUShape(src->shape()), ConverterToNPUFormat(src->format()),
|
||||
ConverterToNPUDataType(src->data_type()));
|
||||
|
||||
ge_tensor->SetTensorDesc(tensor_desc);
|
||||
|
||||
if (src->data_c() != nullptr) {
|
||||
ge_tensor->SetData(reinterpret_cast<const uint8_t *>(src->data_c()), src->Size());
|
||||
}
|
||||
return ge_tensor;
|
||||
}
|
||||
/*
|
||||
* mode : Activation mode, with options as follows:
|
||||
* 0 : Sigmoid
|
||||
* 1 : ReLU
|
||||
* 2 : Tanh
|
||||
* 3 : Clipped ReLU
|
||||
* 4 : ELU
|
||||
* 5 : PReLU
|
||||
* 6 : Abs
|
||||
* 7 : Relu1
|
||||
* 8 : Softsign
|
||||
* 9 : Softplus
|
||||
* 10 : Hardsigmoid
|
||||
* 11 : Threshold ReLU
|
||||
* 12 : Selu
|
||||
* 13 : Linear
|
||||
* 14 : Relu6
|
||||
* 15 : GeLU.
|
||||
*/
|
||||
int ConverterToNPUActMode(schema::ActivationType type) {
|
||||
switch (type) {
|
||||
case schema::ActivationType_NO_ACTIVATION:
|
||||
return -1;
|
||||
case schema::ActivationType_SIGMOID:
|
||||
return 0;
|
||||
case schema::ActivationType_RELU:
|
||||
return 1;
|
||||
case schema::ActivationType_TANH:
|
||||
return 2;
|
||||
case schema::ActivationType_ELU:
|
||||
return 4;
|
||||
case schema::ActivationType_LEAKY_RELU:
|
||||
return 5;
|
||||
case schema::ActivationType_ABS:
|
||||
return 6;
|
||||
case schema::ActivationType_RELU1:
|
||||
return 7;
|
||||
case schema::ActivationType_SOFTSIGN:
|
||||
return 8;
|
||||
case schema::ActivationType_SOFTPLUS:
|
||||
return 9;
|
||||
case schema::ActivationType_HSIGMOID:
|
||||
return 10;
|
||||
case schema::ActivationType_THRESHOLDRELU:
|
||||
return 11;
|
||||
case schema::ActivationType_SELU:
|
||||
return 12;
|
||||
case schema::ActivationType_LINEAR:
|
||||
return 13;
|
||||
case schema::ActivationType_RELU6:
|
||||
return 14;
|
||||
default:
|
||||
MS_LOG(ERROR) << "Unsupport activation type to NPU." << type;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} // namespace mindspore::lite
|
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_CONVERTER_UITLS_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_CONVERTER_UITLS_H_
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "schema/ops_generated.h"
|
||||
#include "include/graph/tensor.h"
|
||||
#include "include/graph/op/array_defs.h"
|
||||
#include "src/tensor.h"
|
||||
|
||||
namespace mindspore::lite {
|
||||
|
||||
std::shared_ptr<ge::Tensor> ConverterToNPUTensor(Tensor *src);
|
||||
|
||||
hiai::op::Data *ConverterToNPUData(Tensor *src, const std::string &name);
|
||||
|
||||
ge::Format ConverterToNPUFormat(schema::Format format);
|
||||
|
||||
ge::DataType ConverterToNPUDataType(TypeId type_id);
|
||||
|
||||
ge::Shape ConverterToNPUShape(const std::vector<int> &src_shape);
|
||||
|
||||
int ConverterToNPUActMode(schema::ActivationType type);
|
||||
|
||||
} // namespace mindspore::lite
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_CONVERTER_UITLS_H_
|
@ -0,0 +1,131 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/agent/npu/npu_executor.h"
|
||||
#include "include/errorcode.h"
|
||||
#include "src/runtime/agent/npu/npu_manager.h"
|
||||
|
||||
namespace mindspore::lite {
|
||||
int NPUExecutor::Prepare(const std::vector<kernel::LiteKernel *> &kernels) {
|
||||
this->client_ = mindspore::lite::NPUManager::GetInstance()->GetClient();
|
||||
if (this->client_ == nullptr) {
|
||||
MS_LOG(ERROR) << "client is nullptr.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
if (GetIOTensorVec() != RET_OK) {
|
||||
MS_LOG(ERROR) << "Load model failed.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int NPUExecutor::Run(std::vector<Tensor *> &in_tensors, std::vector<Tensor *> &out_tensors,
|
||||
std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator, const KernelCallBack &before,
|
||||
const KernelCallBack &after) {
|
||||
hiai::AiContext context;
|
||||
for (int i = 0; i < npu_input_tensors_.size(); ++i) {
|
||||
memcpy(npu_input_tensors_[i]->GetBuffer(), in_tensors[i]->data_c(), in_tensors[i]->Size());
|
||||
}
|
||||
context.AddPara("model_name", model_name_);
|
||||
if (this->client_ == nullptr) {
|
||||
MS_LOG(ERROR) << "NPU client is nullptr";
|
||||
return RET_ERROR;
|
||||
}
|
||||
int stamp;
|
||||
int ret = this->client_->Process(context, this->npu_input_tensors_, this->npu_output_tensors_, 1000, stamp);
|
||||
if (ret != hiai::AI_SUCCESS) {
|
||||
MS_LOG(ERROR) << "NPU Process failed. code is " << ret;
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
for (int i = 0; i < npu_output_tensors_.size(); ++i) {
|
||||
memcpy(out_tensors[i]->MutableData(), npu_output_tensors_[i]->GetBuffer(), npu_output_tensors_[i]->GetSize());
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int NPUExecutor::GetIOTensorVec() {
|
||||
std::vector<hiai::TensorDimension> input_dimension;
|
||||
std::vector<hiai::TensorDimension> output_dimension;
|
||||
input_dimension.clear();
|
||||
output_dimension.clear();
|
||||
if (this->client_ == nullptr) {
|
||||
MS_LOG(ERROR) << "client is nullptr.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
auto ret = this->client_->GetModelIOTensorDim(model_name_, input_dimension, output_dimension);
|
||||
if (ret != hiai::AI_SUCCESS) {
|
||||
MS_LOG(ERROR) << "Get model input and output tensor dims failed." << ret;
|
||||
return RET_ERROR;
|
||||
}
|
||||
ret = UpdateInputTensorVec(input_dimension);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Update input tensor vector failed. " << ret;
|
||||
return RET_ERROR;
|
||||
}
|
||||
ret = UpdateOutputTensorVec(output_dimension);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Update output tensor vector failed. " << ret;
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int NPUExecutor::UpdateInputTensorVec(const std::vector<hiai::TensorDimension> &input_dimension) {
|
||||
if (input_dimension.empty()) {
|
||||
MS_LOG(ERROR) << "npu input tensor dimension is empty.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
npu_input_tensors_.resize(input_dimension.size());
|
||||
npu_input_tensors_.clear();
|
||||
for (const auto &inDim : input_dimension) {
|
||||
std::shared_ptr<hiai::AiTensor> input = std::make_shared<hiai::AiTensor>();
|
||||
if (input->Init(&inDim) != hiai::AI_SUCCESS) {
|
||||
MS_LOG(ERROR) << "Input AiTensor init failed.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
npu_input_tensors_.push_back(input);
|
||||
}
|
||||
if (npu_input_tensors_.empty()) {
|
||||
MS_LOG(ERROR) << "NPU input tensor is empty.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int NPUExecutor::UpdateOutputTensorVec(const std::vector<hiai::TensorDimension> &output_dimension) {
|
||||
if (output_dimension.empty()) {
|
||||
MS_LOG(ERROR) << "output_dimension_ is empty.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
npu_output_tensors_.resize(output_dimension.size());
|
||||
npu_output_tensors_.clear();
|
||||
for (const auto &outDim : output_dimension) {
|
||||
std::shared_ptr<hiai::AiTensor> output = std::make_shared<hiai::AiTensor>();
|
||||
int ret = output->Init(&outDim);
|
||||
if (ret != hiai::AI_SUCCESS) {
|
||||
return RET_ERROR;
|
||||
}
|
||||
npu_output_tensors_.push_back(output);
|
||||
}
|
||||
if (npu_output_tensors_.empty()) {
|
||||
MS_LOG(ERROR) << "NPU output tensor is empty.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
} // namespace mindspore::lite
|
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_EXECUTOR_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_EXECUTOR_H_
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "src/executor.h"
|
||||
#include "include/errorcode.h"
|
||||
#include "include/HiAiModelManagerService.h"
|
||||
|
||||
namespace mindspore::lite {
|
||||
class NPUExecutor : public Executor {
|
||||
public:
|
||||
explicit NPUExecutor(const std::string &model_name) { this->model_name_ = model_name; }
|
||||
~NPUExecutor() override = default;
|
||||
int Prepare(const std::vector<kernel::LiteKernel *> &kernels) override;
|
||||
|
||||
int Run(std::vector<Tensor *> &in_tensors, std::vector<Tensor *> &out_tensors,
|
||||
std::vector<kernel::LiteKernel *> &kernels, Allocator *allocator = nullptr,
|
||||
const KernelCallBack &before = nullptr, const KernelCallBack &after = nullptr) override;
|
||||
|
||||
private:
|
||||
int GetIOTensorVec();
|
||||
|
||||
int UpdateInputTensorVec(const std::vector<hiai::TensorDimension> &input_dimension);
|
||||
|
||||
int UpdateOutputTensorVec(const std::vector<hiai::TensorDimension> &output_dimension);
|
||||
|
||||
private:
|
||||
std::string model_name_;
|
||||
std::shared_ptr<hiai::AiModelMngerClient> client_ = nullptr;
|
||||
std::vector<std::shared_ptr<hiai::AiTensor>> npu_input_tensors_;
|
||||
std::vector<std::shared_ptr<hiai::AiTensor>> npu_output_tensors_;
|
||||
};
|
||||
} // namespace mindspore::lite
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_EXECUTOR_H_
|
@ -0,0 +1,217 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/agent/npu/npu_manager.h"
|
||||
#include <sys/fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include "include/hiai_ir_build.h"
|
||||
#include "include/HiAiModelManagerService.h"
|
||||
#include "include/errorcode.h"
|
||||
#include "include/graph/op/all_ops.h"
|
||||
#include "src/common/file_utils.h"
|
||||
|
||||
namespace mindspore::lite {
|
||||
|
||||
bool NPUManager::IsSupportNPU() {
|
||||
if (!is_npu_check_executor) {
|
||||
CheckSupportNPU();
|
||||
}
|
||||
if (is_support_npu) {
|
||||
MS_LOG(INFO) << "The current device support NPU.";
|
||||
return true;
|
||||
} else {
|
||||
MS_LOG(INFO) << "The current device NOT SUPPORT NPU.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string NPUManager::GetExecutorPath() {
|
||||
std::string executor_path;
|
||||
char cmdline[1024] = {0};
|
||||
int fd = open("/proc/self/cmdline", O_RDONLY);
|
||||
if (fd >= 0) {
|
||||
char ch;
|
||||
int i = 0;
|
||||
while (read(fd, &ch, sizeof(ch)) > 0 && !isspace(ch)) {
|
||||
if (':' == ch) {
|
||||
break;
|
||||
}
|
||||
cmdline[i] = ch;
|
||||
i++;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
executor_path = std::string(cmdline);
|
||||
if (executor_path.empty()) {
|
||||
executor_path = "./";
|
||||
}
|
||||
// android
|
||||
if (executor_path.substr(0, 11) == "/data/data/") {
|
||||
executor_path = executor_path + '/';
|
||||
} else {
|
||||
// Linux
|
||||
executor_path = executor_path.substr(0, executor_path.rfind('/')) + "/";
|
||||
}
|
||||
return executor_path;
|
||||
}
|
||||
|
||||
bool NPUManager::IsKirinChip() {
|
||||
std::ifstream cpu_info("/proc/cpuinfo");
|
||||
if (!(cpu_info.good() && cpu_info.is_open())) {
|
||||
return false;
|
||||
}
|
||||
std::string line;
|
||||
while (!cpu_info.eof()) {
|
||||
getline(cpu_info, line);
|
||||
if (line.find("Hardware") == string::npos) {
|
||||
continue;
|
||||
}
|
||||
auto index = line.find("Kirin");
|
||||
if (index == string::npos) {
|
||||
continue;
|
||||
}
|
||||
auto kirin_number_str = line.substr(index + 5);
|
||||
auto kirin_number = atoi(kirin_number_str.c_str());
|
||||
if (kirin_number >= 985 || kirin_number == 810 || kirin_number == 820) {
|
||||
cpu_info.close();
|
||||
return true;
|
||||
} else {
|
||||
cpu_info.close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WriteToOMFile(domi::ModelBufferData om_model_buff, const std::string &om_file_path) {
|
||||
FILE *fp;
|
||||
fp = fopen(om_file_path.c_str(), "wb");
|
||||
if (fp == nullptr) {
|
||||
MS_LOG(ERROR) << om_file_path.c_str() << " open failed.";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto write_size = (uint32_t)fwrite(om_model_buff.data, 1, om_model_buff.length, fp);
|
||||
if (write_size != om_model_buff.length) {
|
||||
fclose(fp);
|
||||
MS_LOG(ERROR) << "Write om file failed.";
|
||||
return false;
|
||||
}
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NPUManager::CheckOmBuildIr(const std::string &path) {
|
||||
// build test om model
|
||||
std::shared_ptr<hiai::op::Add> add_op(new (std::nothrow) hiai::op::Add("add"));
|
||||
if (add_op == nullptr) {
|
||||
MS_LOG(ERROR) << "new add_op failed.";
|
||||
return false;
|
||||
}
|
||||
ge::TensorDesc desc(ge::Shape({1}), ge::FORMAT_NCHW, ge::DT_FLOAT);
|
||||
std::shared_ptr<hiai::op::Data> data = std::make_shared<hiai::op::Data>("data");
|
||||
data->update_input_desc_x(desc);
|
||||
add_op->set_input_x1(*data);
|
||||
add_op->set_input_x2(*data);
|
||||
domi::HiaiIrBuild ir_build;
|
||||
ge::Graph ir_graph("graph");
|
||||
std::vector<ge::Operator> inputs{*data, *data};
|
||||
std::vector<ge::Operator> outputs{*add_op};
|
||||
ir_graph.SetInputs(inputs).SetOutputs(outputs);
|
||||
ge::Model om_model("test_model", "test_version");
|
||||
om_model.SetGraph(ir_graph);
|
||||
|
||||
domi::ModelBufferData om_model_buff;
|
||||
if (!ir_build.CreateModelBuff(om_model, om_model_buff)) {
|
||||
MS_LOG(ERROR) << "Create model buffer failed.";
|
||||
return false;
|
||||
}
|
||||
if (!ir_build.BuildIRModel(om_model, om_model_buff)) {
|
||||
MS_LOG(ERROR) << "Build IR model failed.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// save test om model
|
||||
remove(path.c_str());
|
||||
bool ret = WriteToOMFile(om_model_buff, path);
|
||||
ir_build.ReleaseModelBuff(om_model_buff);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void NPUManager::CheckSupportNPU() {
|
||||
is_npu_check_executor = true;
|
||||
std::string path_string = GetExecutorPath();
|
||||
|
||||
std::string test_model_path = path_string + "/mindspore_lite_test_npu.om";
|
||||
std::ifstream ifs(test_model_path);
|
||||
if (ifs.good() && ifs.is_open()) {
|
||||
ifs.close();
|
||||
is_support_npu = true;
|
||||
return;
|
||||
}
|
||||
if (!IsKirinChip()) {
|
||||
MS_LOG(ERROR) << "The current device chip NOT SUPPORT NPU";
|
||||
is_support_npu = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CheckOmBuildIr(test_model_path)) {
|
||||
MS_LOG(ERROR) << "Build OM IR error.";
|
||||
is_support_npu = false;
|
||||
return;
|
||||
}
|
||||
is_support_npu = true;
|
||||
}
|
||||
|
||||
int NPUManager::AddModel(void *model_buf, uint32_t size, const std::string &model_name, int frequency) {
|
||||
hiai::MemBuffer *buffer = mc_builder_->InputMemBufferCreate(model_buf, size);
|
||||
if (buffer == nullptr) {
|
||||
MS_LOG(ERROR) << "MemBuffer is null.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
auto desc = std::make_shared<hiai::AiModelDescription>(model_name, frequency, 0, 0, 0);
|
||||
desc->SetModelBuffer(buffer->GetMemBufferData(), buffer->GetMemBufferSize());
|
||||
model_desc_.push_back(desc);
|
||||
mc_builder_->MemBufferDestroy(buffer);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int NPUManager::InitClient() {
|
||||
this->client_ = std::make_shared<hiai::AiModelMngerClient>();
|
||||
if (this->client_ == nullptr) {
|
||||
return RET_ERROR;
|
||||
}
|
||||
int ret = this->client_->Init(nullptr);
|
||||
if (ret != hiai::AI_SUCCESS) {
|
||||
return RET_ERROR;
|
||||
}
|
||||
mc_builder_ = std::make_shared<hiai::AiModelBuilder>(this->client_);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int NPUManager::LoadOMModel() {
|
||||
int ret = this->client_->Load(model_desc_);
|
||||
if (ret != hiai::AI_SUCCESS) {
|
||||
MS_LOG(ERROR) << "Client load model failed." << ret;
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
std::shared_ptr<hiai::AiModelMngerClient> NPUManager::GetClient() { return client_; }
|
||||
} // namespace mindspore::lite
|
@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_UTILS_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_UTILS_H_
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "include/HiAiModelManagerService.h"
|
||||
|
||||
namespace mindspore::lite {
|
||||
|
||||
class NPUManager {
|
||||
public:
|
||||
static NPUManager *GetInstance() {
|
||||
static NPUManager npuManager;
|
||||
return &npuManager;
|
||||
}
|
||||
|
||||
bool IsSupportNPU();
|
||||
|
||||
int InitClient();
|
||||
|
||||
// provide to subgraph to add model.
|
||||
int AddModel(void *model_buf, uint32_t size, const std::string &model_name, int frequency);
|
||||
|
||||
// scheduler to load om model.
|
||||
int LoadOMModel();
|
||||
|
||||
// provide to executor.
|
||||
std::shared_ptr<hiai::AiModelMngerClient> GetClient();
|
||||
|
||||
private:
|
||||
void CheckSupportNPU();
|
||||
|
||||
bool IsKirinChip();
|
||||
|
||||
bool CheckOmBuildIr(const std::string &path);
|
||||
|
||||
std::string GetExecutorPath();
|
||||
|
||||
private:
|
||||
bool is_npu_check_executor = false;
|
||||
|
||||
bool is_support_npu = false;
|
||||
|
||||
std::shared_ptr<hiai::AiModelMngerClient> client_ = nullptr;
|
||||
|
||||
std::vector<std::shared_ptr<hiai::AiModelDescription>> model_desc_;
|
||||
|
||||
std::shared_ptr<hiai::AiModelBuilder> mc_builder_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace mindspore::lite
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_NPU_UTILS_H_
|
@ -0,0 +1,187 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/agent/npu/subgraph_npu_kernel.h"
|
||||
#include "include/errorcode.h"
|
||||
#include "src/runtime/agent/npu/npu_executor.h"
|
||||
#include "include/graph/operator.h"
|
||||
#include "include/graph/graph.h"
|
||||
#include "src/tensor.h"
|
||||
#include "include/graph/model.h"
|
||||
#include "include/hiai_ir_build.h"
|
||||
#include "include/HiAiModelManagerService.h"
|
||||
#include "include/HiAiModelManagerType.h"
|
||||
#include "include/context.h"
|
||||
#include "include/version.h"
|
||||
#include "include/graph/op/array_defs.h"
|
||||
#include "src/common/file_utils.h"
|
||||
#include "src/common/common.h"
|
||||
#include "src/common/utils.h"
|
||||
#include "src/runtime/agent/npu/npu_converter_utils.h"
|
||||
#include "mindspore/lite/src/runtime/kernel/npu/npu_kernel.h"
|
||||
#include "src/runtime/agent/npu/npu_manager.h"
|
||||
namespace mindspore::kernel {
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
|
||||
domi::ModelBufferData *SubGraphNpuKernel::BuildIRModel() {
|
||||
ge::Graph graph("NPUGraph");
|
||||
|
||||
auto ret = BuildNPUInputOp();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Build NPU input operator failed.";
|
||||
return nullptr;
|
||||
}
|
||||
ret = BuildNPUOutputOp();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Build NPU output operator failed.";
|
||||
return nullptr;
|
||||
}
|
||||
graph.SetInputs(subgraph_input_op_).SetOutputs(subgraph_output_op_);
|
||||
ge::Model model(GetOMModelName(), mindspore::lite::Version());
|
||||
model.SetGraph(graph);
|
||||
domi::HiaiIrBuild ir_build;
|
||||
auto om_model_buff = new (std::nothrow) domi::ModelBufferData;
|
||||
if (om_model_buff == nullptr) {
|
||||
MS_LOG(ERROR) << "om model buffer is nullptr.";
|
||||
return nullptr;
|
||||
}
|
||||
if (!ir_build.CreateModelBuff(model, *om_model_buff)) {
|
||||
MS_LOG(ERROR) << "Create model buffer failed.";
|
||||
delete om_model_buff;
|
||||
return nullptr;
|
||||
}
|
||||
if (!ir_build.BuildIRModel(model, *om_model_buff)) {
|
||||
MS_LOG(ERROR) << "Build IR model failed.";
|
||||
ir_build.ReleaseModelBuff(*om_model_buff);
|
||||
delete om_model_buff;
|
||||
return nullptr;
|
||||
}
|
||||
return om_model_buff;
|
||||
}
|
||||
|
||||
int SubGraphNpuKernel::Run() { return this->executor_->Run(in_tensors_, out_tensors_, nodes_, nullptr); }
|
||||
|
||||
int SubGraphNpuKernel::BuildNPUInputOp() {
|
||||
int count = 0;
|
||||
subgraph_input_op_.clear();
|
||||
for (auto node : this->nodes_) {
|
||||
std::vector<ge::Operator *> node_input_op;
|
||||
for (auto in_tensor : node->in_tensors()) {
|
||||
if (IsSubGraphInputTensor(in_tensor)) {
|
||||
auto tensor_name = node->name() + "_" + std::to_string(count++);
|
||||
auto data = mindspore::lite::ConverterToNPUData(in_tensor, tensor_name);
|
||||
subgraph_input_op_.push_back(*data);
|
||||
node_input_op.push_back(data);
|
||||
continue;
|
||||
}
|
||||
|
||||
bool is_weight_tensor = true;
|
||||
for (auto in_kernel : node->in_kernels()) {
|
||||
if (IsContain(in_kernel->out_tensors(), in_tensor)) {
|
||||
if (in_kernel->desc().arch == mindspore::kernel::kNPU) {
|
||||
// input come from npu
|
||||
auto npu_op = reinterpret_cast<NPUKernel *>(in_kernel)->GetNPUOp();
|
||||
if (npu_op != nullptr) {
|
||||
npu_op->GetOutputDesc(0).GetName();
|
||||
node_input_op.push_back(npu_op);
|
||||
is_weight_tensor = false;
|
||||
break;
|
||||
} else {
|
||||
MS_LOG(ERROR) << in_kernel->type_str() << "NPU Operator is nullptr.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
} else {
|
||||
MS_LOG(ERROR) << "The input of the intermediate node comes from the CPU";
|
||||
return RET_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// weight tensor
|
||||
if (is_weight_tensor) {
|
||||
if (!(node->Type() == schema::PrimitiveType_Conv2D || node->Type() == schema::PrimitiveType_DeConv2D ||
|
||||
node->Type() == schema::PrimitiveType_DepthwiseConv2D ||
|
||||
node->Type() == schema::PrimitiveType_DeDepthwiseConv2D)) {
|
||||
auto name = node->name() + "_" + std::to_string(count++);
|
||||
auto weight_const = new (std::nothrow) hiai::op::Const(node->name() + "_" + std::to_string(count++));
|
||||
if (weight_const == nullptr) {
|
||||
MS_LOG(ERROR) << "new weight const failed.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
auto weight_tensor = mindspore::lite::ConverterToNPUTensor(in_tensor);
|
||||
weight_const->set_attr_value(weight_tensor);
|
||||
node_input_op.push_back(weight_const);
|
||||
}
|
||||
}
|
||||
}
|
||||
// set input to NPU
|
||||
reinterpret_cast<NPUKernel *>(node)->SetNPUInputs(node->in_tensors(), node->out_tensors(), node_input_op);
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
bool SubGraphNpuKernel::IsSubGraphInputTensor(lite::Tensor *inputs) { return IsContain(this->in_tensors(), inputs); }
|
||||
|
||||
std::vector<ge::Operator> SubGraphNpuKernel::GetNPUNodes(const vector<kernel::LiteKernel *> &nodes) {
|
||||
std::vector<ge::Operator> ops;
|
||||
ops.reserve(nodes.size());
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
ops.push_back(*reinterpret_cast<NPUKernel *>(nodes[i])->GetNPUOp());
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
int SubGraphNpuKernel::BuildNPUOutputOp() {
|
||||
subgraph_output_op_ = GetNPUNodes(out_nodes_);
|
||||
if (subgraph_output_op_.empty()) {
|
||||
MS_LOG(ERROR) << "NPU subgraph output op is empty.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void SubGraphNpuKernel::SetIndex(int index) { this->index_ = index; }
|
||||
|
||||
std::string SubGraphNpuKernel::GetOMModelName() { return this->name_ + std::to_string(index_) + ".om"; }
|
||||
int SubGraphNpuKernel::Init() {
|
||||
model_buffer_data_ = BuildIRModel();
|
||||
if (model_buffer_data_ == nullptr) {
|
||||
MS_LOG(ERROR) << "Build IR model failed.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
mindspore::lite::NPUManager::GetInstance()->AddModel(model_buffer_data_->data, model_buffer_data_->length,
|
||||
GetOMModelName(), context_->GetNpuInfo().frequency_);
|
||||
|
||||
executor_ = new (std::nothrow) mindspore::lite::NPUExecutor(GetOMModelName());
|
||||
|
||||
if (executor_ == nullptr) {
|
||||
MS_LOG(ERROR) << "Create NPUExecutor failed.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int SubGraphNpuKernel::Prepare() {
|
||||
if (executor_->Prepare(nodes_) != RET_OK) {
|
||||
MS_LOG(ERROR) << "NPU executor prepare failed.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_AGENT_SUBGRAPH_NPU_KERNEL_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_AGENT_SUBGRAPH_NPU_KERNEL_H_
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "include/hiai_ir_build.h"
|
||||
#include "src/sub_graph_kernel.h"
|
||||
#include "src/runtime/agent/npu/npu_executor.h"
|
||||
#include "include/graph/op/all_ops.h"
|
||||
|
||||
namespace mindspore::kernel {
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
class SubGraphNpuKernel : public SubGraphKernel {
|
||||
public:
|
||||
SubGraphNpuKernel(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<kernel::LiteKernel *> &inKernels,
|
||||
const std::vector<kernel::LiteKernel *> &outKernels, const std::vector<kernel::LiteKernel *> &nodes,
|
||||
const lite::InnerContext *ctx = nullptr)
|
||||
: SubGraphKernel(inputs, outputs, inKernels, outKernels, nodes, ctx) {
|
||||
subgraph_type_ = kNpuSubGraph;
|
||||
this->name_ = "NpuSubGraph";
|
||||
}
|
||||
|
||||
~SubGraphNpuKernel() override = default;
|
||||
|
||||
int Init() override;
|
||||
|
||||
int Prepare() override;
|
||||
|
||||
int PreProcess() override { return RET_OK; }
|
||||
|
||||
int Run() override;
|
||||
|
||||
int Run(const KernelCallBack &before, const KernelCallBack &after) override { return this->Run(); }
|
||||
|
||||
int PostProcess() override { return RET_OK; }
|
||||
|
||||
int ReSize() override {
|
||||
MS_LOG(ERROR) << "NPU does not support the resize function temporarily.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
void SetIndex(int index);
|
||||
|
||||
private:
|
||||
domi::ModelBufferData *BuildIRModel();
|
||||
|
||||
int BuildNPUInputOp();
|
||||
|
||||
int BuildNPUOutputOp();
|
||||
|
||||
std::vector<ge::Operator> GetNPUNodes(const std::vector<kernel::LiteKernel *> &nodes);
|
||||
|
||||
bool IsSubGraphInputTensor(lite::Tensor *inputs);
|
||||
|
||||
std::string GetOMModelName();
|
||||
|
||||
private:
|
||||
int index_;
|
||||
|
||||
domi::ModelBufferData *model_buffer_data_;
|
||||
|
||||
std::vector<ge::Operator> subgraph_input_op_;
|
||||
|
||||
std::vector<ge::Operator> subgraph_output_op_;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_AGENT_SUBGRAPH_NPU_KERNEL_H_
|
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/kernel/npu/add_npu.h"
|
||||
#include "include/graph/op/all_ops.h"
|
||||
#include "src/kernel_registry.h"
|
||||
|
||||
using mindspore::kernel::KERNEL_ARCH::kNPU;
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::schema::PrimitiveType_Add;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int AddNPUKernel::IsSupport(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter) {
|
||||
if (inputs[0]->shape() != inputs[1]->shape()) {
|
||||
MS_LOG(INFO) << "ddk 500 does not support broadcast."
|
||||
<< " shape 1 is:" << inputs[0]->shape() << " shape 2 is:" << inputs[1]->shape();
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
void AddNPUKernel::SetNPUInputs(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<ge::Operator *> &npu_inputs) {
|
||||
op_ = new hiai::op::Add(name_);
|
||||
op_->set_input_x1(*npu_inputs[0]);
|
||||
op_->set_input_x2(*npu_inputs[1]);
|
||||
}
|
||||
ge::Operator *mindspore::kernel::AddNPUKernel::GetNPUOp() { return this->op_; }
|
||||
AddNPUKernel::~AddNPUKernel() {
|
||||
if (op_ != nullptr) {
|
||||
delete op_;
|
||||
op_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
REG_KERNEL(kNPU, kNumberTypeFloat32, PrimitiveType_Add, NPUKernelCreator<AddNPUKernel>)
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_ADD_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_ADD_H_
|
||||
#include <vector>
|
||||
#include "src/runtime/kernel/npu/npu_kernel.h"
|
||||
#include "include/graph/op/math_defs.h"
|
||||
namespace mindspore::kernel {
|
||||
class AddNPUKernel : public NPUKernel {
|
||||
public:
|
||||
AddNPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs)
|
||||
: NPUKernel(parameter, inputs, outputs) {}
|
||||
~AddNPUKernel() override;
|
||||
|
||||
int IsSupport(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter) override;
|
||||
void SetNPUInputs(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<ge::Operator *> &npu_inputs) override;
|
||||
|
||||
ge::Operator *GetNPUOp() override;
|
||||
|
||||
private:
|
||||
hiai::op::Add *op_;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_ADD_H_
|
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/kernel/npu/concat_npu.h"
|
||||
#include "src/kernel_registry.h"
|
||||
|
||||
using mindspore::kernel::KERNEL_ARCH::kNPU;
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::schema::PrimitiveType_Concat;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int ConcatNPUKernel::IsSupport(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter) {
|
||||
return RET_OK;
|
||||
}
|
||||
void ConcatNPUKernel::SetNPUInputs(const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<ge::Operator *> &npu_inputs) {
|
||||
op_ = new hiai::op::ConcatD(name_);
|
||||
op_->set_attr_concat_dim(concat_parameter_->axis_);
|
||||
op_->set_attr_N(npu_inputs.size());
|
||||
op_->create_dynamic_input_x(npu_inputs.size());
|
||||
for (int i = 0; i < npu_inputs.size(); ++i) {
|
||||
op_->set_dynamic_input_x(i + 1, *npu_inputs[i]);
|
||||
}
|
||||
}
|
||||
ge::Operator *mindspore::kernel::ConcatNPUKernel::GetNPUOp() { return this->op_; }
|
||||
ConcatNPUKernel::~ConcatNPUKernel() {
|
||||
if (op_ != nullptr) {
|
||||
delete op_;
|
||||
op_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
REG_KERNEL(kNPU, kNumberTypeFloat32, PrimitiveType_Concat, NPUKernelCreator<ConcatNPUKernel>)
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_CONCAT_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_CONCAT_H_
|
||||
#include <vector>
|
||||
#include "nnacl/concat_parameter.h"
|
||||
#include "src/runtime/kernel/npu/npu_kernel.h"
|
||||
#include "include/graph/op/all_ops.h"
|
||||
namespace mindspore::kernel {
|
||||
class ConcatNPUKernel : public NPUKernel {
|
||||
public:
|
||||
ConcatNPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs)
|
||||
: NPUKernel(parameter, inputs, outputs) {
|
||||
concat_parameter_ = reinterpret_cast<ConcatParameter *>(parameter);
|
||||
}
|
||||
~ConcatNPUKernel() override;
|
||||
|
||||
int IsSupport(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter) override;
|
||||
void SetNPUInputs(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<ge::Operator *> &npu_inputs) override;
|
||||
ge::Operator *GetNPUOp() override;
|
||||
|
||||
private:
|
||||
hiai::op::ConcatD *op_;
|
||||
ConcatParameter *concat_parameter_;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_CONCAT_H_
|
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/kernel/npu/div_npu.h"
|
||||
#include "include/graph/op/all_ops.h"
|
||||
#include "src/kernel_registry.h"
|
||||
|
||||
using mindspore::kernel::KERNEL_ARCH::kNPU;
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::schema::PrimitiveType_Div;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int DivNPUKernel::IsSupport(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter) {
|
||||
op_ = new hiai::op::RealDiv(name_);
|
||||
|
||||
if (inputs[0]->shape() != inputs[1]->shape()) {
|
||||
MS_LOG(INFO) << "ddk 500 does not support broadcast."
|
||||
<< " shape 1 is:" << inputs[0]->shape() << " shape 2 is:" << inputs[1]->shape();
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
void DivNPUKernel::SetNPUInputs(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<ge::Operator *> &npu_inputs) {
|
||||
op_->set_input_x1(*npu_inputs[0]);
|
||||
op_->set_input_x2(*npu_inputs[1]);
|
||||
}
|
||||
ge::Operator *mindspore::kernel::DivNPUKernel::GetNPUOp() { return this->op_; }
|
||||
DivNPUKernel::~DivNPUKernel() {
|
||||
if (op_ != nullptr) {
|
||||
delete op_;
|
||||
op_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
REG_KERNEL(kNPU, kNumberTypeFloat32, PrimitiveType_Div, NPUKernelCreator<DivNPUKernel>)
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_DIV_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_DIV_H_
|
||||
#include <vector>
|
||||
#include "src/runtime/kernel/npu/npu_kernel.h"
|
||||
#include "include/graph/op/math_defs.h"
|
||||
namespace mindspore::kernel {
|
||||
class DivNPUKernel : public NPUKernel {
|
||||
public:
|
||||
DivNPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs)
|
||||
: NPUKernel(parameter, inputs, outputs) {}
|
||||
~DivNPUKernel() override;
|
||||
|
||||
int IsSupport(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter) override;
|
||||
void SetNPUInputs(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<ge::Operator *> &npu_inputs) override;
|
||||
ge::Operator *GetNPUOp() override;
|
||||
|
||||
private:
|
||||
hiai::op::RealDiv *op_;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_AGENT_NPU_KERNEL_NPU_DIV_H_
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright 2020 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/runtime/kernel/npu/floor_npu.h"
|
||||
#include "src/kernel_registry.h"
|
||||
|
||||
using mindspore::kernel::KERNEL_ARCH::kNPU;
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::schema::PrimitiveType_Floor;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int FloorNPUKernel::IsSupport(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter) {
|
||||
return RET_OK;
|
||||
}
|
||||
void FloorNPUKernel::SetNPUInputs(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
|
||||
const std::vector<ge::Operator *> &npu_inputs) {
|
||||
op_ = new hiai::op::Floor(name_);
|
||||
|
||||
op_->set_input_x(*npu_inputs[0]);
|
||||
}
|
||||
ge::Operator *mindspore::kernel::FloorNPUKernel::GetNPUOp() { return this->op_; }
|
||||
FloorNPUKernel::~FloorNPUKernel() {
|
||||
if (op_ != nullptr) {
|
||||
delete op_;
|
||||
op_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
REG_KERNEL(kNPU, kNumberTypeFloat32, PrimitiveType_Floor, NPUKernelCreator<FloorNPUKernel>)
|
||||
} // namespace mindspore::kernel
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue