-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Unity] Dispatch cumsum and sort #16254
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4f069f6
[Unity] Add dispatch for scan and sort
yongwww 4b58fae
add test cases
yongwww 26a5466
Use pass instead of pattern rewriter
yongwww d271252
add test case
yongwww 001f0c9
fix lint
yongwww 1e8cad2
fix comments
yongwww 4896b66
fix lint
yongwww 4b16138
Add target context for default pipeline
yongwww 4dc2e9c
fix tests
yongwww 964f2b7
remove 'is_scheduled'
yongwww 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.