volumetric_rendering.cu 8.09 KB
Newer Older
Ruilong Li's avatar
cleanup  
Ruilong Li committed
1
2
3
#include "include/helpers_cuda.h"


Ruilong Li's avatar
cleanup  
Ruilong Li committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
template <typename scalar_t>
__global__ void volumetric_rendering_steps_kernel(
    const uint32_t n_rays,
    const int* packed_info,  // input ray & point indices.
    const scalar_t* starts,  // input start t
    const scalar_t* ends,  // input end t
    const scalar_t* sigmas,  // input density after activation
    // output: should be all zero (false) initialized
    int* num_steps, 
    bool* selector
) {
    CUDA_GET_THREAD_ID(i, n_rays);

    // locate
    const int base = packed_info[i * 2 + 0];  // point idx start.
    const int steps = packed_info[i * 2 + 1];  // point idx shift.
    if (steps == 0) return;

    starts += base;
    ends += base;
    sigmas += base;
    num_steps += i;
    selector += base;

    // accumulated rendering
    scalar_t T = 1.f;
    scalar_t EPSILON = 1e-4f;
    int j = 0;
    for (; j < steps; ++j) {
        if (T < EPSILON) {
            break;
        }
        const scalar_t delta = ends[j] - starts[j];
        const scalar_t alpha = 1.f - __expf(-sigmas[j] * delta);
        const scalar_t weight = alpha * T;
        T *= (1.f - alpha);
        selector[j] = true;
    }
    num_steps[0] = j;
    return;
}


Ruilong Li's avatar
cleanup  
Ruilong Li committed
47
template <typename scalar_t>
Ruilong Li's avatar
Ruilong Li committed
48
__global__ void volumetric_rendering_weights_forward_kernel(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
49
50
51
52
53
54
55
    const uint32_t n_rays,
    const int* packed_info,  // input ray & point indices.
    const scalar_t* starts,  // input start t
    const scalar_t* ends,  // input end t
    const scalar_t* sigmas,  // input density after activation
    // should be all-zero initialized
    scalar_t* weights,  // output
Ruilong Li's avatar
Ruilong Li committed
56
    int* samples_ray_ids // output
Ruilong Li's avatar
cleanup  
Ruilong Li committed
57
) {
Ruilong Li's avatar
Ruilong Li committed
58
    CUDA_GET_THREAD_ID(i, n_rays);
Ruilong Li's avatar
cleanup  
Ruilong Li committed
59
60

    // locate
Ruilong Li's avatar
Ruilong Li committed
61
62
63
    const int base = packed_info[i * 2 + 0];  // point idx start.
    const int steps = packed_info[i * 2 + 1];  // point idx shift.
    if (steps == 0) return;
Ruilong Li's avatar
cleanup  
Ruilong Li committed
64
65
66
67
68
69
70

    starts += base;
    ends += base;
    sigmas += base;
    weights += base;
    samples_ray_ids += base;

Ruilong Li's avatar
Ruilong Li committed
71
    for (int j = 0; j < steps; ++j) {
Ruilong Li's avatar
cleanup  
Ruilong Li committed
72
73
74
75
76
77
        samples_ray_ids[j] = i;
    }

    // accumulated rendering
    scalar_t T = 1.f;
    scalar_t EPSILON = 1e-4f;
Ruilong Li's avatar
Ruilong Li committed
78
    for (int j = 0; j < steps; ++j) {
Ruilong Li's avatar
cleanup  
Ruilong Li committed
79
80
81
82
83
84
85
86
87
88
89
90
91
        if (T < EPSILON) {
            break;
        }
        const scalar_t delta = ends[j] - starts[j];
        const scalar_t alpha = 1.f - __expf(-sigmas[j] * delta);
        const scalar_t weight = alpha * T;
        weights[j] = weight;
        T *= (1.f - alpha);
    }
}


