forked from bycloudai/instant-ngp-Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnerf_network.h
665 lines (550 loc) · 23.9 KB
/
nerf_network.h
1
2
3
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
* Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
/** @file nerf_network.h
* @author Thomas Müller, NVIDIA
* @brief A network that first processes 3D position to density and
* subsequently direction to color.
*/
#pragma once
#include <tiny-cuda-nn/common.h>
#include <tiny-cuda-nn/encoding.h>
#include <tiny-cuda-nn/gpu_matrix.h>
#include <tiny-cuda-nn/gpu_memory.h>
#include <tiny-cuda-nn/multi_stream.h>
#include <tiny-cuda-nn/network.h>
#include <tiny-cuda-nn/network_with_input_encoding.h>
NGP_NAMESPACE_BEGIN
template <typename T>
__global__ void split_inputs_nerf(
const uint32_t n_elements,
const uint32_t n_partition_1_dims,
const uint32_t n_partition_2_dims,
const uint32_t stride,
const T* __restrict__ inputs,
T* __restrict__ partition_1,
T* __restrict__ partition_2
) {
const uint32_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i >= n_elements) return;
const uint32_t elem_idx = i / stride;
const uint32_t dim_idx = i - elem_idx * stride;
T value = inputs[i];
if (dim_idx < n_partition_1_dims) {
partition_1[elem_idx * n_partition_1_dims + dim_idx] = value;
} else {
partition_2[elem_idx * n_partition_2_dims + dim_idx - n_partition_1_dims] = value;
}
}
template <typename T>
__global__ void grab_density_network_output(
const uint32_t n_elements,
const uint32_t n_partition_1_dims,
const uint32_t stride,
const T* __restrict__ partition_1,
T* __restrict__ inputs
) {
const uint32_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i >= n_elements) return;
const uint32_t elem_idx = i / n_partition_1_dims;
const uint32_t dim_idx = i - elem_idx * n_partition_1_dims;
// #define RELU_NON_DENSITY
#ifdef RELU_NON_DENSITY
// ReLU for non-density dims, i.e. dim_idx > 0
inputs[elem_idx * stride + dim_idx] = (dim_idx == 0 || partition_1[i] > (T)0.0f) ? partition_1[i] : (T)0.0f;
#else
// ReLU for non-density dims, i.e. dim_idx > 0
inputs[elem_idx * stride + dim_idx] = partition_1[i];
#endif
}
template <typename T>
__global__ void assemble_rgbd(
const uint32_t n_elements,
const uint32_t density_stride,
const uint32_t rgb_stride,
const T* __restrict__ density,
const T* __restrict__ rgb,
T* __restrict__ rgbd
) {
const uint32_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i >= n_elements) return;
const uint32_t elem_idx = i / 4;
const uint32_t dim_idx = i - elem_idx * 4;
rgbd[i] = dim_idx < 3 ? rgb[elem_idx*rgb_stride + dim_idx] : density[elem_idx*density_stride];
}
template <typename T>
__global__ void extract_rgb(
const uint32_t n_elements,
const uint32_t rgb_stride,
const T* __restrict__ rgbd,
T* __restrict__ rgb
) {
const uint32_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i >= n_elements) return;
const uint32_t elem_idx = i / 3;
const uint32_t dim_idx = i - elem_idx * 3;
rgb[elem_idx*rgb_stride + dim_idx] = rgbd[elem_idx*4 + dim_idx];
}
template <typename T>
__global__ void extract_density_gradient(
const uint32_t n_elements,
const uint32_t width,
const uint32_t stride,
const T* __restrict__ rgbd,
const T* __restrict__ rgb_in,
const T* __restrict__ rgb_in_grad,
T* __restrict__ density_out
) {
const uint32_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i >= n_elements) return;
const uint32_t elem_idx = i / width;
const uint32_t dim_idx = i - elem_idx * width;
#ifdef RELU_NON_DENSITY
// Backprop through ReLU if not the density dimension.
float value = (dim_idx == 0 || rgb_in[i] > (T)0.0f) ? rgb_in_grad[elem_idx*stride + dim_idx] : (T)0.0f;
#else
float value = rgb_in_grad[elem_idx*stride + dim_idx];
#endif
if (dim_idx == 0) {
value += (float)rgbd[elem_idx*4+3]; // Add density gradient
}
density_out[i] = (T)value;
}
template <typename T>
__global__ void extract_dir_gradient(
const uint32_t n_elements,
const uint32_t offset,
const uint32_t width,
const uint32_t stride,
const T* __restrict__ rgb_in,
T* __restrict__ density_out
) {
const uint32_t i = threadIdx.x + blockIdx.x * blockDim.x;
if (i >= n_elements) return;
const uint32_t elem_idx = i / width;
const uint32_t dim_idx = i - elem_idx * width;
density_out[i] = rgb_in[offset + elem_idx*stride + dim_idx];
}
template <typename T>
class NerfNetwork : public tcnn::Network<float, T> {
public:
using json = nlohmann::json;
NerfNetwork(uint32_t n_pos_dims, uint32_t n_dir_dims, uint32_t dir_offset, const json& pos_encoding, const json& dir_encoding, const json& density_network, const json& rgb_network) : m_n_pos_dims{n_pos_dims}, m_n_dir_dims{n_dir_dims}, m_dir_offset{dir_offset} {
m_pos_encoding.reset(tcnn::create_encoding<T>(n_pos_dims, pos_encoding, density_network.contains("otype") && (tcnn::equals_case_insensitive(density_network["otype"], "FullyFusedMLP") || tcnn::equals_case_insensitive(density_network["otype"], "MegakernelMLP")) ? 16u : 8u));
uint32_t rgb_alignment = rgb_network.contains("otype") && (tcnn::equals_case_insensitive(rgb_network["otype"], "FullyFusedMLP") || tcnn::equals_case_insensitive(rgb_network["otype"], "MegakernelMLP")) ? 16u : 8u;
m_dir_encoding.reset(tcnn::create_encoding<T>(n_dir_dims, dir_encoding, rgb_alignment));
json local_density_network_config = density_network;
local_density_network_config["n_input_dims"] = m_pos_encoding->num_encoded_dims();
if (!density_network.contains("n_output_dims")) {
local_density_network_config["n_output_dims"] = 16;
}
m_density_network.reset(tcnn::create_network<T>(local_density_network_config));
m_rgb_network_input_width = tcnn::next_multiple(m_dir_encoding->num_encoded_dims() + m_density_network->padded_output_width() - 1, rgb_alignment);
json local_rgb_network_config = rgb_network;
local_rgb_network_config["n_input_dims"] = m_rgb_network_input_width;
local_rgb_network_config["n_output_dims"] = 3;
m_rgb_network.reset(tcnn::create_network<T>(local_rgb_network_config));
}
virtual ~NerfNetwork() { }
void inference(cudaStream_t stream, const tcnn::GPUMatrix<float>& input, tcnn::GPUMatrix<float>& output) override {
// Make sure our teporary buffers have the correct size for the given batch size
uint32_t batch_size = input.n();
if (m_inference_density_network_input.n() != batch_size) {
allocate_inference_buffers(batch_size);
}
inference_mixed_precision(stream, input, m_inference_network_output);
tcnn::linear_kernel(tcnn::cast_from<T>, 0, stream, m_inference_network_output.n_elements(), m_inference_network_output.data(), output.data());
}
void inference_mixed_precision(cudaStream_t stream, const tcnn::GPUMatrix<float>& input, tcnn::GPUMatrixDynamic<T>& output, bool use_inference_matrices = true) {
if (output.layout() != tcnn::CM) {
throw std::runtime_error("NerfNetwork::inference_mixed_precision output must be in column major format.");
}
// Make sure our teporary buffers have the correct size for the given batch size
uint32_t batch_size = input.n();
if (m_inference_density_network_input.n() != batch_size) {
allocate_inference_buffers(batch_size);
}
// Perform directional encoding and density network query in parallel
{
tcnn::SyncedMultiStream synced_streams{stream, 2};
m_pos_encoding->encode(
synced_streams.get(0),
batch_size,
{input.data(), input.m()},
{m_inference_density_network_input.data(), m_inference_density_network_input.m()},
nullptr,
use_inference_matrices
);
m_dir_encoding->encode(
synced_streams.get(1),
batch_size,
{input.data() + m_dir_offset, input.m()},
{m_inference_rgb_network_input.data() + m_inference_density_network_output.m(), m_inference_rgb_network_input.m()},
nullptr,
use_inference_matrices
);
m_density_network->inference_mixed_precision(synced_streams.get(0), m_inference_density_network_input, m_inference_density_network_output, use_inference_matrices);
tcnn::linear_kernel(grab_density_network_output<T>, 0, synced_streams.get(0),
m_inference_density_network_output.n_elements(), m_inference_density_network_output.m(), m_inference_rgb_network_input.m() /* stride */, m_inference_density_network_output.data(), m_inference_rgb_network_input.data()
);
}
m_rgb_network->inference_mixed_precision(stream, m_inference_rgb_network_input, m_inference_rgb_network_output, use_inference_matrices);
tcnn::linear_kernel(assemble_rgbd<T>, 0, stream,
output.n_elements(), m_inference_density_network_output.m(), m_inference_rgb_network_output.m(), m_inference_density_network_output.data(), m_inference_rgb_network_output.data(), output.data()
);
}
void density(cudaStream_t stream, const tcnn::PitchedPtr<const float>& input, tcnn::GPUMatrixDynamic<T>& output, bool use_inference_matrices = true) {
// Make sure our temporary buffers have the correct size for the given batch size
uint32_t batch_size = output.n();
if (m_inference_density_network_input.n() != batch_size) {
allocate_inference_buffers(batch_size);
}
m_pos_encoding->encode(
stream,
batch_size,
input,
{m_inference_density_network_input.data(), m_inference_density_network_input.m()},
nullptr,
use_inference_matrices
);
m_density_network->inference_mixed_precision(stream, m_inference_density_network_input, output, use_inference_matrices);
}
void density(cudaStream_t stream, const tcnn::GPUMatrix<float>& input, tcnn::GPUMatrixDynamic<T>& output, bool use_inference_matrices = true) {
density(stream, {input.data(), input.m()}, output, use_inference_matrices);
}
uint32_t padded_density_output_width() const {
return m_density_network->padded_output_width();
}
uint32_t num_encoded_dims() const {
return m_pos_encoding->num_encoded_dims();
}
void forward(cudaStream_t stream, const tcnn::GPUMatrix<float>& input, tcnn::GPUMatrixDynamic<T>* output = nullptr, bool use_inference_matrices = false, bool prepare_input_gradients = false) override {
if (output && output->layout() != tcnn::CM) {
throw std::runtime_error("NerfNetwork::forward output must be in column major format.");
}
// Make sure our temporary buffers have the correct size for the given batch size
uint32_t batch_size = input.n();
if (m_forward_density_network_input.n() != batch_size) {
allocate_forward_buffers(batch_size);
}
// Perform directional encoding and density network query in parallel
{
tcnn::SyncedMultiStream synced_streams{stream, 2};
m_pos_encoding->encode(
synced_streams.get(0),
batch_size,
{input.data(), input.m()},
{m_forward_density_network_input.data(), m_forward_density_network_input.m()},
prepare_input_gradients ? m_forward_pos_encoding_forward_gradient.data() : nullptr,
use_inference_matrices
);
m_dir_encoding->encode(
synced_streams.get(1),
batch_size,
{input.data() + m_dir_offset, input.m()},
{m_forward_rgb_network_input.data() + m_forward_density_network_output.m(), m_forward_rgb_network_input.m()},
prepare_input_gradients ? m_forward_dir_encoding_forward_gradient.data() : nullptr,
use_inference_matrices
);
m_density_network->forward(synced_streams.get(0), m_forward_density_network_input, &m_forward_density_network_output, use_inference_matrices, prepare_input_gradients);
tcnn::linear_kernel(grab_density_network_output<T>, 0, synced_streams.get(0),
m_forward_density_network_output.n_elements(), m_forward_density_network_output.m(), m_forward_rgb_network_input.m() /* stride */, m_forward_density_network_output.data(), m_forward_rgb_network_input.data()
);
}
m_rgb_network->forward(stream, m_forward_rgb_network_input, &m_forward_rgb_network_output, use_inference_matrices, prepare_input_gradients);
if (output) {
tcnn::linear_kernel(assemble_rgbd<T>, 0, stream,
output->n_elements(), m_forward_density_network_output.m(), m_forward_rgb_network_output.m(), m_forward_density_network_output.data(), m_forward_rgb_network_output.data(), output->data()
);
}
}
void backward(
cudaStream_t stream,
const tcnn::GPUMatrix<float>& input,
const tcnn::GPUMatrixDynamic<T>& output,
const tcnn::GPUMatrixDynamic<T>& dL_doutput,
tcnn::GPUMatrix<float>* dL_dinput = nullptr,
bool use_inference_matrices = false,
bool compute_param_gradients = true
) override {
if (output.layout() != tcnn::CM || dL_doutput.layout() != tcnn::CM) {
throw std::runtime_error("NerfNetwork::backward output must be in column major format.");
}
// Make sure our teporary buffers have the correct size for the given batch size
uint32_t batch_size = input.n();
if (m_backward_dL_ddensity_network_output.n() != batch_size) {
allocate_backward_buffers(batch_size);
}
tcnn::linear_kernel(extract_rgb<T>, 0, stream,
batch_size*3, m_backward_dL_drgb.m(), dL_doutput.data(), m_backward_dL_drgb.data()
);
m_rgb_network->backward(stream, m_forward_rgb_network_input, m_forward_rgb_network_output, m_backward_dL_drgb, &m_backward_dL_drgb_network_input, use_inference_matrices, compute_param_gradients);
// Backprop through dir encoding if it is trainable or if we need input gradients
tcnn::SyncedMultiStream synced_streams{stream, 2};
if (m_dir_encoding->n_params() > 0 || dL_dinput) {
tcnn::linear_kernel(extract_dir_gradient<T>, 0, synced_streams.get(1),
m_backward_dL_ddir_encoding_output.n_elements(), m_backward_dL_ddensity_network_output.m(), m_backward_dL_ddir_encoding_output.m(), m_backward_dL_drgb_network_input.m(), m_backward_dL_drgb_network_input.data(), m_backward_dL_ddir_encoding_output.data()
);
m_dir_encoding->backward(
synced_streams.get(1),
batch_size,
{m_backward_dL_ddir_encoding_output.data(), m_backward_dL_ddir_encoding_output.m()},
dL_dinput ? m_forward_dir_encoding_forward_gradient.data() : nullptr,
dL_dinput ? tcnn::PitchedPtr<float>{dL_dinput->data() + m_dir_offset, dL_dinput->m()} : tcnn::PitchedPtr<float>{},
{input.data() + m_dir_offset, input.m()}
);
}
tcnn::linear_kernel(extract_density_gradient<T>, 0, synced_streams.get(0),
m_backward_dL_ddensity_network_output.n_elements(),
m_backward_dL_ddensity_network_output.m(),
m_backward_dL_drgb_network_input.m(),
dL_doutput.data(),
m_forward_density_network_output.data(),
m_backward_dL_drgb_network_input.data(),
m_backward_dL_ddensity_network_output.data()
);
tcnn::GPUMatrix<T>* dL_ddensity_network_input = nullptr;
if (m_pos_encoding->n_params() > 0 || dL_dinput) {
dL_ddensity_network_input = &m_backward_dL_ddensity_network_input;
}
m_density_network->backward(synced_streams.get(0), m_forward_density_network_input, m_forward_density_network_output, m_backward_dL_ddensity_network_output, dL_ddensity_network_input, use_inference_matrices, compute_param_gradients);
// Backprop through pos encoding if it is trainable or if we need input gradients
if (dL_ddensity_network_input) {
m_pos_encoding->backward(
synced_streams.get(0),
batch_size,
{dL_ddensity_network_input->data(), dL_ddensity_network_input->m()},
dL_dinput ? m_forward_pos_encoding_forward_gradient.data() : nullptr,
dL_dinput ? tcnn::PitchedPtr<float>{dL_dinput->data(), dL_dinput->m()} : tcnn::PitchedPtr<float>{},
{input.data(), input.m()}
);
}
}
void set_params(T* params, T* inference_params, T* backward_params, T* gradients) override {
size_t offset = 0;
m_density_network->set_params(
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset
);
offset += m_density_network->n_params();
m_rgb_network->set_params(
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset
);
offset += m_rgb_network->n_params();
m_pos_encoding->set_params(
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset
);
offset += m_pos_encoding->n_params();
m_dir_encoding->set_params(
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset
);
offset += m_dir_encoding->n_params();
}
void initialize_params(tcnn::pcg32& rnd, float* params_full_precision, T* params, T* inference_params, T* backward_params, T* gradients, float scale = 1) override {
size_t offset = 0;
m_density_network->initialize_params(
rnd,
params_full_precision + offset,
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset,
scale
);
offset += m_density_network->n_params();
m_rgb_network->initialize_params(
rnd,
params_full_precision + offset,
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset,
scale
);
offset += m_rgb_network->n_params();
m_pos_encoding->initialize_params(
rnd,
params_full_precision + offset,
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset,
scale
);
offset += m_pos_encoding->n_params();
m_dir_encoding->initialize_params(
rnd,
params_full_precision + offset,
params + offset,
inference_params + offset,
backward_params + offset,
gradients + offset,
scale
);
offset += m_dir_encoding->n_params();
}
size_t n_params() const override {
return m_pos_encoding->n_params() + m_density_network->n_params() + m_dir_encoding->n_params() + m_rgb_network->n_params();
}
uint32_t padded_output_width() const override {
return 4;
}
uint32_t output_width() const override {
return 4;
}
uint32_t required_input_alignment() const override {
return 1; // No alignment required due to encoding
}
std::vector<std::pair<uint32_t, uint32_t>> layer_sizes() const override {
auto layers = m_density_network->layer_sizes();
auto rgb_layers = m_rgb_network->layer_sizes();
layers.insert(layers.end(), rgb_layers.begin(), rgb_layers.end());
return layers;
}
uint32_t width(uint32_t layer) const override {
if (layer == 0) {
return m_forward_density_network_input.m();
} else if (layer < m_density_network->num_forward_activations() + 1) {
return m_density_network->width(layer - 1);
} else if (layer == m_density_network->num_forward_activations() + 1) {
return m_forward_rgb_network_input.m();
} else {
return m_rgb_network->width(layer - 2 - m_density_network->num_forward_activations());
}
}
uint32_t num_forward_activations() const override {
return m_density_network->num_forward_activations() + m_rgb_network->num_forward_activations() + 2;
}
const T* forward_activations(uint32_t layer) const override {
if (layer == 0) {
return m_forward_density_network_input.data();
} else if (layer < m_density_network->num_forward_activations() + 1) {
return m_density_network->forward_activations(layer - 1);
} else if (layer == m_density_network->num_forward_activations() + 1) {
return m_forward_rgb_network_input.data();
} else {
return m_rgb_network->forward_activations(layer - 2 - m_density_network->num_forward_activations());
}
}
const std::shared_ptr<tcnn::Encoding<T>>& encoding() const {
return m_pos_encoding;
}
const std::shared_ptr<tcnn::Encoding<T>>& dir_encoding() const {
return m_dir_encoding;
}
private:
void allocate_inference_buffers(uint32_t batch_size) {
m_inference_density_network_input.set_size(m_pos_encoding->num_encoded_dims(), batch_size);
m_inference_density_network_output.set_size(m_density_network->padded_output_width(), batch_size);
m_inference_rgb_network_input.set_size(m_rgb_network_input_width, batch_size);
m_inference_rgb_network_output.set_size(m_rgb_network->padded_output_width(), batch_size);
m_inference_network_output.set_size(4, batch_size);
tcnn::GPUMatrixBase::allocate_shared_memory(
m_inference_buffer,
{
&m_inference_density_network_input,
&m_inference_density_network_output,
&m_inference_rgb_network_input,
&m_inference_rgb_network_output,
&m_inference_network_output,
}
);
CUDA_CHECK_THROW(cudaMemset(m_inference_rgb_network_input.data(), 0, m_inference_rgb_network_input.n_bytes()));
}
void allocate_forward_buffers(uint32_t batch_size) {
m_forward_density_network_input.set_size(m_pos_encoding->num_encoded_dims(), batch_size);
m_forward_density_network_output.set_size(m_density_network->padded_output_width(), batch_size);
m_forward_rgb_network_input.set_size(m_rgb_network_input_width, batch_size);
m_forward_rgb_network_output.set_size(m_rgb_network->padded_output_width(), batch_size);
m_forward_network_output.set_size(4, batch_size);
m_forward_pos_encoding_forward_gradient.set_size(m_pos_encoding->num_forward_gradient_dims(), batch_size);
m_forward_dir_encoding_forward_gradient.set_size(m_dir_encoding->num_forward_gradient_dims(), batch_size);
tcnn::GPUMatrixBase::allocate_shared_memory(
m_forward_buffer,
{
&m_forward_density_network_input,
&m_forward_density_network_output,
&m_forward_rgb_network_input,
&m_forward_rgb_network_output,
&m_forward_network_output,
&m_forward_pos_encoding_forward_gradient,
&m_forward_dir_encoding_forward_gradient,
}
);
CUDA_CHECK_THROW(cudaMemset(m_forward_rgb_network_input.data(), 0, m_forward_rgb_network_input.n_bytes()));
}
void allocate_backward_buffers(uint32_t batch_size) {
m_backward_dL_drgb.set_size(m_rgb_network->padded_output_width(), batch_size);
m_backward_dL_drgb_network_input.set_size(m_rgb_network_input_width, batch_size);
m_backward_dL_ddensity_network_output.set_size(m_density_network->padded_output_width(), batch_size);
m_backward_dL_ddir_encoding_output.set_size(m_dir_encoding->num_encoded_dims(), batch_size);
m_backward_dL_ddensity_network_input.set_size(m_pos_encoding->num_encoded_dims(), batch_size);
m_backward_dL_dpos.set_size(m_n_pos_dims, batch_size);
m_backward_dL_ddir.set_size(m_n_dir_dims, batch_size);
tcnn::GPUMatrixBase::allocate_shared_memory(
m_backward_buffer,
{
&m_backward_dL_drgb,
&m_backward_dL_drgb_network_input,
&m_backward_dL_ddensity_network_output,
&m_backward_dL_ddir_encoding_output,
&m_backward_dL_ddensity_network_input,
&m_backward_dL_dpos,
&m_backward_dL_ddir,
}
);
CUDA_CHECK_THROW(cudaMemset(m_backward_dL_drgb.data(), 0, m_backward_dL_drgb.n_bytes()));
}
private:
std::unique_ptr<tcnn::Network<T>> m_density_network;
std::unique_ptr<tcnn::Network<T>> m_rgb_network;
std::shared_ptr<tcnn::Encoding<T>> m_pos_encoding;
std::shared_ptr<tcnn::Encoding<T>> m_dir_encoding;
uint32_t m_rgb_network_input_width;
uint32_t m_n_pos_dims;
uint32_t m_n_dir_dims;
uint32_t m_dir_offset;
// Temporary buffers to hold inference data
tcnn::GPUMemory<char> m_inference_buffer;
tcnn::GPUMatrix<T> m_inference_density_network_input;
tcnn::GPUMatrix<T> m_inference_density_network_output;
tcnn::GPUMatrix<T> m_inference_rgb_network_input;
tcnn::GPUMatrix<T> m_inference_rgb_network_output;
tcnn::GPUMatrix<T> m_inference_network_output;
// Temporary buffers to hold forward data
tcnn::GPUMemory<char> m_forward_buffer;
tcnn::GPUMatrix<T> m_forward_density_network_input;
tcnn::GPUMatrix<T> m_forward_density_network_output;
tcnn::GPUMatrix<T> m_forward_rgb_network_input;
tcnn::GPUMatrix<T> m_forward_rgb_network_output;
tcnn::GPUMatrix<T> m_forward_network_output; // Only needed when visualizing
tcnn::GPUMatrix<float> m_forward_pos_encoding_forward_gradient; // Only needed when computing input gradients
tcnn::GPUMatrix<float> m_forward_dir_encoding_forward_gradient; // Only needed when computing input gradients
// Temporary buffers to hold backward data
tcnn::GPUMemory<char> m_backward_buffer;
tcnn::GPUMatrix<T> m_backward_dL_drgb;
tcnn::GPUMatrix<T> m_backward_dL_drgb_network_input;
tcnn::GPUMatrix<T> m_backward_dL_ddensity_network_output;
tcnn::GPUMatrix<T> m_backward_dL_ddir_encoding_output; // Only needed when training the dir encoding or computing input gradients
tcnn::GPUMatrix<T> m_backward_dL_ddensity_network_input;
tcnn::GPUMatrix<float> m_backward_dL_dpos; // Only needed when computing input gradients
tcnn::GPUMatrix<float> m_backward_dL_ddir; // Only needed when computing input gradients
};
NGP_NAMESPACE_END