parent
16d19f2d26
commit
280b84b7aa
@ -1,54 +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/fp16/stack_fp16.h"
|
||||
#include "nnacl/common_func.h"
|
||||
|
||||
size_t Fp16GetStackCopyNum(int axis, int *in_shape, size_t shape_size) {
|
||||
size_t one_input_size = 1;
|
||||
for (size_t i = 0; i < shape_size; ++i) {
|
||||
one_input_size *= in_shape[i];
|
||||
}
|
||||
int in_strides[4];
|
||||
ComputeStrides(in_shape, in_strides, shape_size);
|
||||
|
||||
size_t copy_num = axis > 0 ? in_strides[axis - 1] : one_input_size;
|
||||
return copy_num;
|
||||
}
|
||||
|
||||
size_t Fp16GetStackPreAxisCount2(const int *in_shape, int axis) {
|
||||
size_t pre_axis_count = 1;
|
||||
for (size_t i = 0; i < axis; ++i) {
|
||||
pre_axis_count *= in_shape[i];
|
||||
}
|
||||
return pre_axis_count;
|
||||
}
|
||||
|
||||
void DoStackFp16(const float16_t *const *inputs, size_t input_num, int *in_shape, size_t shape_size, int axis,
|
||||
float16_t *output) {
|
||||
size_t copy_num = Fp16GetStackCopyNum(axis, in_shape, shape_size);
|
||||
size_t copy_size = copy_num * sizeof(float16_t);
|
||||
size_t pre_axis_count = Fp16GetStackPreAxisCount2(in_shape, axis);
|
||||
size_t in_offset = 0;
|
||||
size_t out_offset = 0;
|
||||
for (size_t i = 0; i < pre_axis_count; ++i) {
|
||||
for (size_t j = 0; j < input_num; ++j) {
|
||||
memcpy(output + out_offset, inputs[j] + in_offset, copy_size);
|
||||
out_offset += copy_num;
|
||||
}
|
||||
in_offset += copy_num;
|
||||
}
|
||||
}
|
@ -1,72 +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/stack_fp32.h"
|
||||
#include "nnacl/common_func.h"
|
||||
|
||||
size_t GetStackCopyNum(int axis, const int *in_shape, size_t shape_size) {
|
||||
size_t one_input_size = 1;
|
||||
for (size_t i = 0; i < shape_size; ++i) {
|
||||
one_input_size *= in_shape[i];
|
||||
}
|
||||
int in_strides[4];
|
||||
ComputeStrides(in_shape, in_strides, shape_size);
|
||||
|
||||
size_t copy_num = axis > 0 ? in_strides[axis - 1] : one_input_size;
|
||||
return copy_num;
|
||||
}
|
||||
|
||||
size_t GetStackPreAxisCount(const int *in_shape, int axis) {
|
||||
size_t pre_axis_count = 1;
|
||||
for (size_t i = 0; i < axis; ++i) {
|
||||
pre_axis_count *= in_shape[i];
|
||||
}
|
||||
return pre_axis_count;
|
||||
}
|
||||
|
||||
void DoStack(const float *const *inputs, size_t input_num, const int *in_shape, size_t shape_size, int axis,
|
||||
float *output) {
|
||||
size_t copy_num = GetStackCopyNum(axis, in_shape, shape_size);
|
||||
size_t copy_size = copy_num * sizeof(float);
|
||||
size_t pre_axis_count = GetStackPreAxisCount(in_shape, axis);
|
||||
size_t in_offset = 0;
|
||||
size_t out_offset = 0;
|
||||
for (size_t i = 0; i < pre_axis_count; ++i) {
|
||||
for (size_t j = 0; j < input_num; ++j) {
|
||||
memcpy(output + out_offset, inputs[j] + in_offset, copy_size);
|
||||
out_offset += copy_num;
|
||||
}
|
||||
in_offset += copy_num;
|
||||
}
|
||||
}
|
||||
|
||||
void DoStackInt32(const int32_t *const *inputs, size_t input_num, const int *in_shape, size_t shape_size, int axis,
|
||||
int32_t *output) {
|
||||
size_t copy_num = GetStackCopyNum(axis, in_shape, shape_size);
|
||||
size_t copy_size = copy_num * sizeof(int32_t);
|
||||
size_t pre_axis_count = GetStackPreAxisCount(in_shape, axis);
|
||||
size_t in_offset = 0;
|
||||
size_t out_offset = 0;
|
||||
for (size_t i = 0; i < pre_axis_count; ++i) {
|
||||
for (size_t j = 0; j < input_num; ++j) {
|
||||
memcpy(output + out_offset, inputs[j] + in_offset, copy_size);
|
||||
out_offset += copy_num;
|
||||
}
|
||||
in_offset += copy_num;
|
||||
}
|
||||
}
|
||||
|
||||
void DoStackOneInput(const int8_t *input, int8_t *output, size_t data_size) { memcpy(output, input, data_size); }
|
@ -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 "src/runtime/kernel/arm/base/stack_base.h"
|
||||
#include <vector>
|
||||
#include "schema/model_generated.h"
|
||||
#include "src/kernel_registry.h"
|
||||
#include "nnacl/base/stack_base.h"
|
||||
#include "nnacl/stack_parameter.h"
|
||||
#include "include/errorcode.h"
|
||||
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_Stack;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
static int GetCopyNum(const std::vector<int> &in_shape, int axis, int n_dim) {
|
||||
int copy_num = 1;
|
||||
if (axis > 0) {
|
||||
for (int j = n_dim - 1; j > axis - 1; j--) {
|
||||
copy_num *= in_shape[j];
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < n_dim; ++i) {
|
||||
copy_num *= in_shape[i];
|
||||
}
|
||||
}
|
||||
return copy_num;
|
||||
}
|
||||
|
||||
static size_t GetOutterSize(const std::vector<int> &in_shape, int axis) {
|
||||
size_t outter_size = 1;
|
||||
for (int i = 0; i < axis; ++i) {
|
||||
outter_size *= in_shape[i];
|
||||
}
|
||||
return outter_size;
|
||||
}
|
||||
|
||||
int StackBaseCPUKernel::ReSize() {
|
||||
auto param = reinterpret_cast<StackParameter *>(op_parameter_);
|
||||
auto input0_shape = in_tensors_.front()->shape();
|
||||
axis_ = param->axis_ < 0 ? param->axis_ + input0_shape.size() + 1 : param->axis_;
|
||||
auto input_nums = in_tensors_.size();
|
||||
if (input_nums == 1) {
|
||||
copy_size_ = in_tensors_.front()->Size();
|
||||
} else {
|
||||
MS_ASSERT(input_nums > 1);
|
||||
copy_size_ = GetCopyNum(input0_shape, axis_, input0_shape.size()) * data_type_size_;
|
||||
outter_size_ = GetOutterSize(input0_shape, axis_);
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int StackBaseCPUKernel::Init() {
|
||||
auto input0_tensor = in_tensors_.front();
|
||||
data_type_size_ = input0_tensor->Size() / input0_tensor->ElementsNum();
|
||||
if (!InferShapeDone()) {
|
||||
return RET_OK;
|
||||
}
|
||||
return ReSize();
|
||||
}
|
||||
|
||||
int StackBaseCPUKernel::Run() {
|
||||
size_t inputs_num = in_tensors_.size();
|
||||
char **all_inputs = static_cast<char **>(context_->allocator->Malloc(inputs_num * sizeof(char *)));
|
||||
for (size_t j = 0; j < inputs_num; ++j) {
|
||||
all_inputs[j] = reinterpret_cast<char *>(in_tensors_.at(j)->data_c());
|
||||
}
|
||||
auto output_data = reinterpret_cast<char *>(out_tensors_.at(0)->data_c());
|
||||
Stack(all_inputs, output_data, in_tensors_.size(), copy_size_, outter_size_);
|
||||
context_->allocator->Free(all_inputs);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_Stack, LiteKernelCreator<StackBaseCPUKernel>)
|
||||
REG_KERNEL(kCPU, kNumberTypeInt32, PrimitiveType_Stack, LiteKernelCreator<StackBaseCPUKernel>)
|
||||
} // namespace mindspore::kernel
|
@ -1,81 +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/stack_fp32.h"
|
||||
#include <vector>
|
||||
#include "schema/model_generated.h"
|
||||
#include "src/kernel_registry.h"
|
||||
#include "nnacl/fp32/stack_fp32.h"
|
||||
#include "nnacl/stack_parameter.h"
|
||||
#include "include/errorcode.h"
|
||||
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_Stack;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
int StackCPUKernel::ReSize() {
|
||||
StackParameter *param = reinterpret_cast<StackParameter *>(op_parameter_);
|
||||
auto input0_shape = in_tensors_.at(0)->shape();
|
||||
axis_ = param->axis_ < 0 ? param->axis_ + input0_shape.size() + 1 : param->axis_;
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int StackCPUKernel::Init() {
|
||||
if (!InferShapeDone()) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
return ReSize();
|
||||
}
|
||||
|
||||
int StackCPUKernel::Run() {
|
||||
size_t inputs_num = in_tensors_.size();
|
||||
auto input0 = in_tensors_.at(0);
|
||||
if (inputs_num == 1) {
|
||||
auto *output_data = reinterpret_cast<int8_t *>(out_tensors_.at(0)->MutableData());
|
||||
MS_ASSERT(output_data);
|
||||
auto *input_data = reinterpret_cast<const int8_t *>(input0->MutableData());
|
||||
MS_ASSERT(input_data);
|
||||
DoStackOneInput(input_data, output_data, input0->Size());
|
||||
return RET_OK;
|
||||
}
|
||||
auto input0_shape = in_tensors_.at(0)->shape();
|
||||
if (in_tensors_.at(0)->data_type() == kNumberTypeFloat32 || in_tensors_.at(0)->data_type() == kNumberTypeFloat) {
|
||||
auto *output_data = reinterpret_cast<float *>(out_tensors_.at(0)->MutableData());
|
||||
MS_ASSERT(output_data);
|
||||
float *inputs[inputs_num];
|
||||
for (size_t i = 0; i < inputs_num; ++i) {
|
||||
inputs[i] = reinterpret_cast<float *>(in_tensors_.at(i)->MutableData());
|
||||
MS_ASSERT(inputs[i]);
|
||||
}
|
||||
DoStack(inputs, inputs_num, input0_shape.data(), input0_shape.size(), axis_, output_data);
|
||||
} else {
|
||||
auto *output_data = reinterpret_cast<int32_t *>(out_tensors_.at(0)->MutableData());
|
||||
MS_ASSERT(output_data);
|
||||
int32_t *inputs[inputs_num];
|
||||
for (size_t i = 0; i < inputs_num; ++i) {
|
||||
inputs[i] = reinterpret_cast<int32_t *>(in_tensors_.at(i)->MutableData());
|
||||
MS_ASSERT(inputs[i]);
|
||||
}
|
||||
DoStackInt32(inputs, inputs_num, input0_shape.data(), input0_shape.size(), axis_, output_data);
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_Stack, LiteKernelCreator<StackCPUKernel>)
|
||||
REG_KERNEL(kCPU, kNumberTypeInt32, PrimitiveType_Stack, LiteKernelCreator<StackCPUKernel>)
|
||||
} // namespace mindspore::kernel
|
Loading…
Reference in new issue