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
847abe65
Commit
847abe65
authored
Aug 26, 2023
by
Tri Dao
Browse files
[Gen] Refactor decode function a bit
parent
371e2065
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
18 deletions
+23
-18
flash_attn/utils/generation.py
flash_attn/utils/generation.py
+23
-18
No files found.
flash_attn/utils/generation.py
View file @
847abe65
...
...
@@ -124,6 +124,24 @@ def decode(
inference_params
=
InferenceParams
(
max_sequence_len
=
max_length
,
max_batch_size
=
batch_size
,
fused_ft_kernel
=
fused_ft_kernel
)
def
logits_forward_fn
(
input_ids
,
position_ids
,
inference_params
):
if
not
cg
:
return
model
(
input_ids
,
position_ids
=
position_ids
,
inference_params
=
inference_params
,
last_token_only
=
True
,
).
logits
else
:
return
model
.
_decoding_cache
.
run
(
input_ids
,
position_ids
,
inference_params
.
sequence_len_offset
).
clone
()
logits_postprocess_fn
=
(
lambda
logits
:
logits
[...,
:
vocab_size
]
if
vocab_size
is
not
None
else
logits
)
scores
=
[]
with
torch
.
inference_mode
():
if
timing
:
...
...
@@ -132,8 +150,7 @@ def decode(
torch
.
cuda
.
synchronize
()
start
=
time
.
time
()
logits
=
model
(
input_ids
,
inference_params
=
inference_params
,
last_token_only
=
True
).
logits
if
vocab_size
is
not
None
:
logits
=
logits
[...,
:
vocab_size
]
logits
=
logits_postprocess_fn
(
logits
)
scores
.
append
(
logits
if
not
cg
else
logits
.
clone
())
if
teacher_outputs
is
None
or
teacher_output_len
<=
seqlen_og
:
next_token
=
sample
(
logits
,
top_k
=
top_k
,
top_p
=
top_p
,
temperature
=
temperature
)
...
...
@@ -148,22 +165,10 @@ def decode(
dtype
=
torch
.
long
,
device
=
input_ids
.
device
,
)
if
not
cg
:
logits
=
model
(
rearrange
(
next_token
,
"b -> b 1"
),
position_ids
=
position_ids
,
inference_params
=
inference_params
,
last_token_only
=
True
,
).
logits
else
:
logits
=
model
.
_decoding_cache
.
run
(
rearrange
(
next_token
,
"b -> b 1"
),
position_ids
,
inference_params
.
sequence_len_offset
,
)
if
vocab_size
is
not
None
:
logits
=
logits
[...,
:
vocab_size
]
scores
.
append
(
logits
if
not
cg
else
logits
.
clone
())
logits
=
logits_postprocess_fn
(
logits_forward_fn
(
rearrange
(
next_token
,
"b -> b 1"
),
position_ids
,
inference_params
))
scores
.
append
(
logits
)
if
(
teacher_outputs
is
None
or
teacher_output_len
<=
inference_params
.
sequence_len_offset
+
1
...
...
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