Model and training
The Lightning module and the network it wraps.
Generated from the source by tools/make_api.py; do not edit by hand.
papie.model
apply_warmup
apply_warmup(optimizer, step, warmup_steps)
Linear LR warmup, applied in place onoptimizerfor optimizer stepstep(0-based). Remembers each group's base lr, reaches it exactly atstep == warmup_steps - 1and leaves the lr alone afterwards (so ReduceLROnPlateau still works).
quantize_dynamic
quantize_dynamic(model, inplace=True)
Apply INT8 dynamic quantization to the RNN and Linear layers for faster CPU inference (no retraining, small accuracy delta). Opt-in.inplace=Trueavoids a deepcopy of the (freshly-loaded) model: some attached objects (e.g. the label encoder) aren't deepcopy-able. Quantized RNNs are notnn.RNNBasesubclasses, butRNNEmbeddingonly relies on a PackedSequence-compatible forward, which they provide.
get_context
get_context(outs, wemb, wlen, context_type)
Build the per-word context vector fed to a contextual (attentional) decoder, flattening the padded sentence batch to a flat word batch. Mirrors pie.models.model.get_context.
class PIEModel (nn.Module)
PIEModel(self, label_encoder, tasks: dict, wemb_dim, cemb_dim, cemb_layers, sent_layers, sent_hidden_size, include_lm, rnn_cell='GRU', dropout=0.0, use_byt5=False, byt5_model_name='google/byt5-small', byt5_num_layers=1, byt5_output_dim=None, byt5_normalization=None, use_sentence_encoder=True, source_encoder=None, soter_config=None, candidates=None)
PIEModel.enable_cache
enable_cache(self, maxsize=100000)
Enable the char-embedding cache for inference (see RNNEmbedding). Exact: the char encoding is context-free, so cached output is identical to uncached. Gives within-batch dedup and cross-request reuse.
PIEModel.disable_cache
disable_cache(self)
PIEModel.forward
forward(self, inputs, target_tasks, use_lm=False)
PIEModel.predict
predict(self, inputs, target_tasks, return_probs=False, use_lm=False)
inputs : DataLoader batch tasks : list of str, tasks to predict Char-level (attentional) tasks are decoded autoregressively withpredict_max(no gold targets needed); token-level tasks reuse the teacher-forcing-free linear path vialogits_to_preds.
PIEModel.logits_to_preds
logits_to_preds(self, inputs, task_to_logits, return_probs=False, decode=True)
PIEModel.decode_preds
decode_preds(self, preds)
PIEModel.load_state_dict_from_pretrained
load_state_dict_from_pretrained(self, label_encoder_pretrained, state_dict_pretrained, exclude=[])
Warm-start (fine-tuning) from a pretrained model's weights, copied block-by-block and, where vocabularies differ, label-by-label. Ported from pie.models.model.SimpleModel.load_state_dict_from_pretrained.state_dict_pretrainedis an *inner* PIEModel state_dict (keys likecemb.*,encoder.rnn.*,<task>_decoder.*,lm_*) — i.e. the converter's.state_dict.ptor a checkpoint'sstate_dictwith themodel.prefix stripped. Copies into module references, so thePIEEmbeddingwrapper (which shares the same cemb/wemb objects) sees the updates without extra handling. Parameters ========== label_encoder_pretrained : MultiLabelEncoder of the pretrained model state_dict_pretrained : OrderedDict of the pretrained inner PIEModel exclude : list, parts to skip. Values: "wemb", "cemb", "cemb_rnn", "sent_rnn", "lm", and any task name.
class PaPie (L.LightningModule)
PaPie(self, multilabelencoder: MultiLabelEncoder, tasks_dict: dict[str, Task], include_lm: bool, model_config: dict, task_scheduler_config: dict, optimizer_config: dict, lr_scheduler_config: dict, sample_log_n: int=20, candidates: Optional[dict]=None, init_from: Optional[str]=None, init_from_mode: str='exact', init_from_exclude: Optional[list]=None, freeze_encoder: bool=False)
init_from: path of a papie checkpoint whose weights warm-start this model when training begins (fitwithout--ckpt_path; exact load, so architecture and label vocabulary must match; fresh optimizer and scheduler). Used byfit-silverto fine-tune on gold data after a silver phase.candidates(top level on purpose, so a small yaml can be layered on any config with-c): external lemma/POS candidate features, seepapie.lexiconand the "Candidate features" page of the documentation.None= off.
PaPie.train
train(self, mode: bool=True)
PaPie.configure_callbacks
configure_callbacks(self)
PaPie.on_before_optimizer_step
on_before_optimizer_step(self, optimizer)
PaPie.configure_optimizers
configure_optimizers(self)
PaPie.compute_loss
compute_loss(self, batch, logits_dict, target_tasks, skip_lm=False)
PaPie.compute_metrics
compute_metrics(self, inputs, preds)
Compute accuracy for each task :param inputs: batch (dict) :param preds: dict of predicted labels (encoded = integers)
PaPie.log_loss
log_loss(self, total_loss, tasks_losses, prefix, batch_size, on_step=False, on_epoch=True, **log_kwargs)
PaPie.log_metrics
log_metrics(self, prefix, on_step=False, on_epoch=True, **log_kwargs)
PaPie.on_train_epoch_start
on_train_epoch_start(self)
Advance any MaskedCharacterDecoder's masking/length curriculum (full_mask_probability low->high, length_weight high->low) by training progress = current_epoch / (max_epochs - 1).
PaPie.training_step
training_step(self, batch, batch_idx)
PaPie.validation_step
validation_step(self, batch, batch_idx)
PaPie.on_validation_epoch_end
on_validation_epoch_end(self)
PaPie.test_step
test_step(self, batch, batch_idx, dataloader_idx=0)
PaPie.on_test_epoch_start
on_test_epoch_start(self)
PaPie.on_test_epoch_end
on_test_epoch_end(self)
PaPie.predict_step
predict_step(self, batch, batch_idx, dataloader_idx=0)
PaPie.enable_cache
enable_cache(self, maxsize=100000)
Enable the char-embedding cache on the inner model (see RNNEmbedding).
PaPie.disable_cache
disable_cache(self)
PaPie.quantize
quantize(self)
In-place INT8 dynamic quantization of the inner model's RNN/Linear layers for faster CPU inference. Call after loading, before predicting.
PaPie.setup
setup(self, stage=None)
PaPie.init_from_pretrained
init_from_pretrained(self, ckpt_path, exclude=[])
Warm-start this (freshly built) model from a pretrained papie
checkpoint, transferring weights block-/label-by-label so the two may
have different vocabularies or task sets. See
PIEModel.load_state_dict_from_pretrained.