Skip to content

Commit

Permalink
Merge pull request #82 from brocksam/add-differentiation-benchmarks
Browse files Browse the repository at this point in the history
Add differentiation benchmarks
  • Loading branch information
moorepants authored Oct 17, 2022
2 parents 710cf34 + 6ee3acb commit 1b23fde
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 0 deletions.
75 changes: 75 additions & 0 deletions asv.conf.conda.slow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
// The version of the config file format. Do not change, unless
// you know what you are doing.
"version": 1,

// The name of the project being benchmarked
"project": "sympy",

// The project's homepage
"project_url": "http://sympy.org/",

// The URL or local path of the source code repository for the
// project being benchmarked
"repo": "https://github.com/sympy/sympy.git",

// List of branches to benchmark. If not provided, defaults to "master"
// (for git) or "tip" (for mercurial).
// "branches": ["master"], // for git
// "branches": ["tip"], // for mercurial

// The DVCS being used. If not set, it will be automatically
// determined from "repo" by looking at the protocol in the URL
// (if remote), or by looking for special directories, such as
// ".git" (if local).
// "dvcs": "git",

// The tool to use to create environments. May be "conda",
// "virtualenv" or other value depending on the plugins in use.
// If missing or the empty string, the tool will be automatically
// determined by looking for tools on the PATH environment
// variable.
"environment_type": "conda",

// the base URL to show a commit for the project.
"show_commit_url": "http://github.com/sympy/sympy/commit/",

// The Pythons you'd like to test against. If not provided, defaults
// to the current version of Python used to run `asv`.
// "pythons": ["2.7", "3.6"],

// The matrix of dependencies to test. Each key is the name of a
// package (in PyPI) and the values are version numbers. An empty
// list indicates to just test against the default (latest)
// version.
"matrix": {
"fastcache": [],
"mpmath": [],
"numpy": [],
},

// The directory (relative to the current directory) that benchmarks are
// stored in. If not provided, defaults to "benchmarks"
"benchmark_dir": "slow_benchmarks",

// The directory (relative to the current directory) to cache the Python
// environments in. If not provided, defaults to "env"
// "env_dir": "env",


// The directory (relative to the current directory) that raw benchmark
// results are stored in. If not provided, defaults to "results".
// "results_dir": "results",

// The directory (relative to the current directory) that the html tree
// should be written to. If not provided, defaults to "html".
// "html_dir": "html",

// The number of characters to retain in the commit hashes.
// "hash_length": 8,

// `asv` will cache wheels of the recent builds in each
// environment, making them faster to install next time. This is
// number of builds to keep, per environment.
// "wheel_cache_size": 0
}
75 changes: 75 additions & 0 deletions asv.conf.venv.slow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
// The version of the config file format. Do not change, unless
// you know what you are doing.
"version": 1,

// The name of the project being benchmarked
"project": "sympy",

// The project's homepage
"project_url": "http://sympy.org/",

// The URL or local path of the source code repository for the
// project being benchmarked
"repo": "https://github.com/sympy/sympy.git",

// List of branches to benchmark. If not provided, defaults to "master"
// (for git) or "tip" (for mercurial).
// "branches": ["master"], // for git
// "branches": ["tip"], // for mercurial

// The DVCS being used. If not set, it will be automatically
// determined from "repo" by looking at the protocol in the URL
// (if remote), or by looking for special directories, such as
// ".git" (if local).
// "dvcs": "git",

// The tool to use to create environments. May be "conda",
// "virtualenv" or other value depending on the plugins in use.
// If missing or the empty string, the tool will be automatically
// determined by looking for tools on the PATH environment
// variable.
"environment_type": "virtualenv",

// the base URL to show a commit for the project.
"show_commit_url": "http://github.com/sympy/sympy/commit/",

// The Pythons you'd like to test against. If not provided, defaults
// to the current version of Python used to run `asv`.
// "pythons": ["2.7", "3.3"],

// The matrix of dependencies to test. Each key is the name of a
// package (in PyPI) and the values are version numbers. An empty
// list indicates to just test against the default (latest)
// version.
"matrix": {
"fastcache": [],
"mpmath": [],
"numpy": [],
},

// The directory (relative to the current directory) that benchmarks are
// stored in. If not provided, defaults to "benchmarks"
"benchmark_dir": "slow_benchmarks",

// The directory (relative to the current directory) to cache the Python
// environments in. If not provided, defaults to "env"
// "env_dir": "env",


// The directory (relative to the current directory) that raw benchmark
// results are stored in. If not provided, defaults to "results".
// "results_dir": "results",

