test_bloom_infer.py 2.12 KB
Newer Older
1
2
3
import pytest
import torch
from packaging import version
Xu Kai's avatar
Xu Kai committed
4
5
from transformers import BloomForCausalLM
from transformers.models.bloom.configuration_bloom import BloomConfig
6
7
8
9
10
11
12

import colossalai
from colossalai.inference.tensor_parallel import TPInferEngine
from colossalai.logging import disable_existing_loggers
from colossalai.shardformer import ShardConfig
from colossalai.testing import clear_cache_before_run, parameterize, rerun_if_address_is_in_use, spawn

13
14
15
16
try:
    HAS_LIGHTLLM_KERNEL = True
except:
    HAS_LIGHTLLM_KERNEL = False
17

18
19
20
21
22
TP_SIZE = 2
MAX_BATCH_SIZE = 4
MAX_INPUT_LEN = 16
MAX_OUTPUT_LEN = 32

23
CUDA_SUPPORT = version.parse(torch.version.cuda) > version.parse("11.5")
24
25


26
27
28
29
30
31
32
33
@parameterize(
    "test_config",
    [
        {
            "tp_size": TP_SIZE,
        }
    ],
)
34
def run(test_config):
Xu Kai's avatar
Xu Kai committed
35
36
37
    bloom_config = BloomConfig(num_hidden_layers=2, bos_token_id=0, eos_token_id=1, vocab_size=1200, hidden_size=1024)
    model = BloomForCausalLM(bloom_config)
    model = model.half()
38

Xu Kai's avatar
Xu Kai committed
39
    shard_config = ShardConfig(
40
        enable_tensor_parallelism=True if test_config["tp_size"] > 1 else False, extra_kwargs={"inference_only": True}
Xu Kai's avatar
Xu Kai committed
41
42
43
    )
    infer_engine = TPInferEngine(model, shard_config, MAX_BATCH_SIZE, MAX_INPUT_LEN, MAX_OUTPUT_LEN)
    generate_kwargs = dict(max_new_tokens=MAX_OUTPUT_LEN, do_sample=False)
44

Xu Kai's avatar
Xu Kai committed
45
46
47
48
49
    input_tokens = {
        "input_ids": torch.randint(1, 1000, (MAX_BATCH_SIZE, MAX_INPUT_LEN), device="cuda"),
        "attention_mask": torch.ones((MAX_BATCH_SIZE, MAX_INPUT_LEN), device="cuda"),
    }
    outputs = infer_engine.generate(input_tokens, **generate_kwargs)
50

Xu Kai's avatar
Xu Kai committed
51
    assert outputs is not None
52
53
54
55


def check_bloom(rank, world_size, port):
    disable_existing_loggers()
56
    colossalai.launch(config={}, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
57
58
59
    run()


60
61
62
63
@pytest.mark.skipif(
    not CUDA_SUPPORT or not HAS_LIGHTLLM_KERNEL,
    reason="kv-cache manager engine requires cuda version to be higher than 11.5",
)
64
65
66
67
68
69
70
@pytest.mark.dist
@rerun_if_address_is_in_use()
@clear_cache_before_run()
def test_bloom_infer():
    spawn(check_bloom, TP_SIZE)


71
if __name__ == "__main__":
72
    test_bloom_infer()