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

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