GroupedMMUtils.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #include <ATen/core/Tensor.h>
  3. #include <ATen/TensorUtils.h>
  4. #ifndef AT_PER_OPERATOR_HEADERS
  5. #include <ATen/CPUFunctions.h>
  6. #include <ATen/Functions.h>
  7. #include <ATen/NativeFunctions.h>
  8. #else
  9. #include <ATen/ops/bmm.h>
  10. #include <ATen/ops/empty.h>
  11. #include <ATen/ops/empty_strided.h>
  12. #include <ATen/ops/mm.h>
  13. #endif
  14. namespace at::native {
  15. inline bool check_valid_strides_and_return_transposed(const Tensor& mat) {
  16. IntArrayRef tensor_strides = mat.strides();
  17. IntArrayRef tensor_sizes = mat.sizes();
  18. int end_dim = mat.dim() - 1;
  19. int alignment = 16 / mat.element_size();
  20. TORCH_CHECK(uint64_t(mat.data_ptr()) % 16 ==0, "expected data_ptr to be aligned to 16 bytes\n");
  21. if ((tensor_strides[end_dim - 1] == 1) && (tensor_strides[end_dim] >= std::max<int64_t>(1, tensor_sizes[end_dim - 1]))) {
  22. TORCH_CHECK(tensor_strides[end_dim] % alignment == 0, "strides should be multiple of 16 bytes");
  23. return true;
  24. } else if ((tensor_strides[end_dim] == 1) && (tensor_strides[end_dim - 1] >= std::max<int64_t>(1, tensor_sizes[end_dim]))) {
  25. TORCH_CHECK(tensor_strides[end_dim - 1] % alignment == 0, "strides should be multiple of 16 bytes");
  26. return false;
  27. } else {
  28. TORCH_CHECK(false, "Invalid strides/sizes, got ", mat.strides(), " for strides and ", mat.sizes(), " for sizes");
  29. }
  30. }
  31. inline at::Tensor create_grouped_gemm_output_tensor(const Tensor& mat_a,
  32. const Tensor& mat_b,
  33. const std::optional<at::Tensor>& offs,
  34. c10::ScalarType out_dtype
  35. ) {
  36. c10::SmallVector<int64_t, 3> out_size;
  37. const bool a_is_2d = mat_a.dim() == 2;
  38. const bool b_is_2d = mat_b.dim() == 2;
  39. if (a_is_2d) {
  40. if (b_is_2d) {
  41. out_size = {offs->size(0), mat_a.size(0), mat_b.size(1)};
  42. } else {
  43. TORCH_CHECK(offs->size(0) == mat_b.size(0), "matrix batch sizes have to match");
  44. out_size = {mat_a.size(0), mat_b.size(-1)};
  45. }
  46. } else {
  47. if (b_is_2d) {
  48. // this case is not actually encountered for MoE gemms
  49. TORCH_CHECK(offs->size(0) == mat_a.size(0), "matrix batch sizes have to match");
  50. out_size = {mat_a.size(1), mat_b.size(1)};
  51. } else { // regular bmm
  52. TORCH_CHECK(mat_a.size(0) == mat_b.size(0), "batched dimension has to match");
  53. out_size = {mat_a.size(0), mat_a.size(1), mat_b.size(-1)};
  54. }
  55. }
  56. #ifndef USE_ROCM
  57. // For TMA transfers, strides of output tensor have to be either
  58. // 1, or aligned to 16 bytes.
  59. const auto last_dim = out_size.size() - 1;
  60. const auto alignment = 16 / c10::elementSize(out_dtype);
  61. const int64_t size_padded = (out_size[last_dim] + alignment - 1) / alignment * alignment;
  62. std::vector<int64_t> out_stride;
  63. if (a_is_2d != b_is_2d) {
  64. out_stride = {size_padded, 1};
  65. } else {
  66. out_stride = {out_size[1] * size_padded, size_padded, 1};
  67. }
  68. return at::empty_strided(out_size, out_stride, mat_a.options().dtype(out_dtype));
  69. #else
  70. return at::empty(out_size, mat_a.options().dtype(out_dtype));
  71. #endif
  72. }
  73. inline void _grouped_mm_validate_inputs(const Tensor& mat_a, const Tensor& mat_b,
  74. const std::optional<at::Tensor>& offs,
  75. const std::optional<at::Tensor>& bias,
  76. std::optional<c10::ScalarType> out_dtype) {
  77. TORCH_CHECK((mat_a.dtype() == at::kBFloat16) || (mat_a.dtype() == at::kFloat) || (mat_a.dtype() == at::kHalf), "Expected mat_a to be Float32, BFloat16 or Float16 matrix, got ", mat_a.scalar_type());
  78. TORCH_CHECK((mat_b.dtype() == at::kBFloat16) || (mat_b.dtype() == at::kFloat) || (mat_b.dtype() == at::kHalf), "Expected mat_b to be Float32, BFloat16 or Float16 matrix, got ", mat_b.scalar_type());
  79. TORCH_CHECK(mat_a.dim() == 2 || mat_a.dim() == 3, "mat_a has to be 2 or 3d");
  80. TORCH_CHECK(mat_b.dim() == 2 || mat_b.dim() == 3, "mat_b has to be 2 or 3d");
  81. const bool a_is_2d = mat_a.dim() == 2;
  82. const bool b_is_2d = mat_b.dim() == 2;
  83. if (!a_is_2d || !b_is_2d) {
  84. TORCH_CHECK(mat_a.size(-1) == mat_b.size(-2), "contraction dimension of mat_a and mat_b must match");
  85. }
  86. // check that the strides are valid, the fn will throw an error if not
  87. check_valid_strides_and_return_transposed(mat_a);
  88. check_valid_strides_and_return_transposed(mat_b);
  89. TORCH_CHECK(offs.has_value() == (a_is_2d || b_is_2d), "Have to provide offsets if there is a 2d matrix, or no offset if both matrices are 3d");
  90. if (offs.has_value()) {
  91. TORCH_CHECK(offs->dim() == 1, "offs has to be 1D");
  92. TORCH_CHECK(offs->dtype() == at::kInt, "Offsets have to be int32");
  93. }
  94. TORCH_CHECK(!bias.has_value(), "Bias not supported yet");
  95. }
  96. inline c10::ScalarType _resolve_grouped_mm_out_dtype(const Tensor& mat_a, const Tensor& mat_b,
  97. std::optional<c10::ScalarType> out_dtype) {
  98. const auto out_dtype_ = out_dtype.value_or(mat_a.scalar_type());
  99. // TODO(future PR): enable float32 output dtype for bfloat16 and float16 inputs
  100. TORCH_CHECK(out_dtype_ == mat_a.dtype(), "Grouped gemm output dtype must match `mat_a` dtype");
  101. return out_dtype_;
  102. }
  103. inline void _grouped_mm_fallback(const Tensor& mat_a, const Tensor& mat_b,
  104. const std::optional<at::Tensor>& offs,
  105. const std::optional<at::Tensor>& bias,
  106. std::optional<c10::ScalarType> out_dtype,
  107. Tensor out) {
  108. LOG(INFO) << "fallback path for `torch._grouped_mm`, performance may not be optimal";
  109. const bool a_is_2d = mat_a.dim() == 2;
  110. const bool b_is_2d = mat_b.dim() == 2;
  111. if (a_is_2d && !b_is_2d) {
  112. // 2d x 3d with offsets
  113. int group_start_idx = 0;
  114. auto offs_cpu = offs.value().cpu();
  115. for (int group_idx = 0; group_idx < offs_cpu.size(0); group_idx++) {
  116. int group_end_idx = offs_cpu[group_idx].item<int>();
  117. auto mat_a_slice = mat_a.slice(0, group_start_idx, group_end_idx);
  118. auto out_slice = out.slice(0, group_start_idx, group_end_idx);
  119. at::mm_out(out_slice, mat_a_slice, mat_b[group_idx]);
  120. group_start_idx = group_end_idx;
  121. }
  122. } else if (!a_is_2d && b_is_2d) {
  123. // 3d x 2d with offsets
  124. int group_start_idx = 0;
  125. auto offs_cpu = offs.value().cpu();
  126. for (int group_idx = 0; group_idx < offs_cpu.size(0); group_idx++) {
  127. int group_end_idx = offs_cpu[group_idx].item<int>();
  128. auto mat_b_slice = mat_b.slice(1, group_start_idx, group_end_idx);
  129. auto out_slice = out.slice(1, group_start_idx, group_end_idx);
  130. at::mm_out(out_slice, mat_a[group_idx], mat_b_slice);
  131. group_start_idx = group_end_idx;
  132. }
  133. } else if (a_is_2d && b_is_2d) {
  134. // 2d x 2d with offsets
  135. int group_start_idx = 0;
  136. auto offs_cpu = offs.value().cpu();
  137. for (int group_idx = 0; group_idx < offs_cpu.size(0); group_idx++) {
  138. int group_end_idx = offs_cpu[group_idx].item<int>();
  139. auto mat_a_slice = mat_a.slice(1, group_start_idx, group_end_idx);
  140. auto mat_b_slice = mat_b.slice(0, group_start_idx, group_end_idx);
  141. auto out_slice = out[group_idx];
  142. at::mm_out(out_slice, mat_a_slice, mat_b_slice);
  143. group_start_idx = group_end_idx;
  144. }
  145. } else {
  146. // 3d x 3d without offsets - regular bmm
  147. at::bmm_out(out, mat_a, mat_b);
  148. }
  149. }
  150. } // namespace at::native