Guidance for AI assistants working in this repo. For full contributor etiquette (issues, PR process, forking), see CONTRIBUTING.md; for maintainer-only tasks, see MAINTAINING.md.
lightly (lightly-ssl) is a self-supervised learning (SSL) library for
computer vision, built on PyTorch / PyTorch Lightning. It implements 19+ SSL
methods (SimCLR, MoCo, BYOL, DINO, DINOv2, SwaV, VICReg, Barlow Twins, FroSSL,
DetConS, LeJEPA, etc.).
Main entry points:
lightly.models— backbones + method-specific headslightly.loss— SSL loss implementationslightly.transforms— method-specific augmentation pipelineslightly.data— dataset wrapper + collate functionslightly.cli—lightly-*command-line tools (train, embed, crop, magic)lightly.core— one-liner convenience APIs
lightly/— the package (flat layout, nosrc/)tests/— mirrorslightly/'s structureexamples/— training examples in three variants:pytorch/,pytorch_lightning/,pytorch_lightning_distributed/, plus generatednotebooks/docs/source/— Sphinx docs, including one.rstpage per SSL method underdocs/source/examples/benchmarks/— benchmark scriptsCONTRIBUTING.md,MAINTAINING.md,Makefile,pyproject.toml
Package manager is uv.
make install-devThis synchronizes the project environment from the lockfile and installs the pre-commit hooks. Commands should be run through the Makefile or uv run --frozen; manual virtual environment activation is not required.
| Command | Purpose |
|---|---|
make format |
Auto-fix imports/formatting/lint with ruff |
make format-check |
Check formatting and lint without fixing |
make type-check |
mypy on lightly and tests |
make lock |
Regenerate uv.lock after changing dependencies |
make lock-check |
Fail if uv.lock is out of date with pyproject.toml |
make static-checks |
lock-check + format-check + type-check |
make test |
pytest tests --runslow (full suite) |
make test-fast |
pytest tests (skips @pytest.mark.slow) |
make all-checks |
static-checks + test |
make generate-example-notebooks |
Regenerate examples/notebooks/* from examples/{pytorch,pytorch_lightning,pytorch_lightning_distributed} |
make help |
List the common targets |
The install-* and test-* targets not listed above exist for CI: each workflow job
runs exactly one make install-<scenario> followed by one make test-<scenario>.
Every install target is a single uv command; never layer extra packages on top of an
installed environment.
If make format reports changes, re-run it before make all-checks.
Development commands should be executed through the Makefile or uv run --frozen
to ensure they use the locked dependency versions.
All dependencies live in pyproject.toml:
[project] dependencies— required at runtime for every user.[project.optional-dependencies]— optional user-facing features (lightly[timm],lightly[video], ...). These ship with the package.[dependency-groups]— development-only, never distributed.devis installed by default;docs,distandminimalare opt-in via--group.
After changing dependencies run make lock and commit uv.lock; make static-checks
fails when the lockfile is stale.
- Google + PyTorch styleguide. Docstrings use triple double quotes and the Google convention (checked by ruff's pydocstyle rules); required on public functions unless very short and obvious.
- Full type hints everywhere (mypy-clean). Use Python 3.10-style unions (
str | Path, notUnion[str, Path]); this requiresfrom __future__ import annotationsat the top of the module (the package declaresrequires-python = ">=3.8"and CI tests that lowest supported version). - Prefer keyword arguments when calling functions with more than one argument.
- Import functions via their module (
from module import submodule; submodule.fn(...)); import classes directly (from module.submodule import MyClass).
Touch points, mirroring an existing method (e.g. BYOL) as the template:
lightly/models/<name>.py,lightly/loss/<name>_loss.py,lightly/transforms/<name>_transform.pyas needed- Export the new symbol from the relevant subpackage
__init__.py - Add
docs/source/examples/<name>.rstand wire it into its parent toctree - Add example scripts under all three
examples/variants, then runmake generate-example-notebooksand commit the regenerated notebooks (they're tracked in git) - Add tests mirrored under
tests/{models,loss,transforms}/, following existing naming (e.g.test_ModelsBYOL.py,test_barlow_twins_loss.py,test_byol_transform.py)
pytest, config intests/conftest.py. Test tree mirrorslightly/.- Slow tests are marked
@pytest.mark.slowand skipped unless--runslowis passed (make testpasses it,make test-fastdoesn't).
test_code_format.yml—make static-checkstest.yml— main unit test suitetest_minimal_deps.yml— install with lowest-pinned direct dependencies, then testtest_setup.yml— package build/install sanity checkcheck_example_nbs.yml— verifies generated notebooks are up to date withexamples/weekly_dependency_test.yml— scheduled test against latest dependency versionsrelease_pypi.yml— publish to PyPI
- Branch off
upstream/master; never commit directly tomaster. - Before committing:
make format, thenmake all-checks(or at leaststatic-checks+ targeted tests for faster iterations). - If you touched
examples/, regenerate notebooks (make generate-example-notebooks) and commit them. - If you touched
docs/source, verify the docs still build (seedocs/README.md).