scaled_masked_softmax_cuda.cu 3.44 KB
Newer Older
Jared Casper's avatar
Jared Casper committed
1
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. */
2
3
4
5
6
7
8
9
10

#include <ATen/ATen.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cuda_profiler_api.h>
#include <ATen/cuda/CUDAContext.h>
#include <torch/extension.h>
#include "scaled_masked_softmax.h"
11
#include "type_shim.h"
12
13
14
15
16

namespace multihead_attn {
namespace fused_softmax {
namespace scaled_masked_softmax {

17
18
19
20
21
int get_batch_per_block_cuda(int query_seq_len, int key_seq_len, int batches, int attn_heads){
    return get_batch_per_block(query_seq_len, key_seq_len, batches, attn_heads);
}


22
23
24
25
26
27
28
29
30
torch::Tensor fwd_cuda(
    torch::Tensor const& input,
    torch::Tensor const& mask,
    float scale_factor)
{
  // input is a 4d tensor with dimensions [batches, attn_heads, seq_len, seq_len]
  const int batches = input.size(0);
  const int pad_batches = mask.size(0);
  const int attn_heads = input.size(1);
31
32
  const int query_seq_len = input.size(2);
  const int key_seq_len = input.size(3);
33
  TORCH_INTERNAL_ASSERT(key_seq_len <= 4096);
34
  TORCH_INTERNAL_ASSERT(query_seq_len > 1);
35
36
  TORCH_INTERNAL_ASSERT(pad_batches == 1 || pad_batches == batches);
  TORCH_INTERNAL_ASSERT(mask.size(1) == 1);
37
38
  TORCH_INTERNAL_ASSERT(mask.size(2) == query_seq_len);
  TORCH_INTERNAL_ASSERT(mask.size(3) == key_seq_len);
39
40
41
42

  // Output 
  auto act_options = input.options().requires_grad(false);
  torch::Tensor softmax_results = 
43
      torch::empty({batches, attn_heads, query_seq_len, key_seq_len}, act_options);
44
45
46
47
48
49

  // Softmax Intermediate Result Ptr
  void* input_ptr = static_cast<void*>(input.data_ptr());
  void* mask_ptr = static_cast<void*>(mask.data_ptr());
  void* softmax_results_ptr = static_cast<void*>(softmax_results.data_ptr());

50
51
52
53
  DISPATCH_HALF_AND_BFLOAT(
      input.scalar_type(),
      "dispatch_scaled_masked_softmax_forward",
      dispatch_scaled_masked_softmax_forward<scalar_t, scalar_t, float>(
54
      reinterpret_cast<scalar_t*>(softmax_results_ptr),
55
56
57
58
59
60
61
62
63
	  reinterpret_cast<const scalar_t*>(input_ptr),
	  reinterpret_cast<const uint8_t*>(mask_ptr),
	  scale_factor,
	  query_seq_len,
	  key_seq_len,
	  batches,
	  attn_heads,
	  pad_batches);
      );
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  return softmax_results;
}

torch::Tensor bwd_cuda(
    torch::Tensor const& output_grads_, 
    torch::Tensor const& softmax_results_, 
    float scale_factor)  {
	
  auto output_grads = output_grads_.contiguous();
  auto softmax_results = softmax_results_.contiguous();

  //output grads is a 4d tensor with dimensions [batches, attn_heads, seq_len, seq_len]
  const int batches = output_grads.size(0);
  const int attn_heads = output_grads.size(1);
78
79
  const int query_seq_len = output_grads.size(2);
  const int key_seq_len = output_grads.size(3);
80

81
82
83
84
  auto act_options = output_grads.options().requires_grad(false);
  torch::Tensor input_grads = 
            torch::empty({batches, attn_heads, query_seq_len, key_seq_len}, act_options);  

85
  void* output_grads_ptr = static_cast<void*>(output_grads.data_ptr());
86
  void* input_grads_ptr = static_cast<void*>(input_grads.data_ptr());
87
88

  //Softmax Grad
89
90
91
92
  DISPATCH_HALF_AND_BFLOAT(
      output_grads_.scalar_type(),
      "dispatch_scaled_masked_softmax_backward",
      dispatch_scaled_masked_softmax_backward<scalar_t, scalar_t, float>(
93
      reinterpret_cast<scalar_t*>(input_grads_ptr), 
94
95
96
97
98
99
100
	  reinterpret_cast<scalar_t*>(output_grads_ptr), 
	  reinterpret_cast<scalar_t const*>(softmax_results.data_ptr()),
	  scale_factor,
	  query_seq_len,
	  key_seq_len,
	  batches,
	  attn_heads);
101
      );
102
  
103
  return input_grads;
104
105
106
107
}
}
}
}