Model Card for LayoutFormer++¶
This package ports LayoutFormer++, the autoregressive layout transformer method, into a 🤗transformers-style pipeline.
Model Details¶
Model Description¶
LayoutFormer++ is a transformers layout generator that models layout sequences across label, label-size, relation, refinement, completion, and unconditional tasks. Converted checkpoints use task-specific token vocabularies for RICO25 and PubLayNet while preserving the original-implementation discrete layout representation internally. Public outputs use normalized center xywh boxes in [0, 1], dataset-local integer labels, a valid-element mask, and id2label.
Public outputs omit generated elements whose labels cannot be mapped to the selected dataset, and one warning reports the number omitted. The original parser keeps these elements with error label 0 and its parsed box, which may be shifted by truncation; this package deliberately drops them because the public schema has no unknown-label category, so element-count and box metrics may differ from vendor results.
- Developed by: Zhaoyun Jiang et al.
- Shared by: creative-graphic-design.
- Model type: content-agnostic; task: task-aware; conditioning: unconditional, label, label_size, completion, refinement, relation.
- Language(s) (NLP): not applicable.
- License: MIT.
Model Sources¶
- Repository: Microsoft LayoutGeneration LayoutFormer++
- Paper: arXiv 2208.08037
Supported Checkpoints¶
Uses¶
Direct Use¶
Use this package for research inference, conversion checks, and agreement checks against the original implementation for generated layouts.
Each released task has an incompatible task-specific checkpoint, so Hub ids include the task suffix. Supported public conditions map to tasks in the original implementation as follows: gen_t -> label, gen_ts -> label_size, gen_r -> relation, refinement -> refinement, completion -> completion, and ugen -> unconditional.
Task inputs follow the public condition shape for the selected checkpoint:
label takes labels=[["Text", "Image"]]; label_size adds matching normalized center xywh boxes; relation takes five-integer relation tuples such as (0, 1, 2, 3, 1) for (subject_index, relation_id, object_index, constraint_id, value); completion and refinement take partial labels, bbox, and mask; unconditional needs no layout condition. If normalized=False, pass canvas_size=(width, height) so pixel boxes can be converted to the internal discrete ltwh grid.
Downstream Use¶
Generated layouts may feed rendering, design tooling, layout evaluation, or downstream content placement systems after task-specific validation.
Out-of-Scope Use¶
Do not treat generated layouts as production accessibility annotations, OCR output, semantic scene understanding, or license-cleared design assets without separate review.
Bias, Risks, and Limitations¶
The converted behavior follows the upstream checkpoints, prompt fixtures, and datasets. Dataset coverage, label vocabularies, and layout quality inherit the limits of those sources.
Recommendations¶
Re-run the agreement-check suite against the original implementation before publishing converted checkpoints or comparing new results.
How to Get Started with the Model¶
Install the package directly from this repository. The command includes shared packages when they are not published on PyPI.
pip install \
"laygen @ git+https://github.com/creative-graphic-design/design-generators.git#subdirectory=lib/laygen" \
"layoutformerpp @ git+https://github.com/creative-graphic-design/design-generators.git#subdirectory=models/layoutformerpp"
Clone this repository, install the workspace member, and run the download and conversion steps in REPRODUCING.md. Those steps create .cache/layoutformerpp/converted/rico_gen_t.
git clone https://github.com/creative-graphic-design/design-generators.git
cd design-generators
uv sync --package layoutformerpp
uv run --package layoutformerpp python
from layoutformerpp import LayoutFormerPPPipeline
path = ".cache/layoutformerpp/converted/rico_gen_t"
# After Hub publication: from_pretrained("creative-graphic-design/layoutformerpp-rico25-label")
pipe = LayoutFormerPPPipeline.from_pretrained(path)
out = pipe(condition_type="label", labels=[["Text", "Image"]], max_length=16)
print(out.bbox)
print(out.labels)
print(out.id2label)
Training Details¶
Training Data¶
| Dataset | Dataset ID | Notes |
|---|---|---|
| RICO25 | creative-graphic-design/Rico |
ui-screenshots-and-hierarchies-with-semantic-annotations |
| PubLayNet | creative-graphic-design/PubLayNet |
default |
RICO25 public outputs use zero-based dataset-local ids, while the internal LayoutFormer++ tokenizer uses one-based label_<id> tokens. PubLayNet COCO-style document boxes are converted to the internal discrete LayoutFormer++ ltwh token grid and returned publicly as normalized center xywh.
Training Procedure¶
This package ports released behavior and exposes the package-local training path; current trained-checkpoint reproduction status is recorded in TRAINING.md.
Preprocessing¶
Inputs and outputs are normalized to the public layout schema at package boundaries. Boxes, tokens, prompts, or analog bits that follow the original implementation stay inside package adapters and fixtures used for agreement checks.
Training Hyperparameters¶
- Training regime: original upstream training; staged S0-S4 reproduction passes for all twelve recipe families, and trained-checkpoint S5 reproduction is not claimed.
Speeds, Sizes, Times¶
Training-time and carbon measurements are unknown.
Evaluation¶
Testing Data, Factors & Metrics¶
Testing Data¶
Agreement checks use local-only generated fixtures and converted checkpoint directories. Large generated tensors, images, weights, and downloaded artifacts are not committed.
Factors¶
Agreement results are reported separately by dataset, checkpoint, condition mode, seed, or prompt fixture where the package has recorded evidence.
Metrics¶
Metrics are exact tensor equality, exact token or byte equality, or an explicitly stated numeric tolerance against the original implementation.
Parity Results¶
| Checkpoint | Public checkpoint | Vocab source | Tokenizer | Logits max abs | Logits max rel | Generation |
|---|---|---|---|---|---|---|
rico_gen_t |
present | ckpts/rico_gen_t/vocab.json |
exact | 0.0 | 0.0 | exact original-implementation greedy loop; exact label-constrained loop |
rico_gen_ts |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation greedy loop; exact label-size-constrained loop |
rico_gen_r |
present | ckpts/rico_gen_r/vocab.json |
exact | 0.0 | 0.0 | exact original-implementation top-k loop with relation input |
rico_refinement |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation greedy loop |
rico_completion |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation top-k loop |
rico_ugen |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation top-k loop |
publaynet_gen_t |
present | ckpts/publaynet_gen_t/vocab.json |
exact | 0.0 | 0.0 | exact original-implementation greedy loop; exact label-constrained loop |
publaynet_gen_ts |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation greedy loop; exact label-size-constrained loop |
publaynet_gen_r |
present | ckpts/publaynet_gen_r/vocab.json |
exact | 0.0 | 0.0 | exact original-implementation top-k loop with relation input |
publaynet_refinement |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation greedy loop |
publaynet_completion |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation top-k loop |
publaynet_ugen |
present | synthetic task vocab; no task vocab.json is published |
exact | 0.0 | 0.0 | exact original-implementation top-k loop |
The substantive agreement-check sweep covers 28 checkpoint/check combinations: 12 checkpoint-logit checks, 12 checkpoint-generation checks, and 4 label-constrained generation checks. The separate FileNotFoundError loader test is not counted as an agreement case.
Reproducibility¶
See REPRODUCING.md for the commands that download original-implementation assets, generate reference outputs, run agreement checks, convert checkpoints, and smoke-test local loading.
Environmental Impact¶
Training, conversion, and agreement-check costs depend on the selected recipe, checkpoint, and local hardware.
Technical Specifications¶
Model Architecture and Objective¶
LayoutFormer++ uses an encoder-decoder Transformer with task-specific token vocabularies for label, label-size, relation, refinement, completion, and unconditional generation. Optional task prompt embeddings mirror the released checkpoints, while the processor decodes discrete tokens into normalized boxes and dataset labels.
Compute Infrastructure¶
Agreement-check commands are intended for one explicitly selected GPU when the original implementation requires CUDA.
Hardware¶
CPU is sufficient for import and most smoke tests. CUDA is required for heavyweight agreement checks against the original implementation.
Software¶
Use uv run --package layoutformerpp ... from the repository root so workspace dependency sources and extras resolve correctly.
License¶
Repository wrapper code is Apache-2.0. The original implementation is MIT licensed in the parent Microsoft LayoutGeneration repository. Converted code in this package is for research conversion and evaluation of the released LayoutFormer++ checkpoints.
Citation¶
@inproceedings{jiang2023layoutformerpp,
title = {LayoutFormer++: Conditional Graphic Layout Generation via Constraint Serialization and Decoding Space Restriction},
author = {Jiang, Zhaoyun and Guo, Jiaqi and Sun, Shizhao and Deng, Huayu and Wu, Zhongkai and Mijovic, Vuksan and Yang, Zijiang James and Lou, Jian-Guang and Zhang, Dongmei},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
year = {2023}
}
API Reference¶
Transformers-style LayoutFormer++ components.
LayoutFormerPPConfig ¶
Bases: PretrainedConfig
Stores model, tokenizer, and task defaults for converted checkpoints.
Source code in models/layoutformerpp/src/layoutformerpp/configuration_layoutformerpp.py
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 | |
__init__ ¶
__init__(
vocab_size: int = 0,
max_position_embeddings: int | None = None,
d_model: int = 512,
encoder_layers: int = 8,
decoder_layers: int | None = None,
encoder_attention_heads: int = 8,
decoder_attention_heads: int | None = None,
dropout: float = 0.1,
dim_feedforward: int | None = None,
share_embedding: bool = True,
dataset: DatasetName | str = DatasetName.rico25,
task: LayoutFormerPPTask
| ConditionType
| str = LayoutFormerPPTask.gen_t,
max_num_elements: int = 20,
bbox_format: BoxFormat | str = BoxFormat.ltwh,
default_box_format: BoxFormat | str = BoxFormat.xywh,
discrete_x_grid: int = 128,
discrete_y_grid: int = 128,
add_sep_token: bool = True,
sort_by_dict: bool = True,
add_task_embedding: bool = False,
add_task_prompt_token_in_model: bool = False,
num_task_prompt_token: int = 1,
task_id: int | None = None,
decode_max_length: int | None = None,
eval_seed: int | None = None,
gen_t_add_unk_token: bool = False,
gen_ts_add_unk_token: bool = False,
gen_r_add_unk_token: bool = False,
gen_r_compact: bool = False,
bos_token_id: int = 0,
eos_token_id: int = 1,
pad_token_id: int = 2,
is_encoder_decoder: bool = True,
condition_type: ConditionType | str | None = None,
model_type: str | None = None,
transformers_version: str | None = None,
architectures: list[str] | None = None,
output_hidden_states: bool | None = False,
output_attentions: bool | None = False,
return_dict: bool | None = True,
chunk_size_feed_forward: int = 0,
problem_type: Literal[
"regression",
"single_label_classification",
"multi_label_classification",
]
| None = None,
id2label: dict[int | str, str] | None = None,
label2id: dict[str, int] | None = None,
label_translation_metadata: LabelTranslationMetadata
| None = None,
torch_dtype: str | None = None,
dtype: str | None = None,
tie_word_embeddings: bool = True,
task_specific_params: dict[
str,
str
| int
| float
| bool
| list[str | int | float | bool],
]
| None = None,
name_or_path: str = "",
_name_or_path: str | None = None,
_commit_hash: str | None = None,
attn_implementation: str | None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize architecture and task-specific generation defaults.
Source code in models/layoutformerpp/src/layoutformerpp/configuration_layoutformerpp.py
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 | |
LayoutFormerPPForConditionalGeneration ¶
Bases: PreTrainedModel
Transformers PreTrainedModel with checkpoint-compatible module names.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
__init__ ¶
__init__(config: LayoutFormerPPConfig) -> None
Initialize checkpoint-compatible encoder/decoder modules.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
encode ¶
encode(
input_ids: Int[Tensor, "batch tokens"],
padding_mask: Bool[Tensor, "batch tokens"],
task_ids: Int[Tensor, "batch"] | None = None,
) -> tuple[
Float[torch.Tensor, "seq batch channels"],
Bool[torch.Tensor, "batch seq"],
]
Encode input token ids with optional task prompt embeddings.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
prepare_decoder_input_ids_from_labels ¶
prepare_decoder_input_ids_from_labels(
labels: Int[Tensor, "batch tokens"],
) -> Int[torch.Tensor, "batch tokens"]
Shift labels right and prepend BOS.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
225 226 227 228 229 230 | |
forward ¶
forward(
input_ids: Int[Tensor, "batch tokens"],
attention_mask: Bool[Tensor, "batch tokens"]
| None = None,
labels: Int[Tensor, "batch tokens"] | None = None,
decoder_input_ids: Int[Tensor, "batch tokens"]
| None = None,
task_ids: Int[Tensor, "batch"] | None = None,
return_dict: bool | None = None,
) -> (
Seq2SeqLMOutput | tuple[Float[torch.Tensor, "..."], ...]
)
Run teacher-forced LayoutFormer++ decoding.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
LayoutFormerPPPipeline ¶
Bases: LayoutGenerationPipeline
Compose a LayoutFormer++ model and processor for layout generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
LayoutFormerPPForConditionalGeneration
|
Converted LayoutFormer++ model. |
required |
processor
|
LayoutFormerPPProcessor
|
Matching processor/tokenizer. |
required |
config
|
LayoutFormerPPConfig | None
|
Optional root pipeline config. Defaults to |
None
|
Examples:
>>> processor = LayoutFormerPPProcessor.from_config(dataset="rico", task="gen_t")
>>> config = LayoutFormerPPConfig(vocab_size=processor.tokenizer.vocab_size)
>>> pipe = LayoutFormerPPPipeline(
... model=LayoutFormerPPForConditionalGeneration(config),
... processor=processor,
... )
>>> pipe.config.model_type
'layoutformerpp'
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
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 | |
__init__ ¶
__init__(
model: LayoutFormerPPForConditionalGeneration,
processor: LayoutFormerPPProcessor,
config: LayoutFormerPPConfig | None = None,
) -> None
Initialize the pipeline with model and processor components.
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
110 111 112 113 114 115 116 117 118 119 120 | |
__call__ ¶
__call__(
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.unconditional,
labels: list[list[int | str]]
| Int[Tensor, "batch elements"]
| None = None,
bbox: LayoutFormerPPBBoxInput = None,
mask: Bool[Tensor, "batch elements"] | None = None,
relations: list[list[tuple[int, int, int, int, int]]]
| Int[Tensor, "batch relations relation_attrs"]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
max_length: int | None = None,
do_sample: bool | None = None,
top_k: int = 10,
temperature: float = 0.7,
) -> LayoutGenerationOutput | LayoutFormerPPOutputDict
Generate layouts by encoding conditions, generating ids, and decoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of layouts to generate when labels are omitted. |
1
|
seed
|
int | None
|
Convenience seed used only when |
None
|
generator
|
Generator | None
|
Optional PyTorch generator; takes precedence over |
None
|
condition_type
|
ConditionType | str
|
Canonical condition type or supported alias. |
unconditional
|
labels
|
list[list[int | str]] | Int[Tensor, 'batch elements'] | None
|
Optional label conditions. |
None
|
bbox
|
LayoutFormerPPBBoxInput
|
Optional layout boxes for size/completion/refinement conditions. |
None
|
mask
|
Bool[Tensor, 'batch elements'] | None
|
Reserved public validity mask input. |
None
|
relations
|
list[list[tuple[int, int, int, int, int]]] | Int[Tensor, 'batch relations relation_attrs'] | None
|
Relation tuples use one-based indexes within each label. For non-canvas sources, the source element must precede its target in the label sequence used for decoding. |
None
|
num_elements
|
int | list[int] | Int[Tensor, 'batch'] | None
|
Reserved v1 interface argument. |
None
|
box_format
|
BoxFormat | str
|
Input and output bounding-box format. |
xywh
|
normalized
|
bool
|
Whether public boxes are normalized. |
True
|
canvas_size
|
tuple[int, int] | None
|
Reserved v1 interface argument. |
None
|
num_inference_steps
|
int | None
|
Reserved v1 interface argument. |
None
|
output_type
|
OutputType | str
|
Return |
dataclass
|
return_intermediates
|
bool
|
Reserved output detail flag. |
False
|
max_length
|
int | None
|
Optional token decode length override. |
None
|
do_sample
|
bool | None
|
Optional sampling override. |
None
|
top_k
|
int
|
Top-k value used by the reference sampling loop. |
10
|
temperature
|
float
|
Sampling temperature. |
0.7
|
When max_length is omitted and the model config matches its dataset/task
default, the pipeline uses 120 tokens for unconditional, completion, label,
and label-size generation, 150 for relation generation, or input length plus
one for refinement. This equality rule also applies when the config value was
set explicitly; pass max_length to select another per-call limit.
For batches larger than one, the probability gate uses row zero, relation rollback uses the last row's rollback point, and a rollback truncates every row to the same prefix.
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | LayoutFormerPPOutputDict
|
Layout generation output dataclass or dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If processor inputs or relation label/index references are invalid. |
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
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 | |
LayoutFormerPPProcessor ¶
Bases: ProcessorMixin
Build LayoutFormer++ text inputs and parse generated layouts.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
__init__ ¶
__init__(
tokenizer: LayoutFormerPPTokenizer,
dataset: DatasetName | str = DEFAULT_DATASET,
task: LayoutFormerPPTask
| ConditionType
| str = DEFAULT_TASK,
add_sep_token: bool = True,
x_grid: int = 128,
y_grid: int = 128,
id2label: dict[int, str] | None = None,
label_translation_metadata: LabelTranslationMetadata
| None = None,
) -> None
Initialize serializers, label maps, and tokenizer state.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 | |
from_config
classmethod
¶
from_config(
dataset: DatasetName | str = DEFAULT_DATASET,
task: LayoutFormerPPTask
| ConditionType
| str = DEFAULT_TASK,
*,
add_sep_token: bool = True,
x_grid: int = 128,
y_grid: int = 128,
id2label: dict[int, str] | None = None,
) -> "LayoutFormerPPProcessor"
Construct processor and tokenizer without external files.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 | |
normalize_condition_type ¶
normalize_condition_type(
condition_type: ConditionType | str,
) -> SupportedConditionType
Normalize public condition aliases.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
194 195 196 197 198 199 200 201 202 203 204 205 206 | |
__call__ ¶
__call__(
condition_type: ConditionType
| str = ConditionType.unconditional,
labels: list[list[int | str]]
| Int[Tensor, "batch elements"]
| None = None,
bbox: LayoutFormerPPBBoxInput = None,
mask: Bool[Tensor, "batch elements"]
| list[list[bool]]
| list[bool]
| None = None,
relations: list[list[tuple[int, int, int, int, int]]]
| Int[Tensor, "batch relations relation_attrs"]
| None = None,
batch_size: int | None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Build tokenized model inputs for a public condition.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
post_process_layouts ¶
post_process_layouts(
sequences: Int[Tensor, "batch tokens"],
*,
box_format: BoxFormat | str = BoxFormat.xywh,
output_type: OutputType | str = OutputType.dataclass,
return_tensors: Literal["pt"] = "pt",
) -> LayoutGenerationOutput | LayoutFormerPPOutputDict
Parse generated token ids to the common layout output schema.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
LayoutFormerPPTask ¶
Bases: StrEnum
Supported converted LayoutFormer++ checkpoint variants.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
12 13 14 15 16 17 18 19 20 | |
OutputType ¶
Bases: StrEnum
Supported post-processing return shapes.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
23 24 25 26 27 | |
LayoutFormerPPTokenizer ¶
Bases: WhitespaceTokenizerMixin, PreTrainedTokenizer
Whitespace tokenizer backed by the released vocab.json format.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 | |
__init__ ¶
__init__(
vocab_file: str | None = None,
tokens: list[str] | None = None,
x_grid: int = 128,
y_grid: int = 128,
bbox_order: BoxFormat | str = BoxFormat.ltwh,
bos_token: str = "<bos>",
eos_token: str = "<eos>",
pad_token: str = "<pad>",
sep_token: str = "<sep>",
unk_token: str = "<unk>",
model_max_length: int = DEFAULT_MODEL_MAX_LENGTH,
padding_side: str = "right",
truncation_side: str = "right",
clean_up_tokenization_spaces: bool = False,
added_tokens_decoder: dict[int | str, str]
| None = None,
backend: str = "custom",
tokenizer_file: str | None = None,
name_or_path: str = "",
is_local: bool = False,
local_files_only: bool = False,
processor_class: str | None = None,
) -> None
Initialize a tokenizer from a vocab file or synthetic token list.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
27 28 29 30 31 32 33 34 35 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 | |
save_vocabulary ¶
save_vocabulary(
save_directory: str, filename_prefix: str | None = None
) -> tuple[str]
Save vocab.json in checkpoint-compatible token-to-id format.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
78 79 80 81 82 83 84 85 86 87 | |
save_pretrained ¶
save_pretrained(
save_directory: str | PathLike[str],
legacy_format: bool | None = None,
filename_prefix: str | None = None,
push_to_hub: bool = False,
**kwargs: str | int | float | bool | None,
) -> tuple[str, ...]
Save tokenizer files plus LayoutFormer++ tokenizer metadata.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
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 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
cache_dir: str | PathLike[str] | None = None,
force_download: bool = False,
local_files_only: bool = False,
token: str | bool | None = None,
revision: str = "main",
x_grid: int | None = None,
y_grid: int | None = None,
bbox_order: BoxFormat | str | None = None,
) -> "LayoutFormerPPTokenizer"
Load tokenizer and LayoutFormer++ metadata.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
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 | |
encode_text ¶
encode_text(
text: str | list[str],
*,
add_eos: bool = True,
add_bos: bool = False,
) -> BatchEncoding
Tokenize reference-style text while matching the original EOS/BOS behavior.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
configuration_layoutformerpp ¶
Configuration for LayoutFormer++.
TaskDefaults ¶
Bases: TypedDict
Evaluation defaults for one dataset/condition pair.
Attributes:
| Name | Type | Description |
|---|---|---|
max_position_embeddings |
int
|
Upper bound for the model's input token sequence. |
decode_max_length |
int
|
Evaluation-time budget for generated tokens. |
eval_seed |
int
|
Evaluation-time seed used by stochastic decoding. |
Values are selected per dataset and condition to reproduce the evaluation
recipes documented in models/layoutformerpp/REPRODUCING.md.
Source code in models/layoutformerpp/src/layoutformerpp/configuration_layoutformerpp.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
LayoutFormerPPConfig ¶
Bases: PretrainedConfig
Stores model, tokenizer, and task defaults for converted checkpoints.
Source code in models/layoutformerpp/src/layoutformerpp/configuration_layoutformerpp.py
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 | |
__init__ ¶
__init__(
vocab_size: int = 0,
max_position_embeddings: int | None = None,
d_model: int = 512,
encoder_layers: int = 8,
decoder_layers: int | None = None,
encoder_attention_heads: int = 8,
decoder_attention_heads: int | None = None,
dropout: float = 0.1,
dim_feedforward: int | None = None,
share_embedding: bool = True,
dataset: DatasetName | str = DatasetName.rico25,
task: LayoutFormerPPTask
| ConditionType
| str = LayoutFormerPPTask.gen_t,
max_num_elements: int = 20,
bbox_format: BoxFormat | str = BoxFormat.ltwh,
default_box_format: BoxFormat | str = BoxFormat.xywh,
discrete_x_grid: int = 128,
discrete_y_grid: int = 128,
add_sep_token: bool = True,
sort_by_dict: bool = True,
add_task_embedding: bool = False,
add_task_prompt_token_in_model: bool = False,
num_task_prompt_token: int = 1,
task_id: int | None = None,
decode_max_length: int | None = None,
eval_seed: int | None = None,
gen_t_add_unk_token: bool = False,
gen_ts_add_unk_token: bool = False,
gen_r_add_unk_token: bool = False,
gen_r_compact: bool = False,
bos_token_id: int = 0,
eos_token_id: int = 1,
pad_token_id: int = 2,
is_encoder_decoder: bool = True,
condition_type: ConditionType | str | None = None,
model_type: str | None = None,
transformers_version: str | None = None,
architectures: list[str] | None = None,
output_hidden_states: bool | None = False,
output_attentions: bool | None = False,
return_dict: bool | None = True,
chunk_size_feed_forward: int = 0,
problem_type: Literal[
"regression",
"single_label_classification",
"multi_label_classification",
]
| None = None,
id2label: dict[int | str, str] | None = None,
label2id: dict[str, int] | None = None,
label_translation_metadata: LabelTranslationMetadata
| None = None,
torch_dtype: str | None = None,
dtype: str | None = None,
tie_word_embeddings: bool = True,
task_specific_params: dict[
str,
str
| int
| float
| bool
| list[str | int | float | bool],
]
| None = None,
name_or_path: str = "",
_name_or_path: str | None = None,
_commit_hash: str | None = None,
attn_implementation: str | None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize architecture and task-specific generation defaults.
Source code in models/layoutformerpp/src/layoutformerpp/configuration_layoutformerpp.py
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 | |
constraints ¶
Token constraints used by LayoutFormer++ autoregressive generation.
LayoutFormerPPGenerationConstraint
dataclass
¶
Mask output tokens using the public task conditions.
Source code in models/layoutformerpp/src/layoutformerpp/constraints.py
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
__post_init__ ¶
__post_init__() -> None
Prepare token ids and independent decoding states for each row.
Source code in models/layoutformerpp/src/layoutformerpp/constraints.py
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 | |
__call__ ¶
__call__(
batch_id: int,
step: int,
token_ids: Int[Tensor, "tokens"],
) -> tuple[list[int], int | None]
Return allowed next-token ids and an optional relation rollback point.
Source code in models/layoutformerpp/src/layoutformerpp/constraints.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
conversion ¶
Checkpoint conversion helpers for LayoutFormer++.
DatasetCardMetadata ¶
Bases: TypedDict
Hub-facing metadata for one LayoutFormer++ dataset.
Source code in models/layoutformerpp/src/layoutformerpp/conversion.py
37 38 39 40 41 | |
layoutformerpp_hub_id ¶
layoutformerpp_hub_id(
dataset: DatasetName | str,
task: LayoutFormerPPTask | ConditionType | str,
) -> str
Return the task-specific Hub id for a converted checkpoint.
Source code in models/layoutformerpp/src/layoutformerpp/conversion.py
56 57 58 59 60 61 62 63 64 65 66 | |
load_original_state_dict ¶
load_original_state_dict(
path: Path,
) -> dict[str, Shaped[torch.Tensor, "..."]]
Load a published LayoutFormer++ checkpoint and strip DDP prefixes.
Source code in models/layoutformerpp/src/layoutformerpp/conversion.py
69 70 71 72 73 74 75 76 | |
layoutformerpp_model_card ¶
layoutformerpp_model_card(
*,
dataset: DatasetName | str,
task: LayoutFormerPPTask | ConditionType | str,
parity_metrics: list[ParityMetricInput] | None = None,
) -> ModelCard
Build a Hub model card for one LayoutFormer++ checkpoint.
Source code in models/layoutformerpp/src/layoutformerpp/conversion.py
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 | |
write_layoutformerpp_model_card ¶
write_layoutformerpp_model_card(
output_dir: Path,
*,
dataset: DatasetName | str,
task: LayoutFormerPPTask | ConditionType | str,
parity_metrics: list[ParityMetricInput] | None = None,
) -> Path
Write the checkpoint README model card next to converted weights.
Source code in models/layoutformerpp/src/layoutformerpp/conversion.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
geometry ¶
Geometry helpers for LayoutFormer++ discrete ltwh boxes.
discretize_ltwh ¶
discretize_ltwh(
bbox: Float[Tensor, "... 4"],
*,
x_grid: int = 128,
y_grid: int = 128,
) -> Int[torch.Tensor, "... 4"]
Convert normalized ltwh values to LayoutFormer++ integer bins.
Source code in models/layoutformerpp/src/layoutformerpp/geometry.py
16 17 18 19 20 21 22 23 24 25 26 | |
continuize_ltwh ¶
continuize_ltwh(
ids: Int[Tensor, "... 4"],
*,
x_grid: int = 128,
y_grid: int = 128,
) -> Float[torch.Tensor, "... 4"]
Convert LayoutFormer++ integer bins back to normalized ltwh.
Source code in models/layoutformerpp/src/layoutformerpp/geometry.py
29 30 31 32 33 34 35 36 37 38 39 | |
public_to_discrete_ltwh ¶
public_to_discrete_ltwh(
bbox: Float[Tensor, "... 4"],
*,
box_format: BoxFormat | str = BoxFormat.xywh,
x_grid: int = 128,
y_grid: int = 128,
) -> Int[torch.Tensor, "... 4"]
Convert public normalized boxes to internal discrete ltwh tokens.
Source code in models/layoutformerpp/src/layoutformerpp/geometry.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
discrete_ltwh_to_public ¶
discrete_ltwh_to_public(
ids: Int[Tensor, "... 4"],
*,
box_format: BoxFormat | str = BoxFormat.xywh,
x_grid: int = 128,
y_grid: int = 128,
) -> Float[torch.Tensor, "... 4"]
Convert internal discrete ltwh tokens to public normalized boxes.
Source code in models/layoutformerpp/src/layoutformerpp/geometry.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
labels ¶
Semantic label translation for LayoutFormer++ token sequences.
LabelTranslationMetadata ¶
Bases: TypedDict
JSON-compatible persisted semantic label-map evidence.
Source code in models/layoutformerpp/src/layoutformerpp/labels.py
19 20 21 22 23 24 25 26 27 | |
LabelTranslation
dataclass
¶
Persist source label maps and their name-joined translation.
Source code in models/layoutformerpp/src/layoutformerpp/labels.py
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 | |
metadata ¶
metadata() -> LabelTranslationMetadata
Return JSON-safe maps for configs, checkpoints, and evidence.
Source code in models/layoutformerpp/src/layoutformerpp/labels.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
normalize_label_name ¶
normalize_label_name(name: str) -> str
Return the exact semantic join key used by label translations.
Source code in models/layoutformerpp/src/layoutformerpp/labels.py
59 60 61 62 | |
build_label_translation ¶
build_label_translation(
public_id2label: Mapping[int, str],
sequence_id2label: Mapping[int, str],
) -> LabelTranslation
Join two complete label maps by normalized semantic name.
Raises:
| Type | Description |
|---|---|
ValueError
|
If ids, names, or inverse coverage are incomplete or ambiguous. |
Source code in models/layoutformerpp/src/layoutformerpp/labels.py
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 | |
label_translation_for_dataset ¶
label_translation_for_dataset(
dataset: DatasetName,
) -> LabelTranslation
Return the canonical public-to-sequence label translation.
Source code in models/layoutformerpp/src/layoutformerpp/labels.py
187 188 189 190 191 192 193 194 195 | |
validate_label_translation_metadata ¶
validate_label_translation_metadata(
metadata: LabelTranslationMetadata,
expected: LabelTranslation,
) -> LabelTranslationMetadata
Normalize persisted label metadata and reject any changed field.
Source code in models/layoutformerpp/src/layoutformerpp/labels.py
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 | |
modeling_layoutformerpp ¶
PyTorch model wrapper for LayoutFormer++.
PositionalEncoding ¶
Bases: Module
Learned positional embeddings matching the original implementation.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
__init__ ¶
__init__(
d_model: int, dropout: float = 0.1, max_len: int = 512
) -> None
Initialize learned position tokens.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
100 101 102 103 104 | |
forward ¶
forward(
x: Float[Tensor, "seq batch channels"],
) -> Float[torch.Tensor, "seq batch channels"]
Add learned position tokens to (seq, batch, hidden) input.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
106 107 108 109 110 | |
LayoutFormerPPForConditionalGeneration ¶
Bases: PreTrainedModel
Transformers PreTrainedModel with checkpoint-compatible module names.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
__init__ ¶
__init__(config: LayoutFormerPPConfig) -> None
Initialize checkpoint-compatible encoder/decoder modules.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
encode ¶
encode(
input_ids: Int[Tensor, "batch tokens"],
padding_mask: Bool[Tensor, "batch tokens"],
task_ids: Int[Tensor, "batch"] | None = None,
) -> tuple[
Float[torch.Tensor, "seq batch channels"],
Bool[torch.Tensor, "batch seq"],
]
Encode input token ids with optional task prompt embeddings.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
prepare_decoder_input_ids_from_labels ¶
prepare_decoder_input_ids_from_labels(
labels: Int[Tensor, "batch tokens"],
) -> Int[torch.Tensor, "batch tokens"]
Shift labels right and prepend BOS.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
225 226 227 228 229 230 | |
forward ¶
forward(
input_ids: Int[Tensor, "batch tokens"],
attention_mask: Bool[Tensor, "batch tokens"]
| None = None,
labels: Int[Tensor, "batch tokens"] | None = None,
decoder_input_ids: Int[Tensor, "batch tokens"]
| None = None,
task_ids: Int[Tensor, "batch"] | None = None,
return_dict: bool | None = None,
) -> (
Seq2SeqLMOutput | tuple[Float[torch.Tensor, "..."], ...]
)
Run teacher-forced LayoutFormer++ decoding.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
generate_square_subsequent_mask ¶
generate_square_subsequent_mask(
size: int, device: device
) -> Float[torch.Tensor, "target target"]
Create the causal decoder mask used by the checkpoint model.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
77 78 79 80 81 82 | |
top_k_logits ¶
top_k_logits(
logits: Float[Tensor, "batch vocab"], k: int
) -> Float[torch.Tensor, "batch vocab"]
Mask logits outside the top-k set.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
85 86 87 88 89 90 91 92 93 94 | |
pipeline_layoutformerpp ¶
Pipeline wrapper for LayoutFormer++.
LayoutFormerPPPipeline ¶
Bases: LayoutGenerationPipeline
Compose a LayoutFormer++ model and processor for layout generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
LayoutFormerPPForConditionalGeneration
|
Converted LayoutFormer++ model. |
required |
processor
|
LayoutFormerPPProcessor
|
Matching processor/tokenizer. |
required |
config
|
LayoutFormerPPConfig | None
|
Optional root pipeline config. Defaults to |
None
|
Examples:
>>> processor = LayoutFormerPPProcessor.from_config(dataset="rico", task="gen_t")
>>> config = LayoutFormerPPConfig(vocab_size=processor.tokenizer.vocab_size)
>>> pipe = LayoutFormerPPPipeline(
... model=LayoutFormerPPForConditionalGeneration(config),
... processor=processor,
... )
>>> pipe.config.model_type
'layoutformerpp'
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
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 | |
__init__ ¶
__init__(
model: LayoutFormerPPForConditionalGeneration,
processor: LayoutFormerPPProcessor,
config: LayoutFormerPPConfig | None = None,
) -> None
Initialize the pipeline with model and processor components.
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
110 111 112 113 114 115 116 117 118 119 120 | |
__call__ ¶
__call__(
batch_size: int = 1,
seed: int | None = None,
generator: Generator | None = None,
condition_type: ConditionType
| str = ConditionType.unconditional,
labels: list[list[int | str]]
| Int[Tensor, "batch elements"]
| None = None,
bbox: LayoutFormerPPBBoxInput = None,
mask: Bool[Tensor, "batch elements"] | None = None,
relations: list[list[tuple[int, int, int, int, int]]]
| Int[Tensor, "batch relations relation_attrs"]
| None = None,
num_elements: int
| list[int]
| Int[Tensor, "batch"]
| None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
num_inference_steps: int | None = None,
output_type: OutputType | str = OutputType.dataclass,
return_intermediates: bool = False,
max_length: int | None = None,
do_sample: bool | None = None,
top_k: int = 10,
temperature: float = 0.7,
) -> LayoutGenerationOutput | LayoutFormerPPOutputDict
Generate layouts by encoding conditions, generating ids, and decoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of layouts to generate when labels are omitted. |
1
|
seed
|
int | None
|
Convenience seed used only when |
None
|
generator
|
Generator | None
|
Optional PyTorch generator; takes precedence over |
None
|
condition_type
|
ConditionType | str
|
Canonical condition type or supported alias. |
unconditional
|
labels
|
list[list[int | str]] | Int[Tensor, 'batch elements'] | None
|
Optional label conditions. |
None
|
bbox
|
LayoutFormerPPBBoxInput
|
Optional layout boxes for size/completion/refinement conditions. |
None
|
mask
|
Bool[Tensor, 'batch elements'] | None
|
Reserved public validity mask input. |
None
|
relations
|
list[list[tuple[int, int, int, int, int]]] | Int[Tensor, 'batch relations relation_attrs'] | None
|
Relation tuples use one-based indexes within each label. For non-canvas sources, the source element must precede its target in the label sequence used for decoding. |
None
|
num_elements
|
int | list[int] | Int[Tensor, 'batch'] | None
|
Reserved v1 interface argument. |
None
|
box_format
|
BoxFormat | str
|
Input and output bounding-box format. |
xywh
|
normalized
|
bool
|
Whether public boxes are normalized. |
True
|
canvas_size
|
tuple[int, int] | None
|
Reserved v1 interface argument. |
None
|
num_inference_steps
|
int | None
|
Reserved v1 interface argument. |
None
|
output_type
|
OutputType | str
|
Return |
dataclass
|
return_intermediates
|
bool
|
Reserved output detail flag. |
False
|
max_length
|
int | None
|
Optional token decode length override. |
None
|
do_sample
|
bool | None
|
Optional sampling override. |
None
|
top_k
|
int
|
Top-k value used by the reference sampling loop. |
10
|
temperature
|
float
|
Sampling temperature. |
0.7
|
When max_length is omitted and the model config matches its dataset/task
default, the pipeline uses 120 tokens for unconditional, completion, label,
and label-size generation, 150 for relation generation, or input length plus
one for refinement. This equality rule also applies when the config value was
set explicitly; pass max_length to select another per-call limit.
For batches larger than one, the probability gate uses row zero, relation rollback uses the last row's rollback point, and a rollback truncates every row to the same prefix.
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | LayoutFormerPPOutputDict
|
Layout generation output dataclass or dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If processor inputs or relation label/index references are invalid. |
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
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 | |
processing_layoutformerpp ¶
Processor for LayoutFormer++ conditions and generated sequences.
LayoutFormerPPProcessor ¶
Bases: ProcessorMixin
Build LayoutFormer++ text inputs and parse generated layouts.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
__init__ ¶
__init__(
tokenizer: LayoutFormerPPTokenizer,
dataset: DatasetName | str = DEFAULT_DATASET,
task: LayoutFormerPPTask
| ConditionType
| str = DEFAULT_TASK,
add_sep_token: bool = True,
x_grid: int = 128,
y_grid: int = 128,
id2label: dict[int, str] | None = None,
label_translation_metadata: LabelTranslationMetadata
| None = None,
) -> None
Initialize serializers, label maps, and tokenizer state.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 | |
from_config
classmethod
¶
from_config(
dataset: DatasetName | str = DEFAULT_DATASET,
task: LayoutFormerPPTask
| ConditionType
| str = DEFAULT_TASK,
*,
add_sep_token: bool = True,
x_grid: int = 128,
y_grid: int = 128,
id2label: dict[int, str] | None = None,
) -> "LayoutFormerPPProcessor"
Construct processor and tokenizer without external files.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 | |
normalize_condition_type ¶
normalize_condition_type(
condition_type: ConditionType | str,
) -> SupportedConditionType
Normalize public condition aliases.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
194 195 196 197 198 199 200 201 202 203 204 205 206 | |
__call__ ¶
__call__(
condition_type: ConditionType
| str = ConditionType.unconditional,
labels: list[list[int | str]]
| Int[Tensor, "batch elements"]
| None = None,
bbox: LayoutFormerPPBBoxInput = None,
mask: Bool[Tensor, "batch elements"]
| list[list[bool]]
| list[bool]
| None = None,
relations: list[list[tuple[int, int, int, int, int]]]
| Int[Tensor, "batch relations relation_attrs"]
| None = None,
batch_size: int | None = None,
box_format: BoxFormat | str = BoxFormat.xywh,
normalized: bool = True,
canvas_size: tuple[int, int] | None = None,
return_tensors: Literal["pt"] = "pt",
) -> BatchEncoding
Build tokenized model inputs for a public condition.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
post_process_layouts ¶
post_process_layouts(
sequences: Int[Tensor, "batch tokens"],
*,
box_format: BoxFormat | str = BoxFormat.xywh,
output_type: OutputType | str = OutputType.dataclass,
return_tensors: Literal["pt"] = "pt",
) -> LayoutGenerationOutput | LayoutFormerPPOutputDict
Parse generated token ids to the common layout output schema.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
serialization ¶
Task serializers for LayoutFormer++.
RelationType ¶
Bases: StrEnum
Supported LayoutFormer++ relation token names.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
19 20 21 22 23 24 25 26 27 28 29 | |
ParsedLayout
dataclass
¶
Parsed discrete layout sequence.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
35 36 37 38 39 40 | |
T5LayoutSequence ¶
Serialize labels and discrete ltwh bboxes as LayoutFormer++ text.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
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 | |
__init__ ¶
__init__(
id2label: dict[int, str], *, add_sep_token: bool = True
) -> None
Initialize label lookup tables used by the serializer.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
46 47 48 49 50 51 | |
build_seq ¶
build_seq(labels: list[int], bbox: list[list[int]]) -> str
Build full label x y w h sequence.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
53 54 55 56 57 58 59 60 61 | |
parse_seq ¶
parse_seq(output: str) -> ParsedLayout | None
Parse generated text into labels and integer boxes.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.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 | |
T5LayoutSequenceForGenT ¶
Bases: T5LayoutSequence
Serializer for gen_t and gen_ts conditions.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
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 | |
build_input_seq ¶
build_input_seq(
task: LayoutFormerPPTask | str,
labels: list[int],
bbox: list[list[int]],
*,
add_unk_for_label: bool = False,
add_unk_for_label_size: bool = False,
) -> str
Build task input from labels and optional width/height constraints.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
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 | |
T5LayoutSequenceForGenR ¶
Bases: T5LayoutSequence
Serializer for relation-conditioned generation.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
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 | |
build_input_seq ¶
build_input_seq(
labels: list[int],
relations: list[tuple[int, int, int, int, int]],
*,
add_unk_token: bool = False,
compact: bool = False,
) -> str
Build relation-conditioned input sequence.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
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 | |
build_default_tokens ¶
build_default_tokens(
dataset_labels: tuple[str, ...],
*,
task: LayoutFormerPPTask | str,
grid: int,
add_sep_token: bool = True,
) -> list[str]
Construct a checkpoint-compatible vocabulary when no vocab.json is available.
Source code in models/layoutformerpp/src/layoutformerpp/serialization.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
tasks ¶
Shared LayoutFormer++ dataset and checkpoint-task helpers.
LayoutFormerPPTask ¶
Bases: StrEnum
Supported converted LayoutFormer++ checkpoint variants.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
12 13 14 15 16 17 18 19 20 | |
OutputType ¶
Bases: StrEnum
Supported post-processing return shapes.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
23 24 25 26 27 | |
normalize_layoutformerpp_dataset ¶
normalize_layoutformerpp_dataset(
dataset: DatasetName | str,
) -> DatasetName
Normalize public dataset aliases to a LayoutFormer++ supported dataset.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
57 58 59 60 61 62 63 | |
layoutformerpp_dataset_slug ¶
layoutformerpp_dataset_slug(
dataset: DatasetName | str,
) -> str
Return the canonical public dataset slug.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
66 67 68 | |
layoutformerpp_vendor_dataset_slug ¶
layoutformerpp_vendor_dataset_slug(
dataset: DatasetName | str,
) -> str
Translate a canonical dataset name at an original-code boundary.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
71 72 73 | |
normalize_layoutformerpp_task ¶
normalize_layoutformerpp_task(
task: LayoutFormerPPTask | ConditionType | str,
) -> LayoutFormerPPTask
Normalize checkpoint variant aliases to the internal task enum.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
tokenization_layoutformerpp ¶
Tokenizer for LayoutFormer++ layout strings.
LayoutFormerPPTokenizer ¶
Bases: WhitespaceTokenizerMixin, PreTrainedTokenizer
Whitespace tokenizer backed by the released vocab.json format.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 | |
__init__ ¶
__init__(
vocab_file: str | None = None,
tokens: list[str] | None = None,
x_grid: int = 128,
y_grid: int = 128,
bbox_order: BoxFormat | str = BoxFormat.ltwh,
bos_token: str = "<bos>",
eos_token: str = "<eos>",
pad_token: str = "<pad>",
sep_token: str = "<sep>",
unk_token: str = "<unk>",
model_max_length: int = DEFAULT_MODEL_MAX_LENGTH,
padding_side: str = "right",
truncation_side: str = "right",
clean_up_tokenization_spaces: bool = False,
added_tokens_decoder: dict[int | str, str]
| None = None,
backend: str = "custom",
tokenizer_file: str | None = None,
name_or_path: str = "",
is_local: bool = False,
local_files_only: bool = False,
processor_class: str | None = None,
) -> None
Initialize a tokenizer from a vocab file or synthetic token list.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
27 28 29 30 31 32 33 34 35 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 | |
save_vocabulary ¶
save_vocabulary(
save_directory: str, filename_prefix: str | None = None
) -> tuple[str]
Save vocab.json in checkpoint-compatible token-to-id format.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
78 79 80 81 82 83 84 85 86 87 | |
save_pretrained ¶
save_pretrained(
save_directory: str | PathLike[str],
legacy_format: bool | None = None,
filename_prefix: str | None = None,
push_to_hub: bool = False,
**kwargs: str | int | float | bool | None,
) -> tuple[str, ...]
Save tokenizer files plus LayoutFormer++ tokenizer metadata.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
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 | |
from_pretrained
classmethod
¶
from_pretrained(
pretrained_model_name_or_path: str | PathLike[str],
cache_dir: str | PathLike[str] | None = None,
force_download: bool = False,
local_files_only: bool = False,
token: str | bool | None = None,
revision: str = "main",
x_grid: int | None = None,
y_grid: int | None = None,
bbox_order: BoxFormat | str | None = None,
) -> "LayoutFormerPPTokenizer"
Load tokenizer and LayoutFormer++ metadata.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
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 | |
encode_text ¶
encode_text(
text: str | list[str],
*,
add_eos: bool = True,
add_bos: bool = False,
) -> BatchEncoding
Tokenize reference-style text while matching the original EOS/BOS behavior.
Source code in models/layoutformerpp/src/layoutformerpp/tokenization_layoutformerpp.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
training ¶
Package-local LayoutFormer++ training entry points.
lightning_module ¶
PyTorch Lightning module for LayoutFormer++ training.
LayoutFormerPPTrainingConfig ¶
Bases: TypedDict
LightningCLI-safe constructor values for the runtime model config.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
LayoutFormerPPPreOptimizerTrace
dataclass
¶
Fixed-batch values produced before a training optimizer mutation.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
LayoutFormerPPDataModule ¶
Bases: LightningDataModule
Package-local DataModule matching the pinned basic loader behavior.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | |
__init__ ¶
__init__(
*,
recipe_name: str,
data_root: str | None = None,
num_workers: int = 0,
seed: int = 0,
pin_memory: bool = False,
) -> None
Initialize source-root, recipe, and worker-seeding configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recipe_name
|
str
|
Registered recipe used to select dataset and tasks. |
required |
data_root
|
str | None
|
Processed source-data root, or |
None
|
num_workers
|
int
|
Number of DataLoader worker processes. |
0
|
seed
|
int
|
Reserved loader seed recorded with the module configuration. |
0
|
pin_memory
|
bool
|
Whether DataLoader batches use pinned host memory. |
False
|
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
setup ¶
setup(stage: str | None = None) -> None
Load the processed train and validation splits for stage.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
train_dataloader ¶
train_dataloader() -> DataLoader[_LayoutFormerPPSample]
Return the shuffled, drop-last production training loader.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |
val_dataloader ¶
val_dataloader() -> DataLoader[_LayoutFormerPPSample]
Return the deterministic, drop-last production validation loader.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | |
LayoutFormerPPTrainingModule ¶
Bases: LightningModule
Train the genuine runtime LayoutFormer++ model with faithful static wiring.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 | |
__init__ ¶
__init__(
*,
recipe_name: str,
config: LayoutFormerPPTrainingConfig,
model: LayoutFormerPPForConditionalGeneration
| None = None,
) -> None
Initialize one immutable recipe and its runtime model.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
forward ¶
forward(
input_ids: Int[Tensor, "batch tokens"],
attention_mask: Bool[Tensor, "batch tokens"]
| None = None,
decoder_input_ids: Int[Tensor, "batch target_tokens"]
| None = None,
task_ids: Int[Tensor, "batch"] | None = None,
) -> Shaped[torch.Tensor, "batch target_tokens vocab"]
Return logits from the owned runtime model.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
pre_optimizer_trace ¶
pre_optimizer_trace(
batch: Mapping[
str,
Shaped[Tensor, "..."]
| list[str]
| list[bytes]
| None,
],
) -> LayoutFormerPPPreOptimizerTrace
Capture package-model inputs, logits, and loss before an optimizer step.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | |
training_step ¶
training_step(
batch: Mapping[
str,
Shaped[Tensor, "..."]
| list[str]
| list[bytes]
| None,
],
batch_idx: int,
) -> Float[torch.Tensor, ""]
Compute the package training loss without altering runtime loss semantics.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
836 837 838 839 840 841 842 843 844 845 846 847 | |
validation_step ¶
validation_step(
batch: Mapping[
str,
Shaped[Tensor, "..."]
| list[str]
| list[bytes]
| None,
],
batch_idx: int,
) -> Float[torch.Tensor, ""]
Compute aggregate validation loss used for checkpoint selection.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
849 850 851 852 853 854 855 856 857 858 859 860 | |
configure_optimizers ¶
configure_optimizers() -> OptimizerLRScheduler
Construct basic-mode Adam and the post-update logarithmic scheduler.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
lr_scheduler_step ¶
lr_scheduler_step(
scheduler: LRScheduler, metric: float | None
) -> None
Advance exactly once after Lightning completes an optimizer update.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
882 883 884 885 886 887 888 889 | |
vendor_effective_cross_entropy ¶
vendor_effective_cross_entropy(
logits: Float[Tensor, "batch tokens vocab"],
targets: Int[Tensor, "batch tokens"],
) -> Float[torch.Tensor, ""]
Compute the pad-inclusive cross-entropy used by the reference recipe.
Source code in models/layoutformerpp/src/layoutformerpp/training/lightning_module.py
92 93 94 95 96 97 98 99 100 | |
parity ¶
Static-state comparison helpers for LayoutFormer++ training evidence.
StaticStateComparison
dataclass
¶
Exhaustive state-key and parameter-count comparison.
Source code in models/layoutformerpp/src/layoutformerpp/training/parity.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
parameter_count ¶
parameter_count(module: Module) -> int
Count unique parameter storage exposed by a module.
Source code in models/layoutformerpp/src/layoutformerpp/training/parity.py
35 36 37 | |
state_dict_sha256 ¶
state_dict_sha256(
state_dict: Mapping[str, Shaped[Tensor, "..."]],
) -> str
Hash sorted state names, tensor metadata, and raw CPU bytes.
Source code in models/layoutformerpp/src/layoutformerpp/training/parity.py
40 41 42 43 44 45 46 47 48 49 50 51 52 | |
compare_static_state ¶
compare_static_state(
reference: Module, package: Module
) -> StaticStateComparison
Compare exhaustive keys, parameter counts, and copied tensor bytes.
Source code in models/layoutformerpp/src/layoutformerpp/training/parity.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
recipes ¶
Immutable training recipes for LayoutFormer++ checkpoint families.
LayoutFormerPPTrainingRecipe
dataclass
¶
Static training identity for one dataset and condition family.
Source code in models/layoutformerpp/src/layoutformerpp/training/recipes.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 | |
get_training_recipe ¶
get_training_recipe(
name: str,
) -> LayoutFormerPPTrainingRecipe
Return one faithful recipe by its canonical config stem.
Source code in models/layoutformerpp/src/layoutformerpp/training/recipes.py
372 373 374 375 376 377 | |
scheduler ¶
Optimizer-update scheduler used by LayoutFormer++ training.
LayoutFormerPPWarmupLR ¶
Bases: LRScheduler
Logarithmic warmup with an explicit no-eager-step initial state.
Source code in models/layoutformerpp/src/layoutformerpp/training/scheduler.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 | |
__init__ ¶
__init__(
optimizer: Optimizer,
*,
warmup_num_steps: int,
warmup_min_lr: float = 0.0,
warmup_max_lr: float = 0.0001,
last_batch_iteration: int = -1,
) -> None
Capture optimizer state without changing its initial learning rate.
Source code in models/layoutformerpp/src/layoutformerpp/training/scheduler.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
step ¶
step(epoch: int | None = None) -> None
Advance once after a successful optimizer update.
Source code in models/layoutformerpp/src/layoutformerpp/training/scheduler.py
47 48 49 50 51 52 53 54 55 56 57 58 59 | |
get_lr ¶
get_lr() -> list[float | Shaped[torch.Tensor, '']]
Return learning rates for the current original-code index.
Source code in models/layoutformerpp/src/layoutformerpp/training/scheduler.py
61 62 63 64 65 | |