-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates AUTHORS and LICENSE files (#129)
* Fix Apache License file * Add AUTHORS file and update LICENSE * Update author list and add torch_cg
- Loading branch information
Showing
6 changed files
with
296 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Swyft | ||
Copyright 2020 The Swyft contributors | ||
|
||
Maintainer | ||
Christoph Weniger, University of Amsterdam <c.weniger@uva.nl> | ||
|
||
Initial developers | ||
Benjamin Miller | ||
Francesco Nattino | ||
Christoph Weniger | ||
|
||
Contributors | ||
Noemi Anau Montel | ||
Alex Cole | ||
Adam Coogan | ||
Meiert Grootes | ||
Ou Ku | ||
|
||
Dependencies | ||
Shane Barratt (for details see deps/torch_cg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don’t work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright <2019> Shane Barratt | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a slightly modified version of torch_cg from https://github.com/sbarratt/torch_cg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import torch | ||
import time | ||
|
||
|
||
def cg_batch(A_bmm, B, M_bmm=None, X0=None, rtol=1e-3, atol=0., maxiter=None, verbose=False): | ||
"""Solves a batch of PD matrix linear systems using the preconditioned CG algorithm. | ||
This function solves a batch of matrix linear systems of the form | ||
A_i X_i = B_i, i=1,...,K, | ||
where A_i is a n x n positive definite matrix and B_i is a n x m matrix, | ||
and X_i is the n x m matrix representing the solution for the ith system. | ||
Args: | ||
A_bmm: A callable that performs a batch matrix multiply of A and a K x n x m matrix. | ||
B: A K x n x m matrix representing the right hand sides. | ||
M_bmm: (optional) A callable that performs a batch matrix multiply of the preconditioning | ||
matrices M and a K x n x m matrix. (default=identity matrix) | ||
X0: (optional) Initial guess for X, defaults to M_bmm(B). (default=None) | ||
rtol: (optional) Relative tolerance for norm of residual. (default=1e-3) | ||
atol: (optional) Absolute tolerance for norm of residual. (default=0) | ||
maxiter: (optional) Maximum number of iterations to perform. (default=5*n) | ||
verbose: (optional) Whether or not to print status messages. (default=False) | ||
""" | ||
K, n, m = B.shape | ||
|
||
if M_bmm is None: | ||
M_bmm = lambda x: x | ||
if X0 is None: | ||
X0 = M_bmm(B) | ||
if maxiter is None: | ||
maxiter = 5 * n | ||
|
||
assert B.shape == (K, n, m) | ||
assert X0.shape == (K, n, m) | ||
assert rtol > 0 or atol > 0 | ||
assert isinstance(maxiter, int) | ||
|
||
X_k = X0 | ||
R_k = B - A_bmm(X_k) | ||
Z_k = M_bmm(R_k) | ||
|
||
P_k = torch.zeros_like(Z_k) | ||
|
||
P_k1 = P_k | ||
R_k1 = R_k | ||
R_k2 = R_k | ||
X_k1 = X0 | ||
Z_k1 = Z_k | ||
Z_k2 = Z_k | ||
|
||
B_norm = torch.norm(B, dim=1) | ||
stopping_matrix = torch.max(rtol*B_norm, atol*torch.ones_like(B_norm)) | ||
|
||
if verbose: | ||
print("%03s | %010s %06s" % ("it", "dist", "it/s")) | ||
|
||
optimal = False | ||
start = time.perf_counter() | ||
for k in range(1, maxiter + 1): | ||
start_iter = time.perf_counter() | ||
Z_k = M_bmm(R_k) | ||
|
||
if k == 1: | ||
P_k = Z_k | ||
R_k1 = R_k | ||
X_k1 = X_k | ||
Z_k1 = Z_k | ||
else: | ||
R_k2 = R_k1 | ||
Z_k2 = Z_k1 | ||
P_k1 = P_k | ||
R_k1 = R_k | ||
Z_k1 = Z_k | ||
X_k1 = X_k | ||
denominator = (R_k2 * Z_k2).sum(1) | ||
denominator[denominator == 0] = 1e-8 | ||
beta = (R_k1 * Z_k1).sum(1) / denominator | ||
P_k = Z_k1 + beta.unsqueeze(1) * P_k1 | ||
|
||
denominator = (P_k * A_bmm(P_k)).sum(1) | ||
denominator[denominator == 0] = 1e-8 | ||
alpha = (R_k1 * Z_k1).sum(1) / denominator | ||
X_k = X_k1 + alpha.unsqueeze(1) * P_k | ||
R_k = R_k1 - alpha.unsqueeze(1) * A_bmm(P_k) | ||
end_iter = time.perf_counter() | ||
|
||
residual_norm = torch.norm(A_bmm(X_k) - B, dim=1) | ||
|
||
if verbose: | ||
print("%03d | %8.4e %4.2f" % | ||
(k, torch.max(residual_norm-stopping_matrix), | ||
1. / (end_iter - start_iter))) | ||
|
||
if (residual_norm <= stopping_matrix).all(): | ||
optimal = True | ||
break | ||
|
||
end = time.perf_counter() | ||
|
||
if verbose: | ||
if optimal: | ||
print("Terminated in %d steps (reached maxiter). Took %.3f ms." % | ||
(k, (end - start) * 1000)) | ||
else: | ||
print("Terminated in %d steps (optimal). Took %.3f ms." % | ||
(k, (end - start) * 1000)) | ||
|
||
|
||
info = { | ||
"niter": k, | ||
"optimal": optimal | ||
} | ||
|
||
return X_k, info | ||
|
||
|
||
#class CG(torch.autograd.Function): | ||
class CG: | ||
|
||
def __init__(self, A_bmm, M_bmm=None, rtol=1e-3, atol=0., maxiter=None, verbose=False): | ||
self.A_bmm = A_bmm | ||
self.M_bmm = M_bmm | ||
self.rtol = rtol | ||
self.atol = atol | ||
self.maxiter = maxiter | ||
self.verbose = verbose | ||
|
||
def forward(self, B, X0=None): | ||
X, _ = cg_batch(self.A_bmm, B, M_bmm=self.M_bmm, X0=X0, rtol=self.rtol, | ||
atol=self.atol, maxiter=self.maxiter, verbose=self.verbose) | ||
return X | ||
# | ||
# def backward(self, dX): | ||
# dB, _ = cg_batch(self.A_bmm, dX, M_bmm=self.M_bmm, rtol=self.rtol, | ||
# atol=self.atol, maxiter=self.maxiter, verbose=self.verbose) | ||
# return dB |