warp-lang 1.0.1__py3-none-manylinux2014_x86_64.whl → 1.1.0__py3-none-manylinux2014_x86_64.whl

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

Potentially problematic release.


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

Files changed (346) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +115 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3425 -3354
  8. warp/codegen.py +2878 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +45 -45
  11. warp/context.py +5194 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +383 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -279
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +146 -146
  34. warp/examples/benchmarks/benchmark_launches.py +295 -295
  35. warp/examples/browse.py +29 -28
  36. warp/examples/core/example_dem.py +234 -221
  37. warp/examples/core/example_fluid.py +293 -267
  38. warp/examples/core/example_graph_capture.py +144 -129
  39. warp/examples/core/example_marching_cubes.py +188 -176
  40. warp/examples/core/example_mesh.py +174 -154
  41. warp/examples/core/example_mesh_intersect.py +205 -193
  42. warp/examples/core/example_nvdb.py +176 -169
  43. warp/examples/core/example_raycast.py +105 -89
  44. warp/examples/core/example_raymarch.py +199 -178
  45. warp/examples/core/example_render_opengl.py +185 -141
  46. warp/examples/core/example_sph.py +405 -389
  47. warp/examples/core/example_torch.py +222 -181
  48. warp/examples/core/example_wave.py +263 -249
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +407 -391
  51. warp/examples/fem/example_convection_diffusion.py +182 -168
  52. warp/examples/fem/example_convection_diffusion_dg.py +219 -209
  53. warp/examples/fem/example_convection_diffusion_dg0.py +204 -194
  54. warp/examples/fem/example_deformed_geometry.py +177 -159
  55. warp/examples/fem/example_diffusion.py +201 -173
  56. warp/examples/fem/example_diffusion_3d.py +177 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +221 -214
  58. warp/examples/fem/example_mixed_elasticity.py +244 -222
  59. warp/examples/fem/example_navier_stokes.py +259 -243
  60. warp/examples/fem/example_stokes.py +220 -192
  61. warp/examples/fem/example_stokes_transfer.py +265 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +260 -248
  65. warp/examples/optim/example_cloth_throw.py +222 -210
  66. warp/examples/optim/example_diffray.py +566 -535
  67. warp/examples/optim/example_drone.py +864 -835
  68. warp/examples/optim/example_inverse_kinematics.py +176 -169
  69. warp/examples/optim/example_inverse_kinematics_torch.py +185 -170
  70. warp/examples/optim/example_spring_cage.py +239 -234
  71. warp/examples/optim/example_trajectory.py +223 -201
  72. warp/examples/optim/example_walker.py +306 -292
  73. warp/examples/sim/example_cartpole.py +139 -128
  74. warp/examples/sim/example_cloth.py +196 -184
  75. warp/examples/sim/example_granular.py +124 -113
  76. warp/examples/sim/example_granular_collision_sdf.py +197 -185
  77. warp/examples/sim/example_jacobian_ik.py +236 -213
  78. warp/examples/sim/example_particle_chain.py +118 -106
  79. warp/examples/sim/example_quadruped.py +193 -179
  80. warp/examples/sim/example_rigid_chain.py +197 -189
  81. warp/examples/sim/example_rigid_contact.py +189 -176
  82. warp/examples/sim/example_rigid_force.py +127 -126
  83. warp/examples/sim/example_rigid_gyroscopic.py +109 -97
  84. warp/examples/sim/example_rigid_soft_contact.py +134 -124
  85. warp/examples/sim/example_soft_body.py +190 -178
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +60 -27
  88. warp/fem/cache.py +401 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +15 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +744 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +441 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/partition.py +374 -376
  106. warp/fem/geometry/quadmesh_2d.py +532 -532
  107. warp/fem/geometry/tetmesh.py +840 -840
  108. warp/fem/geometry/trimesh_2d.py +577 -577
  109. warp/fem/integrate.py +1630 -1615
  110. warp/fem/operator.py +190 -191
  111. warp/fem/polynomial.py +214 -213
  112. warp/fem/quadrature/__init__.py +2 -2
  113. warp/fem/quadrature/pic_quadrature.py +243 -245
  114. warp/fem/quadrature/quadrature.py +295 -294
  115. warp/fem/space/__init__.py +294 -292
  116. warp/fem/space/basis_space.py +488 -489
  117. warp/fem/space/collocated_function_space.py +100 -105
  118. warp/fem/space/dof_mapper.py +236 -236
  119. warp/fem/space/function_space.py +148 -145
  120. warp/fem/space/grid_2d_function_space.py +267 -267
  121. warp/fem/space/grid_3d_function_space.py +305 -306
  122. warp/fem/space/hexmesh_function_space.py +350 -352
  123. warp/fem/space/partition.py +350 -350
  124. warp/fem/space/quadmesh_2d_function_space.py +368 -369
  125. warp/fem/space/restriction.py +158 -160
  126. warp/fem/space/shape/__init__.py +13 -15
  127. warp/fem/space/shape/cube_shape_function.py +738 -738
  128. warp/fem/space/shape/shape_function.py +102 -103
  129. warp/fem/space/shape/square_shape_function.py +611 -611
  130. warp/fem/space/shape/tet_shape_function.py +565 -567
  131. warp/fem/space/shape/triangle_shape_function.py +429 -429
  132. warp/fem/space/tetmesh_function_space.py +294 -292
  133. warp/fem/space/topology.py +297 -295
  134. warp/fem/space/trimesh_2d_function_space.py +223 -221
  135. warp/fem/types.py +77 -77
  136. warp/fem/utils.py +495 -495
  137. warp/jax.py +166 -141
  138. warp/jax_experimental.py +341 -339
  139. warp/native/array.h +1072 -1025
  140. warp/native/builtin.h +1560 -1560
  141. warp/native/bvh.cpp +398 -398
  142. warp/native/bvh.cu +525 -525
  143. warp/native/bvh.h +429 -429
  144. warp/native/clang/clang.cpp +495 -464
  145. warp/native/crt.cpp +31 -31
  146. warp/native/crt.h +334 -334
  147. warp/native/cuda_crt.h +1049 -1049
  148. warp/native/cuda_util.cpp +549 -540
  149. warp/native/cuda_util.h +288 -203
  150. warp/native/cutlass_gemm.cpp +34 -34
  151. warp/native/cutlass_gemm.cu +372 -372
  152. warp/native/error.cpp +66 -66
  153. warp/native/error.h +27 -27
  154. warp/native/fabric.h +228 -228
  155. warp/native/hashgrid.cpp +301 -278
  156. warp/native/hashgrid.cu +78 -77
  157. warp/native/hashgrid.h +227 -227
  158. warp/native/initializer_array.h +32 -32
  159. warp/native/intersect.h +1204 -1204
  160. warp/native/intersect_adj.h +365 -365
  161. warp/native/intersect_tri.h +322 -322
  162. warp/native/marching.cpp +2 -2
  163. warp/native/marching.cu +497 -497
  164. warp/native/marching.h +2 -2
  165. warp/native/mat.h +1498 -1498
  166. warp/native/matnn.h +333 -333
  167. warp/native/mesh.cpp +203 -203
  168. warp/native/mesh.cu +293 -293
  169. warp/native/mesh.h +1887 -1887
  170. warp/native/nanovdb/NanoVDB.h +4782 -4782
  171. warp/native/nanovdb/PNanoVDB.h +2553 -2553
  172. warp/native/nanovdb/PNanoVDBWrite.h +294 -294
  173. warp/native/noise.h +850 -850
  174. warp/native/quat.h +1084 -1084
  175. warp/native/rand.h +299 -299
  176. warp/native/range.h +108 -108
  177. warp/native/reduce.cpp +156 -156
  178. warp/native/reduce.cu +348 -348
  179. warp/native/runlength_encode.cpp +61 -61
  180. warp/native/runlength_encode.cu +46 -46
  181. warp/native/scan.cpp +30 -30
  182. warp/native/scan.cu +36 -36
  183. warp/native/scan.h +7 -7
  184. warp/native/solid_angle.h +442 -442
  185. warp/native/sort.cpp +94 -94
  186. warp/native/sort.cu +97 -97
  187. warp/native/sort.h +14 -14
  188. warp/native/sparse.cpp +337 -337
  189. warp/native/sparse.cu +544 -544
  190. warp/native/spatial.h +630 -630
  191. warp/native/svd.h +562 -562
  192. warp/native/temp_buffer.h +30 -30
  193. warp/native/vec.h +1132 -1132
  194. warp/native/volume.cpp +297 -297
  195. warp/native/volume.cu +32 -32
  196. warp/native/volume.h +538 -538
  197. warp/native/volume_builder.cu +425 -425
  198. warp/native/volume_builder.h +19 -19
  199. warp/native/warp.cpp +1057 -1052
  200. warp/native/warp.cu +2943 -2828
  201. warp/native/warp.h +313 -305
  202. warp/optim/__init__.py +9 -9
  203. warp/optim/adam.py +120 -120
  204. warp/optim/linear.py +1104 -939
  205. warp/optim/sgd.py +104 -92
  206. warp/render/__init__.py +10 -10
  207. warp/render/render_opengl.py +3217 -3204
  208. warp/render/render_usd.py +768 -749
  209. warp/render/utils.py +152 -150
  210. warp/sim/__init__.py +52 -59
  211. warp/sim/articulation.py +685 -685
  212. warp/sim/collide.py +1594 -1590
  213. warp/sim/import_mjcf.py +489 -481
  214. warp/sim/import_snu.py +220 -221
  215. warp/sim/import_urdf.py +536 -516
  216. warp/sim/import_usd.py +887 -881
  217. warp/sim/inertia.py +316 -317
  218. warp/sim/integrator.py +234 -233
  219. warp/sim/integrator_euler.py +1956 -1956
  220. warp/sim/integrator_featherstone.py +1910 -1991
  221. warp/sim/integrator_xpbd.py +3294 -3312
  222. warp/sim/model.py +4473 -4314
  223. warp/sim/particles.py +113 -112
  224. warp/sim/render.py +417 -403
  225. warp/sim/utils.py +413 -410
  226. warp/sparse.py +1227 -1227
  227. warp/stubs.py +2109 -2469
  228. warp/tape.py +1162 -225
  229. warp/tests/__init__.py +1 -1
  230. warp/tests/__main__.py +4 -4
  231. warp/tests/assets/torus.usda +105 -105
  232. warp/tests/aux_test_class_kernel.py +26 -26
  233. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  234. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  235. warp/tests/aux_test_dependent.py +22 -22
  236. warp/tests/aux_test_grad_customs.py +23 -23
  237. warp/tests/aux_test_reference.py +11 -11
  238. warp/tests/aux_test_reference_reference.py +10 -10
  239. warp/tests/aux_test_square.py +17 -17
  240. warp/tests/aux_test_unresolved_func.py +14 -14
  241. warp/tests/aux_test_unresolved_symbol.py +14 -14
  242. warp/tests/disabled_kinematics.py +239 -239
  243. warp/tests/run_coverage_serial.py +31 -31
  244. warp/tests/test_adam.py +157 -157
  245. warp/tests/test_arithmetic.py +1124 -1124
  246. warp/tests/test_array.py +2417 -2326
  247. warp/tests/test_array_reduce.py +150 -150
  248. warp/tests/test_async.py +668 -656
  249. warp/tests/test_atomic.py +141 -141
  250. warp/tests/test_bool.py +204 -149
  251. warp/tests/test_builtins_resolution.py +1292 -1292
  252. warp/tests/test_bvh.py +164 -171
  253. warp/tests/test_closest_point_edge_edge.py +228 -228
  254. warp/tests/test_codegen.py +566 -553
  255. warp/tests/test_compile_consts.py +97 -101
  256. warp/tests/test_conditional.py +246 -246
  257. warp/tests/test_copy.py +232 -215
  258. warp/tests/test_ctypes.py +632 -632
  259. warp/tests/test_dense.py +67 -67
  260. warp/tests/test_devices.py +91 -98
  261. warp/tests/test_dlpack.py +530 -529
  262. warp/tests/test_examples.py +400 -378
  263. warp/tests/test_fabricarray.py +955 -955
  264. warp/tests/test_fast_math.py +62 -54
  265. warp/tests/test_fem.py +1277 -1278
  266. warp/tests/test_fp16.py +130 -130
  267. warp/tests/test_func.py +338 -337
  268. warp/tests/test_generics.py +571 -571
  269. warp/tests/test_grad.py +746 -640
  270. warp/tests/test_grad_customs.py +333 -336
  271. warp/tests/test_hash_grid.py +210 -164
  272. warp/tests/test_import.py +39 -39
  273. warp/tests/test_indexedarray.py +1134 -1134
  274. warp/tests/test_intersect.py +67 -67
  275. warp/tests/test_jax.py +307 -307
  276. warp/tests/test_large.py +167 -164
  277. warp/tests/test_launch.py +354 -354
  278. warp/tests/test_lerp.py +261 -261
  279. warp/tests/test_linear_solvers.py +191 -171
  280. warp/tests/test_lvalue.py +421 -493
  281. warp/tests/test_marching_cubes.py +65 -65
  282. warp/tests/test_mat.py +1801 -1827
  283. warp/tests/test_mat_lite.py +115 -115
  284. warp/tests/test_mat_scalar_ops.py +2907 -2889
  285. warp/tests/test_math.py +126 -193
  286. warp/tests/test_matmul.py +500 -499
  287. warp/tests/test_matmul_lite.py +410 -410
  288. warp/tests/test_mempool.py +188 -190
  289. warp/tests/test_mesh.py +284 -324
  290. warp/tests/test_mesh_query_aabb.py +228 -241
  291. warp/tests/test_mesh_query_point.py +692 -702
  292. warp/tests/test_mesh_query_ray.py +292 -303
  293. warp/tests/test_mlp.py +276 -276
  294. warp/tests/test_model.py +110 -110
  295. warp/tests/test_modules_lite.py +39 -39
  296. warp/tests/test_multigpu.py +163 -163
  297. warp/tests/test_noise.py +248 -248
  298. warp/tests/test_operators.py +250 -250
  299. warp/tests/test_options.py +123 -125
  300. warp/tests/test_peer.py +133 -137
  301. warp/tests/test_pinned.py +78 -78
  302. warp/tests/test_print.py +54 -54
  303. warp/tests/test_quat.py +2086 -2086
  304. warp/tests/test_rand.py +288 -288
  305. warp/tests/test_reload.py +217 -217
  306. warp/tests/test_rounding.py +179 -179
  307. warp/tests/test_runlength_encode.py +190 -190
  308. warp/tests/test_sim_grad.py +243 -0
  309. warp/tests/test_sim_kinematics.py +91 -97
  310. warp/tests/test_smoothstep.py +168 -168
  311. warp/tests/test_snippet.py +305 -266
  312. warp/tests/test_sparse.py +468 -460
  313. warp/tests/test_spatial.py +2148 -2148
  314. warp/tests/test_streams.py +486 -473
  315. warp/tests/test_struct.py +710 -675
  316. warp/tests/test_tape.py +173 -148
  317. warp/tests/test_torch.py +743 -743
  318. warp/tests/test_transient_module.py +87 -87
  319. warp/tests/test_types.py +556 -659
  320. warp/tests/test_utils.py +490 -499
  321. warp/tests/test_vec.py +1264 -1268
  322. warp/tests/test_vec_lite.py +73 -73
  323. warp/tests/test_vec_scalar_ops.py +2099 -2099
  324. warp/tests/test_verify_fp.py +94 -94
  325. warp/tests/test_volume.py +737 -736
  326. warp/tests/test_volume_write.py +255 -265
  327. warp/tests/unittest_serial.py +37 -37
  328. warp/tests/unittest_suites.py +363 -359
  329. warp/tests/unittest_utils.py +603 -578
  330. warp/tests/unused_test_misc.py +71 -71
  331. warp/tests/walkthrough_debug.py +85 -85
  332. warp/thirdparty/appdirs.py +598 -598
  333. warp/thirdparty/dlpack.py +143 -143
  334. warp/thirdparty/unittest_parallel.py +566 -561
  335. warp/torch.py +321 -295
  336. warp/types.py +4504 -4450
  337. warp/utils.py +1008 -821
  338. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/LICENSE.md +126 -126
  339. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/METADATA +338 -400
  340. warp_lang-1.1.0.dist-info/RECORD +352 -0
  341. warp/examples/assets/cube.usda +0 -42
  342. warp/examples/assets/sphere.usda +0 -56
  343. warp/examples/assets/torus.usda +0 -105
  344. warp_lang-1.0.1.dist-info/RECORD +0 -352
  345. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/WHEEL +0 -0
  346. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/top_level.txt +0 -0
