Skip to content

Fix load_torch #4383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions paddlenlp/utils/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def find_class(self, mod_name, name):
return _rebuild_tensor_stage

# pytorch_lightning tensor builder
if mod_name == "pytorch_lightning":
if "pytorch_lightning" in mod_name:
return dumpy
return super().find_class(mod_name, name)

Expand Down Expand Up @@ -219,23 +219,19 @@ def extract_maybe_dict(result):
for res in result:
extract_maybe_dict(res)
elif isinstance(result, TensorMeta):
metadata.append(result)
if result not in metadata:
metadata.append(result)

extract_maybe_dict(result_stage1)
metadata = sorted(metadata, key=lambda x: x.key)
# 3. parse the tensor of pytorch weight file
stage1_key_to_tensor = {}
content_size = os.stat(path).st_size
with open(path, "rb") as file_handler:
prefix_key = read_prefix_key(file_handler, content_size).decode("latin")
file_handler.seek(pre_offset)

for tensor_meta in metadata:
key = tensor_meta.key
# eg: archive/data/1FB
filename = f"{prefix_key}/data/{key}"
seek_by_string(file_handler, filename, content_size)
file_handler.seek(2, 1)
seek_by_string(file_handler, "FB", content_size)

padding_offset = np.frombuffer(file_handler.read(2)[:1], dtype=np.uint8)[0]
file_handler.seek(padding_offset, 1)
Expand Down