|
| 1 | +# --------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2025 Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause |
| 4 | +# --------------------------------------------------------------------- |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import onnxruntime as ort |
| 12 | +import torch |
| 13 | +from qai_hub_models.models._shared.hf_whisper.app import HfWhisperApp, chunk_and_resample_audio |
| 14 | +from qai_hub_models.models._shared.hf_whisper.model import ( |
| 15 | + CHUNK_LENGTH, |
| 16 | + SAMPLE_RATE, |
| 17 | +) |
| 18 | +from transformers import WhisperProcessor |
| 19 | + |
| 20 | + |
| 21 | +def infer_audio(app, model_id, audio_file, save_data): |
| 22 | + audio_dict = np.load(audio_file, allow_pickle=True).item() |
| 23 | + |
| 24 | + audio = audio_dict["audio"]["array"] |
| 25 | + sample_rate = audio_dict["audio"]["sampling_rate"] |
| 26 | + audio_name = os.path.splitext(os.path.basename(audio_file))[0] if save_data else None |
| 27 | + |
| 28 | + processor = WhisperProcessor.from_pretrained(model_id) |
| 29 | + reference = processor.tokenizer._normalize(audio_dict["text"]) |
| 30 | + print("Reference: ", reference) |
| 31 | + |
| 32 | + # Perform transcription |
| 33 | + transcription = app.transcribe(audio, sample_rate, audio_name, save_data) |
| 34 | + print("done transcription") |
| 35 | + prediction = processor.tokenizer._normalize(transcription) |
| 36 | + print("Prediction:", prediction) |
| 37 | + |
| 38 | + |
| 39 | +class HfWhisperAppWithSave(HfWhisperApp): |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + encoder, |
| 43 | + decoder, |
| 44 | + hf_model_id: str, |
| 45 | + execution_provider: str = "CPUExecutionProvider", |
| 46 | + provider_options: dict = None, |
| 47 | + sample_rate: int = SAMPLE_RATE, |
| 48 | + max_audio_seconds: int = CHUNK_LENGTH, |
| 49 | + ): |
| 50 | + super().__init__(None, None, hf_model_id, sample_rate, max_audio_seconds) |
| 51 | + options = ort.SessionOptions() |
| 52 | + |
| 53 | + self.encoder = ort.InferenceSession( |
| 54 | + encoder, sess_options=options, providers=[execution_provider], provider_options=[provider_options] |
| 55 | + ) |
| 56 | + |
| 57 | + self.decoder = ort.InferenceSession( |
| 58 | + decoder, sess_options=options, providers=[execution_provider], provider_options=[provider_options] |
| 59 | + ) |
| 60 | + |
| 61 | + def transcribe_tokens(self, audio, sample_rate, audio_name, save_data=False) -> list[int]: |
| 62 | + out_chunked_tokens = [] |
| 63 | + for ind, x in enumerate(chunk_and_resample_audio(audio, sample_rate)): |
| 64 | + out_chunked_tokens.append(self._transcribe_single_chunk(x, audio_name, ind, save_data)) |
| 65 | + |
| 66 | + out_tokens: list[int] = [] |
| 67 | + for chunk_tokens in out_chunked_tokens: |
| 68 | + out_tokens.extend(chunk_tokens) |
| 69 | + return out_tokens |
| 70 | + |
| 71 | + def transcribe(self, audio, sample_rate, audio_name, save_data=False) -> str: |
| 72 | + tokens = self.transcribe_tokens(audio, sample_rate, audio_name, save_data) |
| 73 | + return self.tokenizer.decode(tokens, skip_special_tokens=True).strip() |
| 74 | + |
| 75 | + def _transcribe_single_chunk( |
| 76 | + self, audio: np.ndarray, audio_name=None, chunk_number=None, save_data=False |
| 77 | + ) -> list[int]: |
| 78 | + # feature |
| 79 | + input_features = self.feature_extractor(audio, sampling_rate=self.sample_rate, return_tensors="np")[ |
| 80 | + "input_features" |
| 81 | + ] |
| 82 | + |
| 83 | + # encoder |
| 84 | + output_names_encoder = [output.name for output in self.encoder.get_outputs()] |
| 85 | + # kv_cache_cross = self.encoder(input_features) |
| 86 | + input_features_feed = {"input_features": input_features} |
| 87 | + |
| 88 | + if save_data: |
| 89 | + input_features_save_path = os.path.join(save_data, audio_name, f"{chunk_number}_input_features.npy") |
| 90 | + os.makedirs(os.path.dirname(input_features_save_path), exist_ok=True) |
| 91 | + np.save(input_features_save_path, input_features_feed) |
| 92 | + |
| 93 | + kv_cache_cross_numpy = self.encoder.run(output_names_encoder, input_features_feed) |
| 94 | + kv_cache_cross = [torch.from_numpy(arr) for arr in kv_cache_cross_numpy] |
| 95 | + if not isinstance(kv_cache_cross, tuple): |
| 96 | + kv_cache_cross = (kv_cache_cross,) |
| 97 | + if not isinstance(kv_cache_cross[0], (tuple, list)): |
| 98 | + kv_cache_cross = (kv_cache_cross,) |
| 99 | + |
| 100 | + sot = self.config.decoder_start_token_id |
| 101 | + num_decoder_blocks = self.config.decoder_layers |
| 102 | + attention_dim = self.config.d_model |
| 103 | + num_decoder_heads = self.config.decoder_attention_heads |
| 104 | + mask_neg = self.config.mask_neg |
| 105 | + eot = self.config.eos_token_id |
| 106 | + |
| 107 | + # decoder |
| 108 | + output_ids = torch.tensor([[sot]]) # Start of transcript |
| 109 | + output_logits = [] |
| 110 | + output_length = output_ids.shape[1] |
| 111 | + |
| 112 | + position_ids = torch.tensor([0], dtype=torch.int32) |
| 113 | + attention_mask = torch.full( |
| 114 | + (1, 1, 1, self.mean_decode_len), |
| 115 | + mask_neg, |
| 116 | + dtype=torch.float32, |
| 117 | + ) |
| 118 | + |
| 119 | + # init kv_cache_self |
| 120 | + k_cache_self = torch.zeros( |
| 121 | + ( |
| 122 | + num_decoder_heads, |
| 123 | + 1, |
| 124 | + attention_dim // num_decoder_heads, |
| 125 | + self.mean_decode_len - 1, |
| 126 | + ), |
| 127 | + dtype=torch.float32, |
| 128 | + ) |
| 129 | + v_cache_self = torch.zeros( |
| 130 | + ( |
| 131 | + num_decoder_heads, |
| 132 | + 1, |
| 133 | + self.mean_decode_len - 1, |
| 134 | + attention_dim // num_decoder_heads, |
| 135 | + ), |
| 136 | + dtype=torch.float32, |
| 137 | + ) |
| 138 | + kv_cache_self = tuple((k_cache_self, v_cache_self) for _ in range(num_decoder_blocks)) |
| 139 | + |
| 140 | + for n in range(self.mean_decode_len - 1): |
| 141 | + # get current token |
| 142 | + input_ids = output_ids[:, n : n + 1].to(torch.int32) |
| 143 | + |
| 144 | + # update attention_mask |
| 145 | + attention_mask[:, :, :, self.mean_decode_len - n - 1] = 0.0 |
| 146 | + |
| 147 | + # flattened kv caches input |
| 148 | + flattened_kv_cache_self = tuple(item for sublist in kv_cache_self for item in sublist) |
| 149 | + flattened_kv_cache_cross = tuple(item for sublist in kv_cache_cross for item in sublist) |
| 150 | + |
| 151 | + # decode and update kv_cache_self |
| 152 | + decoder_input = ( |
| 153 | + (input_ids, attention_mask) + flattened_kv_cache_self + flattened_kv_cache_cross + (position_ids,) |
| 154 | + ) |
| 155 | + |
| 156 | + # print("decoder_input: ", decoder_input) |
| 157 | + input_names_decoder = [input.name for input in self.decoder.get_inputs()] |
| 158 | + output_names_decoder = [output.name for output in self.decoder.get_outputs()] |
| 159 | + |
| 160 | + # decoder_input_feed = dict(zip(input_names_decoder, decoder_input)) |
| 161 | + decoder_input_feed = { |
| 162 | + name: tensor.numpy() if isinstance(tensor, torch.Tensor) else tensor |
| 163 | + for name, tensor in zip(input_names_decoder, decoder_input) |
| 164 | + } |
| 165 | + |
| 166 | + if save_data: |
| 167 | + decoder_input_save_path = os.path.join(save_data, audio_name, f"{chunk_number}_{n}_decoder_input.npy") |
| 168 | + os.makedirs(os.path.dirname(decoder_input_save_path), exist_ok=True) |
| 169 | + np.save(decoder_input_save_path, decoder_input_feed) |
| 170 | + |
| 171 | + decoder_output_numpy = self.decoder.run(output_names_decoder, decoder_input_feed) |
| 172 | + decoder_output = [torch.from_numpy(arr) for arr in decoder_output_numpy] |
| 173 | + # decoder_output = self.decoder(*decoder_input) |
| 174 | + if isinstance(decoder_output, tuple) and len(decoder_output) == 2: |
| 175 | + logits, kv_cache_self = decoder_output |
| 176 | + else: |
| 177 | + logits = decoder_output[0] |
| 178 | + kv_cache_self = tuple(decoder_output[i : i + 2] for i in range(1, len(decoder_output), 2)) |
| 179 | + |
| 180 | + # update output_logits |
| 181 | + output_logits.append(logits.detach().clone()) |
| 182 | + |
| 183 | + # update output_ids |
| 184 | + output_id = torch.argmax(logits, 1).squeeze(0) |
| 185 | + # end of transcript |
| 186 | + if len(output_logits) == (self.mean_decode_len - 1) or output_id == eot: |
| 187 | + output_ids = torch.cat((output_ids, output_id), -1) |
| 188 | + break |
| 189 | + if n >= output_length - 1: |
| 190 | + output_ids = torch.cat((output_ids, output_id), -1) |
| 191 | + |
| 192 | + # update position_ids |
| 193 | + position_ids += 1 |
| 194 | + |
| 195 | + return output_ids[0].tolist() |
0 commit comments