-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[refactor] [misc] Refactoring benchmark code for performance monitoring #3269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yolo2themoon
merged 2 commits into
taichi-dev:master
from
yolo2themoon:benchmark_refactor
Oct 27, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,107 +1,78 @@ | ||
| import time | ||
|
|
||
| from membound_cases import fill, reduction, saxpy | ||
| from utils import * | ||
| from membound_cases import memory_bound_cases_list | ||
| from utils import (arch_name, dtype2str, geometric_mean, kibibyte, | ||
| md_table_header, size2str) | ||
|
|
||
| import taichi as ti | ||
|
|
||
| test_cases = [fill, saxpy, reduction] | ||
| test_archs = [ti.cuda] | ||
| test_dtype = [ti.i32, ti.i64, ti.f32, ti.f64] | ||
| test_dsize = [(4**i) * kibibyte for i in range(1, 10)] #[4KB,16KB...256MB] | ||
| test_repeat = 10 | ||
| results_evaluation = [geometric_mean] | ||
|
|
||
|
|
||
| class BenchmarkResult: | ||
| def __init__(self, name, arch, dtype, dsize, results_evaluation): | ||
| self.test_name = name | ||
| self.test_arch = arch | ||
| self.data_type = dtype | ||
| self.data_size = dsize | ||
| self.min_time_in_us = [] | ||
| self.results_evaluation = results_evaluation | ||
|
|
||
| def time2mdtableline(self): | ||
| string = '|' + self.test_name + '.' + dtype2str[self.data_type] + '|' | ||
| string += ''.join( | ||
| str(round(time, 4)) + '|' for time in self.min_time_in_us) | ||
| string += ''.join( | ||
| str(round(item(self.min_time_in_us), 4)) + '|' | ||
| for item in self.results_evaluation) | ||
| return string | ||
|
|
||
|
|
||
| class BenchmarkImpl: | ||
| def __init__(self, func, archs, data_type, data_size): | ||
| self.func = func | ||
| self.name = func.__name__ | ||
| self.env = None | ||
| self.device = None | ||
| self.archs = archs | ||
| self.data_type = data_type | ||
| self.data_size = data_size | ||
| self.benchmark_results = [] | ||
| class MemoryBound: | ||
| suite_name = 'memorybound' | ||
| supported_archs = [ti.cpu, ti.cuda] | ||
| test_cases = memory_bound_cases_list | ||
| test_dtype_list = [ti.i32, ti.i64, ti.f32, ti.f64] | ||
| test_dsize_list = [(4**i) * kibibyte | ||
| for i in range(1, 10)] #[4KB,16KB...256MB] | ||
| basic_repeat_times = 10 | ||
| evaluator = [geometric_mean] | ||
|
|
||
| def __init__(self, arch): | ||
| self.arch = arch | ||
| self.cases_impl = [] | ||
| for case in self.test_cases: | ||
| for dtype in self.test_dtype_list: | ||
| impl = CaseImpl(case, arch, dtype, self.test_dsize_list, | ||
| self.evaluator) | ||
| self.cases_impl.append(impl) | ||
|
|
||
| def run(self): | ||
| for arch in self.archs: | ||
| for dtype in self.data_type: | ||
| ti.init(kernel_profiler=True, arch=arch) | ||
| print("TestCase[%s.%s.%s]" % | ||
| (self.func.__name__, ti.core.arch_name(arch), | ||
| dtype2str[dtype])) | ||
| result = BenchmarkResult(self.name, arch, dtype, | ||
| self.data_size, results_evaluation) | ||
| for size in self.data_size: | ||
| print("data_size = %s" % (size2str(size))) | ||
| result.min_time_in_us.append( | ||
| self.func(arch, dtype, size, test_repeat)) | ||
| time.sleep(0.2) | ||
| self.benchmark_results.append(result) | ||
|
|
||
| def print(self): | ||
| i = 0 | ||
| for arch in self.archs: | ||
| for dtype in self.data_type: | ||
| for idx in range(len(self.data_size)): | ||
| print( | ||
| " test_case:[%s] arch:[%s] dtype:[%s] dsize:[%7s] >>> time:[%4.4f]" | ||
| % | ||
| (self.name, ti.core.arch_name(arch), dtype2str[dtype], | ||
| size2str(self.benchmark_results[i].data_size[idx]), | ||
| self.benchmark_results[i].min_time_in_us[idx])) | ||
| i = i + 1 | ||
| for case in self.cases_impl: | ||
| case.run() | ||
|
|
||
| def save2markdown(self, arch): | ||
| header = '|kernel elapsed time(ms)' + ''.join( | ||
| '|' for i in range(len(self.data_size) + len(results_evaluation))) | ||
| lines = [header] | ||
| for result in self.benchmark_results: | ||
| if (result.test_arch == arch): | ||
| lines.append(result.time2mdtableline()) | ||
| def get_markdown_lines(self): | ||
| lines = [] | ||
| lines += md_table_header(self.suite_name, self.arch, | ||
| self.test_dsize_list, self.basic_repeat_times, | ||
| self.evaluator) | ||
|
|
||
| result_header = '|kernel elapsed time(ms)' + ''.join( | ||
| '|' for i in range( | ||
| len(self.test_dsize_list) + len(MemoryBound.evaluator))) | ||
| lines += [result_header] | ||
| for case in self.cases_impl: | ||
| lines += case.get_markdown_lines() | ||
| lines.append('') | ||
| return lines | ||
|
|
||
|
|
||
| class Membound: | ||
| benchmark_imps = [] | ||
|
|
||
| def __init__(self): | ||
| for case in test_cases: | ||
| self.benchmark_imps.append( | ||
| BenchmarkImpl(case, test_archs, test_dtype, test_dsize)) | ||
| class CaseImpl: | ||
| def __init__(self, func, arch, test_dtype, test_dsize_list, evaluator): | ||
| self.func = func | ||
| self.name = func.__name__ | ||
| self.arch = arch | ||
| self.test_dtype = test_dtype | ||
| self.test_dsize_list = test_dsize_list | ||
| self.min_time_in_us = [] #test results | ||
| self.evaluator = evaluator | ||
|
|
||
| def run(self): | ||
| for case in self.benchmark_imps: | ||
| case.run() | ||
|
|
||
| def mdlines(self, arch): | ||
| lines = [] | ||
| lines += md_table_header(self.__class__.__name__, arch, test_dsize, | ||
| test_repeat, results_evaluation) | ||
| for case in self.benchmark_imps: | ||
| if arch in case.archs: | ||
| lines += case.save2markdown(arch) | ||
| else: | ||
| continue | ||
| lines.append('') | ||
| return lines | ||
| ti.init(kernel_profiler=True, arch=self.arch) | ||
| print("TestCase[%s.%s.%s]" % (self.func.__name__, arch_name( | ||
| self.arch), dtype2str[self.test_dtype])) | ||
| for test_dsize in self.test_dsize_list: | ||
| print("test_dsize = %s" % (size2str(test_dsize))) | ||
| self.min_time_in_us.append( | ||
| self.func(self.arch, self.test_dtype, test_dsize, | ||
| MemoryBound.basic_repeat_times)) | ||
| time.sleep(0.2) | ||
| ti.reset() | ||
|
|
||
| def get_markdown_lines(self): | ||
| string = '|' + self.name + '.' + dtype2str[self.test_dtype] + '|' | ||
| string += ''.join( | ||
| str(round(time, 4)) + '|' for time in self.min_time_in_us) | ||
| string += ''.join( | ||
| str(round(item(self.min_time_in_us), 4)) + '|' | ||
| for item in self.evaluator) | ||
| return [string] | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just for my own understanding ;/, why do we need sleep 0.2 here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was to give the device a cooling time, but here
sleep(0.2s)is quite arbitrary.Theoretically, we just need to make sure that this condition is consistent for each benchmark test.
Perhaps we can remove
sleep(0.2s)to avoid performance fluctuations caused by subsequent changes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, ok, good to know. Thanks. The time is probably HW and workload dependent, hard to quantify.