/home/runner/work/HiCR/HiCR/include/hicr/backends/ascend/computationKernel.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/ascend/computationKernel.hpp Source File
HiCR
computationKernel.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 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
24#pragma once
25
26#include <filesystem>
27#include <fstream>
28#include <iostream>
29#include <regex>
30#include <vector>
31#include <acl/acl.h>
36
37namespace HiCR::backend::ascend
38{
44class ComputationKernel final : public Kernel
45{
46 public:
47
52 {
56 const aclDataBuffer *dataBuffer;
60 const aclTensorDesc *tensorDescriptor;
61 };
62
72 ComputationKernel(const char *kernelName, const std::vector<tensorData_t> &inputs, const std::vector<tensorData_t> &outputs, const aclopAttr *kernelAttrs)
73 : Kernel(),
74 _kernelName(kernelName),
75 _kernelAttrs(kernelAttrs)
76 {
77 // populate internal data structure with input and output tensor data
78 initializeDataBuffersAndDescriptors(inputs, _inputTensorDescriptors, _inputDataBuffers);
79 initializeDataBuffersAndDescriptors(outputs, _outputTensorDescriptors, _outputDataBuffers);
80 };
81
92 ComputationKernel(const char *kernelPath, const char *kernelName, const std::vector<tensorData_t> &inputs, const std::vector<tensorData_t> &outputs, const aclopAttr *kernelAttrs)
93 : ComputationKernel(kernelName, inputs, outputs, kernelAttrs)
94 {
95 // load kernel in memory
96 loadKernel(std::string(kernelPath));
97 };
98
99 ComputationKernel() = delete;
100 ~ComputationKernel() = default;
101
109 static tensorData_t createTensorData(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot, aclTensorDesc *tensorDescriptor)
110 {
111 // Using up-casting to determine device types
112 auto ascendSlot = dynamic_pointer_cast<ascend::LocalMemorySlot>(memorySlot);
113
114 // Checking whether the memory slot passed is compatible with this backend
115 if (ascendSlot == NULL) HICR_THROW_LOGIC("Attempting to create Ascend tensor data with a memory slot that is not supported by this backend\n");
116
117 // Creating and returning new tensor
118 return ascend::ComputationKernel::tensorData_t{.dataBuffer = ascendSlot->getDataBuffer(), .tensorDescriptor = tensorDescriptor};
119 }
120
126 __INLINE__ void start(const aclrtStream stream) override
127 {
128 // start the kernel
129 aclError err = aclopExecuteV2(_kernelName.c_str(),
130 (int)_inputTensorDescriptors.size(),
131 (aclTensorDesc **)_inputTensorDescriptors.data(),
132 (aclDataBuffer **)_inputDataBuffers.data(),
133 (int)_outputTensorDescriptors.size(),
134 (aclTensorDesc **)_outputTensorDescriptors.data(),
135 (aclDataBuffer **)_outputDataBuffers.data(),
136 (aclopAttr *)_kernelAttrs,
137 stream);
138
139 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Failed to run the kernel. Error %d", err);
140 }
141
142 private:
143
147 const std::string _kernelName;
151 const aclopAttr *_kernelAttrs;
155 std::vector<const aclTensorDesc *> _inputTensorDescriptors;
159 std::vector<const aclTensorDesc *> _outputTensorDescriptors;
163 std::vector<const aclDataBuffer *> _inputDataBuffers;
167 std::vector<const aclDataBuffer *> _outputDataBuffers;
171 std::string _kernelPtr;
175 size_t _kernelSize;
176
184 __INLINE__ void initializeDataBuffersAndDescriptors(const std::vector<tensorData_t> tensors,
185 std::vector<const aclTensorDesc *> &descriptors,
186 std::vector<const aclDataBuffer *> &dataBuffers)
187 {
188 for (const auto &tensor : tensors)
189 {
190 dataBuffers.push_back(tensor.dataBuffer);
191 descriptors.push_back(tensor.tensorDescriptor);
192 }
193 }
194
200 __INLINE__ void loadKernel(const std::string &kernelPath)
201 {
202 // get size of file to know how much memory to allocate
203 std::uintmax_t filesize = std::filesystem::file_size(kernelPath);
204 _kernelSize = filesize;
205
206 // allocate buffer to hold file
207 _kernelPtr.resize(_kernelSize);
208
209 // read file
210 std::ifstream fin(kernelPath, std::ios::binary);
211 fin.read((char *)_kernelPtr.data(), _kernelSize);
212 if (!fin) HICR_THROW_RUNTIME("Error reading file could only read %d bytes", fin.gcount());
213
214 fin.close();
215
216 // register the operator in the ACL runtime
217 aclError err = aclopLoad(_kernelPtr.data(), _kernelSize);
218
219 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Failed to load kernel into memory. Error %d", err);
220 }
221};
222
223} // namespace HiCR::backend::ascend
This file implements the Kernel class for the Ascend backend.
Provides a definition for the local memory slot class for the Ascend backend.
Definition computationKernel.hpp:45
ComputationKernel(const char *kernelName, const std::vector< tensorData_t > &inputs, const std::vector< tensorData_t > &outputs, const aclopAttr *kernelAttrs)
Definition computationKernel.hpp:72
ComputationKernel(const char *kernelPath, const char *kernelName, const std::vector< tensorData_t > &inputs, const std::vector< tensorData_t > &outputs, const aclopAttr *kernelAttrs)
Definition computationKernel.hpp:92
static tensorData_t createTensorData(const std::shared_ptr< HiCR::LocalMemorySlot > &memorySlot, aclTensorDesc *tensorDescriptor)
Definition computationKernel.hpp:109
__INLINE__ void start(const aclrtStream stream) override
Definition computationKernel.hpp:126
Definition kernel.hpp:36
Provides a definition for a HiCR Local Memory Slot class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67
const aclTensorDesc * tensorDescriptor
Definition computationKernel.hpp:60
const aclDataBuffer * dataBuffer
Definition computationKernel.hpp:56