warp-lang 1.10.0__py3-none-macosx_11_0_arm64.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 (468) hide show
  1. warp/__init__.py +334 -0
  2. warp/__init__.pyi +5856 -0
  3. warp/_src/__init__.py +14 -0
  4. warp/_src/autograd.py +1077 -0
  5. warp/_src/build.py +620 -0
  6. warp/_src/build_dll.py +642 -0
  7. warp/_src/builtins.py +10555 -0
  8. warp/_src/codegen.py +4361 -0
  9. warp/_src/config.py +178 -0
  10. warp/_src/constants.py +59 -0
  11. warp/_src/context.py +8352 -0
  12. warp/_src/dlpack.py +464 -0
  13. warp/_src/fabric.py +362 -0
  14. warp/_src/fem/__init__.py +14 -0
  15. warp/_src/fem/adaptivity.py +510 -0
  16. warp/_src/fem/cache.py +689 -0
  17. warp/_src/fem/dirichlet.py +190 -0
  18. warp/_src/fem/domain.py +553 -0
  19. warp/_src/fem/field/__init__.py +131 -0
  20. warp/_src/fem/field/field.py +703 -0
  21. warp/_src/fem/field/nodal_field.py +403 -0
  22. warp/_src/fem/field/restriction.py +39 -0
  23. warp/_src/fem/field/virtual.py +1021 -0
  24. warp/_src/fem/geometry/__init__.py +32 -0
  25. warp/_src/fem/geometry/adaptive_nanogrid.py +782 -0
  26. warp/_src/fem/geometry/closest_point.py +99 -0
  27. warp/_src/fem/geometry/deformed_geometry.py +277 -0
  28. warp/_src/fem/geometry/element.py +854 -0
  29. warp/_src/fem/geometry/geometry.py +693 -0
  30. warp/_src/fem/geometry/grid_2d.py +478 -0
  31. warp/_src/fem/geometry/grid_3d.py +539 -0
  32. warp/_src/fem/geometry/hexmesh.py +956 -0
  33. warp/_src/fem/geometry/nanogrid.py +660 -0
  34. warp/_src/fem/geometry/partition.py +483 -0
  35. warp/_src/fem/geometry/quadmesh.py +597 -0
  36. warp/_src/fem/geometry/tetmesh.py +762 -0
  37. warp/_src/fem/geometry/trimesh.py +588 -0
  38. warp/_src/fem/integrate.py +2507 -0
  39. warp/_src/fem/linalg.py +385 -0
  40. warp/_src/fem/operator.py +398 -0
  41. warp/_src/fem/polynomial.py +231 -0
  42. warp/_src/fem/quadrature/__init__.py +17 -0
  43. warp/_src/fem/quadrature/pic_quadrature.py +318 -0
  44. warp/_src/fem/quadrature/quadrature.py +665 -0
  45. warp/_src/fem/space/__init__.py +248 -0
  46. warp/_src/fem/space/basis_function_space.py +499 -0
  47. warp/_src/fem/space/basis_space.py +681 -0
  48. warp/_src/fem/space/dof_mapper.py +253 -0
  49. warp/_src/fem/space/function_space.py +312 -0
  50. warp/_src/fem/space/grid_2d_function_space.py +179 -0
  51. warp/_src/fem/space/grid_3d_function_space.py +229 -0
  52. warp/_src/fem/space/hexmesh_function_space.py +255 -0
  53. warp/_src/fem/space/nanogrid_function_space.py +199 -0
  54. warp/_src/fem/space/partition.py +435 -0
  55. warp/_src/fem/space/quadmesh_function_space.py +222 -0
  56. warp/_src/fem/space/restriction.py +221 -0
  57. warp/_src/fem/space/shape/__init__.py +152 -0
  58. warp/_src/fem/space/shape/cube_shape_function.py +1107 -0
  59. warp/_src/fem/space/shape/shape_function.py +134 -0
  60. warp/_src/fem/space/shape/square_shape_function.py +928 -0
  61. warp/_src/fem/space/shape/tet_shape_function.py +829 -0
  62. warp/_src/fem/space/shape/triangle_shape_function.py +674 -0
  63. warp/_src/fem/space/tetmesh_function_space.py +270 -0
  64. warp/_src/fem/space/topology.py +461 -0
  65. warp/_src/fem/space/trimesh_function_space.py +193 -0
  66. warp/_src/fem/types.py +114 -0
  67. warp/_src/fem/utils.py +488 -0
  68. warp/_src/jax.py +188 -0
  69. warp/_src/jax_experimental/__init__.py +14 -0
  70. warp/_src/jax_experimental/custom_call.py +389 -0
  71. warp/_src/jax_experimental/ffi.py +1286 -0
  72. warp/_src/jax_experimental/xla_ffi.py +658 -0
  73. warp/_src/marching_cubes.py +710 -0
  74. warp/_src/math.py +416 -0
  75. warp/_src/optim/__init__.py +14 -0
  76. warp/_src/optim/adam.py +165 -0
  77. warp/_src/optim/linear.py +1608 -0
  78. warp/_src/optim/sgd.py +114 -0
  79. warp/_src/paddle.py +408 -0
  80. warp/_src/render/__init__.py +14 -0
  81. warp/_src/render/imgui_manager.py +291 -0
  82. warp/_src/render/render_opengl.py +3638 -0
  83. warp/_src/render/render_usd.py +939 -0
  84. warp/_src/render/utils.py +162 -0
  85. warp/_src/sparse.py +2718 -0
  86. warp/_src/tape.py +1208 -0
  87. warp/_src/thirdparty/__init__.py +0 -0
  88. warp/_src/thirdparty/appdirs.py +598 -0
  89. warp/_src/thirdparty/dlpack.py +145 -0
  90. warp/_src/thirdparty/unittest_parallel.py +676 -0
  91. warp/_src/torch.py +393 -0
  92. warp/_src/types.py +5888 -0
  93. warp/_src/utils.py +1695 -0
  94. warp/autograd.py +33 -0
  95. warp/bin/libwarp-clang.dylib +0 -0
  96. warp/bin/libwarp.dylib +0 -0
  97. warp/build.py +29 -0
  98. warp/build_dll.py +24 -0
  99. warp/codegen.py +24 -0
  100. warp/constants.py +24 -0
  101. warp/context.py +33 -0
  102. warp/dlpack.py +24 -0
  103. warp/examples/__init__.py +24 -0
  104. warp/examples/assets/bear.usd +0 -0
  105. warp/examples/assets/bunny.usd +0 -0
  106. warp/examples/assets/cube.usd +0 -0
  107. warp/examples/assets/nonuniform.usd +0 -0
  108. warp/examples/assets/nvidia_logo.png +0 -0
  109. warp/examples/assets/pixel.jpg +0 -0
  110. warp/examples/assets/rocks.nvdb +0 -0
  111. warp/examples/assets/rocks.usd +0 -0
  112. warp/examples/assets/sphere.usd +0 -0
  113. warp/examples/assets/square_cloth.usd +0 -0
  114. warp/examples/benchmarks/benchmark_api.py +389 -0
  115. warp/examples/benchmarks/benchmark_cloth.py +296 -0
  116. warp/examples/benchmarks/benchmark_cloth_cupy.py +96 -0
  117. warp/examples/benchmarks/benchmark_cloth_jax.py +105 -0
  118. warp/examples/benchmarks/benchmark_cloth_numba.py +161 -0
  119. warp/examples/benchmarks/benchmark_cloth_numpy.py +85 -0
  120. warp/examples/benchmarks/benchmark_cloth_paddle.py +94 -0
  121. warp/examples/benchmarks/benchmark_cloth_pytorch.py +94 -0
  122. warp/examples/benchmarks/benchmark_cloth_taichi.py +120 -0
  123. warp/examples/benchmarks/benchmark_cloth_warp.py +153 -0
  124. warp/examples/benchmarks/benchmark_gemm.py +164 -0
  125. warp/examples/benchmarks/benchmark_interop_paddle.py +166 -0
  126. warp/examples/benchmarks/benchmark_interop_torch.py +166 -0
  127. warp/examples/benchmarks/benchmark_launches.py +301 -0
  128. warp/examples/benchmarks/benchmark_tile_load_store.py +103 -0
  129. warp/examples/benchmarks/benchmark_tile_sort.py +155 -0
  130. warp/examples/browse.py +37 -0
  131. warp/examples/core/example_cupy.py +86 -0
  132. warp/examples/core/example_dem.py +241 -0
  133. warp/examples/core/example_fluid.py +299 -0
  134. warp/examples/core/example_graph_capture.py +150 -0
  135. warp/examples/core/example_marching_cubes.py +195 -0
  136. warp/examples/core/example_mesh.py +180 -0
  137. warp/examples/core/example_mesh_intersect.py +211 -0
  138. warp/examples/core/example_nvdb.py +182 -0
  139. warp/examples/core/example_raycast.py +111 -0
  140. warp/examples/core/example_raymarch.py +205 -0
  141. warp/examples/core/example_render_opengl.py +290 -0
  142. warp/examples/core/example_sample_mesh.py +300 -0
  143. warp/examples/core/example_sph.py +411 -0
  144. warp/examples/core/example_spin_lock.py +93 -0
  145. warp/examples/core/example_torch.py +211 -0
  146. warp/examples/core/example_wave.py +269 -0
  147. warp/examples/core/example_work_queue.py +118 -0
  148. warp/examples/distributed/example_jacobi_mpi.py +506 -0
  149. warp/examples/fem/example_adaptive_grid.py +286 -0
  150. warp/examples/fem/example_apic_fluid.py +469 -0
  151. warp/examples/fem/example_burgers.py +261 -0
  152. warp/examples/fem/example_convection_diffusion.py +181 -0
  153. warp/examples/fem/example_convection_diffusion_dg.py +225 -0
  154. warp/examples/fem/example_darcy_ls_optimization.py +489 -0
  155. warp/examples/fem/example_deformed_geometry.py +172 -0
  156. warp/examples/fem/example_diffusion.py +196 -0
  157. warp/examples/fem/example_diffusion_3d.py +225 -0
  158. warp/examples/fem/example_diffusion_mgpu.py +225 -0
  159. warp/examples/fem/example_distortion_energy.py +228 -0
  160. warp/examples/fem/example_elastic_shape_optimization.py +387 -0
  161. warp/examples/fem/example_magnetostatics.py +242 -0
  162. warp/examples/fem/example_mixed_elasticity.py +293 -0
  163. warp/examples/fem/example_navier_stokes.py +263 -0
  164. warp/examples/fem/example_nonconforming_contact.py +300 -0
  165. warp/examples/fem/example_stokes.py +213 -0
  166. warp/examples/fem/example_stokes_transfer.py +262 -0
  167. warp/examples/fem/example_streamlines.py +357 -0
  168. warp/examples/fem/utils.py +1047 -0
  169. warp/examples/interop/example_jax_callable.py +146 -0
  170. warp/examples/interop/example_jax_ffi_callback.py +132 -0
  171. warp/examples/interop/example_jax_kernel.py +232 -0
  172. warp/examples/optim/example_diffray.py +561 -0
  173. warp/examples/optim/example_fluid_checkpoint.py +497 -0
  174. warp/examples/tile/example_tile_block_cholesky.py +502 -0
  175. warp/examples/tile/example_tile_cholesky.py +88 -0
  176. warp/examples/tile/example_tile_convolution.py +66 -0
  177. warp/examples/tile/example_tile_fft.py +55 -0
  178. warp/examples/tile/example_tile_filtering.py +113 -0
  179. warp/examples/tile/example_tile_matmul.py +85 -0
  180. warp/examples/tile/example_tile_mcgp.py +191 -0
  181. warp/examples/tile/example_tile_mlp.py +385 -0
  182. warp/examples/tile/example_tile_nbody.py +199 -0
  183. warp/fabric.py +24 -0
  184. warp/fem/__init__.py +173 -0
  185. warp/fem/adaptivity.py +26 -0
  186. warp/fem/cache.py +30 -0
  187. warp/fem/dirichlet.py +24 -0
  188. warp/fem/field/__init__.py +24 -0
  189. warp/fem/field/field.py +26 -0
  190. warp/fem/geometry/__init__.py +21 -0
  191. warp/fem/geometry/closest_point.py +31 -0
  192. warp/fem/linalg.py +38 -0
  193. warp/fem/operator.py +32 -0
  194. warp/fem/polynomial.py +29 -0
  195. warp/fem/space/__init__.py +22 -0
  196. warp/fem/space/basis_space.py +24 -0
  197. warp/fem/space/shape/__init__.py +68 -0
  198. warp/fem/space/topology.py +24 -0
  199. warp/fem/types.py +24 -0
  200. warp/fem/utils.py +32 -0
  201. warp/jax.py +29 -0
  202. warp/jax_experimental/__init__.py +29 -0
  203. warp/jax_experimental/custom_call.py +29 -0
  204. warp/jax_experimental/ffi.py +39 -0
  205. warp/jax_experimental/xla_ffi.py +24 -0
  206. warp/marching_cubes.py +24 -0
  207. warp/math.py +37 -0
  208. warp/native/array.h +1687 -0
  209. warp/native/builtin.h +2327 -0
  210. warp/native/bvh.cpp +562 -0
  211. warp/native/bvh.cu +826 -0
  212. warp/native/bvh.h +555 -0
  213. warp/native/clang/clang.cpp +541 -0
  214. warp/native/coloring.cpp +622 -0
  215. warp/native/crt.cpp +51 -0
  216. warp/native/crt.h +568 -0
  217. warp/native/cuda_crt.h +1058 -0
  218. warp/native/cuda_util.cpp +677 -0
  219. warp/native/cuda_util.h +313 -0
  220. warp/native/error.cpp +77 -0
  221. warp/native/error.h +36 -0
  222. warp/native/exports.h +2023 -0
  223. warp/native/fabric.h +246 -0
  224. warp/native/hashgrid.cpp +311 -0
  225. warp/native/hashgrid.cu +89 -0
  226. warp/native/hashgrid.h +240 -0
  227. warp/native/initializer_array.h +41 -0
  228. warp/native/intersect.h +1253 -0
  229. warp/native/intersect_adj.h +375 -0
  230. warp/native/intersect_tri.h +348 -0
  231. warp/native/mat.h +5189 -0
  232. warp/native/mathdx.cpp +93 -0
  233. warp/native/matnn.h +221 -0
  234. warp/native/mesh.cpp +266 -0
  235. warp/native/mesh.cu +406 -0
  236. warp/native/mesh.h +2097 -0
  237. warp/native/nanovdb/GridHandle.h +533 -0
  238. warp/native/nanovdb/HostBuffer.h +591 -0
  239. warp/native/nanovdb/NanoVDB.h +6246 -0
  240. warp/native/nanovdb/NodeManager.h +323 -0
  241. warp/native/nanovdb/PNanoVDB.h +3390 -0
  242. warp/native/noise.h +859 -0
  243. warp/native/quat.h +1664 -0
  244. warp/native/rand.h +342 -0
  245. warp/native/range.h +145 -0
  246. warp/native/reduce.cpp +174 -0
  247. warp/native/reduce.cu +363 -0
  248. warp/native/runlength_encode.cpp +79 -0
  249. warp/native/runlength_encode.cu +61 -0
  250. warp/native/scan.cpp +47 -0
  251. warp/native/scan.cu +55 -0
  252. warp/native/scan.h +23 -0
  253. warp/native/solid_angle.h +466 -0
  254. warp/native/sort.cpp +251 -0
  255. warp/native/sort.cu +286 -0
  256. warp/native/sort.h +35 -0
  257. warp/native/sparse.cpp +241 -0
  258. warp/native/sparse.cu +435 -0
  259. warp/native/spatial.h +1306 -0
  260. warp/native/svd.h +727 -0
  261. warp/native/temp_buffer.h +46 -0
  262. warp/native/tile.h +4124 -0
  263. warp/native/tile_radix_sort.h +1112 -0
  264. warp/native/tile_reduce.h +838 -0
  265. warp/native/tile_scan.h +240 -0
  266. warp/native/tuple.h +189 -0
  267. warp/native/vec.h +2199 -0
  268. warp/native/version.h +23 -0
  269. warp/native/volume.cpp +501 -0
  270. warp/native/volume.cu +68 -0
  271. warp/native/volume.h +970 -0
  272. warp/native/volume_builder.cu +483 -0
  273. warp/native/volume_builder.h +52 -0
  274. warp/native/volume_impl.h +70 -0
  275. warp/native/warp.cpp +1143 -0
  276. warp/native/warp.cu +4604 -0
  277. warp/native/warp.h +358 -0
  278. warp/optim/__init__.py +20 -0
  279. warp/optim/adam.py +24 -0
  280. warp/optim/linear.py +35 -0
  281. warp/optim/sgd.py +24 -0
  282. warp/paddle.py +24 -0
  283. warp/py.typed +0 -0
  284. warp/render/__init__.py +22 -0
  285. warp/render/imgui_manager.py +29 -0
  286. warp/render/render_opengl.py +24 -0
  287. warp/render/render_usd.py +24 -0
  288. warp/render/utils.py +24 -0
  289. warp/sparse.py +51 -0
  290. warp/tape.py +24 -0
  291. warp/tests/__init__.py +1 -0
  292. warp/tests/__main__.py +4 -0
  293. warp/tests/assets/curlnoise_golden.npy +0 -0
  294. warp/tests/assets/mlp_golden.npy +0 -0
  295. warp/tests/assets/pixel.npy +0 -0
  296. warp/tests/assets/pnoise_golden.npy +0 -0
  297. warp/tests/assets/spiky.usd +0 -0
  298. warp/tests/assets/test_grid.nvdb +0 -0
  299. warp/tests/assets/test_index_grid.nvdb +0 -0
  300. warp/tests/assets/test_int32_grid.nvdb +0 -0
  301. warp/tests/assets/test_vec_grid.nvdb +0 -0
  302. warp/tests/assets/torus.nvdb +0 -0
  303. warp/tests/assets/torus.usda +105 -0
  304. warp/tests/aux_test_class_kernel.py +34 -0
  305. warp/tests/aux_test_compile_consts_dummy.py +18 -0
  306. warp/tests/aux_test_conditional_unequal_types_kernels.py +29 -0
  307. warp/tests/aux_test_dependent.py +29 -0
  308. warp/tests/aux_test_grad_customs.py +29 -0
  309. warp/tests/aux_test_instancing_gc.py +26 -0
  310. warp/tests/aux_test_module_aot.py +7 -0
  311. warp/tests/aux_test_module_unload.py +23 -0
  312. warp/tests/aux_test_name_clash1.py +40 -0
  313. warp/tests/aux_test_name_clash2.py +40 -0
  314. warp/tests/aux_test_reference.py +9 -0
  315. warp/tests/aux_test_reference_reference.py +8 -0
  316. warp/tests/aux_test_square.py +16 -0
  317. warp/tests/aux_test_unresolved_func.py +22 -0
  318. warp/tests/aux_test_unresolved_symbol.py +22 -0
  319. warp/tests/cuda/__init__.py +0 -0
  320. warp/tests/cuda/test_async.py +676 -0
  321. warp/tests/cuda/test_conditional_captures.py +1147 -0
  322. warp/tests/cuda/test_ipc.py +124 -0
  323. warp/tests/cuda/test_mempool.py +233 -0
  324. warp/tests/cuda/test_multigpu.py +169 -0
  325. warp/tests/cuda/test_peer.py +139 -0
  326. warp/tests/cuda/test_pinned.py +84 -0
  327. warp/tests/cuda/test_streams.py +691 -0
  328. warp/tests/geometry/__init__.py +0 -0
  329. warp/tests/geometry/test_bvh.py +335 -0
  330. warp/tests/geometry/test_hash_grid.py +259 -0
  331. warp/tests/geometry/test_marching_cubes.py +294 -0
  332. warp/tests/geometry/test_mesh.py +318 -0
  333. warp/tests/geometry/test_mesh_query_aabb.py +392 -0
  334. warp/tests/geometry/test_mesh_query_point.py +935 -0
  335. warp/tests/geometry/test_mesh_query_ray.py +323 -0
  336. warp/tests/geometry/test_volume.py +1103 -0
  337. warp/tests/geometry/test_volume_write.py +346 -0
  338. warp/tests/interop/__init__.py +0 -0
  339. warp/tests/interop/test_dlpack.py +730 -0
  340. warp/tests/interop/test_jax.py +1673 -0
  341. warp/tests/interop/test_paddle.py +800 -0
  342. warp/tests/interop/test_torch.py +1001 -0
  343. warp/tests/run_coverage_serial.py +39 -0
  344. warp/tests/test_adam.py +162 -0
  345. warp/tests/test_arithmetic.py +1096 -0
  346. warp/tests/test_array.py +3756 -0
  347. warp/tests/test_array_reduce.py +156 -0
  348. warp/tests/test_assert.py +303 -0
  349. warp/tests/test_atomic.py +336 -0
  350. warp/tests/test_atomic_bitwise.py +209 -0
  351. warp/tests/test_atomic_cas.py +312 -0
  352. warp/tests/test_bool.py +220 -0
  353. warp/tests/test_builtins_resolution.py +732 -0
  354. warp/tests/test_closest_point_edge_edge.py +327 -0
  355. warp/tests/test_codegen.py +974 -0
  356. warp/tests/test_codegen_instancing.py +1495 -0
  357. warp/tests/test_compile_consts.py +215 -0
  358. warp/tests/test_conditional.py +298 -0
  359. warp/tests/test_context.py +35 -0
  360. warp/tests/test_copy.py +319 -0
  361. warp/tests/test_ctypes.py +618 -0
  362. warp/tests/test_dense.py +73 -0
  363. warp/tests/test_devices.py +127 -0
  364. warp/tests/test_enum.py +136 -0
  365. warp/tests/test_examples.py +424 -0
  366. warp/tests/test_fabricarray.py +998 -0
  367. warp/tests/test_fast_math.py +72 -0
  368. warp/tests/test_fem.py +2204 -0
  369. warp/tests/test_fixedarray.py +229 -0
  370. warp/tests/test_fp16.py +136 -0
  371. warp/tests/test_func.py +501 -0
  372. warp/tests/test_future_annotations.py +100 -0
  373. warp/tests/test_generics.py +656 -0
  374. warp/tests/test_grad.py +893 -0
  375. warp/tests/test_grad_customs.py +339 -0
  376. warp/tests/test_grad_debug.py +341 -0
  377. warp/tests/test_implicit_init.py +411 -0
  378. warp/tests/test_import.py +45 -0
  379. warp/tests/test_indexedarray.py +1140 -0
  380. warp/tests/test_intersect.py +103 -0
  381. warp/tests/test_iter.py +76 -0
  382. warp/tests/test_large.py +177 -0
  383. warp/tests/test_launch.py +411 -0
  384. warp/tests/test_lerp.py +151 -0
  385. warp/tests/test_linear_solvers.py +223 -0
  386. warp/tests/test_lvalue.py +427 -0
  387. warp/tests/test_map.py +526 -0
  388. warp/tests/test_mat.py +3515 -0
  389. warp/tests/test_mat_assign_copy.py +178 -0
  390. warp/tests/test_mat_constructors.py +573 -0
  391. warp/tests/test_mat_lite.py +122 -0
  392. warp/tests/test_mat_scalar_ops.py +2913 -0
  393. warp/tests/test_math.py +212 -0
  394. warp/tests/test_module_aot.py +287 -0
  395. warp/tests/test_module_hashing.py +258 -0
  396. warp/tests/test_modules_lite.py +70 -0
  397. warp/tests/test_noise.py +252 -0
  398. warp/tests/test_operators.py +299 -0
  399. warp/tests/test_options.py +129 -0
  400. warp/tests/test_overwrite.py +551 -0
  401. warp/tests/test_print.py +408 -0
  402. warp/tests/test_quat.py +2653 -0
  403. warp/tests/test_quat_assign_copy.py +145 -0
  404. warp/tests/test_rand.py +339 -0
  405. warp/tests/test_reload.py +303 -0
  406. warp/tests/test_rounding.py +157 -0
  407. warp/tests/test_runlength_encode.py +196 -0
  408. warp/tests/test_scalar_ops.py +133 -0
  409. warp/tests/test_smoothstep.py +108 -0
  410. warp/tests/test_snippet.py +318 -0
  411. warp/tests/test_sparse.py +845 -0
  412. warp/tests/test_spatial.py +2859 -0
  413. warp/tests/test_spatial_assign_copy.py +160 -0
  414. warp/tests/test_special_values.py +361 -0
  415. warp/tests/test_static.py +640 -0
  416. warp/tests/test_struct.py +901 -0
  417. warp/tests/test_tape.py +242 -0
  418. warp/tests/test_transient_module.py +93 -0
  419. warp/tests/test_triangle_closest_point.py +192 -0
  420. warp/tests/test_tuple.py +361 -0
  421. warp/tests/test_types.py +615 -0
  422. warp/tests/test_utils.py +594 -0
  423. warp/tests/test_vec.py +1408 -0
  424. warp/tests/test_vec_assign_copy.py +143 -0
  425. warp/tests/test_vec_constructors.py +325 -0
  426. warp/tests/test_vec_lite.py +80 -0
  427. warp/tests/test_vec_scalar_ops.py +2327 -0
  428. warp/tests/test_verify_fp.py +100 -0
  429. warp/tests/test_version.py +75 -0
  430. warp/tests/tile/__init__.py +0 -0
  431. warp/tests/tile/test_tile.py +1519 -0
  432. warp/tests/tile/test_tile_atomic_bitwise.py +403 -0
  433. warp/tests/tile/test_tile_cholesky.py +608 -0
  434. warp/tests/tile/test_tile_load.py +724 -0
  435. warp/tests/tile/test_tile_mathdx.py +156 -0
  436. warp/tests/tile/test_tile_matmul.py +179 -0
  437. warp/tests/tile/test_tile_mlp.py +400 -0
  438. warp/tests/tile/test_tile_reduce.py +950 -0
  439. warp/tests/tile/test_tile_shared_memory.py +376 -0
  440. warp/tests/tile/test_tile_sort.py +121 -0
  441. warp/tests/tile/test_tile_view.py +173 -0
  442. warp/tests/unittest_serial.py +47 -0
  443. warp/tests/unittest_suites.py +430 -0
  444. warp/tests/unittest_utils.py +469 -0
  445. warp/tests/walkthrough_debug.py +95 -0
  446. warp/torch.py +24 -0
  447. warp/types.py +51 -0
  448. warp/utils.py +31 -0
  449. warp_lang-1.10.0.dist-info/METADATA +459 -0
  450. warp_lang-1.10.0.dist-info/RECORD +468 -0
  451. warp_lang-1.10.0.dist-info/WHEEL +5 -0
  452. warp_lang-1.10.0.dist-info/licenses/LICENSE.md +176 -0
  453. warp_lang-1.10.0.dist-info/licenses/licenses/Gaia-LICENSE.txt +6 -0
  454. warp_lang-1.10.0.dist-info/licenses/licenses/appdirs-LICENSE.txt +22 -0
  455. warp_lang-1.10.0.dist-info/licenses/licenses/asset_pixel_jpg-LICENSE.txt +3 -0
  456. warp_lang-1.10.0.dist-info/licenses/licenses/cuda-LICENSE.txt +1582 -0
  457. warp_lang-1.10.0.dist-info/licenses/licenses/dlpack-LICENSE.txt +201 -0
  458. warp_lang-1.10.0.dist-info/licenses/licenses/fp16-LICENSE.txt +28 -0
  459. warp_lang-1.10.0.dist-info/licenses/licenses/libmathdx-LICENSE.txt +220 -0
  460. warp_lang-1.10.0.dist-info/licenses/licenses/llvm-LICENSE.txt +279 -0
  461. warp_lang-1.10.0.dist-info/licenses/licenses/moller-LICENSE.txt +16 -0
  462. warp_lang-1.10.0.dist-info/licenses/licenses/nanovdb-LICENSE.txt +2 -0
  463. warp_lang-1.10.0.dist-info/licenses/licenses/nvrtc-LICENSE.txt +1592 -0
  464. warp_lang-1.10.0.dist-info/licenses/licenses/svd-LICENSE.txt +23 -0
  465. warp_lang-1.10.0.dist-info/licenses/licenses/unittest_parallel-LICENSE.txt +21 -0
  466. warp_lang-1.10.0.dist-info/licenses/licenses/usd-LICENSE.txt +213 -0
  467. warp_lang-1.10.0.dist-info/licenses/licenses/windingnumber-LICENSE.txt +21 -0
  468. warp_lang-1.10.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2859 @@
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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
59
+ vec3 = wp._src.types.vector(length=3, dtype=wptype)
60
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
144
+ spatial_vector = wp._src.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
+ elem = 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) * elem[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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
196
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
266
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
342
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
394
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
548
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
617
+ vec3 = wp._src.types.vector(length=3, dtype=wptype)
618
+ transform = wp._src.types.transformation(dtype=wptype)
619
+ quat = wp._src.types.quaternion(dtype=wptype)
620
+
621
+ def check_transform_constructor(
622
+ input: wp.array(dtype=wptype),
623
+ out: wp.array(dtype=wptype),
624
+ ):
625
+ a = transform(vec3(input[0], input[1], input[2]), quat(input[3], input[4], input[5], input[6]))
626
+ b = transform(input[0], input[1], input[2], input[3], input[4], input[5], input[6])
627
+
628
+ out[0] = a[0] + b[0]
629
+ out[1] = a[1] + b[1]
630
+ out[2] = a[2] + b[2]
631
+ out[3] = a[3] + b[3]
632
+ out[4] = a[4] + b[4]
633
+ out[5] = a[5] + b[5]
634
+ out[6] = a[6] + b[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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
676
+ transform = wp._src.types.transformation(dtype=wptype)
677
+
678
+ def check_transform_indexing(
679
+ input: wp.array(dtype=transform),
680
+ out: wp.array(dtype=wptype),
681
+ ):
682
+ elem = 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) * elem[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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
725
+ transform = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
793
+ transform = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
870
+ transform = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
940
+ transform = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1049
+ transform = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1142
+ transform = wp._src.types.transformation(dtype=wptype)
1143
+ vec3 = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1250
+ spatial_matrix = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1346
+ spatial_matrix = wp._src.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
+ elem = 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) * elem[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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1401
+ spatial_matrix = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1477
+ spatial_matrix = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1565
+ spatial_matrix = wp._src.types.matrix(shape=(6, 6), dtype=wptype)
1566
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1626
+ spatial_matrix = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1693
+ spatial_matrix = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1750
+ spatial_vector = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1830
+ mat3 = wp._src.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._src.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._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
1974
+ mat44 = wp._src.types.matrix((4, 4), wptype)
1975
+ vec3 = wp._src.types.vector(3, wptype)
1976
+ quat = wp._src.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._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
2003
+ mat44 = wp._src.types.matrix((4, 4), wptype)
2004
+ vec3 = wp._src.types.vector(3, wptype)
2005
+ quat = wp._src.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
+ def test_transform_compose(test, device, dtype, register_kernels=False):
2034
+ wptype = wp._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
2035
+ mat44 = wp._src.types.matrix((4, 4), wptype)
2036
+ vec3 = wp._src.types.vector(3, wptype)
2037
+ quat = wp._src.types.quaternion(wptype)
2038
+
2039
+ def transform_compose_kernel():
2040
+ p = vec3(wptype(1.0), wptype(2.0), wptype(3.0))
2041
+ q = quat(wptype(-0.4), wptype(0.2), wptype(-0.4), wptype(0.8))
2042
+ s = vec3(wptype(4.0), wptype(5.0), wptype(6.0))
2043
+ m = wp.transform_compose(p, q, s)
2044
+ # fmt: off
2045
+ wp.expect_near(
2046
+ m,
2047
+ mat44(
2048
+ wptype(0.6 * 4.0), wptype(0.48 * 5.0), wptype(0.64 * 6.0), wptype(1.0),
2049
+ wptype(-0.8 * 4.0), wptype(0.36 * 5.0), wptype(0.48 * 6.0), wptype(2.0),
2050
+ wptype(0.0 * 4.0), wptype(-0.8 * 5.0), wptype(0.6 * 6.0), wptype(3.0),
2051
+ wptype(0.0), wptype(0.0), wptype(0.0), wptype(1.0),
2052
+ ),
2053
+ tolerance=wptype(1e-2),
2054
+ )
2055
+ # fmt: on
2056
+
2057
+ kernel = getkernel(transform_compose_kernel, suffix=dtype.__name__)
2058
+
2059
+ if register_kernels:
2060
+ return
2061
+
2062
+ wp.launch(kernel, dim=1, device=device)
2063
+
2064
+
2065
+ def test_transform_decompose(test, device, dtype, register_kernels=False):
2066
+ wptype = wp._src.types.np_dtype_to_warp_type[np.dtype(dtype)]
2067
+ mat44 = wp._src.types.matrix((4, 4), wptype)
2068
+ vec3 = wp._src.types.vector(3, wptype)
2069
+ quat = wp._src.types.quaternion(wptype)
2070
+
2071
+ def transform_decompose_kernel():
2072
+ # fmt: off
2073
+ m = mat44(
2074
+ wptype(0.6 * 4.0), wptype(0.48 * 5.0), wptype(0.64 * 6.0), wptype(1.0),
2075
+ wptype(-0.8 * 4.0), wptype(0.36 * 5.0), wptype(0.48 * 6.0), wptype(2.0),
2076
+ wptype(0.0 * 4.0), wptype(-0.8 * 5.0), wptype(0.6 * 6.0), wptype(3.0),
2077
+ wptype(0.0), wptype(0.0), wptype(0.0), wptype(1.0),
2078
+ )
2079
+ # fmt: on
2080
+ p, q, s = wp.transform_decompose(m)
2081
+ wp.expect_near(p, vec3(wptype(1.0), wptype(2.0), wptype(3.0)), tolerance=wptype(1e-2))
2082
+ wp.expect_near(q, quat(wptype(-0.4), wptype(0.2), wptype(-0.4), wptype(0.8)), tolerance=wptype(1e-2))
2083
+ wp.expect_near(s, vec3(wptype(4.0), wptype(5.0), wptype(6.0)), tolerance=wptype(1e-2))
2084
+
2085
+ kernel = getkernel(transform_decompose_kernel, suffix=dtype.__name__)
2086
+
2087
+ if register_kernels:
2088
+ return
2089
+
2090
+ wp.launch(kernel, dim=1, device=device)
2091
+
2092
+
2093
+ def test_transform_getter_setter(test, device):
2094
+ t = wp.transform()
2095
+
2096
+ a = wp.vec3(1.0, 2.0, 3.0)
2097
+ b = wp.quat(0.0, 0.0, 0.0, 1.0)
2098
+
2099
+ t.p = a
2100
+ t.q = b
2101
+
2102
+ c = t.p
2103
+ d = t.q
2104
+
2105
+ test.assertEqual(c, a)
2106
+ test.assertEqual(d, b)
2107
+
2108
+
2109
+ @wp.kernel
2110
+ def transform_extract_subscript(x: wp.array(dtype=wp.transform), y: wp.array(dtype=float)):
2111
+ tid = wp.tid()
2112
+
2113
+ a = x[tid]
2114
+ b = a[0] + 2.0 * a[1] + 3.0 * a[2] + 4.0 * a[3] + 5.0 * a[4] + 6.0 * a[5] + 7.0 * a[6]
2115
+ y[tid] = b
2116
+
2117
+
2118
+ @wp.kernel
2119
+ def transform_extract_attribute(
2120
+ x: wp.array(dtype=wp.transform), y: wp.array(dtype=wp.vec3), z: wp.array(dtype=wp.quat)
2121
+ ):
2122
+ tid = wp.tid()
2123
+
2124
+ a = x[tid]
2125
+ p = a.p
2126
+ q = a.q
2127
+
2128
+ y[tid] = p
2129
+ z[tid] = q
2130
+
2131
+
2132
+ def test_transform_extract(test, device):
2133
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2134
+ y = wp.zeros(1, dtype=float, requires_grad=True, device=device)
2135
+
2136
+ tape = wp.Tape()
2137
+ with tape:
2138
+ wp.launch(transform_extract_subscript, 1, inputs=[x], outputs=[y], device=device)
2139
+
2140
+ y.grad = wp.ones_like(y)
2141
+
2142
+ tape.backward()
2143
+
2144
+ assert_np_equal(y.numpy(), np.array([28.0], dtype=float))
2145
+ assert_np_equal(x.grad.numpy(), np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]], dtype=float))
2146
+
2147
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2148
+ y = wp.zeros(1, dtype=wp.vec3, requires_grad=True, device=device)
2149
+ z = wp.zeros(1, dtype=wp.quat, requires_grad=True, device=device)
2150
+
2151
+ tape = wp.Tape()
2152
+ with tape:
2153
+ wp.launch(transform_extract_attribute, 1, inputs=[x], outputs=[y, z], device=device)
2154
+
2155
+ y.grad = wp.ones_like(y)
2156
+ z.grad = wp.ones_like(z)
2157
+
2158
+ tape.backward()
2159
+
2160
+ assert_np_equal(y.numpy(), np.array([[1.0, 1.0, 1.0]], dtype=float))
2161
+ assert_np_equal(z.numpy(), np.array([[1.0, 1.0, 1.0, 1.0]], dtype=float))
2162
+ assert_np_equal(x.grad.numpy(), np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], dtype=float))
2163
+
2164
+
2165
+ @wp.kernel
2166
+ def transform_assign_subscript(x: wp.array(dtype=float), y: wp.array(dtype=wp.transform)):
2167
+ i = wp.tid()
2168
+
2169
+ a = wp.transform()
2170
+ a[0] = 1.0 * x[i]
2171
+ a[1] = 2.0 * x[i]
2172
+ a[2] = 3.0 * x[i]
2173
+ a[3] = 4.0 * x[i]
2174
+ a[4] = 5.0 * x[i]
2175
+ a[5] = 6.0 * x[i]
2176
+ a[6] = 7.0 * x[i]
2177
+ y[i] = a
2178
+
2179
+
2180
+ @wp.kernel
2181
+ def transform_assign_attribute(x: wp.array(dtype=wp.vec3), y: wp.array(dtype=wp.quat), z: wp.array(dtype=wp.transform)):
2182
+ i = wp.tid()
2183
+
2184
+ a = wp.transform()
2185
+ a.p = x[i]
2186
+ a.q = y[i]
2187
+ z[i] = a
2188
+
2189
+
2190
+ def test_transform_assign(test, device):
2191
+ x = wp.ones(1, dtype=float, requires_grad=True, device=device)
2192
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2193
+
2194
+ tape = wp.Tape()
2195
+ with tape:
2196
+ wp.launch(transform_assign_subscript, 1, inputs=[x], outputs=[y], device=device)
2197
+
2198
+ y.grad = wp.ones_like(y)
2199
+ tape.backward()
2200
+
2201
+ assert_np_equal(y.numpy(), np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]], dtype=float))
2202
+ assert_np_equal(x.grad.numpy(), np.array([28.0], dtype=float))
2203
+
2204
+ x = wp.ones(1, dtype=wp.vec3, requires_grad=True, device=device)
2205
+ y = wp.ones(1, dtype=wp.quat, requires_grad=True, device=device)
2206
+ z = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2207
+
2208
+ tape = wp.Tape()
2209
+ with tape:
2210
+ wp.launch(transform_assign_attribute, 1, inputs=[x, y], outputs=[z], device=device)
2211
+
2212
+ z.grad = wp.ones_like(z)
2213
+ tape.backward()
2214
+
2215
+ assert_np_equal(z.numpy(), np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], dtype=float))
2216
+ assert_np_equal(x.grad.numpy(), np.array([[1.0, 1.0, 1.0]], dtype=float))
2217
+ assert_np_equal(y.grad.numpy(), np.array([[1.0, 1.0, 1.0, 1.0]], dtype=float))
2218
+
2219
+
2220
+ @wp.kernel
2221
+ def transform_array_extract_subscript(x: wp.array2d(dtype=wp.transform), y: wp.array2d(dtype=float)):
2222
+ i, j = wp.tid()
2223
+ a = x[i, j][0]
2224
+ b = x[i, j][1]
2225
+ c = x[i, j][2]
2226
+ d = x[i, j][3]
2227
+ e = x[i, j][4]
2228
+ f = x[i, j][5]
2229
+ g = x[i, j][6]
2230
+ y[i, j] = 1.0 * a + 2.0 * b + 3.0 * c + 4.0 * d + 5.0 * e + 6.0 * f + 7.0 * g
2231
+
2232
+
2233
+ @wp.kernel
2234
+ def transform_array_extract_attribute(
2235
+ x: wp.array2d(dtype=wp.transform), y: wp.array2d(dtype=wp.vec3), z: wp.array2d(dtype=wp.quat)
2236
+ ):
2237
+ i, j = wp.tid()
2238
+ a = x[i, j].p
2239
+ b = x[i, j].q
2240
+ y[i, j] = a
2241
+ z[i, j] = b
2242
+
2243
+
2244
+ def test_transform_array_extract(test, device):
2245
+ x = wp.ones((1, 1), dtype=wp.transform, requires_grad=True, device=device)
2246
+ y = wp.zeros((1, 1), dtype=float, requires_grad=True, device=device)
2247
+
2248
+ tape = wp.Tape()
2249
+ with tape:
2250
+ wp.launch(transform_array_extract_subscript, (1, 1), inputs=[x], outputs=[y], device=device)
2251
+
2252
+ y.grad = wp.ones_like(y)
2253
+ tape.backward()
2254
+
2255
+ assert_np_equal(y.numpy(), np.array([[28.0]], dtype=float))
2256
+ assert_np_equal(x.grad.numpy(), np.array([[[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]]], dtype=float))
2257
+
2258
+ x = wp.ones((1, 1), dtype=wp.transform, requires_grad=True, device=device)
2259
+ y = wp.zeros((1, 1), dtype=wp.vec3, requires_grad=True, device=device)
2260
+ z = wp.zeros((1, 1), dtype=wp.quat, requires_grad=True, device=device)
2261
+
2262
+ tape = wp.Tape()
2263
+ with tape:
2264
+ wp.launch(transform_array_extract_attribute, (1, 1), inputs=[x], outputs=[y, z], device=device)
2265
+
2266
+ y.grad = wp.ones_like(y)
2267
+ z.grad = wp.ones_like(z)
2268
+ tape.backward()
2269
+
2270
+ assert_np_equal(y.numpy(), np.array([[[1.0, 1.0, 1.0]]], dtype=float))
2271
+ assert_np_equal(z.numpy(), np.array([[[1.0, 1.0, 1.0, 1.0]]], dtype=float))
2272
+ assert_np_equal(x.grad.numpy(), np.array([[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]], dtype=float))
2273
+
2274
+
2275
+ @wp.kernel
2276
+ def transform_array_assign_subscript(x: wp.array2d(dtype=float), y: wp.array2d(dtype=wp.transform)):
2277
+ i, j = wp.tid()
2278
+
2279
+ y[i, j][0] = 1.0 * x[i, j]
2280
+ y[i, j][1] = 2.0 * x[i, j]
2281
+ y[i, j][2] = 3.0 * x[i, j]
2282
+ y[i, j][3] = 4.0 * x[i, j]
2283
+ y[i, j][4] = 5.0 * x[i, j]
2284
+ y[i, j][5] = 6.0 * x[i, j]
2285
+ y[i, j][6] = 7.0 * x[i, j]
2286
+
2287
+
2288
+ # @wp.kernel
2289
+ # def transform_array_assign_attribute(x: wp.array2d(dtype=wp.vec3), y: wp.array2d(dtype=wp.quat), z: wp.array2d(dtype=wp.transform)):
2290
+ # i, j = wp.tid()
2291
+
2292
+ # z[i, j].p = x[i, j]
2293
+ # z[i, j].q = y[i, j]
2294
+
2295
+
2296
+ def test_transform_array_assign(test, device):
2297
+ x = wp.ones((1, 1), dtype=float, requires_grad=True, device=device)
2298
+ y = wp.zeros((1, 1), dtype=wp.transform, requires_grad=True, device=device)
2299
+
2300
+ tape = wp.Tape()
2301
+ with tape:
2302
+ wp.launch(transform_array_assign_subscript, (1, 1), inputs=[x], outputs=[y], device=device)
2303
+
2304
+ assert_np_equal(y.numpy(), np.array([[[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]]], dtype=float))
2305
+ # TODO: gradient propagation for in-place array assignment
2306
+
2307
+ x = wp.ones((1, 1), dtype=wp.vec3, requires_grad=True, device=device)
2308
+ y = wp.ones((1, 1), dtype=wp.quat, requires_grad=True, device=device)
2309
+ z = wp.zeros((1, 1), dtype=wp.transform, requires_grad=True, device=device)
2310
+
2311
+ # TODO: transform_array_assign_attribute
2312
+ # tape = wp.Tape()
2313
+ # with tape:
2314
+ # wp.launch(transform_array_assign_attribute, (1, 1), inputs=[x, y], outputs=[z], device=device)
2315
+
2316
+ # assert_np_equal(z.numpy(), np.array([[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]], dtype=float))
2317
+ # TODO: gradient propagation for in-place array assignment
2318
+
2319
+
2320
+ @wp.kernel
2321
+ def transform_add_inplace_subscript(x: wp.array(dtype=wp.transform), y: wp.array(dtype=wp.transform)):
2322
+ i = wp.tid()
2323
+
2324
+ a = wp.transform()
2325
+ b = x[i]
2326
+
2327
+ a[0] += 1.0 * b[0]
2328
+ a[1] += 2.0 * b[1]
2329
+ a[2] += 3.0 * b[2]
2330
+ a[3] += 4.0 * b[3]
2331
+ a[4] += 5.0 * b[4]
2332
+ a[5] += 6.0 * b[5]
2333
+ a[6] += 7.0 * b[6]
2334
+
2335
+ y[i] = a
2336
+
2337
+
2338
+ @wp.kernel
2339
+ def transform_add_inplace_attribute(x: wp.array(dtype=wp.transform), y: wp.array(dtype=wp.transform)):
2340
+ i = wp.tid()
2341
+
2342
+ a = wp.transform()
2343
+ b = x[i]
2344
+
2345
+ a.p += b.p
2346
+
2347
+ y[i] = a
2348
+
2349
+
2350
+ def test_transform_add_inplace(test, device):
2351
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2352
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2353
+
2354
+ tape = wp.Tape()
2355
+ with tape:
2356
+ wp.launch(transform_add_inplace_subscript, 1, inputs=[x], outputs=[y], device=device)
2357
+
2358
+ y.grad = wp.ones_like(y)
2359
+ tape.backward()
2360
+
2361
+ assert_np_equal(y.numpy(), np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]], dtype=float))
2362
+ assert_np_equal(x.grad.numpy(), np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]], dtype=float))
2363
+
2364
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2365
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2366
+
2367
+ tape = wp.Tape()
2368
+ with tape:
2369
+ wp.launch(transform_add_inplace_attribute, 1, inputs=[x], outputs=[y], device=device)
2370
+
2371
+ y.grad = wp.ones_like(y)
2372
+ tape.backward()
2373
+
2374
+ assert_np_equal(y.numpy(), np.array([[1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]], dtype=float))
2375
+ assert_np_equal(x.grad.numpy(), np.array([[1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]], dtype=float))
2376
+
2377
+
2378
+ @wp.kernel
2379
+ def transform_sub_inplace_subscript(x: wp.array(dtype=wp.transform), y: wp.array(dtype=wp.transform)):
2380
+ i = wp.tid()
2381
+
2382
+ a = wp.transform()
2383
+ b = x[i]
2384
+
2385
+ a[0] -= 1.0 * b[0]
2386
+ a[1] -= 2.0 * b[1]
2387
+ a[2] -= 3.0 * b[2]
2388
+ a[3] -= 4.0 * b[3]
2389
+ a[4] -= 5.0 * b[4]
2390
+ a[5] -= 6.0 * b[5]
2391
+ a[6] -= 7.0 * b[6]
2392
+
2393
+ y[i] = a
2394
+
2395
+
2396
+ @wp.kernel
2397
+ def transform_sub_inplace_attribute(x: wp.array(dtype=wp.transform), y: wp.array(dtype=wp.transform)):
2398
+ i = wp.tid()
2399
+
2400
+ a = wp.transform()
2401
+ b = x[i]
2402
+
2403
+ a.p -= b.p
2404
+
2405
+ y[i] = a
2406
+
2407
+
2408
+ def test_transform_sub_inplace(test, device):
2409
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2410
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2411
+
2412
+ tape = wp.Tape()
2413
+ with tape:
2414
+ wp.launch(transform_sub_inplace_subscript, 1, inputs=[x], outputs=[y], device=device)
2415
+
2416
+ y.grad = wp.ones_like(y)
2417
+ tape.backward()
2418
+
2419
+ assert_np_equal(y.numpy(), np.array([[-1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0]], dtype=float))
2420
+ assert_np_equal(x.grad.numpy(), np.array([[-1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0]], dtype=float))
2421
+
2422
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2423
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2424
+
2425
+ tape = wp.Tape()
2426
+ with tape:
2427
+ wp.launch(transform_sub_inplace_attribute, 1, inputs=[x], outputs=[y], device=device)
2428
+
2429
+ y.grad = wp.ones_like(y)
2430
+ tape.backward()
2431
+
2432
+ assert_np_equal(y.numpy(), np.array([[-1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0]], dtype=float))
2433
+ assert_np_equal(x.grad.numpy(), np.array([[-1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0]], dtype=float))
2434
+
2435
+
2436
+ @wp.kernel
2437
+ def transform_array_add_inplace(x: wp.array(dtype=wp.transform), y: wp.array(dtype=wp.transform)):
2438
+ i = wp.tid()
2439
+
2440
+ y[i] += x[i]
2441
+
2442
+
2443
+ def test_transform_array_add_inplace(test, device):
2444
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2445
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2446
+
2447
+ tape = wp.Tape()
2448
+ with tape:
2449
+ wp.launch(transform_array_add_inplace, 1, inputs=[x], outputs=[y], device=device)
2450
+
2451
+ y.grad = wp.ones_like(y)
2452
+ tape.backward()
2453
+
2454
+ assert_np_equal(y.numpy(), np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], dtype=float))
2455
+ assert_np_equal(x.grad.numpy(), np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], dtype=float))
2456
+
2457
+
2458
+ @wp.kernel
2459
+ def transform_array_sub_inplace(x: wp.array(dtype=wp.transform), y: wp.array(dtype=wp.transform)):
2460
+ i = wp.tid()
2461
+
2462
+ y[i] -= x[i]
2463
+
2464
+
2465
+ def test_transform_array_sub_inplace(test, device):
2466
+ x = wp.ones(1, dtype=wp.transform, requires_grad=True, device=device)
2467
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2468
+
2469
+ tape = wp.Tape()
2470
+ with tape:
2471
+ wp.launch(transform_array_sub_inplace, 1, inputs=[x], outputs=[y], device=device)
2472
+
2473
+ y.grad = wp.ones_like(y)
2474
+ tape.backward()
2475
+
2476
+ assert_np_equal(y.numpy(), np.array([[-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0]], dtype=float))
2477
+ assert_np_equal(x.grad.numpy(), np.array([[-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0]], dtype=float))
2478
+
2479
+
2480
+ def test_transform_indexing_assign(test, device):
2481
+ @wp.func
2482
+ def fn():
2483
+ t = wp.transform(p=wp.vec3(1.0, 2.0, 3.0), q=wp.quat(4.0, 5.0, 6.0, 7.0))
2484
+
2485
+ t[0] = 123.0
2486
+ t[3] *= 2.0
2487
+
2488
+ wp.expect_eq(t[0], 123.0)
2489
+ wp.expect_eq(t[1], 2.0)
2490
+ wp.expect_eq(t[2], 3.0)
2491
+ wp.expect_eq(t[3], 8.0)
2492
+ wp.expect_eq(t[4], 5.0)
2493
+ wp.expect_eq(t[5], 6.0)
2494
+ wp.expect_eq(t[6], 7.0)
2495
+
2496
+ t[-1] = 123.0
2497
+ t[-5] *= 2.0
2498
+
2499
+ wp.expect_eq(t[0], 123.0)
2500
+ wp.expect_eq(t[1], 2.0)
2501
+ wp.expect_eq(t[2], 6.0)
2502
+ wp.expect_eq(t[3], 8.0)
2503
+ wp.expect_eq(t[4], 5.0)
2504
+ wp.expect_eq(t[5], 6.0)
2505
+ wp.expect_eq(t[6], 123.0)
2506
+
2507
+ @wp.kernel(module="unique")
2508
+ def kernel():
2509
+ fn()
2510
+
2511
+ wp.launch(kernel, 1, device=device)
2512
+ wp.synchronize()
2513
+ fn()
2514
+
2515
+
2516
+ def test_transform_slicing_assign(test, device):
2517
+ vec0 = wp.vec(0, float)
2518
+ vec1 = wp.vec(1, float)
2519
+ vec2 = wp.vec(2, float)
2520
+ vec3 = wp.vec(3, float)
2521
+ vec4 = wp.vec(4, float)
2522
+ vec6 = wp.vec(6, float)
2523
+ vec7 = wp.vec(7, float)
2524
+
2525
+ @wp.func
2526
+ def fn():
2527
+ t = wp.transform(p=wp.vec3(1.0, 2.0, 3.0), q=wp.quat(4.0, 5.0, 6.0, 7.0))
2528
+
2529
+ wp.expect_eq(t[:] == vec7(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0), True)
2530
+ wp.expect_eq(t[-123:123] == vec7(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0), True)
2531
+ wp.expect_eq(t[123:] == vec0(), True)
2532
+ wp.expect_eq(t[:-123] == vec0(), True)
2533
+ wp.expect_eq(t[::123] == vec1(1.0), True)
2534
+
2535
+ wp.expect_eq(t[1:] == vec6(2.0, 3.0, 4.0, 5.0, 6.0, 7.0), True)
2536
+ wp.expect_eq(t[-2:] == vec2(6.0, 7.0), True)
2537
+ wp.expect_eq(t[:2] == vec2(1.0, 2.0), True)
2538
+ wp.expect_eq(t[:-1] == vec6(1.0, 2.0, 3.0, 4.0, 5.0, 6.0), True)
2539
+ wp.expect_eq(t[::2] == vec4(1.0, 3.0, 5.0, 7.0), True)
2540
+ wp.expect_eq(t[1::2] == vec3(2.0, 4.0, 6.0), True)
2541
+ wp.expect_eq(t[::-1] == vec7(7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0), True)
2542
+ wp.expect_eq(t[::-2] == vec4(7.0, 5.0, 3.0, 1.0), True)
2543
+ wp.expect_eq(t[1::-2] == vec1(2.0), True)
2544
+
2545
+ t[1:] = vec6(8.0, 9.0, 10.0, 11.0, 12.0, 13.0)
2546
+ wp.expect_eq(t == wp.transform(p=wp.vec3(1.0, 8.0, 9.0), q=wp.quat(10.0, 11.0, 12.0, 13.0)), True)
2547
+
2548
+ t[-2:] = vec2(14.0, 15.0)
2549
+ wp.expect_eq(t == wp.transform(p=wp.vec3(1.0, 8.0, 9.0), q=wp.quat(10.0, 11.0, 14.0, 15.0)), True)
2550
+
2551
+ t[:2] = vec2(16.0, 17.0)
2552
+ wp.expect_eq(t == wp.transform(p=wp.vec3(16.0, 17.0, 9.0), q=wp.quat(10.0, 11.0, 14.0, 15.0)), True)
2553
+
2554
+ t[:-1] = vec6(18.0, 19.0, 20.0, 21.0, 22.0, 23.0)
2555
+ wp.expect_eq(t == wp.transform(p=wp.vec3(18.0, 19.0, 20.0), q=wp.quat(21.0, 22.0, 23.0, 15.0)), True)
2556
+
2557
+ t[::2] = vec4(24.0, 25.0, 26.0, 27.0)
2558
+ wp.expect_eq(t == wp.transform(p=wp.vec3(24.0, 19.0, 25.0), q=wp.quat(21.0, 26.0, 23.0, 27.0)), True)
2559
+
2560
+ t[1::2] = vec3(28.0, 29.0, 30.0)
2561
+ wp.expect_eq(t == wp.transform(p=wp.vec3(24.0, 28.0, 25.0), q=wp.quat(29.0, 26.0, 30.0, 27.0)), True)
2562
+
2563
+ t[::-1] = vec7(31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0)
2564
+ wp.expect_eq(t == wp.transform(p=wp.vec3(37.0, 36.0, 35.0), q=wp.quat(34.0, 33.0, 32.0, 31.0)), True)
2565
+
2566
+ t[::-2] = vec4(38.0, 39.0, 40.0, 41.0)
2567
+ wp.expect_eq(t == wp.transform(p=wp.vec3(41.0, 36.0, 40.0), q=wp.quat(34.0, 39.0, 32.0, 38.0)), True)
2568
+
2569
+ t[1::-2] = vec1(42.0)
2570
+ wp.expect_eq(t == wp.transform(p=wp.vec3(41.0, 42.0, 40.0), q=wp.quat(34.0, 39.0, 32.0, 38.0)), True)
2571
+
2572
+ t[1:] += vec6(43.0, 44.0, 45.0, 46.0, 47.0, 48.0)
2573
+ wp.expect_eq(t == wp.transform(p=wp.vec3(41.0, 85.0, 84.0), q=wp.quat(79.0, 85.0, 79.0, 86.0)), True)
2574
+
2575
+ t[:-1] -= vec6(49.0, 50.0, 51.0, 52.0, 53.0, 54.0)
2576
+ wp.expect_eq(t == wp.transform(p=wp.vec3(-8.0, 35.0, 33.0), q=wp.quat(27.0, 32.0, 25.0, 86.0)), True)
2577
+
2578
+ @wp.kernel(module="unique")
2579
+ def kernel():
2580
+ fn()
2581
+
2582
+ wp.launch(kernel, 1, device=device)
2583
+ wp.synchronize()
2584
+ fn()
2585
+
2586
+
2587
+ def test_transform_slicing_assign_backward(test, device):
2588
+ @wp.kernel(module="unique")
2589
+ def kernel(arr_x: wp.array(dtype=wp.vec2), arr_y: wp.array(dtype=wp.transform)):
2590
+ i = wp.tid()
2591
+
2592
+ y = arr_y[i]
2593
+
2594
+ y[:2] = arr_x[i]
2595
+ y[1:-4] += arr_x[i][:2]
2596
+ y[3:1:-1] -= arr_x[i][0:]
2597
+
2598
+ arr_y[i] = y
2599
+
2600
+ x = wp.ones(1, dtype=wp.vec2, requires_grad=True, device=device)
2601
+ y = wp.zeros(1, dtype=wp.transform, requires_grad=True, device=device)
2602
+
2603
+ tape = wp.Tape()
2604
+ with tape:
2605
+ wp.launch(kernel, 1, inputs=(x,), outputs=(y,), device=device)
2606
+
2607
+ y.grad = wp.ones_like(y)
2608
+ tape.backward()
2609
+
2610
+ assert_np_equal(y.numpy(), np.array(((1.0, 2.0, 0.0, -1.0, 0.0, 0.0, 0.0),), dtype=float))
2611
+ assert_np_equal(x.grad.numpy(), np.array(((1.0, 1.0),), dtype=float))
2612
+
2613
+
2614
+ def test_transform_default_q_arg(test, device):
2615
+ vec7 = wp.vec(7, float)
2616
+
2617
+ @wp.func
2618
+ def fn():
2619
+ t = wp.transform(p=wp.vec3(1.0, 2.0, 3.0))
2620
+
2621
+ wp.expect_eq(t[:] == vec7(1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 1.0), True)
2622
+
2623
+ @wp.kernel(module="unique")
2624
+ def kernel():
2625
+ fn()
2626
+
2627
+ wp.launch(kernel, 1, device=device)
2628
+ wp.synchronize()
2629
+ fn()
2630
+
2631
+
2632
+ devices = get_test_devices()
2633
+
2634
+
2635
+ class TestSpatial(unittest.TestCase):
2636
+ pass
2637
+
2638
+
2639
+ for dtype in np_float_types:
2640
+ add_function_test_register_kernel(
2641
+ TestSpatial,
2642
+ f"test_spatial_vector_constructors_{dtype.__name__}",
2643
+ test_spatial_vector_constructors,
2644
+ devices=devices,
2645
+ dtype=dtype,
2646
+ )
2647
+ add_function_test_register_kernel(
2648
+ TestSpatial,
2649
+ f"test_spatial_vector_indexing_{dtype.__name__}",
2650
+ test_spatial_vector_indexing,
2651
+ devices=devices,
2652
+ dtype=dtype,
2653
+ )
2654
+ add_function_test_register_kernel(
2655
+ TestSpatial,
2656
+ f"test_spatial_vector_scalar_multiplication_{dtype.__name__}",
2657
+ test_spatial_vector_scalar_multiplication,
2658
+ devices=devices,
2659
+ dtype=dtype,
2660
+ )
2661
+ add_function_test_register_kernel(
2662
+ TestSpatial,
2663
+ f"test_spatial_vector_add_sub_{dtype.__name__}",
2664
+ test_spatial_vector_add_sub,
2665
+ devices=devices,
2666
+ dtype=dtype,
2667
+ )
2668
+ add_function_test_register_kernel(
2669
+ TestSpatial, f"test_spatial_dot_{dtype.__name__}", test_spatial_dot, devices=devices, dtype=dtype
2670
+ )
2671
+ add_function_test_register_kernel(
2672
+ TestSpatial, f"test_spatial_cross_{dtype.__name__}", test_spatial_cross, devices=devices, dtype=dtype
2673
+ )
2674
+ add_function_test_register_kernel(
2675
+ TestSpatial,
2676
+ f"test_spatial_top_bottom_{dtype.__name__}",
2677
+ test_spatial_top_bottom,
2678
+ devices=devices,
2679
+ dtype=dtype,
2680
+ )
2681
+
2682
+ add_function_test_register_kernel(
2683
+ TestSpatial,
2684
+ f"test_transform_constructors_{dtype.__name__}",
2685
+ test_transform_constructors,
2686
+ devices=devices,
2687
+ dtype=dtype,
2688
+ )
2689
+ add_function_test_register_kernel(
2690
+ TestSpatial,
2691
+ f"test_transform_anon_type_instance_{dtype.__name__}",
2692
+ test_transform_anon_type_instance,
2693
+ devices=devices,
2694
+ dtype=dtype,
2695
+ )
2696
+ add_function_test_register_kernel(
2697
+ TestSpatial,
2698
+ f"test_transform_identity_{dtype.__name__}",
2699
+ test_transform_identity,
2700
+ devices=devices,
2701
+ dtype=dtype,
2702
+ )
2703
+ add_function_test_register_kernel(
2704
+ TestSpatial,
2705
+ f"test_transform_indexing_{dtype.__name__}",
2706
+ test_transform_indexing,
2707
+ devices=devices,
2708
+ dtype=dtype,
2709
+ )
2710
+ add_function_test_register_kernel(
2711
+ TestSpatial,
2712
+ f"test_transform_get_trans_rot_{dtype.__name__}",
2713
+ test_transform_get_trans_rot,
2714
+ devices=devices,
2715
+ dtype=dtype,
2716
+ )
2717
+ add_function_test_register_kernel(
2718
+ TestSpatial,
2719
+ f"test_transform_multiply_{dtype.__name__}",
2720
+ test_transform_multiply,
2721
+ devices=devices,
2722
+ dtype=dtype,
2723
+ )
2724
+ add_function_test_register_kernel(
2725
+ TestSpatial,
2726
+ f"test_transform_inverse_{dtype.__name__}",
2727
+ test_transform_inverse,
2728
+ devices=devices,
2729
+ dtype=dtype,
2730
+ )
2731
+ add_function_test_register_kernel(
2732
+ TestSpatial,
2733
+ f"test_transform_point_vector_{dtype.__name__}",
2734
+ test_transform_point_vector,
2735
+ devices=devices,
2736
+ dtype=dtype,
2737
+ )
2738
+
2739
+ # are these two valid? They don't seem to be doing things you'd want to do,
2740
+ # maybe they should be removed
2741
+ add_function_test_register_kernel(
2742
+ TestSpatial,
2743
+ f"test_transform_scalar_multiplication_{dtype.__name__}",
2744
+ test_transform_scalar_multiplication,
2745
+ devices=devices,
2746
+ dtype=dtype,
2747
+ )
2748
+ add_function_test_register_kernel(
2749
+ TestSpatial,
2750
+ f"test_transform_add_sub_{dtype.__name__}",
2751
+ test_transform_add_sub,
2752
+ devices=devices,
2753
+ dtype=dtype,
2754
+ )
2755
+
2756
+ add_function_test_register_kernel(
2757
+ TestSpatial,
2758
+ f"test_spatial_matrix_constructors_{dtype.__name__}",
2759
+ test_spatial_matrix_constructors,
2760
+ devices=devices,
2761
+ dtype=dtype,
2762
+ )
2763
+ add_function_test_register_kernel(
2764
+ TestSpatial,
2765
+ f"test_spatial_matrix_indexing_{dtype.__name__}",
2766
+ test_spatial_matrix_indexing,
2767
+ devices=devices,
2768
+ dtype=dtype,
2769
+ )
2770
+ add_function_test_register_kernel(
2771
+ TestSpatial,
2772
+ f"test_spatial_matrix_scalar_multiplication_{dtype.__name__}",
2773
+ test_spatial_matrix_scalar_multiplication,
2774
+ devices=devices,
2775
+ dtype=dtype,
2776
+ )
2777
+ add_function_test_register_kernel(
2778
+ TestSpatial,
2779
+ f"test_spatial_matrix_add_sub_{dtype.__name__}",
2780
+ test_spatial_matrix_add_sub,
2781
+ devices=devices,
2782
+ dtype=dtype,
2783
+ )
2784
+ add_function_test_register_kernel(
2785
+ TestSpatial,
2786
+ f"test_spatial_matvec_multiplication_{dtype.__name__}",
2787
+ test_spatial_matvec_multiplication,
2788
+ devices=devices,
2789
+ dtype=dtype,
2790
+ )
2791
+ add_function_test_register_kernel(
2792
+ TestSpatial,
2793
+ f"test_spatial_matmat_multiplication_{dtype.__name__}",
2794
+ test_spatial_matmat_multiplication,
2795
+ devices=devices,
2796
+ dtype=dtype,
2797
+ )
2798
+ add_function_test_register_kernel(
2799
+ TestSpatial,
2800
+ f"test_spatial_outer_product_{dtype.__name__}",
2801
+ test_spatial_outer_product,
2802
+ devices=devices,
2803
+ dtype=dtype,
2804
+ )
2805
+ add_function_test_register_kernel(
2806
+ TestSpatial, f"test_spatial_adjoint_{dtype.__name__}", test_spatial_adjoint, devices=devices, dtype=dtype
2807
+ )
2808
+ add_function_test_register_kernel(
2809
+ TestSpatial,
2810
+ f"test_transform_from_matrix_{dtype.__name__}",
2811
+ test_transform_from_matrix,
2812
+ devices=devices,
2813
+ dtype=dtype,
2814
+ )
2815
+ add_function_test_register_kernel(
2816
+ TestSpatial,
2817
+ f"test_transform_to_matrix_{dtype.__name__}",
2818
+ test_transform_to_matrix,
2819
+ devices=devices,
2820
+ dtype=dtype,
2821
+ )
2822
+ add_function_test_register_kernel(
2823
+ TestSpatial,
2824
+ f"test_transform_compose_{dtype.__name__}",
2825
+ test_transform_compose,
2826
+ devices=devices,
2827
+ dtype=dtype,
2828
+ )
2829
+ add_function_test_register_kernel(
2830
+ TestSpatial,
2831
+ f"test_transform_decompose_{dtype.__name__}",
2832
+ test_transform_decompose,
2833
+ devices=devices,
2834
+ dtype=dtype,
2835
+ )
2836
+ # \TODO: test spatial_mass and spatial_jacobian
2837
+
2838
+ add_function_test(
2839
+ TestSpatial, "test_transform_getter_setter", test_transform_getter_setter, devices=wp.get_device("cpu")
2840
+ )
2841
+ add_function_test(TestSpatial, "test_transform_extract", test_transform_extract, devices=devices)
2842
+ add_function_test(TestSpatial, "test_transform_assign", test_transform_assign, devices=devices)
2843
+ add_function_test(TestSpatial, "test_transform_array_extract", test_transform_array_extract, devices=devices)
2844
+ add_function_test(TestSpatial, "test_transform_array_assign", test_transform_array_assign, devices=devices)
2845
+ add_function_test(TestSpatial, "test_transform_add_inplace", test_transform_add_inplace, devices=devices)
2846
+ add_function_test(TestSpatial, "test_transform_sub_inplace", test_transform_sub_inplace, devices=devices)
2847
+ add_function_test(TestSpatial, "test_transform_array_add_inplace", test_transform_array_add_inplace, devices=devices)
2848
+ add_function_test(TestSpatial, "test_transform_array_sub_inplace", test_transform_array_sub_inplace, devices=devices)
2849
+ add_function_test(TestSpatial, "test_transform_indexing_assign", test_transform_indexing_assign, devices=devices)
2850
+ add_function_test(TestSpatial, "test_transform_slicing_assign", test_transform_slicing_assign, devices=devices)
2851
+ add_function_test(
2852
+ TestSpatial, "test_transform_slicing_assign_backward", test_transform_slicing_assign_backward, devices=devices
2853
+ )
2854
+ add_function_test(TestSpatial, "test_transform_default_q_arg", test_transform_default_q_arg, devices=devices)
2855
+
2856
+
2857
+ if __name__ == "__main__":
2858
+ wp.clear_kernel_cache()
2859
+ unittest.main(verbosity=2)