program_patch.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from paddle.base.wrapped_decorator import signature_safe_contextmanager
  15. from . import Program
  16. _already_patch_program = False
  17. def monkey_patch_program():
  18. @signature_safe_contextmanager
  19. def _lr_schedule_guard(self, is_with_opt=False):
  20. # TODO(dev): Currently there has not equivalent of op_role in PIR
  21. # mode, so we simply remove _lr_schedule_guard here, this should
  22. # be fixed in the future.
  23. yield
  24. program_attrs = {
  25. "_lr_schedule_guard": _lr_schedule_guard,
  26. }
  27. global _already_patch_program
  28. if not _already_patch_program:
  29. for attr, value in program_attrs.items():
  30. setattr(Program, attr, value)
  31. _already_patch_program = True