3
0

scaled_softmax_cuda.cu 3.2 KB

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