op_version_registry_test.cc 5.3 KB
Newer Older
1
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

#include <gtest/gtest.h>

#include "paddle/fluid/framework/op_version_registry.h"

namespace paddle {
namespace framework {
namespace compatible {

TEST(test_operator_version, test_operator_version) {
24
  REGISTER_OP_VERSION(op_name__)
W
Wilber 已提交
25 26 27 28
      .AddCheckpoint(
          R"ROC(Fix the bug of reshape op, support the case of axis < 0)ROC",
          framework::compatible::OpVersionDesc().BugfixWithBehaviorChanged(
              "Support the case of axis < 0"))
29 30 31 32 33 34 35 36 37
      .AddCheckpoint(
          R"ROC(
        Upgrade reshape, modified one attribute [axis] and add a new attribute [size].
      )ROC",
          framework::compatible::OpVersionDesc()
              .ModifyAttr("axis",
                          "Increased from the original one method to two.", -1)
              .NewAttr("size",
                       "In order to represent a two-dimensional rectangle, the "
38 39
                       "parameter size is added.",
                       0))
40 41 42 43 44 45 46
      .AddCheckpoint(
          R"ROC(
        Add a new attribute [height]
      )ROC",
          framework::compatible::OpVersionDesc().NewAttr(
              "height",
              "In order to represent a two-dimensional rectangle, the "
47
              "parameter height is added.",
48 49 50 51 52 53 54 55
              0))
      .AddCheckpoint(
          R"ROC(
        Add a input [X2] and a output [Y2]
      )ROC",
          framework::compatible::OpVersionDesc()
              .NewInput("X2", "The second input.")
              .NewOutput("Y2", "The second output."));
56 57 58 59 60 61 62 63 64 65 66 67 68

  REGISTER_OP_VERSION(op_name_0__)
      .AddCheckpoint(
          R"ROC(
        Incompatible upgrade of attribute [height], input [X2] and output [Y2]
      )ROC",
          framework::compatible::OpVersionDesc()
              .DeleteAttr("height",
                          "Parameters deleted due to interface alignment.")
              .ModifyInput("X2", "Modify input due to interface alignment.")
              .ModifyOutput("Y2", "Modify output due to interface alignment.")
              .DeleteInput("X2", "Delete input due to interface alignment.")
              .DeleteOutput("Y2", "Delete output due to interface alignment."));
69
}
S
Shang Zhizhou 已提交
70 71

TEST(test_pass_op_version_checker, test_pass_op_version_checker) {
72
  const std::string fake_op_name{"op_name__"};
73 74 75 76
  ASSERT_FALSE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "no_registered_capability_pass"));

  REGISTER_PASS_CAPABILITY(no_bind_pass);
S
Shang Zhizhou 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  ASSERT_TRUE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "no_bind_pass"));

  REGISTER_PASS_CAPABILITY(test_pass1)
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
              .LE("mul", 1)
              .EQ("fc", 0));
  ASSERT_TRUE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "test_pass1"));

  REGISTER_PASS_CAPABILITY(test_pass2)
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
              .GE("mul", 0)
              .NE("fc", 0));
  ASSERT_FALSE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "test_pass2"));

  REGISTER_PASS_CAPABILITY(test_pass3)
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
              .GE("mul", 0)
              .NE("fc", 0))
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
              .LE("mul", 1)
              .EQ("fc", 0));
  ASSERT_TRUE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "test_pass3"));

  REGISTER_PASS_CAPABILITY(test_pass4)
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
111
              .GE(fake_op_name, 5)
S
Shang Zhizhou 已提交
112 113 114 115 116 117 118
              .EQ("fc", 0));
  ASSERT_FALSE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "test_pass4"));

  REGISTER_PASS_CAPABILITY(test_pass5)
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
119
              .GE(fake_op_name, 4)
S
Shang Zhizhou 已提交
120 121 122 123 124 125 126
              .EQ("fc", 0));
  ASSERT_TRUE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "test_pass5"));

  REGISTER_PASS_CAPABILITY(test_pass6)
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
127
              .EQ(fake_op_name, 4)
S
Shang Zhizhou 已提交
128 129 130 131 132 133 134
              .EQ("fc", 0));
  ASSERT_TRUE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "test_pass6"));

  REGISTER_PASS_CAPABILITY(test_pass7)
      .AddCombination(
          paddle::framework::compatible::OpVersionComparatorCombination()
135
              .NE(fake_op_name, 4)
S
Shang Zhizhou 已提交
136 137 138 139 140
              .EQ("fc", 0));
  ASSERT_FALSE(PassVersionCheckerRegistrar::GetInstance().IsPassCompatible(
      "test_pass7"));
}

141 142 143
}  // namespace compatible
}  // namespace framework
}  // namespace paddle