feat(metrics): add Perplexity metric to ignite.metrics.nlp
Closes #3742
Summary
- Add a new
Perplexitymetric implementation inignite.metrics.nlp.perplexity. - Export
Perplexityfrom bothignite.metrics.nlpand top-levelignite.metrics. - Add dedicated tests for correctness, accumulation behavior, reset behavior, return type, and invalid inputs.
Test plan
- [x]
python -m pytest tests/ignite/metrics/nlp/test_perplexity.py -v - [x] Smoke test:
python -c "from ignite.metrics.nlp import Perplexity; import torch; ppl = Perplexity(); ppl.reset(); ppl.update((torch.randn(2,5,3), torch.randint(0,5,(2,3)))); print('PPL =', ppl.compute())"
Files changed
ignite/metrics/nlp/perplexity.pyignite/metrics/nlp/__init__.pyignite/metrics/__init__.pytests/ignite/metrics/nlp/test_perplexity.py
Nice addition! Perplexity is definitely a useful metric for language modeling and it fits well under ignite.metrics.nlp.
The test coverage looks solid — especially the token-weighted accumulation test, which ensures correctness across batches with different sequence lengths.
One small suggestion: it might be useful to add a GPU test to ensure the metric behaves correctly when tensors are on CUDA devices, since many language modeling workloads run on GPU.
Something like:
def test_gpu_support():
if not torch.cuda.is_available():
pytest.skip()
Overall the implementation and tests look clean and consistent with existing Ignite metrics.
Good point, thanks. I’d like to keep this PR scoped to the Perplexity implementation and core correctness tests. We can add a dedicated CUDA test if maintainers want explicit GPU coverage. @vfdev-5 thoughts?
@steaphenai thanks for the PR, I made a quick pass and left few comments.
The tests look shallow and there is no reference implementation that we test against. I suggest to check what we can use for reference implementation. In terms of testing on accelerators check other tests like test_accuracy.py to inspire from.
Thanks for the quick review, @vfdev-5. I addressed the two code comments in the latest push: detached tensors from the grad graph in Perplexity.update() removed test_returns_float I’ll also use existing metric tests (e.g., test_accuracy.py) as reference for accelerator-oriented test patterns as needed.
I added an explicit reference implementation check (_reference_perplexity) and validated both single-batch and multi-batch token-weighted accumulation against it and also I aligned test structure with existing Ignite metric patterns (as in test_accuracy.py): available_device parametrization, device assertions, and distributed-marked test layout.
Local run: python -m pytest tests/ignite/metrics/nlp/test_perplexity.py -m "not distributed" -v -> 10 passed, 12 skipped (CUDA/MPS skipped when unavailable).
@steaphenai code style check is failing: https://github.com/pytorch/ignite/actions/runs/24830927009/job/72725024776?pr=3743
@vfdev-5 Could you approve the workflows to run? The required checks are pending approval.
@steaphenai this failure is real: https://github.com/pytorch/ignite/actions/runs/24847148226/job/72879291595?pr=3743 Check other metrics how do they handle double dtype
@vfdev-5 I checked other metrics in ignite.metrics.nlp. I found that BLEU and ROUGE don't force dtype=torch.double on their accumulators. I've removed the explicit dtype=torch.double and dtype=torch.long from _sum_of_nll and _num_tokens in reset(), and removed dtype=torch.double from the .to() call in update() to match that pattern and fix MPS compatibility.
hi @steaphenai ! are you still working on this? if you are facing any issue, let us know!
Hi @TahaZahid05 ! Yes, I'm still working on this. This needs a workflow approval to get the required checks running. Thanks!
@steaphenai docs build failures are real. CI for unit tests are currently broken, are they passing locally for you?
@steaphenai thanks for the updates! All looks good, just apply the above suggestions and then we can merge.
@steaphenai code style checks are failing. Kindly follow steps given in CONTRIBUTING.MD
@TahaZahid05 I've verified. The tests are passing locally. Let's approve the CI workflows to run.