1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
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
17 #include "ops/custom_predict.h"
18 #include "ops/op_utils.h"
19 #include "utils/check_convert_utils.h"
20 #include "abstract/primitive_infer_map.h"
21
22 namespace mindspore {
23 namespace ops {
Init(const int64_t output_num,const float weight_threshold)24 void CustomPredict::Init(const int64_t output_num, const float weight_threshold) {
25 this->set_output_num(output_num);
26 this->set_weight_threshold(weight_threshold);
27 }
28
set_output_num(const int64_t output_num)29 void CustomPredict::set_output_num(const int64_t output_num) { (void)this->AddAttr(kOutputNum, MakeValue(output_num)); }
30
get_output_num() const31 int64_t CustomPredict::get_output_num() const {
32 auto value_ptr = this->GetAttr(kOutputNum);
33 return GetValue<int64_t>(value_ptr);
34 }
35
set_weight_threshold(const float weight_threshold)36 void CustomPredict::set_weight_threshold(const float weight_threshold) {
37 (void)this->AddAttr(kWeightThreshold, MakeValue(weight_threshold));
38 }
39
get_weight_threshold() const40 float CustomPredict::get_weight_threshold() const {
41 auto value_ptr = this->GetAttr(kWeightThreshold);
42 return GetValue<float>(value_ptr);
43 }
44
CustomPredictInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)45 AbstractBasePtr CustomPredictInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
46 const std::vector<AbstractBasePtr> &input_args) {
47 MS_EXCEPTION_IF_NULL(primitive);
48 for (const auto &input : input_args) {
49 MS_EXCEPTION_IF_NULL(input);
50 }
51 std::vector<int64_t> shape;
52 shape.push_back(GetValue<int64_t>(primitive->GetAttr(kOutputNum)));
53
54 auto output0 = std::make_shared<abstract::AbstractTensor>(kInt32, shape);
55 auto output1 = std::make_shared<abstract::AbstractTensor>(kFloat32, shape);
56 AbstractBasePtrList output = {output0, output1};
57 return std::make_shared<abstract::AbstractTuple>(output);
58 }
59 REGISTER_PRIMITIVE_C(kNameCustomPredict, CustomPredict);
60 } // namespace ops
61 } // namespace mindspore
62