|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | + |
| 15 | +#include "Common.metal" |
| 16 | +#include <metal_stdlib> |
| 17 | + |
| 18 | +using namespace metal; |
| 19 | + |
| 20 | +kernel void reduce_max_c(texture2d_array<ftype, access::read> inTexture[[texture(0)]], |
| 21 | + texture2d_array<ftype, access::write> outTexture[[texture(1)]], |
| 22 | + uint3 gid[[thread_position_in_grid]]) { |
| 23 | + if (gid.x >= outTexture.get_width() || gid.y >= outTexture.get_height() || |
| 24 | + gid.z >= outTexture.get_array_size()) |
| 25 | + return; |
| 26 | + |
| 27 | +#if LITE_WITH_METAL_FULL |
| 28 | + float omax = FLT_MIN; |
| 29 | +#else |
| 30 | + half omax = HALF_MIN; |
| 31 | +#endif |
| 32 | + uint iAL = inTexture.get_array_size(); |
| 33 | + for (uint i = 0; i < iAL; ++i) { |
| 34 | + ftype4 in = inTexture.read(uint2(gid.x, gid.y), i); |
| 35 | + omax = max(omax, in.x); |
| 36 | + omax = max(omax, in.y); |
| 37 | + omax = max(omax, in.z); |
| 38 | + omax = max(omax, in.w); |
| 39 | + } |
| 40 | + outTexture.write(ftype4(omax, 0.0, 0.0, 0.0), gid.xy, 0); |
| 41 | +} |
| 42 | + |
| 43 | +kernel void reduce_min_c(texture2d_array<ftype, access::read> inTexture[[texture(0)]], |
| 44 | + texture2d_array<ftype, access::write> outTexture[[texture(1)]], |
| 45 | + uint3 gid[[thread_position_in_grid]]) { |
| 46 | + if (gid.x >= outTexture.get_width() || gid.y >= outTexture.get_height() || |
| 47 | + gid.z >= outTexture.get_array_size()) |
| 48 | + return; |
| 49 | + |
| 50 | +#if LITE_WITH_METAL_FULL |
| 51 | + float omin = FLT_MAX; |
| 52 | +#else |
| 53 | + half omin = HALF_MAX; |
| 54 | +#endif |
| 55 | + |
| 56 | + uint iAL = inTexture.get_array_size(); |
| 57 | + for (uint i = 0; i < iAL - 1; ++i) { |
| 58 | + ftype4 in = inTexture.read(uint2(gid.x, gid.y), i); |
| 59 | + omin = min(omin, in.x); |
| 60 | + omin = min(omin, in.y); |
| 61 | + omin = min(omin, in.z); |
| 62 | + omin = min(omin, in.w); |
| 63 | + } |
| 64 | + ftype4 in_ = inTexture.read(uint2(gid.x, gid.y), iAL - 1); |
| 65 | + omin = abs(in_.x <= 1e-6) ? omin : min(omin, in_.x); |
| 66 | + omin = abs(in_.y <= 1e-6) ? omin : min(omin, in_.y); |
| 67 | + omin = abs(in_.z <= 1e-6) ? omin : min(omin, in_.z); |
| 68 | + omin = abs(in_.w <= 1e-6) ? omin : min(omin, in_.w); |
| 69 | + outTexture.write(ftype4(omin, 0.0, 0.0, 0.0), gid.xy, 0); |
| 70 | +} |
| 71 | + |
| 72 | +kernel void reduce_mean_c(texture2d_array<ftype, access::read> inTexture[[texture(0)]], |
| 73 | + texture2d_array<ftype, access::write> outTexture[[texture(1)]], |
| 74 | + uint3 gid[[thread_position_in_grid]]) { |
| 75 | + if (gid.x >= outTexture.get_width() || gid.y >= outTexture.get_height() || |
| 76 | + gid.z >= outTexture.get_array_size()) |
| 77 | + return; |
| 78 | + |
| 79 | +#if LITE_WITH_METAL_FULL |
| 80 | + float omean = 0; |
| 81 | +#else |
| 82 | + half omean = 0; |
| 83 | +#endif |
| 84 | + uint iAL = inTexture.get_array_size(); |
| 85 | + uint count = 4 * (iAL - 1); |
| 86 | + for (uint i = 0; i < iAL; ++i) { |
| 87 | + ftype4 in = inTexture.read(uint2(gid.x, gid.y), i); |
| 88 | + omean += in.x; |
| 89 | + omean += in.y; |
| 90 | + omean += in.z; |
| 91 | + omean += in.w; |
| 92 | + } |
| 93 | + ftype4 in_ = inTexture.read(uint2(gid.x, gid.y), iAL - 1); |
| 94 | + count = abs(in_.x <= 1e-6) ? count : count + 1; |
| 95 | + count = abs(in_.y <= 1e-6) ? count : count + 1; |
| 96 | + count = abs(in_.z <= 1e-6) ? count : count + 1; |
| 97 | + count = abs(in_.w <= 1e-6) ? count : count + 1; |
| 98 | + omean = omean / count; |
| 99 | + outTexture.write(ftype4(omean, 0.0, 0.0, 0.0), gid.xy, 0); |
| 100 | +} |
| 101 | + |
| 102 | +kernel void reduce_sum_c(texture2d_array<ftype, access::read> inTexture[[texture(0)]], |
| 103 | + texture2d_array<ftype, access::write> outTexture[[texture(1)]], |
| 104 | + uint3 gid[[thread_position_in_grid]]) { |
| 105 | + if (gid.x >= outTexture.get_width() || gid.y >= outTexture.get_height() || |
| 106 | + gid.z >= outTexture.get_array_size()) |
| 107 | + return; |
| 108 | + |
| 109 | +#if LITE_WITH_METAL_FULL |
| 110 | + float osum = 0; |
| 111 | +#else |
| 112 | + half osum = 0; |
| 113 | +#endif |
| 114 | + uint iAL = inTexture.get_array_size(); |
| 115 | + for (uint i = 0; i < iAL; ++i) { |
| 116 | + ftype4 in = inTexture.read(uint2(gid.x, gid.y), i); |
| 117 | + osum += in.x; |
| 118 | + osum += in.y; |
| 119 | + osum += in.z; |
| 120 | + osum += in.w; |
| 121 | + } |
| 122 | + outTexture.write(ftype4(osum, 0.0, 0.0, 0.0), gid.xy, 0); |
| 123 | +} |
0 commit comments