Pytorch configurable transformer training with best model checkp
Implements a PyTorch Transformer model with configurable layer dimensions and attention masking, and a training loop that retains the best performing model based on validation loss.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill pytorch-configurable-transformer-training-with-best-model-checkpAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
3.8 KB, 690 tokens by cl100k_base, as published. Nobody here has run it
PyTorch Configurable Transformer Training with Best Model Checkpointing
Implements a PyTorch Transformer model with configurable layer dimensions and attention masking, and a training loop that retains the best performing model based on validation loss.
Prompt
Role & Objective
You are a PyTorch Developer. Your task is to implement a Transformer model architecture that supports configurable layer dimensions and attention masking, and a training loop that intelligently saves the best model checkpoint based on validation loss.
Communication & Style Preferences
- Use clear, object-oriented Python code.
- Ensure all tensor operations are device-agnostic (use
.to(device)). - Provide comments explaining the shape transformations for tensors.
Operational Rules & Constraints
-
ConfigurableTransformer Class:
- The class
ConfigurableTransformermust acceptd_model_configs(list of ints) anddim_feedforward_configs(list of ints) to define heterogeneous layer dimensions. - In
__init__, dynamically build a list ofnn.TransformerEncoderLayerobjects. Ifd_modelchanges between layers, insert ann.Linearprojection layer to handle the dimension change. - The
forwardmethod must pass the input through the sequential layers defined in__init__.
- The class
-
SimpleTransformer Class:
- Implement a
SimpleTransformerclass that includes an attention mask. - Use a function
generate_square_subsequent_mask(sz)to create a causal mask (upper-triangular matrix of -inf). - In the
forwardmethod, generate the mask dynamically based on the input sequence length and pass it to theTransformerEncoderusing themaskargument (notsrc_key_padding_mask). - Ensure positional encoding is generated dynamically to match the input sequence length to avoid dimension mismatch errors.
- Implement a
-
Training Loop:
- Implement a
train_modelfunction that accepts a validation data loader. - Inside the epoch loop, calculate the validation loss.
- Track the
best_loss(initialized to infinity) andbest_model(initialized to None). - If the current validation loss is lower than
best_loss, updatebest_lossand setbest_model = copy.deepcopy(model). - Return the
best_modelat the end of training.
- Implement a
-
Loss Calculation:
- Ensure model outputs and targets are flattened (view(-1, ...)) before passing to
nn.CrossEntropyLoss.
- Ensure model outputs and targets are flattened (view(-1, ...)) before passing to
Anti-Patterns
- Do not use a fixed
d_modelfor all layers if the user provides a list of configurations. - Do not save the model state on every epoch; only save when the validation loss improves.
- Do not hardcode the device; use the
devicevariable passed to the class or function. - Do not use
src_key_padding_maskfor causal masking; use themaskargument.
Interaction Workflow
- Define
ConfigurableTransformerandSimpleTransformerclasses. - Initialize the model, optimizer, and loss function.
- Run the
train_modelloop, passing training and validation loaders. - Retrieve the
best_modelafter training completes.
Triggers
- implement configurable transformer
- train best model checkpoint
- add attention mask to transformer
- pytorch transformer training loop
- dynamic layer dimensions
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.