test_tokenizer.py 2.18 KB
Newer Older
zhouxiang's avatar
zhouxiang committed
1
2
import random

3
4
import pytest

zhouxiang's avatar
zhouxiang committed
5
from lmdeploy.tokenizer import DetokenizeState, HuggingFaceTokenizer
6
7
8
9


@pytest.mark.parametrize('model_path', [
    'internlm/internlm-chat-7b', 'Qwen/Qwen-7B-Chat',
zhouxiang's avatar
zhouxiang committed
10
    'baichuan-inc/Baichuan2-7B-Chat', 'upstage/SOLAR-0-70b-16bit',
11
    'baichuan-inc/Baichuan-7B', 'codellama/CodeLlama-7b-hf',
zhouxiang's avatar
zhouxiang committed
12
13
14
15
16
17
18
    'THUDM/chatglm2-6b', '01-ai/Yi-6B-200k', '01-ai/Yi-34B-Chat',
    '01-ai/Yi-6B-Chat', 'WizardLM/WizardLM-70B-V1.0',
    'codellama/CodeLlama-34b-Instruct-hf', 'tiiuae/falcon-7b'
])
@pytest.mark.parametrize('input', [
    'hi, this is a test 😆😆! ' * 5, '為什麼我還在用繁體字 😆😆 gg! ' * 5,
    ' License at\n#\n#' + ' ' * 100 + 'ht', '   '
19
])
zhouxiang's avatar
zhouxiang committed
20
21
22
@pytest.mark.parametrize('interval', [1, 3])
@pytest.mark.parametrize('skip_special_tokens', [True, False])
def test_tokenizer(model_path, input, interval, skip_special_tokens):
23
    tokenizer = HuggingFaceTokenizer(model_path)
zhouxiang's avatar
zhouxiang committed
24
    encoded = tokenizer.encode(input, False, add_special_tokens=False)
25
    output = ''
zhouxiang's avatar
zhouxiang committed
26
27
28
29
30
31
32
33
34
    state = DetokenizeState()
    for i in range(0, len(encoded), interval):
        offset = i + interval
        if offset < len(encoded):
            # lmdeploy may decode nothing when concurrency is high
            if random.randint(1, 10) < 4:
                offset -= interval
        decoded, state = tokenizer.detokenize_incrementally(
            encoded[:offset], state, skip_special_tokens)
35
36
        output += decoded
    assert input == output, 'input string should equal to output after enc-dec'
zhouxiang's avatar
zhouxiang committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


@pytest.mark.parametrize('model_path', [
    'internlm/internlm-chat-7b', 'Qwen/Qwen-7B-Chat',
    'baichuan-inc/Baichuan2-7B-Chat', 'codellama/CodeLlama-7b-hf',
    'upstage/SOLAR-0-70b-16bit'
])
@pytest.mark.parametrize('stop_words', ['.', ' ', '?', ''])
def test_tokenizer_with_stop_words(model_path, stop_words):
    tokenizer = HuggingFaceTokenizer(model_path)
    indexes = tokenizer.indexes_containing_token(stop_words)
    assert indexes is not None


def test_qwen_vl_decode_special():
    from lmdeploy.tokenizer import Tokenizer
    tok = Tokenizer('Qwen/Qwen-VL-Chat')
    try:
        tok.decode([151857])
        assert (0)
    except Exception as e:
        assert str(e) == 'Unclosed image token'