1 /**
2 * Copyright 2021 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
17 #include "src/litert/delegate/delegate_utils.h"
18 #include "nnacl/fp32/pack_fp32.h"
19 namespace mindspore::lite {
PackNHWCToNCHWFp32(const void * src,void * dst,int batches,int plane,int channel)20 void PackNHWCToNCHWFp32(const void *src, void *dst, int batches, int plane, int channel) {
21 int hw8 = plane / C8NUM * C8NUM;
22 int batch = plane * channel;
23 for (int n = 0; n < batches; n++) {
24 const float *src_batch = (const float *)src + n * batch;
25 float *dst_batch = reinterpret_cast<float *>(dst) + n * batch;
26 int hw = 0;
27 for (; hw < hw8; hw += C8NUM) {
28 int c = 0;
29 #ifdef ENABLE_ARM64
30 for (; c <= channel - C8NUM; c += C8NUM) {
31 const float *src_ptr = src_batch + hw * channel + c;
32 float *dst_ptr = dst_batch + c * plane + hw;
33 Transpose8X8Fp32Arm64(src_ptr, dst_ptr, channel, plane);
34 }
35 #endif
36 for (; c < channel; c++) {
37 const float *src_ptr = src_batch + hw * channel + c;
38 float *dst_ptr = dst_batch + c * plane + hw;
39 for (size_t i = 0; i < C8NUM; i++) {
40 dst_ptr[i] = src_ptr[i * channel];
41 }
42 }
43 }
44 for (; hw < plane; hw++) {
45 const float *src_ptr = src_batch + hw * channel;
46 float *dst_ptr = dst_batch + hw;
47 for (int i = 0; i < channel; i++) {
48 dst_ptr[i * plane] = src_ptr[i];
49 }
50 }
51 }
52 }
53
PackNCHWToNHWCFp32(const void * src,void * dst,int batch,int plane,int channel)54 void PackNCHWToNHWCFp32(const void *src, void *dst, int batch, int plane, int channel) {
55 return PackNHWCToNCHWFp32(src, dst, batch, channel, plane);
56 }
57
MaskDataNHWC2NCHWBinary(int mask)58 int MaskDataNHWC2NCHWBinary(int mask) {
59 int mask_vec[COMM_SHAPE_SIZE];
60 for (int i = 0; i < COMM_SHAPE_SIZE; ++i) {
61 mask_vec[i] = (uint32_t)(mask) & (1 << i);
62 }
63 AssistDataNHWC2NCHW<int>(mask_vec, 1);
64 int ret = 0;
65 for (int i = 0; i < COMM_SHAPE_SIZE; ++i) {
66 if (mask_vec[i]) {
67 ret += 1 << i;
68 }
69 }
70 return ret;
71 }
72
BinaryMaskData2Bool(int src_mask,bool * dst_mask,size_t mask_size)73 void BinaryMaskData2Bool(int src_mask, bool *dst_mask, size_t mask_size) {
74 MS_ASSERT(dst_mask != nullptr);
75 for (size_t i = 0; i < mask_size; ++i) {
76 int mask_raw = (uint32_t)(src_mask) & (1 << i);
77 dst_mask[i] = static_cast<bool>(mask_raw);
78 }
79 }
80
IsSubGraphInputTensor(const std::vector<mindspore::MSTensor> & inputs,mindspore::MSTensor input)81 bool IsSubGraphInputTensor(const std::vector<mindspore::MSTensor> &inputs, mindspore::MSTensor input) {
82 return std::find(inputs.begin(), inputs.end(), input) != inputs.end();
83 }
84 } // namespace mindspore::lite
85