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.3 kB

5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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) { (void)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. for (const auto &item : input_args) {
  36. MS_EXCEPTION_IF_NULL(item);
  37. }
  38. TypePtr condition;
  39. if (!(input_args[0]->BuildType()->type_id() == kObjectTypeTensorType)) {
  40. auto condition_values = GetValue<std::vector<bool>>(input_args[0]->BuildValue());
  41. (void)CheckAndConvertUtils::CheckInteger("condition's rank", SizeToLong(condition_values.size()), kLessEqual, 1,
  42. op_name);
  43. if (condition_values.size() == 1) {
  44. if (!condition_values[0]) {
  45. MS_EXCEPTION(ValueError) << "condition value must be `true` when only one value contained.";
  46. }
  47. }
  48. condition = TypeIdToType(kNumberTypeBool);
  49. } else {
  50. auto condition_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
  51. (void)CheckAndConvertUtils::CheckInteger("condition's rank", condition_shape[0], kLessEqual, 1, op_name);
  52. if (condition_shape[0] == 1) {
  53. auto condition_value = reinterpret_cast<bool *>(input_args[0]->BuildValue()->cast<tensor::TensorPtr>()->data_c());
  54. MS_EXCEPTION_IF_NULL(condition_value);
  55. if (!*condition_value) {
  56. MS_EXCEPTION(ValueError) << "condition value must be `true` when only one value contained.";
  57. }
  58. }
  59. condition = input_args[0]->BuildType();
  60. }
  61. std::vector<int64_t> output_shape = {1};
  62. std::set<TypePtr> local_bool = {kBool};
  63. std::map<std::string, TypePtr> args = {{"condition", condition}};
  64. (void)CheckAndConvertUtils::CheckScalarOrTensorTypesSame(args, local_bool, op_name);
  65. auto inputs_type = input_args[1]->BuildType()->cast<TuplePtr>()->elements();
  66. for (auto dtype : inputs_type) {
  67. std::set<TypePtr> template_types = {kTensorType};
  68. (void)CheckAndConvertUtils::CheckSubClass("input", dtype, template_types, op_name);
  69. }
  70. return std::make_shared<abstract::AbstractTensor>(kInt32, output_shape);
  71. }
  72. REGISTER_PRIMITIVE_C(kNameAssert, Assert);
  73. } // namespace ops
  74. } // namespace mindspore