Skip to content

Commit

Permalink
优化 PartialProgramLayer hash逻辑 (#49752)
Browse files Browse the repository at this point in the history
* 优化 PartialProgramLayer hash逻辑

* 修复list弱引用问题

* 递归计算list列表的id

* 处理str类型

* str类型直接返回hash后的值
  • Loading branch information
Skylark-hjyp authored Feb 4, 2023
1 parent fb69204 commit 3e5a6df
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions python/paddle/fluid/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from sys import version_info

from collections.abc import Sequence
from weakref import WeakKeyDictionary
from collections import defaultdict
from uuid import uuid4


def convert_to_list(value, n, name, dtype=int):
Expand Down Expand Up @@ -107,13 +110,33 @@ def is_sequence(seq):
return isinstance(seq, Sequence) and not isinstance(seq, str)


class UniqueIdMap(WeakKeyDictionary):
def __init__(self):
super().__init__(self)
self.data = defaultdict(uuid4)


uniqueidmap = UniqueIdMap()


def uniqueid(obj):
if isinstance(obj, str):
return (hash(obj),)
elif isinstance(obj, list):
return (id(obj),)
else:
return (uniqueidmap[obj].int,)


def _hash_with_id(*args):
"""
Return int hash value calculated by id(arg) or tuple(id1,id2, ...).
"""
assert len(args) > 0
info = tuple([id(v) for v in args])
return hash(info) & 0xFFFFFFF
info = ()
for v in args:
info = info + uniqueid(v)
return hash(info)


def _sorted(dict_):
Expand Down

0 comments on commit 3e5a6df

Please sign in to comment.