Switching to Torch 2.0 by default. (#1922)

* Update requirements for torch 2.0

* Update docs

* Fix missing _LRScheduler import

* Fix pylint issues

* Update docs, drop 1.13

* Fix pip postfix version

* fix docs

* Allow python 1.13 (for now)

* Minor fix for torch.compile

* Switch to torch.compile

* ruff

* Fix test

---------

Co-authored-by: Matt Tancik <mattt4001@gmail.com>
Co-authored-by: Matthew Tancik <tancik@berkeley.edu>
This commit is contained in:
authored and GitHub committed 2023-05-17 16:03:17 -07:00
1 parent be291970db
commit e898f562e6
10 files changed
+83 -75

No files matched your search

+8 -5
View File
@@ -21,6 +21,8 @@ import torch
from jaxtyping import Bool, Float
from torch import Tensor
from nerfstudio.utils.misc import torch_compile
def components_from_spherical_harmonics(
levels: int, directions: Float[Tensor, "*batch 3"]
@@ -197,7 +199,7 @@ def expected_sin(x_means: torch.Tensor, x_vars: torch.Tensor) -> torch.Tensor:
return torch.exp(-0.5 * x_vars) * torch.sin(x_means)
@torch.jit.script
@torch_compile(dynamic=True, mode="reduce-overhead", backend="eager")
def intersect_aabb(
origins: torch.Tensor,
directions: torch.Tensor,
@@ -222,11 +224,11 @@ def intersect_aabb(
tx_min = (aabb[:3] - origins) / directions
tx_max = (aabb[3:] - origins) / directions
t_min = torch.min(tx_min, tx_max)
t_max = torch.max(tx_min, tx_max)
t_min = torch.stack((tx_min, tx_max)).amin(dim=0)
t_max = torch.stack((tx_min, tx_max)).amax(dim=0)
t_min = torch.max(t_min, dim=-1).values
t_max = torch.min(t_max, dim=-1).values
t_min = t_min.amax(dim=-1)
t_max = t_max.amin(dim=-1)
t_min = torch.clamp(t_min, min=0, max=max_bound)
t_max = torch.clamp(t_max, min=0, max=max_bound)
@@ -322,3 +324,4 @@ def normalized_depth_scale_and_shift(
shift[valid] = (-a_01[valid] * b_0[valid] + a_00[valid] * b_1[valid]) / det[valid]
return scale, shift
return scale, shift
+10
View File
@@ -155,3 +155,13 @@ def strtobool(val) -> bool:
FMI https://stackoverflow.com/a/715468
"""
return val.lower() in ("yes", "y", "true", "t", "on", "1")
if hasattr(torch, "compile"):
# PyTorch 2.0+
torch_compile = torch.compile
else:
# PyTorch 1.x
def torch_compile(*args, **kwargs):
"""Fall back to jit script if using pytorch 1.x, args ignored"""
return torch.jit.script
+5 -6
View File
@@ -70,11 +70,10 @@ class TensorDataclass:
This will 1) find the broadcasted shape and 2) broadcast all fields to this shape 3)
set _shape to be the broadcasted shape.
"""
if self._field_custom_dimensions is not None:
for k, v in self._field_custom_dimensions.items():
assert (
isinstance(v, int) and v > 1
), f"Custom dimensions must be an integer greater than 1, since 1 is the default, received {k}: {v}"
for k, v in self._field_custom_dimensions.items():
assert (
isinstance(v, int) and v > 1
), f"Custom dimensions must be an integer greater than 1, since 1 is the default, received {k}: {v}"
# Shim to prevent pyright from narrowing `self` to DataclassInstance.
self_dc = self
@@ -130,6 +129,7 @@ class TensorDataclass:
if isinstance(v, torch.Tensor):
# If custom dimension key, then we need to
if isinstance(self._field_custom_dimensions, dict) and k in self._field_custom_dimensions:
# pylint: disable=unsubscriptable-object
new_dict[k] = v.broadcast_to(
(
*batch_shape,
@@ -326,7 +326,6 @@ class TensorDataclass:
# This is the case when we have a custom dimensions tensor
elif (
isinstance(v, torch.Tensor)
and isinstance(self._field_custom_dimensions, dict)
and f in self._field_custom_dimensions
and custom_tensor_dims_fn is not None
):