-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[Proton][Dialect] Middle-end support of the Proton Dialect and the frontend Python package #5677
Draft
fywkevin
wants to merge
11
commits into
triton-lang:proton-dev
Choose a base branch
from
fywkevin:fywkevin/proton_midend
base: proton-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b63050e
This PR contains 1. Proton dialect's mid-end compiler support (operat…
4bb5d01
fixed the lint problems.
82b7c5c
add intraprof package triton's __init__ export
41be536
change the import order
f29165b
test
d315afe
Merge branch 'main' into fywkevin/proton_midend
a3da426
use the new applyPatternsGreedily API
e592e67
remove the symbolic link
ee88dab
test
64506fc
test
9621d9c
test
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
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
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,9 @@ | ||
from .intra import ( | ||
init, | ||
finalize, | ||
activate, | ||
deactivate, | ||
set_alloc_state, | ||
) | ||
|
||
__all__ = ["init", "finalize", "activate", "deactivate", "set_alloc_state"] |
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,92 @@ | ||
from typing import Optional | ||
from dataclasses import dataclass | ||
import torch | ||
import triton | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Config: | ||
max_shared: int = 0 | ||
alloc_scratch: int = 262144 | ||
alignment: int = 128 | ||
|
||
|
||
@dataclass | ||
class State: | ||
grid_size: int = 1 | ||
alignment: int = 128 | ||
scratch_size: int = 0 | ||
config: Config = Config() | ||
stream: Optional[int] = None | ||
profile_mem_cpu: Optional[torch.Tensor] = None | ||
|
||
|
||
state: Optional[State] = None | ||
global_scratch_mem: Optional[torch.Tensor] = None | ||
activated: bool = False | ||
|
||
|
||
def set_alloc_state(global_scratch: torch.Tensor, grid_size: int, scratch_size: int, alignment: int, | ||
stream: Optional[int]): | ||
global state | ||
global global_scratch_mem | ||
global activated | ||
|
||
if not activated: | ||
return | ||
|
||
assert state, "profiler must be initialized" | ||
state.grid_size = grid_size | ||
state.scratch_size = scratch_size | ||
state.alignment = alignment | ||
state.stream = stream | ||
global_scratch_mem = global_scratch | ||
|
||
|
||
def init(config=dict()): | ||
global state | ||
global activated | ||
|
||
if not activated: | ||
return | ||
|
||
state = State() | ||
device = triton.runtime.driver.active.get_current_device() | ||
shared_mem = triton.runtime.driver.active.utils.get_device_properties(device)["max_shared_mem"] | ||
args = {'max_shared': shared_mem} | ||
args.update({k: config[k] for k in Config.__dataclass_fields__.keys() if k in config}) | ||
state.config = Config(**args) | ||
|
||
|
||
def finalize() -> Optional[State]: | ||
global state | ||
global global_scratch_mem | ||
global activated | ||
|
||
if not activated: | ||
return None | ||
|
||
assert state, "profiler must be initialized" | ||
curr_state = state | ||
size = curr_state.grid_size * curr_state.config.alloc_scratch | ||
# TODO(fywkevin): copy profiling data to profile_mem_cpu, the offset depends on the alignment | ||
curr_state.profile_mem_cpu = torch.empty(size, device="cpu", dtype=torch.int8) | ||
|
||
state = None | ||
global_scratch_mem = None | ||
return curr_state | ||
|
||
|
||
def _alloc_fn(size: int, alignment: int, stream: Optional[int]): | ||
return torch.empty(size, device="cuda", dtype=torch.int8) | ||
|
||
|
||
def activate(): | ||
global activated | ||
activated = True | ||
triton.set_allocator(_alloc_fn) | ||
|
||
|
||
def deactivate(): | ||
global activated | ||
activated = False |
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,9 @@ | ||
// RUN: triton-opt --split-input-file %s -proton-lowering="max-shared-mem=1024 scratch-mem=512 alignment=128" -verify-diagnostics | ||
|
||
module attributes {"ttg.num-warps" = 8 : i32, ttg.shared = 128 : i32} { | ||
// expected-error @+1 {{Global scratch memory for proton is not large enough}} | ||
tt.func @insufficient_global_scratch() { | ||
proton.record() {isStart = true, regionId = 1 : i32} | ||
tt.return | ||
} | ||
} // end module |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: triton-opt --split-input-file %s -proton-lowering="max-shared-mem=1024 scratch-mem=1024 alignment=32" | FileCheck %s | ||
|
||
// CHECK: module attributes{{.*}}ttg.global_scratch_memory_alignment = 32{{.*}}ttg.global_scratch_memory_size = 1024 | ||
module attributes {"ttg.num-warps" = 8 : i32, ttg.shared = 512 : i32} { | ||
// CHECK-LABEL: sufficient_global_scratch_size | ||
tt.func @sufficient_global_scratch_size() { | ||
proton.record() {isStart = true, regionId = 1 : i32} | ||
// CHECK: proton.finalize{{.*}}{size = 1024 : i32} | ||
// CHECK-NEXT: tt.return | ||
tt.return | ||
} | ||
} // end module | ||
|
||
// ----- | ||
|
||
// CHECK: module attributes{{.*}}ttg.global_scratch_memory_alignment = 128{{.*}}ttg.global_scratch_memory_size = 1280 | ||
module attributes {ttg.global_scratch_memory_alignment = 128, ttg.global_scratch_memory_size = 150, "ttg.num-warps" = 8 : i32, ttg.shared = 512 : i32} { | ||
// CHECK-LABEL: unalign_global_scratch_alloc | ||
tt.func @unalign_global_scratch_alloc() { | ||
proton.record() {isStart = true, regionId = 1 : i32} | ||
// CHECK: ttg.global_scratch_alloc {alignment = 128 : i32, nbytes = 1024 : i32, ttg.global_scratch_memory_offset = 256 : i32} | ||
// CHECK: proton.finalize{{.*}}{size = 1024 : i32} | ||
// CHECK-NEXT: tt.return | ||
tt.return | ||
} | ||
} // end module | ||
|
||
// ----- | ||
|
||
// CHECK: module attributes{{.*}}ttg.global_scratch_memory_alignment = 64{{.*}}ttg.global_scratch_memory_size = 1152 | ||
module attributes {ttg.global_scratch_memory_alignment = 64, ttg.global_scratch_memory_size = 128, "ttg.num-warps" = 8 : i32, ttg.shared = 512 : i32} { | ||
// CHECK-LABEL: align_global_scratch_alloc | ||
tt.func @align_global_scratch_alloc() { | ||
proton.record() {isStart = true, regionId = 1 : i32} | ||
// CHECK: %[[ARG0:.*]] = proton.init | ||
// CHECK-NEXT: %[[ARG1:.*]] = ttg.local_alloc | ||
// CHECK-NEXT: %[[ARG2:.*]] = ttg.global_scratch_alloc {alignment = 64 : i32, nbytes = 1024 : i32, ttg.global_scratch_memory_offset = 128 : i32} : !tt.ptr<i32> | ||
// CHECK-NEXT: proton.circular_record %[[ARG1]], %[[ARG0]] {isStart = true, regionId = 1 : i32} : !ttg.memdesc<128xi32, #shared, #smem, mutable>, !tt.ptr<i32> | ||
// CHECK-NEXT: proton.finalize %[[ARG1]], %[[ARG0]], %[[ARG2]] {size = 1024 : i32} : !ttg.memdesc<128xi32, #shared, #smem, mutable>, !tt.ptr<i32>, !tt.ptr<i32> | ||
// CHECK-NEXT: tt.return | ||
tt.return | ||
} | ||
} // end module |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(Dialect) | ||
add_subdirectory(Transforms) |
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.
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.
Yeah, it might be better to just add a new
profile_buffer
argument to the launch functionThere 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.
This way
global_scratch
can be used as it wasThere 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.
And we can profile kernels with both
global_scratch
andprofile_buffer
.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.
My initial thought was actually something like
But still it's not as clean as using an independent buffer