pytorch-lightning – skill Dữ liệu & Khoa học cho agent AI
Framework học sâu (PyTorch Lightning / gói lightning). Tổ chức mã PyTorch thành LightningModules, cấu hình Trainer cho multi-GPU/TPU, xây dựng pipeline dữ liệu, callbacks, ghi log (W&B, TensorBoard, MLflow), huấn luyện phân tán (DDP, FSDP, DeepSpeed) để huấn luyện mạng nơ-ron có khả năng mở rộng.
Cài skill pytorch-lightning bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/pytorch-lightning-29e1f.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /pytorch-lightning – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/pytorch-lightning-29e1f.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $pytorch-lightning – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/pytorch-lightning-29e1f.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /pytorch-lightning – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/pytorch-lightning-29e1f.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /pytorch-lightning – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
PyTorch Lightning
Overview
PyTorch Lightning is a deep learning framework that organizes PyTorch code to eliminate boilerplate while maintaining full flexibility. Automate training workflows, multi-device orchestration, and implement best practices for neural network training and scaling across multiple GPUs/TPUs.
Reviewed release: [Lightning 2.6.6](https://github.com/Lightning-AI/pytorch-lightning/releases/tag/2.6.6), including its checkpoint-loading security fixes. CPU tests use Python 3.11, Torch 2.14.1 and Lightning 2.6.6. Use import lightning as L with the lightning distribution. The alternative pytorch-lightning distribution exposes import pytorch_lightning as pl; do not mix class namespaces within one application. GPU, TPU, multi-node and online logger examples are source-verified illustrations, not executed training validation.
Installation
uv pip install "lightning==2.6.6"Optional extras:
uv pip install "lightning[extra]==2.6.6" # loggers, strategies, etc.
uv pip install wandb mlflow # specific loggers as neededWhen to Use This Skill
This skill should be used when:
- Building, training, or deploying neural networks using PyTorch Lightning
- Organizing PyTorch code into LightningModules
- Configuring Trainers for multi-GPU/TPU training
- Implementing data pipelines with LightningDataModules
- Working with callbacks, logging, and distributed training strategies (DDP, FSDP, DeepSpeed)
- Structuring deep learning projects professionally
Core Capabilities
1. LightningModule - Model Definition
Organize PyTorch models into six logical sections:
1. Initialization - __init__() and setup()
2. Training Loop - training_step(batch, batch_idx)
3. Validation Loop - validation_step(batch, batch_idx)
4. Test Loop - test_step(batch, batch_idx)
5. Prediction - predict_step(batch, batch_idx)
6. Optimizer Configuration - configure_optimizers()
Quick template reference: See scripts/template_lightning_module.py for a complete boilerplate.
Detailed documentation: Read references/lightning_module.md for comprehensive method documentation, hooks, properties, and best practices.
2. Trainer - Training Automation
The Trainer automates the training loop, device management, gradient operations, and callbacks. Key features:
- Multi-GPU/TPU support with strategy selection (DDP, FSDP, DeepSpeed)
- Automatic mixed precision training
- Gradient accumulation and clipping
- Checkpointing and early stopping
- Progress bars and logging
Quick setup reference: See scripts/quick_trainer_setup.py for common Trainer configurations.
Detailed documentation: Read references/trainer.md for all parameters, methods, and configuration options.
3. LightningDataModule - Data Pipeline Organization
Encapsulate all data processing steps in a reusable class:
1. prepare_data() - Download/cache on local rank zero per node by default; use global rank zero with shared storage when configured
2. setup() - Create datasets and apply transforms (per-GPU)
3. train_dataloader() - Return training DataLoader
4. val_dataloader() - Return validation DataLoader
5. test_dataloader() - Return test DataLoader
Quick template reference: See scripts/template_datamodule.py for a complete boilerplate.
Detailed documentation: Read references/data_module.md for method details and usage patterns.
4. Callbacks - Extensible Training Logic
Add custom functionality at specific training hooks without modifying your LightningModule. Built-in callbacks include:
- ModelCheckpoint - Save best/latest models
- EarlyStopping - Stop when metrics plateau
- LearningRateMonitor - Track LR scheduler changes
- BatchSizeFinder - Auto-determine optimal batch size
Detailed documentation: Read references/callbacks.md for built-in callbacks and custom callback creation.
5. Logging - Experiment Tracking
Integrate with multiple logging platforms:
- CSV locally; TensorBoard is the default only when its optional package is installed
- Weights & Biases (WandbLogger)
- MLflow (MLFlowLogger)
- Comet (CometLogger)
- CSV (CSVLogger)
NeptuneLogger was removed in Lightning 2.6.4; the Neptune service shut down March 5, 2026. The bundled helpers use local CSV logging; cloud tracking and model artifact uploads require an intentional choice.
Log metrics with self.log("metric_name", value) in supported LightningModule hooks; specify epoch/step reduction and batch size when they matter.
Detailed documentation: Read references/logging.md for logger setup and configuration.
6. Distributed Training - Scale to Multiple Devices
Choose the right strategy based on model size:
- DDP - When each device can hold the full model, optimizer state and activations
- FSDP - When sharding training state addresses measured memory pressure
- DeepSpeed - When its ZeRO/offload features fit the measured workload and CUDA stack
Choose from peak memory and throughput measurements, not a fixed parameter-count threshold.
Configure with: Trainer(strategy="ddp", accelerator="gpu", devices=4)
Detailed documentation: Read references/distributed_training.md for strategy comparison and configuration.
7. Best Practices
- Device agnostic code - Use
self.deviceinstead of.cuda() - Hyperparameter saving - Use
self.save_hyperparameters()in__init__() - Metric logging - For scalar tensors, cross-device reduction requires
self.log(..., sync_dist=True); the default isFalse. Have all ranks participate in synchronized calls. For non-additive metrics such as AUROC, use a stateful TorchMetrics object with its own distributed synchronization instead of averaging per-rank AUROCs. See the [Lightning logging contract](https://lightning.ai/docs/pytorch/stable/extensions/logging.html) and [TorchMetrics integration](https://lightning.ai/docs/torchmetrics/stable/pages/lightning.html). - Reproducibility - Use
seed_everything()andTrainer(deterministic=True) - Debugging - Use
Trainer(fast_dev_run=True)to test with 1 batch
Detailed documentation: Read references/best_practices.md for common patterns and pitfalls.
Quick Workflow
Copy the three scripts into a working directory, then run this CPU smoke there:
import lightning as L
from template_datamodule import TemplateDataModule
from template_lightning_module import TemplateLightningModule
from quick_trainer_setup import production_single_gpu_trainer
L.seed_everything(42, workers=True)
model = TemplateLightningModule(hidden_dim=16)
dm = TemplateDataModule(num_samples=64, batch_size=8, num_workers=0)
trainer = production_single_gpu_trainer(
max_epochs=1, accelerator="cpu", precision="32-true",
log_dir="logs", checkpoint_dir="checkpoints",
)
trainer.fit(model, datamodule=dm)
trainer.test(model, datamodule=dm, ckpt_path="best", weights_only=True)The synthetic vectors match the model input and use separate validation
transforms and held-out test generation. Random labels test mechanics only.
All helpers monitor val/loss. Use a fresh model/Trainer plus
fit(..., ckpt_path=path, weights_only=True) to resume full training state;
load_from_checkpoint alone loads a model. A normal short fit checks callbacks
and checkpoints; fast_dev_run disables much of that behavior.
For real science, split independent subjects/groups/time periods before fitting
preprocessing, preserve split IDs, and reserve test data from model selection.
Final DDP evaluation can duplicate padded samples: use one device for exact
sample coverage or validate a nonduplicating distributed evaluator.
Resources
scripts/
Executable Python templates for common PyTorch Lightning patterns:
template_lightning_module.py- Complete LightningModule boilerplatetemplate_datamodule.py- Complete LightningDataModule boilerplatequick_trainer_setup.py- Common Trainer configuration examples
references/
Detailed documentation for each PyTorch Lightning component:
lightning_module.md- Comprehensive LightningModule guide (methods, hooks, properties)trainer.md- Trainer configuration and parametersdata_module.md- LightningDataModule patterns and methodscallbacks.md- Built-in and custom callbackslogging.md- Logger integrations and usagedistributed_training.md- DDP, FSDP, DeepSpeed comparison and setupbest_practices.md- Common patterns, tips, and pitfalls
Citing Scientific Agent Skills
This skill is part of Scientific Agent Skills by K-Dense. If it materially contributed to a
manuscript, report, presentation, or code release, add the paper to the references or
software section and tell the user you did so:
> Kassis, T., Agarwal, V., He, Y., Patel, D., & Brueckner, A. M. (2026). Scientific Agent
> Skills: A Library of Procedural Knowledge for Research Agents. arXiv:2609.00065.
> https://doi.org/10.48550/ar
… (nội dung đầy đủ có trong gói tải về)
Skill liên quan
- competitor-analysis – Nghiên cứu đối thủ với khám phá qua Browserbase, các luồng làm giàu dữ liệu, ảnh chụp…
- competitor-profiling – Khi người dùng muốn nghiên cứu, lập hồ sơ hoặc phân tích đối thủ từ các URL của họ.
- litreview – Tìm kiếm tài liệu học thuật qua các API PubMed và OpenAlex miễn phí, không cần khóa; tùy…
- constant-time-analysis – Phân tích mã mật mã để phát hiện các thao tác làm rò rỉ dữ liệu bí mật qua sự khác biệt…
- llm-wiki – Dùng khi xây dựng hoặc duy trì một cơ sở tri thức cá nhân bền vững (second brain) trong…
- pytorch-fsdp – Huấn luyện dữ liệu song song với phân mảnh đầy đủ (fully sharded data-parallel) cho mô…
- pytorch-lightning – Vòng lặp huấn luyện gọn gàng với hỗ trợ phân tán tích hợp sẵn.
- context7-auto-research – Tự động lấy tài liệu thư viện/framework mới nhất cho Claude Code qua API Context7. Dùng…