Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
flash-attention
Commits
d886f881
Unverified
Commit
d886f881
authored
Nov 28, 2024
by
Woosuk Kwon
Committed by
GitHub
Nov 28, 2024
Browse files
Remove redundant code in `varlen_fwd` (#28)
Signed-off-by:
Woosuk Kwon
<
woosuk.kwon@berkeley.edu
>
parent
5259c586
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
18 deletions
+22
-18
csrc/flash_attn/flash_api.cpp
csrc/flash_attn/flash_api.cpp
+18
-16
vllm_flash_attn/flash_attn_interface.py
vllm_flash_attn/flash_attn_interface.py
+4
-2
No files found.
csrc/flash_attn/flash_api.cpp
View file @
d886f881
...
...
@@ -659,22 +659,23 @@ mha_varlen_fwd(at::Tensor &q, // total_q x num_heads x head_size, total_q := \s
p_dropout
,
/*num_splits*/
0
,
dprops
,
opts
);
}
// NOTE(woosuk): Commented out because they are not used in inference.
// number of times random will be generated per thread, to offset philox counter in thc random
// state
// We use a custom RNG that increases the offset by batch_size * nheads * 32.
int64_t
counter_offset
=
params
.
b
*
params
.
h
*
32
;
auto
options
=
torch
::
TensorOptions
().
dtype
(
torch
::
kFloat32
).
device
(
torch
::
kCUDA
);
auto
rng_state
=
torch
::
empty
({
2
},
options
.
dtype
(
torch
::
kInt64
));
// Forward kernel will populate memory with the seed and offset.
params
.
rng_state
=
reinterpret_cast
<
uint64_t
*>
(
rng_state
.
data_ptr
());
if
(
p_dropout
>
0.0
)
{
auto
gen
=
at
::
get_generator_or_default
<
at
::
CUDAGeneratorImpl
>
(
gen_
,
at
::
cuda
::
detail
::
getDefaultCUDAGenerator
());
// See Note [Acquire lock when using random generators]
std
::
lock_guard
<
std
::
mutex
>
lock
(
gen
->
mutex_
);
params
.
philox_args
=
gen
->
philox_cuda_state
(
counter_offset
);
}
//
int64_t counter_offset = params.b * params.h * 32;
//
auto options = torch::TensorOptions().dtype(torch::kFloat32).device(torch::kCUDA);
//
auto rng_state = torch::empty({2}, options.dtype(torch::kInt64));
//
//
Forward kernel will populate memory with the seed and offset.
//
params.rng_state = reinterpret_cast<uint64_t*>(rng_state.data_ptr());
//
if (p_dropout > 0.0) {
//
auto gen = at::get_generator_or_default<at::CUDAGeneratorImpl>(
//
gen_, at::cuda::detail::getDefaultCUDAGenerator());
//
// See Note [Acquire lock when using random generators]
//
std::lock_guard<std::mutex> lock(gen->mutex_);
//
params.philox_args = gen->philox_cuda_state(counter_offset);
//
}
set_params_alibi
(
params
,
alibi_slopes_
,
batch_size
,
num_heads
);
...
...
@@ -697,12 +698,13 @@ mha_varlen_fwd(at::Tensor &q, // total_q x num_heads x head_size, total_q := \s
int64_t
size_before
[]
=
{
batch_size
,
max_seqlen_q
,
num_heads_k
,
head_size_og
};
int64_t
size_after
[]
=
{
batch_size
,
num_heads_k
*
max_seqlen_q
,
head_size_og
};
out
=
out
.
reshape
(
size_before
).
transpose
(
1
,
2
).
reshape
(
size_after
);
out_padded
=
out_padded
.
reshape
(
size_before
).
transpose
(
1
,
2
).
reshape
(
size_after
);
q_padded
=
q_padded
.
reshape
(
size_before
).
transpose
(
1
,
2
).
reshape
(
size_after
);
// NOTE(woosuk): The two lines are not necessary because out_padded and q_padded are not used.
// out_padded = out_padded.reshape(size_before).transpose(1, 2).reshape(size_after);
// q_padded = q_padded.reshape(size_before).transpose(1, 2).reshape(size_after);
softmax_lse
=
softmax_lse
.
reshape
({
num_heads
*
max_seqlen_q
,
batch_size
});
}
return
{
out
,
q_padded
,
k_padded
,
v_padded
,
out_padded
,
softmax_lse
,
p
,
rng_stat
e
};
return
{
out
,
softmax_ls
e
};
}
std
::
vector
<
at
::
Tensor
>
...
...
vllm_flash_attn/flash_attn_interface.py
View file @
d886f881
...
...
@@ -88,7 +88,7 @@ def _flash_attn_varlen_forward(
out
=
None
):
q
,
k
,
v
=
[
maybe_contiguous
(
x
)
for
x
in
(
q
,
k
,
v
)]
out
,
q
,
k
,
v
,
out_padded
,
softmax_lse
,
S_dmask
,
rng_stat
e
=
torch
.
ops
.
vllm_flash_attn_c
.
varlen_fwd
(
out
,
softmax_ls
e
=
torch
.
ops
.
vllm_flash_attn_c
.
varlen_fwd
(
q
,
k
,
v
,
...
...
@@ -112,7 +112,9 @@ def _flash_attn_varlen_forward(
)
# if out.isnan().any() or softmax_lse.isnan().any():
# breakpoint()
return
out
,
q
,
k
,
v
,
out_padded
,
softmax_lse
,
S_dmask
,
rng_state
# NOTE(woosuk): out_padded, S_dmask, and rng_state are None
# because we only use the forward pass in the vLLM.
return
out
,
q
,
k
,
v
,
None
,
softmax_lse
,
None
,
None
def
_flash_attn_backward
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment