(cls)
| 80 | class TestWhisperTextDecoder(argmaxtools_test_utils.CoreMLTestsMixin, unittest.TestCase): |
| 81 | @classmethod |
| 82 | def setUpClass(cls): |
| 83 | cls.test_output_names = TEST_OUTPUT_NAMES |
| 84 | cls.test_cache_dir = TEST_CACHE_DIR |
| 85 | cls.model_name = "TextDecoder" |
| 86 | |
| 87 | if not TEST_TOKEN_TIMESTAMPS: |
| 88 | cls.test_output_names.pop(cls.test_output_names.index("alignment_heads_weights")) |
| 89 | |
| 90 | # Original model |
| 91 | orig_torch_model = load_whisper_model(TEST_WHISPER_VERSION, TEST_TORCH_DTYPE) |
| 92 | cls.orig_torch_model = ( |
| 93 | orig_torch_model.model.decoder.to(TEST_DEV).to(TEST_TORCH_DTYPE).eval() |
| 94 | ) |
| 95 | |
| 96 | # Base test model |
| 97 | cls.test_torch_model = text_decoder.WhisperTextDecoder( |
| 98 | cls.orig_torch_model.config |
| 99 | ) |
| 100 | cls.test_torch_model.load_state_dict(cls.orig_torch_model.state_dict()) |
| 101 | cls.test_torch_model = ( |
| 102 | cls.test_torch_model.to(TEST_DEV).to(TEST_TORCH_DTYPE).eval() |
| 103 | ) |
| 104 | cls.gen_cfg = orig_torch_model.generation_config |
| 105 | |
| 106 | if TEST_TOKEN_TIMESTAMPS: |
| 107 | cls.test_torch_model.configure_for_token_timestamps(cls.gen_cfg) |
| 108 | |
| 109 | # Elaboration: I/O and architecture config |
| 110 | cfg = cls.orig_torch_model.config |
| 111 | cls.cfg = dict( |
| 112 | n_heads=cfg.decoder_attention_heads, |
| 113 | n_layers=cfg.decoder_layers, |
| 114 | embed_dim=cfg.d_model, |
| 115 | batch_size=1, |
| 116 | vocab_size=cfg.vocab_size, |
| 117 | enc_seq_len=cfg.max_source_positions, |
| 118 | dec_kv_seq_len=TEST_DEC_KV_SEQ_LEN or cfg.max_target_positions, |
| 119 | ) |
| 120 | |
| 121 | cls.cfg["active_dec_kv_seq_len"] = random.randint(1, cls.cfg["dec_kv_seq_len"]) |
| 122 | logger.info( |
| 123 | f"Decoding token at index {cls.cfg['active_dec_kv_seq_len']} " |
| 124 | f"(max={cls.cfg['dec_kv_seq_len']}) " |
| 125 | ) |
| 126 | |
| 127 | ( |
| 128 | cls.test_torch_inputs, |
| 129 | cls.orig_inputs, |
| 130 | ) = test_utils._prepare_test_inputs_for_decoder(**cls.cfg) |
| 131 | |
| 132 | # Do casting and device placement per test config |
| 133 | cls.test_torch_inputs = {k: place(v) for k, v in cls.test_torch_inputs.items()} |
| 134 | cls.orig_inputs = { |
| 135 | k: place(v) |
| 136 | if isinstance(v, torch.Tensor) |
| 137 | else [(place(vi[0]), place(vi[1])) for vi in v] |
| 138 | for k, v in cls.orig_inputs.items() |
| 139 | } |
nothing calls this directly
no test coverage detected