flat_tokens_batcher
optimus_dl.modules.data.transforms.flat_tokens_batcher
¶
FlatTokensBatcher
¶
Bases: BaseTransform
Transform that aggregates token IDs and yields fixed-size batches.
Unlike standard batchers that batch whole examples, this batcher pools all tokens from incoming documents and yields packed sequences, minimizing padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
FlatTokensBatcherConfig
|
Batching configuration. |
required |
Source code in optimus_dl/modules/data/transforms/flat_tokens_batcher.py
build(source)
¶
FlatTokensBatcherConfig
dataclass
¶
Bases: RegistryConfigStrict
Configuration for token aggregation and batching.
Attributes:
| Name | Type | Description |
|---|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int | None
|
|
None
|
seq_len
|
int | None
|
|
None
|
max_tokens
|
int | None
|
|
None
|
worker_cfg
|
MapperConfig
|
Config with process-based parallelism by default. |
<dynamic>
|
field
|
str
|
|
'input_ids'
|
mask_documents
|
bool
|
|
False
|
flatten
|
bool
|
|
False
|
Source code in optimus_dl/modules/data/transforms/flat_tokens_batcher.py
FlatTokensBatcherNode
¶
Bases: BaseNode
Internal node for performing token aggregation and batching.
Accumulates pre-shifted segments from variable-length document sources into buffers until it has enough to form a complete batch of the target size. This ensures that document transitions are excluded from the sequence.
Source code in optimus_dl/modules/data/transforms/flat_tokens_batcher.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
target_size
property
¶
Calculate total number of tokens needed for one batch of inputs.
get_state()
¶
Collect current buffer and source state for checkpointing.
Source code in optimus_dl/modules/data/transforms/flat_tokens_batcher.py
next()
¶
Yield the next complete batch of tokens, filling from source as needed.
The batcher maintains an internal buffer of tokens. When the buffer falls below the target size (batch_size * seq_len or max_tokens), it pulls more documents from the source node. Unfinished batches are not yielded.
If 'flatten' is True, it generates metadata required for sequence isolation in FlashAttention (cu_seqlens and max_seqlen).
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary containing: - input_ids: Tensor of shape (B, T) or (1, sum_T) - labels: Tensor of shape (B, T) or (1, sum_T), shifted by 1 - position_ids: (Optional) Position indices within each document - document_ids: (Optional) Unique ID for each document in the batch - cu_seqlens: (Optional, flat only) Cumulative document lengths - max_seqlen: (Optional, flat only) Length of the longest document |
Source code in optimus_dl/modules/data/transforms/flat_tokens_batcher.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
reset(initial_state=None)
¶
Restore batcher buffer and source node state.