You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

assert.cc 3.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include <map>
  17. #include <string>
  18. #include <set>
  19. #include <vector>
  20. #include <memory>
  21. #include "ops/assert.h"
  22. #include "ops/op_utils.h"
  23. namespace mindspore {
  24. namespace ops {
  25. void Assert::Init(const int64_t summarize) { set_summarize(summarize); }
  26. void Assert::set_summarize(const int64_t summarize) { this->AddAttr(kSummarize, MakeValue(summarize)); }
  27. int64_t Assert::get_summarize() const {
  28. auto value_ptr = GetAttr(kSummarize);
  29. return GetValue<int64_t>(value_ptr);
  30. }
  31. AbstractBasePtr AssertInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
  32. const std::vector<AbstractBasePtr> &input_args) {
  33. MS_EXCEPTION_IF_NULL(primitive);
  34. auto op_name = primitive->name();
  35. TypePtr condition;
  36. if (!(input_args[0]->BuildType()->type_id() == kObjectTypeTensorType)) {
  37. auto condition_value = GetValue<std::vector<bool>>(input_args[0]->BuildValue());
  38. CheckAndConvertUtils::CheckInteger("condition's rank", condition_value.size(), kLessEqual, 1, op_name);
  39. if (condition_value.size() == 1) {
  40. CheckAndConvertUtils::CheckInteger("condition[0]", condition_value[0], kEqual, 1, op_name);
  41. }
  42. condition = TypeIdToType(kNumberTypeBool);
  43. } else {
  44. auto condition_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
  45. CheckAndConvertUtils::CheckInteger("condition's rank", condition_shape[0], kLessEqual, 1, op_name);
  46. if (condition_shape[0] == 1) {
  47. auto condition_value = reinterpret_cast<bool *>(input_args[0]->BuildValue()->cast<tensor::TensorPtr>()->data_c());
  48. MS_EXCEPTION_IF_NULL(condition_value);
  49. CheckAndConvertUtils::CheckInteger("condition[0]", *condition_value, kEqual, 1, op_name);
  50. }
  51. condition = input_args[0]->BuildType();
  52. }
  53. std::vector<int64_t> output_shape = {1};
  54. std::set<TypePtr> local_bool = {kBool};
  55. std::map<std::string, TypePtr> args = {{"condition", condition}};
  56. (void)CheckAndConvertUtils::CheckScalarOrTensorTypesSame(args, local_bool, op_name);
  57. auto inputs_type = input_args[1]->BuildType()->cast<TuplePtr>()->elements();
  58. for (auto dtype : inputs_type) {
  59. std::set<TypePtr> template_types = {kTensorType};
  60. CheckAndConvertUtils::CheckSubClass("input", dtype, template_types, op_name);
  61. }
  62. return std::make_shared<abstract::AbstractTensor>(kInt32, output_shape);
  63. }
  64. REGISTER_PRIMITIVE_C(kNameAssert, Assert);
  65. } // namespace ops
  66. } // namespace mindspore