|
| 1 | +#include "cnnl_kernel.hh" |
| 2 | + |
| 3 | +#ifdef USE_BANG |
| 4 | +#include "../../utilities/bang/cnnl_context.hh" |
| 5 | +#include "../../utilities/bang/cnnl_functions.h" |
| 6 | +#include <cnnl.h> |
| 7 | +#endif |
| 8 | + |
| 9 | +namespace refactor::kernel { |
| 10 | + using K = BatchNormalizationCnnl; |
| 11 | + using DT = DataType; |
| 12 | + |
| 13 | + K::BatchNormalizationCnnl(decltype(info) info_) noexcept |
| 14 | + : info(info_) {} |
| 15 | + |
| 16 | + auto K::build(float epsilon, TensorRefs inputs) noexcept -> KernelBox { |
| 17 | +#ifndef USE_BANG |
| 18 | + return nullptr; |
| 19 | +#endif |
| 20 | + |
| 21 | + auto const &x = inputs[0].get(); |
| 22 | + auto const &scale = inputs[1].get(); |
| 23 | + auto const &mean = inputs[3].get(); |
| 24 | + |
| 25 | + if (x.rank() != 4) { |
| 26 | + return nullptr; |
| 27 | + } |
| 28 | + |
| 29 | + // see "Supported Configurations for `cnnlBatchNormalizationForwardInference`" |
| 30 | + if (scale.dataType != mean.dataType) { |
| 31 | + return nullptr; |
| 32 | + } |
| 33 | + if (x.dataType == DT::F64) { |
| 34 | + if (scale.dataType != DT::F64) { |
| 35 | + return nullptr; |
| 36 | + } |
| 37 | + } else { |
| 38 | + if (scale.dataType != DT::F32) { |
| 39 | + return nullptr; |
| 40 | + } |
| 41 | + } |
| 42 | + return std::make_unique<K>(decltype(info){ |
| 43 | + epsilon, |
| 44 | + x.dataType, |
| 45 | + scale.dataType, |
| 46 | + x.layout, |
| 47 | + { |
| 48 | + static_cast<int>(x.shape[0]), |
| 49 | + static_cast<int>(x.shape[1]), |
| 50 | + static_cast<int>(x.shape[2]), |
| 51 | + static_cast<int>(x.shape[3]), |
| 52 | + }}); |
| 53 | + } |
| 54 | + auto K::typeId() noexcept -> size_t { |
| 55 | + static uint8_t ID = 1; |
| 56 | + return reinterpret_cast<size_t>(&ID); |
| 57 | + } |
| 58 | + |
| 59 | + auto K::kernelTypeId() const noexcept -> size_t { return typeId(); } |
| 60 | + auto K::description() const noexcept -> std::string_view { |
| 61 | + return "Performing batch normalization for non-training-mode using CNNL"; |
| 62 | + } |
| 63 | + |
| 64 | +#ifdef USE_BANG |
| 65 | + |
| 66 | + auto K::lower(Resources &res) const -> RoutineWorkspace { |
| 67 | + using namespace cnnl; |
| 68 | + using namespace runtime; |
| 69 | + using DT = DataType; |
| 70 | + |
| 71 | + // RAII for closure |
| 72 | + struct Descriptors { |
| 73 | + cnnlTensorDescriptor_t inDesc, inDescTrans, p; |
| 74 | + cnnlTransposeDescriptor_t NCHW2NHWC, NHWC2NCHW; |
| 75 | + bool f32; |
| 76 | + |
| 77 | + explicit Descriptors(decltype(f32) f32_) |
| 78 | + : inDesc(nullptr), inDescTrans(nullptr), p(nullptr), |
| 79 | + NCHW2NHWC(nullptr), NHWC2NCHW(nullptr), f32(f32_) { |
| 80 | + CNNL_ASSERT(cnnlCreateTensorDescriptor(&inDesc)); |
| 81 | + CNNL_ASSERT(cnnlCreateTensorDescriptor(&inDescTrans)); |
| 82 | + CNNL_ASSERT(cnnlCreateTensorDescriptor(&p)); |
| 83 | + CNNL_ASSERT(cnnlCreateTransposeDescriptor(&NCHW2NHWC)); |
| 84 | + CNNL_ASSERT(cnnlCreateTransposeDescriptor(&NHWC2NCHW)); |
| 85 | + } |
| 86 | + ~Descriptors() noexcept(false) { |
| 87 | + CNNL_ASSERT(cnnlDestroyTensorDescriptor(inDesc)); |
| 88 | + CNNL_ASSERT(cnnlDestroyTensorDescriptor(inDescTrans)); |
| 89 | + CNNL_ASSERT(cnnlDestroyTensorDescriptor(p)); |
| 90 | + CNNL_ASSERT(cnnlDestroyTransposeDescriptor(NCHW2NHWC)); |
| 91 | + CNNL_ASSERT(cnnlDestroyTransposeDescriptor(NHWC2NCHW)); |
| 92 | + } |
| 93 | + |
| 94 | + Descriptors(const Descriptors &) = delete; |
| 95 | + Descriptors(Descriptors &&) = delete; |
| 96 | + }; |
| 97 | + auto d = std::make_shared<Descriptors>(info.dtX != DT::F64); |
| 98 | + int dimNCHW[4] = {info.dimAx[0], info.dimAx[1], info.dimAx[2], info.dimAx[3]}; |
| 99 | + int dimNHWC[4] = {info.dimAx[0], info.dimAx[2], info.dimAx[3], info.dimAx[1]}; |
| 100 | + int dimParam[]{info.dimAx[1]}; |
| 101 | + setCnnlTensor(d->inDesc, info.dtX, slice(dimNCHW, 4)); |
| 102 | + CNNL_ASSERT(cnnlSetTensorDescriptor(d->inDescTrans, CNNL_LAYOUT_NHWC, cnnlDataTypeConvert(info.dtX), 4, dimNHWC)); |
| 103 | + CNNL_ASSERT(cnnlSetTensorDescriptor(d->p, CNNL_LAYOUT_ARRAY, cnnlDataTypeConvert(info.dtP), 1, dimParam)); |
| 104 | + int permute[4] = {0, 2, 3, 1}; |
| 105 | + int permuteOut[4] = {0, 3, 1, 2}; |
| 106 | + CNNL_ASSERT(cnnlSetTransposeDescriptor(d->NCHW2NHWC, 4, permute)); |
| 107 | + CNNL_ASSERT(cnnlSetTransposeDescriptor(d->NHWC2NCHW, 4, permuteOut)); |
| 108 | + |
| 109 | + auto handle = res.fetchOrStore<CnnlContext>()->handle; |
| 110 | + auto xTransSize = cnnlGetTensorElementNum(d->inDescTrans) * sizeof(info.dtX); |
| 111 | + size_t workspaceSize; |
| 112 | + CNNL_ASSERT(cnnlGetTransposeWorkspaceSize(handle, d->inDesc, d->NCHW2NHWC, &workspaceSize)); |
| 113 | + size_t totalWorkspaceSize = xTransSize + workspaceSize; |
| 114 | + |
| 115 | + res.fetchOrStore<CnnlContext>(); |
| 116 | + auto routine = [d = std::move(d), |
| 117 | + epsilon = info.epsilon, |
| 118 | + xTransSize, workspaceSize](Resources &res, void *workspace, void const *const *inputs, void *const *outputs) { |
| 119 | + // fetch cnnl handle from resources |
| 120 | + auto handle = res.fetchOrStore<CnnlContext>()->handle; |
| 121 | + |
| 122 | + // name inputs and outputs |
| 123 | + auto x = inputs[0], |
| 124 | + scale = inputs[1], |
| 125 | + bias = inputs[2], |
| 126 | + mean = inputs[3], |
| 127 | + var = inputs[4]; |
| 128 | + auto y = outputs[0]; |
| 129 | + |
| 130 | + void *xTrans = workspace; |
| 131 | + void *yTrans = xTrans + xTransSize; |
| 132 | + void *cursor = yTrans + workspaceSize; |
| 133 | + |
| 134 | + // transpose NCHW input to NHWC |
| 135 | + CNNL_ASSERT(cnnlTranspose_v2(handle, d->NCHW2NHWC, d->inDesc, x, |
| 136 | + d->inDescTrans, xTrans, cursor, workspaceSize)); |
| 137 | + |
| 138 | + // build alpha/beta for double |
| 139 | + auto a = d->f32 ? factor<fp32_t>(1) : factor<fp64_t>(1), |
| 140 | + b = d->f32 ? factor<fp32_t>(0) : factor<fp64_t>(0); |
| 141 | + CNNL_ASSERT(cnnlBatchNormForwardInference( |
| 142 | + handle, &a, &b, |
| 143 | + d->inDescTrans, xTrans, d->p, scale, bias, mean, var, |
| 144 | + epsilon, d->inDescTrans, yTrans)); |
| 145 | + |
| 146 | + // transpose NHWC intermediates to NCHW |
| 147 | + CNNL_ASSERT(cnnlTranspose_v2(handle, d->NHWC2NCHW, d->inDescTrans, yTrans, |
| 148 | + d->inDesc, y, cursor, workspaceSize)); |
| 149 | + |
| 150 | + BANG_ASSERT(cnrtQueueSync(res.fetchOrStore<CnnlContext>()->queue)); |
| 151 | + }; |
| 152 | + |
| 153 | + return {std::move(routine), totalWorkspaceSize}; |
| 154 | + } |
| 155 | + |
| 156 | +#endif |
| 157 | + |
| 158 | +}// namespace refactor::kernel |
0 commit comments