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
afce73bd
Unverified
Commit
afce73bd
authored
Nov 23, 2022
by
Sylvain Gugger
Committed by
GitHub
Nov 23, 2022
Browse files
Fix ModelOutput instantiation when there is only one tuple (#20416)
parent
993a187c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
1 deletion
+22
-1
src/transformers/utils/generic.py
src/transformers/utils/generic.py
+9
-1
tests/utils/test_model_output.py
tests/utils/test_model_output.py
+13
-0
No files found.
src/transformers/utils/generic.py
View file @
afce73bd
...
...
@@ -227,12 +227,20 @@ class ModelOutput(OrderedDict):
# if we provided an iterator as first field and the iterator is a (key, value) iterator
# set the associated fields
if
first_field_iterator
:
for
element
in
iterator
:
for
idx
,
element
in
enumerate
(
iterator
)
:
if
(
not
isinstance
(
element
,
(
list
,
tuple
))
or
not
len
(
element
)
==
2
or
not
isinstance
(
element
[
0
],
str
)
):
if
idx
==
0
:
# If we do not have an iterator of key/values, set it as attribute
self
[
class_fields
[
0
].
name
]
=
first_field
else
:
# If we have a mixed iterator, raise an error
raise
ValueError
(
f
"Cannot set key/value for
{
element
}
. It needs to be a tuple (key, value)."
)
break
setattr
(
self
,
element
[
0
],
element
[
1
])
if
element
[
1
]
is
not
None
:
...
...
tests/utils/test_model_output.py
View file @
afce73bd
...
...
@@ -107,3 +107,16 @@ class ModelOutputTester(unittest.TestCase):
self
.
assertEqual
(
list
(
x
.
keys
()),
[
"a"
,
"b"
])
self
.
assertEqual
(
x
.
a
,
30
)
self
.
assertEqual
(
x
.
b
,
10
)
def
test_instantiate_from_iterator
(
self
):
x
=
ModelOutputTest
([(
"a"
,
30
),
(
"b"
,
10
)])
self
.
assertEqual
(
list
(
x
.
keys
()),
[
"a"
,
"b"
])
self
.
assertEqual
(
x
.
a
,
30
)
self
.
assertEqual
(
x
.
b
,
10
)
with
self
.
assertRaises
(
ValueError
):
_
=
ModelOutputTest
([(
"a"
,
30
),
(
10
,
10
)])
x
=
ModelOutputTest
(
a
=
(
30
,
30
))
self
.
assertEqual
(
list
(
x
.
keys
()),
[
"a"
])
self
.
assertEqual
(
x
.
a
,
(
30
,
30
))
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