commit
b6f59b41ad
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* 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 <vector>
|
||||
#include "src/runtime/kernel/arm/fp32/pad.h"
|
||||
#include "src/runtime/kernel/arm/int8/pad_int8.h"
|
||||
#include "schema/model_generated.h"
|
||||
#include "src/kernel_factory.h"
|
||||
#include "include/errorcode.h"
|
||||
#include "include/context.h"
|
||||
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_Pad;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
|
||||
kernel::LiteKernel *CpuPadInt8KernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs,
|
||||
OpParameter *opParameter, const lite::Context *ctx,
|
||||
const kernel::KernelKey &desc) {
|
||||
auto *kernel = new (std::nothrow) PadInt8CPUKernel(opParameter, inputs, outputs, ctx);
|
||||
if (kernel == nullptr) {
|
||||
MS_LOG(ERROR) << "new PadCPUKernel failed.";
|
||||
return nullptr;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
kernel::LiteKernel *CpuPadFp32KernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs,
|
||||
OpParameter *opParameter, const lite::Context *ctx,
|
||||
const kernel::KernelKey &desc) {
|
||||
auto *kernel = new (std::nothrow) PadCPUKernel(opParameter, inputs, outputs, ctx);
|
||||
if (kernel == nullptr) {
|
||||
MS_LOG(ERROR) << "new PadCPUKernel failed.";
|
||||
return nullptr;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
kernel::LiteKernel *CpuPadKernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs, OpParameter *opParameter,
|
||||
const lite::Context *ctx, const kernel::KernelKey &desc) {
|
||||
MS_ASSERT(opParameter != nullptr);
|
||||
MS_ASSERT(desc.type == schema::PrimitiveType_Concat);
|
||||
auto input_tensor = inputs.at(kInputIndex);
|
||||
auto data_type = input_tensor->data_type();
|
||||
kernel::LiteKernel *kernel = nullptr;
|
||||
switch (data_type) {
|
||||
case kNumberTypeInt8:
|
||||
kernel = CpuPadInt8KernelCreator(inputs, outputs, opParameter, ctx, desc);
|
||||
break;
|
||||
case kNumberTypeFloat32:
|
||||
kernel = CpuPadFp32KernelCreator(inputs, outputs, opParameter, ctx, desc);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (kernel == nullptr) {
|
||||
MS_LOG(ERROR) << "kernel is nullptr.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto ret = kernel->Init();
|
||||
if (ret != RET_OK) {
|
||||
delete kernel;
|
||||
MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: "
|
||||
<< schema::EnumNamePrimitiveType(static_cast<schema::PrimitiveType>(opParameter->type_));
|
||||
return nullptr;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, PrimitiveType_Pad, CpuPadKernelCreator)
|
||||
} // namespace mindspore::kernel
|
@ -0,0 +1,119 @@
|
||||
/**
|
||||
* 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/int8/pad_int8.h"
|
||||
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_MEMORY_FAILED;
|
||||
using mindspore::lite::RET_OK;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
void PadInt8CPUKernel::FreeQuantParam() {
|
||||
if (pad_param_->pad_quant_arg_.in_quant_args_ != nullptr) {
|
||||
free(pad_param_->pad_quant_arg_.in_quant_args_);
|
||||
pad_param_->pad_quant_arg_.in_quant_args_ = nullptr;
|
||||
}
|
||||
if (pad_param_->pad_quant_arg_.out_quanr_args_ != nullptr) {
|
||||
free(pad_param_->pad_quant_arg_.out_quanr_args_);
|
||||
pad_param_->pad_quant_arg_.out_quanr_args_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int PadInt8CPUKernel::SetQuantParam() {
|
||||
PadQuantArg *pad_quant_args = &pad_param_->pad_quant_arg_;
|
||||
pad_quant_args->in_quant_args_ = reinterpret_cast<QuantArg *>(malloc(sizeof(QuantArg)));
|
||||
if (pad_quant_args->in_quant_args_ == nullptr) {
|
||||
return RET_MEMORY_FAILED;
|
||||
}
|
||||
pad_quant_args->out_quanr_args_ = reinterpret_cast<QuantArg *>(malloc(sizeof(QuantArg)));
|
||||
if (pad_quant_args->out_quanr_args_ == nullptr) {
|
||||
return RET_MEMORY_FAILED;
|
||||
}
|
||||
pad_quant_args->constant_value_ = reinterpret_cast<int8_t *>(malloc(sizeof(int8_t)));
|
||||
if (pad_quant_args->constant_value_ == nullptr) {
|
||||
return RET_MEMORY_FAILED;
|
||||
}
|
||||
|
||||
auto *input_tensor = inputs_.at(kInputIndex);
|
||||
auto *out_tensor = outputs_.at(kOutputIndex);
|
||||
auto in_quant_arg = input_tensor->GetQuantParams();
|
||||
auto out_quant_arg = out_tensor->GetQuantParams();
|
||||
|
||||
pad_quant_args->in_quant_args_->zp_ = in_quant_arg.front().zeroPoint;
|
||||
pad_quant_args->in_quant_args_->scale_ = in_quant_arg.front().scale;
|
||||
pad_quant_args->out_quanr_args_->zp_ = out_quant_arg.front().zeroPoint;
|
||||
pad_quant_args->out_quanr_args_->scale_ = out_quant_arg.front().scale;
|
||||
|
||||
if (pad_quant_args->in_quant_args_->scale_ != pad_quant_args->out_quanr_args_->scale_ ||
|
||||
pad_quant_args->in_quant_args_->zp_ != pad_quant_args->out_quanr_args_->zp_) {
|
||||
MS_LOG(ERROR) << "Pad int8 op : scale & zp of output and input must be equal.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
pad_quant_args->constant_value_[0] = QuantizeToInt8(
|
||||
pad_param_->constant_value_, pad_quant_args->in_quant_args_->scale_, pad_quant_args->in_quant_args_->zp_);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int PadInt8CPUKernel::InitPadParam() {
|
||||
auto in_dims = inputs_[0]->shape();
|
||||
auto out_dims = outputs_[0]->shape();
|
||||
int ndims = in_dims.size();
|
||||
|
||||
int in[] = {1, 1, 1, 1};
|
||||
int out[] = {1, 1, 1, 1};
|
||||
|
||||
for (int i = 0; i < ndims; i++) {
|
||||
in[DEFAULT_PAD_NDIMS - ndims + i] = in_dims[i];
|
||||
out[DEFAULT_PAD_NDIMS - ndims + i] = out_dims[i];
|
||||
}
|
||||
|
||||
memcpy(in_dims_, in, DEFAULT_PAD_NDIMS * sizeof(int));
|
||||
memcpy(out_dims_, out, DEFAULT_PAD_NDIMS * sizeof(int));
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int PadInt8CPUKernel::ReSize() {
|
||||
InitPadParam();
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int PadInt8CPUKernel::Init() {
|
||||
int error_code = InitPadParam();
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "InitPadParam failed. errorcode: " << error_code;
|
||||
return error_code;
|
||||
}
|
||||
|
||||
error_code = SetQuantParam();
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "SetQuantParam failed. errorcode: " << error_code;
|
||||
return error_code;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int PadInt8CPUKernel::Run() {
|
||||
int8_t *in_data = reinterpret_cast<int8_t *>(inputs_[0]->Data());
|
||||
int8_t *out_data = reinterpret_cast<int8_t *>(outputs_[0]->Data());
|
||||
|
||||
memset(out_data, pad_param_->pad_quant_arg_.constant_value_[0], outputs_[0]->ElementsNum() * sizeof(int8_t));
|
||||
PadConstant4D(in_data, out_data, in_dims_, out_dims_, pad_param_->paddings_);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
} // namespace mindspore::kernel
|
@ -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_KERNEL_ARM_INT8_PAD_INT8_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_PAD_INT8_H_
|
||||
|
||||
#include <vector>
|
||||
#include "include/errorcode.h"
|
||||
#include "src/lite_kernel.h"
|
||||
#include "src/runtime/runtime_api.h"
|
||||
#include "src/runtime/kernel/arm/opclib/pad_parameter.h"
|
||||
#include "src/runtime/kernel/arm/opclib/int8/pad.h"
|
||||
|
||||
namespace mindspore::kernel {
|
||||
class PadInt8CPUKernel : public LiteKernel {
|
||||
public:
|
||||
explicit PadInt8CPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
|
||||
const std::vector<lite::tensor::Tensor *> &outputs, const lite::Context *ctx)
|
||||
: LiteKernel(parameter, inputs, outputs) {
|
||||
opParameter->thread_num_ = ctx->threadNum;
|
||||
pad_param_ = reinterpret_cast<PadParameter *>(opParameter);
|
||||
}
|
||||
~PadInt8CPUKernel() override { FreeQuantParam(); };
|
||||
|
||||
int Init() override;
|
||||
int ReSize() override;
|
||||
int Run() override;
|
||||
|
||||
private:
|
||||
int SetQuantParam();
|
||||
int InitPadParam();
|
||||
void FreeQuantParam();
|
||||
|
||||
private:
|
||||
PadParameter *pad_param_;
|
||||
int in_dims_[DEFAULT_PAD_NDIMS];
|
||||
int out_dims_[DEFAULT_PAD_NDIMS];
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_PAD_INT8_H_
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 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/opclib/int8/pad.h"
|
||||
|
||||
void PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_dims, const int32_t *out_dims,
|
||||
const int32_t *paddings) {
|
||||
int32_t copy_size = in_dims[3];
|
||||
for (int n = 0; n < in_dims[0]; n++) {
|
||||
for (int h = 0; h < in_dims[1]; h++) {
|
||||
for (int w = 0; w < in_dims[2]; w++) {
|
||||
const int8_t *in = in_data + offset(in_dims, n, h, w, 0);
|
||||
int8_t *out = out_data + offset(out_dims, n + paddings[0], h + paddings[2], w + paddings[4], paddings[6]);
|
||||
memcpy(out, in, copy_size * sizeof(int8_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* 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_OPCLIB_INT8_PAD_INT8_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_PAD_INT8_H_
|
||||
|
||||
#include <string.h>
|
||||
#include "src/runtime/kernel/arm/opclib/op_base.h"
|
||||
#include "src/runtime/kernel/arm/opclib/offset_utils.h"
|
||||
#include "src/runtime/kernel/arm/opclib/pad_parameter.h"
|
||||
|
||||
void PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_dims, const int32_t *out_dims,
|
||||
const int32_t *paddings);
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_INT8_PAD_INT8_H_
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 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_OPCLIB_PAD_PARAMETER_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_PAD_PARAMETER_H_
|
||||
|
||||
#include "src/runtime/kernel/arm/opclib/op_base.h"
|
||||
|
||||
#define MAX_PAD_SIZE 8
|
||||
#define DEFAULT_PAD_NDIMS 4
|
||||
|
||||
struct PadParameter {
|
||||
OpParameter op_parameter_;
|
||||
PadQuantArg pad_quant_arg_;
|
||||
int paddings_[MAX_PAD_SIZE] = {0};
|
||||
int pad_mode_;
|
||||
float constant_value_;
|
||||
};
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_OPCLIB_PAD_PARAMETER_H_
|
Loading…
Reference in new issue