|
| 1 | +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "backend/register/register.h" |
| 22 | + |
| 23 | +namespace backend { |
| 24 | +const char *const kAccuracy = "accuracy"; |
| 25 | + |
| 26 | +IMPLEMT_EQUIVALENCE_TRANS_FUNC( |
| 27 | + gcu_builder, op, map_inputs, running_mode, AccuracyEquivalenceTrans) { |
| 28 | + auto indices_op = *(map_inputs["Indices"].at(0)); |
| 29 | + builder::Op out_op = *(map_inputs["Out"].at(0)); |
| 30 | + auto label_op = *(map_inputs["Label"].at(0)); |
| 31 | + std::vector<builder::Op> outputs; |
| 32 | + int64_t num_samples = out_op.GetType().GetShape()[0]; |
| 33 | + |
| 34 | + auto total_data = builder::FullLike( |
| 35 | + indices_op, num_samples, indices_op.GetType().GetPrimitiveType(), {}); |
| 36 | + int64_t dims = static_cast<int64_t>(indices_op.GetType().GetRank()); |
| 37 | + std::vector<int64_t> broadcast_dimensions(dims, 0); |
| 38 | + std::iota(broadcast_dimensions.begin(), broadcast_dimensions.end(), 0); |
| 39 | + |
| 40 | + auto label_broad = builder::BroadcastInDim( |
| 41 | + label_op, broadcast_dimensions, indices_op.GetType()); |
| 42 | + auto correct_data = builder::Equal(indices_op, label_broad); |
| 43 | + correct_data = builder::Convert(correct_data, |
| 44 | + {correct_data.GetType().GetShape(), |
| 45 | + indices_op.GetType().GetPrimitiveType()}); |
| 46 | + |
| 47 | + std::vector<int64_t> perm(label_op.GetType().GetRank(), 0); |
| 48 | + std::iota(perm.begin(), perm.end(), 0); |
| 49 | + auto correct_num = |
| 50 | + builder::ReduceSum(correct_data, false, perm, total_data.GetType()); |
| 51 | + |
| 52 | + auto total_data_f = |
| 53 | + builder::Convert(total_data, {{}, out_op.GetType().GetPrimitiveType()}); |
| 54 | + auto correct_num_f = |
| 55 | + builder::Convert(correct_num, {{}, out_op.GetType().GetPrimitiveType()}); |
| 56 | + auto accuracy_data = builder::Div(correct_num_f, total_data_f); |
| 57 | + |
| 58 | + outputs.push_back(accuracy_data); |
| 59 | + outputs.push_back(correct_num); |
| 60 | + outputs.push_back(total_data); |
| 61 | + |
| 62 | + std::vector<std::string> output_names{"Accuracy", "Correct", "Total"}; |
| 63 | + auto output_name_map = op->Outputs(); |
| 64 | + std::string output_names_attr(output_name_map[output_names[0]][0]); |
| 65 | + for (size_t i = 1; i < output_names.size(); ++i) { |
| 66 | + output_names_attr += ";" + output_name_map[output_names[i]][0]; |
| 67 | + } |
| 68 | + builder::Op result = builder::Tuple(outputs); |
| 69 | + result.SetAttribute(kAttrOpOutVarName, |
| 70 | + builder::Attribute(output_names_attr.c_str())); |
| 71 | + return std::make_shared<GcuOp>(result); |
| 72 | +} |
| 73 | + |
| 74 | +EQUIVALENCE_TRANS_FUNC_REG(kAccuracy, INSENSITIVE, AccuracyEquivalenceTrans); |
| 75 | + |
| 76 | +} // namespace backend |
0 commit comments