test_bloom_infer.py 2.11 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
17
18
try:
    import lightllm 
    HAS_LIGHTLLM_KERNEL = True
except:
    HAS_LIGHTLLM_KERNEL = False
    
19
20
21
22
23
TP_SIZE = 2
MAX_BATCH_SIZE = 4
MAX_INPUT_LEN = 16
MAX_OUTPUT_LEN = 32

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


27
28
29
30
31
32
33
34
@parameterize(
    "test_config",
    [
        {
            "tp_size": TP_SIZE,
        }
    ],
)
35
def run(test_config):
Xu Kai's avatar
Xu Kai committed
36
37
38
    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()
39

Xu Kai's avatar
Xu Kai committed
40
41
42
43
44
    shard_config = ShardConfig(
        enable_tensor_parallelism=True if test_config["tp_size"] > 1 else False, inference_only=True
    )
    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)
45

Xu Kai's avatar
Xu Kai committed
46
47
48
49
50
    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)
51

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


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


61
@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")
62
63
64
65
66
67
68
@pytest.mark.dist
@rerun_if_address_is_in_use()
@clear_cache_before_run()
def test_bloom_infer():
    spawn(check_bloom, TP_SIZE)


69
if __name__ == "__main__":
70
    test_bloom_infer()