warp/tests/test_vec.py CHANGED
@@ -1,1268 +1,1264 @@
1
- # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
- # NVIDIA CORPORATION and its licensors retain all intellectual property
3
- # and proprietary rights in and to this software, related documentation
4
- # and any modifications thereto. Any use, reproduction, disclosure or
5
- # distribution of this software and related documentation without an express
6
- # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
-
8
- import unittest
9
-
10
- import numpy as np
11
-
12
- import warp as wp
13
- from warp.tests.unittest_utils import *
14
-
15
- wp.init()
16
-
17
- np_signed_int_types = [
18
- np.int8,
19
- np.int16,
20
- np.int32,
21
- np.int64,
22
- np.byte,
23
- ]
24
-
25
- np_unsigned_int_types = [
26
- np.uint8,
27
- np.uint16,
28
- np.uint32,
29
- np.uint64,
30
- np.ubyte,
31
- ]
32
-
33
- np_float_types = [np.float16, np.float32, np.float64]
34
-
35
-
36
- def randvals(rng, shape, dtype):
37
- if dtype in np_float_types:
38
- return rng.standard_normal(size=shape).astype(dtype)
39
- elif dtype in [np.int8, np.uint8, np.byte, np.ubyte]:
40
- return rng.integers(1, high=3, size=shape, dtype=dtype)
41
- return rng.integers(1, high=5, size=shape, dtype=dtype)
42
-
43
-
44
- kernel_cache = dict()
45
-
46
-
47
- def getkernel(func, suffix=""):
48
- key = func.__name__ + "_" + suffix
49
- if key not in kernel_cache:
50
- kernel_cache[key] = wp.Kernel(func=func, key=key)
51
- return kernel_cache[key]
52
-
53
-
54
- def test_anon_constructor_error_dtype_keyword_missing(test, device):
55
- @wp.kernel
56
- def kernel():
57
- wp.vector(length=123)
58
-
59
- with test.assertRaisesRegex(
60
- RuntimeError,
61
- r"vec\(\) must have dtype as a keyword argument if it has no positional arguments, e.g.: wp.vector\(length=5, dtype=wp.float32\)$",
62
- ):
63
- wp.launch(
64
- kernel,
65
- dim=1,
66
- inputs=[],
67
- device=device,
68
- )
69
-
70
-
71
- def test_anon_constructor_error_length_mismatch(test, device):
72
- @wp.kernel
73
- def kernel():
74
- wp.vector(
75
- wp.vector(length=2, dtype=float),
76
- length=3,
77
- dtype=float,
78
- )
79
-
80
- with test.assertRaisesRegex(
81
- RuntimeError,
82
- r"Incompatible vector lengths for casting copy constructor, 3 vs 2$",
83
- ):
84
- wp.launch(
85
- kernel,
86
- dim=1,
87
- inputs=[],
88
- device=device,
89
- )
90
-
91
-
92
- def test_anon_constructor_error_numeric_arg_missing_1(test, device):
93
- @wp.kernel
94
- def kernel():
95
- wp.vector(1.0, 2.0, length=12345)
96
-
97
- with test.assertRaisesRegex(
98
- RuntimeError,
99
- r"vec\(\) must have one scalar argument or the dtype keyword argument if the length keyword argument is specified, e.g.: wp.vec\(1.0, length=5\)$",
100
- ):
101
- wp.launch(
102
- kernel,
103
- dim=1,
104
- inputs=[],
105
- device=device,
106
- )
107
-
108
-
109
- def test_anon_constructor_error_numeric_arg_missing_2(test, device):
110
- @wp.kernel
111
- def kernel():
112
- wp.vector()
113
-
114
- with test.assertRaisesRegex(
115
- RuntimeError,
116
- r"vec\(\) must have at least one numeric argument, if it's length, dtype is not specified$",
117
- ):
118
- wp.launch(
119
- kernel,
120
- dim=1,
121
- inputs=[],
122
- device=device,
123
- )
124
-
125
-
126
- def test_anon_constructor_error_dtype_keyword_extraneous(test, device):
127
- @wp.kernel
128
- def kernel():
129
- wp.vector(1.0, 2.0, 3.0, dtype=float)
130
-
131
- with test.assertRaisesRegex(
132
- RuntimeError,
133
- r"vec\(\) should not have dtype specified if numeric arguments are given, the dtype will be inferred from the argument types$",
134
- ):
135
- wp.launch(
136
- kernel,
137
- dim=1,
138
- inputs=[],
139
- device=device,
140
- )
141
-
142
-
143
- def test_anon_constructor_error_numeric_args_mismatch(test, device):
144
- @wp.kernel
145
- def kernel():
146
- wp.vector(1.0, 2)
147
-
148
- with test.assertRaisesRegex(
149
- RuntimeError,
150
- r"All numeric arguments to vec\(\) constructor should have the same "
151
- r"type, expected 2 arg_types of type <class 'warp.types.float32'>, "
152
- r"received <class 'warp.types.float32'>,<class 'warp.types.int32'>$",
153
- ):
154
- wp.launch(
155
- kernel,
156
- dim=1,
157
- inputs=[],
158
- device=device,
159
- )
160
-
161
-
162
- def test_tpl_constructor_error_incompatible_sizes(test, device):
163
- @wp.kernel
164
- def kernel():
165
- wp.vec3(wp.vec2(1.0, 2.0))
166
-
167
- with test.assertRaisesRegex(RuntimeError, r"Incompatible matrix sizes for casting copy constructor, 3 vs 2"):
168
- wp.launch(
169
- kernel,
170
- dim=1,
171
- inputs=[],
172
- device=device,
173
- )
174
-
175
-
176
- def test_tpl_constructor_error_numeric_args_mismatch(test, device):
177
- @wp.kernel
178
- def kernel():
179
- wp.vec2(1.0, 2)
180
-
181
- with test.assertRaisesRegex(
182
- RuntimeError,
183
- r"All numeric arguments to vec\(\) constructor should have the same "
184
- r"type, expected 2 arg_types of type <class 'warp.types.float32'>, "
185
- r"received <class 'warp.types.float32'>,<class 'warp.types.int32'>$",
186
- ):
187
- wp.launch(
188
- kernel,
189
- dim=1,
190
- inputs=[],
191
- device=device,
192
- )
193
-
194
-
195
- def test_tpl_ops_with_anon(test, device):
196
- vec3i = wp.vec(3, dtype=int)
197
-
198
- v = wp.vec3i(1, 2, 3)
199
- v += vec3i(2, 3, 4)
200
- v -= vec3i(3, 4, 5)
201
- test.assertSequenceEqual(v, (0, 1, 2))
202
-
203
- v = vec3i(1, 2, 3)
204
- v += wp.vec3i(2, 3, 4)
205
- v -= wp.vec3i(3, 4, 5)
206
- test.assertSequenceEqual(v, (0, 1, 2))
207
-
208
-
209
- def test_negation(test, device, dtype, register_kernels=False):
210
- rng = np.random.default_rng(123)
211
-
212
- tol = {
213
- np.float16: 5.0e-3,
214
- np.float32: 1.0e-6,
215
- np.float64: 1.0e-8,
216
- }.get(dtype, 0)
217
-
218
- wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
219
- vec2 = wp.types.vector(length=2, dtype=wptype)
220
- vec3 = wp.types.vector(length=3, dtype=wptype)
221
- vec4 = wp.types.vector(length=4, dtype=wptype)
222
- vec5 = wp.types.vector(length=5, dtype=wptype)
223
-
224
- def check_negation(
225
- v2: wp.array(dtype=vec2),
226
- v3: wp.array(dtype=vec3),
227
- v4: wp.array(dtype=vec4),
228
- v5: wp.array(dtype=vec5),
229
- v2out: wp.array(dtype=vec2),
230
- v3out: wp.array(dtype=vec3),
231
- v4out: wp.array(dtype=vec4),
232
- v5out: wp.array(dtype=vec5),
233
- v20: wp.array(dtype=wptype),
234
- v21: wp.array(dtype=wptype),
235
- v30: wp.array(dtype=wptype),
236
- v31: wp.array(dtype=wptype),
237
- v32: wp.array(dtype=wptype),
238
- v40: wp.array(dtype=wptype),
239
- v41: wp.array(dtype=wptype),
240
- v42: wp.array(dtype=wptype),
241
- v43: wp.array(dtype=wptype),
242
- v50: wp.array(dtype=wptype),
243
- v51: wp.array(dtype=wptype),
244
- v52: wp.array(dtype=wptype),
245
- v53: wp.array(dtype=wptype),
246
- v54: wp.array(dtype=wptype),
247
- ):
248
- v2result = -v2[0]
249
- v3result = -v3[0]
250
- v4result = -v4[0]
251
- v5result = -v5[0]
252
-
253
- v2out[0] = v2result
254
- v3out[0] = v3result
255
- v4out[0] = v4result
256
- v5out[0] = v5result
257
-
258
- # multiply these outputs by 2 so we've got something to backpropagate:
259
- v20[0] = wptype(2) * v2result[0]
260
- v21[0] = wptype(2) * v2result[1]
261
-
262
- v30[0] = wptype(2) * v3result[0]
263
- v31[0] = wptype(2) * v3result[1]
264
- v32[0] = wptype(2) * v3result[2]
265
-
266
- v40[0] = wptype(2) * v4result[0]
267
- v41[0] = wptype(2) * v4result[1]
268
- v42[0] = wptype(2) * v4result[2]
269
- v43[0] = wptype(2) * v4result[3]
270
-
271
- v50[0] = wptype(2) * v5result[0]
272
- v51[0] = wptype(2) * v5result[1]
273
- v52[0] = wptype(2) * v5result[2]
274
- v53[0] = wptype(2) * v5result[3]
275
- v54[0] = wptype(2) * v5result[4]
276
-
277
- kernel = getkernel(check_negation, suffix=dtype.__name__)
278
-
279
- if register_kernels:
280
- return
281
-
282
- v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
283
- v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
284
- v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
285
- v5_np = randvals(rng, (1, 5), dtype)
286
- v5 = wp.array(v5_np, dtype=vec5, requires_grad=True, device=device)
287
-
288
- v2out = wp.zeros(1, dtype=vec2, device=device)
289
- v3out = wp.zeros(1, dtype=vec3, device=device)
290
- v4out = wp.zeros(1, dtype=vec4, device=device)
291
- v5out = wp.zeros(1, dtype=vec5, device=device)
292
- v20 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
293
- v21 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
294
- v30 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
295
- v31 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
296
- v32 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
297
- v40 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
298
- v41 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
299
- v42 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
300
- v43 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
301
- v50 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
302
- v51 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
303
- v52 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
304
- v53 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
305
- v54 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
306
-
307
- tape = wp.Tape()
308
- with tape:
309
- wp.launch(
310
- kernel,
311
- dim=1,
312
- inputs=[v2, v3, v4, v5],
313
- outputs=[v2out, v3out, v4out, v5out, v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54],
314
- device=device,
315
- )
316
-
317
- if dtype in np_float_types:
318
- for i, l in enumerate([v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54]):
319
- tape.backward(loss=l)
320
- allgrads = np.concatenate([tape.gradients[v].numpy()[0] for v in [v2, v3, v4, v5]])
321
- expected_grads = np.zeros_like(allgrads)
322
- expected_grads[i] = -2
323
- assert_np_equal(allgrads, expected_grads, tol=tol)
324
- tape.zero()
325
-
326
- assert_np_equal(v2out.numpy()[0], -v2.numpy()[0], tol=tol)
327
- assert_np_equal(v3out.numpy()[0], -v3.numpy()[0], tol=tol)
328
- assert_np_equal(v4out.numpy()[0], -v4.numpy()[0], tol=tol)
329
- assert_np_equal(v5out.numpy()[0], -v5.numpy()[0], tol=tol)
330
-
331
-
332
- def test_subtraction_unsigned(test, device, dtype, register_kernels=False):
333
- wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
334
- vec2 = wp.types.vector(length=2, dtype=wptype)
335
- vec3 = wp.types.vector(length=3, dtype=wptype)
336
- vec4 = wp.types.vector(length=4, dtype=wptype)
337
- vec5 = wp.types.vector(length=5, dtype=wptype)
338
-
339
- def check_subtraction_unsigned():
340
- wp.expect_eq(vec2(wptype(3), wptype(4)) - vec2(wptype(1), wptype(2)), vec2(wptype(2), wptype(2)))
341
- wp.expect_eq(
342
- vec3(
343
- wptype(3),
344
- wptype(4),
345
- wptype(4),
346
- )
347
- - vec3(wptype(1), wptype(2), wptype(3)),
348
- vec3(wptype(2), wptype(2), wptype(1)),
349
- )
350
- wp.expect_eq(
351
- vec4(
352
- wptype(3),
353
- wptype(4),
354
- wptype(4),
355
- wptype(5),
356
- )
357
- - vec4(wptype(1), wptype(2), wptype(3), wptype(4)),
358
- vec4(wptype(2), wptype(2), wptype(1), wptype(1)),
359
- )
360
- wp.expect_eq(
361
- vec5(
362
- wptype(3),
363
- wptype(4),
364
- wptype(4),
365
- wptype(5),
366
- wptype(4),
367
- )
368
- - vec5(wptype(1), wptype(2), wptype(3), wptype(4), wptype(4)),
369
- vec5(wptype(2), wptype(2), wptype(1), wptype(1), wptype(0)),
370
- )
371
-
372
- kernel = getkernel(check_subtraction_unsigned, suffix=dtype.__name__)
373
-
374
- if register_kernels:
375
- return
376
-
377
- wp.launch(kernel, dim=1, inputs=[], outputs=[], device=device)
378
-
379
-
380
- def test_subtraction(test, device, dtype, register_kernels=False):
381
- rng = np.random.default_rng(123)
382
-
383
- tol = {
384
- np.float16: 5.0e-3,
385
- np.float32: 1.0e-6,
386
- np.float64: 1.0e-8,
387
- }.get(dtype, 0)
388
-
389
- wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
390
- vec2 = wp.types.vector(length=2, dtype=wptype)
391
- vec3 = wp.types.vector(length=3, dtype=wptype)
392
- vec4 = wp.types.vector(length=4, dtype=wptype)
393
- vec5 = wp.types.vector(length=5, dtype=wptype)
394
-
395
- def check_subtraction(
396
- s2: wp.array(dtype=vec2),
397
- s3: wp.array(dtype=vec3),
398
- s4: wp.array(dtype=vec4),
399
- s5: wp.array(dtype=vec5),
400
- v2: wp.array(dtype=vec2),
401
- v3: wp.array(dtype=vec3),
402
- v4: wp.array(dtype=vec4),
403
- v5: wp.array(dtype=vec5),
404
- v20: wp.array(dtype=wptype),
405
- v21: wp.array(dtype=wptype),
406
- v30: wp.array(dtype=wptype),
407
- v31: wp.array(dtype=wptype),
408
- v32: wp.array(dtype=wptype),
409
- v40: wp.array(dtype=wptype),
410
- v41: wp.array(dtype=wptype),
411
- v42: wp.array(dtype=wptype),
412
- v43: wp.array(dtype=wptype),
413
- v50: wp.array(dtype=wptype),
414
- v51: wp.array(dtype=wptype),
415
- v52: wp.array(dtype=wptype),
416
- v53: wp.array(dtype=wptype),
417
- v54: wp.array(dtype=wptype),
418
- ):
419
- v2result = v2[0] - s2[0]
420
- v3result = v3[0] - s3[0]
421
- v4result = v4[0] - s4[0]
422
- v5result = v5[0] - s5[0]
423
-
424
- # multiply outputs by 2 so there's something to backpropagate:
425
- v20[0] = wptype(2) * v2result[0]
426
- v21[0] = wptype(2) * v2result[1]
427
-
428
- v30[0] = wptype(2) * v3result[0]
429
- v31[0] = wptype(2) * v3result[1]
430
- v32[0] = wptype(2) * v3result[2]
431
-
432
- v40[0] = wptype(2) * v4result[0]
433
- v41[0] = wptype(2) * v4result[1]
434
- v42[0] = wptype(2) * v4result[2]
435
- v43[0] = wptype(2) * v4result[3]
436
-
437
- v50[0] = wptype(2) * v5result[0]
438
- v51[0] = wptype(2) * v5result[1]
439
- v52[0] = wptype(2) * v5result[2]
440
- v53[0] = wptype(2) * v5result[3]
441
- v54[0] = wptype(2) * v5result[4]
442
-
443
- kernel = getkernel(check_subtraction, suffix=dtype.__name__)
444
-
445
- if register_kernels:
446
- return
447
-
448
- s2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
449
- s3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
450
- s4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
451
- s5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
452
- v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
453
- v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
454
- v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
455
- v5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
456
- v20 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
457
- v21 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
458
- v30 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
459
- v31 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
460
- v32 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
461
- v40 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
462
- v41 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
463
- v42 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
464
- v43 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
465
- v50 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
466
- v51 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
467
- v52 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
468
- v53 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
469
- v54 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
470
- tape = wp.Tape()
471
- with tape:
472
- wp.launch(
473
- kernel,
474
- dim=1,
475
- inputs=[
476
- s2,
477
- s3,
478
- s4,
479
- s5,
480
- v2,
481
- v3,
482
- v4,
483
- v5,
484
- ],
485
- outputs=[v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54],
486
- device=device,
487
- )
488
-
489
- assert_np_equal(v20.numpy()[0], 2 * (v2.numpy()[0, 0] - s2.numpy()[0, 0]), tol=tol)
490
- assert_np_equal(v21.numpy()[0], 2 * (v2.numpy()[0, 1] - s2.numpy()[0, 1]), tol=tol)
491
-
492
- assert_np_equal(v30.numpy()[0], 2 * (v3.numpy()[0, 0] - s3.numpy()[0, 0]), tol=tol)
493
- assert_np_equal(v31.numpy()[0], 2 * (v3.numpy()[0, 1] - s3.numpy()[0, 1]), tol=tol)
494
- assert_np_equal(v32.numpy()[0], 2 * (v3.numpy()[0, 2] - s3.numpy()[0, 2]), tol=tol)
495
-
496
- assert_np_equal(v40.numpy()[0], 2 * (v4.numpy()[0, 0] - s4.numpy()[0, 0]), tol=2 * tol)
497
- assert_np_equal(v41.numpy()[0], 2 * (v4.numpy()[0, 1] - s4.numpy()[0, 1]), tol=2 * tol)
498
- assert_np_equal(v42.numpy()[0], 2 * (v4.numpy()[0, 2] - s4.numpy()[0, 2]), tol=2 * tol)
499
- assert_np_equal(v43.numpy()[0], 2 * (v4.numpy()[0, 3] - s4.numpy()[0, 3]), tol=2 * tol)
500
-
501
- assert_np_equal(v50.numpy()[0], 2 * (v5.numpy()[0, 0] - s5.numpy()[0, 0]), tol=tol)
502
- assert_np_equal(v51.numpy()[0], 2 * (v5.numpy()[0, 1] - s5.numpy()[0, 1]), tol=tol)
503
- assert_np_equal(v52.numpy()[0], 2 * (v5.numpy()[0, 2] - s5.numpy()[0, 2]), tol=tol)
504
- assert_np_equal(v53.numpy()[0], 2 * (v5.numpy()[0, 3] - s5.numpy()[0, 3]), tol=tol)
505
- assert_np_equal(v54.numpy()[0], 2 * (v5.numpy()[0, 4] - s5.numpy()[0, 4]), tol=tol)
506
-
507
- if dtype in np_float_types:
508
- for i, l in enumerate([v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54]):
509
- tape.backward(loss=l)
510
- sgrads = np.concatenate([tape.gradients[v].numpy()[0] for v in [s2, s3, s4, s5]])
511
- expected_grads = np.zeros_like(sgrads)
512
-
513
- expected_grads[i] = -2
514
- assert_np_equal(sgrads, expected_grads, tol=10 * tol)
515
-
516
- allgrads = np.concatenate([tape.gradients[v].numpy()[0] for v in [v2, v3, v4, v5]])
517
- expected_grads = np.zeros_like(allgrads)
518
-
519
- # d/dv v/s = 1/s
520
- expected_grads[i] = 2
521
- assert_np_equal(allgrads, expected_grads, tol=tol)
522
-
523
- tape.zero()
524
-
525
-
526
- def test_length(test, device, dtype, register_kernels=False):
527
- rng = np.random.default_rng(123)
528
-
529
- tol = {
530
- np.float16: 5.0e-3,
531
- np.float32: 1.0e-6,
532
- np.float64: 1.0e-7,
533
- }.get(dtype, 0)
534
-
535
- wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
536
- vec2 = wp.types.vector(length=2, dtype=wptype)
537
- vec3 = wp.types.vector(length=3, dtype=wptype)
538
- vec4 = wp.types.vector(length=4, dtype=wptype)
539
- vec5 = wp.types.vector(length=5, dtype=wptype)
540
-
541
- def check_length(
542
- v2: wp.array(dtype=vec2),
543
- v3: wp.array(dtype=vec3),
544
- v4: wp.array(dtype=vec4),
545
- v5: wp.array(dtype=vec5),
546
- l2: wp.array(dtype=wptype),
547
- l3: wp.array(dtype=wptype),
548
- l4: wp.array(dtype=wptype),
549
- l5: wp.array(dtype=wptype),
550
- l22: wp.array(dtype=wptype),
551
- l23: wp.array(dtype=wptype),
552
- l24: wp.array(dtype=wptype),
553
- l25: wp.array(dtype=wptype),
554
- ):
555
- l2[0] = wptype(2) * wp.length(v2[0])
556
- l3[0] = wptype(2) * wp.length(v3[0])
557
- l4[0] = wptype(2) * wp.length(v4[0])
558
- l5[0] = wptype(2) * wp.length(v5[0])
559
-
560
- l22[0] = wptype(2) * wp.length_sq(v2[0])
561
- l23[0] = wptype(2) * wp.length_sq(v3[0])
562
- l24[0] = wptype(2) * wp.length_sq(v4[0])
563
- l25[0] = wptype(2) * wp.length_sq(v5[0])
564
-
565
- kernel = getkernel(check_length, suffix=dtype.__name__)
566
-
567
- if register_kernels:
568
- return
569
-
570
- v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
571
- v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
572
- v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
573
- v5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
574
-
575
- l2 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
576
- l3 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
577
- l4 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
578
- l5 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
579
-
580
- l22 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
581
- l23 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
582
- l24 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
583
- l25 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
584
-
585
- tape = wp.Tape()
586
- with tape:
587
- wp.launch(
588
- kernel,
589
- dim=1,
590
- inputs=[
591
- v2,
592
- v3,
593
- v4,
594
- v5,
595
- ],
596
- outputs=[l2, l3, l4, l5, l22, l23, l24, l25],
597
- device=device,
598
- )
599
-
600
- assert_np_equal(l2.numpy()[0], 2 * np.linalg.norm(v2.numpy()), tol=10 * tol)
601
- assert_np_equal(l3.numpy()[0], 2 * np.linalg.norm(v3.numpy()), tol=10 * tol)
602
- assert_np_equal(l4.numpy()[0], 2 * np.linalg.norm(v4.numpy()), tol=10 * tol)
603
- assert_np_equal(l5.numpy()[0], 2 * np.linalg.norm(v5.numpy()), tol=10 * tol)
604
-
605
- assert_np_equal(l22.numpy()[0], 2 * np.linalg.norm(v2.numpy()) ** 2, tol=10 * tol)
606
- assert_np_equal(l23.numpy()[0], 2 * np.linalg.norm(v3.numpy()) ** 2, tol=10 * tol)
607
- assert_np_equal(l24.numpy()[0], 2 * np.linalg.norm(v4.numpy()) ** 2, tol=10 * tol)
608
- assert_np_equal(l25.numpy()[0], 2 * np.linalg.norm(v5.numpy()) ** 2, tol=10 * tol)
609
-
610
- tape.backward(loss=l2)
611
- grad = tape.gradients[v2].numpy()[0]
612
- expected_grad = 2 * v2.numpy()[0] / np.linalg.norm(v2.numpy())
613
- assert_np_equal(grad, expected_grad, tol=10 * tol)
614
- tape.zero()
615
-
616
- tape.backward(loss=l3)
617
- grad = tape.gradients[v3].numpy()[0]
618
- expected_grad = 2 * v3.numpy()[0] / np.linalg.norm(v3.numpy())
619
- assert_np_equal(grad, expected_grad, tol=10 * tol)
620
- tape.zero()
621
-
622
- tape.backward(loss=l4)
623
- grad = tape.gradients[v4].numpy()[0]
624
- expected_grad = 2 * v4.numpy()[0] / np.linalg.norm(v4.numpy())
625
- assert_np_equal(grad, expected_grad, tol=10 * tol)
626
- tape.zero()
627
-
628
- tape.backward(loss=l5)
629
- grad = tape.gradients[v5].numpy()[0]
630
- expected_grad = 2 * v5.numpy()[0] / np.linalg.norm(v5.numpy())
631
- assert_np_equal(grad, expected_grad, tol=10 * tol)
632
- tape.zero()
633
-
634
- tape.backward(loss=l22)
635
- grad = tape.gradients[v2].numpy()[0]
636
- expected_grad = 4 * v2.numpy()[0]
637
- assert_np_equal(grad, expected_grad, tol=10 * tol)
638
- tape.zero()
639
-
640
- tape.backward(loss=l23)
641
- grad = tape.gradients[v3].numpy()[0]
642
- expected_grad = 4 * v3.numpy()[0]
643
- assert_np_equal(grad, expected_grad, tol=10 * tol)
644
- tape.zero()
645
-
646
- tape.backward(loss=l24)
647
- grad = tape.gradients[v4].numpy()[0]
648
- expected_grad = 4 * v4.numpy()[0]
649
- assert_np_equal(grad, expected_grad, tol=10 * tol)
650
- tape.zero()
651
-
652
- tape.backward(loss=l25)
653
- grad = tape.gradients[v5].numpy()[0]
654
- expected_grad = 4 * v5.numpy()[0]
655
- assert_np_equal(grad, expected_grad, tol=10 * tol)
656
- tape.zero()
657
-
658
-
659
- def test_normalize(test, device, dtype, register_kernels=False):
660
- rng = np.random.default_rng(123)
661
-
662
- tol = {
663
- np.float16: 5.0e-3,
664
- np.float32: 1.0e-6,
665
- np.float64: 1.0e-8,
666
- }.get(dtype, 0)
667
-
668
- wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
669
- vec2 = wp.types.vector(length=2, dtype=wptype)
670
- vec3 = wp.types.vector(length=3, dtype=wptype)
671
- vec4 = wp.types.vector(length=4, dtype=wptype)
672
- vec5 = wp.types.vector(length=5, dtype=wptype)
673
-
674
- def check_normalize(
675
- v2: wp.array(dtype=vec2),
676
- v3: wp.array(dtype=vec3),
677
- v4: wp.array(dtype=vec4),
678
- v5: wp.array(dtype=vec5),
679
- n20: wp.array(dtype=wptype),
680
- n21: wp.array(dtype=wptype),
681
- n30: wp.array(dtype=wptype),
682
- n31: wp.array(dtype=wptype),
683
- n32: wp.array(dtype=wptype),
684
- n40: wp.array(dtype=wptype),
685
- n41: wp.array(dtype=wptype),
686
- n42: wp.array(dtype=wptype),
687
- n43: wp.array(dtype=wptype),
688
- n50: wp.array(dtype=wptype),
689
- n51: wp.array(dtype=wptype),
690
- n52: wp.array(dtype=wptype),
691
- n53: wp.array(dtype=wptype),
692
- n54: wp.array(dtype=wptype),
693
- ):
694
- n2 = wptype(2) * wp.normalize(v2[0])
695
- n3 = wptype(2) * wp.normalize(v3[0])
696
- n4 = wptype(2) * wp.normalize(v4[0])
697
- n5 = wptype(2) * wp.normalize(v5[0])
698
-
699
- n20[0] = n2[0]
700
- n21[0] = n2[1]
701
-
702
- n30[0] = n3[0]
703
- n31[0] = n3[1]
704
- n32[0] = n3[2]
705
-
706
- n40[0] = n4[0]
707
- n41[0] = n4[1]
708
- n42[0] = n4[2]
709
- n43[0] = n4[3]
710
-
711
- n50[0] = n5[0]
712
- n51[0] = n5[1]
713
- n52[0] = n5[2]
714
- n53[0] = n5[3]
715
- n54[0] = n5[4]
716
-
717
- def check_normalize_alt(
718
- v2: wp.array(dtype=vec2),
719
- v3: wp.array(dtype=vec3),
720
- v4: wp.array(dtype=vec4),
721
- v5: wp.array(dtype=vec5),
722
- n20: wp.array(dtype=wptype),
723
- n21: wp.array(dtype=wptype),
724
- n30: wp.array(dtype=wptype),
725
- n31: wp.array(dtype=wptype),
726
- n32: wp.array(dtype=wptype),
727
- n40: wp.array(dtype=wptype),
728
- n41: wp.array(dtype=wptype),
729
- n42: wp.array(dtype=wptype),
730
- n43: wp.array(dtype=wptype),
731
- n50: wp.array(dtype=wptype),
732
- n51: wp.array(dtype=wptype),
733
- n52: wp.array(dtype=wptype),
734
- n53: wp.array(dtype=wptype),
735
- n54: wp.array(dtype=wptype),
736
- ):
737
- n2 = wptype(2) * v2[0] / wp.length(v2[0])
738
- n3 = wptype(2) * v3[0] / wp.length(v3[0])
739
- n4 = wptype(2) * v4[0] / wp.length(v4[0])
740
- n5 = wptype(2) * v5[0] / wp.length(v5[0])
741
-
742
- n20[0] = n2[0]
743
- n21[0] = n2[1]
744
-
745
- n30[0] = n3[0]
746
- n31[0] = n3[1]
747
- n32[0] = n3[2]
748
-
749
- n40[0] = n4[0]
750
- n41[0] = n4[1]
751
- n42[0] = n4[2]
752
- n43[0] = n4[3]
753
-
754
- n50[0] = n5[0]
755
- n51[0] = n5[1]
756
- n52[0] = n5[2]
757
- n53[0] = n5[3]
758
- n54[0] = n5[4]
759
-
760
- normalize_kernel = getkernel(check_normalize, suffix=dtype.__name__)
761
- normalize_alt_kernel = getkernel(check_normalize_alt, suffix=dtype.__name__)
762
-
763
- if register_kernels:
764
- return
765
-
766
- # I've already tested the things I'm using in check_normalize_alt, so I'll just
767
- # make sure the two are giving the same results/gradients
768
- v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
769
- v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
770
- v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
771
- v5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
772
-
773
- n20 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
774
- n21 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
775
- n30 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
776
- n31 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
777
- n32 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
778
- n40 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
779
- n41 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
780
- n42 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
781
- n43 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
782
- n50 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
783
- n51 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
784
- n52 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
785
- n53 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
786
- n54 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
787
-
788
- n20_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
789
- n21_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
790
- n30_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
791
- n31_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
792
- n32_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
793
- n40_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
794
- n41_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
795
- n42_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
796
- n43_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
797
- n50_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
798
- n51_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
799
- n52_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
800
- n53_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
801
- n54_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
802
-
803
- outputs0 = [
804
- n20,
805
- n21,
806
- n30,
807
- n31,
808
- n32,
809
- n40,
810
- n41,
811
- n42,
812
- n43,
813
- n50,
814
- n51,
815
- n52,
816
- n53,
817
- n54,
818
- ]
819
- tape0 = wp.Tape()
820
- with tape0:
821
- wp.launch(
822
- normalize_kernel,
823
- dim=1,
824
- inputs=[
825
- v2,
826
- v3,
827
- v4,
828
- v5,
829
- ],
830
- outputs=outputs0,
831
- device=device,
832
- )
833
-
834
- outputs1 = [
835
- n20_alt,
836
- n21_alt,
837
- n30_alt,
838
- n31_alt,
839
- n32_alt,
840
- n40_alt,
841
- n41_alt,
842
- n42_alt,
843
- n43_alt,
844
- n50_alt,
845
- n51_alt,
846
- n52_alt,
847
- n53_alt,
848
- n54_alt,
849
- ]
850
- tape1 = wp.Tape()
851
- with tape1:
852
- wp.launch(
853
- normalize_alt_kernel,
854
- dim=1,
855
- inputs=[
856
- v2,
857
- v3,
858
- v4,
859
- v5,
860
- ],
861
- outputs=outputs1,
862
- device=device,
863
- )
864
-
865
- for ncmp, ncmpalt in zip(outputs0, outputs1):
866
- assert_np_equal(ncmp.numpy()[0], ncmpalt.numpy()[0], tol=10 * tol)
867
-
868
- invecs = [
869
- v2,
870
- v2,
871
- v3,
872
- v3,
873
- v3,
874
- v4,
875
- v4,
876
- v4,
877
- v4,
878
- v5,
879
- v5,
880
- v5,
881
- v5,
882
- v5,
883
- ]
884
- for ncmp, ncmpalt, v in zip(outputs0, outputs1, invecs):
885
- tape0.backward(loss=ncmp)
886
- tape1.backward(loss=ncmpalt)
887
- assert_np_equal(tape0.gradients[v].numpy()[0], tape1.gradients[v].numpy()[0], tol=10 * tol)
888
- tape0.zero()
889
- tape1.zero()
890
-
891
-
892
- def test_crossproduct(test, device, dtype, register_kernels=False):
893
- rng = np.random.default_rng(123)
894
-
895
- tol = {
896
- np.float16: 5.0e-3,
897
- np.float32: 1.0e-6,
898
- np.float64: 1.0e-8,
899
- }.get(dtype, 0)
900
-
901
- wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
902
- vec3 = wp.types.vector(length=3, dtype=wptype)
903
-
904
- def check_cross(
905
- s3: wp.array(dtype=vec3),
906
- v3: wp.array(dtype=vec3),
907
- c0: wp.array(dtype=wptype),
908
- c1: wp.array(dtype=wptype),
909
- c2: wp.array(dtype=wptype),
910
- ):
911
- c = wp.cross(s3[0], v3[0])
912
-
913
- # multiply outputs by 2 so we've got something to backpropagate:
914
- c0[0] = wptype(2) * c[0]
915
- c1[0] = wptype(2) * c[1]
916
- c2[0] = wptype(2) * c[2]
917
-
918
- kernel = getkernel(check_cross, suffix=dtype.__name__)
919
-
920
- if register_kernels:
921
- return
922
-
923
- s3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
924
- v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
925
- c0 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
926
- c1 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
927
- c2 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
928
- tape = wp.Tape()
929
- with tape:
930
- wp.launch(
931
- kernel,
932
- dim=1,
933
- inputs=[
934
- s3,
935
- v3,
936
- ],
937
- outputs=[c0, c1, c2],
938
- device=device,
939
- )
940
-
941
- result = 2 * np.cross(s3.numpy(), v3.numpy())[0]
942
- assert_np_equal(c0.numpy()[0], result[0], tol=10 * tol)
943
- assert_np_equal(c1.numpy()[0], result[1], tol=10 * tol)
944
- assert_np_equal(c2.numpy()[0], result[2], tol=10 * tol)
945
-
946
- if dtype in np_float_types:
947
- # c.x = sy vz - sz vy
948
- # c.y = sz vx - sx vz
949
- # c.z = sx vy - sy vx
950
-
951
- # ( d/dsx d/dsy d/dsz )c.x = ( 0 vz -vy )
952
- # ( d/dsx d/dsy d/dsz )c.y = ( -vz 0 vx )
953
- # ( d/dsx d/dsy d/dsz )c.z = ( vy -vx 0 )
954
-
955
- # ( d/dvx d/dvy d/dvz )c.x = (0 -sz sy)
956
- # ( d/dvx d/dvy d/dvz )c.y = (sz 0 -sx)
957
- # ( d/dvx d/dvy d/dvz )c.z = (-sy sx 0)
958
-
959
- tape.backward(loss=c0)
960
- assert_np_equal(
961
- tape.gradients[s3].numpy(), 2.0 * np.array([0, v3.numpy()[0, 2], -v3.numpy()[0, 1]]), tol=10 * tol
962
- )
963
- assert_np_equal(
964
- tape.gradients[v3].numpy(), 2.0 * np.array([0, -s3.numpy()[0, 2], s3.numpy()[0, 1]]), tol=10 * tol
965
- )
966
- tape.zero()
967
-
968
- tape.backward(loss=c1)
969
- assert_np_equal(
970
- tape.gradients[s3].numpy(), 2.0 * np.array([-v3.numpy()[0, 2], 0, v3.numpy()[0, 0]]), tol=10 * tol
971
- )
972
- assert_np_equal(
973
- tape.gradients[v3].numpy(), 2.0 * np.array([s3.numpy()[0, 2], 0, -s3.numpy()[0, 0]]), tol=10 * tol
974
- )
975
- tape.zero()
976
-
977
- tape.backward(loss=c2)
978
- assert_np_equal(
979
- tape.gradients[s3].numpy(), 2.0 * np.array([v3.numpy()[0, 1], -v3.numpy()[0, 0], 0]), tol=10 * tol
980
- )
981
- assert_np_equal(
982
- tape.gradients[v3].numpy(), 2.0 * np.array([-s3.numpy()[0, 1], s3.numpy()[0, 0], 0]), tol=10 * tol
983
- )
984
- tape.zero()
985
-
986
-
987
- def test_casting_constructors(test, device, dtype, register_kernels=False):
988
- np_type = np.dtype(dtype)
989
- wp_type = wp.types.np_dtype_to_warp_type[np_type]
990
- vec3 = wp.types.vector(length=3, dtype=wp_type)
991
-
992
- np16 = np.dtype(np.float16)
993
- wp16 = wp.types.np_dtype_to_warp_type[np16]
994
-
995
- np32 = np.dtype(np.float32)
996
- wp32 = wp.types.np_dtype_to_warp_type[np32]
997
-
998
- np64 = np.dtype(np.float64)
999
- wp64 = wp.types.np_dtype_to_warp_type[np64]
1000
-
1001
- def cast_float16(a: wp.array(dtype=wp_type, ndim=2), b: wp.array(dtype=wp16, ndim=2)):
1002
- tid = wp.tid()
1003
-
1004
- v1 = vec3(a[tid, 0], a[tid, 1], a[tid, 2])
1005
- v2 = wp.vector(v1, dtype=wp16)
1006
-
1007
- b[tid, 0] = v2[0]
1008
- b[tid, 1] = v2[1]
1009
- b[tid, 2] = v2[2]
1010
-
1011
- def cast_float32(a: wp.array(dtype=wp_type, ndim=2), b: wp.array(dtype=wp32, ndim=2)):
1012
- tid = wp.tid()
1013
-
1014
- v1 = vec3(a[tid, 0], a[tid, 1], a[tid, 2])
1015
- v2 = wp.vector(v1, dtype=wp32)
1016
-
1017
- b[tid, 0] = v2[0]
1018
- b[tid, 1] = v2[1]
1019
- b[tid, 2] = v2[2]
1020
-
1021
- def cast_float64(a: wp.array(dtype=wp_type, ndim=2), b: wp.array(dtype=wp64, ndim=2)):
1022
- tid = wp.tid()
1023
-
1024
- v1 = vec3(a[tid, 0], a[tid, 1], a[tid, 2])
1025
- v2 = wp.vector(v1, dtype=wp64)
1026
-
1027
- b[tid, 0] = v2[0]
1028
- b[tid, 1] = v2[1]
1029
- b[tid, 2] = v2[2]
1030
-
1031
- kernel_16 = getkernel(cast_float16, suffix=dtype.__name__)
1032
- kernel_32 = getkernel(cast_float32, suffix=dtype.__name__)
1033
- kernel_64 = getkernel(cast_float64, suffix=dtype.__name__)
1034
-
1035
- if register_kernels:
1036
- return
1037
-
1038
- # check casting to float 16
1039
- a = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, requires_grad=True, device=device)
1040
- b = wp.array(np.zeros((1, 3), dtype=np16), dtype=wp16, requires_grad=True, device=device)
1041
- b_result = np.ones((1, 3), dtype=np16)
1042
- b_grad = wp.array(np.ones((1, 3), dtype=np16), dtype=wp16, device=device)
1043
- a_grad = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, device=device)
1044
-
1045
- tape = wp.Tape()
1046
- with tape:
1047
- wp.launch(kernel=kernel_16, dim=1, inputs=[a, b], device=device)
1048
-
1049
- tape.backward(grads={b: b_grad})
1050
- out = tape.gradients[a].numpy()
1051
-
1052
- assert_np_equal(b.numpy(), b_result)
1053
- assert_np_equal(out, a_grad.numpy())
1054
-
1055
- # check casting to float 32
1056
- a = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, requires_grad=True, device=device)
1057
- b = wp.array(np.zeros((1, 3), dtype=np32), dtype=wp32, requires_grad=True, device=device)
1058
- b_result = np.ones((1, 3), dtype=np32)
1059
- b_grad = wp.array(np.ones((1, 3), dtype=np32), dtype=wp32, device=device)
1060
- a_grad = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, device=device)
1061
-
1062
- tape = wp.Tape()
1063
- with tape:
1064
- wp.launch(kernel=kernel_32, dim=1, inputs=[a, b], device=device)
1065
-
1066
- tape.backward(grads={b: b_grad})
1067
- out = tape.gradients[a].numpy()
1068
-
1069
- assert_np_equal(b.numpy(), b_result)
1070
- assert_np_equal(out, a_grad.numpy())
1071
-
1072
- # check casting to float 64
1073
- a = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, requires_grad=True, device=device)
1074
- b = wp.array(np.zeros((1, 3), dtype=np64), dtype=wp64, requires_grad=True, device=device)
1075
- b_result = np.ones((1, 3), dtype=np64)
1076
- b_grad = wp.array(np.ones((1, 3), dtype=np64), dtype=wp64, device=device)
1077
- a_grad = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, device=device)
1078
-
1079
- tape = wp.Tape()
1080
- with tape:
1081
- wp.launch(kernel=kernel_64, dim=1, inputs=[a, b], device=device)
1082
-
1083
- tape.backward(grads={b: b_grad})
1084
- out = tape.gradients[a].numpy()
1085
-
1086
- assert_np_equal(b.numpy(), b_result)
1087
- assert_np_equal(out, a_grad.numpy())
1088
-
1089
-
1090
- @wp.kernel
1091
- def test_vector_constructor_value_func():
1092
- a = wp.vec2()
1093
- b = wp.vector(a, dtype=wp.float16)
1094
- c = wp.vector(a)
1095
- d = wp.vector(a, length=2)
1096
-
1097
-
1098
- # Test matrix constructors using explicit type (float16)
1099
- # note that these tests are specifically not using generics / closure
1100
- # args to create kernels dynamically (like the rest of this file)
1101
- # as those use different code paths to resolve arg types which
1102
- # has lead to regressions.
1103
- @wp.kernel
1104
- def test_constructors_explicit_precision():
1105
- # construction for custom matrix types
1106
- ones = wp.vector(wp.float16(1.0), length=2)
1107
- zeros = wp.vector(length=2, dtype=wp.float16)
1108
- custom = wp.vector(wp.float16(0.0), wp.float16(1.0))
1109
-
1110
- for i in range(2):
1111
- wp.expect_eq(ones[i], wp.float16(1.0))
1112
- wp.expect_eq(zeros[i], wp.float16(0.0))
1113
- wp.expect_eq(custom[i], wp.float16(i))
1114
-
1115
-
1116
- # Same as above but with a default (float/int) type
1117
- # which tests some different code paths that
1118
- # need to ensure types are correctly canonicalized
1119
- # during codegen
1120
- @wp.kernel
1121
- def test_constructors_default_precision():
1122
- # construction for custom matrix types
1123
- ones = wp.vector(1.0, length=2)
1124
- zeros = wp.vector(length=2, dtype=float)
1125
- custom = wp.vector(0.0, 1.0)
1126
-
1127
- for i in range(2):
1128
- wp.expect_eq(ones[i], 1.0)
1129
- wp.expect_eq(zeros[i], 0.0)
1130
- wp.expect_eq(custom[i], float(i))
1131
-
1132
-
1133
- @wp.kernel
1134
- def test_vector_mutation(expected: wp.types.vector(length=10, dtype=float)):
1135
- v = wp.vector(length=10, dtype=float)
1136
-
1137
- # test element indexing
1138
- v[0] = 1.0
1139
-
1140
- for i in range(1, 10):
1141
- v[i] = float(i) + 1.0
1142
-
1143
- wp.expect_eq(v, expected)
1144
-
1145
-
1146
- CONSTANT_LENGTH = wp.constant(10)
1147
-
1148
-
1149
- # tests that we can use global constants in length keyword argument
1150
- # for vector constructor
1151
- @wp.kernel
1152
- def test_constructors_constant_length():
1153
- v = wp.vector(length=(CONSTANT_LENGTH), dtype=float)
1154
-
1155
- for i in range(CONSTANT_LENGTH):
1156
- v[i] = float(i)
1157
-
1158
-
1159
- devices = get_test_devices()
1160
-
1161
-
1162
- class TestVec(unittest.TestCase):
1163
- pass
1164
-
1165
-
1166
- add_kernel_test(TestVec, test_vector_constructor_value_func, dim=1, devices=devices)
1167
- add_kernel_test(TestVec, test_constructors_explicit_precision, dim=1, devices=devices)
1168
- add_kernel_test(TestVec, test_constructors_default_precision, dim=1, devices=devices)
1169
- add_kernel_test(TestVec, test_constructors_constant_length, dim=1, devices=devices)
1170
-
1171
- vec10 = wp.types.vector(length=10, dtype=float)
1172
- add_kernel_test(
1173
- TestVec,
1174
- test_vector_mutation,
1175
- dim=1,
1176
- inputs=[vec10(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)],
1177
- devices=devices,
1178
- )
1179
-
1180
- for dtype in np_unsigned_int_types:
1181
- add_function_test_register_kernel(
1182
- TestVec,
1183
- f"test_subtraction_unsigned_{dtype.__name__}",
1184
- test_subtraction_unsigned,
1185
- devices=devices,
1186
- dtype=dtype,
1187
- )
1188
-
1189
- for dtype in np_signed_int_types + np_float_types:
1190
- add_function_test_register_kernel(
1191
- TestVec, f"test_negation_{dtype.__name__}", test_negation, devices=devices, dtype=dtype
1192
- )
1193
- add_function_test_register_kernel(
1194
- TestVec, f"test_subtraction_{dtype.__name__}", test_subtraction, devices=devices, dtype=dtype
1195
- )
1196
-
1197
- for dtype in np_float_types:
1198
- add_function_test_register_kernel(
1199
- TestVec, f"test_crossproduct_{dtype.__name__}", test_crossproduct, devices=devices, dtype=dtype
1200
- )
1201
- add_function_test_register_kernel(
1202
- TestVec, f"test_length_{dtype.__name__}", test_length, devices=devices, dtype=dtype
1203
- )
1204
- add_function_test_register_kernel(
1205
- TestVec, f"test_normalize_{dtype.__name__}", test_normalize, devices=devices, dtype=dtype
1206
- )
1207
- add_function_test_register_kernel(
1208
- TestVec,
1209
- f"test_casting_constructors_{dtype.__name__}",
1210
- test_casting_constructors,
1211
- devices=devices,
1212
- dtype=dtype,
1213
- )
1214
-
1215
- add_function_test(
1216
- TestVec,
1217
- "test_anon_constructor_error_dtype_keyword_missing",
1218
- test_anon_constructor_error_dtype_keyword_missing,
1219
- devices=devices,
1220
- )
1221
- add_function_test(
1222
- TestVec,
1223
- "test_anon_constructor_error_length_mismatch",
1224
- test_anon_constructor_error_length_mismatch,
1225
- devices=devices,
1226
- )
1227
- add_function_test(
1228
- TestVec,
1229
- "test_anon_constructor_error_numeric_arg_missing_1",
1230
- test_anon_constructor_error_numeric_arg_missing_1,
1231
- devices=devices,
1232
- )
1233
- add_function_test(
1234
- TestVec,
1235
- "test_anon_constructor_error_numeric_arg_missing_2",
1236
- test_anon_constructor_error_numeric_arg_missing_2,
1237
- devices=devices,
1238
- )
1239
- add_function_test(
1240
- TestVec,
1241
- "test_anon_constructor_error_dtype_keyword_extraneous",
1242
- test_anon_constructor_error_dtype_keyword_extraneous,
1243
- devices=devices,
1244
- )
1245
- add_function_test(
1246
- TestVec,
1247
- "test_anon_constructor_error_numeric_args_mismatch",
1248
- test_anon_constructor_error_numeric_args_mismatch,
1249
- devices=devices,
1250
- )
1251
- add_function_test(
1252
- TestVec,
1253
- "test_tpl_constructor_error_incompatible_sizes",
1254
- test_tpl_constructor_error_incompatible_sizes,
1255
- devices=devices,
1256
- )
1257
- add_function_test(
1258
- TestVec,
1259
- "test_tpl_constructor_error_numeric_args_mismatch",
1260
- test_tpl_constructor_error_numeric_args_mismatch,
1261
- devices=devices,
1262
- )
1263
- add_function_test(TestVec, "test_tpl_ops_with_anon", test_tpl_ops_with_anon)
1264
-
1265
-
1266
- if __name__ == "__main__":
1267
- wp.build.clear_kernel_cache()
1268
- unittest.main(verbosity=2, failfast=True)
1
+ # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ import unittest
9
+
10
+ import numpy as np
11
+
12
+ import warp as wp
13
+ from warp.tests.unittest_utils import *
14
+
15
+ wp.init()
16
+
17
+ np_signed_int_types = [
18
+ np.int8,
19
+ np.int16,
20
+ np.int32,
21
+ np.int64,
22
+ np.byte,
23
+ ]
24
+
25
+ np_unsigned_int_types = [
26
+ np.uint8,
27
+ np.uint16,
28
+ np.uint32,
29
+ np.uint64,
30
+ np.ubyte,
31
+ ]
32
+
33
+ np_float_types = [np.float16, np.float32, np.float64]
34
+
35
+
36
+ def randvals(rng, shape, dtype):
37
+ if dtype in np_float_types:
38
+ return rng.standard_normal(size=shape).astype(dtype)
39
+ elif dtype in [np.int8, np.uint8, np.byte, np.ubyte]:
40
+ return rng.integers(1, high=3, size=shape, dtype=dtype)
41
+ return rng.integers(1, high=5, size=shape, dtype=dtype)
42
+
43
+
44
+ kernel_cache = {}
45
+
46
+
47
+ def getkernel(func, suffix=""):
48
+ key = func.__name__ + "_" + suffix
49
+ if key not in kernel_cache:
50
+ kernel_cache[key] = wp.Kernel(func=func, key=key)
51
+ return kernel_cache[key]
52
+
53
+
54
+ def test_anon_constructor_error_dtype_keyword_missing(test, device):
55
+ @wp.kernel
56
+ def kernel():
57
+ wp.vector(length=123)
58
+
59
+ with test.assertRaisesRegex(
60
+ RuntimeError,
61
+ r"vec\(\) must have dtype as a keyword argument if it has no positional arguments, e.g.: wp.vector\(length=5, dtype=wp.float32\)$",
62
+ ):
63
+ wp.launch(
64
+ kernel,
65
+ dim=1,
66
+ inputs=[],
67
+ device=device,
68
+ )
69
+
70
+
71
+ def test_anon_constructor_error_length_mismatch(test, device):
72
+ @wp.kernel
73
+ def kernel():
74
+ wp.vector(
75
+ wp.vector(length=2, dtype=float),
76
+ length=3,
77
+ dtype=float,
78
+ )
79
+
80
+ with test.assertRaisesRegex(
81
+ RuntimeError,
82
+ r"Incompatible vector lengths for casting copy constructor, 3 vs 2$",
83
+ ):
84
+ wp.launch(
85
+ kernel,
86
+ dim=1,
87
+ inputs=[],
88
+ device=device,
89
+ )
90
+
91
+
92
+ def test_anon_constructor_error_numeric_arg_missing_1(test, device):
93
+ @wp.kernel
94
+ def kernel():
95
+ wp.vector(1.0, 2.0, length=12345)
96
+
97
+ with test.assertRaisesRegex(
98
+ RuntimeError,
99
+ r"vec\(\) must have one scalar argument or the dtype keyword argument if the length keyword argument is specified, e.g.: wp.vec\(1.0, length=5\)$",
100
+ ):
101
+ wp.launch(
102
+ kernel,
103
+ dim=1,
104
+ inputs=[],
105
+ device=device,
106
+ )
107
+
108
+
109
+ def test_anon_constructor_error_numeric_arg_missing_2(test, device):
110
+ @wp.kernel
111
+ def kernel():
112
+ wp.vector()
113
+
114
+ with test.assertRaisesRegex(
115
+ RuntimeError,
116
+ r"vec\(\) must have at least one numeric argument, if it's length, dtype is not specified$",
117
+ ):
118
+ wp.launch(
119
+ kernel,
120
+ dim=1,
121
+ inputs=[],
122
+ device=device,
123
+ )
124
+
125
+
126
+ def test_anon_constructor_error_dtype_keyword_extraneous(test, device):
127
+ @wp.kernel
128
+ def kernel():
129
+ wp.vector(1.0, 2.0, 3.0, dtype=float)
130
+
131
+ with test.assertRaisesRegex(
132
+ RuntimeError,
133
+ r"vec\(\) should not have dtype specified if numeric arguments are given, the dtype will be inferred from the argument types$",
134
+ ):
135
+ wp.launch(
136
+ kernel,
137
+ dim=1,
138
+ inputs=[],
139
+ device=device,
140
+ )
141
+
142
+
143
+ def test_anon_constructor_error_numeric_args_mismatch(test, device):
144
+ @wp.kernel
145
+ def kernel():
146
+ wp.vector(1.0, 2)
147
+
148
+ with test.assertRaisesRegex(
149
+ RuntimeError,
150
+ r"All numeric arguments to vec\(\) constructor should have the same "
151
+ r"type, expected 2 arg_types of type <class 'warp.types.float32'>, "
152
+ r"received <class 'warp.types.float32'>,<class 'warp.types.int32'>$",
153
+ ):
154
+ wp.launch(
155
+ kernel,
156
+ dim=1,
157
+ inputs=[],
158
+ device=device,
159
+ )
160
+
161
+
162
+ def test_tpl_constructor_error_incompatible_sizes(test, device):
163
+ @wp.kernel
164
+ def kernel():
165
+ wp.vec3(wp.vec2(1.0, 2.0))
166
+
167
+ with test.assertRaisesRegex(RuntimeError, r"Incompatible matrix sizes for casting copy constructor, 3 vs 2"):
168
+ wp.launch(
169
+ kernel,
170
+ dim=1,
171
+ inputs=[],
172
+ device=device,
173
+ )
174
+
175
+
176
+ def test_tpl_constructor_error_numeric_args_mismatch(test, device):
177
+ @wp.kernel
178
+ def kernel():
179
+ wp.vec2(1.0, 2)
180
+
181
+ with test.assertRaisesRegex(
182
+ RuntimeError,
183
+ r"All numeric arguments to vec\(\) constructor should have the same "
184
+ r"type, expected 2 arg_types of type <class 'warp.types.float32'>, "
185
+ r"received <class 'warp.types.float32'>,<class 'warp.types.int32'>$",
186
+ ):
187
+ wp.launch(
188
+ kernel,
189
+ dim=1,
190
+ inputs=[],
191
+ device=device,
192
+ )
193
+
194
+
195
+ def test_negation(test, device, dtype, register_kernels=False):
196
+ rng = np.random.default_rng(123)
197
+
198
+ tol = {
199
+ np.float16: 5.0e-3,
200
+ np.float32: 1.0e-6,
201
+ np.float64: 1.0e-8,
202
+ }.get(dtype, 0)
203
+
204
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
205
+ vec2 = wp.types.vector(length=2, dtype=wptype)
206
+ vec3 = wp.types.vector(length=3, dtype=wptype)
207
+ vec4 = wp.types.vector(length=4, dtype=wptype)
208
+ vec5 = wp.types.vector(length=5, dtype=wptype)
209
+
210
+ def check_negation(
211
+ v2: wp.array(dtype=vec2),
212
+ v3: wp.array(dtype=vec3),
213
+ v4: wp.array(dtype=vec4),
214
+ v5: wp.array(dtype=vec5),
215
+ v2out: wp.array(dtype=vec2),
216
+ v3out: wp.array(dtype=vec3),
217
+ v4out: wp.array(dtype=vec4),
218
+ v5out: wp.array(dtype=vec5),
219
+ v20: wp.array(dtype=wptype),
220
+ v21: wp.array(dtype=wptype),
221
+ v30: wp.array(dtype=wptype),
222
+ v31: wp.array(dtype=wptype),
223
+ v32: wp.array(dtype=wptype),
224
+ v40: wp.array(dtype=wptype),
225
+ v41: wp.array(dtype=wptype),
226
+ v42: wp.array(dtype=wptype),
227
+ v43: wp.array(dtype=wptype),
228
+ v50: wp.array(dtype=wptype),
229
+ v51: wp.array(dtype=wptype),
230
+ v52: wp.array(dtype=wptype),
231
+ v53: wp.array(dtype=wptype),
232
+ v54: wp.array(dtype=wptype),
233
+ ):
234
+ v2result = -v2[0]
235
+ v3result = -v3[0]
236
+ v4result = -v4[0]
237
+ v5result = -v5[0]
238
+
239
+ v2out[0] = v2result
240
+ v3out[0] = v3result
241
+ v4out[0] = v4result
242
+ v5out[0] = v5result
243
+
244
+ # multiply these outputs by 2 so we've got something to backpropagate:
245
+ v20[0] = wptype(2) * v2result[0]
246
+ v21[0] = wptype(2) * v2result[1]
247
+
248
+ v30[0] = wptype(2) * v3result[0]
249
+ v31[0] = wptype(2) * v3result[1]
250
+ v32[0] = wptype(2) * v3result[2]
251
+
252
+ v40[0] = wptype(2) * v4result[0]
253
+ v41[0] = wptype(2) * v4result[1]
254
+ v42[0] = wptype(2) * v4result[2]
255
+ v43[0] = wptype(2) * v4result[3]
256
+
257
+ v50[0] = wptype(2) * v5result[0]
258
+ v51[0] = wptype(2) * v5result[1]
259
+ v52[0] = wptype(2) * v5result[2]
260
+ v53[0] = wptype(2) * v5result[3]
261
+ v54[0] = wptype(2) * v5result[4]
262
+
263
+ kernel = getkernel(check_negation, suffix=dtype.__name__)
264
+
265
+ if register_kernels:
266
+ return
267
+
268
+ v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
269
+ v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
270
+ v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
271
+ v5_np = randvals(rng, (1, 5), dtype)
272
+ v5 = wp.array(v5_np, dtype=vec5, requires_grad=True, device=device)
273
+
274
+ v2out = wp.zeros(1, dtype=vec2, device=device)
275
+ v3out = wp.zeros(1, dtype=vec3, device=device)
276
+ v4out = wp.zeros(1, dtype=vec4, device=device)
277
+ v5out = wp.zeros(1, dtype=vec5, device=device)
278
+ v20 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
279
+ v21 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
280
+ v30 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
281
+ v31 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
282
+ v32 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
283
+ v40 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
284
+ v41 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
285
+ v42 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
286
+ v43 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
287
+ v50 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
288
+ v51 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
289
+ v52 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
290
+ v53 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
291
+ v54 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
292
+
293
+ tape = wp.Tape()
294
+ with tape:
295
+ wp.launch(
296
+ kernel,
297
+ dim=1,
298
+ inputs=[v2, v3, v4, v5],
299
+ outputs=[v2out, v3out, v4out, v5out, v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54],
300
+ device=device,
301
+ )
302
+
303
+ if dtype in np_float_types:
304
+ for i, l in enumerate([v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54]):
305
+ tape.backward(loss=l)
306
+ allgrads = np.concatenate([tape.gradients[v].numpy()[0] for v in [v2, v3, v4, v5]])
307
+ expected_grads = np.zeros_like(allgrads)
308
+ expected_grads[i] = -2
309
+ assert_np_equal(allgrads, expected_grads, tol=tol)
310
+ tape.zero()
311
+
312
+ assert_np_equal(v2out.numpy()[0], -v2.numpy()[0], tol=tol)
313
+ assert_np_equal(v3out.numpy()[0], -v3.numpy()[0], tol=tol)
314
+ assert_np_equal(v4out.numpy()[0], -v4.numpy()[0], tol=tol)
315
+ assert_np_equal(v5out.numpy()[0], -v5.numpy()[0], tol=tol)
316
+
317
+
318
+ def test_subtraction_unsigned(test, device, dtype, register_kernels=False):
319
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
320
+ vec2 = wp.types.vector(length=2, dtype=wptype)
321
+ vec3 = wp.types.vector(length=3, dtype=wptype)
322
+ vec4 = wp.types.vector(length=4, dtype=wptype)
323
+ vec5 = wp.types.vector(length=5, dtype=wptype)
324
+
325
+ def check_subtraction_unsigned():
326
+ wp.expect_eq(vec2(wptype(3), wptype(4)) - vec2(wptype(1), wptype(2)), vec2(wptype(2), wptype(2)))
327
+ wp.expect_eq(
328
+ vec3(
329
+ wptype(3),
330
+ wptype(4),
331
+ wptype(4),
332
+ )
333
+ - vec3(wptype(1), wptype(2), wptype(3)),
334
+ vec3(wptype(2), wptype(2), wptype(1)),
335
+ )
336
+ wp.expect_eq(
337
+ vec4(
338
+ wptype(3),
339
+ wptype(4),
340
+ wptype(4),
341
+ wptype(5),
342
+ )
343
+ - vec4(wptype(1), wptype(2), wptype(3), wptype(4)),
344
+ vec4(wptype(2), wptype(2), wptype(1), wptype(1)),
345
+ )
346
+ wp.expect_eq(
347
+ vec5(
348
+ wptype(3),
349
+ wptype(4),
350
+ wptype(4),
351
+ wptype(5),
352
+ wptype(4),
353
+ )
354
+ - vec5(wptype(1), wptype(2), wptype(3), wptype(4), wptype(4)),
355
+ vec5(wptype(2), wptype(2), wptype(1), wptype(1), wptype(0)),
356
+ )
357
+
358
+ kernel = getkernel(check_subtraction_unsigned, suffix=dtype.__name__)
359
+
360
+ if register_kernels:
361
+ return
362
+
363
+ wp.launch(kernel, dim=1, inputs=[], outputs=[], device=device)
364
+
365
+
366
+ def test_subtraction(test, device, dtype, register_kernels=False):
367
+ rng = np.random.default_rng(123)
368
+
369
+ tol = {
370
+ np.float16: 5.0e-3,
371
+ np.float32: 1.0e-6,
372
+ np.float64: 1.0e-8,
373
+ }.get(dtype, 0)
374
+
375
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
376
+ vec2 = wp.types.vector(length=2, dtype=wptype)
377
+ vec3 = wp.types.vector(length=3, dtype=wptype)
378
+ vec4 = wp.types.vector(length=4, dtype=wptype)
379
+ vec5 = wp.types.vector(length=5, dtype=wptype)
380
+
381
+ def check_subtraction(
382
+ s2: wp.array(dtype=vec2),
383
+ s3: wp.array(dtype=vec3),
384
+ s4: wp.array(dtype=vec4),
385
+ s5: wp.array(dtype=vec5),
386
+ v2: wp.array(dtype=vec2),
387
+ v3: wp.array(dtype=vec3),
388
+ v4: wp.array(dtype=vec4),
389
+ v5: wp.array(dtype=vec5),
390
+ v20: wp.array(dtype=wptype),
391
+ v21: wp.array(dtype=wptype),
392
+ v30: wp.array(dtype=wptype),
393
+ v31: wp.array(dtype=wptype),
394
+ v32: wp.array(dtype=wptype),
395
+ v40: wp.array(dtype=wptype),
396
+ v41: wp.array(dtype=wptype),
397
+ v42: wp.array(dtype=wptype),
398
+ v43: wp.array(dtype=wptype),
399
+ v50: wp.array(dtype=wptype),
400
+ v51: wp.array(dtype=wptype),
401
+ v52: wp.array(dtype=wptype),
402
+ v53: wp.array(dtype=wptype),
403
+ v54: wp.array(dtype=wptype),
404
+ ):
405
+ v2result = v2[0] - s2[0]
406
+ v3result = v3[0] - s3[0]
407
+ v4result = v4[0] - s4[0]
408
+ v5result = v5[0] - s5[0]
409
+
410
+ # multiply outputs by 2 so there's something to backpropagate:
411
+ v20[0] = wptype(2) * v2result[0]
412
+ v21[0] = wptype(2) * v2result[1]
413
+
414
+ v30[0] = wptype(2) * v3result[0]
415
+ v31[0] = wptype(2) * v3result[1]
416
+ v32[0] = wptype(2) * v3result[2]
417
+
418
+ v40[0] = wptype(2) * v4result[0]
419
+ v41[0] = wptype(2) * v4result[1]
420
+ v42[0] = wptype(2) * v4result[2]
421
+ v43[0] = wptype(2) * v4result[3]
422
+
423
+ v50[0] = wptype(2) * v5result[0]
424
+ v51[0] = wptype(2) * v5result[1]
425
+ v52[0] = wptype(2) * v5result[2]
426
+ v53[0] = wptype(2) * v5result[3]
427
+ v54[0] = wptype(2) * v5result[4]
428
+
429
+ kernel = getkernel(check_subtraction, suffix=dtype.__name__)
430
+
431
+ if register_kernels:
432
+ return
433
+
434
+ s2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
435
+ s3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
436
+ s4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
437
+ s5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
438
+ v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
439
+ v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
440
+ v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
441
+ v5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
442
+ v20 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
443
+ v21 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
444
+ v30 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
445
+ v31 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
446
+ v32 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
447
+ v40 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
448
+ v41 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
449
+ v42 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
450
+ v43 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
451
+ v50 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
452
+ v51 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
453
+ v52 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
454
+ v53 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
455
+ v54 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
456
+ tape = wp.Tape()
457
+ with tape:
458
+ wp.launch(
459
+ kernel,
460
+ dim=1,
461
+ inputs=[
462
+ s2,
463
+ s3,
464
+ s4,
465
+ s5,
466
+ v2,
467
+ v3,
468
+ v4,
469
+ v5,
470
+ ],
471
+ outputs=[v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54],
472
+ device=device,
473
+ )
474
+
475
+ assert_np_equal(v20.numpy()[0], 2 * (v2.numpy()[0, 0] - s2.numpy()[0, 0]), tol=tol)
476
+ assert_np_equal(v21.numpy()[0], 2 * (v2.numpy()[0, 1] - s2.numpy()[0, 1]), tol=tol)
477
+
478
+ assert_np_equal(v30.numpy()[0], 2 * (v3.numpy()[0, 0] - s3.numpy()[0, 0]), tol=tol)
479
+ assert_np_equal(v31.numpy()[0], 2 * (v3.numpy()[0, 1] - s3.numpy()[0, 1]), tol=tol)
480
+ assert_np_equal(v32.numpy()[0], 2 * (v3.numpy()[0, 2] - s3.numpy()[0, 2]), tol=tol)
481
+
482
+ assert_np_equal(v40.numpy()[0], 2 * (v4.numpy()[0, 0] - s4.numpy()[0, 0]), tol=2 * tol)
483
+ assert_np_equal(v41.numpy()[0], 2 * (v4.numpy()[0, 1] - s4.numpy()[0, 1]), tol=2 * tol)
484
+ assert_np_equal(v42.numpy()[0], 2 * (v4.numpy()[0, 2] - s4.numpy()[0, 2]), tol=2 * tol)
485
+ assert_np_equal(v43.numpy()[0], 2 * (v4.numpy()[0, 3] - s4.numpy()[0, 3]), tol=2 * tol)
486
+
487
+ assert_np_equal(v50.numpy()[0], 2 * (v5.numpy()[0, 0] - s5.numpy()[0, 0]), tol=tol)
488
+ assert_np_equal(v51.numpy()[0], 2 * (v5.numpy()[0, 1] - s5.numpy()[0, 1]), tol=tol)
489
+ assert_np_equal(v52.numpy()[0], 2 * (v5.numpy()[0, 2] - s5.numpy()[0, 2]), tol=tol)
490
+ assert_np_equal(v53.numpy()[0], 2 * (v5.numpy()[0, 3] - s5.numpy()[0, 3]), tol=tol)
491
+ assert_np_equal(v54.numpy()[0], 2 * (v5.numpy()[0, 4] - s5.numpy()[0, 4]), tol=tol)
492
+
493
+ if dtype in np_float_types:
494
+ for i, l in enumerate([v20, v21, v30, v31, v32, v40, v41, v42, v43, v50, v51, v52, v53, v54]):
495
+ tape.backward(loss=l)
496
+ sgrads = np.concatenate([tape.gradients[v].numpy()[0] for v in [s2, s3, s4, s5]])
497
+ expected_grads = np.zeros_like(sgrads)
498
+
499
+ expected_grads[i] = -2
500
+ assert_np_equal(sgrads, expected_grads, tol=10 * tol)
501
+
502
+ allgrads = np.concatenate([tape.gradients[v].numpy()[0] for v in [v2, v3, v4, v5]])
503
+ expected_grads = np.zeros_like(allgrads)
504
+
505
+ # d/dv v/s = 1/s
506
+ expected_grads[i] = 2
507
+ assert_np_equal(allgrads, expected_grads, tol=tol)
508
+
509
+ tape.zero()
510
+
511
+
512
+ def test_length(test, device, dtype, register_kernels=False):
513
+ rng = np.random.default_rng(123)
514
+
515
+ tol = {
516
+ np.float16: 5.0e-3,
517
+ np.float32: 1.0e-6,
518
+ np.float64: 1.0e-7,
519
+ }.get(dtype, 0)
520
+
521
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
522
+ vec2 = wp.types.vector(length=2, dtype=wptype)
523
+ vec3 = wp.types.vector(length=3, dtype=wptype)
524
+ vec4 = wp.types.vector(length=4, dtype=wptype)
525
+ vec5 = wp.types.vector(length=5, dtype=wptype)
526
+
527
+ def check_length(
528
+ v2: wp.array(dtype=vec2),
529
+ v3: wp.array(dtype=vec3),
530
+ v4: wp.array(dtype=vec4),
531
+ v5: wp.array(dtype=vec5),
532
+ l2: wp.array(dtype=wptype),
533
+ l3: wp.array(dtype=wptype),
534
+ l4: wp.array(dtype=wptype),
535
+ l5: wp.array(dtype=wptype),
536
+ l22: wp.array(dtype=wptype),
537
+ l23: wp.array(dtype=wptype),
538
+ l24: wp.array(dtype=wptype),
539
+ l25: wp.array(dtype=wptype),
540
+ ):
541
+ l2[0] = wptype(2) * wp.length(v2[0])
542
+ l3[0] = wptype(2) * wp.length(v3[0])
543
+ l4[0] = wptype(2) * wp.length(v4[0])
544
+ l5[0] = wptype(2) * wp.length(v5[0])
545
+
546
+ l22[0] = wptype(2) * wp.length_sq(v2[0])
547
+ l23[0] = wptype(2) * wp.length_sq(v3[0])
548
+ l24[0] = wptype(2) * wp.length_sq(v4[0])
549
+ l25[0] = wptype(2) * wp.length_sq(v5[0])
550
+
551
+ kernel = getkernel(check_length, suffix=dtype.__name__)
552
+
553
+ if register_kernels:
554
+ return
555
+
556
+ v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
557
+ v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
558
+ v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
559
+ v5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
560
+
561
+ l2 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
562
+ l3 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
563
+ l4 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
564
+ l5 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
565
+
566
+ l22 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
567
+ l23 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
568
+ l24 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
569
+ l25 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
570
+
571
+ tape = wp.Tape()
572
+ with tape:
573
+ wp.launch(
574
+ kernel,
575
+ dim=1,
576
+ inputs=[
577
+ v2,
578
+ v3,
579
+ v4,
580
+ v5,
581
+ ],
582
+ outputs=[l2, l3, l4, l5, l22, l23, l24, l25],
583
+ device=device,
584
+ )
585
+
586
+ assert_np_equal(l2.numpy()[0], 2 * np.linalg.norm(v2.numpy()), tol=10 * tol)
587
+ assert_np_equal(l3.numpy()[0], 2 * np.linalg.norm(v3.numpy()), tol=10 * tol)
588
+ assert_np_equal(l4.numpy()[0], 2 * np.linalg.norm(v4.numpy()), tol=10 * tol)
589
+ assert_np_equal(l5.numpy()[0], 2 * np.linalg.norm(v5.numpy()), tol=10 * tol)
590
+
591
+ assert_np_equal(l22.numpy()[0], 2 * np.linalg.norm(v2.numpy()) ** 2, tol=10 * tol)
592
+ assert_np_equal(l23.numpy()[0], 2 * np.linalg.norm(v3.numpy()) ** 2, tol=10 * tol)
593
+ assert_np_equal(l24.numpy()[0], 2 * np.linalg.norm(v4.numpy()) ** 2, tol=10 * tol)
594
+ assert_np_equal(l25.numpy()[0], 2 * np.linalg.norm(v5.numpy()) ** 2, tol=10 * tol)
595
+
596
+ tape.backward(loss=l2)
597
+ grad = tape.gradients[v2].numpy()[0]
598
+ expected_grad = 2 * v2.numpy()[0] / np.linalg.norm(v2.numpy())
599
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
600
+ tape.zero()
601
+
602
+ tape.backward(loss=l3)
603
+ grad = tape.gradients[v3].numpy()[0]
604
+ expected_grad = 2 * v3.numpy()[0] / np.linalg.norm(v3.numpy())
605
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
606
+ tape.zero()
607
+
608
+ tape.backward(loss=l4)
609
+ grad = tape.gradients[v4].numpy()[0]
610
+ expected_grad = 2 * v4.numpy()[0] / np.linalg.norm(v4.numpy())
611
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
612
+ tape.zero()
613
+
614
+ tape.backward(loss=l5)
615
+ grad = tape.gradients[v5].numpy()[0]
616
+ expected_grad = 2 * v5.numpy()[0] / np.linalg.norm(v5.numpy())
617
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
618
+ tape.zero()
619
+
620
+ tape.backward(loss=l22)
621
+ grad = tape.gradients[v2].numpy()[0]
622
+ expected_grad = 4 * v2.numpy()[0]
623
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
624
+ tape.zero()
625
+
626
+ tape.backward(loss=l23)
627
+ grad = tape.gradients[v3].numpy()[0]
628
+ expected_grad = 4 * v3.numpy()[0]
629
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
630
+ tape.zero()
631
+
632
+ tape.backward(loss=l24)
633
+ grad = tape.gradients[v4].numpy()[0]
634
+ expected_grad = 4 * v4.numpy()[0]
635
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
636
+ tape.zero()
637
+
638
+ tape.backward(loss=l25)
639
+ grad = tape.gradients[v5].numpy()[0]
640
+ expected_grad = 4 * v5.numpy()[0]
641
+ assert_np_equal(grad, expected_grad, tol=10 * tol)
642
+ tape.zero()
643
+
644
+
645
+ def test_normalize(test, device, dtype, register_kernels=False):
646
+ rng = np.random.default_rng(123)
647
+
648
+ tol = {
649
+ np.float16: 5.0e-3,
650
+ np.float32: 1.0e-6,
651
+ np.float64: 1.0e-8,
652
+ }.get(dtype, 0)
653
+
654
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
655
+ vec2 = wp.types.vector(length=2, dtype=wptype)
656
+ vec3 = wp.types.vector(length=3, dtype=wptype)
657
+ vec4 = wp.types.vector(length=4, dtype=wptype)
658
+ vec5 = wp.types.vector(length=5, dtype=wptype)
659
+
660
+ def check_normalize(
661
+ v2: wp.array(dtype=vec2),
662
+ v3: wp.array(dtype=vec3),
663
+ v4: wp.array(dtype=vec4),
664
+ v5: wp.array(dtype=vec5),
665
+ n20: wp.array(dtype=wptype),
666
+ n21: wp.array(dtype=wptype),
667
+ n30: wp.array(dtype=wptype),
668
+ n31: wp.array(dtype=wptype),
669
+ n32: wp.array(dtype=wptype),
670
+ n40: wp.array(dtype=wptype),
671
+ n41: wp.array(dtype=wptype),
672
+ n42: wp.array(dtype=wptype),
673
+ n43: wp.array(dtype=wptype),
674
+ n50: wp.array(dtype=wptype),
675
+ n51: wp.array(dtype=wptype),
676
+ n52: wp.array(dtype=wptype),
677
+ n53: wp.array(dtype=wptype),
678
+ n54: wp.array(dtype=wptype),
679
+ ):
680
+ n2 = wptype(2) * wp.normalize(v2[0])
681
+ n3 = wptype(2) * wp.normalize(v3[0])
682
+ n4 = wptype(2) * wp.normalize(v4[0])
683
+ n5 = wptype(2) * wp.normalize(v5[0])
684
+
685
+ n20[0] = n2[0]
686
+ n21[0] = n2[1]
687
+
688
+ n30[0] = n3[0]
689
+ n31[0] = n3[1]
690
+ n32[0] = n3[2]
691
+
692
+ n40[0] = n4[0]
693
+ n41[0] = n4[1]
694
+ n42[0] = n4[2]
695
+ n43[0] = n4[3]
696
+
697
+ n50[0] = n5[0]
698
+ n51[0] = n5[1]
699
+ n52[0] = n5[2]
700
+ n53[0] = n5[3]
701
+ n54[0] = n5[4]
702
+
703
+ def check_normalize_alt(
704
+ v2: wp.array(dtype=vec2),
705
+ v3: wp.array(dtype=vec3),
706
+ v4: wp.array(dtype=vec4),
707
+ v5: wp.array(dtype=vec5),
708
+ n20: wp.array(dtype=wptype),
709
+ n21: wp.array(dtype=wptype),
710
+ n30: wp.array(dtype=wptype),
711
+ n31: wp.array(dtype=wptype),
712
+ n32: wp.array(dtype=wptype),
713
+ n40: wp.array(dtype=wptype),
714
+ n41: wp.array(dtype=wptype),
715
+ n42: wp.array(dtype=wptype),
716
+ n43: wp.array(dtype=wptype),
717
+ n50: wp.array(dtype=wptype),
718
+ n51: wp.array(dtype=wptype),
719
+ n52: wp.array(dtype=wptype),
720
+ n53: wp.array(dtype=wptype),
721
+ n54: wp.array(dtype=wptype),
722
+ ):
723
+ n2 = wptype(2) * v2[0] / wp.length(v2[0])
724
+ n3 = wptype(2) * v3[0] / wp.length(v3[0])
725
+ n4 = wptype(2) * v4[0] / wp.length(v4[0])
726
+ n5 = wptype(2) * v5[0] / wp.length(v5[0])
727
+
728
+ n20[0] = n2[0]
729
+ n21[0] = n2[1]
730
+
731
+ n30[0] = n3[0]
732
+ n31[0] = n3[1]
733
+ n32[0] = n3[2]
734
+
735
+ n40[0] = n4[0]
736
+ n41[0] = n4[1]
737
+ n42[0] = n4[2]
738
+ n43[0] = n4[3]
739
+
740
+ n50[0] = n5[0]
741
+ n51[0] = n5[1]
742
+ n52[0] = n5[2]
743
+ n53[0] = n5[3]
744
+ n54[0] = n5[4]
745
+
746
+ normalize_kernel = getkernel(check_normalize, suffix=dtype.__name__)
747
+ normalize_alt_kernel = getkernel(check_normalize_alt, suffix=dtype.__name__)
748
+
749
+ if register_kernels:
750
+ return
751
+
752
+ # I've already tested the things I'm using in check_normalize_alt, so I'll just
753
+ # make sure the two are giving the same results/gradients
754
+ v2 = wp.array(randvals(rng, (1, 2), dtype), dtype=vec2, requires_grad=True, device=device)
755
+ v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
756
+ v4 = wp.array(randvals(rng, (1, 4), dtype), dtype=vec4, requires_grad=True, device=device)
757
+ v5 = wp.array(randvals(rng, (1, 5), dtype), dtype=vec5, requires_grad=True, device=device)
758
+
759
+ n20 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
760
+ n21 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
761
+ n30 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
762
+ n31 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
763
+ n32 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
764
+ n40 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
765
+ n41 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
766
+ n42 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
767
+ n43 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
768
+ n50 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
769
+ n51 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
770
+ n52 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
771
+ n53 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
772
+ n54 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
773
+
774
+ n20_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
775
+ n21_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
776
+ n30_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
777
+ n31_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
778
+ n32_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
779
+ n40_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
780
+ n41_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
781
+ n42_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
782
+ n43_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
783
+ n50_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
784
+ n51_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
785
+ n52_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
786
+ n53_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
787
+ n54_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
788
+
789
+ outputs0 = [
790
+ n20,
791
+ n21,
792
+ n30,
793
+ n31,
794
+ n32,
795
+ n40,
796
+ n41,
797
+ n42,
798
+ n43,
799
+ n50,
800
+ n51,
801
+ n52,
802
+ n53,
803
+ n54,
804
+ ]
805
+ tape0 = wp.Tape()
806
+ with tape0:
807
+ wp.launch(
808
+ normalize_kernel,
809
+ dim=1,
810
+ inputs=[
811
+ v2,
812
+ v3,
813
+ v4,
814
+ v5,
815
+ ],
816
+ outputs=outputs0,
817
+ device=device,
818
+ )
819
+
820
+ outputs1 = [
821
+ n20_alt,
822
+ n21_alt,
823
+ n30_alt,
824
+ n31_alt,
825
+ n32_alt,
826
+ n40_alt,
827
+ n41_alt,
828
+ n42_alt,
829
+ n43_alt,
830
+ n50_alt,
831
+ n51_alt,
832
+ n52_alt,
833
+ n53_alt,
834
+ n54_alt,
835
+ ]
836
+ tape1 = wp.Tape()
837
+ with tape1:
838
+ wp.launch(
839
+ normalize_alt_kernel,
840
+ dim=1,
841
+ inputs=[
842
+ v2,
843
+ v3,
844
+ v4,
845
+ v5,
846
+ ],
847
+ outputs=outputs1,
848
+ device=device,
849
+ )
850
+
851
+ for ncmp, ncmpalt in zip(outputs0, outputs1):
852
+ assert_np_equal(ncmp.numpy()[0], ncmpalt.numpy()[0], tol=10 * tol)
853
+
854
+ invecs = [
855
+ v2,
856
+ v2,
857
+ v3,
858
+ v3,
859
+ v3,
860
+ v4,
861
+ v4,
862
+ v4,
863
+ v4,
864
+ v5,
865
+ v5,
866
+ v5,
867
+ v5,
868
+ v5,
869
+ ]
870
+ for ncmp, ncmpalt, v in zip(outputs0, outputs1, invecs):
871
+ tape0.backward(loss=ncmp)
872
+ tape1.backward(loss=ncmpalt)
873
+ assert_np_equal(tape0.gradients[v].numpy()[0], tape1.gradients[v].numpy()[0], tol=10 * tol)
874
+ tape0.zero()
875
+ tape1.zero()
876
+
877
+
878
+ def test_crossproduct(test, device, dtype, register_kernels=False):
879
+ rng = np.random.default_rng(123)
880
+
881
+ tol = {
882
+ np.float16: 5.0e-3,
883
+ np.float32: 1.0e-6,
884
+ np.float64: 1.0e-8,
885
+ }.get(dtype, 0)
886
+
887
+ wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
888
+ vec3 = wp.types.vector(length=3, dtype=wptype)
889
+
890
+ def check_cross(
891
+ s3: wp.array(dtype=vec3),
892
+ v3: wp.array(dtype=vec3),
893
+ c0: wp.array(dtype=wptype),
894
+ c1: wp.array(dtype=wptype),
895
+ c2: wp.array(dtype=wptype),
896
+ ):
897
+ c = wp.cross(s3[0], v3[0])
898
+
899
+ # multiply outputs by 2 so we've got something to backpropagate:
900
+ c0[0] = wptype(2) * c[0]
901
+ c1[0] = wptype(2) * c[1]
902
+ c2[0] = wptype(2) * c[2]
903
+
904
+ kernel = getkernel(check_cross, suffix=dtype.__name__)
905
+
906
+ if register_kernels:
907
+ return
908
+
909
+ s3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
910
+ v3 = wp.array(randvals(rng, (1, 3), dtype), dtype=vec3, requires_grad=True, device=device)
911
+ c0 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
912
+ c1 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
913
+ c2 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
914
+ tape = wp.Tape()
915
+ with tape:
916
+ wp.launch(
917
+ kernel,
918
+ dim=1,
919
+ inputs=[
920
+ s3,
921
+ v3,
922
+ ],
923
+ outputs=[c0, c1, c2],
924
+ device=device,
925
+ )
926
+
927
+ result = 2 * np.cross(s3.numpy(), v3.numpy())[0]
928
+ assert_np_equal(c0.numpy()[0], result[0], tol=10 * tol)
929
+ assert_np_equal(c1.numpy()[0], result[1], tol=10 * tol)
930
+ assert_np_equal(c2.numpy()[0], result[2], tol=10 * tol)
931
+
932
+ if dtype in np_float_types:
933
+ # c.x = sy vz - sz vy
934
+ # c.y = sz vx - sx vz
935
+ # c.z = sx vy - sy vx
936
+
937
+ # ( d/dsx d/dsy d/dsz )c.x = ( 0 vz -vy )
938
+ # ( d/dsx d/dsy d/dsz )c.y = ( -vz 0 vx )
939
+ # ( d/dsx d/dsy d/dsz )c.z = ( vy -vx 0 )
940
+
941
+ # ( d/dvx d/dvy d/dvz )c.x = (0 -sz sy)
942
+ # ( d/dvx d/dvy d/dvz )c.y = (sz 0 -sx)
943
+ # ( d/dvx d/dvy d/dvz )c.z = (-sy sx 0)
944
+
945
+ tape.backward(loss=c0)
946
+ assert_np_equal(
947
+ tape.gradients[s3].numpy(), 2.0 * np.array([0, v3.numpy()[0, 2], -v3.numpy()[0, 1]]), tol=10 * tol
948
+ )
949
+ assert_np_equal(
950
+ tape.gradients[v3].numpy(), 2.0 * np.array([0, -s3.numpy()[0, 2], s3.numpy()[0, 1]]), tol=10 * tol
951
+ )
952
+ tape.zero()
953
+
954
+ tape.backward(loss=c1)
955
+ assert_np_equal(
956
+ tape.gradients[s3].numpy(), 2.0 * np.array([-v3.numpy()[0, 2], 0, v3.numpy()[0, 0]]), tol=10 * tol
957
+ )
958
+ assert_np_equal(
959
+ tape.gradients[v3].numpy(), 2.0 * np.array([s3.numpy()[0, 2], 0, -s3.numpy()[0, 0]]), tol=10 * tol
960
+ )
961
+ tape.zero()
962
+
963
+ tape.backward(loss=c2)
964
+ assert_np_equal(
965
+ tape.gradients[s3].numpy(), 2.0 * np.array([v3.numpy()[0, 1], -v3.numpy()[0, 0], 0]), tol=10 * tol
966
+ )
967
+ assert_np_equal(
968
+ tape.gradients[v3].numpy(), 2.0 * np.array([-s3.numpy()[0, 1], s3.numpy()[0, 0], 0]), tol=10 * tol
969
+ )
970
+ tape.zero()
971
+
972
+
973
+ def test_casting_constructors(test, device, dtype, register_kernels=False):
974
+ np_type = np.dtype(dtype)
975
+ wp_type = wp.types.np_dtype_to_warp_type[np_type]
976
+ vec3 = wp.types.vector(length=3, dtype=wp_type)
977
+
978
+ np16 = np.dtype(np.float16)
979
+ wp16 = wp.types.np_dtype_to_warp_type[np16]
980
+
981
+ np32 = np.dtype(np.float32)
982
+ wp32 = wp.types.np_dtype_to_warp_type[np32]
983
+
984
+ np64 = np.dtype(np.float64)
985
+ wp64 = wp.types.np_dtype_to_warp_type[np64]
986
+
987
+ def cast_float16(a: wp.array(dtype=wp_type, ndim=2), b: wp.array(dtype=wp16, ndim=2)):
988
+ tid = wp.tid()
989
+
990
+ v1 = vec3(a[tid, 0], a[tid, 1], a[tid, 2])
991
+ v2 = wp.vector(v1, dtype=wp16)
992
+
993
+ b[tid, 0] = v2[0]
994
+ b[tid, 1] = v2[1]
995
+ b[tid, 2] = v2[2]
996
+
997
+ def cast_float32(a: wp.array(dtype=wp_type, ndim=2), b: wp.array(dtype=wp32, ndim=2)):
998
+ tid = wp.tid()
999
+
1000
+ v1 = vec3(a[tid, 0], a[tid, 1], a[tid, 2])
1001
+ v2 = wp.vector(v1, dtype=wp32)
1002
+
1003
+ b[tid, 0] = v2[0]
1004
+ b[tid, 1] = v2[1]
1005
+ b[tid, 2] = v2[2]
1006
+
1007
+ def cast_float64(a: wp.array(dtype=wp_type, ndim=2), b: wp.array(dtype=wp64, ndim=2)):
1008
+ tid = wp.tid()
1009
+
1010
+ v1 = vec3(a[tid, 0], a[tid, 1], a[tid, 2])
1011
+ v2 = wp.vector(v1, dtype=wp64)
1012
+
1013
+ b[tid, 0] = v2[0]
1014
+ b[tid, 1] = v2[1]
1015
+ b[tid, 2] = v2[2]
1016
+
1017
+ kernel_16 = getkernel(cast_float16, suffix=dtype.__name__)
1018
+ kernel_32 = getkernel(cast_float32, suffix=dtype.__name__)
1019
+ kernel_64 = getkernel(cast_float64, suffix=dtype.__name__)
1020
+
1021
+ if register_kernels:
1022
+ return
1023
+
1024
+ # check casting to float 16
1025
+ a = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, requires_grad=True, device=device)
1026
+ b = wp.array(np.zeros((1, 3), dtype=np16), dtype=wp16, requires_grad=True, device=device)
1027
+ b_result = np.ones((1, 3), dtype=np16)
1028
+ b_grad = wp.array(np.ones((1, 3), dtype=np16), dtype=wp16, device=device)
1029
+ a_grad = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, device=device)
1030
+
1031
+ tape = wp.Tape()
1032
+ with tape:
1033
+ wp.launch(kernel=kernel_16, dim=1, inputs=[a, b], device=device)
1034
+
1035
+ tape.backward(grads={b: b_grad})
1036
+ out = tape.gradients[a].numpy()
1037
+
1038
+ assert_np_equal(b.numpy(), b_result)
1039
+ assert_np_equal(out, a_grad.numpy())
1040
+
1041
+ # check casting to float 32
1042
+ a = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, requires_grad=True, device=device)
1043
+ b = wp.array(np.zeros((1, 3), dtype=np32), dtype=wp32, requires_grad=True, device=device)
1044
+ b_result = np.ones((1, 3), dtype=np32)
1045
+ b_grad = wp.array(np.ones((1, 3), dtype=np32), dtype=wp32, device=device)
1046
+ a_grad = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, device=device)
1047
+
1048
+ tape = wp.Tape()
1049
+ with tape:
1050
+ wp.launch(kernel=kernel_32, dim=1, inputs=[a, b], device=device)
1051
+
1052
+ tape.backward(grads={b: b_grad})
1053
+ out = tape.gradients[a].numpy()
1054
+
1055
+ assert_np_equal(b.numpy(), b_result)
1056
+ assert_np_equal(out, a_grad.numpy())
1057
+
1058
+ # check casting to float 64
1059
+ a = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, requires_grad=True, device=device)
1060
+ b = wp.array(np.zeros((1, 3), dtype=np64), dtype=wp64, requires_grad=True, device=device)
1061
+ b_result = np.ones((1, 3), dtype=np64)
1062
+ b_grad = wp.array(np.ones((1, 3), dtype=np64), dtype=wp64, device=device)
1063
+ a_grad = wp.array(np.ones((1, 3), dtype=np_type), dtype=wp_type, device=device)
1064
+
1065
+ tape = wp.Tape()
1066
+ with tape:
1067
+ wp.launch(kernel=kernel_64, dim=1, inputs=[a, b], device=device)
1068
+
1069
+ tape.backward(grads={b: b_grad})
1070
+ out = tape.gradients[a].numpy()
1071
+
1072
+ assert_np_equal(b.numpy(), b_result)
1073
+ assert_np_equal(out, a_grad.numpy())
1074
+
1075
+
1076
+ @wp.kernel
1077
+ def test_vector_constructor_value_func():
1078
+ a = wp.vec2()
1079
+ b = wp.vector(a, dtype=wp.float16)
1080
+ c = wp.vector(a)
1081
+ d = wp.vector(a, length=2)
1082
+
1083
+
1084
+ # Test matrix constructors using explicit type (float16)
1085
+ # note that these tests are specifically not using generics / closure
1086
+ # args to create kernels dynamically (like the rest of this file)
1087
+ # as those use different code paths to resolve arg types which
1088
+ # has lead to regressions.
1089
+ @wp.kernel
1090
+ def test_constructors_explicit_precision():
1091
+ # construction for custom matrix types
1092
+ ones = wp.vector(wp.float16(1.0), length=2)
1093
+ zeros = wp.vector(length=2, dtype=wp.float16)
1094
+ custom = wp.vector(wp.float16(0.0), wp.float16(1.0))
1095
+
1096
+ for i in range(2):
1097
+ wp.expect_eq(ones[i], wp.float16(1.0))
1098
+ wp.expect_eq(zeros[i], wp.float16(0.0))
1099
+ wp.expect_eq(custom[i], wp.float16(i))
1100
+
1101
+
1102
+ # Same as above but with a default (float/int) type
1103
+ # which tests some different code paths that
1104
+ # need to ensure types are correctly canonicalized
1105
+ # during codegen
1106
+ @wp.kernel
1107
+ def test_constructors_default_precision():
1108
+ # construction for custom matrix types
1109
+ ones = wp.vector(1.0, length=2)
1110
+ zeros = wp.vector(length=2, dtype=float)
1111
+ custom = wp.vector(0.0, 1.0)
1112
+
1113
+ for i in range(2):
1114
+ wp.expect_eq(ones[i], 1.0)
1115
+ wp.expect_eq(zeros[i], 0.0)
1116
+ wp.expect_eq(custom[i], float(i))
1117
+
1118
+
1119
+ @wp.kernel
1120
+ def test_vector_mutation(expected: wp.types.vector(length=10, dtype=float)):
1121
+ v = wp.vector(length=10, dtype=float)
1122
+
1123
+ # test element indexing
1124
+ v[0] = 1.0
1125
+
1126
+ for i in range(1, 10):
1127
+ v[i] = float(i) + 1.0
1128
+
1129
+ wp.expect_eq(v, expected)
1130
+
1131
+
1132
+ CONSTANT_LENGTH = wp.constant(10)
1133
+
1134
+
1135
+ # tests that we can use global constants in length keyword argument
1136
+ # for vector constructor
1137
+ @wp.kernel
1138
+ def test_constructors_constant_length():
1139
+ v = wp.vector(length=(CONSTANT_LENGTH), dtype=float)
1140
+
1141
+ for i in range(CONSTANT_LENGTH):
1142
+ v[i] = float(i)
1143
+
1144
+
1145
+ devices = get_test_devices()
1146
+
1147
+
1148
+ class TestVec(unittest.TestCase):
1149
+ def test_tpl_ops_with_anon(self):
1150
+ vec3i = wp.vec(3, dtype=int)
1151
+
1152
+ v = wp.vec3i(1, 2, 3)
1153
+ v += vec3i(2, 3, 4)
1154
+ v -= vec3i(3, 4, 5)
1155
+ self.assertSequenceEqual(v, (0, 1, 2))
1156
+
1157
+ v = vec3i(1, 2, 3)
1158
+ v += wp.vec3i(2, 3, 4)
1159
+ v -= wp.vec3i(3, 4, 5)
1160
+ self.assertSequenceEqual(v, (0, 1, 2))
1161
+
1162
+
1163
+ add_kernel_test(TestVec, test_vector_constructor_value_func, dim=1, devices=devices)
1164
+ add_kernel_test(TestVec, test_constructors_explicit_precision, dim=1, devices=devices)
1165
+ add_kernel_test(TestVec, test_constructors_default_precision, dim=1, devices=devices)
1166
+ add_kernel_test(TestVec, test_constructors_constant_length, dim=1, devices=devices)
1167
+
1168
+ vec10 = wp.types.vector(length=10, dtype=float)
1169
+ add_kernel_test(
1170
+ TestVec,
1171
+ test_vector_mutation,
1172
+ dim=1,
1173
+ inputs=[vec10(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)],
1174
+ devices=devices,
1175
+ )
1176
+
1177
+ for dtype in np_unsigned_int_types:
1178
+ add_function_test_register_kernel(
1179
+ TestVec,
1180
+ f"test_subtraction_unsigned_{dtype.__name__}",
1181
+ test_subtraction_unsigned,
1182
+ devices=devices,
1183
+ dtype=dtype,
1184
+ )
1185
+
1186
+ for dtype in np_signed_int_types + np_float_types:
1187
+ add_function_test_register_kernel(
1188
+ TestVec, f"test_negation_{dtype.__name__}", test_negation, devices=devices, dtype=dtype
1189
+ )
1190
+ add_function_test_register_kernel(
1191
+ TestVec, f"test_subtraction_{dtype.__name__}", test_subtraction, devices=devices, dtype=dtype
1192
+ )
1193
+
1194
+ for dtype in np_float_types:
1195
+ add_function_test_register_kernel(
1196
+ TestVec, f"test_crossproduct_{dtype.__name__}", test_crossproduct, devices=devices, dtype=dtype
1197
+ )
1198
+ add_function_test_register_kernel(
1199
+ TestVec, f"test_length_{dtype.__name__}", test_length, devices=devices, dtype=dtype
1200
+ )
1201
+ add_function_test_register_kernel(
1202
+ TestVec, f"test_normalize_{dtype.__name__}", test_normalize, devices=devices, dtype=dtype
1203
+ )
1204
+ add_function_test_register_kernel(
1205
+ TestVec,
1206
+ f"test_casting_constructors_{dtype.__name__}",
1207
+ test_casting_constructors,
1208
+ devices=devices,
1209
+ dtype=dtype,
1210
+ )
1211
+
1212
+ add_function_test(
1213
+ TestVec,
1214
+ "test_anon_constructor_error_dtype_keyword_missing",
1215
+ test_anon_constructor_error_dtype_keyword_missing,
1216
+ devices=devices,
1217
+ )
1218
+ add_function_test(
1219
+ TestVec,
1220
+ "test_anon_constructor_error_length_mismatch",
1221
+ test_anon_constructor_error_length_mismatch,
1222
+ devices=devices,
1223
+ )
1224
+ add_function_test(
1225
+ TestVec,
1226
+ "test_anon_constructor_error_numeric_arg_missing_1",
1227
+ test_anon_constructor_error_numeric_arg_missing_1,
1228
+ devices=devices,
1229
+ )
1230
+ add_function_test(
1231
+ TestVec,
1232
+ "test_anon_constructor_error_numeric_arg_missing_2",
1233
+ test_anon_constructor_error_numeric_arg_missing_2,
1234
+ devices=devices,
1235
+ )
1236
+ add_function_test(
1237
+ TestVec,
1238
+ "test_anon_constructor_error_dtype_keyword_extraneous",
1239
+ test_anon_constructor_error_dtype_keyword_extraneous,
1240
+ devices=devices,
1241
+ )
1242
+ add_function_test(
1243
+ TestVec,
1244
+ "test_anon_constructor_error_numeric_args_mismatch",
1245
+ test_anon_constructor_error_numeric_args_mismatch,
1246
+ devices=devices,
1247
+ )
1248
+ add_function_test(
1249
+ TestVec,
1250
+ "test_tpl_constructor_error_incompatible_sizes",
1251
+ test_tpl_constructor_error_incompatible_sizes,
1252
+ devices=devices,
1253
+ )
1254
+ add_function_test(
1255
+ TestVec,
1256
+ "test_tpl_constructor_error_numeric_args_mismatch",
1257
+ test_tpl_constructor_error_numeric_args_mismatch,
1258
+ devices=devices,
1259
+ )
1260
+
1261
+
1262
+ if __name__ == "__main__":
1263
+ wp.build.clear_kernel_cache()
1264
+ unittest.main(verbosity=2, failfast=True)