Skip to main content

A library to compute gradients for convex optimization problems

Project description

Build Status

diffcp

diffcp is a Python package for computing the derivative of a convex cone program, with respect to its problem data. The derivative is implemented as an abstract linear map, with methods for its forward application and its adjoint.

The implementation is based on the calculations in our paper Differentiating through a cone program.

Installation

diffcp is available on PyPI, as a source distribution. Install it with

pip install diffcp

You will need a C++11-capable compiler to build diffcp.

diffcp requires:

diffcp uses Eigen; Eigen operations can be automatically vectorized by compilers. To enable vectorization, install with

MARCH_NATIVE=1 pip install diffcp

OpenMP can be enabled by passing extra arguments to your compiler. For example, on linux, you can tell gcc to activate the OpenMP extension by specifying the flag "-fopenmp":

OPENMP_FLAG="-fopenmp" pip install diffcp

To enable both vectorization and OpenMP (on linux), use

MARCH_NATIVE=1 OPENMP_FLAG="-fopenmp" pip install diffcp

Cone programs

diffcp differentiates through a primal-dual cone program pair. The primal problem must be expressed as

minimize        c'x
subject to      Ax + s = b
                s in K

where x and s are variables, A, b and c are the user-supplied problem data, and K is a user-defined convex cone. The corresponding dual problem is

minimize        b'y
subject to      A'y + c == 0
                y in K^*

with dual variable y.

Usage

diffcp exposes the function

solve_and_derivative(A, b, c, cone_dict, warm_start=None, solver=None, **kwargs).

This function returns a primal-dual solution x, y, and s, along with functions for evaluating the derivative and its adjoint (transpose). These functions respectively compute right and left multiplication of the derivative of the solution map at A, b, and c by a vector. The solver argument determines which solver to use; the available solvers are solver="SCS", solver="ECOS", and solver="Clarabel". If no solver is specified, diffcp will choose the solver itself. In the case that the problem is not solved, i.e. the solver fails for some reason, we will raise a SolverError Exception.

Arguments

The arguments A, b, and c correspond to the problem data of a cone program.

  • A must be a SciPy sparse CSC matrix.
  • b and c must be NumPy arrays.
  • cone_dict is a dictionary that defines the convex cone K.
  • warm_start is an optional tuple (x, y, s) at which to warm-start. (Note: this is only available for the SCS solver).
  • **kwargs are keyword arguments to forward to the solver (e.g., verbose=False).

These inputs must conform to the SCS convention for problem data. The keys in cone_dict correspond to the cones, with

  • diffcp.ZERO for the zero cone,
  • diffcp.POS for the positive orthant,
  • diffcp.SOC for a product of SOC cones,
  • diffcp.PSD for a product of PSD cones, and
  • diffcp.EXP for a product of exponential cones.

The values in cone_dict denote the sizes of each cone; the values of diffcp.SOC, diffcp.PSD, and diffcp.EXP should be lists. The order of the rows of A must match the ordering of the cones given above. For more details, consult the SCS documentation.

Return value

The function solve_and_derivative returns a tuple

(x, y, s, derivative, adjoint_derivative)
  • x, y, and s are a primal-dual solution.

  • derivative is a function that applies the derivative at (A, b, c) to perturbations dA, db, dc. It has the signature derivative(dA, db, dc) -> dx, dy, ds, where dA is a SciPy sparse CSC matrix with the same sparsity pattern as A, and db and dc are NumPy arrays. dx, dy, and ds are NumPy arrays, approximating the change in the primal-dual solution due to the perturbation.

  • adjoint_derivative is a function that applies the adjoint of the derivative to perturbations dx, dy, ds. It has the signature adjoint_derivative(dx, dy, ds) -> dA, db, dc, where dx, dy, and ds are NumPy arrays.

Example

import numpy as np
from scipy import sparse

import diffcp

cone_dict = {
    diffcp.ZERO: 3,
    diffcp.POS: 3,
    diffcp.SOC: [5]
}

m = 3 + 3 + 5
n = 5

A, b, c = diffcp.utils.random_cone_prog(m, n, cone_dict)
x, y, s, D, DT = diffcp.solve_and_derivative(A, b, c, cone_dict)

