mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-29 12:31:53 +08:00
Speed-up NGP inference by 3x. (#211)
This commit is contained in:
1 parent
934bd4bd72
commit
3e28a40c44
10 files changed
+243
-104
No files matched your search
@@ -33,9 +33,9 @@ class VolumeRenderer(torch.autograd.Function):
|
||||
|
||||
@staticmethod
|
||||
@custom_fwd(cast_inputs=torch.float32)
|
||||
def forward(ctx, packed_info, starts, ends, sigmas, rgbs):
|
||||
def forward(ctx, packed_info, starts, ends, sigmas, rgbs, opacities):
|
||||
accumulated_weight, accumulated_depth, accumulated_color, mask = volumetric_rendering_forward(
|
||||
packed_info, starts, ends, sigmas, rgbs
|
||||
packed_info, starts, ends, sigmas, rgbs, opacities
|
||||
)
|
||||
# TODO(ruilongli): accelerate for torch.no_grad()?
|
||||
ctx.save_for_backward(
|
||||
@@ -47,6 +47,7 @@ class VolumeRenderer(torch.autograd.Function):
|
||||
ends,
|
||||
sigmas,
|
||||
rgbs,
|
||||
opacities,
|
||||
)
|
||||
return accumulated_weight, accumulated_depth, accumulated_color, mask
|
||||
|
||||
@@ -62,6 +63,7 @@ class VolumeRenderer(torch.autograd.Function):
|
||||
ends,
|
||||
sigmas,
|
||||
rgbs,
|
||||
opacities,
|
||||
) = ctx.saved_tensors
|
||||
grad_sigmas, grad_rgbs = volumetric_rendering_backward(
|
||||
accumulated_weight,
|
||||
@@ -75,9 +77,10 @@ class VolumeRenderer(torch.autograd.Function):
|
||||
ends,
|
||||
sigmas,
|
||||
rgbs,
|
||||
opacities,
|
||||
)
|
||||
# corresponds to the input argument list of forward()
|
||||
return None, None, None, grad_sigmas, grad_rgbs
|
||||
return None, None, None, grad_sigmas, grad_rgbs, None
|
||||
|
||||
|
||||
def unpackbits(x: torch.Tensor) -> torch.Tensor:
|
||||
|
||||
@@ -28,8 +28,9 @@ std::vector<torch::Tensor> raymarching(
|
||||
const int grid_size,
|
||||
const torch::Tensor grid_bitfield,
|
||||
// sampling args
|
||||
const int marching_steps,
|
||||
const int max_total_samples,
|
||||
const int num_steps,
|
||||
const int max_ray_samples,
|
||||
const float cone_angle,
|
||||
const float step_scale
|
||||
) {};
|
||||
@@ -39,7 +40,8 @@ std::vector<torch::Tensor> volumetric_rendering_forward(
|
||||
torch::Tensor starts,
|
||||
torch::Tensor ends,
|
||||
torch::Tensor sigmas,
|
||||
torch::Tensor rgbs
|
||||
torch::Tensor rgbs,
|
||||
torch::Tensor opacities
|
||||
) {};
|
||||
|
||||
std::vector<torch::Tensor> volumetric_rendering_backward(
|
||||
@@ -53,7 +55,8 @@ std::vector<torch::Tensor> volumetric_rendering_backward(
|
||||
torch::Tensor starts,
|
||||
torch::Tensor ends,
|
||||
torch::Tensor sigmas,
|
||||
torch::Tensor rgbs
|
||||
torch::Tensor rgbs,
|
||||
torch::Tensor opacities
|
||||
) {};
|
||||
|
||||
std::vector<torch::Tensor> occupancy_query(
|
||||
|
||||
@@ -28,8 +28,9 @@ std::vector<torch::Tensor> raymarching(
|
||||
const int grid_size,
|
||||
const torch::Tensor grid_bitfield,
|
||||
// sampling args
|
||||
const int marching_steps,
|
||||
const int max_total_samples,
|
||||
const int num_steps,
|
||||
const int max_ray_samples,
|
||||
const float cone_angle,
|
||||
const float step_scale
|
||||
);
|
||||
@@ -39,7 +40,8 @@ std::vector<torch::Tensor> volumetric_rendering_forward(
|
||||
torch::Tensor starts,
|
||||
torch::Tensor ends,
|
||||
torch::Tensor sigmas,
|
||||
torch::Tensor rgbs
|
||||
torch::Tensor rgbs,
|
||||
torch::Tensor opacities
|
||||
);
|
||||
|
||||
std::vector<torch::Tensor> volumetric_rendering_backward(
|
||||
@@ -53,7 +55,8 @@ std::vector<torch::Tensor> volumetric_rendering_backward(
|
||||
torch::Tensor starts,
|
||||
torch::Tensor ends,
|
||||
torch::Tensor sigmas,
|
||||
torch::Tensor rgbs
|
||||
torch::Tensor rgbs,
|
||||
torch::Tensor opacities
|
||||
);
|
||||
|
||||
std::vector<torch::Tensor> occupancy_query(
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "include/helpers_cuda.h"
|
||||
|
||||
|
||||
inline __device__ float min_step_size(uint32_t num_steps) {
|
||||
return __SQRT3() / num_steps;
|
||||
inline __device__ float min_step_size(uint32_t max_ray_samples) {
|
||||
return __SQRT3() / max_ray_samples;
|
||||
}
|
||||
|
||||
// Maximum step size is the width of the coarsest gridsize cell.
|
||||
@@ -100,6 +100,10 @@ inline __device__ float advance_to_next_voxel(
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
// Two modes:
|
||||
// 1. "vertically" marching: a portion of rays march maximum steps. (during training)
|
||||
// 2. "horizontally" marching: all rays march together for certain steps. (during inference)
|
||||
template <typename scalar_t>
|
||||
__global__ void kernel_raymarching(
|
||||
// rays info
|
||||
@@ -115,11 +119,12 @@ __global__ void kernel_raymarching(
|
||||
const int grid_size,
|
||||
const uint8_t* grid_bitfield,
|
||||
// sampling
|
||||
const int marching_steps, // used only for inference. -1 means disable it.
|
||||
const int max_total_samples,
|
||||
const int num_steps, // default 1024
|
||||
const int max_ray_samples, // default 1024
|
||||
const float cone_angle, // default 0. for nerf-syn and 1/256 for large scene
|
||||
const float step_scale,
|
||||
int* numsteps_counter,
|
||||
int* steps_counter,
|
||||
int* rays_counter,
|
||||
int* packed_info_out,
|
||||
scalar_t* origins_out,
|
||||
@@ -142,60 +147,65 @@ __global__ void kernel_raymarching(
|
||||
|
||||
// TODO(ruilongli): perturb `near` as in ngp_pl?
|
||||
// TODO(ruilongli): pre-compute `dt_min`, `dt_max` as it is a constant?
|
||||
float dt_min = min_step_size(num_steps) * step_scale;
|
||||
float dt_min = min_step_size(max_ray_samples) * step_scale;
|
||||
float dt_max = max_step_size(grid_cascades, grid_size) * grid_scale;
|
||||
|
||||
// first pass to compute an accurate number of steps
|
||||
uint32_t j = 0;
|
||||
float t0 = near;
|
||||
float dt = calc_dt(t0, cone_angle, dt_min, dt_max);
|
||||
float t1 = t0 + dt;
|
||||
float t_mid = (t0 + t1) * 0.5f;
|
||||
|
||||
uint32_t ray_idx, base, marching_samples;
|
||||
uint32_t j;
|
||||
float t0, dt, t1, t_mid;
|
||||
if (marching_steps < 0) {
|
||||
// first pass to compute an accurate number of steps
|
||||
j = 0;
|
||||
t0 = near;
|
||||
dt = calc_dt(t0, cone_angle, dt_min, dt_max);
|
||||
t1 = t0 + dt;
|
||||
t_mid = (t0 + t1) * 0.5f;
|
||||
|
||||
while (t_mid < far && j < num_steps) {
|
||||
// current center
|
||||
const float x = ox + t_mid * dx;
|
||||
const float y = oy + t_mid * dy;
|
||||
const float z = oz + t_mid * dz;
|
||||
uint32_t mip = mip_from_dt(x, y, z, grid_cascades, dt, grid_size, grid_center, grid_scale);
|
||||
|
||||
if (grid_occupied_at(x, y, z, grid_bitfield, mip, grid_size, grid_center, grid_scale)) {
|
||||
++j;
|
||||
// march to next sample
|
||||
t0 = t1;
|
||||
dt = calc_dt(t0, cone_angle, dt_min, dt_max);
|
||||
t1 = t0 + dt;
|
||||
t_mid = (t0 + t1) * 0.5f;
|
||||
}
|
||||
else {
|
||||
// march to next sample
|
||||
float res = (grid_size >> mip) * grid_scale;
|
||||
t_mid = advance_to_next_voxel(
|
||||
t_mid, x, y, z, dx, dy, dz, rdx, rdy, rdz, res, dt_min
|
||||
);
|
||||
dt = calc_dt(t_mid, cone_angle, dt_min, dt_max);
|
||||
t0 = t_mid - dt * 0.5f;
|
||||
t1 = t_mid + dt * 0.5f;
|
||||
while (t_mid < far && j < max_ray_samples) {
|
||||
// current center
|
||||
const float x = ox + t_mid * dx;
|
||||
const float y = oy + t_mid * dy;
|
||||
const float z = oz + t_mid * dz;
|
||||
uint32_t mip = mip_from_dt(x, y, z, grid_cascades, dt, grid_size, grid_center, grid_scale);
|
||||
|
||||
if (grid_occupied_at(x, y, z, grid_bitfield, mip, grid_size, grid_center, grid_scale)) {
|
||||
++j;
|
||||
// march to next sample
|
||||
t0 = t1;
|
||||
dt = calc_dt(t0, cone_angle, dt_min, dt_max);
|
||||
t1 = t0 + dt;
|
||||
t_mid = (t0 + t1) * 0.5f;
|
||||
}
|
||||
else {
|
||||
// march to next sample
|
||||
float res = (grid_size >> mip) * grid_scale;
|
||||
t_mid = advance_to_next_voxel(
|
||||
t_mid, x, y, z, dx, dy, dz, rdx, rdy, rdz, res, dt_min
|
||||
);
|
||||
dt = calc_dt(t_mid, cone_angle, dt_min, dt_max);
|
||||
t0 = t_mid - dt * 0.5f;
|
||||
t1 = t_mid + dt * 0.5f;
|
||||
}
|
||||
}
|
||||
if (j == 0) return;
|
||||
|
||||
marching_samples = j;
|
||||
base = atomicAdd(steps_counter, marching_samples);
|
||||
if (base + marching_samples > max_total_samples) return;
|
||||
ray_idx = atomicAdd(rays_counter, 1);
|
||||
|
||||
} else {
|
||||
marching_samples = marching_steps;
|
||||
base = i * marching_steps;
|
||||
ray_idx = i;
|
||||
}
|
||||
if (j == 0) return;
|
||||
|
||||
uint32_t numsteps = j;
|
||||
uint32_t base = atomicAdd(numsteps_counter, numsteps);
|
||||
if (base + numsteps > max_total_samples) return;
|
||||
|
||||
// locate
|
||||
origins_out += base * 3;
|
||||
dirs_out += base * 3;
|
||||
starts_out += base;
|
||||
ends_out += base;
|
||||
|
||||
uint32_t ray_idx = atomicAdd(rays_counter, 1);
|
||||
|
||||
packed_info_out[ray_idx * 3 + 0] = i; // ray idx in {rays_o, rays_d}
|
||||
packed_info_out[ray_idx * 3 + 1] = base; // point idx start.
|
||||
packed_info_out[ray_idx * 3 + 2] = numsteps; // point idx shift.
|
||||
|
||||
// Second round
|
||||
j = 0;
|
||||
t0 = near;
|
||||
@@ -203,7 +213,7 @@ __global__ void kernel_raymarching(
|
||||
t1 = t0 + dt;
|
||||
t_mid = (t0 + t1) / 2.;
|
||||
|
||||
while (t_mid < far && j < num_steps) {
|
||||
while (t_mid < far && j < marching_samples) {
|
||||
// current center
|
||||
const float x = ox + t_mid * dx;
|
||||
const float y = oy + t_mid * dy;
|
||||
@@ -235,8 +245,13 @@ __global__ void kernel_raymarching(
|
||||
dt = calc_dt(t_mid, cone_angle, dt_min, dt_max);
|
||||
t0 = t_mid - dt * 0.5f;
|
||||
t1 = t_mid + dt * 0.5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
packed_info_out[ray_idx * 3 + 0] = i; // ray idx in {rays_o, rays_d}
|
||||
packed_info_out[ray_idx * 3 + 1] = base; // point idx start.
|
||||
packed_info_out[ray_idx * 3 + 2] = j; // point idx shift (actual marching samples).
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -302,8 +317,9 @@ __global__ void kernel_occupancy_query(
|
||||
* @param grid_cascades Density grid levels.
|
||||
* @param grid_size Density grid resolution.
|
||||
* @param grid_bitfield Density grid uint8 bit field.
|
||||
* @param marching_steps Marching steps during inference.
|
||||
* @param max_total_samples Maximum total number of samples in this batch.
|
||||
* @param num_steps Used to define the minimal step size: SQRT3() / num_steps.
|
||||
* @param max_ray_samples Used to define the minimal step size: SQRT3() / max_ray_samples.
|
||||
* @param cone_angle 0. for nerf-synthetic and 1./256 for real scenes.
|
||||
* @param step_scale Scale up the step size by this much. Usually equals to scene scale.
|
||||
* @return std::vector<torch::Tensor>
|
||||
@@ -330,8 +346,9 @@ std::vector<torch::Tensor> raymarching(
|
||||
const int grid_size,
|
||||
const torch::Tensor grid_bitfield,
|
||||
// sampling args
|
||||
const int marching_steps,
|
||||
const int max_total_samples,
|
||||
const int num_steps,
|
||||
const int max_ray_samples,
|
||||
const float cone_angle,
|
||||
const float step_scale
|
||||
) {
|
||||
@@ -349,18 +366,20 @@ std::vector<torch::Tensor> raymarching(
|
||||
const int blocks = CUDA_N_BLOCKS_NEEDED(n_rays, threads);
|
||||
|
||||
// helper counter
|
||||
torch::Tensor numsteps_counter = torch::zeros(
|
||||
torch::Tensor steps_counter = torch::zeros(
|
||||
{1}, rays_o.options().dtype(torch::kInt32));
|
||||
torch::Tensor rays_counter = torch::zeros(
|
||||
{1}, rays_o.options().dtype(torch::kInt32));
|
||||
|
||||
int output_total_samples = marching_steps < 0 ? max_total_samples : marching_steps * n_rays;
|
||||
|
||||
// output samples
|
||||
torch::Tensor packed_info = torch::zeros(
|
||||
{n_rays, 3}, rays_o.options().dtype(torch::kInt32)); // ray_id, sample_id, num_samples
|
||||
torch::Tensor origins = torch::zeros({max_total_samples, 3}, rays_o.options());
|
||||
torch::Tensor dirs = torch::zeros({max_total_samples, 3}, rays_o.options());
|
||||
torch::Tensor starts = torch::zeros({max_total_samples, 1}, rays_o.options());
|
||||
torch::Tensor ends = torch::zeros({max_total_samples, 1}, rays_o.options());
|
||||
torch::Tensor origins = torch::zeros({output_total_samples, 3}, rays_o.options());
|
||||
torch::Tensor dirs = torch::zeros({output_total_samples, 3}, rays_o.options());
|
||||
torch::Tensor starts = torch::zeros({output_total_samples, 1}, rays_o.options());
|
||||
torch::Tensor ends = torch::zeros({output_total_samples, 1}, rays_o.options());
|
||||
|
||||
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
|
||||
rays_o.scalar_type(),
|
||||
@@ -380,11 +399,12 @@ std::vector<torch::Tensor> raymarching(
|
||||
grid_size,
|
||||
grid_bitfield.data_ptr<uint8_t>(),
|
||||
// sampling args
|
||||
marching_steps, // used only for inference. -1 means disable it.
|
||||
max_total_samples,
|
||||
num_steps,
|
||||
max_ray_samples,
|
||||
cone_angle,
|
||||
step_scale,
|
||||
numsteps_counter.data_ptr<int>(), // total samples.
|
||||
steps_counter.data_ptr<int>(), // total samples.
|
||||
rays_counter.data_ptr<int>(), // total rays.
|
||||
packed_info.data_ptr<int>(),
|
||||
origins.data_ptr<scalar_t>(),
|
||||
|
||||
@@ -9,6 +9,7 @@ __global__ void volumetric_rendering_forward_kernel(
|
||||
const scalar_t* ends, // input end t
|
||||
const scalar_t* sigmas, // input density after activation
|
||||
const scalar_t* rgbs, // input rgb after activation
|
||||
const scalar_t* opacities, // input opacities for each ray
|
||||
// should be all-zero initialized
|
||||
scalar_t* accumulated_weight, // output
|
||||
scalar_t* accumulated_depth, // output
|
||||
@@ -32,9 +33,10 @@ __global__ void volumetric_rendering_forward_kernel(
|
||||
accumulated_depth += i;
|
||||
accumulated_color += i * 3;
|
||||
mask += i;
|
||||
|
||||
opacities += i;
|
||||
|
||||
// accumulated rendering
|
||||
scalar_t T = 1.f;
|
||||
scalar_t T = 1.f - opacities[0];
|
||||
scalar_t EPSILON = 1e-4f;
|
||||
int j = 0;
|
||||
for (; j < numsteps; ++j) {
|
||||
@@ -65,6 +67,7 @@ __global__ void volumetric_rendering_backward_kernel(
|
||||
const scalar_t* ends, // input end t
|
||||
const scalar_t* sigmas, // input density after activation
|
||||
const scalar_t* rgbs, // input rgb after activation
|
||||
const scalar_t* opacities, // input opacities for each ray
|
||||
const scalar_t* accumulated_weight, // forward output
|
||||
const scalar_t* accumulated_depth, // forward output
|
||||
const scalar_t* accumulated_color, // forward output
|
||||
@@ -93,13 +96,14 @@ __global__ void volumetric_rendering_backward_kernel(
|
||||
accumulated_weight += i;
|
||||
accumulated_depth += i;
|
||||
accumulated_color += i * 3;
|
||||
opacities += i;
|
||||
|
||||
grad_weight += i;
|
||||
grad_depth += i;
|
||||
grad_color += i * 3;
|
||||
|
||||
// backward of accumulated rendering
|
||||
scalar_t T = 1.f;
|
||||
scalar_t T = 1.f - opacities[0];
|
||||
scalar_t EPSILON = 1e-4f;
|
||||
int j = 0;
|
||||
scalar_t r = 0, g = 0, b = 0, d = 0;
|
||||
@@ -148,6 +152,7 @@ __global__ void volumetric_rendering_backward_kernel(
|
||||
* @param ends: Where the frustum-shape sample ends along a ray. [total_samples, 1]
|
||||
* @param sigmas Densities at those samples. [total_samples, 1]
|
||||
* @param rgbs RGBs at those samples. [total_samples, 3]
|
||||
* @param opacities Opacities that the rays already have. Zero for training but non-zero for eval. [total_samples, 1]
|
||||
* @return std::vector<torch::Tensor>
|
||||
* - accumulated_weight: Ray opacity. [n_rays, 1]
|
||||
* - accumulated_depth: Ray depth. [n_rays, 1]
|
||||
@@ -159,7 +164,8 @@ std::vector<torch::Tensor> volumetric_rendering_forward(
|
||||
torch::Tensor starts,
|
||||
torch::Tensor ends,
|
||||
torch::Tensor sigmas,
|
||||
torch::Tensor rgbs
|
||||
torch::Tensor rgbs,
|
||||
torch::Tensor opacities
|
||||
) {
|
||||
DEVICE_GUARD(packed_info);
|
||||
CHECK_INPUT(packed_info);
|
||||
@@ -167,11 +173,13 @@ std::vector<torch::Tensor> volumetric_rendering_forward(
|
||||
CHECK_INPUT(ends);
|
||||
CHECK_INPUT(sigmas);
|
||||
CHECK_INPUT(rgbs);
|
||||
CHECK_INPUT(opacities);
|
||||
TORCH_CHECK(packed_info.ndimension() == 2 & packed_info.size(1) == 3);
|
||||
TORCH_CHECK(starts.ndimension() == 2 & starts.size(1) == 1);
|
||||
TORCH_CHECK(ends.ndimension() == 2 & ends.size(1) == 1);
|
||||
TORCH_CHECK(sigmas.ndimension() == 2 & sigmas.size(1) == 1);
|
||||
TORCH_CHECK(rgbs.ndimension() == 2 & rgbs.size(1) == 3);
|
||||
TORCH_CHECK(opacities.ndimension() == 2 & opacities.size(1) == 1);
|
||||
|
||||
const uint32_t n_rays = packed_info.size(0);
|
||||
|
||||
@@ -196,6 +204,7 @@ std::vector<torch::Tensor> volumetric_rendering_forward(
|
||||
ends.data_ptr<scalar_t>(),
|
||||
sigmas.data_ptr<scalar_t>(),
|
||||
rgbs.data_ptr<scalar_t>(),
|
||||
opacities.data_ptr<scalar_t>(),
|
||||
accumulated_weight.data_ptr<scalar_t>(),
|
||||
accumulated_depth.data_ptr<scalar_t>(),
|
||||
accumulated_color.data_ptr<scalar_t>(),
|
||||
@@ -222,7 +231,8 @@ std::vector<torch::Tensor> volumetric_rendering_backward(
|
||||
torch::Tensor starts,
|
||||
torch::Tensor ends,
|
||||
torch::Tensor sigmas,
|
||||
torch::Tensor rgbs
|
||||
torch::Tensor rgbs,
|
||||
torch::Tensor opacities
|
||||
) {
|
||||
DEVICE_GUARD(packed_info);
|
||||
const uint32_t n_rays = packed_info.size(0);
|
||||
@@ -245,6 +255,7 @@ std::vector<torch::Tensor> volumetric_rendering_backward(
|
||||
ends.data_ptr<scalar_t>(),
|
||||
sigmas.data_ptr<scalar_t>(),
|
||||
rgbs.data_ptr<scalar_t>(),
|
||||
opacities.data_ptr<scalar_t>(),
|
||||
accumulated_weight.data_ptr<scalar_t>(),
|
||||
accumulated_depth.data_ptr<scalar_t>(),
|
||||
accumulated_color.data_ptr<scalar_t>(),
|
||||
|
||||
Reference in new issue
Block a user