Skip to content

QuadprogProjector does not validate norm_eps and reg_eps at construction #810

Closed

Description

@ValerianRey

The norm_eps and reg_eps setters of QuadprogProjector raise a ValueError on negative values, but __init__ assigns self._norm_eps and self._reg_eps directly, so the check is skipped at construction:

def __init__(
self,
*,
norm_eps: float = 0.0001,
reg_eps: float = 0.0001,
) -> None:
super().__init__()
self._norm_eps = norm_eps
self._reg_eps = reg_eps
@property
def norm_eps(self) -> float:
return self._norm_eps
@norm_eps.setter
def norm_eps(self, value: float) -> None:
if value < 0.0:
raise ValueError(f"norm_eps must be non-negative, but got {value}.")
self._norm_eps = value
@property
def reg_eps(self) -> float:
return self._reg_eps
@reg_eps.setter
def reg_eps(self, value: float) -> None:
if value < 0.0:
raise ValueError(f"reg_eps must be non-negative, but got {value}.")
self._reg_eps = value

>>> from torchjd.linalg import QuadprogProjector
>>> p = QuadprogProjector(norm_eps=-1.0, reg_eps=-5.0)  # accepted
>>> p.norm_eps = -1.0
ValueError: norm_eps must be non-negative, but got -1.0.

A negative reg_eps is subtracted from the diagonal of the Gramian, which can make the quadratic program non-convex. The user then gets a "Failed to solve the quadratic programming problem." error much later, instead of a clear error when creating the projector. The aggregators (e.g. CAGradWeighting) already go through their setters in __init__.

Suggested fix

super().__init__()
self.norm_eps = norm_eps
self.reg_eps = reg_eps

Checklist

  • Use the setters in QuadprogProjector.__init__.
  • Add a test in tests/unit/linalg/test_dual_cone.py checking that QuadprogProjector(norm_eps=-1.0) and QuadprogProjector(reg_eps=-1.0) raise a ValueError.

See CONTRIBUTING.md to set up the development environment.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssue that should be easy to solve for new contributors

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions