commit
038a41d973
@ -0,0 +1,150 @@
|
||||
/**
|
||||
* 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/arm/fp32/group_convolution.h"
|
||||
#include "schema/model_generated.h"
|
||||
#include "src/kernel_registry.h"
|
||||
#include "include/errorcode.h"
|
||||
|
||||
using mindspore::kernel::KERNEL_ARCH::kCPU;
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_Conv2D;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int GroupConvolutionCPUKernel::Init() {
|
||||
for (int i = 0; i < group_num_; ++i) {
|
||||
auto ret = group_convs_[i]->Init();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Sub kernel init failed.";
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
// if infer shape is done, resize func will be invoked in sub kernels
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int GroupConvolutionCPUKernel::ReSize() {
|
||||
for (int i = 0; i < group_num_; ++i) {
|
||||
auto ret = group_convs_[i]->ReSize();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Sub kernel resize failed.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
}
|
||||
conv_param_->input_channel_ /= group_num_;
|
||||
conv_param_->output_channel_ /= group_num_;
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int GroupConvolutionCPUKernel::PreProcess() {
|
||||
if (!InferShapeDone()) {
|
||||
auto ret = (const_cast<mindspore::lite::PrimitiveC *>(primitive_))->InferShape(in_tensors_, out_tensors_);
|
||||
if (ret != 0) {
|
||||
(const_cast<mindspore::lite::PrimitiveC *>(primitive_))->SetInferFlag(false);
|
||||
MS_LOG(ERROR) << "InferShape fail!";
|
||||
return ret;
|
||||
}
|
||||
(const_cast<mindspore::lite::PrimitiveC *>(primitive_))->SetInferFlag(true);
|
||||
ret = ReSize();
|
||||
if (ret != 0) {
|
||||
MS_LOG(ERROR) << "ReSize fail!ret: " << ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// if infershape func is called in runtime stage, we should malloc memory and set shape info for outputs of sub
|
||||
// kernels here.
|
||||
std::vector<int> in_shape;
|
||||
std::vector<int> out_shape;
|
||||
for (int i = 0; i < group_num_; ++i) {
|
||||
// in
|
||||
int in_batch = conv_param_->input_batch_;
|
||||
int in_h = conv_param_->input_h_;
|
||||
int in_w = conv_param_->input_w_;
|
||||
int in_c = conv_param_->input_channel_;
|
||||
in_shape = {in_batch, in_h, in_w, in_c};
|
||||
auto sub_kernel_in_tensor = group_convs_[i]->in_tensors().front();
|
||||
sub_kernel_in_tensor->set_shape(in_shape);
|
||||
sub_kernel_in_tensor->MallocData();
|
||||
// out
|
||||
int out_batch = conv_param_->output_batch_;
|
||||
int out_h = conv_param_->output_h_;
|
||||
int out_w = conv_param_->output_w_;
|
||||
int out_c = conv_param_->output_channel_;
|
||||
out_shape = {out_batch, out_h, out_w, out_c};
|
||||
auto sub_kernel_out_tensors = group_convs_[i]->out_tensors();
|
||||
for (auto tensor : sub_kernel_out_tensors) {
|
||||
tensor->set_shape(out_shape);
|
||||
tensor->MallocData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto outputs = this->out_tensors();
|
||||
for (auto *output : outputs) {
|
||||
MS_ASSERT(output != nullptr);
|
||||
output->MallocData();
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void GroupConvolutionCPUKernel::SeparateInput(int group_id) {
|
||||
int in_h = conv_param_->input_h_;
|
||||
int in_w = conv_param_->input_w_;
|
||||
int in_plane = in_h * in_w;
|
||||
int sub_in_channel = conv_param_->input_channel_;
|
||||
int ori_in_channel = sub_in_channel * group_num_;
|
||||
auto sub_in_data = reinterpret_cast<float *>(group_convs_[group_id]->in_tensors().front()->data_c());
|
||||
float *src_ptr = ori_in_data_ + group_id * sub_in_channel;
|
||||
float *dst_ptr = sub_in_data;
|
||||
for (int i = 0; i < in_plane; ++i) {
|
||||
memcpy(dst_ptr, src_ptr, sub_in_channel * sizeof(float));
|
||||
src_ptr += ori_in_channel;
|
||||
dst_ptr += sub_in_channel;
|
||||
}
|
||||
}
|
||||
|
||||
void GroupConvolutionCPUKernel::PostConcat(int group_id) {
|
||||
int out_h = conv_param_->output_h_;
|
||||
int out_w = conv_param_->output_w_;
|
||||
int out_plane = out_h * out_w;
|
||||
int sub_out_channel = conv_param_->output_channel_;
|
||||
int ori_out_channel = sub_out_channel * group_num_;
|
||||
auto sub_out_data = reinterpret_cast<float *>(group_convs_[group_id]->out_tensors().front()->data_c());
|
||||
float *src_ptr = sub_out_data;
|
||||
float *dst_ptr = ori_out_data_ + group_id * sub_out_channel;
|
||||
for (int i = 0; i < out_plane; ++i) {
|
||||
memcpy(dst_ptr, src_ptr, sub_out_channel * sizeof(float));
|
||||
src_ptr += sub_out_channel;
|
||||
dst_ptr += ori_out_channel;
|
||||
}
|
||||
}
|
||||
|
||||
int GroupConvolutionCPUKernel::Run() {
|
||||
ori_in_data_ = reinterpret_cast<float *>(in_tensors().front()->data_c());
|
||||
ori_out_data_ = reinterpret_cast<float *>(out_tensors().front()->data_c());
|
||||
for (int i = 0; i < group_num_; ++i) {
|
||||
// first, separate group conv input into several parts. This step must be in runtime stage.
|
||||
SeparateInput(i);
|
||||
// sun kernels run
|
||||
group_convs_[i]->Run();
|
||||
// post process, concat all outputs of sub-kernels into one output
|
||||
PostConcat(i);
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* 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_KERNEL_ARM_FP32_GROUP_CONVOLUTION_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_GROUP_CONVOLUTION_H_
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "src/lite_kernel.h"
|
||||
#include "nnacl/op_base.h"
|
||||
#include "src/runtime/kernel/arm/base/convolution_base.h"
|
||||
#include "nnacl/fp32/conv.h"
|
||||
|
||||
namespace mindspore::kernel {
|
||||
class GroupConvolutionCPUKernel : public ConvolutionBaseCPUKernel {
|
||||
public:
|
||||
GroupConvolutionCPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx,
|
||||
const mindspore::lite::PrimitiveC *primitive, std::vector<kernel::LiteKernel *> group_convs,
|
||||
const int group_num)
|
||||
: ConvolutionBaseCPUKernel(parameter, inputs, outputs, ctx, primitive),
|
||||
group_convs_(std::move(group_convs)),
|
||||
group_num_(group_num) {} // opParameter(in channel, out channel) in this kernel has been split to groups, if
|
||||
// you want to get real params, multiply in channel / out channel with group num
|
||||
~GroupConvolutionCPUKernel() override {
|
||||
for (auto sub_conv : group_convs_) {
|
||||
// free sub conv input tensors / output tensors manually
|
||||
auto sub_in_tensors = sub_conv->in_tensors();
|
||||
auto sub_in_tensor_num = sub_in_tensors.size();
|
||||
for (size_t i = 0; i < sub_in_tensor_num; ++i) {
|
||||
delete sub_in_tensors[i];
|
||||
}
|
||||
auto sub_out_tensors = sub_conv->out_tensors();
|
||||
auto sub_out_tensor_num = sub_out_tensors.size();
|
||||
for (size_t i = 0; i < sub_out_tensor_num; ++i) {
|
||||
delete sub_out_tensors[i];
|
||||
}
|
||||
delete sub_conv;
|
||||
}
|
||||
};
|
||||
|
||||
int Init() override;
|
||||
int ReSize() override;
|
||||
int Run() override;
|
||||
int PreProcess() override;
|
||||
void SeparateInput(int group_id);
|
||||
void PostConcat(int group_id);
|
||||
|
||||
private:
|
||||
std::vector<kernel::LiteKernel *> group_convs_;
|
||||
float *ori_in_data_ = nullptr; // do not free
|
||||
float *ori_out_data_ = nullptr; // do not free
|
||||
const int group_num_;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_GROUP_CONVOLUTION_H_
|
Loading…
Reference in new issue