| 259 | |
| 260 | @dataclass |
| 261 | class CodecStreamingDecodeSession: |
| 262 | codec_meta: dict[str, Any] |
| 263 | session: ort.InferenceSession |
| 264 | |
| 265 | def __post_init__(self) -> None: |
| 266 | self.transformer_specs = list(self.codec_meta.get("streaming_decode", {}).get("transformer_offsets", [])) |
| 267 | self.attention_specs = list(self.codec_meta.get("streaming_decode", {}).get("attention_caches", [])) |
| 268 | self.state_feeds: dict[str, np.ndarray] = {} |
| 269 | self.reset() |
| 270 | |
| 271 | def reset(self) -> None: |
| 272 | self.state_feeds = {} |
| 273 | for spec in self.transformer_specs: |
| 274 | self.state_feeds[str(spec["input_name"])] = np.zeros(tuple(spec["shape"]), dtype=np.int32) |
| 275 | for spec in self.attention_specs: |
| 276 | self.state_feeds[str(spec["offset_input_name"])] = np.zeros(tuple(spec["offset_shape"]), dtype=np.int32) |
| 277 | self.state_feeds[str(spec["cached_keys_input_name"])] = np.zeros(tuple(spec["cache_shape"]), dtype=np.float32) |
| 278 | self.state_feeds[str(spec["cached_values_input_name"])] = np.zeros(tuple(spec["cache_shape"]), dtype=np.float32) |
| 279 | positions = np.full(tuple(spec["positions_shape"]), -1, dtype=np.int32) |
| 280 | self.state_feeds[str(spec["cached_positions_input_name"])] = positions |
| 281 | |
| 282 | def run_frames(self, frame_rows: list[list[int]]) -> tuple[np.ndarray, int] | None: |
| 283 | if not frame_rows: |
| 284 | return None |
| 285 | num_quantizers = int(self.codec_meta["codec_config"]["num_quantizers"]) |
| 286 | frame_count = len(frame_rows) |
| 287 | audio_codes = np.zeros((1, frame_count, num_quantizers), dtype=np.int32) |
| 288 | for frame_index, frame_row in enumerate(frame_rows): |
| 289 | for channel_index in range(num_quantizers): |
| 290 | audio_codes[0, frame_index, channel_index] = int(frame_row[channel_index] if channel_index < len(frame_row) else 0) |
| 291 | feeds: dict[str, np.ndarray] = { |
| 292 | "audio_codes": audio_codes, |
| 293 | "audio_code_lengths": np.asarray([frame_count], dtype=np.int32), |
| 294 | } |
| 295 | feeds.update(self.state_feeds) |
| 296 | outputs = self.session.run(None, feeds) |
| 297 | output_names = [output.name for output in self.session.get_outputs()] |
| 298 | named_outputs = dict(zip(output_names, outputs, strict=True)) |
| 299 | for spec in self.transformer_specs: |
| 300 | self.state_feeds[str(spec["input_name"])] = named_outputs[str(spec["output_name"])] |
| 301 | for spec in self.attention_specs: |
| 302 | self.state_feeds[str(spec["offset_input_name"])] = named_outputs[str(spec["offset_output_name"])] |
| 303 | self.state_feeds[str(spec["cached_keys_input_name"])] = named_outputs[str(spec["cached_keys_output_name"])] |
| 304 | self.state_feeds[str(spec["cached_values_input_name"])] = named_outputs[str(spec["cached_values_output_name"])] |
| 305 | self.state_feeds[str(spec["cached_positions_input_name"])] = named_outputs[str(spec["cached_positions_output_name"])] |
| 306 | return ( |
| 307 | named_outputs["audio"], |
| 308 | int(named_outputs["audio_lengths"].reshape(-1)[0]), |
| 309 | ) |
| 310 | |
| 311 | |
| 312 | class OrtCpuRuntime: |