!4392 use builtin float16 for arm

Merge pull request !4392 from xychow/use-float16-in-arm-neon
This commit is contained in:
mindspore-ci-bot 2020-08-19 09:17:09 +08:00 committed by Gitee
commit 58523a41fe
12 changed files with 53 additions and 27 deletions

View File

@ -46,13 +46,13 @@ tensor::TensorPtr CreateTensor(const AnfNodePtr &node) {
// 2 set value of tensor
auto data_ptr = indices_tensor->data_c();
MS_EXCEPTION_IF_NULL(data_ptr);
std::vector<Eigen::half> half_data;
std::vector<float16> half_data;
for (size_t i = 0; i < last_dim; ++i) {
half_data.emplace_back(Eigen::half(static_cast<float>(i)));
half_data.emplace_back(float16(static_cast<float>(i)));
}
for (size_t i = 0; i < last_dim; ++i) {
auto gap = static_cast<int>(i) - static_cast<int>(Eigen::half(static_cast<float>(i)));
half_data.emplace_back(Eigen::half(static_cast<float>(gap)));
auto gap = static_cast<int>(i) - static_cast<int>(float16(static_cast<float>(i)));
half_data.emplace_back(float16(static_cast<float>(gap)));
}
auto elem_num = last_dim * kFloat16Len * 2;
auto ret_code = memcpy_s(data_ptr, static_cast<size_t>(indices_tensor->data().nbytes()), half_data.data(), elem_num);

View File

@ -91,9 +91,9 @@ ValuePtr FusedBatchNormFusion::GetFactor(const EquivPtr &equiv) const {
auto tensor_ptr = value->cast<tensor::TensorPtr>();
MS_EXCEPTION_IF_NULL(tensor_ptr);
if (tensor_ptr->data_type() == kNumberTypeFloat16) {
auto *half_data = static_cast<const Eigen::half *>(tensor_ptr->data_c());
auto *half_data = static_cast<const float16 *>(tensor_ptr->data_c());
MS_EXCEPTION_IF_NULL(half_data);
float float_data = Eigen::half_impl::half_to_float(half_data[0]);
float float_data = half_to_float(half_data[0]);
return MakeValue(float_data);
} else if (tensor_ptr->data_type() == kNumberTypeFloat32) {
auto *tensor_data = static_cast<const float *>(tensor_ptr->data_c());

View File

@ -138,9 +138,9 @@ template <typename SrcT>
void TransDataSrc2Fp16(const TypeIdArgs &args, void *dst, const size_t data_size) {
CheckMemSize(args);
auto src_data = static_cast<const SrcT *>(args.data);
auto half_data = static_cast<Eigen::half *>(dst);
auto half_data = static_cast<float16 *>(dst);
for (size_t i = 0; i < data_size; i++) {
half_data[i] = Eigen::half(src_data[i]);
half_data[i] = float16(src_data[i]);
}
}

View File

@ -25,8 +25,7 @@
#include "minddata/dataset/core/pybind_support.h"
namespace py = pybind11;
#else
#include "Eigen/Core"
using float16 = Eigen::half;
#include "base/float16.h"
#endif
#include "minddata/dataset/core/constants.h"
namespace mindspore {

View File

@ -21,10 +21,9 @@
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#include "Eigen/Core"
#include "base/float16.h"
namespace py = pybind11;
using float16 = Eigen::half;
namespace pybind11 {
namespace detail {

View File

@ -27,8 +27,6 @@
#include "minddata/dataset/engine/opt/pass.h"
#include "minddata/dataset/kernels/data/data_utils.h"
using float16 = Eigen::half;
namespace mindspore {
namespace dataset {
BatchOp::Builder::Builder(int32_t batch_size) : builder_drop_(false), builder_pad_(false), builder_pad_map_({}) {

View File

@ -345,14 +345,14 @@ Status ToFloat16(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *
for (; out_itr != out_end; in_itr++, out_itr++) {
float element = *in_itr;
float float16_max = static_cast<float>(std::numeric_limits<Eigen::half>::max());
float float16_min = static_cast<float>(std::numeric_limits<Eigen::half>::lowest());
float float16_max = static_cast<float>(std::numeric_limits<float16>::max());
float float16_min = static_cast<float>(std::numeric_limits<float16>::lowest());
if (element > float16_max || element < float16_min) {
RETURN_STATUS_UNEXPECTED("Value " + std::to_string(element) + " is outside of valid float16 range [" +
std::to_string(float16_max) + ", " + std::to_string(float16_min) + "].");
}
*out_itr = Eigen::half(*in_itr);
*out_itr = float16(*in_itr);
}
return Status::OK();

View File

@ -111,7 +111,7 @@ bool FloatToHalfAndSyncHostToDevice(void *dst, size_t dst_size, const void *src,
MS_EXCEPTION(ArgumentError) << "FloatToHalf failed. size not match src_size[" << src_size << "], dst_size["
<< dst_size << "]";
}
std::vector<Eigen::half> half_data(elem_num);
std::vector<float16> half_data(elem_num);
FloatToHalf(half_data.data(), src, elem_num);
SyncMemory(dst, half_data.data(), dst_size, RT_MEMCPY_HOST_TO_DEVICE);
return true;
@ -134,7 +134,7 @@ bool SyncDeviceToHostAndHalfToFloat(void *dst, size_t dst_size, const void *src,
MS_EXCEPTION(ArgumentError) << "HalfToFloat failed. size not match src_size[" << src_size << "], dst_size["
<< dst_size << "]";
}
std::vector<Eigen::half> half_data(elem_num);
std::vector<float16> half_data(elem_num);
SyncMemory(half_data.data(), src, src_size, RT_MEMCPY_DEVICE_TO_HOST);
HalfToFloat(dst, half_data.data(), elem_num);
return true;

View File

@ -18,19 +18,19 @@
namespace mindspore {
namespace device {
void HalfToFloat(void *dst, const void *src, size_t elem_num) {
auto half_data = static_cast<const Eigen::half *>(src);
auto half_data = static_cast<const float16 *>(src);
auto float_data = static_cast<float *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
float tmp = Eigen::half_impl::half_to_float(half_data[i]);
float tmp = half_to_float(half_data[i]);
float_data[i] = tmp;
}
}
void FloatToHalf(void *dst, const void *src, size_t elem_num) {
auto float_data = static_cast<const float *>(src);
auto half_data = static_cast<Eigen::half *>(dst);
auto half_data = static_cast<float16 *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
half_data[i] = Eigen::half(float_data[i]);
half_data[i] = float16(float_data[i]);
}
}

View File

@ -29,7 +29,6 @@ namespace lite {
using int32 = int32_t;
using int64 = int64_t;
using uint64 = uint64_t;
using float16 = Eigen::half;
class MSANFModelParser {
public:
MSANFModelParser() : producer_name_(""), model_version_(0), ir_version_(0) {}

View File

@ -0,0 +1,33 @@
/**
* 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_CORE_BASE_FLOAT16_H_
#define MINDSPORE_CORE_BASE_FLOAT16_H_
#if defined(ENABLE_ARM32) || defined(ENABLE_ARM64)
// Built for lite and ARM
#include <arm_neon.h>
using float16 = float16_t;
inline float half_to_float(float16 h) { return static_cast<float>(h); }
#else
#include <functional>
#include "Eigen/Core"
using float16 = Eigen::half;
using HalfToFloat = std::function<float(float16)>;
const inline HalfToFloat half_to_float = Eigen::half_impl::half_to_float;
#endif
#endif // MINDSPORE_CORE_BASE_FLOAT16_H_

View File

@ -22,12 +22,10 @@
#include <vector>
#include <numeric>
#include "Eigen/Core"
#include "ir/device_sync.h"
#include "ir/meta_tensor.h"
#include "utils/log_adapter.h"
using float16 = Eigen::half;
#include "base/float16.h"
// brief mindspore namespace.
//