Skip to content

How to dequantize a specific layer from gguf into torch tensor? #12467

Answered by compilade
SweetGUOguo asked this question in Q&A
Discussion options

You must be logged in to vote

I would like to ask, if I want to dequantize a specific layer of a GGUF model and save it as a PyTorch tensor, how should I proceed?

You can use the gguf-py library, which has dequantization support for most of the quantization types.

$ pip install gguf

(Assuming you also have PyTorch in your environment)

import gguf
import torch

reader: gguf.GGUFReader = gguf.GGUFReader("path/to/model.gguf")
# Assuming you know which tensor it is
index: int = 3
tensor: gguf.ReaderTensor = reader.get_tensor(index)
torch_tensor = torch.from_numpy(gguf.dequantize(tensor.data, tensor.tensor_type))

I did not test this, but hopefully this is close enough to what is needed.

Hmm, I think this should be made m…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
2 replies
@SweetGUOguo
Comment options

@ejrydhfs
Comment options

Comment options

You must be logged in to vote
2 replies
@SweetGUOguo
Comment options

@SweetGUOguo
Comment options

Answer selected by SweetGUOguo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants