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