Skip to content

Commit 50f8efa

Browse files
Tendocatfacebook-github-bot
authored andcommitted
Use sparse_coo_tensor in laplacian_matrices.py (#1991)
Summary: update obsolete torch.sparse.FloatTensor to torch.sparse_coo_tensor Pull Request resolved: #1991 Reviewed By: MichaelRamamonjisoa Differential Revision: D80084359 Pulled By: bottler fbshipit-source-id: dc6c7a90211113d1ce5338a92c8c0030bfe12e65
1 parent 5043d15 commit 50f8efa

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

pytorch3d/ops/laplacian_matrices.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def laplacian(verts: torch.Tensor, edges: torch.Tensor) -> torch.Tensor:
4747
# i.e. A[i, j] = 1 if (i,j) is an edge, or
4848
# A[e0, e1] = 1 & A[e1, e0] = 1
4949
ones = torch.ones(idx.shape[1], dtype=torch.float32, device=verts.device)
50-
# pyre-fixme[16]: Module `sparse` has no attribute `FloatTensor`.
51-
A = torch.sparse.FloatTensor(idx, ones, (V, V))
50+
A = torch.sparse_coo_tensor(idx, ones, (V, V), dtype=torch.float32)
5251

5352
# the sum of i-th row of A gives the degree of the i-th vertex
5453
deg = torch.sparse.sum(A, dim=1).to_dense()
@@ -62,15 +61,13 @@ def laplacian(verts: torch.Tensor, edges: torch.Tensor) -> torch.Tensor:
6261
# pyre-fixme[58]: `/` is not supported for operand types `float` and `Tensor`.
6362
deg1 = torch.where(deg1 > 0.0, 1.0 / deg1, deg1)
6463
val = torch.cat([deg0, deg1])
65-
# pyre-fixme[16]: Module `sparse` has no attribute `FloatTensor`.
66-
L = torch.sparse.FloatTensor(idx, val, (V, V))
64+
L = torch.sparse_coo_tensor(idx, val, (V, V), dtype=torch.float32)
6765

6866
# Then we add the diagonal values L[i, i] = -1.
6967
idx = torch.arange(V, device=verts.device)
7068
idx = torch.stack([idx, idx], dim=0)
7169
ones = torch.ones(idx.shape[1], dtype=torch.float32, device=verts.device)
72-
# pyre-fixme[16]: Module `sparse` has no attribute `FloatTensor`.
73-
L -= torch.sparse.FloatTensor(idx, ones, (V, V))
70+
L -= torch.sparse_coo_tensor(idx, ones, (V, V), dtype=torch.float32)
7471

7572
return L
7673

@@ -126,8 +123,7 @@ def cot_laplacian(
126123
ii = faces[:, [1, 2, 0]]
127124
jj = faces[:, [2, 0, 1]]
128125
idx = torch.stack([ii, jj], dim=0).view(2, F * 3)
129-
# pyre-fixme[16]: Module `sparse` has no attribute `FloatTensor`.
130-
L = torch.sparse.FloatTensor(idx, cot.view(-1), (V, V))
126+
L = torch.sparse_coo_tensor(idx, cot.view(-1), (V, V), dtype=torch.float32)
131127

132128
# Make it symmetric; this means we are also setting
133129
# L[v2, v1] = cota
@@ -167,16 +163,15 @@ def norm_laplacian(
167163
v0, v1 = edge_verts[:, 0], edge_verts[:, 1]
168164

169165
# Side lengths of each edge, of shape (E,)
170-
w01 = 1.0 / ((v0 - v1).norm(dim=1) + eps)
166+
w01 = torch.reciprocal((v0 - v1).norm(dim=1) + eps)
171167

172168
# Construct a sparse matrix by basically doing:
173169
# L[v0, v1] = w01
174170
# L[v1, v0] = w01
175171
e01 = edges.t() # (2, E)
176172

177173
V = verts.shape[0]
178-
# pyre-fixme[16]: Module `sparse` has no attribute `FloatTensor`.
179-
L = torch.sparse.FloatTensor(e01, w01, (V, V))
174+
L = torch.sparse_coo_tensor(e01, w01, (V, V), dtype=torch.float32)
180175
L = L + L.t()
181176

182177
return L

0 commit comments

Comments
 (0)