Layoutformerpp
Transformers-style LayoutFormer++ components.
ConditionType ¶
Bases: StrEnum
Canonical condition names used by layout generation interfaces.
Source code in lib/laygen/src/laygen/common/conditions.py
9 10 11 12 13 14 15 16 17 18 19 20 21 | |
LayoutGenerationOutput
dataclass
¶
Bases: ModelOutput
Canonical layout-generation output for Transformers-style APIs.
Attributes:
| Name | Type | Description |
|---|---|---|
bbox |
Float[ndarray, 'batch elements 4'] | Float[Tensor, 'batch elements 4']
|
Normalized center |
labels |
Int[ndarray, 'batch elements'] | Int[Tensor, 'batch elements']
|
Dataset-local integer labels with shape |
mask |
Bool[ndarray, 'batch elements'] | Bool[Tensor, 'batch elements']
|
Boolean valid-element mask with shape |
id2label |
dict[int, str]
|
Mapping from integer label ids to display names. |
sequences |
object | None
|
Optional raw token sequences. |
scores |
object | None
|
Optional per-token or per-element scores. |
trajectory |
object | None
|
Optional sampling trajectory. |
intermediates |
object | None
|
Optional model-specific debug or auxiliary data. |
Examples:
>>> import numpy as np
>>> output = LayoutGenerationOutput(
... bbox=np.zeros((1, 1, 4), dtype=np.float32),
... labels=np.zeros((1, 1), dtype=np.int64),
... mask=np.ones((1, 1), dtype=bool),
... id2label={0: "text"},
... )
>>> output["bbox"].shape
(1, 1, 4)
Source code in lib/laygen/src/laygen/modeling_outputs.py
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 | |
LayoutFormerPPConfig ¶
Bases: PretrainedConfig
Stores model, tokenizer, and task defaults for converted checkpoints.
Source code in models/layoutformerpp/src/layoutformerpp/configuration_layoutformerpp.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 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 | |
__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,
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
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 | |
LayoutFormerPPForConditionalGeneration ¶
Bases: PreTrainedModel
Transformers PreTrainedModel with checkpoint-compatible module names.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
__init__ ¶
__init__(config: LayoutFormerPPConfig) -> None
Initialize checkpoint-compatible encoder/decoder modules.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
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
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 | |
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
162 163 164 165 166 167 | |
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
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 | |
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
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 | |
__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
104 105 106 107 108 109 110 111 112 113 114 | |
__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
|
Optional relation tuples for relation-conditioned checkpoints. |
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
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | LayoutFormerPPOutputDict
|
Layout generation output dataclass or dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If processor inputs are invalid. |
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
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 | |
LayoutFormerPPProcessor ¶
Bases: ProcessorMixin
Build LayoutFormer++ text inputs and parse generated layouts.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 | |
__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,
) -> None
Initialize serializers, label maps, and tokenizer state.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 | |
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
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 | |
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
160 161 162 163 164 165 166 167 168 169 170 171 172 | |
__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
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 | |
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
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 | |
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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
LayoutFormerPPConfig ¶
Bases: PretrainedConfig
Stores model, tokenizer, and task defaults for converted checkpoints.
Source code in models/layoutformerpp/src/layoutformerpp/configuration_layoutformerpp.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 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 | |
__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,
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
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 | |
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 | |
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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
__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
42 43 44 45 46 | |
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
48 49 50 51 52 | |
LayoutFormerPPForConditionalGeneration ¶
Bases: PreTrainedModel
Transformers PreTrainedModel with checkpoint-compatible module names.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
__init__ ¶
__init__(config: LayoutFormerPPConfig) -> None
Initialize checkpoint-compatible encoder/decoder modules.
Source code in models/layoutformerpp/src/layoutformerpp/modeling_layoutformerpp.py
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 | |
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
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 | |
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
162 163 164 165 166 167 | |
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
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 | |
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
19 20 21 22 23 24 | |
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
27 28 29 30 31 32 33 34 35 36 | |
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
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 | |
__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
104 105 106 107 108 109 110 111 112 113 114 | |
__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
|
Optional relation tuples for relation-conditioned checkpoints. |
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
|
Returns:
| Type | Description |
|---|---|
LayoutGenerationOutput | LayoutFormerPPOutputDict
|
Layout generation output dataclass or dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If processor inputs are invalid. |
Source code in models/layoutformerpp/src/layoutformerpp/pipeline_layoutformerpp.py
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 | |
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
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 | |
__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,
) -> None
Initialize serializers, label maps, and tokenizer state.
Source code in models/layoutformerpp/src/layoutformerpp/processing_layoutformerpp.py
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 | |
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
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 | |
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
160 161 162 163 164 165 166 167 168 169 170 171 172 | |
__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
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 | |
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
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 | |
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
53 54 55 56 57 58 59 | |
layoutformerpp_dataset_slug ¶
layoutformerpp_dataset_slug(
dataset: DatasetName | str,
) -> str
Return the dataset slug for a LayoutFormer++ dataset.
Source code in models/layoutformerpp/src/layoutformerpp/tasks.py
62 63 64 | |
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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
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 | |