Skip to content
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
2 changes: 1 addition & 1 deletion docs/how_to/tutorials/customize_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def forward(self, x):


# Import cublas pattern
import tvm.relax.backend.contrib.cublas as _cublas
import tvm.relax.backend.cuda.cublas as _cublas


# Define a new pass for CUBLAS dispatch
Expand Down
21 changes: 1 addition & 20 deletions python/tvm/relax/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,7 @@
# under the License.
"""Relax backends"""

from tvm.target import Target

from . import contrib
from . import contrib, cpu_generic, cuda, gpu_generic, metal, rocm
from .dispatch_sampling import DispatchSampling
from .dispatch_sort_scan import DispatchSortScan
from .pattern_registry import get_pattern, get_patterns_with_prefix


def get_default_pipeline(target: Target):
"""Get the default Relax compilation pipeline for the given target."""
if target.kind.name == "cuda":
from . import cuda # pylint: disable=import-outside-toplevel

return cuda.get_default_pipeline(target)
if target.kind.name == "llvm":
from . import cpu_generic # pylint: disable=import-outside-toplevel

return cpu_generic.get_default_pipeline(target)
# Todo(tvm-team): support gpu-generic
raise ValueError(
f"Target {target} is not yet supported by default pipeline. "
"Please lower and build the IRModule manually."
)
23 changes: 23 additions & 0 deletions python/tvm/relax/backend/gpu_generic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""The Relax Metal backend compilation pipeline and other passes."""
from .pipeline import (
finalize_passes,
get_default_pipeline,
legalize_passes,
library_dispatch_passes,
)
87 changes: 87 additions & 0 deletions python/tvm/relax/backend/gpu_generic/pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""The Relax generic GPU backend compilation pipeline and other passes."""
import tvm
from tvm import dlight as dl
from tvm import relax


def library_dispatch_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default library dispatch passes for generic GPU backend."""
return [
relax.backend.DispatchSampling(),
relax.backend.DispatchSortScan(),
]


def legalize_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default legalization passes for generic GPU backend."""
return [
tvm.relax.transform.LegalizeOps(),
tvm.relax.transform.AnnotateTIROpPattern(),
tvm.relax.transform.FoldConstant(),
tvm.relax.transform.FuseOps(),
tvm.relax.transform.FuseTIR(),
dl.ApplyDefaultSchedule(
dl.gpu.Matmul(),
dl.gpu.GEMV(),
dl.gpu.Reduction(),
dl.gpu.GeneralReduction(),
dl.gpu.Fallback(),
),
]


def dataflow_lower_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default dataflow lowering passes for generic GPU backend."""
return [
relax.transform.RewriteDataflowReshape(),
relax.transform.ToNonDataflow(),
relax.transform.RemovePurityChecking(),
relax.transform.CallTIRRewrite(),
]


def finalize_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default finalization passes for generic GPU backend."""
return [
relax.transform.StaticPlanBlockMemory(),
relax.transform.LowerAllocTensor(),
relax.transform.KillAfterLastUse(),
relax.transform.LowerRuntimeBuiltin(),
relax.transform.ComputePrimValue(),
relax.transform.VMShapeLower(),
relax.transform.AttachGlobalSymbol(),
]


def get_default_pipeline(target: tvm.target.Target):
"""Return the default compilation pipeline for generic GPU."""

@tvm.transform.module_pass(opt_level=0)
def _pipeline(mod: tvm.ir.IRModule, _ctx: tvm.transform.PassContext):
with target:
seq = tvm.transform.Sequential(
library_dispatch_passes(target)
+ legalize_passes(target)
+ dataflow_lower_passes(target)
+ finalize_passes(target)
)
mod = seq(mod)
return mod

return _pipeline
17 changes: 17 additions & 0 deletions python/tvm/relax/backend/metal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""The Relax Metal backend compilation pipeline and other passes."""
23 changes: 23 additions & 0 deletions python/tvm/relax/backend/rocm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""The Relax ROCm backend compilation pipeline and other passes."""
from .pipeline import (
finalize_passes,
get_default_pipeline,
legalize_passes,
library_dispatch_passes,
)
87 changes: 87 additions & 0 deletions python/tvm/relax/backend/rocm/pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""The Relax ROCm backend compilation pipeline and other passes."""
import tvm
from tvm import dlight as dl
from tvm import relax


def library_dispatch_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default library dispatch passes for ROCm backend."""
return [
relax.backend.DispatchSampling(),
relax.backend.DispatchSortScan(),
]


def legalize_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default legalization passes for ROCm backend."""
return [
tvm.relax.transform.LegalizeOps(),
tvm.relax.transform.AnnotateTIROpPattern(),
tvm.relax.transform.FoldConstant(),
tvm.relax.transform.FuseOps(),
tvm.relax.transform.FuseTIR(),
dl.ApplyDefaultSchedule(
dl.gpu.Matmul(),
dl.gpu.GEMV(),
dl.gpu.Reduction(),
dl.gpu.GeneralReduction(),
dl.gpu.Fallback(),
),
]


def dataflow_lower_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default dataflow lowering passes for ROCm backend."""
return [
relax.transform.RewriteDataflowReshape(),
relax.transform.ToNonDataflow(),
relax.transform.RemovePurityChecking(),
relax.transform.CallTIRRewrite(),
]


