1 /**
2 * Copyright 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
17 #include "ir/anf_utils.h"
18
19 namespace mindspore {
IsShapeDynamic(const abstract::ShapePtr & shape)20 bool AnfUtils::IsShapeDynamic(const abstract::ShapePtr &shape) {
21 MS_EXCEPTION_IF_NULL(shape);
22 return std::any_of(shape->shape().begin(), shape->shape().end(), [](int64_t s) { return s < 0; });
23 }
24
IsShapeDynamic(const std::vector<size_t> & shape)25 bool AnfUtils::IsShapeDynamic(const std::vector<size_t> &shape) {
26 return std::any_of(shape.begin(), shape.end(), [](int64_t s) { return s < 0; });
27 }
28
IsNodeOutputDynamicShape(const CNodePtr & node)29 bool AnfUtils::IsNodeOutputDynamicShape(const CNodePtr &node) {
30 MS_EXCEPTION_IF_NULL(node);
31 auto base_shape = node->Shape();
32 if (base_shape == nullptr) {
33 MS_LOG(INFO) << "Invalid base shape, node: " << node->fullname_with_scope();
34 return false;
35 }
36 if (base_shape->isa<abstract::Shape>() && IsShapeDynamic(base_shape->cast<abstract::ShapePtr>())) {
37 return true;
38 } else if (base_shape->isa<abstract::TupleShape>()) {
39 auto tuple_shape = base_shape->cast<abstract::TupleShapePtr>();
40 MS_EXCEPTION_IF_NULL(tuple_shape);
41 for (size_t i = 0; i < tuple_shape->size(); i++) {
42 auto b_shape = (*tuple_shape)[i];
43 if (b_shape->isa<abstract::Shape>() && IsShapeDynamic(b_shape->cast<abstract::ShapePtr>())) {
44 return true;
45 }
46 }
47 }
48 return false;
49 }
50 } // namespace mindspore
51