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_range.cc 4.4 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/range.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. class TestRange : public UT::Common {
  27. public:
  28. TestRange() {}
  29. void SetUp() {}
  30. void TearDown() {}
  31. };
  32. TEST_F(TestRange, test_ops_range1) {
  33. auto range = std::make_shared<Range>();
  34. range->Init(1, 3, 34, 4);
  35. EXPECT_EQ(range->get_d_type(), 1);
  36. EXPECT_EQ(range->get_start(), 3);
  37. EXPECT_EQ(range->get_limit(), 34);
  38. EXPECT_EQ(range->get_delta(), 4);
  39. range->set_d_type(1);
  40. range->set_start(3);
  41. range->set_limit(34);
  42. range->set_delta(4);
  43. auto abstract = range->Infer({});
  44. MS_EXCEPTION_IF_NULL(abstract);
  45. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  46. auto shape_ptr = abstract->BuildShape();
  47. MS_EXCEPTION_IF_NULL(shape_ptr);
  48. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  49. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  50. MS_EXCEPTION_IF_NULL(shape);
  51. auto shape_vec = shape->shape();
  52. auto type = abstract->BuildType();
  53. MS_EXCEPTION_IF_NULL(type);
  54. EXPECT_EQ(type->isa<TensorType>(), true);
  55. auto tensor_type = type->cast<TensorTypePtr>();
  56. MS_EXCEPTION_IF_NULL(tensor_type);
  57. auto data_type = tensor_type->element();
  58. MS_EXCEPTION_IF_NULL(data_type);
  59. EXPECT_EQ(data_type->type_id(), kNumberTypeInt32);
  60. EXPECT_EQ(shape_vec.size(), 1);
  61. EXPECT_EQ(shape_vec[0], 8);
  62. EXPECT_EQ(range->get_d_type(), 1);
  63. EXPECT_EQ(range->get_start(), 3);
  64. EXPECT_EQ(range->get_limit(), 34);
  65. EXPECT_EQ(range->get_delta(), 4);
  66. }
  67. TEST_F(TestRange, test_ops_range2) {
  68. auto range = std::make_shared<Range>();
  69. range->Init(1, 1, 1, 1);
  70. EXPECT_EQ(range->get_d_type(), 1);
  71. EXPECT_EQ(range->get_start(), 1);
  72. EXPECT_EQ(range->get_limit(), 1);
  73. EXPECT_EQ(range->get_delta(), 1);
  74. range->set_d_type(1);
  75. range->set_start(1);
  76. range->set_limit(1);
  77. range->set_delta(1);
  78. auto tensor_x1 = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1});
  79. auto tensor_x2 = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1});
  80. auto tensor_x3 = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1});
  81. MS_EXCEPTION_IF_NULL(tensor_x1);
  82. MS_EXCEPTION_IF_NULL(tensor_x2);
  83. MS_EXCEPTION_IF_NULL(tensor_x3);
  84. auto data_x1 = tensor_x1->data_c();
  85. MS_EXCEPTION_IF_NULL(data_x1);
  86. auto val_x1 = reinterpret_cast<float *>(data_x1);
  87. *val_x1 = 1.0;
  88. auto data_x2 = tensor_x2->data_c();
  89. MS_EXCEPTION_IF_NULL(data_x2);
  90. auto val_x2 = reinterpret_cast<float *>(data_x2);
  91. *val_x2 = 42.0;
  92. auto data_x3 = tensor_x3->data_c();
  93. MS_EXCEPTION_IF_NULL(data_x3);
  94. auto val_x3 = reinterpret_cast<float *>(data_x3);
  95. *val_x3 = 3.0;
  96. auto abstract = range->Infer({tensor_x1->ToAbstract(), tensor_x2->ToAbstract(), tensor_x3->ToAbstract()});
  97. MS_EXCEPTION_IF_NULL(abstract);
  98. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  99. auto shape_ptr = abstract->BuildShape();
  100. MS_EXCEPTION_IF_NULL(shape_ptr);
  101. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  102. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  103. MS_EXCEPTION_IF_NULL(shape);
  104. auto shape_vec = shape->shape();
  105. auto type = abstract->BuildType();
  106. MS_EXCEPTION_IF_NULL(type);
  107. EXPECT_EQ(type->isa<TensorType>(), true);
  108. auto tensor_type = type->cast<TensorTypePtr>();
  109. MS_EXCEPTION_IF_NULL(tensor_type);
  110. auto data_type = tensor_type->element();
  111. MS_EXCEPTION_IF_NULL(data_type);
  112. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  113. EXPECT_EQ(shape_vec.size(), 1);
  114. EXPECT_EQ(shape_vec[0], 14);
  115. EXPECT_EQ(range->get_d_type(), 1);
  116. EXPECT_EQ(range->get_start(), 1);
  117. EXPECT_EQ(range->get_limit(), 1);
  118. EXPECT_EQ(range->get_delta(), 1);
  119. }
  120. } // namespace ops
  121. } // namespace mindspore