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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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->HasAttr("is_load")) {
  49. return;
  50. }
  51. if (prim->HasPyEvaluator()) {
  52. MS_LOG(DEBUG) << "Primitive " << prim->name() << " has python evaluator.";
  53. return;
  54. }
  55. if (prim->prim_type() == PrimType::kPrimTypePyInferCheck) {
  56. MS_LOG(DEBUG) << "Primitive " << prim->name() << " has python inference checking method.";
  57. return;
  58. }
  59. if (prim->name() == "fake_bprop") {
  60. MS_LOG(EXCEPTION) << "Illegal primitive: " << GetValue<std::string>(prim->GetAttr("info"));
  61. }
  62. MS_LOG(EXCEPTION) << "Illegal primitive: " << prim->name();
  63. }
  64. bool CheckAbstractScalar(const AnfNodePtr &node) {
  65. AbstractBasePtr ptrBase = node->abstract();
  66. if (ptrBase->isa<AbstractScalar>()) {
  67. TypePtr ptrType = ptrBase->GetTypeTrack();
  68. MS_EXCEPTION_IF_NULL(ptrType);
  69. if (ptrType->isa<EnvType>()) {
  70. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString() << " for node=" << node->DebugString();
  71. }
  72. if (ptrType->isa<Problem>() || ptrType->isa<External>()) {
  73. // only send string in external
  74. if (!IsValueNode<StringImm>(node)) {
  75. // Validate a type.
  76. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString()
  77. << " for node=" << node->DebugString();
  78. }
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. void ValidateAbstract(const AnfNodePtr &node) {
  85. if (node == nullptr) {
  86. MS_LOG(DEBUG) << "Node to validate is invalid";
  87. return;
  88. }
  89. AbstractBasePtr ptrBase = node->abstract();
  90. if (ptrBase == nullptr) {
  91. MS_LOG(DEBUG) << "Abstract is null in node: " << node->DebugString();
  92. return;
  93. }
  94. if (ptrBase->isa<AbstractClass>() || ptrBase->isa<AbstractJTagged>()) {
  95. // Validate a type.
  96. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString() << " for node=" << node->DebugString();
  97. }
  98. if (CheckAbstractScalar(node)) {
  99. return;
  100. }
  101. if (ptrBase->isa<AbstractError>()) {
  102. // NOTICE: validate dead code?
  103. MS_LOG(DEBUG) << "AbstractError in the graph: " << ptrBase->ToString();
  104. return;
  105. }
  106. bool checkAbstractIslegal =
  107. ptrBase->isa<AbstractType>() || ptrBase->isa<AbstractFunction>() || ptrBase->isa<AbstractTuple>() ||
  108. ptrBase->isa<AbstractList>() || ptrBase->isa<AbstractTensor>() || ptrBase->isa<AbstractRowTensor>() ||
  109. ptrBase->isa<AbstractSparseTensor>() || ptrBase->isa<abstract::AbstractRefKey>() || ptrBase->isa<AbstractRef>() ||
  110. ptrBase->isa<abstract::AbstractNone>() || ptrBase->isa<abstract::AbstractMonad>();
  111. if (checkAbstractIslegal) {
  112. return;
  113. }
  114. // Other types show exception
  115. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString();
  116. }
  117. void Validate(const FuncGraphPtr &fg) {
  118. FuncGraphManagerPtr mgr = Manage(fg, false);
  119. MS_EXCEPTION_IF_NULL(mgr);
  120. AnfNodeSet &all_nodes = mgr->all_nodes();
  121. for (const auto &anf_node : all_nodes) {
  122. ValidateOperation(anf_node);
  123. ValidateAbstract(anf_node);
  124. }
  125. }
  126. } // namespace validator
  127. } // namespace mindspore