warp-lang 1.7.0__py3-none-manylinux_2_28_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of warp-lang might be problematic. Click here for more details.

Files changed (429) hide show
  1. warp/__init__.py +139 -0
  2. warp/__init__.pyi +1 -0
  3. warp/autograd.py +1142 -0
  4. warp/bin/warp-clang.so +0 -0
  5. warp/bin/warp.so +0 -0
  6. warp/build.py +557 -0
  7. warp/build_dll.py +405 -0
  8. warp/builtins.py +6855 -0
  9. warp/codegen.py +3969 -0
  10. warp/config.py +158 -0
  11. warp/constants.py +57 -0
  12. warp/context.py +6812 -0
  13. warp/dlpack.py +462 -0
  14. warp/examples/__init__.py +24 -0
  15. warp/examples/assets/bear.usd +0 -0
  16. warp/examples/assets/bunny.usd +0 -0
  17. warp/examples/assets/cartpole.urdf +110 -0
  18. warp/examples/assets/crazyflie.usd +0 -0
  19. warp/examples/assets/cube.usd +0 -0
  20. warp/examples/assets/nonuniform.usd +0 -0
  21. warp/examples/assets/nv_ant.xml +92 -0
  22. warp/examples/assets/nv_humanoid.xml +183 -0
  23. warp/examples/assets/nvidia_logo.png +0 -0
  24. warp/examples/assets/pixel.jpg +0 -0
  25. warp/examples/assets/quadruped.urdf +268 -0
  26. warp/examples/assets/rocks.nvdb +0 -0
  27. warp/examples/assets/rocks.usd +0 -0
  28. warp/examples/assets/sphere.usd +0 -0
  29. warp/examples/assets/square_cloth.usd +0 -0
  30. warp/examples/benchmarks/benchmark_api.py +389 -0
  31. warp/examples/benchmarks/benchmark_cloth.py +296 -0
  32. warp/examples/benchmarks/benchmark_cloth_cupy.py +96 -0
  33. warp/examples/benchmarks/benchmark_cloth_jax.py +105 -0
  34. warp/examples/benchmarks/benchmark_cloth_numba.py +161 -0
  35. warp/examples/benchmarks/benchmark_cloth_numpy.py +85 -0
  36. warp/examples/benchmarks/benchmark_cloth_paddle.py +94 -0
  37. warp/examples/benchmarks/benchmark_cloth_pytorch.py +94 -0
  38. warp/examples/benchmarks/benchmark_cloth_taichi.py +120 -0
  39. warp/examples/benchmarks/benchmark_cloth_warp.py +153 -0
  40. warp/examples/benchmarks/benchmark_gemm.py +164 -0
  41. warp/examples/benchmarks/benchmark_interop_paddle.py +166 -0
  42. warp/examples/benchmarks/benchmark_interop_torch.py +166 -0
  43. warp/examples/benchmarks/benchmark_launches.py +301 -0
  44. warp/examples/benchmarks/benchmark_tile_load_store.py +103 -0
  45. warp/examples/browse.py +37 -0
  46. warp/examples/core/example_cupy.py +86 -0
  47. warp/examples/core/example_dem.py +241 -0
  48. warp/examples/core/example_fluid.py +299 -0
  49. warp/examples/core/example_graph_capture.py +150 -0
  50. warp/examples/core/example_marching_cubes.py +194 -0
  51. warp/examples/core/example_mesh.py +180 -0
  52. warp/examples/core/example_mesh_intersect.py +211 -0
  53. warp/examples/core/example_nvdb.py +182 -0
  54. warp/examples/core/example_raycast.py +111 -0
  55. warp/examples/core/example_raymarch.py +205 -0
  56. warp/examples/core/example_render_opengl.py +193 -0
  57. warp/examples/core/example_sample_mesh.py +300 -0
  58. warp/examples/core/example_sph.py +411 -0
  59. warp/examples/core/example_torch.py +211 -0
  60. warp/examples/core/example_wave.py +269 -0
  61. warp/examples/fem/example_adaptive_grid.py +286 -0
  62. warp/examples/fem/example_apic_fluid.py +423 -0
  63. warp/examples/fem/example_burgers.py +261 -0
  64. warp/examples/fem/example_convection_diffusion.py +178 -0
  65. warp/examples/fem/example_convection_diffusion_dg.py +204 -0
  66. warp/examples/fem/example_deformed_geometry.py +172 -0
  67. warp/examples/fem/example_diffusion.py +196 -0
  68. warp/examples/fem/example_diffusion_3d.py +225 -0
  69. warp/examples/fem/example_diffusion_mgpu.py +220 -0
  70. warp/examples/fem/example_distortion_energy.py +228 -0
  71. warp/examples/fem/example_magnetostatics.py +240 -0
  72. warp/examples/fem/example_mixed_elasticity.py +291 -0
  73. warp/examples/fem/example_navier_stokes.py +261 -0
  74. warp/examples/fem/example_nonconforming_contact.py +298 -0
  75. warp/examples/fem/example_stokes.py +213 -0
  76. warp/examples/fem/example_stokes_transfer.py +262 -0
  77. warp/examples/fem/example_streamlines.py +352 -0
  78. warp/examples/fem/utils.py +1000 -0
  79. warp/examples/interop/example_jax_callable.py +116 -0
  80. warp/examples/interop/example_jax_ffi_callback.py +132 -0
  81. warp/examples/interop/example_jax_kernel.py +205 -0
  82. warp/examples/optim/example_bounce.py +266 -0
  83. warp/examples/optim/example_cloth_throw.py +228 -0
  84. warp/examples/optim/example_diffray.py +561 -0
  85. warp/examples/optim/example_drone.py +870 -0
  86. warp/examples/optim/example_fluid_checkpoint.py +497 -0
  87. warp/examples/optim/example_inverse_kinematics.py +182 -0
  88. warp/examples/optim/example_inverse_kinematics_torch.py +191 -0
  89. warp/examples/optim/example_softbody_properties.py +400 -0
  90. warp/examples/optim/example_spring_cage.py +245 -0
  91. warp/examples/optim/example_trajectory.py +227 -0
  92. warp/examples/sim/example_cartpole.py +143 -0
  93. warp/examples/sim/example_cloth.py +225 -0
  94. warp/examples/sim/example_cloth_self_contact.py +322 -0
  95. warp/examples/sim/example_granular.py +130 -0
  96. warp/examples/sim/example_granular_collision_sdf.py +202 -0
  97. warp/examples/sim/example_jacobian_ik.py +244 -0
  98. warp/examples/sim/example_particle_chain.py +124 -0
  99. warp/examples/sim/example_quadruped.py +203 -0
  100. warp/examples/sim/example_rigid_chain.py +203 -0
  101. warp/examples/sim/example_rigid_contact.py +195 -0
  102. warp/examples/sim/example_rigid_force.py +133 -0
  103. warp/examples/sim/example_rigid_gyroscopic.py +115 -0
  104. warp/examples/sim/example_rigid_soft_contact.py +140 -0
  105. warp/examples/sim/example_soft_body.py +196 -0
  106. warp/examples/tile/example_tile_cholesky.py +87 -0
  107. warp/examples/tile/example_tile_convolution.py +66 -0
  108. warp/examples/tile/example_tile_fft.py +55 -0
  109. warp/examples/tile/example_tile_filtering.py +113 -0
  110. warp/examples/tile/example_tile_matmul.py +85 -0
  111. warp/examples/tile/example_tile_mlp.py +383 -0
  112. warp/examples/tile/example_tile_nbody.py +199 -0
  113. warp/examples/tile/example_tile_walker.py +327 -0
  114. warp/fabric.py +355 -0
  115. warp/fem/__init__.py +106 -0
  116. warp/fem/adaptivity.py +508 -0
  117. warp/fem/cache.py +572 -0
  118. warp/fem/dirichlet.py +202 -0
  119. warp/fem/domain.py +411 -0
  120. warp/fem/field/__init__.py +125 -0
  121. warp/fem/field/field.py +619 -0
  122. warp/fem/field/nodal_field.py +326 -0
  123. warp/fem/field/restriction.py +37 -0
  124. warp/fem/field/virtual.py +848 -0
  125. warp/fem/geometry/__init__.py +32 -0
  126. warp/fem/geometry/adaptive_nanogrid.py +857 -0
  127. warp/fem/geometry/closest_point.py +84 -0
  128. warp/fem/geometry/deformed_geometry.py +221 -0
  129. warp/fem/geometry/element.py +776 -0
  130. warp/fem/geometry/geometry.py +362 -0
  131. warp/fem/geometry/grid_2d.py +392 -0
  132. warp/fem/geometry/grid_3d.py +452 -0
  133. warp/fem/geometry/hexmesh.py +911 -0
  134. warp/fem/geometry/nanogrid.py +571 -0
  135. warp/fem/geometry/partition.py +389 -0
  136. warp/fem/geometry/quadmesh.py +663 -0
  137. warp/fem/geometry/tetmesh.py +855 -0
  138. warp/fem/geometry/trimesh.py +806 -0
  139. warp/fem/integrate.py +2335 -0
  140. warp/fem/linalg.py +419 -0
  141. warp/fem/operator.py +293 -0
  142. warp/fem/polynomial.py +229 -0
  143. warp/fem/quadrature/__init__.py +17 -0
  144. warp/fem/quadrature/pic_quadrature.py +299 -0
  145. warp/fem/quadrature/quadrature.py +591 -0
  146. warp/fem/space/__init__.py +228 -0
  147. warp/fem/space/basis_function_space.py +468 -0
  148. warp/fem/space/basis_space.py +667 -0
  149. warp/fem/space/dof_mapper.py +251 -0
  150. warp/fem/space/function_space.py +309 -0
  151. warp/fem/space/grid_2d_function_space.py +177 -0
  152. warp/fem/space/grid_3d_function_space.py +227 -0
  153. warp/fem/space/hexmesh_function_space.py +257 -0
  154. warp/fem/space/nanogrid_function_space.py +201 -0
  155. warp/fem/space/partition.py +367 -0
  156. warp/fem/space/quadmesh_function_space.py +223 -0
  157. warp/fem/space/restriction.py +179 -0
  158. warp/fem/space/shape/__init__.py +143 -0
  159. warp/fem/space/shape/cube_shape_function.py +1105 -0
  160. warp/fem/space/shape/shape_function.py +133 -0
  161. warp/fem/space/shape/square_shape_function.py +926 -0
  162. warp/fem/space/shape/tet_shape_function.py +834 -0
  163. warp/fem/space/shape/triangle_shape_function.py +672 -0
  164. warp/fem/space/tetmesh_function_space.py +271 -0
  165. warp/fem/space/topology.py +424 -0
  166. warp/fem/space/trimesh_function_space.py +194 -0
  167. warp/fem/types.py +99 -0
  168. warp/fem/utils.py +420 -0
  169. warp/jax.py +187 -0
  170. warp/jax_experimental/__init__.py +16 -0
  171. warp/jax_experimental/custom_call.py +351 -0
  172. warp/jax_experimental/ffi.py +698 -0
  173. warp/jax_experimental/xla_ffi.py +602 -0
  174. warp/math.py +244 -0
  175. warp/native/array.h +1145 -0
  176. warp/native/builtin.h +1800 -0
  177. warp/native/bvh.cpp +492 -0
  178. warp/native/bvh.cu +791 -0
  179. warp/native/bvh.h +554 -0
  180. warp/native/clang/clang.cpp +536 -0
  181. warp/native/coloring.cpp +613 -0
  182. warp/native/crt.cpp +51 -0
  183. warp/native/crt.h +362 -0
  184. warp/native/cuda_crt.h +1058 -0
  185. warp/native/cuda_util.cpp +646 -0
  186. warp/native/cuda_util.h +307 -0
  187. warp/native/error.cpp +77 -0
  188. warp/native/error.h +36 -0
  189. warp/native/exports.h +1878 -0
  190. warp/native/fabric.h +245 -0
  191. warp/native/hashgrid.cpp +311 -0
  192. warp/native/hashgrid.cu +87 -0
  193. warp/native/hashgrid.h +240 -0
  194. warp/native/initializer_array.h +41 -0
  195. warp/native/intersect.h +1230 -0
  196. warp/native/intersect_adj.h +375 -0
  197. warp/native/intersect_tri.h +339 -0
  198. warp/native/marching.cpp +19 -0
  199. warp/native/marching.cu +514 -0
  200. warp/native/marching.h +19 -0
  201. warp/native/mat.h +2220 -0
  202. warp/native/mathdx.cpp +87 -0
  203. warp/native/matnn.h +343 -0
  204. warp/native/mesh.cpp +266 -0
  205. warp/native/mesh.cu +404 -0
  206. warp/native/mesh.h +1980 -0
  207. warp/native/nanovdb/GridHandle.h +366 -0
  208. warp/native/nanovdb/HostBuffer.h +590 -0
  209. warp/native/nanovdb/NanoVDB.h +6624 -0
  210. warp/native/nanovdb/PNanoVDB.h +3390 -0
  211. warp/native/noise.h +859 -0
  212. warp/native/quat.h +1371 -0
  213. warp/native/rand.h +342 -0
  214. warp/native/range.h +139 -0
  215. warp/native/reduce.cpp +174 -0
  216. warp/native/reduce.cu +364 -0
  217. warp/native/runlength_encode.cpp +79 -0
  218. warp/native/runlength_encode.cu +61 -0
  219. warp/native/scan.cpp +47 -0
  220. warp/native/scan.cu +53 -0
  221. warp/native/scan.h +23 -0
  222. warp/native/solid_angle.h +466 -0
  223. warp/native/sort.cpp +251 -0
  224. warp/native/sort.cu +277 -0
  225. warp/native/sort.h +33 -0
  226. warp/native/sparse.cpp +378 -0
  227. warp/native/sparse.cu +524 -0
  228. warp/native/spatial.h +657 -0
  229. warp/native/svd.h +702 -0
  230. warp/native/temp_buffer.h +46 -0
  231. warp/native/tile.h +2584 -0
  232. warp/native/tile_reduce.h +264 -0
  233. warp/native/vec.h +1426 -0
  234. warp/native/volume.cpp +501 -0
  235. warp/native/volume.cu +67 -0
  236. warp/native/volume.h +969 -0
  237. warp/native/volume_builder.cu +477 -0
  238. warp/native/volume_builder.h +52 -0
  239. warp/native/volume_impl.h +70 -0
  240. warp/native/warp.cpp +1082 -0
  241. warp/native/warp.cu +3636 -0
  242. warp/native/warp.h +381 -0
  243. warp/optim/__init__.py +17 -0
  244. warp/optim/adam.py +163 -0
  245. warp/optim/linear.py +1137 -0
  246. warp/optim/sgd.py +112 -0
  247. warp/paddle.py +407 -0
  248. warp/render/__init__.py +18 -0
  249. warp/render/render_opengl.py +3518 -0
  250. warp/render/render_usd.py +784 -0
  251. warp/render/utils.py +160 -0
  252. warp/sim/__init__.py +65 -0
  253. warp/sim/articulation.py +793 -0
  254. warp/sim/collide.py +2395 -0
  255. warp/sim/graph_coloring.py +300 -0
  256. warp/sim/import_mjcf.py +790 -0
  257. warp/sim/import_snu.py +227 -0
  258. warp/sim/import_urdf.py +579 -0
  259. warp/sim/import_usd.py +894 -0
  260. warp/sim/inertia.py +324 -0
  261. warp/sim/integrator.py +242 -0
  262. warp/sim/integrator_euler.py +1997 -0
  263. warp/sim/integrator_featherstone.py +2101 -0
  264. warp/sim/integrator_vbd.py +2048 -0
  265. warp/sim/integrator_xpbd.py +3292 -0
  266. warp/sim/model.py +4791 -0
  267. warp/sim/particles.py +121 -0
  268. warp/sim/render.py +427 -0
  269. warp/sim/utils.py +428 -0
  270. warp/sparse.py +2057 -0
  271. warp/stubs.py +3333 -0
  272. warp/tape.py +1203 -0
  273. warp/tests/__init__.py +1 -0
  274. warp/tests/__main__.py +4 -0
  275. warp/tests/assets/curlnoise_golden.npy +0 -0
  276. warp/tests/assets/mlp_golden.npy +0 -0
  277. warp/tests/assets/pixel.npy +0 -0
  278. warp/tests/assets/pnoise_golden.npy +0 -0
  279. warp/tests/assets/spiky.usd +0 -0
  280. warp/tests/assets/test_grid.nvdb +0 -0
  281. warp/tests/assets/test_index_grid.nvdb +0 -0
  282. warp/tests/assets/test_int32_grid.nvdb +0 -0
  283. warp/tests/assets/test_vec_grid.nvdb +0 -0
  284. warp/tests/assets/torus.nvdb +0 -0
  285. warp/tests/assets/torus.usda +105 -0
  286. warp/tests/aux_test_class_kernel.py +34 -0
  287. warp/tests/aux_test_compile_consts_dummy.py +18 -0
  288. warp/tests/aux_test_conditional_unequal_types_kernels.py +29 -0
  289. warp/tests/aux_test_dependent.py +29 -0
  290. warp/tests/aux_test_grad_customs.py +29 -0
  291. warp/tests/aux_test_instancing_gc.py +26 -0
  292. warp/tests/aux_test_module_unload.py +23 -0
  293. warp/tests/aux_test_name_clash1.py +40 -0
  294. warp/tests/aux_test_name_clash2.py +40 -0
  295. warp/tests/aux_test_reference.py +9 -0
  296. warp/tests/aux_test_reference_reference.py +8 -0
  297. warp/tests/aux_test_square.py +16 -0
  298. warp/tests/aux_test_unresolved_func.py +22 -0
  299. warp/tests/aux_test_unresolved_symbol.py +22 -0
  300. warp/tests/cuda/__init__.py +0 -0
  301. warp/tests/cuda/test_async.py +676 -0
  302. warp/tests/cuda/test_ipc.py +124 -0
  303. warp/tests/cuda/test_mempool.py +233 -0
  304. warp/tests/cuda/test_multigpu.py +169 -0
  305. warp/tests/cuda/test_peer.py +139 -0
  306. warp/tests/cuda/test_pinned.py +84 -0
  307. warp/tests/cuda/test_streams.py +634 -0
  308. warp/tests/geometry/__init__.py +0 -0
  309. warp/tests/geometry/test_bvh.py +200 -0
  310. warp/tests/geometry/test_hash_grid.py +221 -0
  311. warp/tests/geometry/test_marching_cubes.py +74 -0
  312. warp/tests/geometry/test_mesh.py +316 -0
  313. warp/tests/geometry/test_mesh_query_aabb.py +399 -0
  314. warp/tests/geometry/test_mesh_query_point.py +932 -0
  315. warp/tests/geometry/test_mesh_query_ray.py +311 -0
  316. warp/tests/geometry/test_volume.py +1103 -0
  317. warp/tests/geometry/test_volume_write.py +346 -0
  318. warp/tests/interop/__init__.py +0 -0
  319. warp/tests/interop/test_dlpack.py +729 -0
  320. warp/tests/interop/test_jax.py +371 -0
  321. warp/tests/interop/test_paddle.py +800 -0
  322. warp/tests/interop/test_torch.py +1001 -0
  323. warp/tests/run_coverage_serial.py +39 -0
  324. warp/tests/sim/__init__.py +0 -0
  325. warp/tests/sim/disabled_kinematics.py +244 -0
  326. warp/tests/sim/flaky_test_sim_grad.py +290 -0
  327. warp/tests/sim/test_collision.py +604 -0
  328. warp/tests/sim/test_coloring.py +258 -0
  329. warp/tests/sim/test_model.py +224 -0
  330. warp/tests/sim/test_sim_grad_bounce_linear.py +212 -0
  331. warp/tests/sim/test_sim_kinematics.py +98 -0
  332. warp/tests/sim/test_vbd.py +597 -0
  333. warp/tests/test_adam.py +163 -0
  334. warp/tests/test_arithmetic.py +1096 -0
  335. warp/tests/test_array.py +2972 -0
  336. warp/tests/test_array_reduce.py +156 -0
  337. warp/tests/test_assert.py +250 -0
  338. warp/tests/test_atomic.py +153 -0
  339. warp/tests/test_bool.py +220 -0
  340. warp/tests/test_builtins_resolution.py +1298 -0
  341. warp/tests/test_closest_point_edge_edge.py +327 -0
  342. warp/tests/test_codegen.py +810 -0
  343. warp/tests/test_codegen_instancing.py +1495 -0
  344. warp/tests/test_compile_consts.py +215 -0
  345. warp/tests/test_conditional.py +252 -0
  346. warp/tests/test_context.py +42 -0
  347. warp/tests/test_copy.py +238 -0
  348. warp/tests/test_ctypes.py +638 -0
  349. warp/tests/test_dense.py +73 -0
  350. warp/tests/test_devices.py +97 -0
  351. warp/tests/test_examples.py +482 -0
  352. warp/tests/test_fabricarray.py +996 -0
  353. warp/tests/test_fast_math.py +74 -0
  354. warp/tests/test_fem.py +2003 -0
  355. warp/tests/test_fp16.py +136 -0
  356. warp/tests/test_func.py +454 -0
  357. warp/tests/test_future_annotations.py +98 -0
  358. warp/tests/test_generics.py +656 -0
  359. warp/tests/test_grad.py +893 -0
  360. warp/tests/test_grad_customs.py +339 -0
  361. warp/tests/test_grad_debug.py +341 -0
  362. warp/tests/test_implicit_init.py +411 -0
  363. warp/tests/test_import.py +45 -0
  364. warp/tests/test_indexedarray.py +1140 -0
  365. warp/tests/test_intersect.py +73 -0
  366. warp/tests/test_iter.py +76 -0
  367. warp/tests/test_large.py +177 -0
  368. warp/tests/test_launch.py +411 -0
  369. warp/tests/test_lerp.py +151 -0
  370. warp/tests/test_linear_solvers.py +193 -0
  371. warp/tests/test_lvalue.py +427 -0
  372. warp/tests/test_mat.py +2089 -0
  373. warp/tests/test_mat_lite.py +122 -0
  374. warp/tests/test_mat_scalar_ops.py +2913 -0
  375. warp/tests/test_math.py +178 -0
  376. warp/tests/test_mlp.py +282 -0
  377. warp/tests/test_module_hashing.py +258 -0
  378. warp/tests/test_modules_lite.py +44 -0
  379. warp/tests/test_noise.py +252 -0
  380. warp/tests/test_operators.py +299 -0
  381. warp/tests/test_options.py +129 -0
  382. warp/tests/test_overwrite.py +551 -0
  383. warp/tests/test_print.py +339 -0
  384. warp/tests/test_quat.py +2315 -0
  385. warp/tests/test_rand.py +339 -0
  386. warp/tests/test_reload.py +302 -0
  387. warp/tests/test_rounding.py +185 -0
  388. warp/tests/test_runlength_encode.py +196 -0
  389. warp/tests/test_scalar_ops.py +105 -0
  390. warp/tests/test_smoothstep.py +108 -0
  391. warp/tests/test_snippet.py +318 -0
  392. warp/tests/test_sparse.py +582 -0
  393. warp/tests/test_spatial.py +2229 -0
  394. warp/tests/test_special_values.py +361 -0
  395. warp/tests/test_static.py +592 -0
  396. warp/tests/test_struct.py +734 -0
  397. warp/tests/test_tape.py +204 -0
  398. warp/tests/test_transient_module.py +93 -0
  399. warp/tests/test_triangle_closest_point.py +145 -0
  400. warp/tests/test_types.py +562 -0
  401. warp/tests/test_utils.py +588 -0
  402. warp/tests/test_vec.py +1487 -0
  403. warp/tests/test_vec_lite.py +80 -0
  404. warp/tests/test_vec_scalar_ops.py +2327 -0
  405. warp/tests/test_verify_fp.py +100 -0
  406. warp/tests/tile/__init__.py +0 -0
  407. warp/tests/tile/test_tile.py +780 -0
  408. warp/tests/tile/test_tile_load.py +407 -0
  409. warp/tests/tile/test_tile_mathdx.py +208 -0
  410. warp/tests/tile/test_tile_mlp.py +402 -0
  411. warp/tests/tile/test_tile_reduce.py +447 -0
  412. warp/tests/tile/test_tile_shared_memory.py +247 -0
  413. warp/tests/tile/test_tile_view.py +173 -0
  414. warp/tests/unittest_serial.py +47 -0
  415. warp/tests/unittest_suites.py +427 -0
  416. warp/tests/unittest_utils.py +468 -0
  417. warp/tests/walkthrough_debug.py +93 -0
  418. warp/thirdparty/__init__.py +0 -0
  419. warp/thirdparty/appdirs.py +598 -0
  420. warp/thirdparty/dlpack.py +145 -0
  421. warp/thirdparty/unittest_parallel.py +570 -0
  422. warp/torch.py +391 -0
  423. warp/types.py +5230 -0
  424. warp/utils.py +1137 -0
  425. warp_lang-1.7.0.dist-info/METADATA +516 -0
  426. warp_lang-1.7.0.dist-info/RECORD +429 -0
  427. warp_lang-1.7.0.dist-info/WHEEL +5 -0
  428. warp_lang-1.7.0.dist-info/licenses/LICENSE.md +202 -0
  429. warp_lang-1.7.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2229 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import unittest
