Skip to main content

Flake8 plugin to prohibit calling .forward() on objects (use obj(inputs) instead so DDP backward hooks run)

Project description

flake8-no-module-forward-call

PyPI version Python versions License CI

A flake8 plugin that forbids calling .forward() directly on PyTorch nn.Module objects.

Why?

Calling model.forward(inputs) bypasses nn.Module.__call__, which means PyTorch hooks are silently skipped — including the backward hooks that DistributedDataParallel (DDP) relies on for gradient synchronization.

# Bad — DDP backward hooks won't run, gradients won't sync
loss = model.forward(inputs)

# Good — goes through __call__, all hooks fire correctly
loss = model(inputs)

This bug is easy to introduce, hard to notice (training may appear to work), and can silently corrupt distributed training runs.

Installation

pip install flake8-no-module-forward-call

For development / editable install:

git clone https://github.com/mrsndmn/flake8-no-module-forward-call.git
cd flake8-no-module-forward-call
pip install -e .

Usage

Run flake8 with the check enabled:

flake8 --extend-select=NMF001 .

Or add it to your flake8 config so it runs automatically:

.flake8 / setup.cfg:

[flake8]
extend-select = NMF001
max-line-length = 128

pyproject.toml:

[tool.flake8]
extend-select = ["NMF001"]
max-line-length = 128

Error codes

Code Message
NMF001 Do not call .forward() directly; use model(inputs) instead so DDP backward hooks run

Example

import torch.nn as nn

class MyModel(nn.Module):
    def forward(self, x):
        return x * 2

model = MyModel()

model.forward(x)   # NMF001 — flagged
model(x)           # OK

Running flake8:

example.py:10:1: NMF001 Do not call .forward() directly; use model(inputs) instead so DDP backward hooks run

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b my-feature
  3. Install dev dependencies: pip install -e ".[dev]"
  4. Run tests: pytest
  5. Open a pull request

License

Apache 2.0 — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_no_module_forward_call-0.1.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_no_module_forward_call-0.1.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file flake8_no_module_forward_call-0.1.1.tar.gz.

File metadata

File hashes

Hashes for flake8_no_module_forward_call-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0ff33d82baa81da033d9a5a07699c766e8ffdb4a17f8d1b8e48da4fff0fd1105
MD5 382803a32f73888fb3a29623488550b0
BLAKE2b-256 a7a9bd6de40b4c9f96c5473a52e9fd28fcf811fb085f5cc2fff027b8b5fb47cb

See more details on using hashes here.

File details

Details for the file flake8_no_module_forward_call-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_no_module_forward_call-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3eb7e1b6249b8cf178ef460c282699515b5d50e3c44c35a540ff1cd3be83e859
MD5 b5ce1df5b613ebf14bdc3d4d84bb25e4
BLAKE2b-256 f7f9b41cbcd6d6967242db14954d4e0c7e81dad97317e109391809d24c7697c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page