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

+2 -2
View File
@@ -114,8 +114,8 @@ SHELL ["/bin/bash", "-c"]
# Upgrade pip and install packages. # Upgrade pip and install packages.
RUN python3.10 -m pip install --upgrade pip setuptools pathtools promise pybind11 RUN python3.10 -m pip install --upgrade pip setuptools pathtools promise pybind11
# Install pytorch and submodules (Currently, we still use cu116 which is the latest version for torch 1.12.1 and is compatible with CUDA 11.8). # Install pytorch and submodules
RUN python3.10 -m pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116 RUN python3.10 -m pip install torch==2.0.1+cu118 torchvision==0.15.2+cu118 --extra-index-url https://download.pytorch.org/whl/cu118
# Install tynyCUDNN (we need to set the target architectures as environment variable first). # Install tynyCUDNN (we need to set the target architectures as environment variable first).
ENV TCNN_CUDA_ARCHITECTURES=${CUDA_ARCHITECTURES} ENV TCNN_CUDA_ARCHITECTURES=${CUDA_ARCHITECTURES}
RUN python3.10 -m pip install git+https://github.com/NVlabs/tiny-cuda-nn.git@v1.6#subdirectory=bindings/torch RUN python3.10 -m pip install git+https://github.com/NVlabs/tiny-cuda-nn.git@v1.6#subdirectory=bindings/torch
+10 -10
View File
@@ -83,7 +83,7 @@ For more complex changes (e.g., running with your own data/setting up a new NeRF
### Prerequisites ### Prerequisites
You must have an NVIDIA video card with CUDA installed on the system. This library has been tested with version 11.3 of CUDA. You can find more information about installing CUDA [here](https://docs.nvidia.com/cuda/cuda-quick-start-guide/index.html) You must have an NVIDIA video card with CUDA installed on the system. This library has been tested with version 11.8 of CUDA. You can find more information about installing CUDA [here](https://docs.nvidia.com/cuda/cuda-quick-start-guide/index.html)
### Create environment ### Create environment
@@ -97,19 +97,19 @@ python -m pip install --upgrade pip
### Dependencies ### Dependencies
Install pytorch with CUDA (this repo has been tested with CUDA 11.3 and CUDA 11.7) and [tiny-cuda-nn](https://github.com/NVlabs/tiny-cuda-nn) Install pytorch with CUDA (this repo has been tested with CUDA 11.7 and CUDA 11.8) and [tiny-cuda-nn](https://github.com/NVlabs/tiny-cuda-nn)
For CUDA 11.3:
```bash
pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 -f https://download.pytorch.org/whl/torch_stable.html
pip install ninja git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch
```
For CUDA 11.7: For CUDA 11.7:
```bash ```bash
pip install torch==1.13.1 torchvision functorch --extra-index-url https://download.pytorch.org/whl/cu117 pip install torch==2.0.1+cu117 torchvision==0.15.2+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
pip install ninja git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch
```
For CUDA 11.8:
```bash
pip install torch==2.0.1+cu118 torchvision==0.15.2+cu118 --extra-index-url https://download.pytorch.org/whl/cu118
pip install ninja git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch pip install ninja git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch
``` ```
+28 -41
View File
@@ -4,20 +4,13 @@
::::::{tab-set} ::::::{tab-set}
:::::{tab-item} Linux :::::{tab-item} Linux
Install CUDA. This library has been tested with version 11.3. You can find CUDA download links [here](https://developer.nvidia.com/cuda-toolkit-archive) and more information about installing CUDA [here](https://docs.nvidia.com/cuda/cuda-quick-start-guide/index.html). Install CUDA. This library has been tested with version 11.8. You can find CUDA download links [here](https://developer.nvidia.com/cuda-toolkit-archive) and more information about installing CUDA [here](https://docs.nvidia.com/cuda/cuda-quick-start-guide/index.html).
::::: :::::
:::::{tab-item} Windows :::::{tab-item} Windows
Install [Git](https://git-scm.com/downloads). Install [Git](https://git-scm.com/downloads).
Install CUDA. This library and the Windows installation process has been tested with version 11.3. Install CUDA. This library and the Windows installation process has been tested with version 11.7.
::::{tab-set}
:::{tab-item} 11.3 with Visual Studio 2019 v16.9
Install Visual Studio 2019 ver 16.9. This must be done before installing CUDA. The necessary components are included in the `Desktop Development with C++` workflow (also called `C++ Build Tools` in the BuildTools edition). You can find older versions of Visual Studio 2019 [here](https://learn.microsoft.com/en-us/visualstudio/releases/2019/history#release-dates-and-build-numbers). The latest version, 16.11, may cause errors when installing tiny-cuda-nn.
Install CUDA 11.3. You can find CUDA download links [here](https://developer.nvidia.com/cuda-toolkit-archive) and more information about installing CUDA [here](https://docs.nvidia.com/cuda/cuda-quick-start-guide/index.html).
::: :::
:::{tab-item} 11.7 with Visual Studio 2022 :::{tab-item} 11.7 with Visual Studio 2022
@@ -48,30 +41,39 @@ python -m pip install --upgrade pip
### pytorch ### pytorch
::::{tab-set} ::::{tab-set}
:::{tab-item} Torch 1.12.1 with CUDA 11.3 :::{tab-item} Torch 2.0.1 with CUDA 11.8
- To install 1.12.1 with CUDA 11.3: - To install 2.0.1 with CUDA 11.8:
```bash Note that if a pytorch version prior to 2.0.1 is installed,
pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 -f https://download.pytorch.org/whl/torch_stable.html
```
:::
:::{tab-item} Torch 1.13.1 with CUDA 11.7
- To install 1.13.1 with CUDA 11.7:
Note that if a pytorch version prior to 1.13 is installed,
it should be uninstalled first to avoid upgrade issues (e.g. with functorch) it should be uninstalled first to avoid upgrade issues (e.g. with functorch)
```bash ```bash
pip uninstall torch torchvision functorch pip uninstall torch torchvision functorch
``` ```
Install pytorch 1.13.1 with CUDA and [tiny-cuda-nn](https://github.com/NVlabs/tiny-cuda-nn) Install pytorch 2.0.1 with CUDA and [tiny-cuda-nn](https://github.com/NVlabs/tiny-cuda-nn)
```bash ```bash
pip install torch==1.13.1 torchvision functorch --extra-index-url https://download.pytorch.org/whl/cu117 pip install torch==2.0.1+cu118 torchvision==0.15.2+cu118 --extra-index-url https://download.pytorch.org/whl/cu118
```
:::
:::{tab-item} Torch 2.0.1 with CUDA 11.7
- To install 2.0.1 with CUDA 11.7:
Note that if a pytorch version prior to 2.0.1 is installed,
it should be uninstalled first to avoid upgrade issues (e.g. with functorch)
```bash
pip uninstall torch torchvision functorch
```
Install pytorch 2.0.1 with CUDA and [tiny-cuda-nn](https://github.com/NVlabs/tiny-cuda-nn)
```bash
pip install torch==2.0.1+cu117 torchvision==0.15.2+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
``` ```
::: :::
@@ -194,7 +196,6 @@ K80 | 37
- [TinyCUDA installation errors out with cuda mismatch](tiny-cuda-mismatch-error) - [TinyCUDA installation errors out with cuda mismatch](tiny-cuda-mismatch-error)
- [TinyCUDA installation errors out with no CUDA toolset found](tiny-cuda-integration-error) - [TinyCUDA installation errors out with no CUDA toolset found](tiny-cuda-integration-error)
- [TinyCUDA installation errors out with syntax errors](tiny-cuda-syntax-error)
- [Installation errors, File "setup.py" not found](pip-install-error) - [Installation errors, File "setup.py" not found](pip-install-error)
- [Runtime errors, "len(sources) > 0".](cuda-sources-error) - [Runtime errors, "len(sources) > 0".](cuda-sources-error)
@@ -221,7 +222,7 @@ While installing tiny-cuda on Windows, you run into: `No CUDA toolset found.`
**Solution**: **Solution**:
Confirm that you have Visual Studio installed (CUDA 11.3 is only compatible up to 2019 ver 16.9, not 16.11 or 2022). Confirm that you have Visual Studio installed.
Make sure CUDA Visual Studio integration is enabled. This should be done automatically by the CUDA installer if it is run after Visual Studio is installed. You can also manually enable integration. Make sure CUDA Visual Studio integration is enabled. This should be done automatically by the CUDA installer if it is run after Visual Studio is installed. You can also manually enable integration.
@@ -231,7 +232,7 @@ Make sure CUDA Visual Studio integration is enabled. This should be done automat
To manually enable integration for Visual Studio 2019, copy all 4 files from To manually enable integration for Visual Studio 2019, copy all 4 files from
``` ```
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\extras\visual_studio_integration\MSBuildExtensions C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\extras\visual_studio_integration\MSBuildExtensions
``` ```
to to
@@ -246,7 +247,7 @@ C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft
To manually enable integration for Visual Studio 2022, copy all 4 files from To manually enable integration for Visual Studio 2022, copy all 4 files from
``` ```
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\extras\visual_studio_integration\MSBuildExtensions C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\extras\visual_studio_integration\MSBuildExtensions
``` ```
to to
@@ -260,20 +261,6 @@ C:\Program Files\Microsoft Visual Studio\2022\[Community, Professional, Enterpri
<br /> <br />
(tiny-cuda-syntax-error)=
**(Windows) TinyCUDA installation errors out with syntax errors**
While installing tiny-cuda on Windows, you run into: `Expected a "("`
**Solution**:
If you are using CUDA 11.3 with Visual Studio 2019, confirm that Visual Studio is on version 16.9 and not 16.11.
If you are using a different version of CUDA and Visual Studio, try switching to CUDA 11.3 with Visual Studio 2019 ver 16.9.
<br />
(pip-install-error)= (pip-install-error)=
**Installation errors, File "setup.py" not found** **Installation errors, File "setup.py" not found**
+3 -2
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
@@ -340,7 +342,6 @@ def get_distortion_params(
return torch.Tensor([k1, k2, k3, k4, p1, p2]) return torch.Tensor([k1, k2, k3, k4, p1, p2])
@torch.jit.script
def _compute_residual_and_jacobian( def _compute_residual_and_jacobian(
x: torch.Tensor, x: torch.Tensor,
y: torch.Tensor, y: torch.Tensor,
@@ -406,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.jit.script @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,
+12 -4
View File
@@ -24,6 +24,14 @@ from torch.optim import Optimizer, lr_scheduler
from nerfstudio.configs.base_config import InstantiateConfig from nerfstudio.configs.base_config import InstantiateConfig
try:
from torch.optim.lr_scheduler import ( # pylint: disable=ungrouped-imports
LRScheduler,
)
except ImportError:
# Backward compatibility for PyTorch 1.x
from torch.optim.lr_scheduler import _LRScheduler as LRScheduler
@dataclass @dataclass
class SchedulerConfig(InstantiateConfig): class SchedulerConfig(InstantiateConfig):
@@ -43,7 +51,7 @@ class Scheduler:
self.config = config self.config = config
@abstractmethod @abstractmethod
def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> lr_scheduler._LRScheduler: def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> LRScheduler:
"""Abstract method that returns a scheduler object. """Abstract method that returns a scheduler object.
Args: Args:
@@ -73,7 +81,7 @@ class MultiStepScheduler(Scheduler):
config: MultiStepSchedulerConfig config: MultiStepSchedulerConfig
def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> lr_scheduler._LRScheduler: def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> LRScheduler:
scheduler = lr_scheduler.MultiStepLR( scheduler = lr_scheduler.MultiStepLR(
optimizer=optimizer, optimizer=optimizer,
milestones=self.config.milestones, milestones=self.config.milestones,
@@ -107,7 +115,7 @@ class ExponentialDecayScheduler(Scheduler):
config: ExponentialDecaySchedulerConfig config: ExponentialDecaySchedulerConfig
def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> lr_scheduler._LRScheduler: def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> LRScheduler:
if self.config.lr_final is None: if self.config.lr_final is None:
lr_final = lr_init lr_final = lr_init
else: else:
@@ -154,7 +162,7 @@ class CosineDecayScheduler(Scheduler):
config: CosineDecaySchedulerConfig config: CosineDecaySchedulerConfig
def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> lr_scheduler._LRScheduler: def get_scheduler(self, optimizer: Optimizer, lr_init: float) -> LRScheduler:
def func(step): def func(step):
if step < self.config.warm_up_end: if step < self.config.warm_up_end:
learning_factor = step / self.config.warm_up_end learning_factor = step / self.config.warm_up_end
@@ -22,6 +22,7 @@ import torch
from jaxtyping import Float from jaxtyping import Float
from torch import Tensor, nn from torch import Tensor, nn
from nerfstudio.utils.misc import torch_compile
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
@@ -108,7 +109,7 @@ class AABBBoxCollider(SceneCollider):
return ray_bundle return ray_bundle
@torch.jit.script @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
): ):
+8 -5
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.jit.script @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,
@@ -222,11 +224,11 @@ def intersect_aabb(
tx_min = (aabb[:3] - origins) / directions tx_min = (aabb[:3] - origins) / directions
tx_max = (aabb[3:] - origins) / directions tx_max = (aabb[3:] - origins) / directions
t_min = torch.min(tx_min, tx_max) t_min = torch.stack((tx_min, tx_max)).amin(dim=0)
t_max = torch.max(tx_min, tx_max) t_max = torch.stack((tx_min, tx_max)).amax(dim=0)
t_min = torch.max(t_min, dim=-1).values t_min = t_min.amax(dim=-1)
t_max = torch.min(t_max, dim=-1).values t_max = t_max.amin(dim=-1)
t_min = torch.clamp(t_min, min=0, max=max_bound) t_min = torch.clamp(t_min, min=0, max=max_bound)
t_max = torch.clamp(t_max, 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] shift[valid] = (-a_01[valid] * b_0[valid] + a_00[valid] * b_1[valid]) / det[valid]
return scale, shift return scale, shift
return scale, shift
+10
View File
@@ -155,3 +155,13 @@ 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")
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) This will 1) find the broadcasted shape and 2) broadcast all fields to this shape 3)
set _shape to be the broadcasted shape. set _shape to be the broadcasted shape.
""" """
if self._field_custom_dimensions is not None: for k, v in self._field_custom_dimensions.items():
for k, v in self._field_custom_dimensions.items(): assert (
assert ( isinstance(v, int) and v > 1
isinstance(v, int) and v > 1 ), f"Custom dimensions must be an integer greater than 1, since 1 is the default, received {k}: {v}"
), 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. # Shim to prevent pyright from narrowing `self` to DataclassInstance.
self_dc = self self_dc = self
@@ -130,6 +129,7 @@ class TensorDataclass:
if isinstance(v, torch.Tensor): if isinstance(v, torch.Tensor):
# If custom dimension key, then we need to # If custom dimension key, then we need to
if isinstance(self._field_custom_dimensions, dict) and k in self._field_custom_dimensions: if isinstance(self._field_custom_dimensions, dict) and k in self._field_custom_dimensions:
# pylint: disable=unsubscriptable-object
new_dict[k] = v.broadcast_to( new_dict[k] = v.broadcast_to(
( (
*batch_shape, *batch_shape,
@@ -326,7 +326,6 @@ class TensorDataclass:
# This is the case when we have a custom dimensions tensor # This is the case when we have a custom dimensions tensor
elif ( elif (
isinstance(v, torch.Tensor) isinstance(v, torch.Tensor)
and isinstance(self._field_custom_dimensions, dict)
and f in self._field_custom_dimensions and f in self._field_custom_dimensions
and custom_tensor_dims_fn is not None and custom_tensor_dims_fn is not None
): ):
+3 -4
View File
@@ -20,7 +20,6 @@ dependencies = [
"tyro>=0.3.31", "tyro>=0.3.31",
"gdown>=4.6.0", "gdown>=4.6.0",
"ninja>=1.10", "ninja>=1.10",
"functorch>=0.2.1",
"h5py>=2.9.0", "h5py>=2.9.0",
"imageio>=2.21.1", "imageio>=2.21.1",
'importlib-metadata>=6.0.0; python_version < "3.10"', 'importlib-metadata>=6.0.0; python_version < "3.10"',
@@ -48,9 +47,9 @@ 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>=1.12.1,<2.0.0", "torch>=1.13.1",
"torchvision>=0.14.1",
"torchmetrics[image]>=0.9.3", "torchmetrics[image]>=0.9.3",
"torchvision>=0.13.0",
"typing_extensions>=4.4.0", "typing_extensions>=4.4.0",
"viser>=0.0.5", "viser>=0.0.5",
"nuscenes-devkit>=1.1.1", "nuscenes-devkit>=1.1.1",
@@ -115,7 +114,7 @@ ns-dev-sync-viser-message-defs = "nerfstudio.scripts.viewer.sync_viser_message_d
[options] [options]
# equivalent to using --extra-index-url with pip, which is needed for specifying the CUDA version torch and torchvision # equivalent to using --extra-index-url with pip, which is needed for specifying the CUDA version torch and torchvision
dependency_links = [ dependency_links = [
"https://download.pytorch.org/whl/cu113" "https://download.pytorch.org/whl/cu118"
] ]
[tool.setuptools.packages.find] [tool.setuptools.packages.find]