decoder_xqa_common.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2020-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 <cassert>
  18. #include <limits.h>
  19. #include <stdint.h>
  20. #include <torch/all.h>
  21. #include <c10/cuda/CUDAGuard.h>
  22. #include <c10/cuda/CUDAStream.h>
  23. #include "cuda_compat.h"
  24. #include <ATen/cuda/CUDAContext.h>
  25. #define HOST_DEVICE_FUNC __host__ __device__
  26. #define DEVICE_FUNC __device__
  27. inline void cuErrCheck_(CUresult stat, char const* file, int line) {
  28. if (stat != CUDA_SUCCESS) {
  29. char const* msg = nullptr;
  30. cuGetErrorName(stat, &msg);
  31. fprintf(stderr, "CUDA Error: %s %s %d\n", msg, file, line);
  32. }
  33. }
  34. #define cuErrCheck(stat) \
  35. { \
  36. cuErrCheck_((stat), __FILE__, __LINE__); \
  37. }
  38. #define CUDACHECK(cmd) \
  39. do { \
  40. cudaError_t e = cmd; \
  41. if (e != cudaSuccess) { \
  42. printf("Failed: Cuda error %s:%d '%s'\n", __FILE__, __LINE__, \
  43. cudaGetErrorString(e)); \
  44. exit(EXIT_FAILURE); \
  45. } \
  46. } while (0)
  47. inline constexpr int kMinHistoryTokensPerBlock = 128;
  48. inline constexpr float kEnableMinBlockFactor = 4.0;
  49. inline constexpr int kTargetWaveFactor = 8;
  50. // For multi-block mode. We reserve workspace for this amount of sub-sequences.
  51. // This should be enough. Huge batch size may result in larger value, but for
  52. // large batch size, multi-block mode is not useful. For llama v2 70b, 6000
  53. // results in ~12MB multi-block workspace, and is enough for > 10 waves.
  54. inline constexpr int kXQA_MAX_NUM_SUB_SEQ = 6000;
  55. inline constexpr int kMaxBeamWidth = 1;
  56. inline int getDevice() {
  57. int current_dev_id = 0;
  58. CUDACHECK(cudaGetDevice(&current_dev_id));
  59. return current_dev_id;
  60. }
  61. inline int getSMVersion() {
  62. int device{-1};
  63. CUDACHECK(cudaGetDevice(&device));
  64. int sm_major = 0;
  65. int sm_minor = 0;
  66. CUDACHECK(cudaDeviceGetAttribute(&sm_major, cudaDevAttrComputeCapabilityMajor,
  67. device));
  68. CUDACHECK(cudaDeviceGetAttribute(&sm_minor, cudaDevAttrComputeCapabilityMinor,
  69. device));
  70. return sm_major * 10 + sm_minor;
  71. }
  72. // For xqa kernel IO
  73. enum Data_type {
  74. DATA_TYPE_FP16,
  75. DATA_TYPE_BF16,
  76. DATA_TYPE_FP32,
  77. DATA_TYPE_INT8,
  78. DATA_TYPE_INT32,
  79. DATA_TYPE_E4M3,
  80. DATA_TYPE_E5M2,
  81. DATA_TYPE_UNKNOWN
  82. };
  83. // Type trait to map types to enum values
  84. template <typename T>
  85. struct TypeToDataType {
  86. static constexpr Data_type value = Data_type::DATA_TYPE_UNKNOWN;
  87. };
  88. // Specialize the trait for specific types
  89. template <>
  90. struct TypeToDataType<__nv_bfloat16> {
  91. static constexpr Data_type value = Data_type::DATA_TYPE_BF16;
  92. };
  93. template <>
  94. struct TypeToDataType<__half> {
  95. static constexpr Data_type value = Data_type::DATA_TYPE_FP16;
  96. };
  97. template <>
  98. struct TypeToDataType<uint8_t> {
  99. static constexpr Data_type value = Data_type::DATA_TYPE_E4M3;
  100. };
  101. static inline size_t get_size_in_bytes(size_t n, Data_type dtype) {
  102. switch (dtype) {
  103. case DATA_TYPE_FP32:
  104. return n * 4;
  105. case DATA_TYPE_FP16:
  106. return n * 2;
  107. case DATA_TYPE_INT32:
  108. return n * 4;
  109. case DATA_TYPE_INT8:
  110. return n;
  111. case DATA_TYPE_BF16:
  112. return n * 2;
  113. case DATA_TYPE_E4M3:
  114. return n;
  115. case DATA_TYPE_E5M2:
  116. return n;
  117. default:
  118. TORCH_CHECK(false, "FMHA Data Type is not supported.");
  119. return 0;
  120. }
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////////////////////////
  123. static inline size_t get_size_in_bytes(Data_type dtype) {
  124. return get_size_in_bytes(1, dtype);
  125. }
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////
  127. constexpr int32_t kSM_70 = 70;
  128. constexpr int32_t kSM_72 = 72;
  129. constexpr int32_t kSM_75 = 75;
  130. constexpr int32_t kSM_80 = 80;
  131. constexpr int32_t kSM_86 = 86;
  132. constexpr int32_t kSM_89 = 89;
  133. constexpr int32_t kSM_90 = 90;