| 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_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | #include "arm_usability.h" |
| 22 | #include "cpu.h" |
| 23 | |
| 24 | namespace ncnn { |
| 25 | |
| 26 | #include "cast_bf16.h" |
| 27 | #include "cast_fp16.h" |
| 28 | |
| 29 | Cast_arm::Cast_arm() |
| 30 | { |
| 31 | support_packing = true; |
| 32 | #if NCNN_ARM82 |
| 33 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 34 | #endif |
| 35 | |
| 36 | support_bf16_storage = true; |
| 37 | } |
| 38 | |
| 39 | int Cast_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 40 | { |
| 41 | if (type_from == type_to) |
| 42 | { |
| 43 | top_blob = bottom_blob; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | int w = bottom_blob.w; |
| 48 | int h = bottom_blob.h; |
| 49 | int d = bottom_blob.d; |
| 50 | int channels = bottom_blob.c; |
| 51 | int dims = bottom_blob.dims; |
| 52 | size_t elemsize = bottom_blob.elemsize; |
| 53 | int elempack = bottom_blob.elempack; |
| 54 | |
| 55 | size_t out_elemsize = elemsize; |
| 56 | if (type_to == 1) |
| 57 | { |
| 58 | if (type_from == 3) |
| 59 | { |
| 60 | Cast::forward(bottom_blob, top_blob, opt); |
| 61 | } |
| 62 | |
| 63 | // float32 |
| 64 | out_elemsize = 4 * elempack; |
| 65 | } |
| 66 | else if (type_to == 2) |
| 67 | { |
| 68 | // float16 |
| 69 | out_elemsize = 2 * elempack; |
| 70 | } |
| 71 | else if (type_to == 3) |
| 72 | { |
| 73 | // int8 |
| 74 | out_elemsize = elempack; |
| 75 | } |
| 76 | else if (type_to == 4) |
| 77 | { |
| 78 | // bfloat16 |
| 79 | out_elemsize = 2 * elempack; |
| 80 | } |
| 81 | |
| 82 | if (dims == 1) |
| 83 | { |
| 84 | top_blob.create(w, out_elemsize, elempack, opt.blob_allocator); |
| 85 | } |
| 86 | else if (dims == 2) |
| 87 | { |
| 88 | top_blob.create(w, h, out_elemsize, elempack, opt.blob_allocator); |
| 89 | } |
| 90 | else if (dims == 3) |
| 91 | { |
| 92 | top_blob.create(w, h, channels, out_elemsize, elempack, opt.blob_allocator); |
| 93 | } |
| 94 | else if (dims == 4) |
| 95 | { |
| 96 | top_blob.create(w, h, d, channels, out_elemsize, elempack, opt.blob_allocator); |
| 97 | } |
| 98 | if (top_blob.empty()) |
| 99 | return -100; |
| 100 | |
| 101 | int size = w * h * d * elempack; |
| 102 | |
| 103 | if (type_from == 1 && type_to == 2) |
| 104 | { |
| 105 | cast_fp32_to_fp16_neon(bottom_blob, top_blob, opt); |
| 106 | } |
| 107 | |
| 108 | if (type_from == 2 && type_to == 1) |
| 109 | { |
| 110 | cast_fp16_to_fp32_neon(bottom_blob, top_blob, opt); |
| 111 | } |
| 112 | |
| 113 | if (type_from == 3 && type_to == 1) |
| 114 | { |
| 115 | #pragma omp parallel for num_threads(opt.num_threads) |
| 116 | for (int q = 0; q < channels; q++) |
| 117 | { |
| 118 | const signed char* ptr = bottom_blob.channel(q); |
| 119 | float* outptr = top_blob.channel(q); |
| 120 | |
| 121 | for (int i = 0; i < size; i++) |
| 122 | { |
| 123 | outptr[i] = (float)ptr[i]; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (type_from == 1 && type_to == 4) |
| 129 | { |
| 130 | cast_fp32_to_bf16_neon(bottom_blob, top_blob, opt); |
| 131 | } |
| 132 | |
| 133 | if (type_from == 4 && type_to == 1) |
| 134 | { |
| 135 | cast_bf16_to_fp32_neon(bottom_blob, top_blob, opt); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | } // namespace ncnn |