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