diff --git a/nerfstudio/cameras/camera_utils.py b/nerfstudio/cameras/camera_utils.py index c6344c4e7..2c0b746d1 100644 --- a/nerfstudio/cameras/camera_utils.py +++ b/nerfstudio/cameras/camera_utils.py @@ -25,6 +25,8 @@ from jaxtyping import Float from numpy.typing import NDArray from torch import Tensor +from nerfstudio.utils.misc import torch_compile + _EPS = np.finfo(float).eps * 4.0 @@ -405,7 +407,7 @@ def _compute_residual_and_jacobian( return fx, fy, fx_x, fx_y, fy_x, fy_y -@torch.compile(dynamic=True, mode="reduce-overhead", backend="eager") +@torch_compile(dynamic=True, mode="reduce-overhead", backend="eager") def radial_and_tangential_undistort( coords: torch.Tensor, distortion_params: torch.Tensor, diff --git a/nerfstudio/engine/schedulers.py b/nerfstudio/engine/schedulers.py index a60e5c2f0..35b3138cb 100644 --- a/nerfstudio/engine/schedulers.py +++ b/nerfstudio/engine/schedulers.py @@ -21,7 +21,12 @@ from typing import Literal, Optional, Tuple, Type import numpy as np from torch.optim import Optimizer, lr_scheduler -from torch.optim.lr_scheduler import LRScheduler + +try: + from torch.optim.lr_scheduler import LRScheduler +except ImportError: + # Backwards compatibility for PyTorch 1.x + from torch.optim.lr_scheduler import _LRScheduler as LRScheduler from nerfstudio.configs.base_config import InstantiateConfig diff --git a/nerfstudio/model_components/scene_colliders.py b/nerfstudio/model_components/scene_colliders.py index 5dfbd1b3b..fa6242011 100644 --- a/nerfstudio/model_components/scene_colliders.py +++ b/nerfstudio/model_components/scene_colliders.py @@ -24,6 +24,7 @@ from torch import Tensor, nn from nerfstudio.cameras.rays import RayBundle from nerfstudio.data.scene_box import SceneBox +from nerfstudio.utils.misc import torch_compile class SceneCollider(nn.Module): @@ -108,7 +109,7 @@ class AABBBoxCollider(SceneCollider): return ray_bundle -@torch.compile(dynamic=True, mode="reduce-overhead") +@torch_compile(dynamic=True, mode="reduce-overhead") def _intersect_with_sphere( rays_o: torch.Tensor, rays_d: torch.Tensor, center: torch.Tensor, radius: float = 1.0, near_plane: float = 0.0 ): diff --git a/nerfstudio/utils/math.py b/nerfstudio/utils/math.py index 980ce195b..866d280f8 100644 --- a/nerfstudio/utils/math.py +++ b/nerfstudio/utils/math.py @@ -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.compile(dynamic=True, mode="reduce-overhead", backend="eager") +@torch_compile(dynamic=True, mode="reduce-overhead", backend="eager") def intersect_aabb( origins: torch.Tensor, directions: torch.Tensor, diff --git a/nerfstudio/utils/misc.py b/nerfstudio/utils/misc.py index e21aece48..5b40117e9 100644 --- a/nerfstudio/utils/misc.py +++ b/nerfstudio/utils/misc.py @@ -17,6 +17,7 @@ Miscellaneous helper code. """ +import platform from typing import Any, Callable, Dict, List, Optional, TypeVar, Union import torch @@ -158,3 +159,19 @@ def strtobool(val) -> bool: FMI https://stackoverflow.com/a/715468 """ return val.lower() in ("yes", "y", "true", "t", "on", "1") + + +def torch_compile(*args, **kwargs): + """ + Safe torch.compile with backward compatibility for PyTorch 1.x + """ + if not hasattr(torch, "compile"): + # Backward compatibility for PyTorch 1.x + return torch.jit.script + elif platform.system() == "Windows": + # torch.compile is not supported on Windows + # https://github.com/orgs/pytorch/projects/27 + # TODO: @jkulhanek, remove this once torch.compile is supported on Windows + return lambda x: x + else: + return torch.compile(*args, **kwargs) diff --git a/pyproject.toml b/pyproject.toml index 2732c2b02..2e55e77e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,8 +47,8 @@ dependencies = [ "rich>=12.5.1", "scikit-image>=0.19.3", "tensorboard>=2.13.0", - "torch>=2.0.1", - "torchvision>=0.15.2", + "torch>=1.13.1", + "torchvision>=0.14.1", "torchmetrics[image]>=0.9.3", "typing_extensions>=4.4.0", "viser>=0.0.5",