template <typename scalar_t>
Ruilong Li's avatar
Ruilong Li committed
92
__global__ void volumetric_rendering_weights_backward_kernel(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
93
94
95
96
97
98
99
100
101
    const uint32_t n_rays,
    const int* packed_info,  // input ray & point indices.
    const scalar_t* starts,  // input start t
    const scalar_t* ends,  // input end t
    const scalar_t* sigmas,  // input density after activation
    const scalar_t* weights,  // forward output
    const scalar_t* grad_weights,  // input
    scalar_t* grad_sigmas  // output
) {
Ruilong Li's avatar
Ruilong Li committed
102
    CUDA_GET_THREAD_ID(i, n_rays);
Ruilong Li's avatar
cleanup  
Ruilong Li committed
103
104

    // locate
Ruilong Li's avatar
Ruilong Li committed
105
106
107
    const int base = packed_info[i * 2 + 0];  // point idx start.
    const int steps = packed_info[i * 2 + 1];  // point idx shift.
    if (steps == 0) return;
Ruilong Li's avatar
cleanup  
Ruilong Li committed
108
109
110
111
112
113
114
115
116

    starts += base;
    ends += base;
    sigmas += base;
    weights += base;
    grad_weights += base;
    grad_sigmas += base;

    scalar_t accum = 0;
Ruilong Li's avatar
Ruilong Li committed
117
    for (int j = 0; j < steps; ++j) {
Ruilong Li's avatar
cleanup  
Ruilong Li committed
118
119
120
121
122
123
        accum += grad_weights[j] * weights[j];
    }

    // backward of accumulated rendering
    scalar_t T = 1.f;
    scalar_t EPSILON = 1e-4f;
Ruilong Li's avatar
Ruilong Li committed
124
    for (int j = 0; j < steps; ++j) {
Ruilong Li's avatar
cleanup  
Ruilong Li committed
125
126
127
128
129
130
131
132
133
134
135
136
137
        if (T < EPSILON) {
            break;
        }
        const scalar_t delta = ends[j] - starts[j];
        const scalar_t alpha = 1.f - __expf(-sigmas[j] * delta);

        grad_sigmas[j] = delta * (grad_weights[j] * T - accum);
        accum -= grad_weights[j] * weights[j];
        T *= (1.f - alpha);
    }
}


Ruilong Li's avatar
cleanup  
Ruilong Li committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
std::vector<torch::Tensor> volumetric_rendering_steps(
    torch::Tensor packed_info, 
    torch::Tensor starts, 
    torch::Tensor ends, 
    torch::Tensor sigmas
) {
    DEVICE_GUARD(packed_info);

    CHECK_INPUT(packed_info);
    CHECK_INPUT(starts);
    CHECK_INPUT(ends);
    CHECK_INPUT(sigmas);
    
    TORCH_CHECK(packed_info.ndimension() == 2 & packed_info.size(1) == 2);
    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);

    const uint32_t n_rays = packed_info.size(0);
    const uint32_t n_samples = sigmas.size(0);

    const int threads = 256;
    const int blocks = CUDA_N_BLOCKS_NEEDED(n_rays, threads);

    torch::Tensor num_steps = torch::zeros({n_rays}, packed_info.options());
    torch::Tensor selector = torch::zeros({n_samples}, packed_info.options().dtype(torch::kBool));

    AT_DISPATCH_FLOATING_TYPES_AND_HALF(
        sigmas.scalar_type(),
Ruilong Li's avatar
Ruilong Li committed
167
        "volumetric_marching_steps",
Ruilong Li's avatar
cleanup  
Ruilong Li committed
168
        ([&]
Ruilong Li's avatar
steam  
Ruilong Li committed
169
         { volumetric_rendering_steps_kernel<scalar_t><<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
                n_rays,
                packed_info.data_ptr<int>(), 
                starts.data_ptr<scalar_t>(),
                ends.data_ptr<scalar_t>(),
                sigmas.data_ptr<scalar_t>(),
                num_steps.data_ptr<int>(),
                selector.data_ptr<bool>()
            ); 
        }));

    torch::Tensor cum_steps = num_steps.cumsum(0, torch::kInt32);
    torch::Tensor compact_packed_info = torch::stack({cum_steps - num_steps, num_steps}, 1);

    return {compact_packed_info, selector};
}


