Skip to content

Commit 4be951d

Browse files
authored
[RELAX][PASS] Annotate Custom Scope layout pass for Adreno GPU (apache#17599)
This PR adds custom scope layout passes for Andreno GPU https://discuss.tvm.apache.org/t/rfc-annotate-custom-scope-layout-relax-pass-for-adreno-gpu/18052/6 for details about texture scope handling.
1 parent 13ea9dc commit 4be951d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+4250
-62
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ tvm_file_glob(GLOB_RECURSE COMPILER_SRCS
307307
src/relax/analysis/*.cc
308308
src/relax/transform/*.cc
309309
src/relax/backend/vm/*.cc
310+
src/relax/backend/adreno/*.cc
310311
src/relax/backend/task_extraction.cc
311312
src/relax/backend/pattern_registry.cc
312313
src/relax/utils.cc

include/tvm/relax/attrs/op.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ struct ToVDeviceAttrs : public AttrsNodeReflAdapter<ToVDeviceAttrs> {
104104
struct HintOnDeviceAttrs : public AttrsNodeReflAdapter<HintOnDeviceAttrs> {
105105
int32_t device_type;
106106
int32_t index;
107+
MemoryScope memory_scope;
107108

108109
static void RegisterReflection() {
109110
namespace refl = tvm::ffi::reflection;
110111
refl::ObjectDef<HintOnDeviceAttrs>()
111112
.def_ro("device_type", &HintOnDeviceAttrs::device_type,
112113
"The device type where the data is supposed to be executed.")
113-
.def_ro("index", &HintOnDeviceAttrs::index, "The device id.");
114+
.def_ro("index", &HintOnDeviceAttrs::index, "The device id.")
115+
.def_ro("memory_scope", &HintOnDeviceAttrs::memory_scope, "The device memory scope.");
114116
}
115117
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.attrs.HintOnDeviceAttrs", HintOnDeviceAttrs,
116118
BaseAttrsNode);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file tvm/relax/backend/adreno/transform.h
22+
* \brief Adreno GPU specific transformation passes.
23+
*/
24+
#ifndef TVM_RELAX_BACKEND_ADRENO_TRANSFORM_H_
25+
#define TVM_RELAX_BACKEND_ADRENO_TRANSFORM_H_
26+
27+
#include <tvm/relax/expr.h>
28+
#include <tvm/relax/transform.h>
29+
namespace tvm {
30+
namespace relax {
31+
namespace backend {
32+
namespace adreno {
33+
namespace transform {
34+
35+
using Pass = tvm::transform::Pass;
36+
using PassInfo = tvm::transform::PassInfo;
37+
using PassContext = tvm::transform::PassContext;
38+
using Function = tvm::relax::Function;
39+
using DataflowBlock = tvm::relax::DataflowBlock;
40+
using tvm::relax::transform::CreateFunctionPass;
41+
using tvm::transform::CreateModulePass;
42+
43+
/*!
44+
* \brief This pass is designed to annotate the memory scope information via VDevice attribute.
45+
* This pass need operator attrbutes which in general vanish aftre legalization.
46+
* FuseOps and FuseTIR are modified to pass on the operator specific attributes and also
47+
* op_pattern details as part of the PrimFunc. This pass is Adreno specific and annotates each
48+
* BindingVar with appropriate HintInDevice. RealizeVDevice pass followed by handles these hints.
49+
* Followed by this pass we also invoke SpecializePrimFuncBasedOnCallSite which updates the
50+
* var_buffer_map based on this new VDevice information.
51+
*/
52+
TVM_DLL Pass AnnotateCustomMemoryScope(Target target);
53+
54+
/*
55+
* \brief This is a texture specific pass that can optimize unnecessary to_device copies.
56+
* Like texture_scope -> ToVDevice -> global scope. In this case the producer can directly
57+
* store into global scope avoiding unnecessary device copy.
58+
*/
59+
TVM_DLL Pass FoldVDeviceScopeChange();
60+
61+
} // namespace transform
62+
} // namespace adreno
63+
} // namespace backend
64+
} // namespace relax
65+
} // namespace tvm
66+
67+
#endif // TVM_RELAX_BACKEND_ADRENO_TRANSFORM_H_

include/tvm/relax/expr.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,13 @@ class CallNode : public ExprNode {
156156

157157
/*!
158158
* \brief The structure info arguments of a CallNode.
159-
* sinfo_args is designed to be non-empty only for intrinsic op (e.g.,
159+
* sinfo_args is by default designed to be non-empty only for intrinsic op (e.g.,
160160
* call_tir, call_builtin_with_ctx, etc.) and calls to ExternFuncs, with the main
161161
* usage of structure info inference.
162+
*
163+
* Regular ops also at times may have sinfo_args defined to specialize partial
164+
* or complete structure info. Like VDevice customization with mixed input memory_scopes.
165+
* The customized pass can set this info and operator specific inference will respect it.
162166
*/
163167
ffi::Array<StructInfo> sinfo_args;
164168

include/tvm/relax/transform.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,13 @@ TVM_DLL Pass FoldConstant();
245245
*
246246
* \param cmap The customized operator legalization function map. The customized function
247247
* will override the default one.
248+
* \param skip_ops The list operator names which need to be skipped from legalization
248249
* \param enable_warning A boolean value indicating if to print warnings for TIR functions not
249250
* showing up in the database.
250251
* \return The Pass.
251252
*/
252253
TVM_DLL Pass LegalizeOps(ffi::Optional<ffi::Map<ffi::String, ffi::Function>> cmap,
254+
ffi::Optional<ffi::Array<ffi::String>> skip_ops,
253255
bool enable_warning = false);
254256

255257
/*!
@@ -680,6 +682,13 @@ TVM_DLL Pass RewriteCUDAGraph();
680682
*/
681683
TVM_DLL Pass FewShotTuning(int valid_count, bool benchmark);
682684

685+
/*!
686+
* \brief This pass updates the var_buffer mapping of PrimFunctions from the call_tir info.
687+
* Primarily used to update the VDevice information if any changes occured from the caller.
688+
* This pass recreates the buffers and updates the map.
689+
*/
690+
TVM_DLL Pass SpecializePrimFuncBasedOnCallSite();
691+
683692
} // namespace transform
684693
} // namespace relax
685694
} // namespace tvm

