test_idefics.py 1.92 KB
Newer Older
jixx's avatar
init  
jixx committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import pytest


@pytest.fixture(scope="module")
def idefics_handle(launcher):
    with launcher(
        "HuggingFaceM4/idefics-9b-instruct", num_shard=2, dtype="float16"
    ) as handle:
        yield handle


@pytest.fixture(scope="module")
async def idefics(idefics_handle):
    await idefics_handle.health(300)
    return idefics_handle.client


@pytest.mark.asyncio
jixx's avatar
jixx committed
19
async def test_idefics(idefics, response_snapshot, chicken):
jixx's avatar
init  
jixx committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    response = await idefics.generate(
        f"User:![]({chicken})Can you tell me a very short story based on the image?",
        max_new_tokens=10,
        decoder_input_details=True,
    )

    assert response.details.generated_tokens == 10
    assert (
        response.generated_text == " \nAssistant: A rooster stands"
    ), f"{repr(response.generated_text)}"
    assert response == response_snapshot


@pytest.mark.release
@pytest.mark.asyncio
@pytest.mark.private
jixx's avatar
jixx committed
36
async def test_idefics_two_images(idefics, response_snapshot, chicken, cow_beach):
jixx's avatar
init  
jixx committed
37
38
39
40
41
42
43
44
45
46
47
48
    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


@pytest.mark.release
@pytest.mark.asyncio
jixx's avatar
jixx committed
49
async def test_idefics_load(idefics, generate_load, response_snapshot, chicken):
jixx's avatar
init  
jixx committed
50
51
52
53
54
55
56
57
58
    responses = await generate_load(
        idefics,
        f"User:![]({chicken})Can you tell me a very short story based on the image?",
        max_new_tokens=10,
        n=4,
    )

    generated_texts = [r.generated_text for r in responses]

jixx's avatar
jixx committed
59
    assert generated_texts[0] == " \nAssistant: A rooster stands"
jixx's avatar
init  
jixx committed
60
61
62
63
64
65
    assert len(generated_texts) == 4
    assert generated_texts, all(
        [text == generated_texts[0] for text in generated_texts]
    )

    assert responses == response_snapshot