Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame^] | 2 | // Copyright (c) 2020-2021, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include "scatter_gather.h" |
| 17 | #include "quant_util.h" |
| 18 | |
| 19 | using namespace TosaReference; |
| 20 | using namespace Eigen; |
| 21 | using namespace tosa; |
| 22 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 23 | template <DType Dtype> |
| 24 | OpGather<Dtype>::OpGather(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 25 | : GraphNode(Op_GATHER, id_) |
| 26 | { |
| 27 | setRequiredOperands(2, 1); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 30 | template <DType Dtype> |
| 31 | OpGather<Dtype>::~OpGather() |
| 32 | {} |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 33 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 34 | template <DType Dtype> |
| 35 | int OpGather<Dtype>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 36 | { |
| 37 | if (validateRequiredOperands()) |
| 38 | return 1; |
| 39 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 40 | if (inputs[0]->getRank() != 3) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 41 | { |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 42 | printNodeValidationError("OpGather: values needs to be rank 3 tensor"); |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | if (inputs[1]->getRank() != 2) |
| 47 | { |
| 48 | printNodeValidationError("OpGather: indices needs to be rank 2 tensor"); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | if (outputs[0]->getRank() != 3) |
| 53 | { |
| 54 | printNodeValidationError("OpGather: output needs to be rank 3 tensor"); |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | K = inputs[0]->getShape()[1]; |
| 59 | N = outputs[0]->getShape()[0]; |
| 60 | W = outputs[0]->getShape()[1]; |
| 61 | C = outputs[0]->getShape()[2]; |
| 62 | |
| 63 | if (N != inputs[0]->getShape()[0] || N != inputs[1]->getShape()[0]) |
| 64 | { |
| 65 | printNodeValidationError("OpGather: dimension N mismatch"); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | if (W != inputs[1]->getShape()[1]) |
| 70 | { |
| 71 | printNodeValidationError("OpGather: dimension W mismatch"); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | if (C != inputs[0]->getShape()[2]) |
| 76 | { |
| 77 | printNodeValidationError("OpGather: dimension C mismatch"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | // output and input must be the same types |
| 82 | if (inputs[0]->matchType(*outputs[0])) |
| 83 | { |
| 84 | printNodeValidationError("Failure to match input and output type"); |
| 85 | return 1; |
| 86 | } |
| 87 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 88 | values = dynamic_cast<TosaReference::TensorTemplate<TValue>*>(inputs[0]); |
| 89 | indices = dynamic_cast<TosaReference::TensorTemplate<TIndex>*>(inputs[1]); |
| 90 | output = dynamic_cast<TosaReference::TensorTemplate<TOutput>*>(outputs[0]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 91 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 92 | ASSERT_MEM(values && indices && output); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 97 | template <DType Dtype> |
| 98 | int OpGather<Dtype>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 99 | { |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 100 | for (int32_t n = 0; n < N; n++) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 101 | { |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 102 | for (int32_t w = 0; w < W; w++) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 103 | { |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 104 | int32_t k = this->indices->getTensor()(n, w); |
| 105 | ASSERT_MSG_NODE(k >= 0 && k < K, "OpGather: index(%d, %d)=%d exceed valid range [0, %d]", n, w, k, K); |
| 106 | for (int32_t c = 0; c < C; c++) |
| 107 | { |
| 108 | EigenType value = this->values->getTensor()(n, k, c); |
| 109 | this->output->getTensor()(n, w, c) = value; |
| 110 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 114 | return GraphNode::eval(); |
| 115 | } |
| 116 | |
| 117 | template <DType Dtype> |
| 118 | OpScatter<Dtype>::OpScatter(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_) |
| 119 | : GraphNode(Op_SCATTER, id_) |
| 120 | { |
| 121 | setRequiredOperands(3, 1); |
| 122 | } |
| 123 | |
| 124 | template <DType Dtype> |
| 125 | OpScatter<Dtype>::~OpScatter() |
| 126 | {} |
| 127 | |
| 128 | template <DType Dtype> |
| 129 | int OpScatter<Dtype>::checkTensorAttributes() |
| 130 | { |
| 131 | if (validateRequiredOperands()) |
| 132 | return 1; |
| 133 | |
| 134 | if (inputs[0]->getRank() != 3) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 135 | { |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 136 | printNodeValidationError("OpGather: values_in needs to be rank 3 tensor"); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | if (inputs[1]->getRank() != 2) |
| 141 | { |
| 142 | printNodeValidationError("OpGather: indices needs to be rank 2 tensor"); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | if (inputs[2]->getRank() != 3) |
| 147 | { |
| 148 | printNodeValidationError("OpGather: input needs to be rank 3 tensor"); |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | if (outputs[0]->getRank() != 3) |
| 153 | { |
| 154 | printNodeValidationError("OpGather: values_out needs to be rank 3 tensor"); |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | W = inputs[2]->getShape()[1]; |
| 159 | N = outputs[0]->getShape()[0]; |
| 160 | K = outputs[0]->getShape()[1]; |
| 161 | C = outputs[0]->getShape()[2]; |
| 162 | |
| 163 | if (N != inputs[0]->getShape()[0] || N != inputs[1]->getShape()[0] || N != inputs[2]->getShape()[0]) |
| 164 | { |
| 165 | printNodeValidationError("OpScatter: dimension N mismatch"); |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | if (W != inputs[1]->getShape()[1]) |
| 170 | { |
| 171 | printNodeValidationError("OpGather: dimension W mismatch"); |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | if (C != inputs[0]->getShape()[2] || C != inputs[2]->getShape()[2]) |
| 176 | { |
| 177 | printNodeValidationError("OpGather: dimension C mismatch"); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | // output and input must be the same types |
| 182 | if (inputs[0]->matchType(*outputs[0])) |
| 183 | { |
| 184 | printNodeValidationError("Failure to match input and output type"); |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | values_in = dynamic_cast<TosaReference::TensorTemplate<TValue>*>(inputs[0]); |
| 189 | indices = dynamic_cast<TosaReference::TensorTemplate<TIndex>*>(inputs[1]); |
| 190 | input = dynamic_cast<TosaReference::TensorTemplate<TValue>*>(inputs[2]); |
| 191 | values_out = dynamic_cast<TosaReference::TensorTemplate<TOutput>*>(outputs[0]); |
| 192 | |
| 193 | ASSERT_MEM(values_in && indices && input && values_out); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | template <DType Dtype> |
| 199 | int OpScatter<Dtype>::eval() |
| 200 | { |
| 201 | // Initializes the output tensor with the input value for values that are unchanged by the scatter operation. |
| 202 | this->values_out->getTensor() = this->values_in->getTensor(); |
| 203 | |
| 204 | for (int n = 0; n < N; n++) |
| 205 | { |
| 206 | for (int w = 0; w < W; w++) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 207 | { |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 208 | int32_t k = this->indices->getTensor()(n, w); |
| 209 | ASSERT_MSG_NODE(k >= 0 && k < K, "OpScatter: index(%d, %d)=%d exceed valid range [0, %d]", n, w, k, K); |
| 210 | for (int c = 0; c < C; c++) |
| 211 | { |
| 212 | EigenType value = this->input->getTensor()(n, w, c); |
| 213 | this->values_out->getTensor()(n, k, c) = value; |
| 214 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | return GraphNode::eval(); |
| 219 | } |
| 220 | |
| 221 | // template explicit instantiation |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame^] | 222 | DEF_INSTANTIATE_ONE_TYPE(OpGather, INT8); |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 223 | DEF_INSTANTIATE_ONE_TYPE(OpGather, INT16); |
| 224 | DEF_INSTANTIATE_ONE_TYPE(OpGather, INT32); |
| 225 | DEF_INSTANTIATE_ONE_TYPE(OpGather, FLOAT); |
| 226 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame^] | 227 | DEF_INSTANTIATE_ONE_TYPE(OpScatter, INT8); |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 228 | DEF_INSTANTIATE_ONE_TYPE(OpScatter, INT16); |
| 229 | DEF_INSTANTIATE_ONE_TYPE(OpScatter, INT32); |
| 230 | DEF_INSTANTIATE_ONE_TYPE(OpScatter, FLOAT); |