include/tvm/runtime/tensor.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ class Tensor : public tvm::ffi::Tensor {
178178
*/
179179
TVM_DLL static void CopyToBytes(const DLTensor* from, void* to, size_t nbytes,
180180
TVMStreamHandle stream = nullptr);
181+
182+
/*!
183+
* \brief Function to copy data from one array to a byte buffer.
184+
* \param from The source array.
185+
* \param to The target byte buffer.
186+
* \param nbytes The size of the data buffer.
187+
* \param stream The stream used in copy.
188+
*/
189+
TVM_DLL static void CopyFromBytes(const DLTensor* to, void* from, size_t nbytes,
190+
TVMStreamHandle stream = nullptr);
181191
};
182192

183193
/*!

python/tvm/dlight/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
"""DLight package provides efficient schedules out-of-box for deep learning workloads."""
1818
from . import gpu
19+
from . import adreno
1920
from . import cpu
2021
from .analysis import (
2122
BlockInfo,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
"""
18+
Adreno schedule rules.
19+
"""
20+
from .convolution import Conv2d

python/tvm/dlight/adreno/base.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
"""Base schedule rule for Adreno operators."""
18+
19+
from tvm.target import Target
20+
21+
from ..base import ScheduleRule
22+
23+
24+
class AdrenoScheduleRule(ScheduleRule): # pylint: disable=too-few-public-methods
25+
"""The Schedule Rule specific to Adreno targets,
26+
will return None if the target is not Adreno."""
27+
28+
def is_target_available(self, target: Target) -> bool:
29+
"""Check whether the target is available for Adreno rule.
30+
31+
Parameters
32+
----------
33+
target : Target
34+
The compilation target to check.
35+
36+
Returns
37+
-------
38+
available : bool
39+
Whether the target is available for this rule.
40+
"""
41+
return super().is_target_available(target) and "adreno" in target.keys

0 commit comments

Comments
 (0)