generic_scaled_masked_softmax_cuda.cu 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* coding=utf-8
  2. * Copyright (c) 2022, 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. #include <ATen/ATen.h>
  17. #include <cuda.h>
  18. #include <cuda_runtime.h>
  19. #include <cuda_fp16.h>
  20. #include <cuda_profiler_api.h>
  21. #include <ATen/cuda/CUDAContext.h>
  22. #include <torch/extension.h>
  23. #include "generic_scaled_masked_softmax.h"
  24. #include "type_shim.h"
  25. namespace multihead_attn {
  26. namespace fused_softmax {
  27. namespace generic_scaled_masked_softmax {
  28. torch::Tensor fwd_cuda(
  29. torch::Tensor const& input,
  30. torch::Tensor const& mask,
  31. float scale_factor)
  32. {
  33. // input is a 4d tensor with dimensions [batches, attn_heads, seq_len, seq_len]
  34. const int batches = input.size(0);
  35. const int pad_batches = mask.size(0);
  36. const int attn_heads = input.size(1);
  37. const int query_seq_len = input.size(2);
  38. const int key_seq_len = input.size(3);
  39. TORCH_INTERNAL_ASSERT(pad_batches == 1 || pad_batches == batches);
  40. TORCH_INTERNAL_ASSERT(mask.size(1) == 1);
  41. TORCH_INTERNAL_ASSERT(mask.size(2) == query_seq_len);
  42. TORCH_INTERNAL_ASSERT(mask.size(3) == key_seq_len);
  43. // Output
  44. auto act_options = input.options().requires_grad(false);
  45. torch::Tensor softmax_results =
  46. torch::empty({batches, attn_heads, query_seq_len, key_seq_len}, act_options);
  47. // Softmax Intermediate Result Ptr
  48. void* input_ptr = static_cast<void*>(input.data_ptr());
  49. void* mask_ptr = static_cast<void*>(mask.data_ptr());
  50. void* softmax_results_ptr = static_cast<void*>(softmax_results.data_ptr());
  51. DISPATCH_HALF_AND_BFLOAT(
  52. input.scalar_type(),
  53. "dispatch_scaled_masked_softmax_forward",
  54. dispatch_scaled_masked_softmax_forward_new<scalar_t, scalar_t, float>(
  55. reinterpret_cast<scalar_t*>(softmax_results_ptr),
  56. reinterpret_cast<const scalar_t*>(input_ptr),
  57. reinterpret_cast<const uint8_t*>(mask_ptr),
  58. scale_factor,
  59. query_seq_len,
  60. key_seq_len,
  61. batches,
  62. attn_heads,
  63. pad_batches);
  64. );
  65. return softmax_results;
  66. }
  67. torch::Tensor bwd_cuda(
  68. torch::Tensor const& output_grads_,
  69. torch::Tensor const& softmax_results_,
  70. float scale_factor) {
  71. auto output_grads = output_grads_.contiguous();
  72. auto softmax_results = softmax_results_.contiguous();
  73. //output grads is a 4d tensor with dimensions [batches, attn_heads, seq_len, seq_len]
  74. const int batches = output_grads.size(0);
  75. const int attn_heads = output_grads.size(1);
  76. const int query_seq_len = output_grads.size(2);
  77. const int key_seq_len = output_grads.size(3);
  78. auto act_options = output_grads.options();
  79. torch::Tensor input_grad =
  80. torch::empty({batches, attn_heads, query_seq_len, key_seq_len}, act_options);
  81. void* output_grads_ptr = static_cast<void*>(output_grads.data_ptr());
  82. //Softmax Grad
  83. DISPATCH_HALF_AND_BFLOAT(
  84. output_grads_.scalar_type(),
  85. "dispatch_scaled_masked_softmax_backward",
  86. dispatch_scaled_masked_softmax_backward_new<scalar_t, scalar_t, float>(
  87. reinterpret_cast<scalar_t*>(static_cast<void*>(input_grad.data_ptr())),
  88. reinterpret_cast<scalar_t*>(output_grads_ptr),
  89. reinterpret_cast<scalar_t const*>(softmax_results.data_ptr()),
  90. scale_factor,
  91. query_seq_len,
  92. key_seq_len,
  93. batches,
  94. attn_heads);
  95. );
  96. //backward pass is completely in-place
  97. return input_grad;
  98. }
  99. }
  100. }
  101. }