|
| 1 | +defmodule Emily.Conformance.WhisperFullTest do |
| 2 | + @moduledoc """ |
| 3 | + Full `openai/whisper-tiny` end-to-end conformance test. |
| 4 | +
|
| 5 | + Like the other `*_full` suites, this is excluded even from |
| 6 | + `mix test --only conformance`: the model is ~150 MB on first |
| 7 | + fetch. Run explicitly: |
| 8 | +
|
| 9 | + mix test --only whisper_full |
| 10 | +
|
| 11 | + Uses a deterministic synthetic mel-features tensor rather than a |
| 12 | + checked-in audio fixture, by the same reasoning as |
| 13 | + `vit_full_test.exs`: binary test assets in git are annoying, and |
| 14 | + the intent is to catch numerical drift on real-size weight |
| 15 | + tensors (encoder conv frontend + 4 encoder blocks + 4 decoder |
| 16 | + blocks with cross-attention), not to verify mel-spectrogram |
| 17 | + computation. Input shape is Whisper's canonical 30-second window: |
| 18 | + 3000 time-steps × 80 mel bins. |
| 19 | +
|
| 20 | + A failure means the backend has drifted, Bumblebee's Whisper port |
| 21 | + has changed, or the HF checkpoint has been republished — all of |
| 22 | + which are real signals. |
| 23 | + """ |
| 24 | + |
| 25 | + use ExUnit.Case, async: false |
| 26 | + use Emily.ConformanceHelper |
| 27 | + |
| 28 | + @moduletag :whisper_full |
| 29 | + @moduletag capture_log: true |
| 30 | + @moduletag timeout: 600_000 |
| 31 | + |
| 32 | + test "openai/whisper-tiny forward pass matches pinned logits slice" do |
| 33 | + {:ok, %{model: model, params: params, spec: spec}} = |
| 34 | + Bumblebee.load_model({:hf, "openai/whisper-tiny"}) |
| 35 | + |
| 36 | + assert %Bumblebee.Audio.Whisper{architecture: :for_conditional_generation} = spec |
| 37 | + |
| 38 | + # Synthetic 30 s mel window. Nx.iota + sin produces a |
| 39 | + # fully-deterministic, feature-rich signal — more useful than |
| 40 | + # a constant because it exercises the attention pattern rather |
| 41 | + # than landing on a degenerate uniform hidden state. |
| 42 | + input_features = |
| 43 | + Nx.sin(Nx.iota({1, 3000, 80}, type: :f32) |> Nx.multiply(0.01)) |
| 44 | + |
| 45 | + # Short decoder prompt: the four Whisper special tokens that open |
| 46 | + # every English-language transcription (<|startoftranscript|>, |
| 47 | + # <|en|>, <|transcribe|>, <|notimestamps|>) plus two text-token |
| 48 | + # placeholders. Exact ids don't matter for a numerical pin — |
| 49 | + # they just need to be in-vocab and deterministic. |
| 50 | + decoder_input_ids = Nx.tensor([[50_258, 50_259, 50_359, 50_363, 50, 100]]) |
| 51 | + decoder_attention_mask = Nx.tensor([[1, 1, 1, 1, 1, 1]]) |
| 52 | + |
| 53 | + inputs = %{ |
| 54 | + "input_features" => input_features, |
| 55 | + "decoder_input_ids" => decoder_input_ids, |
| 56 | + "decoder_attention_mask" => decoder_attention_mask |
| 57 | + } |
| 58 | + |
| 59 | + outputs = Axon.predict(model, params, inputs) |
| 60 | + |
| 61 | + assert Nx.shape(outputs.logits) == {1, 6, 51_865} |
| 62 | + |
| 63 | + argmax = |
| 64 | + outputs.logits[[.., -1, ..]] |
| 65 | + |> Nx.argmax(axis: -1) |
| 66 | + |> Nx.backend_transfer(Nx.BinaryBackend) |
| 67 | + |> Nx.to_flat_list() |
| 68 | + |> hd() |
| 69 | + |
| 70 | + # On the synthetic mel input the decoder collapses to |
| 71 | + # <|endoftext|> (50257) by the last position. That's fine for a |
| 72 | + # pin — the assertion is "the same backend + the same weights + |
| 73 | + # the same input reproduce the same token", not "the model says |
| 74 | + # something interesting". |
| 75 | + assert argmax == 50_257 |
| 76 | + |
| 77 | + assert_all_close( |
| 78 | + outputs.logits[[.., 0..2, 0..2]], |
| 79 | + Nx.tensor([ |
| 80 | + [[2.9246, 0.2663, 3.8530], [-4.5523, -8.4833, -4.4232], [17.7350, 16.3070, 13.2149]] |
| 81 | + ]), |
| 82 | + # Whisper-tiny under BinaryBackend fallback for conv produces |
| 83 | + # slightly different rounding than a pure Emily path would, so |
| 84 | + # loosen the numerical tolerance marginally relative to the |
| 85 | + # tiny-random suite. Still three orders of magnitude inside |
| 86 | + # f16/bf16 accumulation drift territory. |
| 87 | + atol: 1.0e-3, |
| 88 | + rtol: 1.0e-3 |
| 89 | + ) |
| 90 | + end |
| 91 | +end |
0 commit comments