binary.cc 5.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.
 */

Tianqi Chen committed
20 21 22 23 24 25 26
/*!
 *  Copyright (c) 2018 by Contributors
 * \file binary.cc
 * \brief binary broadcast operators.
 */
#include <tvm/relay/expr.h>
#include <tvm/relay/op.h>
27
#include <topi/broadcast.h>
Tianqi Chen committed
28
#include "../type_relations.h"
雾雨魔理沙 committed
29
#include "../op_common.h"
Tianqi Chen committed
30 31 32 33

namespace tvm {
namespace relay {

34 35 36 37 38 39 40 41 42
#define RELAY_BINARY_COMPUTE(FTOPI)                        \
  [] (const Attrs& attrs,                                  \
      const Array<Tensor>& inputs,                         \
      const Type& out_type,                                \
      const Target& target) -> Array<Tensor> {             \
    CHECK_EQ(inputs.size(), 2U);                           \
    return {FTOPI(inputs[0], inputs[1])};                  \
  }                                                        \

雾雨魔理沙 committed
43
// Addition
44
RELAY_REGISTER_BINARY_OP("add")
Tianqi Chen committed
45
.describe("Elementwise add with with broadcasting")
46 47
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::add));
Tianqi Chen committed
48

49
// Subtraction
50
RELAY_REGISTER_BINARY_OP("subtract")
Tianqi Chen committed
51
.describe("Elementwise substract with broadcasting")
52 53
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::subtract));
Tianqi Chen committed
54

55
// Right shift
56
RELAY_REGISTER_BINARY_OP("right_shift")
Tianqi Chen committed
57
.describe("Elementwise right shift with broadcasting")
58 59
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::right_shift));
Tianqi Chen committed
60

61 62

RELAY_REGISTER_BINARY_OP("left_shift")
63
.describe("Elementwise left shift with broadcasting")
64 65
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::left_shift));
66

67 68

RELAY_REGISTER_BINARY_OP("maximum")
69
.describe("Elementwise maximum of two tensors with broadcasting")
70 71
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::maximum));
72

73 74

RELAY_REGISTER_BINARY_OP("minimum")
75
.describe("Elementwise minimum of two tensors with broadcasting")
76 77
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::minimum));
78

79 80

RELAY_REGISTER_BINARY_OP("divide")
81
.describe("Elementwise divide with broadcasting")
82 83
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::divide));
84

85 86

RELAY_REGISTER_BINARY_OP("multiply")
87
.describe("Elementwise multiply with broadcasting")
88 89
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::multiply));
90

91 92

RELAY_REGISTER_BINARY_OP("power")
93
.describe("Elementwise power with broadcasting")
94 95
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::power));
96

97 98

RELAY_REGISTER_BINARY_OP("mod")
99
.describe("Elementwise mod with broadcasting")
100 101
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::mod));
102

103

104 105 106 107 108 109 110 111 112 113 114 115
RELAY_REGISTER_BINARY_OP("logical_and")
.describe("Elementwise logical AND with broadcasting")
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::logical_and));


RELAY_REGISTER_BINARY_OP("logical_or")
.describe("Elementwise logical OR with broadcasting")
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::logical_or));


116 117
RELAY_REGISTER_CMP_OP("equal")
.describe("Elementwise equal compare with broadcasting")
118 119 120 121
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::equal));


122 123
RELAY_REGISTER_CMP_OP("not_equal")
.describe("Elementwise not equal with broadcasting")
124 125 126 127
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::not_equal));


128 129
RELAY_REGISTER_CMP_OP("less")
.describe("Elementwise less than with broadcasting")
130 131 132 133
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::less));


134 135
RELAY_REGISTER_CMP_OP("less_equal")
.describe("Elementwise less than or equal compare with broadcasting")
136 137 138 139
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::less_equal));


140 141
RELAY_REGISTER_CMP_OP("greater")
.describe("Elementwise greater than compare with broadcasting")
142 143 144 145
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::greater));


146 147
RELAY_REGISTER_CMP_OP("greater_equal")
.describe("Elementwise greater than or equal compare with broadcasting")
148 149
.set_support_level(4)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_BINARY_COMPUTE(topi::greater_equal));
Tianqi Chen committed
150 151 152

}  // namespace relay
}  // namespace tvm