-
Notifications
You must be signed in to change notification settings - Fork 19
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
adding benchmarking #3
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d2814f1
adding benchmarking
6bddde4
benchmarking initial suite complete
233e857
comments
c328334
removing testing error
adamomainz 10bc4a6
removing commented code
adamomainz 10a6e68
fixing one bug
adamomainz cbc8f30
changing math_ops name and making a few edits
adamomainz d16d667
fixing black again
adamomainz 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 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 |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
__pycache__/ | ||
*.py[cod] | ||
.pytest_cache | ||
**/.cache | ||
**/meta-llama/**/* |
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,2 @@ | ||
from .profiler import Profiler | ||
from .benchmark_utils import compare_benchmarks |
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,24 @@ | ||
from typing import Any, Dict | ||
import pandas as pd | ||
|
||
|
||
def compare_benchmarks(benchmarks: Dict[str, Dict[str, Any]]) -> Dict[str, Any]: | ||
series_dict = {k: pd.Series(v.values()) for k, v in benchmarks.items()} | ||
series_dict["kernel_path"] = pd.Series(benchmarks[list(benchmarks.keys())[0]].keys()) | ||
series_dict["kernel"] = pd.Series([k.split(".")[-1] for k in series_dict["kernel_path"]]) | ||
df = pd.DataFrame() | ||
|
||
for k, v in series_dict.items(): | ||
df[k] = v | ||
columns = [c for c in df.columns if not "kernel" in c] | ||
for i in range(len(columns)): | ||
for j in range(i+1, len(columns)): | ||
# calculate the difference between the two columns | ||
diff_col_name = f"{columns[i]}-{columns[j]}" | ||
df[diff_col_name] = df[columns[i]] - df[columns[j]] | ||
df.sort_values(by = 'kernel_path', inplace = True) | ||
columns = [c for c in df.columns if not "kernel" in c] | ||
columns = ["kernel", "kernel_path"] + columns | ||
df = df[columns] | ||
df.set_index("kernel", inplace=True) | ||
return df |
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,65 @@ | ||
import torch | ||
import contextlib | ||
import time | ||
from collections import defaultdict | ||
|
||
|
||
class Profiler: | ||
_instance = None | ||
|
||
def __new__(cls, should_profile: bool = False, benchmark: bool = False): | ||
if cls._instance is None: | ||
cls._instance = super().__new__(cls) | ||
cls._instance.profiler = torch.profiler.profile(record_shapes=True, with_flops=True, profile_memory=True, with_stack=True, with_modules=True) if should_profile else None | ||
cls._instance.benchmark = benchmark | ||
cls._instance.benchmark_vals = defaultdict(list) | ||
cls._instance.function_stack = [] | ||
|
||
return cls._instance | ||
|
||
@classmethod | ||
def reset(cls): | ||
cls._instance = None | ||
|
||
@classmethod | ||
def profiling_decorator(cls, record_name: str = None, skip_profiling: bool = False, skip_benchmark: bool = False): | ||
def inner(func): | ||
def wrapper(*args, **kwargs): | ||
if not cls._instance or (skip_profiling and skip_benchmark): | ||
return func(*args, **kwargs) | ||
cls._instance.function_stack.append(record_name or func.__name__) | ||
name = ".".join(cls._instance.function_stack) | ||
if cls._instance.profiler and not skip_profiling: | ||
cls._instance.profiler.start() | ||
start_time = time.perf_counter() | ||
|
||
with torch.profiler.record_function(name): | ||
result = func(*args, **kwargs) | ||
|
||
end_time = time.perf_counter() | ||
if cls._instance.benchmark and not skip_benchmark: | ||
cls._instance.benchmark_vals[name].append(end_time - start_time) | ||
if cls._instance.profiler and not skip_profiling: | ||
cls._instance.profiler.stop() | ||
cls._instance.function_stack.pop() | ||
return result | ||
return wrapper | ||
return inner | ||
|
||
@classmethod | ||
def step(cls): | ||
if cls._instance and cls._instance.profiler: | ||
cls._instance.profiler.step() | ||
|
||
@classmethod | ||
def get_benchmark_vals(cls): | ||
if cls._instance and cls._instance.benchmark: | ||
return {k: sum(v) / len(v) for k, v in cls._instance.benchmark_vals.items()} | ||
return None | ||
|
||
@classmethod | ||
def get_profiling_data(cls): | ||
if cls._instance and cls._instance.profiler: | ||
return self.profiler.key_averages() | ||
return None | ||
|
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
Oops, something went wrong.
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.
perf_counter
isn't the right way to measure GPU time. Usetorch.cuda.Event
instead:https://discuss.pytorch.org/t/how-to-measure-time-in-pytorch/26964
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.
#5