parent
74dc7d069d
commit
7454b9688c
@ -1,102 +0,0 @@
|
||||
/**
|
||||
* 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 "nnacl/arg_min_max.h"
|
||||
#include "nnacl/fp32/arg_min_max_fp32.h"
|
||||
|
||||
#define FLOAT_DATA_TYPE 43
|
||||
|
||||
void GetCalcParameter(const int *shape, int dims_number, int axis, int *pre_axis_count, int *axis_count,
|
||||
int *after_axis_count) {
|
||||
*pre_axis_count = 1;
|
||||
for (int i = 0; i < axis; ++i) {
|
||||
*pre_axis_count = (*pre_axis_count) * shape[i];
|
||||
}
|
||||
|
||||
*axis_count = shape[axis];
|
||||
|
||||
*after_axis_count = 1;
|
||||
for (int i = axis + 1; i < dims_number; ++i) {
|
||||
*after_axis_count = (*after_axis_count) * shape[i];
|
||||
}
|
||||
}
|
||||
|
||||
void ArgMinMaxTopk1(const void *input, void *output, const int *shape, const ArgMinMaxParameter *param) {
|
||||
int pre_axis_count = 1;
|
||||
int axis_count = 1;
|
||||
int after_axis_count = 1;
|
||||
GetCalcParameter(shape, param->dims_size_, param->axis_, &pre_axis_count, &axis_count, &after_axis_count);
|
||||
if (param->data_type_ != FLOAT_DATA_TYPE) {
|
||||
return;
|
||||
}
|
||||
if (param->get_max_) {
|
||||
ArgMax(input, output, param, pre_axis_count, axis_count, after_axis_count);
|
||||
} else {
|
||||
ArgMin(input, output, param, pre_axis_count, axis_count, after_axis_count);
|
||||
}
|
||||
}
|
||||
|
||||
void ArgMinMaxTopknFp32(const float *input, float *output, const int *in_shape, const ArgMinMaxParameter *param) {
|
||||
if (param->get_max_) {
|
||||
switch (param->axis_) {
|
||||
case 0:
|
||||
ArgMaxDim0(input, output, in_shape, param);
|
||||
break;
|
||||
case 1:
|
||||
ArgMaxDim1(input, output, in_shape, param);
|
||||
break;
|
||||
case 2:
|
||||
ArgMaxDim2(input, output, in_shape, param);
|
||||
break;
|
||||
case 3:
|
||||
ArgMaxDim3(input, output, in_shape, param);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (param->axis_) {
|
||||
case 0:
|
||||
ArgMinDim0(input, output, in_shape, param);
|
||||
break;
|
||||
case 1:
|
||||
ArgMinDim1(input, output, in_shape, param);
|
||||
break;
|
||||
case 2:
|
||||
ArgMinDim2(input, output, in_shape, param);
|
||||
break;
|
||||
case 3:
|
||||
ArgMinDim3(input, output, in_shape, param);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ArgMinMax(const void *input, void *output, const int *in_shape, const ArgMinMaxParameter *param) {
|
||||
if (param->topk_ == 1) {
|
||||
ArgMinMaxTopk1(input, output, in_shape, param);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (param->data_type_) {
|
||||
case FLOAT_DATA_TYPE: {
|
||||
ArgMinMaxTopknFp32(input, output, in_shape, param);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#undef FLOAT_DATA_TYPE
|
||||
#undef INT8_DATA_TYPE
|
@ -1,29 +0,0 @@
|
||||
/**
|
||||
* 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_NNACL_ARG_MIN_MAX_H_
|
||||
#define MINDSPORE_LITE_NNACL_ARG_MIN_MAX_H_
|
||||
|
||||
#include "nnacl/arg_min_max_parameter.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
void ArgMinMax(const void *input, void *output, const int *in_shape, const ArgMinMaxParameter *param);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // MINDSPORE_LITE_NNACL_ARG_MIN_MAX_H_
|
File diff suppressed because it is too large
Load Diff
@ -1,47 +0,0 @@
|
||||
/**
|
||||
* 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 "nnacl/fp32/instance_norm_fp32.h"
|
||||
#include <math.h>
|
||||
#include "nnacl/errorcode.h"
|
||||
#include "nnacl/op_base.h"
|
||||
|
||||
int InstanceNorm(int outer_size, int inner_size, const float *src_data, const float *scale_data, const float *bias_data,
|
||||
const InstanceNormParameter *param, float *dst_data, int task_id, int thread_num) {
|
||||
if (src_data == NULL || dst_data == NULL || scale_data == NULL || bias_data == NULL) {
|
||||
return NNACL_NULL_PTR;
|
||||
}
|
||||
for (int j = task_id; j < outer_size; j += thread_num) {
|
||||
int offset = (j / param->channel_) * inner_size * param->channel_;
|
||||
const float *src = src_data + offset;
|
||||
float *dst = dst_data + offset;
|
||||
float mean = 0.0f;
|
||||
float square_mean = 0.0f;
|
||||
for (int i = 0; i < inner_size; i++) {
|
||||
int idx = j % param->channel_ + i * param->channel_;
|
||||
mean += src[idx];
|
||||
square_mean += src[idx] * src[idx];
|
||||
}
|
||||
mean /= (float)inner_size;
|
||||
square_mean /= (float)inner_size;
|
||||
const float deno = 1 / sqrtf(square_mean - mean * mean + param->epsilon_);
|
||||
for (int i = 0; i < inner_size; ++i) {
|
||||
int idx = j % param->channel_ + i * param->channel_;
|
||||
int scale_idx = (j / param->channel_) * param->channel_ + j % param->channel_;
|
||||
dst[idx] = ((src[idx] - mean) * deno) * scale_data[scale_idx] + bias_data[scale_idx];
|
||||
}
|
||||
}
|
||||
return NNACL_OK;
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
/**
|
||||
* 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_NNACL_FP32_INSTANCE_NORM_H_
|
||||
#define MINDSPORE_LITE_NNACL_FP32_INSTANCE_NORM_H_
|
||||
|
||||
#include "nnacl/op_base.h"
|
||||
#include "nnacl/instance_norm_parameter.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int InstanceNorm(int outer_size, int inner_size, const float *src_data, const float *scale_data, const float *bias_data,
|
||||
const InstanceNormParameter *param, float *dst_data, int task_id, int thread_num);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // MINDSPORE_LITE_NNACL_FP32_INSTANCE_NORM_H_
|
@ -1,118 +0,0 @@
|
||||
/**
|
||||
* 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/base/arg_min_max_base.h"
|
||||
#include "nnacl/arg_min_max.h"
|
||||
#include "src/runtime/kernel/arm/fp32/argminmax_fp32.h"
|
||||
#include "nnacl/arithmetic_common.h"
|
||||
#include "schema/model_generated.h"
|
||||
#include "src/kernel_registry.h"
|
||||
#include "include/errorcode.h"
|
||||
#include "include/context.h"
|
||||
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_FORMAT_ERR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::lite::RET_PARAM_INVALID;
|
||||
using mindspore::schema::PrimitiveType_ArgMax;
|
||||
using mindspore::schema::PrimitiveType_ArgMin;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int ArgMinMaxBaseCPUKernel::Init() {
|
||||
auto param = reinterpret_cast<ArgMinMaxParameter *>(op_parameter_);
|
||||
switch (op_parameter_->type_) {
|
||||
case PrimitiveType_ArgMax:
|
||||
param->get_max_ = true;
|
||||
break;
|
||||
case PrimitiveType_ArgMin:
|
||||
param->get_max_ = false;
|
||||
break;
|
||||
default:
|
||||
MS_LOG(ERROR) << "Unexpected type " << op_parameter_->type_;
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int ArgMinMaxBaseCPUKernel::ReSize() {
|
||||
auto in_shape = in_tensors_.at(0)->shape();
|
||||
auto dims_size = in_shape.size();
|
||||
auto param = reinterpret_cast<ArgMinMaxParameter *>(op_parameter_);
|
||||
int axis = param->axis_ < 0 ? param->axis_ + dims_size : param->axis_;
|
||||
param->axis_ = axis;
|
||||
param->dims_size_ = dims_size;
|
||||
if (param->topk_ <= 0) {
|
||||
MS_LOG(ERROR) << "Invalid topk " << param->topk_;
|
||||
return RET_PARAM_INVALID;
|
||||
}
|
||||
param->topk_ = MSMIN(param->topk_, in_shape.at(axis));
|
||||
ComputeStrides(in_shape.data(), param->in_strides_, in_shape.size());
|
||||
auto out_shape = out_tensors_.at(0)->shape();
|
||||
ComputeStrides(out_shape.data(), param->out_strides_, out_shape.size());
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int ArgMinMaxBaseCPUKernel::Run() {
|
||||
auto input_data = in_tensors_.at(0)->MutableData();
|
||||
auto output_data = out_tensors_.at(0)->MutableData();
|
||||
|
||||
auto shape = in_tensors_.at(0)->shape();
|
||||
|
||||
auto param = reinterpret_cast<ArgMinMaxParameter *>(op_parameter_);
|
||||
MS_ASSERT(context_->allocator != nullptr);
|
||||
if (param->topk_ > 1 || param->keep_dims_) {
|
||||
param->arg_elements_ =
|
||||
reinterpret_cast<ArgElement *>(context_->allocator->Malloc(sizeof(ArgElement) * shape[param->axis_]));
|
||||
if (param->arg_elements_ == nullptr) {
|
||||
MS_LOG(ERROR) << "malloc memroy fail!";
|
||||
return RET_ERROR;
|
||||
}
|
||||
}
|
||||
ArgMinMax(input_data, output_data, reinterpret_cast<const int *>(shape.data()), param);
|
||||
context_->allocator->Free(param->arg_elements_);
|
||||
param->arg_elements_ = nullptr;
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
kernel::LiteKernel *CpuArgMinMaxFp32KernelCreator(const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs, OpParameter *op_parameter,
|
||||
const lite::InnerContext *ctx, const kernel::KernelKey &desc,
|
||||
const mindspore::lite::PrimitiveC *primitive) {
|
||||
if (op_parameter == nullptr) {
|
||||
MS_LOG(ERROR) << "Input op_parameter is nullptr!";
|
||||
return nullptr;
|
||||
}
|
||||
auto kernel = new (std::nothrow) ArgMinMaxCPUKernel(op_parameter, inputs, outputs, ctx, primitive);
|
||||
if (kernel == nullptr) {
|
||||
MS_LOG(ERROR) << "new ArgMinMaxCPUKernel fail!";
|
||||
free(op_parameter);
|
||||
return nullptr;
|
||||
}
|
||||
auto ret = kernel->Init();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Init kernel failed, name: " << op_parameter->name_ << ", type: "
|
||||
<< schema::EnumNamePrimitiveType(static_cast<schema::PrimitiveType>(op_parameter->type_));
|
||||
delete kernel;
|
||||
return nullptr;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_ArgMax, CpuArgMinMaxFp32KernelCreator)
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_ArgMin, CpuArgMinMaxFp32KernelCreator)
|
||||
|
||||
} // namespace mindspore::kernel
|
@ -1,41 +0,0 @@
|
||||
/**
|
||||
* 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_BASE_ARG_MIN_MAX_BASE_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_ARG_MIN_MAX_BASE_H_
|
||||
|
||||
#include <vector>
|
||||
#include "src/lite_kernel.h"
|
||||
|
||||
namespace mindspore::kernel {
|
||||
class ArgMinMaxBaseCPUKernel : public LiteKernel {
|
||||
public:
|
||||
ArgMinMaxBaseCPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx,
|
||||
const mindspore::lite::PrimitiveC *primitive)
|
||||
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {}
|
||||
|
||||
virtual ~ArgMinMaxBaseCPUKernel() = default;
|
||||
|
||||
int Init() override;
|
||||
int ReSize() override;
|
||||
int Run() override;
|
||||
|
||||
private:
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_BASE_ARG_MIN_MAX_BASE_H_
|
@ -1,107 +0,0 @@
|
||||
/**
|
||||
* 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/instance_norm_fp32.h"
|
||||
#include <vector>
|
||||
#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_InstanceNorm;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int InstanceNormCPUKernel::Init() {
|
||||
if (!InferShapeDone()) {
|
||||
return RET_OK;
|
||||
}
|
||||
return ReSize();
|
||||
}
|
||||
|
||||
int InstanceNormCPUKernel::ReSize() {
|
||||
auto input_shapes = in_tensors_.front()->shape();
|
||||
auto n_dim = input_shapes.size();
|
||||
outer_size_ = input_shapes.at(0) * input_shapes.at(n_dim - 1);
|
||||
inner_size_ = 1;
|
||||
for (size_t i = 0; i < n_dim - 1; ++i) {
|
||||
inner_size_ *= input_shapes.at(i);
|
||||
}
|
||||
param_->channel_ = input_shapes.at(n_dim - 1);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int InstanceNormCPUKernel::DoInstanceNorm(int task_id) {
|
||||
int ret = InstanceNorm(outer_size_, inner_size_, src_data_, scale_data_, bias_data_, param_, dst_data_, task_id,
|
||||
op_parameter_->thread_num_);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "DoInstanceNorm error error_code[" << ret << "]";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int InstanceNormRun(void *cdata, int task_id) {
|
||||
auto kernel = reinterpret_cast<InstanceNormCPUKernel *>(cdata);
|
||||
auto ret = kernel->DoInstanceNorm(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "InstanceNormRun error task_id[" << task_id << "] error_code[" << ret << "]";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int InstanceNormCPUKernel::Run() {
|
||||
src_data_ = reinterpret_cast<float *>(in_tensors_.at(0)->MutableData());
|
||||
scale_data_ = reinterpret_cast<float *>(in_tensors_.at(1)->MutableData());
|
||||
bias_data_ = reinterpret_cast<float *>(in_tensors_.at(2)->MutableData());
|
||||
dst_data_ = reinterpret_cast<float *>(out_tensors_.at(0)->MutableData());
|
||||
auto ret = ParallelLaunch(this->context_->thread_pool_, InstanceNormRun, this, op_parameter_->thread_num_);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "FillRun error error_code[" << ret << "]";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
kernel::LiteKernel *CpuInstanceNormFp32KernelCreator(const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs,
|
||||
OpParameter *opParameter, const lite::InnerContext *ctx,
|
||||
const kernel::KernelKey &desc,
|
||||
const mindspore::lite::PrimitiveC *primitive) {
|
||||
if (opParameter == nullptr) {
|
||||
MS_LOG(ERROR) << "Create kernel failed, opParameter is nullptr, type: PrimitiveType_InstanceNorm. ";
|
||||
return nullptr;
|
||||
}
|
||||
MS_ASSERT(desc.type == schema::PrimitiveType_InstanceNorm);
|
||||
auto *kernel = new (std::nothrow) InstanceNormCPUKernel(opParameter, inputs, outputs, ctx, primitive);
|
||||
if (kernel == nullptr) {
|
||||
MS_LOG(ERROR) << "new InstanceNormCPUKernel fail!";
|
||||
free(opParameter);
|
||||
return nullptr;
|
||||
}
|
||||
auto ret = kernel->Init();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: "
|
||||
<< schema::EnumNamePrimitiveType(static_cast<schema::PrimitiveType>(opParameter->type_));
|
||||
delete kernel;
|
||||
return nullptr;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_InstanceNorm, CpuInstanceNormFp32KernelCreator)
|
||||
} // namespace mindspore::kernel
|
@ -1,53 +0,0 @@
|
||||
/**
|
||||
* 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_INSTANCE_NORM_H_
|
||||
#define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_INSTANCE_NORM_H_
|
||||
|
||||
#include <vector>
|
||||
#include "src/lite_kernel.h"
|
||||
#include "include/context.h"
|
||||
#include "nnacl/fp32/instance_norm_fp32.h"
|
||||
|
||||
using mindspore::lite::InnerContext;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
class InstanceNormCPUKernel : public LiteKernel {
|
||||
public:
|
||||
InstanceNormCPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
|
||||
const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx,
|
||||
const mindspore::lite::PrimitiveC *primitive)
|
||||
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {
|
||||
param_ = reinterpret_cast<InstanceNormParameter *>(parameter);
|
||||
}
|
||||
~InstanceNormCPUKernel() override{};
|
||||
|
||||
int Init() override;
|
||||
int ReSize() override;
|
||||
int Run() override;
|
||||
int DoInstanceNorm(int thread_id);
|
||||
|
||||
private:
|
||||
InstanceNormParameter *param_ = nullptr;
|
||||
int outer_size_;
|
||||
int inner_size_;
|
||||
float *src_data_ = nullptr;
|
||||
float *dst_data_ = nullptr;
|
||||
float *scale_data_ = nullptr;
|
||||
float *bias_data_ = nullptr;
|
||||
};
|
||||
} // namespace mindspore::kernel
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_FP32_INSTANCE_NORM_H_
|
File diff suppressed because it is too large
Load Diff
@ -1,134 +0,0 @@
|
||||
/**
|
||||
* 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 <iostream>
|
||||
#include "src/common/log_adapter.h"
|
||||
#include "common/common_test.h"
|
||||
#include "mindspore/lite/nnacl/fp32/instance_norm_fp32.h"
|
||||
#include "mindspore/lite/src/kernel_registry.h"
|
||||
#include "mindspore/lite/src/lite_kernel.h"
|
||||
|
||||
namespace mindspore {
|
||||
class TestInstanceNormFp32 : public mindspore::CommonTest {
|
||||
public:
|
||||
TestInstanceNormFp32() {}
|
||||
};
|
||||
|
||||
TEST_F(TestInstanceNormFp32, INTest1) {
|
||||
std::vector<float> in_data = {-11.18675, 11.433986, 11.386012, 11.245945, -2.7614849, 14.692399,
|
||||
-1.1983503, -6.6790967, 6.383416, -13.3213005, -8.693595, 9.476344};
|
||||
std::vector<float> in_data1 = {12.352293, 5.122387, 14.249514};
|
||||
std::vector<float> in_data2 = {14.632595, 0.70900035, 11.179003};
|
||||
|
||||
InstanceNormParameter op_param;
|
||||
op_param.op_parameter_.type_ = schema::PrimitiveType_InstanceNorm;
|
||||
op_param.epsilon_ = 0.001f;
|
||||
|
||||
lite::Tensor input0_tensor(kNumberTypeFloat32, {1, 2, 2, 3});
|
||||
lite::Tensor input1_tensor(kNumberTypeFloat32, {3});
|
||||
lite::Tensor input2_tensor(kNumberTypeFloat32, {3});
|
||||
input0_tensor.set_data(in_data.data());
|
||||
input1_tensor.set_data(in_data1.data());
|
||||
input2_tensor.set_data(in_data2.data());
|
||||
std::vector<lite::Tensor *> inputs_tensor = {&input0_tensor, &input1_tensor, &input2_tensor};
|
||||
|
||||
std::vector<float> output(12);
|
||||
std::vector<float> corr_out = {5.0145645, 9.248516, 15.439679, 33.51017, 0.0012711287, 31.0666883,
|
||||
17.70254, -2.5507483, -8.204435, 2.3031063, -3.8630369, 6.4138837};
|
||||
|
||||
lite::Tensor output0_tensor(kNumberTypeFloat32, {1, 2, 2, 3});
|
||||
output0_tensor.set_data(output.data());
|
||||
std::vector<lite::Tensor *> outputs_tensor = {&output0_tensor};
|
||||
|
||||
kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_InstanceNorm};
|
||||
auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc);
|
||||
ASSERT_NE(creator, nullptr);
|
||||
lite::InnerContext ctx;
|
||||
ctx.thread_num_ = 4;
|
||||
ASSERT_EQ(lite::RET_OK, ctx.Init());
|
||||
kernel::LiteKernel *kernel =
|
||||
creator(inputs_tensor, outputs_tensor, reinterpret_cast<OpParameter *>(&op_param), &ctx, desc, nullptr);
|
||||
ASSERT_NE(kernel, nullptr);
|
||||
auto output_tensor_shape = output0_tensor.shape();
|
||||
kernel->Run();
|
||||
|
||||
printf("==================output data=================\n");
|
||||
for (int i = 0; i < output0_tensor.ElementsNum(); i++) {
|
||||
std::cout << output[i] << " ,";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
ASSERT_EQ(0, CompareOutputData(output.data(), corr_out.data(), output0_tensor.ElementsNum(), 0.001));
|
||||
|
||||
input0_tensor.set_data(nullptr);
|
||||
input1_tensor.set_data(nullptr);
|
||||
input2_tensor.set_data(nullptr);
|
||||
output0_tensor.set_data(nullptr);
|
||||
}
|
||||
|
||||
TEST_F(TestInstanceNormFp32, INTest2) {
|
||||
std::vector<float> in_data = {-11.18675, 11.433986, 11.386012, 11.245945, -2.7614849, 14.692399,
|
||||
-1.1983503, -6.6790967, 6.383416, -13.3213005, -8.693595, 9.476344,
|
||||
-12.18675, 12.433986, 12.386012, 12.245945, -3.7614849, 15.692399,
|
||||
-2.1983503, -7.6790967, 7.383416, -14.3213005, -9.693595, 10.476344};
|
||||
std::vector<float> in_data1 = {12.352293, 5.122387, 14.249514, 12.352293, 5.122387, 14.249514};
|
||||
std::vector<float> in_data2 = {14.632595, 0.70900035, 11.179003, 14.632595, 0.70900035, 11.179003};
|
||||
|
||||
InstanceNormParameter op_param;
|
||||
op_param.op_parameter_.type_ = schema::PrimitiveType_InstanceNorm;
|
||||
op_param.epsilon_ = 0.001f;
|
||||
|
||||
lite::Tensor input0_tensor(kNumberTypeFloat32, {2, 2, 2, 3});
|
||||
lite::Tensor input1_tensor(kNumberTypeFloat32, {2, 3});
|
||||
lite::Tensor input2_tensor(kNumberTypeFloat32, {2, 3});
|
||||
input0_tensor.set_data(in_data.data());
|
||||
input1_tensor.set_data(in_data1.data());
|
||||
input2_tensor.set_data(in_data2.data());
|
||||
std::vector<lite::Tensor *> inputs_tensor = {&input0_tensor, &input1_tensor, &input2_tensor};
|
||||
|
||||
std::vector<float> output(24);
|
||||
std::vector<float> corr_out = {5.0145645, 9.248516, 15.439679, 33.51017, 0.0012711287, 31.0666883,
|
||||
17.70254, -2.5507483, -8.204435, 2.3031063, -3.8630369, 6.4138837,
|
||||
5.133601, 9.310399, 15.439679, 33.886883, -0.22505027, 31.066883,
|
||||
16.888313, -2.5316327, -8.204435, 2.6215858, -3.717714, 6.4138837};
|
||||
|
||||
lite::Tensor output0_tensor(kNumberTypeFloat32, {2, 2, 2, 3});
|
||||
output0_tensor.set_data(output.data());
|
||||
std::vector<lite::Tensor *> outputs_tensor = {&output0_tensor};
|
||||
|
||||
kernel::KernelKey desc = {kernel::KERNEL_ARCH::kCPU, kNumberTypeFloat32, schema::PrimitiveType_InstanceNorm};
|
||||
auto creator = lite::KernelRegistry::GetInstance()->GetCreator(desc);
|
||||
ASSERT_NE(creator, nullptr);
|
||||
lite::InnerContext ctx;
|
||||
ctx.thread_num_ = 4;
|
||||
ASSERT_EQ(lite::RET_OK, ctx.Init());
|
||||
kernel::LiteKernel *kernel =
|
||||
creator(inputs_tensor, outputs_tensor, reinterpret_cast<OpParameter *>(&op_param), &ctx, desc, nullptr);
|
||||
ASSERT_NE(kernel, nullptr);
|
||||
auto output_tensor_shape = output0_tensor.shape();
|
||||
kernel->Run();
|
||||
|
||||
printf("==================output data=================\n");
|
||||
for (int i = 0; i < output0_tensor.ElementsNum(); i++) {
|
||||
std::cout << output[i] << " ,";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
ASSERT_EQ(0, CompareOutputData(output.data(), corr_out.data(), output0_tensor.ElementsNum(), 0.001));
|
||||
|
||||
input0_tensor.set_data(nullptr);
|
||||
input1_tensor.set_data(nullptr);
|
||||
input2_tensor.set_data(nullptr);
|
||||
output0_tensor.set_data(nullptr);
|
||||
}
|
||||
} // namespace mindspore
|
Loading…
Reference in new issue