-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In tutorials/quantize_vit, extract common methods to util.py (#238)
* Extract common methods to util.py * Update tutorials/quantize_vit/run_vit_b.py Co-authored-by: Mark Saroufim <[email protected]> * Update tutorials/quantize_vit/run_vit_b_quant.py Co-authored-by: Mark Saroufim <[email protected]> * amend * amend * Include the torchao utils --------- Co-authored-by: Mark Saroufim <[email protected]> Co-authored-by: Mark Saroufim <[email protected]>
- Loading branch information
1 parent
adfe570
commit f0f00ce
Showing
3 changed files
with
30 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import torch | ||
|
||
|
||
def benchmark_model(model, num_runs, input_tensor): | ||
torch.cuda.synchronize() | ||
start_event = torch.cuda.Event(enable_timing=True) | ||
end_event = torch.cuda.Event(enable_timing=True) | ||
start_event.record() | ||
|
||
# benchmark | ||
for _ in range(num_runs): | ||
with torch.autograd.profiler.record_function("timed region"): | ||
model(input_tensor) | ||
|
||
end_event.record() | ||
torch.cuda.synchronize() | ||
return start_event.elapsed_time(end_event) / num_runs | ||
|
||
def profiler_runner(path, fn, *args, **kwargs): | ||
with torch.profiler.profile( | ||
activities=[torch.profiler.ProfilerActivity.CPU, | ||
torch.profiler.ProfilerActivity.CUDA], | ||
record_shapes=True) as prof: | ||
result = fn(*args, **kwargs) | ||
prof.export_chrome_trace(path) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters