2023-08-17 06:38:49 -06:00
|
|
|
import pytest
|
2024-03-21 15:21:03 -06:00
|
|
|
import base64
|
2023-08-17 06:38:49 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def idefics_handle(launcher):
|
2023-12-11 06:49:52 -07:00
|
|
|
with launcher(
|
|
|
|
"HuggingFaceM4/idefics-9b-instruct", num_shard=2, dtype="float16"
|
|
|
|
) as handle:
|
2023-08-17 06:38:49 -06:00
|
|
|
yield handle
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
async def idefics(idefics_handle):
|
|
|
|
await idefics_handle.health(300)
|
|
|
|
return idefics_handle.client
|
|
|
|
|
|
|
|
|
2024-03-21 15:21:03 -06:00
|
|
|
# TODO fix the server parsser to count inline image tokens correctly
|
|
|
|
def get_chicken():
|
|
|
|
with open("integration-tests/images/chicken_on_money.png", "rb") as image_file:
|
|
|
|
encoded_string = base64.b64encode(image_file.read())
|
2024-03-22 10:14:54 -06:00
|
|
|
return f"data:image/png;base64,{encoded_string.decode('utf-8')}"
|
2024-03-21 15:21:03 -06:00
|
|
|
|
|
|
|
|
2024-06-17 02:49:41 -06:00
|
|
|
def get_cow_beach():
|
|
|
|
with open("integration-tests/images/cow_beach.png", "rb") as image_file:
|
|
|
|
encoded_string = base64.b64encode(image_file.read())
|
|
|
|
return f"data:image/png;base64,{encoded_string.decode('utf-8')}"
|
|
|
|
|
|
|
|
|
2023-08-17 06:38:49 -06:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_idefics(idefics, response_snapshot):
|
2024-03-22 10:14:54 -06:00
|
|
|
chicken = get_chicken()
|
2023-08-17 06:38:49 -06:00
|
|
|
response = await idefics.generate(
|
2024-03-22 10:14:54 -06:00
|
|
|
f"User:![]({chicken})Can you tell me a very short story based on the image?",
|
2023-08-17 06:38:49 -06:00
|
|
|
max_new_tokens=10,
|
|
|
|
decoder_input_details=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert response.details.generated_tokens == 10
|
Adding Llava-Next (Llava 1.6) with full support. (#1709)
# What does this PR do?
- Changed all models to extract `embed_tokens` in order to enable llava
to separately call the embeddings and the core model layers.
- Added VlmCausalLM to inherit from FlashMistral in order to be
maximally supported. The only added logics sits on top and parses images
into pixel values, preallocates input_ids space for the image
embeddings, and passes them for the model.
- Added Clip for the vision tower.
- Didn't add flash for the vision tower since there's no padding anyway.
- Added heuristic (potentially incomplete) to calculate number of
features *before* calculating the clip patches (allows for easier logic
reuse of the LLM under the hood).
Still needs to be done:
- [x] Implement the image parsing in the controller side, to avoid
downloading n times per TP shard and also refusing requests too large
early and avoid issues where the truncation actually truncates the
image.
- [ ] Make sure it works with quantization properly.
- [x] Make sure it works with TP>1
<!--
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).
- [ ] 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.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
2024-04-09 13:32:00 -06:00
|
|
|
assert (
|
|
|
|
response.generated_text == " \nAssistant: A rooster stands"
|
|
|
|
), f"{repr(response.generated_text)}"
|
2023-08-17 06:38:49 -06:00
|
|
|
assert response == response_snapshot
|
|
|
|
|
|
|
|
|
2024-06-17 02:49:41 -06:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.private
|
|
|
|
async def test_idefics_two_images(idefics, response_snapshot):
|
|
|
|
chicken = get_chicken()
|
|
|
|
cow_beach = get_cow_beach()
|
|
|
|
response = await idefics.generate(
|
|
|
|
f"User:![]({chicken})![]({cow_beach})Where are the cow and chicken?<end_of_utterance> \nAssistant:",
|
|
|
|
max_new_tokens=20,
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
response.generated_text == " The cow and chicken are on a beach."
|
|
|
|
), f"{repr(response.generated_text)}"
|
|
|
|
assert response == response_snapshot
|
|
|
|
|
|
|
|
|
2023-08-17 06:38:49 -06:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_idefics_load(idefics, generate_load, response_snapshot):
|
2024-03-22 10:14:54 -06:00
|
|
|
chicken = get_chicken()
|
2023-08-17 06:38:49 -06:00
|
|
|
responses = await generate_load(
|
|
|
|
idefics,
|
2024-03-22 10:14:54 -06:00
|
|
|
f"User:![]({chicken})Can you tell me a very short story based on the image?",
|
2023-08-17 06:38:49 -06:00
|
|
|
max_new_tokens=10,
|
|
|
|
n=4,
|
|
|
|
)
|
|
|
|
|
|
|
|
generated_texts = [r.generated_text for r in responses]
|
|
|
|
|
Adding Llava-Next (Llava 1.6) with full support. (#1709)
# What does this PR do?
- Changed all models to extract `embed_tokens` in order to enable llava
to separately call the embeddings and the core model layers.
- Added VlmCausalLM to inherit from FlashMistral in order to be
maximally supported. The only added logics sits on top and parses images
into pixel values, preallocates input_ids space for the image
embeddings, and passes them for the model.
- Added Clip for the vision tower.
- Didn't add flash for the vision tower since there's no padding anyway.
- Added heuristic (potentially incomplete) to calculate number of
features *before* calculating the clip patches (allows for easier logic
reuse of the LLM under the hood).
Still needs to be done:
- [x] Implement the image parsing in the controller side, to avoid
downloading n times per TP shard and also refusing requests too large
early and avoid issues where the truncation actually truncates the
image.
- [ ] Make sure it works with quantization properly.
- [x] Make sure it works with TP>1
<!--
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).
- [ ] 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.
<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @
@OlivierDehaene OR @Narsil
-->
2024-04-09 13:32:00 -06:00
|
|
|
assert (
|
|
|
|
generated_texts[0] == " \nAssistant: A rooster stands"
|
|
|
|
), f"{response.generated_text}"
|
2023-08-17 06:38:49 -06:00
|
|
|
assert len(generated_texts) == 4
|
|
|
|
assert generated_texts, all(
|
|
|
|
[text == generated_texts[0] for text in generated_texts]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert responses == response_snapshot
|