marlin.cuh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #include <torch/all.h>
  3. #include <ATen/cuda/CUDAContext.h>
  4. #include <c10/cuda/CUDAGuard.h>
  5. #include <cuda.h>
  6. #include <cuda_fp16.h>
  7. #include <cuda_runtime.h>
  8. #include <iostream>
  9. namespace marlin {
  10. // Marlin params
  11. // 8 warps are a good choice since every SM has 4 schedulers and having more
  12. // than 1 warp per schedule allows some more latency hiding. At the same time,
  13. // we want relatively few warps to have many registers per warp and small tiles.
  14. static constexpr int default_threads = 256;
  15. static constexpr int pipe_stages =
  16. 4; // 4 pipeline stages fit into shared memory
  17. static constexpr int min_thread_n = 64;
  18. static constexpr int min_thread_k = 64;
  19. static constexpr int tile_size = 16;
  20. static constexpr int max_par = 16;
  21. // Repack params
  22. static constexpr int repack_stages = 8;
  23. static constexpr int repack_threads = 256;
  24. static constexpr int tile_k_size = tile_size;
  25. static constexpr int tile_n_size = tile_k_size * 4;
  26. // Helpers
  27. template <typename T, int n>
  28. struct Vec {
  29. T elems[n];
  30. __device__ T& operator[](int i) { return elems[i]; }
  31. };
  32. using I4 = Vec<int, 4>;
  33. constexpr int div_ceil(int a, int b) { return (a + b - 1) / b; }
  34. #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800
  35. // No support for async
  36. #else
  37. __device__ inline void cp_async4_pred(void* smem_ptr, const void* glob_ptr,
  38. bool pred = true) {
  39. const int BYTES = 16;
  40. uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr));
  41. asm volatile(
  42. "{\n"
  43. " .reg .pred p;\n"
  44. " setp.ne.b32 p, %0, 0;\n"
  45. " @p cp.async.cg.shared.global [%1], [%2], %3;\n"
  46. "}\n" ::"r"((int)pred),
  47. "r"(smem), "l"(glob_ptr), "n"(BYTES));
  48. }
  49. __device__ inline void cp_async4(void* smem_ptr, const void* glob_ptr) {
  50. const int BYTES = 16;
  51. uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr));
  52. asm volatile(
  53. "{\n"
  54. " cp.async.cg.shared.global [%0], [%1], %2;\n"
  55. "}\n" ::"r"(smem),
  56. "l"(glob_ptr), "n"(BYTES));
  57. }
  58. __device__ inline void cp_async_fence() {
  59. asm volatile("cp.async.commit_group;\n" ::);
  60. }
  61. template <int n>
  62. __device__ inline void cp_async_wait() {
  63. asm volatile("cp.async.wait_group %0;\n" ::"n"(n));
  64. }
  65. #endif
  66. } // namespace marlin