Skip to content
Open
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
4 changes: 2 additions & 2 deletions docs/guide/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CvxpyLayer(
| `problem` | `cp.Problem` | A DPP-compliant CVXPY problem |
| `parameters` | `list[cp.Parameter]` | Parameters that will receive values at runtime |
| `variables` | `list[cp.Variable]` | Variables to return from the solution |
| `solver` | `str` or `None` | Solver backend (e.g., `cp.CLARABEL`, `cp.SCS`) |
| `solver` | `str`, `SolverInterface`, or `None` | Layer backend (e.g., `cp.DIFFCP`, `"MOREAU"`, `cp.CUCLARABEL`), a custom solver, or `None` for diffcp |
| `gp` | `bool` | Set `True` for geometric/log-log convex programs |
| `verbose` | `bool` | Print solver output |
| `solver_args` | `dict` | Default arguments passed to the solver |
Expand Down Expand Up @@ -170,6 +170,6 @@ SolverError: Solver 'SCS' failed.
```

Try:
1. Different solver: `solver=cp.CLARABEL`
1. Different diffcp solver: `solver_args={"solve_method": "Clarabel"}`
2. More iterations: `solver_args={"max_iters": 10000}`
3. Looser tolerance: `solver_args={"eps": 1e-6}`
14 changes: 4 additions & 10 deletions docs/guide/batching.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ print(b_batch.grad.shape) # (10, 3)

## JAX: vmap Integration

In JAX, you can also use `jax.vmap` for batching:
With the default diffcp backend, pass batched arrays directly. `jax.vmap`
and `jax.jit` require the Moreau backend; diffcp uses Python-based solving
and cannot be traced by these transformations.

```python
import jax
Expand All @@ -116,17 +118,9 @@ from cvxpylayers.jax import CvxpyLayer

layer = CvxpyLayer(problem, parameters=[A, b], variables=[x])

# Option 1: Pass batched arrays directly (built-in support)
# Pass batched arrays directly (works with the default diffcp backend)
A_batch = jax.random.normal(key, (batch_size, 3, 2))
(x_batch,) = layer(A_batch, b_single)

# Option 2: Use vmap explicitly
def solve_single(A):
(x,) = layer(A, b_single)
return x

solve_batched = jax.vmap(solve_single)
x_batch = solve_batched(A_batch)
```

## Example: Batch Portfolio Optimization
Expand Down
20 changes: 15 additions & 5 deletions docs/guide/solvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ CVXPYlayers supports multiple solver backends for different use cases.

## Specifying a Solver

`solver` selects the CVXPYlayers backend, such as `cp.DIFFCP` (the default),
`"MOREAU"`, or `cp.CUCLARABEL`. To select SCS or Clarabel within diffcp,
use `solver_args={"solve_method": "SCS"}` or
`solver_args={"solve_method": "Clarabel"}`; do not pass them as `solver`.

### At Construction

```python
Expand All @@ -27,7 +32,7 @@ layer = CvxpyLayer(
parameters=[A, b],
variables=[x],
solver=cp.DIFFCP,
solver_args={'solver': cp.CLARABEL} # Use Clarabel
solver_args={"solve_method": "Clarabel"} # Use Clarabel within diffcp
)
```

Expand All @@ -38,7 +43,7 @@ layer = CvxpyLayer(
(x,) = layer(A_tensor, b_tensor)

# Override with different solver
(x,) = layer(A_tensor, b_tensor, solver_args={"solver": cp.SCS})
(x,) = layer(A_tensor, b_tensor, solver_args={"solve_method": "SCS"})
```

## Specifying a Custom Solver
Expand Down Expand Up @@ -131,8 +136,10 @@ layer = CvxpyLayer(

| Argument | Solver | Description |
|----------|--------|-------------|
| `eps` | SCS, Clarabel | Convergence tolerance |
| `max_iters` | All | Maximum iterations |
| `eps` | SCS | Convergence tolerance |
| `max_iters` | SCS | Maximum iterations |
| `tol_gap_abs`, `tol_gap_rel`, `tol_feas` | Clarabel | Convergence tolerances |
| `max_iter` | Clarabel | Maximum iterations |
| `verbose` | All | Print solver output |
| `acceleration_lookback` | SCS | Anderson acceleration window |

Expand All @@ -155,7 +162,10 @@ If SCS still struggles, try Clarabel:

```python
# Clarabel for better cone support
layer = CvxpyLayer(problem, parameters=[A, b], variables=[x], solver=cp.CLARABEL)
layer = CvxpyLayer(
problem, parameters=[A, b], variables=[x],
solver_args={"solve_method": "Clarabel"}
)
```

## Moreau
Expand Down
6 changes: 4 additions & 2 deletions src/cvxpylayers/jax/cvxpylayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,12 @@ def __init__(
at runtime. Order must match the order of arrays passed to __call__().
variables: List of CVXPY Variables whose optimal values will be returned
by __call__(). Order determines the order of returned arrays.
solver: CVXPY solver to use (e.g., ``cp.CLARABEL``, ``cp.SCS``),
solver: Layer backend (e.g., ``cp.DIFFCP``, ``"MOREAU"``, ``cp.CUCLARABEL``),
a :class:`~cvxpylayers.interfaces.base.SolverInterface` instance
for a custom solver (e.g. CVXPYgen), or ``None`` (uses diffcp
by default).
by default). To select SCS or Clarabel within diffcp, use
``solver_args={"solve_method": "SCS"}`` or
``solver_args={"solve_method": "Clarabel"}``.
gp: If True, problem is a geometric program. Parameters will be
log-transformed before solving.
verbose: If True, print solver output.
Expand Down
6 changes: 4 additions & 2 deletions src/cvxpylayers/mlx/cvxpylayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ def __init__(
at runtime. Order must match the order of arrays passed to __call__().
variables: List of CVXPY Variables whose optimal values will be returned
by __call__(). Order determines the order of returned arrays.
solver: CVXPY solver to use (e.g., ``cp.CLARABEL``, ``cp.SCS``).
If None, uses the default diffcp solver.
solver: Layer backend. If None, uses the default diffcp solver.
To select SCS or Clarabel within diffcp, use
``solver_args={"solve_method": "SCS"}`` or
``solver_args={"solve_method": "Clarabel"}``.
gp: If True, problem is a geometric program. Parameters will be
log-transformed before solving.
verbose: If True, print solver output.
Expand Down
5 changes: 4 additions & 1 deletion src/cvxpylayers/torch/cvxpylayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,15 @@ def __init__(
at runtime. Order must match the order of tensors passed to forward().
variables: List of CVXPY Variables whose optimal values will be returned
by forward(). Order determines the order of returned tensors.
solver: CVXPY solver string (e.g., ``cp.CLARABEL``, ``cp.SCS``),
solver: Layer backend (e.g., ``cp.DIFFCP``, ``"MOREAU"``, ``cp.CUCLARABEL``),
a :class:`~cvxpylayers.interfaces.base.SolverInterface` instance
for a custom solver, or ``None`` (uses diffcp by default).
When a ``SolverInterface`` is passed, its ``canon_solver`` attribute
selects the CVXPY canonicalization solver; the instance itself is
called during the forward/backward pass instead of diffcp.
To select SCS or Clarabel within diffcp, use
``solver_args={"solve_method": "SCS"}`` or
``solver_args={"solve_method": "Clarabel"}``.
gp: If True, problem is a geometric program. Parameters will be
log-transformed before solving.
verbose: If True, print solver output.
Expand Down