commit
9bb4785777
@ -0,0 +1,110 @@
|
||||
/**
|
||||
* 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 "backend/kernel_compiler/cpu/unpack_cpu_kernel.h"
|
||||
#include "runtime/device/cpu/cpu_device_address.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace kernel {
|
||||
template <typename T>
|
||||
void UnpackCPUKernel<T>::InitKernel(const CNodePtr &kernel_node) {
|
||||
CheckParam(kernel_node);
|
||||
int64_t axis_tmp = AnfAlgo::GetNodeAttr<int64_t>(kernel_node, "axis");
|
||||
auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
|
||||
if (axis_tmp < 0) {
|
||||
axis_tmp += SizeToLong(input_shape.size());
|
||||
}
|
||||
size_t axis_ = LongToSize(axis_tmp);
|
||||
output_num_ = LongToSize(AnfAlgo::GetNodeAttr<int64_t>(kernel_node, "num"));
|
||||
for (size_t i = 0; i < input_shape.size(); i++) {
|
||||
input_size_ *= input_shape[i];
|
||||
if (i > IntToSize(axis_)) {
|
||||
dims_after_axis_ *= input_shape[i];
|
||||
}
|
||||
}
|
||||
dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void UnpackCPUKernel<T>::InitInputOutputSize(const CNodePtr &kernel_node) {
|
||||
CPUKernel::InitInputOutputSize(kernel_node);
|
||||
workspace_size_list_.emplace_back(sizeof(T *) * output_num_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool UnpackCPUKernel<T>::Launch(const std::vector<kernel::AddressPtr> &inputs,
|
||||
const std::vector<kernel::AddressPtr> &workspace,
|
||||
const std::vector<kernel::AddressPtr> &outputs) {
|
||||
LaunchKernel(inputs, workspace, outputs);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void UnpackCPUKernel<T>::LaunchKernel(const std::vector<AddressPtr> &inputs,
|
||||
const std::vector<kernel::AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs) {
|
||||
input_ = reinterpret_cast<T *>(inputs[0]->addr);
|
||||
MS_EXCEPTION_IF_NULL(input_);
|
||||
outputs_host_ = reinterpret_cast<T **>(workspace[0]->addr);
|
||||
MS_EXCEPTION_IF_NULL(outputs_host_);
|
||||
for (size_t i = 0; i < outputs.size(); i++) {
|
||||
outputs_host_[i] = reinterpret_cast<T *>(outputs[i]->addr);
|
||||
MS_EXCEPTION_IF_NULL(outputs_host_[i]);
|
||||
}
|
||||
auto max_thread_num = std::thread::hardware_concurrency();
|
||||
size_t thread_num = input_size_ < 128 * max_thread_num ? std::ceil(input_size_ / 128.0) : max_thread_num;
|
||||
if (thread_num < 1) {
|
||||
MS_LOG(ERROR) << "Invalid value: thread_num" << thread_num;
|
||||
return;
|
||||
}
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(thread_num);
|
||||
size_t start = 0;
|
||||
size_t one_gap = (input_size_ + thread_num - 1) / thread_num;
|
||||
if (one_gap < 1) {
|
||||
MS_LOG(ERROR) << "Invalid value: one_gap " << one_gap;
|
||||
return;
|
||||
}
|
||||
while (start < input_size_) {
|
||||
size_t end = (start + one_gap) > input_size_ ? input_size_ : (start + one_gap);
|
||||
threads.emplace_back(std::thread(&UnpackCPUKernel::UnpackResult, this, start, end));
|
||||
start += one_gap;
|
||||
}
|
||||
for (size_t i = 0; i < threads.size(); ++i) {
|
||||
threads[i].join();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void UnpackCPUKernel<T>::UnpackResult(const size_t start, const size_t end) {
|
||||
for (size_t i = start; i < end; ++i) {
|
||||
size_t output_index = (i / dims_after_axis_) % output_num_;
|
||||
size_t number_of_reset = output_num_ * dims_after_axis_;
|
||||
size_t tensor_index = i / number_of_reset * dims_after_axis_ + i % dims_after_axis_;
|
||||
outputs_host_[output_index][tensor_index] = input_[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void UnpackCPUKernel<T>::CheckParam(const CNodePtr &kernel_node) {
|
||||
MS_EXCEPTION_IF_NULL(kernel_node);
|
||||
size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
|
||||
if (input_num != 1) {
|
||||
MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but UnpackCPUKernel needs 1 input.";
|
||||
}
|
||||
}
|
||||
} // namespace kernel
|
||||
} // namespace mindspore
|
@ -0,0 +1,88 @@
|
||||
/**
|
||||
* 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_CCSRC_BACKEND_KERNEL_COMPILER_CPU_UNPACK_CPU_KERNEL_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_UNPACK_CPU_KERNEL_H_
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "backend/kernel_compiler/cpu/cpu_kernel.h"
|
||||
#include "backend/kernel_compiler/cpu/cpu_kernel_factory.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace kernel {
|
||||
template <typename T>
|
||||
class UnpackCPUKernel : public CPUKernel {
|
||||
public:
|
||||
UnpackCPUKernel() = default;
|
||||
~UnpackCPUKernel() override = default;
|
||||
|
||||
void InitKernel(const CNodePtr &kernel_node) override;
|
||||
bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs) override;
|
||||
void LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs);
|
||||
void InitInputOutputSize(const CNodePtr &kernel_node) override;
|
||||
|
||||
protected:
|
||||
virtual void CheckParam(const CNodePtr &kernel_node);
|
||||
virtual void UnpackResult(const size_t start, const size_t end);
|
||||
size_t input_size_{1};
|
||||
size_t output_num_{0};
|
||||
size_t dims_after_axis_{1};
|
||||
T *input_{nullptr};
|
||||
T **outputs_host_{nullptr};
|
||||
TypeId dtype_{kTypeUnknown};
|
||||
};
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8),
|
||||
UnpackCPUKernel, int8_t);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16),
|
||||
UnpackCPUKernel, int16_t);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32),
|
||||
UnpackCPUKernel, int);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64),
|
||||
UnpackCPUKernel, int64_t);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeBool).AddOutputAttr(kNumberTypeBool),
|
||||
UnpackCPUKernel, bool);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeUInt8).AddOutputAttr(kNumberTypeUInt8),
|
||||
UnpackCPUKernel, uint8_t);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeUInt16).AddOutputAttr(kNumberTypeUInt16),
|
||||
UnpackCPUKernel, uint16_t);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeUInt32).AddOutputAttr(kNumberTypeUInt32),
|
||||
UnpackCPUKernel, uint32_t);
|
||||
MS_REG_CPU_KERNEL_T(Unpack,
|
||||
KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeUInt64).AddOutputAttr(kNumberTypeUInt64),
|
||||
UnpackCPUKernel, uint64_t);
|
||||
MS_REG_CPU_KERNEL_T(
|
||||
Unpack, KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeFloat16).AddOutputAttr(kNumberTypeFloat16),
|
||||
UnpackCPUKernel, float16);
|
||||
MS_REG_CPU_KERNEL_T(
|
||||
Unpack, KernelAttr().SetAllSameAttr(true).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32),
|
||||
UnpackCPUKernel, float);
|
||||
} // namespace kernel
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_UNPACK_CPU_KERNEL_H_
|
@ -0,0 +1,215 @@
|
||||
# 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.
|
||||
# ============================================================================
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import mindspore.context as context
|
||||
import mindspore.nn as nn
|
||||
import mindspore.ops.operations.array_ops as P
|
||||
from mindspore import Tensor
|
||||
from mindspore.common.api import ms_function
|
||||
from mindspore.common.initializer import initializer
|
||||
from mindspore.common.parameter import Parameter
|
||||
|
||||
|
||||
class Net(nn.Cell):
|
||||
def __init__(self, nptype):
|
||||
super(Net, self).__init__()
|
||||
|
||||
self.unpack = P.Unpack(axis=3)
|
||||
self.data_np = np.array([[[[[0, 0],
|
||||
[-2, -1]],
|
||||
[[0, 0],
|
||||
[0, 1]]],
|
||||
[[[0, 0],
|
||||
[2, 3]],
|
||||
[[0, 0],
|
||||
[4, 5]]],
|
||||
[[[0, 0],
|
||||
[6, 7]],
|
||||
[[0, 0],
|
||||
[8, 9]]]],
|
||||
[[[[0, 0],
|
||||
[10, 11]],
|
||||
[[0, 0],
|
||||
[12, 13]]],
|
||||
[[[0, 0],
|
||||
[14, 15]],
|
||||
[[0, 0],
|
||||
[16, 17]]],
|
||||
[[[0, 0],
|
||||
[18, 19]],
|
||||
[[0, 0],
|
||||
[20, 21]]]],
|
||||
[[[[0, 0],
|
||||
[22, 23]],
|
||||
[[0, 0],
|
||||
[24, 25]]],
|
||||
[[[0, 0],
|
||||
[26, 27]],
|
||||
[[0, 0],
|
||||
[28, 29]]],
|
||||
[[[0, 0],
|
||||
[30, 31]],
|
||||
[[0, 0],
|
||||
[32, 33]]]]]).astype(nptype)
|
||||
self.x1 = Parameter(initializer(Tensor(self.data_np), [3, 3, 2, 2, 2]), name='x1')
|
||||
|
||||
@ms_function
|
||||
def construct(self):
|
||||
return self.unpack(self.x1)
|
||||
|
||||
|
||||
def unpack(nptype):
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
|
||||
unpack_ = Net(nptype)
|
||||
output = unpack_()
|
||||
expect = (np.reshape(np.array([0] * 36).astype(nptype), (3, 3, 2, 2)),
|
||||
np.arange(-2, 34, 1).reshape(3, 3, 2, 2).astype(nptype))
|
||||
|
||||
for i, exp in enumerate(expect):
|
||||
assert (output[i].asnumpy() == exp).all()
|
||||
|
||||
|
||||
def unpack_pynative(nptype):
|
||||
context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU')
|
||||
x1 = np.array([[[[[0, 0],
|
||||
[-2, -1]],
|
||||
[[0, 0],
|
||||
[0, 1]]],
|
||||
[[[0, 0],
|
||||
[2, 3]],
|
||||
[[0, 0],
|
||||
[4, 5]]],
|
||||
[[[0, 0],
|
||||
[6, 7]],
|
||||
[[0, 0],
|
||||
[8, 9]]]],
|
||||
[[[[0, 0],
|
||||
[10, 11]],
|
||||
[[0, 0],
|
||||
[12, 13]]],
|
||||
[[[0, 0],
|
||||
[14, 15]],
|
||||
[[0, 0],
|
||||
[16, 17]]],
|
||||
[[[0, 0],
|
||||
[18, 19]],
|
||||
[[0, 0],
|
||||
[20, 21]]]],
|
||||
[[[[0, 0],
|
||||
[22, 23]],
|
||||
[[0, 0],
|
||||
[24, 25]]],
|
||||
[[[0, 0],
|
||||
[26, 27]],
|
||||
[[0, 0],
|
||||
[28, 29]]],
|
||||
[[[0, 0],
|
||||
[30, 31]],
|
||||
[[0, 0],
|
||||
[32, 33]]]]]).astype(nptype)
|
||||
x1 = Tensor(x1)
|
||||
expect = (np.reshape(np.array([0] * 36).astype(nptype), (3, 3, 2, 2)),
|
||||
np.arange(-2, 34, 1).reshape(3, 3, 2, 2).astype(nptype))
|
||||
output = P.Unpack(axis=3)(x1)
|
||||
|
||||
for i, exp in enumerate(expect):
|
||||
assert (output[i].asnumpy() == exp).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_graph_float32():
|
||||
unpack(np.float32)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_graph_float16():
|
||||
unpack(np.float16)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_graph_int32():
|
||||
unpack(np.int32)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_graph_int16():
|
||||
unpack(np.int16)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_graph_uint8():
|
||||
unpack(np.uint8)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_graph_bool():
|
||||
unpack(np.bool)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_pynative_float32():
|
||||
unpack_pynative(np.float32)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_pynative_float16():
|
||||
unpack_pynative(np.float16)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_pynative_int32():
|
||||
unpack_pynative(np.int32)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_pynative_int16():
|
||||
unpack_pynative(np.int16)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_pynative_uint8():
|
||||
unpack_pynative(np.uint8)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_cpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unpack_pynative_bool():
|
||||
unpack_pynative(np.bool)
|
Loading…
Reference in new issue