| 3848 | |
| 3849 | @dataclass |
| 3850 | class PromptSegment: |
| 3851 | @dataclass |
| 3852 | class Media: |
| 3853 | embeddings: np.ndarray |
| 3854 | positions: np.ndarray |
| 3855 | non_causal: bool = False |
| 3856 | |
| 3857 | kind: Literal["text", "image", "audio", "video"] |
| 3858 | start_pos: int |
| 3859 | n_pos: int |
| 3860 | identity_tokens: List[int] |
| 3861 | decode_start_pos: int |
| 3862 | decode_n_pos: int |
| 3863 | text_tokens: List[int] = field(default_factory=list) |
| 3864 | media: Optional["PromptSegment.Media"] = None |
| 3865 | |
| 3866 | @property |
| 3867 | def end_pos(self) -> int: |
| 3868 | return self.start_pos + self.n_pos |
| 3869 | |
| 3870 | @property |
| 3871 | def batch_rows(self) -> int: |
| 3872 | if self.kind != "text": |
| 3873 | if self.media is None: |
| 3874 | return 0 |
| 3875 | return int(self.media.embeddings.shape[0]) |
| 3876 | return len(self.text_tokens) |
| 3877 | |
| 3878 | @property |
| 3879 | def decoder_position_increments(self) -> List[int]: |
| 3880 | if not self.identity_tokens: |
| 3881 | return [] |
| 3882 | if self.kind == "text": |
| 3883 | return [1] * len(self.identity_tokens) |
| 3884 | return [*([0] * (len(self.identity_tokens) - 1)), self.decode_n_pos] |
| 3885 | |
| 3886 | def rows_for_capacity(self, offset: int, capacity: int) -> int: |
| 3887 | if capacity <= 0: |
| 3888 | return 0 |
| 3889 | return min(capacity, self.end_pos - self.start_pos - offset) |
| 3890 | |
| 3891 | def media_slice( |
| 3892 | self, |
| 3893 | row_offset: int, |
| 3894 | row_count: int, |
| 3895 | ) -> Tuple[np.ndarray, np.ndarray, List[int]]: |
| 3896 | if self.media is None: |
| 3897 | raise RuntimeError("media segment is missing embeddings or positions") |
| 3898 | row_start = row_offset |
| 3899 | row_end = row_offset + row_count |
| 3900 | embeddings = self.media.embeddings[row_start:row_end] |
| 3901 | if len(self.media.positions) == self.batch_rows: |
| 3902 | positions = self.media.positions[row_start:row_end] |
| 3903 | else: |
| 3904 | positions = ( |
| 3905 | self.media.positions.reshape(4, self.batch_rows)[:, row_start:row_end] |
| 3906 | .reshape(-1) |
| 3907 | ) |
no outgoing calls
no test coverage detected
searching dependent graphs…