| 1 | /******************************************************************************* |
| 2 | * |
| 3 | * MIT License |
| 4 | * |
| 5 | * Copyright (C) 2022-2023 Advanced Micro Devices, Inc. All rights reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | * |
| 25 | *******************************************************************************/ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <hip/hip_runtime.h> |
| 30 | #include <hip/hip_runtime_api.h> |
| 31 | #include <sstream> |
| 32 | #include <stdexcept> |
| 33 | |
| 34 | #include <Tensile/TensorDescriptor.hpp> |
| 35 | #include <Tensile/Utils.hpp> |
| 36 | |
| 37 | #define HIP_CHECK_EXC(expr) \ |
| 38 | do \ |
| 39 | { \ |
| 40 | hipError_t e = (expr); \ |
| 41 | if(e) \ |
| 42 | { \ |
| 43 | const char* errName = hipGetErrorName(e); \ |
| 44 | const char* errMsg = hipGetErrorString(e); \ |
| 45 | std::ostringstream msg; \ |
| 46 | msg << "Error " << e << "(" << errName << ") " << __FILE__ << ":" << __LINE__ << ": " \ |
| 47 | << std::endl \ |
| 48 | << #expr << std::endl \ |
| 49 | << errMsg << std::endl; \ |
| 50 | throw std::runtime_error(msg.str()); \ |
| 51 | } \ |
| 52 | } while(0) |
| 53 | |
| 54 | #define HIP_CHECK_EXC_MESSAGE(expr, message) \ |
| 55 | do \ |
| 56 | { \ |
| 57 | hipError_t e = (expr); \ |
| 58 | if(e) \ |
| 59 | { \ |
| 60 | const char* errName = hipGetErrorName(e); \ |
| 61 | const char* errMsg = hipGetErrorString(e); \ |
| 62 | std::ostringstream msg; \ |
| 63 | msg << "Error " << e << "(" << errName << ") " << __FILE__ << ":" << __LINE__ << ": " \ |
| 64 | << std::endl \ |
| 65 | << #expr << std::endl \ |
| 66 | << errMsg << std::endl \ |
| 67 | << (message) << std::endl; \ |
| 68 | throw std::runtime_error(msg.str()); \ |
| 69 | } \ |
| 70 | } while(0) |
| 71 | |
| 72 | #define HIP_CHECK_RETURN(expr) \ |
| 73 | do \ |
| 74 | { \ |
| 75 | hipError_t e = (expr); \ |
| 76 | if(e) \ |
| 77 | return e; \ |
| 78 | } while(0) |
| 79 | |
| 80 | #define HIP_CHECK_PRINT(expr) \ |
| 81 | { \ |
| 82 | hipError_t e = (expr); \ |
| 83 | if(e) \ |
| 84 | std::cout << "Error code " << e << std::endl; \ |
| 85 | } |
| 86 | |
| 87 | namespace TensileLite |
| 88 | { |
| 89 | namespace hip |
| 90 | { |
| 91 | inline void CopyTensorVoid(void* dst, |
| 92 | void const* src, |
| 93 | TensorDescriptor const& desc, |
| 94 | hipMemcpyKind direction, |
| 95 | hipStream_t stream = 0) |
| 96 | { |
| 97 | if(desc.dimensions() == 0 || desc.totalLogicalElements() == 0) |
| 98 | return; |
| 99 | |
| 100 | auto const& sizes = desc.sizes(); |
| 101 | auto const& strides = desc.strides(); |
| 102 | std::vector<size_t> coord(desc.dimensions(), 0); |
| 103 | |
| 104 | size_t contiguousDimensions = 0; |
| 105 | size_t expectedStride = 1; |
| 106 | |
| 107 | // Optimize the number of copy operations by coalescing all the |
| 108 | // dimensions that are contiguous in memory. |
| 109 | for(size_t i = 0; i < desc.dimensions(); i++) |
| 110 | { |
| 111 | if(strides[i] > expectedStride) |
| 112 | break; |
| 113 | |
| 114 | contiguousDimensions = i + 1; |
| 115 | |
| 116 | if(i < desc.dimensions() - 1) |
| 117 | expectedStride = strides[i] * sizes[i]; |
| 118 | } |
| 119 | |
| 120 | auto copyCount = CoordCount(sizes.begin() + contiguousDimensions, sizes.end()); |
| 121 | |
| 122 | size_t maxStride |
| 123 | = *std::max_element(strides.begin(), strides.begin() + contiguousDimensions); |
| 124 | size_t copyBytes = maxStride * sizes.at(contiguousDimensions - 1) * desc.elementBytes(); |
| 125 | |
| 126 | for(size_t idx = 0; idx < copyCount; idx++) |
| 127 | { |
| 128 | CoordNumbered(idx, |
| 129 | coord.begin() + contiguousDimensions, |
| 130 | coord.end(), |
| 131 | sizes.begin() + contiguousDimensions, |
| 132 | sizes.end()); |
| 133 | |
| 134 | auto beginOffset = desc.index(coord); |
| 135 | auto bytesOffset = desc.elementBytes() * beginOffset; |
| 136 | uint8_t* dstBytes = (uint8_t*)dst + bytesOffset; |
| 137 | uint8_t* srcBytes = (uint8_t*)dst + bytesOffset; |
| 138 | |
| 139 | HIP_CHECK_EXC(hipMemcpyAsync(dstBytes, srcBytes, copyBytes, direction, stream)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | template <typename T> |
| 144 | void CopyTensor(T* dst, |
| 145 | T const* src, |
| 146 | TensorDescriptor const& desc, |
| 147 | hipMemcpyKind direction, |
| 148 | hipStream_t stream = 0) |
| 149 | { |
| 150 | if(desc.dimensions() == 0 || desc.totalLogicalElements() == 0) |
| 151 | return; |
| 152 | |
| 153 | auto const& sizes = desc.sizes(); |
| 154 | auto const& strides = desc.strides(); |
| 155 | std::vector<size_t> coord(desc.dimensions(), 0); |
| 156 | |
| 157 | size_t contiguousDimensions = 0; |
| 158 | size_t expectedStride = 1; |
| 159 | |
| 160 | // Optimize the number of copy operations by coalescing all the |
| 161 | // dimensions that are contiguous in memory. |
| 162 | for(size_t i = 0; i < desc.dimensions(); i++) |
| 163 | { |
| 164 | if(strides[i] > expectedStride) |
| 165 | break; |
| 166 | |
| 167 | contiguousDimensions = i + 1; |
| 168 | |
| 169 | if(i < desc.dimensions() - 1) |
| 170 | expectedStride = strides[i] * sizes[i]; |
| 171 | } |
| 172 | |
| 173 | auto copyCount = CoordCount(sizes.begin() + contiguousDimensions, sizes.end()); |
| 174 | |
| 175 | size_t maxStride |
| 176 | = *std::max_element(strides.begin(), strides.begin() + contiguousDimensions); |
| 177 | size_t copyBytes = maxStride * sizes.at(contiguousDimensions - 1) * sizeof(T); |
| 178 | |
| 179 | for(size_t idx = 0; idx < copyCount; idx++) |
| 180 | { |
| 181 | CoordNumbered(idx, |
| 182 | coord.begin() + contiguousDimensions, |
| 183 | coord.end(), |
| 184 | sizes.begin() + contiguousDimensions, |
| 185 | sizes.end()); |
| 186 | |
| 187 | auto beginOffset = desc.index(coord); |
| 188 | |
| 189 | HIP_CHECK_EXC(hipMemcpyAsync( |
| 190 | dst + beginOffset, src + beginOffset, copyBytes, direction, stream)); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | template <typename T> |
| 195 | void CopyBuffer(T* dst, |
| 196 | T const* src, |
| 197 | const size_t copyBytes, |
| 198 | hipMemcpyKind direction, |
| 199 | hipStream_t stream = 0) |
| 200 | { |
| 201 | HIP_CHECK_EXC(hipMemcpyAsync(dst, src, copyBytes, direction, stream)); |
| 202 | } |
| 203 | } // namespace hip |
| 204 | } // namespace TensileLite |