cyd0806 commited on
Commit
59b4df7
·
verified ·
1 Parent(s): d799d96

Upload apex-master/csrc/megatron/scaled_upper_triang_masked_softmax_cuda.cu with huggingface_hub

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