Skip to content

convert_hf : faster lazy safetensors - #8482

Merged
compilade merged 3 commits into
masterfrom
compilade/faster-lazy-safetensors
Jul 16, 2024
Merged

compilade merged 3 commits into
masterfrom
compilade/faster-lazy-safetensors

Conversation

@compilade

@compilade compilade commented Jul 14, 2024 •

Copy link
Copy Markdown
Collaborator

Currently, with Lazy conversion, a relatively big portion of the model files is read before even beginning to write the output file, and then if the disk cache it smaller than the model, it will be read from disk again when actually converting.

Most of the time in the initial read is spent on model_part.get_tensor(name) (at least when using safetensors).

Turns out safetensors has the much faster .get_slice(name) which doesn't read the tensor data before it's needed, while still giving access to the shape and dtype of each tensor.

As a nice result, this makes convert_hf_to_gguf.py --dry-run much, much faster than before for slow disks and/or big models (seconds instead of minutes). Normal lazy conversion is also faster, since the initial metadata reading step doesn't unnecessarily read all the data anymore.

Note that I've also removed some unused code in gguf-py/gguf/tensor_mapping.py related to the number of experts. xid does not exist in the mappings since stacked experts were implemented, so .format(xid = xid) does not do anything.

Testing

After fixing the problem found in #8482 (comment), I've ran some more tests.

-no-slices- means master at commit 97bdd26, while -slices-recurse- means after the memory leak was fixed in b971122.


@github-actions github-actions Bot added the python python script changes label Jul 14, 2024
@compilade compilade added the Review Complexity : Low Trivial changes to code that most beginner devs (or those who want a break) can tackle. e.g. UI fix label Jul 15, 2024
@compilade compilade added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Jul 15, 2024
@compilade

compilade commented Jul 15, 2024 •

Copy link
Copy Markdown
Collaborator Author

This seems to cause a memory usage regression when lazily converting MoE models. Looks a lot like a memory leak. I'll try to fix this before merging.

EDIT: So in my tests it's only happening with MoE models, so this means it's likely not a safetensors issue, although this is weird if it only affects torch.stack. I'll try to understand and report back.

EDIT2: Seems like it might be related to a reference cycle in LazyBase, I assume in the _lazy deque. Manually running gc.collect() "fixes" the problem. I wonder why this problem didn't manifest itself before? I think the real solution will be to remove that queue and use recursion for evaluation instead. That queue was used to avoid deep recursion, but it has unintuitive behaviors (with correct results) in some cases like when concatenating tensors, and I don't think there's deep enough conversion chains in any of the model subclasses for the recursion limit (1000 by default) to be a problem, especially since complex operations like quantization are abstracted away with meta_noop.

EDIT3: implemented the recursive solution in b971122.

@compilade compilade removed the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Jul 15, 2024
The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
@compilade compilade added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Jul 16, 2024
@compilade
compilade merged commit 7acfd4e into master Jul 16, 2024
arthw pushed a commit to arthw/llama.cpp that referenced this pull request Jul 27, 2024
* convert_hf : faster lazy safetensors

This makes '--dry-run' much, much faster.

* convert_hf : fix memory leak in lazy MoE conversion

The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* convert_hf : faster lazy safetensors

This makes '--dry-run' much, much faster.

* convert_hf : fix memory leak in lazy MoE conversion

The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* convert_hf : faster lazy safetensors

This makes '--dry-run' much, much faster.

* convert_hf : fix memory leak in lazy MoE conversion

The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* convert_hf : faster lazy safetensors

This makes '--dry-run' much, much faster.

* convert_hf : fix memory leak in lazy MoE conversion

The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* convert_hf : faster lazy safetensors

This makes '--dry-run' much, much faster.

* convert_hf : fix memory leak in lazy MoE conversion

The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* convert_hf : faster lazy safetensors

This makes '--dry-run' much, much faster.

* convert_hf : fix memory leak in lazy MoE conversion

The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* convert_hf : faster lazy safetensors

This makes '--dry-run' much, much faster.

* convert_hf : fix memory leak in lazy MoE conversion

The '_lazy' queue was sometimes self-referential,
which caused reference cycles of objects old enough
to avoid garbage collection until potential memory exhaustion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. python python script changes Review Complexity : Low Trivial changes to code that most beginner devs (or those who want a break) can tackle. e.g. UI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants