mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-23 16:15:36 +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
@@ -357,6 +357,9 @@ class NGPSpacedSampler(Sampler):
|
||||
ray_bundle: RayBundle,
|
||||
aabb: TensorType[2, 3],
|
||||
num_samples: Optional[int] = None,
|
||||
marching_steps: Optional[int] = 128,
|
||||
t_min: Optional[TensorType["num_rays"]] = None,
|
||||
t_max: Optional[TensorType["num_rays"]] = None,
|
||||
) -> Tuple[RaySamples, TensorType["total_samples", 3], TensorType["total_samples"], TensorType["total_samples"]]:
|
||||
"""Generate ray samples in a bounding box.
|
||||
|
||||
@@ -379,13 +382,24 @@ class NGPSpacedSampler(Sampler):
|
||||
aabb = aabb.flatten()
|
||||
rays_o = ray_bundle.origins.contiguous()
|
||||
rays_d = ray_bundle.directions.contiguous()
|
||||
t_min, t_max = nerfactory_cuda.ray_aabb_intersect(rays_o, rays_d, aabb)
|
||||
scene_scale = (aabb[3:6] - aabb[0:3]).max()
|
||||
|
||||
# TODO(ruilongli): * 16 is for original impl for training. Need to run
|
||||
# some profiling test with this choice.
|
||||
max_samples_per_batch = len(rays_o) * num_samples
|
||||
if t_min is None or t_max is None:
|
||||
# TODO(ruilongli): this clipping here is stupid. Try to avoid that.
|
||||
t_min, t_max = nerfactory_cuda.ray_aabb_intersect(rays_o, rays_d, aabb)
|
||||
t_min = torch.clamp(t_min, max=1e10)
|
||||
t_max = torch.clamp(t_max, max=1e10)
|
||||
|
||||
# sampling arguments
|
||||
if self.training:
|
||||
# TODO(ruilongli): * 16 is for original impl for training. Need to run
|
||||
# some profiling test with this choice.
|
||||
marching_steps = -1
|
||||
max_samples_per_batch = len(rays_o) * num_samples
|
||||
else:
|
||||
max_samples_per_batch = len(rays_o) * marching_steps
|
||||
|
||||
# marching
|
||||
packed_info, origins, dirs, starts, ends = nerfactory_cuda.raymarching(
|
||||
# rays
|
||||
rays_o,
|
||||
@@ -399,20 +413,28 @@ class NGPSpacedSampler(Sampler):
|
||||
self.density_field.resolution,
|
||||
self.density_field.density_bitfield,
|
||||
# sampling args
|
||||
marching_steps,
|
||||
max_samples_per_batch,
|
||||
num_samples,
|
||||
0.0,
|
||||
scene_scale,
|
||||
)
|
||||
total_samples = max(packed_info[:, -1].sum(), 1)
|
||||
origins = origins[:total_samples]
|
||||
dirs = dirs[:total_samples]
|
||||
starts = starts[:total_samples]
|
||||
ends = ends[:total_samples]
|
||||
|
||||
# squeeze valid samples
|
||||
if self.training:
|
||||
total_samples = max(packed_info[:, -1].sum(), 1)
|
||||
origins = origins[:total_samples]
|
||||
dirs = dirs[:total_samples]
|
||||
starts = starts[:total_samples]
|
||||
ends = ends[:total_samples]
|
||||
valid_mask = None
|
||||
else:
|
||||
valid_mask = (dirs != 0.0).any(dim=-1, keepdim=True)
|
||||
|
||||
# return samples
|
||||
zeros = torch.zeros_like(origins[:, :1])
|
||||
|
||||
ray_samples = RaySamples(
|
||||
frustums=Frustums(origins=origins, directions=dirs, starts=starts, ends=ends, pixel_area=zeros),
|
||||
valid_mask=valid_mask,
|
||||
)
|
||||
return ray_samples, packed_info, t_min, t_max
|
||||
Reference in new issue
Block a user