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.

cast.cpp 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "cast.h"
  15. namespace ncnn {
  16. Cast::Cast()
  17. {
  18. one_blob_only = true;
  19. support_inplace = false;
  20. support_packing = true;
  21. }
  22. int Cast::load_param(const ParamDict& pd)
  23. {
  24. type_from = pd.get(0, 0);
  25. type_to = pd.get(1, 0);
  26. return 0;
  27. }
  28. // round to nearest
  29. signed char float32_to_int8(float value)
  30. {
  31. float tmp;
  32. if (value >= 0.f)
  33. tmp = value + 0.5f;
  34. else
  35. tmp = value - 0.5f;
  36. if (tmp > 127)
  37. return 127;
  38. if (tmp < -128)
  39. return -128;
  40. return static_cast<signed char>(tmp);
  41. }
  42. int Cast::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  43. {
  44. if (type_from == type_to)
  45. {
  46. top_blob = bottom_blob;
  47. return 0;
  48. }
  49. int w = bottom_blob.w;
  50. int h = bottom_blob.h;
  51. int channels = bottom_blob.c;
  52. int dims = bottom_blob.dims;
  53. size_t elemsize = bottom_blob.elemsize;
  54. int elempack = bottom_blob.elempack;
  55. size_t out_elemsize = elemsize;
  56. if (type_to == 1)
  57. {
  58. // float32
  59. out_elemsize = 4 * elempack;
  60. }
  61. else if (type_to == 2)
  62. {
  63. // float16
  64. out_elemsize = 2 * elempack;
  65. }
  66. else if (type_to == 3)
  67. {
  68. // int8
  69. out_elemsize = elempack;
  70. }
  71. else if (type_to == 4)
  72. {
  73. // bfloat16
  74. out_elemsize = 2 * elempack;
  75. }
  76. if (dims == 1)
  77. {
  78. top_blob.create(w, out_elemsize, elempack, opt.blob_allocator);
  79. }
  80. else if (dims == 2)
  81. {
  82. top_blob.create(w, h, out_elemsize, elempack, opt.blob_allocator);
  83. }
  84. else if (dims == 3)
  85. {
  86. top_blob.create(w, h, channels, out_elemsize, elempack, opt.blob_allocator);
  87. }
  88. if (top_blob.empty())
  89. return -100;
  90. int size = w * h * elempack;
  91. if (type_from == 1 && type_to == 2)
  92. {
  93. #pragma omp parallel for num_threads(opt.num_threads)
  94. for (int q = 0; q < channels; q++)
  95. {
  96. const float* ptr = bottom_blob.channel(q);
  97. unsigned short* outptr = top_blob.channel(q);
  98. for (int i = 0; i < size; i++)
  99. {
  100. outptr[i] = float32_to_float16(ptr[i]);
  101. }
  102. }
  103. }
  104. if (type_from == 2 && type_to == 1)
  105. {
  106. #pragma omp parallel for num_threads(opt.num_threads)
  107. for (int q = 0; q < channels; q++)
  108. {
  109. const unsigned short* ptr = bottom_blob.channel(q);
  110. float* outptr = top_blob.channel(q);
  111. for (int i = 0; i < size; i++)
  112. {
  113. outptr[i] = float16_to_float32(ptr[i]);
  114. }
  115. }
  116. }
  117. if (type_from == 3 && type_to == 1)
  118. {
  119. #pragma omp parallel for num_threads(opt.num_threads)
  120. for (int q = 0; q < channels; q++)
  121. {
  122. const signed char* ptr = bottom_blob.channel(q);
  123. float* outptr = top_blob.channel(q);
  124. for (int i = 0; i < size; i++)
  125. {
  126. outptr[i] = (float)ptr[i];
  127. }
  128. }
  129. }
  130. if (type_from == 1 && type_to == 4)
  131. {
  132. #pragma omp parallel for num_threads(opt.num_threads)
  133. for (int q = 0; q < channels; q++)
  134. {
  135. const float* ptr = bottom_blob.channel(q);
  136. unsigned short* outptr = top_blob.channel(q);
  137. for (int i = 0; i < size; i++)
  138. {
  139. outptr[i] = float32_to_bfloat16(ptr[i]);
  140. }
  141. }
  142. }
  143. if (type_from == 4 && type_to == 1)
  144. {
  145. #pragma omp parallel for num_threads(opt.num_threads)
  146. for (int q = 0; q < channels; q++)
  147. {
  148. const unsigned short* ptr = bottom_blob.channel(q);
  149. float* outptr = top_blob.channel(q);
  150. for (int i = 0; i < size; i++)
  151. {
  152. outptr[i] = bfloat16_to_float32(ptr[i]);
  153. }
  154. }
  155. }
  156. // TODO more cast type
  157. return 0;
  158. }
  159. } // namespace ncnn