cuda_utils.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2019-2023, 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. #pragma once
  17. #include "logger.h"
  18. #include <cuda_runtime.h>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <string>
  22. #include <vector>
  23. namespace fastertransformer {
  24. /* **************************** debug tools ********************************* */
  25. template<typename T>
  26. void check(T result, char const* const func, const char* const file, int const line)
  27. {
  28. if (result) {
  29. throw std::runtime_error(std::string("[FT][ERROR] CUDA runtime error: ") + ("<unknown>") + " "
  30. + file + ":" + std::to_string(line) + " \n");
  31. }
  32. }
  33. #define check_cuda_error(val) check((val), #val, __FILE__, __LINE__)
  34. [[noreturn]] inline void throwRuntimeError(const char* const file, int const line, std::string const& info = "")
  35. {
  36. throw std::runtime_error(std::string("[FT][ERROR] ") + info + " Assertion fail: " + file + ":"
  37. + std::to_string(line) + " \n");
  38. }
  39. inline void myAssert(bool result, const char* const file, int const line, std::string const& info = "")
  40. {
  41. if (!result) {
  42. throwRuntimeError(file, line, info);
  43. }
  44. }
  45. #define FT_CHECK(val) myAssert(val, __FILE__, __LINE__)
  46. #define FT_CHECK_WITH_INFO(val, info) \
  47. do { \
  48. bool is_valid_val = (val); \
  49. if (!is_valid_val) { \
  50. fastertransformer::myAssert(is_valid_val, __FILE__, __LINE__, (info)); \
  51. } \
  52. } while (0)
  53. /* ***************************** common utils ****************************** */
  54. inline int getSMVersion()
  55. {
  56. int device{-1};
  57. check_cuda_error(cudaGetDevice(&device));
  58. int sm_major = 0;
  59. int sm_minor = 0;
  60. check_cuda_error(cudaDeviceGetAttribute(&sm_major, cudaDevAttrComputeCapabilityMajor, device));
  61. check_cuda_error(cudaDeviceGetAttribute(&sm_minor, cudaDevAttrComputeCapabilityMinor, device));
  62. return sm_major * 10 + sm_minor;
  63. }
  64. cudaError_t getSetDevice(int i_device, int* o_device = NULL);
  65. /* ************************** end of common utils ************************** */
  66. } // namespace fastertransformer