|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 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 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include "cutlass/cutlass.h" |
| 20 | +#include <climits> |
| 21 | + |
| 22 | +namespace vllm { |
| 23 | +namespace common { |
| 24 | + |
| 25 | +class CudaException : public std::runtime_error { |
| 26 | + public: |
| 27 | + CudaException(const std::string& file, int line, const std::string& message) |
| 28 | + : std::runtime_error("CUDA Error at " + file + ":" + |
| 29 | + std::to_string(line) + " - " + message) {} |
| 30 | +}; |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +void check(T result, const char* func, const char* file, int line) { |
| 34 | + if (result != cudaSuccess) { |
| 35 | + throw CudaException( |
| 36 | + file, line, |
| 37 | + std::string("[VLLM][ERROR] CUDA runtime error in ") + func + ": " + |
| 38 | + cudaGetErrorString(static_cast<cudaError_t>(result))); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#define check_cuda_error(val) check((val), #val, __FILE__, __LINE__) |
| 43 | + |
| 44 | +inline int getMaxSharedMemoryPerBlockOptin() { |
| 45 | + int device_id; |
| 46 | + int max_shared_memory_per_block; |
| 47 | + check_cuda_error(cudaGetDevice(&device_id)); |
| 48 | + check_cuda_error(cudaDeviceGetAttribute( |
| 49 | + &max_shared_memory_per_block, cudaDevAttrMaxSharedMemoryPerBlockOptin, |
| 50 | + device_id)); |
| 51 | + return max_shared_memory_per_block; |
| 52 | +} |
| 53 | + |
| 54 | +inline int getSMVersion() { |
| 55 | + int device{-1}; |
| 56 | + check_cuda_error(cudaGetDevice(&device)); |
| 57 | + int sm_major = 0; |
| 58 | + int sm_minor = 0; |
| 59 | + check_cuda_error(cudaDeviceGetAttribute( |
| 60 | + &sm_major, cudaDevAttrComputeCapabilityMajor, device)); |
| 61 | + check_cuda_error(cudaDeviceGetAttribute( |
| 62 | + &sm_minor, cudaDevAttrComputeCapabilityMinor, device)); |
| 63 | + return sm_major * 10 + sm_minor; |
| 64 | +} |
| 65 | + |
| 66 | +} // namespace common |
| 67 | +} // namespace vllm |
0 commit comments