Skip to content

Commit 3b6b183

Browse files
wj-McatguoshengCS
andauthored
[BugFix] fix supporting OrderedDict bug in paddle.jit module (#3364)
* convert keys to `__dict__` * use fields to get keys Co-authored-by: Guo Sheng <[email protected]>
1 parent 131750a commit 3b6b183

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

paddlenlp/transformers/model_outputs.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import Optional, Tuple
2121
from collections import OrderedDict
2222
from dataclasses import fields, dataclass
23-
from typing import Any, List, Tuple, Optional
23+
from typing import Any, Tuple, Optional
2424
from paddle.nn.layer.transformer import _convert_attention_mask, MultiHeadAttention
2525
from paddle.distributed.fleet.utils import recompute
2626

@@ -357,7 +357,16 @@ def to_tuple(self) -> Tuple[Any]:
357357
"""
358358
Convert self to a tuple containing all the attributes/keys that are not `None`.
359359
"""
360-
return tuple(self[k] for k in self.keys())
360+
# try to fix: https://github.com/PaddlePaddle/PaddleNLP/issues/3355
361+
# when trying to get the keys of `OrderedDict`, `keys` method return empty values.
362+
# TODO(wj-Mcat): this bug should be fixed in Paddle framework
363+
tuples = ()
364+
for field in fields(self):
365+
if getattr(self, field.name, None) is None:
366+
continue
367+
tuples = tuples + (getattr(self, field.name), )
368+
369+
return tuples
361370

362371

363372
@dataclass

0 commit comments

Comments
 (0)