forked from pierotofy/OpenSplat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasterize_gaussians.cpp
235 lines (194 loc) · 8.02 KB
/
rasterize_gaussians.cpp
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
#include "rasterize_gaussians.hpp"
#include "gsplat.hpp"
#if defined(USE_HIP) || defined(USE_CUDA) || defined(USE_MPS)
std::tuple<torch::Tensor,
torch::Tensor,
torch::Tensor,
torch::Tensor,
torch::Tensor> binAndSortGaussians(int numPoints, int numIntersects,
torch::Tensor xys,
torch::Tensor depths,
torch::Tensor radii,
torch::Tensor cumTilesHit,
TileBounds tileBounds){
auto t = map_gaussian_to_intersects_tensor(numPoints, numIntersects,
xys, depths, radii, cumTilesHit, tileBounds);
// unique IDs for each gaussian in the form (tile | depth id)
torch::Tensor isectIds = std::get<0>(t);
// Tensor that maps isect_ids back to cumHitTiles
torch::Tensor gaussianIds = std::get<1>(t);
auto sorted = torch::sort(isectIds);
// sorted unique IDs for each gaussian in the form (tile | depth id)
torch::Tensor isectIdsSorted = std::get<0>(sorted);
torch::Tensor sortedIndices = std::get<1>(sorted);
// sorted Tensor that maps isect_ids back to cumHitTiles
torch::Tensor gaussianIdsSorted = torch::gather(gaussianIds, 0, sortedIndices);
// range of gaussians hit per tile
torch::Tensor tileBins = get_tile_bin_edges_tensor(numIntersects, isectIdsSorted);
return std::make_tuple(isectIds, gaussianIds, isectIdsSorted, gaussianIdsSorted, tileBins);
}
torch::Tensor RasterizeGaussians::forward(AutogradContext *ctx,
torch::Tensor xys,
torch::Tensor depths,
torch::Tensor radii,
torch::Tensor conics,
torch::Tensor numTilesHit,
torch::Tensor colors,
torch::Tensor opacity,
int imgHeight,
int imgWidth,
torch::Tensor background
){
int numPoints = xys.size(0);
TileBounds tileBounds = std::make_tuple(
(imgWidth + BLOCK_X - 1) / BLOCK_X,
(imgHeight + BLOCK_Y - 1) / BLOCK_Y,
1
);
std::tuple<int, int, int> block = std::make_tuple(BLOCK_X, BLOCK_Y, 1);
std::tuple<int, int, int> imgSize = std::make_tuple(imgWidth, imgHeight, 1);
torch::Tensor cumTilesHit = torch::cumsum(numTilesHit, 0, torch::kInt32);
int numIntersects = cumTilesHit[cumTilesHit.size(0) - 1].item<int>();
auto b = binAndSortGaussians(numPoints, numIntersects, xys, depths, radii, cumTilesHit, tileBounds);
torch::Tensor gaussianIdsSorted = std::get<3>(b);
torch::Tensor tileBins = std::get<4>(b);
auto t = rasterize_forward_tensor(tileBounds, block, imgSize,
gaussianIdsSorted,
tileBins,
xys,
conics,
colors,
opacity,
background);
// Final image
torch::Tensor outImg = std::get<0>(t);
torch::Tensor finalTs = std::get<1>(t);
// Map of tile bin IDs
torch::Tensor finalIdx = std::get<2>(t);
ctx->saved_data["imgWidth"] = imgWidth;
ctx->saved_data["imgHeight"] = imgHeight;
ctx->save_for_backward({ gaussianIdsSorted, tileBins, xys, conics, colors, opacity, background, finalTs, finalIdx });
return outImg;
}
tensor_list RasterizeGaussians::backward(AutogradContext *ctx, tensor_list grad_outputs) {
torch::Tensor v_outImg = grad_outputs[0];
int imgHeight = ctx->saved_data["imgHeight"].toInt();
int imgWidth = ctx->saved_data["imgWidth"].toInt();
variable_list saved = ctx->get_saved_variables();
torch::Tensor gaussianIdsSorted = saved[0];
torch::Tensor tileBins = saved[1];
torch::Tensor xys = saved[2];
torch::Tensor conics = saved[3];
torch::Tensor colors = saved[4];
torch::Tensor opacity = saved[5];
torch::Tensor background = saved[6];
torch::Tensor finalTs = saved[7];
torch::Tensor finalIdx = saved[8];
torch::Tensor v_outAlpha = torch::zeros_like(v_outImg.index({"...", 0}));
auto t = rasterize_backward_tensor(imgHeight, imgWidth,
gaussianIdsSorted,
tileBins,
xys,
conics,
colors,
opacity,
background,
finalTs,
finalIdx,
v_outImg,
v_outAlpha);
torch::Tensor v_xy = std::get<0>(t);
torch::Tensor v_conic = std::get<1>(t);
torch::Tensor v_colors = std::get<2>(t);
torch::Tensor v_opacity = std::get<3>(t);
torch::Tensor none;
return { v_xy,
none, // depths
none, // radii
v_conic,
none, // numTilesHit
v_colors,
v_opacity,
none, // imgHeight
none, // imgWidth
none // background
};
}
#endif
torch::Tensor RasterizeGaussiansCPU::forward(AutogradContext *ctx,
torch::Tensor xys,
torch::Tensor radii,
torch::Tensor conics,
torch::Tensor colors,
torch::Tensor opacity,
torch::Tensor cov2d,
torch::Tensor camDepths,
int imgHeight,
int imgWidth,
torch::Tensor background
){
int numPoints = xys.size(0);
auto t = rasterize_forward_tensor_cpu(imgWidth, imgHeight,
xys,
conics,
colors,
opacity,
background,
cov2d,
camDepths
);
// Final image
torch::Tensor outImg = std::get<0>(t);
torch::Tensor finalTs = std::get<1>(t);
std::vector<int32_t> *px2gid = std::get<2>(t);
ctx->saved_data["px2gid"] = reinterpret_cast<int64_t>(px2gid);
ctx->saved_data["imgWidth"] = imgWidth;
ctx->saved_data["imgHeight"] = imgHeight;
ctx->save_for_backward({ xys, conics, colors, opacity, background, cov2d, camDepths, finalTs });
return outImg;
}
tensor_list RasterizeGaussiansCPU::backward(AutogradContext *ctx, tensor_list grad_outputs) {
torch::Tensor v_outImg = grad_outputs[0];
int imgHeight = ctx->saved_data["imgHeight"].toInt();
int imgWidth = ctx->saved_data["imgWidth"].toInt();
const std::vector<int32_t> *px2gid = reinterpret_cast<const std::vector<int32_t> *>(ctx->saved_data["px2gid"].toInt());
variable_list saved = ctx->get_saved_variables();
torch::Tensor xys = saved[0];
torch::Tensor conics = saved[1];
torch::Tensor colors = saved[2];
torch::Tensor opacity = saved[3];
torch::Tensor background = saved[4];
torch::Tensor cov2d = saved[5];
torch::Tensor camDepths = saved[6];
torch::Tensor finalTs = saved[7];
torch::Tensor v_outAlpha = torch::zeros_like(v_outImg.index({"...", 0}));
auto t = rasterize_backward_tensor_cpu(imgHeight, imgWidth,
xys,
conics,
colors,
opacity,
background,
cov2d,
camDepths,
finalTs,
px2gid,
v_outImg,
v_outAlpha);
delete[] px2gid;
torch::Tensor v_xy = std::get<0>(t);
torch::Tensor v_conic = std::get<1>(t);
torch::Tensor v_colors = std::get<2>(t);
torch::Tensor v_opacity = std::get<3>(t);
torch::Tensor none;
return { v_xy,
none, // radii
v_conic,
v_colors,
v_opacity,
none, // cov2d
none, // camDepths
none, // imgHeight
none, // imgWidth
none // background
};
}