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

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