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.

validator.cc 4.3 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "pipeline/jit/validator.h"
  19. #include <memory>
  20. #include <mutex>
  21. #include "ir/manager.h"
  22. #include "ir/dtype.h"
  23. #include "pipeline/jit/static_analysis/prim.h"
  24. namespace mindspore {
  25. namespace validator {
  26. using mindspore::abstract::AbstractBase;
  27. using mindspore::abstract::AbstractClass;
  28. using mindspore::abstract::AbstractError;
  29. using mindspore::abstract::AbstractFunction;
  30. using mindspore::abstract::AbstractJTagged;
  31. using mindspore::abstract::AbstractList;
  32. using mindspore::abstract::AbstractRef;
  33. using mindspore::abstract::AbstractRowTensor;
  34. using mindspore::abstract::AbstractScalar;
  35. using mindspore::abstract::AbstractSparseTensor;
  36. using mindspore::abstract::AbstractTensor;
  37. using mindspore::abstract::AbstractTuple;
  38. using mindspore::abstract::AbstractType;
  39. void ValidateOperation(const AnfNodePtr &node) {
  40. if (!IsValueNode<Primitive>(node)) {
  41. return;
  42. }
  43. // Primitive must in whitelist
  44. auto prim = GetValueNode<PrimitivePtr>(node);
  45. if (abstract::IsInWhiteList(prim)) {
  46. return;
  47. }
  48. if (prim->HasPyEvaluator()) {
  49. MS_LOG(DEBUG) << "Primitive " << prim->name() << " has python evaluator.";
  50. return;
  51. }
  52. if (prim->prim_type() == PrimType::kPrimTypePyInferCheck) {
  53. MS_LOG(DEBUG) << "Primitive " << prim->name() << " has python inference checking method.";
  54. return;
  55. }
  56. if (prim->name() == "fake_bprop") {
  57. MS_LOG(EXCEPTION) << "Illegal primitive: " << GetValue<std::string>(prim->GetAttr("info"));
  58. }
  59. MS_LOG(EXCEPTION) << "Illegal primitive: " << prim->name();
  60. }
  61. void ValidateAbstract(const AnfNodePtr &node) {
  62. if (node == nullptr) {
  63. MS_LOG(DEBUG) << "Node to validate is invalid";
  64. return;
  65. }
  66. AbstractBasePtr ptrBase = node->abstract();
  67. if (ptrBase == nullptr) {
  68. MS_LOG(DEBUG) << "Abstract is null in node: " << node->DebugString();
  69. return;
  70. }
  71. if (ptrBase->isa<AbstractClass>() || ptrBase->isa<AbstractJTagged>()) {
  72. // Validate a type.
  73. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString();
  74. }
  75. if (ptrBase->isa<AbstractScalar>()) {
  76. TypePtr ptrType = ptrBase->GetTypeTrack();
  77. MS_EXCEPTION_IF_NULL(ptrType);
  78. if (ptrType->isa<Problem>() || ptrType->isa<External>()) {
  79. // only send string in external
  80. if (!IsValueNode<StringImm>(node)) {
  81. // Validate a type.
  82. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString()
  83. << " for node=" << node->DebugString();
  84. }
  85. }
  86. return;
  87. }
  88. if (ptrBase->isa<AbstractError>()) {
  89. // NOTICE: validate dead code?
  90. MS_LOG(DEBUG) << "AbstractError in the graph: " << ptrBase->ToString();
  91. return;
  92. }
  93. if (ptrBase->isa<AbstractType>() || ptrBase->isa<AbstractFunction>() || ptrBase->isa<AbstractTuple>() ||
  94. ptrBase->isa<AbstractList>() || ptrBase->isa<AbstractTensor>() || ptrBase->isa<AbstractRowTensor>() ||
  95. ptrBase->isa<AbstractSparseTensor>() || ptrBase->isa<abstract::AbstractRefKey>() || ptrBase->isa<AbstractRef>()) {
  96. return;
  97. }
  98. if (ptrBase->isa<abstract::AbstractNone>()) {
  99. return;
  100. }
  101. // UMonad or IOMonad
  102. if (ptrBase->isa<abstract::AbstractMonad>()) {
  103. return;
  104. }
  105. // Other types show exception
  106. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString();
  107. }
  108. void Validate(const FuncGraphPtr &fg) {
  109. FuncGraphManagerPtr mgr = Manage(fg, false);
  110. MS_EXCEPTION_IF_NULL(mgr);
  111. AnfNodeSet &all_nodes = mgr->all_nodes();
  112. for (const auto &anf_node : all_nodes) {
  113. ValidateOperation(anf_node);
  114. ValidateAbstract(anf_node);
  115. }
  116. }
  117. } // namespace validator
  118. } // namespace mindspore