nerfstudio-project / nerfacc

A General NeRF Acceleration Toolbox in PyTorch.
https://www.nerfacc.com/
Other
1.37k stars 113 forks source link

removed unpack_data() #216

Closed salykova closed 1 year ago

salykova commented 1 year ago

Hi @liruilong940607

I noticed that you removed the unpack functions due to the new backend. Is there a simple way to unpack tensors with python/pytorch? or this needs to be implemented with c++/cuda code?

liruilong940607 commented 1 year ago

Hi unfortunately it requires some cuda programming which is not yet pop to the top of my priority list.

Simply borrow the relevant python&c&cuda code from nerfacc<=0.3.5 should be the easiest fix as this function had a pretty standalone implementation.

The new backend should enable a more optimized way of implementation which I have not yet do it.

salykova commented 1 year ago

nice, thanks for the hint!

liruilong940607 commented 1 year ago

@salykovaa Hi I figured out a way to implement the function in pure python:

def unpack_data(packed_info, data, n_samples):
    n_rays = packed_info.shape[0]
    all_samples = data.shape[0]
    row_strides = torch.arange(n_rays, device=device, dtype=torch.long) * n_samples
    row_starts, row_lengths = torch.unbind(packed_info, dim=1)
    idxs = torch.arange(all_samples, device=device, dtype=torch.long) - row_starts.repeat_interleave(row_lengths) + row_strides.repeat_interleave(packed_info[:, 1])
    outputs = torch.zeros((n_rays * n_samples, data.shape[-1]), device=device, dtype=data.dtype)
    outputs[idxs, :] = data
    outputs = outputs.reshape((n_rays, n_samples, data.shape[-1]))
    return outputs

And it is verified to be equivalent to the old implementation we had. Though I'm not sure how efficient this is comparing to the fused CUDA implementation.

salykova commented 1 year ago

@liruilong940607 many thanks!!! As you advised, I took the old pack and cuda unpack functions :sweat_smile: I will test the cuda unpack this pytorch unpack in terms of speed. Let's see how fast it is :blush: