lsnu commited on
Commit
cecf7ae
·
verified ·
1 Parent(s): 4ad7711

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/ext.cpp +186 -0
  2. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/marching_cubes/tables.h +294 -0
  3. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/mesh_normal_consistency/mesh_normal_consistency.h +30 -0
  4. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/mesh_normal_consistency/mesh_normal_consistency_cpu.cpp +53 -0
  5. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/packed_to_padded_tensor/packed_to_padded_tensor.cu +241 -0
  6. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/packed_to_padded_tensor/packed_to_padded_tensor.h +109 -0
  7. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/packed_to_padded_tensor/packed_to_padded_tensor_cpu.cpp +70 -0
  8. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/point_mesh/point_mesh_cpu.cpp +428 -0
  9. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/point_mesh/point_mesh_cuda.cu +833 -0
  10. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/point_mesh/point_mesh_cuda.h +707 -0
  11. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/points_to_volumes/points_to_volumes.cu +349 -0
  12. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/points_to_volumes/points_to_volumes.h +198 -0
  13. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/points_to_volumes/points_to_volumes_cpu.cpp +319 -0
  14. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/constants.h +19 -0
  15. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/README.md +5 -0
  16. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/commands.h +505 -0
  17. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.backward.gpu.cu +9 -0
  18. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.backward_dbg.gpu.cu +9 -0
  19. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.calc_gradients.gpu.cu +9 -0
  20. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.calc_signature.gpu.cu +9 -0
  21. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.construct.gpu.cu +9 -0
  22. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.create_selector.gpu.cu +9 -0
  23. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.destruct.gpu.cu +9 -0
  24. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.fill_bg.gpu.cu +9 -0
  25. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.forward.gpu.cu +9 -0
  26. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.norm_cam_gradients.gpu.cu +9 -0
  27. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.norm_sphere_gradients.gpu.cu +9 -0
  28. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.render.gpu.cu +9 -0
  29. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/global.h +118 -0
  30. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/README.md +5 -0
  31. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/commands.h +391 -0
  32. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.backward.cpu.cpp +9 -0
  33. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.backward_dbg.cpu.cpp +9 -0
  34. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.calc_gradients.cpu.cpp +9 -0
  35. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.calc_signature.cpu.cpp +9 -0
  36. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.construct.cpu.cpp +9 -0
  37. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.create_selector.cpu.cpp +9 -0
  38. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.destruct.cpu.cpp +9 -0
  39. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.fill_bg.cpu.cpp +9 -0
  40. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.forward.cpu.cpp +9 -0
  41. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.norm_cam_gradients.cpu.cpp +9 -0
  42. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.norm_sphere_gradients.cpu.cpp +9 -0
  43. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.render.cpu.cpp +9 -0
  44. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/README.md +16 -0
  45. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/camera.device.h +25 -0
  46. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/camera.h +80 -0
  47. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/closest_sphere_tracker.device.h +138 -0
  48. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/commands.h +35 -0
  49. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/fastermath.h +88 -0
  50. third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/math.h +157 -0
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/ext.cpp ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ // clang-format off
10
+ #include "./pulsar/global.h" // Include before <torch/extension.h>.
11
+ #include <torch/extension.h>
12
+ // clang-format on
13
+ #include "./pulsar/pytorch/renderer.h"
14
+ #include "./pulsar/pytorch/tensor_util.h"
15
+ #include "ball_query/ball_query.h"
16
+ #include "blending/sigmoid_alpha_blend.h"
17
+ #include "compositing/alpha_composite.h"
18
+ #include "compositing/norm_weighted_sum.h"
19
+ #include "compositing/weighted_sum.h"
20
+ #include "face_areas_normals/face_areas_normals.h"
21
+ #include "gather_scatter/gather_scatter.h"
22
+ #include "interp_face_attrs/interp_face_attrs.h"
23
+ #include "iou_box3d/iou_box3d.h"
24
+ #include "knn/knn.h"
25
+ #include "marching_cubes/marching_cubes.h"
26
+ #include "mesh_normal_consistency/mesh_normal_consistency.h"
27
+ #include "packed_to_padded_tensor/packed_to_padded_tensor.h"
28
+ #include "point_mesh/point_mesh_cuda.h"
29
+ #include "points_to_volumes/points_to_volumes.h"
30
+ #include "rasterize_meshes/rasterize_meshes.h"
31
+ #include "rasterize_points/rasterize_points.h"
32
+ #include "sample_farthest_points/sample_farthest_points.h"
33
+ #include "sample_pdf/sample_pdf.h"
34
+
35
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
36
+ m.def("face_areas_normals_forward", &FaceAreasNormalsForward);
37
+ m.def("face_areas_normals_backward", &FaceAreasNormalsBackward);
38
+ m.def("packed_to_padded", &PackedToPadded);
39
+ m.def("padded_to_packed", &PaddedToPacked);
40
+ m.def("interp_face_attrs_forward", &InterpFaceAttrsForward);
41
+ m.def("interp_face_attrs_backward", &InterpFaceAttrsBackward);
42
+ #ifdef WITH_CUDA
43
+ m.def("knn_check_version", &KnnCheckVersion);
44
+ #endif
45
+ m.def("knn_points_idx", &KNearestNeighborIdx);
46
+ m.def("knn_points_backward", &KNearestNeighborBackward);
47
+ m.def("ball_query", &BallQuery);
48
+ m.def("sample_farthest_points", &FarthestPointSampling);
49
+ m.def(
50
+ "mesh_normal_consistency_find_verts", &MeshNormalConsistencyFindVertices);
51
+ m.def("gather_scatter", &GatherScatter);
52
+ m.def("points_to_volumes_forward", PointsToVolumesForward);
53
+ m.def("points_to_volumes_backward", PointsToVolumesBackward);
54
+ m.def("rasterize_points", &RasterizePoints);
55
+ m.def("rasterize_points_backward", &RasterizePointsBackward);
56
+ m.def("rasterize_meshes_backward", &RasterizeMeshesBackward);
57
+ m.def("rasterize_meshes", &RasterizeMeshes);
58
+ m.def("sigmoid_alpha_blend", &SigmoidAlphaBlend);
59
+ m.def("sigmoid_alpha_blend_backward", &SigmoidAlphaBlendBackward);
60
+
61
+ // Accumulation functions
62
+ m.def("accum_weightedsumnorm", &weightedSumNormForward);
63
+ m.def("accum_weightedsum", &weightedSumForward);
64
+ m.def("accum_alphacomposite", &alphaCompositeForward);
65
+ m.def("accum_weightedsumnorm_backward", &weightedSumNormBackward);
66
+ m.def("accum_weightedsum_backward", &weightedSumBackward);
67
+ m.def("accum_alphacomposite_backward", &alphaCompositeBackward);
68
+
69
+ // These are only visible for testing; users should not call them directly
70
+ m.def("_rasterize_points_coarse", &RasterizePointsCoarse);
71
+ m.def("_rasterize_points_naive", &RasterizePointsNaive);
72
+ m.def("_rasterize_meshes_naive", &RasterizeMeshesNaive);
73
+ m.def("_rasterize_meshes_coarse", &RasterizeMeshesCoarse);
74
+ m.def("_rasterize_meshes_fine", &RasterizeMeshesFine);
75
+
76
+ // PointEdge distance functions
77
+ m.def("point_edge_dist_forward", &PointEdgeDistanceForward);
78
+ m.def("point_edge_dist_backward", &PointEdgeDistanceBackward);
79
+ m.def("edge_point_dist_forward", &EdgePointDistanceForward);
80
+ m.def("edge_point_dist_backward", &EdgePointDistanceBackward);
81
+ m.def("point_edge_array_dist_forward", &PointEdgeArrayDistanceForward);
82
+ m.def("point_edge_array_dist_backward", &PointEdgeArrayDistanceBackward);
83
+
84
+ // PointFace distance functions
85
+ m.def("point_face_dist_forward", &PointFaceDistanceForward);
86
+ m.def("point_face_dist_backward", &PointFaceDistanceBackward);
87
+ m.def("face_point_dist_forward", &FacePointDistanceForward);
88
+ m.def("face_point_dist_backward", &FacePointDistanceBackward);
89
+ m.def("point_face_array_dist_forward", &PointFaceArrayDistanceForward);
90
+ m.def("point_face_array_dist_backward", &PointFaceArrayDistanceBackward);
91
+
92
+ // Sample PDF
93
+ m.def("sample_pdf", &SamplePdf);
94
+
95
+ // 3D IoU
96
+ m.def("iou_box3d", &IoUBox3D);
97
+
98
+ // Marching cubes
99
+ m.def("marching_cubes", &MarchingCubes);
100
+
101
+ // Pulsar.
102
+ #ifdef PULSAR_LOGGING_ENABLED
103
+ c10::ShowLogInfoToStderr();
104
+ #endif
105
+ py::class_<
106
+ pulsar::pytorch::Renderer,
107
+ std::shared_ptr<pulsar::pytorch::Renderer>>(m, "PulsarRenderer")
108
+ .def(py::init<
109
+ const uint&,
110
+ const uint&,
111
+ const uint&,
112
+ const bool&,
113
+ const bool&,
114
+ const float&,
115
+ const uint&,
116
+ const uint&>())
117
+ .def(
118
+ "__eq__",
119
+ [](const pulsar::pytorch::Renderer& a,
120
+ const pulsar::pytorch::Renderer& b) { return a == b; },
121
+ py::is_operator())
122
+ .def(
123
+ "__ne__",
124
+ [](const pulsar::pytorch::Renderer& a,
125
+ const pulsar::pytorch::Renderer& b) { return !(a == b); },
126
+ py::is_operator())
127
+ .def(
128
+ "__repr__",
129
+ [](const pulsar::pytorch::Renderer& self) {
130
+ std::stringstream ss;
131
+ ss << self;
132
+ return ss.str();
133
+ })
134
+ .def(
135
+ "forward",
136
+ &pulsar::pytorch::Renderer::forward,
137
+ py::arg("vert_pos"),
138
+ py::arg("vert_col"),
139
+ py::arg("vert_radii"),
140
+
141
+ py::arg("cam_pos"),
142
+ py::arg("pixel_0_0_center"),
143
+ py::arg("pixel_vec_x"),
144
+ py::arg("pixel_vec_y"),
145
+ py::arg("focal_length"),
146
+ py::arg("principal_point_offsets"),
147
+
148
+ py::arg("gamma"),
149
+ py::arg("max_depth"),
150
+ py::arg("min_depth") /* = 0.f*/,
151
+ py::arg(
152
+ "bg_col") /* = at::nullopt not exposed properly in pytorch 1.1. */
153
+ ,
154
+ py::arg("opacity") /* = at::nullopt ... */,
155
+ py::arg("percent_allowed_difference") = 0.01f,
156
+ py::arg("max_n_hits") = MAX_UINT,
157
+ py::arg("mode") = 0)
158
+ .def("backward", &pulsar::pytorch::Renderer::backward)
159
+ .def_property(
160
+ "device_tracker",
161
+ [](const pulsar::pytorch::Renderer& self) {
162
+ return self.device_tracker;
163
+ },
164
+ [](pulsar::pytorch::Renderer& self, const torch::Tensor& val) {
165
+ self.device_tracker = val;
166
+ })
167
+ .def_property_readonly("width", &pulsar::pytorch::Renderer::width)
168
+ .def_property_readonly("height", &pulsar::pytorch::Renderer::height)
169
+ .def_property_readonly(
170
+ "max_num_balls", &pulsar::pytorch::Renderer::max_num_balls)
171
+ .def_property_readonly(
172
+ "orthogonal", &pulsar::pytorch::Renderer::orthogonal)
173
+ .def_property_readonly(
174
+ "right_handed", &pulsar::pytorch::Renderer::right_handed)
175
+ .def_property_readonly("n_track", &pulsar::pytorch::Renderer::n_track);
176
+ m.def(
177
+ "pulsar_sphere_ids_from_result_info_nograd",
178
+ &pulsar::pytorch::sphere_ids_from_result_info_nograd);
179
+ // Constants.
180
+ m.attr("EPS") = py::float_(EPS);
181
+ m.attr("MAX_FLOAT") = py::float_(MAX_FLOAT);
182
+ m.attr("MAX_INT") = py::int_(MAX_INT);
183
+ m.attr("MAX_UINT") = py::int_(MAX_UINT);
184
+ m.attr("MAX_USHORT") = py::int_(MAX_USHORT);
185
+ m.attr("PULSAR_MAX_GRAD_SPHERES") = py::int_(MAX_GRAD_SPHERES);
186
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/marching_cubes/tables.h ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #pragma once
10
+ using uint = unsigned int;
11
+
12
+ // A table mapping from cubeindex to a list of face configurations.
13
+ // Each list contains at most 5 faces, where each face is represented with
14
+ // 3 consecutive numbers
15
+ // Table adapted from http://paulbourke.net/geometry/polygonise/
16
+ //
17
+ #define X 255
18
+ const unsigned char _FACE_TABLE[256][16] = {
19
+ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X},
20
+ {0, 8, 3, X, X, X, X, X, X, X, X, X, X, X, X, X},
21
+ {0, 1, 9, X, X, X, X, X, X, X, X, X, X, X, X, X},
22
+ {1, 8, 3, 9, 8, 1, X, X, X, X, X, X, X, X, X, X},
23
+ {1, 2, 10, X, X, X, X, X, X, X, X, X, X, X, X, X},
24
+ {0, 8, 3, 1, 2, 10, X, X, X, X, X, X, X, X, X, X},
25
+ {9, 2, 10, 0, 2, 9, X, X, X, X, X, X, X, X, X, X},
26
+ {2, 8, 3, 2, 10, 8, 10, 9, 8, X, X, X, X, X, X, X},
27
+ {3, 11, 2, X, X, X, X, X, X, X, X, X, X, X, X, X},
28
+ {0, 11, 2, 8, 11, 0, X, X, X, X, X, X, X, X, X, X},
29
+ {1, 9, 0, 2, 3, 11, X, X, X, X, X, X, X, X, X, X},
30
+ {1, 11, 2, 1, 9, 11, 9, 8, 11, X, X, X, X, X, X, X},
31
+ {3, 10, 1, 11, 10, 3, X, X, X, X, X, X, X, X, X, X},
32
+ {0, 10, 1, 0, 8, 10, 8, 11, 10, X, X, X, X, X, X, X},
33
+ {3, 9, 0, 3, 11, 9, 11, 10, 9, X, X, X, X, X, X, X},
34
+ {9, 8, 10, 10, 8, 11, X, X, X, X, X, X, X, X, X, X},
35
+ {4, 7, 8, X, X, X, X, X, X, X, X, X, X, X, X, X},
36
+ {4, 3, 0, 7, 3, 4, X, X, X, X, X, X, X, X, X, X},
37
+ {0, 1, 9, 8, 4, 7, X, X, X, X, X, X, X, X, X, X},
38
+ {4, 1, 9, 4, 7, 1, 7, 3, 1, X, X, X, X, X, X, X},
39
+ {1, 2, 10, 8, 4, 7, X, X, X, X, X, X, X, X, X, X},
40
+ {3, 4, 7, 3, 0, 4, 1, 2, 10, X, X, X, X, X, X, X},
41
+ {9, 2, 10, 9, 0, 2, 8, 4, 7, X, X, X, X, X, X, X},
42
+ {2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, X, X, X, X},
43
+ {8, 4, 7, 3, 11, 2, X, X, X, X, X, X, X, X, X, X},
44
+ {11, 4, 7, 11, 2, 4, 2, 0, 4, X, X, X, X, X, X, X},
45
+ {9, 0, 1, 8, 4, 7, 2, 3, 11, X, X, X, X, X, X, X},
46
+ {4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, X, X, X, X},
47
+ {3, 10, 1, 3, 11, 10, 7, 8, 4, X, X, X, X, X, X, X},
48
+ {1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, X, X, X, X},
49
+ {4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, X, X, X, X},
50
+ {4, 7, 11, 4, 11, 9, 9, 11, 10, X, X, X, X, X, X, X},
51
+ {9, 5, 4, X, X, X, X, X, X, X, X, X, X, X, X, X},
52
+ {9, 5, 4, 0, 8, 3, X, X, X, X, X, X, X, X, X, X},
53
+ {0, 5, 4, 1, 5, 0, X, X, X, X, X, X, X, X, X, X},
54
+ {8, 5, 4, 8, 3, 5, 3, 1, 5, X, X, X, X, X, X, X},
55
+ {1, 2, 10, 9, 5, 4, X, X, X, X, X, X, X, X, X, X},
56
+ {3, 0, 8, 1, 2, 10, 4, 9, 5, X, X, X, X, X, X, X},
57
+ {5, 2, 10, 5, 4, 2, 4, 0, 2, X, X, X, X, X, X, X},
58
+ {2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, X, X, X, X},
59
+ {9, 5, 4, 2, 3, 11, X, X, X, X, X, X, X, X, X, X},
60
+ {0, 11, 2, 0, 8, 11, 4, 9, 5, X, X, X, X, X, X, X},
61
+ {0, 5, 4, 0, 1, 5, 2, 3, 11, X, X, X, X, X, X, X},
62
+ {2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, X, X, X, X},
63
+ {10, 3, 11, 10, 1, 3, 9, 5, 4, X, X, X, X, X, X, X},
64
+ {4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, X, X, X, X},
65
+ {5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, X, X, X, X},
66
+ {5, 4, 8, 5, 8, 10, 10, 8, 11, X, X, X, X, X, X, X},
67
+ {9, 7, 8, 5, 7, 9, X, X, X, X, X, X, X, X, X, X},
68
+ {9, 3, 0, 9, 5, 3, 5, 7, 3, X, X, X, X, X, X, X},
69
+ {0, 7, 8, 0, 1, 7, 1, 5, 7, X, X, X, X, X, X, X},
70
+ {1, 5, 3, 3, 5, 7, X, X, X, X, X, X, X, X, X, X},
71
+ {9, 7, 8, 9, 5, 7, 10, 1, 2, X, X, X, X, X, X, X},
72
+ {10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, X, X, X, X},
73
+ {8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, X, X, X, X},
74
+ {2, 10, 5, 2, 5, 3, 3, 5, 7, X, X, X, X, X, X, X},
75
+ {7, 9, 5, 7, 8, 9, 3, 11, 2, X, X, X, X, X, X, X},
76
+ {9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, X, X, X, X},
77
+ {2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, X, X, X, X},
78
+ {11, 2, 1, 11, 1, 7, 7, 1, 5, X, X, X, X, X, X, X},
79
+ {9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, X, X, X, X},
80
+ {5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, X},
81
+ {11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, X},
82
+ {11, 10, 5, 7, 11, 5, X, X, X, X, X, X, X, X, X, X},
83
+ {10, 6, 5, X, X, X, X, X, X, X, X, X, X, X, X, X},
84
+ {0, 8, 3, 5, 10, 6, X, X, X, X, X, X, X, X, X, X},
85
+ {9, 0, 1, 5, 10, 6, X, X, X, X, X, X, X, X, X, X},
86
+ {1, 8, 3, 1, 9, 8, 5, 10, 6, X, X, X, X, X, X, X},
87
+ {1, 6, 5, 2, 6, 1, X, X, X, X, X, X, X, X, X, X},
88
+ {1, 6, 5, 1, 2, 6, 3, 0, 8, X, X, X, X, X, X, X},
89
+ {9, 6, 5, 9, 0, 6, 0, 2, 6, X, X, X, X, X, X, X},
90
+ {5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, X, X, X, X},
91
+ {2, 3, 11, 10, 6, 5, X, X, X, X, X, X, X, X, X, X},
92
+ {11, 0, 8, 11, 2, 0, 10, 6, 5, X, X, X, X, X, X, X},
93
+ {0, 1, 9, 2, 3, 11, 5, 10, 6, X, X, X, X, X, X, X},
94
+ {5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, X, X, X, X},
95
+ {6, 3, 11, 6, 5, 3, 5, 1, 3, X, X, X, X, X, X, X},
96
+ {0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, X, X, X, X},
97
+ {3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, X, X, X, X},
98
+ {6, 5, 9, 6, 9, 11, 11, 9, 8, X, X, X, X, X, X, X},
99
+ {5, 10, 6, 4, 7, 8, X, X, X, X, X, X, X, X, X, X},
100
+ {4, 3, 0, 4, 7, 3, 6, 5, 10, X, X, X, X, X, X, X},
101
+ {1, 9, 0, 5, 10, 6, 8, 4, 7, X, X, X, X, X, X, X},
102
+ {10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, X, X, X, X},
103
+ {6, 1, 2, 6, 5, 1, 4, 7, 8, X, X, X, X, X, X, X},
104
+ {1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, X, X, X, X},
105
+ {8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, X, X, X, X},
106
+ {7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, X},
107
+ {3, 11, 2, 7, 8, 4, 10, 6, 5, X, X, X, X, X, X, X},
108
+ {5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, X, X, X, X},
109
+ {0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, X, X, X, X},
110
+ {9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, X},
111
+ {8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, X, X, X, X},
112
+ {5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, X},
113
+ {0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, X},
114
+ {6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, X, X, X, X},
115
+ {10, 4, 9, 6, 4, 10, X, X, X, X, X, X, X, X, X, X},
116
+ {4, 10, 6, 4, 9, 10, 0, 8, 3, X, X, X, X, X, X, X},
117
+ {10, 0, 1, 10, 6, 0, 6, 4, 0, X, X, X, X, X, X, X},
118
+ {8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, X, X, X, X},
119
+ {1, 4, 9, 1, 2, 4, 2, 6, 4, X, X, X, X, X, X, X},
120
+ {3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, X, X, X, X},
121
+ {0, 2, 4, 4, 2, 6, X, X, X, X, X, X, X, X, X, X},
122
+ {8, 3, 2, 8, 2, 4, 4, 2, 6, X, X, X, X, X, X, X},
123
+ {10, 4, 9, 10, 6, 4, 11, 2, 3, X, X, X, X, X, X, X},
124
+ {0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, X, X, X, X},
125
+ {3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, X, X, X, X},
126
+ {6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, X},
127
+ {9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, X, X, X, X},
128
+ {8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, X},
129
+ {3, 11, 6, 3, 6, 0, 0, 6, 4, X, X, X, X, X, X, X},
130
+ {6, 4, 8, 11, 6, 8, X, X, X, X, X, X, X, X, X, X},
131
+ {7, 10, 6, 7, 8, 10, 8, 9, 10, X, X, X, X, X, X, X},
132
+ {0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, X, X, X, X},
133
+ {10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, X, X, X, X},
134
+ {10, 6, 7, 10, 7, 1, 1, 7, 3, X, X, X, X, X, X, X},
135
+ {1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, X, X, X, X},
136
+ {2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, X},
137
+ {7, 8, 0, 7, 0, 6, 6, 0, 2, X, X, X, X, X, X, X},
138
+ {7, 3, 2, 6, 7, 2, X, X, X, X, X, X, X, X, X, X},
139
+ {2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, X, X, X, X},
140
+ {2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, X},
141
+ {1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, X},
142
+ {11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, X, X, X, X},
143
+ {8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, X},
144
+ {0, 9, 1, 11, 6, 7, X, X, X, X, X, X, X, X, X, X},
145
+ {7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, X, X, X, X},
146
+ {7, 11, 6, X, X, X, X, X, X, X, X, X, X, X, X, X},
147
+ {7, 6, 11, X, X, X, X, X, X, X, X, X, X, X, X, X},
148
+ {3, 0, 8, 11, 7, 6, X, X, X, X, X, X, X, X, X, X},
149
+ {0, 1, 9, 11, 7, 6, X, X, X, X, X, X, X, X, X, X},
150
+ {8, 1, 9, 8, 3, 1, 11, 7, 6, X, X, X, X, X, X, X},
151
+ {10, 1, 2, 6, 11, 7, X, X, X, X, X, X, X, X, X, X},
152
+ {1, 2, 10, 3, 0, 8, 6, 11, 7, X, X, X, X, X, X, X},
153
+ {2, 9, 0, 2, 10, 9, 6, 11, 7, X, X, X, X, X, X, X},
154
+ {6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, X, X, X, X},
155
+ {7, 2, 3, 6, 2, 7, X, X, X, X, X, X, X, X, X, X},
156
+ {7, 0, 8, 7, 6, 0, 6, 2, 0, X, X, X, X, X, X, X},
157
+ {2, 7, 6, 2, 3, 7, 0, 1, 9, X, X, X, X, X, X, X},
158
+ {1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, X, X, X, X},
159
+ {10, 7, 6, 10, 1, 7, 1, 3, 7, X, X, X, X, X, X, X},
160
+ {10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, X, X, X, X},
161
+ {0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, X, X, X, X},
162
+ {7, 6, 10, 7, 10, 8, 8, 10, 9, X, X, X, X, X, X, X},
163
+ {6, 8, 4, 11, 8, 6, X, X, X, X, X, X, X, X, X, X},
164
+ {3, 6, 11, 3, 0, 6, 0, 4, 6, X, X, X, X, X, X, X},
165
+ {8, 6, 11, 8, 4, 6, 9, 0, 1, X, X, X, X, X, X, X},
166
+ {9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, X, X, X, X},
167
+ {6, 8, 4, 6, 11, 8, 2, 10, 1, X, X, X, X, X, X, X},
168
+ {1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, X, X, X, X},
169
+ {4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, X, X, X, X},
170
+ {10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, X},
171
+ {8, 2, 3, 8, 4, 2, 4, 6, 2, X, X, X, X, X, X, X},
172
+ {0, 4, 2, 4, 6, 2, X, X, X, X, X, X, X, X, X, X},
173
+ {1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, X, X, X, X},
174
+ {1, 9, 4, 1, 4, 2, 2, 4, 6, X, X, X, X, X, X, X},
175
+ {8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, X, X, X, X},
176
+ {10, 1, 0, 10, 0, 6, 6, 0, 4, X, X, X, X, X, X, X},
177
+ {4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, X},
178
+ {10, 9, 4, 6, 10, 4, X, X, X, X, X, X, X, X, X, X},
179
+ {4, 9, 5, 7, 6, 11, X, X, X, X, X, X, X, X, X, X},
180
+ {0, 8, 3, 4, 9, 5, 11, 7, 6, X, X, X, X, X, X, X},
181
+ {5, 0, 1, 5, 4, 0, 7, 6, 11, X, X, X, X, X, X, X},
182
+ {11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, X, X, X, X},
183
+ {9, 5, 4, 10, 1, 2, 7, 6, 11, X, X, X, X, X, X, X},
184
+ {6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, X, X, X, X},
185
+ {7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, X, X, X, X},
186
+ {3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, X},
187
+ {7, 2, 3, 7, 6, 2, 5, 4, 9, X, X, X, X, X, X, X},
188
+ {9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, X, X, X, X},
189
+ {3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, X, X, X, X},
190
+ {6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, X},
191
+ {9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, X, X, X, X},
192
+ {1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, X},
193
+ {4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, X},
194
+ {7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, X, X, X, X},
195
+ {6, 9, 5, 6, 11, 9, 11, 8, 9, X, X, X, X, X, X, X},
196
+ {3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, X, X, X, X},
197
+ {0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, X, X, X, X},
198
+ {6, 11, 3, 6, 3, 5, 5, 3, 1, X, X, X, X, X, X, X},
199
+ {1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, X, X, X, X},
200
+ {0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, X},
201
+ {11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, X},
202
+ {6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, X, X, X, X},
203
+ {5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, X, X, X, X},
204
+ {9, 5, 6, 9, 6, 0, 0, 6, 2, X, X, X, X, X, X, X},
205
+ {1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, X},
206
+ {1, 5, 6, 2, 1, 6, X, X, X, X, X, X, X, X, X, X},
207
+ {1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, X},
208
+ {10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, X, X, X, X},
209
+ {0, 3, 8, 5, 6, 10, X, X, X, X, X, X, X, X, X, X},
210
+ {10, 5, 6, X, X, X, X, X, X, X, X, X, X, X, X, X},
211
+ {11, 5, 10, 7, 5, 11, X, X, X, X, X, X, X, X, X, X},
212
+ {11, 5, 10, 11, 7, 5, 8, 3, 0, X, X, X, X, X, X, X},
213
+ {5, 11, 7, 5, 10, 11, 1, 9, 0, X, X, X, X, X, X, X},
214
+ {10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, X, X, X, X},
215
+ {11, 1, 2, 11, 7, 1, 7, 5, 1, X, X, X, X, X, X, X},
216
+ {0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, X, X, X, X},
217
+ {9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, X, X, X, X},
218
+ {7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, X},
219
+ {2, 5, 10, 2, 3, 5, 3, 7, 5, X, X, X, X, X, X, X},
220
+ {8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, X, X, X, X},
221
+ {9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, X, X, X, X},
222
+ {9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, X},
223
+ {1, 3, 5, 3, 7, 5, X, X, X, X, X, X, X, X, X, X},
224
+ {0, 8, 7, 0, 7, 1, 1, 7, 5, X, X, X, X, X, X, X},
225
+ {9, 0, 3, 9, 3, 5, 5, 3, 7, X, X, X, X, X, X, X},
226
+ {9, 8, 7, 5, 9, 7, X, X, X, X, X, X, X, X, X, X},
227
+ {5, 8, 4, 5, 10, 8, 10, 11, 8, X, X, X, X, X, X, X},
228
+ {5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, X, X, X, X},
229
+ {0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, X, X, X, X},
230
+ {10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, X},
231
+ {2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, X, X, X, X},
232
+ {0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, X},
233
+ {0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, X},
234
+ {9, 4, 5, 2, 11, 3, X, X, X, X, X, X, X, X, X, X},
235
+ {2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, X, X, X, X},
236
+ {5, 10, 2, 5, 2, 4, 4, 2, 0, X, X, X, X, X, X, X},
237
+ {3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, X},
238
+ {5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, X, X, X, X},
239
+ {8, 4, 5, 8, 5, 3, 3, 5, 1, X, X, X, X, X, X, X},
240
+ {0, 4, 5, 1, 0, 5, X, X, X, X, X, X, X, X, X, X},
241
+ {8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, X, X, X, X},
242
+ {9, 4, 5, X, X, X, X, X, X, X, X, X, X, X, X, X},
243
+ {4, 11, 7, 4, 9, 11, 9, 10, 11, X, X, X, X, X, X, X},
244
+ {0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, X, X, X, X},
245
+ {1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, X, X, X, X},
246
+ {3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, X},
247
+ {4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, X, X, X, X},
248
+ {9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, X},
249
+ {11, 7, 4, 11, 4, 2, 2, 4, 0, X, X, X, X, X, X, X},
250
+ {11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, X, X, X, X},
251
+ {2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, X, X, X, X},
252
+ {9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, X},
253
+ {3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, X},
254
+ {1, 10, 2, 8, 7, 4, X, X, X, X, X, X, X, X, X, X},
255
+ {4, 9, 1, 4, 1, 7, 7, 1, 3, X, X, X, X, X, X, X},
256
+ {4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, X, X, X, X},
257
+ {4, 0, 3, 7, 4, 3, X, X, X, X, X, X, X, X, X, X},
258
+ {4, 8, 7, X, X, X, X, X, X, X, X, X, X, X, X, X},
259
+ {9, 10, 8, 10, 11, 8, X, X, X, X, X, X, X, X, X, X},
260
+ {3, 0, 9, 3, 9, 11, 11, 9, 10, X, X, X, X, X, X, X},
261
+ {0, 1, 10, 0, 10, 8, 8, 10, 11, X, X, X, X, X, X, X},
262
+ {3, 1, 10, 11, 3, 10, X, X, X, X, X, X, X, X, X, X},
263
+ {1, 2, 11, 1, 11, 9, 9, 11, 8, X, X, X, X, X, X, X},
264
+ {3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, X, X, X, X},
265
+ {0, 2, 11, 8, 0, 11, X, X, X, X, X, X, X, X, X, X},
266
+ {3, 2, 11, X, X, X, X, X, X, X, X, X, X, X, X, X},
267
+ {2, 3, 8, 2, 8, 10, 10, 8, 9, X, X, X, X, X, X, X},
268
+ {9, 10, 2, 0, 9, 2, X, X, X, X, X, X, X, X, X, X},
269
+ {2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, X, X, X, X},
270
+ {1, 10, 2, X, X, X, X, X, X, X, X, X, X, X, X, X},
271
+ {1, 3, 8, 9, 1, 8, X, X, X, X, X, X, X, X, X, X},
272
+ {0, 9, 1, X, X, X, X, X, X, X, X, X, X, X, X, X},
273
+ {0, 3, 8, X, X, X, X, X, X, X, X, X, X, X, X, X},
274
+ {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X}};
275
+ #undef X
276
+
277
+ // Table mapping each edge to the corresponding cube vertices offsets
278
+ const uint _EDGE_TO_VERTICES[12][2] = {
279
+ {0, 1},
280
+ {1, 5},
281
+ {4, 5},
282
+ {0, 4},
283
+ {2, 3},
284
+ {3, 7},
285
+ {6, 7},
286
+ {2, 6},
287
+ {0, 2},
288
+ {1, 3},
289
+ {5, 7},
290
+ {4, 6},
291
+ };
292
+
293
+ // Table mapping from 0-7 to v0-v7 in cube.vertices
294
+ const int _INDEX_TABLE[8] = {0, 1, 5, 4, 2, 3, 7, 6};
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/mesh_normal_consistency/mesh_normal_consistency.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #pragma once
10
+ #include <torch/extension.h>
11
+ #include "utils/pytorch3d_cutils.h"
12
+
13
+ // For mesh_normal_consistency, find pairs of vertices opposite the same edge.
14
+ //
15
+ // Args:
16
+ // edge_num: int64 Tensor of shape (E,) giving the number of vertices
17
+ // corresponding to each edge.
18
+ //
19
+ // Returns:
20
+ // pairs: int64 Tensor of shape (N,2)
21
+
22
+ at::Tensor MeshNormalConsistencyFindVerticesCpu(const at::Tensor& edge_num);
23
+
24
+ // Exposed implementation.
25
+ at::Tensor MeshNormalConsistencyFindVertices(const at::Tensor& edge_num) {
26
+ if (edge_num.is_cuda()) {
27
+ AT_ERROR("This function needs a CPU tensor.");
28
+ }
29
+ return MeshNormalConsistencyFindVerticesCpu(edge_num);
30
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/mesh_normal_consistency/mesh_normal_consistency_cpu.cpp ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include <ATen/ATen.h>
10
+ #include <utility>
11
+ #include <vector>
12
+
13
+ at::Tensor MeshNormalConsistencyFindVerticesCpu(const at::Tensor& edge_num) {
14
+ // We take a LongTensor of shape (E,) giving the number of things intersecting
15
+ // each edge. The things are taken to be numbered in order.
16
+ // (In fact, the "things" are opposite vertices to edges, renumbered).
17
+ // We return a tensor of shape (?, 2) where for every pair of things which
18
+ // intersect the same edge there is a row of their numbers in the output.
19
+
20
+ // Example possible inputs and outputs (order of output is not specified):
21
+ // [1,0,1,1,0] => [[]]
22
+ // [3] => [[0,1], [0,2], [1,2]]
23
+ // [0,3] => [[0,1], [0,2], [1,2]]
24
+ // [1,3] => [[1,2], [1,3], [2,3]]
25
+ //[1,0,2,1,0,2] => [[1,2], [4,5]]
26
+
27
+ const auto num_edges = edge_num.size(0);
28
+ auto edges_a = edge_num.accessor<int64_t, 1>();
29
+
30
+ int64_t vert_idx = 0;
31
+ std::vector<std::pair<int64_t, int64_t>> pairs;
32
+ for (int64_t i_edge = 0; i_edge < num_edges; ++i_edge) {
33
+ int64_t e = edges_a[i_edge];
34
+ for (int64_t j = 0; j < e; ++j) {
35
+ for (int64_t i = 0; i < j; ++i) {
36
+ pairs.emplace_back(vert_idx + i, vert_idx + j);
37
+ }
38
+ }
39
+ vert_idx += e;
40
+ }
41
+
42
+ // Convert from std::vector by copying over the items to a new empty torch
43
+ // tensor.
44
+ auto pairs_tensor = at::empty({(int64_t)pairs.size(), 2}, edge_num.options());
45
+ auto pairs_a = pairs_tensor.accessor<int64_t, 2>();
46
+ for (int64_t i_pair = 0; i_pair < pairs.size(); ++i_pair) {
47
+ auto accessor = pairs_a[i_pair];
48
+ accessor[0] = pairs[i_pair].first;
49
+ accessor[1] = pairs[i_pair].second;
50
+ }
51
+
52
+ return pairs_tensor;
53
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/packed_to_padded_tensor/packed_to_padded_tensor.cu ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include <ATen/ATen.h>
10
+ #include <ATen/cuda/CUDAContext.h>
11
+ #include <c10/cuda/CUDAGuard.h>
12
+
13
+ // Kernel for inputs_packed of shape (F, D), where D > 1
14
+ template <typename scalar_t>
15
+ __global__ void PackedToPaddedKernel(
16
+ const scalar_t* __restrict__ inputs_packed,
17
+ const int64_t* __restrict__ first_idxs,
18
+ scalar_t* __restrict__ inputs_padded,
19
+ const size_t batch_size,
20
+ const size_t max_size,
21
+ const size_t num_inputs,
22
+ const size_t D) {
23
+ // Batch elements split evenly across blocks (num blocks = batch_size) and
24
+ // values for each element split across threads in the block. Each thread adds
25
+ // the values of its respective input elements to the global inputs_padded
26
+ // tensor.
27
+ const size_t tid = threadIdx.x;
28
+ const size_t batch_idx = blockIdx.x;
29
+
30
+ const int64_t start = first_idxs[batch_idx];
31
+ const int64_t end =
32
+ batch_idx + 1 < batch_size ? first_idxs[batch_idx + 1] : num_inputs;
33
+ const int num = end - start;
34
+ for (size_t f = tid; f < num; f += blockDim.x) {
35
+ for (size_t j = 0; j < D; ++j) {
36
+ inputs_padded[batch_idx * max_size * D + f * D + j] =
37
+ inputs_packed[(start + f) * D + j];
38
+ }
39
+ }
40
+ }
41
+
42
+ // Kernel for inputs of shape (F, 1)
43
+ template <typename scalar_t>
44
+ __global__ void PackedToPaddedKernelD1(
45
+ const scalar_t* __restrict__ inputs_packed,
46
+ const int64_t* __restrict__ first_idxs,
47
+ scalar_t* __restrict__ inputs_padded,
48
+ const size_t batch_size,
49
+ const size_t max_size,
50
+ const size_t num_inputs) {
51
+ // Batch elements split evenly across blocks (num blocks = batch_size) and
52
+ // values for each element split across threads in the block. Each thread adds
53
+ // the values of its respective input elements to the global inputs_padded
54
+ // tensor.
55
+ const size_t tid = threadIdx.x;
56
+ const size_t batch_idx = blockIdx.x;
57
+
58
+ const int64_t start = first_idxs[batch_idx];
59
+ const int64_t end =
60
+ batch_idx + 1 < batch_size ? first_idxs[batch_idx + 1] : num_inputs;
61
+ const int num = end - start;
62
+ for (size_t f = tid; f < num; f += blockDim.x) {
63
+ inputs_padded[batch_idx * max_size + f] = inputs_packed[start + f];
64
+ }
65
+ }
66
+
67
+ // Kernel for inputs_padded of shape (B, F, D), where D > 1
68
+ template <typename scalar_t>
69
+ __global__ void PaddedToPackedKernel(
70
+ const scalar_t* __restrict__ inputs_padded,
71
+ const int64_t* __restrict__ first_idxs,
72
+ scalar_t* __restrict__ inputs_packed,
73
+ const size_t batch_size,
74
+ const size_t max_size,
75
+ const size_t num_inputs,
76
+ const size_t D) {
77
+ // Batch elements split evenly across blocks (num blocks = batch_size) and
78
+ // values for each element split across threads in the block. Each thread adds
79
+ // the values of its respective input elements to the global inputs_packed
80
+ // tensor.
81
+ const size_t tid = threadIdx.x;
82
+ const size_t batch_idx = blockIdx.x;
83
+
84
+ const int64_t start = first_idxs[batch_idx];
85
+ const int64_t end =
86
+ batch_idx + 1 < batch_size ? first_idxs[batch_idx + 1] : num_inputs;
87
+ const int num = end - start;
88
+ for (size_t f = tid; f < num; f += blockDim.x) {
89
+ for (size_t j = 0; j < D; ++j) {
90
+ inputs_packed[(start + f) * D + j] =
91
+ inputs_padded[batch_idx * max_size * D + f * D + j];
92
+ }
93
+ }
94
+ }
95
+
96
+ // Kernel for inputs_padded of shape (B, F, 1)
97
+ template <typename scalar_t>
98
+ __global__ void PaddedToPackedKernelD1(
99
+ const scalar_t* __restrict__ inputs_padded,
100
+ const int64_t* __restrict__ first_idxs,
101
+ scalar_t* __restrict__ inputs_packed,
102
+ const size_t batch_size,
103
+ const size_t max_size,
104
+ const size_t num_inputs) {
105
+ // Batch elements split evenly across blocks (num blocks = batch_size) and
106
+ // values for each element split across threads in the block. Each thread adds
107
+ // the values of its respective input elements to the global inputs_packed
108
+ // tensor.
109
+ const size_t tid = threadIdx.x;
110
+ const size_t batch_idx = blockIdx.x;
111
+
112
+ const int64_t start = first_idxs[batch_idx];
113
+ const int64_t end =
114
+ batch_idx + 1 < batch_size ? first_idxs[batch_idx + 1] : num_inputs;
115
+ const int num = end - start;
116
+ for (size_t f = tid; f < num; f += blockDim.x) {
117
+ inputs_packed[start + f] = inputs_padded[batch_idx * max_size + f];
118
+ }
119
+ }
120
+
121
+ at::Tensor PackedToPaddedCuda(
122
+ const at::Tensor inputs_packed,
123
+ const at::Tensor first_idxs,
124
+ const int64_t max_size) {
125
+ // Check inputs are on the same device
126
+ at::TensorArg inputs_packed_t{inputs_packed, "inputs_packed", 1},
127
+ first_idxs_t{first_idxs, "first_idxs", 2};
128
+ at::CheckedFrom c = "PackedToPaddedCuda";
129
+ at::checkAllSameGPU(c, {inputs_packed_t, first_idxs_t});
130
+
131
+ // Set the device for the kernel launch based on the device of the input
132
+ at::cuda::CUDAGuard device_guard(inputs_packed.device());
133
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
134
+
135
+ const int64_t num_inputs = inputs_packed.size(0);
136
+ const int64_t batch_size = first_idxs.size(0);
137
+
138
+ TORCH_CHECK(
139
+ inputs_packed.dim() == 2, "inputs_packed must be a 2-dimensional tensor");
140
+ const int64_t D = inputs_packed.size(1);
141
+ at::Tensor inputs_padded =
142
+ at::zeros({batch_size, max_size, D}, inputs_packed.options());
143
+
144
+ if (inputs_padded.numel() == 0) {
145
+ AT_CUDA_CHECK(cudaGetLastError());
146
+ return inputs_padded;
147
+ }
148
+
149
+ const int threads = 512;
150
+ const int blocks = batch_size;
151
+ if (D == 1) {
152
+ AT_DISPATCH_FLOATING_TYPES(
153
+ inputs_packed.scalar_type(), "packed_to_padded_d1_kernel", ([&] {
154
+ PackedToPaddedKernelD1<scalar_t><<<blocks, threads, 0, stream>>>(
155
+ inputs_packed.contiguous().data_ptr<scalar_t>(),
156
+ first_idxs.contiguous().data_ptr<int64_t>(),
157
+ inputs_padded.data_ptr<scalar_t>(),
158
+ batch_size,
159
+ max_size,
160
+ num_inputs);
161
+ }));
162
+ } else {
163
+ AT_DISPATCH_FLOATING_TYPES(
164
+ inputs_packed.scalar_type(), "packed_to_padded_kernel", ([&] {
165
+ PackedToPaddedKernel<scalar_t><<<blocks, threads, 0, stream>>>(
166
+ inputs_packed.contiguous().data_ptr<scalar_t>(),
167
+ first_idxs.contiguous().data_ptr<int64_t>(),
168
+ inputs_padded.data_ptr<scalar_t>(),
169
+ batch_size,
170
+ max_size,
171
+ num_inputs,
172
+ D);
173
+ }));
174
+ }
175
+
176
+ AT_CUDA_CHECK(cudaGetLastError());
177
+ return inputs_padded;
178
+ }
179
+
180
+ at::Tensor PaddedToPackedCuda(
181
+ const at::Tensor inputs_padded,
182
+ const at::Tensor first_idxs,
183
+ const int64_t num_inputs) {
184
+ // Check inputs are on the same device
185
+ at::TensorArg inputs_padded_t{inputs_padded, "inputs_padded", 1},
186
+ first_idxs_t{first_idxs, "first_idxs", 2};
187
+ at::CheckedFrom c = "PaddedToPackedCuda";
188
+ at::checkAllSameGPU(c, {inputs_padded_t, first_idxs_t});
189
+
190
+ // Set the device for the kernel launch based on the device of the input
191
+ at::cuda::CUDAGuard device_guard(inputs_padded.device());
192
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
193
+
194
+ const int64_t batch_size = inputs_padded.size(0);
195
+ const int64_t max_size = inputs_padded.size(1);
196
+
197
+ TORCH_CHECK(batch_size == first_idxs.size(0), "sizes mismatch");
198
+ TORCH_CHECK(
199
+ inputs_padded.dim() == 3,
200
+ "inputs_padded must be a 3-dimensional tensor");
201
+ const int64_t D = inputs_padded.size(2);
202
+
203
+ at::Tensor inputs_packed =
204
+ at::zeros({num_inputs, D}, inputs_padded.options());
205
+
206
+ if (inputs_packed.numel() == 0) {
207
+ AT_CUDA_CHECK(cudaGetLastError());
208
+ return inputs_packed;
209
+ }
210
+
211
+ const int threads = 512;
212
+ const int blocks = batch_size;
213
+
214
+ if (D == 1) {
215
+ AT_DISPATCH_FLOATING_TYPES(
216
+ inputs_padded.scalar_type(), "padded_to_packed_d1_kernel", ([&] {
217
+ PaddedToPackedKernelD1<scalar_t><<<blocks, threads, 0, stream>>>(
218
+ inputs_padded.contiguous().data_ptr<scalar_t>(),
219
+ first_idxs.contiguous().data_ptr<int64_t>(),
220
+ inputs_packed.data_ptr<scalar_t>(),
221
+ batch_size,
222
+ max_size,
223
+ num_inputs);
224
+ }));
225
+ } else {
226
+ AT_DISPATCH_FLOATING_TYPES(
227
+ inputs_padded.scalar_type(), "padded_to_packed_kernel", ([&] {
228
+ PaddedToPackedKernel<scalar_t><<<blocks, threads, 0, stream>>>(
229
+ inputs_padded.contiguous().data_ptr<scalar_t>(),
230
+ first_idxs.contiguous().data_ptr<int64_t>(),
231
+ inputs_packed.data_ptr<scalar_t>(),
232
+ batch_size,
233
+ max_size,
234
+ num_inputs,
235
+ D);
236
+ }));
237
+ }
238
+
239
+ AT_CUDA_CHECK(cudaGetLastError());
240
+ return inputs_packed;
241
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/packed_to_padded_tensor/packed_to_padded_tensor.h ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #pragma once
10
+ #include <torch/extension.h>
11
+ #include "utils/pytorch3d_cutils.h"
12
+
13
+ // PackedToPadded
14
+ // Converts a packed tensor into a padded tensor, restoring the batch dimension.
15
+ // Refer to pytorch3d/structures/meshes.py for details on packed/padded tensors.
16
+ //
17
+ // Inputs:
18
+ // inputs_packed: FloatTensor of shape (F, D), representing the packed batch
19
+ // tensor, e.g. areas for faces in a batch of meshes.
20
+ // first_idxs: LongTensor of shape (N,) where N is the number of
21
+ // elements in the batch and `first_idxs[i] = f`
22
+ // means that the inputs for batch element i begin at
23
+ // `inputs[f]`.
24
+ // max_size: Max length of an element in the batch.
25
+ // Returns:
26
+ // inputs_padded: FloatTensor of shape (N, max_size, D) where max_size is max
27
+ // of `sizes`. The values for batch element i which start at
28
+ // `inputs_packed[first_idxs[i]]` will be copied to
29
+ // `inputs_padded[i, :]`, with zeros padding out the extra
30
+ // inputs.
31
+ //
32
+
33
+ // PaddedToPacked
34
+ // Converts a padded tensor into a packed tensor.
35
+ // Refer to pytorch3d/structures/meshes.py for details on packed/padded tensors.
36
+ //
37
+ // Inputs:
38
+ // inputs_padded: FloatTensor of shape (N, max_size, D), representing the
39
+ // padded tensor, e.g. areas for faces in a batch of meshes.
40
+ // first_idxs: LongTensor of shape (N,) where N is the number of
41
+ // elements in the batch and `first_idxs[i] = f`
42
+ // means that the inputs for batch element i begin at
43
+ // `inputs_packed[f]`.
44
+ // num_inputs: Number of packed entries (= F)
45
+ // Returns:
46
+ // inputs_packed: FloatTensor of shape (F, D), where
47
+ // `inputs_packed[first_idx[i]:] = inputs_padded[i, :]`.
48
+ //
49
+ //
50
+
51
+ // Cpu implementation.
52
+ at::Tensor PackedToPaddedCpu(
53
+ const at::Tensor inputs_packed,
54
+ const at::Tensor first_idxs,
55
+ const int64_t max_size);
56
+
57
+ // Cpu implementation.
58
+ at::Tensor PaddedToPackedCpu(
59
+ const at::Tensor inputs_padded,
60
+ const at::Tensor first_idxs,
61
+ const int64_t num_inputs);
62
+
63
+ #ifdef WITH_CUDA
64
+ // Cuda implementation.
65
+ at::Tensor PackedToPaddedCuda(
66
+ const at::Tensor inputs_packed,
67
+ const at::Tensor first_idxs,
68
+ const int64_t max_size);
69
+
70
+ // Cuda implementation.
71
+ at::Tensor PaddedToPackedCuda(
72
+ const at::Tensor inputs_padded,
73
+ const at::Tensor first_idxs,
74
+ const int64_t num_inputs);
75
+ #endif
76
+
77
+ // Implementation which is exposed.
78
+ at::Tensor PackedToPadded(
79
+ const at::Tensor inputs_packed,
80
+ const at::Tensor first_idxs,
81
+ const int64_t max_size) {
82
+ if (inputs_packed.is_cuda()) {
83
+ #ifdef WITH_CUDA
84
+ CHECK_CUDA(inputs_packed);
85
+ CHECK_CUDA(first_idxs);
86
+ return PackedToPaddedCuda(inputs_packed, first_idxs, max_size);
87
+ #else
88
+ AT_ERROR("Not compiled with GPU support.");
89
+ #endif
90
+ }
91
+ return PackedToPaddedCpu(inputs_packed, first_idxs, max_size);
92
+ }
93
+
94
+ // Implementation which is exposed.
95
+ at::Tensor PaddedToPacked(
96
+ const at::Tensor inputs_padded,
97
+ const at::Tensor first_idxs,
98
+ const int64_t num_inputs) {
99
+ if (inputs_padded.is_cuda()) {
100
+ #ifdef WITH_CUDA
101
+ CHECK_CUDA(inputs_padded);
102
+ CHECK_CUDA(first_idxs);
103
+ return PaddedToPackedCuda(inputs_padded, first_idxs, num_inputs);
104
+ #else
105
+ AT_ERROR("Not compiled with GPU support.");
106
+ #endif
107
+ }
108
+ return PaddedToPackedCpu(inputs_padded, first_idxs, num_inputs);
109
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/packed_to_padded_tensor/packed_to_padded_tensor_cpu.cpp ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include <torch/extension.h>
10
+
11
+ at::Tensor PackedToPaddedCpu(
12
+ const at::Tensor inputs_packed,
13
+ const at::Tensor first_idxs,
14
+ const int64_t max_size) {
15
+ const int64_t num_inputs = inputs_packed.size(0);
16
+ const int64_t batch_size = first_idxs.size(0);
17
+
18
+ AT_ASSERTM(
19
+ inputs_packed.dim() == 2, "inputs_packed must be a 2-dimensional tensor");
20
+ const int64_t D = inputs_packed.size(1);
21
+
22
+ torch::Tensor inputs_padded =
23
+ torch::zeros({batch_size, max_size, D}, inputs_packed.options());
24
+
25
+ auto inputs_packed_a = inputs_packed.accessor<float, 2>();
26
+ auto first_idxs_a = first_idxs.accessor<int64_t, 1>();
27
+ auto inputs_padded_a = inputs_padded.accessor<float, 3>();
28
+
29
+ for (int b = 0; b < batch_size; ++b) {
30
+ const int64_t start = first_idxs_a[b];
31
+ const int64_t end = b + 1 < batch_size ? first_idxs_a[b + 1] : num_inputs;
32
+ const int64_t num = end - start;
33
+ for (int i = 0; i < num; ++i) {
34
+ for (int j = 0; j < D; ++j) {
35
+ inputs_padded_a[b][i][j] = inputs_packed_a[start + i][j];
36
+ }
37
+ }
38
+ }
39
+ return inputs_padded;
40
+ }
41
+
42
+ at::Tensor PaddedToPackedCpu(
43
+ const at::Tensor inputs_padded,
44
+ const at::Tensor first_idxs,
45
+ const int64_t num_inputs) {
46
+ const int64_t batch_size = inputs_padded.size(0);
47
+
48
+ AT_ASSERTM(
49
+ inputs_padded.dim() == 3, "inputs_padded must be a 3-dimensional tensor");
50
+ const int64_t D = inputs_padded.size(2);
51
+
52
+ torch::Tensor inputs_packed =
53
+ torch::zeros({num_inputs, D}, inputs_padded.options());
54
+
55
+ auto inputs_padded_a = inputs_padded.accessor<float, 3>();
56
+ auto first_idxs_a = first_idxs.accessor<int64_t, 1>();
57
+ auto inputs_packed_a = inputs_packed.accessor<float, 2>();
58
+
59
+ for (int b = 0; b < batch_size; ++b) {
60
+ const int64_t start = first_idxs_a[b];
61
+ const int64_t end = b + 1 < batch_size ? first_idxs_a[b + 1] : num_inputs;
62
+ const int64_t num = end - start;
63
+ for (int i = 0; i < num; ++i) {
64
+ for (int j = 0; j < D; ++j) {
65
+ inputs_packed_a[start + i][j] = inputs_padded_a[b][i][j];
66
+ }
67
+ }
68
+ }
69
+ return inputs_packed;
70
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/point_mesh/point_mesh_cpu.cpp ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include <torch/extension.h>
10
+ #include <array>
11
+ #include <limits>
12
+ #include "utils/geometry_utils.h"
13
+ #include "utils/vec3.h"
14
+
15
+ // - We start with implementations of simple operations on points, edges and
16
+ // faces. The hull of H points is a point if H=1, an edge if H=2, a face if H=3.
17
+
18
+ template <typename T>
19
+ vec3<T> ExtractPoint(const at::TensorAccessor<T, 1>& t) {
20
+ return vec3<T>(t[0], t[1], t[2]);
21
+ }
22
+
23
+ template <typename Accessor>
24
+ static std::array<vec3<std::remove_pointer_t<typename Accessor::PtrType>>, 1>
25
+ ExtractHullHelper(const Accessor& t, std::array<char, 1> /*tag*/) {
26
+ return {ExtractPoint(t)};
27
+ }
28
+
29
+ template <typename Accessor>
30
+ static std::array<vec3<std::remove_pointer_t<typename Accessor::PtrType>>, 2>
31
+ ExtractHullHelper(const Accessor& t, std::array<char, 2> /*tag*/) {
32
+ return {ExtractPoint(t[0]), ExtractPoint(t[1])};
33
+ }
34
+
35
+ template <typename Accessor>
36
+ static std::array<vec3<std::remove_pointer_t<typename Accessor::PtrType>>, 3>
37
+ ExtractHullHelper(const Accessor& t, std::array<char, 3> /*tag*/) {
38
+ return {ExtractPoint(t[0]), ExtractPoint(t[1]), ExtractPoint(t[2])};
39
+ }
40
+
41
+ template <int H, typename Accessor>
42
+ std::array<vec3<std::remove_pointer_t<typename Accessor::PtrType>>, H>
43
+ ExtractHull(const Accessor& t) {
44
+ std::array<char, H> tag;
45
+ return ExtractHullHelper(t, tag);
46
+ }
47
+
48
+ template <typename T>
49
+ void IncrementPoint(at::TensorAccessor<T, 1>&& t, const vec3<T>& point) {
50
+ t[0] += point.x;
51
+ t[1] += point.y;
52
+ t[2] += point.z;
53
+ }
54
+
55
+ // distance between the convex hull of A points and B points
56
+ // this could be done in c++17 with tuple_cat and invoke
57
+ template <typename T>
58
+ T HullDistance(
59
+ const std::array<vec3<T>, 1>& a,
60
+ const std::array<vec3<T>, 2>& b,
61
+ const double /*min_triangle_area*/) {
62
+ using std::get;
63
+ return PointLine3DistanceForward(get<0>(a), get<0>(b), get<1>(b));
64
+ }
65
+ template <typename T>
66
+ T HullDistance(
67
+ const std::array<vec3<T>, 1>& a,
68
+ const std::array<vec3<T>, 3>& b,
69
+ const double min_triangle_area) {
70
+ using std::get;
71
+ return PointTriangle3DistanceForward(
72
+ get<0>(a), get<0>(b), get<1>(b), get<2>(b), min_triangle_area);
73
+ }
74
+ template <typename T>
75
+ T HullDistance(
76
+ const std::array<vec3<T>, 2>& a,
77
+ const std::array<vec3<T>, 1>& b,
78
+ const double /*min_triangle_area*/) {
79
+ return HullDistance(b, a, 1);
80
+ }
81
+ template <typename T>
82
+ T HullDistance(
83
+ const std::array<vec3<T>, 3>& a,
84
+ const std::array<vec3<T>, 1>& b,
85
+ const double min_triangle_area) {
86
+ return HullDistance(b, a, min_triangle_area);
87
+ }
88
+
89
+ template <typename T>
90
+ void HullHullDistanceBackward(
91
+ const std::array<vec3<T>, 1>& a,
92
+ const std::array<vec3<T>, 2>& b,
93
+ T grad_dist,
94
+ at::TensorAccessor<T, 1>&& grad_a,
95
+ at::TensorAccessor<T, 2>&& grad_b,
96
+ const double /*min_triangle_area*/) {
97
+ using std::get;
98
+ auto res =
99
+ PointLine3DistanceBackward(get<0>(a), get<0>(b), get<1>(b), grad_dist);
100
+ IncrementPoint(std::move(grad_a), get<0>(res));
101
+ IncrementPoint(grad_b[0], get<1>(res));
102
+ IncrementPoint(grad_b[1], get<2>(res));
103
+ }
104
+ template <typename T>
105
+ void HullHullDistanceBackward(
106
+ const std::array<vec3<T>, 1>& a,
107
+ const std::array<vec3<T>, 3>& b,
108
+ T grad_dist,
109
+ at::TensorAccessor<T, 1>&& grad_a,
110
+ at::TensorAccessor<T, 2>&& grad_b,
111
+ const double min_triangle_area) {
112
+ using std::get;
113
+ auto res = PointTriangle3DistanceBackward(
114
+ get<0>(a), get<0>(b), get<1>(b), get<2>(b), grad_dist, min_triangle_area);
115
+ IncrementPoint(std::move(grad_a), get<0>(res));
116
+ IncrementPoint(grad_b[0], get<1>(res));
117
+ IncrementPoint(grad_b[1], get<2>(res));
118
+ IncrementPoint(grad_b[2], get<3>(res));
119
+ }
120
+ template <typename T>
121
+ void HullHullDistanceBackward(
122
+ const std::array<vec3<T>, 3>& a,
123
+ const std::array<vec3<T>, 1>& b,
124
+ T grad_dist,
125
+ at::TensorAccessor<T, 2>&& grad_a,
126
+ at::TensorAccessor<T, 1>&& grad_b,
127
+ const double min_triangle_area) {
128
+ return HullHullDistanceBackward(
129
+ b, a, grad_dist, std::move(grad_b), std::move(grad_a), min_triangle_area);
130
+ }
131
+ template <typename T>
132
+ void HullHullDistanceBackward(
133
+ const std::array<vec3<T>, 2>& a,
134
+ const std::array<vec3<T>, 1>& b,
135
+ T grad_dist,
136
+ at::TensorAccessor<T, 2>&& grad_a,
137
+ at::TensorAccessor<T, 1>&& grad_b,
138
+ const double /*min_triangle_area*/) {
139
+ return HullHullDistanceBackward(
140
+ b, a, grad_dist, std::move(grad_b), std::move(grad_a), 1);
141
+ }
142
+
143
+ template <int H>
144
+ void ValidateShape(const at::Tensor& as) {
145
+ if (H == 1) {
146
+ TORCH_CHECK(as.size(1) == 3);
147
+ } else {
148
+ TORCH_CHECK(as.size(2) == 3 && as.size(1) == H);
149
+ }
150
+ }
151
+
152
+ // ----------- Here begins the implementation of each top-level
153
+ // function using non-type template parameters to
154
+ // implement all the cases in one go. ----------- //
155
+
156
+ template <int H1, int H2>
157
+ std::tuple<at::Tensor, at::Tensor> HullHullDistanceForwardCpu(
158
+ const at::Tensor& as,
159
+ const at::Tensor& as_first_idx,
160
+ const at::Tensor& bs,
161
+ const at::Tensor& bs_first_idx,
162
+ const double min_triangle_area) {
163
+ const int64_t A_N = as.size(0);
164
+ const int64_t B_N = bs.size(0);
165
+ const int64_t BATCHES = as_first_idx.size(0);
166
+
167
+ ValidateShape<H1>(as);
168
+ ValidateShape<H2>(bs);
169
+
170
+ TORCH_CHECK(bs_first_idx.size(0) == BATCHES);
171
+
172
+ // clang-format off
173
+ at::Tensor dists = at::zeros({A_N,}, as.options());
174
+ at::Tensor idxs = at::zeros({A_N,}, as_first_idx.options());
175
+ // clang-format on
176
+
177
+ auto as_a = as.accessor < float, H1 == 1 ? 2 : 3 > ();
178
+ auto bs_a = bs.accessor < float, H2 == 1 ? 2 : 3 > ();
179
+ auto as_first_idx_a = as_first_idx.accessor<int64_t, 1>();
180
+ auto bs_first_idx_a = bs_first_idx.accessor<int64_t, 1>();
181
+ auto dists_a = dists.accessor<float, 1>();
182
+ auto idxs_a = idxs.accessor<int64_t, 1>();
183
+ int64_t a_batch_end = 0;
184
+ int64_t b_batch_start = 0, b_batch_end = 0;
185
+ int64_t batch_idx = 0;
186
+ for (int64_t a_n = 0; a_n < A_N; ++a_n) {
187
+ if (a_n == a_batch_end) {
188
+ ++batch_idx;
189
+ b_batch_start = b_batch_end;
190
+ if (batch_idx == BATCHES) {
191
+ a_batch_end = std::numeric_limits<int64_t>::max();
192
+ b_batch_end = B_N;
193
+ } else {
194
+ a_batch_end = as_first_idx_a[batch_idx];
195
+ b_batch_end = bs_first_idx_a[batch_idx];
196
+ }
197
+ }
198
+ float min_dist = std::numeric_limits<float>::max();
199
+ size_t min_idx = 0;
200
+ auto a = ExtractHull<H1>(as_a[a_n]);
201
+ for (int64_t b_n = b_batch_start; b_n < b_batch_end; ++b_n) {
202
+ float dist =
203
+ HullDistance(a, ExtractHull<H2>(bs_a[b_n]), min_triangle_area);
204
+ if (dist <= min_dist) {
205
+ min_dist = dist;
206
+ min_idx = b_n;
207
+ }
208
+ }
209
+ dists_a[a_n] = min_dist;
210
+ idxs_a[a_n] = min_idx;
211
+ }
212
+
213
+ return std::make_tuple(dists, idxs);
214
+ }
215
+
216
+ template <int H1, int H2>
217
+ std::tuple<at::Tensor, at::Tensor> HullHullDistanceBackwardCpu(
218
+ const at::Tensor& as,
219
+ const at::Tensor& bs,
220
+ const at::Tensor& idx_bs,
221
+ const at::Tensor& grad_dists,
222
+ const double min_triangle_area) {
223
+ const int64_t A_N = as.size(0);
224
+
225
+ TORCH_CHECK(idx_bs.size(0) == A_N);
226
+ TORCH_CHECK(grad_dists.size(0) == A_N);
227
+ ValidateShape<H1>(as);
228
+ ValidateShape<H2>(bs);
229
+
230
+ at::Tensor grad_as = at::zeros_like(as);
231
+ at::Tensor grad_bs = at::zeros_like(bs);
232
+
233
+ auto as_a = as.accessor < float, H1 == 1 ? 2 : 3 > ();
234
+ auto bs_a = bs.accessor < float, H2 == 1 ? 2 : 3 > ();
235
+ auto grad_as_a = grad_as.accessor < float, H1 == 1 ? 2 : 3 > ();
236
+ auto grad_bs_a = grad_bs.accessor < float, H2 == 1 ? 2 : 3 > ();
237
+ auto idx_bs_a = idx_bs.accessor<int64_t, 1>();
238
+ auto grad_dists_a = grad_dists.accessor<float, 1>();
239
+
240
+ for (int64_t a_n = 0; a_n < A_N; ++a_n) {
241
+ auto a = ExtractHull<H1>(as_a[a_n]);
242
+ auto b = ExtractHull<H2>(bs_a[idx_bs_a[a_n]]);
243
+ HullHullDistanceBackward(
244
+ a,
245
+ b,
246
+ grad_dists_a[a_n],
247
+ grad_as_a[a_n],
248
+ grad_bs_a[idx_bs_a[a_n]],
249
+ min_triangle_area);
250
+ }
251
+ return std::make_tuple(grad_as, grad_bs);
252
+ }
253
+
254
+ template <int H>
255
+ torch::Tensor PointHullArrayDistanceForwardCpu(
256
+ const torch::Tensor& points,
257
+ const torch::Tensor& bs,
258
+ const double min_triangle_area) {
259
+ const int64_t P = points.size(0);
260
+ const int64_t B_N = bs.size(0);
261
+
262
+ TORCH_CHECK(points.size(1) == 3, "points must be of shape Px3");
263
+ ValidateShape<H>(bs);
264
+
265
+ at::Tensor dists = at::zeros({P, B_N}, points.options());
266
+ auto points_a = points.accessor<float, 2>();
267
+ auto bs_a = bs.accessor<float, 3>();
268
+ auto dists_a = dists.accessor<float, 2>();
269
+ for (int64_t p = 0; p < P; ++p) {
270
+ auto point = ExtractHull<1>(points_a[p]);
271
+ auto dest = dists_a[p];
272
+ for (int64_t b_n = 0; b_n < B_N; ++b_n) {
273
+ auto b = ExtractHull<H>(bs_a[b_n]);
274
+ dest[b_n] = HullDistance(point, b, min_triangle_area);
275
+ }
276
+ }
277
+ return dists;
278
+ }
279
+
280
+ template <int H>
281
+ std::tuple<at::Tensor, at::Tensor> PointHullArrayDistanceBackwardCpu(
282
+ const at::Tensor& points,
283
+ const at::Tensor& bs,
284
+ const at::Tensor& grad_dists,
285
+ const double min_triangle_area) {
286
+ const int64_t P = points.size(0);
287
+ const int64_t B_N = bs.size(0);
288
+
289
+ TORCH_CHECK(points.size(1) == 3, "points must be of shape Px3");
290
+ ValidateShape<H>(bs);
291
+ TORCH_CHECK((grad_dists.size(0) == P) && (grad_dists.size(1) == B_N));
292
+
293
+ at::Tensor grad_points = at::zeros({P, 3}, points.options());
294
+ at::Tensor grad_bs = at::zeros({B_N, H, 3}, bs.options());
295
+
296
+ auto points_a = points.accessor<float, 2>();
297
+ auto bs_a = bs.accessor<float, 3>();
298
+ auto grad_dists_a = grad_dists.accessor<float, 2>();
299
+ auto grad_points_a = grad_points.accessor<float, 2>();
300
+ auto grad_bs_a = grad_bs.accessor<float, 3>();
301
+ for (int64_t p = 0; p < P; ++p) {
302
+ auto point = ExtractHull<1>(points_a[p]);
303
+ auto grad_point = grad_points_a[p];
304
+ auto grad_dist = grad_dists_a[p];
305
+ for (int64_t b_n = 0; b_n < B_N; ++b_n) {
306
+ auto b = ExtractHull<H>(bs_a[b_n]);
307
+ HullHullDistanceBackward(
308
+ point,
309
+ b,
310
+ grad_dist[b_n],
311
+ std::move(grad_point),
312
+ grad_bs_a[b_n],
313
+ min_triangle_area);
314
+ }
315
+ }
316
+ return std::make_tuple(grad_points, grad_bs);
317
+ }
318
+
319
+ // ---------- Here begin the exported functions ------------ //
320
+
321
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceForwardCpu(
322
+ const torch::Tensor& points,
323
+ const torch::Tensor& points_first_idx,
324
+ const torch::Tensor& tris,
325
+ const torch::Tensor& tris_first_idx,
326
+ const double min_triangle_area) {
327
+ return HullHullDistanceForwardCpu<1, 3>(
328
+ points, points_first_idx, tris, tris_first_idx, min_triangle_area);
329
+ }
330
+
331
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceBackwardCpu(
332
+ const torch::Tensor& points,
333
+ const torch::Tensor& tris,
334
+ const torch::Tensor& idx_points,
335
+ const torch::Tensor& grad_dists,
336
+ const double min_triangle_area) {
337
+ return HullHullDistanceBackwardCpu<1, 3>(
338
+ points, tris, idx_points, grad_dists, min_triangle_area);
339
+ }
340
+
341
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceForwardCpu(
342
+ const torch::Tensor& points,
343
+ const torch::Tensor& points_first_idx,
344
+ const torch::Tensor& tris,
345
+ const torch::Tensor& tris_first_idx,
346
+ const double min_triangle_area) {
347
+ return HullHullDistanceForwardCpu<3, 1>(
348
+ tris, tris_first_idx, points, points_first_idx, min_triangle_area);
349
+ }
350
+
351
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceBackwardCpu(
352
+ const torch::Tensor& points,
353
+ const torch::Tensor& tris,
354
+ const torch::Tensor& idx_tris,
355
+ const torch::Tensor& grad_dists,
356
+ const double min_triangle_area) {
357
+ auto res = HullHullDistanceBackwardCpu<3, 1>(
358
+ tris, points, idx_tris, grad_dists, min_triangle_area);
359
+ return std::make_tuple(std::get<1>(res), std::get<0>(res));
360
+ }
361
+
362
+ torch::Tensor PointEdgeArrayDistanceForwardCpu(
363
+ const torch::Tensor& points,
364
+ const torch::Tensor& segms) {
365
+ return PointHullArrayDistanceForwardCpu<2>(points, segms, 1);
366
+ }
367
+
368
+ std::tuple<at::Tensor, at::Tensor> PointFaceArrayDistanceBackwardCpu(
369
+ const at::Tensor& points,
370
+ const at::Tensor& tris,
371
+ const at::Tensor& grad_dists,
372
+ const double min_triangle_area) {
373
+ return PointHullArrayDistanceBackwardCpu<3>(
374
+ points, tris, grad_dists, min_triangle_area);
375
+ }
376
+
377
+ torch::Tensor PointFaceArrayDistanceForwardCpu(
378
+ const torch::Tensor& points,
379
+ const torch::Tensor& tris,
380
+ const double min_triangle_area) {
381
+ return PointHullArrayDistanceForwardCpu<3>(points, tris, min_triangle_area);
382
+ }
383
+
384
+ std::tuple<at::Tensor, at::Tensor> PointEdgeArrayDistanceBackwardCpu(
385
+ const at::Tensor& points,
386
+ const at::Tensor& segms,
387
+ const at::Tensor& grad_dists) {
388
+ return PointHullArrayDistanceBackwardCpu<2>(points, segms, grad_dists, 1);
389
+ }
390
+
391
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceForwardCpu(
392
+ const torch::Tensor& points,
393
+ const torch::Tensor& points_first_idx,
394
+ const torch::Tensor& segms,
395
+ const torch::Tensor& segms_first_idx,
396
+ const int64_t /*max_points*/) {
397
+ return HullHullDistanceForwardCpu<1, 2>(
398
+ points, points_first_idx, segms, segms_first_idx, 1);
399
+ }
400
+
401
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceBackwardCpu(
402
+ const torch::Tensor& points,
403
+ const torch::Tensor& segms,
404
+ const torch::Tensor& idx_points,
405
+ const torch::Tensor& grad_dists) {
406
+ return HullHullDistanceBackwardCpu<1, 2>(
407
+ points, segms, idx_points, grad_dists, 1);
408
+ }
409
+
410
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceForwardCpu(
411
+ const torch::Tensor& points,
412
+ const torch::Tensor& points_first_idx,
413
+ const torch::Tensor& segms,
414
+ const torch::Tensor& segms_first_idx,
415
+ const int64_t /*max_segms*/) {
416
+ return HullHullDistanceForwardCpu<2, 1>(
417
+ segms, segms_first_idx, points, points_first_idx, 1);
418
+ }
419
+
420
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceBackwardCpu(
421
+ const torch::Tensor& points,
422
+ const torch::Tensor& segms,
423
+ const torch::Tensor& idx_segms,
424
+ const torch::Tensor& grad_dists) {
425
+ auto res = HullHullDistanceBackwardCpu<2, 1>(
426
+ segms, points, idx_segms, grad_dists, 1);
427
+ return std::make_tuple(std::get<1>(res), std::get<0>(res));
428
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/point_mesh/point_mesh_cuda.cu ADDED
@@ -0,0 +1,833 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include <ATen/ATen.h>
10
+ #include <ATen/cuda/CUDAContext.h>
11
+ #include <c10/cuda/CUDAGuard.h>
12
+ #include <algorithm>
13
+ #include <list>
14
+ #include <queue>
15
+ #include <tuple>
16
+ #include "utils/float_math.cuh"
17
+ #include "utils/geometry_utils.cuh"
18
+ #include "utils/warp_reduce.cuh"
19
+
20
+ // ****************************************************************************
21
+ // * Generic Forward/Backward Kernels *
22
+ // ****************************************************************************
23
+
24
+ __global__ void DistanceForwardKernel(
25
+ const float* __restrict__ objects, // (O * oD * 3)
26
+ const size_t objects_size, // O
27
+ const size_t objects_dim, // oD
28
+ const float* __restrict__ targets, // (T * tD * 3)
29
+ const size_t targets_size, // T
30
+ const size_t targets_dim, // tD
31
+ const int64_t* __restrict__ objects_first_idx, // (B,)
32
+ const int64_t* __restrict__ targets_first_idx, // (B,)
33
+ const size_t batch_size, // B
34
+ float* __restrict__ dist_objects, // (O,)
35
+ int64_t* __restrict__ idx_objects, // (O,)
36
+ const double min_triangle_area) {
37
+ // This kernel is used interchangeably to compute bi-directional distances
38
+ // between points and triangles/lines. The direction of the distance computed,
39
+ // i.e. point to triangle/line or triangle/line to point, depends on the order
40
+ // of the input arguments and is inferred based on their shape. The shape is
41
+ // used to distinguish between triangles and lines
42
+
43
+ // Single shared memory buffer which is split and cast to different types.
44
+ extern __shared__ char shared_buf[];
45
+ float* min_dists = (float*)shared_buf; // float[NUM_THREADS]
46
+ int64_t* min_idxs = (int64_t*)&min_dists[blockDim.x]; // int64_t[NUM_THREADS]
47
+
48
+ const size_t batch_idx = blockIdx.y; // index of batch element.
49
+
50
+ // start and end for objects in batch_idx
51
+ const int64_t starto = objects_first_idx[batch_idx];
52
+ const int64_t endo = batch_idx + 1 < batch_size
53
+ ? objects_first_idx[batch_idx + 1]
54
+ : objects_size;
55
+
56
+ // start and end for targets in batch_idx
57
+ const int64_t startt = targets_first_idx[batch_idx];
58
+ const int64_t endt = batch_idx + 1 < batch_size
59
+ ? targets_first_idx[batch_idx + 1]
60
+ : targets_size;
61
+
62
+ const size_t i = blockIdx.x; // index within batch element.
63
+ const size_t tid = threadIdx.x; // thread index
64
+
65
+ // Set references to points/face based on which of objects/targets refer to
66
+ // points/faces
67
+ float3* points_f3 = objects_dim == 1 ? (float3*)objects : (float3*)targets;
68
+ float3* face_f3 = objects_dim == 1 ? (float3*)targets : (float3*)objects;
69
+ // Distinguishes whether we're computing distance against triangle vs edge
70
+ bool isTriangle = objects_dim == 3 || targets_dim == 3;
71
+
72
+ // Each block will compute one element of the output idx_objects[starto + i],
73
+ // dist_objects[starto + i]. Within the block we will use threads to compute
74
+ // the distances between objects[starto + i] and targets[j] for all j
75
+ // belonging in the same batch as i, i.e. j in [startt, endt]. Then use a
76
+ // block reduction to take an argmin of the distances.
77
+
78
+ // If i exceeds the number of objects in batch_idx, then do nothing
79
+ if (i < (endo - starto)) {
80
+ // Compute the distances between objects[starto + i] and targets[j] for
81
+ // all j belonging in the same batch as i, i.e. j in [startt, endt].
82
+ // Here each thread will reduce over (endt-startt) / blockDim.x in serial,
83
+ // and store its result to shared memory
84
+ float min_dist = FLT_MAX;
85
+ size_t min_idx = 0;
86
+ for (size_t j = tid; j < (endt - startt); j += blockDim.x) {
87
+ size_t point_idx = objects_dim == 1 ? starto + i : startt + j;
88
+ size_t face_idx = objects_dim == 1 ? (startt + j) * targets_dim
89
+ : (starto + i) * objects_dim;
90
+
91
+ float dist;
92
+ if (isTriangle) {
93
+ dist = PointTriangle3DistanceForward(
94
+ points_f3[point_idx],
95
+ face_f3[face_idx],
96
+ face_f3[face_idx + 1],
97
+ face_f3[face_idx + 2],
98
+ min_triangle_area);
99
+ } else {
100
+ dist = PointLine3DistanceForward(
101
+ points_f3[point_idx], face_f3[face_idx], face_f3[face_idx + 1]);
102
+ }
103
+
104
+ min_dist = (j == tid) ? dist : min_dist;
105
+ min_idx = (dist <= min_dist) ? (startt + j) : min_idx;
106
+ min_dist = (dist <= min_dist) ? dist : min_dist;
107
+ }
108
+ min_dists[tid] = min_dist;
109
+ min_idxs[tid] = min_idx;
110
+ __syncthreads();
111
+
112
+ // Perform reduction in shared memory.
113
+ for (int s = blockDim.x / 2; s > 32; s >>= 1) {
114
+ if (tid < s) {
115
+ if (min_dists[tid] > min_dists[tid + s]) {
116
+ min_dists[tid] = min_dists[tid + s];
117
+ min_idxs[tid] = min_idxs[tid + s];
118
+ }
119
+ }
120
+ __syncthreads();
121
+ }
122
+
123
+ // Unroll the last 6 iterations of the loop since they will happen
124
+ // synchronized within a single warp.
125
+ if (tid < 32)
126
+ WarpReduceMin<float>(min_dists, min_idxs, tid);
127
+
128
+ // Finally thread 0 writes the result to the output buffer.
129
+ if (tid == 0) {
130
+ idx_objects[starto + i] = min_idxs[0];
131
+ dist_objects[starto + i] = min_dists[0];
132
+ }
133
+ }
134
+ }
135
+
136
+ std::tuple<at::Tensor, at::Tensor> DistanceForwardCuda(
137
+ const at::Tensor& objects,
138
+ const size_t objects_dim,
139
+ const at::Tensor& objects_first_idx,
140
+ const at::Tensor& targets,
141
+ const size_t targets_dim,
142
+ const at::Tensor& targets_first_idx,
143
+ const int64_t max_objects,
144
+ const double min_triangle_area) {
145
+ // Check inputs are on the same device
146
+ at::TensorArg objects_t{objects, "objects", 1},
147
+ objects_first_idx_t{objects_first_idx, "objects_first_idx", 2},
148
+ targets_t{targets, "targets", 3},
149
+ targets_first_idx_t{targets_first_idx, "targets_first_idx", 4};
150
+ at::CheckedFrom c = "DistanceForwardCuda";
151
+ at::checkAllSameGPU(
152
+ c, {objects_t, objects_first_idx_t, targets_t, targets_first_idx_t});
153
+ at::checkAllSameType(c, {objects_t, targets_t});
154
+
155
+ // Set the device for the kernel launch based on the device of the input
156
+ at::cuda::CUDAGuard device_guard(objects.device());
157
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
158
+
159
+ const int64_t objects_size = objects.size(0);
160
+ const int64_t targets_size = targets.size(0);
161
+ const int64_t batch_size = objects_first_idx.size(0);
162
+
163
+ TORCH_CHECK(targets_first_idx.size(0) == batch_size);
164
+ if (objects_dim == 1) {
165
+ TORCH_CHECK(
166
+ targets_dim >= 2 && targets_dim <= 3,
167
+ "either object or target must be edge or face");
168
+ TORCH_CHECK(objects.size(1) == 3, "points must be of shape Px3");
169
+ TORCH_CHECK(
170
+ targets.size(2) == 3,
171
+ "face must be of shape Tx3x3, lines must be of shape Tx2x3");
172
+ } else {
173
+ TORCH_CHECK(targets_dim == 1, "either object or target must be point");
174
+ TORCH_CHECK(
175
+ objects_dim >= 2 && objects_dim <= 3,
176
+ "either object or target must be edge or face");
177
+ TORCH_CHECK(targets.size(1) == 3, "points must be of shape Px3");
178
+ TORCH_CHECK(
179
+ objects.size(2) == 3,
180
+ "face must be of shape Tx3x3, lines must be of shape Tx2x3");
181
+ }
182
+
183
+ // clang-format off
184
+ at::Tensor dists = at::zeros({objects_size,}, objects.options());
185
+ at::Tensor idxs = at::zeros({objects_size,}, objects_first_idx.options());
186
+ // clang-format on
187
+
188
+ if (dists.numel() == 0) {
189
+ AT_CUDA_CHECK(cudaGetLastError());
190
+ return std::make_tuple(dists, idxs);
191
+ }
192
+
193
+ const int threads = 128;
194
+ const dim3 blocks(max_objects, batch_size);
195
+ size_t shared_size = threads * sizeof(size_t) + threads * sizeof(int64_t);
196
+
197
+ DistanceForwardKernel<<<blocks, threads, shared_size, stream>>>(
198
+ objects.contiguous().data_ptr<float>(),
199
+ objects_size,
200
+ objects_dim,
201
+ targets.contiguous().data_ptr<float>(),
202
+ targets_size,
203
+ targets_dim,
204
+ objects_first_idx.contiguous().data_ptr<int64_t>(),
205
+ targets_first_idx.contiguous().data_ptr<int64_t>(),
206
+ batch_size,
207
+ dists.data_ptr<float>(),
208
+ idxs.data_ptr<int64_t>(),
209
+ min_triangle_area);
210
+
211
+ AT_CUDA_CHECK(cudaGetLastError());
212
+ return std::make_tuple(dists, idxs);
213
+ }
214
+
215
+ __global__ void DistanceBackwardKernel(
216
+ const float* __restrict__ objects, // (O * oD * 3)
217
+ const size_t objects_size, // O
218
+ const size_t objects_dim, // oD
219
+ const float* __restrict__ targets, // (T * tD * 3)
220
+ const size_t targets_dim, // tD
221
+ const int64_t* __restrict__ idx_objects, // (O,)
222
+ const float* __restrict__ grad_dists, // (O,)
223
+ float* __restrict__ grad_points, // ((O or T) * 3)
224
+ float* __restrict__ grad_face, // ((O or T) * max(oD, tD) * 3)
225
+ const double min_triangle_area) {
226
+ // This kernel is used interchangeably to compute bi-directional backward
227
+ // distances between points and triangles/lines. The direction of the distance
228
+ // computed, i.e. point to triangle/line or triangle/line to point, depends on
229
+ // the order of the input arguments and is inferred based on their shape. The
230
+ // shape is used to distinguish between triangles and lines. Note that
231
+ // grad_points will always be used for the point data and grad_face for the
232
+ // edge/triangle
233
+
234
+ // Set references to points/face based on whether objects/targets are which
235
+ float3* points_f3 = objects_dim == 1 ? (float3*)objects : (float3*)targets;
236
+ float3* face_f3 = objects_dim == 1 ? (float3*)targets : (float3*)objects;
237
+
238
+ const size_t tid = blockIdx.x * blockDim.x + threadIdx.x;
239
+ const size_t stride = gridDim.x * blockDim.x;
240
+
241
+ for (size_t o = tid; o < objects_size; o += stride) {
242
+ const int64_t tidx = idx_objects[o];
243
+
244
+ size_t point_index = objects_dim == 1 ? o : tidx;
245
+ size_t face_index = objects_dim == 1 ? tidx * targets_dim : o * objects_dim;
246
+ bool isTriangle = objects_dim == 3 || targets_dim == 3;
247
+
248
+ float3 grad_point, grad_v0, grad_v1, grad_v2;
249
+ if (isTriangle) {
250
+ const auto grads = PointTriangle3DistanceBackward(
251
+ points_f3[point_index],
252
+ face_f3[face_index],
253
+ face_f3[face_index + 1],
254
+ face_f3[face_index + 2],
255
+ grad_dists[o],
256
+ min_triangle_area);
257
+ grad_point = thrust::get<0>(grads);
258
+ grad_v0 = thrust::get<1>(grads);
259
+ grad_v1 = thrust::get<2>(grads);
260
+ grad_v2 = thrust::get<3>(grads);
261
+ } else {
262
+ const auto grads = PointLine3DistanceBackward(
263
+ points_f3[point_index],
264
+ face_f3[face_index],
265
+ face_f3[face_index + 1],
266
+ grad_dists[o]);
267
+ grad_point = thrust::get<0>(grads);
268
+ grad_v0 = thrust::get<1>(grads);
269
+ grad_v1 = thrust::get<2>(grads);
270
+ }
271
+
272
+ atomicAdd(grad_points + point_index * 3 + 0, grad_point.x);
273
+ atomicAdd(grad_points + point_index * 3 + 1, grad_point.y);
274
+ atomicAdd(grad_points + point_index * 3 + 2, grad_point.z);
275
+
276
+ atomicAdd(grad_face + face_index * 3 + 0 * 3 + 0, grad_v0.x);
277
+ atomicAdd(grad_face + face_index * 3 + 0 * 3 + 1, grad_v0.y);
278
+ atomicAdd(grad_face + face_index * 3 + 0 * 3 + 2, grad_v0.z);
279
+
280
+ atomicAdd(grad_face + face_index * 3 + 1 * 3 + 0, grad_v1.x);
281
+ atomicAdd(grad_face + face_index * 3 + 1 * 3 + 1, grad_v1.y);
282
+ atomicAdd(grad_face + face_index * 3 + 1 * 3 + 2, grad_v1.z);
283
+
284
+ if (isTriangle) {
285
+ atomicAdd(grad_face + face_index * 3 + 2 * 3 + 0, grad_v2.x);
286
+ atomicAdd(grad_face + face_index * 3 + 2 * 3 + 1, grad_v2.y);
287
+ atomicAdd(grad_face + face_index * 3 + 2 * 3 + 2, grad_v2.z);
288
+ }
289
+ }
290
+ }
291
+
292
+ std::tuple<at::Tensor, at::Tensor> DistanceBackwardCuda(
293
+ const at::Tensor& objects,
294
+ const size_t objects_dim,
295
+ const at::Tensor& targets,
296
+ const size_t targets_dim,
297
+ const at::Tensor& idx_objects,
298
+ const at::Tensor& grad_dists,
299
+ const double min_triangle_area) {
300
+ // Check inputs are on the same device
301
+ at::TensorArg objects_t{objects, "objects", 1},
302
+ targets_t{targets, "targets", 2},
303
+ idx_objects_t{idx_objects, "idx_objects", 3},
304
+ grad_dists_t{grad_dists, "grad_dists", 4};
305
+ at::CheckedFrom c = "DistanceBackwardCuda";
306
+ at::checkAllSameGPU(c, {objects_t, targets_t, idx_objects_t, grad_dists_t});
307
+ at::checkAllSameType(c, {objects_t, targets_t, grad_dists_t});
308
+ // This is nondeterministic because atomicAdd
309
+ at::globalContext().alertNotDeterministic("DistanceBackwardCuda");
310
+
311
+ // Set the device for the kernel launch based on the device of the input
312
+ at::cuda::CUDAGuard device_guard(objects.device());
313
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
314
+
315
+ const int64_t objects_size = objects.size(0);
316
+ const int64_t targets_size = targets.size(0);
317
+
318
+ at::Tensor grad_points;
319
+ at::Tensor grad_tris;
320
+
321
+ TORCH_CHECK(idx_objects.size(0) == objects_size);
322
+ TORCH_CHECK(grad_dists.size(0) == objects_size);
323
+ if (objects_dim == 1) {
324
+ TORCH_CHECK(
325
+ targets_dim >= 2 && targets_dim <= 3,
326
+ "either object or target must be edge or face");
327
+ TORCH_CHECK(objects.size(1) == 3, "points must be of shape Px3");
328
+ TORCH_CHECK(
329
+ targets.size(2) == 3,
330
+ "face must be of shape Tx3x3, lines must be of shape Tx2x3");
331
+ // clang-format off
332
+ grad_points = at::zeros({objects_size, 3}, objects.options());
333
+ grad_tris = at::zeros({targets_size, int64_t(targets_dim), 3}, targets.options());
334
+ // clang-format on
335
+ } else {
336
+ TORCH_CHECK(targets_dim == 1, "either object or target must be point");
337
+ TORCH_CHECK(
338
+ objects_dim >= 2 && objects_dim <= 3,
339
+ "either object or target must be edge or face");
340
+ TORCH_CHECK(targets.size(1) == 3, "points must be of shape Px3");
341
+ TORCH_CHECK(
342
+ objects.size(2) == 3,
343
+ "face must be of shape Tx3x3, lines must be of shape Tx2x3");
344
+ // clang-format off
345
+ grad_points = at::zeros({targets_size, 3}, targets.options());
346
+ grad_tris = at::zeros({objects_size, int64_t(objects_dim), 3}, objects.options());
347
+ // clang-format on
348
+ }
349
+
350
+ if (grad_points.numel() == 0 || grad_tris.numel() == 0) {
351
+ AT_CUDA_CHECK(cudaGetLastError());
352
+ return std::make_tuple(grad_points, grad_tris);
353
+ }
354
+
355
+ const int blocks = 64;
356
+ const int threads = 512;
357
+
358
+ DistanceBackwardKernel<<<blocks, threads, 0, stream>>>(
359
+ objects.contiguous().data_ptr<float>(),
360
+ objects_size,
361
+ objects_dim,
362
+ targets.contiguous().data_ptr<float>(),
363
+ targets_dim,
364
+ idx_objects.contiguous().data_ptr<int64_t>(),
365
+ grad_dists.contiguous().data_ptr<float>(),
366
+ grad_points.data_ptr<float>(),
367
+ grad_tris.data_ptr<float>(),
368
+ min_triangle_area);
369
+
370
+ AT_CUDA_CHECK(cudaGetLastError());
371
+ return std::make_tuple(grad_points, grad_tris);
372
+ }
373
+
374
+ // ****************************************************************************
375
+ // * PointFaceDistance *
376
+ // ****************************************************************************
377
+
378
+ std::tuple<at::Tensor, at::Tensor> PointFaceDistanceForwardCuda(
379
+ const at::Tensor& points,
380
+ const at::Tensor& points_first_idx,
381
+ const at::Tensor& tris,
382
+ const at::Tensor& tris_first_idx,
383
+ const int64_t max_points,
384
+ const double min_triangle_area) {
385
+ return DistanceForwardCuda(
386
+ points,
387
+ 1,
388
+ points_first_idx,
389
+ tris,
390
+ 3,
391
+ tris_first_idx,
392
+ max_points,
393
+ min_triangle_area);
394
+ }
395
+
396
+ std::tuple<at::Tensor, at::Tensor> PointFaceDistanceBackwardCuda(
397
+ const at::Tensor& points,
398
+ const at::Tensor& tris,
399
+ const at::Tensor& idx_points,
400
+ const at::Tensor& grad_dists,
401
+ const double min_triangle_area) {
402
+ return DistanceBackwardCuda(
403
+ points, 1, tris, 3, idx_points, grad_dists, min_triangle_area);
404
+ }
405
+
406
+ // ****************************************************************************
407
+ // * FacePointDistance *
408
+ // ****************************************************************************
409
+
410
+ std::tuple<at::Tensor, at::Tensor> FacePointDistanceForwardCuda(
411
+ const at::Tensor& points,
412
+ const at::Tensor& points_first_idx,
413
+ const at::Tensor& tris,
414
+ const at::Tensor& tris_first_idx,
415
+ const int64_t max_tris,
416
+ const double min_triangle_area) {
417
+ return DistanceForwardCuda(
418
+ tris,
419
+ 3,
420
+ tris_first_idx,
421
+ points,
422
+ 1,
423
+ points_first_idx,
424
+ max_tris,
425
+ min_triangle_area);
426
+ }
427
+
428
+ std::tuple<at::Tensor, at::Tensor> FacePointDistanceBackwardCuda(
429
+ const at::Tensor& points,
430
+ const at::Tensor& tris,
431
+ const at::Tensor& idx_tris,
432
+ const at::Tensor& grad_dists,
433
+ const double min_triangle_area) {
434
+ return DistanceBackwardCuda(
435
+ tris, 3, points, 1, idx_tris, grad_dists, min_triangle_area);
436
+ }
437
+
438
+ // ****************************************************************************
439
+ // * PointEdgeDistance *
440
+ // ****************************************************************************
441
+
442
+ std::tuple<at::Tensor, at::Tensor> PointEdgeDistanceForwardCuda(
443
+ const at::Tensor& points,
444
+ const at::Tensor& points_first_idx,
445
+ const at::Tensor& segms,
446
+ const at::Tensor& segms_first_idx,
447
+ const int64_t max_points) {
448
+ return DistanceForwardCuda(
449
+ points,
450
+ 1,
451
+ points_first_idx,
452
+ segms,
453
+ 2,
454
+ segms_first_idx,
455
+ max_points,
456
+ 1); // todo: unused parameter handling for min_triangle_area
457
+ }
458
+
459
+ std::tuple<at::Tensor, at::Tensor> PointEdgeDistanceBackwardCuda(
460
+ const at::Tensor& points,
461
+ const at::Tensor& segms,
462
+ const at::Tensor& idx_points,
463
+ const at::Tensor& grad_dists) {
464
+ return DistanceBackwardCuda(points, 1, segms, 2, idx_points, grad_dists, 1);
465
+ }
466
+
467
+ // ****************************************************************************
468
+ // * EdgePointDistance *
469
+ // ****************************************************************************
470
+
471
+ std::tuple<at::Tensor, at::Tensor> EdgePointDistanceForwardCuda(
472
+ const at::Tensor& points,
473
+ const at::Tensor& points_first_idx,
474
+ const at::Tensor& segms,
475
+ const at::Tensor& segms_first_idx,
476
+ const int64_t max_segms) {
477
+ return DistanceForwardCuda(
478
+ segms, 2, segms_first_idx, points, 1, points_first_idx, max_segms, 1);
479
+ }
480
+
481
+ std::tuple<at::Tensor, at::Tensor> EdgePointDistanceBackwardCuda(
482
+ const at::Tensor& points,
483
+ const at::Tensor& segms,
484
+ const at::Tensor& idx_segms,
485
+ const at::Tensor& grad_dists) {
486
+ return DistanceBackwardCuda(segms, 2, points, 1, idx_segms, grad_dists, 1);
487
+ }
488
+
489
+ // ****************************************************************************
490
+ // * PointFaceArrayDistance *
491
+ // ****************************************************************************
492
+ // TODO: Create wrapper function and merge kernel with other array kernel
493
+
494
+ __global__ void PointFaceArrayForwardKernel(
495
+ const float* __restrict__ points, // (P, 3)
496
+ const float* __restrict__ tris, // (T, 3, 3)
497
+ float* __restrict__ dists, // (P, T)
498
+ const size_t P,
499
+ const size_t T,
500
+ const double min_triangle_area) {
501
+ const float3* points_f3 = (float3*)points;
502
+ const float3* tris_f3 = (float3*)tris;
503
+
504
+ // Parallelize over P * S computations
505
+ const int num_threads = gridDim.x * blockDim.x;
506
+ const int tid = blockIdx.x * blockDim.x + threadIdx.x;
507
+
508
+ for (int t_i = tid; t_i < P * T; t_i += num_threads) {
509
+ const int t = t_i / P; // segment index.
510
+ const int p = t_i % P; // point index
511
+ const float3 v0 = tris_f3[t * 3 + 0];
512
+ const float3 v1 = tris_f3[t * 3 + 1];
513
+ const float3 v2 = tris_f3[t * 3 + 2];
514
+
515
+ const float3 point = points_f3[p];
516
+ float dist =
517
+ PointTriangle3DistanceForward(point, v0, v1, v2, min_triangle_area);
518
+ dists[p * T + t] = dist;
519
+ }
520
+ }
521
+
522
+ at::Tensor PointFaceArrayDistanceForwardCuda(
523
+ const at::Tensor& points,
524
+ const at::Tensor& tris,
525
+ const double min_triangle_area) {
526
+ // Check inputs are on the same device
527
+ at::TensorArg points_t{points, "points", 1}, tris_t{tris, "tris", 2};
528
+ at::CheckedFrom c = "PointFaceArrayDistanceForwardCuda";
529
+ at::checkAllSameGPU(c, {points_t, tris_t});
530
+ at::checkAllSameType(c, {points_t, tris_t});
531
+
532
+ // Set the device for the kernel launch based on the device of the input
533
+ at::cuda::CUDAGuard device_guard(points.device());
534
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
535
+
536
+ const int64_t P = points.size(0);
537
+ const int64_t T = tris.size(0);
538
+
539
+ TORCH_CHECK(points.size(1) == 3, "points must be of shape Px3");
540
+ TORCH_CHECK(
541
+ (tris.size(1) == 3) && (tris.size(2) == 3),
542
+ "tris must be of shape Tx3x3");
543
+
544
+ at::Tensor dists = at::zeros({P, T}, points.options());
545
+
546
+ if (dists.numel() == 0) {
547
+ AT_CUDA_CHECK(cudaGetLastError());
548
+ return dists;
549
+ }
550
+
551
+ const size_t blocks = 1024;
552
+ const size_t threads = 64;
553
+
554
+ PointFaceArrayForwardKernel<<<blocks, threads, 0, stream>>>(
555
+ points.contiguous().data_ptr<float>(),
556
+ tris.contiguous().data_ptr<float>(),
557
+ dists.data_ptr<float>(),
558
+ P,
559
+ T,
560
+ min_triangle_area);
561
+
562
+ AT_CUDA_CHECK(cudaGetLastError());
563
+ return dists;
564
+ }
565
+
566
+ __global__ void PointFaceArrayBackwardKernel(
567
+ const float* __restrict__ points, // (P, 3)
568
+ const float* __restrict__ tris, // (T, 3, 3)
569
+ const float* __restrict__ grad_dists, // (P, T)
570
+ float* __restrict__ grad_points, // (P, 3)
571
+ float* __restrict__ grad_tris, // (T, 3, 3)
572
+ const size_t P,
573
+ const size_t T,
574
+ const double min_triangle_area) {
575
+ const float3* points_f3 = (float3*)points;
576
+ const float3* tris_f3 = (float3*)tris;
577
+
578
+ // Parallelize over P * S computations
579
+ const int num_threads = gridDim.x * blockDim.x;
580
+ const int tid = blockIdx.x * blockDim.x + threadIdx.x;
581
+
582
+ for (int t_i = tid; t_i < P * T; t_i += num_threads) {
583
+ const int t = t_i / P; // triangle index.
584
+ const int p = t_i % P; // point index
585
+ const float3 v0 = tris_f3[t * 3 + 0];
586
+ const float3 v1 = tris_f3[t * 3 + 1];
587
+ const float3 v2 = tris_f3[t * 3 + 2];
588
+
589
+ const float3 point = points_f3[p];
590
+
591
+ const float grad_dist = grad_dists[p * T + t];
592
+ const auto grad = PointTriangle3DistanceBackward(
593
+ point, v0, v1, v2, grad_dist, min_triangle_area);
594
+
595
+ const float3 grad_point = thrust::get<0>(grad);
596
+ const float3 grad_v0 = thrust::get<1>(grad);
597
+ const float3 grad_v1 = thrust::get<2>(grad);
598
+ const float3 grad_v2 = thrust::get<3>(grad);
599
+
600
+ atomicAdd(grad_points + 3 * p + 0, grad_point.x);
601
+ atomicAdd(grad_points + 3 * p + 1, grad_point.y);
602
+ atomicAdd(grad_points + 3 * p + 2, grad_point.z);
603
+
604
+ atomicAdd(grad_tris + t * 3 * 3 + 0 * 3 + 0, grad_v0.x);
605
+ atomicAdd(grad_tris + t * 3 * 3 + 0 * 3 + 1, grad_v0.y);
606
+ atomicAdd(grad_tris + t * 3 * 3 + 0 * 3 + 2, grad_v0.z);
607
+
608
+ atomicAdd(grad_tris + t * 3 * 3 + 1 * 3 + 0, grad_v1.x);
609
+ atomicAdd(grad_tris + t * 3 * 3 + 1 * 3 + 1, grad_v1.y);
610
+ atomicAdd(grad_tris + t * 3 * 3 + 1 * 3 + 2, grad_v1.z);
611
+
612
+ atomicAdd(grad_tris + t * 3 * 3 + 2 * 3 + 0, grad_v2.x);
613
+ atomicAdd(grad_tris + t * 3 * 3 + 2 * 3 + 1, grad_v2.y);
614
+ atomicAdd(grad_tris + t * 3 * 3 + 2 * 3 + 2, grad_v2.z);
615
+ }
616
+ }
617
+
618
+ std::tuple<at::Tensor, at::Tensor> PointFaceArrayDistanceBackwardCuda(
619
+ const at::Tensor& points,
620
+ const at::Tensor& tris,
621
+ const at::Tensor& grad_dists,
622
+ const double min_triangle_area) {
623
+ // Check inputs are on the same device
624
+ at::TensorArg points_t{points, "points", 1}, tris_t{tris, "tris", 2},
625
+ grad_dists_t{grad_dists, "grad_dists", 3};
626
+ at::CheckedFrom c = "PointFaceArrayDistanceBackwardCuda";
627
+ at::checkAllSameGPU(c, {points_t, tris_t, grad_dists_t});
628
+ at::checkAllSameType(c, {points_t, tris_t, grad_dists_t});
629
+ // This is nondeterministic because atomicAdd
630
+ at::globalContext().alertNotDeterministic(
631
+ "PointFaceArrayDistanceBackwardCuda");
632
+
633
+ // Set the device for the kernel launch based on the device of the input
634
+ at::cuda::CUDAGuard device_guard(points.device());
635
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
636
+
637
+ const int64_t P = points.size(0);
638
+ const int64_t T = tris.size(0);
639
+
640
+ TORCH_CHECK(points.size(1) == 3, "points must be of shape Px3");
641
+ TORCH_CHECK(
642
+ (tris.size(1) == 3) && (tris.size(2) == 3),
643
+ "tris must be of shape Tx3x3");
644
+ TORCH_CHECK((grad_dists.size(0) == P) && (grad_dists.size(1) == T));
645
+
646
+ at::Tensor grad_points = at::zeros({P, 3}, points.options());
647
+ at::Tensor grad_tris = at::zeros({T, 3, 3}, tris.options());
648
+
649
+ if (grad_points.numel() == 0 || grad_tris.numel() == 0) {
650
+ AT_CUDA_CHECK(cudaGetLastError());
651
+ return std::make_tuple(grad_points, grad_tris);
652
+ }
653
+
654
+ const size_t blocks = 1024;
655
+ const size_t threads = 64;
656
+
657
+ PointFaceArrayBackwardKernel<<<blocks, threads, 0, stream>>>(
658
+ points.contiguous().data_ptr<float>(),
659
+ tris.contiguous().data_ptr<float>(),
660
+ grad_dists.contiguous().data_ptr<float>(),
661
+ grad_points.data_ptr<float>(),
662
+ grad_tris.data_ptr<float>(),
663
+ P,
664
+ T,
665
+ min_triangle_area);
666
+
667
+ AT_CUDA_CHECK(cudaGetLastError());
668
+ return std::make_tuple(grad_points, grad_tris);
669
+ }
670
+
671
+ // ****************************************************************************
672
+ // * PointEdgeArrayDistance *
673
+ // ****************************************************************************
674
+ // TODO: Create wrapper function and merge kernel with other array kernel
675
+
676
+ __global__ void PointEdgeArrayForwardKernel(
677
+ const float* __restrict__ points, // (P, 3)
678
+ const float* __restrict__ segms, // (S, 2, 3)
679
+ float* __restrict__ dists, // (P, S)
680
+ const size_t P,
681
+ const size_t S) {
682
+ float3* points_f3 = (float3*)points;
683
+ float3* segms_f3 = (float3*)segms;
684
+
685
+ // Parallelize over P * S computations
686
+ const int num_threads = gridDim.x * blockDim.x;
687
+ const int tid = blockIdx.x * blockDim.x + threadIdx.x;
688
+
689
+ for (int t_i = tid; t_i < P * S; t_i += num_threads) {
690
+ const int s = t_i / P; // segment index.
691
+ const int p = t_i % P; // point index
692
+ float3 a = segms_f3[s * 2 + 0];
693
+ float3 b = segms_f3[s * 2 + 1];
694
+
695
+ float3 point = points_f3[p];
696
+ float dist = PointLine3DistanceForward(point, a, b);
697
+ dists[p * S + s] = dist;
698
+ }
699
+ }
700
+
701
+ at::Tensor PointEdgeArrayDistanceForwardCuda(
702
+ const at::Tensor& points,
703
+ const at::Tensor& segms) {
704
+ // Check inputs are on the same device
705
+ at::TensorArg points_t{points, "points", 1}, segms_t{segms, "segms", 2};
706
+ at::CheckedFrom c = "PointEdgeArrayDistanceForwardCuda";
707
+ at::checkAllSameGPU(c, {points_t, segms_t});
708
+ at::checkAllSameType(c, {points_t, segms_t});
709
+
710
+ // Set the device for the kernel launch based on the device of the input
711
+ at::cuda::CUDAGuard device_guard(points.device());
712
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
713
+
714
+ const int64_t P = points.size(0);
715
+ const int64_t S = segms.size(0);
716
+
717
+ TORCH_CHECK(points.size(1) == 3, "points must be of shape Px3");
718
+ TORCH_CHECK(
719
+ (segms.size(1) == 2) && (segms.size(2) == 3),
720
+ "segms must be of shape Sx2x3");
721
+
722
+ at::Tensor dists = at::zeros({P, S}, points.options());
723
+
724
+ if (dists.numel() == 0) {
725
+ AT_CUDA_CHECK(cudaGetLastError());
726
+ return dists;
727
+ }
728
+
729
+ const size_t blocks = 1024;
730
+ const size_t threads = 64;
731
+
732
+ PointEdgeArrayForwardKernel<<<blocks, threads, 0, stream>>>(
733
+ points.contiguous().data_ptr<float>(),
734
+ segms.contiguous().data_ptr<float>(),
735
+ dists.data_ptr<float>(),
736
+ P,
737
+ S);
738
+
739
+ AT_CUDA_CHECK(cudaGetLastError());
740
+ return dists;
741
+ }
742
+
743
+ __global__ void PointEdgeArrayBackwardKernel(
744
+ const float* __restrict__ points, // (P, 3)
745
+ const float* __restrict__ segms, // (S, 2, 3)
746
+ const float* __restrict__ grad_dists, // (P, S)
747
+ float* __restrict__ grad_points, // (P, 3)
748
+ float* __restrict__ grad_segms, // (S, 2, 3)
749
+ const size_t P,
750
+ const size_t S) {
751
+ float3* points_f3 = (float3*)points;
752
+ float3* segms_f3 = (float3*)segms;
753
+
754
+ // Parallelize over P * S computations
755
+ const int num_threads = gridDim.x * blockDim.x;
756
+ const int tid = blockIdx.x * blockDim.x + threadIdx.x;
757
+
758
+ for (int t_i = tid; t_i < P * S; t_i += num_threads) {
759
+ const int s = t_i / P; // segment index.
760
+ const int p = t_i % P; // point index
761
+ const float3 a = segms_f3[s * 2 + 0];
762
+ const float3 b = segms_f3[s * 2 + 1];
763
+
764
+ const float3 point = points_f3[p];
765
+ const float grad_dist = grad_dists[p * S + s];
766
+ const auto grads = PointLine3DistanceBackward(point, a, b, grad_dist);
767
+ const float3 grad_point = thrust::get<0>(grads);
768
+ const float3 grad_a = thrust::get<1>(grads);
769
+ const float3 grad_b = thrust::get<2>(grads);
770
+
771
+ atomicAdd(grad_points + p * 3 + 0, grad_point.x);
772
+ atomicAdd(grad_points + p * 3 + 1, grad_point.y);
773
+ atomicAdd(grad_points + p * 3 + 2, grad_point.z);
774
+
775
+ atomicAdd(grad_segms + s * 2 * 3 + 0 * 3 + 0, grad_a.x);
776
+ atomicAdd(grad_segms + s * 2 * 3 + 0 * 3 + 1, grad_a.y);
777
+ atomicAdd(grad_segms + s * 2 * 3 + 0 * 3 + 2, grad_a.z);
778
+
779
+ atomicAdd(grad_segms + s * 2 * 3 + 1 * 3 + 0, grad_b.x);
780
+ atomicAdd(grad_segms + s * 2 * 3 + 1 * 3 + 1, grad_b.y);
781
+ atomicAdd(grad_segms + s * 2 * 3 + 1 * 3 + 2, grad_b.z);
782
+ }
783
+ }
784
+
785
+ std::tuple<at::Tensor, at::Tensor> PointEdgeArrayDistanceBackwardCuda(
786
+ const at::Tensor& points,
787
+ const at::Tensor& segms,
788
+ const at::Tensor& grad_dists) {
789
+ // Check inputs are on the same device
790
+ at::TensorArg points_t{points, "points", 1}, segms_t{segms, "segms", 2},
791
+ grad_dists_t{grad_dists, "grad_dists", 3};
792
+ at::CheckedFrom c = "PointEdgeArrayDistanceBackwardCuda";
793
+ at::checkAllSameGPU(c, {points_t, segms_t, grad_dists_t});
794
+ at::checkAllSameType(c, {points_t, segms_t, grad_dists_t});
795
+ // This is nondeterministic because atomicAdd
796
+ at::globalContext().alertNotDeterministic(
797
+ "PointEdgeArrayDistanceBackwardCuda");
798
+
799
+ // Set the device for the kernel launch based on the device of the input
800
+ at::cuda::CUDAGuard device_guard(points.device());
801
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
802
+
803
+ const int64_t P = points.size(0);
804
+ const int64_t S = segms.size(0);
805
+
806
+ TORCH_CHECK(points.size(1) == 3, "points must be of shape Px3");
807
+ TORCH_CHECK(
808
+ (segms.size(1) == 2) && (segms.size(2) == 3),
809
+ "segms must be of shape Sx2x3");
810
+ TORCH_CHECK((grad_dists.size(0) == P) && (grad_dists.size(1) == S));
811
+
812
+ at::Tensor grad_points = at::zeros({P, 3}, points.options());
813
+ at::Tensor grad_segms = at::zeros({S, 2, 3}, segms.options());
814
+
815
+ if (grad_points.numel() == 0 || grad_segms.numel() == 0) {
816
+ AT_CUDA_CHECK(cudaGetLastError());
817
+ return std::make_tuple(grad_points, grad_segms);
818
+ }
819
+
820
+ const size_t blocks = 1024;
821
+ const size_t threads = 64;
822
+
823
+ PointEdgeArrayBackwardKernel<<<blocks, threads, 0, stream>>>(
824
+ points.contiguous().data_ptr<float>(),
825
+ segms.contiguous().data_ptr<float>(),
826
+ grad_dists.contiguous().data_ptr<float>(),
827
+ grad_points.data_ptr<float>(),
828
+ grad_segms.data_ptr<float>(),
829
+ P,
830
+ S);
831
+ AT_CUDA_CHECK(cudaGetLastError());
832
+ return std::make_tuple(grad_points, grad_segms);
833
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/point_mesh/point_mesh_cuda.h ADDED
@@ -0,0 +1,707 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #pragma once
10
+ #include <torch/extension.h>
11
+ #include <cstdio>
12
+ #include <tuple>
13
+ #include "utils/pytorch3d_cutils.h"
14
+
15
+ // ****************************************************************************
16
+ // * PointFaceDistance *
17
+ // ****************************************************************************
18
+
19
+ // Computes the squared euclidean distance of each p in points to it closest
20
+ // triangular face belonging to the corresponding mesh example in the batch of
21
+ // size N.
22
+ //
23
+ // Args:
24
+ // points: FloatTensor of shape (P, 3)
25
+ // points_first_idx: LongTensor of shape (N,) indicating the first point
26
+ // index for each example in the batch
27
+ // tris: FloatTensor of shape (T, 3, 3) of the triangular faces. The t-th
28
+ // triangular face is spanned by (tris[t, 0], tris[t, 1], tris[t, 2])
29
+ // tris_first_idx: LongTensor of shape (N,) indicating the first face
30
+ // index for each example in the batch
31
+ // max_points: Scalar equal to max(P_i) for i in [0, N - 1] containing
32
+ // the maximum number of points in the batch and is used to set
33
+ // the block dimensions in the CUDA implementation.
34
+ // min_triangle_area: triangles less than this size are considered
35
+ // points/lines.
36
+ //
37
+ // Returns:
38
+ // dists: FloatTensor of shape (P,), where dists[p] is the minimum
39
+ // squared euclidean distance of points[p] to the faces in the same
40
+ // example in the batch.
41
+ // idxs: LongTensor of shape (P,), where idxs[p] is the index of the closest
42
+ // face in the batch.
43
+ // So, dists[p] = d(points[p], tris[idxs[p], 0], tris[idxs[p], 1],
44
+ // tris[idxs[p], 2]) where d(u, v0, v1, v2) is the distance of u from the
45
+ // face spanned by (v0, v1, v2)
46
+ //
47
+ //
48
+
49
+ #ifdef WITH_CUDA
50
+
51
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceForwardCuda(
52
+ const torch::Tensor& points,
53
+ const torch::Tensor& points_first_idx,
54
+ const torch::Tensor& tris,
55
+ const torch::Tensor& tris_first_idx,
56
+ const int64_t max_points,
57
+ const double min_triangle_area);
58
+ #endif
59
+
60
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceForwardCpu(
61
+ const torch::Tensor& points,
62
+ const torch::Tensor& points_first_idx,
63
+ const torch::Tensor& tris,
64
+ const torch::Tensor& tris_first_idx,
65
+ const double min_triangle_area);
66
+
67
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceForward(
68
+ const torch::Tensor& points,
69
+ const torch::Tensor& points_first_idx,
70
+ const torch::Tensor& tris,
71
+ const torch::Tensor& tris_first_idx,
72
+ const int64_t max_points,
73
+ const double min_triangle_area) {
74
+ if (points.is_cuda()) {
75
+ #ifdef WITH_CUDA
76
+ CHECK_CUDA(points);
77
+ CHECK_CUDA(points_first_idx);
78
+ CHECK_CUDA(tris);
79
+ CHECK_CUDA(tris_first_idx);
80
+ return PointFaceDistanceForwardCuda(
81
+ points,
82
+ points_first_idx,
83
+ tris,
84
+ tris_first_idx,
85
+ max_points,
86
+ min_triangle_area);
87
+ #else
88
+ AT_ERROR("Not compiled with GPU support.");
89
+ #endif
90
+ }
91
+ return PointFaceDistanceForwardCpu(
92
+ points, points_first_idx, tris, tris_first_idx, min_triangle_area);
93
+ }
94
+
95
+ // Backward pass for PointFaceDistance.
96
+ //
97
+ // Args:
98
+ // points: FloatTensor of shape (P, 3)
99
+ // tris: FloatTensor of shape (T, 3, 3)
100
+ // idx_points: LongTensor of shape (P,) containing the indices
101
+ // of the closest face in the example in the batch.
102
+ // This is computed by the forward pass
103
+ // grad_dists: FloatTensor of shape (P,)
104
+ // min_triangle_area: triangles less than this size are considered
105
+ // points/lines.
106
+ //
107
+ // Returns:
108
+ // grad_points: FloatTensor of shape (P, 3)
109
+ // grad_tris: FloatTensor of shape (T, 3, 3)
110
+ //
111
+
112
+ #ifdef WITH_CUDA
113
+
114
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceBackwardCuda(
115
+ const torch::Tensor& points,
116
+ const torch::Tensor& tris,
117
+ const torch::Tensor& idx_points,
118
+ const torch::Tensor& grad_dists,
119
+ const double min_triangle_area);
120
+ #endif
121
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceBackwardCpu(
122
+ const torch::Tensor& points,
123
+ const torch::Tensor& tris,
124
+ const torch::Tensor& idx_points,
125
+ const torch::Tensor& grad_dists,
126
+ const double min_triangle_area);
127
+
128
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceDistanceBackward(
129
+ const torch::Tensor& points,
130
+ const torch::Tensor& tris,
131
+ const torch::Tensor& idx_points,
132
+ const torch::Tensor& grad_dists,
133
+ const double min_triangle_area) {
134
+ if (points.is_cuda()) {
135
+ #ifdef WITH_CUDA
136
+ CHECK_CUDA(points);
137
+ CHECK_CUDA(tris);
138
+ CHECK_CUDA(idx_points);
139
+ CHECK_CUDA(grad_dists);
140
+ return PointFaceDistanceBackwardCuda(
141
+ points, tris, idx_points, grad_dists, min_triangle_area);
142
+ #else
143
+ AT_ERROR("Not compiled with GPU support.");
144
+ #endif
145
+ }
146
+ return PointFaceDistanceBackwardCpu(
147
+ points, tris, idx_points, grad_dists, min_triangle_area);
148
+ }
149
+
150
+ // ****************************************************************************
151
+ // * FacePointDistance *
152
+ // ****************************************************************************
153
+
154
+ // Computes the squared euclidean distance of each triangular face to its
155
+ // closest point belonging to the corresponding example in the batch of size N.
156
+ //
157
+ // Args:
158
+ // points: FloatTensor of shape (P, 3)
159
+ // points_first_idx: LongTensor of shape (N,) indicating the first point
160
+ // index for each example in the batch
161
+ // tris: FloatTensor of shape (T, 3, 3) of the triangular faces. The t-th
162
+ // triangular face is spanned by (tris[t, 0], tris[t, 1], tris[t, 2])
163
+ // tris_first_idx: LongTensor of shape (N,) indicating the first face
164
+ // index for each example in the batch
165
+ // max_tris: Scalar equal to max(T_i) for i in [0, N - 1] containing
166
+ // the maximum number of faces in the batch and is used to set
167
+ // the block dimensions in the CUDA implementation.
168
+ // min_triangle_area: triangles less than this size are considered
169
+ // points/lines.
170
+ //
171
+ // Returns:
172
+ // dists: FloatTensor of shape (T,), where dists[t] is the minimum squared
173
+ // euclidean distance of t-th triangular face from the closest point in
174
+ // the batch.
175
+ // idxs: LongTensor of shape (T,), where idxs[t] is the index of the closest
176
+ // point in the batch.
177
+ // So, dists[t] = d(points[idxs[t]], tris[t, 0], tris[t, 1], tris[t, 2])
178
+ // where d(u, v0, v1, v2) is the distance of u from the triangular face
179
+ // spanned by (v0, v1, v2)
180
+ //
181
+
182
+ #ifdef WITH_CUDA
183
+
184
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceForwardCuda(
185
+ const torch::Tensor& points,
186
+ const torch::Tensor& points_first_idx,
187
+ const torch::Tensor& tris,
188
+ const torch::Tensor& tris_first_idx,
189
+ const int64_t max_tris,
190
+ const double min_triangle_area);
191
+ #endif
192
+
193
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceForwardCpu(
194
+ const torch::Tensor& points,
195
+ const torch::Tensor& points_first_idx,
196
+ const torch::Tensor& tris,
197
+ const torch::Tensor& tris_first_idx,
198
+ const double min_triangle_area);
199
+
200
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceForward(
201
+ const torch::Tensor& points,
202
+ const torch::Tensor& points_first_idx,
203
+ const torch::Tensor& tris,
204
+ const torch::Tensor& tris_first_idx,
205
+ const int64_t max_tris,
206
+ const double min_triangle_area) {
207
+ if (points.is_cuda()) {
208
+ #ifdef WITH_CUDA
209
+ CHECK_CUDA(points);
210
+ CHECK_CUDA(points_first_idx);
211
+ CHECK_CUDA(tris);
212
+ CHECK_CUDA(tris_first_idx);
213
+ return FacePointDistanceForwardCuda(
214
+ points,
215
+ points_first_idx,
216
+ tris,
217
+ tris_first_idx,
218
+ max_tris,
219
+ min_triangle_area);
220
+ #else
221
+ AT_ERROR("Not compiled with GPU support.");
222
+ #endif
223
+ }
224
+ return FacePointDistanceForwardCpu(
225
+ points, points_first_idx, tris, tris_first_idx, min_triangle_area);
226
+ }
227
+
228
+ // Backward pass for FacePointDistance.
229
+ //
230
+ // Args:
231
+ // points: FloatTensor of shape (P, 3)
232
+ // tris: FloatTensor of shape (T, 3, 3)
233
+ // idx_tris: LongTensor of shape (T,) containing the indices
234
+ // of the closest point in the example in the batch.
235
+ // This is computed by the forward pass
236
+ // grad_dists: FloatTensor of shape (T,)
237
+ // min_triangle_area: triangles less than this size are considered
238
+ // points/lines.
239
+ //
240
+ // Returns:
241
+ // grad_points: FloatTensor of shape (P, 3)
242
+ // grad_tris: FloatTensor of shape (T, 3, 3)
243
+ //
244
+
245
+ #ifdef WITH_CUDA
246
+
247
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceBackwardCuda(
248
+ const torch::Tensor& points,
249
+ const torch::Tensor& tris,
250
+ const torch::Tensor& idx_tris,
251
+ const torch::Tensor& grad_dists,
252
+ const double min_triangle_area);
253
+ #endif
254
+
255
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceBackwardCpu(
256
+ const torch::Tensor& points,
257
+ const torch::Tensor& tris,
258
+ const torch::Tensor& idx_tris,
259
+ const torch::Tensor& grad_dists,
260
+ const double min_triangle_area);
261
+
262
+ std::tuple<torch::Tensor, torch::Tensor> FacePointDistanceBackward(
263
+ const torch::Tensor& points,
264
+ const torch::Tensor& tris,
265
+ const torch::Tensor& idx_tris,
266
+ const torch::Tensor& grad_dists,
267
+ const double min_triangle_area) {
268
+ if (points.is_cuda()) {
269
+ #ifdef WITH_CUDA
270
+ CHECK_CUDA(points);
271
+ CHECK_CUDA(tris);
272
+ CHECK_CUDA(idx_tris);
273
+ CHECK_CUDA(grad_dists);
274
+ return FacePointDistanceBackwardCuda(
275
+ points, tris, idx_tris, grad_dists, min_triangle_area);
276
+ #else
277
+ AT_ERROR("Not compiled with GPU support.");
278
+ #endif
279
+ }
280
+ return FacePointDistanceBackwardCpu(
281
+ points, tris, idx_tris, grad_dists, min_triangle_area);
282
+ }
283
+
284
+ // ****************************************************************************
285
+ // * PointEdgeDistance *
286
+ // ****************************************************************************
287
+
288
+ // Computes the squared euclidean distance of each p in points to the closest
289
+ // mesh edge belonging to the corresponding example in the batch of size N.
290
+ //
291
+ // Args:
292
+ // points: FloatTensor of shape (P, 3)
293
+ // points_first_idx: LongTensor of shape (N,) indicating the first point
294
+ // index for each example in the batch
295
+ // segms: FloatTensor of shape (S, 2, 3) of edge segments. The s-th edge
296
+ // segment is spanned by (segms[s, 0], segms[s, 1])
297
+ // segms_first_idx: LongTensor of shape (N,) indicating the first edge
298
+ // index for each example in the batch
299
+ // max_points: Scalar equal to max(P_i) for i in [0, N - 1] containing
300
+ // the maximum number of points in the batch and is used to set
301
+ // the grid dimensions in the CUDA implementation.
302
+ //
303
+ // Returns:
304
+ // dists: FloatTensor of shape (P,), where dists[p] is the squared euclidean
305
+ // distance of points[p] to the closest edge in the same example in the
306
+ // batch.
307
+ // idxs: LongTensor of shape (P,), where idxs[p] is the index of the closest
308
+ // edge in the batch.
309
+ // So, dists[p] = d(points[p], segms[idxs[p], 0], segms[idxs[p], 1]),
310
+ // where d(u, v0, v1) is the distance of u from the segment spanned by
311
+ // (v0, v1).
312
+ //
313
+
314
+ #ifdef WITH_CUDA
315
+
316
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceForwardCuda(
317
+ const torch::Tensor& points,
318
+ const torch::Tensor& points_first_idx,
319
+ const torch::Tensor& segms,
320
+ const torch::Tensor& segms_first_idx,
321
+ const int64_t max_points);
322
+ #endif
323
+
324
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceForwardCpu(
325
+ const torch::Tensor& points,
326
+ const torch::Tensor& points_first_idx,
327
+ const torch::Tensor& segms,
328
+ const torch::Tensor& segms_first_idx,
329
+ const int64_t max_points);
330
+
331
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceForward(
332
+ const torch::Tensor& points,
333
+ const torch::Tensor& points_first_idx,
334
+ const torch::Tensor& segms,
335
+ const torch::Tensor& segms_first_idx,
336
+ const int64_t max_points) {
337
+ if (points.is_cuda()) {
338
+ #ifdef WITH_CUDA
339
+ CHECK_CUDA(points);
340
+ CHECK_CUDA(points_first_idx);
341
+ CHECK_CUDA(segms);
342
+ CHECK_CUDA(segms_first_idx);
343
+ return PointEdgeDistanceForwardCuda(
344
+ points, points_first_idx, segms, segms_first_idx, max_points);
345
+ #else
346
+ AT_ERROR("Not compiled with GPU support.");
347
+ #endif
348
+ }
349
+ return PointEdgeDistanceForwardCpu(
350
+ points, points_first_idx, segms, segms_first_idx, max_points);
351
+ }
352
+
353
+ // Backward pass for PointEdgeDistance.
354
+ //
355
+ // Args:
356
+ // points: FloatTensor of shape (P, 3)
357
+ // segms: FloatTensor of shape (S, 2, 3)
358
+ // idx_points: LongTensor of shape (P,) containing the indices
359
+ // of the closest edge in the example in the batch.
360
+ // This is computed by the forward pass.
361
+ // grad_dists: FloatTensor of shape (P,)
362
+ //
363
+ // Returns:
364
+ // grad_points: FloatTensor of shape (P, 3)
365
+ // grad_segms: FloatTensor of shape (S, 2, 3)
366
+ //
367
+
368
+ #ifdef WITH_CUDA
369
+
370
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceBackwardCuda(
371
+ const torch::Tensor& points,
372
+ const torch::Tensor& segms,
373
+ const torch::Tensor& idx_points,
374
+ const torch::Tensor& grad_dists);
375
+ #endif
376
+
377
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceBackwardCpu(
378
+ const torch::Tensor& points,
379
+ const torch::Tensor& segms,
380
+ const torch::Tensor& idx_points,
381
+ const torch::Tensor& grad_dists);
382
+
383
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeDistanceBackward(
384
+ const torch::Tensor& points,
385
+ const torch::Tensor& segms,
386
+ const torch::Tensor& idx_points,
387
+ const torch::Tensor& grad_dists) {
388
+ if (points.is_cuda()) {
389
+ #ifdef WITH_CUDA
390
+ CHECK_CUDA(points);
391
+ CHECK_CUDA(segms);
392
+ CHECK_CUDA(idx_points);
393
+ CHECK_CUDA(grad_dists);
394
+ return PointEdgeDistanceBackwardCuda(points, segms, idx_points, grad_dists);
395
+ #else
396
+ AT_ERROR("Not compiled with GPU support.");
397
+ #endif
398
+ }
399
+ return PointEdgeDistanceBackwardCpu(points, segms, idx_points, grad_dists);
400
+ }
401
+
402
+ // ****************************************************************************
403
+ // * EdgePointDistance *
404
+ // ****************************************************************************
405
+
406
+ // Computes the squared euclidean distance of each edge segment to the closest
407
+ // point belonging to the corresponding example in the batch of size N.
408
+ //
409
+ // Args:
410
+ // points: FloatTensor of shape (P, 3)
411
+ // points_first_idx: LongTensor of shape (N,) indicating the first point
412
+ // index for each example in the batch
413
+ // segms: FloatTensor of shape (S, 2, 3) of edge segments. The s-th edge
414
+ // segment is spanned by (segms[s, 0], segms[s, 1])
415
+ // segms_first_idx: LongTensor of shape (N,) indicating the first edge
416
+ // index for each example in the batch
417
+ // max_segms: Scalar equal to max(S_i) for i in [0, N - 1] containing
418
+ // the maximum number of edges in the batch and is used to set
419
+ // the block dimensions in the CUDA implementation.
420
+ //
421
+ // Returns:
422
+ // dists: FloatTensor of shape (S,), where dists[s] is the squared
423
+ // euclidean distance of s-th edge to the closest point in the
424
+ // corresponding example in the batch.
425
+ // idxs: LongTensor of shape (S,), where idxs[s] is the index of the closest
426
+ // point in the example in the batch.
427
+ // So, dists[s] = d(points[idxs[s]], segms[s, 0], segms[s, 1]), where
428
+ // d(u, v0, v1) is the distance of u from the segment spanned by (v0, v1)
429
+ //
430
+ //
431
+
432
+ #ifdef WITH_CUDA
433
+
434
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceForwardCuda(
435
+ const torch::Tensor& points,
436
+ const torch::Tensor& points_first_idx,
437
+ const torch::Tensor& segms,
438
+ const torch::Tensor& segms_first_idx,
439
+ const int64_t max_segms);
440
+ #endif
441
+
442
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceForwardCpu(
443
+ const torch::Tensor& points,
444
+ const torch::Tensor& points_first_idx,
445
+ const torch::Tensor& segms,
446
+ const torch::Tensor& segms_first_idx,
447
+ const int64_t max_segms);
448
+
449
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceForward(
450
+ const torch::Tensor& points,
451
+ const torch::Tensor& points_first_idx,
452
+ const torch::Tensor& segms,
453
+ const torch::Tensor& segms_first_idx,
454
+ const int64_t max_segms) {
455
+ if (points.is_cuda()) {
456
+ #ifdef WITH_CUDA
457
+ CHECK_CUDA(points);
458
+ CHECK_CUDA(points_first_idx);
459
+ CHECK_CUDA(segms);
460
+ CHECK_CUDA(segms_first_idx);
461
+ return EdgePointDistanceForwardCuda(
462
+ points, points_first_idx, segms, segms_first_idx, max_segms);
463
+ #else
464
+ AT_ERROR("Not compiled with GPU support.");
465
+ #endif
466
+ }
467
+ return EdgePointDistanceForwardCpu(
468
+ points, points_first_idx, segms, segms_first_idx, max_segms);
469
+ }
470
+
471
+ // Backward pass for EdgePointDistance.
472
+ //
473
+ // Args:
474
+ // points: FloatTensor of shape (P, 3)
475
+ // segms: FloatTensor of shape (S, 2, 3)
476
+ // idx_segms: LongTensor of shape (S,) containing the indices
477
+ // of the closest point in the example in the batch.
478
+ // This is computed by the forward pass
479
+ // grad_dists: FloatTensor of shape (S,)
480
+ //
481
+ // Returns:
482
+ // grad_points: FloatTensor of shape (P, 3)
483
+ // grad_segms: FloatTensor of shape (S, 2, 3)
484
+ //
485
+
486
+ #ifdef WITH_CUDA
487
+
488
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceBackwardCuda(
489
+ const torch::Tensor& points,
490
+ const torch::Tensor& segms,
491
+ const torch::Tensor& idx_segms,
492
+ const torch::Tensor& grad_dists);
493
+ #endif
494
+
495
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceBackwardCpu(
496
+ const torch::Tensor& points,
497
+ const torch::Tensor& segms,
498
+ const torch::Tensor& idx_segms,
499
+ const torch::Tensor& grad_dists);
500
+
501
+ std::tuple<torch::Tensor, torch::Tensor> EdgePointDistanceBackward(
502
+ const torch::Tensor& points,
503
+ const torch::Tensor& segms,
504
+ const torch::Tensor& idx_segms,
505
+ const torch::Tensor& grad_dists) {
506
+ if (points.is_cuda()) {
507
+ #ifdef WITH_CUDA
508
+ CHECK_CUDA(points);
509
+ CHECK_CUDA(segms);
510
+ CHECK_CUDA(idx_segms);
511
+ CHECK_CUDA(grad_dists);
512
+ return EdgePointDistanceBackwardCuda(points, segms, idx_segms, grad_dists);
513
+ #else
514
+ AT_ERROR("Not compiled with GPU support.");
515
+ #endif
516
+ }
517
+ return EdgePointDistanceBackwardCpu(points, segms, idx_segms, grad_dists);
518
+ }
519
+
520
+ // ****************************************************************************
521
+ // * PointFaceArrayDistance *
522
+ // ****************************************************************************
523
+
524
+ // Computes the squared euclidean distance of each p in points to each
525
+ // triangular face spanned by (v0, v1, v2) in tris.
526
+ //
527
+ // Args:
528
+ // points: FloatTensor of shape (P, 3)
529
+ // tris: FloatTensor of shape (T, 3, 3) of the triangular faces. The t-th
530
+ // triangular face is spanned by (tris[t, 0], tris[t, 1], tris[t, 2])
531
+ // min_triangle_area: triangles less than this size are considered
532
+ // points/lines.
533
+ //
534
+ // Returns:
535
+ // dists: FloatTensor of shape (P, T), where dists[p, t] is the squared
536
+ // euclidean distance of points[p] to the face spanned by (v0, v1, v2)
537
+ // where v0 = tris[t, 0], v1 = tris[t, 1] and v2 = tris[t, 2]
538
+ //
539
+ // For pointcloud and meshes of batch size N, this function requires N
540
+ // computations. The memory occupied is O(NPT) which can become quite large.
541
+ // For example, a medium sized batch with N = 32 with P = 10000 and T = 5000
542
+ // will require for the forward pass 5.8G of memory to store dists.
543
+
544
+ #ifdef WITH_CUDA
545
+
546
+ torch::Tensor PointFaceArrayDistanceForwardCuda(
547
+ const torch::Tensor& points,
548
+ const torch::Tensor& tris,
549
+ const double min_triangle_area);
550
+ #endif
551
+
552
+ torch::Tensor PointFaceArrayDistanceForwardCpu(
553
+ const torch::Tensor& points,
554
+ const torch::Tensor& tris,
555
+ const double min_triangle_area);
556
+
557
+ torch::Tensor PointFaceArrayDistanceForward(
558
+ const torch::Tensor& points,
559
+ const torch::Tensor& tris,
560
+ const double min_triangle_area) {
561
+ if (points.is_cuda()) {
562
+ #ifdef WITH_CUDA
563
+ CHECK_CUDA(points);
564
+ CHECK_CUDA(tris);
565
+ return PointFaceArrayDistanceForwardCuda(points, tris, min_triangle_area);
566
+ #else
567
+ AT_ERROR("Not compiled with GPU support.");
568
+ #endif
569
+ }
570
+ return PointFaceArrayDistanceForwardCpu(points, tris, min_triangle_area);
571
+ }
572
+
573
+ // Backward pass for PointFaceArrayDistance.
574
+ //
575
+ // Args:
576
+ // points: FloatTensor of shape (P, 3)
577
+ // tris: FloatTensor of shape (T, 3, 3)
578
+ // grad_dists: FloatTensor of shape (P, T)
579
+ // min_triangle_area: triangles less than this size are considered
580
+ // points/lines.
581
+ //
582
+ // Returns:
583
+ // grad_points: FloatTensor of shape (P, 3)
584
+ // grad_tris: FloatTensor of shape (T, 3, 3)
585
+ //
586
+
587
+ #ifdef WITH_CUDA
588
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceArrayDistanceBackwardCuda(
589
+ const torch::Tensor& points,
590
+ const torch::Tensor& tris,
591
+ const torch::Tensor& grad_dists,
592
+ const double min_triangle_area);
593
+ #endif
594
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceArrayDistanceBackwardCpu(
595
+ const torch::Tensor& points,
596
+ const torch::Tensor& tris,
597
+ const torch::Tensor& grad_dists,
598
+ const double min_triangle_area);
599
+
600
+ std::tuple<torch::Tensor, torch::Tensor> PointFaceArrayDistanceBackward(
601
+ const torch::Tensor& points,
602
+ const torch::Tensor& tris,
603
+ const torch::Tensor& grad_dists,
604
+ const double min_triangle_area) {
605
+ if (points.is_cuda()) {
606
+ #ifdef WITH_CUDA
607
+ CHECK_CUDA(points);
608
+ CHECK_CUDA(tris);
609
+ CHECK_CUDA(grad_dists);
610
+ return PointFaceArrayDistanceBackwardCuda(
611
+ points, tris, grad_dists, min_triangle_area);
612
+ #else
613
+ AT_ERROR("Not compiled with GPU support.");
614
+ #endif
615
+ }
616
+ return PointFaceArrayDistanceBackwardCpu(
617
+ points, tris, grad_dists, min_triangle_area);
618
+ }
619
+
620
+ // ****************************************************************************
621
+ // * PointEdgeArrayDistance *
622
+ // ****************************************************************************
623
+
624
+ // Computes the squared euclidean distance of each p in points to each edge
625
+ // segment in segms.
626
+ //
627
+ // Args:
628
+ // points: FloatTensor of shape (P, 3)
629
+ // segms: FloatTensor of shape (S, 2, 3) of edge segments. The s-th
630
+ // edge segment is spanned by (segms[s, 0], segms[s, 1])
631
+ //
632
+ // Returns:
633
+ // dists: FloatTensor of shape (P, S), where dists[p, s] is the squared
634
+ // euclidean distance of points[p] to the segment spanned by
635
+ // (segms[s, 0], segms[s, 1])
636
+ //
637
+ // For pointcloud and meshes of batch size N, this function requires N
638
+ // computations. The memory occupied is O(NPS) which can become quite large.
639
+ // For example, a medium sized batch with N = 32 with P = 10000 and S = 5000
640
+ // will require for the forward pass 5.8G of memory to store dists.
641
+
642
+ #ifdef WITH_CUDA
643
+ torch::Tensor PointEdgeArrayDistanceForwardCuda(
644
+ const torch::Tensor& points,
645
+ const torch::Tensor& segms);
646
+ #endif
647
+
648
+ torch::Tensor PointEdgeArrayDistanceForwardCpu(
649
+ const torch::Tensor& points,
650
+ const torch::Tensor& segms);
651
+
652
+ torch::Tensor PointEdgeArrayDistanceForward(
653
+ const torch::Tensor& points,
654
+ const torch::Tensor& segms) {
655
+ if (points.is_cuda()) {
656
+ #ifdef WITH_CUDA
657
+ CHECK_CUDA(points);
658
+ CHECK_CUDA(segms);
659
+ return PointEdgeArrayDistanceForwardCuda(points, segms);
660
+ #else
661
+ AT_ERROR("Not compiled with GPU support.");
662
+ #endif
663
+ }
664
+ return PointEdgeArrayDistanceForwardCpu(points, segms);
665
+ }
666
+
667
+ // Backward pass for PointEdgeArrayDistance.
668
+ //
669
+ // Args:
670
+ // points: FloatTensor of shape (P, 3)
671
+ // segms: FloatTensor of shape (S, 2, 3)
672
+ // grad_dists: FloatTensor of shape (P, S)
673
+ //
674
+ // Returns:
675
+ // grad_points: FloatTensor of shape (P, 3)
676
+ // grad_segms: FloatTensor of shape (S, 2, 3)
677
+ //
678
+
679
+ #ifdef WITH_CUDA
680
+
681
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeArrayDistanceBackwardCuda(
682
+ const torch::Tensor& points,
683
+ const torch::Tensor& segms,
684
+ const torch::Tensor& grad_dists);
685
+ #endif
686
+
687
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeArrayDistanceBackwardCpu(
688
+ const torch::Tensor& points,
689
+ const torch::Tensor& segms,
690
+ const torch::Tensor& grad_dists);
691
+
692
+ std::tuple<torch::Tensor, torch::Tensor> PointEdgeArrayDistanceBackward(
693
+ const torch::Tensor& points,
694
+ const torch::Tensor& segms,
695
+ const torch::Tensor& grad_dists) {
696
+ if (points.is_cuda()) {
697
+ #ifdef WITH_CUDA
698
+ CHECK_CUDA(points);
699
+ CHECK_CUDA(segms);
700
+ CHECK_CUDA(grad_dists);
701
+ return PointEdgeArrayDistanceBackwardCuda(points, segms, grad_dists);
702
+ #else
703
+ AT_ERROR("Not compiled with GPU support.");
704
+ #endif
705
+ }
706
+ return PointEdgeArrayDistanceBackwardCpu(points, segms, grad_dists);
707
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/points_to_volumes/points_to_volumes.cu ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include <ATen/ATen.h>
10
+ #include <ATen/cuda/CUDAContext.h>
11
+ #include <c10/cuda/CUDAGuard.h>
12
+
13
+ using at::PackedTensorAccessor64;
14
+ using at::RestrictPtrTraits;
15
+
16
+ // A chunk of work is blocksize-many points.
17
+ // There are N clouds in the batch, and P points in each cloud.
18
+ // The number of potential chunks to do per cloud is (1+(P-1)/blocksize),
19
+ // which we call chunks_per_cloud.
20
+ // These (N*chunks_per_cloud) chunks are divided among the gridSize-many blocks.
21
+ // In block b, we work on chunks b, b+gridSize, b+2*gridSize etc .
22
+ // In chunk i, we work on cloud (i/chunks_per_cloud) on points starting from
23
+ // blocksize*(i%chunks_per_cloud).
24
+
25
+ // Explanation of the calculation is in the cpp file.
26
+
27
+ // EightDirections(t) runs t(a,b,c) for every combination of boolean a, b, c.
28
+ template <class T>
29
+ static __device__ void EightDirections(T&& t) {
30
+ t(false, false, false);
31
+ t(false, false, true);
32
+ t(false, true, false);
33
+ t(false, true, true);
34
+ t(true, false, false);
35
+ t(true, false, true);
36
+ t(true, true, false);
37
+ t(true, true, true);
38
+ }
39
+
40
+ __global__ void PointsToVolumesForwardKernel(
41
+ const PackedTensorAccessor64<float, 3, RestrictPtrTraits> points_3d,
42
+ const PackedTensorAccessor64<float, 3, RestrictPtrTraits> points_features,
43
+ PackedTensorAccessor64<float, 5, RestrictPtrTraits> volume_densities,
44
+ PackedTensorAccessor64<float, 5, RestrictPtrTraits> volume_features,
45
+ PackedTensorAccessor64<int64_t, 2, RestrictPtrTraits> grid_sizes,
46
+ PackedTensorAccessor64<float, 2, RestrictPtrTraits> mask,
47
+ const float point_weight,
48
+ const bool align_corners,
49
+ const bool splat,
50
+ const int64_t batch_size,
51
+ const int64_t P,
52
+ const int64_t n_features) {
53
+ const int64_t chunks_per_cloud = (1 + (P - 1) / blockDim.x);
54
+ const int64_t chunks_to_do = batch_size * chunks_per_cloud;
55
+ const int scale_offset = align_corners ? 1 : 0;
56
+ const float offset = align_corners ? 0 : 0.5;
57
+ for (int64_t chunk = blockIdx.x; chunk < chunks_to_do; chunk += gridDim.x) {
58
+ const int64_t batch_index = chunk / chunks_per_cloud;
59
+ const int64_t start_point = blockDim.x * (chunk % chunks_per_cloud);
60
+ int64_t point_idx = start_point + threadIdx.x;
61
+ if (point_idx >= P) {
62
+ continue;
63
+ }
64
+ if (mask[batch_index][point_idx] == 0) {
65
+ continue;
66
+ }
67
+ auto volume_densities_aa = volume_densities[batch_index][0];
68
+ auto volume_features_aa = volume_features[batch_index];
69
+ auto point = points_3d[batch_index][point_idx];
70
+ auto point_features = points_features[batch_index][point_idx];
71
+ const int64_t grid_size_x = grid_sizes[batch_index][2];
72
+ const int64_t grid_size_y = grid_sizes[batch_index][1];
73
+ const int64_t grid_size_z = grid_sizes[batch_index][0];
74
+ auto increment_location =
75
+ [&](int64_t x, int64_t y, int64_t z, float weight) {
76
+ if (x >= grid_size_x || y >= grid_size_y || z >= grid_size_z) {
77
+ return;
78
+ }
79
+ if (x < 0 || y < 0 || z < 0) {
80
+ return;
81
+ }
82
+
83
+ atomicAdd(&volume_densities_aa[z][y][x], weight * point_weight);
84
+
85
+ for (int64_t feature_idx = 0; feature_idx < n_features;
86
+ ++feature_idx) {
87
+ atomicAdd(
88
+ &volume_features_aa[feature_idx][z][y][x],
89
+ point_features[feature_idx] * weight * point_weight);
90
+ }
91
+ };
92
+ if (!splat) {
93
+ long x = std::lround(
94
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset);
95
+ long y = std::lround(
96
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset);
97
+ long z = std::lround(
98
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset);
99
+ increment_location(x, y, z, 1);
100
+ } else {
101
+ float x = 0, y = 0, z = 0;
102
+ float rx = std::modf(
103
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset, &x);
104
+ float ry = std::modf(
105
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset, &y);
106
+ float rz = std::modf(
107
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset, &z);
108
+ auto handle_point = [&](bool up_x, bool up_y, bool up_z) {
109
+ float weight =
110
+ (up_x ? rx : 1 - rx) * (up_y ? ry : 1 - ry) * (up_z ? rz : 1 - rz);
111
+ increment_location(x + up_x, y + up_y, z + up_z, weight);
112
+ };
113
+ EightDirections(handle_point);
114
+ }
115
+ }
116
+ }
117
+
118
+ void PointsToVolumesForwardCuda(
119
+ const at::Tensor& points_3d,
120
+ const at::Tensor& points_features,
121
+ const at::Tensor& volume_densities,
122
+ const at::Tensor& volume_features,
123
+ const at::Tensor& grid_sizes,
124
+ const at::Tensor& mask,
125
+ const float point_weight,
126
+ const bool align_corners,
127
+ const bool splat) {
128
+ // Check inputs are on the same device
129
+ at::TensorArg points_3d_t{points_3d, "points_3d", 1},
130
+ points_features_t{points_features, "points_features", 2},
131
+ volume_densities_t{volume_densities, "volume_densities", 3},
132
+ volume_features_t{volume_features, "volume_features", 4},
133
+ grid_sizes_t{grid_sizes, "grid_sizes", 5}, mask_t{mask, "mask", 6};
134
+ at::CheckedFrom c = "PointsToVolumesForwardCuda";
135
+ at::checkAllSameGPU(
136
+ c,
137
+ {points_3d_t,
138
+ points_features_t,
139
+ volume_densities_t,
140
+ volume_features_t,
141
+ grid_sizes_t,
142
+ mask_t});
143
+
144
+ // This is nondeterministic because atomicAdd
145
+ at::globalContext().alertNotDeterministic("PointsToVolumesForwardCuda");
146
+
147
+ // Set the device for the kernel launch based on the device of the input
148
+ at::cuda::CUDAGuard device_guard(points_3d.device());
149
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
150
+
151
+ const int blocks = 1024;
152
+ const int threads = 32;
153
+
154
+ const int64_t batch_size = points_3d.size(0);
155
+ const int64_t P = points_3d.size(1);
156
+ const int64_t n_features = points_features.size(2);
157
+
158
+ PointsToVolumesForwardKernel<<<blocks, threads, 0, stream>>>(
159
+ points_3d.packed_accessor64<float, 3, RestrictPtrTraits>(),
160
+ points_features.packed_accessor64<float, 3, RestrictPtrTraits>(),
161
+ volume_densities.packed_accessor64<float, 5, RestrictPtrTraits>(),
162
+ volume_features.packed_accessor64<float, 5, RestrictPtrTraits>(),
163
+ grid_sizes.packed_accessor64<int64_t, 2, RestrictPtrTraits>(),
164
+ mask.packed_accessor64<float, 2, RestrictPtrTraits>(),
165
+ point_weight,
166
+ align_corners,
167
+ splat,
168
+ batch_size,
169
+ P,
170
+ n_features);
171
+ }
172
+
173
+ __global__ void PointsToVolumesBackwardKernel(
174
+ const PackedTensorAccessor64<float, 3, RestrictPtrTraits> points_3d,
175
+ const PackedTensorAccessor64<float, 3, RestrictPtrTraits> points_features,
176
+ const PackedTensorAccessor64<int64_t, 2, RestrictPtrTraits> grid_sizes,
177
+ const PackedTensorAccessor64<float, 2, RestrictPtrTraits> mask,
178
+ PackedTensorAccessor64<float, 5, RestrictPtrTraits> grad_volume_densities,
179
+ PackedTensorAccessor64<float, 5, RestrictPtrTraits> grad_volume_features,
180
+ PackedTensorAccessor64<float, 3, RestrictPtrTraits> grad_points_3d,
181
+ PackedTensorAccessor64<float, 3, RestrictPtrTraits> grad_points_features,
182
+ const float point_weight,
183
+ const bool align_corners,
184
+ const bool splat,
185
+ const int64_t batch_size,
186
+ const int64_t P,
187
+ const int64_t n_features) {
188
+ const int64_t chunks_per_cloud = (1 + (P - 1) / blockDim.x);
189
+ const int64_t chunks_to_do = batch_size * chunks_per_cloud;
190
+ const int scale_offset = align_corners ? 1 : 0;
191
+ const float offset = align_corners ? 0 : 0.5;
192
+ // Note that the gradients belonging to each point are only touched by
193
+ // a single thread in one of our "chunks", which is in a single block.
194
+ // So unlike in the forward pass, there's no need for atomics here.
195
+ for (int64_t chunk = blockIdx.x; chunk < chunks_to_do; chunk += gridDim.x) {
196
+ const int64_t batch_index = chunk / chunks_per_cloud;
197
+ const int64_t start_point = blockDim.x * (chunk % chunks_per_cloud);
198
+ int64_t point_idx = start_point + threadIdx.x;
199
+ if (point_idx >= P) {
200
+ continue;
201
+ }
202
+ if (mask[batch_index][point_idx] == 0) {
203
+ continue;
204
+ }
205
+ auto point = points_3d[batch_index][point_idx];
206
+ auto point_features = points_features[batch_index][point_idx];
207
+ auto grad_point = grad_points_3d[batch_index][point_idx];
208
+ auto grad_point_features = grad_points_features[batch_index][point_idx];
209
+ auto grad_volume_densities_a = grad_volume_densities[batch_index][0];
210
+ auto grad_volume_features_a = grad_volume_features[batch_index];
211
+ const int64_t grid_size_x = grid_sizes[batch_index][2];
212
+ const int64_t grid_size_y = grid_sizes[batch_index][1];
213
+ const int64_t grid_size_z = grid_sizes[batch_index][0];
214
+
215
+ auto increment_location =
216
+ [&](int64_t x, int64_t y, int64_t z, float weight) {
217
+ if (x >= grid_size_x || y >= grid_size_y || z >= grid_size_z) {
218
+ return false;
219
+ }
220
+ if (x < 0 || y < 0 || z < 0) {
221
+ return false;
222
+ }
223
+
224
+ // This is a forward line, for comparison
225
+ // volume_densities_aa[z][y][x] += weight * point_weight;
226
+
227
+ for (int64_t feature_idx = 0; feature_idx < n_features;
228
+ ++feature_idx) {
229
+ // This is a forward line, for comparison
230
+ // volume_features_aa[feature_idx][z][y][x] +=
231
+ // point_features[feature_idx] * weight * point_weight;
232
+ grad_point_features[feature_idx] +=
233
+ grad_volume_features_a[feature_idx][z][y][x] * weight *
234
+ point_weight;
235
+ }
236
+ return true;
237
+ };
238
+
239
+ if (!splat) {
240
+ long x = std::lround(
241
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset);
242
+ long y = std::lround(
243
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset);
244
+ long z = std::lround(
245
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset);
246
+ increment_location(x, y, z, 1);
247
+ } else {
248
+ float x = 0, y = 0, z = 0;
249
+ float rx = std::modf(
250
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset, &x);
251
+ float ry = std::modf(
252
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset, &y);
253
+ float rz = std::modf(
254
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset, &z);
255
+ auto handle_point = [&](bool up_x, bool up_y, bool up_z) {
256
+ float weight_x = (up_x ? rx : 1 - rx);
257
+ float weight_y = (up_y ? ry : 1 - ry);
258
+ float weight_z = (up_z ? rz : 1 - rz);
259
+ float weight = weight_x * weight_y * weight_z;
260
+ if (increment_location(x + up_x, y + up_y, z + up_z, weight)) {
261
+ // weight * point_weight has been added to
262
+ // volume_densities_aa[z+up_z][y+up_y][x+up_x]
263
+ // Also for each feature_idx,
264
+ // point_features[feature_idx] * weight * point_weight
265
+ // has been added to
266
+ // volume_features_aa[feature_idx][z+up_z][y+up_y][x+up_x]
267
+
268
+ double source_gradient =
269
+ grad_volume_densities_a[z + up_z][y + up_y][x + up_x];
270
+ for (int64_t feature_idx = 0; feature_idx < n_features;
271
+ ++feature_idx) {
272
+ source_gradient += point_features[feature_idx] *
273
+ grad_volume_features_a[feature_idx][z + up_z][y + up_y]
274
+ [x + up_x];
275
+ }
276
+ grad_point[0] += source_gradient * (up_x ? 1 : -1) * weight_y *
277
+ weight_z * 0.5 * (grid_size_x - scale_offset) * point_weight;
278
+ grad_point[1] += source_gradient * (up_y ? 1 : -1) * weight_x *
279
+ weight_z * 0.5 * (grid_size_y - scale_offset) * point_weight;
280
+ grad_point[2] += source_gradient * (up_z ? 1 : -1) * weight_x *
281
+ weight_y * 0.5 * (grid_size_z - scale_offset) * point_weight;
282
+ }
283
+ };
284
+ EightDirections(handle_point);
285
+ }
286
+ }
287
+ }
288
+
289
+ void PointsToVolumesBackwardCuda(
290
+ const at::Tensor& points_3d,
291
+ const at::Tensor& points_features,
292
+ const at::Tensor& grid_sizes,
293
+ const at::Tensor& mask,
294
+ const float point_weight,
295
+ const bool align_corners,
296
+ const bool splat,
297
+ const at::Tensor& grad_volume_densities,
298
+ const at::Tensor& grad_volume_features,
299
+ const at::Tensor& grad_points_3d,
300
+ const at::Tensor& grad_points_features) {
301
+ // Check inputs are on the same device
302
+ at::TensorArg points_3d_t{points_3d, "points_3d", 1},
303
+ points_features_t{points_features, "points_features", 2},
304
+ grid_sizes_t{grid_sizes, "grid_sizes", 3}, mask_t{mask, "mask", 4},
305
+ grad_volume_densities_t{
306
+ grad_volume_densities, "grad_volume_densities", 8},
307
+ grad_volume_features_t{grad_volume_features, "grad_volume_features", 9},
308
+ grad_points_3d_t{grad_points_3d, "grad_points_3d", 10},
309
+ grad_points_features_t{grad_points_features, "grad_points_features", 11};
310
+
311
+ at::CheckedFrom c = "PointsToVolumesBackwardCuda";
312
+ at::checkAllSameGPU(
313
+ c,
314
+ {points_3d_t,
315
+ points_features_t,
316
+ grid_sizes_t,
317
+ mask_t,
318
+ grad_volume_densities_t,
319
+ grad_volume_features_t,
320
+ grad_points_3d_t,
321
+ grad_points_features_t});
322
+
323
+ // Set the device for the kernel launch based on the device of the input
324
+ at::cuda::CUDAGuard device_guard(points_3d.device());
325
+ cudaStream_t stream = at::cuda::getCurrentCUDAStream();
326
+
327
+ const int blocks = 1024;
328
+ const int threads = 32;
329
+
330
+ const int64_t batch_size = points_3d.size(0);
331
+ const int64_t P = points_3d.size(1);
332
+ const int64_t n_features = points_features.size(2);
333
+
334
+ PointsToVolumesBackwardKernel<<<blocks, threads, 0, stream>>>(
335
+ points_3d.packed_accessor64<float, 3, RestrictPtrTraits>(),
336
+ points_features.packed_accessor64<float, 3, RestrictPtrTraits>(),
337
+ grid_sizes.packed_accessor64<int64_t, 2, RestrictPtrTraits>(),
338
+ mask.packed_accessor64<float, 2, RestrictPtrTraits>(),
339
+ grad_volume_densities.packed_accessor64<float, 5, RestrictPtrTraits>(),
340
+ grad_volume_features.packed_accessor64<float, 5, RestrictPtrTraits>(),
341
+ grad_points_3d.packed_accessor64<float, 3, RestrictPtrTraits>(),
342
+ grad_points_features.packed_accessor64<float, 3, RestrictPtrTraits>(),
343
+ point_weight,
344
+ align_corners,
345
+ splat,
346
+ batch_size,
347
+ P,
348
+ n_features);
349
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/points_to_volumes/points_to_volumes.h ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #pragma once
10
+ #include <torch/csrc/autograd/VariableTypeUtils.h>
11
+ #include <torch/extension.h>
12
+ #include <cstdio>
13
+ #include <tuple>
14
+ #include "utils/pytorch3d_cutils.h"
15
+
16
+ /*
17
+ volume_features and volume_densities are modified in place.
18
+
19
+ Args:
20
+ points_3d: Batch of 3D point cloud coordinates of shape
21
+ `(minibatch, N, 3)` where N is the number of points
22
+ in each point cloud. Coordinates have to be specified in the
23
+ local volume coordinates (ranging in [-1, 1]).
24
+ points_features: Features of shape `(minibatch, N, feature_dim)`
25
+ corresponding to the points of the input point cloud `points_3d`.
26
+ volume_features: Batch of input feature volumes
27
+ of shape `(minibatch, feature_dim, D, H, W)`
28
+ volume_densities: Batch of input feature volume densities
29
+ of shape `(minibatch, 1, D, H, W)`. Each voxel should
30
+ contain a non-negative number corresponding to its
31
+ opaqueness (the higher, the less transparent).
32
+
33
+ grid_sizes: `LongTensor` of shape (minibatch, 3) representing the
34
+ spatial resolutions of each of the the non-flattened `volumes`
35
+ tensors. Note that the following has to hold:
36
+ `torch.prod(grid_sizes, dim=1)==N_voxels`.
37
+
38
+ point_weight: A scalar controlling how much weight a single point has.
39
+
40
+ mask: A binary mask of shape `(minibatch, N)` determining
41
+ which 3D points are going to be converted to the resulting
42
+ volume. Set to `None` if all points are valid.
43
+
44
+ align_corners: as for grid_sample.
45
+
46
+ splat: if true, trilinear interpolation. If false all the weight goes in
47
+ the nearest voxel.
48
+ */
49
+
50
+ void PointsToVolumesForwardCpu(
51
+ const torch::Tensor& points_3d,
52
+ const torch::Tensor& points_features,
53
+ const torch::Tensor& volume_densities,
54
+ const torch::Tensor& volume_features,
55
+ const torch::Tensor& grid_sizes,
56
+ const torch::Tensor& mask,
57
+ float point_weight,
58
+ bool align_corners,
59
+ bool splat);
60
+
61
+ void PointsToVolumesForwardCuda(
62
+ const torch::Tensor& points_3d,
63
+ const torch::Tensor& points_features,
64
+ const torch::Tensor& volume_densities,
65
+ const torch::Tensor& volume_features,
66
+ const torch::Tensor& grid_sizes,
67
+ const torch::Tensor& mask,
68
+ float point_weight,
69
+ bool align_corners,
70
+ bool splat);
71
+
72
+ inline void PointsToVolumesForward(
73
+ const torch::Tensor& points_3d,
74
+ const torch::Tensor& points_features,
75
+ const torch::Tensor& volume_densities,
76
+ const torch::Tensor& volume_features,
77
+ const torch::Tensor& grid_sizes,
78
+ const torch::Tensor& mask,
79
+ float point_weight,
80
+ bool align_corners,
81
+ bool splat) {
82
+ if (points_3d.is_cuda()) {
83
+ #ifdef WITH_CUDA
84
+ CHECK_CUDA(points_3d);
85
+ CHECK_CUDA(points_features);
86
+ CHECK_CUDA(volume_densities);
87
+ CHECK_CUDA(volume_features);
88
+ CHECK_CUDA(grid_sizes);
89
+ CHECK_CUDA(mask);
90
+ PointsToVolumesForwardCuda(
91
+ points_3d,
92
+ points_features,
93
+ volume_densities,
94
+ volume_features,
95
+ grid_sizes,
96
+ mask,
97
+ point_weight,
98
+ align_corners,
99
+ splat);
100
+ torch::autograd::increment_version(volume_features);
101
+ torch::autograd::increment_version(volume_densities);
102
+ return;
103
+ #else
104
+ AT_ERROR("Not compiled with GPU support.");
105
+ #endif
106
+ }
107
+ PointsToVolumesForwardCpu(
108
+ points_3d,
109
+ points_features,
110
+ volume_densities,
111
+ volume_features,
112
+ grid_sizes,
113
+ mask,
114
+ point_weight,
115
+ align_corners,
116
+ splat);
117
+ }
118
+
119
+ // grad_points_3d and grad_points_features are modified in place.
120
+
121
+ void PointsToVolumesBackwardCpu(
122
+ const torch::Tensor& points_3d,
123
+ const torch::Tensor& points_features,
124
+ const torch::Tensor& grid_sizes,
125
+ const torch::Tensor& mask,
126
+ float point_weight,
127
+ bool align_corners,
128
+ bool splat,
129
+ const torch::Tensor& grad_volume_densities,
130
+ const torch::Tensor& grad_volume_features,
131
+ const torch::Tensor& grad_points_3d,
132
+ const torch::Tensor& grad_points_features);
133
+
134
+ void PointsToVolumesBackwardCuda(
135
+ const torch::Tensor& points_3d,
136
+ const torch::Tensor& points_features,
137
+ const torch::Tensor& grid_sizes,
138
+ const torch::Tensor& mask,
139
+ float point_weight,
140
+ bool align_corners,
141
+ bool splat,
142
+ const torch::Tensor& grad_volume_densities,
143
+ const torch::Tensor& grad_volume_features,
144
+ const torch::Tensor& grad_points_3d,
145
+ const torch::Tensor& grad_points_features);
146
+
147
+ inline void PointsToVolumesBackward(
148
+ const torch::Tensor& points_3d,
149
+ const torch::Tensor& points_features,
150
+ const torch::Tensor& grid_sizes,
151
+ const torch::Tensor& mask,
152
+ float point_weight,
153
+ bool align_corners,
154
+ bool splat,
155
+ const torch::Tensor& grad_volume_densities,
156
+ const torch::Tensor& grad_volume_features,
157
+ const torch::Tensor& grad_points_3d,
158
+ const torch::Tensor& grad_points_features) {
159
+ if (points_3d.is_cuda()) {
160
+ #ifdef WITH_CUDA
161
+ CHECK_CUDA(points_3d);
162
+ CHECK_CUDA(points_features);
163
+ CHECK_CUDA(grid_sizes);
164
+ CHECK_CUDA(mask);
165
+ CHECK_CUDA(grad_volume_densities);
166
+ CHECK_CUDA(grad_volume_features);
167
+ CHECK_CUDA(grad_points_3d);
168
+ CHECK_CUDA(grad_points_features);
169
+ PointsToVolumesBackwardCuda(
170
+ points_3d,
171
+ points_features,
172
+ grid_sizes,
173
+ mask,
174
+ point_weight,
175
+ align_corners,
176
+ splat,
177
+ grad_volume_densities,
178
+ grad_volume_features,
179
+ grad_points_3d,
180
+ grad_points_features);
181
+ return;
182
+ #else
183
+ AT_ERROR("Not compiled with GPU support.");
184
+ #endif
185
+ }
186
+ PointsToVolumesBackwardCpu(
187
+ points_3d,
188
+ points_features,
189
+ grid_sizes,
190
+ mask,
191
+ point_weight,
192
+ align_corners,
193
+ splat,
194
+ grad_volume_densities,
195
+ grad_volume_features,
196
+ grad_points_3d,
197
+ grad_points_features);
198
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/points_to_volumes/points_to_volumes_cpu.cpp ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include <torch/csrc/autograd/VariableTypeUtils.h>
10
+ #include <torch/extension.h>
11
+ #include <algorithm>
12
+ #include <cmath>
13
+ #include <thread>
14
+ #include <vector>
15
+
16
+ // In the x direction, the location {0, ..., grid_size_x - 1} correspond to
17
+ // points px in [-1, 1]. There are two ways to do this.
18
+
19
+ // If align_corners=True, px=-1 is the exact location 0 and px=1 is the exact
20
+ // location grid_size_x - 1.
21
+ // So the location of px is {(px + 1) * 0.5} * (grid_size_x - 1).
22
+ // Note that if you generate random points within the bounds you are less likely
23
+ // to hit the edge locations than other locations.
24
+ // This can be thought of as saying "location i" means a specific point.
25
+
26
+ // If align_corners=False, px=-1 is half way between the exact location 0 and
27
+ // the non-existent location -1, i.e. location -0.5.
28
+ // Similarly px=1 is is half way between the exact location grid_size_x-1 and
29
+ // the non-existent location grid_size, i.e. the location grid_size_x - 0.5.
30
+ // So the location of px is ({(px + 1) * 0.5} * grid_size_x) - 0.5.
31
+ // Note that if you generate random points within the bounds you are equally
32
+ // likely to hit any location.
33
+ // This can be thought of as saying "location i" means the whole box from
34
+ // (i-0.5) to (i+0.5)
35
+
36
+ // EightDirections(t) runs t(a,b,c) for every combination of boolean a, b, c.
37
+ template <class T>
38
+ static void EightDirections(T&& t) {
39
+ t(false, false, false);
40
+ t(false, false, true);
41
+ t(false, true, false);
42
+ t(false, true, true);
43
+ t(true, false, false);
44
+ t(true, false, true);
45
+ t(true, true, false);
46
+ t(true, true, true);
47
+ }
48
+
49
+ void PointsToVolumesForwardCpu(
50
+ const torch::Tensor& points_3d,
51
+ const torch::Tensor& points_features,
52
+ const torch::Tensor& volume_densities,
53
+ const torch::Tensor& volume_features,
54
+ const torch::Tensor& grid_sizes,
55
+ const torch::Tensor& mask,
56
+ const float point_weight,
57
+ const bool align_corners,
58
+ const bool splat) {
59
+ const int64_t batch_size = points_3d.size(0);
60
+ const int64_t P = points_3d.size(1);
61
+ const int64_t n_features = points_features.size(2);
62
+
63
+ // We unify the formula for the location of px in the comment above as
64
+ // ({(px + 1) * 0.5} * (grid_size_x-scale_offset)) - offset.
65
+ const int scale_offset = align_corners ? 1 : 0;
66
+ const float offset = align_corners ? 0 : 0.5;
67
+
68
+ auto points_3d_a = points_3d.accessor<float, 3>();
69
+ auto points_features_a = points_features.accessor<float, 3>();
70
+ auto volume_densities_a = volume_densities.accessor<float, 5>();
71
+ auto volume_features_a = volume_features.accessor<float, 5>();
72
+ auto grid_sizes_a = grid_sizes.accessor<int64_t, 2>();
73
+ auto mask_a = mask.accessor<float, 2>();
74
+
75
+ // For each batch element
76
+ for (int64_t batch_idx = 0; batch_idx < batch_size; ++batch_idx) {
77
+ auto points_3d_aa = points_3d_a[batch_idx];
78
+ auto points_features_aa = points_features_a[batch_idx];
79
+ auto volume_densities_aa = volume_densities_a[batch_idx][0];
80
+ auto volume_features_aa = volume_features_a[batch_idx];
81
+ auto grid_sizes_aa = grid_sizes_a[batch_idx];
82
+ auto mask_aa = mask_a[batch_idx];
83
+
84
+ const int64_t grid_size_x = grid_sizes_aa[2];
85
+ const int64_t grid_size_y = grid_sizes_aa[1];
86
+ const int64_t grid_size_z = grid_sizes_aa[0];
87
+
88
+ // For each point
89
+ for (int64_t point_idx = 0; point_idx < P; ++point_idx) {
90
+ // Ignore point if mask is 0
91
+ if (mask_aa[point_idx] == 0) {
92
+ continue;
93
+ }
94
+ auto point = points_3d_aa[point_idx];
95
+ auto point_features = points_features_aa[point_idx];
96
+
97
+ // Define how to increment a location in the volume by an amount. The need
98
+ // for this depends on the interpolation method:
99
+ // once per point for nearest, eight times for splat.
100
+ auto increment_location =
101
+ [&](int64_t x, int64_t y, int64_t z, float weight) {
102
+ if (x >= grid_size_x || y >= grid_size_y || z >= grid_size_z) {
103
+ return;
104
+ }
105
+ if (x < 0 || y < 0 || z < 0) {
106
+ return;
107
+ }
108
+
109
+ volume_densities_aa[z][y][x] += weight * point_weight;
110
+
111
+ for (int64_t feature_idx = 0; feature_idx < n_features;
112
+ ++feature_idx) {
113
+ volume_features_aa[feature_idx][z][y][x] +=
114
+ point_features[feature_idx] * weight * point_weight;
115
+ }
116
+ };
117
+
118
+ if (!splat) {
119
+ // Increment the location nearest the point.
120
+ long x = std::lround(
121
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset);
122
+ long y = std::lround(
123
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset);
124
+ long z = std::lround(
125
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset);
126
+ increment_location(x, y, z, 1);
127
+ } else {
128
+ // There are 8 locations around the point which we need to worry about.
129
+ // Their coordinates are (x or x+1, y or y+1, z or z+1).
130
+ // rx is a number between 0 and 1 for the proportion in the x direction:
131
+ // rx==0 means weight all on the lower bound, x, rx=1-eps means most
132
+ // weight on x+1. Ditto for ry and yz.
133
+ float x = 0, y = 0, z = 0;
134
+ float rx = std::modf(
135
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset, &x);
136
+ float ry = std::modf(
137
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset, &y);
138
+ float rz = std::modf(
139
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset, &z);
140
+ // Define how to fractionally increment one of the 8 locations around
141
+ // the point.
142
+ auto handle_point = [&](bool up_x, bool up_y, bool up_z) {
143
+ float weight = (up_x ? rx : 1 - rx) * (up_y ? ry : 1 - ry) *
144
+ (up_z ? rz : 1 - rz);
145
+ increment_location(x + up_x, y + up_y, z + up_z, weight);
146
+ };
147
+ // and do so.
148
+ EightDirections(handle_point);
149
+ }
150
+ }
151
+ }
152
+ torch::autograd::increment_version(volume_features);
153
+ torch::autograd::increment_version(volume_densities);
154
+ }
155
+
156
+ // With nearest, the only smooth dependence is that volume features
157
+ // depend on points features.
158
+ //
159
+ // With splat, the dependencies are as follows, with gradients passing
160
+ // in the opposite direction.
161
+ //
162
+ // points_3d points_features
163
+ // │ │ │
164
+ // │ │ │
165
+ // │ └───────────┐ │
166
+ // │ │ │
167
+ // │ │ │
168
+ // ▼ ▼ ▼
169
+ // volume_densities volume_features
170
+
171
+ // It is also the case that the input volume_densities and
172
+ // volume_features affect the corresponding outputs (they are
173
+ // modified in place).
174
+ // But the forward pass just increments these by a value which
175
+ // does not depend on them. So our autograd backwards pass needs
176
+ // to copy the gradient for each of those outputs to the
177
+ // corresponding input. We just do that in the Python layer.
178
+
179
+ void PointsToVolumesBackwardCpu(
180
+ const torch::Tensor& points_3d,
181
+ const torch::Tensor& points_features,
182
+ const torch::Tensor& grid_sizes,
183
+ const torch::Tensor& mask,
184
+ const float point_weight,
185
+ const bool align_corners,
186
+ const bool splat,
187
+ const torch::Tensor& grad_volume_densities,
188
+ const torch::Tensor& grad_volume_features,
189
+ const torch::Tensor& grad_points_3d,
190
+ const torch::Tensor& grad_points_features) {
191
+ const int64_t batch_size = points_3d.size(0);
192
+ const int64_t P = points_3d.size(1);
193
+ const int64_t n_features = grad_points_features.size(2);
194
+ const int scale_offset = align_corners ? 1 : 0;
195
+ const float offset = align_corners ? 0 : 0.5;
196
+
197
+ auto points_3d_a = points_3d.accessor<float, 3>();
198
+ auto points_features_a = points_features.accessor<float, 3>();
199
+ auto grid_sizes_a = grid_sizes.accessor<int64_t, 2>();
200
+ auto mask_a = mask.accessor<float, 2>();
201
+ auto grad_volume_densities_a = grad_volume_densities.accessor<float, 5>();
202
+ auto grad_volume_features_a = grad_volume_features.accessor<float, 5>();
203
+ auto grad_points_3d_a = grad_points_3d.accessor<float, 3>();
204
+ auto grad_points_features_a = grad_points_features.accessor<float, 3>();
205
+
206
+ // For each batch element
207
+ for (int64_t batch_idx = 0; batch_idx < batch_size; ++batch_idx) {
208
+ auto points_3d_aa = points_3d_a[batch_idx];
209
+ auto points_features_aa = points_features_a[batch_idx];
210
+ auto grid_sizes_aa = grid_sizes_a[batch_idx];
211
+ auto mask_aa = mask_a[batch_idx];
212
+ auto grad_volume_densities_aa = grad_volume_densities_a[batch_idx][0];
213
+ auto grad_volume_features_aa = grad_volume_features_a[batch_idx];
214
+ auto grad_points_3d_aa = grad_points_3d_a[batch_idx];
215
+ auto grad_points_features_aa = grad_points_features_a[batch_idx];
216
+
217
+ const int64_t grid_size_x = grid_sizes_aa[2];
218
+ const int64_t grid_size_y = grid_sizes_aa[1];
219
+ const int64_t grid_size_z = grid_sizes_aa[0];
220
+
221
+ // For each point
222
+ for (int64_t point_idx = 0; point_idx < P; ++point_idx) {
223
+ if (mask_aa[point_idx] == 0) {
224
+ continue;
225
+ }
226
+ auto point = points_3d_aa[point_idx];
227
+ auto point_features = points_features_aa[point_idx];
228
+ auto grad_point_features = grad_points_features_aa[point_idx];
229
+ auto grad_point = grad_points_3d_aa[point_idx];
230
+
231
+ // Define how to (backwards) increment a location in the point cloud,
232
+ // to take gradients to the features.
233
+ // We return false if the location does not really exist, so there was
234
+ // nothing to do.
235
+ // This happens once per point for nearest, eight times for splat.
236
+ auto increment_location =
237
+ [&](int64_t x, int64_t y, int64_t z, float weight) {
238
+ if (x >= grid_size_x || y >= grid_size_y || z >= grid_size_z) {
239
+ return false;
240
+ }
241
+ if (x < 0 || y < 0 || z < 0) {
242
+ return false;
243
+ }
244
+
245
+ for (int64_t feature_idx = 0; feature_idx < n_features;
246
+ ++feature_idx) {
247
+ // This is a forward line, for comparison
248
+ // volume_features_aa[feature_idx][z][y][x] +=
249
+ // point_features[feature_idx] * weight * point_weight;
250
+ grad_point_features[feature_idx] +=
251
+ grad_volume_features_aa[feature_idx][z][y][x] * weight *
252
+ point_weight;
253
+ }
254
+ return true;
255
+ };
256
+
257
+ if (!splat) {
258
+ long x = std::lround(
259
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset);
260
+ long y = std::lround(
261
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset);
262
+ long z = std::lround(
263
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset);
264
+ increment_location(x, y, z, 1);
265
+ } else {
266
+ float x = 0, y = 0, z = 0;
267
+ float rx = std::modf(
268
+ (point[0] + 1) * 0.5 * (grid_size_x - scale_offset) - offset, &x);
269
+ float ry = std::modf(
270
+ (point[1] + 1) * 0.5 * (grid_size_y - scale_offset) - offset, &y);
271
+ float rz = std::modf(
272
+ (point[2] + 1) * 0.5 * (grid_size_z - scale_offset) - offset, &z);
273
+ auto handle_point = [&](bool up_x, bool up_y, bool up_z) {
274
+ float weight_x = (up_x ? rx : 1 - rx);
275
+ float weight_y = (up_y ? ry : 1 - ry);
276
+ float weight_z = (up_z ? rz : 1 - rz);
277
+ float weight = weight_x * weight_y * weight_z;
278
+ // For each of the eight locations, we first increment the feature
279
+ // gradient.
280
+ if (increment_location(x + up_x, y + up_y, z + up_z, weight)) {
281
+ // If the location is a real location, we also (in this splat
282
+ // case) need to update the gradient w.r.t. the point position.
283
+ // - the amount in this location is controlled by the weight.
284
+ // There are two contributions:
285
+ // (1) The point position affects how much density we added
286
+ // to the location's density, so we have a contribution
287
+ // from grad_volume_density. Specifically,
288
+ // weight * point_weight has been added to
289
+ // volume_densities_aa[z+up_z][y+up_y][x+up_x]
290
+ //
291
+ // (2) The point position affects how much of each of the
292
+ // point's features were added to the corresponding feature
293
+ // of this location, so we have a contribution from
294
+ // grad_volume_features. Specifically, for each feature_idx,
295
+ // point_features[feature_idx] * weight * point_weight
296
+ // has been added to
297
+ // volume_features_aa[feature_idx][z+up_z][y+up_y][x+up_x]
298
+
299
+ float source_gradient =
300
+ grad_volume_densities_aa[z + up_z][y + up_y][x + up_x];
301
+ for (int64_t feature_idx = 0; feature_idx < n_features;
302
+ ++feature_idx) {
303
+ source_gradient += point_features[feature_idx] *
304
+ grad_volume_features_aa[feature_idx][z + up_z][y + up_y]
305
+ [x + up_x];
306
+ }
307
+ grad_point[0] += source_gradient * (up_x ? 1 : -1) * weight_y *
308
+ weight_z * 0.5 * (grid_size_x - scale_offset) * point_weight;
309
+ grad_point[1] += source_gradient * (up_y ? 1 : -1) * weight_x *
310
+ weight_z * 0.5 * (grid_size_y - scale_offset) * point_weight;
311
+ grad_point[2] += source_gradient * (up_z ? 1 : -1) * weight_x *
312
+ weight_y * 0.5 * (grid_size_z - scale_offset) * point_weight;
313
+ }
314
+ };
315
+ EightDirections(handle_point);
316
+ }
317
+ }
318
+ }
319
+ }
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/constants.h ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_CONSTANTS_H_
10
+ #define PULSAR_NATIVE_CONSTANTS_H_
11
+
12
+ #define EPS 1E-6
13
+ #define FEPS 1E-6f
14
+ #define MAX_FLOAT 3.4E38f
15
+ #define MAX_INT 2147483647
16
+ #define MAX_UINT 4294967295u
17
+ #define MAX_USHORT 65535u
18
+
19
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/README.md ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # CUDA device compilation units
2
+
3
+ This folder contains `.cu` files to create compilation units
4
+ for device-specific functions. See `../include/README.md` for
5
+ more information.
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/commands.h ADDED
@@ -0,0 +1,505 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_CUDA_COMMANDS_H_
10
+ #define PULSAR_NATIVE_CUDA_COMMANDS_H_
11
+
12
+ // Definitions for GPU commands.
13
+ #include <cooperative_groups.h>
14
+ #include <cub/cub.cuh>
15
+ namespace cg = cooperative_groups;
16
+
17
+ #ifdef __DRIVER_TYPES_H__
18
+ #ifndef DEVICE_RESET
19
+ #define DEVICE_RESET cudaDeviceReset();
20
+ #endif
21
+ #else
22
+ #ifndef DEVICE_RESET
23
+ #define DEVICE_RESET
24
+ #endif
25
+ #endif
26
+
27
+ #define HANDLECUDA(CMD) CMD
28
+ // handleCudaError((CMD), __FILE__, __LINE__)
29
+ inline void
30
+ handleCudaError(const cudaError_t err, const char* file, const int line) {
31
+ if (err != cudaSuccess) {
32
+ #ifndef __NVCC__
33
+ fprintf(
34
+ stderr,
35
+ "%s(%i) : getLastCudaError() CUDA error :"
36
+ " (%d) %s.\n",
37
+ file,
38
+ line,
39
+ static_cast<int>(err),
40
+ cudaGetErrorString(err));
41
+ DEVICE_RESET
42
+ exit(1);
43
+ #endif
44
+ }
45
+ }
46
+ inline void
47
+ getLastCudaError(const char* errorMessage, const char* file, const int line) {
48
+ cudaError_t err = cudaGetLastError();
49
+ if (cudaSuccess != err) {
50
+ fprintf(stderr, "Error: %s.", errorMessage);
51
+ handleCudaError(err, file, line);
52
+ }
53
+ }
54
+
55
+ #define ALIGN(VAL) __align__(VAL)
56
+ #define SYNC() HANDLECUDE(cudaDeviceSynchronize())
57
+ #define THREADFENCE_B() __threadfence_block()
58
+ #define SHFL_SYNC(a, b, c) __shfl_sync((a), (b), (c))
59
+ #define SHARED __shared__
60
+ #define ACTIVEMASK() __activemask()
61
+ #define BALLOT(mask, val) __ballot_sync((mask), val)
62
+ /**
63
+ * Find the cumulative sum within a warp up to the current
64
+ * thread lane, with each mask thread contributing base.
65
+ */
66
+ template <typename T>
67
+ DEVICE T
68
+ WARP_CUMSUM(const cg::coalesced_group& group, const uint& mask, const T& base) {
69
+ T ret = base;
70
+ T shfl_val;
71
+ shfl_val = __shfl_down_sync(mask, ret, 1u); // Deactivate the rightmost lane.
72
+ ret += (group.thread_rank() < 31) * shfl_val;
73
+ shfl_val = __shfl_down_sync(mask, ret, 2u);
74
+ ret += (group.thread_rank() < 30) * shfl_val;
75
+ shfl_val = __shfl_down_sync(mask, ret, 4u); // ...4
76
+ ret += (group.thread_rank() < 28) * shfl_val;
77
+ shfl_val = __shfl_down_sync(mask, ret, 8u); // ...8
78
+ ret += (group.thread_rank() < 24) * shfl_val;
79
+ shfl_val = __shfl_down_sync(mask, ret, 16u); // ...16
80
+ ret += (group.thread_rank() < 16) * shfl_val;
81
+ return ret;
82
+ }
83
+
84
+ template <typename T>
85
+ DEVICE T
86
+ WARP_MAX(const cg::coalesced_group& group, const uint& mask, const T& base) {
87
+ T ret = base;
88
+ ret = max(ret, __shfl_down_sync(mask, ret, 16u));
89
+ ret = max(ret, __shfl_down_sync(mask, ret, 8u));
90
+ ret = max(ret, __shfl_down_sync(mask, ret, 4u));
91
+ ret = max(ret, __shfl_down_sync(mask, ret, 2u));
92
+ ret = max(ret, __shfl_down_sync(mask, ret, 1u));
93
+ return ret;
94
+ }
95
+
96
+ template <typename T>
97
+ DEVICE T
98
+ WARP_SUM(const cg::coalesced_group& group, const uint& mask, const T& base) {
99
+ T ret = base;
100
+ ret = ret + __shfl_down_sync(mask, ret, 16u);
101
+ ret = ret + __shfl_down_sync(mask, ret, 8u);
102
+ ret = ret + __shfl_down_sync(mask, ret, 4u);
103
+ ret = ret + __shfl_down_sync(mask, ret, 2u);
104
+ ret = ret + __shfl_down_sync(mask, ret, 1u);
105
+ return ret;
106
+ }
107
+
108
+ INLINE DEVICE float3 WARP_SUM_FLOAT3(
109
+ const cg::coalesced_group& group,
110
+ const uint& mask,
111
+ const float3& base) {
112
+ float3 ret = base;
113
+ ret.x = WARP_SUM(group, mask, base.x);
114
+ ret.y = WARP_SUM(group, mask, base.y);
115
+ ret.z = WARP_SUM(group, mask, base.z);
116
+ return ret;
117
+ }
118
+
119
+ // Floating point.
120
+ // #define FMUL(a, b) __fmul_rn((a), (b))
121
+ #define FMUL(a, b) ((a) * (b))
122
+ #define FDIV(a, b) __fdiv_rn((a), (b))
123
+ // #define FSUB(a, b) __fsub_rn((a), (b))
124
+ #define FSUB(a, b) ((a) - (b))
125
+ #define FADD(a, b) __fadd_rn((a), (b))
126
+ #define FSQRT(a) __fsqrt_rn(a)
127
+ #define FEXP(a) fasterexp(a)
128
+ #define FLN(a) fasterlog(a)
129
+ #define FPOW(a, b) __powf((a), (b))
130
+ #define FMAX(a, b) fmax((a), (b))
131
+ #define FMIN(a, b) fmin((a), (b))
132
+ #define FCEIL(a) ceilf(a)
133
+ #define FFLOOR(a) floorf(a)
134
+ #define FROUND(x) nearbyintf(x)
135
+ #define FSATURATE(x) __saturatef(x)
136
+ #define FABS(a) abs(a)
137
+ #define IASF(a, loc) (loc) = __int_as_float(a)
138
+ #define FASI(a, loc) (loc) = __float_as_int(a)
139
+ #define FABSLEQAS(a, b, c) \
140
+ ((a) <= (b) ? FSUB((b), (a)) <= (c) : FSUB((a), (b)) < (c))
141
+ /** Calculates x*y+z. */
142
+ #define FMA(x, y, z) __fmaf_rn((x), (y), (z))
143
+ #define I2F(a) __int2float_rn(a)
144
+ #define FRCP(x) __frcp_rn(x)
145
+ __device__ static float atomicMax(float* address, float val) {
146
+ int* address_as_i = (int*)address;
147
+ int old = *address_as_i, assumed;
148
+ do {
149
+ assumed = old;
150
+ old = ::atomicCAS(
151
+ address_as_i,
152
+ assumed,
153
+ __float_as_int(::fmaxf(val, __int_as_float(assumed))));
154
+ } while (assumed != old);
155
+ return __int_as_float(old);
156
+ }
157
+ __device__ static float atomicMin(float* address, float val) {
158
+ int* address_as_i = (int*)address;
159
+ int old = *address_as_i, assumed;
160
+ do {
161
+ assumed = old;
162
+ old = ::atomicCAS(
163
+ address_as_i,
164
+ assumed,
165
+ __float_as_int(::fminf(val, __int_as_float(assumed))));
166
+ } while (assumed != old);
167
+ return __int_as_float(old);
168
+ }
169
+ #define DMAX(a, b) FMAX(a, b)
170
+ #define DMIN(a, b) FMIN(a, b)
171
+ #define DSQRT(a) sqrt(a)
172
+ #define DSATURATE(a) DMIN(1., DMAX(0., (a)))
173
+ // half
174
+ #define HADD(a, b) __hadd((a), (b))
175
+ #define HSUB2(a, b) __hsub2((a), (b))
176
+ #define HMUL2(a, b) __hmul2((a), (b))
177
+ #define HSQRT(a) hsqrt(a)
178
+
179
+ // uint.
180
+ #define CLZ(VAL) __clz(VAL)
181
+ #define POPC(a) __popc(a)
182
+ //
183
+ //
184
+ //
185
+ //
186
+ //
187
+ //
188
+ //
189
+ //
190
+ //
191
+ #define ATOMICADD(PTR, VAL) atomicAdd((PTR), (VAL))
192
+ #define ATOMICADD_F3(PTR, VAL) \
193
+ ATOMICADD(&((PTR)->x), VAL.x); \
194
+ ATOMICADD(&((PTR)->y), VAL.y); \
195
+ ATOMICADD(&((PTR)->z), VAL.z);
196
+ #if (CUDART_VERSION >= 10000) && (__CUDA_ARCH__ >= 600)
197
+ #define ATOMICADD_B(PTR, VAL) atomicAdd_block((PTR), (VAL))
198
+ #else
199
+ #define ATOMICADD_B(PTR, VAL) ATOMICADD(PTR, VAL)
200
+ #endif
201
+ //
202
+ //
203
+ //
204
+ //
205
+ // int.
206
+ #define IMIN(a, b) min((a), (b))
207
+ #define IMAX(a, b) max((a), (b))
208
+ #define IABS(a) abs(a)
209
+
210
+ // Checks.
211
+ // like TORCH_CHECK_ARG in PyTorch > 1.10
212
+ #define ARGCHECK(cond, argN, ...) \
213
+ TORCH_CHECK(cond, "invalid argument ", argN, ": ", __VA_ARGS__)
214
+
215
+ // Math.
216
+ #define NORM3DF(x, y, z) norm3df(x, y, z)
217
+ #define RNORM3DF(x, y, z) rnorm3df(x, y, z)
218
+
219
+ // High level.
220
+ #define GET_SORT_WS_SIZE(RES_PTR, KEY_TYPE, VAL_TYPE, NUM_OBJECTS) \
221
+ cub::DeviceRadixSort::SortPairsDescending( \
222
+ (void*)NULL, \
223
+ *(RES_PTR), \
224
+ reinterpret_cast<KEY_TYPE*>(NULL), \
225
+ reinterpret_cast<KEY_TYPE*>(NULL), \
226
+ reinterpret_cast<VAL_TYPE*>(NULL), \
227
+ reinterpret_cast<VAL_TYPE*>(NULL), \
228
+ (NUM_OBJECTS));
229
+ #define GET_REDUCE_WS_SIZE(RES_PTR, TYPE, REDUCE_OP, NUM_OBJECTS) \
230
+ { \
231
+ TYPE init = TYPE(); \
232
+ cub::DeviceReduce::Reduce( \
233
+ (void*)NULL, \
234
+ *(RES_PTR), \
235
+ (TYPE*)NULL, \
236
+ (TYPE*)NULL, \
237
+ (NUM_OBJECTS), \
238
+ (REDUCE_OP), \
239
+ init); \
240
+ }
241
+ #define GET_SELECT_WS_SIZE( \
242
+ RES_PTR, TYPE_SELECTOR, TYPE_SELECTION, NUM_OBJECTS) \
243
+ { \
244
+ cub::DeviceSelect::Flagged( \
245
+ (void*)NULL, \
246
+ *(RES_PTR), \
247
+ (TYPE_SELECTION*)NULL, \
248
+ (TYPE_SELECTOR*)NULL, \
249
+ (TYPE_SELECTION*)NULL, \
250
+ (int*)NULL, \
251
+ (NUM_OBJECTS)); \
252
+ }
253
+ #define GET_SUM_WS_SIZE(RES_PTR, TYPE_SUM, NUM_OBJECTS) \
254
+ { \
255
+ cub::DeviceReduce::Sum( \
256
+ (void*)NULL, \
257
+ *(RES_PTR), \
258
+ (TYPE_SUM*)NULL, \
259
+ (TYPE_SUM*)NULL, \
260
+ NUM_OBJECTS); \
261
+ }
262
+ #define GET_MM_WS_SIZE(RES_PTR, TYPE, NUM_OBJECTS) \
263
+ { \
264
+ TYPE init = TYPE(); \
265
+ cub::DeviceReduce::Max( \
266
+ (void*)NULL, *(RES_PTR), (TYPE*)NULL, (TYPE*)NULL, (NUM_OBJECTS)); \
267
+ }
268
+ #define SORT_DESCENDING( \
269
+ TMPN1, SORT_PTR, SORTED_PTR, VAL_PTR, VAL_SORTED_PTR, NUM_OBJECTS) \
270
+ void* TMPN1 = NULL; \
271
+ size_t TMPN1##_bytes = 0; \
272
+ cub::DeviceRadixSort::SortPairsDescending( \
273
+ TMPN1, \
274
+ TMPN1##_bytes, \
275
+ (SORT_PTR), \
276
+ (SORTED_PTR), \
277
+ (VAL_PTR), \
278
+ (VAL_SORTED_PTR), \
279
+ (NUM_OBJECTS)); \
280
+ HANDLECUDA(cudaMalloc(&TMPN1, TMPN1##_bytes)); \
281
+ cub::DeviceRadixSort::SortPairsDescending( \
282
+ TMPN1, \
283
+ TMPN1##_bytes, \
284
+ (SORT_PTR), \
285
+ (SORTED_PTR), \
286
+ (VAL_PTR), \
287
+ (VAL_SORTED_PTR), \
288
+ (NUM_OBJECTS)); \
289
+ HANDLECUDA(cudaFree(TMPN1));
290
+ #define SORT_DESCENDING_WS( \
291
+ TMPN1, \
292
+ SORT_PTR, \
293
+ SORTED_PTR, \
294
+ VAL_PTR, \
295
+ VAL_SORTED_PTR, \
296
+ NUM_OBJECTS, \
297
+ WORKSPACE_PTR, \
298
+ WORKSPACE_BYTES) \
299
+ cub::DeviceRadixSort::SortPairsDescending( \
300
+ (WORKSPACE_PTR), \
301
+ (WORKSPACE_BYTES), \
302
+ (SORT_PTR), \
303
+ (SORTED_PTR), \
304
+ (VAL_PTR), \
305
+ (VAL_SORTED_PTR), \
306
+ (NUM_OBJECTS));
307
+ #define SORT_ASCENDING_WS( \
308
+ SORT_PTR, \
309
+ SORTED_PTR, \
310
+ VAL_PTR, \
311
+ VAL_SORTED_PTR, \
312
+ NUM_OBJECTS, \
313
+ WORKSPACE_PTR, \
314
+ WORKSPACE_BYTES, \
315
+ STREAM) \
316
+ cub::DeviceRadixSort::SortPairs( \
317
+ (WORKSPACE_PTR), \
318
+ (WORKSPACE_BYTES), \
319
+ (SORT_PTR), \
320
+ (SORTED_PTR), \
321
+ (VAL_PTR), \
322
+ (VAL_SORTED_PTR), \
323
+ (NUM_OBJECTS), \
324
+ 0, \
325
+ sizeof(*(SORT_PTR)) * 8, \
326
+ (STREAM));
327
+ #define SUM_WS( \
328
+ SUM_PTR, OUT_PTR, NUM_OBJECTS, WORKSPACE_PTR, WORKSPACE_BYTES, STREAM) \
329
+ cub::DeviceReduce::Sum( \
330
+ (WORKSPACE_PTR), \
331
+ (WORKSPACE_BYTES), \
332
+ (SUM_PTR), \
333
+ (OUT_PTR), \
334
+ (NUM_OBJECTS), \
335
+ (STREAM));
336
+ #define MIN_WS( \
337
+ MIN_PTR, OUT_PTR, NUM_OBJECTS, WORKSPACE_PTR, WORKSPACE_BYTES, STREAM) \
338
+ cub::DeviceReduce::Min( \
339
+ (WORKSPACE_PTR), \
340
+ (WORKSPACE_BYTES), \
341
+ (MIN_PTR), \
342
+ (OUT_PTR), \
343
+ (NUM_OBJECTS), \
344
+ (STREAM));
345
+ #define MAX_WS( \
346
+ MAX_PTR, OUT_PTR, NUM_OBJECTS, WORKSPACE_PTR, WORKSPACE_BYTES, STREAM) \
347
+ cub::DeviceReduce::Min( \
348
+ (WORKSPACE_PTR), \
349
+ (WORKSPACE_BYTES), \
350
+ (MAX_PTR), \
351
+ (OUT_PTR), \
352
+ (NUM_OBJECTS), \
353
+ (STREAM));
354
+ //
355
+ //
356
+ //
357
+ // TODO: rewrite using nested contexts instead of temporary names.
358
+ #define REDUCE(REDUCE_PTR, RESULT_PTR, NUM_ITEMS, REDUCE_OP, REDUCE_INIT) \
359
+ cub::DeviceReduce::Reduce( \
360
+ TMPN1, \
361
+ TMPN1##_bytes, \
362
+ (REDUCE_PTR), \
363
+ (RESULT_PTR), \
364
+ (NUM_ITEMS), \
365
+ (REDUCE_OP), \
366
+ (REDUCE_INIT)); \
367
+ HANDLECUDA(cudaMalloc(&TMPN1, TMPN1##_bytes)); \
368
+ cub::DeviceReduce::Reduce( \
369
+ TMPN1, \
370
+ TMPN1##_bytes, \
371
+ (REDUCE_PTR), \
372
+ (RESULT_PTR), \
373
+ (NUM_ITEMS), \
374
+ (REDUCE_OP), \
375
+ (REDUCE_INIT)); \
376
+ HANDLECUDA(cudaFree(TMPN1));
377
+ #define REDUCE_WS( \
378
+ REDUCE_PTR, \
379
+ RESULT_PTR, \
380
+ NUM_ITEMS, \
381
+ REDUCE_OP, \
382
+ REDUCE_INIT, \
383
+ WORKSPACE_PTR, \
384
+ WORSPACE_BYTES, \
385
+ STREAM) \
386
+ cub::DeviceReduce::Reduce( \
387
+ (WORKSPACE_PTR), \
388
+ (WORSPACE_BYTES), \
389
+ (REDUCE_PTR), \
390
+ (RESULT_PTR), \
391
+ (NUM_ITEMS), \
392
+ (REDUCE_OP), \
393
+ (REDUCE_INIT), \
394
+ (STREAM));
395
+ #define SELECT_FLAGS_WS( \
396
+ FLAGS_PTR, \
397
+ ITEM_PTR, \
398
+ OUT_PTR, \
399
+ NUM_SELECTED_PTR, \
400
+ NUM_ITEMS, \
401
+ WORKSPACE_PTR, \
402
+ WORSPACE_BYTES, \
403
+ STREAM) \
404
+ cub::DeviceSelect::Flagged( \
405
+ (WORKSPACE_PTR), \
406
+ (WORSPACE_BYTES), \
407
+ (ITEM_PTR), \
408
+ (FLAGS_PTR), \
409
+ (OUT_PTR), \
410
+ (NUM_SELECTED_PTR), \
411
+ (NUM_ITEMS), \
412
+ stream = (STREAM));
413
+
414
+ #define COPY_HOST_DEV(PTR_D, PTR_H, TYPE, SIZE) \
415
+ HANDLECUDA(cudaMemcpy( \
416
+ (PTR_D), (PTR_H), sizeof(TYPE) * (SIZE), cudaMemcpyHostToDevice))
417
+ #define COPY_DEV_HOST(PTR_H, PTR_D, TYPE, SIZE) \
418
+ HANDLECUDA(cudaMemcpy( \
419
+ (PTR_H), (PTR_D), sizeof(TYPE) * (SIZE), cudaMemcpyDeviceToHost))
420
+ #define COPY_DEV_DEV(PTR_T, PTR_S, TYPE, SIZE) \
421
+ HANDLECUDA(cudaMemcpy( \
422
+ (PTR_T), (PTR_S), sizeof(TYPE) * (SIZE), cudaMemcpyDeviceToDevice))
423
+ //
424
+ // We *must* use cudaMallocManaged for pointers on device that should
425
+ // interact with pytorch. However, this comes at a significant speed penalty.
426
+ // We're using plain CUDA pointers for the rendering operations and
427
+ // explicitly copy results to managed pointers wrapped for pytorch (see
428
+ // pytorch/util.h).
429
+ #define MALLOC(VAR, TYPE, SIZE) cudaMalloc(&(VAR), sizeof(TYPE) * (SIZE))
430
+ #define FREE(PTR) HANDLECUDA(cudaFree(PTR))
431
+ #define MEMSET(VAR, VAL, TYPE, SIZE, STREAM) \
432
+ HANDLECUDA(cudaMemsetAsync((VAR), (VAL), sizeof(TYPE) * (SIZE), (STREAM)))
433
+
434
+ #define LAUNCH_MAX_PARALLEL_1D(FUNC, N, STREAM, ...) \
435
+ { \
436
+ int64_t max_threads = \
437
+ at::cuda::getCurrentDeviceProperties()->maxThreadsPerBlock; \
438
+ uint num_threads = min((N), max_threads); \
439
+ uint num_blocks = iDivCeil((N), num_threads); \
440
+ FUNC<<<num_blocks, num_threads, 0, (STREAM)>>>(__VA_ARGS__); \
441
+ }
442
+ #define LAUNCH_PARALLEL_1D(FUNC, N, TN, STREAM, ...) \
443
+ { \
444
+ uint num_threads = min(static_cast<int>(N), static_cast<int>(TN)); \
445
+ uint num_blocks = iDivCeil((N), num_threads); \
446
+ FUNC<<<num_blocks, num_threads, 0, (STREAM)>>>(__VA_ARGS__); \
447
+ }
448
+ #define LAUNCH_MAX_PARALLEL_2D(FUNC, NX, NY, STREAM, ...) \
449
+ { \
450
+ int64_t max_threads = \
451
+ at::cuda::getCurrentDeviceProperties()->maxThreadsPerBlock; \
452
+ int64_t max_threads_sqrt = static_cast<int64_t>(sqrt(max_threads)); \
453
+ dim3 num_threads, num_blocks; \
454
+ num_threads.x = min((NX), max_threads_sqrt); \
455
+ num_blocks.x = iDivCeil((NX), num_threads.x); \
456
+ num_threads.y = min((NY), max_threads_sqrt); \
457
+ num_blocks.y = iDivCeil((NY), num_threads.y); \
458
+ num_threads.z = 1; \
459
+ num_blocks.z = 1; \
460
+ FUNC<<<num_blocks, num_threads, 0, (STREAM)>>>(__VA_ARGS__); \
461
+ }
462
+ #define LAUNCH_PARALLEL_2D(FUNC, NX, NY, TX, TY, STREAM, ...) \
463
+ { \
464
+ dim3 num_threads, num_blocks; \
465
+ num_threads.x = min((NX), (TX)); \
466
+ num_blocks.x = iDivCeil((NX), num_threads.x); \
467
+ num_threads.y = min((NY), (TY)); \
468
+ num_blocks.y = iDivCeil((NY), num_threads.y); \
469
+ num_threads.z = 1; \
470
+ num_blocks.z = 1; \
471
+ FUNC<<<num_blocks, num_threads, 0, (STREAM)>>>(__VA_ARGS__); \
472
+ }
473
+
474
+ #define GET_PARALLEL_IDX_1D(VARNAME, N) \
475
+ const uint VARNAME = __mul24(blockIdx.x, blockDim.x) + threadIdx.x; \
476
+ if (VARNAME >= (N)) { \
477
+ return; \
478
+ }
479
+ #define GET_PARALLEL_IDS_2D(VAR_X, VAR_Y, WIDTH, HEIGHT) \
480
+ const uint VAR_X = __mul24(blockIdx.x, blockDim.x) + threadIdx.x; \
481
+ const uint VAR_Y = __mul24(blockIdx.y, blockDim.y) + threadIdx.y; \
482
+ if (VAR_X >= (WIDTH) || VAR_Y >= (HEIGHT)) \
483
+ return;
484
+ #define END_PARALLEL()
485
+ #define END_PARALLEL_NORET()
486
+ #define END_PARALLEL_2D_NORET()
487
+ #define END_PARALLEL_2D()
488
+ #define RETURN_PARALLEL() return
489
+ #define CHECKLAUNCH() C10_CUDA_CHECK(cudaGetLastError());
490
+ #define ISONDEVICE true
491
+ #define SYNCDEVICE() HANDLECUDA(cudaDeviceSynchronize())
492
+ #define START_TIME(TN) \
493
+ cudaEvent_t __time_start_##TN, __time_stop_##TN; \
494
+ cudaEventCreate(&__time_start_##TN); \
495
+ cudaEventCreate(&__time_stop_##TN); \
496
+ cudaEventRecord(__time_start_##TN);
497
+ #define STOP_TIME(TN) cudaEventRecord(__time_stop_##TN);
498
+ #define GET_TIME(TN, TOPTR) \
499
+ cudaEventSynchronize(__time_stop_##TN); \
500
+ cudaEventElapsedTime((TOPTR), __time_start_##TN, __time_stop_##TN);
501
+ #define START_TIME_CU(TN) START_TIME(CN)
502
+ #define STOP_TIME_CU(TN) STOP_TIME(TN)
503
+ #define GET_TIME_CU(TN, TOPTR) GET_TIME(TN, TOPTR)
504
+
505
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.backward.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.backward.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.backward_dbg.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.backward_dbg.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.calc_gradients.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.calc_gradients.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.calc_signature.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.calc_signature.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.construct.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.construct.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.create_selector.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.create_selector.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.destruct.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.destruct.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.fill_bg.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.fill_bg.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.forward.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.forward.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.norm_cam_gradients.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.norm_cam_gradients.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.norm_sphere_gradients.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.norm_sphere_gradients.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/cuda/renderer.render.gpu.cu ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.render.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/global.h ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_GLOBAL_H
10
+ #define PULSAR_GLOBAL_H
11
+
12
+ #include "./constants.h"
13
+ #ifndef WIN32
14
+ #include <csignal>
15
+ #endif
16
+
17
+ #if defined(_WIN64) || defined(_WIN32)
18
+ #define uint unsigned int
19
+ #define ushort unsigned short
20
+ #endif
21
+
22
+ #include "./logging.h" // <- include before torch/extension.h
23
+
24
+ #define MAX_GRAD_SPHERES 128
25
+
26
+ #ifdef __CUDACC__
27
+ #define INLINE __forceinline__
28
+ #define HOST __host__
29
+ #define DEVICE __device__
30
+ #define GLOBAL __global__
31
+ #define RESTRICT __restrict__
32
+ #define DEBUGBREAK()
33
+ #ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
34
+ #pragma nv_diag_suppress 1866
35
+ #pragma nv_diag_suppress 2941
36
+ #pragma nv_diag_suppress 2951
37
+ #pragma nv_diag_suppress 2967
38
+ #else
39
+ #pragma diag_suppress = attribute_not_allowed
40
+ #pragma diag_suppress = 1866
41
+ #pragma diag_suppress = 2941
42
+ #pragma diag_suppress = 2951
43
+ #pragma diag_suppress = 2967
44
+ #endif
45
+ #else // __CUDACC__
46
+ #define INLINE inline
47
+ #define HOST
48
+ #define DEVICE
49
+ #define GLOBAL
50
+ #define RESTRICT
51
+ #define DEBUGBREAK() std::raise(SIGINT)
52
+ // Don't care about pytorch warnings; they shouldn't clutter our warnings.
53
+ #pragma clang diagnostic push
54
+ #pragma clang diagnostic ignored "-Weverything"
55
+ #include <torch/extension.h>
56
+ #pragma clang diagnostic pop
57
+ #ifdef WITH_CUDA
58
+ #include <ATen/cuda/CUDAContext.h>
59
+ #include <vector_functions.h>
60
+ #else
61
+ #ifndef cudaStream_t
62
+ typedef void* cudaStream_t;
63
+ #endif
64
+ struct int2 {
65
+ int x, y;
66
+ };
67
+ struct ushort2 {
68
+ unsigned short x, y;
69
+ };
70
+ struct float2 {
71
+ float x, y;
72
+ };
73
+ struct float3 {
74
+ float x, y, z;
75
+ };
76
+ inline float3 make_float3(const float& x, const float& y, const float& z) {
77
+ float3 res;
78
+ res.x = x;
79
+ res.y = y;
80
+ res.z = z;
81
+ return res;
82
+ }
83
+ #endif
84
+ namespace py = pybind11;
85
+
86
+ inline bool operator==(const float3& a, const float3& b) {
87
+ return a.x == b.x && a.y == b.y && a.z == b.z;
88
+ }
89
+ #endif // __CUDACC__
90
+ #define IHD INLINE HOST DEVICE
91
+
92
+ // An assertion command that can be used on host and device.
93
+ #ifdef PULSAR_ASSERTIONS
94
+ #ifdef __CUDACC__
95
+ #define PASSERT(VAL) \
96
+ if (!(VAL)) { \
97
+ printf( \
98
+ "Pulsar assertion failed in %s, line %d: %s.\n", \
99
+ __FILE__, \
100
+ __LINE__, \
101
+ #VAL); \
102
+ }
103
+ #else
104
+ #define PASSERT(VAL) \
105
+ if (!(VAL)) { \
106
+ printf( \
107
+ "Pulsar assertion failed in %s, line %d: %s.\n", \
108
+ __FILE__, \
109
+ __LINE__, \
110
+ #VAL); \
111
+ std::raise(SIGINT); \
112
+ }
113
+ #endif
114
+ #else
115
+ #define PASSERT(VAL)
116
+ #endif
117
+
118
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/README.md ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # Device-specific host compilation units
2
+
3
+ This folder contains `.cpp` files to create compilation units
4
+ for device specific functions. See `../include/README.md` for
5
+ more information.
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/commands.h ADDED
@@ -0,0 +1,391 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_COMMANDS_H_
10
+ #define PULSAR_NATIVE_COMMANDS_H_
11
+
12
+ #ifdef _MSC_VER
13
+ #include <intrin.h>
14
+ #define __builtin_popcount (int)__popcnt
15
+ #endif
16
+
17
+ // Definitions for CPU commands.
18
+ // #include <execution>
19
+ // #include <numeric>
20
+
21
+ namespace cg {
22
+ struct coalesced_group {
23
+ INLINE uint thread_rank() const {
24
+ return 0u;
25
+ }
26
+ INLINE uint size() const {
27
+ return 1u;
28
+ }
29
+ INLINE uint ballot(uint val) const {
30
+ return static_cast<uint>(val > 0);
31
+ }
32
+ };
33
+
34
+ struct thread_block {
35
+ INLINE uint thread_rank() const {
36
+ return 0u;
37
+ }
38
+ INLINE uint size() const {
39
+ return 1u;
40
+ }
41
+ INLINE void sync() const {}
42
+ };
43
+
44
+ INLINE coalesced_group coalesced_threads() {
45
+ coalesced_group ret;
46
+ return ret;
47
+ }
48
+
49
+ INLINE thread_block this_thread_block() {
50
+ thread_block ret;
51
+ return ret;
52
+ }
53
+ } // namespace cg
54
+ #define SHFL_SYNC(a, b, c) (b)
55
+ template <typename T>
56
+ T WARP_CUMSUM(
57
+ const cg::coalesced_group& group,
58
+ const uint& mask,
59
+ const T& base) {
60
+ return base;
61
+ }
62
+
63
+ template <typename T>
64
+ DEVICE T
65
+ WARP_MAX(const cg::coalesced_group& group, const uint& mask, const T& base) {
66
+ return base;
67
+ }
68
+
69
+ template <typename T>
70
+ DEVICE T
71
+ WARP_SUM(const cg::coalesced_group& group, const uint& mask, const T& base) {
72
+ return base;
73
+ }
74
+
75
+ INLINE DEVICE float3 WARP_SUM_FLOAT3(
76
+ const cg::coalesced_group& group,
77
+ const uint& mask,
78
+ const float3& base) {
79
+ return base;
80
+ }
81
+
82
+ #define ACTIVEMASK() (1u << 31)
83
+ #define ALIGN(VAL)
84
+ #define SYNC()
85
+ #define THREADFENCE_B()
86
+ #define BALLOT(mask, val) (val != 0)
87
+ #define SHARED
88
+ // Floating point.
89
+ #define FMAX(a, b) std::fmax((a), (b))
90
+ #define FMIN(a, b) std::fmin((a), (b))
91
+ INLINE float atomicMax(float* address, float val) {
92
+ *address = std::max(*address, val);
93
+ return *address;
94
+ }
95
+ INLINE float atomicMin(float* address, float val) {
96
+ *address = std::min(*address, val);
97
+ return *address;
98
+ }
99
+ #define FMUL(a, b) ((a) * (b))
100
+ #define FDIV(a, b) ((a) / (b))
101
+ #define FSUB(a, b) ((a) - (b))
102
+ #define FABSLEQAS(a, b, c) \
103
+ ((a) <= (b) ? FSUB((b), (a)) <= (c) : FSUB((a), (b)) < (c))
104
+ #define FADD(a, b) ((a) + (b))
105
+ #define FSQRT(a) sqrtf(a)
106
+ #define FEXP(a) fasterexp(a)
107
+ #define FLN(a) fasterlog(a)
108
+ #define FPOW(a, b) powf((a), (b))
109
+ #define FROUND(x) roundf(x)
110
+ #define FCEIL(a) ceilf(a)
111
+ #define FFLOOR(a) floorf(a)
112
+ #define FSATURATE(x) std::max(0.f, std::min(1.f, x))
113
+ #define FABS(a) abs(a)
114
+ #define FMA(x, y, z) ((x) * (y) + (z))
115
+ #define I2F(a) static_cast<float>(a)
116
+ #define FRCP(x) (1.f / (x))
117
+ #define IASF(x, loc) memcpy(&(loc), &(x), sizeof(x))
118
+ #define FASI(x, loc) memcpy(&(loc), &(x), sizeof(x))
119
+ #define DMAX(a, b) std::max((a), (b))
120
+ #define DMIN(a, b) std::min((a), (b))
121
+ #define DSATURATE(a) DMIN(1., DMAX(0., (a)))
122
+ #define DSQRT(a) sqrt(a)
123
+ //
124
+ //
125
+ //
126
+ //
127
+ //
128
+ //
129
+ //
130
+ //
131
+ //
132
+ //
133
+ //
134
+ //
135
+ // uint.
136
+ #define CLZ(VAL) _clz(VAL)
137
+ template <typename T>
138
+ INLINE T ATOMICADD(T* address, T val) {
139
+ T old = *address;
140
+ *address += val;
141
+ return old;
142
+ }
143
+ template <typename T>
144
+ INLINE void ATOMICADD_F3(T* address, T val) {
145
+ ATOMICADD(&(address->x), val.x);
146
+ ATOMICADD(&(address->y), val.y);
147
+ ATOMICADD(&(address->z), val.z);
148
+ }
149
+ #define ATOMICADD_B(a, b) ATOMICADD((a), (b))
150
+ #define POPC(a) __builtin_popcount(a)
151
+
152
+ // int.
153
+ #define IMIN(a, b) std::min((a), (b))
154
+ #define IMAX(a, b) std::max((a), (b))
155
+ #define IABS(a) abs(a)
156
+
157
+ // Checks.
158
+ // like TORCH_CHECK_ARG in PyTorch > 1.10
159
+ #define ARGCHECK(cond, argN, ...) \
160
+ TORCH_CHECK(cond, "invalid argument ", argN, ": ", __VA_ARGS__)
161
+
162
+ // Math.
163
+ #define NORM3DF(x, y, z) sqrtf(x* x + y * y + z * z)
164
+ #define RNORM3DF(x, y, z) (1.f / sqrtf(x * x + y * y + z * z))
165
+
166
+ // High level.
167
+ #define PREFETCH(PTR)
168
+ #define GET_SORT_WS_SIZE(RES_PTR, KEY_TYPE, VAL_TYPE, NUM_OBJECTS) \
169
+ *(RES_PTR) = 0;
170
+ #define GET_REDUCE_WS_SIZE(RES_PTR, TYPE, REDUCE_OP, NUM_OBJECTS) \
171
+ *(RES_PTR) = 0;
172
+ #define GET_SELECT_WS_SIZE( \
173
+ RES_PTR, TYPE_SELECTOR, TYPE_SELECTION, NUM_OBJECTS) \
174
+ *(RES_PTR) = 0;
175
+ #define GET_SUM_WS_SIZE(RES_PTR, TYPE_SUM, NUM_OBJECTS) *(RES_PTR) = 0;
176
+ #define GET_MM_WS_SIZE(RES_PTR, TYPE, NUM_OBJECTS) *(RES_PTR) = 0;
177
+
178
+ #define SORT_DESCENDING( \
179
+ TMPN1, SORT_PTR, SORTED_PTR, VAL_PTR, VAL_SORTED_PTR, NUM_OBJECTS) \
180
+ std::vector<size_t> TMPN1(NUM_OBJECTS); \
181
+ std::iota(TMPN1.begin(), TMPN1.end(), 0); \
182
+ const auto TMPN1##_val_ptr = (SORT_PTR); \
183
+ std::sort( \
184
+ TMPN1.begin(), TMPN1.end(), [&TMPN1##_val_ptr](size_t i1, size_t i2) { \
185
+ return TMPN1##_val_ptr[i1] > TMPN1##_val_ptr[i2]; \
186
+ }); \
187
+ for (int i = 0; i < (NUM_OBJECTS); ++i) { \
188
+ (SORTED_PTR)[i] = (SORT_PTR)[TMPN1[i]]; \
189
+ } \
190
+ for (int i = 0; i < (NUM_OBJECTS); ++i) { \
191
+ (VAL_SORTED_PTR)[i] = (VAL_PTR)[TMPN1[i]]; \
192
+ }
193
+
194
+ #define SORT_ASCENDING( \
195
+ SORT_PTR, SORTED_PTR, VAL_PTR, VAL_SORTED_PTR, NUM_OBJECTS, STREAM) \
196
+ { \
197
+ std::vector<size_t> TMPN1(NUM_OBJECTS); \
198
+ std::iota(TMPN1.begin(), TMPN1.end(), 0); \
199
+ const auto TMPN1_val_ptr = (SORT_PTR); \
200
+ std::sort( \
201
+ TMPN1.begin(), \
202
+ TMPN1.end(), \
203
+ [&TMPN1_val_ptr](size_t i1, size_t i2) -> bool { \
204
+ return TMPN1_val_ptr[i1] < TMPN1_val_ptr[i2]; \
205
+ }); \
206
+ for (int i = 0; i < (NUM_OBJECTS); ++i) { \
207
+ (SORTED_PTR)[i] = (SORT_PTR)[TMPN1[i]]; \
208
+ } \
209
+ for (int i = 0; i < (NUM_OBJECTS); ++i) { \
210
+ (VAL_SORTED_PTR)[i] = (VAL_PTR)[TMPN1[i]]; \
211
+ } \
212
+ }
213
+
214
+ #define SORT_DESCENDING_WS( \
215
+ TMPN1, \
216
+ SORT_PTR, \
217
+ SORTED_PTR, \
218
+ VAL_PTR, \
219
+ VAL_SORTED_PTR, \
220
+ NUM_OBJECTS, \
221
+ WORSPACE_PTR, \
222
+ WORKSPACE_SIZE) \
223
+ SORT_DESCENDING( \
224
+ TMPN1, SORT_PTR, SORTED_PTR, VAL_PTR, VAL_SORTED_PTR, NUM_OBJECTS)
225
+
226
+ #define SORT_ASCENDING_WS( \
227
+ SORT_PTR, \
228
+ SORTED_PTR, \
229
+ VAL_PTR, \
230
+ VAL_SORTED_PTR, \
231
+ NUM_OBJECTS, \
232
+ WORSPACE_PTR, \
233
+ WORKSPACE_SIZE, \
234
+ STREAM) \
235
+ SORT_ASCENDING( \
236
+ SORT_PTR, SORTED_PTR, VAL_PTR, VAL_SORTED_PTR, NUM_OBJECTS, STREAM)
237
+
238
+ #define REDUCE(REDUCE_PTR, RESULT_PTR, NUM_ITEMS, REDUCE_OP, REDUCE_INIT) \
239
+ { \
240
+ *(RESULT_PTR) = (REDUCE_INIT); \
241
+ for (int i = 0; i < (NUM_ITEMS); ++i) { \
242
+ *(RESULT_PTR) = REDUCE_OP(*(RESULT_PTR), (REDUCE_PTR)[i]); \
243
+ } \
244
+ }
245
+ #define REDUCE_WS( \
246
+ REDUCE_PTR, \
247
+ RESULT_PTR, \
248
+ NUM_ITEMS, \
249
+ REDUCE_OP, \
250
+ REDUCE_INIT, \
251
+ WORKSPACE_PTR, \
252
+ WORKSPACE_SIZE, \
253
+ STREAM) \
254
+ REDUCE(REDUCE_PTR, RESULT_PTR, NUM_ITEMS, REDUCE_OP, REDUCE_INIT)
255
+
256
+ #define SELECT_FLAGS_WS( \
257
+ FLAGS_PTR, \
258
+ ITEM_PTR, \
259
+ OUT_PTR, \
260
+ NUM_SELECTED_PTR, \
261
+ NUM_ITEMS, \
262
+ WORKSPACE_PTR, \
263
+ WORSPACE_BYTES, \
264
+ STREAM) \
265
+ { \
266
+ *NUM_SELECTED_PTR = 0; \
267
+ ptrdiff_t write_pos = 0; \
268
+ for (int i = 0; i < NUM_ITEMS; ++i) { \
269
+ if (FLAGS_PTR[i]) { \
270
+ OUT_PTR[write_pos++] = ITEM_PTR[i]; \
271
+ *NUM_SELECTED_PTR += 1; \
272
+ } \
273
+ } \
274
+ }
275
+
276
+ template <typename T>
277
+ void SUM_WS(
278
+ T* SUM_PTR,
279
+ T* OUT_PTR,
280
+ size_t NUM_OBJECTS,
281
+ char* WORKSPACE_PTR,
282
+ size_t WORKSPACE_BYTES,
283
+ cudaStream_t STREAM) {
284
+ *(OUT_PTR) = T();
285
+ for (int i = 0; i < (NUM_OBJECTS); ++i) {
286
+ *(OUT_PTR) = *(OUT_PTR) + (SUM_PTR)[i];
287
+ }
288
+ }
289
+
290
+ template <typename T>
291
+ void MIN_WS(
292
+ T* MIN_PTR,
293
+ T* OUT_PTR,
294
+ size_t NUM_OBJECTS,
295
+ char* WORKSPACE_PTR,
296
+ size_t WORKSPACE_BYTES,
297
+ cudaStream_t STREAM) {
298
+ *(OUT_PTR) = T();
299
+ for (int i = 0; i < (NUM_OBJECTS); ++i) {
300
+ *(OUT_PTR) = std::min<T>(*(OUT_PTR), (MIN_PTR)[i]);
301
+ }
302
+ }
303
+
304
+ template <typename T>
305
+ void MAX_WS(
306
+ T* MAX_PTR,
307
+ T* OUT_PTR,
308
+ size_t NUM_OBJECTS,
309
+ char* WORKSPACE_PTR,
310
+ size_t WORKSPACE_BYTES,
311
+ cudaStream_t STREAM) {
312
+ *(OUT_PTR) = T();
313
+ for (int i = 0; i < (NUM_OBJECTS); ++i) {
314
+ *(OUT_PTR) = std::max<T>(*(OUT_PTR), (MAX_PTR)[i]);
315
+ }
316
+ }
317
+ //
318
+ //
319
+ //
320
+ //
321
+ #define COPY_HOST_DEV(PTR_D, PTR_H, TYPE, SIZE) \
322
+ std::memcpy((PTR_D), (PTR_H), sizeof(TYPE) * (SIZE))
323
+ //
324
+ #define COPY_DEV_HOST(PTR_H, PTR_D, TYPE, SIZE) \
325
+ std::memcpy((PTR_H), (PTR_D), sizeof(TYPE) * (SIZE))
326
+ //
327
+ #define COPY_DEV_DEV(PTR_T, PTR_S, TYPE, SIZE) \
328
+ std::memcpy((PTR_T), (PTR_S), sizeof(TYPE) * SIZE)
329
+ //
330
+
331
+ #define MALLOC(VAR, TYPE, SIZE) MALLOC_HOST(VAR, TYPE, SIZE)
332
+ #define FREE(PTR) FREE_HOST(PTR)
333
+ #define MEMSET(VAR, VAL, TYPE, SIZE, STREAM) \
334
+ memset((VAR), (VAL), sizeof(TYPE) * (SIZE))
335
+ //
336
+
337
+ #define LAUNCH_MAX_PARALLEL_1D(FUNC, N, STREAM, ...) FUNC(__VA_ARGS__);
338
+ #define LAUNCH_PARALLEL_1D(FUNC, N, TN, STREAM, ...) FUNC(__VA_ARGS__);
339
+ #define LAUNCH_MAX_PARALLEL_2D(FUNC, NX, NY, STREAM, ...) FUNC(__VA_ARGS__);
340
+ #define LAUNCH_PARALLEL_2D(FUNC, NX, NY, TX, TY, STREAM, ...) FUNC(__VA_ARGS__);
341
+ //
342
+ //
343
+ //
344
+ //
345
+ //
346
+ #define GET_PARALLEL_IDX_1D(VARNAME, N) \
347
+ for (uint VARNAME = 0; VARNAME < (N); ++VARNAME) {
348
+ #define GET_PARALLEL_IDS_2D(VAR_X, VAR_Y, WIDTH, HEIGHT) \
349
+ int2 blockDim; \
350
+ blockDim.x = 1; \
351
+ blockDim.y = 1; \
352
+ uint __parallel_2d_width = WIDTH; \
353
+ uint __parallel_2d_height = HEIGHT; \
354
+ for (uint VAR_Y = 0; VAR_Y < __parallel_2d_height; ++(VAR_Y)) { \
355
+ for (uint VAR_X = 0; VAR_X < __parallel_2d_width; ++(VAR_X)) {
356
+ //
357
+ //
358
+ //
359
+ #define END_PARALLEL() \
360
+ end_parallel :; \
361
+ }
362
+ #define END_PARALLEL_NORET() }
363
+ #define END_PARALLEL_2D() \
364
+ end_parallel :; \
365
+ } \
366
+ }
367
+ #define END_PARALLEL_2D_NORET() \
368
+ } \
369
+ }
370
+ #define RETURN_PARALLEL() goto end_parallel;
371
+ #define CHECKLAUNCH()
372
+ #define ISONDEVICE false
373
+ #define SYNCDEVICE()
374
+ #define START_TIME(TN) \
375
+ auto __time_start_##TN = std::chrono::steady_clock::now();
376
+ #define STOP_TIME(TN) auto __time_stop_##TN = std::chrono::steady_clock::now();
377
+ #define GET_TIME(TN, TOPTR) \
378
+ *TOPTR = std::chrono::duration_cast<std::chrono::milliseconds>( \
379
+ __time_stop_##TN - __time_start_##TN) \
380
+ .count()
381
+ #define START_TIME_CU(TN) \
382
+ cudaEvent_t __time_start_##TN, __time_stop_##TN; \
383
+ cudaEventCreate(&__time_start_##TN); \
384
+ cudaEventCreate(&__time_stop_##TN); \
385
+ cudaEventRecord(__time_start_##TN);
386
+ #define STOP_TIME_CU(TN) cudaEventRecord(__time_stop_##TN);
387
+ #define GET_TIME_CU(TN, TOPTR) \
388
+ cudaEventSynchronize(__time_stop_##TN); \
389
+ cudaEventElapsedTime((TOPTR), __time_start_##TN, __time_stop_##TN);
390
+
391
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.backward.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.backward.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.backward_dbg.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.backward_dbg.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.calc_gradients.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.calc_gradients.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.calc_signature.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.calc_signature.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.construct.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.construct.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.create_selector.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.create_selector.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.destruct.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.destruct.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.fill_bg.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.fill_bg.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.forward.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.forward.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.norm_cam_gradients.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.norm_cam_gradients.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.norm_sphere_gradients.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.norm_sphere_gradients.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/host/renderer.render.cpu.cpp ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #include "../include/renderer.render.instantiate.h"
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/README.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # The `include` folder
2
+
3
+ This folder contains header files with implementations of several useful
4
+ algorithms. These implementations are usually done in files called `x.device.h`
5
+ and use macros that route every device specific command to the right
6
+ implementation (see `commands.h`).
7
+
8
+ If you're using a device specific implementation, include `x.device.h`.
9
+ This gives you the high-speed, device specific implementation that lets
10
+ you work with all the details of the datastructure. All function calls are
11
+ inlined. If you need to work with the high-level interface and be able to
12
+ dynamically pick a device, only include `x.h`. The functions there are
13
+ templated with a boolean `DEV` flag and are instantiated in device specific
14
+ compilation units. You will not be able to use any other functions, but can
15
+ use `func<true>(params)` to work on a CUDA device, or `func<false>(params)`
16
+ to work on the host.
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/camera.device.h ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_INCLUDE_CAMERA_DEVICE_H_
10
+ #define PULSAR_NATIVE_INCLUDE_CAMERA_DEVICE_H_
11
+
12
+ #include "../global.h"
13
+ #include "./camera.h"
14
+ #include "./commands.h"
15
+
16
+ namespace pulsar {
17
+ IHD CamGradInfo::CamGradInfo() {
18
+ cam_pos = make_float3(0.f, 0.f, 0.f);
19
+ pixel_0_0_center = make_float3(0.f, 0.f, 0.f);
20
+ pixel_dir_x = make_float3(0.f, 0.f, 0.f);
21
+ pixel_dir_y = make_float3(0.f, 0.f, 0.f);
22
+ }
23
+ } // namespace pulsar
24
+
25
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/camera.h ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_INCLUDE_CAMERA_H_
10
+ #define PULSAR_NATIVE_INCLUDE_CAMERA_H_
11
+
12
+ #include <stdint.h>
13
+ #include "../global.h"
14
+
15
+ namespace pulsar {
16
+ /**
17
+ * Everything that's needed to raycast with our camera model.
18
+ */
19
+ struct CamInfo {
20
+ float3 eye; /** Position in world coordinates. */
21
+ float3 pixel_0_0_center; /** LUC center of pixel position in world
22
+ coordinates. */
23
+ float3 pixel_dir_x; /** Direction for increasing x for one pixel to the next,
24
+ * in world coordinates. */
25
+ float3 pixel_dir_y; /** Direction for increasing y for one pixel to the next,
26
+ * in world coordinates. */
27
+ float3 sensor_dir_z; /** Normalized direction vector from eye through the
28
+ * sensor in z direction (optical axis). */
29
+ float half_pixel_size; /** Half size of a pixel, in world coordinates. This
30
+ * must be consistent with pixel_dir_x and pixel_dir_y!
31
+ */
32
+ float focal_length; /** The focal length, if applicable. */
33
+ uint aperture_width; /** Full image width in px, possibly not fully used
34
+ * in case of a shifted principal point. */
35
+ uint aperture_height; /** Full image height in px, possibly not fully used
36
+ * in case of a shifted principal point. */
37
+ uint film_width; /** Resulting image width. */
38
+ uint film_height; /** Resulting image height. */
39
+ /** The top left coordinates (inclusive) of the film in the full aperture. */
40
+ uint film_border_left, film_border_top;
41
+ int32_t principal_point_offset_x; /** Horizontal principal point offset. */
42
+ int32_t principal_point_offset_y; /** Vertical principal point offset. */
43
+ float min_dist; /** Minimum distance for a ball to be rendered. */
44
+ float max_dist; /** Maximum distance for a ball to be rendered. */
45
+ float norm_fac; /** 1 / (max_dist - min_dist), pre-computed. */
46
+ /** The depth where to place the background, in normalized coordinates where
47
+ * 0. is the backmost depth and 1. the frontmost. */
48
+ float background_normalization_depth;
49
+ /** The number of image content channels to use. Usually three. */
50
+ uint n_channels;
51
+ /** Whether to use an orthogonal instead of a perspective projection. */
52
+ bool orthogonal_projection;
53
+ /** Whether to use a right-handed system (inverts the z axis). */
54
+ bool right_handed;
55
+ };
56
+
57
+ inline bool operator==(const CamInfo& a, const CamInfo& b) {
58
+ return a.film_width == b.film_width && a.film_height == b.film_height &&
59
+ a.background_normalization_depth == b.background_normalization_depth &&
60
+ a.n_channels == b.n_channels &&
61
+ a.orthogonal_projection == b.orthogonal_projection &&
62
+ a.right_handed == b.right_handed;
63
+ };
64
+
65
+ struct CamGradInfo {
66
+ HOST DEVICE CamGradInfo();
67
+ float3 cam_pos;
68
+ float3 pixel_0_0_center;
69
+ float3 pixel_dir_x;
70
+ float3 pixel_dir_y;
71
+ };
72
+
73
+ // TODO: remove once https://github.com/NVlabs/cub/issues/172 is resolved.
74
+ struct IntWrapper {
75
+ int val;
76
+ };
77
+
78
+ } // namespace pulsar
79
+
80
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/closest_sphere_tracker.device.h ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_INCLUDE_CLOSEST_SPHERE_TRACKER_DEVICE_H_
10
+ #define PULSAR_NATIVE_INCLUDE_CLOSEST_SPHERE_TRACKER_DEVICE_H_
11
+
12
+ #include "../global.h"
13
+
14
+ namespace pulsar {
15
+ namespace Renderer {
16
+
17
+ /**
18
+ * A facility to track the closest spheres to the camera.
19
+ *
20
+ * Their max number is defined by MAX_GRAD_SPHERES (this is defined in
21
+ * `pulsar/native/global.h`). This is done to keep the performance as high as
22
+ * possible because this struct needs to do updates continuously on the GPU.
23
+ */
24
+ struct ClosestSphereTracker {
25
+ public:
26
+ IHD ClosestSphereTracker(const int& n_track) : n_hits(0), n_track(n_track) {
27
+ PASSERT(n_track < MAX_GRAD_SPHERES);
28
+ // Initialize the sphere IDs to -1 and the weights to 0.
29
+ for (int i = 0; i < n_track; ++i) {
30
+ this->most_important_sphere_ids[i] = -1;
31
+ this->closest_sphere_intersection_depths[i] = MAX_FLOAT;
32
+ }
33
+ };
34
+
35
+ IHD void track(
36
+ const uint& sphere_idx,
37
+ const float& intersection_depth,
38
+ const uint& coord_x,
39
+ const uint& coord_y) {
40
+ PULSAR_LOG_DEV_PIX(
41
+ PULSAR_LOG_TRACKER_PIX,
42
+ "tracker|tracking sphere %u (depth: %f).\n",
43
+ sphere_idx,
44
+ intersection_depth);
45
+ for (int i = IMIN(this->n_hits, n_track) - 1; i >= -1; --i) {
46
+ if (i < 0 ||
47
+ this->closest_sphere_intersection_depths[i] < intersection_depth) {
48
+ // Write position is i+1.
49
+ PULSAR_LOG_DEV_PIX(
50
+ PULSAR_LOG_TRACKER_PIX,
51
+ "tracker|determined writing position: %d.\n",
52
+ i + 1);
53
+ if (i + 1 < n_track) {
54
+ // Shift every other sphere back.
55
+ for (int j = n_track - 1; j > i + 1; --j) {
56
+ this->closest_sphere_intersection_depths[j] =
57
+ this->closest_sphere_intersection_depths[j - 1];
58
+ this->most_important_sphere_ids[j] =
59
+ this->most_important_sphere_ids[j - 1];
60
+ }
61
+ this->closest_sphere_intersection_depths[i + 1] = intersection_depth;
62
+ this->most_important_sphere_ids[i + 1] = sphere_idx;
63
+ }
64
+ break;
65
+ }
66
+ }
67
+ #if PULSAR_LOG_TRACKER_PIX
68
+ PULSAR_LOG_DEV_PIX(
69
+ PULSAR_LOG_TRACKER_PIX,
70
+ "tracker|sphere list after adding sphere %u:\n",
71
+ sphere_idx);
72
+ for (int i = 0; i < n_track; ++i) {
73
+ PULSAR_LOG_DEV_PIX(
74
+ PULSAR_LOG_TRACKER_PIX,
75
+ "tracker|sphere %d: %d (depth: %f).\n",
76
+ i,
77
+ this->most_important_sphere_ids[i],
78
+ this->closest_sphere_intersection_depths[i]);
79
+ }
80
+ #endif // PULSAR_LOG_TRACKER_PIX
81
+ this->n_hits += 1;
82
+ }
83
+
84
+ /**
85
+ * Get the number of hits registered.
86
+ */
87
+ IHD int get_n_hits() const {
88
+ return this->n_hits;
89
+ }
90
+
91
+ /**
92
+ * Get the idx closest sphere ID.
93
+ *
94
+ * For example, get_closest_sphere_id(0) gives the overall closest
95
+ * sphere id.
96
+ *
97
+ * This method is implemented for highly optimized scenarios and will *not*
98
+ * perform an index check at runtime if assertions are disabled. idx must be
99
+ * >=0 and < IMIN(n_hits, n_track) for a valid result, if it is >=
100
+ * n_hits it will return -1.
101
+ */
102
+ IHD int get_closest_sphere_id(const int& idx) {
103
+ PASSERT(idx >= 0 && idx < n_track);
104
+ return this->most_important_sphere_ids[idx];
105
+ }
106
+
107
+ /**
108
+ * Get the idx closest sphere normalized_depth.
109
+ *
110
+ * For example, get_closest_sphere_depth(0) gives the overall closest
111
+ * sphere depth (normalized).
112
+ *
113
+ * This method is implemented for highly optimized scenarios and will *not*
114
+ * perform an index check at runtime if assertions are disabled. idx must be
115
+ * >=0 and < IMIN(n_hits, n_track) for a valid result, if it is >=
116
+ * n_hits it will return 1. + FEPS.
117
+ */
118
+ IHD float get_closest_sphere_depth(const int& idx) {
119
+ PASSERT(idx >= 0 && idx < n_track);
120
+ return this->closest_sphere_intersection_depths[idx];
121
+ }
122
+
123
+ private:
124
+ /** The number of registered hits so far. */
125
+ int n_hits;
126
+ /** The number of intersections to track. Must be <MAX_GRAD_SPHERES. */
127
+ int n_track;
128
+ /** The sphere ids of the n_track spheres with the highest color
129
+ * contribution. */
130
+ int most_important_sphere_ids[MAX_GRAD_SPHERES];
131
+ /** The normalized depths of the closest n_track spheres. */
132
+ float closest_sphere_intersection_depths[MAX_GRAD_SPHERES];
133
+ };
134
+
135
+ } // namespace Renderer
136
+ } // namespace pulsar
137
+
138
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/commands.h ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_COMMANDS_ROUTING_H_
10
+ #define PULSAR_NATIVE_COMMANDS_ROUTING_H_
11
+
12
+ #include "../global.h"
13
+
14
+ // Commands available everywhere.
15
+ #define MALLOC_HOST(VAR, TYPE, SIZE) \
16
+ VAR = static_cast<TYPE*>(malloc(sizeof(TYPE) * (SIZE)))
17
+ #define FREE_HOST(PTR) free(PTR)
18
+
19
+ /* Include command definitions depending on CPU or GPU use. */
20
+
21
+ #ifdef __CUDACC__
22
+ // TODO: find out which compiler we're using here and use the suppression.
23
+ // #pragma push
24
+ // #pragma diag_suppress = 68
25
+ #include <ATen/cuda/CUDAContext.h>
26
+ // #pragma pop
27
+ #include "../cuda/commands.h"
28
+ #else
29
+ #pragma clang diagnostic push
30
+ #pragma clang diagnostic ignored "-Weverything"
31
+ #pragma clang diagnostic pop
32
+ #include "../host/commands.h"
33
+ #endif
34
+
35
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/fastermath.h ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef PULSAR_NATIVE_INCLUDE_FASTERMATH_H_
2
+ #define PULSAR_NATIVE_INCLUDE_FASTERMATH_H_
3
+
4
+ // @lint-ignore-every LICENSELINT
5
+ /*=====================================================================*
6
+ * Copyright (C) 2011 Paul Mineiro *
7
+ * All rights reserved. *
8
+ * *
9
+ * Redistribution and use in source and binary forms, with *
10
+ * or without modification, are permitted provided that the *
11
+ * following conditions are met: *
12
+ * *
13
+ * * Redistributions of source code must retain the *
14
+ * above copyright notice, this list of conditions and *
15
+ * the following disclaimer. *
16
+ * *
17
+ * * Redistributions in binary form must reproduce the *
18
+ * above copyright notice, this list of conditions and *
19
+ * the following disclaimer in the documentation and/or *
20
+ * other materials provided with the distribution. *
21
+ * *
22
+ * * Neither the name of Paul Mineiro nor the names *
23
+ * of other contributors may be used to endorse or promote *
24
+ * products derived from this software without specific *
25
+ * prior written permission. *
26
+ * *
27
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
28
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
29
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
30
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
31
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER *
32
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
33
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
34
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE *
35
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
36
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
37
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
38
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
39
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
40
+ * POSSIBILITY OF SUCH DAMAGE. *
41
+ * *
42
+ * Contact: Paul Mineiro <paul@mineiro.com> *
43
+ *=====================================================================*/
44
+
45
+ #include <stdint.h>
46
+ #include "./commands.h"
47
+
48
+ #ifdef __cplusplus
49
+ #define cast_uint32_t static_cast<uint32_t>
50
+ #else
51
+ #define cast_uint32_t (uint32_t)
52
+ #endif
53
+
54
+ IHD float fasterlog2(float x) {
55
+ union {
56
+ float f;
57
+ uint32_t i;
58
+ } vx = {x};
59
+ float y = vx.i;
60
+ y *= 1.1920928955078125e-7f;
61
+ return y - 126.94269504f;
62
+ }
63
+
64
+ IHD float fasterlog(float x) {
65
+ // return 0.69314718f * fasterlog2 (x);
66
+ union {
67
+ float f;
68
+ uint32_t i;
69
+ } vx = {x};
70
+ float y = vx.i;
71
+ y *= 8.2629582881927490e-8f;
72
+ return y - 87.989971088f;
73
+ }
74
+
75
+ IHD float fasterpow2(float p) {
76
+ float clipp = (p < -126) ? -126.0f : p;
77
+ union {
78
+ uint32_t i;
79
+ float f;
80
+ } v = {cast_uint32_t((1 << 23) * (clipp + 126.94269504f))};
81
+ return v.f;
82
+ }
83
+
84
+ IHD float fasterexp(float p) {
85
+ return fasterpow2(1.442695040f * p);
86
+ }
87
+
88
+ #endif
third_party/AnyBimanual/third_party/pytorch3d/pytorch3d/csrc/pulsar/include/math.h ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #ifndef PULSAR_NATIVE_IMPL_MATH_H_
10
+ #define PULSAR_NATIVE_IMPL_MATH_H_
11
+
12
+ #include "./camera.h"
13
+ #include "./commands.h"
14
+ #include "./fastermath.h"
15
+
16
+ /**
17
+ * Get the direction of val.
18
+ *
19
+ * Returns +1 if val is positive, -1 if val is zero or negative.
20
+ */
21
+ IHD int sign_dir(const int& val) {
22
+ return -(static_cast<int>((val <= 0)) << 1) + 1;
23
+ };
24
+
25
+ /**
26
+ * Get the direction of val.
27
+ *
28
+ * Returns +1 if val is positive, -1 if val is zero or negative.
29
+ */
30
+ IHD float sign_dir(const float& val) {
31
+ return static_cast<float>(1 - (static_cast<int>((val <= 0)) << 1));
32
+ };
33
+
34
+ /**
35
+ * Integer ceil division.
36
+ */
37
+ IHD uint iDivCeil(uint a, uint b) {
38
+ return (a % b != 0) ? (a / b + 1) : (a / b);
39
+ }
40
+
41
+ IHD float3 outer_product_sum(const float3& a) {
42
+ return make_float3(
43
+ a.x * a.x + a.x * a.y + a.x * a.z,
44
+ a.x * a.y + a.y * a.y + a.y * a.z,
45
+ a.x * a.z + a.y * a.z + a.z * a.z);
46
+ }
47
+
48
+ // TODO: put intrinsics here.
49
+ IHD float3 operator+(const float3& a, const float3& b) {
50
+ return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
51
+ }
52
+
53
+ IHD void operator+=(float3& a, const float3& b) {
54
+ a.x += b.x;
55
+ a.y += b.y;
56
+ a.z += b.z;
57
+ }
58
+
59
+ IHD void operator-=(float3& a, const float3& b) {
60
+ a.x -= b.x;
61
+ a.y -= b.y;
62
+ a.z -= b.z;
63
+ }
64
+
65
+ IHD void operator/=(float3& a, const float& b) {
66
+ a.x /= b;
67
+ a.y /= b;
68
+ a.z /= b;
69
+ }
70
+
71
+ IHD void operator*=(float3& a, const float& b) {
72
+ a.x *= b;
73
+ a.y *= b;
74
+ a.z *= b;
75
+ }
76
+
77
+ IHD float3 operator/(const float3& a, const float& b) {
78
+ return make_float3(a.x / b, a.y / b, a.z / b);
79
+ }
80
+
81
+ IHD float3 operator-(const float3& a, const float3& b) {
82
+ return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
83
+ }
84
+
85
+ IHD float3 operator*(const float3& a, const float& b) {
86
+ return make_float3(a.x * b, a.y * b, a.z * b);
87
+ }
88
+
89
+ IHD float3 operator*(const float3& a, const float3& b) {
90
+ return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
91
+ }
92
+
93
+ IHD float3 operator*(const float& a, const float3& b) {
94
+ return b * a;
95
+ }
96
+
97
+ INLINE DEVICE float length(const float3& v) {
98
+ // TODO: benchmark what's faster.
99
+ return NORM3DF(v.x, v.y, v.z);
100
+ // return __fsqrt_rn(v.x * v.x + v.y * v.y + v.z * v.z);
101
+ }
102
+
103
+ /**
104
+ * Left-hand multiplication of the constructed rotation matrix with the vector.
105
+ */
106
+ IHD float3 rotate(
107
+ const float3& v,
108
+ const float3& dir_x,
109
+ const float3& dir_y,
110
+ const float3& dir_z) {
111
+ return make_float3(
112
+ dir_x.x * v.x + dir_x.y * v.y + dir_x.z * v.z,
113
+ dir_y.x * v.x + dir_y.y * v.y + dir_y.z * v.z,
114
+ dir_z.x * v.x + dir_z.y * v.y + dir_z.z * v.z);
115
+ }
116
+
117
+ INLINE DEVICE float3 normalize(const float3& v) {
118
+ return v * RNORM3DF(v.x, v.y, v.z);
119
+ }
120
+
121
+ INLINE DEVICE float dot(const float3& a, const float3& b) {
122
+ return FADD(FADD(FMUL(a.x, b.x), FMUL(a.y, b.y)), FMUL(a.z, b.z));
123
+ }
124
+
125
+ INLINE DEVICE float3 cross(const float3& a, const float3& b) {
126
+ // TODO: faster
127
+ return make_float3(
128
+ a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
129
+ }
130
+
131
+ namespace pulsar {
132
+ IHD CamGradInfo operator+(const CamGradInfo& a, const CamGradInfo& b) {
133
+ CamGradInfo res;
134
+ res.cam_pos = a.cam_pos + b.cam_pos;
135
+ res.pixel_0_0_center = a.pixel_0_0_center + b.pixel_0_0_center;
136
+ res.pixel_dir_x = a.pixel_dir_x + b.pixel_dir_x;
137
+ res.pixel_dir_y = a.pixel_dir_y + b.pixel_dir_y;
138
+ return res;
139
+ }
140
+
141
+ IHD CamGradInfo operator*(const CamGradInfo& a, const float& b) {
142
+ CamGradInfo res;
143
+ res.cam_pos = a.cam_pos * b;
144
+ res.pixel_0_0_center = a.pixel_0_0_center * b;
145
+ res.pixel_dir_x = a.pixel_dir_x * b;
146
+ res.pixel_dir_y = a.pixel_dir_y * b;
147
+ return res;
148
+ }
149
+
150
+ IHD IntWrapper operator+(const IntWrapper& a, const IntWrapper& b) {
151
+ IntWrapper res;
152
+ res.val = a.val + b.val;
153
+ return res;
154
+ }
155
+ } // namespace pulsar
156
+
157
+ #endif