mlx3d 0.3.0__tar.gz → 0.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mlx3d-0.3.0 → mlx3d-0.3.2}/.github/workflows/tests.yml +2 -2
- {mlx3d-0.3.0 → mlx3d-0.3.2}/CHANGELOG.md +19 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/PKG-INFO +2 -2
- mlx3d-0.3.2/examples/validate_gaussian_gradients.py +116 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/_version.py +2 -2
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/rasterize.py +7 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/tiles.py +123 -4
- mlx3d-0.3.2/tests/__init__.py +0 -0
- mlx3d-0.3.2/tests/test_gradcheck_rasterizer.py +113 -0
- mlx3d-0.3.2/tests/test_tiles_fast_binning.py +127 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/uv.lock +199 -106
- {mlx3d-0.3.0 → mlx3d-0.3.2}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/.github/workflows/documentation.yml +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/.github/workflows/publish.yml +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/.gitignore +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/CITATION.cff +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/CODE_OF_CONDUCT.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/CONTRIBUTING.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/LICENSE +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/Logo.jpeg +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/README.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/SECURITY.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/cameras.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/capture.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/datasets.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/io.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/losses.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/nn.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/ops.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/renderer.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/splatting.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/structures.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/transforms.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/utils.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/api/viewer.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/capture_castle.jpg +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/fast_4dgs_juggle.mp4 +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/fast_4dgs_juggle_strip.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/fast_truck_diff_x50.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/fast_truck_fast.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/fast_truck_reference.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/gallery_gsplat.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/gallery_nerf.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/logo.jpeg +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/mlx3d-icon.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/mlx3d-logo.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/render_aa.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/render_lit_sphere.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/render_lit_torus.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/render_normals.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/render_textured.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/v020_lego_ngp.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/v020_truck_depth_hi.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/v020_truck_normals_hi.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/assets/v020_truck_render_hi.png +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/benchmarks.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/conventions.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/index.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/quickstart.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/capture.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/extending.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/fast_rendering.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/gaussian_splatting.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/hashgrid_nerf.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/mesh_lighting.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/mesh_optimization.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/mesh_rendering.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/nerf.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/tutorials/pointcloud_fitting.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/docs/viewer.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/README.md +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/benchmark.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/benchmark_fast_rasterization.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/benchmark_gaussian_splatting.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/extend_renderer.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/extract_mesh.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/fit_gaussians.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/fit_mesh.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/fit_nerf.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/fit_pointcloud.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/play_dynamic_gaussians.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/raytrace_volume.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/refine_pose.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/render_mesh.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/render_mesh_lit.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/sdf_csg.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/train_gaussian_splatting.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/train_nerf.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/examples/train_ngp_nerf.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/mkdocs.yml +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/pyproject.toml +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/cameras/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/cameras/cameras.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/capture/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/capture/colmap_wrap.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/capture/frames.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/capture/pipeline.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/capture/sfm.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/cli/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/cli/capture.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/cli/compact.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/cli/eval.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/cli/render.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/datasets/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/datasets/blender.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/datasets/colmap.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/datasets/images.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/datasets/instant_ngp.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/io/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/io/gltf_io.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/io/image_io.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/io/obj_io.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/io/ply_io.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/losses/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/losses/chamfer.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/losses/image_metrics.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/losses/lpips.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/losses/mesh_losses.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/losses/point_mesh.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/nn/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/nn/accel.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/nn/fused_mlp.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/nn/hashgrid.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/nn/instant_ngp.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/nn/nerf.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/nn/occupancy.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/ball_query.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/geometry.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/knn.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/marching_cubes.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/ray_mesh.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/sample_points.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/sdf.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/ops/subdivide.py +0 -0
- /mlx3d-0.3.0/tests/__init__.py → /mlx3d-0.3.2/src/mlx3d/py.typed +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/renderer/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/renderer/mesh.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/renderer/points.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/renderer/protocols.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/renderer/rasterizer.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/renderer/rays.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/renderer/shading.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/fast.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/model.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/projection.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/reference.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/render.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/sh.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/splatting/trainer.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/structures/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/structures/meshes.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/structures/pointclouds.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/transforms/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/transforms/rotations.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/transforms/se3.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/utils/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/utils/primitives.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/viewer/__init__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/viewer/__main__.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/viewer/server.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/src/mlx3d/viewer/viewer.html +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/conftest.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_behavior.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_cameras.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_capture.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_cli_compact.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_cli_eval.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_cli_render.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_datasets.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_image_io.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_import.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_io.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_mesh_rendering.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_nerf_renderer.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_ops_losses.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_ops_sdf.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_rasterizer.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_se3.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_smoke.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_splatting.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_splatting_fast.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_structures.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_transforms.py +0 -0
- {mlx3d-0.3.0 → mlx3d-0.3.2}/tests/test_viewer.py +0 -0
|
@@ -17,9 +17,9 @@ jobs:
|
|
|
17
17
|
python-version: "3.12"
|
|
18
18
|
enable-cache: true
|
|
19
19
|
- name: Ruff lint
|
|
20
|
-
run: uvx ruff check .
|
|
20
|
+
run: uvx ruff@0.14.0 check .
|
|
21
21
|
- name: Ruff format
|
|
22
|
-
run: uvx ruff format --check .
|
|
22
|
+
run: uvx ruff@0.14.0 format --check .
|
|
23
23
|
|
|
24
24
|
test:
|
|
25
25
|
needs: lint
|
|
@@ -2,8 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- Faster Gaussian tile binning (`bin_gaussians`), on the critical path of every
|
|
8
|
+
render and training step. The default path now fuses expansion and
|
|
9
|
+
range-finding into two Metal kernels — tile duplicates are emitted in depth
|
|
10
|
+
order so only a stable 32-bit tile-key sort is needed, and per-tile ranges are
|
|
11
|
+
recovered by boundary detection instead of a scatter-min/max pass — replacing
|
|
12
|
+
the int64 composite-key argsort. Bit-identical output; ~2.5× faster binning
|
|
13
|
+
and ~26% faster forward rendering on a 500k-Gaussian scene at 1080p (so
|
|
14
|
+
viewers, evaluation, capture previews, and every training forward speed up).
|
|
15
|
+
|
|
5
16
|
### Added
|
|
6
17
|
|
|
18
|
+
- Type support: mlx3d now ships a `py.typed` marker, so downstream projects
|
|
19
|
+
get its inline type hints under mypy / pyright.
|
|
20
|
+
- Finite-difference gradient check for the Metal Gaussian rasterizer's
|
|
21
|
+
hand-written backward pass (`examples/validate_gaussian_gradients.py` plus a
|
|
22
|
+
`unit` test). Confirms the analytic gradients match numerical derivatives
|
|
23
|
+
(geometry to ~1e-2, appearance to ~1e-3), a runnable correctness artifact on
|
|
24
|
+
Apple Silicon.
|
|
25
|
+
|
|
7
26
|
- Fast forward-only Gaussian rasterization (`FastGaussianRenderer`,
|
|
8
27
|
`render_gaussians_fast`), adapting the geometry-shader-style pipeline of
|
|
9
28
|
dendenxu/fast-gaussian-rasterization to Metal compute: a fused per-Gaussian
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: mlx3d
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2
|
|
4
4
|
Summary: Differentiable 3D computer vision on Apple Silicon, built on MLX
|
|
5
5
|
Project-URL: Homepage, https://github.com/amirhossein-razlighi/mlx3D
|
|
6
6
|
Project-URL: Documentation, https://amirhossein-razlighi.github.io/mlx3D/
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Validate the Metal Gaussian rasterizer's analytic gradients numerically.
|
|
2
|
+
|
|
3
|
+
The 3DGS backward pass is a hand-written Metal kernel, not autodiff. This script
|
|
4
|
+
checks it against central finite differences and prints a report, so you can
|
|
5
|
+
trust the training gradients (and catch regressions if you touch the kernel).
|
|
6
|
+
|
|
7
|
+
For each parameter it reports the symmetric relative error between the analytic
|
|
8
|
+
directional derivative ``grad . v`` and the finite-difference estimate
|
|
9
|
+
``(L(x + eps v) - L(x - eps v)) / (2 eps)`` along random directions ``v``.
|
|
10
|
+
|
|
11
|
+
python examples/validate_gaussian_gradients.py
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import sys
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
_SRC = Path(__file__).resolve().parents[1] / "src"
|
|
18
|
+
if _SRC.exists():
|
|
19
|
+
sys.path.insert(0, str(_SRC))
|
|
20
|
+
|
|
21
|
+
import mlx.core as mx
|
|
22
|
+
import numpy as np
|
|
23
|
+
|
|
24
|
+
from mlx3d.cameras import Camera
|
|
25
|
+
from mlx3d.splatting import render_gaussians
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def loss_fn(cam, target, bg, sh_degree=0):
|
|
29
|
+
def loss(means, quats, log_scales, logit_op, sh):
|
|
30
|
+
out = render_gaussians(
|
|
31
|
+
cam,
|
|
32
|
+
means,
|
|
33
|
+
quats,
|
|
34
|
+
mx.exp(log_scales),
|
|
35
|
+
mx.sigmoid(logit_op),
|
|
36
|
+
sh=sh,
|
|
37
|
+
sh_degree=sh_degree,
|
|
38
|
+
background=bg,
|
|
39
|
+
)
|
|
40
|
+
return mx.mean((out["image"] - target) ** 2)
|
|
41
|
+
|
|
42
|
+
return loss
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def directional_report(loss, args, names, indices=None, eps=2e-3, n_dirs=6):
|
|
46
|
+
_, grads = mx.value_and_grad(loss, argnums=tuple(range(len(args))))(*args)
|
|
47
|
+
mx.eval(grads)
|
|
48
|
+
indices = range(len(args)) if indices is None else indices
|
|
49
|
+
print(
|
|
50
|
+
f" {'param':12s} {'analytic':>13s} {'numeric':>13s} {'median rel':>12s} {'max rel':>10s}"
|
|
51
|
+
)
|
|
52
|
+
worst = 0.0
|
|
53
|
+
for i in indices:
|
|
54
|
+
name = names[i]
|
|
55
|
+
errs, a_last, n_last = [], 0.0, 0.0
|
|
56
|
+
for s in range(n_dirs):
|
|
57
|
+
mx.random.seed(500 + s)
|
|
58
|
+
v = mx.random.normal(args[i].shape)
|
|
59
|
+
a = float(mx.sum(grads[i] * v).item())
|
|
60
|
+
plus = list(args)
|
|
61
|
+
plus[i] = args[i] + eps * v
|
|
62
|
+
minus = list(args)
|
|
63
|
+
minus[i] = args[i] - eps * v
|
|
64
|
+
num = (float(loss(*plus).item()) - float(loss(*minus).item())) / (2 * eps)
|
|
65
|
+
errs.append(abs(a - num) / (abs(a) + abs(num) + 1e-8))
|
|
66
|
+
a_last, n_last = a, num
|
|
67
|
+
med, mx_err = float(np.median(errs)), float(np.max(errs))
|
|
68
|
+
worst = max(worst, med)
|
|
69
|
+
print(f" {name:12s} {a_last:+13.3e} {n_last:+13.3e} {med:12.2e} {mx_err:10.2e}")
|
|
70
|
+
return worst
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def main():
|
|
74
|
+
cam = Camera.look_at(eye=(0, 0, -4.0), at=(0, 0, 0), width=64, height=64)
|
|
75
|
+
mx.random.seed(7)
|
|
76
|
+
target = mx.random.uniform(shape=(64, 64, 3))
|
|
77
|
+
|
|
78
|
+
print("Single isolated Gaussian (per-Gaussian gradient formulas):")
|
|
79
|
+
args = [
|
|
80
|
+
mx.array([[0.05, -0.03, 0.0]]),
|
|
81
|
+
mx.array([[1.0, 0.2, 0.1, 0.0]]),
|
|
82
|
+
mx.array([[-1.2, -1.0, -1.3]]),
|
|
83
|
+
mx.array([0.4]),
|
|
84
|
+
mx.array([[[0.3, 0.1, -0.2]]]),
|
|
85
|
+
]
|
|
86
|
+
names = ["means", "quats", "log_scales", "opacity", "color"]
|
|
87
|
+
w1 = directional_report(loss_fn(cam, target, mx.zeros((3,))), args, names)
|
|
88
|
+
|
|
89
|
+
print("\nFour depth-stacked Gaussians (compositing colour gradient):")
|
|
90
|
+
# Only colour is finite-difference-clean under overlap; opacity/geometry are
|
|
91
|
+
# validated on the isolated Gaussian above (their FD crosses the discrete
|
|
92
|
+
# n_contrib / tile boundaries once Gaussians overlap).
|
|
93
|
+
mx.random.seed(5)
|
|
94
|
+
target2 = mx.random.uniform(shape=(64, 64, 3))
|
|
95
|
+
args2 = [
|
|
96
|
+
mx.array([[0.0, 0.0, 0.0], [0.05, 0.02, 0.3], [-0.03, 0.04, 0.6], [0.02, -0.05, 0.9]]),
|
|
97
|
+
mx.tile(mx.array([[1.0, 0.0, 0.0, 0.0]]), (4, 1)),
|
|
98
|
+
mx.full((4, 3), -1.0),
|
|
99
|
+
mx.array([0.2, -0.1, 0.3, 0.0]),
|
|
100
|
+
mx.random.normal((4, 1, 3)) * 0.5,
|
|
101
|
+
]
|
|
102
|
+
w2 = directional_report(
|
|
103
|
+
loss_fn(cam, target2, mx.array([0.2, 0.1, 0.3])), args2, names, indices=[4]
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
worst = max(w1, w2)
|
|
107
|
+
print(
|
|
108
|
+
f"\nWorst median relative error: {worst:.2e} "
|
|
109
|
+
f"({'PASS' if worst < 0.10 else 'CHECK'} — geometry ~1e-2 is finite-difference "
|
|
110
|
+
"truncation error, not a wrong gradient; per-direction max is noisier because "
|
|
111
|
+
"some directions cross discrete tile/sort boundaries)."
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if __name__ == "__main__":
|
|
116
|
+
main()
|
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '0.3.
|
|
22
|
-
__version_tuple__ = version_tuple = (0, 3,
|
|
21
|
+
__version__ = version = '0.3.2'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 3, 2)
|
|
23
23
|
|
|
24
24
|
__commit_id__ = commit_id = None
|
|
@@ -13,6 +13,13 @@ the tile-batched style of gsplat:
|
|
|
13
13
|
|
|
14
14
|
The pair is wrapped in :func:`mx.custom_function`, so MLX autodiff chains
|
|
15
15
|
the kernel gradients into the (pure-MLX) projection math upstream.
|
|
16
|
+
|
|
17
|
+
Note: a SIMD-group-reduced backward (sum each Gaussian's per-pixel gradients
|
|
18
|
+
with ``simd_sum`` and do one atomic per 32-lane group instead of up to 256)
|
|
19
|
+
was tried and is ~3% *slower* on Apple GPUs — the backward is bound by the
|
|
20
|
+
per-pixel recompute and the sequential transmittance walk, not by atomic
|
|
21
|
+
contention, so the reduction only adds overhead. Left as one atomic per
|
|
22
|
+
contributing (pixel, Gaussian) pair.
|
|
16
23
|
"""
|
|
17
24
|
|
|
18
25
|
from __future__ import annotations
|
|
@@ -2,11 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
Each Gaussian is duplicated once per 16x16 screen tile its 3-sigma extent
|
|
4
4
|
touches, then all duplicates are sorted by ``(tile, depth)``. The Metal
|
|
5
|
-
rasterizer consumes the resulting per-tile contiguous ranges.
|
|
5
|
+
rasterizer consumes the resulting per-tile contiguous ranges. None of this
|
|
6
|
+
carries gradients, but it runs on every frame and every training step.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
The default (no conic refinement) path is fused into two Metal kernels: tile
|
|
9
|
+
duplicates are emitted already in depth order, so only a *stable 32-bit*
|
|
10
|
+
tile-key sort is needed, and per-tile ranges are recovered by boundary
|
|
11
|
+
detection. This avoids the reference path's int64 composite-key argsort and
|
|
12
|
+
scatter-min/max range pass (~2.5x faster binning on large scenes, bit-identical
|
|
13
|
+
output). The reference pure-MLX path is kept for the conic-refinement case and
|
|
14
|
+
as an A/B baseline (see ``_USE_FAST_BIN``).
|
|
10
15
|
"""
|
|
11
16
|
|
|
12
17
|
import mlx.core as mx
|
|
@@ -15,6 +20,110 @@ TILE_SIZE = 16
|
|
|
15
20
|
|
|
16
21
|
__all__ = ["TILE_SIZE", "bin_gaussians"]
|
|
17
22
|
|
|
23
|
+
# Toggled off only in tests to A/B the fused kernel path against the reference
|
|
24
|
+
# pure-MLX composite-key path. Keep True in production.
|
|
25
|
+
_USE_FAST_BIN = True
|
|
26
|
+
|
|
27
|
+
# --------------------------------------------------------------------------
|
|
28
|
+
# Fused expansion + range-finding kernels for the common (no-conic-refine)
|
|
29
|
+
# path. These do in two GPU passes what the pure-MLX version does with an
|
|
30
|
+
# int64 composite-key argsort and a scatter-min/max range pass: emit tile
|
|
31
|
+
# duplicates already in depth order (so a *stable 32-bit* tile-key sort is
|
|
32
|
+
# enough), then recover per-tile [start, end) ranges by boundary detection
|
|
33
|
+
# instead of scatter atomics. (Mirrors the kernels in ``fast.py``.)
|
|
34
|
+
# --------------------------------------------------------------------------
|
|
35
|
+
_EXPAND_SRC = """
|
|
36
|
+
const uint i = thread_position_in_grid.x; // depth-rank position
|
|
37
|
+
const uint N = (uint)iparams[0];
|
|
38
|
+
if (i >= N) return;
|
|
39
|
+
|
|
40
|
+
const int tiles_x = iparams[1];
|
|
41
|
+
const int capacity = iparams[2];
|
|
42
|
+
|
|
43
|
+
const int g = order[i];
|
|
44
|
+
const int count = counts[g];
|
|
45
|
+
if (count == 0) return;
|
|
46
|
+
int slot = offsets[i];
|
|
47
|
+
|
|
48
|
+
const int xmin = bbox[4 * g + 0];
|
|
49
|
+
const int xmax = bbox[4 * g + 1];
|
|
50
|
+
const int ymin = bbox[4 * g + 2];
|
|
51
|
+
const int ymax = bbox[4 * g + 3];
|
|
52
|
+
|
|
53
|
+
for (int ty = ymin; ty <= ymax; ty++) {
|
|
54
|
+
for (int tx = xmin; tx <= xmax; tx++) {
|
|
55
|
+
if (slot >= capacity) return;
|
|
56
|
+
keys[slot] = (uint)(ty * tiles_x + tx);
|
|
57
|
+
dup_ids[slot] = g;
|
|
58
|
+
slot++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
_RANGES_SRC = """
|
|
64
|
+
const uint i = thread_position_in_grid.x;
|
|
65
|
+
const uint capacity = (uint)iparams[0];
|
|
66
|
+
const uint num_tiles = (uint)iparams[1];
|
|
67
|
+
if (i >= capacity) return;
|
|
68
|
+
|
|
69
|
+
const uint k = keys[sort_idx[i]];
|
|
70
|
+
const uint kp = (i == 0) ? 0xFFFFFFFFu : keys[sort_idx[i - 1]];
|
|
71
|
+
|
|
72
|
+
if (i == 0 || k != kp) {
|
|
73
|
+
if (k < num_tiles) tile_ranges[2 * k] = (int)i;
|
|
74
|
+
if (i > 0 && kp < num_tiles) tile_ranges[2 * kp + 1] = (int)i;
|
|
75
|
+
}
|
|
76
|
+
if (i == capacity - 1 && k < num_tiles) tile_ranges[2 * k + 1] = (int)(i + 1);
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
_expand_kernel = mx.fast.metal_kernel(
|
|
80
|
+
name="gs_bin_expand",
|
|
81
|
+
input_names=["order", "offsets", "counts", "bbox", "iparams"],
|
|
82
|
+
output_names=["keys", "dup_ids"],
|
|
83
|
+
source=_EXPAND_SRC,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
_ranges_kernel = mx.fast.metal_kernel(
|
|
87
|
+
name="gs_bin_ranges",
|
|
88
|
+
input_names=["keys", "sort_idx", "iparams"],
|
|
89
|
+
output_names=["tile_ranges"],
|
|
90
|
+
source=_RANGES_SRC,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _bin_fast(xmin, xmax, ymin, ymax, counts, depths, total, N, tiles_x, num_tiles):
|
|
95
|
+
"""Depth-ordered expansion + 32-bit tile sort + boundary-detection ranges.
|
|
96
|
+
|
|
97
|
+
Produces the same ``(sorted_ids, tile_ranges)`` as the composite-key path:
|
|
98
|
+
duplicates are emitted in ascending-depth order and a stable sort by tile
|
|
99
|
+
keeps that order within each tile.
|
|
100
|
+
"""
|
|
101
|
+
order = mx.argsort(depths).astype(mx.int32) # nearest first
|
|
102
|
+
counts_sorted = counts[order]
|
|
103
|
+
offsets = (mx.cumsum(counts_sorted) - counts_sorted).astype(mx.int32)
|
|
104
|
+
bbox = mx.stack([xmin, xmax, ymin, ymax], axis=-1).astype(mx.int32)
|
|
105
|
+
|
|
106
|
+
keys, dup_ids = _expand_kernel(
|
|
107
|
+
inputs=[order, offsets, counts, bbox, mx.array([N, tiles_x, total], dtype=mx.int32)],
|
|
108
|
+
output_shapes=[(total,), (total,)],
|
|
109
|
+
output_dtypes=[mx.uint32, mx.int32],
|
|
110
|
+
grid=(N, 1, 1),
|
|
111
|
+
threadgroup=(min(256, N), 1, 1),
|
|
112
|
+
init_value=0,
|
|
113
|
+
)
|
|
114
|
+
sort_idx = mx.argsort(keys).astype(mx.int32)
|
|
115
|
+
sorted_ids = dup_ids[sort_idx].astype(mx.int32)
|
|
116
|
+
|
|
117
|
+
(tile_ranges,) = _ranges_kernel(
|
|
118
|
+
inputs=[keys, sort_idx, mx.array([total, num_tiles], dtype=mx.int32)],
|
|
119
|
+
output_shapes=[(num_tiles, 2)],
|
|
120
|
+
output_dtypes=[mx.int32],
|
|
121
|
+
grid=(total, 1, 1),
|
|
122
|
+
threadgroup=(256, 1, 1),
|
|
123
|
+
init_value=0, # empty tiles keep start == end == 0
|
|
124
|
+
)
|
|
125
|
+
return sorted_ids, tile_ranges
|
|
126
|
+
|
|
18
127
|
|
|
19
128
|
def bin_gaussians(
|
|
20
129
|
means2d: mx.array,
|
|
@@ -74,6 +183,16 @@ def bin_gaussians(
|
|
|
74
183
|
tiles_y,
|
|
75
184
|
)
|
|
76
185
|
|
|
186
|
+
# Fast path (no conic refinement): fused Metal expansion + 32-bit tile sort
|
|
187
|
+
# + boundary-detection ranges, avoiding the int64 composite-key argsort and
|
|
188
|
+
# the scatter-min/max range pass below. ``_USE_FAST_BIN`` exists only so
|
|
189
|
+
# tests can A/B the two implementations; leave it True.
|
|
190
|
+
if conics is None and _USE_FAST_BIN:
|
|
191
|
+
sorted_ids, tile_ranges = _bin_fast(
|
|
192
|
+
xmin, xmax, ymin, ymax, counts, depths, total, N, tiles_x, num_tiles
|
|
193
|
+
)
|
|
194
|
+
return sorted_ids, tile_ranges, tiles_x, tiles_y
|
|
195
|
+
|
|
77
196
|
# Expand: duplicate j belongs to Gaussian g(j). Scatter a 1 at each
|
|
78
197
|
# Gaussian's first duplicate slot, cumsum, subtract 1. Zero-count
|
|
79
198
|
# Gaussians scatter onto the same slot as their successor, and the
|
|
File without changes
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Finite-difference gradient check for the Metal Gaussian rasterizer.
|
|
2
|
+
|
|
3
|
+
The rasterizer's backward pass is a hand-written Metal kernel (see
|
|
4
|
+
``rasterize.py``), so its gradients are not produced by autodiff and deserve an
|
|
5
|
+
independent correctness check. Here we compare the analytic gradient (autodiff
|
|
6
|
+
through the custom VJP) against central finite differences along random
|
|
7
|
+
directions, using the symmetric relative error
|
|
8
|
+
``|analytic - numeric| / (|analytic| + |numeric|)`` which is robust when a
|
|
9
|
+
directional derivative is near zero.
|
|
10
|
+
|
|
11
|
+
Finite differences are only meaningful where the loss is smooth, so the scenes
|
|
12
|
+
are built to avoid the rasterizer's genuinely non-differentiable boundaries
|
|
13
|
+
(tile assignment, depth-sort order, the alpha 1/255 cutoff, transmittance
|
|
14
|
+
early-termination): a single large centered Gaussian for the per-Gaussian
|
|
15
|
+
geometry/appearance formulas, and a few depth-separated Gaussians for the
|
|
16
|
+
multi-Gaussian compositing colour gradient.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import mlx.core as mx
|
|
20
|
+
import numpy as np
|
|
21
|
+
import pytest
|
|
22
|
+
|
|
23
|
+
from mlx3d.cameras import Camera
|
|
24
|
+
from mlx3d.splatting import render_gaussians
|
|
25
|
+
|
|
26
|
+
pytestmark = pytest.mark.unit
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _loss_fn(cam, target, bg):
|
|
30
|
+
def loss(means, quats, log_scales, logit_op, sh_dc):
|
|
31
|
+
out = render_gaussians(
|
|
32
|
+
cam,
|
|
33
|
+
means,
|
|
34
|
+
quats,
|
|
35
|
+
mx.exp(log_scales),
|
|
36
|
+
mx.sigmoid(logit_op),
|
|
37
|
+
sh=sh_dc,
|
|
38
|
+
sh_degree=0,
|
|
39
|
+
background=bg,
|
|
40
|
+
)
|
|
41
|
+
return mx.mean((out["image"] - target) ** 2)
|
|
42
|
+
|
|
43
|
+
return loss
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _directional_errors(loss, args, arg_index, grad, eps=2e-3, n_dirs=5, seed=200):
|
|
47
|
+
"""Symmetric relative error between analytic and FD directional derivatives."""
|
|
48
|
+
errors = []
|
|
49
|
+
for s in range(n_dirs):
|
|
50
|
+
mx.random.seed(seed + s)
|
|
51
|
+
v = mx.random.normal(args[arg_index].shape)
|
|
52
|
+
analytic = float(mx.sum(grad * v).item())
|
|
53
|
+
plus = list(args)
|
|
54
|
+
plus[arg_index] = args[arg_index] + eps * v
|
|
55
|
+
minus = list(args)
|
|
56
|
+
minus[arg_index] = args[arg_index] - eps * v
|
|
57
|
+
numeric = (float(loss(*plus).item()) - float(loss(*minus).item())) / (2 * eps)
|
|
58
|
+
errors.append(abs(analytic - numeric) / (abs(analytic) + abs(numeric) + 1e-8))
|
|
59
|
+
return float(np.median(errors)), float(np.max(errors))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_gradcheck_single_gaussian_all_params():
|
|
63
|
+
"""Each per-Gaussian gradient formula (means, conics via quats/scales,
|
|
64
|
+
opacity, colour) matches finite differences for an isolated Gaussian."""
|
|
65
|
+
cam = Camera.look_at(eye=(0, 0, -4.0), at=(0, 0, 0), width=64, height=64)
|
|
66
|
+
mx.random.seed(7)
|
|
67
|
+
target = mx.random.uniform(shape=(64, 64, 3))
|
|
68
|
+
bg = mx.zeros((3,))
|
|
69
|
+
|
|
70
|
+
args = [
|
|
71
|
+
mx.array([[0.05, -0.03, 0.0]]), # means
|
|
72
|
+
mx.array([[1.0, 0.2, 0.1, 0.0]]), # quats
|
|
73
|
+
mx.array([[-1.2, -1.0, -1.3]]), # log_scales
|
|
74
|
+
mx.array([0.4]), # logit opacity
|
|
75
|
+
mx.array([[[0.3, 0.1, -0.2]]]), # sh dc (colour)
|
|
76
|
+
]
|
|
77
|
+
loss = _loss_fn(cam, target, bg)
|
|
78
|
+
_, grads = mx.value_and_grad(loss, argnums=(0, 1, 2, 3, 4))(*args)
|
|
79
|
+
mx.eval(grads)
|
|
80
|
+
|
|
81
|
+
# The median over random directions is the robust statistic: an occasional
|
|
82
|
+
# direction crosses a discrete tile/sort kink and inflates the max. Geometry
|
|
83
|
+
# gradients also carry finite-difference truncation error (the image is
|
|
84
|
+
# smooth but curved); appearance gradients are near-linear and much tighter.
|
|
85
|
+
median_tol = {0: 0.08, 1: 0.08, 2: 0.08, 3: 0.02, 4: 0.02}
|
|
86
|
+
names = ["means", "quats", "log_scales", "opacity", "color"]
|
|
87
|
+
for i, name in enumerate(names):
|
|
88
|
+
median, mx_err = _directional_errors(loss, args, i, grads[i], n_dirs=8)
|
|
89
|
+
assert median < median_tol[i], f"{name}: median rel err {median:.3f} (max {mx_err:.3f})"
|
|
90
|
+
assert mx_err < 1.5, f"{name}: catastrophic max rel err {mx_err:.3f}"
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_gradcheck_compositing_color():
|
|
94
|
+
"""The multi-Gaussian compositing backward (front-to-back colour blending)
|
|
95
|
+
matches finite differences for depth-stacked Gaussians."""
|
|
96
|
+
cam = Camera.look_at(eye=(0, 0, -4.0), at=(0, 0, 0), width=64, height=64)
|
|
97
|
+
mx.random.seed(5)
|
|
98
|
+
target = mx.random.uniform(shape=(64, 64, 3))
|
|
99
|
+
bg = mx.array([0.2, 0.1, 0.3])
|
|
100
|
+
|
|
101
|
+
args = [
|
|
102
|
+
mx.array([[0.0, 0.0, 0.0], [0.05, 0.02, 0.3], [-0.03, 0.04, 0.6], [0.02, -0.05, 0.9]]),
|
|
103
|
+
mx.tile(mx.array([[1.0, 0.0, 0.0, 0.0]]), (4, 1)),
|
|
104
|
+
mx.full((4, 3), -1.0),
|
|
105
|
+
mx.array([0.2, -0.1, 0.3, 0.0]),
|
|
106
|
+
mx.random.normal((4, 1, 3)) * 0.5,
|
|
107
|
+
]
|
|
108
|
+
loss = _loss_fn(cam, target, bg)
|
|
109
|
+
_, grads = mx.value_and_grad(loss, argnums=(0, 1, 2, 3, 4))(*args)
|
|
110
|
+
mx.eval(grads)
|
|
111
|
+
|
|
112
|
+
median, mx_err = _directional_errors(loss, args, 4, grads[4], n_dirs=8, seed=300)
|
|
113
|
+
assert median < 0.03, f"color: median rel err {median:.3f} (max {mx_err:.3f})"
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""The fused Metal tile-binning path must match the reference pure-MLX path.
|
|
2
|
+
|
|
3
|
+
``bin_gaussians`` gained a fused-kernel fast path (depth-ordered expansion +
|
|
4
|
+
32-bit tile sort + boundary-detection ranges). It is non-differentiable and
|
|
5
|
+
must be a drop-in for the composite-key reference: identical rendered images
|
|
6
|
+
and (up to atomic-add ordering noise) identical gradients.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import mlx.core as mx
|
|
10
|
+
import numpy as np
|
|
11
|
+
import pytest
|
|
12
|
+
|
|
13
|
+
import mlx3d.splatting.tiles as tiles
|
|
14
|
+
from mlx3d.cameras import Camera
|
|
15
|
+
from mlx3d.splatting import GaussianModel, render_gaussians
|
|
16
|
+
from mlx3d.splatting.projection import project_gaussians
|
|
17
|
+
from mlx3d.splatting.tiles import bin_gaussians
|
|
18
|
+
|
|
19
|
+
pytestmark = pytest.mark.unit
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _scene(n, seed=0):
|
|
23
|
+
mx.random.seed(seed)
|
|
24
|
+
m = GaussianModel.from_points(mx.random.normal((n, 3)) * 1.5, sh_degree=3)
|
|
25
|
+
m.params["sh_rest"] = mx.random.normal(m.params["sh_rest"].shape) * 0.3
|
|
26
|
+
m.params["scales"] = mx.clip(m.params["scales"] - 1.2, -8.0, -3.2)
|
|
27
|
+
mx.eval(m.params)
|
|
28
|
+
return m
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _render(m, cam, fast):
|
|
32
|
+
tiles._USE_FAST_BIN = fast
|
|
33
|
+
try:
|
|
34
|
+
out = render_gaussians(
|
|
35
|
+
cam,
|
|
36
|
+
m.params["means"],
|
|
37
|
+
m.params["quats"],
|
|
38
|
+
m.scales_act,
|
|
39
|
+
m.opacities_act,
|
|
40
|
+
sh=m.sh,
|
|
41
|
+
sh_degree=3,
|
|
42
|
+
background=mx.zeros((3,)),
|
|
43
|
+
)
|
|
44
|
+
mx.eval(out["image"], out["alpha"])
|
|
45
|
+
return np.array(out["image"]), np.array(out["alpha"])
|
|
46
|
+
finally:
|
|
47
|
+
tiles._USE_FAST_BIN = True
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@pytest.mark.parametrize("n,w,h", [(2000, 160, 120), (30000, 320, 240), (150000, 640, 360)])
|
|
51
|
+
def test_fast_binning_image_is_identical(n, w, h):
|
|
52
|
+
m = _scene(n)
|
|
53
|
+
cam = Camera.look_at(eye=(0.3, 0.1, -6.0), at=(0, 0, 0), width=w, height=h)
|
|
54
|
+
fast_img, fast_a = _render(m, cam, True)
|
|
55
|
+
ref_img, ref_a = _render(m, cam, False)
|
|
56
|
+
# Same set + same depth order per tile => bit-identical compositing.
|
|
57
|
+
assert np.array_equal(fast_img, ref_img)
|
|
58
|
+
assert np.array_equal(fast_a, ref_a)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_fast_binning_gradients_match():
|
|
62
|
+
m = _scene(40000)
|
|
63
|
+
cam = Camera.look_at(eye=(0.2, 0.0, -6.0), at=(0, 0, 0), width=400, height=300)
|
|
64
|
+
target = mx.random.uniform(shape=(300, 400, 3))
|
|
65
|
+
bg = mx.zeros((3,))
|
|
66
|
+
|
|
67
|
+
def loss(p):
|
|
68
|
+
out = render_gaussians(
|
|
69
|
+
cam,
|
|
70
|
+
p["means"],
|
|
71
|
+
p["quats"],
|
|
72
|
+
mx.exp(p["scales"]),
|
|
73
|
+
mx.sigmoid(p["opacities"]),
|
|
74
|
+
sh=mx.concatenate([p["sh_dc"], p["sh_rest"]], axis=1),
|
|
75
|
+
sh_degree=3,
|
|
76
|
+
background=bg,
|
|
77
|
+
)
|
|
78
|
+
return mx.mean((out["image"] - target) ** 2)
|
|
79
|
+
|
|
80
|
+
gf = mx.value_and_grad(loss)
|
|
81
|
+
tiles._USE_FAST_BIN = True
|
|
82
|
+
lf, gfast = gf(m.params)
|
|
83
|
+
tiles._USE_FAST_BIN = False
|
|
84
|
+
lr, gref = gf(m.params)
|
|
85
|
+
mx.eval(lf, lr, gfast, gref)
|
|
86
|
+
tiles._USE_FAST_BIN = True
|
|
87
|
+
|
|
88
|
+
assert abs(float(lf) - float(lr)) < 1e-6
|
|
89
|
+
for k in gfast:
|
|
90
|
+
# Only atomic-add ordering differs; tolerance is loose relative noise.
|
|
91
|
+
assert float(mx.abs(gfast[k] - gref[k]).max()) < 1e-5, k
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_fast_binning_matches_reference_ranges():
|
|
95
|
+
"""Directly compare bin_gaussians outputs, not just the rendered image."""
|
|
96
|
+
m = _scene(20000)
|
|
97
|
+
cam = Camera.look_at(eye=(0.1, 0.0, -6.0), at=(0, 0, 0), width=320, height=240)
|
|
98
|
+
proj = project_gaussians(cam, m.params["means"], m.params["quats"], m.scales_act)
|
|
99
|
+
mx.eval(proj["means2d"], proj["radii"], proj["depths"])
|
|
100
|
+
|
|
101
|
+
tiles._USE_FAST_BIN = True
|
|
102
|
+
f_ids, f_ranges, tx, ty = bin_gaussians(
|
|
103
|
+
proj["means2d"], proj["radii"], proj["depths"], 320, 240
|
|
104
|
+
)
|
|
105
|
+
tiles._USE_FAST_BIN = False
|
|
106
|
+
r_ids, r_ranges, *_ = bin_gaussians(proj["means2d"], proj["radii"], proj["depths"], 320, 240)
|
|
107
|
+
tiles._USE_FAST_BIN = True
|
|
108
|
+
mx.eval(f_ids, f_ranges, r_ids, r_ranges)
|
|
109
|
+
|
|
110
|
+
# Per-tile ranges must be identical (same counts + boundaries).
|
|
111
|
+
assert np.array_equal(np.array(f_ranges), np.array(r_ranges))
|
|
112
|
+
# The multiset of (tile, gaussian) pairs must match; within a tile the
|
|
113
|
+
# order is the same depth order, so the full sorted id arrays match too.
|
|
114
|
+
assert np.array_equal(np.array(f_ids), np.array(r_ids))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def test_fast_binning_empty_scene():
|
|
118
|
+
# Camera looking away: everything culled -> total 0 branch.
|
|
119
|
+
m = _scene(200)
|
|
120
|
+
cam = Camera.look_at(eye=(0, 0, 40.0), at=(0, 0, 50.0), width=64, height=48)
|
|
121
|
+
proj = project_gaussians(cam, m.params["means"], m.params["quats"], m.scales_act)
|
|
122
|
+
sorted_ids, tile_ranges, tx, ty = bin_gaussians(
|
|
123
|
+
proj["means2d"], proj["radii"], proj["depths"], 64, 48
|
|
124
|
+
)
|
|
125
|
+
mx.eval(sorted_ids, tile_ranges)
|
|
126
|
+
assert sorted_ids.shape[0] == 0
|
|
127
|
+
assert int(mx.sum(tile_ranges).item()) == 0
|