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 sort.cc
22 * \brief Sort operators
23 */
24#include <tvm/relay/attrs/algorithm.h>
25#include <tvm/relay/op.h>
26
27namespace tvm {
28namespace relay {
29
30bool SortRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
31 const TypeReporter& reporter) {
32 // `types` contains: [data, result]
33 ICHECK_EQ(types.size(), 2);
34 const auto* data = types[0].as<TensorTypeNode>();
35 if (data == nullptr) {
36 ICHECK(types[0].as<IncompleteTypeNode>())
37 << "Sort: expect input type to be TensorType but get " << types[0];
38 return false;
39 }
40 reporter->Assign(types[1], TensorType(data->shape, data->dtype));
41 return true;
42}
43
44Expr MakeSort(Expr data, int axis, bool is_ascend) {
45 auto attrs = make_object<ArgsortAttrs>();
46 attrs->axis = axis;
47 attrs->is_ascend = is_ascend;
48 static const Op& op = Op::Get("sort");
49 return Call(op, {data}, Attrs(attrs), {});
50}
51
52TVM_REGISTER_GLOBAL("relay.op._make.sort").set_body_typed(MakeSort);
53
54RELAY_REGISTER_OP("sort")
55 .describe(R"doc(Returns the indices that would sort an
56input array along the given axis.
57)doc" TVM_ADD_FILELINE)
58 .set_num_inputs(1)
59 .set_attrs_type<ArgsortAttrs>()
60 .add_argument("data", "Tensor", "Input data.")
61 .set_support_level(6)
62 .add_type_rel("Sort", SortRel);
63
64} // namespace relay
65} // namespace tvm
66