|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright (c) 2024, Ampere Computing LLC |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import torch |
| 6 | + |
| 7 | + |
| 8 | +def run_pytorch_fp32(model_name, num_runs, timeout, dataset_path, **kwargs): |
| 9 | + batch_size = 1 |
| 10 | + sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "whisper")) |
| 11 | + from utils.benchmark import run_model |
| 12 | + from utils.misc import print_warning_message |
| 13 | + from utils.pytorch import PyTorchRunnerV2 |
| 14 | + from utils.speech_recognition.covost2 import Covost2 |
| 15 | + from speech_recognition.whisper.whisper.whisper import load_model |
| 16 | + from speech_recognition.whisper.whisper.whisper.transcribe import transcribe |
| 17 | + model = load_model(model_name) |
| 18 | + model.eval() |
| 19 | + |
| 20 | + def single_pass_pytorch(_runner, _covost2): |
| 21 | + array = _covost2.get_input_array() |
| 22 | + audio = torch.from_numpy(array.astype("float32")) |
| 23 | + _covost2.submit_translation( |
| 24 | + _runner.run(batch_size * array.shape[0], audio)["text"].lstrip().replace(".", "") |
| 25 | + ) |
| 26 | + |
| 27 | + def translate_wrapper(audio): |
| 28 | + return transcribe(model, audio, verbose=None, task="translate", language="ja") |
| 29 | + |
| 30 | + runner = PyTorchRunnerV2(translate_wrapper, throughput_only=True) |
| 31 | + librispeech = Covost2(dataset_path) |
| 32 | + print_warning_message("Sampling rate Whisper operates at is 16,000 Hz, therefore throughput values below can be " |
| 33 | + "divided by 16,000 to derive 'seconds of processed audio per second'") |
| 34 | + return run_model(single_pass_pytorch, runner, librispeech, batch_size, num_runs, timeout) |
| 35 | + |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + from utils.helpers import DefaultArgParser |
| 39 | + whisper_variants = ["tiny", "base", "small", "medium", "large"] |
| 40 | + parser = DefaultArgParser(["pytorch"]) |
| 41 | + parser.require_model_name(whisper_variants) |
| 42 | + parser.add_argument("--dataset_path", type=str, required=True, |
| 43 | + help="path to the CommonVoice Japanese dataset directory") |
| 44 | + run_pytorch_fp32(**vars(parser.parse())) |
0 commit comments