(
self,
media_chunks: Sequence["MTMDProcessor.MediaChunk"],
start_index: int,
)
| 10870 | self.embedding_cache.save(media_chunk.key, media_chunk.embeddings) |
| 10871 | |
| 10872 | def _encode_media_batch( |
| 10873 | self, |
| 10874 | media_chunks: Sequence["MTMDProcessor.MediaChunk"], |
| 10875 | start_index: int, |
| 10876 | ) -> int: |
| 10877 | batch = mtmd_cpp.mtmd_batch_init(self.ctx) |
| 10878 | if batch is None: |
| 10879 | raise CompletionRequestValidationError("failed to create MTMD media batch") |
| 10880 | try: |
| 10881 | first = media_chunks[start_index] |
| 10882 | result = int(mtmd_cpp.mtmd_batch_add_chunk(batch, first.chunk)) |
| 10883 | if result != 0: |
| 10884 | raise CompletionRequestValidationError( |
| 10885 | f"failed to add {first.kind} chunk to MTMD batch: error code {result}" |
| 10886 | ) |
| 10887 | group = [first] |
| 10888 | next_index = start_index + 1 |
| 10889 | while next_index < len(media_chunks): |
| 10890 | candidate = media_chunks[next_index] |
| 10891 | result = int(mtmd_cpp.mtmd_batch_add_chunk(batch, candidate.chunk)) |
| 10892 | if result == 0: |
| 10893 | group.append(candidate) |
| 10894 | next_index += 1 |
| 10895 | continue |
| 10896 | if result in {2, 3}: |
| 10897 | break |
| 10898 | raise CompletionRequestValidationError( |
| 10899 | f"failed to add {candidate.kind} chunk to MTMD batch: error code {result}" |
| 10900 | ) |
| 10901 | result = int(mtmd_cpp.mtmd_batch_encode(batch)) |
| 10902 | if result != 0: |
| 10903 | raise CompletionRequestValidationError( |
| 10904 | f"failed to encode MTMD media batch: error code {result}" |
| 10905 | ) |
| 10906 | for media_chunk in group: |
| 10907 | output = mtmd_cpp.mtmd_batch_get_output_embd(batch, media_chunk.chunk) |
| 10908 | if output is None: |
| 10909 | raise CompletionRequestValidationError( |
| 10910 | f"MTMD {media_chunk.kind} encoder returned no embeddings" |
| 10911 | ) |
| 10912 | media_chunk.embeddings = self._embeddings_from_pointer( |
| 10913 | output, |
| 10914 | media_chunk.n_tokens, |
| 10915 | ) |
| 10916 | self._save_media_chunk(media_chunk) |
| 10917 | return len(group) |
| 10918 | finally: |
| 10919 | mtmd_cpp.mtmd_batch_free(batch) |
| 10920 | |
| 10921 | def _encode_media_chunks( |
| 10922 | self, |
no test coverage detected