in phi/run_eval.py [0:0]
def main(args):
model = AutoModelForCausalLM.from_pretrained(
args.model_id,
trust_remote_code=True,
torch_dtype="auto",
_attn_implementation="flash_attention_2",
).to(args.device)
model.eval()
processor = AutoProcessor.from_pretrained(args.model_id, trust_remote_code=True)
user = "<|user|>"
assistant = "<|assistant|>"
prompt_suffix = "<|end|>"
prompt = f"{user}<|audio_1|>{args.user_prompt}{prompt_suffix}{assistant}"
gen_kwargs = {"max_new_tokens": args.max_new_tokens, "num_beams": args.num_beams}
stop_tokens = [prompt_suffix, processor.tokenizer.eos_token]
stop_tokens_ids = processor.tokenizer(stop_tokens, add_special_tokens=False, padding="longest", return_tensors="pt")["input_ids"]
stop_tokens_ids = stop_tokens_ids.to(model.device)
def benchmark(batch, min_new_tokens=None):
# Load audio inputs
audios = [(audio["array"], audio["sampling_rate"]) for audio in batch["audio"]]
minibatch_size = len(audios)
gen_kwargs["stopping_criteria"] = StoppingCriteriaList(
[MultipleTokenBatchStoppingCriteria(stop_tokens_ids, batch_size=args.num_beams * minibatch_size)]
)
# START TIMING
start_time = time.time()
with torch.autocast(model.device.type, enabled=True):
inputs = processor(text=[prompt] * minibatch_size, audios=audios, return_tensors="pt").to(args.device)
# Model Inference
pred_ids = model.generate(
**inputs,
pad_token_id=processor.tokenizer.pad_token_id,
eos_token_id=processor.tokenizer.eos_token_id,
**gen_kwargs,
min_new_tokens=min_new_tokens,
)
# Gather the sequence index of the stop token
stop_tokens_idx = gen_kwargs["stopping_criteria"][0].stop_tokens_idx.reshape(minibatch_size, -1)[:, 0]
# If a stop token was produced, we need to remove its length from the found index,
# however there might be a chance that the stop token was not produced and the index
# returned is the length of the generated sequence
stop_tokens_idx = torch.where(
stop_tokens_idx > 0,
stop_tokens_idx - stop_tokens_ids.shape[-1],
pred_ids.shape[-1],
)
# Convert token ids to text transcription
pred_text = [
processor.decode(_pred_ids[inputs["input_ids"].shape[1] : _stop_tokens_idx], skip_special_tokens=True, clean_up_tokenization_spaces=False)
for _pred_ids, _stop_tokens_idx in zip(pred_ids, stop_tokens_idx)
]
# END TIMING
runtime = time.time() - start_time
# normalize by minibatch size since we want the per-sample time
batch["transcription_time_s"] = minibatch_size * [runtime / minibatch_size]
# normalize transcriptions with English normalizer
batch["predictions"] = [data_utils.normalizer(pred) for pred in pred_text]
batch["references"] = batch["norm_text"]
return batch
if args.warmup_steps is not None:
dataset = data_utils.load_data(args)
dataset = data_utils.prepare_data(dataset)
num_warmup_samples = args.warmup_steps * args.batch_size
if args.streaming:
warmup_dataset = dataset.take(num_warmup_samples)
else:
warmup_dataset = dataset.select(range(min(num_warmup_samples, len(dataset))))
warmup_dataset = iter(warmup_dataset.map(benchmark, batch_size=args.batch_size, batched=True, fn_kwargs={"min_new_tokens": args.max_new_tokens}))
for _ in tqdm(warmup_dataset, desc="Warming up..."):
continue
dataset = data_utils.load_data(args)
if args.max_eval_samples is not None and args.max_eval_samples > 0:
print(f"Subsampling dataset to first {args.max_eval_samples} samples!")
if args.streaming:
dataset = dataset.take(args.max_eval_samples)
else:
dataset = dataset.select(range(min(args.max_eval_samples, len(dataset))))
dataset = data_utils.prepare_data(dataset)
dataset = dataset.map(
benchmark, batch_size=args.batch_size, batched=True, remove_columns=["audio"],
)
all_results = {
"audio_length_s": [],
"transcription_time_s": [],
"predictions": [],
"references": [],
}
result_iter = iter(dataset)
for result in tqdm(result_iter, desc="Samples..."):
for key in all_results:
all_results[key].append(result[key])
# Write manifest results (WER and RTFX)
manifest_path = data_utils.write_manifest(
all_results["references"],
all_results["predictions"],
args.model_id,
args.dataset_path,
args.dataset,
args.split,
audio_length=all_results["audio_length_s"],
transcription_time=all_results["transcription_time_s"],
)
print("Results saved at path:", os.path.abspath(manifest_path))
wer = wer_metric.compute(
references=all_results["references"], predictions=all_results["predictions"]
)
wer = round(100 * wer, 2)
rtfx = round(sum(all_results["audio_length_s"]) / sum(all_results["transcription_time_s"]), 2)
print("WER:", wer, "%", "RTFx:", rtfx)