1 /**
2 * Copyright 2020-2021 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 #include "ops/unsorted_segment_sum.h"
17 #include <string>
18 #include <algorithm>
19 #include <memory>
20 #include <set>
21 #include <vector>
22 #include "ops/op_utils.h"
23 #include "utils/check_convert_utils.h"
24 #include "abstract/primitive_infer_map.h"
25
26 namespace mindspore {
27 namespace ops {
UnsortedSegmentSumInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)28 AbstractBasePtr UnsortedSegmentSumInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
29 const std::vector<AbstractBasePtr> &input_args) {
30 MS_EXCEPTION_IF_NULL(primitive);
31 auto prim_name = primitive->name();
32
33 // Infer type
34 for (const auto &item : input_args) {
35 MS_EXCEPTION_IF_NULL(item);
36 }
37 auto x_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
38 // Infer shape
39 auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
40 (void)CheckAndConvertUtils::CheckInteger("x_shape", SizeToLong(x_shape.size()), kGreaterThan, 0, prim_name);
41 auto shp = x_shape;
42 auto segment_ids_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
43 (void)CheckAndConvertUtils::CheckInteger("segment_ids_shape", SizeToLong(segment_ids_shape.size()), kGreaterThan, 0,
44 prim_name);
45 CheckAndConvertUtils::Check("input_x", int64_t(x_shape.size()), kGreaterEqual, "segment_ids_shape",
46 int64_t(segment_ids_shape.size()), prim_name);
47
48 if ((x_shape.end() != find(x_shape.begin(), x_shape.end(), -1)) &&
49 (segment_ids_shape.end() != find(segment_ids_shape.begin(), segment_ids_shape.end(), -1))) {
50 size_t size = segment_ids_shape.size();
51 for (size_t i = 0; i < size; ++i) {
52 CheckAndConvertUtils::Check("segment_ids_shp", segment_ids_shape[i], kEqual, "x_shape", x_shape[i], prim_name);
53 }
54 }
55
56 const std::set<TypePtr> valid_num_segments_types = {kInt32, kInt64};
57 (void)CheckAndConvertUtils::CheckTensorTypeValid("num_segments", input_args[kInputIndex2]->BuildType(),
58 valid_num_segments_types, prim_name);
59 size_t size_segment_ids_shp = segment_ids_shape.size();
60 size_t size_x_shape = x_shape.size();
61 for (size_t i = size_segment_ids_shp; i < size_x_shape; ++i) {
62 (void)shp.emplace_back(x_shape[i]);
63 }
64
65 return std::make_shared<abstract::AbstractTensor>(x_type, shp);
66 }
67 REGISTER_PRIMITIVE_C(kNameUnsortedSegmentSum, UnsortedSegmentSum);
68 } // namespace ops
69 } // namespace mindspore
70