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_struct.py CHANGED
@@ -1,675 +1,708 @@
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
- from typing import Any
10
-
11
- import numpy as np
12
-
13
- import warp as wp
14
- from warp.tests.unittest_utils import *
15
-
16
- from warp.fem import Sample as StructFromAnotherModule
17
-
18
- wp.init()
19
-
20
-
21
- @wp.struct
22
- class Model:
23
- dt: float
24
- gravity: wp.vec3
25
- m: wp.array(dtype=float)
26
-
27
-
28
- @wp.struct
29
- class State:
30
- x: wp.array(dtype=wp.vec3)
31
- v: wp.array(dtype=wp.vec3)
32
-
33
-
34
- @wp.kernel
35
- def kernel_step(state_in: State, state_out: State, model: Model):
36
- i = wp.tid()
37
-
38
- state_out.v[i] = state_in.v[i] + model.gravity / model.m[i] * model.dt
39
- state_out.x[i] = state_in.x[i] + state_out.v[i] * model.dt
40
-
41
-
42
- @wp.kernel
43
- def kernel_step_with_copy(state_in: State, state_out: State, model: Model):
44
- i = wp.tid()
45
-
46
- model_rescaled = Model(1.0, model.gravity / model.m[i] * model.dt, model.m)
47
-
48
- state_out_copy = State(state_out.x, state_out.v)
49
- state_out_copy.v[i] = state_in.v[i] + model_rescaled.gravity
50
- state_out_copy.x[i] = state_in.x[i] + state_out_copy.v[i] * model.dt
51
-
52
-
53
- def test_step(test, device):
54
- rng = np.random.default_rng(123)
55
-
56
- dim = 5
57
-
58
- dt = 0.01
59
- gravity = np.array([0, 0, -9.81])
60
-
61
- m = np.ones(dim)
62
-
63
- m_model = wp.array(m, dtype=float, device=device)
64
-
65
- model = Model()
66
- model.m = m_model
67
- model.dt = dt
68
- model.gravity = wp.vec3(0, 0, -9.81)
69
-
70
- x = rng.normal(size=(dim, 3))
71
- v = rng.normal(size=(dim, 3))
72
-
73
- x_expected = x + (v + gravity / m[:, None] * dt) * dt
74
-
75
- x_in = wp.array(x, dtype=wp.vec3, device=device)
76
- v_in = wp.array(v, dtype=wp.vec3, device=device)
77
-
78
- state_in = State()
79
- state_in.x = x_in
80
- state_in.v = v_in
81
-
82
- state_out = State()
83
- state_out.x = wp.empty_like(x_in)
84
- state_out.v = wp.empty_like(v_in)
85
-
86
- for step_kernel in [kernel_step, kernel_step_with_copy]:
87
- with CheckOutput(test):
88
- wp.launch(step_kernel, dim=dim, inputs=[state_in, state_out, model], device=device)
89
-
90
- assert_np_equal(state_out.x.numpy(), x_expected, tol=1e-6)
91
-
92
-
93
- @wp.kernel
94
- def kernel_loss(x: wp.array(dtype=wp.vec3), loss: wp.array(dtype=float)):
95
- i = wp.tid()
96
- wp.atomic_add(loss, 0, x[i][0] * x[i][0] + x[i][1] * x[i][1] + x[i][2] * x[i][2])
97
-
98
-
99
- def test_step_grad(test, device):
100
- rng = np.random.default_rng(123)
101
-
102
- dim = 5
103
-
104
- dt = 0.01
105
- gravity = np.array([0, 0, -9.81])
106
-
107
- m = rng.random(size=dim) + 0.1
108
-
109
- m_model = wp.array(m, dtype=float, device=device, requires_grad=True)
110
-
111
- model = Model()
112
- model.m = m_model
113
- model.dt = dt
114
- model.gravity = wp.vec3(0, 0, -9.81)
115
-
116
- x = rng.normal(size=(dim, 3))
117
- v = rng.normal(size=(dim, 3))
118
-
119
- x_in = wp.array(x, dtype=wp.vec3, device=device, requires_grad=True)
120
- v_in = wp.array(v, dtype=wp.vec3, device=device, requires_grad=True)
121
-
122
- state_in = State()
123
- state_in.x = x_in
124
- state_in.v = v_in
125
-
126
- state_out = State()
127
- state_out.x = wp.empty_like(x_in, requires_grad=True)
128
- state_out.v = wp.empty_like(v_in, requires_grad=True)
129
-
130
- loss = wp.empty(1, dtype=float, device=device, requires_grad=True)
131
-
132
- for step_kernel in [kernel_step, kernel_step_with_copy]:
133
- tape = wp.Tape()
134
-
135
- with tape:
136
- wp.launch(step_kernel, dim=dim, inputs=[state_in, state_out, model], device=device)
137
- wp.launch(kernel_loss, dim=dim, inputs=[state_out.x, loss], device=device)
138
-
139
- tape.backward(loss)
140
-
141
- dl_dx = 2 * state_out.x.numpy()
142
- dl_dv = dl_dx * dt
143
-
144
- dv_dm = -gravity * dt / m[:, None] ** 2
145
- dl_dm = (dl_dv * dv_dm).sum(-1)
146
-
147
- assert_np_equal(state_out.x.grad.numpy(), dl_dx, tol=1e-6)
148
- assert_np_equal(state_in.x.grad.numpy(), dl_dx, tol=1e-6)
149
- assert_np_equal(state_out.v.grad.numpy(), dl_dv, tol=1e-6)
150
- assert_np_equal(state_in.v.grad.numpy(), dl_dv, tol=1e-6)
151
- assert_np_equal(model.m.grad.numpy(), dl_dm, tol=1e-6)
152
-
153
- tape.zero()
154
-
155
- assert state_out.x.grad.numpy().sum() == 0.0
156
- assert state_in.x.grad.numpy().sum() == 0.0
157
- assert state_out.v.grad.numpy().sum() == 0.0
158
- assert state_in.v.grad.numpy().sum() == 0.0
159
- assert model.m.grad.numpy().sum() == 0.0
160
-
161
-
162
- @wp.struct
163
- class Empty:
164
- pass
165
-
166
-
167
- @wp.kernel
168
- def test_empty(input: Empty):
169
- tid = wp.tid()
170
-
171
-
172
- @wp.struct
173
- class Uninitialized:
174
- data: wp.array(dtype=int)
175
-
176
-
177
- @wp.kernel
178
- def test_uninitialized(input: Uninitialized):
179
- tid = wp.tid()
180
-
181
-
182
- @wp.struct
183
- class Baz:
184
- data: wp.array(dtype=int)
185
- z: wp.vec3
186
-
187
-
188
- @wp.struct
189
- class Bar:
190
- baz: Baz
191
- y: float
192
-
193
-
194
- @wp.struct
195
- class Foo:
196
- bar: Bar
197
- x: int
198
-
199
-
200
- @wp.kernel
201
- def kernel_nested_struct(foo: Foo):
202
- tid = wp.tid()
203
- foo.bar.baz.data[tid] = (
204
- foo.bar.baz.data[tid] + foo.x + int(foo.bar.y * 100.0) + int(wp.length_sq(foo.bar.baz.z)) + tid * 2
205
- )
206
-
207
-
208
- def test_nested_struct(test, device):
209
- dim = 3
210
-
211
- foo = Foo()
212
- foo.bar = Bar()
213
- foo.bar.baz = Baz()
214
- foo.bar.baz.data = wp.zeros(dim, dtype=int, device=device)
215
- foo.bar.baz.z = wp.vec3(1, 2, 3)
216
- foo.bar.y = 1.23
217
- foo.x = 123
218
-
219
- wp.launch(kernel_nested_struct, dim=dim, inputs=[foo], device=device)
220
-
221
- assert_array_equal(
222
- foo.bar.baz.data,
223
- wp.array((260, 262, 264), dtype=int, device=device),
224
- )
225
-
226
-
227
- def test_struct_attribute_error(test, device):
228
- @wp.kernel
229
- def kernel(foo: Foo):
230
- _ = foo.nonexisting
231
-
232
- with test.assertRaisesRegex(AttributeError, r"`nonexisting` is not an attribute of 'foo' \([\w.]+\.Foo\)$"):
233
- wp.launch(
234
- kernel,
235
- dim=1,
236
- inputs=[Foo()],
237
- device=device,
238
- )
239
-
240
-
241
- @wp.kernel
242
- def test_struct_instantiate(data: wp.array(dtype=int)):
243
- baz = Baz(data, wp.vec3(0.0, 0.0, 26.0))
244
- bar = Bar(baz, 25.0)
245
- foo = Foo(bar, 24)
246
-
247
- wp.expect_eq(foo.x, 24)
248
- wp.expect_eq(foo.bar.y, 25.0)
249
- wp.expect_eq(foo.bar.baz.z[2], 26.0)
250
- wp.expect_eq(foo.bar.baz.data[0], 1)
251
-
252
-
253
- @wp.struct
254
- class MathThings:
255
- v1: wp.vec3
256
- v2: wp.vec3
257
- v3: wp.vec3
258
- m1: wp.mat22
259
- m2: wp.mat22
260
- m3: wp.mat22
261
- m4: wp.mat22
262
- m5: wp.mat22
263
- m6: wp.mat22
264
-
265
-
266
- @wp.kernel
267
- def check_math_conversions(s: MathThings):
268
- wp.expect_eq(s.v1, wp.vec3(1.0, 2.0, 3.0))
269
- wp.expect_eq(s.v2, wp.vec3(10.0, 20.0, 30.0))
270
- wp.expect_eq(s.v3, wp.vec3(100.0, 200.0, 300.0))
271
- wp.expect_eq(s.m1, wp.mat22(1.0, 2.0, 3.0, 4.0))
272
- wp.expect_eq(s.m2, wp.mat22(10.0, 20.0, 30.0, 40.0))
273
- wp.expect_eq(s.m3, wp.mat22(100.0, 200.0, 300.0, 400.0))
274
- wp.expect_eq(s.m4, wp.mat22(1.0, 2.0, 3.0, 4.0))
275
- wp.expect_eq(s.m5, wp.mat22(10.0, 20.0, 30.0, 40.0))
276
- wp.expect_eq(s.m6, wp.mat22(100.0, 200.0, 300.0, 400.0))
277
-
278
-
279
- def test_struct_math_conversions(test, device):
280
- s = MathThings()
281
-
282
- # test assigning various containers to vector and matrix attributes
283
- s.v1 = (1, 2, 3)
284
- s.v2 = [10, 20, 30]
285
- s.v3 = np.array([100, 200, 300])
286
- # 2d containers for matrices
287
- s.m1 = ((1, 2), (3, 4))
288
- s.m2 = [[10, 20], [30, 40]]
289
- s.m3 = np.array([[100, 200], [300, 400]])
290
- # 1d containers for matrices
291
- s.m4 = (1, 2, 3, 4)
292
- s.m5 = [10, 20, 30, 40]
293
- s.m6 = np.array([100, 200, 300, 400])
294
-
295
- wp.launch(check_math_conversions, dim=1, inputs=[s], device=device)
296
-
297
-
298
- @wp.struct
299
- class TestData:
300
- value: wp.int32
301
-
302
-
303
- @wp.func
304
- def GetTestData(value: wp.int32):
305
- return TestData(value * 2)
306
-
307
-
308
- @wp.kernel
309
- def test_return_struct(data: wp.array(dtype=wp.int32)):
310
- tid = wp.tid()
311
- data[tid] = GetTestData(tid).value
312
-
313
- wp.expect_eq(data[tid], tid * 2)
314
-
315
-
316
- @wp.struct
317
- class ReturnStruct:
318
- a: int
319
- b: int
320
-
321
-
322
- @wp.func
323
- def test_return_func():
324
- a = ReturnStruct(1, 2)
325
- return a
326
-
327
-
328
- @wp.kernel
329
- def test_return():
330
- t = test_return_func()
331
- wp.expect_eq(t.a, 1)
332
- wp.expect_eq(t.b, 2)
333
-
334
-
335
- @wp.struct
336
- class DefaultAttribNested:
337
- f: float
338
-
339
-
340
- @wp.struct
341
- class DefaultAttribStruct:
342
- i: int
343
- d: wp.float64
344
- v: wp.vec3
345
- m: wp.mat22
346
- a: wp.array(dtype=wp.int32)
347
- s: DefaultAttribNested
348
-
349
-
350
- @wp.func
351
- def check_default_attributes_func(data: DefaultAttribStruct):
352
- wp.expect_eq(data.i, wp.int32(0))
353
- wp.expect_eq(data.d, wp.float64(0))
354
- wp.expect_eq(data.v, wp.vec3(0.0, 0.0, 0.0))
355
- wp.expect_eq(data.m, wp.mat22(0.0, 0.0, 0.0, 0.0))
356
- wp.expect_eq(data.a.shape[0], 0)
357
- wp.expect_eq(data.s.f, wp.float32(0.0))
358
-
359
-
360
- @wp.kernel
361
- def check_default_attributes_kernel(data: DefaultAttribStruct):
362
- check_default_attributes_func(data)
363
-
364
-
365
- # check structs default initialized in Python correctly
366
- def test_struct_default_attributes_python(test, device):
367
- s = DefaultAttribStruct()
368
-
369
- wp.launch(check_default_attributes_kernel, dim=1, inputs=[s])
370
-
371
-
372
- # check structs default initialized in kernels correctly
373
- @wp.kernel
374
- def test_struct_default_attributes_kernel():
375
- s = DefaultAttribStruct()
376
-
377
- check_default_attributes_func(s)
378
-
379
-
380
- @wp.struct
381
- class MutableStruct:
382
- param1: int
383
- param2: float
384
-
385
-
386
- @wp.kernel
387
- def test_struct_mutate_attributes_kernel():
388
- t = MutableStruct()
389
- t.param1 = 1
390
- t.param2 = 1.1
391
-
392
- wp.expect_eq(t.param1, 1)
393
- wp.expect_eq(t.param2, 1.1)
394
-
395
-
396
- @wp.struct
397
- class InnerStruct:
398
- i: int
399
-
400
-
401
- @wp.struct
402
- class ArrayStruct:
403
- array: wp.array(dtype=InnerStruct)
404
-
405
-
406
- @wp.kernel
407
- def struct2_reader(test: ArrayStruct):
408
- k = wp.tid()
409
- wp.expect_eq(k + 1, test.array[k].i)
410
-
411
-
412
- def test_nested_array_struct(test, device):
413
- var1 = InnerStruct()
414
- var1.i = 1
415
-
416
- var2 = InnerStruct()
417
- var2.i = 2
418
-
419
- struct = ArrayStruct()
420
- struct.array = wp.array([var1, var2], dtype=InnerStruct, device=device)
421
-
422
- wp.launch(struct2_reader, dim=2, inputs=[struct], device=device)
423
-
424
-
425
- @wp.struct
426
- class EmptyNest1:
427
- a: Empty
428
- z: int
429
-
430
-
431
- @wp.struct
432
- class EmptyNest2:
433
- a: Empty
434
- b: Empty
435
- z: int
436
-
437
-
438
- @wp.struct
439
- class EmptyNest3:
440
- a: Empty
441
- b: Empty
442
- c: Empty
443
- z: int
444
-
445
-
446
- @wp.struct
447
- class EmptyNest4:
448
- a: Empty
449
- b: Empty
450
- c: Empty
451
- d: Empty
452
- z: int
453
-
454
-
455
- @wp.struct
456
- class EmptyNest5:
457
- a: Empty
458
- b: Empty
459
- c: Empty
460
- d: Empty
461
- e: Empty
462
- z: int
463
-
464
-
465
- @wp.struct
466
- class EmptyNest6:
467
- a: Empty
468
- b: Empty
469
- c: Empty
470
- d: Empty
471
- e: Empty
472
- f: Empty
473
- z: int
474
-
475
-
476
- @wp.struct
477
- class EmptyNest7:
478
- a: Empty
479
- b: Empty
480
- c: Empty
481
- d: Empty
482
- e: Empty
483
- f: Empty
484
- g: Empty
485
- z: int
486
-
487
-
488
- @wp.struct
489
- class EmptyNest8:
490
- a: Empty
491
- b: Empty
492
- c: Empty
493
- d: Empty
494
- e: Empty
495
- f: Empty
496
- g: Empty
497
- h: Empty
498
- z: int
499
-
500
-
501
- @wp.kernel
502
- def empty_nest_kernel(s: Any):
503
- wp.expect_eq(s.z, 42)
504
-
505
-
506
- wp.overload(empty_nest_kernel, [EmptyNest1])
507
- wp.overload(empty_nest_kernel, [EmptyNest2])
508
- wp.overload(empty_nest_kernel, [EmptyNest3])
509
- wp.overload(empty_nest_kernel, [EmptyNest4])
510
- wp.overload(empty_nest_kernel, [EmptyNest5])
511
- wp.overload(empty_nest_kernel, [EmptyNest6])
512
- wp.overload(empty_nest_kernel, [EmptyNest7])
513
- wp.overload(empty_nest_kernel, [EmptyNest8])
514
-
515
-
516
- def test_nested_empty_struct(test, device):
517
- with wp.ScopedDevice(device):
518
- e1 = EmptyNest1()
519
- e1.z = 42
520
- e2 = EmptyNest2()
521
- e2.z = 42
522
- e3 = EmptyNest3()
523
- e3.z = 42
524
- e4 = EmptyNest4()
525
- e4.z = 42
526
- e5 = EmptyNest5()
527
- e5.z = 42
528
- e6 = EmptyNest6()
529
- e6.z = 42
530
- e7 = EmptyNest7()
531
- e7.z = 42
532
- e8 = EmptyNest8()
533
- e8.z = 42
534
-
535
- wp.launch(empty_nest_kernel, dim=1, inputs=[e1])
536
- wp.launch(empty_nest_kernel, dim=1, inputs=[e2])
537
- wp.launch(empty_nest_kernel, dim=1, inputs=[e3])
538
- wp.launch(empty_nest_kernel, dim=1, inputs=[e4])
539
- wp.launch(empty_nest_kernel, dim=1, inputs=[e5])
540
- wp.launch(empty_nest_kernel, dim=1, inputs=[e6])
541
- wp.launch(empty_nest_kernel, dim=1, inputs=[e7])
542
- wp.launch(empty_nest_kernel, dim=1, inputs=[e8])
543
-
544
- wp.synchronize_device()
545
-
546
-
547
- @wp.struct
548
- class DependentModuleImport_A:
549
- s: StructFromAnotherModule
550
-
551
-
552
- @wp.struct
553
- class DependentModuleImport_B:
554
- s: StructFromAnotherModule
555
-
556
-
557
- @wp.struct
558
- class DependentModuleImport_C:
559
- a: DependentModuleImport_A
560
- b: DependentModuleImport_B
561
-
562
-
563
- @wp.kernel
564
- def test_dependent_module_import(c: DependentModuleImport_C):
565
- wp.tid() # nop, we're just testing codegen
566
-
567
-
568
- devices = get_test_devices()
569
-
570
-
571
- class TestStruct(unittest.TestCase):
572
- pass
573
-
574
-
575
- add_function_test(TestStruct, "test_step", test_step, devices=devices)
576
- add_function_test(TestStruct, "test_step_grad", test_step_grad, devices=devices)
577
- add_kernel_test(TestStruct, kernel=test_empty, name="test_empty", dim=1, inputs=[Empty()], devices=devices)
578
- add_kernel_test(
579
- TestStruct,
580
- kernel=test_uninitialized,
581
- name="test_uninitialized",
582
- dim=1,
583
- inputs=[Uninitialized()],
584
- devices=devices,
585
- )
586
- add_kernel_test(TestStruct, kernel=test_return, name="test_return", dim=1, inputs=[], devices=devices)
587
- add_function_test(TestStruct, "test_nested_struct", test_nested_struct, devices=devices)
588
- add_function_test(TestStruct, "test_nested_array_struct", test_nested_array_struct, devices=devices)
589
- add_function_test(TestStruct, "test_nested_empty_struct", test_nested_empty_struct, devices=devices)
590
- add_function_test(TestStruct, "test_struct_math_conversions", test_struct_math_conversions, devices=devices)
591
- add_function_test(
592
- TestStruct, "test_struct_default_attributes_python", test_struct_default_attributes_python, devices=devices
593
- )
594
- add_kernel_test(
595
- TestStruct,
596
- name="test_struct_default_attributes",
597
- kernel=test_struct_default_attributes_kernel,
598
- dim=1,
599
- inputs=[],
600
- devices=devices,
601
- )
602
-
603
- add_kernel_test(
604
- TestStruct,
605
- name="test_struct_mutate_attributes",
606
- kernel=test_struct_mutate_attributes_kernel,
607
- dim=1,
608
- inputs=[],
609
- devices=devices,
610
- )
611
- add_kernel_test(
612
- TestStruct,
613
- kernel=test_uninitialized,
614
- name="test_uninitialized",
615
- dim=1,
616
- inputs=[Uninitialized()],
617
- devices=devices,
618
- )
619
- add_kernel_test(TestStruct, kernel=test_return, name="test_return", dim=1, inputs=[], devices=devices)
620
- add_function_test(TestStruct, "test_nested_struct", test_nested_struct, devices=devices)
621
- add_function_test(TestStruct, "test_nested_array_struct", test_nested_array_struct, devices=devices)
622
- add_function_test(TestStruct, "test_nested_empty_struct", test_nested_empty_struct, devices=devices)
623
- add_function_test(TestStruct, "test_struct_math_conversions", test_struct_math_conversions, devices=devices)
624
- add_function_test(
625
- TestStruct, "test_struct_default_attributes_python", test_struct_default_attributes_python, devices=devices
626
- )
627
- add_kernel_test(
628
- TestStruct,
629
- name="test_struct_default_attributes",
630
- kernel=test_struct_default_attributes_kernel,
631
- dim=1,
632
- inputs=[],
633
- devices=devices,
634
- )
635
-
636
- add_kernel_test(
637
- TestStruct,
638
- name="test_struct_mutate_attributes",
639
- kernel=test_struct_mutate_attributes_kernel,
640
- dim=1,
641
- inputs=[],
642
- devices=devices,
643
- )
644
-
645
- for device in devices:
646
- add_kernel_test(
647
- TestStruct,
648
- kernel=test_struct_instantiate,
649
- name="test_struct_instantiate",
650
- dim=1,
651
- inputs=[wp.array([1], dtype=int, device=device)],
652
- devices=[device],
653
- )
654
- add_kernel_test(
655
- TestStruct,
656
- kernel=test_return_struct,
657
- name="test_return_struct",
658
- dim=1,
659
- inputs=[wp.zeros(10, dtype=int, device=device)],
660
- devices=[device],
661
- )
662
-
663
- add_kernel_test(
664
- TestStruct,
665
- kernel=test_dependent_module_import,
666
- name="test_dependent_module_import",
667
- dim=1,
668
- inputs=[DependentModuleImport_C()],
669
- devices=devices,
670
- )
671
-
672
-
673
- if __name__ == "__main__":
674
- wp.build.clear_kernel_cache()
675
- unittest.main(verbosity=2)
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
+ from typing import Any
10
+
11
+ import numpy as np
12
+
13
+ import warp as wp
14
+ from warp.fem import Sample as StructFromAnotherModule
15
+ from warp.tests.unittest_utils import *
16
+
17
+
18
+ @wp.struct
19
+ class Model:
20
+ dt: float
21
+ gravity: wp.vec3
22
+ m: wp.array(dtype=float)
23
+
24
+
25
+ @wp.struct
26
+ class State:
27
+ x: wp.array(dtype=wp.vec3)
28
+ v: wp.array(dtype=wp.vec3)
29
+
30
+
31
+ @wp.kernel
32
+ def kernel_step(state_in: State, state_out: State, model: Model):
33
+ i = wp.tid()
34
+
35
+ state_out.v[i] = state_in.v[i] + model.gravity / model.m[i] * model.dt
36
+ state_out.x[i] = state_in.x[i] + state_out.v[i] * model.dt
37
+
38
+
39
+ @wp.kernel
40
+ def kernel_step_with_copy(state_in: State, state_out: State, model: Model):
41
+ i = wp.tid()
42
+
43
+ model_rescaled = Model(1.0, model.gravity / model.m[i] * model.dt, model.m)
44
+
45
+ state_out_copy = State(state_out.x, state_out.v)
46
+ state_out_copy.v[i] = state_in.v[i] + model_rescaled.gravity
47
+ state_out_copy.x[i] = state_in.x[i] + state_out_copy.v[i] * model.dt
48
+
49
+
50
+ def test_step(test, device):
51
+ rng = np.random.default_rng(123)
52
+
53
+ dim = 5
54
+
55
+ dt = 0.01
56
+ gravity = np.array([0, 0, -9.81])
57
+
58
+ m = np.ones(dim)
59
+
60
+ m_model = wp.array(m, dtype=float, device=device)
61
+
62
+ model = Model()
63
+ model.m = m_model
64
+ model.dt = dt
65
+ model.gravity = wp.vec3(0, 0, -9.81)
66
+
67
+ x = rng.normal(size=(dim, 3))
68
+ v = rng.normal(size=(dim, 3))
69
+
70
+ x_expected = x + (v + gravity / m[:, None] * dt) * dt
71
+
72
+ x_in = wp.array(x, dtype=wp.vec3, device=device)
73
+ v_in = wp.array(v, dtype=wp.vec3, device=device)
74
+
75
+ state_in = State()
76
+ state_in.x = x_in
77
+ state_in.v = v_in
78
+
79
+ state_out = State()
80
+ state_out.x = wp.empty_like(x_in)
81
+ state_out.v = wp.empty_like(v_in)
82
+
83
+ for step_kernel in [kernel_step, kernel_step_with_copy]:
84
+ with CheckOutput(test):
85
+ wp.launch(step_kernel, dim=dim, inputs=[state_in, state_out, model], device=device)
86
+
87
+ assert_np_equal(state_out.x.numpy(), x_expected, tol=1e-6)
88
+
89
+
90
+ @wp.kernel
91
+ def kernel_loss(x: wp.array(dtype=wp.vec3), loss: wp.array(dtype=float)):
92
+ i = wp.tid()
93
+ wp.atomic_add(loss, 0, x[i][0] * x[i][0] + x[i][1] * x[i][1] + x[i][2] * x[i][2])
94
+
95
+
96
+ def test_step_grad(test, device):
97
+ rng = np.random.default_rng(123)
98
+
99
+ dim = 5
100
+
101
+ dt = 0.01
102
+ gravity = np.array([0, 0, -9.81])
103
+
104
+ m = rng.random(size=dim) + 0.1
105
+
106
+ m_model = wp.array(m, dtype=float, device=device, requires_grad=True)
107
+
108
+ model = Model()
109
+ model.m = m_model
110
+ model.dt = dt
111
+ model.gravity = wp.vec3(0, 0, -9.81)
112
+
113
+ x = rng.normal(size=(dim, 3))
114
+ v = rng.normal(size=(dim, 3))
115
+
116
+ x_in = wp.array(x, dtype=wp.vec3, device=device, requires_grad=True)
117
+ v_in = wp.array(v, dtype=wp.vec3, device=device, requires_grad=True)
118
+
119
+ state_in = State()
120
+ state_in.x = x_in
121
+ state_in.v = v_in
122
+
123
+ state_out = State()
124
+ state_out.x = wp.empty_like(x_in, requires_grad=True)
125
+ state_out.v = wp.empty_like(v_in, requires_grad=True)
126
+
127
+ loss = wp.empty(1, dtype=float, device=device, requires_grad=True)
128
+
129
+ for step_kernel in [kernel_step, kernel_step_with_copy]:
130
+ tape = wp.Tape()
131
+
132
+ with tape:
133
+ wp.launch(step_kernel, dim=dim, inputs=[state_in, state_out, model], device=device)
134
+ wp.launch(kernel_loss, dim=dim, inputs=[state_out.x, loss], device=device)
135
+
136
+ tape.backward(loss)
137
+
138
+ dl_dx = 2 * state_out.x.numpy()
139
+ dl_dv = dl_dx * dt
140
+
141
+ dv_dm = -gravity * dt / m[:, None] ** 2
142
+ dl_dm = (dl_dv * dv_dm).sum(-1)
143
+
144
+ assert_np_equal(state_out.x.grad.numpy(), dl_dx, tol=1e-6)
145
+ assert_np_equal(state_in.x.grad.numpy(), dl_dx, tol=1e-6)
146
+ assert_np_equal(state_out.v.grad.numpy(), dl_dv, tol=1e-6)
147
+ assert_np_equal(state_in.v.grad.numpy(), dl_dv, tol=1e-6)
148
+ assert_np_equal(model.m.grad.numpy(), dl_dm, tol=1e-6)
149
+
150
+ tape.zero()
151
+
152
+ assert state_out.x.grad.numpy().sum() == 0.0
153
+ assert state_in.x.grad.numpy().sum() == 0.0
154
+ assert state_out.v.grad.numpy().sum() == 0.0
155
+ assert state_in.v.grad.numpy().sum() == 0.0
156
+ assert model.m.grad.numpy().sum() == 0.0
157
+
158
+
159
+ @wp.struct
160
+ class Empty:
161
+ pass
162
+
163
+
164
+ @wp.kernel
165
+ def test_empty(input: Empty):
166
+ tid = wp.tid()
167
+
168
+
169
+ @wp.struct
170
+ class Uninitialized:
171
+ data: wp.array(dtype=int)
172
+
173
+
174
+ @wp.kernel
175
+ def test_uninitialized(input: Uninitialized):
176
+ tid = wp.tid()
177
+
178
+
179
+ @wp.struct
180
+ class Baz:
181
+ data: wp.array(dtype=int)
182
+ z: wp.vec3
183
+
184
+
185
+ @wp.struct
186
+ class Bar:
187
+ baz: Baz
188
+ y: float
189
+
190
+
191
+ @wp.struct
192
+ class Foo:
193
+ bar: Bar
194
+ x: int
195
+
196
+
197
+ @wp.kernel
198
+ def kernel_nested_struct(foo: Foo):
199
+ tid = wp.tid()
200
+ foo.bar.baz.data[tid] = (
201
+ foo.bar.baz.data[tid] + foo.x + int(foo.bar.y * 100.0) + int(wp.length_sq(foo.bar.baz.z)) + tid * 2
202
+ )
203
+
204
+
205
+ def test_nested_struct(test, device):
206
+ dim = 3
207
+
208
+ foo = Foo()
209
+ foo.bar = Bar()
210
+ foo.bar.baz = Baz()
211
+ foo.bar.baz.data = wp.zeros(dim, dtype=int, device=device)
212
+ foo.bar.baz.z = wp.vec3(1, 2, 3)
213
+ foo.bar.y = 1.23
214
+ foo.x = 123
215
+
216
+ wp.launch(kernel_nested_struct, dim=dim, inputs=[foo], device=device)
217
+
218
+ assert_array_equal(
219
+ foo.bar.baz.data,
220
+ wp.array((260, 262, 264), dtype=int, device=device),
221
+ )
222
+
223
+
224
+ def test_struct_attribute_error(test, device):
225
+ @wp.kernel
226
+ def kernel(foo: Foo):
227
+ _ = foo.nonexisting
228
+
229
+ with test.assertRaisesRegex(AttributeError, r"`nonexisting` is not an attribute of 'foo' \([\w.]+\.Foo\)$"):
230
+ wp.launch(
231
+ kernel,
232
+ dim=1,
233
+ inputs=[Foo()],
234
+ device=device,
235
+ )
236
+
237
+
238
+ @wp.kernel
239
+ def test_struct_instantiate(data: wp.array(dtype=int)):
240
+ baz = Baz(data, wp.vec3(0.0, 0.0, 26.0))
241
+ bar = Bar(baz, 25.0)
242
+ foo = Foo(bar, 24)
243
+
244
+ wp.expect_eq(foo.x, 24)
245
+ wp.expect_eq(foo.bar.y, 25.0)
246
+ wp.expect_eq(foo.bar.baz.z[2], 26.0)
247
+ wp.expect_eq(foo.bar.baz.data[0], 1)
248
+
249
+
250
+ @wp.struct
251
+ class MathThings:
252
+ v1: wp.vec3
253
+ v2: wp.vec3
254
+ v3: wp.vec3
255
+ m1: wp.mat22
256
+ m2: wp.mat22
257
+ m3: wp.mat22
258
+ m4: wp.mat22
259
+ m5: wp.mat22
260
+ m6: wp.mat22
261
+
262
+
263
+ @wp.kernel
264
+ def check_math_conversions(s: MathThings):
265
+ wp.expect_eq(s.v1, wp.vec3(1.0, 2.0, 3.0))
266
+ wp.expect_eq(s.v2, wp.vec3(10.0, 20.0, 30.0))
267
+ wp.expect_eq(s.v3, wp.vec3(100.0, 200.0, 300.0))
268
+ wp.expect_eq(s.m1, wp.mat22(1.0, 2.0, 3.0, 4.0))
269
+ wp.expect_eq(s.m2, wp.mat22(10.0, 20.0, 30.0, 40.0))
270
+ wp.expect_eq(s.m3, wp.mat22(100.0, 200.0, 300.0, 400.0))
271
+ wp.expect_eq(s.m4, wp.mat22(1.0, 2.0, 3.0, 4.0))
272
+ wp.expect_eq(s.m5, wp.mat22(10.0, 20.0, 30.0, 40.0))
273
+ wp.expect_eq(s.m6, wp.mat22(100.0, 200.0, 300.0, 400.0))
274
+
275
+
276
+ def test_struct_math_conversions(test, device):
277
+ s = MathThings()
278
+
279
+ # test assigning various containers to vector and matrix attributes
280
+ s.v1 = (1, 2, 3)
281
+ s.v2 = [10, 20, 30]
282
+ s.v3 = np.array([100, 200, 300])
283
+ # 2d containers for matrices
284
+ s.m1 = ((1, 2), (3, 4))
285
+ s.m2 = [[10, 20], [30, 40]]
286
+ s.m3 = np.array([[100, 200], [300, 400]])
287
+ # 1d containers for matrices
288
+ s.m4 = (1, 2, 3, 4)
289
+ s.m5 = [10, 20, 30, 40]
290
+ s.m6 = np.array([100, 200, 300, 400])
291
+
292
+ wp.launch(check_math_conversions, dim=1, inputs=[s], device=device)
293
+
294
+
295
+ @wp.struct
296
+ class TestData:
297
+ value: wp.int32
298
+
299
+
300
+ @wp.func
301
+ def GetTestData(value: wp.int32):
302
+ return TestData(value * 2)
303
+
304
+
305
+ @wp.kernel
306
+ def test_return_struct(data: wp.array(dtype=wp.int32)):
307
+ tid = wp.tid()
308
+ data[tid] = GetTestData(tid).value
309
+
310
+ wp.expect_eq(data[tid], tid * 2)
311
+
312
+
313
+ @wp.struct
314
+ class ReturnStruct:
315
+ a: int
316
+ b: int
317
+
318
+
319
+ @wp.func
320
+ def test_return_func():
321
+ a = ReturnStruct(1, 2)
322
+ return a
323
+
324
+
325
+ @wp.kernel
326
+ def test_return():
327
+ t = test_return_func()
328
+ wp.expect_eq(t.a, 1)
329
+ wp.expect_eq(t.b, 2)
330
+
331
+
332
+ @wp.struct
333
+ class DefaultAttribNested:
334
+ f: float
335
+
336
+
337
+ @wp.struct
338
+ class DefaultAttribStruct:
339
+ i: int
340
+ d: wp.float64
341
+ v: wp.vec3
342
+ m: wp.mat22
343
+ a: wp.array(dtype=wp.int32)
344
+ s: DefaultAttribNested
345
+
346
+
347
+ @wp.func
348
+ def check_default_attributes_func(data: DefaultAttribStruct):
349
+ wp.expect_eq(data.i, wp.int32(0))
350
+ wp.expect_eq(data.d, wp.float64(0))
351
+ wp.expect_eq(data.v, wp.vec3(0.0, 0.0, 0.0))
352
+ wp.expect_eq(data.m, wp.mat22(0.0, 0.0, 0.0, 0.0))
353
+ wp.expect_eq(data.a.shape[0], 0)
354
+ wp.expect_eq(data.s.f, wp.float32(0.0))
355
+
356
+
357
+ @wp.kernel
358
+ def check_default_attributes_kernel(data: DefaultAttribStruct):
359
+ check_default_attributes_func(data)
360
+
361
+
362
+ # check structs default initialized in kernels correctly
363
+ @wp.kernel
364
+ def test_struct_default_attributes_kernel():
365
+ s = DefaultAttribStruct()
366
+
367
+ check_default_attributes_func(s)
368
+
369
+
370
+ @wp.struct
371
+ class MutableStruct:
372
+ param1: int
373
+ param2: float
374
+
375
+
376
+ @wp.kernel
377
+ def test_struct_mutate_attributes_kernel():
378
+ t = MutableStruct()
379
+ t.param1 = 1
380
+ t.param2 = 1.1
381
+
382
+ wp.expect_eq(t.param1, 1)
383
+ wp.expect_eq(t.param2, 1.1)
384
+
385
+
386
+ @wp.struct
387
+ class InnerStruct:
388
+ i: int
389
+
390
+
391
+ @wp.struct
392
+ class ArrayStruct:
393
+ array: wp.array(dtype=InnerStruct)
394
+
395
+
396
+ @wp.kernel
397
+ def struct2_reader(test: ArrayStruct):
398
+ k = wp.tid()
399
+ wp.expect_eq(k + 1, test.array[k].i)
400
+
401
+
402
+ def test_nested_array_struct(test, device):
403
+ var1 = InnerStruct()
404
+ var1.i = 1
405
+
406
+ var2 = InnerStruct()
407
+ var2.i = 2
408
+
409
+ struct = ArrayStruct()
410
+ struct.array = wp.array([var1, var2], dtype=InnerStruct, device=device)
411
+
412
+ wp.launch(struct2_reader, dim=2, inputs=[struct], device=device)
413
+
414
+
415
+ @wp.struct
416
+ class VecStruct:
417
+ value: wp.vec3
418
+
419
+
420
+ @wp.struct
421
+ class Bar2:
422
+ z: wp.array(dtype=float)
423
+
424
+
425
+ @wp.struct
426
+ class Foo2:
427
+ x: wp.array(dtype=float)
428
+ y: Bar2
429
+
430
+
431
+ def test_convert_to_device(test, device):
432
+ foo = Foo2()
433
+ foo.x = wp.array((1.23, 2.34), dtype=float, device=device)
434
+ foo.y = Bar2()
435
+ foo.y.z = wp.array((3.45, 4.56), dtype=float, device=device)
436
+
437
+ if device.is_cpu and wp.is_cuda_available():
438
+ dst_device = "cuda:0"
439
+ elif device.is_cuda and wp.is_cpu_available():
440
+ dst_device = "cpu"
441
+ else:
442
+ return
443
+
444
+ result = foo.to(dst_device)
445
+ assert result.x.device == dst_device
446
+ assert result.y.z.device == dst_device
447
+
448
+
449
+ @wp.struct
450
+ class EmptyNest1:
451
+ a: Empty
452
+ z: int
453
+
454
+
455
+ @wp.struct
456
+ class EmptyNest2:
457
+ a: Empty
458
+ b: Empty
459
+ z: int
460
+
461
+
462
+ @wp.struct
463
+ class EmptyNest3:
464
+ a: Empty
465
+ b: Empty
466
+ c: Empty
467
+ z: int
468
+
469
+
470
+ @wp.struct
471
+ class EmptyNest4:
472
+ a: Empty
473
+ b: Empty
474
+ c: Empty
475
+ d: Empty
476
+ z: int
477
+
478
+
479
+ @wp.struct
480
+ class EmptyNest5:
481
+ a: Empty
482
+ b: Empty
483
+ c: Empty
484
+ d: Empty
485
+ e: Empty
486
+ z: int
487
+
488
+
489
+ @wp.struct
490
+ class EmptyNest6:
491
+ a: Empty
492
+ b: Empty
493
+ c: Empty
494
+ d: Empty
495
+ e: Empty
496
+ f: Empty
497
+ z: int
498
+
499
+
500
+ @wp.struct
501
+ class EmptyNest7:
502
+ a: Empty
503
+ b: Empty
504
+ c: Empty
505
+ d: Empty
506
+ e: Empty
507
+ f: Empty
508
+ g: Empty
509
+ z: int
510
+
511
+
512
+ @wp.struct
513
+ class EmptyNest8:
514
+ a: Empty
515
+ b: Empty
516
+ c: Empty
517
+ d: Empty
518
+ e: Empty
519
+ f: Empty
520
+ g: Empty
521
+ h: Empty
522
+ z: int
523
+
524
+
525
+ @wp.kernel
526
+ def empty_nest_kernel(s: Any):
527
+ wp.expect_eq(s.z, 42)
528
+
529
+
530
+ wp.overload(empty_nest_kernel, [EmptyNest1])
531
+ wp.overload(empty_nest_kernel, [EmptyNest2])
532
+ wp.overload(empty_nest_kernel, [EmptyNest3])
533
+ wp.overload(empty_nest_kernel, [EmptyNest4])
534
+ wp.overload(empty_nest_kernel, [EmptyNest5])
535
+ wp.overload(empty_nest_kernel, [EmptyNest6])
536
+ wp.overload(empty_nest_kernel, [EmptyNest7])
537
+ wp.overload(empty_nest_kernel, [EmptyNest8])
538
+
539
+
540
+ def test_nested_empty_struct(test, device):
541
+ with wp.ScopedDevice(device):
542
+ e1 = EmptyNest1()
543
+ e1.z = 42
544
+ e2 = EmptyNest2()
545
+ e2.z = 42
546
+ e3 = EmptyNest3()
547
+ e3.z = 42
548
+ e4 = EmptyNest4()
549
+ e4.z = 42
550
+ e5 = EmptyNest5()
551
+ e5.z = 42
552
+ e6 = EmptyNest6()
553
+ e6.z = 42
554
+ e7 = EmptyNest7()
555
+ e7.z = 42
556
+ e8 = EmptyNest8()
557
+ e8.z = 42
558
+
559
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e1])
560
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e2])
561
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e3])
562
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e4])
563
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e5])
564
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e6])
565
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e7])
566
+ wp.launch(empty_nest_kernel, dim=1, inputs=[e8])
567
+
568
+ wp.synchronize_device()
569
+
570
+
571
+ @wp.struct
572
+ class DependentModuleImport_A:
573
+ s: StructFromAnotherModule
574
+
575
+
576
+ @wp.struct
577
+ class DependentModuleImport_B:
578
+ s: StructFromAnotherModule
579
+
580
+
581
+ @wp.struct
582
+ class DependentModuleImport_C:
583
+ a: DependentModuleImport_A
584
+ b: DependentModuleImport_B
585
+
586
+
587
+ @wp.kernel
588
+ def test_dependent_module_import(c: DependentModuleImport_C):
589
+ wp.tid() # nop, we're just testing codegen
590
+
591
+
592
+ devices = get_test_devices()
593
+
594
+
595
+ class TestStruct(unittest.TestCase):
596
+ # check structs default initialized in Python correctly
597
+ def test_struct_default_attributes_python(self):
598
+ s = DefaultAttribStruct()
599
+
600
+ wp.launch(check_default_attributes_kernel, dim=1, inputs=[s])
601
+
602
+ def test_nested_vec_assignment(self):
603
+ v = VecStruct()
604
+ v.value[0] = 1.0
605
+ v.value[1] = 2.0
606
+ v.value[2] = 3.0
607
+
608
+ arr = wp.array([v], dtype=VecStruct)
609
+ expected = np.array(([1.0, 2.0, 3.0],))
610
+ assert np.all(arr.numpy().tolist() == expected)
611
+
612
+
613
+ add_function_test(TestStruct, "test_step", test_step, devices=devices)
614
+ add_function_test(TestStruct, "test_step_grad", test_step_grad, devices=devices)
615
+ add_kernel_test(TestStruct, kernel=test_empty, name="test_empty", dim=1, inputs=[Empty()], devices=devices)
616
+ add_kernel_test(
617
+ TestStruct,
618
+ kernel=test_uninitialized,
619
+ name="test_uninitialized",
620
+ dim=1,
621
+ inputs=[Uninitialized()],
622
+ devices=devices,
623
+ )
624
+ add_kernel_test(TestStruct, kernel=test_return, name="test_return", dim=1, inputs=[], devices=devices)
625
+ add_function_test(TestStruct, "test_nested_struct", test_nested_struct, devices=devices)
626
+ add_function_test(TestStruct, "test_nested_array_struct", test_nested_array_struct, devices=devices)
627
+ add_function_test(TestStruct, "test_convert_to_device", test_convert_to_device, devices=devices)
628
+ add_function_test(TestStruct, "test_nested_empty_struct", test_nested_empty_struct, devices=devices)
629
+ add_function_test(TestStruct, "test_struct_math_conversions", test_struct_math_conversions, devices=devices)
630
+ add_kernel_test(
631
+ TestStruct,
632
+ name="test_struct_default_attributes",
633
+ kernel=test_struct_default_attributes_kernel,
634
+ dim=1,
635
+ inputs=[],
636
+ devices=devices,
637
+ )
638
+
639
+ add_kernel_test(
640
+ TestStruct,
641
+ name="test_struct_mutate_attributes",
642
+ kernel=test_struct_mutate_attributes_kernel,
643
+ dim=1,
644
+ inputs=[],
645
+ devices=devices,
646
+ )
647
+ add_kernel_test(
648
+ TestStruct,
649
+ kernel=test_uninitialized,
650
+ name="test_uninitialized",
651
+ dim=1,
652
+ inputs=[Uninitialized()],
653
+ devices=devices,
654
+ )
655
+ add_kernel_test(TestStruct, kernel=test_return, name="test_return", dim=1, inputs=[], devices=devices)
656
+ add_function_test(TestStruct, "test_nested_struct", test_nested_struct, devices=devices)
657
+ add_function_test(TestStruct, "test_nested_array_struct", test_nested_array_struct, devices=devices)
658
+ add_function_test(TestStruct, "test_nested_empty_struct", test_nested_empty_struct, devices=devices)
659
+ add_function_test(TestStruct, "test_struct_math_conversions", test_struct_math_conversions, devices=devices)
660
+ add_kernel_test(
661
+ TestStruct,
662
+ name="test_struct_default_attributes",
663
+ kernel=test_struct_default_attributes_kernel,
664
+ dim=1,
665
+ inputs=[],
666
+ devices=devices,
667
+ )
668
+
669
+ add_kernel_test(
670
+ TestStruct,
671
+ name="test_struct_mutate_attributes",
672
+ kernel=test_struct_mutate_attributes_kernel,
673
+ dim=1,
674
+ inputs=[],
675
+ devices=devices,
676
+ )
677
+
678
+ for device in devices:
679
+ add_kernel_test(
680
+ TestStruct,
681
+ kernel=test_struct_instantiate,
682
+ name="test_struct_instantiate",
683
+ dim=1,
684
+ inputs=[wp.array([1], dtype=int, device=device)],
685
+ devices=[device],
686
+ )
687
+ add_kernel_test(
688
+ TestStruct,
689
+ kernel=test_return_struct,
690
+ name="test_return_struct",
691
+ dim=1,
692
+ inputs=[wp.zeros(10, dtype=int, device=device)],
693
+ devices=[device],
694
+ )
695
+
696
+ add_kernel_test(
697
+ TestStruct,
698
+ kernel=test_dependent_module_import,
699
+ name="test_dependent_module_import",
700
+ dim=1,
701
+ inputs=[DependentModuleImport_C()],
702
+ devices=devices,
703
+ )
704
+
705
+
706
+ if __name__ == "__main__":
707
+ wp.build.clear_kernel_cache()
708
+ unittest.main(verbosity=2)