2023-10-11 03:40:06 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
|
|
|
import transformers
|
2023-10-27 08:19:54 +00:00
|
|
|
from packaging import version
|
2023-10-11 03:40:06 +00:00
|
|
|
|
|
|
|
import colossalai
|
2023-10-27 08:19:54 +00:00
|
|
|
from colossalai.inference.pipeline import PPInferEngine
|
|
|
|
from colossalai.inference.pipeline.policies import LlamaModelInferPolicy
|
2023-10-11 03:40:06 +00:00
|
|
|
from colossalai.testing import clear_cache_before_run, parameterize, rerun_if_address_is_in_use, spawn
|
|
|
|
|
2023-10-27 08:19:54 +00:00
|
|
|
CUDA_SUPPORT = version.parse(torch.version.cuda) > version.parse("11.5")
|
|
|
|
|
2023-10-11 03:40:06 +00:00
|
|
|
|
|
|
|
def data_gen():
|
|
|
|
input_ids = torch.tensor([[15496, 11, 616, 3290, 318, 13779, 318, 13779]], dtype=torch.int64)
|
|
|
|
attention_mask = torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1]], dtype=torch.int64)
|
|
|
|
return dict(input_ids=input_ids, attention_mask=attention_mask)
|
|
|
|
|
|
|
|
|
|
|
|
inputs = data_gen()
|
|
|
|
for k, v in inputs.items():
|
2023-10-18 03:46:37 +00:00
|
|
|
if torch.is_tensor(v) or "Tensor" in v.__class__.__name__:
|
2023-10-11 03:40:06 +00:00
|
|
|
new_shape = [1] * v.dim()
|
|
|
|
new_shape[0] = 16
|
2023-10-18 03:46:37 +00:00
|
|
|
inputs[k] = v.to("cuda").repeat(*new_shape)
|
2023-10-11 03:40:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pipeline_inference_test(pp_size, new_length, micro_batch_size):
|
2023-10-27 08:19:54 +00:00
|
|
|
model = transformers.LlamaForCausalLM(transformers.LlamaConfig(num_hidden_layers=4))
|
|
|
|
|
2023-10-18 03:46:37 +00:00
|
|
|
engine = PPInferEngine(
|
|
|
|
pp_size=pp_size,
|
|
|
|
model=model,
|
2023-10-27 08:19:54 +00:00
|
|
|
model_policy=LlamaModelInferPolicy(),
|
2023-10-18 03:46:37 +00:00
|
|
|
new_length=new_length,
|
|
|
|
micro_batch_size=micro_batch_size,
|
|
|
|
)
|
2023-10-27 08:19:54 +00:00
|
|
|
output = engine.inference(inputs)
|
2023-10-11 03:40:06 +00:00
|
|
|
if dist.get_rank() == 0:
|
|
|
|
assert len(output[0]) == new_length, f"{len(output)}, {new_length}"
|
|
|
|
|
|
|
|
|
2023-10-27 08:19:54 +00:00
|
|
|
@parameterize("pp_size", [2])
|
2023-10-18 03:46:37 +00:00
|
|
|
@parameterize("new_length", [4, 8, 16])
|
|
|
|
@parameterize("micro_batch_size", [1, 4])
|
2023-10-11 03:40:06 +00:00
|
|
|
@clear_cache_before_run()
|
|
|
|
def run_pipeline_inference_test(pp_size, new_length, micro_batch_size):
|
|
|
|
pipeline_inference_test(pp_size, new_length, micro_batch_size)
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
def check_pipeline_inference(rank, world_size, port):
|
2023-10-18 03:46:37 +00:00
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2023-10-11 03:40:06 +00:00
|
|
|
run_pipeline_inference_test()
|
|
|
|
|
|
|
|
|
2023-10-27 08:19:54 +00:00
|
|
|
@pytest.mark.skipif(not CUDA_SUPPORT, reason="kv-cache manager engine requires cuda version to be higher than 11.5")
|
2023-10-11 03:40:06 +00:00
|
|
|
@pytest.mark.dist
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
@clear_cache_before_run()
|
|
|
|
def test_pipeline_inference():
|
2023-10-27 08:19:54 +00:00
|
|
|
spawn(check_pipeline_inference, nprocs=2)
|
2023-10-11 03:40:06 +00:00
|
|
|
|
|
|
|
|
2023-10-18 03:46:37 +00:00
|
|
|
if __name__ == "__main__":
|
2023-10-11 03:40:06 +00:00
|
|
|
test_pipeline_inference()
|