|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <ATen/cuda/CUDAContext.h> |
| 4 | +#include <filesystem> |
| 5 | +#include <fstream> |
| 6 | +#include <regex> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#include "../utils/exception.hpp" |
| 10 | +#include "../utils/format.hpp" |
| 11 | +#include "../utils/hash.hpp" |
| 12 | +#include "../utils/system.hpp" |
| 13 | +#include "cache.hpp" |
| 14 | +#include "device_runtime.hpp" |
| 15 | + |
| 16 | +namespace deep_gemm { |
| 17 | + |
| 18 | +class Compiler { |
| 19 | + std::string library_version; |
| 20 | + std::filesystem::path library_root_path; |
| 21 | + |
| 22 | + std::string get_library_version() const { |
| 23 | + // Recursively walk through all subdirectories and update hash |
| 24 | + std::stringstream ss; |
| 25 | + for (const auto& entry: std::filesystem::recursive_directory_iterator(library_include_path / "deep_gemm")) { |
| 26 | + if (entry.is_regular_file() and entry.path().extension() == ".cuh") { |
| 27 | + std::ifstream file(entry.path(), std::ios::binary); |
| 28 | + std::string content((std::istreambuf_iterator(file)), std::istreambuf_iterator<char>()); |
| 29 | + ss << content; |
| 30 | + } |
| 31 | + } |
| 32 | + return get_hex_digest(ss.str()); |
| 33 | + } |
| 34 | + |
| 35 | +public: |
| 36 | + std::string signature, flags; |
| 37 | + std::filesystem::path library_include_path; |
| 38 | + std::filesystem::path cache_dir_path; |
| 39 | + |
| 40 | + explicit Compiler(const std::filesystem::path& library_root_path) { |
| 41 | + // Static library paths |
| 42 | + this->library_root_path = library_root_path; |
| 43 | + this->library_include_path = library_root_path / "include"; |
| 44 | + this->library_version = get_library_version(); |
| 45 | + |
| 46 | + // Cache settings |
| 47 | + cache_dir_path = std::filesystem::path(get_env<std::string>("HOME")) / ".deep_gemm"; |
| 48 | + if (const auto& env_cache_dir_path = get_env<std::string>("DG_JIT_CACHE_DIR"); not env_cache_dir_path.empty()) |
| 49 | + cache_dir_path = env_cache_dir_path; |
| 50 | + |
| 51 | + // The compiler flags applied to all derived compilers |
| 52 | + signature = "unknown-compiler"; |
| 53 | + std::string ptxas_flags = "--ptxas-options=--register-usage-level=10"; |
| 54 | + if (get_env<int>("DG_JIT_PTXAS_VERBOSE", 0)) |
| 55 | + ptxas_flags += ",--verbose"; |
| 56 | + flags = fmt::format("-std=c++20 --diag-suppress=39,161,174,177,186,940 {}", ptxas_flags); |
| 57 | + } |
| 58 | + |
| 59 | + virtual ~Compiler() = default; |
| 60 | + |
| 61 | + std::filesystem::path make_tmp_dir() const { |
| 62 | + return make_dirs(cache_dir_path / "tmp"); |
| 63 | + } |
| 64 | + |
| 65 | + std::filesystem::path get_tmp_file_path() const { |
| 66 | + return make_tmp_dir() / get_uuid(); |
| 67 | + } |
| 68 | + |
| 69 | + void put(const std::filesystem::path& path, const std::string& data) const { |
| 70 | + const auto tmp_file_path = get_tmp_file_path(); |
| 71 | + |
| 72 | + // Write into the temporary file |
| 73 | + std::ofstream out(tmp_file_path, std::ios::binary); |
| 74 | + DG_HOST_ASSERT(out.write(data.data(), data.size())); |
| 75 | + out.close(); |
| 76 | + |
| 77 | + // Atomically replace |
| 78 | + std::filesystem::rename(tmp_file_path, path); |
| 79 | + } |
| 80 | + |
| 81 | + std::shared_ptr<KernelRuntime> build(const std::string& name, const std::string& code) const { |
| 82 | + const auto kernel_signature = fmt::format("{}$${}$${}$${}$${}", name, library_version, signature, flags, code); |
| 83 | + const auto dir_path = cache_dir_path / "cache" / fmt::format("kernel.{}.{}", name, get_hex_digest(kernel_signature)); |
| 84 | + |
| 85 | + // Hit the runtime cache |
| 86 | + if (const auto& runtime = kernel_runtime_cache->get(dir_path); runtime != nullptr) |
| 87 | + return runtime; |
| 88 | + |
| 89 | + // Create the kernel directory |
| 90 | + make_dirs(dir_path); |
| 91 | + |
| 92 | + // Compile into a temporary CUBIN |
| 93 | + const auto tmp_cubin_path = get_tmp_file_path(); |
| 94 | + compile(code, dir_path, tmp_cubin_path); |
| 95 | + |
| 96 | + // Replace into the cache directory |
| 97 | + make_dirs(dir_path); |
| 98 | + std::filesystem::rename(tmp_cubin_path, dir_path / "kernel.cubin"); |
| 99 | + |
| 100 | + // Put into the runtime cache |
| 101 | + const auto& runtime = kernel_runtime_cache->get(dir_path); |
| 102 | + DG_HOST_ASSERT(runtime != nullptr); |
| 103 | + return runtime; |
| 104 | + } |
| 105 | + |
| 106 | + virtual void compile(const std::string &code, const std::filesystem::path& dir_path, const std::filesystem::path &cubin_path) const = 0; |
| 107 | +}; |
| 108 | + |
| 109 | +class NVCCCompiler final: public Compiler { |
| 110 | + std::filesystem::path nvcc_path; |
| 111 | + |
| 112 | + std::pair<int, int> get_nvcc_version() const { |
| 113 | + DG_HOST_ASSERT(std::filesystem::exists(nvcc_path)); |
| 114 | + |
| 115 | + // Call the version command |
| 116 | + const auto& command = std::string(nvcc_path) + " --version"; |
| 117 | + const auto& [return_code, output] = call_external_command(command); |
| 118 | + DG_HOST_ASSERT(return_code == 0); |
| 119 | + |
| 120 | + // The version should be at least 12.3, for the best performance with 12.9 |
| 121 | + int major, minor; |
| 122 | + std::smatch match; |
| 123 | + DG_HOST_ASSERT(std::regex_search(output, match, std::regex(R"(release (\d+\.\d+))"))); |
| 124 | + std::sscanf(match[1].str().c_str(), "%d.%d", &major, &minor); |
| 125 | + DG_HOST_ASSERT((major > 12 or (major == 12 and minor >= 3)) and "NVCC version should be >= 12.3"); |
| 126 | + if (major < 12 or (major == 12 and minor < 9)) |
| 127 | + printf("Warning: please use at least NVCC 12.9 for the best DeepGEMM performance"); |
| 128 | + return {major, minor}; |
| 129 | + } |
| 130 | + |
| 131 | +public: |
| 132 | + NVCCCompiler(const std::filesystem::path& library_root_path, |
| 133 | + const std::filesystem::path& cuda_home_path_by_torch): |
| 134 | + Compiler(library_root_path) { |
| 135 | + // Override the compiler signature |
| 136 | + nvcc_path = cuda_home_path_by_torch / "bin" / "nvcc"; |
| 137 | + if (const auto& env_nvcc_path = get_env<std::string>("DG_JIT_NVCC_COMPILER"); not env_nvcc_path.empty()) |
| 138 | + nvcc_path = env_nvcc_path; |
| 139 | + const auto& [nvcc_major, nvcc_minor] = get_nvcc_version(); |
| 140 | + signature = fmt::format("NVCC{}.{}", nvcc_major, nvcc_minor); |
| 141 | + |
| 142 | + // The override the compiler flags |
| 143 | + flags = fmt::format("{} -I{} --gpu-architecture=sm_{}a " |
| 144 | + "--compiler-options=-fPIC,-O3,-fconcepts,-Wno-deprecated-declarations,-Wno-abi " |
| 145 | + "-cubin -O3 --expt-relaxed-constexpr --expt-extended-lambda", |
| 146 | + flags, library_include_path.c_str(), device_runtime->get_arch()); |
| 147 | + } |
| 148 | + |
| 149 | + void compile(const std::string &code, const std::filesystem::path& dir_path, const std::filesystem::path &cubin_path) const override { |
| 150 | + // Write the code into the cache directory |
| 151 | + const auto& code_path = dir_path / "kernel.cu"; |
| 152 | + put(code_path, code); |
| 153 | + |
| 154 | + // Compile |
| 155 | + const auto& command = fmt::format("{} {} -o {} {}", nvcc_path.c_str(), code_path.c_str(), cubin_path.c_str(), flags); |
| 156 | + if (get_env("DG_JIT_DEBUG", 0) or get_env("DG_JIT_PRINT_COMPILER_COMMAND", 0)) |
| 157 | + printf("Running NVCC command: %s", command.c_str()); |
| 158 | + const auto& [return_code, output] = call_external_command(command); |
| 159 | + if (return_code != 0) { |
| 160 | + printf("NVCC compilation failed: %s", output.c_str()); |
| 161 | + DG_HOST_ASSERT(false and "NVCC compilation failed"); |
| 162 | + } |
| 163 | + |
| 164 | + // Print PTXAS log |
| 165 | + if (get_env("DG_JIT_DEBUG", 0) or get_env("DG_JIT_PTXAS_VERBOSE", 0)) |
| 166 | + printf("%s", output.c_str()); |
| 167 | + } |
| 168 | +}; |
| 169 | + |
| 170 | +static std::shared_ptr<Compiler> compiler = nullptr; |
| 171 | + |
| 172 | +} // namespace deep_gemm |
0 commit comments