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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/validator.h"
  19. #include <memory>
  20. #include <mutex>
  21. #include "ir/manager.h"
  22. #include "ir/dtype.h"
  23. #include "./common.h"
  24. #include "pipeline/static_analysis/prim.h"
  25. namespace mindspore {
  26. namespace validator {
  27. using mindspore::abstract::AbstractBase;
  28. using mindspore::abstract::AbstractClass;
  29. using mindspore::abstract::AbstractError;
  30. using mindspore::abstract::AbstractFunction;
  31. using mindspore::abstract::AbstractIndexedSlices;
  32. using mindspore::abstract::AbstractJTagged;
  33. using mindspore::abstract::AbstractList;
  34. using mindspore::abstract::AbstractScalar;
  35. using mindspore::abstract::AbstractTensor;
  36. using mindspore::abstract::AbstractTuple;
  37. using mindspore::abstract::AbstractType;
  38. void ValidateOperation(const AnfNodePtr &node) {
  39. if (!IsValueNode<Primitive>(node)) {
  40. return;
  41. }
  42. // Primitive must in whitelist
  43. PrimitivePtr prim = GetValueNode<PrimitivePtr>(node);
  44. if (abstract::IsInWhiteList(prim)) {
  45. return;
  46. }
  47. if (prim->HasPyEvaluator()) {
  48. MS_LOG(DEBUG) << "Primitive " << prim->name() << " has python evaluator.";
  49. return;
  50. }
  51. if (prim->name() == "fake_bprop") {
  52. MS_LOG(EXCEPTION) << "Illegal primitive: " << GetValue<std::string>(prim->GetAttr("info"));
  53. }
  54. MS_LOG(EXCEPTION) << "Illegal primitive: " << prim->name();
  55. }
  56. void ValidateAbstract(const AnfNodePtr &node) {
  57. if (node == nullptr) {
  58. MS_LOG(DEBUG) << "Node to validate is invalid";
  59. return;
  60. }
  61. AbstractBasePtr ptrBase = node->abstract();
  62. if (ptrBase == nullptr) {
  63. MS_LOG(DEBUG) << "Abstract is null in node: " << node->DebugString();
  64. return;
  65. }
  66. if (ptrBase->isa<AbstractClass>() || ptrBase->isa<AbstractJTagged>()) {
  67. // Validate a type.
  68. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString();
  69. }
  70. if (ptrBase->isa<AbstractScalar>()) {
  71. TypePtr ptrType = ptrBase->GetTypeTrack();
  72. MS_EXCEPTION_IF_NULL(ptrType);
  73. if (ptrType->isa<Problem>() || ptrType->isa<External>()) {
  74. // only send string in external
  75. if (!IsValueNode<StringImm>(node)) {
  76. // Validate a type.
  77. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString();
  78. }
  79. }
  80. return;
  81. }
  82. if (ptrBase->isa<AbstractError>()) {
  83. // NOTICE: validate dead code?
  84. MS_LOG(DEBUG) << "AbstractError in the graph: " << ptrBase->ToString();
  85. return;
  86. }
  87. if (ptrBase->isa<AbstractType>() || ptrBase->isa<AbstractFunction>() || ptrBase->isa<AbstractTuple>() ||
  88. ptrBase->isa<AbstractList>() || ptrBase->isa<AbstractTensor>() || ptrBase->isa<AbstractIndexedSlices>() ||
  89. ptrBase->isa<abstract::AbstractRefKey>()) {
  90. return;
  91. }
  92. if (ptrBase->isa<abstract::AbstractNone>()) {
  93. return;
  94. }
  95. // Other types show exception
  96. MS_LOG(EXCEPTION) << "Illegal type in the graph: " << ptrBase->ToString();
  97. }
  98. void Validate(const FuncGraphPtr &fg) {
  99. FuncGraphManagerPtr mgr = Manage(fg, false);
  100. MS_EXCEPTION_IF_NULL(mgr);
  101. AnfNodeSet &all_nodes = mgr->all_nodes();
  102. for (const auto &anf_node : all_nodes) {
  103. ValidateOperation(anf_node);
  104. ValidateAbstract(anf_node);
  105. }
  106. }
  107. } // namespace validator
  108. } // namespace mindspore