profiler.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (c) 2021 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. import sys
  15. import paddle
  16. # A global variable to record the number of calling times for profiler
  17. # functions. It is used to specify the tracing range of training steps.
  18. _profiler_step_id = 0
  19. # A global variable to avoid parsing from string every time.
  20. _profiler_options = None
  21. class ProfilerOptions(object):
  22. '''
  23. Use a string to initialize a ProfilerOptions.
  24. The string should be in the format: "key1=value1;key2=value;key3=value3".
  25. For example:
  26. "profile_path=model.profile"
  27. "batch_range=[50, 60]; profile_path=model.profile"
  28. "batch_range=[50, 60]; tracer_option=OpDetail; profile_path=model.profile"
  29. ProfilerOptions supports following key-value pair:
  30. batch_range - a integer list, e.g. [100, 110].
  31. state - a string, the optional values are 'CPU', 'GPU' or 'All'.
  32. sorted_key - a string, the optional values are 'calls', 'total',
  33. 'max', 'min' or 'ave.
  34. tracer_option - a string, the optional values are 'Default', 'OpDetail',
  35. 'AllOpDetail'.
  36. profile_path - a string, the path to save the serialized profile data,
  37. which can be used to generate a timeline.
  38. exit_on_finished - a boolean.
  39. '''
  40. def __init__(self, options_str):
  41. assert isinstance(options_str, str)
  42. self._options = {
  43. 'batch_range': [10, 20],
  44. 'state': 'All',
  45. 'sorted_key': 'total',
  46. 'tracer_option': 'Default',
  47. 'profile_path': '/tmp/profile',
  48. 'exit_on_finished': True
  49. }
  50. self._parse_from_string(options_str)
  51. def _parse_from_string(self, options_str):
  52. for kv in options_str.replace(' ', '').split(';'):
  53. key, value = kv.split('=')
  54. if key == 'batch_range':
  55. value_list = value.replace('[', '').replace(']', '').split(',')
  56. value_list = list(map(int, value_list))
  57. if len(value_list) >= 2 and value_list[0] >= 0 and value_list[
  58. 1] > value_list[0]:
  59. self._options[key] = value_list
  60. elif key == 'exit_on_finished':
  61. self._options[key] = value.lower() in ("yes", "true", "t", "1")
  62. elif key in [
  63. 'state', 'sorted_key', 'tracer_option', 'profile_path'
  64. ]:
  65. self._options[key] = value
  66. def __getitem__(self, name):
  67. if self._options.get(name, None) is None:
  68. raise ValueError(
  69. "ProfilerOptions does not have an option named %s." % name)
  70. return self._options[name]
  71. def add_profiler_step(options_str=None):
  72. '''
  73. Enable the operator-level timing using PaddlePaddle's profiler.
  74. The profiler uses a independent variable to count the profiler steps.
  75. One call of this function is treated as a profiler step.
  76. Args:
  77. profiler_options - a string to initialize the ProfilerOptions.
  78. Default is None, and the profiler is disabled.
  79. '''
  80. if options_str is None:
  81. return
  82. global _profiler_step_id
  83. global _profiler_options
  84. if _profiler_options is None:
  85. _profiler_options = ProfilerOptions(options_str)
  86. if _profiler_step_id == _profiler_options['batch_range'][0]:
  87. paddle.utils.profiler.start_profiler(_profiler_options['state'],
  88. _profiler_options['tracer_option'])
  89. elif _profiler_step_id == _profiler_options['batch_range'][1]:
  90. paddle.utils.profiler.stop_profiler(_profiler_options['sorted_key'],
  91. _profiler_options['profile_path'])
  92. if _profiler_options['exit_on_finished']:
  93. sys.exit(0)
  94. _profiler_step_id += 1