Skip to content

Fix tests and code before CI implementation #58

Description

@emapuljak

Today tests/run_tests.sh is the only place that knows which test module runs under which
backend, so a plain pytest collects torch-only modules under the keras backend and fails at import
(cannot import name 'PQBatchNorm2d' from 'pquant.core.keras.layers'). CI's matrix runs plain
pytest per backend, so this must be fixed in the test files themselves.

Implementation note: gating must use pytest.skip(..., allow_module_level=True) placed
above the backend-specific imports (or pytest.importorskip). A pytest.mark.skipif or a
custom marker will not work — markers are evaluated after the module is imported, and the
failure here happens at import time. (This is also why no markers = [...] entry is needed in
[tool.pytest.ini_options].)

  • Backend-gate each backend-specific test module so it skips at collection instead of
    erroring. Guard before the backend-specific imports, e.g.:
    python import keras, pytest if keras.backend.backend() != "torch": pytest.skip("torch backend only", allow_module_level=True)
    Apply to the torch-only modules (test_torch_compression_layers.py,
    test_torch_onnx_converter.py, test_torch_alkaid_conversion.py, test_hgq_torch.py) and
    symmetrically to the keras-only ones (test_keras_*, test_hgq_keras.py). Leave
    test_ap.py / test_pdp.py / test_wanda.py ungated — they run under both.
  • Remove the ineffective os.environ["KERAS_BACKEND"] = "torch" at the top of
    test_torch_compression_layers.py — it's a no-op once keras is already imported by
    conftest.py, and it masks the real gating problem.
  • Skip optional-dependency tests when the dep is absent: add pytest.importorskip("onnx")
    to the onnx converter tests and pytest.importorskip("alkaid") to the alkaid conversion
    tests. (alkaid is the plugin host — it must not become a pquant dependency; onnx is
    opt-in via the [onnx] extra.)
  • Preserve channels_last coverage: run_tests.sh runs test_keras_compression_layers.py
    a second time with DATA_FORMAT=channels_last. Keep this axis alive by parametrizing the
    set_image_data_format fixture in conftest.py over channels_first/channels_last (or by
    adding a dedicated CI job) — otherwise it silently stops being tested.
  • Delete tests/run_tests.sh once the above lands — KERAS_BACKEND=<backend> pytest then
    selects the right tests automatically, and the CI matrix supersedes the script.
  • Fix CUDA-forcing in the torch tests so they run on CPU-only machines. ~40 torch tests
    currently fail with AssertionError: Torch not compiled with CUDA enabled
    (torch/cuda/__init__.py:522) on any machine without a GPU. conftest.py already tries to
    fall back correctly (device = "cuda" if torch.cuda.is_available() else "cpu"), so something
    in the tests or library code is initialising CUDA regardless — find and fix it. This is a CI
    blocker:
    GitHub's ubuntu-latest runners have no GPU, so these tests will fail in the
    KERAS_BACKEND=torch matrix jobs exactly as they do locally on macOS. Verify with
    KERAS_BACKEND=torch pytest tests on a CPU-only machine — expect zero CUDA-related failures.
  • Fix hardcoded CUDA in add_compression_layers. src/pquant/core/torch/layers.py:641-644
    calls model.to("cuda") (and .to("cuda") on the warm-up tensor) unconditionally, with no
    torch.cuda.is_available() guard — so the function crashes with
    AssertionError: Torch not compiled with CUDA enabled on any CPU-only machine. This is the
    root cause of the ~40 failing torch tests, it makes the library unusable for CPU users, and it
    blocks the CI torch matrix (GitHub runners have no GPU). Fix by resolving the device once
    ("cuda" if torch.cuda.is_available() else "cpu") and using it for all three calls. Verify
    with KERAS_BACKEND=torch pytest tests on a CPU-only machine.

Activity

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions