Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
10056d89
Unverified
Commit
10056d89
authored
Feb 06, 2023
by
Joao Gante
Committed by
GitHub
Feb 06, 2023
Browse files
OPT: BLIP2-ready `prepare_inputs_for_generation` (#21477)
parent
baf4bacb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
12 deletions
+16
-12
src/transformers/models/opt/modeling_opt.py
src/transformers/models/opt/modeling_opt.py
+16
-12
No files found.
src/transformers/models/opt/modeling_opt.py
View file @
10056d89
...
@@ -965,21 +965,25 @@ class OPTForCausalLM(OPTPreTrainedModel):
...
@@ -965,21 +965,25 @@ class OPTForCausalLM(OPTPreTrainedModel):
)
)
def
prepare_inputs_for_generation
(
def
prepare_inputs_for_generation
(
self
,
input_ids
,
past_key_values
=
None
,
attention_mask
=
None
,
use_cache
=
None
,
**
kwargs
self
,
input_ids
,
past_key_values
=
None
,
attention_mask
=
None
,
inputs_embeds
=
None
,
**
kwargs
):
):
# if model is used as a decoder in encoder-decoder model, the decoder attention mask is created on the fly
if
attention_mask
is
None
:
attention_mask
=
input_ids
.
new_ones
(
input_ids
.
shape
)
if
past_key_values
:
if
past_key_values
:
input_ids
=
input_ids
[:,
-
1
:]
input_ids
=
input_ids
[:,
-
1
:]
# first step, decoder_cached_states are empty
return
{
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
"input_ids"
:
input_ids
,
# encoder_outputs is defined. input_ids not needed
if
inputs_embeds
is
not
None
and
past_key_values
is
None
:
"attention_mask"
:
attention_mask
,
model_inputs
=
{
"inputs_embeds"
:
inputs_embeds
}
"past_key_values"
:
past_key_values
,
else
:
"use_cache"
:
use_cache
,
model_inputs
=
{
"input_ids"
:
input_ids
}
}
model_inputs
.
update
(
{
"past_key_values"
:
past_key_values
,
"use_cache"
:
kwargs
.
get
(
"use_cache"
),
"attention_mask"
:
attention_mask
,
}
)
return
model_inputs
@
staticmethod
@
staticmethod
def
_reorder_cache
(
past
,
beam_idx
):
def
_reorder_cache
(
past
,
beam_idx
):
...
...
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