test_utils.py 2.06 KB
Newer Older
1
2
import pytest

3
4
from huggingface_hub.utils import RevisionNotFoundError

5
6
7
8
from text_generation.utils import (
    weight_hub_files,
    download_weights,
    weight_files,
9
10
    StopSequenceCriteria,
    StoppingCriteria,
11
12
13
14
    LocalEntryNotFoundError,
)


15
def test_stop_sequence_criteria():
16
    criteria = StopSequenceCriteria("/test;")
17

18
19
20
21
    assert not criteria("/")
    assert not criteria("/test")
    assert criteria("/test;")
    assert not criteria("/test; ")
22
23


24
25
26
27
def test_stopping_criteria():
    criteria = StoppingCriteria(0, [StopSequenceCriteria("/test;")], max_new_tokens=5)
    assert criteria(65827, "/test") == (False, None)
    assert criteria(30, ";") == (True, "stop_sequence")
28
29


30
31
32
33
def test_stopping_criteria_eos():
    criteria = StoppingCriteria(0, [StopSequenceCriteria("/test;")], max_new_tokens=5)
    assert criteria(1, "") == (False, None)
    assert criteria(0, "") == (True, "eos_token")
34
35
36


def test_stopping_criteria_max():
37
38
39
40
41
42
    criteria = StoppingCriteria(0, [StopSequenceCriteria("/test;")], max_new_tokens=5)
    assert criteria(1, "") == (False, None)
    assert criteria(1, "") == (False, None)
    assert criteria(1, "") == (False, None)
    assert criteria(1, "") == (False, None)
    assert criteria(1, "") == (True, "length")
43
44


45
46
47
48
49
50
51
52
53
54
55
def test_weight_hub_files():
    filenames = weight_hub_files("bigscience/bloom-560m")
    assert filenames == ["model.safetensors"]


def test_weight_hub_files_llm():
    filenames = weight_hub_files("bigscience/bloom")
    assert filenames == [f"model_{i:05d}-of-00072.safetensors" for i in range(1, 73)]


def test_weight_hub_files_empty():
56
    filenames = weight_hub_files("bigscience/bloom", extension=".errors")
57
58
59
60
61
62
63
64
65
66
    assert filenames == []


def test_download_weights():
    files = download_weights("bigscience/bloom-560m")
    local_files = weight_files("bigscience/bloom-560m")
    assert files == local_files


def test_weight_files_error():
67
68
    with pytest.raises(RevisionNotFoundError):
        weight_files("bigscience/bloom-560m", revision="error")
69
70
    with pytest.raises(LocalEntryNotFoundError):
        weight_files("bert-base-uncased")