Skip to content

Commit 9f25f47

Browse files
compiladetimwu
authored andcommitted
gguf-py : support lazy tensor splitting (ggml-org#12809)
* gguf-py : support lazy tensor splitting Splitting usually involves returning tuples of tensors, which need to be handled properly to avoid early eager evaluation. * gguf-py : fix flake8 lint
1 parent 2de93fc commit 9f25f47

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

gguf-py/gguf/lazy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ def wrapped_fn(*args, **kwargs):
139139

140140
if isinstance(res, cls._tensor_type):
141141
return cls(meta=cls.eager_to_meta(res), args=args, kwargs=kwargs, func=fn)
142+
elif isinstance(res, tuple) and all(isinstance(t, cls._tensor_type) for t in res):
143+
# share the evaluation between lazy tuple elements
144+
shared_args: list = [args, None]
145+
146+
def eager_tuple_element(a: list[Any], i: int = 0, /, **kw) -> LazyBase:
147+
assert len(a) == 2
148+
if a[1] is None:
149+
a[1] = fn(*a[0], **kw)
150+
return a[1][i]
151+
return tuple(cls(meta=cls.eager_to_meta(res[i]), args=(shared_args, i), kwargs=kwargs, func=eager_tuple_element) for i in range(len(res)))
142152
else:
143153
del res # not needed
144154
# non-tensor return likely relies on the contents of the args

0 commit comments

Comments
 (0)