def finalize_passes(target: tvm.target.Target): # pylint: disable=unused-argument
"""The default finalization passes for ROCm backend."""
return [
relax.transform.StaticPlanBlockMemory(),
relax.transform.LowerAllocTensor(),
relax.transform.KillAfterLastUse(),
relax.transform.LowerRuntimeBuiltin(),
relax.transform.ComputePrimValue(),
relax.transform.VMShapeLower(),
relax.transform.AttachGlobalSymbol(),
]


def get_default_pipeline(target: tvm.target.Target):
"""Return the default compilation pipeline for ROCm."""

@tvm.transform.module_pass(opt_level=0)
def _pipeline(mod: tvm.ir.IRModule, _ctx: tvm.transform.PassContext):
with target:
seq = tvm.transform.Sequential(
library_dispatch_passes(target)
+ legalize_passes(target)
+ dataflow_lower_passes(target)
+ finalize_passes(target)
)
mod = seq(mod)
return mod

return _pipeline
76 changes: 75 additions & 1 deletion python/tvm/relax/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
"""
# pylint: disable=unused-argument
from typing import Union

import tvm
from tvm import meta_schedule as ms

from . import transform, backend
from . import backend, transform


def zero_pipeline(*, enable_warning: bool = False):
Expand Down Expand Up @@ -237,3 +238,76 @@ def _register(func):
return func

return _register


def library_dispatch_passes(target: tvm.target.Target):
"""Get the default library dispatch passes for the given target."""
if target.kind.name == "cuda":
return backend.cuda.library_dispatch_passes(target)
if target.kind.name == "rocm":
return backend.rocm.library_dispatch_passes(target)
if target.kind.name == "metal":
return backend.gpu_generic.library_dispatch_passes(target)
if target.kind.name == "llvm":
return backend.cpu_generic.library_dispatch_passes(target)
# Todo(tvm-team): support gpu-generic
raise ValueError(f"Target {target} is not yet supported by library dispatch passes.")


def legalize_passes(target: tvm.target.Target):
"""Get the default legalization passes for the given target."""
if target.kind.name == "cuda":
return backend.cuda.legalize_passes(target)
if target.kind.name == "rocm":
return backend.rocm.legalize_passes(target)
if target.kind.name == "metal":
return backend.gpu_generic.legalize_passes(target)
if target.kind.name == "llvm":
return backend.cpu_generic.legalize_passes(target)
# Todo(tvm-team): support gpu-generic
raise ValueError(f"Target {target} is not yet supported by library dispatch passes.")


def dataflow_lower_passes(target: tvm.target.Target):
"""Get the default legalization passes for the given target."""
if target.kind.name == "cuda":
return backend.cuda.dataflow_lower_passes(target)
if target.kind.name == "rocm":
return backend.rocm.dataflow_lower_passes(target)
if target.kind.name == "metal":
return backend.gpu_generic.dataflow_lower_passes(target)
if target.kind.name == "llvm":
return backend.cpu_generic.dataflow_lower_passes(target)
# Todo(tvm-team): support gpu-generic
raise ValueError(f"Target {target} is not yet supported by dataflow lowering passes.")


def finalize_passes(target: tvm.target.Target):
"""Get the default legalization passes for the given target."""
if target.kind.name == "cuda":
return backend.cuda.finalize_passes(target)
if target.kind.name == "rocm":
return backend.rocm.finalize_passes(target)
if target.kind.name == "metal":
return backend.gpu_generic.finalize_passes(target)
if target.kind.name == "llvm":
return backend.cpu_generic.finalize_passes(target)
# Todo(tvm-team): support gpu-generic
raise ValueError(f"Target {target} is not yet supported by finalization passes.")


def get_default_pipeline(target: tvm.target.Target):
"""Get the default Relax compilation pipeline for the given target."""
if target.kind.name == "cuda":
return backend.cuda.get_default_pipeline(target)
if target.kind.name == "rocm":
return backend.rocm.get_default_pipeline(target)
if target.kind.name == "metal":
return backend.gpu_generic.get_default_pipeline(target)
if target.kind.name == "llvm":
return backend.cpu_generic.get_default_pipeline(target)
# Todo(tvm-team): support gpu-generic
raise ValueError(
f"Target {target} is not yet supported by default pipeline. "
"Please lower and build the IRModule manually."
)
2 changes: 1 addition & 1 deletion tests/python/relax/test_codegen_coreml.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _has_xcode():


def verify(mod, inputs):
from tvm.relax.backend.contrib.coreml import partition_for_coreml
from tvm.relax.backend.metal.coreml import partition_for_coreml

mod1 = partition_for_coreml(mod)
mod1 = relax.transform.RunCodegen()(mod1)
Expand Down
2 changes: 1 addition & 1 deletion tests/python/relax/test_codegen_cublas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import tvm.testing
import tvm.topi.testing
from tvm import relax
from tvm.relax.backend.contrib.cublas import partition_for_cublas
from tvm.relax.backend.cuda.cublas import partition_for_cublas
from tvm.relax.testing import get_relax_matmul_module
from tvm.script import relax as R
from tvm.script.ir_builder import IRBuilder
Expand Down
Loading
Loading