Backward compatibility for torch 1.x and fix for windows (#1962)

* backward compatibility for torch 1.x and fix for windows

* Update pyproject.toml

* Update README.md

* Update README.md

(As per request)

---------

Co-authored-by: machenmusik <machenmusik@users.noreply.github.com>
This commit is contained in:
Jonáš Kulhánekandmachenmusik authored and GitHub committed 2023-05-22 08:58:08 -07:00
1 parent c86ae8a07f
commit 3bc160cc7d
6 files changed
+33 -6

No files matched your search

+3 -1
View File
@@ -25,6 +25,8 @@ from jaxtyping import Float
from numpy.typing import NDArray from numpy.typing import NDArray
from torch import Tensor from torch import Tensor
from nerfstudio.utils.misc import torch_compile
_EPS = np.finfo(float).eps * 4.0 _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 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( def radial_and_tangential_undistort(
coords: torch.Tensor, coords: torch.Tensor,
distortion_params: torch.Tensor, distortion_params: torch.Tensor,
+6 -1
View File
@@ -21,7 +21,12 @@ from typing import Literal, Optional, Tuple, Type
import numpy as np import numpy as np
from torch.optim import Optimizer, lr_scheduler 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 from nerfstudio.configs.base_config import InstantiateConfig
@@ -24,6 +24,7 @@ from torch import Tensor, nn
from nerfstudio.cameras.rays import RayBundle from nerfstudio.cameras.rays import RayBundle
from nerfstudio.data.scene_box import SceneBox from nerfstudio.data.scene_box import SceneBox
from nerfstudio.utils.misc import torch_compile
class SceneCollider(nn.Module): class SceneCollider(nn.Module):
@@ -108,7 +109,7 @@ class AABBBoxCollider(SceneCollider):
return ray_bundle return ray_bundle
@torch.compile(dynamic=True, mode="reduce-overhead") @torch_compile(dynamic=True, mode="reduce-overhead")
def _intersect_with_sphere( def _intersect_with_sphere(
rays_o: torch.Tensor, rays_d: torch.Tensor, center: torch.Tensor, radius: float = 1.0, near_plane: float = 0.0 rays_o: torch.Tensor, rays_d: torch.Tensor, center: torch.Tensor, radius: float = 1.0, near_plane: float = 0.0
): ):
+3 -1
View File
@@ -21,6 +21,8 @@ import torch
from jaxtyping import Bool, Float from jaxtyping import Bool, Float
from torch import Tensor from torch import Tensor
from nerfstudio.utils.misc import torch_compile
def components_from_spherical_harmonics( def components_from_spherical_harmonics(
levels: int, directions: Float[Tensor, "*batch 3"] 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) 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( def intersect_aabb(
origins: torch.Tensor, origins: torch.Tensor,
directions: torch.Tensor, directions: torch.Tensor,
+17
View File
@@ -17,6 +17,7 @@ Miscellaneous helper code.
""" """
import platform
from typing import Any, Callable, Dict, List, Optional, TypeVar, Union from typing import Any, Callable, Dict, List, Optional, TypeVar, Union
import torch import torch
@@ -158,3 +159,19 @@ def strtobool(val) -> bool:
FMI https://stackoverflow.com/a/715468 FMI https://stackoverflow.com/a/715468
""" """
return val.lower() in ("yes", "y", "true", "t", "on", "1") 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)
+2 -2
View File
@@ -47,8 +47,8 @@ dependencies = [
"rich>=12.5.1", "rich>=12.5.1",
"scikit-image>=0.19.3", "scikit-image>=0.19.3",
"tensorboard>=2.13.0", "tensorboard>=2.13.0",
"torch>=2.0.1", "torch>=1.13.1",
"torchvision>=0.15.2", "torchvision>=0.14.1",
"torchmetrics[image]>=0.9.3", "torchmetrics[image]>=0.9.3",
"typing_extensions>=4.4.0", "typing_extensions>=4.4.0",
"viser>=0.0.5", "viser>=0.0.5",