Skip to content

Commit

Permalink
[BugFix] fix supporting OrderedDict bug in paddle.jit module (#3364)
Browse files Browse the repository at this point in the history
* convert keys to `__dict__`

* use fields to get keys

Co-authored-by: Guo Sheng <[email protected]>
  • Loading branch information
wj-Mcat and guoshengCS authored Sep 29, 2022
1 parent 131750a commit 3b6b183
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions paddlenlp/transformers/model_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing import Optional, Tuple
from collections import OrderedDict
from dataclasses import fields, dataclass
from typing import Any, List, Tuple, Optional
from typing import Any, Tuple, Optional
from paddle.nn.layer.transformer import _convert_attention_mask, MultiHeadAttention
from paddle.distributed.fleet.utils import recompute

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

return tuples


@dataclass
Expand Down

0 comments on commit 3b6b183

Please sign in to comment.