-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Unity] Dispatch cumsum and sort (#16254)
* [Unity] Add dispatch for scan and sort * add test cases * Use pass instead of pattern rewriter * add test case * fix lint * fix comments * fix lint * Add target context for default pipeline * fix tests * remove 'is_scheduled'
- Loading branch information
Showing
17 changed files
with
740 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file tvm/relax/attrs/sort.h | ||
* \brief Attributes for sorting operators. | ||
*/ | ||
#ifndef TVM_RELAX_ATTRS_SORT_H_ | ||
#define TVM_RELAX_ATTRS_SORT_H_ | ||
|
||
#include <tvm/relax/expr.h> | ||
#include <tvm/tir/index_map.h> | ||
|
||
namespace tvm { | ||
namespace relax { | ||
|
||
/*! \brief Attributes used in sort operator */ | ||
struct SortAttrs : public tvm::AttrsNode<SortAttrs> { | ||
int axis; | ||
bool descending; | ||
|
||
TVM_DECLARE_ATTRS(SortAttrs, "relax.attrs.SortAttrs") { | ||
TVM_ATTR_FIELD(axis).set_default(-1).describe( | ||
"Axis along which the sort is computed." | ||
"The default the last axis is used."); | ||
TVM_ATTR_FIELD(descending) | ||
.set_default(false) | ||
.describe( | ||
"Whether to sort in descending order." | ||
"If it is not specified, it defaults to the ascending order."); | ||
} | ||
}; // struct SortAttrs | ||
} // namespace relax | ||
} // namespace tvm | ||
|
||
#endif // TVM_RELAX_ATTRS_SORT_H_ |
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,102 @@ | ||
# 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. | ||
# pylint: disable=invalid-name, unused-argument, redefined-argument-from-local | ||
"""Dispatch sort and scan operators to platform dependent implementation.""" | ||
|
||
from tvm import topi | ||
from tvm.ir import Op | ||
from tvm.ir.module import IRModule | ||
from tvm.ir.transform import PassContext, module_pass | ||
from tvm.target import Target | ||
from tvm.contrib.thrust import can_use_thrust | ||
from tvm.relax import Expr, Function, Call, PyExprMutator, expr_functor, TensorStructInfo | ||
|
||
|
||
@expr_functor.mutator | ||
class SortScanDispatcher(PyExprMutator): | ||
""" | ||
Dispatcher to dispatch sort and scan. | ||
""" | ||
|
||
def __init__(self, mod): | ||
super().__init__(mod) | ||
|
||
def _get_target(self, expr: Expr) -> Target: | ||
sinfo = expr.struct_info | ||
# Get target information from TensorStructInfo | ||
if isinstance(sinfo, TensorStructInfo): | ||
vdevice = sinfo.vdevice | ||
if vdevice is not None: | ||
return vdevice.target | ||
# Return the target in current context | ||
target = Target.current() | ||
if target is None: | ||
raise ValueError( | ||
"Target not found. Please ensure that the target is annotated within the module, " | ||
"or alternatively, execute this within a specified target context." | ||
) | ||
return target | ||
|
||
def visit_call_(self, call: Call) -> Expr: | ||
if not isinstance(call.op, Op): | ||
return super().visit_call_(call) | ||
|
||
if call.op.name == "relax.sort": | ||
tgt = self._get_target(call) | ||
with tgt: | ||
if can_use_thrust(tgt, "tvm.contrib.thrust.sort"): | ||
return self.builder_.call_te( | ||
topi.cuda.sort_thrust, | ||
call.args[0], | ||
call.attrs.axis, | ||
not call.attrs.descending, | ||
) | ||
return self.builder_.call_te( | ||
topi.cuda.sort if tgt.kind.name == "cuda" else topi.sort, | ||
call.args[0], | ||
call.attrs.axis, | ||
not call.attrs.descending, | ||
) | ||
|
||
if call.op.name == "relax.cumsum": | ||
tgt = self._get_target(call) | ||
axis = int(call.attrs.axis) if call.attrs.axis is not None else call.attrs.axis | ||
with tgt: | ||
return self.builder_.call_te( | ||
topi.cuda.cumsum if tgt.kind.name == "cuda" else topi.cumsum, | ||
call.args[0], | ||
axis, | ||
call.attrs.dtype, | ||
) | ||
|
||
return super().visit_call_(call) | ||
|
||
|
||
@module_pass(opt_level=0, name="DispatchSortScan") | ||
class DispatchSortScan: | ||
""" | ||
Pass to dispatch scan and sort operators to platform dependent implementation. | ||
""" | ||
|
||
def transform_module(self, mod: IRModule, ctx: PassContext) -> IRModule: | ||
sort_scan_dispater = SortScanDispatcher(mod) | ||
for gv, func in mod.functions_items(): | ||
if isinstance(func, Function): | ||
func = sort_scan_dispater.visit_expr(func) | ||
sort_scan_dispater.builder_.update_func(gv, func) | ||
return sort_scan_dispater.builder_.get() |
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,45 @@ | ||
# 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. | ||
"""Sortings operators.""" | ||
|
||
from . import _ffi_api | ||
from ..expr import Expr | ||
|
||
|
||
def sort(x: Expr, axis: int = -1, descending: bool = False): | ||
"""Performs sorting along the given axis and returns an array | ||
in sorted order. | ||
Parameters | ||
---------- | ||
x : relax.Expr | ||
The input tensor. | ||
axis : int | ||
Axis along which to sort the input tensor. | ||
By default the last axis of the input is used. | ||
descending : bool | ||
Whether to sort in descending order, the default is False | ||
Returns | ||
------- | ||
out : relax.Expr | ||
Sorted tensor. | ||
""" | ||
return _ffi_api.sort(x, axis, descending) # type: ignore |
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
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,56 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file sort.cc | ||
* \brief sorting operators. | ||
*/ | ||
|
||
#include "sort.h" | ||
|
||
namespace tvm { | ||
namespace relax { | ||
|
||
/* relax.sort */ | ||
TVM_REGISTER_NODE_TYPE(SortAttrs); | ||
|
||
Expr sort(Expr data, int axis, bool descending) { | ||
auto attrs = make_object<SortAttrs>(); | ||
attrs->axis = std::move(axis); | ||
attrs->descending = std::move(descending); | ||
|
||
static const Op& op = Op::Get("relax.sort"); | ||
return Call(op, {std::move(data)}, Attrs{attrs}, {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relax.op.sort").set_body_typed(sort); | ||
|
||
StructInfo InferStructInfoSort(const Call& call, const BlockBuilder& ctx) { | ||
return GetUnaryInputTensorStructInfo(call, ctx); | ||
} | ||
|
||
TVM_REGISTER_OP("relax.sort") | ||
.set_attrs_type<SortAttrs>() | ||
.set_num_inputs(1) | ||
.add_argument("data", "Tensor", "The input tensor.") | ||
.set_attr<FInferStructInfo>("FInferStructInfo", InferStructInfoSort) | ||
.set_attr<Bool>("FPurity", Bool(true)); | ||
|
||
} // namespace relax | ||
} // namespace tvm |
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,49 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file sort.h | ||
* \brief The functions to make Relax tensor sorting operator calls. | ||
*/ | ||
#ifndef TVM_RELAX_OP_TENSOR_SORT_H_ | ||
#define TVM_RELAX_OP_TENSOR_SORT_H_ | ||
|
||
#include <tvm/relax/attrs/sort.h> | ||
|
||
#include <algorithm> | ||
#include <utility> | ||
|
||
#include "../op_common.h" | ||
|
||
namespace tvm { | ||
namespace relax { | ||
|
||
/*! | ||
* \brief Reverses the order of elements along given axis. | ||
* \param data The input tensor. | ||
* \param axis The axis to sort on. | ||
* \param descending Whether to sort in descending order. | ||
* \return The computed result. | ||
*/ | ||
Expr sort(Expr data, int axis, bool descending); | ||
|
||
} // namespace relax | ||
} // namespace tvm | ||
|
||
#endif // TVM_RELAX_OP_TENSOR_SORT_H_ |
Oops, something went wrong.