Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
-ra --durations=15 --junitxml=test-results/core.xml
--cov=src/cbfkit/controllers --cov=src/cbfkit/optimization
--cov=src/cbfkit/simulation --cov-report=xml
- uses: actions/upload-artifact@v5
- uses: actions/upload-artifact@v7
if: always()
with:
name: core-python-${{ matrix.python-version }}
Expand All @@ -95,7 +95,7 @@ jobs:
- run: >-
uv run --no-sync pytest -m slow tests --ignore=tests/test_mujoco
-ra --durations=15 --junitxml=test-results/examples.xml
- uses: actions/upload-artifact@v5
- uses: actions/upload-artifact@v7
if: always()
with:
name: examples
Expand Down Expand Up @@ -131,7 +131,7 @@ jobs:
- run: >-
uv run --no-sync pytest tests/test_mujoco
-ra --durations=15 --junitxml=test-results/mujoco.xml
- uses: actions/upload-artifact@v5
- uses: actions/upload-artifact@v7
if: always()
with:
name: mujoco
Expand Down Expand Up @@ -237,7 +237,9 @@ jobs:
- name: Verify wheel outside checkout
run: |
python -m venv "$RUNNER_TEMP/wheel-env"
"$RUNNER_TEMP/wheel-env/bin/pip" install dist/*.whl 'mujoco==3.11.0'
for wheel in dist/*.whl; do
"$RUNNER_TEMP/wheel-env/bin/pip" install "${wheel}[mujoco]"
done
cd "$RUNNER_TEMP"
"$RUNNER_TEMP/wheel-env/bin/python" "$GITHUB_WORKSPACE/scripts/check_installed_package.py" --mujoco
- name: Verify sdist outside checkout
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
uv run --no-sync pytest -ra --durations=15 --junitxml=test-results/g1.xml
tests/test_mujoco/test_g1.py tests/test_mujoco/test_unitree_policy.py
tests/test_mujoco/test_amo_policy.py tests/test_mujoco/test_groot_policy.py
- uses: actions/upload-artifact@v5
- uses: actions/upload-artifact@v7
if: always()
with:
name: g1-integration-results
Expand All @@ -77,7 +77,7 @@ jobs:
python -c 'import jax, torch; assert torch.cuda.is_available(); assert any(d.platform == "gpu" for d in jax.devices())'
python -m pip install --no-deps -e .
- run: python -m pytest -ra --junitxml=test-results/cuda.xml tests/test_wrappers/test_torch_safety_filter.py
- uses: actions/upload-artifact@v5
- uses: actions/upload-artifact@v7
if: always()
with:
name: cuda-integration-results
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ cvxopt = [
codegen = ["black>=23.12.1,<27.0"]
# MuJoCo/MJX plant backend. mujoco and mujoco-mjx MUST be pinned to the same
# version: mjx does not upper-bound mujoco and a skew crashes at import.
mujoco = ["mujoco==3.11.0", "mujoco-mjx==3.11.0"]
mujoco = ["mujoco==3.13.0", "mujoco-mjx==3.13.0"]

# --- Convenience umbrella extras ---
solvers = ["cbfkit[casadi]", "cbfkit[cvxopt]"]
Expand All @@ -95,12 +95,12 @@ all = [
test = ["pytest>=8.0.2", "pytest-cov", "python-dotenv>=1.2.1"]
test-all = ["cbfkit[test]", "cbfkit[all]"]
quality = [
"mypy==1.18.2",
"ruff==0.14.6",
"mypy==2.3.1",
"ruff==0.16.8",
"black==25.11.0",
"isort==7.0.0",
"pre-commit==4.3.0",
"types-PyYAML==6.0.12.20250915",
"types-PyYAML==6.0.12.20260906",
]
build = ["build>=1.0", "twine>=5.0"]
dev = [
Expand Down
29 changes: 13 additions & 16 deletions src/cbfkit/optimization/quadratic_program/qp_solver_cvxopt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Quadratic program solver using the CVXOPT library."""

import platform
from importlib import import_module
from types import ModuleType
from typing import Any, Dict, Tuple, Union

import jax.numpy as jnp
Expand All @@ -9,30 +11,25 @@

from cbfkit.utils.user_types.solvers import QpSolution

# Resolve cvxopt vs kvxopt once at import time (ARM vs x86)
_cvxopt_available = True
_cvxopt_error = None
# Resolve the optional, platform-specific backend once at import time. Keep its
# native matrix types behind the module boundary: cvxopt and kvxopt ship
# different stubs even though both accept NumPy arrays at runtime.
_cvxopt_backend: ModuleType | None = None
_cvxopt_error: ImportError | None = None
try:
_mach = platform.machine().lower()
if "arm" in _mach or "aarch" in _mach:
from kvxopt import matrix as _matrix # type: ignore[reportMissingImports]
from kvxopt import solvers as _solvers
else:
from cvxopt import matrix as _matrix
from cvxopt import solvers as _solvers
_cvxopt_backend = import_module("kvxopt" if "arm" in _mach or "aarch" in _mach else "cvxopt")
except ImportError as _e:
_cvxopt_available = False
_cvxopt_error = _e
_matrix = None # type: ignore[assignment]
_solvers = None # type: ignore[assignment]


def _ensure_cvxopt():
if not _cvxopt_available:
def _ensure_cvxopt() -> ModuleType:
if _cvxopt_backend is None:
raise ImportError(
"To use the cvxopt solver, please install the 'cvxopt' extra: "
"pip install 'cbfkit[cvxopt]'"
) from _cvxopt_error
return _cvxopt_backend


def solve(
Expand All @@ -57,8 +54,8 @@ def solve(
-------
(sol, success): Solution array and boolean success flag.
"""
_ensure_cvxopt()
matrix, solvers = _matrix, _solvers
backend = _ensure_cvxopt()
matrix, solvers = backend.matrix, backend.solvers

# Use the cvxopt library to solve the quadratic program
p_mat = matrix(np.array(p_mat, dtype=float))
Expand Down
31 changes: 31 additions & 0 deletions tests/test_optimization/test_qp_solver_cvxopt_optional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Missing platform-specific solvers should fail on use, not module import."""

import importlib
import platform
import runpy
from pathlib import Path

import jax.numpy as jnp
import pytest

from cbfkit.optimization.quadratic_program import qp_solver_cvxopt


@pytest.mark.parametrize(
"machine,package", [("arm64", "kvxopt"), ("aarch64", "kvxopt"), ("x86_64", "cvxopt")]
)
def test_missing_backend_reports_install_extra(monkeypatch, machine, package):
missing = ImportError(f"No module named {package}")

def unavailable(name):
assert name == package
raise missing

with monkeypatch.context() as patch:
patch.setattr(platform, "machine", lambda: machine)
patch.setattr(importlib, "import_module", unavailable)
namespace = runpy.run_path(str(Path(qp_solver_cvxopt.__file__)))

with pytest.raises(ImportError, match=r"pip install 'cbfkit\[cvxopt\]'") as exc:
namespace["solve"](jnp.eye(1), jnp.zeros(1))
assert exc.value.__cause__ is missing
Loading
Loading