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
0454e4bd
Unverified
Commit
0454e4bd
authored
Aug 10, 2021
by
Sylvain Gugger
Committed by
GitHub
Aug 10, 2021
Browse files
Fix ModelOutput instantiation form dictionaries (#13067)
* Fix ModelOutput instantiation form dictionaries * Style
parent
3157fa3c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
4 deletions
+14
-4
src/transformers/file_utils.py
src/transformers/file_utils.py
+8
-4
tests/test_model_output.py
tests/test_model_output.py
+6
-0
No files found.
src/transformers/file_utils.py
View file @
0454e4bd
...
...
@@ -1850,6 +1850,10 @@ class ModelOutput(OrderedDict):
other_fields_are_none
=
all
(
getattr
(
self
,
field
.
name
)
is
None
for
field
in
class_fields
[
1
:])
if
other_fields_are_none
and
not
is_tensor
(
first_field
):
if
isinstance
(
first_field
,
dict
):
iterator
=
first_field
.
items
()
first_field_iterator
=
True
else
:
try
:
iterator
=
iter
(
first_field
)
first_field_iterator
=
True
...
...
tests/test_model_output.py
View file @
0454e4bd
...
...
@@ -101,3 +101,9 @@ class ModelOutputTester(unittest.TestCase):
x
[
"a"
]
=
10
self
.
assertEqual
(
x
.
a
,
10
)
self
.
assertEqual
(
x
[
"a"
],
10
)
def
test_instantiate_from_dict
(
self
):
x
=
ModelOutputTest
({
"a"
:
30
,
"b"
:
10
})
self
.
assertEqual
(
list
(
x
.
keys
()),
[
"a"
,
"b"
])
self
.
assertEqual
(
x
.
a
,
30
)
self
.
assertEqual
(
x
.
b
,
10
)
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