From c6bb76703f2e7d1904417130994e8a8fdb0b6531 Mon Sep 17 00:00:00 2001 From: Vince Jankovics Date: Thu, 23 Nov 2023 14:00:09 +0000 Subject: [PATCH] Fix IDEFICS dtype (#1214) # What does this PR do? This forces the use of `bfloat16` for IDEFICS. The issue is that with `float16` the 80b model gives garbage output. Let me know if this solution is not appropriate and I'll adjust accordingly. For the details see below. The current behaviour: ```sh $ curl 127.0.0.1:8080/generate -X POST -d '{"inputs":"What is Deep Learning?","parameters":{"max_new_tokens":20}}' -H 'Content-Type: application/json' {"generated_text":""} ``` On closer inspection with: ```python import requests headers = { "Content-Type": "application/json"} query = "What is Deep Learning?" data = { "inputs": query, "parameters": { "max_new_tokens": 10, "return_full_text": True, "decoder_input_details": True, "do_sample": False, }, } api_url = "http://127.0.0.1:8080" response = requests.post(api_url + "/generate", headers=headers, json=data).json() for i in ['prefill', 'tokens']: print(f'### {i}') print(repr(''.join([t['text'] for t in response['details'][i]]))) ``` Prints: ``` ### prefill 'WhatisDeepLearning?' ### tokens '' ######## ``` With the change in this PR it prints: ``` ### prefill 'WhatisDeepLearning?' ### tokens '\n\nDeep Learning is a subset of machine' ``` Note, using the Transformers implementation (with `IdeficsForVisionText2Text.from_pretrained`) produces the latter (correct) output as well. This only happens with the 80b model, the 9b model is not as sensitive to the dtype (as also mentioned in the code). The reason for "forcing" this in the IDEFICS init method, is because if quantization is used, then the dtype cannot be set explicitly. And since it's left as `None`, it's set to `float16` by default [here](https://github.com/huggingface/text-generation-inference/blob/96a982ad8fc232479384476b1596a880697cc1d0/server/text_generation_server/models/__init__.py#L90). I.e. there's no other way to manually change the dtype if someone is using quantization: ```sh $ docker run .... ghcr.io/huggingface/text-generation-inference:latest --model-id HuggingFaceM4/idefics-80b-instruct --dtype bfloat16 --quantize bitsandbytes-nf4 ..... 2023-10-31T12:42:26.710401Z INFO shard-manager: text_generation_launcher: Starting shard rank=0 2023-10-31T12:42:30.315734Z ERROR shard-manager: text_generation_launcher: Shard complete standard error output: Traceback (most recent call last): File "/opt/conda/bin/text-generation-server", line 8, in sys.exit(app()) File "/opt/conda/lib/python3.9/site-packages/text_generation_server/cli.py", line 80, in serve raise RuntimeError( RuntimeError: Only 1 can be set between `dtype` and `quantize`, as they both decide how goes the final model. rank=0 Error: ShardCannotStart 2023-10-31T12:42:30.414010Z ERROR text_generation_launcher: Shard 0 failed to start 2023-10-31T12:42:30.414044Z INFO text_generation_launcher: Shutting down shards ``` ## 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#start-contributing-pull-requests), Pull Request section? - [ ] 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. - [ ] 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). - [ ] Did you write any new necessary tests? ## Who can review? Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR. @Narsil what do you think? --------- Co-authored-by: Nicolas Patry --- server/text_generation_server/models/idefics_causal_lm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/text_generation_server/models/idefics_causal_lm.py b/server/text_generation_server/models/idefics_causal_lm.py index 2472caf6..dcad1fa9 100644 --- a/server/text_generation_server/models/idefics_causal_lm.py +++ b/server/text_generation_server/models/idefics_causal_lm.py @@ -583,7 +583,7 @@ class IdeficsCausalLM(Model): if torch.cuda.is_available(): device = torch.device("cuda") - dtype = torch.float16 if dtype is None else dtype + dtype = torch.bfloat16 if dtype is None else dtype else: if quantize: raise ValueError("quantization is not available on CPU")