|
| 1 | +// Copyright (C) 2018-2026 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "permute_kernel_b_f_axes.h" |
| 6 | + |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#include "common_tools.h" |
| 10 | +#include "kernel_selector_utils.h" |
| 11 | + |
| 12 | +namespace kernel_selector { |
| 13 | + |
| 14 | +// Vector width chosen to make each vload/vstore a 16-byte transaction. |
| 15 | +static size_t GetVecWidth(const permute_params& params) { |
| 16 | + switch (params.inputs[0].GetDType()) { |
| 17 | + case Datatype::F16: |
| 18 | + case Datatype::INT16: |
| 19 | + case Datatype::UINT16: |
| 20 | + return 8; |
| 21 | + case Datatype::F32: |
| 22 | + case Datatype::INT32: |
| 23 | + return 4; |
| 24 | + case Datatype::INT8: |
| 25 | + case Datatype::UINT8: |
| 26 | + return 16; |
| 27 | + case Datatype::INT64: |
| 28 | + return 2; |
| 29 | + default: |
| 30 | + return 4; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +ParamsKey PermuteKernel_b_f_axes::GetSupportedKey() const { |
| 35 | + ParamsKey k; |
| 36 | + k.EnableInputDataType(Datatype::F16); |
| 37 | + k.EnableInputDataType(Datatype::F32); |
| 38 | + k.EnableInputDataType(Datatype::INT8); |
| 39 | + k.EnableInputDataType(Datatype::UINT8); |
| 40 | + k.EnableInputDataType(Datatype::INT32); |
| 41 | + k.EnableInputDataType(Datatype::INT64); |
| 42 | + k.EnableOutputDataType(Datatype::F16); |
| 43 | + k.EnableOutputDataType(Datatype::F32); |
| 44 | + k.EnableOutputDataType(Datatype::INT8); |
| 45 | + k.EnableOutputDataType(Datatype::UINT8); |
| 46 | + k.EnableOutputDataType(Datatype::INT32); |
| 47 | + k.EnableOutputDataType(Datatype::INT64); |
| 48 | + k.EnableDifferentTypes(); |
| 49 | + k.EnableInputLayout(DataLayout::bfyx); |
| 50 | + k.EnableOutputLayout(DataLayout::bfyx); |
| 51 | + k.EnableInputLayout(DataLayout::bfzyx); |
| 52 | + k.EnableOutputLayout(DataLayout::bfzyx); |
| 53 | + k.EnableInputLayout(DataLayout::bfwzyx); |
| 54 | + k.EnableOutputLayout(DataLayout::bfwzyx); |
| 55 | + k.EnableTensorOffset(); |
| 56 | + k.EnableTensorPitches(); |
| 57 | + k.EnableBatching(); |
| 58 | + k.EnableDynamicShapesSupport(); |
| 59 | + return k; |
| 60 | +} |
| 61 | + |
| 62 | +JitConstants PermuteKernel_b_f_axes::GetJitConstants(const permute_params& params, |
| 63 | + const CommonDispatchData& /*dispatchData*/) const { |
| 64 | + auto jit = Parent::GetJitConstants(params, {}); |
| 65 | + |
| 66 | + const size_t vec_width = GetVecWidth(params); |
| 67 | + const size_t x_size = params.inputs[0].X().v; |
| 68 | + const size_t x_tiles = x_size / vec_width; |
| 69 | + const size_t x_rem = x_size % vec_width; |
| 70 | + |
| 71 | + jit.AddConstant(MakeJitConstant("VEC_WIDTH", vec_width)); |
| 72 | + jit.AddConstant(MakeJitConstant("X_TILES", x_tiles)); |
| 73 | + jit.AddConstant(MakeJitConstant("X_REMAINDER_SIZE", x_rem)); |
| 74 | + |
| 75 | + jit.AddConstant(MakeJitConstant("INPUTVTYPE", "CAT(INPUT0_TYPE, VEC_WIDTH)")); |
| 76 | + jit.AddConstant(MakeJitConstant("OUTPUTVTYPE", "CAT(OUTPUT_TYPE, VEC_WIDTH)")); |
| 77 | + |
| 78 | + if (!params.fused_ops.empty()) { |
| 79 | + std::vector<std::string> output_order; |
| 80 | + switch (params.inputs[0].GetDims().size()) { |
| 81 | + case 4: output_order = {"b", "f", "y", "x"}; break; |
| 82 | + case 5: output_order = {"b", "f", "z", "y", "x"}; break; |
| 83 | + case 6: output_order = {"b", "f", "w", "z", "y", "x"}; break; |
| 84 | + default: break; |
| 85 | + } |
| 86 | + FusedOpsConfiguration conf = {"", output_order, "input_var", params.inputs[0].GetDType(), 1}; |
| 87 | + jit.Merge(MakeFusedOpsJitConstants(params, {conf})); |
| 88 | + } |
| 89 | + |
| 90 | + return jit; |
| 91 | +} |
| 92 | + |
| 93 | +CommonDispatchData PermuteKernel_b_f_axes::SetDefault(const permute_params& params) const { |
| 94 | + CommonDispatchData dispatchData; |
| 95 | + |
| 96 | + const auto& in = params.inputs[0]; |
| 97 | + const auto in_layout = in.GetLayout(); |
| 98 | + const auto out_layout = params.outputs[0].GetLayout(); |
| 99 | + const size_t vec_width = GetVecWidth(params); |
| 100 | + const size_t x_tiles = CeilDiv(in.X().v, vec_width); |
| 101 | + |
| 102 | + size_t spatial_outer = 1; |
| 103 | + if (in.GetDims().size() >= 5) spatial_outer *= in.Z().v; |
| 104 | + if (in.GetDims().size() >= 6) spatial_outer *= in.W().v; |
| 105 | + |
| 106 | + // F is looped inside the kernel; GWS[2] covers B only. |
| 107 | + dispatchData.gws = {x_tiles, in.Y().v * spatial_outer, in.Batch().v}; |
| 108 | + |
| 109 | + const std::vector<std::vector<Tensor::DataChannelName>> dims_by_gws = { |
| 110 | + {Tensor::DataChannelName::X}, |
| 111 | + {Tensor::DataChannelName::Y, Tensor::DataChannelName::Z, Tensor::DataChannelName::W}, |
| 112 | + {Tensor::DataChannelName::BATCH}}; |
| 113 | + dispatchData.lws = GetOptimalLocalWorkGroupSizes(dispatchData.gws, params.engineInfo, |
| 114 | + in_layout, out_layout, dims_by_gws); |
| 115 | + |
| 116 | + return dispatchData; |
| 117 | +} |
| 118 | + |
| 119 | +bool PermuteKernel_b_f_axes::Validate(const Params& p) const { |
| 120 | + if (!Parent::Validate(p)) DO_NOT_USE_THIS_KERNEL(p.layerID); |
| 121 | + |
| 122 | + const permute_params& params = static_cast<const permute_params&>(p); |
| 123 | + |
| 124 | + if (params.outputs[0].PitchesDifferFromLogicalDims() || params.inputs[0].PitchesDifferFromLogicalDims()) |
| 125 | + DO_NOT_USE_THIS_KERNEL(p.layerID); |
| 126 | + |
| 127 | + if (!SimpleLayout(params.inputs[0].GetLayout())) |
| 128 | + DO_NOT_USE_THIS_KERNEL(p.layerID); |
| 129 | + |
| 130 | + if (params.inputs[0].GetLayout() != params.outputs[0].GetLayout()) |
| 131 | + DO_NOT_USE_THIS_KERNEL(p.layerID); |
| 132 | + |
| 133 | + // Only accept [1, 0, 2, ...] — B <-> F swap with spatial axes unchanged. |
| 134 | + const auto& order = params.order; |
| 135 | + const size_t ndim = order.size(); |
| 136 | + if (ndim < 3 || ndim > 6) |
| 137 | + DO_NOT_USE_THIS_KERNEL(p.layerID); |
| 138 | + |
| 139 | + if (order[0] != 1 || order[1] != 0) |
| 140 | + DO_NOT_USE_THIS_KERNEL(p.layerID); |
| 141 | + |
| 142 | + for (size_t i = 2; i < ndim; ++i) { |
| 143 | + if (order[i] != static_cast<uint16_t>(i)) |
| 144 | + DO_NOT_USE_THIS_KERNEL(p.layerID); |
| 145 | + } |
| 146 | + |
| 147 | + return true; |
| 148 | +} |
| 149 | + |
| 150 | +KernelsPriority PermuteKernel_b_f_axes::GetKernelsPriority(const Params& params) const { |
| 151 | + KernelData kd = KernelData::Default<permute_params>(params); |
| 152 | + permute_params& newParams = *static_cast<permute_params*>(kd.params.get()); |
| 153 | + |
| 154 | + const size_t vec_width = GetVecWidth(newParams); |
| 155 | + const size_t x_size = newParams.inputs[0].X().v; |
| 156 | + |
| 157 | + if (x_size >= vec_width * 2 && (x_size % vec_width == 0)) |
| 158 | + return FORCE_PRIORITY_2; |
| 159 | + if (x_size >= vec_width) |
| 160 | + return FORCE_PRIORITY_3; |
| 161 | + |
| 162 | + return FORCE_PRIORITY_4; |
| 163 | +} |
| 164 | + |
| 165 | +} // namespace kernel_selector |
0 commit comments