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
chenpangpang
transformers
Commits
81fe8afa
"...resnet50_tensorflow.git" did not exist on "9b57f41ce21cd7264c52140c9ab31cdfc5169fcd"
Unverified
Commit
81fe8afa
authored
Nov 19, 2021
by
Nicolas Patry
Committed by
GitHub
Nov 19, 2021
Browse files
Adding support for `hidden_states` and `attentions` in unbatching (#14420)
support.
parent
f25a9332
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
3 deletions
+22
-3
src/transformers/pipelines/base.py
src/transformers/pipelines/base.py
+8
-3
tests/test_pipelines_common.py
tests/test_pipelines_common.py
+14
-0
No files found.
src/transformers/pipelines/base.py
View file @
81fe8afa
...
...
@@ -747,9 +747,14 @@ if is_torch_available():
else
:
loader_batched
=
{}
for
k
,
element
in
self
.
_loader_batch_data
.
items
():
if
k
==
"past_key_values"
:
continue
if
isinstance
(
element
[
self
.
_loader_batch_index
],
torch
.
Tensor
):
if
k
in
{
"hidden_states"
,
"past_key_values"
,
"attentions"
}
and
isinstance
(
element
,
tuple
):
if
isinstance
(
element
[
0
],
torch
.
Tensor
):
loader_batched
[
k
]
=
tuple
(
el
[
self
.
_loader_batch_index
].
unsqueeze
(
0
)
for
el
in
element
)
elif
isinstance
(
element
[
0
],
np
.
ndarray
):
loader_batched
[
k
]
=
tuple
(
np
.
expand_dims
(
el
[
self
.
_loader_batch_index
],
0
)
for
el
in
element
)
elif
isinstance
(
element
[
self
.
_loader_batch_index
],
torch
.
Tensor
):
loader_batched
[
k
]
=
element
[
self
.
_loader_batch_index
].
unsqueeze
(
0
)
elif
isinstance
(
element
[
self
.
_loader_batch_index
],
np
.
ndarray
):
loader_batched
[
k
]
=
np
.
expand_dims
(
element
[
self
.
_loader_batch_index
],
0
)
...
...
tests/test_pipelines_common.py
View file @
81fe8afa
...
...
@@ -27,6 +27,7 @@ from transformers import (
TOKENIZER_MAPPING
,
AutoFeatureExtractor
,
AutoTokenizer
,
DistilBertForSequenceClassification
,
IBertConfig
,
RobertaConfig
,
TextClassificationPipeline
,
...
...
@@ -322,6 +323,19 @@ class CommonPipelineTest(unittest.TestCase):
results
.
append
(
out
)
self
.
assertEqual
(
len
(
results
),
10
)
@
require_torch
def
test_unbatch_attentions_hidden_states
(
self
):
model
=
DistilBertForSequenceClassification
.
from_pretrained
(
"Narsil/tiny-distilbert-sequence-classification"
,
output_hidden_states
=
True
,
output_attentions
=
True
)
tokenizer
=
AutoTokenizer
.
from_pretrained
(
"Narsil/tiny-distilbert-sequence-classification"
)
text_classifier
=
TextClassificationPipeline
(
model
=
model
,
tokenizer
=
tokenizer
)
# Used to throw an error because `hidden_states` are a tuple of tensors
# instead of the expected tensor.
outputs
=
text_classifier
([
"This is great !"
]
*
20
,
batch_size
=
32
)
self
.
assertEqual
(
len
(
outputs
),
20
)
@
is_pipeline_test
class
PipelinePadTest
(
unittest
.
TestCase
):
...
...
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