|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <mutex> |
| 4 | +#include <stdexcept> |
| 5 | +#include <string> |
| 6 | +#include <unordered_map> |
| 7 | + |
| 8 | +#include "c10/util/Logging.h" |
| 9 | +#include "tops_runtime_api.h" |
| 10 | +#include "fmt/core.h" |
| 11 | +#include "triton_jit/backend_policy.h" |
| 12 | +#include "triton_jit/kernel_metadata.h" |
| 13 | + |
| 14 | +namespace triton_jit { |
| 15 | + |
| 16 | +struct GcuBackend { |
| 17 | + using StreamType = topsStream_t; |
| 18 | + using ContextType = void*; |
| 19 | + using KernelHandle = topsFunction_t; |
| 20 | + |
| 21 | + // GCU blockDimX = num_warps directly, so WARP_SIZE = 1 |
| 22 | + static constexpr unsigned int WARP_SIZE = 1; |
| 23 | + |
| 24 | + struct LaunchOptions { |
| 25 | + unsigned int shared_memory = 0; |
| 26 | + }; |
| 27 | + |
| 28 | + static inline std::unordered_map<std::string, std::pair<topsModule_t, topsFunction_t>> module_cache_; |
| 29 | + static inline std::mutex cache_mutex_; |
| 30 | + |
| 31 | + static LaunchOptions prepare_launch(const std::string& dir, |
| 32 | + const std::string& name, |
| 33 | + unsigned int shared_mem, |
| 34 | + const std::string& sig, |
| 35 | + size_t num_args) { |
| 36 | + return {.shared_memory = shared_mem}; |
| 37 | + } |
| 38 | + |
| 39 | + static void launch_kernel(topsStream_t stream, |
| 40 | + topsFunction_t kernel, |
| 41 | + unsigned grid_x, |
| 42 | + unsigned grid_y, |
| 43 | + unsigned grid_z, |
| 44 | + unsigned block_x, |
| 45 | + unsigned block_y, |
| 46 | + unsigned block_z, |
| 47 | + void** args, |
| 48 | + const LaunchOptions& opts) { |
| 49 | + LOG(INFO) << fmt::format("topsModuleLaunchKernelEx: grid=({},{},{}), num_warps={}, shmem={}", |
| 50 | + grid_x, grid_y, grid_z, block_x, opts.shared_memory); |
| 51 | + |
| 52 | + topsLaunchConfig_t config; |
| 53 | + memset(&config, 0, sizeof(config)); |
| 54 | + config.gridDim = dim3(grid_x, grid_y, grid_z); |
| 55 | + config.blockDim = dim3(1, 1, 1); |
| 56 | + config.dynamicSmemBytes = opts.shared_memory; |
| 57 | + config.stream = stream; |
| 58 | + |
| 59 | + topsLaunchAttribute_t att; |
| 60 | + att.id = topsLaunchAttributeThreadDimension; |
| 61 | + att.val.ThreadDim.x = block_x; |
| 62 | + att.val.ThreadDim.y = 1; |
| 63 | + att.val.ThreadDim.z = 1; |
| 64 | + config.attrs = &att; |
| 65 | + config.numAttrs = 1; |
| 66 | + |
| 67 | + topsError_t result = topsModuleLaunchKernelEx(&config, kernel, args, nullptr); |
| 68 | + |
| 69 | + if (result != topsSuccess) { |
| 70 | + throw std::runtime_error(fmt::format("GCU kernel launch failed: {} (error code {})", |
| 71 | + topsGetErrorString(result), static_cast<int>(result))); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + static void ensure_context() { |
| 76 | + // GCU uses tops runtime which manages context implicitly via topsSetDevice |
| 77 | + } |
| 78 | + |
| 79 | + static int get_device_index() { |
| 80 | + int device_id = 0; |
| 81 | + topsError_t result = topsGetDevice(&device_id); |
| 82 | + |
| 83 | + if (result != topsSuccess) { |
| 84 | + throw std::runtime_error(fmt::format("Failed to get GCU device index: {} (error code {})", |
| 85 | + topsGetErrorString(result), static_cast<int>(result))); |
| 86 | + } |
| 87 | + return device_id; |
| 88 | + } |
| 89 | + |
| 90 | + static topsFunction_t load_kernel(const std::string& dir, const std::string& kernel_name) { |
| 91 | + std::string key = fmt::format("{}::{}", dir, kernel_name); |
| 92 | + std::lock_guard<std::mutex> lock(cache_mutex_); |
| 93 | + |
| 94 | + auto it = module_cache_.find(key); |
| 95 | + if (it != module_cache_.end()) { |
| 96 | + return it->second.second; |
| 97 | + } |
| 98 | + |
| 99 | + LOG(INFO) << fmt::format("Loading GCU kernel {}", kernel_name); |
| 100 | + |
| 101 | + std::string fatbin_path = fmt::format("{}/{}.fatbin", dir, kernel_name); |
| 102 | + LOG(INFO) << fmt::format("Loading fatbin from {}", fatbin_path); |
| 103 | + |
| 104 | + topsModule_t module; |
| 105 | + topsError_t err = topsModuleLoad(&module, fatbin_path.c_str()); |
| 106 | + if (err != topsSuccess) { |
| 107 | + throw std::runtime_error(fmt::format("Failed to load GCU module from: {} ({}, error {})", |
| 108 | + fatbin_path, topsGetErrorString(err), static_cast<int>(err))); |
| 109 | + } |
| 110 | + |
| 111 | + topsFunction_t function; |
| 112 | + err = topsModuleGetFunction(&function, module, kernel_name.c_str()); |
| 113 | + if (err != topsSuccess) { |
| 114 | + topsModuleUnload(module); |
| 115 | + throw std::runtime_error(fmt::format("Failed to get function '{}' from module ({}, error {})", |
| 116 | + kernel_name, topsGetErrorString(err), static_cast<int>(err))); |
| 117 | + } |
| 118 | + |
| 119 | + module_cache_[key] = {module, function}; |
| 120 | + return function; |
| 121 | + } |
| 122 | + |
| 123 | + static unsigned int get_shared_memory(const std::string& dir, const std::string& kernel_name) { |
| 124 | + return load_shared_memory(dir, kernel_name); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +static_assert(BackendPolicy<GcuBackend>, "GcuBackend must satisfy BackendPolicy concept"); |
| 129 | + |
| 130 | +} // namespace triton_jit |
0 commit comments