Ruilong Li's avatar
Ruilong Li committed
187
std::vector<torch::Tensor> volumetric_rendering_weights_forward(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
188
189
190
191
192
193
194
195
196
197
    torch::Tensor packed_info, 
    torch::Tensor starts, 
    torch::Tensor ends, 
    torch::Tensor sigmas
) {
    DEVICE_GUARD(packed_info);
    CHECK_INPUT(packed_info);
    CHECK_INPUT(starts);
    CHECK_INPUT(ends);
    CHECK_INPUT(sigmas);
Ruilong Li's avatar
Ruilong Li committed
198
    TORCH_CHECK(packed_info.ndimension() == 2 & packed_info.size(1) == 2);
Ruilong Li's avatar
cleanup  
Ruilong Li committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    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);

    const uint32_t n_rays = packed_info.size(0);
    const uint32_t n_samples = sigmas.size(0);

    const int threads = 256;
    const int blocks = CUDA_N_BLOCKS_NEEDED(n_rays, threads);

    // outputs
    torch::Tensor weights = torch::zeros({n_samples}, sigmas.options()); 
    torch::Tensor ray_indices = torch::zeros({n_samples}, packed_info.options()); 

    AT_DISPATCH_FLOATING_TYPES_AND_HALF(
        sigmas.scalar_type(),
Ruilong Li's avatar
Ruilong Li committed
215
        "volumetric_rendering_weights_forward",
Ruilong Li's avatar
cleanup  
Ruilong Li committed
216
        ([&]
Ruilong Li's avatar
steam  
Ruilong Li committed
217
         { volumetric_rendering_weights_forward_kernel<scalar_t><<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
218
219
220
221
222
223
                n_rays,
                packed_info.data_ptr<int>(), 
                starts.data_ptr<scalar_t>(),
                ends.data_ptr<scalar_t>(),
                sigmas.data_ptr<scalar_t>(),
                weights.data_ptr<scalar_t>(),
Ruilong Li's avatar
Ruilong Li committed
224
                ray_indices.data_ptr<int>()
Ruilong Li's avatar
cleanup  
Ruilong Li committed
225
226
227
            ); 
        }));

Ruilong Li's avatar
Ruilong Li committed
228
    return {weights, ray_indices};
Ruilong Li's avatar
cleanup  
Ruilong Li committed
229
230
231
}


Ruilong Li's avatar
Ruilong Li committed
232
torch::Tensor volumetric_rendering_weights_backward(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    torch::Tensor weights, 
    torch::Tensor grad_weights, 
    torch::Tensor packed_info, 
    torch::Tensor starts, 
    torch::Tensor ends, 
    torch::Tensor sigmas
) {
    DEVICE_GUARD(packed_info);
    const uint32_t n_rays = packed_info.size(0);
    const uint32_t n_samples = sigmas.size(0);

    const int threads = 256;
    const int blocks = CUDA_N_BLOCKS_NEEDED(n_rays, threads);

    // outputs
    torch::Tensor grad_sigmas = torch::zeros(sigmas.sizes(), sigmas.options()); 

    AT_DISPATCH_FLOATING_TYPES_AND_HALF(
        sigmas.scalar_type(),
Ruilong Li's avatar
Ruilong Li committed
252
        "volumetric_rendering_weights_backward",
Ruilong Li's avatar
cleanup  
Ruilong Li committed
253
        ([&]
Ruilong Li's avatar
steam  
Ruilong Li committed
254
         { volumetric_rendering_weights_backward_kernel<scalar_t><<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
255
256
257
258
259
260
261
262
263
264
265
266
267
                n_rays,
                packed_info.data_ptr<int>(), 
                starts.data_ptr<scalar_t>(),
                ends.data_ptr<scalar_t>(),
                sigmas.data_ptr<scalar_t>(),
                weights.data_ptr<scalar_t>(),
                grad_weights.data_ptr<scalar_t>(),
                grad_sigmas.data_ptr<scalar_t>()
            ); 
        }));

    return grad_sigmas;
}