|
| 1 | +#include "cnnl_kernel.hh" |
| 2 | +#include <numeric> |
| 3 | + |
| 4 | +#ifdef USE_BANG |
| 5 | +#include "../../utilities/bang/cnnl_context.hh" |
| 6 | +#include "../../utilities/bang/cnnl_functions.h" |
| 7 | +#include <cnnl.h> |
| 8 | +#endif |
| 9 | + |
| 10 | +namespace refactor::kernel { |
| 11 | + using K = MatMulCnnl; |
| 12 | + using DT = DataType; |
| 13 | + |
| 14 | + K::MatMulCnnl(decltype(info) info_) noexcept |
| 15 | + : Kernel(), info(std::move(info_)) {} |
| 16 | + |
| 17 | + auto K::build(TensorRefs inputs_, TensorRefs outputs_, bool transA_, bool transB_, float alpha_, float beta_) noexcept -> KernelBox { |
| 18 | +#ifndef USE_BANG |
| 19 | + return nullptr; |
| 20 | +#endif |
| 21 | + auto dt = inputs_[0].get().dataType; |
| 22 | + return dt.isIeee754() || dt == DT::I8 |
| 23 | + ? std::make_unique<K>(decltype(info){ |
| 24 | + dt, |
| 25 | + transA_, |
| 26 | + transB_, |
| 27 | + alpha_, |
| 28 | + beta_, |
| 29 | + std::vector<int>(inputs_[0].get().shape.begin(), inputs_[0].get().shape.end()), |
| 30 | + std::vector<int>(inputs_[1].get().shape.begin(), inputs_[1].get().shape.end()), |
| 31 | + std::vector<int>(outputs_[0].get().shape.begin(), outputs_[0].get().shape.end()), |
| 32 | + inputs_.size() == 3 |
| 33 | + ? inputs_[2].get().shape.size() == 0 ? std::make_optional(std::vector<int>(1, 1)) |
| 34 | + : std::make_optional(std::vector<int>( |
| 35 | + inputs_[2].get().shape.begin(), |
| 36 | + inputs_[2].get().shape.end())) |
| 37 | + : std::nullopt, |
| 38 | + }) |
| 39 | + : nullptr; |
| 40 | + } |
| 41 | + |
| 42 | + auto K::typeId() noexcept -> size_t { |
| 43 | + static uint8_t ID = 1; |
| 44 | + return reinterpret_cast<size_t>(&ID); |
| 45 | + } |
| 46 | + |
| 47 | + auto K::kernelTypeId() const noexcept -> size_t { return typeId(); } |
| 48 | + auto K::description() const noexcept -> std::string_view { |
| 49 | + return "Performing MatMul using CNNL"; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +#ifdef USE_BANG |
| 54 | + auto K::lower(Resources &res) const noexcept -> RoutineWorkspace { |
| 55 | + using namespace cnnl; |
| 56 | + using namespace runtime; |
| 57 | + using DT = DataType; |
| 58 | + |
| 59 | + // RAII for closure |
| 60 | + struct Descriptors { |
| 61 | + cnnlTensorDescriptor_t a, b, c; |
| 62 | + cnnlMatMulDescriptor_t bmm; |
| 63 | + cnnlMatMulAlgo_t algo; |
| 64 | + cnnlMatMulHeuristicResult_t heuristic; |
| 65 | + cnnlTensorDescriptor_t bias; |
| 66 | + bool addBias, f32; |
| 67 | + |
| 68 | + explicit Descriptors(bool addBias_, bool f32_) |
| 69 | + : a(nullptr), b(nullptr), c(nullptr), |
| 70 | + bmm(nullptr), algo(nullptr), heuristic(nullptr), |
| 71 | + bias(nullptr), addBias(addBias_), f32(f32_) { |
| 72 | + CNNL_ASSERT(cnnlCreateTensorDescriptor(&a)); |
| 73 | + CNNL_ASSERT(cnnlCreateTensorDescriptor(&b)); |
| 74 | + CNNL_ASSERT(cnnlCreateTensorDescriptor(&c)); |
| 75 | + if (addBias) { |
| 76 | + CNNL_ASSERT(cnnlCreateTensorDescriptor(&bias)); |
| 77 | + } |
| 78 | + CNNL_ASSERT(cnnlMatMulDescCreate(&bmm)); |
| 79 | + CNNL_ASSERT(cnnlMatMulAlgoCreate(&algo)); |
| 80 | + CNNL_ASSERT(cnnlCreateMatMulHeuristicResult(&heuristic)); |
| 81 | + } |
| 82 | + ~Descriptors() noexcept(false) { |
| 83 | + CNNL_ASSERT(cnnlDestroyTensorDescriptor(a)); |
| 84 | + CNNL_ASSERT(cnnlDestroyTensorDescriptor(b)); |
| 85 | + CNNL_ASSERT(cnnlDestroyTensorDescriptor(c)); |
| 86 | + if (addBias) { |
| 87 | + CNNL_ASSERT(cnnlDestroyTensorDescriptor(bias)); |
| 88 | + } |
| 89 | + CNNL_ASSERT(cnnlMatMulDescDestroy(bmm)); |
| 90 | + CNNL_ASSERT(cnnlMatMulAlgoDestroy(algo)); |
| 91 | + CNNL_ASSERT(cnnlDestroyMatMulHeuristicResult(heuristic)); |
| 92 | + } |
| 93 | + |
| 94 | + Descriptors(const Descriptors &) = delete; |
| 95 | + Descriptors(Descriptors &&) = delete; |
| 96 | + }; |
| 97 | + auto d = std::make_shared<Descriptors>(info.biasDim.has_value(), info.dataType != DT::F64); |
| 98 | + setCnnlTensor(d->a, info.dataType, slice(info.aDim.data(), info.aDim.size())); |
| 99 | + setCnnlTensor(d->b, info.dataType, slice(info.bDim.data(), info.bDim.size())); |
| 100 | + setCnnlTensor(d->c, info.dataType, slice(info.cDim.data(), info.cDim.size())); |
| 101 | + if (d->addBias) { |
| 102 | + CNNL_ASSERT(cnnlSetTensorDescriptor( |
| 103 | + d->bias, CNNL_LAYOUT_NCHW, cnnlDataTypeConvert(info.dataType), |
| 104 | + info.biasDim.value().size(), info.biasDim.value().data())); |
| 105 | + } |
| 106 | + int32_t tA = info.transA, tB = info.transB; |
| 107 | + CNNL_ASSERT(cnnlSetMatMulDescAttr(d->bmm, CNNL_MATMUL_DESC_TRANSA, |
| 108 | + &tA, sizeof(int32_t))); |
| 109 | + CNNL_ASSERT(cnnlSetMatMulDescAttr(d->bmm, CNNL_MATMUL_DESC_TRANSB, |
| 110 | + &tB, sizeof(int32_t))); |
| 111 | + auto handle = res.fetchOrStore<CnnlContext>()->handle; |
| 112 | + int returnedAlgoCount = 0; |
| 113 | + CNNL_ASSERT(cnnlGetBatchMatMulAlgoHeuristic( |
| 114 | + handle, d->bmm, d->a, d->b, d->c, |
| 115 | + NULL, 1, &(d->heuristic), &returnedAlgoCount)); |
| 116 | + |
| 117 | + size_t algoWorkspaceSize; |
| 118 | + CNNL_ASSERT(cnnlGetBatchMatMulHeuristicResult(d->heuristic, d->algo, &algoWorkspaceSize)); |
| 119 | + |
| 120 | + res.fetchOrStore<CnnlContext>(); |
| 121 | + auto routine = [d = std::move(d), algoWorkspaceSize, |
| 122 | + aa = info.alpha, bb = info.beta](Resources &res, void *workspace, void const *const *inputs, void *const *outputs) { |
| 123 | + // fetch cnnl handle from resources |
| 124 | + auto handle = res.fetchOrStore<CnnlContext>()->handle; |
| 125 | + |
| 126 | + // build alpha/beta for double |
| 127 | + auto alpha = d->f32 ? factor<fp32_t>(aa) : factor<fp64_t>(aa), |
| 128 | + beta = d->f32 ? factor<fp32_t>(bb) : factor<fp64_t>(bb), |
| 129 | + // one = d->f32 ? factor<fp32_t>(1) : factor<fp64_t>(1), |
| 130 | + zero = d->f32 ? factor<fp32_t>(0) : factor<fp64_t>(0); |
| 131 | + |
| 132 | + if (d->addBias) { |
| 133 | + CNNL_ASSERT(cnnlExpand(handle, d->bias, inputs[2], d->c, outputs[0])); |
| 134 | + } |
| 135 | + |
| 136 | + if (alpha != 0) { |
| 137 | + CNNL_ASSERT(cnnlBatchMatMulBCast_v2( |
| 138 | + handle, d->bmm, d->algo, &alpha, |
| 139 | + d->a, inputs[0], d->b, inputs[1], |
| 140 | + d->addBias ? &beta : &zero, d->c, outputs[0], |
| 141 | + workspace, algoWorkspaceSize)); |
| 142 | + } |
| 143 | + |
| 144 | + BANG_ASSERT(cnrtQueueSync(res.fetchOrStore<CnnlContext>()->queue)); |
| 145 | + }; |
| 146 | + |
| 147 | + return {std::move(routine), algoWorkspaceSize}; |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | +#endif |
| 152 | + |
| 153 | +}// namespace refactor::kernel |
0 commit comments