| 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 | |
| 15 | #include "cast.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | Cast::Cast() |
| 20 | { |
| 21 | one_blob_only = true; |
| 22 | support_inplace = false; |
| 23 | support_packing = true; |
| 24 | } |
| 25 | |
| 26 | int Cast::load_param(const ParamDict& pd) |
| 27 | { |
| 28 | type_from = pd.get(0, 0); |
| 29 | type_to = pd.get(1, 0); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | // round to nearest |
| 35 | signed char float32_to_int8(float value) |
| 36 | { |
| 37 | float tmp; |
| 38 | if (value >= 0.f) |
| 39 | tmp = value + 0.5f; |
| 40 | else |
| 41 | tmp = value - 0.5f; |
| 42 | |
| 43 | if (tmp > 127) |
| 44 | return 127; |
| 45 | if (tmp < -128) |
| 46 | return -128; |
| 47 | |
| 48 | return static_cast<signed char>(tmp); |
| 49 | } |
| 50 | |
| 51 | int Cast::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 52 | { |
| 53 | if (type_from == type_to) |
| 54 | { |
| 55 | top_blob = bottom_blob; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int w = bottom_blob.w; |
| 60 | int h = bottom_blob.h; |
| 61 | int d = bottom_blob.d; |
| 62 | int channels = bottom_blob.c; |
| 63 | int dims = bottom_blob.dims; |
| 64 | size_t elemsize = bottom_blob.elemsize; |
| 65 | int elempack = bottom_blob.elempack; |
| 66 | |
| 67 | size_t out_elemsize = elemsize; |
| 68 | if (type_to == 1) |
| 69 | { |
| 70 | // float32 |
| 71 | out_elemsize = 4 * elempack; |
| 72 | } |
| 73 | else if (type_to == 2) |
| 74 | { |
| 75 | // float16 |
| 76 | out_elemsize = 2 * elempack; |
| 77 | } |
| 78 | else if (type_to == 3) |
| 79 | { |
| 80 | // int8 |
| 81 | out_elemsize = elempack; |
| 82 | } |
| 83 | else if (type_to == 4) |
| 84 | { |
| 85 | // bfloat16 |
| 86 | out_elemsize = 2 * elempack; |
| 87 | } |
| 88 | |
| 89 | if (dims == 1) |
| 90 | { |
| 91 | top_blob.create(w, out_elemsize, elempack, opt.blob_allocator); |
| 92 | } |
| 93 | else if (dims == 2) |
| 94 | { |
| 95 | top_blob.create(w, h, out_elemsize, elempack, opt.blob_allocator); |
| 96 | } |
| 97 | else if (dims == 3) |
| 98 | { |
| 99 | top_blob.create(w, h, channels, out_elemsize, elempack, opt.blob_allocator); |
| 100 | } |
| 101 | else if (dims == 4) |
| 102 | { |
| 103 | top_blob.create(w, h, d, channels, out_elemsize, elempack, opt.blob_allocator); |
| 104 | } |
| 105 | if (top_blob.empty()) |
| 106 | return -100; |
| 107 | |
| 108 | int size = w * h * d * elempack; |
| 109 | |
| 110 | if (type_from == 1 && type_to == 2) |
| 111 | { |
| 112 | #pragma omp parallel for num_threads(opt.num_threads) |
| 113 | for (int q = 0; q < channels; q++) |
| 114 | { |
| 115 | const float* ptr = bottom_blob.channel(q); |
| 116 | unsigned short* outptr = top_blob.channel(q); |
| 117 | |
| 118 | for (int i = 0; i < size; i++) |
| 119 | { |
| 120 | outptr[i] = float32_to_float16(ptr[i]); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (type_from == 2 && type_to == 1) |
| 126 | { |
| 127 | #pragma omp parallel for num_threads(opt.num_threads) |
| 128 | for (int q = 0; q < channels; q++) |
| 129 | { |
| 130 | const unsigned short* ptr = bottom_blob.channel(q); |
| 131 | float* outptr = top_blob.channel(q); |
| 132 | |
| 133 | for (int i = 0; i < size; i++) |
| 134 | { |
| 135 | outptr[i] = float16_to_float32(ptr[i]); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (type_from == 3 && type_to == 1) |
| 141 | { |
| 142 | #pragma omp parallel for num_threads(opt.num_threads) |
| 143 | for (int q = 0; q < channels; q++) |
| 144 | { |
| 145 | const signed char* ptr = bottom_blob.channel(q); |
| 146 | float* outptr = top_blob.channel(q); |
| 147 | |
| 148 | for (int i = 0; i < size; i++) |
| 149 | { |
| 150 | outptr[i] = (float)ptr[i]; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (type_from == 1 && type_to == 4) |
| 156 | { |
| 157 | #pragma omp parallel for num_threads(opt.num_threads) |
| 158 | for (int q = 0; q < channels; q++) |
| 159 | { |
| 160 | const float* ptr = bottom_blob.channel(q); |
| 161 | unsigned short* outptr = top_blob.channel(q); |
| 162 | |
| 163 | for (int i = 0; i < size; i++) |
| 164 | { |
| 165 | outptr[i] = float32_to_bfloat16(ptr[i]); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (type_from == 4 && type_to == 1) |
| 171 | { |
| 172 | #pragma omp parallel for num_threads(opt.num_threads) |
| 173 | for (int q = 0; q < channels; q++) |
| 174 | { |
| 175 | const unsigned short* ptr = bottom_blob.channel(q); |
| 176 | float* outptr = top_blob.channel(q); |
| 177 | |
| 178 | for (int i = 0; i < size; i++) |
| 179 | { |
| 180 | outptr[i] = bfloat16_to_float32(ptr[i]); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // TODO more cast type |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | } // namespace ncnn |