Skip to content

[sharktank] Fix module patching and safetensors comparison tool #1506

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
May 28, 2025
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions sharktank/sharktank/tools/compare_safetensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def main(argv):
type=Path,
default=None,
help=(
"Path to JSON or YAML file that maps names of expected tensors to names of actual tensors."
"Path to JSON or YAML file that maps names of expected tensors to names of "
'actual tensors. E.g. {"a": "b", "c": "d"}'
),
)
parser.add_argument(
Expand Down Expand Up @@ -179,7 +180,9 @@ def main(argv):
act_name = expected_actual_to_key_map[name]
if act_name not in act_keys:
continue
reporter.compare(name, exp_f.get_tensor(name), act_f.get_tensor(act_name))
reporter.compare(
name, exp_f.get_tensor(name), act_f.get_tensor(act_name)
)
finally:
reporter.close()

Expand Down
11 changes: 6 additions & 5 deletions sharktank/sharktank/utils/patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from typing import Any
from collections.abc import Mapping, Iterable
from sharktank.types import InferenceTensor, unbox_tensor
import logging
import torch

Expand Down Expand Up @@ -124,6 +125,7 @@ def save_file(self, output_path: Path, *, skip_unsupported_dtypes: bool = False)
for k, v in tensor_dict.items()
if k not in unsupported_tensor_dict.keys()
}

save_file(tensor_dict, output_path)

def _add_nested_tensors(
Expand All @@ -132,16 +134,16 @@ def _add_nested_tensors(
tensors: list[Any] | dict[str, Any] | torch.Tensor,
name_delimiter: str,
):
if isinstance(tensors, torch.Tensor):
self._add_tensor(name=name_prefix, tensor=tensors)
if isinstance(tensors, (torch.Tensor, InferenceTensor)):
self._add_tensor(name=name_prefix, tensor=unbox_tensor(tensors))
elif isinstance(tensors, Mapping):
for k, v in tensors.items():
self._add_nested_tensor(
self._add_nested_tensors(
f"{name_prefix}{name_delimiter}{k}", v, name_delimiter
)
elif isinstance(tensors, Iterable):
for i, v in enumerate(tensors):
self._add_nested_tensor(
self._add_nested_tensors(
f"{name_prefix}{name_delimiter}{i}", v, name_delimiter
)
else:
Expand All @@ -154,7 +156,6 @@ def _add_tensor(self, name: str, tensor: torch.Tensor):
del self.tensors[name]
self.duplicate_tensors[name] = 0
self.tensors[f"{name}#0"] = orig_dup

if name in self.duplicate_tensors:
index = self.duplicate_tensors[name] + 1
self.duplicate_tensors[name] = index
Expand Down
Loading