Build the multi-channel (text + audio) packed sequence.
(
tokenizer: Tokenizer,
content: str,
audio_codes_list: list[np.ndarray],
is_user: bool = True,
truncation: bool = False,
)
| 165 | |
| 166 | |
| 167 | def _get_unified_codes( |
| 168 | tokenizer: Tokenizer, |
| 169 | content: str, |
| 170 | audio_codes_list: list[np.ndarray], |
| 171 | is_user: bool = True, |
| 172 | truncation: bool = False, |
| 173 | ) -> np.ndarray: |
| 174 | """Build the multi-channel (text + audio) packed sequence.""" |
| 175 | text_ids = np.array(tokenizer.encode(content), dtype=np.int64) |
| 176 | n_vq = N_VQ |
| 177 | |
| 178 | if len(audio_codes_list) == 0: |
| 179 | audio_channel = np.full((len(text_ids), n_vq), AUDIO_PAD_CODE, dtype=np.int64) |
| 180 | return np.concatenate([text_ids[:, np.newaxis], audio_channel], axis=1) |
| 181 | |
| 182 | audio_start_indices = np.where(text_ids == AUDIO_START_TOKEN_ID)[0] |
| 183 | audio_end_indices = np.where(text_ids == AUDIO_END_TOKEN_ID)[0] |
| 184 | |
| 185 | if len(audio_start_indices) != len(audio_codes_list) or len(audio_end_indices) != len(audio_codes_list): |
| 186 | raise ValueError( |
| 187 | f"Audio markers ({len(audio_start_indices)} starts, {len(audio_end_indices)} ends) " |
| 188 | f"don't match codes ({len(audio_codes_list)})" |
| 189 | ) |
| 190 | |
| 191 | delay_parts: list[np.ndarray] = [] |
| 192 | prefix_idx = 0 |
| 193 | |
| 194 | for start_idx, end_idx, codes in zip(audio_start_indices, audio_end_indices, audio_codes_list): |
| 195 | start_idx = int(start_idx) |
| 196 | end_idx = int(end_idx) |
| 197 | |
| 198 | delayed = apply_delay_pattern(codes, AUDIO_PAD_CODE) |
| 199 | |
| 200 | pad_before = np.full( |
| 201 | (start_idx - prefix_idx + 1, n_vq), AUDIO_PAD_CODE, dtype=np.int64, |
| 202 | ) |
| 203 | delay_parts.extend([pad_before, delayed]) |
| 204 | prefix_idx = end_idx |
| 205 | |
| 206 | if truncation: |
| 207 | delay_parts[-1] = delay_parts[-1][:-(n_vq - 1), :] |
| 208 | else: |
| 209 | last_end = int(audio_end_indices[-1]) |
| 210 | pad_after = np.full( |
| 211 | (len(text_ids) - last_end, n_vq), AUDIO_PAD_CODE, dtype=np.int64, |
| 212 | ) |
| 213 | delay_parts.append(pad_after) |
| 214 | |
| 215 | delay_audio = np.concatenate(delay_parts, axis=0) |
| 216 | |
| 217 | if len(text_ids) != delay_audio.shape[0]: |
| 218 | text_ids = text_ids[:delay_audio.shape[0]] |
| 219 | |
| 220 | return np.concatenate([text_ids[:, np.newaxis], delay_audio], axis=1) |
| 221 | |
| 222 | |
| 223 | def parse_generation_output( |
no test coverage detected