test_bloom_infer.py 1.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import pytest
import torch
from packaging import version

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
from tests.kit.model_zoo import model_zoo

TP_SIZE = 2
MAX_BATCH_SIZE = 4
MAX_INPUT_LEN = 16
MAX_OUTPUT_LEN = 32

17
CUDA_SUPPORT = version.parse(torch.version.cuda) > version.parse("11.5")
18
19


20
21
22
23
24
25
26
27
@parameterize(
    "test_config",
    [
        {
            "tp_size": TP_SIZE,
        }
    ],
)
28
def run(test_config):
29
    sub_model_zoo = model_zoo.get_sub_registry("transformers_bloom_for_causal_lm")
30
31
32
33
34
    for name, (model_fn, data_gen_fn, _, _, _) in sub_model_zoo.items():
        orig_model = model_fn()
        orig_model = orig_model.half()
        data = data_gen_fn()

35
36
37
        shard_config = ShardConfig(
            enable_tensor_parallelism=True if test_config["tp_size"] > 1 else False, inference_only=True
        )
38
39
40
41
42
43
44
45
46
47
        infer_engine = TPInferEngine(orig_model, shard_config, MAX_BATCH_SIZE, MAX_INPUT_LEN, MAX_OUTPUT_LEN)

        generate_kwargs = dict(do_sample=False)
        outputs = infer_engine.generate(data, **generate_kwargs)

        assert outputs is not None


def check_bloom(rank, world_size, port):
    disable_existing_loggers()
48
    colossalai.launch(config={}, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
49
50
51
52
53
54
55
56
57
58
59
    run()


@pytest.mark.skipif(not CUDA_SUPPORT, reason="kv-cache manager engine requires cuda version to be higher than 11.5")
@pytest.mark.dist
@rerun_if_address_is_in_use()
@clear_cache_before_run()
def test_bloom_infer():
    spawn(check_bloom, TP_SIZE)


60
if __name__ == "__main__":
61
    test_bloom_infer()