inference_perf.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # *****************************************************************************
  2. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of the NVIDIA CORPORATION nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #
  26. # *****************************************************************************
  27. import argparse
  28. import json
  29. import time
  30. import torch
  31. import numpy as np
  32. import dllogger as DLLogger
  33. from apex import amp
  34. from dllogger import StdOutBackend, JSONStreamBackend, Verbosity
  35. import models
  36. from inference import load_and_setup_model, MeasureTime
  37. def parse_args(parser):
  38. """
  39. Parse commandline arguments.
  40. """
  41. parser.add_argument('--amp-run', action='store_true',
  42. help='inference with AMP')
  43. parser.add_argument('-bs', '--batch-size', type=int, default=1)
  44. parser.add_argument('-o', '--output', type=str, required=True,
  45. help='Directory to save results')
  46. parser.add_argument('--log-file', type=str, default='nvlog.json',
  47. help='Filename for logging')
  48. return parser
  49. def main():
  50. """
  51. Launches inference benchmark.
  52. Inference is executed on a single GPU.
  53. """
  54. parser = argparse.ArgumentParser(
  55. description='PyTorch FastPitch Inference Benchmark')
  56. parser = parse_args(parser)
  57. args, _ = parser.parse_known_args()
  58. log_file = args.log_file
  59. DLLogger.init(backends=[JSONStreamBackend(Verbosity.DEFAULT,
  60. args.log_file),
  61. StdOutBackend(Verbosity.VERBOSE)])
  62. for k,v in vars(args).items():
  63. DLLogger.log(step="PARAMETER", data={k:v})
  64. DLLogger.log(step="PARAMETER", data={'model_name': 'FastPitch_PyT'})
  65. model = load_and_setup_model('FastPitch', parser, None, args.amp_run,
  66. 'cuda', unk_args=[], forward_is_infer=True,
  67. ema=False, jitable=True)
  68. # FIXME Temporarily disabled due to nn.LayerNorm fp16 casting bug in pytorch:20.02-py3 and 20.03
  69. # model = torch.jit.script(model)
  70. warmup_iters = 3
  71. iters = 1
  72. gen_measures = MeasureTime()
  73. all_frames = 0
  74. for i in range(-warmup_iters, iters):
  75. text_padded = torch.randint(low=0, high=148, size=(args.batch_size, 128),
  76. dtype=torch.long).to('cuda')
  77. input_lengths = torch.IntTensor([text_padded.size(1)] * args.batch_size).to('cuda')
  78. durs = torch.ones_like(text_padded).mul_(4).to('cuda')
  79. with torch.no_grad(), gen_measures:
  80. mels, *_ = model(text_padded, input_lengths, dur_tgt=durs)
  81. num_frames = mels.size(0) * mels.size(2)
  82. if i >= 0:
  83. all_frames += num_frames
  84. DLLogger.log(step=(i,), data={"latency": gen_measures[-1]})
  85. DLLogger.log(step=(i,), data={"frames/s": num_frames / gen_measures[-1]})
  86. measures = gen_measures[warmup_iters:]
  87. DLLogger.log(step=(), data={'avg latency': np.mean(measures)})
  88. DLLogger.log(step=(), data={'avg frames/s': all_frames / np.sum(measures)})
  89. DLLogger.flush()
  90. if __name__ == '__main__':
  91. main()