|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +import torch_tensorrt |
| 4 | +from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteriaList |
| 5 | +from transformers.generation.stopping_criteria import ( |
| 6 | + EosTokenCriteria, |
| 7 | + MaxLengthCriteria, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.unit |
| 12 | +def test_dynamic_generation_python_rt(): |
| 13 | + """ |
| 14 | + Tests HuggingFace Generate Code with dynamic shapes |
| 15 | + Code Credit: @peri044 |
| 16 | + """ |
| 17 | + # Define tokenizer and model |
| 18 | + tokenizer = AutoTokenizer.from_pretrained("gpt2") |
| 19 | + model = ( |
| 20 | + AutoModelForCausalLM.from_pretrained( |
| 21 | + "gpt2", pad_token_id=tokenizer.eos_token_id, use_cache=False |
| 22 | + ) |
| 23 | + .eval() |
| 24 | + .to("cuda") |
| 25 | + ) |
| 26 | + |
| 27 | + # Input prompt |
| 28 | + model_inputs = tokenizer(("Repeat " * 128)[:-1], return_tensors="pt").to("cuda") |
| 29 | + input_ids = model_inputs["input_ids"] |
| 30 | + max_tokens = 40 |
| 31 | + |
| 32 | + # Pyt model outputs |
| 33 | + greedy_output = model.generate(**model_inputs, max_new_tokens=max_tokens) |
| 34 | + print( |
| 35 | + "Pytorch model generated text: ", |
| 36 | + tokenizer.decode(greedy_output[0], skip_special_tokens=True), |
| 37 | + ) |
| 38 | + |
| 39 | + # Compile Torch-TRT model |
| 40 | + torch._dynamo.mark_dynamic(input_ids, 1, min=2, max=1023) |
| 41 | + model.forward = torch.compile( |
| 42 | + model.forward, |
| 43 | + backend="tensorrt", |
| 44 | + dynamic=None, |
| 45 | + options={ |
| 46 | + "enabled_precisions": {torch.float}, |
| 47 | + "torch_executed_ops": {"torch.ops.aten.slice.Tensor"}, |
| 48 | + "use_python_runtime": True, |
| 49 | + "optimization_level": 0, |
| 50 | + "min_block_size": 29, |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + # Auto-regressive generation loop for greedy search |
| 55 | + stopping_criteria = StoppingCriteriaList( |
| 56 | + [ |
| 57 | + MaxLengthCriteria(max_length=max_tokens), |
| 58 | + EosTokenCriteria(eos_token_id=tokenizer.eos_token_id), |
| 59 | + ] |
| 60 | + ) |
| 61 | + while True: |
| 62 | + trt_outputs = model(input_ids) |
| 63 | + logits = trt_outputs.logits |
| 64 | + next_token_logits = logits[:, -1, :] |
| 65 | + next_tokens = torch.argmax(next_token_logits, dim=-1) |
| 66 | + input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1) |
| 67 | + if stopping_criteria(input_ids, logits).item(): |
| 68 | + break |
| 69 | + |
| 70 | + # TODO: Add test for correctness |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.unit |
| 74 | +def test_dynamic_generation_cpp_rt(): |
| 75 | + """ |
| 76 | + Tests HuggingFace Generate Code with dynamic shapes |
| 77 | + Code Credit: @peri044 |
| 78 | + """ |
| 79 | + # Define tokenizer and model |
| 80 | + tokenizer = AutoTokenizer.from_pretrained("gpt2") |
| 81 | + model = ( |
| 82 | + AutoModelForCausalLM.from_pretrained( |
| 83 | + "gpt2", pad_token_id=tokenizer.eos_token_id, use_cache=False |
| 84 | + ) |
| 85 | + .eval() |
| 86 | + .to("cuda") |
| 87 | + ) |
| 88 | + |
| 89 | + # Input prompt |
| 90 | + model_inputs = tokenizer(("Repeat " * 128)[:-1], return_tensors="pt").to("cuda") |
| 91 | + input_ids = model_inputs["input_ids"] |
| 92 | + max_tokens = 40 |
| 93 | + |
| 94 | + # Pyt model outputs |
| 95 | + greedy_output = model.generate(**model_inputs, max_new_tokens=max_tokens) |
| 96 | + print( |
| 97 | + "Pytorch model generated text: ", |
| 98 | + tokenizer.decode(greedy_output[0], skip_special_tokens=True), |
| 99 | + ) |
| 100 | + |
| 101 | + # Compile Torch-TRT model |
| 102 | + torch._dynamo.mark_dynamic(input_ids, 1, min=2, max=1023) |
| 103 | + model.forward = torch.compile( |
| 104 | + model.forward, |
| 105 | + backend="tensorrt", |
| 106 | + dynamic=None, |
| 107 | + options={ |
| 108 | + "enabled_precisions": {torch.float}, |
| 109 | + "torch_executed_ops": {"torch.ops.aten.slice.Tensor"}, |
| 110 | + "use_python_runtime": False, |
| 111 | + "optimization_level": 0, |
| 112 | + "min_block_size": 29, |
| 113 | + }, |
| 114 | + ) |
| 115 | + |
| 116 | + # Auto-regressive generation loop for greedy search |
| 117 | + stopping_criteria = StoppingCriteriaList( |
| 118 | + [ |
| 119 | + MaxLengthCriteria(max_length=max_tokens), |
| 120 | + EosTokenCriteria(eos_token_id=tokenizer.eos_token_id), |
| 121 | + ] |
| 122 | + ) |
| 123 | + while True: |
| 124 | + trt_outputs = model(input_ids) |
| 125 | + logits = trt_outputs.logits |
| 126 | + next_token_logits = logits[:, -1, :] |
| 127 | + next_tokens = torch.argmax(next_token_logits, dim=-1) |
| 128 | + input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1) |
| 129 | + if stopping_criteria(input_ids, logits).item(): |
| 130 | + break |
| 131 | + |
| 132 | + # TODO: Add test for correctness |
0 commit comments