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_array.py CHANGED
@@ -1,2326 +1,2415 @@
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
-
18
- @wp.kernel
19
- def kernel_1d(a: wp.array(dtype=int, ndim=1)):
20
- i = wp.tid()
21
-
22
- wp.expect_eq(a[i], wp.tid())
23
-
24
- a[i] = a[i] * 2
25
- wp.atomic_add(a, i, 1)
26
-
27
- wp.expect_eq(a[i], wp.tid() * 2 + 1)
28
-
29
-
30
- def test_1d(test, device):
31
- dim_x = 4
32
-
33
- a = np.arange(0, dim_x, dtype=np.int32)
34
-
35
- arr = wp.array(a, device=device)
36
-
37
- test.assertEqual(arr.shape, a.shape)
38
- test.assertEqual(arr.size, a.size)
39
- test.assertEqual(arr.ndim, a.ndim)
40
-
41
- with CheckOutput(test):
42
- wp.launch(kernel_1d, dim=arr.size, inputs=[arr], device=device)
43
-
44
-
45
- @wp.kernel
46
- def kernel_2d(a: wp.array(dtype=int, ndim=2), m: int, n: int):
47
- i = wp.tid() // n
48
- j = wp.tid() % n
49
-
50
- wp.expect_eq(a[i, j], wp.tid())
51
- wp.expect_eq(a[i][j], wp.tid())
52
-
53
- a[i, j] = a[i, j] * 2
54
- wp.atomic_add(a, i, j, 1)
55
-
56
- wp.expect_eq(a[i, j], wp.tid() * 2 + 1)
57
-
58
-
59
- def test_2d(test, device):
60
- dim_x = 4
61
- dim_y = 2
62
-
63
- a = np.arange(0, dim_x * dim_y, dtype=np.int32)
64
- a = a.reshape(dim_x, dim_y)
65
-
66
- arr = wp.array(a, device=device)
67
-
68
- test.assertEqual(arr.shape, a.shape)
69
- test.assertEqual(arr.size, a.size)
70
- test.assertEqual(arr.ndim, a.ndim)
71
-
72
- with CheckOutput(test):
73
- wp.launch(kernel_2d, dim=arr.size, inputs=[arr, dim_x, dim_y], device=device)
74
-
75
-
76
- @wp.kernel
77
- def kernel_3d(a: wp.array(dtype=int, ndim=3), m: int, n: int, o: int):
78
- i = wp.tid() // (n * o)
79
- j = wp.tid() % (n * o) // o
80
- k = wp.tid() % o
81
-
82
- wp.expect_eq(a[i, j, k], wp.tid())
83
- wp.expect_eq(a[i][j][k], wp.tid())
84
-
85
- a[i, j, k] = a[i, j, k] * 2
86
- a[i][j][k] = a[i][j][k] * 2
87
- wp.atomic_add(a, i, j, k, 1)
88
-
89
- wp.expect_eq(a[i, j, k], wp.tid() * 4 + 1)
90
-
91
-
92
- def test_3d(test, device):
93
- dim_x = 8
94
- dim_y = 4
95
- dim_z = 2
96
-
97
- a = np.arange(0, dim_x * dim_y * dim_z, dtype=np.int32)
98
- a = a.reshape(dim_x, dim_y, dim_z)
99
-
100
- arr = wp.array(a, device=device)
101
-
102
- test.assertEqual(arr.shape, a.shape)
103
- test.assertEqual(arr.size, a.size)
104
- test.assertEqual(arr.ndim, a.ndim)
105
-
106
- with CheckOutput(test):
107
- wp.launch(kernel_3d, dim=arr.size, inputs=[arr, dim_x, dim_y, dim_z], device=device)
108
-
109
-
110
- @wp.kernel
111
- def kernel_4d(a: wp.array(dtype=int, ndim=4), m: int, n: int, o: int, p: int):
112
- i = wp.tid() // (n * o * p)
113
- j = wp.tid() % (n * o * p) // (o * p)
114
- k = wp.tid() % (o * p) / p
115
- l = wp.tid() % p
116
-
117
- wp.expect_eq(a[i, j, k, l], wp.tid())
118
- wp.expect_eq(a[i][j][k][l], wp.tid())
119
-
120
-
121
- def test_4d(test, device):
122
- dim_x = 16
123
- dim_y = 8
124
- dim_z = 4
125
- dim_w = 2
126
-
127
- a = np.arange(0, dim_x * dim_y * dim_z * dim_w, dtype=np.int32)
128
- a = a.reshape(dim_x, dim_y, dim_z, dim_w)
129
-
130
- arr = wp.array(a, device=device)
131
-
132
- test.assertEqual(arr.shape, a.shape)
133
- test.assertEqual(arr.size, a.size)
134
- test.assertEqual(arr.ndim, a.ndim)
135
-
136
- with CheckOutput(test):
137
- wp.launch(kernel_4d, dim=arr.size, inputs=[arr, dim_x, dim_y, dim_z, dim_w], device=device)
138
-
139
-
140
- @wp.kernel
141
- def kernel_4d_transposed(a: wp.array(dtype=int, ndim=4), m: int, n: int, o: int, p: int):
142
- i = wp.tid() // (n * o * p)
143
- j = wp.tid() % (n * o * p) // (o * p)
144
- k = wp.tid() % (o * p) / p
145
- l = wp.tid() % p
146
-
147
- wp.expect_eq(a[l, k, j, i], wp.tid())
148
- wp.expect_eq(a[l][k][j][i], wp.tid())
149
-
150
-
151
- def test_4d_transposed(test, device):
152
- dim_x = 16
153
- dim_y = 8
154
- dim_z = 4
155
- dim_w = 2
156
-
157
- a = np.arange(0, dim_x * dim_y * dim_z * dim_w, dtype=np.int32)
158
- a = a.reshape(dim_x, dim_y, dim_z, dim_w)
159
-
160
- arr = wp.array(a, device=device)
161
-
162
- # Transpose the array manually, as using the wp.array() constructor with arr.T would make it contiguous first
163
- a_T = a.T
164
- arr_T = wp.array(
165
- dtype=arr.dtype,
166
- shape=a_T.shape,
167
- strides=a_T.__array_interface__["strides"],
168
- capacity=arr.capacity,
169
- ptr=arr.ptr,
170
- requires_grad=arr.requires_grad,
171
- device=device,
172
- )
173
-
174
- test.assertFalse(arr_T.is_contiguous)
175
- test.assertEqual(arr_T.shape, a_T.shape)
176
- test.assertEqual(arr_T.strides, a_T.__array_interface__["strides"])
177
- test.assertEqual(arr_T.size, a_T.size)
178
- test.assertEqual(arr_T.ndim, a_T.ndim)
179
-
180
- with CheckOutput(test):
181
- wp.launch(kernel_4d_transposed, dim=arr_T.size, inputs=[arr_T, dim_x, dim_y, dim_z, dim_w], device=device)
182
-
183
-
184
- @wp.kernel
185
- def lower_bound_kernel(values: wp.array(dtype=float), arr: wp.array(dtype=float), indices: wp.array(dtype=int)):
186
- tid = wp.tid()
187
-
188
- indices[tid] = wp.lower_bound(arr, values[tid])
189
-
190
-
191
- def test_lower_bound(test, device):
192
- arr = wp.array(np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float), dtype=float, device=device)
193
- values = wp.array(np.array([-0.1, 0.0, 2.5, 4.0, 5.0, 5.5], dtype=float), dtype=float, device=device)
194
- indices = wp.zeros(6, dtype=int, device=device)
195
-
196
- wp.launch(kernel=lower_bound_kernel, dim=6, inputs=[values, arr, indices], device=device)
197
-
198
- test.assertTrue((np.array([0, 0, 3, 4, 5, 5]) == indices.numpy()).all())
199
-
200
-
201
- @wp.kernel
202
- def f1(arr: wp.array(dtype=float)):
203
- wp.expect_eq(arr.shape[0], 10)
204
-
205
-
206
- @wp.kernel
207
- def f2(arr: wp.array2d(dtype=float)):
208
- wp.expect_eq(arr.shape[0], 10)
209
- wp.expect_eq(arr.shape[1], 20)
210
-
211
- slice = arr[0]
212
- wp.expect_eq(slice.shape[0], 20)
213
-
214
-
215
- @wp.kernel
216
- def f3(arr: wp.array3d(dtype=float)):
217
- wp.expect_eq(arr.shape[0], 10)
218
- wp.expect_eq(arr.shape[1], 20)
219
- wp.expect_eq(arr.shape[2], 30)
220
-
221
- slice = arr[0, 0]
222
- wp.expect_eq(slice.shape[0], 30)
223
-
224
-
225
- @wp.kernel
226
- def f4(arr: wp.array4d(dtype=float)):
227
- wp.expect_eq(arr.shape[0], 10)
228
- wp.expect_eq(arr.shape[1], 20)
229
- wp.expect_eq(arr.shape[2], 30)
230
- wp.expect_eq(arr.shape[3], 40)
231
-
232
- slice = arr[0, 0, 0]
233
- wp.expect_eq(slice.shape[0], 40)
234
-
235
-
236
- def test_shape(test, device):
237
- with CheckOutput(test):
238
- a1 = wp.zeros(dtype=float, shape=10, device=device)
239
- wp.launch(f1, dim=1, inputs=[a1], device=device)
240
-
241
- a2 = wp.zeros(dtype=float, shape=(10, 20), device=device)
242
- wp.launch(f2, dim=1, inputs=[a2], device=device)
243
-
244
- a3 = wp.zeros(dtype=float, shape=(10, 20, 30), device=device)
245
- wp.launch(f3, dim=1, inputs=[a3], device=device)
246
-
247
- a4 = wp.zeros(dtype=float, shape=(10, 20, 30, 40), device=device)
248
- wp.launch(f4, dim=1, inputs=[a4], device=device)
249
-
250
-
251
- def test_negative_shape(test, device):
252
- with test.assertRaisesRegex(ValueError, "Array shapes must be non-negative"):
253
- _ = wp.zeros(shape=-1, dtype=int, device=device)
254
-
255
- with test.assertRaisesRegex(ValueError, "Array shapes must be non-negative"):
256
- _ = wp.zeros(shape=-(2**32), dtype=int, device=device)
257
-
258
- with test.assertRaisesRegex(ValueError, "Array shapes must be non-negative"):
259
- _ = wp.zeros(shape=(10, -1), dtype=int, device=device)
260
-
261
-
262
- @wp.kernel
263
- def sum_array(arr: wp.array(dtype=float), loss: wp.array(dtype=float)):
264
- tid = wp.tid()
265
- wp.atomic_add(loss, 0, arr[tid])
266
-
267
-
268
- def test_flatten(test, device):
269
- np_arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=float)
270
- arr = wp.array(np_arr, dtype=float, shape=np_arr.shape, device=device, requires_grad=True)
271
- arr_flat = arr.flatten()
272
- arr_comp = wp.array(np_arr.flatten(), dtype=float, device=device)
273
- assert_array_equal(arr_flat, arr_comp)
274
-
275
- loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
276
- tape = wp.Tape()
277
- with tape:
278
- wp.launch(kernel=sum_array, dim=len(arr_flat), inputs=[arr_flat, loss], device=device)
279
-
280
- tape.backward(loss=loss)
281
- grad = tape.gradients[arr_flat]
282
-
283
- ones = wp.array(
284
- np.ones(
285
- (8,),
286
- dtype=float,
287
- ),
288
- dtype=float,
289
- device=device,
290
- )
291
- assert_array_equal(grad, ones)
292
- test.assertEqual(loss.numpy()[0], 36)
293
-
294
-
295
- def test_reshape(test, device):
296
- np_arr = np.arange(6, dtype=float)
297
- arr = wp.array(np_arr, dtype=float, device=device, requires_grad=True)
298
- arr_reshaped = arr.reshape((3, 2))
299
- arr_comp = wp.array(np_arr.reshape((3, 2)), dtype=float, device=device)
300
- assert_array_equal(arr_reshaped, arr_comp)
301
-
302
- arr_reshaped = arr_reshaped.reshape(6)
303
- assert_array_equal(arr_reshaped, arr)
304
-
305
- loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
306
- tape = wp.Tape()
307
- with tape:
308
- wp.launch(kernel=sum_array, dim=len(arr_reshaped), inputs=[arr_reshaped, loss], device=device)
309
-
310
- tape.backward(loss=loss)
311
- grad = tape.gradients[arr_reshaped]
312
-
313
- ones = wp.array(
314
- np.ones(
315
- (6,),
316
- dtype=float,
317
- ),
318
- dtype=float,
319
- device=device,
320
- )
321
- assert_array_equal(grad, ones)
322
- test.assertEqual(loss.numpy()[0], 15)
323
-
324
- np_arr = np.arange(6, dtype=float)
325
- arr = wp.array(np_arr, dtype=float, device=device)
326
- arr_infer = arr.reshape((-1, 3))
327
- arr_comp = wp.array(np_arr.reshape((-1, 3)), dtype=float, device=device)
328
- assert_array_equal(arr_infer, arr_comp)
329
-
330
-
331
- @wp.kernel
332
- def compare_stepped_window_a(x: wp.array2d(dtype=float)):
333
- wp.expect_eq(x[0, 0], 1.0)
334
- wp.expect_eq(x[0, 1], 2.0)
335
- wp.expect_eq(x[1, 0], 9.0)
336
- wp.expect_eq(x[1, 1], 10.0)
337
-
338
-
339
- @wp.kernel
340
- def compare_stepped_window_b(x: wp.array2d(dtype=float)):
341
- wp.expect_eq(x[0, 0], 3.0)
342
- wp.expect_eq(x[0, 1], 4.0)
343
- wp.expect_eq(x[1, 0], 7.0)
344
- wp.expect_eq(x[1, 1], 8.0)
345
- wp.expect_eq(x[2, 0], 11.0)
346
- wp.expect_eq(x[2, 1], 12.0)
347
-
348
-
349
- def test_slicing(test, device):
350
- np_arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=float)
351
- arr = wp.array(np_arr, dtype=float, shape=np_arr.shape, device=device, requires_grad=True)
352
-
353
- slice_a = arr[1, :, :] # test indexing
354
- slice_b = arr[1:2, :, :] # test slicing
355
- slice_c = arr[-1, :, :] # test negative indexing
356
- slice_d = arr[-2:-1, :, :] # test negative slicing
357
- slice_e = arr[-1:3, :, :] # test mixed slicing
358
- slice_e2 = slice_e[0, 0, :] # test 2x slicing
359
- slice_f = arr[0:3:2, 0, :] # test step
360
-
361
- assert_array_equal(slice_a, wp.array(np_arr[1, :, :], dtype=float, device=device))
362
- assert_array_equal(slice_b, wp.array(np_arr[1:2, :, :], dtype=float, device=device))
363
- assert_array_equal(slice_c, wp.array(np_arr[-1, :, :], dtype=float, device=device))
364
- assert_array_equal(slice_d, wp.array(np_arr[-2:-1, :, :], dtype=float, device=device))
365
- assert_array_equal(slice_e, wp.array(np_arr[-1:3, :, :], dtype=float, device=device))
366
- assert_array_equal(slice_e2, wp.array(np_arr[2, 0, :], dtype=float, device=device))
367
-
368
- # wp does not support copying from/to non-contiguous arrays
369
- # stepped windows must read on the device the original array was created on
370
- wp.launch(kernel=compare_stepped_window_a, dim=1, inputs=[slice_f], device=device)
371
-
372
- slice_flat = slice_b.flatten()
373
- loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
374
- tape = wp.Tape()
375
- with tape:
376
- wp.launch(kernel=sum_array, dim=len(slice_flat), inputs=[slice_flat, loss], device=device)
377
-
378
- tape.backward(loss=loss)
379
- grad = tape.gradients[slice_flat]
380
-
381
- ones = wp.array(
382
- np.ones(
383
- (4,),
384
- dtype=float,
385
- ),
386
- dtype=float,
387
- device=device,
388
- )
389
- assert_array_equal(grad, ones)
390
- test.assertEqual(loss.numpy()[0], 26)
391
-
392
- index_a = arr[1]
393
- index_b = arr[2, 1]
394
- index_c = arr[1, :]
395
- index_d = arr[:, 1]
396
-
397
- assert_array_equal(index_a, wp.array(np_arr[1], dtype=float, device=device))
398
- assert_array_equal(index_b, wp.array(np_arr[2, 1], dtype=float, device=device))
399
- assert_array_equal(index_c, wp.array(np_arr[1, :], dtype=float, device=device))
400
- wp.launch(kernel=compare_stepped_window_b, dim=1, inputs=[index_d], device=device)
401
-
402
- np_arr = np.zeros(10, dtype=int)
403
- wp_arr = wp.array(np_arr, dtype=int, device=device)
404
-
405
- assert_array_equal(wp_arr[:5], wp.array(np_arr[:5], dtype=int, device=device))
406
- assert_array_equal(wp_arr[1:5], wp.array(np_arr[1:5], dtype=int, device=device))
407
- assert_array_equal(wp_arr[-9:-5:1], wp.array(np_arr[-9:-5:1], dtype=int, device=device))
408
- assert_array_equal(wp_arr[:5,], wp.array(np_arr[:5], dtype=int, device=device)) # noqa: E231
409
-
410
-
411
- def test_view(test, device):
412
- np_arr_a = np.arange(1, 10, 1, dtype=np.uint32)
413
- np_arr_b = np.arange(1, 10, 1, dtype=np.float32)
414
- np_arr_c = np.arange(1, 10, 1, dtype=np.uint16)
415
- np_arr_d = np.arange(1, 10, 1, dtype=np.float16)
416
- np_arr_e = np.ones((4, 4), dtype=np.float32)
417
-
418
- wp_arr_a = wp.array(np_arr_a, dtype=wp.uint32, device=device)
419
- wp_arr_b = wp.array(np_arr_b, dtype=wp.float32, device=device)
420
- wp_arr_c = wp.array(np_arr_a, dtype=wp.uint16, device=device)
421
- wp_arr_d = wp.array(np_arr_b, dtype=wp.float16, device=device)
422
- wp_arr_e = wp.array(np_arr_e, dtype=wp.vec4, device=device)
423
- wp_arr_f = wp.array(np_arr_e, dtype=wp.quat, device=device)
424
-
425
- assert np.array_equal(np_arr_a.view(dtype=np.float32), wp_arr_a.view(dtype=wp.float32).numpy())
426
- assert np.array_equal(np_arr_b.view(dtype=np.uint32), wp_arr_b.view(dtype=wp.uint32).numpy())
427
- assert np.array_equal(np_arr_c.view(dtype=np.float16), wp_arr_c.view(dtype=wp.float16).numpy())
428
- assert np.array_equal(np_arr_d.view(dtype=np.uint16), wp_arr_d.view(dtype=wp.uint16).numpy())
429
- assert_array_equal(wp_arr_e.view(dtype=wp.quat), wp_arr_f)
430
-
431
-
432
- @wp.kernel
433
- def compare_2darrays(x: wp.array2d(dtype=float), y: wp.array2d(dtype=float), z: wp.array2d(dtype=int)):
434
- i, j = wp.tid()
435
-
436
- if x[i, j] == y[i, j]:
437
- z[i, j] = 1
438
-
439
-
440
- @wp.kernel
441
- def compare_3darrays(x: wp.array3d(dtype=float), y: wp.array3d(dtype=float), z: wp.array3d(dtype=int)):
442
- i, j, k = wp.tid()
443
-
444
- if x[i, j, k] == y[i, j, k]:
445
- z[i, j, k] = 1
446
-
447
-
448
- def test_transpose(test, device):
449
- # test default transpose in non-square 2d case
450
- # wp does not support copying from/to non-contiguous arrays so check in kernel
451
- np_arr = np.array([[1, 2], [3, 4], [5, 6]], dtype=float)
452
- arr = wp.array(np_arr, dtype=float, device=device)
453
- arr_transpose = arr.transpose()
454
- arr_compare = wp.array(np_arr.transpose(), dtype=float, device=device)
455
- check = wp.zeros(shape=(2, 3), dtype=int, device=device)
456
-
457
- wp.launch(compare_2darrays, dim=(2, 3), inputs=[arr_transpose, arr_compare, check], device=device)
458
- assert np.array_equal(check.numpy(), np.ones((2, 3), dtype=int))
459
-
460
- # test transpose in square 3d case
461
- # wp does not support copying from/to non-contiguous arrays so check in kernel
462
- np_arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=float)
463
- arr = wp.array3d(np_arr, dtype=float, shape=np_arr.shape, device=device, requires_grad=True)
464
- arr_transpose = arr.transpose((0, 2, 1))
465
- arr_compare = wp.array3d(np_arr.transpose((0, 2, 1)), dtype=float, device=device)
466
- check = wp.zeros(shape=(3, 2, 2), dtype=int, device=device)
467
-
468
- wp.launch(compare_3darrays, dim=(3, 2, 2), inputs=[arr_transpose, arr_compare, check], device=device)
469
- assert np.array_equal(check.numpy(), np.ones((3, 2, 2), dtype=int))
470
-
471
- # test transpose in square 3d case without axes supplied
472
- arr_transpose = arr.transpose()
473
- arr_compare = wp.array3d(np_arr.transpose(), dtype=float, device=device)
474
- check = wp.zeros(shape=(2, 2, 3), dtype=int, device=device)
475
-
476
- wp.launch(compare_3darrays, dim=(2, 2, 3), inputs=[arr_transpose, arr_compare, check], device=device)
477
- assert np.array_equal(check.numpy(), np.ones((2, 2, 3), dtype=int))
478
-
479
- # test transpose in 1d case (should be noop)
480
- np_arr = np.array([1, 2, 3], dtype=float)
481
- arr = wp.array(np_arr, dtype=float, device=device)
482
-
483
- assert np.array_equal(np_arr.transpose(), arr.transpose().numpy())
484
-
485
-
486
- def test_fill_scalar(test, device):
487
- dim_x = 4
488
-
489
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
490
- a1 = wp.zeros(dim_x, dtype=wptype, device=device)
491
- a2 = wp.zeros((dim_x, dim_x), dtype=wptype, device=device)
492
- a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=wptype, device=device)
493
- a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=wptype, device=device)
494
-
495
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
496
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
497
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
498
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
499
-
500
- # fill with int value
501
- fill_value = 42
502
-
503
- a1.fill_(fill_value)
504
- a2.fill_(fill_value)
505
- a3.fill_(fill_value)
506
- a4.fill_(fill_value)
507
-
508
- assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
509
- assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
510
- assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
511
- assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
512
-
513
- a1.zero_()
514
- a2.zero_()
515
- a3.zero_()
516
- a4.zero_()
517
-
518
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
519
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
520
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
521
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
522
-
523
- if wptype in wp.types.float_types:
524
- # fill with float value
525
- fill_value = 13.37
526
-
527
- a1.fill_(fill_value)
528
- a2.fill_(fill_value)
529
- a3.fill_(fill_value)
530
- a4.fill_(fill_value)
531
-
532
- assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
533
- assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
534
- assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
535
- assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
536
-
537
- # fill with Warp scalar value
538
- fill_value = wptype(17)
539
-
540
- a1.fill_(fill_value)
541
- a2.fill_(fill_value)
542
- a3.fill_(fill_value)
543
- a4.fill_(fill_value)
544
-
545
- assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value.value, dtype=nptype))
546
- assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value.value, dtype=nptype))
547
- assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value.value, dtype=nptype))
548
- assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value.value, dtype=nptype))
549
-
550
-
551
- def test_fill_vector(test, device):
552
- # test filling a vector array with scalar or vector values (vec_type, list, or numpy array)
553
-
554
- dim_x = 4
555
-
556
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
557
- # vector types
558
- vector_types = [
559
- wp.types.vector(2, wptype),
560
- wp.types.vector(3, wptype),
561
- wp.types.vector(4, wptype),
562
- wp.types.vector(5, wptype),
563
- ]
564
-
565
- for vec_type in vector_types:
566
- vec_len = vec_type._length_
567
-
568
- a1 = wp.zeros(dim_x, dtype=vec_type, device=device)
569
- a2 = wp.zeros((dim_x, dim_x), dtype=vec_type, device=device)
570
- a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=vec_type, device=device)
571
- a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=vec_type, device=device)
572
-
573
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
574
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
575
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
576
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
577
-
578
- # fill with int scalar
579
- fill_value = 42
580
-
581
- a1.fill_(fill_value)
582
- a2.fill_(fill_value)
583
- a3.fill_(fill_value)
584
- a4.fill_(fill_value)
585
-
586
- assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
587
- assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
588
- assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
589
- assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
590
-
591
- # test zeroing
592
- a1.zero_()
593
- a2.zero_()
594
- a3.zero_()
595
- a4.zero_()
596
-
597
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
598
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
599
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
600
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
601
-
602
- # vector values can be passed as a list, numpy array, or Warp vector instance
603
- fill_list = [17, 42, 99, 101, 127][:vec_len]
604
- fill_arr = np.array(fill_list, dtype=nptype)
605
- fill_vec = vec_type(fill_list)
606
-
607
- expected1 = np.tile(fill_arr, a1.size).reshape((*a1.shape, vec_len))
608
- expected2 = np.tile(fill_arr, a2.size).reshape((*a2.shape, vec_len))
609
- expected3 = np.tile(fill_arr, a3.size).reshape((*a3.shape, vec_len))
610
- expected4 = np.tile(fill_arr, a4.size).reshape((*a4.shape, vec_len))
611
-
612
- # fill with list of vector length
613
- a1.fill_(fill_list)
614
- a2.fill_(fill_list)
615
- a3.fill_(fill_list)
616
- a4.fill_(fill_list)
617
-
618
- assert_np_equal(a1.numpy(), expected1)
619
- assert_np_equal(a2.numpy(), expected2)
620
- assert_np_equal(a3.numpy(), expected3)
621
- assert_np_equal(a4.numpy(), expected4)
622
-
623
- # clear
624
- a1.zero_()
625
- a2.zero_()
626
- a3.zero_()
627
- a4.zero_()
628
-
629
- # fill with numpy array of vector length
630
- a1.fill_(fill_arr)
631
- a2.fill_(fill_arr)
632
- a3.fill_(fill_arr)
633
- a4.fill_(fill_arr)
634
-
635
- assert_np_equal(a1.numpy(), expected1)
636
- assert_np_equal(a2.numpy(), expected2)
637
- assert_np_equal(a3.numpy(), expected3)
638
- assert_np_equal(a4.numpy(), expected4)
639
-
640
- # clear
641
- a1.zero_()
642
- a2.zero_()
643
- a3.zero_()
644
- a4.zero_()
645
-
646
- # fill with vec instance
647
- a1.fill_(fill_vec)
648
- a2.fill_(fill_vec)
649
- a3.fill_(fill_vec)
650
- a4.fill_(fill_vec)
651
-
652
- assert_np_equal(a1.numpy(), expected1)
653
- assert_np_equal(a2.numpy(), expected2)
654
- assert_np_equal(a3.numpy(), expected3)
655
- assert_np_equal(a4.numpy(), expected4)
656
-
657
- if wptype in wp.types.float_types:
658
- # fill with float scalar
659
- fill_value = 13.37
660
-
661
- a1.fill_(fill_value)
662
- a2.fill_(fill_value)
663
- a3.fill_(fill_value)
664
- a4.fill_(fill_value)
665
-
666
- assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
667
- assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
668
- assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
669
- assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
670
-
671
- # fill with float list of vector length
672
- fill_list = [-2.5, -1.25, 1.25, 2.5, 5.0][:vec_len]
673
-
674
- a1.fill_(fill_list)
675
- a2.fill_(fill_list)
676
- a3.fill_(fill_list)
677
- a4.fill_(fill_list)
678
-
679
- expected1 = np.tile(np.array(fill_list, dtype=nptype), a1.size).reshape((*a1.shape, vec_len))
680
- expected2 = np.tile(np.array(fill_list, dtype=nptype), a2.size).reshape((*a2.shape, vec_len))
681
- expected3 = np.tile(np.array(fill_list, dtype=nptype), a3.size).reshape((*a3.shape, vec_len))
682
- expected4 = np.tile(np.array(fill_list, dtype=nptype), a4.size).reshape((*a4.shape, vec_len))
683
-
684
- assert_np_equal(a1.numpy(), expected1)
685
- assert_np_equal(a2.numpy(), expected2)
686
- assert_np_equal(a3.numpy(), expected3)
687
- assert_np_equal(a4.numpy(), expected4)
688
-
689
-
690
- def test_fill_matrix(test, device):
691
- # test filling a matrix array with scalar or matrix values (mat_type, nested list, or 2d numpy array)
692
-
693
- dim_x = 4
694
-
695
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
696
- # matrix types
697
- matrix_types = [
698
- # square matrices
699
- wp.types.matrix((2, 2), wptype),
700
- wp.types.matrix((3, 3), wptype),
701
- wp.types.matrix((4, 4), wptype),
702
- wp.types.matrix((5, 5), wptype),
703
- # non-square matrices
704
- wp.types.matrix((2, 3), wptype),
705
- wp.types.matrix((3, 2), wptype),
706
- wp.types.matrix((3, 4), wptype),
707
- wp.types.matrix((4, 3), wptype),
708
- ]
709
-
710
- for mat_type in matrix_types:
711
- mat_len = mat_type._length_
712
- mat_shape = mat_type._shape_
713
-
714
- a1 = wp.zeros(dim_x, dtype=mat_type, device=device)
715
- a2 = wp.zeros((dim_x, dim_x), dtype=mat_type, device=device)
716
- a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=mat_type, device=device)
717
- a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=mat_type, device=device)
718
-
719
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
720
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
721
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
722
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
723
-
724
- # fill with scalar
725
- fill_value = 42
726
-
727
- a1.fill_(fill_value)
728
- a2.fill_(fill_value)
729
- a3.fill_(fill_value)
730
- a4.fill_(fill_value)
731
-
732
- assert_np_equal(a1.numpy(), np.full((*a1.shape, *mat_shape), fill_value, dtype=nptype))
733
- assert_np_equal(a2.numpy(), np.full((*a2.shape, *mat_shape), fill_value, dtype=nptype))
734
- assert_np_equal(a3.numpy(), np.full((*a3.shape, *mat_shape), fill_value, dtype=nptype))
735
- assert_np_equal(a4.numpy(), np.full((*a4.shape, *mat_shape), fill_value, dtype=nptype))
736
-
737
- # test zeroing
738
- a1.zero_()
739
- a2.zero_()
740
- a3.zero_()
741
- a4.zero_()
742
-
743
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
744
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
745
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
746
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
747
-
748
- # matrix values can be passed as a 1d numpy array, 2d numpy array, flat list, nested list, or Warp matrix instance
749
- if wptype != wp.bool:
750
- fill_arr1 = np.arange(mat_len, dtype=nptype)
751
- else:
752
- fill_arr1 = np.ones(mat_len, dtype=nptype)
753
- fill_arr2 = fill_arr1.reshape(mat_shape)
754
- fill_list1 = list(fill_arr1)
755
- fill_list2 = [list(row) for row in fill_arr2]
756
- fill_mat = mat_type(fill_arr1)
757
-
758
- expected1 = np.tile(fill_arr1, a1.size).reshape((*a1.shape, *mat_shape))
759
- expected2 = np.tile(fill_arr1, a2.size).reshape((*a2.shape, *mat_shape))
760
- expected3 = np.tile(fill_arr1, a3.size).reshape((*a3.shape, *mat_shape))
761
- expected4 = np.tile(fill_arr1, a4.size).reshape((*a4.shape, *mat_shape))
762
-
763
- # fill with 1d numpy array
764
- a1.fill_(fill_arr1)
765
- a2.fill_(fill_arr1)
766
- a3.fill_(fill_arr1)
767
- a4.fill_(fill_arr1)
768
-
769
- assert_np_equal(a1.numpy(), expected1)
770
- assert_np_equal(a2.numpy(), expected2)
771
- assert_np_equal(a3.numpy(), expected3)
772
- assert_np_equal(a4.numpy(), expected4)
773
-
774
- # clear
775
- a1.zero_()
776
- a2.zero_()
777
- a3.zero_()
778
- a4.zero_()
779
-
780
- # fill with 2d numpy array
781
- a1.fill_(fill_arr2)
782
- a2.fill_(fill_arr2)
783
- a3.fill_(fill_arr2)
784
- a4.fill_(fill_arr2)
785
-
786
- assert_np_equal(a1.numpy(), expected1)
787
- assert_np_equal(a2.numpy(), expected2)
788
- assert_np_equal(a3.numpy(), expected3)
789
- assert_np_equal(a4.numpy(), expected4)
790
-
791
- # clear
792
- a1.zero_()
793
- a2.zero_()
794
- a3.zero_()
795
- a4.zero_()
796
-
797
- # fill with flat list
798
- a1.fill_(fill_list1)
799
- a2.fill_(fill_list1)
800
- a3.fill_(fill_list1)
801
- a4.fill_(fill_list1)
802
-
803
- assert_np_equal(a1.numpy(), expected1)
804
- assert_np_equal(a2.numpy(), expected2)
805
- assert_np_equal(a3.numpy(), expected3)
806
- assert_np_equal(a4.numpy(), expected4)
807
-
808
- # clear
809
- a1.zero_()
810
- a2.zero_()
811
- a3.zero_()
812
- a4.zero_()
813
-
814
- # fill with nested list
815
- a1.fill_(fill_list2)
816
- a2.fill_(fill_list2)
817
- a3.fill_(fill_list2)
818
- a4.fill_(fill_list2)
819
-
820
- assert_np_equal(a1.numpy(), expected1)
821
- assert_np_equal(a2.numpy(), expected2)
822
- assert_np_equal(a3.numpy(), expected3)
823
- assert_np_equal(a4.numpy(), expected4)
824
-
825
- # clear
826
- a1.zero_()
827
- a2.zero_()
828
- a3.zero_()
829
- a4.zero_()
830
-
831
- # fill with mat instance
832
- a1.fill_(fill_mat)
833
- a2.fill_(fill_mat)
834
- a3.fill_(fill_mat)
835
- a4.fill_(fill_mat)
836
-
837
- assert_np_equal(a1.numpy(), expected1)
838
- assert_np_equal(a2.numpy(), expected2)
839
- assert_np_equal(a3.numpy(), expected3)
840
- assert_np_equal(a4.numpy(), expected4)
841
-
842
-
843
- @wp.struct
844
- class FillStruct:
845
- # scalar members (make sure to test float16)
846
- i1: wp.int8
847
- i2: wp.int16
848
- i4: wp.int32
849
- i8: wp.int64
850
- f2: wp.float16
851
- f4: wp.float32
852
- f8: wp.float16
853
- # vector members (make sure to test vectors of float16)
854
- v2: wp.types.vector(2, wp.int64)
855
- v3: wp.types.vector(3, wp.float32)
856
- v4: wp.types.vector(4, wp.float16)
857
- v5: wp.types.vector(5, wp.uint8)
858
- # matrix members (make sure to test matrices of float16)
859
- m2: wp.types.matrix((2, 2), wp.float64)
860
- m3: wp.types.matrix((3, 3), wp.int32)
861
- m4: wp.types.matrix((4, 4), wp.float16)
862
- m5: wp.types.matrix((5, 5), wp.int8)
863
- # arrays
864
- a1: wp.array(dtype=float)
865
- a2: wp.array2d(dtype=float)
866
- a3: wp.array3d(dtype=float)
867
- a4: wp.array4d(dtype=float)
868
-
869
-
870
- def test_fill_struct(test, device):
871
- dim_x = 4
872
-
873
- nptype = FillStruct.numpy_dtype()
874
-
875
- a1 = wp.zeros(dim_x, dtype=FillStruct, device=device)
876
- a2 = wp.zeros((dim_x, dim_x), dtype=FillStruct, device=device)
877
- a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
878
- a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
879
-
880
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
881
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
882
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
883
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
884
-
885
- s = FillStruct()
886
-
887
- # fill with default struct value (should be all zeros)
888
- a1.fill_(s)
889
- a2.fill_(s)
890
- a3.fill_(s)
891
- a4.fill_(s)
892
-
893
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
894
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
895
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
896
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
897
-
898
- # scalars
899
- s.i1 = -17
900
- s.i2 = 42
901
- s.i4 = 99
902
- s.i8 = 101
903
- s.f2 = -1.25
904
- s.f4 = 13.37
905
- s.f8 = 0.125
906
- # vectors
907
- s.v2 = [21, 22]
908
- s.v3 = [31, 32, 33]
909
- s.v4 = [41, 42, 43, 44]
910
- s.v5 = [51, 52, 53, 54, 55]
911
- # matrices
912
- s.m2 = [[61, 62]] * 2
913
- s.m3 = [[71, 72, 73]] * 3
914
- s.m4 = [[81, 82, 83, 84]] * 4
915
- s.m5 = [[91, 92, 93, 94, 95]] * 5
916
- # arrays
917
- s.a1 = wp.zeros((2,) * 1, dtype=float, device=device)
918
- s.a2 = wp.zeros((2,) * 2, dtype=float, device=device)
919
- s.a3 = wp.zeros((2,) * 3, dtype=float, device=device)
920
- s.a4 = wp.zeros((2,) * 4, dtype=float, device=device)
921
-
922
- # fill with custom struct value
923
- a1.fill_(s)
924
- a2.fill_(s)
925
- a3.fill_(s)
926
- a4.fill_(s)
927
-
928
- ns = s.numpy_value()
929
-
930
- expected1 = np.empty(a1.shape, dtype=nptype)
931
- expected2 = np.empty(a2.shape, dtype=nptype)
932
- expected3 = np.empty(a3.shape, dtype=nptype)
933
- expected4 = np.empty(a4.shape, dtype=nptype)
934
-
935
- expected1.fill(ns)
936
- expected2.fill(ns)
937
- expected3.fill(ns)
938
- expected4.fill(ns)
939
-
940
- assert_np_equal(a1.numpy(), expected1)
941
- assert_np_equal(a2.numpy(), expected2)
942
- assert_np_equal(a3.numpy(), expected3)
943
- assert_np_equal(a4.numpy(), expected4)
944
-
945
- # test clearing
946
- a1.zero_()
947
- a2.zero_()
948
- a3.zero_()
949
- a4.zero_()
950
-
951
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
952
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
953
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
954
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
955
-
956
-
957
- def test_fill_slices(test, device):
958
- # test fill_ and zero_ for non-contiguous arrays
959
- # Note: we don't need to test the whole range of dtypes (vectors, matrices, structs) here
960
-
961
- dim_x = 8
962
-
963
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
964
- a1 = wp.zeros(dim_x, dtype=wptype, device=device)
965
- a2 = wp.zeros((dim_x, dim_x), dtype=wptype, device=device)
966
- a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=wptype, device=device)
967
- a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=wptype, device=device)
968
-
969
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
970
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
971
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
972
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
973
-
974
- # partititon each array into even and odd slices
975
- a1a = a1[::2]
976
- a1b = a1[1::2]
977
- a2a = a2[::2]
978
- a2b = a2[1::2]
979
- a3a = a3[::2]
980
- a3b = a3[1::2]
981
- a4a = a4[::2]
982
- a4b = a4[1::2]
983
-
984
- # fill even slices
985
- fill_a = 17
986
- a1a.fill_(fill_a)
987
- a2a.fill_(fill_a)
988
- a3a.fill_(fill_a)
989
- a4a.fill_(fill_a)
990
-
991
- # ensure filled slices are correct
992
- assert_np_equal(a1a.numpy(), np.full(a1a.shape, fill_a, dtype=nptype))
993
- assert_np_equal(a2a.numpy(), np.full(a2a.shape, fill_a, dtype=nptype))
994
- assert_np_equal(a3a.numpy(), np.full(a3a.shape, fill_a, dtype=nptype))
995
- assert_np_equal(a4a.numpy(), np.full(a4a.shape, fill_a, dtype=nptype))
996
-
997
- # ensure unfilled slices are unaffected
998
- assert_np_equal(a1b.numpy(), np.zeros(a1b.shape, dtype=nptype))
999
- assert_np_equal(a2b.numpy(), np.zeros(a2b.shape, dtype=nptype))
1000
- assert_np_equal(a3b.numpy(), np.zeros(a3b.shape, dtype=nptype))
1001
- assert_np_equal(a4b.numpy(), np.zeros(a4b.shape, dtype=nptype))
1002
-
1003
- # fill odd slices
1004
- fill_b = 42
1005
- a1b.fill_(fill_b)
1006
- a2b.fill_(fill_b)
1007
- a3b.fill_(fill_b)
1008
- a4b.fill_(fill_b)
1009
-
1010
- # ensure filled slices are correct
1011
- assert_np_equal(a1b.numpy(), np.full(a1b.shape, fill_b, dtype=nptype))
1012
- assert_np_equal(a2b.numpy(), np.full(a2b.shape, fill_b, dtype=nptype))
1013
- assert_np_equal(a3b.numpy(), np.full(a3b.shape, fill_b, dtype=nptype))
1014
- assert_np_equal(a4b.numpy(), np.full(a4b.shape, fill_b, dtype=nptype))
1015
-
1016
- # ensure unfilled slices are unaffected
1017
- assert_np_equal(a1a.numpy(), np.full(a1a.shape, fill_a, dtype=nptype))
1018
- assert_np_equal(a2a.numpy(), np.full(a2a.shape, fill_a, dtype=nptype))
1019
- assert_np_equal(a3a.numpy(), np.full(a3a.shape, fill_a, dtype=nptype))
1020
- assert_np_equal(a4a.numpy(), np.full(a4a.shape, fill_a, dtype=nptype))
1021
-
1022
- # clear even slices
1023
- a1a.zero_()
1024
- a2a.zero_()
1025
- a3a.zero_()
1026
- a4a.zero_()
1027
-
1028
- # ensure cleared slices are correct
1029
- assert_np_equal(a1a.numpy(), np.zeros(a1a.shape, dtype=nptype))
1030
- assert_np_equal(a2a.numpy(), np.zeros(a2a.shape, dtype=nptype))
1031
- assert_np_equal(a3a.numpy(), np.zeros(a3a.shape, dtype=nptype))
1032
- assert_np_equal(a4a.numpy(), np.zeros(a4a.shape, dtype=nptype))
1033
-
1034
- # ensure uncleared slices are unaffected
1035
- assert_np_equal(a1b.numpy(), np.full(a1b.shape, fill_b, dtype=nptype))
1036
- assert_np_equal(a2b.numpy(), np.full(a2b.shape, fill_b, dtype=nptype))
1037
- assert_np_equal(a3b.numpy(), np.full(a3b.shape, fill_b, dtype=nptype))
1038
- assert_np_equal(a4b.numpy(), np.full(a4b.shape, fill_b, dtype=nptype))
1039
-
1040
- # re-fill even slices
1041
- a1a.fill_(fill_a)
1042
- a2a.fill_(fill_a)
1043
- a3a.fill_(fill_a)
1044
- a4a.fill_(fill_a)
1045
-
1046
- # clear odd slices
1047
- a1b.zero_()
1048
- a2b.zero_()
1049
- a3b.zero_()
1050
- a4b.zero_()
1051
-
1052
- # ensure cleared slices are correct
1053
- assert_np_equal(a1b.numpy(), np.zeros(a1b.shape, dtype=nptype))
1054
- assert_np_equal(a2b.numpy(), np.zeros(a2b.shape, dtype=nptype))
1055
- assert_np_equal(a3b.numpy(), np.zeros(a3b.shape, dtype=nptype))
1056
- assert_np_equal(a4b.numpy(), np.zeros(a4b.shape, dtype=nptype))
1057
-
1058
- # ensure uncleared slices are unaffected
1059
- assert_np_equal(a1a.numpy(), np.full(a1a.shape, fill_a, dtype=nptype))
1060
- assert_np_equal(a2a.numpy(), np.full(a2a.shape, fill_a, dtype=nptype))
1061
- assert_np_equal(a3a.numpy(), np.full(a3a.shape, fill_a, dtype=nptype))
1062
- assert_np_equal(a4a.numpy(), np.full(a4a.shape, fill_a, dtype=nptype))
1063
-
1064
-
1065
- def test_full_scalar(test, device):
1066
- dim = 4
1067
-
1068
- for ndim in range(1, 5):
1069
- shape = (dim,) * ndim
1070
-
1071
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1072
- # fill with int value and specific dtype
1073
- fill_value = 42
1074
- a = wp.full(shape, fill_value, dtype=wptype, device=device)
1075
- na = a.numpy()
1076
-
1077
- test.assertEqual(a.shape, shape)
1078
- test.assertEqual(a.dtype, wptype)
1079
- test.assertEqual(na.shape, shape)
1080
- test.assertEqual(na.dtype, nptype)
1081
- assert_np_equal(na, np.full(shape, fill_value, dtype=nptype))
1082
-
1083
- if wptype in wp.types.float_types:
1084
- # fill with float value and specific dtype
1085
- fill_value = 13.37
1086
- a = wp.full(shape, fill_value, dtype=wptype, device=device)
1087
- na = a.numpy()
1088
-
1089
- test.assertEqual(a.shape, shape)
1090
- test.assertEqual(a.dtype, wptype)
1091
- test.assertEqual(na.shape, shape)
1092
- test.assertEqual(na.dtype, nptype)
1093
- assert_np_equal(na, np.full(shape, fill_value, dtype=nptype))
1094
-
1095
- # fill with int value and automatically inferred dtype
1096
- fill_value = 42
1097
- a = wp.full(shape, fill_value, device=device)
1098
- na = a.numpy()
1099
-
1100
- test.assertEqual(a.shape, shape)
1101
- test.assertEqual(a.dtype, wp.int32)
1102
- test.assertEqual(na.shape, shape)
1103
- test.assertEqual(na.dtype, np.int32)
1104
- assert_np_equal(na, np.full(shape, fill_value, dtype=np.int32))
1105
-
1106
- # fill with float value and automatically inferred dtype
1107
- fill_value = 13.37
1108
- a = wp.full(shape, fill_value, device=device)
1109
- na = a.numpy()
1110
-
1111
- test.assertEqual(a.shape, shape)
1112
- test.assertEqual(a.dtype, wp.float32)
1113
- test.assertEqual(na.shape, shape)
1114
- test.assertEqual(na.dtype, np.float32)
1115
- assert_np_equal(na, np.full(shape, fill_value, dtype=np.float32))
1116
-
1117
-
1118
- def test_full_vector(test, device):
1119
- dim = 4
1120
-
1121
- for ndim in range(1, 5):
1122
- shape = (dim,) * ndim
1123
-
1124
- # full from scalar
1125
- for veclen in [2, 3, 4, 5]:
1126
- npshape = (*shape, veclen)
1127
-
1128
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1129
- vectype = wp.types.vector(veclen, wptype)
1130
-
1131
- # fill with scalar int value and specific dtype
1132
- fill_value = 42
1133
- a = wp.full(shape, fill_value, dtype=vectype, device=device)
1134
- na = a.numpy()
1135
-
1136
- test.assertEqual(a.shape, shape)
1137
- test.assertEqual(a.dtype, vectype)
1138
- test.assertEqual(na.shape, npshape)
1139
- test.assertEqual(na.dtype, nptype)
1140
- assert_np_equal(na, np.full(a.size * veclen, fill_value, dtype=nptype).reshape(npshape))
1141
-
1142
- if wptype in wp.types.float_types:
1143
- # fill with scalar float value and specific dtype
1144
- fill_value = 13.37
1145
- a = wp.full(shape, fill_value, dtype=vectype, device=device)
1146
- na = a.numpy()
1147
-
1148
- test.assertEqual(a.shape, shape)
1149
- test.assertEqual(a.dtype, vectype)
1150
- test.assertEqual(na.shape, npshape)
1151
- test.assertEqual(na.dtype, nptype)
1152
- assert_np_equal(na, np.full(a.size * veclen, fill_value, dtype=nptype).reshape(npshape))
1153
-
1154
- # fill with vector value and specific dtype
1155
- fill_vec = vectype(42)
1156
- a = wp.full(shape, fill_vec, dtype=vectype, device=device)
1157
- na = a.numpy()
1158
-
1159
- test.assertEqual(a.shape, shape)
1160
- test.assertEqual(a.dtype, vectype)
1161
- test.assertEqual(na.shape, npshape)
1162
- test.assertEqual(na.dtype, nptype)
1163
- assert_np_equal(na, np.full(a.size * veclen, 42, dtype=nptype).reshape(npshape))
1164
-
1165
- # fill with vector value and automatically inferred dtype
1166
- a = wp.full(shape, fill_vec, device=device)
1167
- na = a.numpy()
1168
-
1169
- test.assertEqual(a.shape, shape)
1170
- test.assertEqual(a.dtype, vectype)
1171
- test.assertEqual(na.shape, npshape)
1172
- test.assertEqual(na.dtype, nptype)
1173
- assert_np_equal(na, np.full(a.size * veclen, 42, dtype=nptype).reshape(npshape))
1174
-
1175
- fill_lists = [
1176
- [17, 42],
1177
- [17, 42, 99],
1178
- [17, 42, 99, 101],
1179
- [17, 42, 99, 101, 127],
1180
- ]
1181
-
1182
- # full from list and numpy array
1183
- for fill_list in fill_lists:
1184
- veclen = len(fill_list)
1185
- npshape = (*shape, veclen)
1186
-
1187
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1188
- vectype = wp.types.vector(veclen, wptype)
1189
-
1190
- # fill with list and specific dtype
1191
- a = wp.full(shape, fill_list, dtype=vectype, device=device)
1192
- na = a.numpy()
1193
-
1194
- test.assertEqual(a.shape, shape)
1195
- test.assertEqual(a.dtype, vectype)
1196
- test.assertEqual(na.shape, npshape)
1197
- test.assertEqual(na.dtype, nptype)
1198
-
1199
- expected = np.tile(np.array(fill_list, dtype=nptype), a.size).reshape(npshape)
1200
- assert_np_equal(na, expected)
1201
-
1202
- fill_arr = np.array(fill_list, dtype=nptype)
1203
-
1204
- # fill with numpy array and specific dtype
1205
- a = wp.full(shape, fill_arr, dtype=vectype, device=device)
1206
- na = a.numpy()
1207
-
1208
- test.assertEqual(a.shape, shape)
1209
- test.assertEqual(a.dtype, vectype)
1210
- test.assertEqual(na.shape, npshape)
1211
- test.assertEqual(na.dtype, nptype)
1212
- assert_np_equal(na, expected)
1213
-
1214
- # fill with numpy array and automatically infer dtype
1215
- a = wp.full(shape, fill_arr, device=device)
1216
- na = a.numpy()
1217
-
1218
- test.assertEqual(a.shape, shape)
1219
- test.assertTrue(wp.types.types_equal(a.dtype, vectype))
1220
- test.assertEqual(na.shape, npshape)
1221
- test.assertEqual(na.dtype, nptype)
1222
- assert_np_equal(na, expected)
1223
-
1224
- # fill with list and automatically infer dtype
1225
- a = wp.full(shape, fill_list, device=device)
1226
- na = a.numpy()
1227
-
1228
- test.assertEqual(a.shape, shape)
1229
-
1230
- # check that the inferred dtype is a vector
1231
- # Note that we cannot guarantee the scalar type, because it depends on numpy and may vary by platform
1232
- # (e.g. int64 on Linux and int32 on Windows).
1233
- test.assertEqual(a.dtype._wp_generic_type_str_, "vec_t")
1234
- test.assertEqual(a.dtype._length_, veclen)
1235
-
1236
- expected = np.tile(np.array(fill_list), a.size).reshape(npshape)
1237
- assert_np_equal(na, expected)
1238
-
1239
-
1240
- def test_full_matrix(test, device):
1241
- dim = 4
1242
-
1243
- for ndim in range(1, 5):
1244
- shape = (dim,) * ndim
1245
-
1246
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1247
- matrix_types = [
1248
- # square matrices
1249
- wp.types.matrix((2, 2), wptype),
1250
- wp.types.matrix((3, 3), wptype),
1251
- wp.types.matrix((4, 4), wptype),
1252
- wp.types.matrix((5, 5), wptype),
1253
- # non-square matrices
1254
- wp.types.matrix((2, 3), wptype),
1255
- wp.types.matrix((3, 2), wptype),
1256
- wp.types.matrix((3, 4), wptype),
1257
- wp.types.matrix((4, 3), wptype),
1258
- ]
1259
-
1260
- for mattype in matrix_types:
1261
- npshape = (*shape, *mattype._shape_)
1262
-
1263
- # fill with scalar int value and specific dtype
1264
- fill_value = 42
1265
- a = wp.full(shape, fill_value, dtype=mattype, device=device)
1266
- na = a.numpy()
1267
-
1268
- test.assertEqual(a.shape, shape)
1269
- test.assertEqual(a.dtype, mattype)
1270
- test.assertEqual(na.shape, npshape)
1271
- test.assertEqual(na.dtype, nptype)
1272
- assert_np_equal(na, np.full(a.size * mattype._length_, fill_value, dtype=nptype).reshape(npshape))
1273
-
1274
- if wptype in wp.types.float_types:
1275
- # fill with scalar float value and specific dtype
1276
- fill_value = 13.37
1277
- a = wp.full(shape, fill_value, dtype=mattype, device=device)
1278
- na = a.numpy()
1279
-
1280
- test.assertEqual(a.shape, shape)
1281
- test.assertEqual(a.dtype, mattype)
1282
- test.assertEqual(na.shape, npshape)
1283
- test.assertEqual(na.dtype, nptype)
1284
- assert_np_equal(na, np.full(a.size * mattype._length_, fill_value, dtype=nptype).reshape(npshape))
1285
-
1286
- # fill with matrix value and specific dtype
1287
- fill_mat = mattype(42)
1288
- a = wp.full(shape, fill_mat, dtype=mattype, device=device)
1289
- na = a.numpy()
1290
-
1291
- test.assertEqual(a.shape, shape)
1292
- test.assertEqual(a.dtype, mattype)
1293
- test.assertEqual(na.shape, npshape)
1294
- test.assertEqual(na.dtype, nptype)
1295
- assert_np_equal(na, np.full(a.size * mattype._length_, 42, dtype=nptype).reshape(npshape))
1296
-
1297
- # fill with matrix value and automatically inferred dtype
1298
- fill_mat = mattype(42)
1299
- a = wp.full(shape, fill_mat, device=device)
1300
- na = a.numpy()
1301
-
1302
- test.assertEqual(a.shape, shape)
1303
- test.assertEqual(a.dtype, mattype)
1304
- test.assertEqual(na.shape, npshape)
1305
- test.assertEqual(na.dtype, nptype)
1306
- assert_np_equal(na, np.full(a.size * mattype._length_, 42, dtype=nptype).reshape(npshape))
1307
-
1308
- # fill with 1d numpy array and specific dtype
1309
- if wptype != wp.bool:
1310
- fill_arr1d = np.arange(mattype._length_, dtype=nptype)
1311
- else:
1312
- fill_arr1d = np.ones(mattype._length_, dtype=nptype)
1313
- a = wp.full(shape, fill_arr1d, dtype=mattype, device=device)
1314
- na = a.numpy()
1315
-
1316
- test.assertEqual(a.shape, shape)
1317
- test.assertEqual(a.dtype, mattype)
1318
- test.assertEqual(na.shape, npshape)
1319
- test.assertEqual(na.dtype, nptype)
1320
-
1321
- expected = np.tile(fill_arr1d, a.size).reshape(npshape)
1322
- assert_np_equal(na, expected)
1323
-
1324
- # fill with 2d numpy array and specific dtype
1325
- fill_arr2d = fill_arr1d.reshape(mattype._shape_)
1326
- a = wp.full(shape, fill_arr2d, dtype=mattype, device=device)
1327
- na = a.numpy()
1328
-
1329
- test.assertEqual(a.shape, shape)
1330
- test.assertEqual(a.dtype, mattype)
1331
- test.assertEqual(na.shape, npshape)
1332
- test.assertEqual(na.dtype, nptype)
1333
- assert_np_equal(na, expected)
1334
-
1335
- # fill with 2d numpy array and automatically infer dtype
1336
- a = wp.full(shape, fill_arr2d, device=device)
1337
- na = a.numpy()
1338
-
1339
- test.assertEqual(a.shape, shape)
1340
- test.assertTrue(wp.types.types_equal(a.dtype, mattype))
1341
- test.assertEqual(na.shape, npshape)
1342
- test.assertEqual(na.dtype, nptype)
1343
- assert_np_equal(na, expected)
1344
-
1345
- # fill with flat list and specific dtype
1346
- fill_list1d = list(fill_arr1d)
1347
- a = wp.full(shape, fill_list1d, dtype=mattype, device=device)
1348
- na = a.numpy()
1349
-
1350
- test.assertEqual(a.shape, shape)
1351
- test.assertEqual(a.dtype, mattype)
1352
- test.assertEqual(na.shape, npshape)
1353
- test.assertEqual(na.dtype, nptype)
1354
- assert_np_equal(na, expected)
1355
-
1356
- # fill with nested list and specific dtype
1357
- fill_list2d = [list(row) for row in fill_arr2d]
1358
- a = wp.full(shape, fill_list2d, dtype=mattype, device=device)
1359
- na = a.numpy()
1360
-
1361
- test.assertEqual(a.shape, shape)
1362
- test.assertEqual(a.dtype, mattype)
1363
- test.assertEqual(na.shape, npshape)
1364
- test.assertEqual(na.dtype, nptype)
1365
- assert_np_equal(na, expected)
1366
-
1367
- mat_lists = [
1368
- # square matrices
1369
- [[1, 2], [3, 4]],
1370
- [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
1371
- [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]],
1372
- # non-square matrices
1373
- [[1, 2, 3, 4], [5, 6, 7, 8]],
1374
- [[1, 2], [3, 4], [5, 6], [7, 8]],
1375
- ]
1376
-
1377
- # fill with nested lists and automatically infer dtype
1378
- for fill_list in mat_lists:
1379
- num_rows = len(fill_list)
1380
- num_cols = len(fill_list[0])
1381
- npshape = (*shape, num_rows, num_cols)
1382
-
1383
- a = wp.full(shape, fill_list, device=device)
1384
- na = a.numpy()
1385
-
1386
- test.assertEqual(a.shape, shape)
1387
-
1388
- # check that the inferred dtype is a correctly shaped matrix
1389
- # Note that we cannot guarantee the scalar type, because it depends on numpy and may vary by platform
1390
- # (e.g. int64 on Linux and int32 on Windows).
1391
- test.assertEqual(a.dtype._wp_generic_type_str_, "mat_t")
1392
- test.assertEqual(a.dtype._shape_, (num_rows, num_cols))
1393
-
1394
- expected = np.tile(np.array(fill_list).flatten(), a.size).reshape(npshape)
1395
- assert_np_equal(na, expected)
1396
-
1397
-
1398
- def test_full_struct(test, device):
1399
- dim = 4
1400
-
1401
- for ndim in range(1, 5):
1402
- shape = (dim,) * ndim
1403
-
1404
- s = FillStruct()
1405
-
1406
- # fill with default struct (should be zeros)
1407
- a = wp.full(shape, s, dtype=FillStruct, device=device)
1408
- na = a.numpy()
1409
-
1410
- test.assertEqual(a.shape, shape)
1411
- test.assertEqual(a.dtype, FillStruct)
1412
- test.assertEqual(na.shape, shape)
1413
- test.assertEqual(na.dtype, FillStruct.numpy_dtype())
1414
- assert_np_equal(na, np.zeros(a.size, dtype=FillStruct.numpy_dtype()))
1415
-
1416
- # scalars
1417
- s.i1 = -17
1418
- s.i2 = 42
1419
- s.i4 = 99
1420
- s.i8 = 101
1421
- s.f2 = -1.25
1422
- s.f4 = 13.37
1423
- s.f8 = 0.125
1424
- # vectors
1425
- s.v2 = [21, 22]
1426
- s.v3 = [31, 32, 33]
1427
- s.v4 = [41, 42, 43, 44]
1428
- s.v5 = [51, 52, 53, 54, 55]
1429
- # matrices
1430
- s.m2 = [[61, 62]] * 2
1431
- s.m3 = [[71, 72, 73]] * 3
1432
- s.m4 = [[81, 82, 83, 84]] * 4
1433
- s.m5 = [[91, 92, 93, 94, 95]] * 5
1434
- # arrays
1435
- s.a1 = wp.zeros((2,) * 1, dtype=float, device=device)
1436
- s.a2 = wp.zeros((2,) * 2, dtype=float, device=device)
1437
- s.a3 = wp.zeros((2,) * 3, dtype=float, device=device)
1438
- s.a4 = wp.zeros((2,) * 4, dtype=float, device=device)
1439
-
1440
- # fill with initialized struct and explicit dtype
1441
- a = wp.full(shape, s, dtype=FillStruct, device=device)
1442
- na = a.numpy()
1443
-
1444
- test.assertEqual(a.shape, shape)
1445
- test.assertEqual(a.dtype, FillStruct)
1446
- test.assertEqual(na.shape, shape)
1447
- test.assertEqual(na.dtype, FillStruct.numpy_dtype())
1448
-
1449
- expected = np.empty(shape, dtype=FillStruct.numpy_dtype())
1450
- expected.fill(s.numpy_value())
1451
- assert_np_equal(na, expected)
1452
-
1453
- # fill with initialized struct and automatically inferred dtype
1454
- a = wp.full(shape, s, device=device)
1455
- na = a.numpy()
1456
-
1457
- test.assertEqual(a.shape, shape)
1458
- test.assertEqual(a.dtype, FillStruct)
1459
- test.assertEqual(na.shape, shape)
1460
- test.assertEqual(na.dtype, FillStruct.numpy_dtype())
1461
- assert_np_equal(na, expected)
1462
-
1463
-
1464
- def test_ones_scalar(test, device):
1465
- dim = 4
1466
-
1467
- for ndim in range(1, 5):
1468
- shape = (dim,) * ndim
1469
-
1470
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1471
- a = wp.ones(shape, dtype=wptype, device=device)
1472
- na = a.numpy()
1473
-
1474
- test.assertEqual(a.shape, shape)
1475
- test.assertEqual(a.dtype, wptype)
1476
- test.assertEqual(na.shape, shape)
1477
- test.assertEqual(na.dtype, nptype)
1478
- assert_np_equal(na, np.ones(shape, dtype=nptype))
1479
-
1480
-
1481
- def test_ones_vector(test, device):
1482
- dim = 4
1483
-
1484
- for ndim in range(1, 5):
1485
- shape = (dim,) * ndim
1486
-
1487
- for veclen in [2, 3, 4, 5]:
1488
- npshape = (*shape, veclen)
1489
-
1490
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1491
- vectype = wp.types.vector(veclen, wptype)
1492
-
1493
- a = wp.ones(shape, dtype=vectype, device=device)
1494
- na = a.numpy()
1495
-
1496
- test.assertEqual(a.shape, shape)
1497
- test.assertEqual(a.dtype, vectype)
1498
- test.assertEqual(na.shape, npshape)
1499
- test.assertEqual(na.dtype, nptype)
1500
- assert_np_equal(na, np.ones(npshape, dtype=nptype))
1501
-
1502
-
1503
- def test_ones_matrix(test, device):
1504
- dim = 4
1505
-
1506
- for ndim in range(1, 5):
1507
- shape = (dim,) * ndim
1508
-
1509
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1510
- matrix_types = [
1511
- # square matrices
1512
- wp.types.matrix((2, 2), wptype),
1513
- wp.types.matrix((3, 3), wptype),
1514
- wp.types.matrix((4, 4), wptype),
1515
- wp.types.matrix((5, 5), wptype),
1516
- # non-square matrices
1517
- wp.types.matrix((2, 3), wptype),
1518
- wp.types.matrix((3, 2), wptype),
1519
- wp.types.matrix((3, 4), wptype),
1520
- wp.types.matrix((4, 3), wptype),
1521
- ]
1522
-
1523
- for mattype in matrix_types:
1524
- npshape = (*shape, *mattype._shape_)
1525
-
1526
- a = wp.ones(shape, dtype=mattype, device=device)
1527
- na = a.numpy()
1528
-
1529
- test.assertEqual(a.shape, shape)
1530
- test.assertEqual(a.dtype, mattype)
1531
- test.assertEqual(na.shape, npshape)
1532
- test.assertEqual(na.dtype, nptype)
1533
- assert_np_equal(na, np.ones(npshape, dtype=nptype))
1534
-
1535
-
1536
- def test_ones_like_scalar(test, device):
1537
- dim = 4
1538
-
1539
- for ndim in range(1, 5):
1540
- shape = (dim,) * ndim
1541
-
1542
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1543
-
1544
- # source array
1545
- a = wp.zeros(shape, dtype=wptype, device=device)
1546
- na = a.numpy()
1547
- test.assertEqual(a.shape, shape)
1548
- test.assertEqual(a.dtype, wptype)
1549
- test.assertEqual(na.shape, shape)
1550
- test.assertEqual(na.dtype, nptype)
1551
- assert_np_equal(na, np.zeros(shape, dtype=nptype))
1552
-
1553
- # ones array
1554
- b = wp.ones_like(a)
1555
- nb = b.numpy()
1556
- test.assertEqual(b.shape, shape)
1557
- test.assertEqual(b.dtype, wptype)
1558
- test.assertEqual(nb.shape, shape)
1559
- test.assertEqual(nb.dtype, nptype)
1560
- assert_np_equal(nb, np.ones(shape, dtype=nptype))
1561
-
1562
-
1563
- def test_ones_like_vector(test, device):
1564
- dim = 4
1565
-
1566
- for ndim in range(1, 5):
1567
- shape = (dim,) * ndim
1568
-
1569
- for veclen in [2, 3, 4, 5]:
1570
- npshape = (*shape, veclen)
1571
-
1572
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1573
- vectype = wp.types.vector(veclen, wptype)
1574
-
1575
- # source array
1576
- a = wp.zeros(shape, dtype=vectype, device=device)
1577
- na = a.numpy()
1578
- test.assertEqual(a.shape, shape)
1579
- test.assertEqual(a.dtype, vectype)
1580
- test.assertEqual(na.shape, npshape)
1581
- test.assertEqual(na.dtype, nptype)
1582
- assert_np_equal(na, np.zeros(npshape, dtype=nptype))
1583
-
1584
- # ones array
1585
- b = wp.ones_like(a)
1586
- nb = b.numpy()
1587
- test.assertEqual(b.shape, shape)
1588
- test.assertEqual(b.dtype, vectype)
1589
- test.assertEqual(nb.shape, npshape)
1590
- test.assertEqual(nb.dtype, nptype)
1591
- assert_np_equal(nb, np.ones(npshape, dtype=nptype))
1592
-
1593
-
1594
- def test_ones_like_matrix(test, device):
1595
- dim = 4
1596
-
1597
- for ndim in range(1, 5):
1598
- shape = (dim,) * ndim
1599
-
1600
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1601
- matrix_types = [
1602
- # square matrices
1603
- wp.types.matrix((2, 2), wptype),
1604
- wp.types.matrix((3, 3), wptype),
1605
- wp.types.matrix((4, 4), wptype),
1606
- wp.types.matrix((5, 5), wptype),
1607
- # non-square matrices
1608
- wp.types.matrix((2, 3), wptype),
1609
- wp.types.matrix((3, 2), wptype),
1610
- wp.types.matrix((3, 4), wptype),
1611
- wp.types.matrix((4, 3), wptype),
1612
- ]
1613
-
1614
- for mattype in matrix_types:
1615
- npshape = (*shape, *mattype._shape_)
1616
-
1617
- # source array
1618
- a = wp.zeros(shape, dtype=mattype, device=device)
1619
- na = a.numpy()
1620
- test.assertEqual(a.shape, shape)
1621
- test.assertEqual(a.dtype, mattype)
1622
- test.assertEqual(na.shape, npshape)
1623
- test.assertEqual(na.dtype, nptype)
1624
- assert_np_equal(na, np.zeros(npshape, dtype=nptype))
1625
-
1626
- # ones array
1627
- b = wp.ones_like(a)
1628
- nb = b.numpy()
1629
- test.assertEqual(b.shape, shape)
1630
- test.assertEqual(b.dtype, mattype)
1631
- test.assertEqual(nb.shape, npshape)
1632
- test.assertEqual(nb.dtype, nptype)
1633
- assert_np_equal(nb, np.ones(npshape, dtype=nptype))
1634
-
1635
-
1636
- def test_round_trip(test, device):
1637
- rng = np.random.default_rng(123)
1638
- dim_x = 4
1639
-
1640
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1641
- a_np = rng.standard_normal(size=dim_x).astype(nptype)
1642
- a = wp.array(a_np, device=device)
1643
- test.assertEqual(a.dtype, wptype)
1644
-
1645
- assert_np_equal(a.numpy(), a_np)
1646
-
1647
- v_np = rng.standard_normal(size=(dim_x, 3)).astype(nptype)
1648
- v = wp.array(v_np, dtype=wp.types.vector(3, wptype), device=device)
1649
-
1650
- assert_np_equal(v.numpy(), v_np)
1651
-
1652
-
1653
- def test_empty_array(test, device):
1654
- # Test whether common operations work with empty (zero-sized) arrays
1655
- # without throwing exceptions.
1656
-
1657
- def test_empty_ops(ndim, nrows, ncols, wptype, nptype):
1658
- shape = (0,) * ndim
1659
- dtype_shape = ()
1660
-
1661
- if wptype in wp.types.scalar_types:
1662
- # scalar, vector, or matrix
1663
- if ncols > 0:
1664
- if nrows > 0:
1665
- wptype = wp.types.matrix((nrows, ncols), wptype)
1666
- else:
1667
- wptype = wp.types.vector(ncols, wptype)
1668
- dtype_shape = wptype._shape_
1669
- fill_value = wptype(42)
1670
- else:
1671
- # struct
1672
- fill_value = wptype()
1673
-
1674
- # create a zero-sized array
1675
- a = wp.empty(shape, dtype=wptype, device=device, requires_grad=True)
1676
-
1677
- test.assertEqual(a.ptr, None)
1678
- test.assertEqual(a.size, 0)
1679
- test.assertEqual(a.shape, shape)
1680
- test.assertEqual(a.grad.ptr, None)
1681
- test.assertEqual(a.grad.size, 0)
1682
- test.assertEqual(a.grad.shape, shape)
1683
-
1684
- # all of these methods should succeed with zero-sized arrays
1685
- a.zero_()
1686
- a.fill_(fill_value)
1687
- b = a.flatten()
1688
- b = a.reshape((0,))
1689
- b = a.transpose()
1690
- b = a.contiguous()
1691
-
1692
- b = wp.empty_like(a)
1693
- b = wp.zeros_like(a)
1694
- b = wp.full_like(a, fill_value)
1695
- b = wp.clone(a)
1696
-
1697
- wp.copy(a, b)
1698
- a.assign(b)
1699
-
1700
- na = a.numpy()
1701
- test.assertEqual(na.size, 0)
1702
- test.assertEqual(na.shape, (*shape, *dtype_shape))
1703
- test.assertEqual(na.dtype, nptype)
1704
-
1705
- test.assertEqual(a.list(), [])
1706
-
1707
- for ndim in range(1, 5):
1708
- # test with scalars, vectors, and matrices
1709
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1710
- # scalars
1711
- test_empty_ops(ndim, 0, 0, wptype, nptype)
1712
-
1713
- for ncols in [2, 3, 4, 5]:
1714
- # vectors
1715
- test_empty_ops(ndim, 0, ncols, wptype, nptype)
1716
- # square matrices
1717
- test_empty_ops(ndim, ncols, ncols, wptype, nptype)
1718
-
1719
- # non-square matrices
1720
- test_empty_ops(ndim, 2, 3, wptype, nptype)
1721
- test_empty_ops(ndim, 3, 2, wptype, nptype)
1722
- test_empty_ops(ndim, 3, 4, wptype, nptype)
1723
- test_empty_ops(ndim, 4, 3, wptype, nptype)
1724
-
1725
- # test with structs
1726
- test_empty_ops(ndim, 0, 0, FillStruct, FillStruct.numpy_dtype())
1727
-
1728
-
1729
- def test_empty_from_numpy(test, device):
1730
- # Test whether wrapping an empty (zero-sized) numpy array works correctly
1731
-
1732
- def test_empty_from_data(ndim, nrows, ncols, wptype, nptype):
1733
- shape = (0,) * ndim
1734
- dtype_shape = ()
1735
-
1736
- if ncols > 0:
1737
- if nrows > 0:
1738
- wptype = wp.types.matrix((nrows, ncols), wptype)
1739
- else:
1740
- wptype = wp.types.vector(ncols, wptype)
1741
- dtype_shape = wptype._shape_
1742
-
1743
- npshape = (*shape, *dtype_shape)
1744
-
1745
- na = np.empty(npshape, dtype=nptype)
1746
- a = wp.array(na, dtype=wptype, device=device)
1747
- test.assertEqual(a.size, 0)
1748
- test.assertEqual(a.shape, shape)
1749
-
1750
- for ndim in range(1, 5):
1751
- # test with scalars, vectors, and matrices
1752
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1753
- # scalars
1754
- test_empty_from_data(ndim, 0, 0, wptype, nptype)
1755
-
1756
- for ncols in [2, 3, 4, 5]:
1757
- # vectors
1758
- test_empty_from_data(ndim, 0, ncols, wptype, nptype)
1759
- # square matrices
1760
- test_empty_from_data(ndim, ncols, ncols, wptype, nptype)
1761
-
1762
- # non-square matrices
1763
- test_empty_from_data(ndim, 2, 3, wptype, nptype)
1764
- test_empty_from_data(ndim, 3, 2, wptype, nptype)
1765
- test_empty_from_data(ndim, 3, 4, wptype, nptype)
1766
- test_empty_from_data(ndim, 4, 3, wptype, nptype)
1767
-
1768
-
1769
- def test_empty_from_list(test, device):
1770
- # Test whether creating an array from an empty Python list works correctly
1771
-
1772
- def test_empty_from_data(nrows, ncols, wptype):
1773
- if ncols > 0:
1774
- if nrows > 0:
1775
- wptype = wp.types.matrix((nrows, ncols), wptype)
1776
- else:
1777
- wptype = wp.types.vector(ncols, wptype)
1778
-
1779
- a = wp.array([], dtype=wptype, device=device)
1780
- test.assertEqual(a.size, 0)
1781
- test.assertEqual(a.shape, (0,))
1782
-
1783
- # test with scalars, vectors, and matrices
1784
- for wptype in wp.types.scalar_types:
1785
- # scalars
1786
- test_empty_from_data(0, 0, wptype)
1787
-
1788
- for ncols in [2, 3, 4, 5]:
1789
- # vectors
1790
- test_empty_from_data(0, ncols, wptype)
1791
- # square matrices
1792
- test_empty_from_data(ncols, ncols, wptype)
1793
-
1794
- # non-square matrices
1795
- test_empty_from_data(2, 3, wptype)
1796
- test_empty_from_data(3, 2, wptype)
1797
- test_empty_from_data(3, 4, wptype)
1798
- test_empty_from_data(4, 3, wptype)
1799
-
1800
-
1801
- def test_to_list_scalar(test, device):
1802
- dim = 3
1803
- fill_value = 42
1804
-
1805
- for ndim in range(1, 5):
1806
- shape = (dim,) * ndim
1807
-
1808
- for wptype in wp.types.scalar_types:
1809
- a = wp.full(shape, fill_value, dtype=wptype, device=device)
1810
- l = a.list()
1811
-
1812
- test.assertEqual(len(l), a.size)
1813
- test.assertTrue(all(x == fill_value for x in l))
1814
-
1815
-
1816
- def test_to_list_vector(test, device):
1817
- dim = 3
1818
-
1819
- for ndim in range(1, 5):
1820
- shape = (dim,) * ndim
1821
-
1822
- for veclen in [2, 3, 4, 5]:
1823
- for wptype in wp.types.scalar_types:
1824
- vectype = wp.types.vector(veclen, wptype)
1825
- fill_value = vectype(42)
1826
-
1827
- a = wp.full(shape, fill_value, dtype=vectype, device=device)
1828
- l = a.list()
1829
-
1830
- test.assertEqual(len(l), a.size)
1831
- test.assertTrue(all(x == fill_value for x in l))
1832
-
1833
-
1834
- def test_to_list_matrix(test, device):
1835
- dim = 3
1836
-
1837
- for ndim in range(1, 5):
1838
- shape = (dim,) * ndim
1839
-
1840
- for wptype in wp.types.scalar_types:
1841
- matrix_types = [
1842
- # square matrices
1843
- wp.types.matrix((2, 2), wptype),
1844
- wp.types.matrix((3, 3), wptype),
1845
- wp.types.matrix((4, 4), wptype),
1846
- wp.types.matrix((5, 5), wptype),
1847
- # non-square matrices
1848
- wp.types.matrix((2, 3), wptype),
1849
- wp.types.matrix((3, 2), wptype),
1850
- wp.types.matrix((3, 4), wptype),
1851
- wp.types.matrix((4, 3), wptype),
1852
- ]
1853
-
1854
- for mattype in matrix_types:
1855
- fill_value = mattype(42)
1856
-
1857
- a = wp.full(shape, fill_value, dtype=mattype, device=device)
1858
- l = a.list()
1859
-
1860
- test.assertEqual(len(l), a.size)
1861
- test.assertTrue(all(x == fill_value for x in l))
1862
-
1863
-
1864
- def test_to_list_struct(test, device):
1865
- @wp.struct
1866
- class Inner:
1867
- h: wp.float16
1868
- v: wp.vec3
1869
-
1870
- @wp.struct
1871
- class ListStruct:
1872
- i: int
1873
- f: float
1874
- h: wp.float16
1875
- vi: wp.vec2i
1876
- vf: wp.vec3f
1877
- vh: wp.vec4h
1878
- mi: wp.types.matrix((2, 2), int)
1879
- mf: wp.types.matrix((3, 3), float)
1880
- mh: wp.types.matrix((4, 4), wp.float16)
1881
- inner: Inner
1882
- a1: wp.array(dtype=int)
1883
- a2: wp.array2d(dtype=float)
1884
- a3: wp.array3d(dtype=wp.float16)
1885
- bool: wp.bool
1886
-
1887
- dim = 3
1888
-
1889
- s = ListStruct()
1890
- s.i = 42
1891
- s.f = 2.5
1892
- s.h = -1.25
1893
- s.vi = wp.vec2i(1, 2)
1894
- s.vf = wp.vec3f(0.1, 0.2, 0.3)
1895
- s.vh = wp.vec4h(1.0, 2.0, 3.0, 4.0)
1896
- s.mi = [[1, 2], [3, 4]]
1897
- s.mf = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
1898
- s.mh = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
1899
- s.inner = Inner()
1900
- s.inner.h = 1.5
1901
- s.inner.v = [1, 2, 3]
1902
- s.a1 = wp.empty(1, dtype=int, device=device)
1903
- s.a2 = wp.empty((1, 1), dtype=float, device=device)
1904
- s.a3 = wp.empty((1, 1, 1), dtype=wp.float16, device=device)
1905
- s.bool = True
1906
-
1907
- for ndim in range(1, 5):
1908
- shape = (dim,) * ndim
1909
-
1910
- a = wp.full(shape, s, dtype=ListStruct, device=device)
1911
- l = a.list()
1912
-
1913
- for i in range(a.size):
1914
- test.assertEqual(l[i].i, s.i)
1915
- test.assertEqual(l[i].f, s.f)
1916
- test.assertEqual(l[i].h, s.h)
1917
- test.assertEqual(l[i].vi, s.vi)
1918
- test.assertEqual(l[i].vf, s.vf)
1919
- test.assertEqual(l[i].vh, s.vh)
1920
- test.assertEqual(l[i].mi, s.mi)
1921
- test.assertEqual(l[i].mf, s.mf)
1922
- test.assertEqual(l[i].mh, s.mh)
1923
- test.assertEqual(l[i].bool, s.bool)
1924
- test.assertEqual(l[i].inner.h, s.inner.h)
1925
- test.assertEqual(l[i].inner.v, s.inner.v)
1926
- test.assertEqual(l[i].a1.dtype, s.a1.dtype)
1927
- test.assertEqual(l[i].a1.ndim, s.a1.ndim)
1928
- test.assertEqual(l[i].a2.dtype, s.a2.dtype)
1929
- test.assertEqual(l[i].a2.ndim, s.a2.ndim)
1930
- test.assertEqual(l[i].a3.dtype, s.a3.dtype)
1931
- test.assertEqual(l[i].a3.ndim, s.a3.ndim)
1932
-
1933
-
1934
- @wp.kernel
1935
- def kernel_array_to_bool(array_null: wp.array(dtype=float), array_valid: wp.array(dtype=float)):
1936
- if not array_null:
1937
- # always succeed
1938
- wp.expect_eq(0, 0)
1939
- else:
1940
- # force failure
1941
- wp.expect_eq(1, 2)
1942
-
1943
- if array_valid:
1944
- # always succeed
1945
- wp.expect_eq(0, 0)
1946
- else:
1947
- # force failure
1948
- wp.expect_eq(1, 2)
1949
-
1950
-
1951
- def test_array_to_bool(test, device):
1952
- arr = wp.zeros(8, dtype=float, device=device)
1953
-
1954
- wp.launch(kernel_array_to_bool, dim=1, inputs=[None, arr], device=device)
1955
-
1956
-
1957
- @wp.struct
1958
- class InputStruct:
1959
- param1: int
1960
- param2: float
1961
- param3: wp.vec3
1962
- param4: wp.array(dtype=float)
1963
-
1964
-
1965
- @wp.struct
1966
- class OutputStruct:
1967
- param1: int
1968
- param2: float
1969
- param3: wp.vec3
1970
-
1971
-
1972
- @wp.kernel
1973
- def struct_array_kernel(inputs: wp.array(dtype=InputStruct), outputs: wp.array(dtype=OutputStruct)):
1974
- tid = wp.tid()
1975
-
1976
- wp.expect_eq(inputs[tid].param1, tid)
1977
- wp.expect_eq(inputs[tid].param2, float(tid * tid))
1978
-
1979
- wp.expect_eq(inputs[tid].param3[0], 1.0)
1980
- wp.expect_eq(inputs[tid].param3[1], 2.0)
1981
- wp.expect_eq(inputs[tid].param3[2], 3.0)
1982
-
1983
- wp.expect_eq(inputs[tid].param4[0], 1.0)
1984
- wp.expect_eq(inputs[tid].param4[1], 2.0)
1985
- wp.expect_eq(inputs[tid].param4[2], 3.0)
1986
-
1987
- o = OutputStruct()
1988
- o.param1 = inputs[tid].param1
1989
- o.param2 = inputs[tid].param2
1990
- o.param3 = inputs[tid].param3
1991
-
1992
- outputs[tid] = o
1993
-
1994
-
1995
- def test_array_of_structs(test, device):
1996
- num_items = 10
1997
-
1998
- l = []
1999
- for i in range(num_items):
2000
- s = InputStruct()
2001
- s.param1 = i
2002
- s.param2 = float(i * i)
2003
- s.param3 = wp.vec3(1.0, 2.0, 3.0)
2004
- s.param4 = wp.array([1.0, 2.0, 3.0], dtype=float, device=device)
2005
-
2006
- l.append(s)
2007
-
2008
- # initialize array from list of structs
2009
- inputs = wp.array(l, dtype=InputStruct, device=device)
2010
- outputs = wp.zeros(num_items, dtype=OutputStruct, device=device)
2011
-
2012
- # pass to our compute kernel
2013
- wp.launch(struct_array_kernel, dim=num_items, inputs=[inputs, outputs], device=device)
2014
-
2015
- out_numpy = outputs.numpy()
2016
- out_list = outputs.list()
2017
- out_cptr = outputs.to("cpu").cptr()
2018
-
2019
- for i in range(num_items):
2020
- test.assertEqual(out_numpy[i][0], l[i].param1)
2021
- test.assertEqual(out_numpy[i][1], l[i].param2)
2022
- assert_np_equal(out_numpy[i][2], np.array(l[i].param3))
2023
-
2024
- # test named slices of numpy structured array
2025
- test.assertEqual(out_numpy["param1"][i], l[i].param1)
2026
- test.assertEqual(out_numpy["param2"][i], l[i].param2)
2027
- assert_np_equal(out_numpy["param3"][i], np.array(l[i].param3))
2028
-
2029
- test.assertEqual(out_list[i].param1, l[i].param1)
2030
- test.assertEqual(out_list[i].param2, l[i].param2)
2031
- test.assertEqual(out_list[i].param3, l[i].param3)
2032
-
2033
- test.assertEqual(out_cptr[i].param1, l[i].param1)
2034
- test.assertEqual(out_cptr[i].param2, l[i].param2)
2035
- test.assertEqual(out_cptr[i].param3, l[i].param3)
2036
-
2037
-
2038
- @wp.struct
2039
- class GradStruct:
2040
- param1: int
2041
- param2: float
2042
- param3: wp.vec3
2043
-
2044
-
2045
- @wp.kernel
2046
- def test_array_of_structs_grad_kernel(inputs: wp.array(dtype=GradStruct), loss: wp.array(dtype=float)):
2047
- tid = wp.tid()
2048
-
2049
- wp.atomic_add(loss, 0, inputs[tid].param2 * 2.0)
2050
-
2051
-
2052
- def test_array_of_structs_grad(test, device):
2053
- num_items = 10
2054
-
2055
- l = []
2056
- for i in range(num_items):
2057
- g = GradStruct()
2058
- g.param2 = float(i)
2059
-
2060
- l.append(g)
2061
-
2062
- a = wp.array(l, dtype=GradStruct, device=device, requires_grad=True)
2063
- loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
2064
-
2065
- with wp.Tape() as tape:
2066
- wp.launch(test_array_of_structs_grad_kernel, dim=num_items, inputs=[a, loss], device=device)
2067
-
2068
- tape.backward(loss)
2069
-
2070
- grads = a.grad.numpy()
2071
- assert_np_equal(grads["param2"], np.full(num_items, 2.0, dtype=np.float32))
2072
-
2073
-
2074
- @wp.struct
2075
- class NumpyStruct:
2076
- x: int
2077
- v: wp.vec3
2078
-
2079
-
2080
- def test_array_of_structs_from_numpy(test, device):
2081
- num_items = 10
2082
-
2083
- na = np.zeros(num_items, dtype=NumpyStruct.numpy_dtype())
2084
- na["x"] = 17
2085
- na["v"] = (1, 2, 3)
2086
-
2087
- a = wp.array(data=na, dtype=NumpyStruct, device=device)
2088
-
2089
- assert_np_equal(a.numpy(), na)
2090
-
2091
-
2092
- def test_array_of_structs_roundtrip(test, device):
2093
- num_items = 10
2094
-
2095
- value = NumpyStruct()
2096
- value.x = 17
2097
- value.v = wp.vec3(1.0, 2.0, 3.0)
2098
-
2099
- # create Warp structured array
2100
- a = wp.full(num_items, value, device=device)
2101
-
2102
- # convert to NumPy structured array
2103
- na = a.numpy()
2104
-
2105
- expected = np.zeros(num_items, dtype=NumpyStruct.numpy_dtype())
2106
- expected["x"] = value.x
2107
- expected["v"] = value.v
2108
-
2109
- assert_np_equal(na, expected)
2110
-
2111
- # modify a field
2112
- na["x"] = 42
2113
-
2114
- # convert back to Warp array
2115
- a = wp.from_numpy(na, NumpyStruct, device=device)
2116
-
2117
- expected["x"] = 42
2118
-
2119
- assert_np_equal(a.numpy(), expected)
2120
-
2121
-
2122
- def test_array_from_numpy(test, device):
2123
- arr = np.array((1.0, 2.0, 3.0), dtype=float)
2124
-
2125
- result = wp.from_numpy(arr)
2126
- expected = wp.array((1.0, 2.0, 3.0), dtype=wp.float32, shape=(3,))
2127
- assert_np_equal(result.numpy(), expected.numpy())
2128
-
2129
- result = wp.from_numpy(arr, dtype=wp.vec3)
2130
- expected = wp.array(((1.0, 2.0, 3.0),), dtype=wp.vec3, shape=(1,))
2131
- assert_np_equal(result.numpy(), expected.numpy())
2132
-
2133
- # --------------------------------------------------------------------------
2134
-
2135
- arr = np.array(((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), dtype=float)
2136
-
2137
- result = wp.from_numpy(arr)
2138
- expected = wp.array(((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), dtype=wp.vec3, shape=(2,))
2139
- assert_np_equal(result.numpy(), expected.numpy())
2140
-
2141
- result = wp.from_numpy(arr, dtype=wp.float32)
2142
- expected = wp.array(((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), dtype=wp.float32, shape=(2, 3))
2143
- assert_np_equal(result.numpy(), expected.numpy())
2144
-
2145
- result = wp.from_numpy(arr, dtype=wp.float32, shape=(6,))
2146
- expected = wp.array((1.0, 2.0, 3.0, 4.0, 5.0, 6.0), dtype=wp.float32, shape=(6,))
2147
- assert_np_equal(result.numpy(), expected.numpy())
2148
-
2149
- # --------------------------------------------------------------------------
2150
-
2151
- arr = np.array(
2152
- (
2153
- (
2154
- (1.0, 2.0, 3.0, 4.0),
2155
- (2.0, 3.0, 4.0, 5.0),
2156
- (3.0, 4.0, 5.0, 6.0),
2157
- (4.0, 5.0, 6.0, 7.0),
2158
- ),
2159
- (
2160
- (2.0, 3.0, 4.0, 5.0),
2161
- (3.0, 4.0, 5.0, 6.0),
2162
- (4.0, 5.0, 6.0, 7.0),
2163
- (5.0, 6.0, 7.0, 8.0),
2164
- ),
2165
- ),
2166
- dtype=float,
2167
- )
2168
-
2169
- result = wp.from_numpy(arr)
2170
- expected = wp.array(
2171
- (
2172
- (
2173
- (1.0, 2.0, 3.0, 4.0),
2174
- (2.0, 3.0, 4.0, 5.0),
2175
- (3.0, 4.0, 5.0, 6.0),
2176
- (4.0, 5.0, 6.0, 7.0),
2177
- ),
2178
- (
2179
- (2.0, 3.0, 4.0, 5.0),
2180
- (3.0, 4.0, 5.0, 6.0),
2181
- (4.0, 5.0, 6.0, 7.0),
2182
- (5.0, 6.0, 7.0, 8.0),
2183
- ),
2184
- ),
2185
- dtype=wp.mat44,
2186
- shape=(2,),
2187
- )
2188
- assert_np_equal(result.numpy(), expected.numpy())
2189
-
2190
- result = wp.from_numpy(arr, dtype=wp.float32)
2191
- expected = wp.array(
2192
- (
2193
- (
2194
- (1.0, 2.0, 3.0, 4.0),
2195
- (2.0, 3.0, 4.0, 5.0),
2196
- (3.0, 4.0, 5.0, 6.0),
2197
- (4.0, 5.0, 6.0, 7.0),
2198
- ),
2199
- (
2200
- (2.0, 3.0, 4.0, 5.0),
2201
- (3.0, 4.0, 5.0, 6.0),
2202
- (4.0, 5.0, 6.0, 7.0),
2203
- (5.0, 6.0, 7.0, 8.0),
2204
- ),
2205
- ),
2206
- dtype=wp.float32,
2207
- shape=(2, 4, 4),
2208
- )
2209
- assert_np_equal(result.numpy(), expected.numpy())
2210
-
2211
- result = wp.from_numpy(arr, dtype=wp.vec4)
2212
- expected = wp.array(
2213
- (
2214
- (1.0, 2.0, 3.0, 4.0),
2215
- (2.0, 3.0, 4.0, 5.0),
2216
- (3.0, 4.0, 5.0, 6.0),
2217
- (4.0, 5.0, 6.0, 7.0),
2218
- (2.0, 3.0, 4.0, 5.0),
2219
- (3.0, 4.0, 5.0, 6.0),
2220
- (4.0, 5.0, 6.0, 7.0),
2221
- (5.0, 6.0, 7.0, 8.0),
2222
- ),
2223
- dtype=wp.vec4,
2224
- shape=(8,),
2225
- )
2226
- assert_np_equal(result.numpy(), expected.numpy())
2227
-
2228
- result = wp.from_numpy(arr, dtype=wp.float32, shape=(32,))
2229
- expected = wp.array(
2230
- (
2231
- 1.0,
2232
- 2.0,
2233
- 3.0,
2234
- 4.0,
2235
- 2.0,
2236
- 3.0,
2237
- 4.0,
2238
- 5.0,
2239
- 3.0,
2240
- 4.0,
2241
- 5.0,
2242
- 6.0,
2243
- 4.0,
2244
- 5.0,
2245
- 6.0,
2246
- 7.0,
2247
- 2.0,
2248
- 3.0,
2249
- 4.0,
2250
- 5.0,
2251
- 3.0,
2252
- 4.0,
2253
- 5.0,
2254
- 6.0,
2255
- 4.0,
2256
- 5.0,
2257
- 6.0,
2258
- 7.0,
2259
- 5.0,
2260
- 6.0,
2261
- 7.0,
2262
- 8.0,
2263
- ),
2264
- dtype=wp.float32,
2265
- shape=(32,),
2266
- )
2267
- assert_np_equal(result.numpy(), expected.numpy())
2268
-
2269
-
2270
- devices = get_test_devices()
2271
-
2272
-
2273
- class TestArray(unittest.TestCase):
2274
- pass
2275
-
2276
-
2277
- add_function_test(TestArray, "test_shape", test_shape, devices=devices)
2278
- add_function_test(TestArray, "test_negative_shape", test_negative_shape, devices=devices)
2279
- add_function_test(TestArray, "test_flatten", test_flatten, devices=devices)
2280
- add_function_test(TestArray, "test_reshape", test_reshape, devices=devices)
2281
- add_function_test(TestArray, "test_slicing", test_slicing, devices=devices)
2282
- add_function_test(TestArray, "test_transpose", test_transpose, devices=devices)
2283
- add_function_test(TestArray, "test_view", test_view, devices=devices)
2284
-
2285
- add_function_test(TestArray, "test_1d_array", test_1d, devices=devices)
2286
- add_function_test(TestArray, "test_2d_array", test_2d, devices=devices)
2287
- add_function_test(TestArray, "test_3d_array", test_3d, devices=devices)
2288
- add_function_test(TestArray, "test_4d_array", test_4d, devices=devices)
2289
- add_function_test(TestArray, "test_4d_array_transposed", test_4d_transposed, devices=devices)
2290
-
2291
- add_function_test(TestArray, "test_fill_scalar", test_fill_scalar, devices=devices)
2292
- add_function_test(TestArray, "test_fill_vector", test_fill_vector, devices=devices)
2293
- add_function_test(TestArray, "test_fill_matrix", test_fill_matrix, devices=devices)
2294
- add_function_test(TestArray, "test_fill_struct", test_fill_struct, devices=devices)
2295
- add_function_test(TestArray, "test_fill_slices", test_fill_slices, devices=devices)
2296
- add_function_test(TestArray, "test_full_scalar", test_full_scalar, devices=devices)
2297
- add_function_test(TestArray, "test_full_vector", test_full_vector, devices=devices)
2298
- add_function_test(TestArray, "test_full_matrix", test_full_matrix, devices=devices)
2299
- add_function_test(TestArray, "test_full_struct", test_full_struct, devices=devices)
2300
- add_function_test(TestArray, "test_ones_scalar", test_ones_scalar, devices=devices)
2301
- add_function_test(TestArray, "test_ones_vector", test_ones_vector, devices=devices)
2302
- add_function_test(TestArray, "test_ones_matrix", test_ones_matrix, devices=devices)
2303
- add_function_test(TestArray, "test_ones_like_scalar", test_ones_like_scalar, devices=devices)
2304
- add_function_test(TestArray, "test_ones_like_vector", test_ones_like_vector, devices=devices)
2305
- add_function_test(TestArray, "test_ones_like_matrix", test_ones_like_matrix, devices=devices)
2306
- add_function_test(TestArray, "test_empty_array", test_empty_array, devices=devices)
2307
- add_function_test(TestArray, "test_empty_from_numpy", test_empty_from_numpy, devices=devices)
2308
- add_function_test(TestArray, "test_empty_from_list", test_empty_from_list, devices=devices)
2309
- add_function_test(TestArray, "test_to_list_scalar", test_to_list_scalar, devices=devices)
2310
- add_function_test(TestArray, "test_to_list_vector", test_to_list_vector, devices=devices)
2311
- add_function_test(TestArray, "test_to_list_matrix", test_to_list_matrix, devices=devices)
2312
- add_function_test(TestArray, "test_to_list_struct", test_to_list_struct, devices=devices)
2313
-
2314
- add_function_test(TestArray, "test_lower_bound", test_lower_bound, devices=devices)
2315
- add_function_test(TestArray, "test_round_trip", test_round_trip, devices=devices)
2316
- add_function_test(TestArray, "test_array_to_bool", test_array_to_bool, devices=devices)
2317
- add_function_test(TestArray, "test_array_of_structs", test_array_of_structs, devices=devices)
2318
- add_function_test(TestArray, "test_array_of_structs_grad", test_array_of_structs_grad, devices=devices)
2319
- add_function_test(TestArray, "test_array_of_structs_from_numpy", test_array_of_structs_from_numpy, devices=devices)
2320
- add_function_test(TestArray, "test_array_of_structs_roundtrip", test_array_of_structs_roundtrip, devices=devices)
2321
- add_function_test(TestArray, "test_array_from_numpy", test_array_from_numpy, devices=devices)
2322
-
2323
-
2324
- if __name__ == "__main__":
2325
- wp.build.clear_kernel_cache()
2326
- 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
+
10
+ import numpy as np
11
+
12
+ import warp as wp
13
+ from warp.tests.unittest_utils import *
14
+
15
+
16
+ @wp.kernel
17
+ def kernel_1d(a: wp.array(dtype=int, ndim=1)):
18
+ i = wp.tid()
19
+
20
+ wp.expect_eq(a[i], wp.tid())
21
+
22
+ a[i] = a[i] * 2
23
+ wp.atomic_add(a, i, 1)
24
+
25
+ wp.expect_eq(a[i], wp.tid() * 2 + 1)
26
+
27
+
28
+ def test_1d(test, device):
29
+ dim_x = 4
30
+
31
+ a = np.arange(0, dim_x, dtype=np.int32)
32
+
33
+ arr = wp.array(a, device=device)
34
+
35
+ test.assertEqual(arr.shape, a.shape)
36
+ test.assertEqual(arr.size, a.size)
37
+ test.assertEqual(arr.ndim, a.ndim)
38
+
39
+ with CheckOutput(test):
40
+ wp.launch(kernel_1d, dim=arr.size, inputs=[arr], device=device)
41
+
42
+
43
+ @wp.kernel
44
+ def kernel_2d(a: wp.array(dtype=int, ndim=2), m: int, n: int):
45
+ i = wp.tid() // n
46
+ j = wp.tid() % n
47
+
48
+ wp.expect_eq(a[i, j], wp.tid())
49
+ wp.expect_eq(a[i][j], wp.tid())
50
+
51
+ a[i, j] = a[i, j] * 2
52
+ wp.atomic_add(a, i, j, 1)
53
+
54
+ wp.expect_eq(a[i, j], wp.tid() * 2 + 1)
55
+
56
+
57
+ def test_2d(test, device):
58
+ dim_x = 4
59
+ dim_y = 2
60
+
61
+ a = np.arange(0, dim_x * dim_y, dtype=np.int32)
62
+ a = a.reshape(dim_x, dim_y)
63
+
64
+ arr = wp.array(a, device=device)
65
+
66
+ test.assertEqual(arr.shape, a.shape)
67
+ test.assertEqual(arr.size, a.size)
68
+ test.assertEqual(arr.ndim, a.ndim)
69
+
70
+ with CheckOutput(test):
71
+ wp.launch(kernel_2d, dim=arr.size, inputs=[arr, dim_x, dim_y], device=device)
72
+
73
+
74
+ @wp.kernel
75
+ def kernel_3d(a: wp.array(dtype=int, ndim=3), m: int, n: int, o: int):
76
+ i = wp.tid() // (n * o)
77
+ j = wp.tid() % (n * o) // o
78
+ k = wp.tid() % o
79
+
80
+ wp.expect_eq(a[i, j, k], wp.tid())
81
+ wp.expect_eq(a[i][j][k], wp.tid())
82
+
83
+ a[i, j, k] = a[i, j, k] * 2
84
+ a[i][j][k] = a[i][j][k] * 2
85
+ wp.atomic_add(a, i, j, k, 1)
86
+
87
+ wp.expect_eq(a[i, j, k], wp.tid() * 4 + 1)
88
+
89
+
90
+ def test_3d(test, device):
91
+ dim_x = 8
92
+ dim_y = 4
93
+ dim_z = 2
94
+
95
+ a = np.arange(0, dim_x * dim_y * dim_z, dtype=np.int32)
96
+ a = a.reshape(dim_x, dim_y, dim_z)
97
+
98
+ arr = wp.array(a, device=device)
99
+
100
+ test.assertEqual(arr.shape, a.shape)
101
+ test.assertEqual(arr.size, a.size)
102
+ test.assertEqual(arr.ndim, a.ndim)
103
+
104
+ with CheckOutput(test):
105
+ wp.launch(kernel_3d, dim=arr.size, inputs=[arr, dim_x, dim_y, dim_z], device=device)
106
+
107
+
108
+ @wp.kernel
109
+ def kernel_4d(a: wp.array(dtype=int, ndim=4), m: int, n: int, o: int, p: int):
110
+ i = wp.tid() // (n * o * p)
111
+ j = wp.tid() % (n * o * p) // (o * p)
112
+ k = wp.tid() % (o * p) / p
113
+ l = wp.tid() % p
114
+
115
+ wp.expect_eq(a[i, j, k, l], wp.tid())
116
+ wp.expect_eq(a[i][j][k][l], wp.tid())
117
+
118
+
119
+ def test_4d(test, device):
120
+ dim_x = 16
121
+ dim_y = 8
122
+ dim_z = 4
123
+ dim_w = 2
124
+
125
+ a = np.arange(0, dim_x * dim_y * dim_z * dim_w, dtype=np.int32)
126
+ a = a.reshape(dim_x, dim_y, dim_z, dim_w)
127
+
128
+ arr = wp.array(a, device=device)
129
+
130
+ test.assertEqual(arr.shape, a.shape)
131
+ test.assertEqual(arr.size, a.size)
132
+ test.assertEqual(arr.ndim, a.ndim)
133
+
134
+ with CheckOutput(test):
135
+ wp.launch(kernel_4d, dim=arr.size, inputs=[arr, dim_x, dim_y, dim_z, dim_w], device=device)
136
+
137
+
138
+ @wp.kernel
139
+ def kernel_4d_transposed(a: wp.array(dtype=int, ndim=4), m: int, n: int, o: int, p: int):
140
+ i = wp.tid() // (n * o * p)
141
+ j = wp.tid() % (n * o * p) // (o * p)
142
+ k = wp.tid() % (o * p) / p
143
+ l = wp.tid() % p
144
+
145
+ wp.expect_eq(a[l, k, j, i], wp.tid())
146
+ wp.expect_eq(a[l][k][j][i], wp.tid())
147
+
148
+
149
+ def test_4d_transposed(test, device):
150
+ dim_x = 16
151
+ dim_y = 8
152
+ dim_z = 4
153
+ dim_w = 2
154
+
155
+ a = np.arange(0, dim_x * dim_y * dim_z * dim_w, dtype=np.int32)
156
+ a = a.reshape(dim_x, dim_y, dim_z, dim_w)
157
+
158
+ arr = wp.array(a, device=device)
159
+
160
+ # Transpose the array manually, as using the wp.array() constructor with arr.T would make it contiguous first
161
+ a_T = a.T
162
+ arr_T = wp.array(
163
+ dtype=arr.dtype,
164
+ shape=a_T.shape,
165
+ strides=a_T.__array_interface__["strides"],
166
+ capacity=arr.capacity,
167
+ ptr=arr.ptr,
168
+ requires_grad=arr.requires_grad,
169
+ device=device,
170
+ )
171
+
172
+ test.assertFalse(arr_T.is_contiguous)
173
+ test.assertEqual(arr_T.shape, a_T.shape)
174
+ test.assertEqual(arr_T.strides, a_T.__array_interface__["strides"])
175
+ test.assertEqual(arr_T.size, a_T.size)
176
+ test.assertEqual(arr_T.ndim, a_T.ndim)
177
+
178
+ with CheckOutput(test):
179
+ wp.launch(kernel_4d_transposed, dim=arr_T.size, inputs=[arr_T, dim_x, dim_y, dim_z, dim_w], device=device)
180
+
181
+
182
+ @wp.kernel
183
+ def lower_bound_kernel(values: wp.array(dtype=float), arr: wp.array(dtype=float), indices: wp.array(dtype=int)):
184
+ tid = wp.tid()
185
+
186
+ indices[tid] = wp.lower_bound(arr, values[tid])
187
+
188
+
189
+ def test_lower_bound(test, device):
190
+ arr = wp.array(np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float), dtype=float, device=device)
191
+ values = wp.array(np.array([-0.1, 0.0, 2.5, 4.0, 5.0, 5.5], dtype=float), dtype=float, device=device)
192
+ indices = wp.zeros(6, dtype=int, device=device)
193
+
194
+ wp.launch(kernel=lower_bound_kernel, dim=6, inputs=[values, arr, indices], device=device)
195
+
196
+ test.assertTrue((np.array([0, 0, 3, 4, 5, 5]) == indices.numpy()).all())
197
+
198
+
199
+ @wp.kernel
200
+ def f1(arr: wp.array(dtype=float)):
201
+ wp.expect_eq(arr.shape[0], 10)
202
+
203
+
204
+ @wp.kernel
205
+ def f2(arr: wp.array2d(dtype=float)):
206
+ wp.expect_eq(arr.shape[0], 10)
207
+ wp.expect_eq(arr.shape[1], 20)
208
+
209
+ slice = arr[0]
210
+ wp.expect_eq(slice.shape[0], 20)
211
+
212
+
213
+ @wp.kernel
214
+ def f3(arr: wp.array3d(dtype=float)):
215
+ wp.expect_eq(arr.shape[0], 10)
216
+ wp.expect_eq(arr.shape[1], 20)
217
+ wp.expect_eq(arr.shape[2], 30)
218
+
219
+ slice = arr[0, 0]
220
+ wp.expect_eq(slice.shape[0], 30)
221
+
222
+
223
+ @wp.kernel
224
+ def f4(arr: wp.array4d(dtype=float)):
225
+ wp.expect_eq(arr.shape[0], 10)
226
+ wp.expect_eq(arr.shape[1], 20)
227
+ wp.expect_eq(arr.shape[2], 30)
228
+ wp.expect_eq(arr.shape[3], 40)
229
+
230
+ slice = arr[0, 0, 0]
231
+ wp.expect_eq(slice.shape[0], 40)
232
+
233
+
234
+ def test_shape(test, device):
235
+ with CheckOutput(test):
236
+ a1 = wp.zeros(dtype=float, shape=10, device=device)
237
+ wp.launch(f1, dim=1, inputs=[a1], device=device)
238
+
239
+ a2 = wp.zeros(dtype=float, shape=(10, 20), device=device)
240
+ wp.launch(f2, dim=1, inputs=[a2], device=device)
241
+
242
+ a3 = wp.zeros(dtype=float, shape=(10, 20, 30), device=device)
243
+ wp.launch(f3, dim=1, inputs=[a3], device=device)
244
+
245
+ a4 = wp.zeros(dtype=float, shape=(10, 20, 30, 40), device=device)
246
+ wp.launch(f4, dim=1, inputs=[a4], device=device)
247
+
248
+
249
+ def test_negative_shape(test, device):
250
+ with test.assertRaisesRegex(ValueError, "Array shapes must be non-negative"):
251
+ _ = wp.zeros(shape=-1, dtype=int, device=device)
252
+
253
+ with test.assertRaisesRegex(ValueError, "Array shapes must be non-negative"):
254
+ _ = wp.zeros(shape=-(2**32), dtype=int, device=device)
255
+
256
+ with test.assertRaisesRegex(ValueError, "Array shapes must be non-negative"):
257
+ _ = wp.zeros(shape=(10, -1), dtype=int, device=device)
258
+
259
+
260
+ @wp.kernel
261
+ def sum_array(arr: wp.array(dtype=float), loss: wp.array(dtype=float)):
262
+ tid = wp.tid()
263
+ wp.atomic_add(loss, 0, arr[tid])
264
+
265
+
266
+ def test_flatten(test, device):
267
+ np_arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=float)
268
+ arr = wp.array(np_arr, dtype=float, shape=np_arr.shape, device=device, requires_grad=True)
269
+ arr_flat = arr.flatten()
270
+ arr_comp = wp.array(np_arr.flatten(), dtype=float, device=device)
271
+ assert_array_equal(arr_flat, arr_comp)
272
+
273
+ loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
274
+ tape = wp.Tape()
275
+ with tape:
276
+ wp.launch(kernel=sum_array, dim=len(arr_flat), inputs=[arr_flat, loss], device=device)
277
+
278
+ tape.backward(loss=loss)
279
+ grad = tape.gradients[arr_flat]
280
+
281
+ ones = wp.array(
282
+ np.ones(
283
+ (8,),
284
+ dtype=float,
285
+ ),
286
+ dtype=float,
287
+ device=device,
288
+ )
289
+ assert_array_equal(grad, ones)
290
+ test.assertEqual(loss.numpy()[0], 36)
291
+
292
+
293
+ def test_reshape(test, device):
294
+ np_arr = np.arange(6, dtype=float)
295
+ arr = wp.array(np_arr, dtype=float, device=device, requires_grad=True)
296
+ arr_reshaped = arr.reshape((3, 2))
297
+ arr_comp = wp.array(np_arr.reshape((3, 2)), dtype=float, device=device)
298
+ assert_array_equal(arr_reshaped, arr_comp)
299
+
300
+ arr_reshaped = arr_reshaped.reshape(6)
301
+ assert_array_equal(arr_reshaped, arr)
302
+
303
+ loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
304
+ tape = wp.Tape()
305
+ with tape:
306
+ wp.launch(kernel=sum_array, dim=len(arr_reshaped), inputs=[arr_reshaped, loss], device=device)
307
+
308
+ tape.backward(loss=loss)
309
+ grad = tape.gradients[arr_reshaped]
310
+
311
+ ones = wp.array(
312
+ np.ones(
313
+ (6,),
314
+ dtype=float,
315
+ ),
316
+ dtype=float,
317
+ device=device,
318
+ )
319
+ assert_array_equal(grad, ones)
320
+ test.assertEqual(loss.numpy()[0], 15)
321
+
322
+ np_arr = np.arange(6, dtype=float)
323
+ arr = wp.array(np_arr, dtype=float, device=device)
324
+ arr_infer = arr.reshape((-1, 3))
325
+ arr_comp = wp.array(np_arr.reshape((-1, 3)), dtype=float, device=device)
326
+ assert_array_equal(arr_infer, arr_comp)
327
+
328
+
329
+ @wp.kernel
330
+ def compare_stepped_window_a(x: wp.array2d(dtype=float)):
331
+ wp.expect_eq(x[0, 0], 1.0)
332
+ wp.expect_eq(x[0, 1], 2.0)
333
+ wp.expect_eq(x[1, 0], 9.0)
334
+ wp.expect_eq(x[1, 1], 10.0)
335
+
336
+
337
+ @wp.kernel
338
+ def compare_stepped_window_b(x: wp.array2d(dtype=float)):
339
+ wp.expect_eq(x[0, 0], 3.0)
340
+ wp.expect_eq(x[0, 1], 4.0)
341
+ wp.expect_eq(x[1, 0], 7.0)
342
+ wp.expect_eq(x[1, 1], 8.0)
343
+ wp.expect_eq(x[2, 0], 11.0)
344
+ wp.expect_eq(x[2, 1], 12.0)
345
+
346
+
347
+ def test_slicing(test, device):
348
+ np_arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=float)
349
+ arr = wp.array(np_arr, dtype=float, shape=np_arr.shape, device=device, requires_grad=True)
350
+
351
+ slice_a = arr[1, :, :] # test indexing
352
+ slice_b = arr[1:2, :, :] # test slicing
353
+ slice_c = arr[-1, :, :] # test negative indexing
354
+ slice_d = arr[-2:-1, :, :] # test negative slicing
355
+ slice_e = arr[-1:3, :, :] # test mixed slicing
356
+ slice_e2 = slice_e[0, 0, :] # test 2x slicing
357
+ slice_f = arr[0:3:2, 0, :] # test step
358
+
359
+ assert_array_equal(slice_a, wp.array(np_arr[1, :, :], dtype=float, device=device))
360
+ assert_array_equal(slice_b, wp.array(np_arr[1:2, :, :], dtype=float, device=device))
361
+ assert_array_equal(slice_c, wp.array(np_arr[-1, :, :], dtype=float, device=device))
362
+ assert_array_equal(slice_d, wp.array(np_arr[-2:-1, :, :], dtype=float, device=device))
363
+ assert_array_equal(slice_e, wp.array(np_arr[-1:3, :, :], dtype=float, device=device))
364
+ assert_array_equal(slice_e2, wp.array(np_arr[2, 0, :], dtype=float, device=device))
365
+
366
+ # wp does not support copying from/to non-contiguous arrays
367
+ # stepped windows must read on the device the original array was created on
368
+ wp.launch(kernel=compare_stepped_window_a, dim=1, inputs=[slice_f], device=device)
369
+
370
+ slice_flat = slice_b.flatten()
371
+ loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
372
+ tape = wp.Tape()
373
+ with tape:
374
+ wp.launch(kernel=sum_array, dim=len(slice_flat), inputs=[slice_flat, loss], device=device)
375
+
376
+ tape.backward(loss=loss)
377
+ grad = tape.gradients[slice_flat]
378
+
379
+ ones = wp.array(
380
+ np.ones(
381
+ (4,),
382
+ dtype=float,
383
+ ),
384
+ dtype=float,
385
+ device=device,
386
+ )
387
+ assert_array_equal(grad, ones)
388
+ test.assertEqual(loss.numpy()[0], 26)
389
+
390
+ index_a = arr[1]
391
+ index_b = arr[2, 1]
392
+ index_c = arr[1, :]
393
+ index_d = arr[:, 1]
394
+
395
+ assert_array_equal(index_a, wp.array(np_arr[1], dtype=float, device=device))
396
+ assert_array_equal(index_b, wp.array(np_arr[2, 1], dtype=float, device=device))
397
+ assert_array_equal(index_c, wp.array(np_arr[1, :], dtype=float, device=device))
398
+ wp.launch(kernel=compare_stepped_window_b, dim=1, inputs=[index_d], device=device)
399
+
400
+ np_arr = np.zeros(10, dtype=int)
401
+ wp_arr = wp.array(np_arr, dtype=int, device=device)
402
+
403
+ assert_array_equal(wp_arr[:5], wp.array(np_arr[:5], dtype=int, device=device))
404
+ assert_array_equal(wp_arr[1:5], wp.array(np_arr[1:5], dtype=int, device=device))
405
+ assert_array_equal(wp_arr[-9:-5:1], wp.array(np_arr[-9:-5:1], dtype=int, device=device))
406
+ assert_array_equal(wp_arr[:5,], wp.array(np_arr[:5], dtype=int, device=device)) # noqa: E231
407
+
408
+
409
+ def test_view(test, device):
410
+ np_arr_a = np.arange(1, 10, 1, dtype=np.uint32)
411
+ np_arr_b = np.arange(1, 10, 1, dtype=np.float32)
412
+ np_arr_c = np.arange(1, 10, 1, dtype=np.uint16)
413
+ np_arr_d = np.arange(1, 10, 1, dtype=np.float16)
414
+ np_arr_e = np.ones((4, 4), dtype=np.float32)
415
+
416
+ wp_arr_a = wp.array(np_arr_a, dtype=wp.uint32, device=device)
417
+ wp_arr_b = wp.array(np_arr_b, dtype=wp.float32, device=device)
418
+ wp_arr_c = wp.array(np_arr_a, dtype=wp.uint16, device=device)
419
+ wp_arr_d = wp.array(np_arr_b, dtype=wp.float16, device=device)
420
+ wp_arr_e = wp.array(np_arr_e, dtype=wp.vec4, device=device)
421
+ wp_arr_f = wp.array(np_arr_e, dtype=wp.quat, device=device)
422
+
423
+ assert_np_equal(wp_arr_a.view(dtype=wp.float32).numpy(), np_arr_a.view(dtype=np.float32))
424
+ assert_np_equal(wp_arr_b.view(dtype=wp.uint32).numpy(), np_arr_b.view(dtype=np.uint32))
425
+ assert_np_equal(wp_arr_c.view(dtype=wp.float16).numpy(), np_arr_c.view(dtype=np.float16))
426
+ assert_np_equal(wp_arr_d.view(dtype=wp.uint16).numpy(), np_arr_d.view(dtype=np.uint16))
427
+ assert_array_equal(wp_arr_e.view(dtype=wp.quat), wp_arr_f)
428
+
429
+
430
+ def test_clone_adjoint(test, device):
431
+ state_in = wp.from_numpy(
432
+ np.array([1.0, 2.0, 3.0]).astype(np.float32), dtype=wp.float32, requires_grad=True, device=device
433
+ )
434
+
435
+ tape = wp.Tape()
436
+ with tape:
437
+ state_out = wp.clone(state_in)
438
+
439
+ grads = {state_out: wp.from_numpy(np.array([1.0, 1.0, 1.0]).astype(np.float32), dtype=wp.float32, device=device)}
440
+ tape.backward(grads=grads)
441
+
442
+ assert_np_equal(state_in.grad.numpy(), np.array([1.0, 1.0, 1.0]).astype(np.float32))
443
+
444
+
445
+ def test_assign_adjoint(test, device):
446
+ state_in = wp.from_numpy(
447
+ np.array([1.0, 2.0, 3.0]).astype(np.float32), dtype=wp.float32, requires_grad=True, device=device
448
+ )
449
+ state_out = wp.zeros(state_in.shape, dtype=wp.float32, requires_grad=True, device=device)
450
+
451
+ tape = wp.Tape()
452
+ with tape:
453
+ state_out.assign(state_in)
454
+
455
+ grads = {state_out: wp.from_numpy(np.array([1.0, 1.0, 1.0]).astype(np.float32), dtype=wp.float32, device=device)}
456
+ tape.backward(grads=grads)
457
+
458
+ assert_np_equal(state_in.grad.numpy(), np.array([1.0, 1.0, 1.0]).astype(np.float32))
459
+
460
+
461
+ @wp.kernel
462
+ def compare_2darrays(x: wp.array2d(dtype=float), y: wp.array2d(dtype=float), z: wp.array2d(dtype=int)):
463
+ i, j = wp.tid()
464
+
465
+ if x[i, j] == y[i, j]:
466
+ z[i, j] = 1
467
+
468
+
469
+ @wp.kernel
470
+ def compare_3darrays(x: wp.array3d(dtype=float), y: wp.array3d(dtype=float), z: wp.array3d(dtype=int)):
471
+ i, j, k = wp.tid()
472
+
473
+ if x[i, j, k] == y[i, j, k]:
474
+ z[i, j, k] = 1
475
+
476
+
477
+ def test_transpose(test, device):
478
+ # test default transpose in non-square 2d case
479
+ # wp does not support copying from/to non-contiguous arrays so check in kernel
480
+ np_arr = np.array([[1, 2], [3, 4], [5, 6]], dtype=float)
481
+ arr = wp.array(np_arr, dtype=float, device=device)
482
+ arr_transpose = arr.transpose()
483
+ arr_compare = wp.array(np_arr.transpose(), dtype=float, device=device)
484
+ check = wp.zeros(shape=(2, 3), dtype=int, device=device)
485
+
486
+ wp.launch(compare_2darrays, dim=(2, 3), inputs=[arr_transpose, arr_compare, check], device=device)
487
+ assert_np_equal(check.numpy(), np.ones((2, 3), dtype=int))
488
+
489
+ # test transpose in square 3d case
490
+ # wp does not support copying from/to non-contiguous arrays so check in kernel
491
+ np_arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=float)
492
+ arr = wp.array3d(np_arr, dtype=float, shape=np_arr.shape, device=device, requires_grad=True)
493
+ arr_transpose = arr.transpose((0, 2, 1))
494
+ arr_compare = wp.array3d(np_arr.transpose((0, 2, 1)), dtype=float, device=device)
495
+ check = wp.zeros(shape=(3, 2, 2), dtype=int, device=device)
496
+
497
+ wp.launch(compare_3darrays, dim=(3, 2, 2), inputs=[arr_transpose, arr_compare, check], device=device)
498
+ assert_np_equal(check.numpy(), np.ones((3, 2, 2), dtype=int))
499
+
500
+ # test transpose in square 3d case without axes supplied
501
+ arr_transpose = arr.transpose()
502
+ arr_compare = wp.array3d(np_arr.transpose(), dtype=float, device=device)
503
+ check = wp.zeros(shape=(2, 2, 3), dtype=int, device=device)
504
+
505
+ wp.launch(compare_3darrays, dim=(2, 2, 3), inputs=[arr_transpose, arr_compare, check], device=device)
506
+ assert_np_equal(check.numpy(), np.ones((2, 2, 3), dtype=int))
507
+
508
+ # test transpose in 1d case (should be noop)
509
+ np_arr = np.array([1, 2, 3], dtype=float)
510
+ arr = wp.array(np_arr, dtype=float, device=device)
511
+
512
+ assert_np_equal(arr.transpose().numpy(), np_arr.transpose())
513
+
514
+
515
+ def test_fill_scalar(test, device):
516
+ dim_x = 4
517
+
518
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
519
+ a1 = wp.zeros(dim_x, dtype=wptype, device=device)
520
+ a2 = wp.zeros((dim_x, dim_x), dtype=wptype, device=device)
521
+ a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=wptype, device=device)
522
+ a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=wptype, device=device)
523
+
524
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
525
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
526
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
527
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
528
+
529
+ # fill with int value
530
+ fill_value = 42
531
+
532
+ a1.fill_(fill_value)
533
+ a2.fill_(fill_value)
534
+ a3.fill_(fill_value)
535
+ a4.fill_(fill_value)
536
+
537
+ assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
538
+ assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
539
+ assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
540
+ assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
541
+
542
+ a1.zero_()
543
+ a2.zero_()
544
+ a3.zero_()
545
+ a4.zero_()
546
+
547
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
548
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
549
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
550
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
551
+
552
+ if wptype in wp.types.float_types:
553
+ # fill with float value
554
+ fill_value = 13.37
555
+
556
+ a1.fill_(fill_value)
557
+ a2.fill_(fill_value)
558
+ a3.fill_(fill_value)
559
+ a4.fill_(fill_value)
560
+
561
+ assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
562
+ assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
563
+ assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
564
+ assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
565
+
566
+ # fill with Warp scalar value
567
+ fill_value = wptype(17)
568
+
569
+ a1.fill_(fill_value)
570
+ a2.fill_(fill_value)
571
+ a3.fill_(fill_value)
572
+ a4.fill_(fill_value)
573
+
574
+ assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value.value, dtype=nptype))
575
+ assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value.value, dtype=nptype))
576
+ assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value.value, dtype=nptype))
577
+ assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value.value, dtype=nptype))
578
+
579
+
580
+ def test_fill_vector(test, device):
581
+ # test filling a vector array with scalar or vector values (vec_type, list, or numpy array)
582
+
583
+ dim_x = 4
584
+
585
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
586
+ # vector types
587
+ vector_types = [
588
+ wp.types.vector(2, wptype),
589
+ wp.types.vector(3, wptype),
590
+ wp.types.vector(4, wptype),
591
+ wp.types.vector(5, wptype),
592
+ ]
593
+
594
+ for vec_type in vector_types:
595
+ vec_len = vec_type._length_
596
+
597
+ a1 = wp.zeros(dim_x, dtype=vec_type, device=device)
598
+ a2 = wp.zeros((dim_x, dim_x), dtype=vec_type, device=device)
599
+ a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=vec_type, device=device)
600
+ a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=vec_type, device=device)
601
+
602
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
603
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
604
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
605
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
606
+
607
+ # fill with int scalar
608
+ fill_value = 42
609
+
610
+ a1.fill_(fill_value)
611
+ a2.fill_(fill_value)
612
+ a3.fill_(fill_value)
613
+ a4.fill_(fill_value)
614
+
615
+ assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
616
+ assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
617
+ assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
618
+ assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
619
+
620
+ # test zeroing
621
+ a1.zero_()
622
+ a2.zero_()
623
+ a3.zero_()
624
+ a4.zero_()
625
+
626
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
627
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
628
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
629
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
630
+
631
+ # vector values can be passed as a list, numpy array, or Warp vector instance
632
+ fill_list = [17, 42, 99, 101, 127][:vec_len]
633
+ fill_arr = np.array(fill_list, dtype=nptype)
634
+ fill_vec = vec_type(fill_list)
635
+
636
+ expected1 = np.tile(fill_arr, a1.size).reshape((*a1.shape, vec_len))
637
+ expected2 = np.tile(fill_arr, a2.size).reshape((*a2.shape, vec_len))
638
+ expected3 = np.tile(fill_arr, a3.size).reshape((*a3.shape, vec_len))
639
+ expected4 = np.tile(fill_arr, a4.size).reshape((*a4.shape, vec_len))
640
+
641
+ # fill with list of vector length
642
+ a1.fill_(fill_list)
643
+ a2.fill_(fill_list)
644
+ a3.fill_(fill_list)
645
+ a4.fill_(fill_list)
646
+
647
+ assert_np_equal(a1.numpy(), expected1)
648
+ assert_np_equal(a2.numpy(), expected2)
649
+ assert_np_equal(a3.numpy(), expected3)
650
+ assert_np_equal(a4.numpy(), expected4)
651
+
652
+ # clear
653
+ a1.zero_()
654
+ a2.zero_()
655
+ a3.zero_()
656
+ a4.zero_()
657
+
658
+ # fill with numpy array of vector length
659
+ a1.fill_(fill_arr)
660
+ a2.fill_(fill_arr)
661
+ a3.fill_(fill_arr)
662
+ a4.fill_(fill_arr)
663
+
664
+ assert_np_equal(a1.numpy(), expected1)
665
+ assert_np_equal(a2.numpy(), expected2)
666
+ assert_np_equal(a3.numpy(), expected3)
667
+ assert_np_equal(a4.numpy(), expected4)
668
+
669
+ # clear
670
+ a1.zero_()
671
+ a2.zero_()
672
+ a3.zero_()
673
+ a4.zero_()
674
+
675
+ # fill with vec instance
676
+ a1.fill_(fill_vec)
677
+ a2.fill_(fill_vec)
678
+ a3.fill_(fill_vec)
679
+ a4.fill_(fill_vec)
680
+
681
+ assert_np_equal(a1.numpy(), expected1)
682
+ assert_np_equal(a2.numpy(), expected2)
683
+ assert_np_equal(a3.numpy(), expected3)
684
+ assert_np_equal(a4.numpy(), expected4)
685
+
686
+ if wptype in wp.types.float_types:
687
+ # fill with float scalar
688
+ fill_value = 13.37
689
+
690
+ a1.fill_(fill_value)
691
+ a2.fill_(fill_value)
692
+ a3.fill_(fill_value)
693
+ a4.fill_(fill_value)
694
+
695
+ assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
696
+ assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
697
+ assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
698
+ assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
699
+
700
+ # fill with float list of vector length
701
+ fill_list = [-2.5, -1.25, 1.25, 2.5, 5.0][:vec_len]
702
+
703
+ a1.fill_(fill_list)
704
+ a2.fill_(fill_list)
705
+ a3.fill_(fill_list)
706
+ a4.fill_(fill_list)
707
+
708
+ expected1 = np.tile(np.array(fill_list, dtype=nptype), a1.size).reshape((*a1.shape, vec_len))
709
+ expected2 = np.tile(np.array(fill_list, dtype=nptype), a2.size).reshape((*a2.shape, vec_len))
710
+ expected3 = np.tile(np.array(fill_list, dtype=nptype), a3.size).reshape((*a3.shape, vec_len))
711
+ expected4 = np.tile(np.array(fill_list, dtype=nptype), a4.size).reshape((*a4.shape, vec_len))
712
+
713
+ assert_np_equal(a1.numpy(), expected1)
714
+ assert_np_equal(a2.numpy(), expected2)
715
+ assert_np_equal(a3.numpy(), expected3)
716
+ assert_np_equal(a4.numpy(), expected4)
717
+
718
+
719
+ def test_fill_matrix(test, device):
720
+ # test filling a matrix array with scalar or matrix values (mat_type, nested list, or 2d numpy array)
721
+
722
+ dim_x = 4
723
+
724
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
725
+ # matrix types
726
+ matrix_types = [
727
+ # square matrices
728
+ wp.types.matrix((2, 2), wptype),
729
+ wp.types.matrix((3, 3), wptype),
730
+ wp.types.matrix((4, 4), wptype),
731
+ wp.types.matrix((5, 5), wptype),
732
+ # non-square matrices
733
+ wp.types.matrix((2, 3), wptype),
734
+ wp.types.matrix((3, 2), wptype),
735
+ wp.types.matrix((3, 4), wptype),
736
+ wp.types.matrix((4, 3), wptype),
737
+ ]
738
+
739
+ for mat_type in matrix_types:
740
+ mat_len = mat_type._length_
741
+ mat_shape = mat_type._shape_
742
+
743
+ a1 = wp.zeros(dim_x, dtype=mat_type, device=device)
744
+ a2 = wp.zeros((dim_x, dim_x), dtype=mat_type, device=device)
745
+ a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=mat_type, device=device)
746
+ a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=mat_type, device=device)
747
+
748
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
749
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
750
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
751
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
752
+
753
+ # fill with scalar
754
+ fill_value = 42
755
+
756
+ a1.fill_(fill_value)
757
+ a2.fill_(fill_value)
758
+ a3.fill_(fill_value)
759
+ a4.fill_(fill_value)
760
+
761
+ assert_np_equal(a1.numpy(), np.full((*a1.shape, *mat_shape), fill_value, dtype=nptype))
762
+ assert_np_equal(a2.numpy(), np.full((*a2.shape, *mat_shape), fill_value, dtype=nptype))
763
+ assert_np_equal(a3.numpy(), np.full((*a3.shape, *mat_shape), fill_value, dtype=nptype))
764
+ assert_np_equal(a4.numpy(), np.full((*a4.shape, *mat_shape), fill_value, dtype=nptype))
765
+
766
+ # test zeroing
767
+ a1.zero_()
768
+ a2.zero_()
769
+ a3.zero_()
770
+ a4.zero_()
771
+
772
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
773
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
774
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
775
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
776
+
777
+ # matrix values can be passed as a 1d numpy array, 2d numpy array, flat list, nested list, or Warp matrix instance
778
+ if wptype != wp.bool:
779
+ fill_arr1 = np.arange(mat_len, dtype=nptype)
780
+ else:
781
+ fill_arr1 = np.ones(mat_len, dtype=nptype)
782
+ fill_arr2 = fill_arr1.reshape(mat_shape)
783
+ fill_list1 = list(fill_arr1)
784
+ fill_list2 = [list(row) for row in fill_arr2]
785
+ fill_mat = mat_type(fill_arr1)
786
+
787
+ expected1 = np.tile(fill_arr1, a1.size).reshape((*a1.shape, *mat_shape))
788
+ expected2 = np.tile(fill_arr1, a2.size).reshape((*a2.shape, *mat_shape))
789
+ expected3 = np.tile(fill_arr1, a3.size).reshape((*a3.shape, *mat_shape))
790
+ expected4 = np.tile(fill_arr1, a4.size).reshape((*a4.shape, *mat_shape))
791
+
792
+ # fill with 1d numpy array
793
+ a1.fill_(fill_arr1)
794
+ a2.fill_(fill_arr1)
795
+ a3.fill_(fill_arr1)
796
+ a4.fill_(fill_arr1)
797
+
798
+ assert_np_equal(a1.numpy(), expected1)
799
+ assert_np_equal(a2.numpy(), expected2)
800
+ assert_np_equal(a3.numpy(), expected3)
801
+ assert_np_equal(a4.numpy(), expected4)
802
+
803
+ # clear
804
+ a1.zero_()
805
+ a2.zero_()
806
+ a3.zero_()
807
+ a4.zero_()
808
+
809
+ # fill with 2d numpy array
810
+ a1.fill_(fill_arr2)
811
+ a2.fill_(fill_arr2)
812
+ a3.fill_(fill_arr2)
813
+ a4.fill_(fill_arr2)
814
+
815
+ assert_np_equal(a1.numpy(), expected1)
816
+ assert_np_equal(a2.numpy(), expected2)
817
+ assert_np_equal(a3.numpy(), expected3)
818
+ assert_np_equal(a4.numpy(), expected4)
819
+
820
+ # clear
821
+ a1.zero_()
822
+ a2.zero_()
823
+ a3.zero_()
824
+ a4.zero_()
825
+
826
+ # fill with flat list
827
+ a1.fill_(fill_list1)
828
+ a2.fill_(fill_list1)
829
+ a3.fill_(fill_list1)
830
+ a4.fill_(fill_list1)
831
+
832
+ assert_np_equal(a1.numpy(), expected1)
833
+ assert_np_equal(a2.numpy(), expected2)
834
+ assert_np_equal(a3.numpy(), expected3)
835
+ assert_np_equal(a4.numpy(), expected4)
836
+
837
+ # clear
838
+ a1.zero_()
839
+ a2.zero_()
840
+ a3.zero_()
841
+ a4.zero_()
842
+
843
+ # fill with nested list
844
+ a1.fill_(fill_list2)
845
+ a2.fill_(fill_list2)
846
+ a3.fill_(fill_list2)
847
+ a4.fill_(fill_list2)
848
+
849
+ assert_np_equal(a1.numpy(), expected1)
850
+ assert_np_equal(a2.numpy(), expected2)
851
+ assert_np_equal(a3.numpy(), expected3)
852
+ assert_np_equal(a4.numpy(), expected4)
853
+
854
+ # clear
855
+ a1.zero_()
856
+ a2.zero_()
857
+ a3.zero_()
858
+ a4.zero_()
859
+
860
+ # fill with mat instance
861
+ a1.fill_(fill_mat)
862
+ a2.fill_(fill_mat)
863
+ a3.fill_(fill_mat)
864
+ a4.fill_(fill_mat)
865
+
866
+ assert_np_equal(a1.numpy(), expected1)
867
+ assert_np_equal(a2.numpy(), expected2)
868
+ assert_np_equal(a3.numpy(), expected3)
869
+ assert_np_equal(a4.numpy(), expected4)
870
+
871
+
872
+ @wp.struct
873
+ class FillStruct:
874
+ # scalar members (make sure to test float16)
875
+ i1: wp.int8
876
+ i2: wp.int16
877
+ i4: wp.int32
878
+ i8: wp.int64
879
+ f2: wp.float16
880
+ f4: wp.float32
881
+ f8: wp.float16
882
+ # vector members (make sure to test vectors of float16)
883
+ v2: wp.types.vector(2, wp.int64)
884
+ v3: wp.types.vector(3, wp.float32)
885
+ v4: wp.types.vector(4, wp.float16)
886
+ v5: wp.types.vector(5, wp.uint8)
887
+ # matrix members (make sure to test matrices of float16)
888
+ m2: wp.types.matrix((2, 2), wp.float64)
889
+ m3: wp.types.matrix((3, 3), wp.int32)
890
+ m4: wp.types.matrix((4, 4), wp.float16)
891
+ m5: wp.types.matrix((5, 5), wp.int8)
892
+ # arrays
893
+ a1: wp.array(dtype=float)
894
+ a2: wp.array2d(dtype=float)
895
+ a3: wp.array3d(dtype=float)
896
+ a4: wp.array4d(dtype=float)
897
+
898
+
899
+ def test_fill_struct(test, device):
900
+ dim_x = 4
901
+
902
+ nptype = FillStruct.numpy_dtype()
903
+
904
+ a1 = wp.zeros(dim_x, dtype=FillStruct, device=device)
905
+ a2 = wp.zeros((dim_x, dim_x), dtype=FillStruct, device=device)
906
+ a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
907
+ a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
908
+
909
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
910
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
911
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
912
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
913
+
914
+ s = FillStruct()
915
+
916
+ # fill with default struct value (should be all zeros)
917
+ a1.fill_(s)
918
+ a2.fill_(s)
919
+ a3.fill_(s)
920
+ a4.fill_(s)
921
+
922
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
923
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
924
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
925
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
926
+
927
+ # scalars
928
+ s.i1 = -17
929
+ s.i2 = 42
930
+ s.i4 = 99
931
+ s.i8 = 101
932
+ s.f2 = -1.25
933
+ s.f4 = 13.37
934
+ s.f8 = 0.125
935
+ # vectors
936
+ s.v2 = [21, 22]
937
+ s.v3 = [31, 32, 33]
938
+ s.v4 = [41, 42, 43, 44]
939
+ s.v5 = [51, 52, 53, 54, 55]
940
+ # matrices
941
+ s.m2 = [[61, 62]] * 2
942
+ s.m3 = [[71, 72, 73]] * 3
943
+ s.m4 = [[81, 82, 83, 84]] * 4
944
+ s.m5 = [[91, 92, 93, 94, 95]] * 5
945
+ # arrays
946
+ s.a1 = wp.zeros((2,) * 1, dtype=float, device=device)
947
+ s.a2 = wp.zeros((2,) * 2, dtype=float, device=device)
948
+ s.a3 = wp.zeros((2,) * 3, dtype=float, device=device)
949
+ s.a4 = wp.zeros((2,) * 4, dtype=float, device=device)
950
+
951
+ # fill with custom struct value
952
+ a1.fill_(s)
953
+ a2.fill_(s)
954
+ a3.fill_(s)
955
+ a4.fill_(s)
956
+
957
+ ns = s.numpy_value()
958
+
959
+ expected1 = np.empty(a1.shape, dtype=nptype)
960
+ expected2 = np.empty(a2.shape, dtype=nptype)
961
+ expected3 = np.empty(a3.shape, dtype=nptype)
962
+ expected4 = np.empty(a4.shape, dtype=nptype)
963
+
964
+ expected1.fill(ns)
965
+ expected2.fill(ns)
966
+ expected3.fill(ns)
967
+ expected4.fill(ns)
968
+
969
+ assert_np_equal(a1.numpy(), expected1)
970
+ assert_np_equal(a2.numpy(), expected2)
971
+ assert_np_equal(a3.numpy(), expected3)
972
+ assert_np_equal(a4.numpy(), expected4)
973
+
974
+ # test clearing
975
+ a1.zero_()
976
+ a2.zero_()
977
+ a3.zero_()
978
+ a4.zero_()
979
+
980
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
981
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
982
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
983
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
984
+
985
+
986
+ def test_fill_slices(test, device):
987
+ # test fill_ and zero_ for non-contiguous arrays
988
+ # Note: we don't need to test the whole range of dtypes (vectors, matrices, structs) here
989
+
990
+ dim_x = 8
991
+
992
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
993
+ a1 = wp.zeros(dim_x, dtype=wptype, device=device)
994
+ a2 = wp.zeros((dim_x, dim_x), dtype=wptype, device=device)
995
+ a3 = wp.zeros((dim_x, dim_x, dim_x), dtype=wptype, device=device)
996
+ a4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=wptype, device=device)
997
+
998
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
999
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
1000
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
1001
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
1002
+
1003
+ # partititon each array into even and odd slices
1004
+ a1a = a1[::2]
1005
+ a1b = a1[1::2]
1006
+ a2a = a2[::2]
1007
+ a2b = a2[1::2]
1008
+ a3a = a3[::2]
1009
+ a3b = a3[1::2]
1010
+ a4a = a4[::2]
1011
+ a4b = a4[1::2]
1012
+
1013
+ # fill even slices
1014
+ fill_a = 17
1015
+ a1a.fill_(fill_a)
1016
+ a2a.fill_(fill_a)
1017
+ a3a.fill_(fill_a)
1018
+ a4a.fill_(fill_a)
1019
+
1020
+ # ensure filled slices are correct
1021
+ assert_np_equal(a1a.numpy(), np.full(a1a.shape, fill_a, dtype=nptype))
1022
+ assert_np_equal(a2a.numpy(), np.full(a2a.shape, fill_a, dtype=nptype))
1023
+ assert_np_equal(a3a.numpy(), np.full(a3a.shape, fill_a, dtype=nptype))
1024
+ assert_np_equal(a4a.numpy(), np.full(a4a.shape, fill_a, dtype=nptype))
1025
+
1026
+ # ensure unfilled slices are unaffected
1027
+ assert_np_equal(a1b.numpy(), np.zeros(a1b.shape, dtype=nptype))
1028
+ assert_np_equal(a2b.numpy(), np.zeros(a2b.shape, dtype=nptype))
1029
+ assert_np_equal(a3b.numpy(), np.zeros(a3b.shape, dtype=nptype))
1030
+ assert_np_equal(a4b.numpy(), np.zeros(a4b.shape, dtype=nptype))
1031
+
1032
+ # fill odd slices
1033
+ fill_b = 42
1034
+ a1b.fill_(fill_b)
1035
+ a2b.fill_(fill_b)
1036
+ a3b.fill_(fill_b)
1037
+ a4b.fill_(fill_b)
1038
+
1039
+ # ensure filled slices are correct
1040
+ assert_np_equal(a1b.numpy(), np.full(a1b.shape, fill_b, dtype=nptype))
1041
+ assert_np_equal(a2b.numpy(), np.full(a2b.shape, fill_b, dtype=nptype))
1042
+ assert_np_equal(a3b.numpy(), np.full(a3b.shape, fill_b, dtype=nptype))
1043
+ assert_np_equal(a4b.numpy(), np.full(a4b.shape, fill_b, dtype=nptype))
1044
+
1045
+ # ensure unfilled slices are unaffected
1046
+ assert_np_equal(a1a.numpy(), np.full(a1a.shape, fill_a, dtype=nptype))
1047
+ assert_np_equal(a2a.numpy(), np.full(a2a.shape, fill_a, dtype=nptype))
1048
+ assert_np_equal(a3a.numpy(), np.full(a3a.shape, fill_a, dtype=nptype))
1049
+ assert_np_equal(a4a.numpy(), np.full(a4a.shape, fill_a, dtype=nptype))
1050
+
1051
+ # clear even slices
1052
+ a1a.zero_()
1053
+ a2a.zero_()
1054
+ a3a.zero_()
1055
+ a4a.zero_()
1056
+
1057
+ # ensure cleared slices are correct
1058
+ assert_np_equal(a1a.numpy(), np.zeros(a1a.shape, dtype=nptype))
1059
+ assert_np_equal(a2a.numpy(), np.zeros(a2a.shape, dtype=nptype))
1060
+ assert_np_equal(a3a.numpy(), np.zeros(a3a.shape, dtype=nptype))
1061
+ assert_np_equal(a4a.numpy(), np.zeros(a4a.shape, dtype=nptype))
1062
+
1063
+ # ensure uncleared slices are unaffected
1064
+ assert_np_equal(a1b.numpy(), np.full(a1b.shape, fill_b, dtype=nptype))
1065
+ assert_np_equal(a2b.numpy(), np.full(a2b.shape, fill_b, dtype=nptype))
1066
+ assert_np_equal(a3b.numpy(), np.full(a3b.shape, fill_b, dtype=nptype))
1067
+ assert_np_equal(a4b.numpy(), np.full(a4b.shape, fill_b, dtype=nptype))
1068
+
1069
+ # re-fill even slices
1070
+ a1a.fill_(fill_a)
1071
+ a2a.fill_(fill_a)
1072
+ a3a.fill_(fill_a)
1073
+ a4a.fill_(fill_a)
1074
+
1075
+ # clear odd slices
1076
+ a1b.zero_()
1077
+ a2b.zero_()
1078
+ a3b.zero_()
1079
+ a4b.zero_()
1080
+
1081
+ # ensure cleared slices are correct
1082
+ assert_np_equal(a1b.numpy(), np.zeros(a1b.shape, dtype=nptype))
1083
+ assert_np_equal(a2b.numpy(), np.zeros(a2b.shape, dtype=nptype))
1084
+ assert_np_equal(a3b.numpy(), np.zeros(a3b.shape, dtype=nptype))
1085
+ assert_np_equal(a4b.numpy(), np.zeros(a4b.shape, dtype=nptype))
1086
+
1087
+ # ensure uncleared slices are unaffected
1088
+ assert_np_equal(a1a.numpy(), np.full(a1a.shape, fill_a, dtype=nptype))
1089
+ assert_np_equal(a2a.numpy(), np.full(a2a.shape, fill_a, dtype=nptype))
1090
+ assert_np_equal(a3a.numpy(), np.full(a3a.shape, fill_a, dtype=nptype))
1091
+ assert_np_equal(a4a.numpy(), np.full(a4a.shape, fill_a, dtype=nptype))
1092
+
1093
+
1094
+ def test_full_scalar(test, device):
1095
+ dim = 4
1096
+
1097
+ for ndim in range(1, 5):
1098
+ shape = (dim,) * ndim
1099
+
1100
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1101
+ # fill with int value and specific dtype
1102
+ fill_value = 42
1103
+ a = wp.full(shape, fill_value, dtype=wptype, device=device)
1104
+ na = a.numpy()
1105
+
1106
+ test.assertEqual(a.shape, shape)
1107
+ test.assertEqual(a.dtype, wptype)
1108
+ test.assertEqual(na.shape, shape)
1109
+ test.assertEqual(na.dtype, nptype)
1110
+ assert_np_equal(na, np.full(shape, fill_value, dtype=nptype))
1111
+
1112
+ if wptype in wp.types.float_types:
1113
+ # fill with float value and specific dtype
1114
+ fill_value = 13.37
1115
+ a = wp.full(shape, fill_value, dtype=wptype, device=device)
1116
+ na = a.numpy()
1117
+
1118
+ test.assertEqual(a.shape, shape)
1119
+ test.assertEqual(a.dtype, wptype)
1120
+ test.assertEqual(na.shape, shape)
1121
+ test.assertEqual(na.dtype, nptype)
1122
+ assert_np_equal(na, np.full(shape, fill_value, dtype=nptype))
1123
+
1124
+ # fill with int value and automatically inferred dtype
1125
+ fill_value = 42
1126
+ a = wp.full(shape, fill_value, device=device)
1127
+ na = a.numpy()
1128
+
1129
+ test.assertEqual(a.shape, shape)
1130
+ test.assertEqual(a.dtype, wp.int32)
1131
+ test.assertEqual(na.shape, shape)
1132
+ test.assertEqual(na.dtype, np.int32)
1133
+ assert_np_equal(na, np.full(shape, fill_value, dtype=np.int32))
1134
+
1135
+ # fill with float value and automatically inferred dtype
1136
+ fill_value = 13.37
1137
+ a = wp.full(shape, fill_value, device=device)
1138
+ na = a.numpy()
1139
+
1140
+ test.assertEqual(a.shape, shape)
1141
+ test.assertEqual(a.dtype, wp.float32)
1142
+ test.assertEqual(na.shape, shape)
1143
+ test.assertEqual(na.dtype, np.float32)
1144
+ assert_np_equal(na, np.full(shape, fill_value, dtype=np.float32))
1145
+
1146
+
1147
+ def test_full_vector(test, device):
1148
+ dim = 4
1149
+
1150
+ for ndim in range(1, 5):
1151
+ shape = (dim,) * ndim
1152
+
1153
+ # full from scalar
1154
+ for veclen in [2, 3, 4, 5]:
1155
+ npshape = (*shape, veclen)
1156
+
1157
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1158
+ vectype = wp.types.vector(veclen, wptype)
1159
+
1160
+ # fill with scalar int value and specific dtype
1161
+ fill_value = 42
1162
+ a = wp.full(shape, fill_value, dtype=vectype, device=device)
1163
+ na = a.numpy()
1164
+
1165
+ test.assertEqual(a.shape, shape)
1166
+ test.assertEqual(a.dtype, vectype)
1167
+ test.assertEqual(na.shape, npshape)
1168
+ test.assertEqual(na.dtype, nptype)
1169
+ assert_np_equal(na, np.full(a.size * veclen, fill_value, dtype=nptype).reshape(npshape))
1170
+
1171
+ if wptype in wp.types.float_types:
1172
+ # fill with scalar float value and specific dtype
1173
+ fill_value = 13.37
1174
+ a = wp.full(shape, fill_value, dtype=vectype, device=device)
1175
+ na = a.numpy()
1176
+
1177
+ test.assertEqual(a.shape, shape)
1178
+ test.assertEqual(a.dtype, vectype)
1179
+ test.assertEqual(na.shape, npshape)
1180
+ test.assertEqual(na.dtype, nptype)
1181
+ assert_np_equal(na, np.full(a.size * veclen, fill_value, dtype=nptype).reshape(npshape))
1182
+
1183
+ # fill with vector value and specific dtype
1184
+ fill_vec = vectype(42)
1185
+ a = wp.full(shape, fill_vec, dtype=vectype, device=device)
1186
+ na = a.numpy()
1187
+
1188
+ test.assertEqual(a.shape, shape)
1189
+ test.assertEqual(a.dtype, vectype)
1190
+ test.assertEqual(na.shape, npshape)
1191
+ test.assertEqual(na.dtype, nptype)
1192
+ assert_np_equal(na, np.full(a.size * veclen, 42, dtype=nptype).reshape(npshape))
1193
+
1194
+ # fill with vector value and automatically inferred dtype
1195
+ a = wp.full(shape, fill_vec, device=device)
1196
+ na = a.numpy()
1197
+
1198
+ test.assertEqual(a.shape, shape)
1199
+ test.assertEqual(a.dtype, vectype)
1200
+ test.assertEqual(na.shape, npshape)
1201
+ test.assertEqual(na.dtype, nptype)
1202
+ assert_np_equal(na, np.full(a.size * veclen, 42, dtype=nptype).reshape(npshape))
1203
+
1204
+ fill_lists = [
1205
+ [17, 42],
1206
+ [17, 42, 99],
1207
+ [17, 42, 99, 101],
1208
+ [17, 42, 99, 101, 127],
1209
+ ]
1210
+
1211
+ # full from list and numpy array
1212
+ for fill_list in fill_lists:
1213
+ veclen = len(fill_list)
1214
+ npshape = (*shape, veclen)
1215
+
1216
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1217
+ vectype = wp.types.vector(veclen, wptype)
1218
+
1219
+ # fill with list and specific dtype
1220
+ a = wp.full(shape, fill_list, dtype=vectype, device=device)
1221
+ na = a.numpy()
1222
+
1223
+ test.assertEqual(a.shape, shape)
1224
+ test.assertEqual(a.dtype, vectype)
1225
+ test.assertEqual(na.shape, npshape)
1226
+ test.assertEqual(na.dtype, nptype)
1227
+
1228
+ expected = np.tile(np.array(fill_list, dtype=nptype), a.size).reshape(npshape)
1229
+ assert_np_equal(na, expected)
1230
+
1231
+ fill_arr = np.array(fill_list, dtype=nptype)
1232
+
1233
+ # fill with numpy array and specific dtype
1234
+ a = wp.full(shape, fill_arr, dtype=vectype, device=device)
1235
+ na = a.numpy()
1236
+
1237
+ test.assertEqual(a.shape, shape)
1238
+ test.assertEqual(a.dtype, vectype)
1239
+ test.assertEqual(na.shape, npshape)
1240
+ test.assertEqual(na.dtype, nptype)
1241
+ assert_np_equal(na, expected)
1242
+
1243
+ # fill with numpy array and automatically infer dtype
1244
+ a = wp.full(shape, fill_arr, device=device)
1245
+ na = a.numpy()
1246
+
1247
+ test.assertEqual(a.shape, shape)
1248
+ test.assertTrue(wp.types.types_equal(a.dtype, vectype))
1249
+ test.assertEqual(na.shape, npshape)
1250
+ test.assertEqual(na.dtype, nptype)
1251
+ assert_np_equal(na, expected)
1252
+
1253
+ # fill with list and automatically infer dtype
1254
+ a = wp.full(shape, fill_list, device=device)
1255
+ na = a.numpy()
1256
+
1257
+ test.assertEqual(a.shape, shape)
1258
+
1259
+ # check that the inferred dtype is a vector
1260
+ # Note that we cannot guarantee the scalar type, because it depends on numpy and may vary by platform
1261
+ # (e.g. int64 on Linux and int32 on Windows).
1262
+ test.assertEqual(a.dtype._wp_generic_type_str_, "vec_t")
1263
+ test.assertEqual(a.dtype._length_, veclen)
1264
+
1265
+ expected = np.tile(np.array(fill_list), a.size).reshape(npshape)
1266
+ assert_np_equal(na, expected)
1267
+
1268
+
1269
+ def test_full_matrix(test, device):
1270
+ dim = 4
1271
+
1272
+ for ndim in range(1, 5):
1273
+ shape = (dim,) * ndim
1274
+
1275
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1276
+ matrix_types = [
1277
+ # square matrices
1278
+ wp.types.matrix((2, 2), wptype),
1279
+ wp.types.matrix((3, 3), wptype),
1280
+ wp.types.matrix((4, 4), wptype),
1281
+ wp.types.matrix((5, 5), wptype),
1282
+ # non-square matrices
1283
+ wp.types.matrix((2, 3), wptype),
1284
+ wp.types.matrix((3, 2), wptype),
1285
+ wp.types.matrix((3, 4), wptype),
1286
+ wp.types.matrix((4, 3), wptype),
1287
+ ]
1288
+
1289
+ for mattype in matrix_types:
1290
+ npshape = (*shape, *mattype._shape_)
1291
+
1292
+ # fill with scalar int value and specific dtype
1293
+ fill_value = 42
1294
+ a = wp.full(shape, fill_value, dtype=mattype, device=device)
1295
+ na = a.numpy()
1296
+
1297
+ test.assertEqual(a.shape, shape)
1298
+ test.assertEqual(a.dtype, mattype)
1299
+ test.assertEqual(na.shape, npshape)
1300
+ test.assertEqual(na.dtype, nptype)
1301
+ assert_np_equal(na, np.full(a.size * mattype._length_, fill_value, dtype=nptype).reshape(npshape))
1302
+
1303
+ if wptype in wp.types.float_types:
1304
+ # fill with scalar float value and specific dtype
1305
+ fill_value = 13.37
1306
+ a = wp.full(shape, fill_value, dtype=mattype, device=device)
1307
+ na = a.numpy()
1308
+
1309
+ test.assertEqual(a.shape, shape)
1310
+ test.assertEqual(a.dtype, mattype)
1311
+ test.assertEqual(na.shape, npshape)
1312
+ test.assertEqual(na.dtype, nptype)
1313
+ assert_np_equal(na, np.full(a.size * mattype._length_, fill_value, dtype=nptype).reshape(npshape))
1314
+
1315
+ # fill with matrix value and specific dtype
1316
+ fill_mat = mattype(42)
1317
+ a = wp.full(shape, fill_mat, dtype=mattype, device=device)
1318
+ na = a.numpy()
1319
+
1320
+ test.assertEqual(a.shape, shape)
1321
+ test.assertEqual(a.dtype, mattype)
1322
+ test.assertEqual(na.shape, npshape)
1323
+ test.assertEqual(na.dtype, nptype)
1324
+ assert_np_equal(na, np.full(a.size * mattype._length_, 42, dtype=nptype).reshape(npshape))
1325
+
1326
+ # fill with matrix value and automatically inferred dtype
1327
+ fill_mat = mattype(42)
1328
+ a = wp.full(shape, fill_mat, device=device)
1329
+ na = a.numpy()
1330
+
1331
+ test.assertEqual(a.shape, shape)
1332
+ test.assertEqual(a.dtype, mattype)
1333
+ test.assertEqual(na.shape, npshape)
1334
+ test.assertEqual(na.dtype, nptype)
1335
+ assert_np_equal(na, np.full(a.size * mattype._length_, 42, dtype=nptype).reshape(npshape))
1336
+
1337
+ # fill with 1d numpy array and specific dtype
1338
+ if wptype != wp.bool:
1339
+ fill_arr1d = np.arange(mattype._length_, dtype=nptype)
1340
+ else:
1341
+ fill_arr1d = np.ones(mattype._length_, dtype=nptype)
1342
+ a = wp.full(shape, fill_arr1d, dtype=mattype, device=device)
1343
+ na = a.numpy()
1344
+
1345
+ test.assertEqual(a.shape, shape)
1346
+ test.assertEqual(a.dtype, mattype)
1347
+ test.assertEqual(na.shape, npshape)
1348
+ test.assertEqual(na.dtype, nptype)
1349
+
1350
+ expected = np.tile(fill_arr1d, a.size).reshape(npshape)
1351
+ assert_np_equal(na, expected)
1352
+
1353
+ # fill with 2d numpy array and specific dtype
1354
+ fill_arr2d = fill_arr1d.reshape(mattype._shape_)
1355
+ a = wp.full(shape, fill_arr2d, dtype=mattype, device=device)
1356
+ na = a.numpy()
1357
+
1358
+ test.assertEqual(a.shape, shape)
1359
+ test.assertEqual(a.dtype, mattype)
1360
+ test.assertEqual(na.shape, npshape)
1361
+ test.assertEqual(na.dtype, nptype)
1362
+ assert_np_equal(na, expected)
1363
+
1364
+ # fill with 2d numpy array and automatically infer dtype
1365
+ a = wp.full(shape, fill_arr2d, device=device)
1366
+ na = a.numpy()
1367
+
1368
+ test.assertEqual(a.shape, shape)
1369
+ test.assertTrue(wp.types.types_equal(a.dtype, mattype))
1370
+ test.assertEqual(na.shape, npshape)
1371
+ test.assertEqual(na.dtype, nptype)
1372
+ assert_np_equal(na, expected)
1373
+
1374
+ # fill with flat list and specific dtype
1375
+ fill_list1d = list(fill_arr1d)
1376
+ a = wp.full(shape, fill_list1d, dtype=mattype, device=device)
1377
+ na = a.numpy()
1378
+
1379
+ test.assertEqual(a.shape, shape)
1380
+ test.assertEqual(a.dtype, mattype)
1381
+ test.assertEqual(na.shape, npshape)
1382
+ test.assertEqual(na.dtype, nptype)
1383
+ assert_np_equal(na, expected)
1384
+
1385
+ # fill with nested list and specific dtype
1386
+ fill_list2d = [list(row) for row in fill_arr2d]
1387
+ a = wp.full(shape, fill_list2d, dtype=mattype, device=device)
1388
+ na = a.numpy()
1389
+
1390
+ test.assertEqual(a.shape, shape)
1391
+ test.assertEqual(a.dtype, mattype)
1392
+ test.assertEqual(na.shape, npshape)
1393
+ test.assertEqual(na.dtype, nptype)
1394
+ assert_np_equal(na, expected)
1395
+
1396
+ mat_lists = [
1397
+ # square matrices
1398
+ [[1, 2], [3, 4]],
1399
+ [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
1400
+ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]],
1401
+ # non-square matrices
1402
+ [[1, 2, 3, 4], [5, 6, 7, 8]],
1403
+ [[1, 2], [3, 4], [5, 6], [7, 8]],
1404
+ ]
1405
+
1406
+ # fill with nested lists and automatically infer dtype
1407
+ for fill_list in mat_lists:
1408
+ num_rows = len(fill_list)
1409
+ num_cols = len(fill_list[0])
1410
+ npshape = (*shape, num_rows, num_cols)
1411
+
1412
+ a = wp.full(shape, fill_list, device=device)
1413
+ na = a.numpy()
1414
+
1415
+ test.assertEqual(a.shape, shape)
1416
+
1417
+ # check that the inferred dtype is a correctly shaped matrix
1418
+ # Note that we cannot guarantee the scalar type, because it depends on numpy and may vary by platform
1419
+ # (e.g. int64 on Linux and int32 on Windows).
1420
+ test.assertEqual(a.dtype._wp_generic_type_str_, "mat_t")
1421
+ test.assertEqual(a.dtype._shape_, (num_rows, num_cols))
1422
+
1423
+ expected = np.tile(np.array(fill_list).flatten(), a.size).reshape(npshape)
1424
+ assert_np_equal(na, expected)
1425
+
1426
+
1427
+ def test_full_struct(test, device):
1428
+ dim = 4
1429
+
1430
+ for ndim in range(1, 5):
1431
+ shape = (dim,) * ndim
1432
+
1433
+ s = FillStruct()
1434
+
1435
+ # fill with default struct (should be zeros)
1436
+ a = wp.full(shape, s, dtype=FillStruct, device=device)
1437
+ na = a.numpy()
1438
+
1439
+ test.assertEqual(a.shape, shape)
1440
+ test.assertEqual(a.dtype, FillStruct)
1441
+ test.assertEqual(na.shape, shape)
1442
+ test.assertEqual(na.dtype, FillStruct.numpy_dtype())
1443
+ assert_np_equal(na, np.zeros(a.shape, dtype=FillStruct.numpy_dtype()))
1444
+
1445
+ # scalars
1446
+ s.i1 = -17
1447
+ s.i2 = 42
1448
+ s.i4 = 99
1449
+ s.i8 = 101
1450
+ s.f2 = -1.25
1451
+ s.f4 = 13.37
1452
+ s.f8 = 0.125
1453
+ # vectors
1454
+ s.v2 = [21, 22]
1455
+ s.v3 = [31, 32, 33]
1456
+ s.v4 = [41, 42, 43, 44]
1457
+ s.v5 = [51, 52, 53, 54, 55]
1458
+ # matrices
1459
+ s.m2 = [[61, 62]] * 2
1460
+ s.m3 = [[71, 72, 73]] * 3
1461
+ s.m4 = [[81, 82, 83, 84]] * 4
1462
+ s.m5 = [[91, 92, 93, 94, 95]] * 5
1463
+ # arrays
1464
+ s.a1 = wp.zeros((2,) * 1, dtype=float, device=device)
1465
+ s.a2 = wp.zeros((2,) * 2, dtype=float, device=device)
1466
+ s.a3 = wp.zeros((2,) * 3, dtype=float, device=device)
1467
+ s.a4 = wp.zeros((2,) * 4, dtype=float, device=device)
1468
+
1469
+ # fill with initialized struct and explicit dtype
1470
+ a = wp.full(shape, s, dtype=FillStruct, device=device)
1471
+ na = a.numpy()
1472
+
1473
+ test.assertEqual(a.shape, shape)
1474
+ test.assertEqual(a.dtype, FillStruct)
1475
+ test.assertEqual(na.shape, shape)
1476
+ test.assertEqual(na.dtype, FillStruct.numpy_dtype())
1477
+
1478
+ expected = np.empty(shape, dtype=FillStruct.numpy_dtype())
1479
+ expected.fill(s.numpy_value())
1480
+ assert_np_equal(na, expected)
1481
+
1482
+ # fill with initialized struct and automatically inferred dtype
1483
+ a = wp.full(shape, s, device=device)
1484
+ na = a.numpy()
1485
+
1486
+ test.assertEqual(a.shape, shape)
1487
+ test.assertEqual(a.dtype, FillStruct)
1488
+ test.assertEqual(na.shape, shape)
1489
+ test.assertEqual(na.dtype, FillStruct.numpy_dtype())
1490
+ assert_np_equal(na, expected)
1491
+
1492
+
1493
+ def test_ones_scalar(test, device):
1494
+ dim = 4
1495
+
1496
+ for ndim in range(1, 5):
1497
+ shape = (dim,) * ndim
1498
+
1499
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1500
+ a = wp.ones(shape, dtype=wptype, device=device)
1501
+ na = a.numpy()
1502
+
1503
+ test.assertEqual(a.shape, shape)
1504
+ test.assertEqual(a.dtype, wptype)
1505
+ test.assertEqual(na.shape, shape)
1506
+ test.assertEqual(na.dtype, nptype)
1507
+ assert_np_equal(na, np.ones(shape, dtype=nptype))
1508
+
1509
+
1510
+ def test_ones_vector(test, device):
1511
+ dim = 4
1512
+
1513
+ for ndim in range(1, 5):
1514
+ shape = (dim,) * ndim
1515
+
1516
+ for veclen in [2, 3, 4, 5]:
1517
+ npshape = (*shape, veclen)
1518
+
1519
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1520
+ vectype = wp.types.vector(veclen, wptype)
1521
+
1522
+ a = wp.ones(shape, dtype=vectype, device=device)
1523
+ na = a.numpy()
1524
+
1525
+ test.assertEqual(a.shape, shape)
1526
+ test.assertEqual(a.dtype, vectype)
1527
+ test.assertEqual(na.shape, npshape)
1528
+ test.assertEqual(na.dtype, nptype)
1529
+ assert_np_equal(na, np.ones(npshape, dtype=nptype))
1530
+
1531
+
1532
+ def test_ones_matrix(test, device):
1533
+ dim = 4
1534
+
1535
+ for ndim in range(1, 5):
1536
+ shape = (dim,) * ndim
1537
+
1538
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1539
+ matrix_types = [
1540
+ # square matrices
1541
+ wp.types.matrix((2, 2), wptype),
1542
+ wp.types.matrix((3, 3), wptype),
1543
+ wp.types.matrix((4, 4), wptype),
1544
+ wp.types.matrix((5, 5), wptype),
1545
+ # non-square matrices
1546
+ wp.types.matrix((2, 3), wptype),
1547
+ wp.types.matrix((3, 2), wptype),
1548
+ wp.types.matrix((3, 4), wptype),
1549
+ wp.types.matrix((4, 3), wptype),
1550
+ ]
1551
+
1552
+ for mattype in matrix_types:
1553
+ npshape = (*shape, *mattype._shape_)
1554
+
1555
+ a = wp.ones(shape, dtype=mattype, device=device)
1556
+ na = a.numpy()
1557
+
1558
+ test.assertEqual(a.shape, shape)
1559
+ test.assertEqual(a.dtype, mattype)
1560
+ test.assertEqual(na.shape, npshape)
1561
+ test.assertEqual(na.dtype, nptype)
1562
+ assert_np_equal(na, np.ones(npshape, dtype=nptype))
1563
+
1564
+
1565
+ def test_ones_like_scalar(test, device):
1566
+ dim = 4
1567
+
1568
+ for ndim in range(1, 5):
1569
+ shape = (dim,) * ndim
1570
+
1571
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1572
+ # source array
1573
+ a = wp.zeros(shape, dtype=wptype, device=device)
1574
+ na = a.numpy()
1575
+ test.assertEqual(a.shape, shape)
1576
+ test.assertEqual(a.dtype, wptype)
1577
+ test.assertEqual(na.shape, shape)
1578
+ test.assertEqual(na.dtype, nptype)
1579
+ assert_np_equal(na, np.zeros(shape, dtype=nptype))
1580
+
1581
+ # ones array
1582
+ b = wp.ones_like(a)
1583
+ nb = b.numpy()
1584
+ test.assertEqual(b.shape, shape)
1585
+ test.assertEqual(b.dtype, wptype)
1586
+ test.assertEqual(nb.shape, shape)
1587
+ test.assertEqual(nb.dtype, nptype)
1588
+ assert_np_equal(nb, np.ones(shape, dtype=nptype))
1589
+
1590
+
1591
+ def test_ones_like_vector(test, device):
1592
+ dim = 4
1593
+
1594
+ for ndim in range(1, 5):
1595
+ shape = (dim,) * ndim
1596
+
1597
+ for veclen in [2, 3, 4, 5]:
1598
+ npshape = (*shape, veclen)
1599
+
1600
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1601
+ vectype = wp.types.vector(veclen, wptype)
1602
+
1603
+ # source array
1604
+ a = wp.zeros(shape, dtype=vectype, device=device)
1605
+ na = a.numpy()
1606
+ test.assertEqual(a.shape, shape)
1607
+ test.assertEqual(a.dtype, vectype)
1608
+ test.assertEqual(na.shape, npshape)
1609
+ test.assertEqual(na.dtype, nptype)
1610
+ assert_np_equal(na, np.zeros(npshape, dtype=nptype))
1611
+
1612
+ # ones array
1613
+ b = wp.ones_like(a)
1614
+ nb = b.numpy()
1615
+ test.assertEqual(b.shape, shape)
1616
+ test.assertEqual(b.dtype, vectype)
1617
+ test.assertEqual(nb.shape, npshape)
1618
+ test.assertEqual(nb.dtype, nptype)
1619
+ assert_np_equal(nb, np.ones(npshape, dtype=nptype))
1620
+
1621
+
1622
+ def test_ones_like_matrix(test, device):
1623
+ dim = 4
1624
+
1625
+ for ndim in range(1, 5):
1626
+ shape = (dim,) * ndim
1627
+
1628
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1629
+ matrix_types = [
1630
+ # square matrices
1631
+ wp.types.matrix((2, 2), wptype),
1632
+ wp.types.matrix((3, 3), wptype),
1633
+ wp.types.matrix((4, 4), wptype),
1634
+ wp.types.matrix((5, 5), wptype),
1635
+ # non-square matrices
1636
+ wp.types.matrix((2, 3), wptype),
1637
+ wp.types.matrix((3, 2), wptype),
1638
+ wp.types.matrix((3, 4), wptype),
1639
+ wp.types.matrix((4, 3), wptype),
1640
+ ]
1641
+
1642
+ for mattype in matrix_types:
1643
+ npshape = (*shape, *mattype._shape_)
1644
+
1645
+ # source array
1646
+ a = wp.zeros(shape, dtype=mattype, device=device)
1647
+ na = a.numpy()
1648
+ test.assertEqual(a.shape, shape)
1649
+ test.assertEqual(a.dtype, mattype)
1650
+ test.assertEqual(na.shape, npshape)
1651
+ test.assertEqual(na.dtype, nptype)
1652
+ assert_np_equal(na, np.zeros(npshape, dtype=nptype))
1653
+
1654
+ # ones array
1655
+ b = wp.ones_like(a)
1656
+ nb = b.numpy()
1657
+ test.assertEqual(b.shape, shape)
1658
+ test.assertEqual(b.dtype, mattype)
1659
+ test.assertEqual(nb.shape, npshape)
1660
+ test.assertEqual(nb.dtype, nptype)
1661
+ assert_np_equal(nb, np.ones(npshape, dtype=nptype))
1662
+
1663
+
1664
+ def test_round_trip(test, device):
1665
+ rng = np.random.default_rng(123)
1666
+ dim_x = 4
1667
+
1668
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1669
+ a_np = rng.standard_normal(size=dim_x).astype(nptype)
1670
+ a = wp.array(a_np, device=device)
1671
+ test.assertEqual(a.dtype, wptype)
1672
+
1673
+ assert_np_equal(a.numpy(), a_np)
1674
+
1675
+ v_np = rng.standard_normal(size=(dim_x, 3)).astype(nptype)
1676
+ v = wp.array(v_np, dtype=wp.types.vector(3, wptype), device=device)
1677
+
1678
+ assert_np_equal(v.numpy(), v_np)
1679
+
1680
+
1681
+ def test_empty_array(test, device):
1682
+ # Test whether common operations work with empty (zero-sized) arrays
1683
+ # without throwing exceptions.
1684
+
1685
+ def test_empty_ops(ndim, nrows, ncols, wptype, nptype):
1686
+ shape = (0,) * ndim
1687
+ dtype_shape = ()
1688
+
1689
+ if wptype in wp.types.scalar_types:
1690
+ # scalar, vector, or matrix
1691
+ if ncols > 0:
1692
+ if nrows > 0:
1693
+ wptype = wp.types.matrix((nrows, ncols), wptype)
1694
+ else:
1695
+ wptype = wp.types.vector(ncols, wptype)
1696
+ dtype_shape = wptype._shape_
1697
+ fill_value = wptype(42)
1698
+ else:
1699
+ # struct
1700
+ fill_value = wptype()
1701
+
1702
+ # create a zero-sized array
1703
+ a = wp.empty(shape, dtype=wptype, device=device, requires_grad=True)
1704
+
1705
+ test.assertEqual(a.ptr, None)
1706
+ test.assertEqual(a.size, 0)
1707
+ test.assertEqual(a.shape, shape)
1708
+ test.assertEqual(a.grad.ptr, None)
1709
+ test.assertEqual(a.grad.size, 0)
1710
+ test.assertEqual(a.grad.shape, shape)
1711
+
1712
+ # all of these methods should succeed with zero-sized arrays
1713
+ a.zero_()
1714
+ a.fill_(fill_value)
1715
+ b = a.flatten()
1716
+ b = a.reshape((0,))
1717
+ b = a.transpose()
1718
+ b = a.contiguous()
1719
+
1720
+ b = wp.empty_like(a)
1721
+ b = wp.zeros_like(a)
1722
+ b = wp.full_like(a, fill_value)
1723
+ b = wp.clone(a)
1724
+
1725
+ wp.copy(a, b)
1726
+ a.assign(b)
1727
+
1728
+ na = a.numpy()
1729
+ test.assertEqual(na.size, 0)
1730
+ test.assertEqual(na.shape, (*shape, *dtype_shape))
1731
+ test.assertEqual(na.dtype, nptype)
1732
+
1733
+ test.assertEqual(a.list(), [])
1734
+
1735
+ for ndim in range(1, 5):
1736
+ # test with scalars, vectors, and matrices
1737
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1738
+ # scalars
1739
+ test_empty_ops(ndim, 0, 0, wptype, nptype)
1740
+
1741
+ for ncols in [2, 3, 4, 5]:
1742
+ # vectors
1743
+ test_empty_ops(ndim, 0, ncols, wptype, nptype)
1744
+ # square matrices
1745
+ test_empty_ops(ndim, ncols, ncols, wptype, nptype)
1746
+
1747
+ # non-square matrices
1748
+ test_empty_ops(ndim, 2, 3, wptype, nptype)
1749
+ test_empty_ops(ndim, 3, 2, wptype, nptype)
1750
+ test_empty_ops(ndim, 3, 4, wptype, nptype)
1751
+ test_empty_ops(ndim, 4, 3, wptype, nptype)
1752
+
1753
+ # test with structs
1754
+ test_empty_ops(ndim, 0, 0, FillStruct, FillStruct.numpy_dtype())
1755
+
1756
+
1757
+ def test_empty_from_numpy(test, device):
1758
+ # Test whether wrapping an empty (zero-sized) numpy array works correctly
1759
+
1760
+ def test_empty_from_data(ndim, nrows, ncols, wptype, nptype):
1761
+ shape = (0,) * ndim
1762
+ dtype_shape = ()
1763
+
1764
+ if ncols > 0:
1765
+ if nrows > 0:
1766
+ wptype = wp.types.matrix((nrows, ncols), wptype)
1767
+ else:
1768
+ wptype = wp.types.vector(ncols, wptype)
1769
+ dtype_shape = wptype._shape_
1770
+
1771
+ npshape = (*shape, *dtype_shape)
1772
+
1773
+ na = np.empty(npshape, dtype=nptype)
1774
+ a = wp.array(na, dtype=wptype, device=device)
1775
+ test.assertEqual(a.size, 0)
1776
+ test.assertEqual(a.shape, shape)
1777
+
1778
+ for ndim in range(1, 5):
1779
+ # test with scalars, vectors, and matrices
1780
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
1781
+ # scalars
1782
+ test_empty_from_data(ndim, 0, 0, wptype, nptype)
1783
+
1784
+ for ncols in [2, 3, 4, 5]:
1785
+ # vectors
1786
+ test_empty_from_data(ndim, 0, ncols, wptype, nptype)
1787
+ # square matrices
1788
+ test_empty_from_data(ndim, ncols, ncols, wptype, nptype)
1789
+
1790
+ # non-square matrices
1791
+ test_empty_from_data(ndim, 2, 3, wptype, nptype)
1792
+ test_empty_from_data(ndim, 3, 2, wptype, nptype)
1793
+ test_empty_from_data(ndim, 3, 4, wptype, nptype)
1794
+ test_empty_from_data(ndim, 4, 3, wptype, nptype)
1795
+
1796
+
1797
+ def test_empty_from_list(test, device):
1798
+ # Test whether creating an array from an empty Python list works correctly
1799
+
1800
+ def test_empty_from_data(nrows, ncols, wptype):
1801
+ if ncols > 0:
1802
+ if nrows > 0:
1803
+ wptype = wp.types.matrix((nrows, ncols), wptype)
1804
+ else:
1805
+ wptype = wp.types.vector(ncols, wptype)
1806
+
1807
+ a = wp.array([], dtype=wptype, device=device)
1808
+ test.assertEqual(a.size, 0)
1809
+ test.assertEqual(a.shape, (0,))
1810
+
1811
+ # test with scalars, vectors, and matrices
1812
+ for wptype in wp.types.scalar_types:
1813
+ # scalars
1814
+ test_empty_from_data(0, 0, wptype)
1815
+
1816
+ for ncols in [2, 3, 4, 5]:
1817
+ # vectors
1818
+ test_empty_from_data(0, ncols, wptype)
1819
+ # square matrices
1820
+ test_empty_from_data(ncols, ncols, wptype)
1821
+
1822
+ # non-square matrices
1823
+ test_empty_from_data(2, 3, wptype)
1824
+ test_empty_from_data(3, 2, wptype)
1825
+ test_empty_from_data(3, 4, wptype)
1826
+ test_empty_from_data(4, 3, wptype)
1827
+
1828
+
1829
+ def test_to_list_scalar(test, device):
1830
+ dim = 3
1831
+ fill_value = 42
1832
+
1833
+ for ndim in range(1, 5):
1834
+ shape = (dim,) * ndim
1835
+
1836
+ for wptype in wp.types.scalar_types:
1837
+ a = wp.full(shape, fill_value, dtype=wptype, device=device)
1838
+ l = a.list()
1839
+
1840
+ test.assertEqual(len(l), a.size)
1841
+ test.assertTrue(all(x == fill_value for x in l))
1842
+
1843
+
1844
+ def test_to_list_vector(test, device):
1845
+ dim = 3
1846
+
1847
+ for ndim in range(1, 5):
1848
+ shape = (dim,) * ndim
1849
+
1850
+ for veclen in [2, 3, 4, 5]:
1851
+ for wptype in wp.types.scalar_types:
1852
+ vectype = wp.types.vector(veclen, wptype)
1853
+ fill_value = vectype(42)
1854
+
1855
+ a = wp.full(shape, fill_value, dtype=vectype, device=device)
1856
+ l = a.list()
1857
+
1858
+ test.assertEqual(len(l), a.size)
1859
+ test.assertTrue(all(x == fill_value for x in l))
1860
+
1861
+
1862
+ def test_to_list_matrix(test, device):
1863
+ dim = 3
1864
+
1865
+ for ndim in range(1, 5):
1866
+ shape = (dim,) * ndim
1867
+
1868
+ for wptype in wp.types.scalar_types:
1869
+ matrix_types = [
1870
+ # square matrices
1871
+ wp.types.matrix((2, 2), wptype),
1872
+ wp.types.matrix((3, 3), wptype),
1873
+ wp.types.matrix((4, 4), wptype),
1874
+ wp.types.matrix((5, 5), wptype),
1875
+ # non-square matrices
1876
+ wp.types.matrix((2, 3), wptype),
1877
+ wp.types.matrix((3, 2), wptype),
1878
+ wp.types.matrix((3, 4), wptype),
1879
+ wp.types.matrix((4, 3), wptype),
1880
+ ]
1881
+
1882
+ for mattype in matrix_types:
1883
+ fill_value = mattype(42)
1884
+
1885
+ a = wp.full(shape, fill_value, dtype=mattype, device=device)
1886
+ l = a.list()
1887
+
1888
+ test.assertEqual(len(l), a.size)
1889
+ test.assertTrue(all(x == fill_value for x in l))
1890
+
1891
+
1892
+ def test_to_list_struct(test, device):
1893
+ @wp.struct
1894
+ class Inner:
1895
+ h: wp.float16
1896
+ v: wp.vec3
1897
+
1898
+ @wp.struct
1899
+ class ListStruct:
1900
+ i: int
1901
+ f: float
1902
+ h: wp.float16
1903
+ vi: wp.vec2i
1904
+ vf: wp.vec3f
1905
+ vh: wp.vec4h
1906
+ mi: wp.types.matrix((2, 2), int)
1907
+ mf: wp.types.matrix((3, 3), float)
1908
+ mh: wp.types.matrix((4, 4), wp.float16)
1909
+ inner: Inner
1910
+ a1: wp.array(dtype=int)
1911
+ a2: wp.array2d(dtype=float)
1912
+ a3: wp.array3d(dtype=wp.float16)
1913
+ bool: wp.bool
1914
+
1915
+ dim = 3
1916
+
1917
+ s = ListStruct()
1918
+ s.i = 42
1919
+ s.f = 2.5
1920
+ s.h = -1.25
1921
+ s.vi = wp.vec2i(1, 2)
1922
+ s.vf = wp.vec3f(0.1, 0.2, 0.3)
1923
+ s.vh = wp.vec4h(1.0, 2.0, 3.0, 4.0)
1924
+ s.mi = [[1, 2], [3, 4]]
1925
+ s.mf = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
1926
+ s.mh = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
1927
+ s.inner = Inner()
1928
+ s.inner.h = 1.5
1929
+ s.inner.v = [1, 2, 3]
1930
+ s.a1 = wp.empty(1, dtype=int, device=device)
1931
+ s.a2 = wp.empty((1, 1), dtype=float, device=device)
1932
+ s.a3 = wp.empty((1, 1, 1), dtype=wp.float16, device=device)
1933
+ s.bool = True
1934
+
1935
+ for ndim in range(1, 5):
1936
+ shape = (dim,) * ndim
1937
+
1938
+ a = wp.full(shape, s, dtype=ListStruct, device=device)
1939
+ l = a.list()
1940
+
1941
+ for i in range(a.size):
1942
+ test.assertEqual(l[i].i, s.i)
1943
+ test.assertEqual(l[i].f, s.f)
1944
+ test.assertEqual(l[i].h, s.h)
1945
+ test.assertEqual(l[i].vi, s.vi)
1946
+ test.assertEqual(l[i].vf, s.vf)
1947
+ test.assertEqual(l[i].vh, s.vh)
1948
+ test.assertEqual(l[i].mi, s.mi)
1949
+ test.assertEqual(l[i].mf, s.mf)
1950
+ test.assertEqual(l[i].mh, s.mh)
1951
+ test.assertEqual(l[i].bool, s.bool)
1952
+ test.assertEqual(l[i].inner.h, s.inner.h)
1953
+ test.assertEqual(l[i].inner.v, s.inner.v)
1954
+ test.assertEqual(l[i].a1.dtype, s.a1.dtype)
1955
+ test.assertEqual(l[i].a1.ndim, s.a1.ndim)
1956
+ test.assertEqual(l[i].a2.dtype, s.a2.dtype)
1957
+ test.assertEqual(l[i].a2.ndim, s.a2.ndim)
1958
+ test.assertEqual(l[i].a3.dtype, s.a3.dtype)
1959
+ test.assertEqual(l[i].a3.ndim, s.a3.ndim)
1960
+
1961
+
1962
+ @wp.kernel
1963
+ def kernel_array_to_bool(array_null: wp.array(dtype=float), array_valid: wp.array(dtype=float)):
1964
+ if not array_null:
1965
+ # always succeed
1966
+ wp.expect_eq(0, 0)
1967
+ else:
1968
+ # force failure
1969
+ wp.expect_eq(1, 2)
1970
+
1971
+ if array_valid:
1972
+ # always succeed
1973
+ wp.expect_eq(0, 0)
1974
+ else:
1975
+ # force failure
1976
+ wp.expect_eq(1, 2)
1977
+
1978
+
1979
+ def test_array_to_bool(test, device):
1980
+ arr = wp.zeros(8, dtype=float, device=device)
1981
+
1982
+ wp.launch(kernel_array_to_bool, dim=1, inputs=[None, arr], device=device)
1983
+
1984
+
1985
+ @wp.struct
1986
+ class InputStruct:
1987
+ param1: int
1988
+ param2: float
1989
+ param3: wp.vec3
1990
+ param4: wp.array(dtype=float)
1991
+
1992
+
1993
+ @wp.struct
1994
+ class OutputStruct:
1995
+ param1: int
1996
+ param2: float
1997
+ param3: wp.vec3
1998
+
1999
+
2000
+ @wp.kernel
2001
+ def struct_array_kernel(inputs: wp.array(dtype=InputStruct), outputs: wp.array(dtype=OutputStruct)):
2002
+ tid = wp.tid()
2003
+
2004
+ wp.expect_eq(inputs[tid].param1, tid)
2005
+ wp.expect_eq(inputs[tid].param2, float(tid * tid))
2006
+
2007
+ wp.expect_eq(inputs[tid].param3[0], 1.0)
2008
+ wp.expect_eq(inputs[tid].param3[1], 2.0)
2009
+ wp.expect_eq(inputs[tid].param3[2], 3.0)
2010
+
2011
+ wp.expect_eq(inputs[tid].param4[0], 1.0)
2012
+ wp.expect_eq(inputs[tid].param4[1], 2.0)
2013
+ wp.expect_eq(inputs[tid].param4[2], 3.0)
2014
+
2015
+ o = OutputStruct()
2016
+ o.param1 = inputs[tid].param1
2017
+ o.param2 = inputs[tid].param2
2018
+ o.param3 = inputs[tid].param3
2019
+
2020
+ outputs[tid] = o
2021
+
2022
+
2023
+ def test_array_of_structs(test, device):
2024
+ num_items = 10
2025
+
2026
+ l = []
2027
+ for i in range(num_items):
2028
+ s = InputStruct()
2029
+ s.param1 = i
2030
+ s.param2 = float(i * i)
2031
+ s.param3 = wp.vec3(1.0, 2.0, 3.0)
2032
+ s.param4 = wp.array([1.0, 2.0, 3.0], dtype=float, device=device)
2033
+
2034
+ l.append(s)
2035
+
2036
+ # initialize array from list of structs
2037
+ inputs = wp.array(l, dtype=InputStruct, device=device)
2038
+ outputs = wp.zeros(num_items, dtype=OutputStruct, device=device)
2039
+
2040
+ # pass to our compute kernel
2041
+ wp.launch(struct_array_kernel, dim=num_items, inputs=[inputs, outputs], device=device)
2042
+
2043
+ out_numpy = outputs.numpy()
2044
+ out_list = outputs.list()
2045
+ out_cptr = outputs.to("cpu").cptr()
2046
+
2047
+ for i in range(num_items):
2048
+ test.assertEqual(out_numpy[i][0], l[i].param1)
2049
+ test.assertEqual(out_numpy[i][1], l[i].param2)
2050
+ assert_np_equal(out_numpy[i][2], np.array(l[i].param3))
2051
+
2052
+ # test named slices of numpy structured array
2053
+ test.assertEqual(out_numpy["param1"][i], l[i].param1)
2054
+ test.assertEqual(out_numpy["param2"][i], l[i].param2)
2055
+ assert_np_equal(out_numpy["param3"][i], np.array(l[i].param3))
2056
+
2057
+ test.assertEqual(out_list[i].param1, l[i].param1)
2058
+ test.assertEqual(out_list[i].param2, l[i].param2)
2059
+ test.assertEqual(out_list[i].param3, l[i].param3)
2060
+
2061
+ test.assertEqual(out_cptr[i].param1, l[i].param1)
2062
+ test.assertEqual(out_cptr[i].param2, l[i].param2)
2063
+ test.assertEqual(out_cptr[i].param3, l[i].param3)
2064
+
2065
+
2066
+ @wp.struct
2067
+ class GradStruct:
2068
+ param1: int
2069
+ param2: float
2070
+ param3: wp.vec3
2071
+
2072
+
2073
+ @wp.kernel
2074
+ def test_array_of_structs_grad_kernel(inputs: wp.array(dtype=GradStruct), loss: wp.array(dtype=float)):
2075
+ tid = wp.tid()
2076
+
2077
+ wp.atomic_add(loss, 0, inputs[tid].param2 * 2.0)
2078
+
2079
+
2080
+ def test_array_of_structs_grad(test, device):
2081
+ num_items = 10
2082
+
2083
+ l = []
2084
+ for i in range(num_items):
2085
+ g = GradStruct()
2086
+ g.param2 = float(i)
2087
+
2088
+ l.append(g)
2089
+
2090
+ a = wp.array(l, dtype=GradStruct, device=device, requires_grad=True)
2091
+ loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
2092
+
2093
+ with wp.Tape() as tape:
2094
+ wp.launch(test_array_of_structs_grad_kernel, dim=num_items, inputs=[a, loss], device=device)
2095
+
2096
+ tape.backward(loss)
2097
+
2098
+ grads = a.grad.numpy()
2099
+ assert_np_equal(grads["param2"], np.full(num_items, 2.0, dtype=np.float32))
2100
+
2101
+
2102
+ @wp.struct
2103
+ class NumpyStruct:
2104
+ x: int
2105
+ v: wp.vec3
2106
+
2107
+
2108
+ def test_array_of_structs_from_numpy(test, device):
2109
+ num_items = 10
2110
+
2111
+ na = np.zeros(num_items, dtype=NumpyStruct.numpy_dtype())
2112
+ na["x"] = 17
2113
+ na["v"] = (1, 2, 3)
2114
+
2115
+ a = wp.array(data=na, dtype=NumpyStruct, device=device)
2116
+
2117
+ assert_np_equal(a.numpy(), na)
2118
+
2119
+
2120
+ def test_array_of_structs_roundtrip(test, device):
2121
+ num_items = 10
2122
+
2123
+ value = NumpyStruct()
2124
+ value.x = 17
2125
+ value.v = wp.vec3(1.0, 2.0, 3.0)
2126
+
2127
+ # create Warp structured array
2128
+ a = wp.full(num_items, value, device=device)
2129
+
2130
+ # convert to NumPy structured array
2131
+ na = a.numpy()
2132
+
2133
+ expected = np.zeros(num_items, dtype=NumpyStruct.numpy_dtype())
2134
+ expected["x"] = value.x
2135
+ expected["v"] = value.v
2136
+
2137
+ assert_np_equal(na, expected)
2138
+
2139
+ # modify a field
2140
+ na["x"] = 42
2141
+
2142
+ # convert back to Warp array
2143
+ a = wp.from_numpy(na, NumpyStruct, device=device)
2144
+
2145
+ expected["x"] = 42
2146
+
2147
+ assert_np_equal(a.numpy(), expected)
2148
+
2149
+
2150
+ def test_array_from_numpy(test, device):
2151
+ arr = np.array((1.0, 2.0, 3.0), dtype=float)
2152
+
2153
+ result = wp.from_numpy(arr, device=device)
2154
+ expected = wp.array((1.0, 2.0, 3.0), dtype=wp.float32, shape=(3,))
2155
+ assert_np_equal(result.numpy(), expected.numpy())
2156
+
2157
+ result = wp.from_numpy(arr, dtype=wp.vec3, device=device)
2158
+ expected = wp.array(((1.0, 2.0, 3.0),), dtype=wp.vec3, shape=(1,))
2159
+ assert_np_equal(result.numpy(), expected.numpy())
2160
+
2161
+ # --------------------------------------------------------------------------
2162
+
2163
+ arr = np.array(((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), dtype=float)
2164
+
2165
+ result = wp.from_numpy(arr, device=device)
2166
+ expected = wp.array(((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), dtype=wp.vec3, shape=(2,))
2167
+ assert_np_equal(result.numpy(), expected.numpy())
2168
+
2169
+ result = wp.from_numpy(arr, dtype=wp.float32, device=device)
2170
+ expected = wp.array(((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)), dtype=wp.float32, shape=(2, 3))
2171
+ assert_np_equal(result.numpy(), expected.numpy())
2172
+
2173
+ result = wp.from_numpy(arr, dtype=wp.float32, shape=(6,), device=device)
2174
+ expected = wp.array((1.0, 2.0, 3.0, 4.0, 5.0, 6.0), dtype=wp.float32, shape=(6,))
2175
+ assert_np_equal(result.numpy(), expected.numpy())
2176
+
2177
+ # --------------------------------------------------------------------------
2178
+
2179
+ arr = np.array(
2180
+ (
2181
+ (
2182
+ (1.0, 2.0, 3.0, 4.0),
2183
+ (2.0, 3.0, 4.0, 5.0),
2184
+ (3.0, 4.0, 5.0, 6.0),
2185
+ (4.0, 5.0, 6.0, 7.0),
2186
+ ),
2187
+ (
2188
+ (2.0, 3.0, 4.0, 5.0),
2189
+ (3.0, 4.0, 5.0, 6.0),
2190
+ (4.0, 5.0, 6.0, 7.0),
2191
+ (5.0, 6.0, 7.0, 8.0),
2192
+ ),
2193
+ ),
2194
+ dtype=float,
2195
+ )
2196
+
2197
+ result = wp.from_numpy(arr, device=device)
2198
+ expected = wp.array(
2199
+ (
2200
+ (
2201
+ (1.0, 2.0, 3.0, 4.0),
2202
+ (2.0, 3.0, 4.0, 5.0),
2203
+ (3.0, 4.0, 5.0, 6.0),
2204
+ (4.0, 5.0, 6.0, 7.0),
2205
+ ),
2206
+ (
2207
+ (2.0, 3.0, 4.0, 5.0),
2208
+ (3.0, 4.0, 5.0, 6.0),
2209
+ (4.0, 5.0, 6.0, 7.0),
2210
+ (5.0, 6.0, 7.0, 8.0),
2211
+ ),
2212
+ ),
2213
+ dtype=wp.mat44,
2214
+ shape=(2,),
2215
+ )
2216
+ assert_np_equal(result.numpy(), expected.numpy())
2217
+
2218
+ result = wp.from_numpy(arr, dtype=wp.float32, device=device)
2219
+ expected = wp.array(
2220
+ (
2221
+ (
2222
+ (1.0, 2.0, 3.0, 4.0),
2223
+ (2.0, 3.0, 4.0, 5.0),
2224
+ (3.0, 4.0, 5.0, 6.0),
2225
+ (4.0, 5.0, 6.0, 7.0),
2226
+ ),
2227
+ (
2228
+ (2.0, 3.0, 4.0, 5.0),
2229
+ (3.0, 4.0, 5.0, 6.0),
2230
+ (4.0, 5.0, 6.0, 7.0),
2231
+ (5.0, 6.0, 7.0, 8.0),
2232
+ ),
2233
+ ),
2234
+ dtype=wp.float32,
2235
+ shape=(2, 4, 4),
2236
+ )
2237
+ assert_np_equal(result.numpy(), expected.numpy())
2238
+
2239
+ result = wp.from_numpy(arr, dtype=wp.vec4, device=device).reshape((8,)) # Reshape from (2, 4)
2240
+ expected = wp.array(
2241
+ (
2242
+ (1.0, 2.0, 3.0, 4.0),
2243
+ (2.0, 3.0, 4.0, 5.0),
2244
+ (3.0, 4.0, 5.0, 6.0),
2245
+ (4.0, 5.0, 6.0, 7.0),
2246
+ (2.0, 3.0, 4.0, 5.0),
2247
+ (3.0, 4.0, 5.0, 6.0),
2248
+ (4.0, 5.0, 6.0, 7.0),
2249
+ (5.0, 6.0, 7.0, 8.0),
2250
+ ),
2251
+ dtype=wp.vec4,
2252
+ shape=(8,),
2253
+ )
2254
+ assert_np_equal(result.numpy(), expected.numpy())
2255
+
2256
+ result = wp.from_numpy(arr, dtype=wp.float32, shape=(32,), device=device)
2257
+ expected = wp.array(
2258
+ (
2259
+ 1.0,
2260
+ 2.0,
2261
+ 3.0,
2262
+ 4.0,
2263
+ 2.0,
2264
+ 3.0,
2265
+ 4.0,
2266
+ 5.0,
2267
+ 3.0,
2268
+ 4.0,
2269
+ 5.0,
2270
+ 6.0,
2271
+ 4.0,
2272
+ 5.0,
2273
+ 6.0,
2274
+ 7.0,
2275
+ 2.0,
2276
+ 3.0,
2277
+ 4.0,
2278
+ 5.0,
2279
+ 3.0,
2280
+ 4.0,
2281
+ 5.0,
2282
+ 6.0,
2283
+ 4.0,
2284
+ 5.0,
2285
+ 6.0,
2286
+ 7.0,
2287
+ 5.0,
2288
+ 6.0,
2289
+ 7.0,
2290
+ 8.0,
2291
+ ),
2292
+ dtype=wp.float32,
2293
+ shape=(32,),
2294
+ )
2295
+ assert_np_equal(result.numpy(), expected.numpy())
2296
+
2297
+
2298
+ def test_array_from_cai(test, device):
2299
+ import torch
2300
+
2301
+ @wp.kernel
2302
+ def first_row_plus_one(x: wp.array2d(dtype=float)):
2303
+ i, j = wp.tid()
2304
+ if i == 0:
2305
+ x[i, j] += 1.0
2306
+
2307
+ # start with torch tensor
2308
+ arr = torch.zeros((3, 3))
2309
+ torch_device = wp.device_to_torch(device)
2310
+ arr_torch = arr.to(torch_device)
2311
+
2312
+ # wrap as warp array via __cuda_array_interface__
2313
+ arr_warp = wp.array(arr_torch, device=device)
2314
+
2315
+ wp.launch(kernel=first_row_plus_one, dim=(3, 3), inputs=[arr_warp], device=device)
2316
+
2317
+ # re-wrap as torch array
2318
+ arr_torch = wp.to_torch(arr_warp)
2319
+
2320
+ # transpose
2321
+ arr_torch = torch.as_strided(arr_torch, size=(3, 3), stride=(arr_torch.stride(1), arr_torch.stride(0)))
2322
+
2323
+ # re-wrap as warp array with new strides
2324
+ arr_warp = wp.array(arr_torch, device=device)
2325
+
2326
+ wp.launch(kernel=first_row_plus_one, dim=(3, 3), inputs=[arr_warp], device=device)
2327
+
2328
+ assert_np_equal(arr_warp.numpy(), np.array([[2, 1, 1], [1, 0, 0], [1, 0, 0]]))
2329
+
2330
+
2331
+ devices = get_test_devices()
2332
+
2333
+
2334
+ class TestArray(unittest.TestCase):
2335
+ def test_array_new_del(self):
2336
+ # test the scenario in which an array instance is created but not initialized before gc
2337
+ instance = wp.array.__new__(wp.array)
2338
+ instance.__del__()
2339
+
2340
+
2341
+ add_function_test(TestArray, "test_shape", test_shape, devices=devices)
2342
+ add_function_test(TestArray, "test_negative_shape", test_negative_shape, devices=devices)
2343
+ add_function_test(TestArray, "test_flatten", test_flatten, devices=devices)
2344
+ add_function_test(TestArray, "test_reshape", test_reshape, devices=devices)
2345
+ add_function_test(TestArray, "test_slicing", test_slicing, devices=devices)
2346
+ add_function_test(TestArray, "test_transpose", test_transpose, devices=devices)
2347
+ add_function_test(TestArray, "test_view", test_view, devices=devices)
2348
+ add_function_test(TestArray, "test_clone_adjoint", test_clone_adjoint, devices=devices)
2349
+ add_function_test(TestArray, "test_assign_adjoint", test_assign_adjoint, devices=devices)
2350
+
2351
+ add_function_test(TestArray, "test_1d_array", test_1d, devices=devices)
2352
+ add_function_test(TestArray, "test_2d_array", test_2d, devices=devices)
2353
+ add_function_test(TestArray, "test_3d_array", test_3d, devices=devices)
2354
+ add_function_test(TestArray, "test_4d_array", test_4d, devices=devices)
2355
+ add_function_test(TestArray, "test_4d_array_transposed", test_4d_transposed, devices=devices)
2356
+
2357
+ add_function_test(TestArray, "test_fill_scalar", test_fill_scalar, devices=devices)
2358
+ add_function_test(TestArray, "test_fill_vector", test_fill_vector, devices=devices)
2359
+ add_function_test(TestArray, "test_fill_matrix", test_fill_matrix, devices=devices)
2360
+ add_function_test(TestArray, "test_fill_struct", test_fill_struct, devices=devices)
2361
+ add_function_test(TestArray, "test_fill_slices", test_fill_slices, devices=devices)
2362
+ add_function_test(TestArray, "test_full_scalar", test_full_scalar, devices=devices)
2363
+ add_function_test(TestArray, "test_full_vector", test_full_vector, devices=devices)
2364
+ add_function_test(TestArray, "test_full_matrix", test_full_matrix, devices=devices)
2365
+ add_function_test(TestArray, "test_full_struct", test_full_struct, devices=devices)
2366
+ add_function_test(TestArray, "test_ones_scalar", test_ones_scalar, devices=devices)
2367
+ add_function_test(TestArray, "test_ones_vector", test_ones_vector, devices=devices)
2368
+ add_function_test(TestArray, "test_ones_matrix", test_ones_matrix, devices=devices)
2369
+ add_function_test(TestArray, "test_ones_like_scalar", test_ones_like_scalar, devices=devices)
2370
+ add_function_test(TestArray, "test_ones_like_vector", test_ones_like_vector, devices=devices)
2371
+ add_function_test(TestArray, "test_ones_like_matrix", test_ones_like_matrix, devices=devices)
2372
+ add_function_test(TestArray, "test_empty_array", test_empty_array, devices=devices)
2373
+ add_function_test(TestArray, "test_empty_from_numpy", test_empty_from_numpy, devices=devices)
2374
+ add_function_test(TestArray, "test_empty_from_list", test_empty_from_list, devices=devices)
2375
+ add_function_test(TestArray, "test_to_list_scalar", test_to_list_scalar, devices=devices)
2376
+ add_function_test(TestArray, "test_to_list_vector", test_to_list_vector, devices=devices)
2377
+ add_function_test(TestArray, "test_to_list_matrix", test_to_list_matrix, devices=devices)
2378
+ add_function_test(TestArray, "test_to_list_struct", test_to_list_struct, devices=devices)
2379
+
2380
+ add_function_test(TestArray, "test_lower_bound", test_lower_bound, devices=devices)
2381
+ add_function_test(TestArray, "test_round_trip", test_round_trip, devices=devices)
2382
+ add_function_test(TestArray, "test_array_to_bool", test_array_to_bool, devices=devices)
2383
+ add_function_test(TestArray, "test_array_of_structs", test_array_of_structs, devices=devices)
2384
+ add_function_test(TestArray, "test_array_of_structs_grad", test_array_of_structs_grad, devices=devices)
2385
+ add_function_test(TestArray, "test_array_of_structs_from_numpy", test_array_of_structs_from_numpy, devices=devices)
2386
+ add_function_test(TestArray, "test_array_of_structs_roundtrip", test_array_of_structs_roundtrip, devices=devices)
2387
+ add_function_test(TestArray, "test_array_from_numpy", test_array_from_numpy, devices=devices)
2388
+
2389
+ try:
2390
+ import torch
2391
+
2392
+ # check which Warp devices work with Torch
2393
+ # CUDA devices may fail if Torch was not compiled with CUDA support
2394
+ torch_compatible_devices = []
2395
+ torch_compatible_cuda_devices = []
2396
+
2397
+ for d in devices:
2398
+ try:
2399
+ t = torch.arange(10, device=wp.device_to_torch(d))
2400
+ t += 1
2401
+ torch_compatible_devices.append(d)
2402
+ if d.is_cuda:
2403
+ torch_compatible_cuda_devices.append(d)
2404
+ except Exception as e:
2405
+ print(f"Skipping Array tests that use Torch on device '{d}' due to exception: {e}")
2406
+
2407
+ add_function_test(TestArray, "test_array_from_cai", test_array_from_cai, devices=torch_compatible_cuda_devices)
2408
+
2409
+ except Exception as e:
2410
+ print(f"Skipping Array tests that use Torch due to exception: {e}")
2411
+
2412
+
2413
+ if __name__ == "__main__":
2414
+ wp.build.clear_kernel_cache()
2415
+ unittest.main(verbosity=2)