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 binary.cc
22  * \brief binary broadcast operators.
23  */
24 #include <tvm/relay/expr.h>
25 #include <tvm/relay/op.h>
26 #include <topi/broadcast.h>
27 #include "../type_relations.h"
28 #include "../op_common.h"
29 
30 namespace tvm {
31 namespace relay {
32 
33 #define RELAY_BINARY_COMPUTE(FTOPI)                        \
34   [] (const Attrs& attrs,                                  \
35       const Array<Tensor>& inputs,                         \
36       const Type& out_type,                                \
37       const Target& target) -> Array<Tensor> {             \
38     CHECK_EQ(inputs.size(), 2U);                           \
39     return {FTOPI(inputs[0], inputs[1])};                  \
40   }                                                        \
41 
42 // Addition
43 RELAY_REGISTER_BINARY_OP("add")
44 .describe("Elementwise add with with broadcasting")
45 .set_support_level(1)
46 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::add));
47 
48 // Subtraction
49 RELAY_REGISTER_BINARY_OP("subtract")
50 .describe("Elementwise substract with broadcasting")
51 .set_support_level(1)
52 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::subtract));
53 
54 // Right shift
55 RELAY_REGISTER_BINARY_OP("right_shift")
56 .describe("Elementwise right shift with broadcasting")
57 .set_support_level(4)
58 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::right_shift));
59 
60 
61 RELAY_REGISTER_BINARY_OP("left_shift")
62 .describe("Elementwise left shift with broadcasting")
63 .set_support_level(4)
64 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::left_shift));
65 
66 
67 RELAY_REGISTER_BINARY_OP("maximum")
68 .describe("Elementwise maximum of two tensors with broadcasting")
69 .set_support_level(4)
70 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::maximum));
71 
72 
73 RELAY_REGISTER_BINARY_OP("minimum")
74 .describe("Elementwise minimum of two tensors with broadcasting")
75 .set_support_level(4)
76 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::minimum));
77 
78 
79 RELAY_REGISTER_BINARY_OP("divide")
80 .describe("Elementwise divide with broadcasting")
81 .set_support_level(1)
82 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::divide));
83 
84 
85 RELAY_REGISTER_BINARY_OP("floor_divide")
86 .describe("Elementwise floor divide with broadcasting")
87 .set_support_level(1)
88 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::floor_divide));
89 
90 
91 RELAY_REGISTER_BINARY_OP("multiply")
92 .describe("Elementwise multiply with broadcasting")
93 .set_support_level(1)
94 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::multiply));
95 
96 
97 RELAY_REGISTER_BINARY_OP("power")
98 .describe("Elementwise power with broadcasting")
99 .set_support_level(4)
100 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::power));
101 
102 
103 RELAY_REGISTER_BINARY_OP("mod")
104 .describe("Elementwise mod with broadcasting")
105 .set_support_level(1)
106 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::mod));
107 
108 
109 RELAY_REGISTER_BINARY_OP("floor_mod")
110   .describe("Elementwise floor mod with broadcasting")
111   .set_support_level(1)
112   .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::floor_mod));
113 
114 
115 RELAY_REGISTER_BINARY_OP("logical_and")
116 .describe("Elementwise logical AND with broadcasting")
117 .set_support_level(4)
118 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::logical_and));
119 
120 
121 RELAY_REGISTER_BINARY_OP("logical_or")
122 .describe("Elementwise logical OR with broadcasting")
123 .set_support_level(4)
124 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::logical_or));
125 
126 
127 RELAY_REGISTER_CMP_OP("equal")
128 .describe("Elementwise equal compare with broadcasting")
129 .set_support_level(4)
130 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::equal));
131 
132 
133 RELAY_REGISTER_CMP_OP("not_equal")
134 .describe("Elementwise not equal with broadcasting")
135 .set_support_level(4)
136 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::not_equal));
137 
138 
139 RELAY_REGISTER_CMP_OP("less")
140 .describe("Elementwise less than with broadcasting")
141 .set_support_level(4)
142 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::less));
143 
144 
145 RELAY_REGISTER_CMP_OP("less_equal")
146 .describe("Elementwise less than or equal compare with broadcasting")
147 .set_support_level(4)
148 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::less_equal));
149 
150 
151 RELAY_REGISTER_CMP_OP("greater")
152 .describe("Elementwise greater than compare with broadcasting")
153 .set_support_level(4)
154 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::greater));
155 
156 
157 RELAY_REGISTER_CMP_OP("greater_equal")
158 .describe("Elementwise greater than or equal compare with broadcasting")
159 .set_support_level(4)
160 .set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::greater_equal));
161 
162 }  // namespace relay
163 }  // namespace tvm
164