volumetric_rendering.cu 8.23 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
56
57
58
    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
    int* samples_ray_ids, // output
    bool* mask  // output
) {
Ruilong Li's avatar
Ruilong Li committed
59
    CUDA_GET_THREAD_ID(i, n_rays);
Ruilong Li's avatar
cleanup  
Ruilong Li committed
60
61

    // locate
Ruilong Li's avatar
Ruilong Li committed
62
63
64
    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
65
66
67
68
69
70
71
72

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

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

    // accumulated rendering
    scalar_t T = 1.f;
    scalar_t EPSILON = 1e-4f;
Ruilong Li's avatar
Ruilong Li committed
80
    for (int j = 0; j < steps; ++j) {
Ruilong Li's avatar
cleanup  
Ruilong Li committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        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);
    }
    mask[0] = true;
}


template <typename scalar_t>
Ruilong Li's avatar
Ruilong Li committed
95
__global__ void volumetric_rendering_weights_backward_kernel(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
96
97
98
99
100
101
102
103
104
    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
105
    CUDA_GET_THREAD_ID(i, n_rays);
Ruilong Li's avatar
cleanup  
Ruilong Li committed
106
107

    // locate
Ruilong Li's avatar
Ruilong Li committed
108
109
110
    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
111
112
113
114
115
116
117
118
119

    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
120
    for (int j = 0; j < steps; ++j) {
Ruilong Li's avatar
cleanup  
Ruilong Li committed
121
122
123
124
125
126
        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
127
    for (int j = 0; j < steps; ++j) {
Ruilong Li's avatar
cleanup  
Ruilong Li committed
128
129
130
131
132
133
134
135
136
137
138
139
140
        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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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(),
        "volumetric_rendering_inference",
        ([&]
         { volumetric_rendering_steps_kernel<scalar_t><<<blocks, threads>>>(
                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
190
std::vector<torch::Tensor> volumetric_rendering_weights_forward(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
191
192
193
194
195
196
197
198
199
200
    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
201
    TORCH_CHECK(packed_info.ndimension() == 2 & packed_info.size(1) == 2);
Ruilong Li's avatar
cleanup  
Ruilong Li committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    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()); 
    // The rays that are not skipped during sampling.
    torch::Tensor mask = torch::zeros({n_rays}, sigmas.options().dtype(torch::kBool)); 

    AT_DISPATCH_FLOATING_TYPES_AND_HALF(
        sigmas.scalar_type(),
Ruilong Li's avatar
Ruilong Li committed
220
        "volumetric_rendering_weights_forward",
Ruilong Li's avatar
cleanup  
Ruilong Li committed
221
        ([&]
Ruilong Li's avatar
Ruilong Li committed
222
         { volumetric_rendering_weights_forward_kernel<scalar_t><<<blocks, threads>>>(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
                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>(),
                ray_indices.data_ptr<int>(),
                mask.data_ptr<bool>()
            ); 
        }));

    return {weights, ray_indices, mask};
}


Ruilong Li's avatar
Ruilong Li committed
238
torch::Tensor volumetric_rendering_weights_backward(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    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
258
        "volumetric_rendering_weights_backward",
Ruilong Li's avatar
cleanup  
Ruilong Li committed
259
        ([&]
Ruilong Li's avatar
Ruilong Li committed
260
         { volumetric_rendering_weights_backward_kernel<scalar_t><<<blocks, threads>>>(
Ruilong Li's avatar
cleanup  
Ruilong Li committed
261
262
263
264
265
266
267
268
269
270
271
272
273
                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;
}