Fix some typing problems (#1940)

* Fix some typing problems

* nerfstudio.data typing error fixes

* Add tiny fixes

* Remove ABC from pipeline

* Prevent circular imports

* Tweak ModuleList typing

* TrainingCallbackAttributes cleanup

* Various minor nits

* More nits

* Run `ruff`

---------

Co-authored-by: Brent Yi <yibrenth@gmail.com>
This commit is contained in:
Jonáš KulhánekandBrent Yi authored and GitHub committed 2023-05-17 17:36:56 -07:00
1 parent c6111e377e
commit f481f5ebec
27 files changed
+154 -75

No files matched your search

+15 -3
View File
@@ -16,7 +16,11 @@
Special activation functions.
"""
from typing import TYPE_CHECKING
import torch
from jaxtyping import Float
from torch import Tensor
from torch.autograd import Function
from torch.cuda.amp import custom_bwd, custom_fwd
@@ -37,6 +41,14 @@ class _TruncExp(Function):
return g * torch.exp(x.clamp(-15, 15))
trunc_exp = _TruncExp.apply
"""Same as torch.exp, but with the backward pass clipped to prevent vanishing/exploding
gradients."""
if TYPE_CHECKING:
def trunc_exp(_: Float[Tensor, "*bs"], /) -> Float[Tensor, "*bs"]:
"""Same as torch.exp, but with the backward pass clipped to prevent vanishing/exploding
gradients."""
raise NotImplementedError()
else:
trunc_exp = _TruncExp.apply
"""Same as torch.exp, but with the backward pass clipped to prevent vanishing/exploding
gradients."""
+3
View File
@@ -621,6 +621,7 @@ class KPlanesEncoding(Encoding):
"""Sample features from this encoder. Expects ``in_tensor`` to be in range [-1, 1]"""
original_shape = in_tensor.shape
assert any(self.coo_combs)
output = 1.0 if self.reduce == "product" else 0.0 # identity for corresponding op
for ci, coo_comb in enumerate(self.coo_combs):
grid = self.plane_coefs[ci].unsqueeze(0) # [1, feature_dim, reso1, reso2]
@@ -634,6 +635,8 @@ class KPlanesEncoding(Encoding):
else:
output = output + interp
# Typing: output gets converted to a tensor after the first iteration of the loop
assert isinstance(output, Tensor)
return output.reshape(*original_shape[:-1], self.num_components)
+7 -1
View File
@@ -174,6 +174,11 @@ class TemporalGridEncoder(nn.Module):
align_corners: same as other interpolation operators
"""
sampling_index: Tensor
index_a_mask: Tensor
index_b_mask: Tensor
index_list: Tensor
def __init__(
self,
temporal_dim: int = 64,
@@ -345,6 +350,7 @@ class TemporalGridEncoder(nn.Module):
self.gridtype_id,
self.align_corners,
)
assert isinstance(outputs, Tensor)
return outputs
def get_temporal_tv_loss(self) -> Float[Tensor, ""]:
@@ -352,6 +358,6 @@ class TemporalGridEncoder(nn.Module):
Sample a random channel combination (i.e., row for the combination table),
and then compute loss on it.
"""
row_idx = torch.randint(0, len(self.index_list), [1]).item()
row_idx = torch.randint(0, len(self.index_list), [1])
feat_idx = self.index_list[row_idx]
return (self.embeddings[:, feat_idx[0]] - self.embeddings[:, feat_idx[1]]).abs().mean()