NVFLARE Convert PyTorch Lightning
Use When
Use only when the user asks to convert PyTorch Lightning code into an NVFLARE federated training job; require both federation intent and Lightning ownership. Treat requests for multiple sites or institutions to train collaboratively while each site's data remains local as federation intent, even when the request does not say "federated" or "NVFLARE."
Lightning source evidence alone is not sufficient. Relevant source may contain a LightningModule, LightningDataModule, a Trainer fit/validate/test loop,
Lightning callbacks, checkpointing, or loggers.
Supported: the PyTorch recipe family with flare.patch(trainer) as the model
exchange integration, Lightning-native evaluation, custom aggregation through
the same recipe aggregator= hook, and local validation and export.
Standard Path
Always read this converter SKILL.md with
../nvflare-shared/references/conversion-common.md. For an explicit-FedAvg
conversion, load only these references, in workflow order:
- During inspection,
references/lightning-detection.md.
- For generated splits, relative paths, or per-site data locations,
../nvflare-shared/references/site-data-and-paths.md.
- After
nvflare recipe show fedavg-pt --format json,
../nvflare-shared/references/pytorch-family-recipe-construction.md.
- During conversion,
references/lightning-conversion.md, then
../nvflare-shared/references/pytorch-model-exchange.md.
- Only after generated files exist,
../nvflare-shared/references/validation-evidence.md, then
references/lightning-validation.md.
Complete each workflow phase before loading the next phase's reference. Do not
enumerate reference directories or preload validation, DDP/tracking, broad
workflow, dependency, runtime-output, or reporting references. Do not depend on
NVFLARE repository examples.
Do Not Use When
Do not use for non-federated Lightning changes such as DDP-only configuration, profiling, inference serving, callbacks, early stopping, or schedulers; or for plain torch.nn.Module manual training loops without Lightning
(route to nvflare-convert-pytorch), Hugging Face Trainer (route to nvflare-convert-huggingface), TensorFlow,
XGBoost, scikit-learn, a failed job (route to nvflare-diagnose-job),
federated statistics without training (route to nvflare-fed-stats), or
generic Lightning debugging without FLARE intent; when the inspected project
actively contains both Lightning and Hugging Face Trainer entrypoints, route to
nvflare-orient. Out of conversion scope: production deployment, Kubernetes,
POC lifecycle, deployment privacy/security policy design, custom distributed
launch policies not expressible by product APIs, experiment tracking redesign,
and experiment search across recipes. Privacy-protection requests — homomorphic encryption (HE) /
encrypted aggregation, differential privacy, and privacy filters — are not
supported: they require provisioning or deployment policy beyond conversion
scope, so report such a request as unsupported and route it to
provisioning/deployment, never substituting an unprotected recipe or disclaimer.
If a request combines federated statistics and model-training conversion,
treat it as two independent jobs and workflows: do not merge or automatically
chain them, do not route the combination to nvflare-orient, and ask which
workflow to run first before generating or running either job. Recommend
nvflare-fed-stats first only when the user's purpose is to understand data
distribution; handle conversion later as a separate request.
Workflow
- Apply
../nvflare-shared/references/conversion-common.md for the whole
conversion; this SKILL.md states only the framework-specific deltas.
- Inspect before editing with
nvflare agent inspect source <path> --format json
plus direct reading; fact extraction is static. Confirm Lightning versus plain
PyTorch and hand off to nvflare-convert-pytorch when no Lightning evidence
exists. If inspection recommends nvflare-orient for active Lightning and
Hugging Face Trainer owners, stop and hand off before editing.
- Apply the dependency-install ordering rule in
../nvflare-shared/references/conversion-common.md before any Python command
imports user, Lightning, NVFLARE, or declared dependency modules. Determine
applicable dependencies from the selected execution path first. If required
data artifacts already exist and static inspection shows that the selected
path will not reach a download helper or its imports, treat its download-only
requirements as inapplicable: do not install or import-probe them. Probe only
modules the generated conversion and selected validation path will execute,
and keep an optional probe separate and exit-zero when unavailable.
- Identify the existing
LightningModule, LightningDataModule, trainer
construction, callbacks, checkpointing, validation_step/test_step and
dataloaders, metrics, logger usage, source partition evidence, distributed
process-spawning evidence, custom aggregation intent, and the concrete model
constructor values that server and clients must share.
- Reuse the PyTorch recipe family; Lightning is not a separate recipe family.
For the standard case — the user explicitly requests FedAvg and inspection
identifies Lightning — run
nvflare recipe show fedavg-pt --format json
directly and construct it. For fedavg-pt, import FedAvgRecipe only from
nvflare.app_opt.pt.recipes.fedavg, never from nvflare.recipe. Use FedEval
for evaluation-only. After every recipe show, derive construction capabilities
from the construction reference. Then use that documented path: do not run
exploratory NVFLARE imports or use inspect, hasattr, constant discovery,
SDK source/docstring reads, or lifecycle probes. If a required detail is absent,
report a skill gap or fail closed instead of guessing. Call recipe.execute(SimEnv(...)).
- Convert the training entry point to the Lightning Client API: build the
Trainer, call flare.patch(trainer), and let the patched trainer own
model exchange. Keep evaluation inside Lightning per
references/lightning-conversion.md and use self.log. Derive
evaluate_only=True only for FedEval; omit it for training recipes so its
default stays False. Derive evaluate_before_train = recipe_algorithm != "cyclic": Cyclic persists only its final sequential model; every other
algorithm uses explicit validation for server metrics and, for training,
best-model selection. Verify the key in server evidence or fail closed.
- Add or update
job.py under the shared constructor-serialization rule. Use
the recipe's class_path or path key plus complete args when values are
needed; a permitted zero-argument instance is the complete module. Add
requested aggregator= wiring and the metric, tensor-transport, server
offload, and execution settings derived from the shared PyTorch-family
construction profile. If sites need distinct train_args, make every site
override the complete argument string; never split shared arguments and a
site-specific data path across recipe-level and per-site values expecting a
merge.
- Immediately after generated files exist and before any preflight, smoke test,
cleanup, validation, or execution command, load the two validation references
in Standard Path order. Before executing a full run, select and record exactly
one final validation target:
- for a requested local or first-run simulation without an export claim, run
python job.py and do not export or run the exported simulator afterward;
- for a requested exported/deployable artifact, export first and run only the
exported folder with the simulator CLI; do not first run
python job.py.
If the selected full-run target fails, diagnose it, apply a scoped fix, and
rerun that same target. Change targets only when evidence shows the original
target does not represent the requested artifact, and record that reason.
Export inspection belongs only to the exported path. Keep cleanup, export,
and simulation as separate tool calls; never combine recursive cleanup with
execution. Stop at the first failed validation rung before diagnosing it;
do not add speculative recovery probes. Use
the environment and permission mechanisms supplied by the agent host; do not
inspect or enforce its security boundary.
- Report the recipe, changed files, selected validation target, validation
status, metrics, and exact artifact paths.
Non-standard Cases
Load only the reference matching an encountered case:
../nvflare-shared/references/conversion-workflow.md for an unresolved
non-standard rerun, authorization, or missing-semantics case; it no longer
holds the data-location or partitioning contracts.
../nvflare-shared/references/pytorch-family-recipe-selection.md for an
ambiguous or non-FedAvg algorithm; use its catalog for FedAvg, FedOpt, FedProx,
SCAFFOLD, Cyclic, Swarm, or FedEval, and reserve nvflare recipe list for these cases.
../nvflare-shared/references/dependency-install.md when an applicable
dependency is missing.
../nvflare-shared/references/runtime-output-guidance.md for a read-only
source root or user-chosen output destination.
references/lightning-ddp-and-tracking.md when inspection finds its trigger.
../nvflare-shared/references/metrics-and-artifact-reporting.md when normal
metric artifacts are absent or inconsistent.
Requirements
- Must integrate through
flare.patch(trainer) and let the patched trainer own
model exchange. Must not generate a manual FLModel send/receive path as the
default Lightning exchange, and must not pass the received input_model into
the Trainer.
- Must treat
flare.receive() inside the patched loop as optional metadata or
task-progression access only, not as a second model-load path.
- Must keep evaluation inside Lightning (
trainer.validate/trainer.test,
validation_step, self.log); must not generate a raw PyTorch
model.eval() loop for ordinary Lightning conversion.
- Except for Cyclic, must run an explicit standalone
trainer.validate(...)
before trainer.fit(...) and rely on the patched callback to attach its finite
scalar metrics; never populate model.__fl_meta__[MetaKey.INITIAL_METRICS].
Validation inside trainer.fit(...) is not a received-global-model metric. Cyclic must
skip the pre-fit call and report its persisted final model, not a best model.
- Must audit model constructor arguments before writing
job.py by reading the
LightningModule.__init__ signature and the selected recipe's model
parameter from nvflare recipe show <recipe-name> --format json, not by
reading NVFLARE library source. Emit the recipe-documented class_path or
path key plus complete args for every required or overridden value. Direct
LightningModule use is allowed only when unchanged zero-argument defaults reconstruct it. Values
must be clear from source, configuration, or supplied metadata. Otherwise ask
one semantic question when an answer channel exists or fail closed.
- Must use the PyTorch recipe family; must not invent a Lightning-only recipe.
Apply the construction reference after
recipe show; it is canonical for
optional recipe parameters, model selection, tensor transport, server disk
offload, and execution mode.
- Must preserve local-only callbacks and logger behavior where safe. Existing
network-connected tracking, upload callbacks, and custom/unknown loggers are
evidence, not a user request: keep them disabled during validation unless
explicitly requested, and do not ask solely to enable them. This narrows
references/lightning-conversion.md.
- Must not make non-PyTorch-family skills load
../nvflare-shared/references/pytorch-model-exchange.md.
- Site partitioning, custom aggregation, the Source Of Truth Boundary, and user
input/authorization follow
../nvflare-shared/references/conversion-common.md.
1---2name: nvflare-convert-lightning3description: Convert existing PyTorch Lightning training code into an NVFLARE federated job using the Lightning Client API patch, local validation, and job export; use only when the request names federated/NVFLARE conversion or asks multiple sites to train collaboratively while keeping each site's data local, and either names PyTorch Lightning or preliminary source inspection identifies one Lightning owner; do not use for non-federated Lightning work such as DDP, profiling, inference serving, or training-loop changes, nor for plain PyTorch, TensorFlow/Keras, other frameworks, deployment, POC/production lifecycle, or experiment workflows.4license: Apache-2.05---67# NVFLARE Convert PyTorch Lightning89## Use When1011Use only when the user asks to convert PyTorch Lightning code into an NVFLARE federated training job; require both federation intent and Lightning ownership. Treat requests for multiple sites or institutions to train collaboratively while each site's data remains local as federation intent, even when the request does not say "federated" or "NVFLARE."12Lightning source evidence alone is not sufficient. Relevant source may contain a `LightningModule`, `LightningDataModule`, a `Trainer` fit/validate/test loop,13Lightning callbacks, checkpointing, or loggers.14Supported: the PyTorch recipe family with `flare.patch(trainer)` as the model15exchange integration, Lightning-native evaluation, custom aggregation through16the same recipe `aggregator=` hook, and local validation and export.1718## Standard Path1920Always read this converter SKILL.md with21`../nvflare-shared/references/conversion-common.md`. For an explicit-FedAvg22conversion, load only these references, in workflow order:23241. During inspection, `references/lightning-detection.md`.252. For generated splits, relative paths, or per-site data locations,26 `../nvflare-shared/references/site-data-and-paths.md`.273. After `nvflare recipe show fedavg-pt --format json`,28 `../nvflare-shared/references/pytorch-family-recipe-construction.md`.294. During conversion, `references/lightning-conversion.md`, then30 `../nvflare-shared/references/pytorch-model-exchange.md`.315. Only after generated files exist,32 `../nvflare-shared/references/validation-evidence.md`, then33 `references/lightning-validation.md`.3435Complete each workflow phase before loading the next phase's reference. Do not36enumerate reference directories or preload validation, DDP/tracking, broad37workflow, dependency, runtime-output, or reporting references. Do not depend on38NVFLARE repository examples.3940## Do Not Use When4142Do not use for non-federated Lightning changes such as DDP-only configuration, profiling, inference serving, callbacks, early stopping, or schedulers; or for plain `torch.nn.Module` manual training loops without Lightning43(route to `nvflare-convert-pytorch`), Hugging Face Trainer (route to `nvflare-convert-huggingface`), TensorFlow,44XGBoost, scikit-learn, a failed job (route to `nvflare-diagnose-job`),45federated statistics without training (route to `nvflare-fed-stats`), or46generic Lightning debugging without FLARE intent; when the inspected project47actively contains both Lightning and Hugging Face Trainer entrypoints, route to48`nvflare-orient`. Out of conversion scope: production deployment, Kubernetes,49POC lifecycle, deployment privacy/security policy design, custom distributed50launch policies not expressible by product APIs, experiment tracking redesign,51and experiment search across recipes. Privacy-protection requests — homomorphic encryption (HE) /52encrypted aggregation, differential privacy, and privacy filters — are not53supported: they require provisioning or deployment policy beyond conversion54scope, so report such a request as unsupported and route it to55provisioning/deployment, never substituting an unprotected recipe or disclaimer.56If a request combines federated statistics and model-training conversion,57treat it as two independent jobs and workflows: do not merge or automatically58chain them, do not route the combination to `nvflare-orient`, and ask which59workflow to run first before generating or running either job. Recommend60`nvflare-fed-stats` first only when the user's purpose is to understand data61distribution; handle conversion later as a separate request.6263## Workflow64651. Apply `../nvflare-shared/references/conversion-common.md` for the whole66 conversion; this SKILL.md states only the framework-specific deltas.672. Inspect before editing with `nvflare agent inspect source <path> --format json`68 plus direct reading; fact extraction is static. Confirm Lightning versus plain69 PyTorch and hand off to `nvflare-convert-pytorch` when no Lightning evidence70 exists. If inspection recommends `nvflare-orient` for active Lightning and71 Hugging Face Trainer owners, stop and hand off before editing.723. Apply the dependency-install ordering rule in73 `../nvflare-shared/references/conversion-common.md` before any Python command74 imports user, Lightning, NVFLARE, or declared dependency modules. Determine75 applicable dependencies from the selected execution path first. If required76 data artifacts already exist and static inspection shows that the selected77 path will not reach a download helper or its imports, treat its download-only78 requirements as inapplicable: do not install or import-probe them. Probe only79 modules the generated conversion and selected validation path will execute,80 and keep an optional probe separate and exit-zero when unavailable.814. Identify the existing `LightningModule`, `LightningDataModule`, trainer82 construction, callbacks, checkpointing, `validation_step`/`test_step` and83 dataloaders, metrics, logger usage, source partition evidence, distributed84 process-spawning evidence, custom aggregation intent, and the concrete model85 constructor values that server and clients must share.865. Reuse the PyTorch recipe family; Lightning is not a separate recipe family.87 For the standard case — the user explicitly requests FedAvg and inspection88 identifies Lightning — run `nvflare recipe show fedavg-pt --format json`89 directly and construct it. For `fedavg-pt`, import `FedAvgRecipe` only from90 `nvflare.app_opt.pt.recipes.fedavg`, never from `nvflare.recipe`. Use FedEval91 for evaluation-only. After every `recipe show`, derive construction capabilities92 from the construction reference. Then use that documented path: do not run93 exploratory NVFLARE imports or use `inspect`, `hasattr`, constant discovery,94 SDK source/docstring reads, or lifecycle probes. If a required detail is absent,95 report a skill gap or fail closed instead of guessing. Call `recipe.execute(SimEnv(...))`.966. Convert the training entry point to the Lightning Client API: build the97 `Trainer`, call `flare.patch(trainer)`, and let the patched trainer own98 model exchange. Keep evaluation inside Lightning per99 `references/lightning-conversion.md` and use `self.log`. Derive100 `evaluate_only=True` only for FedEval; omit it for training recipes so its101 default stays `False`. Derive `evaluate_before_train = recipe_algorithm !=102 "cyclic"`: Cyclic persists only its final sequential model; every other103 algorithm uses explicit validation for server metrics and, for training,104 best-model selection. Verify the key in server evidence or fail closed.1057. Add or update `job.py` under the shared constructor-serialization rule. Use106 the recipe's `class_path` or `path` key plus complete `args` when values are107 needed; a permitted zero-argument instance is the complete module. Add108 requested `aggregator=` wiring and the metric, tensor-transport, server109 offload, and execution settings derived from the shared PyTorch-family110 construction profile. If sites need distinct `train_args`, make every site111 override the complete argument string; never split shared arguments and a112 site-specific data path across recipe-level and per-site values expecting a113 merge.1148. Immediately after generated files exist and before any preflight, smoke test,115 cleanup, validation, or execution command, load the two validation references116 in Standard Path order. Before executing a full run, select and record exactly117 one final validation target:118 - for a requested local or first-run simulation without an export claim, run119 `python job.py` and do not export or run the exported simulator afterward;120 - for a requested exported/deployable artifact, export first and run only the121 exported folder with the simulator CLI; do not first run `python job.py`.122 If the selected full-run target fails, diagnose it, apply a scoped fix, and123 rerun that same target. Change targets only when evidence shows the original124 target does not represent the requested artifact, and record that reason.125 Export inspection belongs only to the exported path. Keep cleanup, export,126 and simulation as separate tool calls; never combine recursive cleanup with127 execution. Stop at the first failed validation rung before diagnosing it;128 do not add speculative recovery probes. Use129 the environment and permission mechanisms supplied by the agent host; do not130 inspect or enforce its security boundary.1319. Report the recipe, changed files, selected validation target, validation132 status, metrics, and exact artifact paths.133134## Non-standard Cases135136Load only the reference matching an encountered case:137138- `../nvflare-shared/references/conversion-workflow.md` for an unresolved139 non-standard rerun, authorization, or missing-semantics case; it no longer140 holds the data-location or partitioning contracts.141- `../nvflare-shared/references/pytorch-family-recipe-selection.md` for an142 ambiguous or non-FedAvg algorithm; use its catalog for FedAvg, FedOpt, FedProx,143 SCAFFOLD, Cyclic, Swarm, or FedEval, and reserve `nvflare recipe list` for these cases.144- `../nvflare-shared/references/dependency-install.md` when an applicable145 dependency is missing.146- `../nvflare-shared/references/runtime-output-guidance.md` for a read-only147 source root or user-chosen output destination.148- `references/lightning-ddp-and-tracking.md` when inspection finds its trigger.149- `../nvflare-shared/references/metrics-and-artifact-reporting.md` when normal150 metric artifacts are absent or inconsistent.151152## Requirements153154- Must integrate through `flare.patch(trainer)` and let the patched trainer own155 model exchange. Must not generate a manual `FLModel` send/receive path as the156 default Lightning exchange, and must not pass the received `input_model` into157 the `Trainer`.158- Must treat `flare.receive()` inside the patched loop as optional metadata or159 task-progression access only, not as a second model-load path.160- Must keep evaluation inside Lightning (`trainer.validate`/`trainer.test`,161 `validation_step`, `self.log`); must not generate a raw PyTorch162 `model.eval()` loop for ordinary Lightning conversion.163- Except for Cyclic, must run an explicit standalone `trainer.validate(...)`164 before `trainer.fit(...)` and rely on the patched callback to attach its finite165 scalar metrics; never populate `model.__fl_meta__[MetaKey.INITIAL_METRICS]`.166 Validation inside `trainer.fit(...)` is not a received-global-model metric. Cyclic must167 skip the pre-fit call and report its persisted final model, not a best model.168- Must audit model constructor arguments before writing `job.py` by reading the169 `LightningModule.__init__` signature and the selected recipe's `model`170 parameter from `nvflare recipe show <recipe-name> --format json`, not by171 reading NVFLARE library source. Emit the recipe-documented `class_path` or172 `path` key plus complete `args` for every required or overridden value. Direct173 `LightningModule` use is allowed only when unchanged zero-argument defaults reconstruct it. Values174 must be clear from source, configuration, or supplied metadata. Otherwise ask175 one semantic question when an answer channel exists or fail closed.176- Must use the PyTorch recipe family; must not invent a Lightning-only recipe.177 Apply the construction reference after `recipe show`; it is canonical for178 optional recipe parameters, model selection, tensor transport, server disk179 offload, and execution mode.180- Must preserve local-only callbacks and logger behavior where safe. Existing181 network-connected tracking, upload callbacks, and custom/unknown loggers are182 evidence, not a user request: keep them disabled during validation unless183 explicitly requested, and do not ask solely to enable them. This narrows184 `references/lightning-conversion.md`.185- Must not make non-PyTorch-family skills load186 `../nvflare-shared/references/pytorch-model-exchange.md`.187- Site partitioning, custom aggregation, the Source Of Truth Boundary, and user188 input/authorization follow `../nvflare-shared/references/conversion-common.md`.189