You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert_tensor_utils.cc 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "device/convert_tensor_utils.h"
  17. #include <vector>
  18. namespace mindspore {
  19. namespace device {
  20. void HalfToFloat(void *dst, const void *src, size_t elem_num) {
  21. auto half_data = static_cast<const Eigen::half *>(src);
  22. auto float_data = static_cast<float *>(dst);
  23. for (size_t i = 0; i < elem_num; ++i) {
  24. float tmp = Eigen::half_impl::half_to_float(half_data[i]);
  25. float_data[i] = tmp;
  26. }
  27. }
  28. void FloatToHalf(void *dst, const void *src, size_t elem_num) {
  29. auto float_data = static_cast<const float *>(src);
  30. auto half_data = static_cast<Eigen::half *>(dst);
  31. for (size_t i = 0; i < elem_num; ++i) {
  32. half_data[i] = Eigen::half(float_data[i]);
  33. }
  34. }
  35. void DoubleToFloat(void *dst, const void *src, size_t elem_num) {
  36. auto double_data = static_cast<const double *>(src);
  37. auto float_data = static_cast<float *>(dst);
  38. for (size_t i = 0; i < elem_num; ++i) {
  39. float_data[i] = static_cast<float>(double_data[i]);
  40. }
  41. }
  42. void FloatToDouble(void *dst, const void *src, size_t elem_num) {
  43. auto float_data = static_cast<const float *>(src);
  44. auto double_data = static_cast<double *>(dst);
  45. for (size_t i = 0; i < elem_num; ++i) {
  46. double_data[i] = static_cast<double>(float_data[i]);
  47. }
  48. }
  49. } // namespace device
  50. } // namespace mindspore