-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hf_modelzoo] Adds import rust model from Huggingface (#3125)
- Loading branch information
Showing
4 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ transformers | |
torch | ||
protobuf==3.20.2 | ||
optimum[exporters,onnxruntime] | ||
safetensors |
101 changes: 101 additions & 0 deletions
101
extensions/tokenizers/src/main/python/safetensors_convert.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file | ||
# except in compliance with the License. A copy of the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" | ||
# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for | ||
# the specific language governing permissions and limitations under the License. | ||
import os | ||
from collections import defaultdict | ||
from typing import List, Dict | ||
|
||
import torch | ||
from safetensors.torch import _find_shared_tensors, _is_complete, load_file, save_file | ||
|
||
|
||
def _remove_duplicate_names( | ||
state_dict: Dict[str, torch.Tensor], | ||
*, | ||
preferred_names: List[str] = None, | ||
discard_names: List[str] = None, | ||
) -> Dict[str, List[str]]: | ||
if preferred_names is None: | ||
preferred_names = [] | ||
preferred_names = set(preferred_names) | ||
if discard_names is None: | ||
discard_names = [] | ||
discard_names = set(discard_names) | ||
|
||
shareds = _find_shared_tensors(state_dict) | ||
to_remove = defaultdict(list) | ||
for shared in shareds: | ||
complete_names = set( | ||
[name for name in shared if _is_complete(state_dict[name])]) | ||
if not complete_names: | ||
if len(shared) == 1: | ||
# Force contiguous | ||
name = list(shared)[0] | ||
state_dict[name] = state_dict[name].clone() | ||
complete_names = {name} | ||
else: | ||
raise RuntimeError( | ||
f"Error while trying to find names to remove to save state dict, but found no suitable name to keep for saving amongst: {shared}. None is covering the entire storage.Refusing to save/load the model since you could be storing much more memory than needed. Please refer to https://huggingface.co/docs/safetensors/torch_shared_tensors for more information. Or open an issue." | ||
) | ||
|
||
keep_name = sorted(list(complete_names))[0] | ||
|
||
preferred = complete_names.difference(discard_names) | ||
if preferred: | ||
keep_name = sorted(list(preferred))[0] | ||
|
||
if preferred_names: | ||
preferred = preferred_names.intersection(complete_names) | ||
if preferred: | ||
keep_name = sorted(list(preferred))[0] | ||
for name in sorted(shared): | ||
if name != keep_name: | ||
to_remove[keep_name].append(name) | ||
return to_remove | ||
|
||
|
||
def convert_file(pt_filename: str, sf_filename: str): | ||
loaded = torch.load(pt_filename, map_location="cpu") | ||
if "state_dict" in loaded: | ||
loaded = loaded["state_dict"] | ||
to_removes = _remove_duplicate_names(loaded) | ||
|
||
metadata = {"format": "pt"} | ||
for kept_name, to_remove_group in to_removes.items(): | ||
for to_remove in to_remove_group: | ||
if to_remove not in metadata: | ||
metadata[to_remove] = kept_name | ||
del loaded[to_remove] | ||
# Force tensors to be contiguous | ||
loaded = {k: v.contiguous() for k, v in loaded.items()} | ||
|
||
dir_name = os.path.dirname(sf_filename) | ||
os.makedirs(dir_name, exist_ok=True) | ||
save_file(loaded, sf_filename, metadata=metadata) | ||
check_file_size(sf_filename, pt_filename) | ||
reloaded = load_file(sf_filename) | ||
for k in loaded: | ||
pt_tensor = loaded[k] | ||
sf_tensor = reloaded[k] | ||
if not torch.equal(pt_tensor, sf_tensor): | ||
raise RuntimeError(f"The output tensors do not match for key {k}") | ||
|
||
|
||
def check_file_size(sf_filename: str, pt_filename: str): | ||
sf_size = os.stat(sf_filename).st_size | ||
pt_size = os.stat(pt_filename).st_size | ||
|
||
if (sf_size - pt_size) / pt_size > 0.01: | ||
raise RuntimeError(f"""The file size different is more than 1%: | ||
- {sf_filename}: {sf_size} | ||
- {pt_filename}: {pt_size} | ||
""") |