-
Notifications
You must be signed in to change notification settings - Fork 273
Autotuner for int mm Triton kernels #41
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
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
ba24af7
int_mm benchmarks
cpuhrsch 1f5a811
Baseline results
cpuhrsch b8bcf27
Running shapes over night
cpuhrsch 44c97a6
Rerun result 1 with empty cache
cpuhrsch 579fc5e
Filter results_2
cpuhrsch 8251780
Triton matmul
cpuhrsch 434b92f
Store best configs
cpuhrsch e3dcf6d
Faster autotuner
cpuhrsch cacef6b
Inject intmm triton
cpuhrsch 0f2a706
Scaled int mm
cpuhrsch 395b93c
More scaled matmul
cpuhrsch f2bf0fd
evict_last
cpuhrsch ea635ed
Only 1 scales and better Triton code
cpuhrsch ffd0d66
a100 specific configs based on all configs
cpuhrsch 75ef774
data pkl based on regular configs
cpuhrsch 404814f
Enable all configs
cpuhrsch 145a498
Move configs to torchao
cpuhrsch 016a839
Read configs from library package
cpuhrsch 36bfe59
More environment variables
cpuhrsch 62bc110
Benchmark for sam shapes
cpuhrsch 8ce5707
More A100 configs
cpuhrsch 2797cf3
Revert quant primitives
cpuhrsch 73f6671
Merge remote-tracking branch 'origin/main' into intmmbenchmarks1
cpuhrsch 20333cf
Make autotuner work with compile
cpuhrsch 3f6ddb4
Merge branch 'main' of github.com:pytorch-labs/ao into intmmbenchmarks1
cpuhrsch c846b70
Make benchmark output a bit more clear
cpuhrsch 8f4d6cc
Make benchmark output a bit more clear
cpuhrsch 54976d4
Merge branch 'main' of github.com:pytorch-labs/ao into intmmbenchmarks1
cpuhrsch e590bce
Basic test harness
cpuhrsch b376dae
Basic test harness
cpuhrsch 171c491
scaled int mm tests
cpuhrsch 5437476
dev requirements
cpuhrsch ef26555
Address comments
cpuhrsch f4e6b7f
Merge branch 'main' of github.com:pytorch-labs/ao into intmmbenchmarks1
cpuhrsch bc6d23f
lintrunner.toml
cpuhrsch 9c45710
Much lint, so wow
cpuhrsch b4ddebc
Merge branch 'main' of github.com:pytorch-labs/ao into intmmbenchmarks1
cpuhrsch 771fadd
Merge branch 'main' of github.com:pytorch-labs/ao into intmmbenchmarks1
cpuhrsch 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
merge_base_with = "origin/main" | ||
|
||
[[linter]] | ||
code = 'FLAKE8' | ||
include_patterns = ['**/*.py'] | ||
exclude_patterns = [ | ||
'third-party/**', | ||
'**/third-party/**', | ||
] | ||
command = [ | ||
'python', | ||
'-m', | ||
'lintrunner_adapters', | ||
'run', | ||
'flake8_linter', | ||
'--', | ||
'@{{PATHSFILE}}' | ||
] | ||
init_command = [ | ||
'python', | ||
'-m', | ||
'lintrunner_adapters', | ||
'run', | ||
'pip_init', | ||
'--dry-run={{DRYRUN}}', | ||
'--requirement=requirements-lintrunner.txt', | ||
] | ||
|
||
# Black + usort | ||
[[linter]] | ||
code = 'UFMT' | ||
include_patterns = [ | ||
'**/*.py', | ||
'**/*.pyi', | ||
] | ||
exclude_patterns = [ | ||
'third-party/**', | ||
'**/third-party/**', | ||
] | ||
command = [ | ||
'python', | ||
'-m', | ||
'lintrunner_adapters', | ||
'run', | ||
'ufmt_linter', | ||
'--', | ||
'@{{PATHSFILE}}' | ||
] | ||
init_command = [ | ||
'python', | ||
'-m', | ||
'lintrunner_adapters', | ||
'run', | ||
'pip_init', | ||
'--dry-run={{DRYRUN}}', | ||
'--no-black-binary', | ||
'--requirement=requirements-lintrunner.txt', | ||
] | ||
is_formatter = true |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import argparse | ||
import csv | ||
import itertools | ||
import math | ||
import pathlib | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
import torch.utils.benchmark as benchmark | ||
from torchao.kernel.intmm_triton import int_matmul, int_scaled_matmul | ||
|
||
torch._dynamo.config.cache_size_limit = 128 | ||
torch._dynamo.config.accumulated_cache_size_limit = 128 | ||
|
||
dtype = torch.float16 | ||
device = "cuda" | ||
|
||
|
||
def benchmark_in_ms(warmup, iters, f, *args, **kwargs): | ||
for _ in range(warmup): | ||
f(*args, **kwargs) | ||
torch.cuda.synchronize() | ||
start_event = torch.cuda.Event(enable_timing=True) | ||
end_event = torch.cuda.Event(enable_timing=True) | ||
start_event.record() | ||
|
||
for _ in range(iters): | ||
f(*args, **kwargs) | ||
|
||
end_event.record() | ||
torch.cuda.synchronize() | ||
return start_event.elapsed_time(end_event) / float(iters) | ||
|
||
|
||
@torch.compile(mode="max-autotune") | ||
def compiled_mm(x, w): | ||
return torch.mm(x, w) | ||
|
||
|
||
@torch.compile(mode="max-autotune") | ||
def compiled_int_mm(x, w): | ||
return torch._int_mm(x, w) | ||
|
||
|
||
def run_int_mm_benchmark(x, w, b): | ||
fp_time = benchmark_in_ms(10, 100, torch.mm, x, w) | ||
x_int = x.to(dtype=torch.int8) | ||
w_int = w.to(dtype=torch.int8) | ||
int_mm_time = benchmark_in_ms(10, 100, int_matmul, x_int, w_int) | ||
return fp_time, int_mm_time | ||
|
||
|
||
def run_int_scaled_mm_benchmark(x, w, b): | ||
scales = x.sum(-1, keepdim=True) | ||
fp_time = benchmark_in_ms(10, 100, lambda x, w, s: torch.mm(x, w) * s, x, w, scales) | ||
x_int = x.to(dtype=torch.int8) | ||
w_int = w.to(dtype=torch.int8) | ||
int_scaled_mm_time = benchmark_in_ms( | ||
10, 100, int_scaled_matmul, x_int, w_int, scales | ||
) | ||
return fp_time, int_scaled_mm_time | ||
|
||
|
||
def run_benchmarks(shapes): | ||
print("fn,m,k,n,fp_time,int_mm_time,ratio") | ||
positives = [] | ||
dtype = torch.bfloat16 | ||
device = "cuda" | ||
for fn, (m, k, n) in itertools.product( | ||
[run_int_mm_benchmark, run_int_scaled_mm_benchmark], shapes | ||
): | ||
x = torch.randn(m, k, dtype=dtype, device=device) | ||
w = torch.randn(n, k, dtype=dtype, device=device).t() | ||
b = torch.randn(m, n, dtype=dtype, device=device) | ||
|
||
fp_time, int_mm_time = fn(x, w, b) | ||
ratio = fp_time / int_mm_time | ||
result = ",".join(map(str, [fn, m, k, n, fp_time, int_mm_time, ratio])) | ||
print(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="integer matmul benchmarks") | ||
parser.add_argument("file_path", type=str, help="Path to csv file with shapes") | ||
args = parser.parse_args() | ||
# Access the file path provided as an argument | ||
file_path = args.file_path | ||
file_path = pathlib.Path(file_path) | ||
assert file_path.is_file() | ||
|
||
# Format is (m, k, n) | ||
shapes = list(csv.reader(open(file_path, "r")))[1:] | ||
# Turn into list of int tuples | ||
shapes = list(map(lambda x: tuple(map(int, x)), shapes)) | ||
|
||
run_benchmarks(shapes) |
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
m,k,n | ||
1024,1024,2304 | ||
cpuhrsch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1024,1024,4608 | ||
1024,8192,2304 | ||
1024,8192,4608 | ||
1152,1024,2048 | ||
1152,2048,16384 | ||
1152,2048,2048 | ||
1152,3072,2048 | ||
1152,4096,2048 | ||
1152,8192,2048 | ||
1,2048,1024 | ||
1,2048,2048 | ||
1,2048,4096 | ||
144,2048,16384 | ||
144,2048,2048 | ||
144,4096,2048 | ||
144,8192,2048 | ||
1472,1024,154 | ||
1472,1024,308 | ||
1472,2048,154 | ||
1472,2048,308 | ||
1472,512,154 | ||
1472,512,308 | ||
1,512,2048 | ||
154,1472,1024 | ||
154,1472,2048 | ||
154,1472,512 | ||
18432,1024,512 | ||
18432,1536,512 | ||
18432,2048,512 | ||
18432,512,4096 | ||
18432,512,512 | ||
2048,1024,1 | ||
2048,1024,2 | ||
2048,16384,1152 | ||
2048,16384,144 | ||
2048,16384,288 | ||
2048,16384,576 | ||
2048,2048,1 | ||
2048,2048,1152 | ||
2048,2048,144 | ||
2048,2048,2 | ||
2048,2048,288 | ||
2048,2048,576 | ||
2048,4096,1 | ||
2048,4096,2 | ||
2048,512,18432 | ||
2048,512,9216 | ||
2,2048,1024 | ||
2,2048,2048 | ||
2,2048,4096 | ||
2304,1024,1024 | ||
2304,1024,8192 | ||
2304,1536,1024 | ||
2304,2048,1024 | ||
2304,3072,1024 | ||
2304,4096,1024 | ||
2304,512,1024 | ||
231,4096,1024 | ||
231,4096,2048 | ||
231,4096,512 | ||
231,768,1024 | ||
231,768,2048 | ||
231,768,512 | ||
2,512,2048 | ||
288,2048,16384 | ||
288,2048,2048 | ||
288,4096,2048 | ||
288,8192,2048 | ||
308,1472,1024 | ||
308,1472,2048 | ||
308,1472,512 | ||
4096,1024,2304 | ||
4096,1024,231 | ||
4096,1024,4608 | ||
4096,1024,462 | ||
4096,2048,231 | ||
4096,2048,462 | ||
4096,512,231 | ||
4096,512,462 | ||
4608,1024,1024 | ||
4608,1024,8192 | ||
4608,1536,1024 | ||
4608,2048,1024 | ||
4608,3072,1024 | ||
4608,4096,1024 | ||
4608,512,1024 | ||
462,4096,1024 | ||
462,4096,2048 | ||
462,4096,512 | ||
462,768,1024 | ||
462,768,2048 | ||
462,768,512 | ||
512,2048,1 | ||
512,2048,2 | ||
512,4096,18432 | ||
512,4096,9216 | ||
512,512,18432 | ||
512,512,9216 | ||
576,1024,2048 | ||
576,2048,16384 | ||
576,2048,2048 | ||
576,3072,2048 | ||
576,4096,2048 | ||
576,8192,2048 | ||
768,1024,231 | ||
768,1024,462 | ||
768,2048,231 | ||
768,2048,462 | ||
768,512,231 | ||
768,512,462 | ||
8192,2048,1152 | ||
8192,2048,144 | ||
8192,2048,288 | ||
8192,2048,576 | ||
9216,1024,512 | ||
9216,1536,512 | ||
9216,2048,512 | ||
9216,512,4096 | ||
9216,512,512 | ||
32768,3072,768 | ||
32768,768,2304 | ||
32768,768,3072 | ||
32768,768,768 | ||
39200,768,2304 | ||
39200,768,768 |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import torchao | ||
|
||
from torchao.kernel import autotuner | ||
|
||
configs = autotuner._load_best_configs() | ||
|
||
print("m,k,n") | ||
for k, v in configs.items(): | ||
a_shape = k[1] | ||
b_shape = k[4] | ||
M, K0 = a_shape | ||
K1, N = b_shape | ||
|
||
assert K0 == K1 | ||
|
||
print(f"{M},{K0},{N}") |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
m,k,n | ||
32768,3072,768 | ||
32768,768,2304 | ||
32768,768,3072 | ||
32768,768,768 | ||
39200,768,2304 | ||
39200,768,768 |
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,3 +1,4 @@ | ||
pytest | ||
expecttest | ||
packaging | ||
parameterized | ||
packaging |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Lintrunner itself | ||
lintrunner==0.11.0 | ||
lintrunner-adapters==0.11.0 | ||
|
||
# Flake 8 and its dependencies | ||
flake8==6.0.0 | ||
flake8-breakpoint==1.1.0 | ||
flake8-bugbear==23.6.5 | ||
flake8-comprehensions==3.12.0 | ||
flake8-pyi==23.5.0 | ||
mccabe==0.7.0 | ||
pycodestyle==2.10.0 | ||
torchfix==0.1.1 | ||
|
||
# UFMT | ||
black==24.2.0 | ||
ufmt==2.5.1 | ||
usort==1.0.5 | ||
|
||
# Other linters | ||
clang-format==12.0.1 | ||
cmakelint==1.4.1 |
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
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.
put this in benchmark util instead?
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.
Once I add the next benchmark for weight only