benchmark_hooks.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import time
  17. import tensorflow as tf
  18. import dllogger
  19. from .training_hooks import MeanAccumulator
  20. __all__ = ['BenchmarkLoggingHook']
  21. class BenchmarkLoggingHook(tf.train.SessionRunHook):
  22. def __init__(self, global_batch_size, warmup_steps=100):
  23. self.warmup_steps = warmup_steps
  24. self.global_batch_size = global_batch_size
  25. self.current_step = 0
  26. self.t0 = None
  27. self.mean_throughput = MeanAccumulator()
  28. def before_run(self, run_context):
  29. self.t0 = time.time()
  30. def after_run(self, run_context, run_values):
  31. batch_time = time.time() - self.t0
  32. samplesps = self.global_batch_size / batch_time
  33. if self.current_step >= self.warmup_steps:
  34. self.mean_throughput.consume(samplesps)
  35. dllogger.log(data={"samplesps" : samplesps}, step=(0, self.current_step))
  36. self.current_step += 1