# evaluate the derivative
nonzeros = A.nonzero()
data = 1e-4 * np.random.randn(A.size)
dA = sparse.csc_matrix((data, nonzeros), shape=A.shape)
db = 1e-4 * np.random.randn(m)
dc = 1e-4 * np.random.randn(n)
dx, dy, ds = D(dA, db, dc)

# evaluate the adjoint of the derivative
dx = c
dy = np.zeros(m)
ds = np.zeros(m)
dA, db, dc = DT(dx, dy, ds)

For more examples, including the SDP example described in the paper, see the examples directory.

Citing

If you wish to cite diffcp, please use the following BibTex:

@article{diffcp2019,
    author       = {Agrawal, A. and Barratt, S. and Boyd, S. and Busseti, E. and Moursi, W.},
    title        = {Differentiating through a Cone Program},
    journal      = {Journal of Applied and Numerical Optimization},
    year         = {2019},
    volume       = {1},
    number       = {2},
    pages        = {107--115},
}

@misc{diffcp,
    author       = {Agrawal, A. and Barratt, S. and Boyd, S. and Busseti, E. and Moursi, W.},
    title        = {{diffcp}: differentiating through a cone program, version 1.0},
    howpublished = {\url{https://github.com/cvxgrp/diffcp}},
    year         = 2019
}

The following thesis concurrently derived the mathematics behind differentiating cone programs.

@phdthesis{amos2019differentiable,
  author       = {Brandon Amos},
  title        = {{Differentiable Optimization-Based Modeling for Machine Learning}},
  school       = {Carnegie Mellon University},
  year         = 2019,
  month        = May,
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffcp-1.1.4.tar.gz (2.1 MB view details)

Uploaded Source

Built Distributions

diffcp-1.1.4-cp313-cp313-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

diffcp-1.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (248.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

diffcp-1.1.4-cp313-cp313-macosx_11_0_arm64.whl (200.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

diffcp-1.1.4-cp313-cp313-macosx_10_13_x86_64.whl (224.7 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

diffcp-1.1.4-cp312-cp312-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

diffcp-1.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (248.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

diffcp-1.1.4-cp312-cp312-macosx_11_0_arm64.whl (200.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

diffcp-1.1.4-cp312-cp312-macosx_10_9_x86_64.whl (224.6 kB view details)

Uploaded CPython 3.12macOS 10.9+ x86-64

diffcp-1.1.4-cp311-cp311-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

diffcp-1.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (249.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

diffcp-1.1.4-cp311-cp311-macosx_11_0_arm64.whl (200.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

diffcp-1.1.4-cp311-cp311-macosx_10_9_x86_64.whl (223.6 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

diffcp-1.1.4-cp310-cp310-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

diffcp-1.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (248.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

diffcp-1.1.4-cp310-cp310-macosx_11_0_arm64.whl (199.5 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

diffcp-1.1.4-cp310-cp310-macosx_10_9_x86_64.whl (222.4 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file diffcp-1.1.4.tar.gz.

File metadata

  • Download URL: diffcp-1.1.4.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for diffcp-1.1.4.tar.gz
Algorithm Hash digest
SHA256 2c7aac13544fedcf858b33fec6aaf50cc8be6e6996ac6a54a46697e2697ab24c
MD5 2c38c4984f8ab940c8c88dc88bfc5166
BLAKE2b-256 b9279bdcc0b0559f452d2746a19e525c2f78361ed2fc0a909079030f8793dc5a

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d9031caf41ba01a5942df1b69ac4600d68973bf0db1c71ae4892ca9d4c8d34e9
MD5 c47edfd004a488de6ba40dfc0f7df309
BLAKE2b-256 b6b25853f108d38b1fbbd20130adf75c017480e8c897cc9f0227ebfcaa603c9c

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bfc188ade6f87a0a2aac3016e4e5ea54d4004ac8eaa546db3963425c46eabaf8
MD5 e9f69454b8831b90d84760744fc0824e
BLAKE2b-256 4c8596ecee50162c2db0bbee7506520d0fb16c7a4595e32e6a21141f71e4f2ff

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f928ddc032f2f18e284c585f7b70a58b16cde6980ae79bf8a624fa9f8e029804
MD5 c7da8ac9343fdf2369079f6b78cd8ff5
BLAKE2b-256 f5ccc9296b23c8b3b26ce2203551d32e3a9969ee03bc2c31b2ccd2a8c8f0d2a1

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 c50dccc6c144f536a8490e17dc426b176664ea318dce66a87d82e4498916203f
MD5 ef95b9dac40c049092a0388cd132fc74
BLAKE2b-256 84e1ca171d30689a8ba4afebbc3b347c55c3af19405059a2fad96586cbee8b3b

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5ec267629777d0143844a645625abe45f98999ef0e83635c9f30f6789a97f373
MD5 032ded9b0e8470fa87f70e756b55c412
BLAKE2b-256 1c667c6bd013e9a45adfba594d334eec8040f87d909587a7968357399d9e1005

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cc655c4af380214fae477dcbbf4ef23e424dbb78471ce5cfe292c6823f605c8d
MD5 a8bb3e9cff019485ebbeb9331894a3be
BLAKE2b-256 5ca46e162ed7cf8ba42c87ba179cecb3f623ebe0360dffc9983246fb866391ff

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e46fa2aac1c69e4d17c71ca134bbdacc32fe7be6634198526806fe6b335a0ef9
MD5 c6589a74664fd3ecd82b3cc6c7080501
BLAKE2b-256 95d5dcc67effa470fa60525ef0c28f7e47c698baf056eb048af26eb23ea0bde7

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 40c573b63167cf9bc71b24a423826b74ff05ff425b66b081ac3566e2eb72589c
MD5 982ac95b9b5348002b048b24cf9ed7fb
BLAKE2b-256 a57d36697377fafc6034ed8bcde313d7752e8a75be81fa64b502968d58217cdf

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2ca684039fbb011d073831100ae86bf46108c27e1220612d8bdaa31ade83e9c2
MD5 af07fab1f48966497f927825b88bd103
BLAKE2b-256 3d5d4c61ed7271643bab357ed5ca799bfdaeab47d874e3cba949b9460b92f134

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 07f9150663aa62468223dc18e5512e8f195e98bd0320fed56d427e7aa18479f5
MD5 c7c85337fff5fd0f9d5c0dd39d02134e
BLAKE2b-256 4f20b3e7071f1b06116ffac140951f369d22f380283bdcef3ac0fbdaa92c8c1e

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c93e55f210179701dfb4d835250691099f109fbae0e93819fe833b11f4130be0
MD5 bdb7c1342a010b782b7fa7982130e202
BLAKE2b-256 475571dc2532a5c88dea365d7838b7c3f565a5ab075a44772b17a55c576f0a62

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3471138129b28f3f80bc0fde2e8c8c670aaf7da530fb3c9b36134b941795960a
MD5 aaceaf7e7d22546ef10e94d5fb6ca570
BLAKE2b-256 816f1b71e6002267483e95bdb7ae1a78bb7d65a305d45bb20ccaadd63e1ca904

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2b0ce191a85a1b0f0f31a17498669b339a522f20fdcc7ff91ef3c5c7c9edd780
MD5 66927a040bd8d2d249d205b87481c771
BLAKE2b-256 b83ddc6b1ca23221c9107d38ef3c223dd9f68badfaf4999827817cc55162cf74

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 197c7a62efffead68549809dc993876460a568eed19b3436552935055d8abf8d
MD5 3bbbd57be66ecb77dfe772c24f2bd020
BLAKE2b-256 e4bb2d7cb4e467d1a2a49cdb9aa0c9588b898973f7e1b81eef9bcc31734c17c0

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e85db837778e00f89d8f30f4b3fb6a0ecde55ffd5e0551cb1f9498a0ed1c874
MD5 8cf1ee6fbc0226d5c8c81653b3cdf53d
BLAKE2b-256 fdd1175c05344860e4e80d9541a85250789127a1156f8fb079dce70bff8cbc9a

See more details on using hashes here.

File details

Details for the file diffcp-1.1.4-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for diffcp-1.1.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 aca2bc7e663af9a591687509556038e879694b5520422562a511dd0d93468715
MD5 c6793817bd594f19df3452dd53104607
BLAKE2b-256 fb8c62f7f4baed82daa15173619252fb789bb9546e9e329339aeba4f18cc6b5a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page