Profile a single inference run.
()
| 16 | |
| 17 | |
| 18 | def profile_single_run(): |
| 19 | """Profile a single inference run.""" |
| 20 | from questions.inference_server.model_cache import ModelCache |
| 21 | from questions.models import GenerateParams |
| 22 | from questions.text_generator_inference import fast_inference |
| 23 | |
| 24 | model_cache = ModelCache() |
| 25 | params = GenerateParams( |
| 26 | text="Once upon a time", |
| 27 | max_length=20, # Short generation for profiling |
| 28 | temperature=0.7, |
| 29 | top_p=0.9, |
| 30 | top_k=40, |
| 31 | number_of_results=1, |
| 32 | model="any", |
| 33 | ) |
| 34 | |
| 35 | # Single warmup |
| 36 | print("Warmup...") |
| 37 | fast_inference(params, model_cache) |
| 38 | if torch.cuda.is_available(): |
| 39 | torch.cuda.synchronize() |
| 40 | |
| 41 | gc.collect() |
| 42 | if torch.cuda.is_available(): |
| 43 | torch.cuda.empty_cache() |
| 44 | |
| 45 | # Profile single run |
| 46 | print("Profiling single inference run...") |
| 47 | profiler = cProfile.Profile() |
| 48 | profiler.enable() |
| 49 | |
| 50 | result = fast_inference(params, model_cache) |
| 51 | |
| 52 | if torch.cuda.is_available(): |
| 53 | torch.cuda.synchronize() |
| 54 | |
| 55 | profiler.disable() |
| 56 | |
| 57 | # Print stats |
| 58 | stream = io.StringIO() |
| 59 | stats = pstats.Stats(profiler, stream=stream) |
| 60 | stats.strip_dirs() |
| 61 | stats.sort_stats("cumulative") |
| 62 | |
| 63 | print("\n" + "="*60) |
| 64 | print("TOP 25 FUNCTIONS BY CUMULATIVE TIME") |
| 65 | print("="*60 + "\n") |
| 66 | |
| 67 | stats.print_stats(25) |
| 68 | print(stream.getvalue()) |
| 69 | |
| 70 | # Component analysis |
| 71 | print("\n" + "="*60) |
| 72 | print("TIME BY COMPONENT") |
| 73 | print("="*60) |
| 74 | |
| 75 | component_times = { |
no test coverage detected