inference_perf.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 models
  28. import torch
  29. import argparse
  30. import numpy as np
  31. import json
  32. import time
  33. from inference import checkpoint_from_distributed, unwrap_distributed, load_and_setup_model, MeasureTime
  34. import dllogger as DLLogger
  35. from dllogger import StdOutBackend, JSONStreamBackend, Verbosity
  36. from apex import amp
  37. def parse_args(parser):
  38. """
  39. Parse commandline arguments.
  40. """
  41. parser.add_argument('-m', '--model-name', type=str, default='', required=True,
  42. help='Model to train')
  43. parser.add_argument('-sr', '--sampling-rate', default=22050, type=int,
  44. help='Sampling rate')
  45. parser.add_argument('--amp-run', action='store_true',
  46. help='inference with AMP')
  47. parser.add_argument('-bs', '--batch-size', type=int, default=1)
  48. parser.add_argument('-o', '--output', type=str, required=True,
  49. help='Directory to save results')
  50. parser.add_argument('--log-file', type=str, default='nvlog.json',
  51. help='Filename for logging')
  52. return parser
  53. def main():
  54. """
  55. Launches inference benchmark.
  56. Inference is executed on a single GPU.
  57. """
  58. parser = argparse.ArgumentParser(
  59. description='PyTorch Tacotron 2 Inference')
  60. parser = parse_args(parser)
  61. args, _ = parser.parse_known_args()
  62. log_file = args.log_file
  63. DLLogger.init(backends=[JSONStreamBackend(Verbosity.DEFAULT,
  64. args.output+'/'+args.log_file),
  65. StdOutBackend(Verbosity.VERBOSE)])
  66. for k,v in vars(args).items():
  67. DLLogger.log(step="PARAMETER", data={k:v})
  68. DLLogger.log(step="PARAMETER", data={'model_name':'Tacotron2_PyT'})
  69. model = load_and_setup_model(args.model_name, parser, None, args.amp_run,
  70. forward_is_infer=True)
  71. if args.model_name == "Tacotron2":
  72. model = torch.jit.script(model)
  73. warmup_iters = 3
  74. num_iters = 1+warmup_iters
  75. for i in range(num_iters):
  76. measurements = {}
  77. if args.model_name == 'Tacotron2':
  78. text_padded = torch.randint(low=0, high=148, size=(args.batch_size, 140),
  79. dtype=torch.long).cuda()
  80. input_lengths = torch.IntTensor([text_padded.size(1)]*args.batch_size).cuda().long()
  81. with torch.no_grad(), MeasureTime(measurements, "inference_time"):
  82. mels, _, _ = model(text_padded, input_lengths)
  83. num_items = mels.size(0)*mels.size(2)
  84. if args.model_name == 'WaveGlow':
  85. n_mel_channels = model.upsample.in_channels
  86. num_mels = 895
  87. mel_padded = torch.zeros(args.batch_size, n_mel_channels,
  88. num_mels).normal_(-5.62, 1.98).cuda()
  89. if args.amp_run:
  90. mel_padded = mel_padded.half()
  91. with torch.no_grad(), MeasureTime(measurements, "inference_time"):
  92. audios = model(mel_padded)
  93. audios = audios.float()
  94. num_items = audios.size(0)*audios.size(1)
  95. if i >= warmup_iters:
  96. DLLogger.log(step=(i-warmup_iters,), data={"latency": measurements['inference_time']})
  97. DLLogger.log(step=(i-warmup_iters,), data={"items_per_sec": num_items/measurements['inference_time']})
  98. DLLogger.log(step=tuple(),
  99. data={'infer_latency': measurements['inference_time']})
  100. DLLogger.log(step=tuple(),
  101. data={'infer_items_per_sec': num_items/measurements['inference_time']})
  102. DLLogger.flush()
  103. if __name__ == '__main__':
  104. main()