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.

abstract_test.cc 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright 2020-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. #include <iostream>
  17. #include <memory>
  18. #include "common/common_test.h"
  19. #include "pybind11/pybind11.h"
  20. #include "pipeline/jit/static_analysis/static_analysis.h"
  21. #include "abstract/utils.h"
  22. #include "pipeline/jit/static_analysis/prim.h"
  23. #include "pipeline/jit/parse/parse.h"
  24. #include "pipeline/jit/parse/resolve.h"
  25. #include "pipeline/jit/parse/data_converter.h"
  26. #include "frontend/operator/ops.h"
  27. namespace mindspore {
  28. namespace abstract {
  29. class TestAbstract : public UT::Common {
  30. public:
  31. TestAbstract() {}
  32. virtual void SetUp() {}
  33. virtual void TearDown() {}
  34. };
  35. TEST_F(TestAbstract, TestParseDataClass) {
  36. // Check initialization before callback to Python.
  37. if (Py_IsInitialized() == 0) {
  38. Py_Initialize();
  39. }
  40. PyEval_InitThreads();
  41. py::object fn = parse::python_adapter::GetPyFn("gtest_input.pipeline.parse.parser_test", "TestFoo");
  42. ClassPtr cls_ptr = parse::ParseDataClass(fn);
  43. ASSERT_TRUE(nullptr != cls_ptr);
  44. std::shared_ptr<Class> cls = dyn_cast<Class>(cls_ptr);
  45. ASSERT_TRUE(nullptr != cls);
  46. MS_LOG(INFO) << "" << cls->ToString();
  47. ASSERT_EQ(cls->tag(), Named(std::string("TestFoo")));
  48. ClassAttrVector attributes = cls->GetAttributes();
  49. ASSERT_EQ(attributes.size(), 2);
  50. for (auto &v : attributes) {
  51. if (v.first == std::string("x")) {
  52. ASSERT_TRUE(nullptr != dyn_cast<Float>(v.second));
  53. }
  54. if (v.first == std::string("y")) {
  55. ASSERT_TRUE(nullptr != dyn_cast<Int>(v.second));
  56. }
  57. }
  58. auto methods = cls->methods();
  59. ASSERT_EQ(methods.size(), 4);
  60. int counts = 0;
  61. for (auto &v : methods) {
  62. if (v.first == std::string("inf")) {
  63. counts++;
  64. }
  65. MS_LOG(INFO) << "" << v.first;
  66. }
  67. ASSERT_EQ(counts, 1);
  68. ValuePtr obj = std::make_shared<parse::ClassObject>(fn, "TestFoo");
  69. ValueNodePtr fn_node = NewValueNode(obj);
  70. AnfNodeConfigPtr fn_conf = std::make_shared<AnfNodeConfig>(nullptr, fn_node, nullptr, nullptr);
  71. AbstractBasePtr foo = ToAbstract(obj, nullptr, fn_conf);
  72. ASSERT_TRUE(foo != nullptr);
  73. AbstractBasePtr abstract_x = FromValue(1.1, true);
  74. AbstractBasePtr abstract_y = FromValue(static_cast<int64_t>(5), true);
  75. auto partical_func = dyn_cast<PartialAbstractClosure>(foo);
  76. AbstractBasePtrList args_spec_list = partical_func->args();
  77. ASSERT_GT(args_spec_list.size(), 0);
  78. AbstractScalarPtr abs_scalar = dyn_cast<AbstractScalar>(args_spec_list[0]);
  79. AbstractBasePtrList args_list = {abs_scalar, abstract_x, abstract_y};
  80. auto eval_impl = GetPrimitiveInferImpl(prim::kPrimMakeRecord);
  81. ASSERT_TRUE(nullptr != eval_impl.infer_shape_impl_);
  82. AbstractBasePtr new_cls = eval_impl.infer_shape_impl_(nullptr, prim::kPrimMakeRecord, args_list);
  83. ASSERT_TRUE(nullptr != new_cls);
  84. }
  85. } // namespace abstract
  86. } // namespace mindspore