Add gguf support for bloom
Author: VladOS95-cyber
Pull request: yes

# What does this PR do?
Add Bloom GGUF loading support
<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case. Link: https://github.com/huggingface/transformers/issues/33260
- [x] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [x] Did you write any new necessary tests?


## Who can review?
Regarding the task @SunMarc @LysandreJik @ArthurZucker .

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @ylacombe, @eustlb
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
Hi @SunMarc @LysandreJik @ArthurZucker! This PR is ready for review. There is one thing that looks odd to me. After dequantization and loading the model, It generates a wrong sequence, not as expected when using a normal pretrained model. Instead of ```tensor([[59414,    15,   473,  3370,  4026,   427,  5894,   861,   473,   912, 5636]]) ```, it generates smth like `[[59414,    15,   15,  15,  15,   15,  15,   15,   15,   15, 15]]`. I cannot find a root cause of this problem, i've already checked mapping and so on several times, it should be correct. It looks like that weights are not correct but I am not sure...

> This PR is ready for review. There is one thing that looks odd to me. After dequantization and loading the model, It genereates a wrong sequence, not as expected when using a normal pretrained model. Instead oftensor([[59414,    15,   473,  3370,  4026,   427,  5894,   861,   473,   912, 5636]]) , it generates smth like [[59414,    15,   15,  15,  15,   15,  15,   15,   15,   15, 15]]. I cannot find a root cause of this problem, i've already checked mapping and so on several times, it should be correct. It looks like that weights are not correct but I am not sure...

Since the model was quantized, THis is normal that it is not behaving the same of the normal pretrained model. Dequantization doesn't recover the precision of the original model. Could you check that it behaves similarly as the original model that was converted to gguf in fp16 precision or full precision ? This way we have a way to compare the model loaded from gguf file. 

> > This PR is ready for review. There is one thing that looks odd to me. After dequantization and loading the model, It genereates a wrong sequence, not as expected when using a normal pretrained model. Instead oftensor([[59414,    15,   473,  3370,  4026,   427,  5894,   861,   473,   912, 5636]]) , it generates smth like [[59414,    15,   15,  15,  15,   15,  15,   15,   15,   15, 15]]. I cannot find a root cause of this problem, i've already checked mapping and so on several times, it should be correct. It looks like that weights are not correct but I am not sure...
> 
> Since the model was quantized, THis is normal that it is not behaving the same of the normal pretrained model. Dequantization doesn't recover the precision of the original model. Could you check that it behaves similarly as the original model that was converted to gguf in fp16 precision or full precision ? This way we have a way to compare the model loaded from gguf file.

hi @SunMarc. Still odd behaviour, it does not matter which format I take. So, i debugged model_state loading and parsing logic several times and tried to compare it with working one (like llama). Everything looks good from this perspective and it seems that weights, params, config and so on are loaded correctly into the model. I'am just worring about quantized data itself. 

Hi @SunMarc! I finally found the issue and the reason of bloom model strange behaviour. This is because of https://github.com/ggerganov/llama.cpp/blob/master/convert_hf_to_gguf.py (L972-998), this algorithm reshapes qkv data and that's why we got completely different weights in the end. I implemented reverse reshaping algorithm in **modeling_gguf_pytorch_utils.py** to place them back and after that, everything is ok, and model outputs expected values. Please, take a look on my changes, now, everything should be correct.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_33473). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

> Nice job finding the issue ! Could you add a final test to check that the fp16 from transformers and the fp16 model from gguf share the same weights ? That would be a nice test to check that the conversion was done correctly ! Can you also check that the quantized model doesn't return gibberish ?

Yes, sure, I added one more test to compare weights. But what about gibberish? Should I implement additional test to check particular generation or?

> Yes, sure, I added one more test to compare weights. But what about gibberish? Should I implement additional test to check particular generation or?

Just add a simple generation check for a q4 quants for example, just like what was done for other models ! 

> > Yes, sure, I added one more test to compare weights. But what about gibberish? Should I implement additional test to check particular generation or?
> 
> Just add a simple generation check for a q4 quants for example, just like what was done for other models !

But this kind of test is already added, it is called test_bloom_f16. Or is it not enough?

> But this kind of test is already added, it is called test_bloom_f16. Or is it not enough?

But we need one for a quantized model since most users uses these models. With fp16, we can't be sure that the dequantize step was done correctly. 

> > But this kind of test is already added, it is called test_bloom_f16. Or is it not enough?
> 
> But we need one for a quantized model since most users uses these models. With fp16, we can't be sure that the dequantize step was done correctly.

Hello! Ok, got it, I just added q8_0 test as well. Would it be sufficient?