Skip to content

test: expand test suite with fixtures, utils, integration, and model forward-pass tests - #183

Open
atong01 wants to merge 1 commit into
mainfrom
add-tests
Open

test: expand test suite with fixtures, utils, integration, and model forward-pass tests#183
atong01 wants to merge 1 commit into
mainfrom
add-tests

Conversation

@atong01

@atong01 atong01 commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Expands the test suite with shared fixtures, utility tests, an integration smoke test, and model forward-pass tests.

Changes

  • tests/conftest.py (new): shared fixtures — shapes (parametrized over [(2,3),(5,2),(10,1),(3,3,2)]), rng (torch.Generator seed 42), sample_data (x0/x1 standard normal), and one fixture per matcher class
  • tests/test_utils.py (new): Arrange-Act-Assert tests for eight_normal_sample, sample_moons, sample_8gaussians, and torch_wrapper. plot_trajectories is @pytest.mark.skip (needs matplotlib display)
  • tests/test_integration.py (new): @pytest.mark.slow end-to-end CFM training smoke test — 256 moons samples, MLP(dim=2, w=32, time_varying=True), ConditionalFlowMatcher(sigma=0.0), 100 Adam steps at lr=1e-2; asserts final_loss < initial_loss
  • **tests/test_models.py(expanded 13 → 78 lines): added forward-pass tests forMLP(time-varying / non-time-varying / out_dim) andGradModel` (instantiation, forward, gradient correctness via autograd comparison)
  • Fix typo classersclasses in test_conditional_flow_matcher.py and test_optimal_transport.py docstrings

Motivation

Previously tests/ had only 4 files, no conftest.py, no integration tests, no tests for utils.py, and test_models.py only instantiated models without testing the forward pass. This fills those gaps.

Validation

All new tests pass against a merged branch containing all 4 PRs in this batch:

pytest -v --ignore=examples --ignore=runner
172 passed, 1 skipped, 0 failed

Coverage increased from 30% → 47% total. Core modules at 100%.

Notes

  • torchcfm/utils.py function signatures differed from initial expectations (eight_normal_sample(n, dim, scale=1, var=1) requires dim; sample_moons(n) returns numpy; torch_wrapper is a class). Tests were written against the actual source.
  • The --doctest-modules flag in pyproject.toml is a harmless no-op (no >>> doctests exist in torchcfm/).

Summary by Sourcery

Expand the test suite with shared fixtures, utility function coverage, model forward-pass checks, and an end-to-end CFM training smoke test.

Tests:

  • Add shared pytest fixtures for common shapes, RNG, sample data, and conditional flow matcher variants in tests/conftest.py.
  • Introduce tests for sampling utilities and the torch_wrapper helper, including shape, type, and finiteness checks plus a skipped plotting test.
  • Add forward-pass and gradient correctness tests for MLP and GradModel to ensure outputs have expected shapes and match autograd gradients.
  • Add a slow integration test that trains a small CFM model on two-moons data and verifies the training loss decreases.
  • Fix minor docstring typos in conditional flow matcher and optimal transport test modules.

…forward-pass tests

- Add tests/conftest.py with shared fixtures: shapes, rng, sample_data, and one fixture per CFM matcher class
- Add tests/test_utils.py covering eight_normal_sample, sample_moons, sample_8gaussians, and torch_wrapper
- Add tests/test_integration.py with a @pytest.mark.slow end-to-end CFM training smoke test (asserts loss decreases)
- Expand tests/test_models.py with forward-pass tests for MLP (time-varying/non-time-varying/out_dim) and GradModel (instantiation, forward, gradient correctness)
- Fix typo 'classers' -> 'classes' in test_conditional_flow_matcher.py and test_optimal_transport.py docstrings
@sourcery-ai

sourcery-ai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Expands the TorchCFM test suite with shared fixtures, utility function coverage, model forward-pass tests, and an end-to-end CFM training smoke test, plus minor docstring typo fixes.

File-Level Changes

Change Details Files
Add shared pytest fixtures for common shapes, RNG, sample data, and conditional flow matcher instances.
  • Introduce SHAPES constant and parametrized shapes fixture covering several batch/feature combinations.
  • Add seeded torch.Generator fixture and reusable (x0, x1) sample_data fixture for 2D standard normal batches.
  • Provide one fixture per ConditionalFlowMatcher variant to simplify and standardize matcher setup across tests.
tests/conftest.py
Add tests for utility sampling and wrapper functions in torchcfm.utils.
  • Test eight_normal_sample across multiple batch sizes/dims and edge parameters, checking tensor type, shape, and finiteness.
  • Test sample_moons and sample_8gaussians for correct shapes, dtypes, and finite values, including single-sample edge cases.
  • Test torch_wrapper returns a torch.nn.Module wrapper with correct call signature, output shape, and underlying model reference.
  • Add a skipped test for plot_trajectories to document its interactive backend requirement.
tests/test_utils.py
Add an end-to-end slow integration test to verify CFM training reduces loss on a two-moons task.
  • Seed NumPy and PyTorch RNGs for deterministic behavior in the integration test.
  • Construct two-moons source/target datasets via sample_moons and a small time-varying MLP model with Adam optimizer.
  • Use ConditionalFlowMatcher to sample training pairs and measure MSE loss on a fixed evaluation batch before and after 100 training steps, asserting final loss decreases.
tests/test_integration.py
Expand model tests to cover MLP forward behavior and GradModel gradient semantics.
  • Import torch and GradModel, and add a module docstring for the models test file.
  • Add tests for time-varying vs non-time-varying MLP inputs and for explicit out_dim handling, asserting shapes and finiteness.
  • Add tests that GradModel wraps an action network, returns gradients with expected truncated shape, and matches autograd-computed gradients (excluding last dimension).
tests/test_models.py
Fix minor docstring typos in existing tests.
  • Update conditional flow matcher test docstrings to say "classes" instead of "classers" in two files.
tests/test_conditional_flow_matcher.py
tests/test_optimal_transport.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In tests/conftest.py, consider removing the from torchcfm.conditional_flow_matching import * star import and explicitly importing only the matcher classes that are actually used to keep the test namespace minimal and avoid accidental name clashes.
  • The gradient correctness test in test_grad_model_is_gradient uses create_graph=True even though the resulting graph is not reused; you can drop this flag to reduce autograd overhead while keeping the test behavior identical.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `tests/conftest.py`, consider removing the `from torchcfm.conditional_flow_matching import *` star import and explicitly importing only the matcher classes that are actually used to keep the test namespace minimal and avoid accidental name clashes.
- The gradient correctness test in `test_grad_model_is_gradient` uses `create_graph=True` even though the resulting graph is not reused; you can drop this flag to reduce autograd overhead while keeping the test behavior identical.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant