-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor code into torch.1.0.0 (#39)
* refactor code into torch.1.0.0 * fix typo in a comment * black * one more typo in comment spotted * torch script fix * fix list add bug * comment * move to linalg
- Loading branch information
Showing
14 changed files
with
129 additions
and
106 deletions.
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
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
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 was deleted.
Oops, something went wrong.
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 @@ | ||
from .batch_linalg import svd, qr, sym, extract_diag, matrix_rank |
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,95 @@ | ||
import torch | ||
|
||
__all__ = ["svd", "qr", "sym", "extract_diag", "matrix_rank"] | ||
|
||
|
||
@torch.jit.script | ||
def svd(x): | ||
# inspired by | ||
# https://discuss.pytorch.org/t/multidimensional-svd/4366/2 | ||
# prolonged here: | ||
if x.dim() == 2: | ||
# 17 milliseconds on my mac to check that condition, that is low overhead | ||
result = torch.svd(x) | ||
else: | ||
batches = x.shape[:-2] | ||
other = x.shape[-2:] | ||
flat = x.view((-1,) + other) | ||
slices = flat.unbind(0) | ||
U, D, V = [], [], [] | ||
# I wish I had a parallel_for | ||
for i in range(flat.shape[0]): | ||
u, d, v = torch.svd(slices[i]) | ||
U += [u] | ||
D += [d] | ||
V += [v] | ||
U = torch.stack(U).view(batches + U[0].shape) | ||
D = torch.stack(D).view(batches + D[0].shape) | ||
V = torch.stack(V).view(batches + V[0].shape) | ||
result = U, D, V | ||
return result | ||
|
||
|
||
@torch.jit.script | ||
def qr(x): | ||
# inspired by | ||
# https://discuss.pytorch.org/t/multidimensional-svd/4366/2 | ||
# prolonged here: | ||
if x.dim() == 2: | ||
result = torch.qr(x) | ||
else: | ||
batches = x.shape[:-2] | ||
other = x.shape[-2:] | ||
flat = x.view((-1,) + other) | ||
slices = flat.unbind(0) | ||
Q, R = [], [] | ||
# I wish I had a parallel_for | ||
for i in range(flat.shape[0]): | ||
q, r = torch.qr(slices[i]) | ||
Q += [q] | ||
R += [r] | ||
Q = torch.stack(Q).view(batches + Q[0].shape) | ||
R = torch.stack(R).view(batches + R[0].shape) | ||
result = Q, R | ||
return result | ||
|
||
|
||
@torch.jit.script | ||
def sym(x): | ||
return 0.5 * (x.transpose(-1, -2) + x) | ||
|
||
|
||
@torch.jit.script | ||
def extract_diag(x): | ||
n, m = x.shape[-2:] | ||
batch = x.shape[:-2] | ||
k = n if n < m else m | ||
idx = torch.arange(k, dtype=torch.long, device=x.device) | ||
# torch script does not support Ellipsis indexing | ||
x = x.view(-1, n, m) | ||
return x[:, idx, idx].view(batch + (k,)) | ||
|
||
|
||
@torch.jit.script | ||
def matrix_rank(x): | ||
# inspired by | ||
# https://discuss.pytorch.org/t/multidimensional-svd/4366/2 | ||
# prolonged here: | ||
if x.dim() == 2: | ||
result = torch.matrix_rank(x) | ||
else: | ||
batches = x.shape[:-2] | ||
other = x.shape[-2:] | ||
flat = x.view((-1,) + other) | ||
slices = flat.unbind(0) | ||
ranks = [] | ||
# I wish I had a parallel_for | ||
for i in range(flat.shape[0]): | ||
r = torch.matrix_rank(slices[i]) | ||
# interesting, | ||
# ranks.append(r) | ||
# does not work on pytorch 1.0.0 | ||
# but the below code does | ||
ranks += [r] | ||
result = torch.stack(ranks).view(batches) | ||
return result |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
import pytest | ||
import torch | ||
import numpy as np | ||
import geoopt | ||
|
||
|
||
@pytest.fixture | ||
def A(): | ||
torch.manual_seed(42) | ||
n = 10 | ||
a = torch.randn(n, 3, 3) | ||
a[:, 2, :] = 0 | ||
return a | ||
|
||
|
||
def test_svd(A): | ||
u, d, v = geoopt.linalg.batch_linalg.svd(A) |