Skip to content
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

[Unity] Allocate workspace for all functions #15118

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/relax/transform/allocate_workspace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,17 @@ class WorkspaceProvider : ExprMutator {
builder_->GetContextIRModule()->Remove(GetRef<GlobalVar>(gvar));
}

auto gvar = mod_->GetGlobalVar("main");
auto func = Downcast<Function>(mod_->Lookup(gvar));
auto new_func = Function(func->params, VisitExpr(func->body), func->ret_struct_info,
func->is_pure, func->attrs);
builder_->UpdateFunction(gvar, new_func);
for (const auto& [gvar, f] : mod_->functions) {
workspace_var_main_ = Var();
if (!f->IsInstance<relax::FunctionNode>() || f->GetAttr<String>(attr::kCodegen) ||
f->GetAttr<String>(attr::kComposite)) {
continue;
}
auto func = Downcast<Function>(mod_->Lookup(gvar));
auto new_func = Function(func->params, VisitExpr(func->body), func->ret_struct_info,
func->is_pure, func->attrs);
builder_->UpdateFunction(gvar, new_func);
}
return builder_->GetContextIRModule();
}

Expand Down
36 changes: 34 additions & 2 deletions tests/python/relax/test_transform_allocate_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def gv(
return gv1

@R.function
def main(
def entry_a(
q: R.Tensor((32, 8, 16, 8), dtype="float16"),
k: R.Tensor((32, 8, 16, 8), dtype="float16"),
v: R.Tensor((32, 8, 16, 8), dtype="float16"),
Expand All @@ -68,6 +68,20 @@ def main(
R.output(gv)
return gv

@R.function
def entry_b(
q: R.Tensor((32, 8, 16, 8), dtype="float16"),
k: R.Tensor((32, 8, 16, 8), dtype="float16"),
v: R.Tensor((32, 8, 16, 8), dtype="float16"),
) -> R.Tensor((32, 8, 16, 8), dtype="float16"):
cls = Module
with R.dataflow():
gv: R.Tensor((32, 8, 16, 8), dtype="float16") = cls.fused_relax_nn_attention_cutlass(
q, k, v
) + R.const(1, dtype="float16")
R.output(gv)
return gv


@I.ir_module
class Expected:
Expand Down Expand Up @@ -105,7 +119,7 @@ def gv(
return gv1

@R.function
def main(
def entry_a(
q: R.Tensor((32, 8, 16, 8), dtype="float16"),
k: R.Tensor((32, 8, 16, 8), dtype="float16"),
v: R.Tensor((32, 8, 16, 8), dtype="float16"),
Expand All @@ -122,6 +136,24 @@ def main(
R.output(gv)
return gv

@R.function
def entry_b(
q: R.Tensor((32, 8, 16, 8), dtype="float16"),
k: R.Tensor((32, 8, 16, 8), dtype="float16"),
v: R.Tensor((32, 8, 16, 8), dtype="float16"),
) -> R.Tensor((32, 8, 16, 8), dtype="float16"):
cls = Expected
with R.dataflow():
lv: R.Object = R.vm.alloc_storage(R.shape([65536]), R.prim_value(0), R.dtype("uint8"))
workspace_main: R.Tensor((65536,), dtype="uint8") = R.vm.alloc_tensor(
lv, R.prim_value(0), R.shape([65536]), R.dtype("uint8")
)
gv: R.Tensor((32, 8, 16, 8), dtype="float16") = cls.fused_relax_nn_attention_cutlass1(
q, k, v, workspace_main
) + R.const(1, dtype="float16")
R.output(gv)
return gv


def test_single_attention():
rewritten = relax.transform.AllocateWorkspace()(Module)
Expand Down