// The directory (relative to the current directory) that the html tree
// should be written to. If not provided, defaults to "html".
// "html_dir": "html",

// The number of characters to retain in the commit hashes.
// "hash_length": 8,

// `asv` will cache wheels of the recent builds in each
// environment, making them faster to install next time. This is
// number of builds to keep, per environment.
// "wheel_cache_size": 0
}
49 changes: 49 additions & 0 deletions benchmarks/core/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Benchmarks for symbolic differentiation."""

from sympy import exp, sin, symbols, tan, Matrix


class GriewankBabyExampleDerivatives:
"""Simple test function in two variables.
Function is the "baby example" taken from Griewank, A., & Walther, A.
(2008). Evaluating derivatives: principles and techniques of algorithmic
differentiation. SIAM.
"""

def setup(self):
"""Create the required symbols (x1, x2) and the expression (y)."""
x1, x2 = symbols("x1, x2")
self.x = Matrix([x1, x2])
self.y = (sin(x1 / x2) + (x1 / x2) - exp(x2)) * ((x1 / x2) - exp(x2))

def time_jacobian(self):
"""Time differentiation of the baby example."""
self.G = self.y.diff(self.x)


class GriewankLighthouseExampleDerivatives:
"""Simple matrix test function in four variables.
Function is the "lighthouse example" taken from Griewank, A., & Walther, A.
(2008). Evaluating derivatives: principles and techniques of algorithmic
differentiation. SIAM.
"""

def setup(self):
"""Create the required symbols (nu, gamma, omega, t) and the expression
(y).
"""
nu, gamma, omega, t = symbols("nu, gamma, omega, t")
self.x = Matrix([nu, gamma, omega, t])
self.y = Matrix([
nu * tan(omega * t) / (gamma - tan(omega * t)),
nu * gamma * tan(omega * t) / (gamma - tan(omega * t)),
])

def time_jacobian(self):
"""Time differentation of the lighthouse example."""
self.G = self.y.jacobian(self.x)
Empty file.
1 change: 1 addition & 0 deletions slow_benchmarks/core/artifacts/dis_eom.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions slow_benchmarks/core/artifacts/partials.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(Symbol('qaxi'), Symbol('qayi'), Symbol('qai'), Symbol('qbi'), Symbol('qci'), Symbol('qdi'), Symbol('qei'), Symbol('qfi'), Symbol('qgi'), Symbol('uaxi'), Symbol('uayi'), Symbol('uai'), Symbol('ubi'), Symbol('uci'), Symbol('udi'), Symbol('uei'), Symbol('ufi'), Symbol('ugi'), Symbol('qaxp'), Symbol('qayp'), Symbol('qap'), Symbol('qbp'), Symbol('qcp'), Symbol('qdp'), Symbol('qep'), Symbol('qfp'), Symbol('qgp'), Symbol('uaxp'), Symbol('uayp'), Symbol('uap'), Symbol('ubp'), Symbol('ucp'), Symbol('udp'), Symbol('uep'), Symbol('ufp'), Symbol('ugp'), Symbol('Tbi'), Symbol('Tci'), Symbol('Tdi'), Symbol('Tei'), Symbol('Tfi'), Symbol('Tgi'))
36 changes: 36 additions & 0 deletions slow_benchmarks/core/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Benchmarks for symbolic differentiation."""

import pathlib

from sympy import sympify


class Gait2DDerivatives:
"""Large real-world example equations of motion in 42 variables.
The equations of motion (EoMs) are derived from the Gait2D dynamic model of
gait (see here: https://github.com/csu-hmc/gait2d). This specific benchmark,
involving the derivation of the 18x42 Jacobian matrix, was highlighted in
SymPy issue #8191 (see here: https://github.com/sympy/sympy/issues/8191).
"""

def setup(self):
"""Read the equation of motion and variables from artifact files.
The 18 equations of motion are specified in `dis_eom.txt` while the 42
variables are specified in `partials.txt`. Copies of both `dis_eom.txt`
and `partials.txt` are in the `artifacts` subdirectory.
"""
artifacts_dir_path = pathlib.Path(__file__).parents[0] / "artifacts"
dis_eom_path = pathlib.Path(artifacts_dir_path, "dis_eom.txt")
with open(dis_eom_path, "r") as A_file:
self.A = sympify(A_file.read())
partials_path = pathlib.Path(artifacts_dir_path, "partials.txt")
with open(partials_path, "r") as x_file:
self.x = sympify(x_file.read())

def time_jacobian(self):
"""Time differentiation to construct the 18x42 Jacobian matrix."""
self.A.jacobian(self.x)

0 comments on commit 1b23fde

Please sign in to comment.