17
+
18
+ import numpy as np
19
+
20
+ import warp as wp
21
+ from warp.tests.unittest_utils import *
22
+
23
+ np_float_types = [np.float32, np.float64, np.float16]
24
+
25
+ kernel_cache = {}
26
+
27
+
28
+ def getkernel(func, suffix=""):
29
+ key = func.__name__ + "_" + suffix
30
+ if key not in kernel_cache:
31
+ kernel_cache[key] = wp.Kernel(func=func, key=key)
32
+ return kernel_cache[key]
33
+
34
+
35
+ def get_select_kernel(dtype):
36
+ def output_select_kernel_fn(
37
+ input: wp.array(dtype=dtype),
38
+ index: int,
39
+ out: wp.array(dtype=dtype),
40
+ ):
41
+ out[0] = input[index]
42
+
43
+ return getkernel(output_select_kernel_fn, suffix=dtype.__name__)
44
+
45
+
46
+ ############################################################
47
+
48
+
49
+ def test_spatial_vector_constructors(test, device, dtype, register_kernels=False):
50
+ rng = np.random.default_rng(123)
51
+
52
+ tol = {
53
+ np.float16: 5.0e-3,
54
+ np.float32: 1.0e-6,
55
+ np.float64: 1.0e-8,
56
+ }.get(dtype, 0)
57
+
58
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
59
+ vec3 = wp.types.vector(length=3, dtype=wptype)
60
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
61
+
62
+ def check_spatial_vector_component_constructor(
63
+ input: wp.array(dtype=wptype),
64
+ out: wp.array(dtype=wptype),
65
+ ):
66
+ result = spatial_vector(input[0], input[1], input[2], input[3], input[4], input[5])
67
+
68
+ # multiply the output by 2 so we've got something to backpropagate:
69
+ out[0] = wptype(2) * result[0]
70
+ out[1] = wptype(2) * result[1]
71
+ out[2] = wptype(2) * result[2]
72
+ out[3] = wptype(2) * result[3]
73
+ out[4] = wptype(2) * result[4]
74
+ out[5] = wptype(2) * result[5]
75
+
76
+ def check_spatial_vector_vector_constructor(
77
+ input: wp.array(dtype=wptype),
78
+ out: wp.array(dtype=wptype),
79
+ ):
80
+ result = spatial_vector(vec3(input[0], input[1], input[2]), vec3(input[3], input[4], input[5]))
81
+
82
+ # multiply the output by 2 so we've got something to backpropagate:
83
+ out[0] = wptype(2) * result[0]
84
+ out[1] = wptype(2) * result[1]
85
+ out[2] = wptype(2) * result[2]
86
+ out[3] = wptype(2) * result[3]
87
+ out[4] = wptype(2) * result[4]
88
+ out[5] = wptype(2) * result[5]
89
+
90
+ kernel = getkernel(check_spatial_vector_component_constructor, suffix=dtype.__name__)
91
+ output_select_kernel = get_select_kernel(wptype)
92
+ vec_kernel = getkernel(check_spatial_vector_vector_constructor, suffix=dtype.__name__)
93
+
94
+ if register_kernels:
95
+ return
96
+
97
+ input = wp.array(rng.standard_normal(size=6).astype(dtype), requires_grad=True, device=device)
98
+ output = wp.zeros_like(input)
99
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[output], device=device)
100
+
101
+ assert_np_equal(output.numpy(), 2 * input.numpy(), tol=tol)
102
+
103
+ for i in range(len(input)):
104
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
105
+ tape = wp.Tape()
106
+ with tape:
107
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[output], device=device)
108
+ wp.launch(output_select_kernel, dim=1, inputs=[output, i], outputs=[cmp], device=device)
109
+ tape.backward(loss=cmp)
110
+ expectedgrads = np.zeros(len(input))
111
+ expectedgrads[i] = 2
112
+ assert_np_equal(tape.gradients[input].numpy(), expectedgrads)
113
+ tape.zero()
114
+
115
+ input = wp.array(rng.standard_normal(size=6).astype(dtype), requires_grad=True, device=device)
116
+ output = wp.zeros_like(input)
117
+ wp.launch(vec_kernel, dim=1, inputs=[input], outputs=[output], device=device)
118
+
119
+ assert_np_equal(output.numpy(), 2 * input.numpy(), tol=tol)
120
+
121
+ for i in range(len(input)):
122
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
123
+ tape = wp.Tape()
124
+ with tape:
125
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[output], device=device)
126
+ wp.launch(output_select_kernel, dim=1, inputs=[output, i], outputs=[cmp], device=device)
127
+ tape.backward(loss=cmp)
128
+ expectedgrads = np.zeros(len(input))
129
+ expectedgrads[i] = 2
130
+ assert_np_equal(tape.gradients[input].numpy(), expectedgrads)
131
+ tape.zero()
132
+
133
+
134
+ def test_spatial_vector_indexing(test, device, dtype, register_kernels=False):
135
+ rng = np.random.default_rng(123)
136
+
137
+ tol = {
138
+ np.float16: 5.0e-3,
139
+ np.float32: 1.0e-6,
140
+ np.float64: 1.0e-8,
141
+ }.get(dtype, 0)
142
+
143
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
144
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
145
+
146
+ def check_spatial_vector_indexing(
147
+ input: wp.array(dtype=spatial_vector),
148
+ out: wp.array(dtype=wptype),
149
+ ):
150
+ inpt = input[0]
151
+
152
+ # multiply outputs by 2 so we've got something to backpropagate:
153
+ idx = 0
154
+ for i in range(6):
155
+ out[idx] = wptype(2) * inpt[i]
156
+ idx = idx + 1
157
+
158
+ kernel = getkernel(check_spatial_vector_indexing, suffix=dtype.__name__)
159
+ output_select_kernel = get_select_kernel(wptype)
160
+
161
+ if register_kernels:
162
+ return
163
+
164
+ input = wp.array(
165
+ rng.standard_normal(size=(1, 6)).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device
166
+ )
167
+ outcmps = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
168
+
169
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[outcmps], device=device)
170
+
171
+ assert_np_equal(outcmps.numpy(), 2 * input.numpy().ravel(), tol=tol)
172
+
173
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
174
+ for i in range(6):
175
+ tape = wp.Tape()
176
+ with tape:
177
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[outcmps], device=device)
178
+ wp.launch(output_select_kernel, dim=1, inputs=[outcmps, i], outputs=[out], device=device)
179
+ tape.backward(loss=out)
180
+ expectedresult = np.zeros(6, dtype=dtype)
181
+ expectedresult[i] = 2
182
+ assert_np_equal(tape.gradients[input].numpy()[0], expectedresult)
183
+ tape.zero()
184
+
185
+
186
+ def test_spatial_vector_scalar_multiplication(test, device, dtype, register_kernels=False):
187
+ rng = np.random.default_rng(123)
188
+
189
+ tol = {
190
+ np.float16: 5.0e-3,
191
+ np.float32: 1.0e-6,
192
+ np.float64: 1.0e-8,
193
+ }.get(dtype, 0)
194
+
195
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
196
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
197
+
198
+ def check_spatial_vector_scalar_mul(
199
+ s: wp.array(dtype=wptype),
200
+ q: wp.array(dtype=spatial_vector),
201
+ outcmps_l: wp.array(dtype=wptype),
202
+ outcmps_r: wp.array(dtype=wptype),
203
+ ):
204
+ lresult = s[0] * q[0]
205
+ rresult = q[0] * s[0]
206
+
207
+ # multiply outputs by 2 so we've got something to backpropagate:
208
+ for i in range(6):
209
+ outcmps_l[i] = wptype(2) * lresult[i]
210
+ outcmps_r[i] = wptype(2) * rresult[i]
211
+
212
+ kernel = getkernel(check_spatial_vector_scalar_mul, suffix=dtype.__name__)
213
+ output_select_kernel = get_select_kernel(wptype)
214
+
215
+ if register_kernels:
216
+ return
217
+
218
+ s = wp.array(rng.standard_normal(size=1).astype(dtype), requires_grad=True, device=device)
219
+ q = wp.array(
220
+ rng.standard_normal(size=(1, 6)).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device
221
+ )
222
+
223
+ outcmps_l = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
224
+ outcmps_r = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
225
+
226
+ wp.launch(
227
+ kernel,
228
+ dim=1,
229
+ inputs=[s, q],
230
+ outputs=[
231
+ outcmps_l,
232
+ outcmps_r,
233
+ ],
234
+ device=device,
235
+ )
236
+
237
+ assert_np_equal(outcmps_l.numpy(), 2 * s.numpy()[0] * q.numpy(), tol=tol)
238
+ assert_np_equal(outcmps_r.numpy(), 2 * s.numpy()[0] * q.numpy(), tol=tol)
239
+
240
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
241
+ for i in range(6):
242
+ # test left/right mul gradients:
243
+ for wrt in [outcmps_l, outcmps_r]:
244
+ tape = wp.Tape()
245
+ with tape:
246
+ wp.launch(kernel, dim=1, inputs=[s, q], outputs=[outcmps_l, outcmps_r], device=device)
247
+ wp.launch(output_select_kernel, dim=1, inputs=[wrt, i], outputs=[out], device=device)
248
+ tape.backward(loss=out)
249
+ expectedresult = np.zeros(6, dtype=dtype)
250
+ expectedresult[i] = 2 * s.numpy()[0]
251
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
252
+ assert_np_equal(tape.gradients[s].numpy()[0], 2 * q.numpy()[0, i], tol=tol)
253
+ tape.zero()
254
+
255
+
256
+ def test_spatial_vector_add_sub(test, device, dtype, register_kernels=False):
257
+ rng = np.random.default_rng(123)
258
+
259
+ tol = {
260
+ np.float16: 5.0e-3,
261
+ np.float32: 1.0e-6,
262
+ np.float64: 1.0e-8,
263
+ }.get(dtype, 0)
264
+
265
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
266
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
267
+
268
+ def check_spatial_vector_add_sub(
269
+ q: wp.array(dtype=spatial_vector),
270
+ v: wp.array(dtype=spatial_vector),
271
+ outputs_add: wp.array(dtype=wptype),
272
+ outputs_sub: wp.array(dtype=wptype),
273
+ ):
274
+ addresult = q[0] + v[0]
275
+ subresult = q[0] - v[0]
276
+ for i in range(6):
277
+ outputs_add[i] = wptype(2) * addresult[i]
278
+ outputs_sub[i] = wptype(2) * subresult[i]
279
+
280
+ kernel = getkernel(check_spatial_vector_add_sub, suffix=dtype.__name__)
281
+ output_select_kernel = get_select_kernel(wptype)
282
+ if register_kernels:
283
+ return
284
+
285
+ q = wp.array(rng.standard_normal(size=6).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device)
286
+ v = wp.array(rng.standard_normal(size=6).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device)
287
+
288
+ outputs_add = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
289
+ outputs_sub = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
290
+
291
+ wp.launch(
292
+ kernel,
293
+ dim=1,
294
+ inputs=[
295
+ q,
296
+ v,
297
+ ],
298
+ outputs=[outputs_add, outputs_sub],
299
+ device=device,
300
+ )
301
+
302
+ assert_np_equal(outputs_add.numpy(), 2 * (q.numpy() + v.numpy()), tol=tol)
303
+ assert_np_equal(outputs_sub.numpy(), 2 * (q.numpy() - v.numpy()), tol=tol)
304
+
305
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
306
+ for i in range(6):
307
+ # test add gradients:
308
+ tape = wp.Tape()
309
+ with tape:
310
+ wp.launch(kernel, dim=1, inputs=[q, v], outputs=[outputs_add, outputs_sub], device=device)
311
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_add, i], outputs=[out], device=device)
312
+ tape.backward(loss=out)
313
+ expectedresult = np.zeros(6, dtype=dtype)
314
+ expectedresult[i] = 2
315
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
316
+ assert_np_equal(tape.gradients[v].numpy()[0], expectedresult, tol=tol)
317
+ tape.zero()
318
+
319
+ # test subtraction gradients:
320
+ tape = wp.Tape()
321
+ with tape:
322
+ wp.launch(kernel, dim=1, inputs=[q, v], outputs=[outputs_add, outputs_sub], device=device)
323
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_sub, i], outputs=[out], device=device)
324
+ tape.backward(loss=out)
325
+ expectedresult = np.zeros(6, dtype=dtype)
326
+ expectedresult[i] = 2
327
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
328
+ assert_np_equal(tape.gradients[v].numpy()[0], -expectedresult, tol=tol)
329
+ tape.zero()
330
+
331
+
332
+ def test_spatial_dot(test, device, dtype, register_kernels=False):
333
+ rng = np.random.default_rng(123)
334
+
335
+ tol = {
336
+ np.float16: 1.0e-2,
337
+ np.float32: 1.0e-6,
338
+ np.float64: 1.0e-8,
339
+ }.get(dtype, 0)
340
+
341
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
342
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
343
+
344
+ def check_spatial_dot(
345
+ s: wp.array(dtype=spatial_vector),
346
+ v: wp.array(dtype=spatial_vector),
347
+ dot: wp.array(dtype=wptype),
348
+ ):
349
+ dot[0] = wptype(2) * wp.spatial_dot(v[0], s[0])
350
+
351
+ kernel = getkernel(check_spatial_dot, suffix=dtype.__name__)
352
+ if register_kernels:
353
+ return
354
+
355
+ s = wp.array(rng.standard_normal(size=6).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device)
356
+ v = wp.array(rng.standard_normal(size=6).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device)
357
+ dot = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
358
+
359
+ tape = wp.Tape()
360
+ with tape:
361
+ wp.launch(
362
+ kernel,
363
+ dim=1,
364
+ inputs=[
365
+ s,
366
+ v,
367
+ ],
368
+ outputs=[dot],
369
+ device=device,
370
+ )
371
+
372
+ assert_np_equal(dot.numpy()[0], 2.0 * (v.numpy() * s.numpy()).sum(), tol=tol)
373
+
374
+ tape.backward(loss=dot)
375
+ sgrads = tape.gradients[s].numpy()[0]
376
+ expected_grads = 2.0 * v.numpy()[0]
377
+ assert_np_equal(sgrads, expected_grads, tol=10 * tol)
378
+
379
+ vgrads = tape.gradients[v].numpy()[0]
380
+ expected_grads = 2.0 * s.numpy()[0]
381
+ assert_np_equal(vgrads, expected_grads, tol=tol)
382
+
383
+
384
+ def test_spatial_cross(test, device, dtype, register_kernels=False):
385
+ rng = np.random.default_rng(123)
386
+
387
+ tol = {
388
+ np.float16: 5.0e-3,
389
+ np.float32: 1.0e-6,
390
+ np.float64: 1.0e-8,
391
+ }.get(dtype, 0)
392
+
393
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
394
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
395
+
396
+ def check_spatial_cross(
397
+ s: wp.array(dtype=spatial_vector),
398
+ v: wp.array(dtype=spatial_vector),
399
+ outputs: wp.array(dtype=wptype),
400
+ outputs_dual: wp.array(dtype=wptype),
401
+ outputs_wcrossw: wp.array(dtype=wptype),
402
+ outputs_vcrossw: wp.array(dtype=wptype),
403
+ outputs_wcrossv: wp.array(dtype=wptype),
404
+ outputs_vcrossv: wp.array(dtype=wptype),
405
+ ):
406
+ c = wp.spatial_cross(s[0], v[0])
407
+ d = wp.spatial_cross_dual(s[0], v[0])
408
+
409
+ # multiply outputs by 2 so we've got something to backpropagate:
410
+ for i in range(6):
411
+ outputs[i] = wptype(2) * c[i]
412
+ outputs_dual[i] = wptype(2) * d[i]
413
+
414
+ sw = wp.spatial_top(s[0])
415
+ sv = wp.spatial_bottom(s[0])
416
+ vw = wp.spatial_top(v[0])
417
+ vv = wp.spatial_bottom(v[0])
418
+
419
+ wcrossw = wp.cross(sw, vw)
420
+ vcrossw = wp.cross(sv, vw)
421
+ wcrossv = wp.cross(sw, vv)
422
+ vcrossv = wp.cross(sv, vv)
423
+
424
+ for i in range(3):
425
+ outputs_wcrossw[i] = wcrossw[i]
426
+ outputs_vcrossw[i] = vcrossw[i]
427
+ outputs_wcrossv[i] = wcrossv[i]
428
+ outputs_vcrossv[i] = vcrossv[i]
429
+
430
+ kernel = getkernel(check_spatial_cross, suffix=dtype.__name__)
431
+ output_select_kernel = get_select_kernel(wptype)
432
+
433
+ if register_kernels:
434
+ return
435
+
436
+ s = wp.array(rng.standard_normal(size=6).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device)
437
+ v = wp.array(rng.standard_normal(size=6).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device)
438
+ outputs = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
439
+ outputs_dual = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
440
+ outputs_wcrossw = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
441
+ outputs_vcrossw = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
442
+ outputs_wcrossv = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
443
+ outputs_vcrossv = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
444
+
445
+ wp.launch(
446
+ kernel,
447
+ dim=1,
448
+ inputs=[
449
+ s,
450
+ v,
451
+ ],
452
+ outputs=[outputs, outputs_dual, outputs_wcrossw, outputs_vcrossw, outputs_wcrossv, outputs_vcrossv],
453
+ device=device,
454
+ )
455
+
456
+ sw = s.numpy()[0, :3]
457
+ sv = s.numpy()[0, 3:]
458
+ vw = v.numpy()[0, :3]
459
+ vv = v.numpy()[0, 3:]
460
+
461
+ wcrossw = np.cross(sw, vw)
462
+ vcrossw = np.cross(sv, vw)
463
+ wcrossv = np.cross(sw, vv)
464
+ vcrossv = np.cross(sv, vv)
465
+
466
+ assert_np_equal(outputs.numpy()[:3], 2 * wcrossw, tol=tol)
467
+ assert_np_equal(outputs.numpy()[3:], 2 * (vcrossw + wcrossv), tol=tol)
468
+
469
+ assert_np_equal(outputs_dual.numpy()[:3], 2 * (wcrossw + vcrossv), tol=tol)
470
+ assert_np_equal(outputs_dual.numpy()[3:], 2 * wcrossv, tol=tol)
471
+
472
+ for i in range(3):
473
+ cmp_w = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
474
+ cmp_v = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
475
+ cmp_w_dual = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
476
+ cmp_v_dual = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
477
+ cmp_wcrossw = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
478
+ cmp_vcrossw = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
479
+ cmp_wcrossv = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
480
+ cmp_vcrossv = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
481
+ tape = wp.Tape()
482
+ with tape:
483
+ wp.launch(
484
+ kernel,
485
+ dim=1,
486
+ inputs=[
487
+ s,
488
+ v,
489
+ ],
490
+ outputs=[outputs, outputs_dual, outputs_wcrossw, outputs_vcrossw, outputs_wcrossv, outputs_vcrossv],
491
+ device=device,
492
+ )
493
+
494
+ # ith w and v vector components of spatial_cross:
495
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs, i], outputs=[cmp_w], device=device)
496
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs, i + 3], outputs=[cmp_v], device=device)
497
+
498
+ # ith w and v vector components of spatial_cross_dual:
499
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_dual, i], outputs=[cmp_w_dual], device=device)
500
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_dual, i + 3], outputs=[cmp_v_dual], device=device)
501
+
502
+ # ith vector components of some cross products:
503
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_wcrossw, i], outputs=[cmp_wcrossw], device=device)
504
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_vcrossw, i], outputs=[cmp_vcrossw], device=device)
505
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_wcrossv, i], outputs=[cmp_wcrossv], device=device)
506
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_vcrossv, i], outputs=[cmp_vcrossv], device=device)
507
+
508
+ def getgrads(cmp, tape=tape):
509
+ tape.backward(loss=cmp)
510
+ sgrads = 1.0 * tape.gradients[s].numpy()
511
+ vgrads = 1.0 * tape.gradients[v].numpy()
512
+ tape.zero()
513
+ return sgrads, vgrads
514
+
515
+ dcmp_w_ds, dcmp_w_dv = getgrads(cmp_w)
516
+ dcmp_v_ds, dcmp_v_dv = getgrads(cmp_v)
517
+ dcmp_w_dual_ds, dcmp_w_dual_dv = getgrads(cmp_w_dual)
518
+ dcmp_v_dual_ds, dcmp_v_dual_dv = getgrads(cmp_v_dual)
519
+
520
+ dcmp_wcrossw_ds, dcmp_wcrossw_dv = getgrads(cmp_wcrossw)
521
+ dcmp_vcrossw_ds, dcmp_vcrossw_dv = getgrads(cmp_vcrossw)
522
+ dcmp_wcrossv_ds, dcmp_wcrossv_dv = getgrads(cmp_wcrossv)
523
+ dcmp_vcrossv_ds, dcmp_vcrossv_dv = getgrads(cmp_vcrossv)
524
+
525
+ assert_np_equal(dcmp_w_ds, 2 * dcmp_wcrossw_ds, tol=tol)
526
+ assert_np_equal(dcmp_w_dv, 2 * dcmp_wcrossw_dv, tol=tol)
527
+
528
+ assert_np_equal(dcmp_v_ds, 2 * (dcmp_vcrossw_ds + dcmp_wcrossv_ds), tol=tol)
529
+ assert_np_equal(dcmp_v_dv, 2 * (dcmp_vcrossw_dv + dcmp_wcrossv_dv), tol=tol)
530
+
531
+ assert_np_equal(dcmp_w_dual_ds, 2 * (dcmp_wcrossw_ds + dcmp_vcrossv_ds), tol=tol)
532
+ assert_np_equal(dcmp_w_dual_dv, 2 * (dcmp_wcrossw_dv + dcmp_vcrossv_dv), tol=tol)
533
+
534
+ assert_np_equal(dcmp_v_dual_ds, 2 * dcmp_wcrossv_ds, tol=tol)
535
+ assert_np_equal(dcmp_v_dual_dv, 2 * dcmp_wcrossv_dv, tol=tol)
536
+
537
+
538
+ def test_spatial_top_bottom(test, device, dtype, register_kernels=False):
539
+ rng = np.random.default_rng(123)
540
+
541
+ tol = {
542
+ np.float16: 1.0e-2,
543
+ np.float32: 1.0e-6,
544
+ np.float64: 1.0e-8,
545
+ }.get(dtype, 0)
546
+
547
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
548
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
549
+
550
+ def check_spatial_top_bottom(
551
+ s: wp.array(dtype=spatial_vector),
552
+ outputs: wp.array(dtype=wptype),
553
+ ):
554
+ top = wp.spatial_top(s[0])
555
+ bottom = wp.spatial_bottom(s[0])
556
+
557
+ outputs[0] = wptype(2) * top[0]
558
+ outputs[1] = wptype(2) * top[1]
559
+ outputs[2] = wptype(2) * top[2]
560
+
561
+ outputs[3] = wptype(2) * bottom[0]
562
+ outputs[4] = wptype(2) * bottom[1]
563
+ outputs[5] = wptype(2) * bottom[2]
564
+
565
+ kernel = getkernel(check_spatial_top_bottom, suffix=dtype.__name__)
566
+ output_select_kernel = get_select_kernel(wptype)
567
+
568
+ if register_kernels:
569
+ return
570
+
571
+ s = wp.array(rng.standard_normal(size=6).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device)
572
+ outputs = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
573
+
574
+ wp.launch(
575
+ kernel,
576
+ dim=1,
577
+ inputs=[
578
+ s,
579
+ ],
580
+ outputs=[outputs],
581
+ device=device,
582
+ )
583
+
584
+ assert_np_equal(outputs.numpy(), 2.0 * s.numpy(), tol=tol)
585
+
586
+ for i in range(6):
587
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
588
+ tape = wp.Tape()
589
+ with tape:
590
+ wp.launch(
591
+ kernel,
592
+ dim=1,
593
+ inputs=[
594
+ s,
595
+ ],
596
+ outputs=[outputs],
597
+ device=device,
598
+ )
599
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs, i], outputs=[cmp], device=device)
600
+ tape.backward(loss=cmp)
601
+ expectedgrads = np.zeros(6)
602
+ expectedgrads[i] = 2
603
+ assert_np_equal(tape.gradients[s].numpy(), expectedgrads.reshape((1, 6)))
604
+ tape.zero()
605
+
606
+
607
+ def test_transform_constructors(test, device, dtype, register_kernels=False):
608
+ rng = np.random.default_rng(123)
609
+
610
+ tol = {
611
+ np.float16: 5.0e-3,
612
+ np.float32: 1.0e-6,
613
+ np.float64: 1.0e-8,
614
+ }.get(dtype, 0)
615
+
616
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
617
+ vec3 = wp.types.vector(length=3, dtype=wptype)
618
+ transform = wp.types.transformation(dtype=wptype)
619
+ quat = wp.types.quaternion(dtype=wptype)
620
+
621
+ def check_transform_constructor(
622
+ input: wp.array(dtype=wptype),
623
+ out: wp.array(dtype=wptype),
624
+ ):
625
+ result = transform(vec3(input[0], input[1], input[2]), quat(input[3], input[4], input[5], input[6]))
626
+
627
+ # multiply the output by 2 so we've got something to backpropagate:
628
+ out[0] = wptype(2) * result[0]
629
+ out[1] = wptype(2) * result[1]
630
+ out[2] = wptype(2) * result[2]
631
+ out[3] = wptype(2) * result[3]
632
+ out[4] = wptype(2) * result[4]
633
+ out[5] = wptype(2) * result[5]
634
+ out[6] = wptype(2) * result[6]
635
+
636
+ kernel = getkernel(check_transform_constructor, suffix=dtype.__name__)
637
+ output_select_kernel = get_select_kernel(wptype)
638
+
639
+ if register_kernels:
640
+ return
641
+
642
+ p = rng.standard_normal(size=3).astype(dtype)
643
+ q = rng.standard_normal(size=4).astype(dtype)
644
+ q /= np.linalg.norm(q)
645
+
646
+ input = wp.array(np.concatenate((p, q)), requires_grad=True, device=device)
647
+ output = wp.zeros_like(input)
648
+
649
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[output], device=device)
650
+
651
+ assert_np_equal(output.numpy(), 2 * input.numpy(), tol=tol)
652
+
653
+ for i in range(len(input)):
654
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
655
+ tape = wp.Tape()
656
+ with tape:
657
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[output], device=device)
658
+ wp.launch(output_select_kernel, dim=1, inputs=[output, i], outputs=[cmp], device=device)
659
+ tape.backward(loss=cmp)
660
+ expectedgrads = np.zeros(len(input))
661
+ expectedgrads[i] = 2
662
+ assert_np_equal(tape.gradients[input].numpy(), expectedgrads)
663
+ tape.zero()
664
+
665
+
666
+ def test_transform_indexing(test, device, dtype, register_kernels=False):
667
+ rng = np.random.default_rng(123)
668
+
669
+ tol = {
670
+ np.float16: 5.0e-3,
671
+ np.float32: 1.0e-6,
672
+ np.float64: 1.0e-8,
673
+ }.get(dtype, 0)
674
+
675
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
676
+ transform = wp.types.transformation(dtype=wptype)
677
+
678
+ def check_transform_indexing(
679
+ input: wp.array(dtype=transform),
680
+ out: wp.array(dtype=wptype),
681
+ ):
682
+ inpt = input[0]
683
+
684
+ # multiply outputs by 2 so we've got something to backpropagate:
685
+ idx = 0
686
+ for i in range(7):
687
+ out[idx] = wptype(2) * inpt[i]
688
+ idx = idx + 1
689
+
690
+ kernel = getkernel(check_transform_indexing, suffix=dtype.__name__)
691
+ output_select_kernel = get_select_kernel(wptype)
692
+
693
+ if register_kernels:
694
+ return
695
+
696
+ input = wp.array(rng.standard_normal(size=(1, 7)).astype(dtype), dtype=transform, requires_grad=True, device=device)
697
+ outcmps = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
698
+
699
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[outcmps], device=device)
700
+
701
+ assert_np_equal(outcmps.numpy(), 2 * input.numpy().ravel(), tol=tol)
702
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
703
+ for i in range(7):
704
+ tape = wp.Tape()
705
+ with tape:
706
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[outcmps], device=device)
707
+ wp.launch(output_select_kernel, dim=1, inputs=[outcmps, i], outputs=[out], device=device)
708
+ tape.backward(loss=out)
709
+ expectedresult = np.zeros(7, dtype=dtype)
710
+ expectedresult[i] = 2
711
+ assert_np_equal(tape.gradients[input].numpy()[0], expectedresult)
712
+ tape.zero()
713
+
714
+
715
+ def test_transform_scalar_multiplication(test, device, dtype, register_kernels=False):
716
+ rng = np.random.default_rng(123)
717
+
718
+ tol = {
719
+ np.float16: 5.0e-3,
720
+ np.float32: 1.0e-6,
721
+ np.float64: 1.0e-8,
722
+ }.get(dtype, 0)
723
+
724
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
725
+ transform = wp.types.transformation(dtype=wptype)
726
+
727
+ def check_transform_scalar_mul(
728
+ s: wp.array(dtype=wptype),
729
+ q: wp.array(dtype=transform),
730
+ outcmps_l: wp.array(dtype=wptype),
731
+ outcmps_r: wp.array(dtype=wptype),
732
+ ):
733
+ lresult = s[0] * q[0]
734
+ rresult = q[0] * s[0]
735
+
736
+ # multiply outputs by 2 so we've got something to backpropagate:
737
+ for i in range(7):
738
+ outcmps_l[i] = wptype(2) * lresult[i]
739
+ outcmps_r[i] = wptype(2) * rresult[i]
740
+
741
+ kernel = getkernel(check_transform_scalar_mul, suffix=dtype.__name__)
742
+ output_select_kernel = get_select_kernel(wptype)
743
+
744
+ if register_kernels:
745
+ return
746
+
747
+ s = wp.array(rng.standard_normal(size=1).astype(dtype), requires_grad=True, device=device)
748
+ q = wp.array(rng.standard_normal(size=(1, 7)).astype(dtype), dtype=transform, requires_grad=True, device=device)
749
+
750
+ outcmps_l = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
751
+ outcmps_r = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
752
+
753
+ wp.launch(
754
+ kernel,
755
+ dim=1,
756
+ inputs=[s, q],
757
+ outputs=[
758
+ outcmps_l,
759
+ outcmps_r,
760
+ ],
761
+ device=device,
762
+ )
763
+
764
+ assert_np_equal(outcmps_l.numpy(), 2 * s.numpy()[0] * q.numpy(), tol=tol)
765
+ assert_np_equal(outcmps_r.numpy(), 2 * s.numpy()[0] * q.numpy(), tol=tol)
766
+
767
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
768
+ for i in range(7):
769
+ # test left/right mul gradients:
770
+ for wrt in [outcmps_l, outcmps_r]:
771
+ tape = wp.Tape()
772
+ with tape:
773
+ wp.launch(kernel, dim=1, inputs=[s, q], outputs=[outcmps_l, outcmps_r], device=device)
774
+ wp.launch(output_select_kernel, dim=1, inputs=[wrt, i], outputs=[out], device=device)
775
+ tape.backward(loss=out)
776
+ expectedresult = np.zeros(7, dtype=dtype)
777
+ expectedresult[i] = 2 * s.numpy()[0]
778
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
779
+ assert_np_equal(tape.gradients[s].numpy()[0], 2 * q.numpy()[0, i], tol=tol)
780
+ tape.zero()
781
+
782
+
783
+ def test_transform_add_sub(test, device, dtype, register_kernels=False):
784
+ rng = np.random.default_rng(123)
785
+
786
+ tol = {
787
+ np.float16: 5.0e-3,
788
+ np.float32: 1.0e-6,
789
+ np.float64: 1.0e-8,
790
+ }.get(dtype, 0)
791
+
792
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
793
+ transform = wp.types.transformation(dtype=wptype)
794
+
795
+ def check_transform_add_sub(
796
+ q: wp.array(dtype=transform),
797
+ v: wp.array(dtype=transform),
798
+ outputs_add: wp.array(dtype=wptype),
799
+ outputs_sub: wp.array(dtype=wptype),
800
+ ):
801
+ addresult = q[0] + v[0]
802
+ subresult = q[0] - v[0]
803
+ for i in range(7):
804
+ outputs_add[i] = wptype(2) * addresult[i]
805
+ outputs_sub[i] = wptype(2) * subresult[i]
806
+
807
+ kernel = getkernel(check_transform_add_sub, suffix=dtype.__name__)
808
+ output_select_kernel = get_select_kernel(wptype)
809
+
810
+ if register_kernels:
811
+ return
812
+
813
+ q = wp.array(rng.standard_normal(size=7).astype(dtype), dtype=transform, requires_grad=True, device=device)
814
+ v = wp.array(rng.standard_normal(size=7).astype(dtype), dtype=transform, requires_grad=True, device=device)
815
+
816
+ outputs_add = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
817
+ outputs_sub = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
818
+
819
+ wp.launch(
820
+ kernel,
821
+ dim=1,
822
+ inputs=[
823
+ q,
824
+ v,
825
+ ],
826
+ outputs=[outputs_add, outputs_sub],
827
+ device=device,
828
+ )
829
+
830
+ assert_np_equal(outputs_add.numpy(), 2 * (q.numpy() + v.numpy()), tol=tol)
831
+ assert_np_equal(outputs_sub.numpy(), 2 * (q.numpy() - v.numpy()), tol=tol)
832
+
833
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
834
+ for i in range(7):
835
+ # test add gradients:
836
+ tape = wp.Tape()
837
+ with tape:
838
+ wp.launch(kernel, dim=1, inputs=[q, v], outputs=[outputs_add, outputs_sub], device=device)
839
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_add, i], outputs=[out], device=device)
840
+ tape.backward(loss=out)
841
+ expectedresult = np.zeros(7, dtype=dtype)
842
+ expectedresult[i] = 2
843
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
844
+ assert_np_equal(tape.gradients[v].numpy()[0], expectedresult, tol=tol)
845
+ tape.zero()
846
+
847
+ # test subtraction gradients:
848
+ tape = wp.Tape()
849
+ with tape:
850
+ wp.launch(kernel, dim=1, inputs=[q, v], outputs=[outputs_add, outputs_sub], device=device)
851
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_sub, i], outputs=[out], device=device)
852
+ tape.backward(loss=out)
853
+ expectedresult = np.zeros(7, dtype=dtype)
854
+ expectedresult[i] = 2
855
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
856
+ assert_np_equal(tape.gradients[v].numpy()[0], -expectedresult, tol=tol)
857
+ tape.zero()
858
+
859
+
860
+ def test_transform_get_trans_rot(test, device, dtype, register_kernels=False):
861
+ rng = np.random.default_rng(123)
862
+
863
+ tol = {
864
+ np.float16: 1.0e-2,
865
+ np.float32: 1.0e-6,
866
+ np.float64: 1.0e-8,
867
+ }.get(dtype, 0)
868
+
869
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
870
+ transform = wp.types.transformation(dtype=wptype)
871
+
872
+ def check_transform_get_trans_rot(
873
+ s: wp.array(dtype=transform),
874
+ outputs: wp.array(dtype=wptype),
875
+ ):
876
+ trans = wp.transform_get_translation(s[0])
877
+ q = wp.transform_get_rotation(s[0])
878
+
879
+ outputs[0] = wptype(2) * trans[0]
880
+ outputs[1] = wptype(2) * trans[1]
881
+ outputs[2] = wptype(2) * trans[2]
882
+
883
+ outputs[3] = wptype(2) * q[0]
884
+ outputs[4] = wptype(2) * q[1]
885
+ outputs[5] = wptype(2) * q[2]
886
+ outputs[6] = wptype(2) * q[3]
887
+
888
+ kernel = getkernel(check_transform_get_trans_rot, suffix=dtype.__name__)
889
+ output_select_kernel = get_select_kernel(wptype)
890
+
891
+ if register_kernels:
892
+ return
893
+
894
+ s = wp.array(rng.standard_normal(size=7).astype(dtype), dtype=transform, requires_grad=True, device=device)
895
+ outputs = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
896
+
897
+ wp.launch(
898
+ kernel,
899
+ dim=1,
900
+ inputs=[
901
+ s,
902
+ ],
903
+ outputs=[outputs],
904
+ device=device,
905
+ )
906
+
907
+ assert_np_equal(outputs.numpy(), 2.0 * s.numpy(), tol=tol)
908
+
909
+ for i in range(7):
910
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
911
+ tape = wp.Tape()
912
+ with tape:
913
+ wp.launch(
914
+ kernel,
915
+ dim=1,
916
+ inputs=[
917
+ s,
918
+ ],
919
+ outputs=[outputs],
920
+ device=device,
921
+ )
922
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs, i], outputs=[cmp], device=device)
923
+ tape.backward(loss=cmp)
924
+ expectedgrads = np.zeros(7)
925
+ expectedgrads[i] = 2
926
+ assert_np_equal(tape.gradients[s].numpy(), expectedgrads.reshape((1, 7)))
927
+ tape.zero()
928
+
929
+
930
+ def test_transform_multiply(test, device, dtype, register_kernels=False):
931
+ rng = np.random.default_rng(123)
932
+
933
+ tol = {
934
+ np.float16: 1.0e-2,
935
+ np.float32: 1.0e-6,
936
+ np.float64: 1.0e-8,
937
+ }.get(dtype, 0)
938
+
939
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
940
+ transform = wp.types.transformation(dtype=wptype)
941
+
942
+ def check_transform_multiply(
943
+ a: wp.array(dtype=transform),
944
+ b: wp.array(dtype=transform),
945
+ outputs: wp.array(dtype=wptype),
946
+ outputs_fn: wp.array(dtype=wptype),
947
+ outputs_manual: wp.array(dtype=wptype),
948
+ ):
949
+ result = a[0] * b[0]
950
+ result_fn = wp.transform_multiply(a[0], b[0])
951
+
952
+ # let's just work out the transform multiplication manually
953
+ # and compare value/gradients with that:
954
+ atrans = wp.transform_get_translation(a[0])
955
+ arot = wp.transform_get_rotation(a[0])
956
+
957
+ btrans = wp.transform_get_translation(b[0])
958
+ brot = wp.transform_get_rotation(b[0])
959
+
960
+ trans = wp.quat_rotate(arot, btrans) + atrans
961
+ rot = arot * brot
962
+ result_manual = transform(trans, rot)
963
+
964
+ for i in range(7):
965
+ outputs[i] = wptype(2) * result[i]
966
+ outputs_fn[i] = wptype(2) * result_fn[i]
967
+ outputs_manual[i] = wptype(2) * result_manual[i]
968
+
969
+ kernel = getkernel(check_transform_multiply, suffix=dtype.__name__)
970
+ output_select_kernel = get_select_kernel(wptype)
971
+
972
+ if register_kernels:
973
+ return
974
+
975
+ q = rng.standard_normal(size=7)
976
+ s = rng.standard_normal(size=7)
977
+ q[3:] /= np.linalg.norm(q[3:])
978
+ s[3:] /= np.linalg.norm(s[3:])
979
+
980
+ q = wp.array(q.astype(dtype), dtype=transform, requires_grad=True, device=device)
981
+ s = wp.array(s.astype(dtype), dtype=transform, requires_grad=True, device=device)
982
+ outputs = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
983
+ outputs_fn = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
984
+ outputs_manual = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
985
+
986
+ wp.launch(
987
+ kernel,
988
+ dim=1,
989
+ inputs=[
990
+ q,
991
+ s,
992
+ ],
993
+ outputs=[outputs, outputs_fn, outputs_manual],
994
+ device=device,
995
+ )
996
+
997
+ assert_np_equal(outputs.numpy(), outputs_fn.numpy(), tol=tol)
998
+ assert_np_equal(outputs.numpy(), outputs_manual.numpy(), tol=tol)
999
+
1000
+ for i in range(7):
1001
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1002
+ cmp_fn = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1003
+ cmp_manual = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1004
+ tape = wp.Tape()
1005
+ with tape:
1006
+ wp.launch(
1007
+ kernel,
1008
+ dim=1,
1009
+ inputs=[
1010
+ q,
1011
+ s,
1012
+ ],
1013
+ outputs=[outputs, outputs_fn, outputs_manual],
1014
+ device=device,
1015
+ )
1016
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs, i], outputs=[cmp], device=device)
1017
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_fn, i], outputs=[cmp_fn], device=device)
1018
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_manual, i], outputs=[cmp_manual], device=device)
1019
+ tape.backward(loss=cmp)
1020
+ qgrads = 1.0 * tape.gradients[q].numpy()
1021
+ sgrads = 1.0 * tape.gradients[s].numpy()
1022
+ tape.zero()
1023
+ tape.backward(loss=cmp_fn)
1024
+ qgrads_fn = 1.0 * tape.gradients[q].numpy()
1025
+ sgrads_fn = 1.0 * tape.gradients[s].numpy()
1026
+ tape.zero()
1027
+ tape.backward(loss=cmp_manual)
1028
+ qgrads_manual = 1.0 * tape.gradients[q].numpy()
1029
+ sgrads_manual = 1.0 * tape.gradients[s].numpy()
1030
+ tape.zero()
1031
+
1032
+ assert_np_equal(qgrads, qgrads_fn, tol=tol)
1033
+ assert_np_equal(sgrads, sgrads_fn, tol=tol)
1034
+
1035
+ assert_np_equal(qgrads, qgrads_manual, tol=tol)
1036
+ assert_np_equal(sgrads, sgrads_manual, tol=tol)
1037
+
1038
+
1039
+ def test_transform_inverse(test, device, dtype, register_kernels=False):
1040
+ rng = np.random.default_rng(123)
1041
+
1042
+ tol = {
1043
+ np.float16: 1.0e-2,
1044
+ np.float32: 1.0e-6,
1045
+ np.float64: 1.0e-8,
1046
+ }.get(dtype, 0)
1047
+
1048
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1049
+ transform = wp.types.transformation(dtype=wptype)
1050
+
1051
+ def check_transform_inverse(
1052
+ a: wp.array(dtype=transform),
1053
+ outputs: wp.array(dtype=wptype),
1054
+ outputs_shouldbeidentity: wp.array(dtype=wptype),
1055
+ outputs_manual: wp.array(dtype=wptype),
1056
+ ):
1057
+ result = wp.transform_inverse(a[0])
1058
+ idt = result * a[0]
1059
+
1060
+ # let's just work out the transform inverse manually
1061
+ # and compare value/gradients with that:
1062
+ atrans = wp.transform_get_translation(a[0])
1063
+ arot = wp.transform_get_rotation(a[0])
1064
+
1065
+ rotinv = wp.quat_inverse(arot)
1066
+ result_manual = transform(-wp.quat_rotate(rotinv, atrans), rotinv)
1067
+
1068
+ for i in range(7):
1069
+ outputs[i] = wptype(2) * result[i]
1070
+ outputs_shouldbeidentity[i] = wptype(2) * idt[i]
1071
+ outputs_manual[i] = wptype(2) * result_manual[i]
1072
+
1073
+ kernel = getkernel(check_transform_inverse, suffix=dtype.__name__)
1074
+ output_select_kernel = get_select_kernel(wptype)
1075
+
1076
+ if register_kernels:
1077
+ return
1078
+
1079
+ q = rng.standard_normal(size=7)
1080
+ s = rng.standard_normal(size=7)
1081
+ q[3:] /= np.linalg.norm(q[3:])
1082
+ s[3:] /= np.linalg.norm(s[3:])
1083
+
1084
+ q = wp.array(q.astype(dtype), dtype=transform, requires_grad=True, device=device)
1085
+ outputs = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
1086
+ outputs_shouldbeidentity = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
1087
+ outputs_manual = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
1088
+
1089
+ wp.launch(
1090
+ kernel,
1091
+ dim=1,
1092
+ inputs=[
1093
+ q,
1094
+ ],
1095
+ outputs=[outputs, outputs_shouldbeidentity, outputs_manual],
1096
+ device=device,
1097
+ )
1098
+
1099
+ # check inverse:
1100
+ assert_np_equal(outputs_shouldbeidentity.numpy(), np.array([0, 0, 0, 0, 0, 0, 2]), tol=tol)
1101
+
1102
+ # same as manual result:
1103
+ assert_np_equal(outputs.numpy(), outputs_manual.numpy(), tol=tol)
1104
+
1105
+ for i in range(7):
1106
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1107
+ cmp_manual = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1108
+ tape = wp.Tape()
1109
+ with tape:
1110
+ wp.launch(
1111
+ kernel,
1112
+ dim=1,
1113
+ inputs=[
1114
+ q,
1115
+ ],
1116
+ outputs=[outputs, outputs_shouldbeidentity, outputs_manual],
1117
+ device=device,
1118
+ )
1119
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs, i], outputs=[cmp], device=device)
1120
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_manual, i], outputs=[cmp_manual], device=device)
1121
+ tape.backward(loss=cmp)
1122
+ qgrads = 1.0 * tape.gradients[q].numpy()
1123
+ tape.zero()
1124
+ tape.backward(loss=cmp_manual)
1125
+ qgrads_manual = 1.0 * tape.gradients[q].numpy()
1126
+ tape.zero()
1127
+
1128
+ # check gradients against manual result:
1129
+ assert_np_equal(qgrads, qgrads_manual, tol=tol)
1130
+
1131
+
1132
+ def test_transform_point_vector(test, device, dtype, register_kernels=False):
1133
+ rng = np.random.default_rng(123)
1134
+
1135
+ tol = {
1136
+ np.float16: 1.0e-2,
1137
+ np.float32: 1.0e-6,
1138
+ np.float64: 1.0e-8,
1139
+ }.get(dtype, 0)
1140
+
1141
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1142
+ transform = wp.types.transformation(dtype=wptype)
1143
+ vec3 = wp.types.vector(length=3, dtype=wptype)
1144
+
1145
+ def check_transform_point_vector(
1146
+ t: wp.array(dtype=transform),
1147
+ v: wp.array(dtype=vec3),
1148
+ outputs_pt: wp.array(dtype=wptype),
1149
+ outputs_pt_manual: wp.array(dtype=wptype),
1150
+ outputs_vec: wp.array(dtype=wptype),
1151
+ outputs_vec_manual: wp.array(dtype=wptype),
1152
+ ):
1153
+ result_pt = wp.transform_point(t[0], v[0])
1154
+ result_pt_manual = wp.transform_get_translation(t[0]) + wp.quat_rotate(wp.transform_get_rotation(t[0]), v[0])
1155
+
1156
+ result_vec = wp.transform_vector(t[0], v[0])
1157
+ result_vec_manual = wp.quat_rotate(wp.transform_get_rotation(t[0]), v[0])
1158
+
1159
+ for i in range(3):
1160
+ outputs_pt[i] = wptype(2) * result_pt[i]
1161
+ outputs_pt_manual[i] = wptype(2) * result_pt_manual[i]
1162
+ outputs_vec[i] = wptype(2) * result_vec[i]
1163
+ outputs_vec_manual[i] = wptype(2) * result_vec_manual[i]
1164
+
1165
+ kernel = getkernel(check_transform_point_vector, suffix=dtype.__name__)
1166
+ output_select_kernel = get_select_kernel(wptype)
1167
+
1168
+ if register_kernels:
1169
+ return
1170
+
1171
+ q = rng.standard_normal(size=7)
1172
+ q[3:] /= np.linalg.norm(q[3:])
1173
+
1174
+ t = wp.array(q.astype(dtype), dtype=transform, requires_grad=True, device=device)
1175
+ v = wp.array(rng.standard_normal(size=3), dtype=vec3, requires_grad=True, device=device)
1176
+ outputs_pt = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
1177
+ outputs_pt_manual = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
1178
+ outputs_vec = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
1179
+ outputs_vec_manual = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
1180
+
1181
+ wp.launch(
1182
+ kernel,
1183
+ dim=1,
1184
+ inputs=[t, v],
1185
+ outputs=[outputs_pt, outputs_pt_manual, outputs_vec, outputs_vec_manual],
1186
+ device=device,
1187
+ )
1188
+
1189
+ # same as manual results:
1190
+ assert_np_equal(outputs_pt.numpy(), outputs_pt_manual.numpy(), tol=tol)
1191
+ assert_np_equal(outputs_vec.numpy(), outputs_vec_manual.numpy(), tol=tol)
1192
+
1193
+ for i in range(3):
1194
+ cmp_pt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1195
+ cmp_pt_manual = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1196
+ cmp_vec = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1197
+ cmp_vec_manual = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1198
+
1199
+ tape = wp.Tape()
1200
+ with tape:
1201
+ wp.launch(
1202
+ kernel,
1203
+ dim=1,
1204
+ inputs=[t, v],
1205
+ outputs=[outputs_pt, outputs_pt_manual, outputs_vec, outputs_vec_manual],
1206
+ device=device,
1207
+ )
1208
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_pt, i], outputs=[cmp_pt], device=device)
1209
+ wp.launch(
1210
+ output_select_kernel, dim=1, inputs=[outputs_pt_manual, i], outputs=[cmp_pt_manual], device=device
1211
+ )
1212
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_vec, i], outputs=[cmp_vec], device=device)
1213
+ wp.launch(
1214
+ output_select_kernel, dim=1, inputs=[outputs_vec_manual, i], outputs=[cmp_vec_manual], device=device
1215
+ )
1216
+ tape.backward(loss=cmp_pt)
1217
+ tgrads_pt = 1.0 * tape.gradients[t].numpy()
1218
+ vgrads_pt = 1.0 * tape.gradients[v].numpy()
1219
+ tape.zero()
1220
+ tape.backward(loss=cmp_pt_manual)
1221
+ tgrads_pt_manual = 1.0 * tape.gradients[t].numpy()
1222
+ vgrads_pt_manual = 1.0 * tape.gradients[v].numpy()
1223
+ tape.zero()
1224
+ tape.backward(loss=cmp_vec)
1225
+ tgrads_vec = 1.0 * tape.gradients[t].numpy()
1226
+ vgrads_vec = 1.0 * tape.gradients[v].numpy()
1227
+ tape.zero()
1228
+ tape.backward(loss=cmp_vec_manual)
1229
+ tgrads_vec_manual = 1.0 * tape.gradients[t].numpy()
1230
+ vgrads_vec_manual = 1.0 * tape.gradients[v].numpy()
1231
+ tape.zero()
1232
+
1233
+ # check gradients against manual result:
1234
+ assert_np_equal(tgrads_pt, tgrads_pt_manual, tol=tol)
1235
+ assert_np_equal(vgrads_pt, vgrads_pt_manual, tol=tol)
1236
+ assert_np_equal(tgrads_vec, tgrads_vec_manual, tol=tol)
1237
+ assert_np_equal(vgrads_vec, vgrads_vec_manual, tol=tol)
1238
+
1239
+
1240
+ def test_spatial_matrix_constructors(test, device, dtype, register_kernels=False):
1241
+ rng = np.random.default_rng(123)
1242
+
1243
+ tol = {
1244
+ np.float16: 5.0e-3,
1245
+ np.float32: 1.0e-6,
1246
+ np.float64: 1.0e-8,
1247
+ }.get(dtype, 0)
1248
+
1249
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1250
+ spatial_matrix = wp.types.matrix(shape=(6, 6), dtype=wptype)
1251
+
1252
+ def check_spatial_matrix_constructor(
1253
+ input: wp.array(dtype=wptype),
1254
+ out: wp.array(dtype=wptype),
1255
+ ):
1256
+ # multiply the output by 2 so we've got something to backpropagate:
1257
+ result0 = spatial_matrix(
1258
+ input[0],
1259
+ input[1],
1260
+ input[2],
1261
+ input[3],
1262
+ input[4],
1263
+ input[5],
1264
+ input[6],
1265
+ input[7],
1266
+ input[8],
1267
+ input[9],
1268
+ input[10],
1269
+ input[11],
1270
+ input[12],
1271
+ input[13],
1272
+ input[14],
1273
+ input[15],
1274
+ input[16],
1275
+ input[17],
1276
+ input[18],
1277
+ input[19],
1278
+ input[20],
1279
+ input[21],
1280
+ input[22],
1281
+ input[23],
1282
+ input[24],
1283
+ input[25],
1284
+ input[26],
1285
+ input[27],
1286
+ input[28],
1287
+ input[29],
1288
+ input[30],
1289
+ input[31],
1290
+ input[32],
1291
+ input[33],
1292
+ input[34],
1293
+ input[35],
1294
+ )
1295
+ result1 = spatial_matrix()
1296
+
1297
+ idx = 0
1298
+ for i in range(6):
1299
+ for j in range(6):
1300
+ out[idx] = wptype(2) * result0[i, j]
1301
+ idx = idx + 1
1302
+
1303
+ for i in range(6):
1304
+ for j in range(6):
1305
+ out[idx] = result1[i, j]
1306
+ idx = idx + 1
1307
+
1308
+ kernel = getkernel(check_spatial_matrix_constructor, suffix=dtype.__name__)
1309
+ output_select_kernel = get_select_kernel(wptype)
1310
+
1311
+ if register_kernels:
1312
+ return
1313
+
1314
+ input = wp.array(rng.standard_normal(size=6 * 6).astype(dtype), requires_grad=True, device=device)
1315
+ output = wp.zeros(2 * 6 * 6, dtype=wptype, requires_grad=True, device=device)
1316
+
1317
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[output], device=device)
1318
+
1319
+ assert_np_equal(output.numpy()[: 6 * 6], 2 * input.numpy(), tol=tol)
1320
+ assert_np_equal(output.numpy()[6 * 6 :], np.zeros_like(input.numpy()), tol=tol)
1321
+
1322
+ for i in range(len(input)):
1323
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1324
+ tape = wp.Tape()
1325
+ with tape:
1326
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[output], device=device)
1327
+ wp.launch(output_select_kernel, dim=1, inputs=[output, i], outputs=[cmp], device=device)
1328
+ tape.backward(loss=cmp)
1329
+ expectedgrads = np.zeros(len(input))
1330
+ expectedgrads[i] = 2
1331
+ assert_np_equal(tape.gradients[input].numpy(), expectedgrads)
1332
+ tape.zero()
1333
+ break
1334
+
1335
+
1336
+ def test_spatial_matrix_indexing(test, device, dtype, register_kernels=False):
1337
+ rng = np.random.default_rng(123)
1338
+
1339
+ tol = {
1340
+ np.float16: 5.0e-3,
1341
+ np.float32: 1.0e-6,
1342
+ np.float64: 1.0e-8,
1343
+ }.get(dtype, 0)
1344
+
1345
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1346
+ spatial_matrix = wp.types.matrix(shape=(6, 6), dtype=wptype)
1347
+
1348
+ def check_spatial_matrix_indexing(
1349
+ input: wp.array(dtype=spatial_matrix),
1350
+ out: wp.array(dtype=wptype),
1351
+ ):
1352
+ inpt = input[0]
1353
+
1354
+ # multiply outputs by 2 so we've got something to backpropagate:
1355
+ idx = 0
1356
+ for i in range(6):
1357
+ for j in range(6):
1358
+ out[idx] = wptype(2) * inpt[i, j]
1359
+ idx = idx + 1
1360
+
1361
+ kernel = getkernel(check_spatial_matrix_indexing, suffix=dtype.__name__)
1362
+ output_select_kernel = get_select_kernel(wptype)
1363
+
1364
+ if register_kernels:
1365
+ return
1366
+
1367
+ input = wp.array(
1368
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1369
+ )
1370
+ outcmps = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1371
+
1372
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[outcmps], device=device)
1373
+
1374
+ assert_np_equal(outcmps.numpy(), 2 * input.numpy().ravel(), tol=tol)
1375
+ idx = 0
1376
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1377
+ for i in range(6):
1378
+ for j in range(6):
1379
+ tape = wp.Tape()
1380
+ with tape:
1381
+ wp.launch(kernel, dim=1, inputs=[input], outputs=[outcmps], device=device)
1382
+ wp.launch(output_select_kernel, dim=1, inputs=[outcmps, idx], outputs=[out], device=device)
1383
+ tape.backward(loss=out)
1384
+ expectedresult = np.zeros((6, 6), dtype=dtype)
1385
+ expectedresult[i, j] = 2
1386
+ assert_np_equal(tape.gradients[input].numpy()[0], expectedresult)
1387
+ tape.zero()
1388
+ idx = idx + 1
1389
+
1390
+
1391
+ def test_spatial_matrix_scalar_multiplication(test, device, dtype, register_kernels=False):
1392
+ rng = np.random.default_rng(123)
1393
+
1394
+ tol = {
1395
+ np.float16: 5.0e-3,
1396
+ np.float32: 1.0e-6,
1397
+ np.float64: 1.0e-8,
1398
+ }.get(dtype, 0)
1399
+
1400
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1401
+ spatial_matrix = wp.types.matrix(shape=(6, 6), dtype=wptype)
1402
+
1403
+ def check_spatial_matrix_scalar_mul(
1404
+ s: wp.array(dtype=wptype),
1405
+ q: wp.array(dtype=spatial_matrix),
1406
+ outcmps_l: wp.array(dtype=wptype),
1407
+ outcmps_r: wp.array(dtype=wptype),
1408
+ ):
1409
+ lresult = s[0] * q[0]
1410
+ rresult = q[0] * s[0]
1411
+
1412
+ # multiply outputs by 2 so we've got something to backpropagate:
1413
+ idx = 0
1414
+ for i in range(6):
1415
+ for j in range(6):
1416
+ outcmps_l[idx] = wptype(2) * lresult[i, j]
1417
+ outcmps_r[idx] = wptype(2) * rresult[i, j]
1418
+ idx = idx + 1
1419
+
1420
+ kernel = getkernel(check_spatial_matrix_scalar_mul, suffix=dtype.__name__)
1421
+ output_select_kernel = get_select_kernel(wptype)
1422
+
1423
+ if register_kernels:
1424
+ return
1425
+
1426
+ s = wp.array(rng.standard_normal(size=1).astype(dtype), requires_grad=True, device=device)
1427
+ q = wp.array(
1428
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1429
+ )
1430
+
1431
+ outcmps_l = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1432
+ outcmps_r = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1433
+
1434
+ wp.launch(
1435
+ kernel,
1436
+ dim=1,
1437
+ inputs=[s, q],
1438
+ outputs=[
1439
+ outcmps_l,
1440
+ outcmps_r,
1441
+ ],
1442
+ device=device,
1443
+ )
1444
+
1445
+ assert_np_equal(outcmps_l.numpy(), 2 * s.numpy()[0] * q.numpy(), tol=tol)
1446
+ assert_np_equal(outcmps_r.numpy(), 2 * s.numpy()[0] * q.numpy(), tol=tol)
1447
+
1448
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1449
+ idx = 0
1450
+ for i in range(6):
1451
+ for j in range(6):
1452
+ # test left/right mul gradients:
1453
+ for wrt in [outcmps_l, outcmps_r]:
1454
+ tape = wp.Tape()
1455
+ with tape:
1456
+ wp.launch(kernel, dim=1, inputs=[s, q], outputs=[outcmps_l, outcmps_r], device=device)
1457
+ wp.launch(output_select_kernel, dim=1, inputs=[wrt, idx], outputs=[out], device=device)
1458
+ tape.backward(loss=out)
1459
+ expectedresult = np.zeros((6, 6), dtype=dtype)
1460
+ expectedresult[i, j] = 2 * s.numpy()[0]
1461
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
1462
+ assert_np_equal(tape.gradients[s].numpy()[0], 2 * q.numpy()[0, i, j], tol=tol)
1463
+ tape.zero()
1464
+ idx = idx + 1
1465
+
1466
+
1467
+ def test_spatial_matrix_add_sub(test, device, dtype, register_kernels=False):
1468
+ rng = np.random.default_rng(123)
1469
+
1470
+ tol = {
1471
+ np.float16: 5.0e-3,
1472
+ np.float32: 1.0e-6,
1473
+ np.float64: 1.0e-8,
1474
+ }.get(dtype, 0)
1475
+
1476
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1477
+ spatial_matrix = wp.types.matrix(shape=(6, 6), dtype=wptype)
1478
+
1479
+ def check_spatial_matrix_add_sub(
1480
+ q: wp.array(dtype=spatial_matrix),
1481
+ v: wp.array(dtype=spatial_matrix),
1482
+ outputs_add: wp.array(dtype=wptype),
1483
+ outputs_sub: wp.array(dtype=wptype),
1484
+ ):
1485
+ addresult = q[0] + v[0]
1486
+ subresult = q[0] - v[0]
1487
+ idx = 0
1488
+ for i in range(6):
1489
+ for j in range(6):
1490
+ outputs_add[idx] = wptype(2) * addresult[i, j]
1491
+ outputs_sub[idx] = wptype(2) * subresult[i, j]
1492
+ idx = idx + 1
1493
+
1494
+ kernel = getkernel(check_spatial_matrix_add_sub, suffix=dtype.__name__)
1495
+ output_select_kernel = get_select_kernel(wptype)
1496
+
1497
+ if register_kernels:
1498
+ return
1499
+
1500
+ q = wp.array(
1501
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1502
+ )
1503
+ v = wp.array(
1504
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1505
+ )
1506
+
1507
+ outputs_add = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1508
+ outputs_sub = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1509
+
1510
+ wp.launch(
1511
+ kernel,
1512
+ dim=1,
1513
+ inputs=[
1514
+ q,
1515
+ v,
1516
+ ],
1517
+ outputs=[outputs_add, outputs_sub],
1518
+ device=device,
1519
+ )
1520
+
1521
+ assert_np_equal(outputs_add.numpy(), 2 * (q.numpy() + v.numpy()), tol=tol)
1522
+ assert_np_equal(outputs_sub.numpy(), 2 * (q.numpy() - v.numpy()), tol=tol)
1523
+
1524
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1525
+ idx = 0
1526
+ for i in range(6):
1527
+ for j in range(6):
1528
+ # test add gradients:
1529
+ tape = wp.Tape()
1530
+ with tape:
1531
+ wp.launch(kernel, dim=1, inputs=[q, v], outputs=[outputs_add, outputs_sub], device=device)
1532
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_add, idx], outputs=[out], device=device)
1533
+ tape.backward(loss=out)
1534
+ expectedresult = np.zeros((6, 6), dtype=dtype)
1535
+ expectedresult[i, j] = 2
1536
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
1537
+ assert_np_equal(tape.gradients[v].numpy()[0], expectedresult, tol=tol)
1538
+ tape.zero()
1539
+
1540
+ # test subtraction gradients:
1541
+ tape = wp.Tape()
1542
+ with tape:
1543
+ wp.launch(kernel, dim=1, inputs=[q, v], outputs=[outputs_add, outputs_sub], device=device)
1544
+ wp.launch(output_select_kernel, dim=1, inputs=[outputs_sub, idx], outputs=[out], device=device)
1545
+ tape.backward(loss=out)
1546
+ expectedresult = np.zeros((6, 6), dtype=dtype)
1547
+ expectedresult[i, j] = 2
1548
+ assert_np_equal(tape.gradients[q].numpy()[0], expectedresult, tol=tol)
1549
+ assert_np_equal(tape.gradients[v].numpy()[0], -expectedresult, tol=tol)
1550
+ tape.zero()
1551
+
1552
+ idx = idx + 1
1553
+
1554
+
1555
+ def test_spatial_matvec_multiplication(test, device, dtype, register_kernels=False):
1556
+ rng = np.random.default_rng(123)
1557
+
1558
+ tol = {
1559
+ np.float16: 2.0e-2,
1560
+ np.float32: 5.0e-6,
1561
+ np.float64: 1.0e-8,
1562
+ }.get(dtype, 0)
1563
+
1564
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1565
+ spatial_matrix = wp.types.matrix(shape=(6, 6), dtype=wptype)
1566
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
1567
+
1568
+ output_select_kernel = get_select_kernel(wptype)
1569
+
1570
+ def check_spatial_mat_vec_mul(
1571
+ v: wp.array(dtype=spatial_vector),
1572
+ m: wp.array(dtype=spatial_matrix),
1573
+ outcomponents: wp.array(dtype=wptype),
1574
+ ):
1575
+ result = m[0] * v[0]
1576
+
1577
+ # multiply outputs by 2 so we've got something to backpropagate:
1578
+ idx = 0
1579
+ for i in range(6):
1580
+ outcomponents[idx] = wptype(2) * result[i]
1581
+ idx = idx + 1
1582
+
1583
+ kernel = getkernel(check_spatial_mat_vec_mul, suffix=dtype.__name__)
1584
+
1585
+ if register_kernels:
1586
+ return
1587
+
1588
+ v = wp.array(
1589
+ rng.standard_normal(size=(1, 6)).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device
1590
+ )
1591
+ m = wp.array(
1592
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1593
+ )
1594
+ outcomponents = wp.zeros(6, dtype=wptype, requires_grad=True, device=device)
1595
+
1596
+ wp.launch(kernel, dim=1, inputs=[v, m], outputs=[outcomponents], device=device)
1597
+
1598
+ assert_np_equal(outcomponents.numpy(), 2 * np.matmul(m.numpy()[0], v.numpy()[0]), tol=tol)
1599
+
1600
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1601
+ for i in range(6):
1602
+ tape = wp.Tape()
1603
+ with tape:
1604
+ wp.launch(kernel, dim=1, inputs=[v, m], outputs=[outcomponents], device=device)
1605
+ wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, i], outputs=[out], device=device)
1606
+ tape.backward(loss=out)
1607
+
1608
+ assert_np_equal(tape.gradients[v].numpy()[0], 2 * m.numpy()[0, i, :], tol=tol)
1609
+ expectedresult = np.zeros((6, 6), dtype=dtype)
1610
+ expectedresult[i, :] = 2 * v.numpy()[0]
1611
+ assert_np_equal(tape.gradients[m].numpy()[0], expectedresult, tol=tol)
1612
+
1613
+ tape.zero()
1614
+
1615
+
1616
+ def test_spatial_matmat_multiplication(test, device, dtype, register_kernels=False):
1617
+ rng = np.random.default_rng(123)
1618
+
1619
+ tol = {
1620
+ np.float16: 2.0e-2,
1621
+ np.float32: 5.0e-6,
1622
+ np.float64: 5.0e-7,
1623
+ }.get(dtype, 0)
1624
+
1625
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1626
+ spatial_matrix = wp.types.matrix(shape=(6, 6), dtype=wptype)
1627
+
1628
+ output_select_kernel = get_select_kernel(wptype)
1629
+
1630
+ def check_mat_mat_mul(
1631
+ v: wp.array(dtype=spatial_matrix),
1632
+ m: wp.array(dtype=spatial_matrix),
1633
+ outcomponents: wp.array(dtype=wptype),
1634
+ ):
1635
+ result = m[0] * v[0]
1636
+
1637
+ # multiply outputs by 2 so we've got something to backpropagate:
1638
+ idx = 0
1639
+ for i in range(6):
1640
+ for j in range(6):
1641
+ outcomponents[idx] = wptype(2) * result[i, j]
1642
+ idx = idx + 1
1643
+
1644
+ kernel = getkernel(check_mat_mat_mul, suffix=dtype.__name__)
1645
+
1646
+ if register_kernels:
1647
+ return
1648
+
1649
+ v = wp.array(
1650
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1651
+ )
1652
+ m = wp.array(
1653
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1654
+ )
1655
+ outcomponents = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1656
+
1657
+ wp.launch(kernel, dim=1, inputs=[v, m], outputs=[outcomponents], device=device)
1658
+
1659
+ assert_np_equal(outcomponents.numpy(), 2 * np.matmul(m.numpy()[0], v.numpy()[0]), tol=tol)
1660
+
1661
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1662
+ idx = 0
1663
+ for i in range(6):
1664
+ for j in range(6):
1665
+ tape = wp.Tape()
1666
+ with tape:
1667
+ wp.launch(kernel, dim=1, inputs=[v, m], outputs=[outcomponents], device=device)
1668
+ wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
1669
+ tape.backward(loss=out)
1670
+
1671
+ expected = np.zeros((6, 6), dtype=dtype)
1672
+ expected[:, j] = 2 * m.numpy()[0, i, :]
1673
+ assert_np_equal(tape.gradients[v].numpy()[0], expected, tol=10 * tol)
1674
+
1675
+ expected = np.zeros((6, 6), dtype=dtype)
1676
+ expected[i, :] = 2 * v.numpy()[0, :, j]
1677
+ assert_np_equal(tape.gradients[m].numpy()[0], expected, tol=10 * tol)
1678
+
1679
+ tape.zero()
1680
+ idx = idx + 1
1681
+
1682
+
1683
+ def test_spatial_mat_transpose(test, device, dtype, register_kernels=False):
1684
+ rng = np.random.default_rng(123)
1685
+
1686
+ tol = {
1687
+ np.float16: 1.0e-2,
1688
+ np.float32: 1.0e-6,
1689
+ np.float64: 1.0e-8,
1690
+ }.get(dtype, 0)
1691
+
1692
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1693
+ spatial_matrix = wp.types.matrix(shape=(6, 6), dtype=wptype)
1694
+
1695
+ output_select_kernel = get_select_kernel(wptype)
1696
+
1697
+ def check_spatial_mat_transpose(
1698
+ m: wp.array(dtype=spatial_matrix),
1699
+ outcomponents: wp.array(dtype=wptype),
1700
+ ):
1701
+ # multiply outputs by 2 so we've got something to backpropagate:
1702
+ mat = wptype(2) * wp.transpose(m[0])
1703
+
1704
+ idx = 0
1705
+ for i in range(6):
1706
+ for j in range(6):
1707
+ outcomponents[idx] = mat[i, j]
1708
+ idx = idx + 1
1709
+
1710
+ kernel = getkernel(check_spatial_mat_transpose, suffix=dtype.__name__)
1711
+
1712
+ if register_kernels:
1713
+ return
1714
+
1715
+ m = wp.array(
1716
+ rng.standard_normal(size=(1, 6, 6)).astype(dtype), dtype=spatial_matrix, requires_grad=True, device=device
1717
+ )
1718
+ outcomponents = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1719
+
1720
+ wp.launch(kernel, dim=1, inputs=[m], outputs=[outcomponents], device=device)
1721
+
1722
+ assert_np_equal(outcomponents.numpy(), 2 * m.numpy()[0].T, tol=tol)
1723
+
1724
+ idx = 0
1725
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1726
+ for i in range(6):
1727
+ for j in range(6):
1728
+ tape = wp.Tape()
1729
+ with tape:
1730
+ wp.launch(kernel, dim=1, inputs=[m], outputs=[outcomponents], device=device)
1731
+ wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
1732
+ tape.backward(loss=out)
1733
+ expectedresult = np.zeros((6, 6), dtype=dtype)
1734
+ expectedresult[j, i] = 2
1735
+ assert_np_equal(tape.gradients[m].numpy()[0], expectedresult)
1736
+ tape.zero()
1737
+ idx = idx + 1
1738
+
1739
+
1740
+ def test_spatial_outer_product(test, device, dtype, register_kernels=False):
1741
+ rng = np.random.default_rng(123)
1742
+
1743
+ tol = {
1744
+ np.float16: 5.0e-3,
1745
+ np.float32: 1.0e-6,
1746
+ np.float64: 1.0e-8,
1747
+ }.get(dtype, 0)
1748
+
1749
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1750
+ spatial_vector = wp.types.vector(length=6, dtype=wptype)
1751
+
1752
+ output_select_kernel = get_select_kernel(wptype)
1753
+
1754
+ def check_spatial_outer_product(
1755
+ s: wp.array(dtype=spatial_vector),
1756
+ v: wp.array(dtype=spatial_vector),
1757
+ outcomponents: wp.array(dtype=wptype),
1758
+ ):
1759
+ mresult = wptype(2) * wp.outer(s[0], v[0])
1760
+
1761
+ # multiply outputs by 2 so we've got something to backpropagate:
1762
+ idx = 0
1763
+ for i in range(6):
1764
+ for j in range(6):
1765
+ outcomponents[idx] = mresult[i, j]
1766
+ idx = idx + 1
1767
+
1768
+ kernel = getkernel(check_spatial_outer_product, suffix=dtype.__name__)
1769
+
1770
+ if register_kernels:
1771
+ return
1772
+
1773
+ s = wp.array(
1774
+ rng.standard_normal(size=(1, 6)).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device
1775
+ )
1776
+ v = wp.array(
1777
+ rng.standard_normal(size=(1, 6)).astype(dtype), dtype=spatial_vector, requires_grad=True, device=device
1778
+ )
1779
+ outcomponents = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1780
+
1781
+ wp.launch(kernel, dim=1, inputs=[s, v], outputs=[outcomponents], device=device)
1782
+
1783
+ assert_np_equal(outcomponents.numpy(), 2 * s.numpy()[0, :, None] * v.numpy()[0, None, :], tol=tol)
1784
+
1785
+ idx = 0
1786
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1787
+
1788
+ for i in range(6):
1789
+ for j in range(6):
1790
+ tape = wp.Tape()
1791
+ with tape:
1792
+ wp.launch(
1793
+ kernel,
1794
+ dim=1,
1795
+ inputs=[
1796
+ s,
1797
+ v,
1798
+ ],
1799
+ outputs=[outcomponents],
1800
+ device=device,
1801
+ )
1802
+ wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
1803
+ tape.backward(loss=out)
1804
+
1805
+ # this component's gonna be s_i * v_j, so its s gradient is gonna be nozero
1806
+ # at the ith component and its v gradient will be nonzero at the jth component:
1807
+
1808
+ expectedresult = np.zeros((6), dtype=dtype)
1809
+ expectedresult[i] = 2 * v.numpy()[0, j]
1810
+ assert_np_equal(tape.gradients[s].numpy()[0], expectedresult, tol=10 * tol)
1811
+
1812
+ expectedresult = np.zeros((6), dtype=dtype)
1813
+ expectedresult[j] = 2 * s.numpy()[0, i]
1814
+ assert_np_equal(tape.gradients[v].numpy()[0], expectedresult, tol=10 * tol)
1815
+ tape.zero()
1816
+
1817
+ idx = idx + 1
1818
+
1819
+
1820
+ def test_spatial_adjoint(test, device, dtype, register_kernels=False):
1821
+ rng = np.random.default_rng(123)
1822
+
1823
+ tol = {
1824
+ np.float16: 5.0e-3,
1825
+ np.float32: 1.0e-6,
1826
+ np.float64: 1.0e-8,
1827
+ }.get(dtype, 0)
1828
+
1829
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1830
+ mat3 = wp.types.matrix(shape=(3, 3), dtype=wptype)
1831
+
1832
+ output_select_kernel = get_select_kernel(wptype)
1833
+
1834
+ def check_spatial_adjoint(
1835
+ R: wp.array(dtype=mat3),
1836
+ S: wp.array(dtype=mat3),
1837
+ outcomponents: wp.array(dtype=wptype),
1838
+ ):
1839
+ mresult = wptype(2) * wp.spatial_adjoint(R[0], S[0])
1840
+
1841
+ # multiply outputs by 2 so we've got something to backpropagate:
1842
+ idx = 0
1843
+ for i in range(6):
1844
+ for j in range(6):
1845
+ outcomponents[idx] = mresult[i, j]
1846
+ idx = idx + 1
1847
+
1848
+ kernel = getkernel(check_spatial_adjoint, suffix=dtype.__name__)
1849
+
1850
+ if register_kernels:
1851
+ return
1852
+
1853
+ R = wp.array(rng.standard_normal(size=(1, 3, 3)).astype(dtype), dtype=mat3, requires_grad=True, device=device)
1854
+ S = wp.array(rng.standard_normal(size=(1, 3, 3)).astype(dtype), dtype=mat3, requires_grad=True, device=device)
1855
+ outcomponents = wp.zeros(6 * 6, dtype=wptype, requires_grad=True, device=device)
1856
+
1857
+ wp.launch(kernel, dim=1, inputs=[R, S], outputs=[outcomponents], device=device)
1858
+
1859
+ result = outcomponents.numpy().reshape(6, 6)
1860
+ expected = np.zeros_like(result)
1861
+ expected[:3, :3] = R.numpy()
1862
+ expected[3:, 3:] = R.numpy()
1863
+ expected[3:, :3] = S.numpy()
1864
+
1865
+ assert_np_equal(result, 2 * expected, tol=tol)
1866
+
1867
+ idx = 0
1868
+ out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1869
+ for i in range(6):
1870
+ for j in range(6):
1871
+ tape = wp.Tape()
1872
+ with tape:
1873
+ wp.launch(
1874
+ kernel,
1875
+ dim=1,
1876
+ inputs=[
1877
+ R,
1878
+ S,
1879
+ ],
1880
+ outputs=[outcomponents],
1881
+ device=device,
1882
+ )
1883
+ wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
1884
+ tape.backward(loss=out)
1885
+
1886
+ # this component's gonna be s_i * v_j, so its s gradient is gonna be nozero
1887
+ # at the ith component and its v gradient will be nonzero at the jth component:
1888
+
1889
+ expectedresult = np.zeros((3, 3), dtype=dtype)
1890
+ if (i // 3 == 0 and j // 3 == 0) or (i // 3 == 1 and j // 3 == 1):
1891
+ expectedresult[i % 3, j % 3] = 2
1892
+ assert_np_equal(tape.gradients[R].numpy()[0], expectedresult, tol=10 * tol)
1893
+
1894
+ expectedresult = np.zeros((3, 3), dtype=dtype)
1895
+ if i // 3 == 1 and j // 3 == 0:
1896
+ expectedresult[i % 3, j % 3] = 2
1897
+ assert_np_equal(tape.gradients[S].numpy()[0], expectedresult, tol=10 * tol)
1898
+ tape.zero()
1899
+
1900
+ idx = idx + 1
1901
+
1902
+
1903
+ def test_transform_identity(test, device, dtype, register_kernels=False):
1904
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1905
+
1906
+ def transform_identity_test(output: wp.array(dtype=wptype)):
1907
+ t = wp.transform_identity(dtype=wptype)
1908
+ for i in range(7):
1909
+ output[i] = t[i]
1910
+
1911
+ def transform_identity_test_default(output: wp.array(dtype=wp.float32)):
1912
+ t = wp.transform_identity()
1913
+ for i in range(7):
1914
+ output[i] = t[i]
1915
+
1916
+ quat_identity_kernel = getkernel(transform_identity_test, suffix=dtype.__name__)
1917
+ quat_identity_default_kernel = getkernel(transform_identity_test_default, suffix=np.float32.__name__)
1918
+
1919
+ if register_kernels:
1920
+ return
1921
+
1922
+ output = wp.zeros(7, dtype=wptype, device=device)
1923
+ wp.launch(quat_identity_kernel, dim=1, inputs=[], outputs=[output], device=device)
1924
+ expected = np.zeros_like(output.numpy())
1925
+ expected[-1] = 1
1926
+ assert_np_equal(output.numpy(), expected)
1927
+
1928
+ # let's just test that it defaults to float32:
1929
+ output = wp.zeros(7, dtype=wp.float32, device=device)
1930
+ wp.launch(quat_identity_default_kernel, dim=1, inputs=[], outputs=[output], device=device)
1931
+ expected = np.zeros_like(output.numpy())
1932
+ expected[-1] = 1
1933
+ assert_np_equal(output.numpy(), expected)
1934
+
1935
+
1936
+ def test_transform_anon_type_instance(test, device, dtype, register_kernels=False):
1937
+ rng = np.random.default_rng(123)
1938
+
1939
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1940
+
1941
+ def transform_create_test(input: wp.array(dtype=wptype), output: wp.array(dtype=wptype)):
1942
+ t = wp.transformation(
1943
+ wp.vector(input[0], input[1], input[2]), wp.quaternion(input[3], input[4], input[5], input[6])
1944
+ )
1945
+ for i in range(7):
1946
+ output[i] = wptype(2) * t[i]
1947
+
1948
+ transform_create_kernel = getkernel(transform_create_test, suffix=dtype.__name__)
1949
+ output_select_kernel = get_select_kernel(wptype)
1950
+
1951
+ if register_kernels:
1952
+ return
1953
+
1954
+ input = wp.array(rng.standard_normal(size=7).astype(dtype), requires_grad=True, device=device)
1955
+ output = wp.zeros(7, dtype=wptype, requires_grad=True, device=device)
1956
+ wp.launch(transform_create_kernel, dim=1, inputs=[input], outputs=[output], device=device)
1957
+ assert_np_equal(output.numpy(), 2 * input.numpy())
1958
+
1959
+ for i in range(len(input)):
1960
+ cmp = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
1961
+ tape = wp.Tape()
1962
+ with tape:
1963
+ wp.launch(transform_create_kernel, dim=1, inputs=[input], outputs=[output], device=device)
1964
+ wp.launch(output_select_kernel, dim=1, inputs=[output, i], outputs=[cmp], device=device)
1965
+ tape.backward(loss=cmp)
1966
+ expectedgrads = np.zeros(len(input))
1967
+ expectedgrads[i] = 2
1968
+ assert_np_equal(tape.gradients[input].numpy(), expectedgrads)
1969
+ tape.zero()
1970
+
1971
+
1972
+ def test_transform_from_matrix(test, device, dtype, register_kernels=False):
1973
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
1974
+ mat44 = wp.types.matrix((4, 4), wptype)
1975
+ vec3 = wp.types.vector(3, wptype)
1976
+ quat = wp.types.quaternion(wptype)
1977
+
1978
+ def transform_from_matrix_kernel():
1979
+ # fmt: off
1980
+ m = mat44(
1981
+ wptype(0.6), wptype(0.48), wptype(0.64), wptype(1.0),
1982
+ wptype(-0.8), wptype(0.36), wptype(0.48), wptype(2.0),
1983
+ wptype(0.0), wptype(-0.8), wptype(0.6), wptype(3.0),
1984
+ wptype(0.0), wptype(0.0), wptype(0.0), wptype(1.0),
1985
+ )
1986
+ # fmt: on
1987
+ t = wp.transform_from_matrix(m)
1988
+ p = wp.transform_get_translation(t)
1989
+ q = wp.transform_get_rotation(t)
1990
+ wp.expect_near(p, vec3(wptype(1.0), wptype(2.0), wptype(3.0)), tolerance=wptype(1e-3))
1991
+ wp.expect_near(q, quat(wptype(-0.4), wptype(0.2), wptype(-0.4), wptype(0.8)), tolerance=wptype(1e-3))
1992
+
1993
+ kernel = getkernel(transform_from_matrix_kernel, suffix=dtype.__name__)
1994
+
1995
+ if register_kernels:
1996
+ return
1997
+
1998
+ wp.launch(kernel, dim=1, device=device)
1999
+
2000
+
2001
+ def test_transform_to_matrix(test, device, dtype, register_kernels=False):
2002
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
2003
+ mat44 = wp.types.matrix((4, 4), wptype)
2004
+ vec3 = wp.types.vector(3, wptype)
2005
+ quat = wp.types.quaternion(wptype)
2006
+
2007
+ def transform_to_matrix_kernel():
2008
+ p = vec3(wptype(1.0), wptype(2.0), wptype(3.0))
2009
+ q = quat(wptype(-0.4), wptype(0.2), wptype(-0.4), wptype(0.8))
2010
+ t = wp.transformation(p, q)
2011
+ m = wp.transform_to_matrix(t)
2012
+ # fmt: off
2013
+ wp.expect_near(
2014
+ m,
2015
+ mat44(
2016
+ wptype(0.6), wptype(0.48), wptype(0.64), wptype(1.0),
2017
+ wptype(-0.8), wptype(0.36), wptype(0.48), wptype(2.0),
2018
+ wptype(0.0), wptype(-0.8), wptype(0.6), wptype(3.0),
2019
+ wptype(0.0), wptype(0.0), wptype(0.0), wptype(1.0),
2020
+ ),
2021
+ tolerance=wptype(1e-3),
2022
+ )
2023
+ # fmt: on
2024
+
2025
+ kernel = getkernel(transform_to_matrix_kernel, suffix=dtype.__name__)
2026
+
2027
+ if register_kernels:
2028
+ return
2029
+
2030
+ wp.launch(kernel, dim=1, device=device)
2031
+
2032
+
2033
+ devices = get_test_devices()
2034
+
2035
+
2036
+ class TestSpatial(unittest.TestCase):
2037
+ pass
2038
+
2039
+
2040
+ for dtype in np_float_types:
2041
+ add_function_test_register_kernel(
2042
+ TestSpatial,
2043
+ f"test_spatial_vector_constructors_{dtype.__name__}",
2044
+ test_spatial_vector_constructors,
2045
+ devices=devices,
2046
+ dtype=dtype,
2047
+ )
2048
+ add_function_test_register_kernel(
2049
+ TestSpatial,
2050
+ f"test_spatial_vector_indexing_{dtype.__name__}",
2051
+ test_spatial_vector_indexing,
2052
+ devices=devices,
2053
+ dtype=dtype,
2054
+ )
2055
+ add_function_test_register_kernel(
2056
+ TestSpatial,
2057
+ f"test_spatial_vector_scalar_multiplication_{dtype.__name__}",
2058
+ test_spatial_vector_scalar_multiplication,
2059
+ devices=devices,
2060
+ dtype=dtype,
2061
+ )
2062
+ add_function_test_register_kernel(
2063
+ TestSpatial,
2064
+ f"test_spatial_vector_add_sub_{dtype.__name__}",
2065
+ test_spatial_vector_add_sub,
2066
+ devices=devices,
2067
+ dtype=dtype,
2068
+ )
2069
+ add_function_test_register_kernel(
2070
+ TestSpatial, f"test_spatial_dot_{dtype.__name__}", test_spatial_dot, devices=devices, dtype=dtype
2071
+ )
2072
+ add_function_test_register_kernel(
2073
+ TestSpatial, f"test_spatial_cross_{dtype.__name__}", test_spatial_cross, devices=devices, dtype=dtype
2074
+ )
2075
+ add_function_test_register_kernel(
2076
+ TestSpatial,
2077
+ f"test_spatial_top_bottom_{dtype.__name__}",
2078
+ test_spatial_top_bottom,
2079
+ devices=devices,
2080
+ dtype=dtype,
2081
+ )
2082
+
2083
+ add_function_test_register_kernel(
2084
+ TestSpatial,
2085
+ f"test_transform_constructors_{dtype.__name__}",
2086
+ test_transform_constructors,
2087
+ devices=devices,
2088
+ dtype=dtype,
2089
+ )
2090
+ add_function_test_register_kernel(
2091
+ TestSpatial,
2092
+ f"test_transform_anon_type_instance_{dtype.__name__}",
2093
+ test_transform_anon_type_instance,
2094
+ devices=devices,
2095
+ dtype=dtype,
2096
+ )
2097
+ add_function_test_register_kernel(
2098
+ TestSpatial,
2099
+ f"test_transform_identity_{dtype.__name__}",
2100
+ test_transform_identity,
2101
+ devices=devices,
2102
+ dtype=dtype,
2103
+ )
2104
+ add_function_test_register_kernel(
2105
+ TestSpatial,
2106
+ f"test_transform_indexing_{dtype.__name__}",
2107
+ test_transform_indexing,
2108
+ devices=devices,
2109
+ dtype=dtype,
2110
+ )
2111
+ add_function_test_register_kernel(
2112
+ TestSpatial,
2113
+ f"test_transform_get_trans_rot_{dtype.__name__}",
2114
+ test_transform_get_trans_rot,
2115
+ devices=devices,
2116
+ dtype=dtype,
2117
+ )
2118
+ add_function_test_register_kernel(
2119
+ TestSpatial,
2120
+ f"test_transform_multiply_{dtype.__name__}",
2121
+ test_transform_multiply,
2122
+ devices=devices,
2123
+ dtype=dtype,
2124
+ )
2125
+ add_function_test_register_kernel(
2126
+ TestSpatial,
2127
+ f"test_transform_inverse_{dtype.__name__}",
2128
+ test_transform_inverse,
2129
+ devices=devices,
2130
+ dtype=dtype,
2131
+ )
2132
+ add_function_test_register_kernel(
2133
+ TestSpatial,
2134
+ f"test_transform_point_vector_{dtype.__name__}",
2135
+ test_transform_point_vector,
2136
+ devices=devices,
2137
+ dtype=dtype,
2138
+ )
2139
+
2140
+ # are these two valid? They don't seem to be doing things you'd want to do,
2141
+ # maybe they should be removed
2142
+ add_function_test_register_kernel(
2143
+ TestSpatial,
2144
+ f"test_transform_scalar_multiplication_{dtype.__name__}",
2145
+ test_transform_scalar_multiplication,
2146
+ devices=devices,
2147
+ dtype=dtype,
2148
+ )
2149
+ add_function_test_register_kernel(
2150
+ TestSpatial,
2151
+ f"test_transform_add_sub_{dtype.__name__}",
2152
+ test_transform_add_sub,
2153
+ devices=devices,
2154
+ dtype=dtype,
2155
+ )
2156
+
2157
+ add_function_test_register_kernel(
2158
+ TestSpatial,
2159
+ f"test_spatial_matrix_constructors_{dtype.__name__}",
2160
+ test_spatial_matrix_constructors,
2161
+ devices=devices,
2162
+ dtype=dtype,
2163
+ )
2164
+ add_function_test_register_kernel(
2165
+ TestSpatial,
2166
+ f"test_spatial_matrix_indexing_{dtype.__name__}",
2167
+ test_spatial_matrix_indexing,
2168
+ devices=devices,
2169
+ dtype=dtype,
2170
+ )
2171
+ add_function_test_register_kernel(
2172
+ TestSpatial,
2173
+ f"test_spatial_matrix_scalar_multiplication_{dtype.__name__}",
2174
+ test_spatial_matrix_scalar_multiplication,
2175
+ devices=devices,
2176
+ dtype=dtype,
2177
+ )
2178
+ add_function_test_register_kernel(
2179
+ TestSpatial,
2180
+ f"test_spatial_matrix_add_sub_{dtype.__name__}",
2181
+ test_spatial_matrix_add_sub,
2182
+ devices=devices,
2183
+ dtype=dtype,
2184
+ )
2185
+ add_function_test_register_kernel(
2186
+ TestSpatial,
2187
+ f"test_spatial_matvec_multiplication_{dtype.__name__}",
2188
+ test_spatial_matvec_multiplication,
2189
+ devices=devices,
2190
+ dtype=dtype,
2191
+ )
2192
+ add_function_test_register_kernel(
2193
+ TestSpatial,
2194
+ f"test_spatial_matmat_multiplication_{dtype.__name__}",
2195
+ test_spatial_matmat_multiplication,
2196
+ devices=devices,
2197
+ dtype=dtype,
2198
+ )
2199
+ add_function_test_register_kernel(
2200
+ TestSpatial,
2201
+ f"test_spatial_outer_product_{dtype.__name__}",
2202
+ test_spatial_outer_product,
2203
+ devices=devices,
2204
+ dtype=dtype,
2205
+ )
2206
+ add_function_test_register_kernel(
2207
+ TestSpatial, f"test_spatial_adjoint_{dtype.__name__}", test_spatial_adjoint, devices=devices, dtype=dtype
2208
+ )
2209
+ add_function_test_register_kernel(
2210
+ TestSpatial,
2211
+ f"test_transform_from_matrix_{dtype.__name__}",
2212
+ test_transform_from_matrix,
2213
+ devices=devices,
2214
+ dtype=dtype,
2215
+ )
2216
+ add_function_test_register_kernel(
2217
+ TestSpatial,
2218
+ f"test_transform_to_matrix_{dtype.__name__}",
2219
+ test_transform_to_matrix,
2220
+ devices=devices,
2221
+ dtype=dtype,
2222
+ )
2223
+
2224
+ # \TODO: test spatial_mass and spatial_jacobian
2225
+
2226
+
2227
+ if __name__ == "__main__":
2228
+ wp.clear_kernel_cache()
2229
+ unittest.main(verbosity=2)