Data¶
This section covers the pipeline from parquet files containing CIF text to the batches consumed during training. It includes the tokenizer that converts CIF strings to token IDs, the dataset loader that tokenizes and filters the data, and the collator that assembles fixed-length training batches.
The preprocessing CLIs used to generate the parquet files are documented on the CLI entry points page. The models that consume these batches are documented on the Models page.
Tokenizer¶
The CIF tokenizer provides the common text interface used by all model families.
CustomCIFTokenizer
¶
Bases: PreTrainedTokenizer
Hugging Face-compatible tokenizer for CIF text.
Tokenisation follows CIF structure rather than subwords, keeping element symbols, numbers, and
CIF keywords as whole tokens. Space-group tokens receive an internal _sg suffix during
tokenisation and the suffix is removed during decoding. from_pretrained and save_pretrained
use vocabulary.json, spacegroups.txt, and tokenizer_config.json.
Source code in _tokenizer.py
36 37 38 39 40 41 42 43 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
Dataset loading¶
load_data tokenizes a Hugging Face dataset and returns the tokenized dataset together with the collator required by the Hugging Face Trainer. For conditional runs, condition_columns specifies the dataset columns containing the conditioning values. These values are preserved through tokenization and passed through to the training batch.
Filtering is optional. By default, CIFs longer than the context length are truncated (but i typically prefer to do this in preprocessing), and CIFs containing unknown tokens are retained. Set remove_CIFs_above_context or remove_CIFs_with_unk to discard these samples instead.
load_data
¶
load_data(tokenizer, dataset, context_length, mode='unconditional', condition_columns=None, remove_CIFs_above_context=False, remove_CIFs_with_unk=False, show_token_stats=False, validate_conditions=False)
Tokenize a CIF dataset and build the collator used by the Hugging Face Trainer.
Conditional datasets must provide the requested condition columns. CIFs exceeding the context length or containing unknown tokens can be removed instead of being truncated or retained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
CustomCIFTokenizer
|
CIF tokenizer used to encode structures and provide the pad token id. |
required |
dataset
|
object
|
Hugging Face dataset containing CIF text and, for conditional training, the required condition columns. |
required |
context_length
|
int
|
Number of tokens in each training sequence. |
required |
mode
|
str
|
Whether training is unconditional or conditional. |
'unconditional'
|
condition_columns
|
list[str] | None
|
Dataset columns containing the conditioning values for conditional training. |
None
|
remove_CIFs_above_context
|
bool
|
Whether to drop CIFs longer than the context length. |
False
|
remove_CIFs_with_unk
|
bool
|
Whether to drop CIFs containing unknown tokens. |
False
|
show_token_stats
|
bool
|
Whether to print token-length statistics. |
False
|
validate_conditions
|
bool
|
Whether to validate conditioning values before training. |
False
|
Returns:
| Type | Description |
|---|---|
tuple
|
The tokenized dataset and data collator ready for the Hugging Face Trainer. |
Source code in _dataloader.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
Custom Dataloader for Conditioning¶
CustomCIFDataCollator constructs the batches consumed during training. Changes to the training pipeline may require changes here.
Every sequence produced by the collator has exactly context_length tokens. If a CIF exceeds the context window, it is truncated from the beginning. Shorter CIFs are then packed with tokens from other CIFs, selected round-robin from the remainder of the batch, until the window is filled. This packing keeps the context window occupied by CIF tokens rather than padding, but a single sequence can therefore contain parts of multiple structures.
The collator determines whether a batch is conditional from the features themselves by checking for condition_values. The same collator therefore handles both conditional and unconditional training without an additional mode flag. For a packed sequence, the associated condition values come from the CIF that provided the start of the sequence rather than from every CIF contributing tokens to it.
Each batch is returned as a dictionary containing [B, context_length] tensors for input_ids, labels, fixed_mask, attention_mask, and special_tokens_mask. Padding positions in labels are set to -100. Conditional batches also contain condition_values.
Scalar conditions are stacked into tensors of shape [B, n_conditions]. Continuous XRD conditions can contain a different number of rows for each sample, so they are front-padded with -100.0 to match the longest sample in the batch before stacking.
CustomCIFDataCollator
¶
Collate tokenized CIF rows into fixed-length training batches.
Every sequence in a batch is exactly context_length tokens. A CIF longer than that is sliced
from the beginning, a shorter one is packed with further CIFs taken round-robin from the rest of
the batch until the window is full. Packing keeps the context window full of real tokens instead
of padding.
Conditional mode is detected from the features themselves, by whether condition_values is
present, so the same collator serves both training modes.
Source code in _dataloader.py
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |