Skip to content

model load performance tweak idea #799

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions exllamav2/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ def load_multi(self,

for v, ks in submap_i.items():
stfile = STFile.open(v, keymap = self.model.config.arch.keymap)
for k in ks:
if measure:
if measure:
for k in ks:
size += stfile.measure(key + "." + k)
else:
tensors[k] = stfile.get_tensor(key + "." + k, device = self.device() if not cpu else "cpu")
else:
loaded = stfile.get_tensors([key + "." + k for k in ks], device = self.device() if not cpu else "cpu")
for k, tensor in zip(ks, loaded.values()):
tensors[k] = tensor

return size if measure else tensors

Expand Down
46 changes: 45 additions & 1 deletion exllamav2/stloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,48 @@ def get_tensor(
)
if out_dtype:
tensor = tensor.to(out_dtype)
return tensor
return tensor

def get_tensors(
self,
keys: list,
device,
out_dtypes = None
) -> dict:
"""
Batch load multiple tensors from file.
:param keys:
List of tensor names
:param device:
Target device
:param out_dtypes:
Optional list of output dtypes (or None for each)
:return:
dict of {key: tensor}
"""
tensors = {}
if out_dtypes is None:
out_dtypes = [None] * len(keys)
for key, out_dtype in zip(keys, out_dtypes):
h = self.header[key]
dtype, esize = convert_dtype(h["dtype"])
beg, end = h["data_offsets"]
size = end - beg
shape = h["shape"]
tensor = torch.empty(shape, dtype = dtype, device = device)
torch.cuda.synchronize()
assert tensor.is_contiguous, "Non-contiguous tensor"
ext_c.stloader_read(
self.filename,
beg + self.header_size,
size,
tensor
)
if out_dtype:
tensor = tensor.to(out_dtype)
tensors[key] = tensor
return tensors