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.

test_ops_assert.cc 3.9 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <vector>
  17. #include <memory>
  18. #include "common/common_test.h"
  19. #include "ops/assert.h"
  20. #include "ir/dtype/type.h"
  21. #include "ir/value.h"
  22. #include "abstract/dshape.h"
  23. #include "utils/tensor_construct_utils.h"
  24. namespace mindspore {
  25. namespace ops {
  26. namespace {
  27. template <typename T>
  28. void SetTensorData(void *data, T num, size_t data_length) {
  29. MS_EXCEPTION_IF_NULL(data);
  30. auto tensor_data = reinterpret_cast<T *>(data);
  31. MS_EXCEPTION_IF_NULL(tensor_data);
  32. for (size_t index = 0; index < data_length; ++index) {
  33. *tensor_data = num;
  34. ++tensor_data;
  35. }
  36. }
  37. } // namespace
  38. class TestAssert : public UT::Common {
  39. public:
  40. TestAssert() {}
  41. void SetUp() {}
  42. void TearDown() {}
  43. };
  44. TEST_F(TestAssert, test_ops_assert1) {
  45. auto assert = std::make_shared<Assert>();
  46. assert->Init(3);
  47. EXPECT_EQ(assert->get_summarize(), 3);
  48. std::vector<ValuePtr> inputs_ = {TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{1})};
  49. auto condition = MakeValue(std::vector<bool>{true});
  50. auto inputs = std::make_shared<ValueTuple>(inputs_);
  51. auto abstract = assert->Infer({condition->ToAbstract(), inputs->ToAbstract()});
  52. MS_EXCEPTION_IF_NULL(abstract);
  53. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  54. auto shape_ptr = abstract->BuildShape();
  55. MS_EXCEPTION_IF_NULL(shape_ptr);
  56. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  57. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  58. MS_EXCEPTION_IF_NULL(shape);
  59. auto shape_vec = shape->shape();
  60. EXPECT_EQ(shape_vec.size(), 1);
  61. EXPECT_EQ(shape_vec[0], 1);
  62. auto type = abstract->BuildType();
  63. MS_EXCEPTION_IF_NULL(type);
  64. EXPECT_EQ(type->isa<TensorType>(), true);
  65. auto tensor_type = type->cast<TensorTypePtr>();
  66. MS_EXCEPTION_IF_NULL(tensor_type);
  67. auto data_type = tensor_type->element();
  68. MS_EXCEPTION_IF_NULL(data_type);
  69. EXPECT_EQ(data_type->type_id(), kNumberTypeInt32);
  70. }
  71. TEST_F(TestAssert, test_ops_assert2) {
  72. auto assert = std::make_shared<Assert>();
  73. assert->Init(3);
  74. EXPECT_EQ(assert->get_summarize(), 3);
  75. std::vector<ValuePtr> inputs_ = {TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{1})};
  76. auto tensor = std::make_shared<tensor::Tensor>(kNumberTypeBool, std::vector<int64_t>{1});
  77. MS_EXCEPTION_IF_NULL(tensor);
  78. auto mem_size = IntToSize(tensor->ElementsNum());
  79. SetTensorData<bool>(tensor->data_c(), true, mem_size);
  80. auto inputs = std::make_shared<ValueTuple>(inputs_);
  81. auto abstract = assert->Infer({tensor->ToAbstract(), inputs->ToAbstract()});
  82. MS_EXCEPTION_IF_NULL(abstract);
  83. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  84. auto shape_ptr = abstract->BuildShape();
  85. MS_EXCEPTION_IF_NULL(shape_ptr);
  86. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  87. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  88. MS_EXCEPTION_IF_NULL(shape);
  89. auto shape_vec = shape->shape();
  90. EXPECT_EQ(shape_vec.size(), 1);
  91. EXPECT_EQ(shape_vec[0], 1);
  92. auto type = abstract->BuildType();
  93. MS_EXCEPTION_IF_NULL(type);
  94. EXPECT_EQ(type->isa<TensorType>(), true);
  95. auto tensor_type = type->cast<TensorTypePtr>();
  96. MS_EXCEPTION_IF_NULL(tensor_type);
  97. auto data_type = tensor_type->element();
  98. MS_EXCEPTION_IF_NULL(data_type);
  99. EXPECT_EQ(data_type->type_id(), kNumberTypeInt32);
  100. }
  101. } // namespace ops
  102. } // namespace mindspore