• Vince Jankovics's avatar
    Fix IDEFICS dtype (#1214) · c6bb7670
    Vince Jankovics authored
    # What does this PR do?
    
    <!--
    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 -->
    
    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
    '<s>WhatisDeepLearning?'
    ### tokens
    '<unk><unk><unk><unk><unk><unk><unk><unk><unk><unk>'
    ########
    ```
    
    With the change in this PR it prints:
    ```
    ### prefill
    '<s>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 <module>
        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?
    
    <!-- Your PR will be replied to more quickly if you can figure out the
    right person to tag with @
    
    
    @OlivierDehaene OR @Narsil
    
     -->
    
    ---------
    Co-authored-by: default avatarNicolas Patry <patry.nicolas@protonmail.com>
    c6bb7670
idefics_causal_lm.py 31.3 KB