-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rasterizer.py
More file actions
357 lines (279 loc) · 10.2 KB
/
test_rasterizer.py
File metadata and controls
357 lines (279 loc) · 10.2 KB
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
#!/usr/bin/env python3
"""
Test script for Intel XPU Gaussian Rasterization
"""
import torch
import numpy as np
import time
import sys
try:
import intel_diff_gaussian_rasterization as idgr
print("✓ Successfully imported intel_diff_gaussian_rasterization")
except ImportError as e:
print(f"✗ Failed to import: {e}")
sys.exit(1)
def create_test_gaussians(num_gaussians=1000, device='cpu'):
"""Create synthetic test Gaussians"""
torch.manual_seed(42)
# Random positions in a sphere
means3D = torch.randn(num_gaussians, 3, device=device, dtype=torch.float32) * 2.0
means2D = torch.randn(num_gaussians, 3, device=device, dtype=torch.float32) # Placeholder
# Random colors (RGB)
colors = torch.rand(num_gaussians, 3, device=device, dtype=torch.float32)
# Random opacities
opacities = torch.rand(num_gaussians, 1, device=device, dtype=torch.float32) * 0.9 + 0.1
# Random scales
scales = torch.rand(num_gaussians, 3, device=device, dtype=torch.float32) * 0.1 + 0.05
# Random rotations (quaternions)
rotations = torch.randn(num_gaussians, 4, device=device, dtype=torch.float32)
rotations = rotations / torch.norm(rotations, dim=1, keepdim=True)
return means3D, means2D, colors, opacities, scales, rotations
def create_camera_matrices(device='cpu'):
"""Create simple camera matrices"""
# View matrix (identity)
viewmatrix = torch.eye(4, device=device, dtype=torch.float32)
# Projection matrix (simple perspective)
fov = 0.8
aspect = 1.0
near = 0.1
far = 100.0
f = 1.0 / np.tan(fov / 2.0)
projmatrix = torch.tensor([
[f/aspect, 0, 0, 0],
[0, f, 0, 0],
[0, 0, (far+near)/(near-far), (2*far*near)/(near-far)],
[0, 0, -1, 0]
], device=device, dtype=torch.float32)
campos = torch.tensor([0.0, 0.0, 5.0], device=device, dtype=torch.float32)
return viewmatrix, projmatrix, campos
def test_basic_rendering():
"""Test basic rendering functionality"""
print("\n" + "="*60)
print("Test 1: Basic Rendering")
print("="*60)
device = 'cpu'
width, height = 512, 512
num_gaussians = 1000
# Create test data
print(f"Creating {num_gaussians} test Gaussians...")
means3D, means2D, colors, opacities, scales, rotations = \
create_test_gaussians(num_gaussians, device)
viewmatrix, projmatrix, campos = create_camera_matrices(device)
# Setup rasterization settings
bg_color = torch.tensor([0.0, 0.0, 0.0], device=device, dtype=torch.float32)
tanfovx = tanfovy = 0.5
raster_settings = idgr.GaussianRasterizationSettings(
image_height=height,
image_width=width,
tanfovx=tanfovx,
tanfovy=tanfovy,
bg=bg_color,
scale_modifier=1.0,
viewmatrix=viewmatrix,
projmatrix=projmatrix,
sh_degree=0,
campos=campos,
prefiltered=False,
debug=False
)
# Create rasterizer
rasterizer = idgr.GaussianRasterizer(raster_settings)
# Render
print("Rendering...")
start_time = time.time()
try:
with torch.no_grad():
rendered_image, radii = rasterizer(
means3D=means3D,
means2D=means2D,
opacities=opacities,
colors_precomp=colors,
scales=scales,
rotations=rotations
)
elapsed = time.time() - start_time
print(f"✓ Rendering successful!")
print(f" Time: {elapsed:.3f}s")
print(f" Output shape: {rendered_image.shape}")
print(f" Output range: [{rendered_image.min():.3f}, {rendered_image.max():.3f}]")
print(f" Radii: {(radii > 0).sum()}/{len(radii)} visible")
return True
except Exception as e:
print(f"✗ Rendering failed: {e}")
import traceback
traceback.print_exc()
return False
def test_mark_visible():
"""Test frustum culling"""
print("\n" + "="*60)
print("Test 2: Frustum Culling (markVisible)")
print("="*60)
device = 'cpu'
num_gaussians = 1000
means3D, _, _, _, _, _ = create_test_gaussians(num_gaussians, device)
viewmatrix, projmatrix, _ = create_camera_matrices(device)
raster_settings = idgr.GaussianRasterizationSettings(
image_height=512,
image_width=512,
tanfovx=0.5,
tanfovy=0.5,
bg=torch.zeros(3, device=device, dtype=torch.float32),
scale_modifier=1.0,
viewmatrix=viewmatrix,
projmatrix=projmatrix,
sh_degree=0,
campos=torch.zeros(3, device=device, dtype=torch.float32),
prefiltered=False,
debug=False
)
rasterizer = idgr.GaussianRasterizer(raster_settings)
try:
visible = rasterizer.markVisible(means3D)
num_visible = visible.sum().item()
print(f"✓ Frustum culling successful!")
print(f" Visible Gaussians: {num_visible}/{num_gaussians}")
return True
except Exception as e:
print(f"✗ Frustum culling failed: {e}")
import traceback
traceback.print_exc()
return False
def test_spherical_harmonics():
"""Test rendering with spherical harmonics"""
print("\n" + "="*60)
print("Test 3: Spherical Harmonics Rendering")
print("="*60)
device = 'cpu'
width, height = 512, 512
num_gaussians = 500
sh_degree = 3
# Create test data
means3D, means2D, _, opacities, scales, rotations = \
create_test_gaussians(num_gaussians, device)
# Create random SH coefficients
# For degree 3: (degree+1)^2 = 16 coefficients per channel
num_sh_coeffs = (sh_degree + 1) ** 2
shs = torch.randn(num_gaussians, num_sh_coeffs, 3, device=device, dtype=torch.float32) * 0.1
viewmatrix, projmatrix, campos = create_camera_matrices(device)
raster_settings = idgr.GaussianRasterizationSettings(
image_height=height,
image_width=width,
tanfovx=0.5,
tanfovy=0.5,
bg=torch.zeros(3, device=device, dtype=torch.float32),
scale_modifier=1.0,
viewmatrix=viewmatrix,
projmatrix=projmatrix,
sh_degree=sh_degree,
campos=campos,
prefiltered=False,
debug=False
)
rasterizer = idgr.GaussianRasterizer(raster_settings)
try:
with torch.no_grad():
rendered_image, radii = rasterizer(
means3D=means3D,
means2D=means2D,
opacities=opacities,
shs=shs,
scales=scales,
rotations=rotations
)
print(f"✓ SH rendering successful!")
print(f" SH degree: {sh_degree}")
print(f" SH coefficients: {num_sh_coeffs}")
print(f" Output shape: {rendered_image.shape}")
return True
except Exception as e:
print(f"✗ SH rendering failed: {e}")
import traceback
traceback.print_exc()
return False
def benchmark_performance():
"""Benchmark rendering performance"""
print("\n" + "="*60)
print("Performance Benchmark")
print("="*60)
device = 'cpu'
width, height = 800, 800
test_configs = [
(100, "100 Gaussians"),
(1000, "1K Gaussians"),
(10000, "10K Gaussians"),
(50000, "50K Gaussians"),
(100000, "100K Gaussians"),
(150000, "150K Gaussians"),
(200000, "200K Gaussians"),
(500000, "500K Gaussians"),
]
viewmatrix, projmatrix, campos = create_camera_matrices(device)
print(f"\nResolution: {width}x{height}")
print("-" * 60)
for num_gaussians, label in test_configs:
means3D, means2D, colors, opacities, scales, rotations = \
create_test_gaussians(num_gaussians, device)
raster_settings = idgr.GaussianRasterizationSettings(
image_height=height,
image_width=width,
tanfovx=0.5,
tanfovy=0.5,
bg=torch.zeros(3, device=device, dtype=torch.float32),
scale_modifier=1.0,
viewmatrix=viewmatrix,
projmatrix=projmatrix,
sh_degree=0,
campos=campos,
prefiltered=False,
debug=False
)
rasterizer = idgr.GaussianRasterizer(raster_settings)
# Warmup
with torch.no_grad():
_ = rasterizer(means3D, means2D, opacities,
colors_precomp=colors, scales=scales, rotations=rotations)
# Benchmark
num_iterations = 10
start_time = time.time()
for _ in range(num_iterations):
with torch.no_grad():
_ = rasterizer(means3D, means2D, opacities,
colors_precomp=colors, scales=scales, rotations=rotations)
elapsed = time.time() - start_time
avg_time = elapsed / num_iterations
fps = 1.0 / avg_time
print(f"{label:20s}: {avg_time*1000:6.2f}ms/frame ({fps:5.2f} FPS)")
def main():
"""Run all tests"""
print("\n" + "="*60)
print("Intel XPU Gaussian Rasterization Test Suite")
print("="*60)
results = []
# Run tests
results.append(("Basic Rendering", test_basic_rendering()))
results.append(("Frustum Culling", test_mark_visible()))
results.append(("Spherical Harmonics", test_spherical_harmonics()))
# Performance benchmark
try:
benchmark_performance()
except Exception as e:
print(f"\n⚠ Benchmark failed: {e}")
# Summary
print("\n" + "="*60)
print("Test Summary")
print("="*60)
passed = sum(1 for _, result in results if result)
total = len(results)
for name, result in results:
status = "✓ PASS" if result else "✗ FAIL"
print(f"{name:30s}: {status}")
print("-" * 60)
print(f"Total: {passed}/{total} tests passed")
if passed == total:
print("\n✓ All tests passed!")
return 0
else:
print(f"\n✗ {total - passed} test(s) failed")
return 1
if __name__ == "__main__":
sys.exit(main())