warp-lang 1.0.2__py3-none-manylinux2014_x86_64.whl → 1.2.0__py3-none-manylinux2014_x86_64.whl

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

Potentially problematic release.


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

Files changed (356) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +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_matmul.py CHANGED
@@ -1,499 +1,498 @@
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 typing import Any
14
- from warp.tests.unittest_utils import *
15
-
16
- wp.init()
17
-
18
- from warp.context import runtime # noqa: E402
19
-
20
-
21
- class gemm_test_bed_runner:
22
- def __init__(self, dtype, device):
23
- self.dtype = dtype
24
- self.device = device
25
-
26
- def alloc(self, m, n, k, batch_count):
27
- rng = np.random.default_rng(42)
28
- low = -4.5
29
- high = 3.5
30
- if batch_count == 1:
31
- A = wp.array2d(
32
- np.ceil(rng.uniform(low=low, high=high, size=(m, k))),
33
- dtype=self.dtype,
34
- device=self.device,
35
- requires_grad=True,
36
- )
37
- B = wp.array2d(
38
- np.ceil(rng.uniform(low=low, high=high, size=(k, n))),
39
- dtype=self.dtype,
40
- device=self.device,
41
- requires_grad=True,
42
- )
43
- C = wp.array2d(
44
- np.ceil(rng.uniform(low=low, high=high, size=(m, n))),
45
- dtype=self.dtype,
46
- device=self.device,
47
- requires_grad=True,
48
- )
49
- D = wp.array2d(np.zeros((m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
50
- else:
51
- A = wp.array3d(
52
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, k))),
53
- dtype=self.dtype,
54
- device=self.device,
55
- requires_grad=True,
56
- )
57
- B = wp.array3d(
58
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, k, n))),
59
- dtype=self.dtype,
60
- device=self.device,
61
- requires_grad=True,
62
- )
63
- C = wp.array3d(
64
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, n))),
65
- dtype=self.dtype,
66
- device=self.device,
67
- requires_grad=True,
68
- )
69
- D = wp.array3d(np.zeros((batch_count, m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
70
- return A, B, C, D
71
-
72
- def run_and_verify(self, m, n, k, batch_count, alpha, beta):
73
- A, B, C, D = self.alloc(m, n, k, batch_count)
74
- ones = wp.zeros_like(D)
75
- ones.fill_(1.0)
76
-
77
- if batch_count == 1:
78
- tape = wp.Tape()
79
- with tape:
80
- wp.matmul(A, B, C, D, alpha, beta, False)
81
- tape.backward(grads={D: ones})
82
-
83
- D_np = alpha * (A.numpy() @ B.numpy()) + beta * C.numpy()
84
- assert np.array_equal(D_np, D.numpy())
85
-
86
- adj_A_np = alpha * np.matmul(ones.numpy(), B.numpy().transpose())
87
- adj_B_np = alpha * (A.numpy().transpose() @ ones.numpy())
88
- adj_C_np = beta * ones.numpy()
89
-
90
- else:
91
- tape = wp.Tape()
92
- with tape:
93
- wp.batched_matmul(A, B, C, D, alpha, beta, False)
94
- tape.backward(grads={D: ones})
95
-
96
- D_np = alpha * np.matmul(A.numpy(), B.numpy()) + beta * C.numpy()
97
- assert np.array_equal(D_np, D.numpy())
98
-
99
- adj_A_np = alpha * np.matmul(ones.numpy(), B.numpy().transpose((0, 2, 1)))
100
- adj_B_np = alpha * np.matmul(A.numpy().transpose((0, 2, 1)), ones.numpy())
101
- adj_C_np = beta * ones.numpy()
102
-
103
- assert np.array_equal(adj_A_np, A.grad.numpy())
104
- assert np.array_equal(adj_B_np, B.grad.numpy())
105
- assert np.array_equal(adj_C_np, C.grad.numpy())
106
-
107
- def run(self):
108
- Ms = [64, 128, 512]
109
- Ns = [64, 128, 512]
110
- Ks = [64, 128, 512]
111
- batch_counts = [1, 4]
112
- betas = [0.0, 1.0]
113
- alpha = 1.0
114
-
115
- for batch_count in batch_counts:
116
- for m in Ms:
117
- for n in Ns:
118
- for k in Ks:
119
- for beta in betas:
120
- self.run_and_verify(m, n, k, batch_count, alpha, beta)
121
-
122
-
123
- class gemm_test_bed_runner_transpose:
124
- def __init__(self, dtype, device):
125
- self.dtype = dtype
126
- self.device = device
127
-
128
- def alloc(self, m, n, k, batch_count):
129
- rng = np.random.default_rng(42)
130
- low = -4.5
131
- high = 3.5
132
- if batch_count == 1:
133
- A = wp.array2d(
134
- np.ceil(rng.uniform(low=low, high=high, size=(m, k))),
135
- dtype=self.dtype,
136
- device=self.device,
137
- requires_grad=True,
138
- )
139
- B = wp.array2d(
140
- np.ceil(rng.uniform(low=low, high=high, size=(k, n))),
141
- dtype=self.dtype,
142
- device=self.device,
143
- requires_grad=True,
144
- )
145
- C = wp.array2d(
146
- np.ceil(rng.uniform(low=low, high=high, size=(m, n))),
147
- dtype=self.dtype,
148
- device=self.device,
149
- requires_grad=True,
150
- )
151
- D = wp.array2d(np.zeros((m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
152
- AT = wp.array2d(A.numpy().transpose([1, 0]), dtype=self.dtype, device=self.device, requires_grad=True)
153
- BT = wp.array2d(B.numpy().transpose([1, 0]), dtype=self.dtype, device=self.device, requires_grad=True)
154
- else:
155
- A = wp.array3d(
156
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, k))),
157
- dtype=self.dtype,
158
- device=self.device,
159
- requires_grad=True,
160
- )
161
- B = wp.array3d(
162
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, k, n))),
163
- dtype=self.dtype,
164
- device=self.device,
165
- requires_grad=True,
166
- )
167
- C = wp.array3d(
168
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, n))),
169
- dtype=self.dtype,
170
- device=self.device,
171
- requires_grad=True,
172
- )
173
- D = wp.array3d(np.zeros((batch_count, m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
174
- AT = wp.array3d(A.numpy().transpose([0, 2, 1]), dtype=self.dtype, device=self.device, requires_grad=True)
175
- BT = wp.array3d(B.numpy().transpose([0, 2, 1]), dtype=self.dtype, device=self.device, requires_grad=True)
176
- return A, B, C, D, AT, BT
177
-
178
- def run_and_verify(self, m, n, k, batch_count, alpha, beta):
179
- A, B, C1, D1, AT1, BT1 = self.alloc(m, n, k, batch_count)
180
- C2 = wp.clone(C1)
181
- C3 = wp.clone(C1)
182
- D2 = wp.clone(D1)
183
- D3 = wp.clone(D1)
184
- AT2 = wp.clone(AT1)
185
- BT2 = wp.clone(BT1)
186
- ones1 = wp.zeros_like(D1)
187
- ones1.fill_(1.0)
188
- ones2 = wp.zeros_like(D2)
189
- ones2.fill_(1.0)
190
- ones3 = wp.zeros_like(D3)
191
- ones3.fill_(1.0)
192
-
193
- if batch_count == 1:
194
- ATT1 = AT1.transpose([1, 0])
195
- BTT1 = BT1.transpose([1, 0])
196
- ATT2 = AT2.transpose([1, 0])
197
- BTT2 = BT2.transpose([1, 0])
198
- tape = wp.Tape()
199
- with tape:
200
- wp.matmul(A, BTT1, C1, D1, alpha, beta, False)
201
- wp.matmul(ATT1, B, C2, D2, alpha, beta, False)
202
- wp.matmul(ATT2, BTT2, C3, D3, alpha, beta, False)
203
- tape.backward(grads={D1: ones1, D2: ones2, D3: ones3})
204
-
205
- D_np = alpha * (A.numpy() @ B.numpy()) + beta * C1.numpy()
206
- assert np.array_equal(D_np, D1.numpy())
207
- assert np.array_equal(D_np, D2.numpy())
208
- assert np.array_equal(D_np, D3.numpy())
209
-
210
- adj_A_np = alpha * (ones1.numpy() @ B.numpy().transpose())
211
- adj_B_np = alpha * (A.numpy().transpose() @ ones1.numpy())
212
- adj_C_np = beta * ones1.numpy()
213
-
214
- else:
215
- ATT1 = AT1.transpose([0, 2, 1])
216
- BTT1 = BT1.transpose([0, 2, 1])
217
- ATT2 = AT2.transpose([0, 2, 1])
218
- BTT2 = BT2.transpose([0, 2, 1])
219
- tape = wp.Tape()
220
- with tape:
221
- wp.batched_matmul(A, BTT1, C1, D1, alpha, beta, False)
222
- wp.batched_matmul(ATT1, B, C2, D2, alpha, beta, False)
223
- wp.batched_matmul(ATT2, BTT2, C3, D3, alpha, beta, False)
224
- tape.backward(grads={D1: ones1, D2: ones2, D3: ones3})
225
-
226
- D_np = alpha * np.matmul(A.numpy(), B.numpy()) + beta * C1.numpy()
227
- assert np.array_equal(D_np, D1.numpy())
228
- assert np.array_equal(D_np, D2.numpy())
229
- assert np.array_equal(D_np, D3.numpy())
230
-
231
- adj_A_np = alpha * np.matmul(ones1.numpy(), B.numpy().transpose((0, 2, 1)))
232
- adj_B_np = alpha * np.matmul(A.numpy().transpose((0, 2, 1)), ones1.numpy())
233
- adj_C_np = beta * ones1.numpy()
234
-
235
- assert np.array_equal(adj_A_np, A.grad.numpy())
236
- assert np.array_equal(adj_A_np, ATT1.grad.numpy())
237
- assert np.array_equal(adj_A_np, ATT2.grad.numpy())
238
- assert np.array_equal(adj_B_np, B.grad.numpy())
239
- assert np.array_equal(adj_B_np, BTT1.grad.numpy())
240
- assert np.array_equal(adj_B_np, BTT2.grad.numpy())
241
- assert np.array_equal(adj_C_np, C1.grad.numpy())
242
- assert np.array_equal(adj_C_np, C2.grad.numpy())
243
- assert np.array_equal(adj_C_np, C3.grad.numpy())
244
-
245
- def run(self):
246
- m = 16
247
- n = 32
248
- k = 64
249
- batch_counts = [1, 4]
250
- beta = 1.0
251
- alpha = 1.0
252
-
253
- for batch_count in batch_counts:
254
- self.run_and_verify(m, n, k, batch_count, alpha, beta)
255
-
256
-
257
- # NOTE: F16 tests are slow due to the performance of the reference numpy F16 matmuls performed on CPU.
258
- def test_f16(test, device):
259
- gemm_test_bed_runner(wp.float16, device).run()
260
- gemm_test_bed_runner_transpose(wp.float16, device).run()
261
-
262
-
263
- @unittest.skipUnless(runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
264
- def test_f32(test, device):
265
- gemm_test_bed_runner(wp.float32, device).run()
266
- gemm_test_bed_runner_transpose(wp.float32, device).run()
267
-
268
-
269
- @unittest.skipUnless(runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
270
- def test_f64(test, device):
271
- gemm_test_bed_runner(wp.float64, device).run()
272
- gemm_test_bed_runner_transpose(wp.float64, device).run()
273
-
274
-
275
- @wp.kernel
276
- def matrix_sum_kernel(arr: wp.array2d(dtype=float), loss: wp.array(dtype=float)):
277
- i, j = wp.tid()
278
- wp.atomic_add(loss, 0, arr[i, j])
279
-
280
-
281
- @unittest.skipUnless(runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
282
- def test_tape(test, device):
283
- rng = np.random.default_rng(42)
284
- low = -4.5
285
- high = 3.5
286
- m = 64
287
- n = 128
288
- k = 256
289
- A = wp.array2d(
290
- np.ceil(rng.uniform(low=low, high=high, size=(m, k))), dtype=float, device=device, requires_grad=True
291
- )
292
- B = wp.array2d(
293
- np.ceil(rng.uniform(low=low, high=high, size=(k, n))), dtype=float, device=device, requires_grad=True
294
- )
295
- C = wp.array2d(
296
- np.ceil(rng.uniform(low=low, high=high, size=(m, n))), dtype=float, device=device, requires_grad=True
297
- )
298
- D = wp.array2d(np.zeros((m, n)), dtype=float, device=device, requires_grad=True)
299
- loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
300
-
301
- # test tape
302
- tape = wp.Tape()
303
- with tape:
304
- wp.matmul(A, B, C, D)
305
- wp.launch(matrix_sum_kernel, dim=(m, n), inputs=[D, loss], device=device)
306
-
307
- tape.backward(loss=loss)
308
- A_grad = A.grad.numpy()
309
- tape.reset()
310
-
311
- # test adjoint
312
- D.grad = wp.ones((m, n), dtype=float, device=device)
313
- wp.adj_matmul(A, B, C, A.grad, B.grad, C.grad, D.grad)
314
- assert_np_equal(A_grad, A.grad.numpy())
315
-
316
- # test zero
317
- tape.zero()
318
- assert_array_equal(A.grad, wp.zeros_like(A))
319
-
320
-
321
- @unittest.skipUnless(runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
322
- def test_operator(test, device):
323
- rng = np.random.default_rng(42)
324
- low = -4.5
325
- high = 3.5
326
- m = 64
327
- n = 128
328
- k = 256
329
- A = wp.array2d(
330
- np.ceil(rng.uniform(low=low, high=high, size=(m, k))), dtype=float, device=device, requires_grad=True
331
- )
332
- B = wp.array2d(
333
- np.ceil(rng.uniform(low=low, high=high, size=(k, n))), dtype=float, device=device, requires_grad=True
334
- )
335
- loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
336
-
337
- # test tape
338
- tape = wp.Tape()
339
- with tape:
340
- D = A @ B
341
- wp.launch(matrix_sum_kernel, dim=(m, n), inputs=[D, loss], device=device)
342
-
343
- tape.backward(loss=loss)
344
-
345
- # test adjoint
346
- D.grad = wp.ones((m, n), dtype=float, device=device)
347
- B_transpose = wp.array2d(B.transpose().numpy(), dtype=float, device=device)
348
-
349
- adj_A = D.grad @ B_transpose
350
- assert_array_equal(adj_A, A.grad)
351
-
352
- # test zero
353
- tape.zero()
354
- assert_array_equal(A.grad, wp.zeros_like(A))
355
-
356
-
357
- @unittest.skipUnless(runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
358
- def test_large_batch_count(test, device):
359
- rng = np.random.default_rng(42)
360
- low = -4.5
361
- high = 3.5
362
- m = 2
363
- n = 3
364
- k = 4
365
- batch_count = 65535 * 2 + int(65535 / 2)
366
- A = wp.array3d(
367
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, k))),
368
- dtype=float,
369
- device=device,
370
- requires_grad=True,
371
- )
372
- B = wp.array3d(
373
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, k, n))),
374
- dtype=float,
375
- device=device,
376
- requires_grad=True,
377
- )
378
- C = wp.array3d(
379
- np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, n))),
380
- dtype=float,
381
- device=device,
382
- requires_grad=True,
383
- )
384
- D = wp.array3d(np.zeros((batch_count, m, n)), dtype=float, device=device, requires_grad=True)
385
- ones = wp.zeros_like(D)
386
- ones.fill_(1.0)
387
-
388
- alpha = 1.0
389
- beta = 1.0
390
-
391
- tape = wp.Tape()
392
- with tape:
393
- wp.batched_matmul(A, B, C, D, alpha=alpha, beta=beta, allow_tf32x3_arith=False)
394
- tape.backward(grads={D: ones})
395
-
396
- D_np = alpha * np.matmul(A.numpy(), B.numpy()) + beta * C.numpy()
397
- assert np.array_equal(D_np, D.numpy())
398
-
399
- adj_A_np = alpha * np.matmul(ones.numpy(), B.numpy().transpose((0, 2, 1)))
400
- adj_B_np = alpha * np.matmul(A.numpy().transpose((0, 2, 1)), ones.numpy())
401
- adj_C_np = beta * ones.numpy()
402
-
403
- assert np.array_equal(adj_A_np, A.grad.numpy())
404
- assert np.array_equal(adj_B_np, B.grad.numpy())
405
- assert np.array_equal(adj_C_np, C.grad.numpy())
406
-
407
-
408
- @unittest.skipUnless(runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
409
- def test_adjoint_accumulation(test, device):
410
- a_np = np.ones(shape=(2, 3))
411
- b_np = np.ones(shape=(3, 2))
412
- c_np = np.zeros(shape=(2, 2))
413
- d_np = np.zeros(shape=(2, 2))
414
-
415
- a_wp = wp.from_numpy(a_np, dtype=float, requires_grad=True, device=device)
416
- b_wp = wp.from_numpy(b_np, dtype=float, requires_grad=True, device=device)
417
- c_wp = wp.from_numpy(c_np, dtype=float, requires_grad=True, device=device)
418
- d1_wp = wp.from_numpy(d_np, dtype=float, requires_grad=True, device=device)
419
- d2_wp = wp.from_numpy(d_np, dtype=float, requires_grad=True, device=device)
420
-
421
- tape = wp.Tape()
422
-
423
- with tape:
424
- wp.matmul(a_wp, b_wp, c_wp, d1_wp, alpha=1.0, beta=1.0)
425
- wp.matmul(a_wp, b_wp, d1_wp, d2_wp, alpha=1.0, beta=1.0)
426
-
427
- d_grad = wp.zeros_like(d2_wp, device=device)
428
- d_grad.fill_(1.0)
429
- grads = {d2_wp: d_grad}
430
- tape.backward(grads=grads)
431
-
432
- assert np.array_equal(a_wp.grad.numpy(), 4.0 * np.ones(shape=(2, 3)))
433
- assert np.array_equal(b_wp.grad.numpy(), 4.0 * np.ones(shape=(3, 2)))
434
- assert np.array_equal(c_wp.grad.numpy(), np.ones(shape=(2, 2)))
435
-
436
-
437
- @unittest.skipUnless(runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
438
- def test_cuda_graph_capture(test, device):
439
- @wp.kernel
440
- def mat_sum(mat: wp.array2d(dtype=Any), loss: wp.array(dtype=Any)):
441
- i, j = wp.tid()
442
- e = mat[i,j]
443
- wp.atomic_add(loss, 0, e)
444
-
445
- for T in [wp.float16, wp.float32, wp.float64]:
446
- wp.overload(mat_sum, [wp.array2d(dtype=T), wp.array(dtype=T)])
447
-
448
- wp.load_module(device=device)
449
- wp.load_module(module="warp.utils", device=device)
450
-
451
- for T in [wp.float16, wp.float32, wp.float64]:
452
- m = 8
453
- n = 8
454
- k = 8
455
-
456
- A = wp.ones((m, n), dtype=T, device=device, requires_grad=True)
457
- B = wp.ones((n, k), dtype=T, device=device, requires_grad=True)
458
- C = wp.zeros((m, k), dtype=T, device=device, requires_grad=True)
459
- D = wp.zeros((m, k), dtype=T, device=device, requires_grad=True)
460
-
461
- loss = wp.zeros(1, dtype=T, device=device, requires_grad=True)
462
-
463
- wp.capture_begin(device, force_module_load=False)
464
- try:
465
- tape = wp.Tape()
466
-
467
- with tape:
468
- wp.matmul(A, B, C, D)
469
- wp.launch(mat_sum, dim=(m, k), inputs=[D, loss], device=device)
470
-
471
- tape.backward(loss=loss)
472
- finally:
473
- graph = wp.capture_end(device)
474
-
475
- wp.capture_launch(graph)
476
-
477
- assert_np_equal(A.grad.numpy(), 8.0 * np.ones((m, n), dtype=T))
478
-
479
-
480
- devices = get_test_devices()
481
- cuda_devices = get_unique_cuda_test_devices()
482
-
483
- class TestMatmul(unittest.TestCase):
484
- pass
485
-
486
-
487
- # add_function_test(TestMatmul, "test_f16", test_f16, devices=devices)
488
- add_function_test(TestMatmul, "test_f32", test_f32, devices=devices)
489
- add_function_test(TestMatmul, "test_f64", test_f64, devices=devices)
490
- add_function_test(TestMatmul, "test_tape", test_tape, devices=devices)
491
- add_function_test(TestMatmul, "test_operator", test_operator, devices=devices)
492
- add_function_test(TestMatmul, "test_large_batch_count", test_large_batch_count, devices=devices)
493
- add_function_test(TestMatmul, "test_adjoint_accumulation", test_adjoint_accumulation, devices=devices)
494
- add_function_test(TestMatmul, "test_cuda_graph_capture", test_cuda_graph_capture, devices=cuda_devices)
495
-
496
-
497
- if __name__ == "__main__":
498
- wp.build.clear_kernel_cache()
499
- unittest.main(verbosity=2, failfast=False)
1
+ # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ import unittest
9
+ from typing import Any
10
+
11
+ import numpy as np
12
+
13
+ import warp as wp
14
+ from warp.tests.unittest_utils import *
15
+
16
+ wp.init() # For wp.context.runtime.core.is_cutlass_enabled()
17
+
18
+
19
+ class gemm_test_bed_runner:
20
+ def __init__(self, dtype, device):
21
+ self.dtype = dtype
22
+ self.device = device
23
+
24
+ def alloc(self, m, n, k, batch_count):
25
+ rng = np.random.default_rng(42)
26
+ low = -4.5
27
+ high = 3.5
28
+ if batch_count == 1:
29
+ A = wp.array2d(
30
+ np.ceil(rng.uniform(low=low, high=high, size=(m, k))),
31
+ dtype=self.dtype,
32
+ device=self.device,
33
+ requires_grad=True,
34
+ )
35
+ B = wp.array2d(
36
+ np.ceil(rng.uniform(low=low, high=high, size=(k, n))),
37
+ dtype=self.dtype,
38
+ device=self.device,
39
+ requires_grad=True,
40
+ )
41
+ C = wp.array2d(
42
+ np.ceil(rng.uniform(low=low, high=high, size=(m, n))),
43
+ dtype=self.dtype,
44
+ device=self.device,
45
+ requires_grad=True,
46
+ )
47
+ D = wp.array2d(np.zeros((m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
48
+ else:
49
+ A = wp.array3d(
50
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, k))),
51
+ dtype=self.dtype,
52
+ device=self.device,
53
+ requires_grad=True,
54
+ )
55
+ B = wp.array3d(
56
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, k, n))),
57
+ dtype=self.dtype,
58
+ device=self.device,
59
+ requires_grad=True,
60
+ )
61
+ C = wp.array3d(
62
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, n))),
63
+ dtype=self.dtype,
64
+ device=self.device,
65
+ requires_grad=True,
66
+ )
67
+ D = wp.array3d(np.zeros((batch_count, m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
68
+ return A, B, C, D
69
+
70
+ def run_and_verify(self, m, n, k, batch_count, alpha, beta):
71
+ A, B, C, D = self.alloc(m, n, k, batch_count)
72
+ ones = wp.zeros_like(D)
73
+ ones.fill_(1.0)
74
+
75
+ if batch_count == 1:
76
+ tape = wp.Tape()
77
+ with tape:
78
+ wp.matmul(A, B, C, D, alpha, beta, False)
79
+ tape.backward(grads={D: ones})
80
+
81
+ D_np = alpha * (A.numpy() @ B.numpy()) + beta * C.numpy()
82
+ assert_np_equal(D.numpy(), D_np)
83
+
84
+ adj_A_np = alpha * np.matmul(ones.numpy(), B.numpy().transpose())
85
+ adj_B_np = alpha * (A.numpy().transpose() @ ones.numpy())
86
+ adj_C_np = beta * ones.numpy()
87
+
88
+ else:
89
+ tape = wp.Tape()
90
+ with tape:
91
+ wp.batched_matmul(A, B, C, D, alpha, beta, False)
92
+ tape.backward(grads={D: ones})
93
+
94
+ D_np = alpha * np.matmul(A.numpy(), B.numpy()) + beta * C.numpy()
95
+ assert_np_equal(D.numpy(), D_np)
96
+
97
+ adj_A_np = alpha * np.matmul(ones.numpy(), B.numpy().transpose((0, 2, 1)))
98
+ adj_B_np = alpha * np.matmul(A.numpy().transpose((0, 2, 1)), ones.numpy())
99
+ adj_C_np = beta * ones.numpy()
100
+
101
+ assert_np_equal(A.grad.numpy(), adj_A_np)
102
+ assert_np_equal(B.grad.numpy(), adj_B_np)
103
+ assert_np_equal(C.grad.numpy(), adj_C_np)
104
+
105
+ def run(self):
106
+ Ms = [64, 128, 512]
107
+ Ns = [64, 128, 512]
108
+ Ks = [64, 128, 512]
109
+ batch_counts = [1, 4]
110
+ betas = [0.0, 1.0]
111
+ alpha = 1.0
112
+
113
+ for batch_count in batch_counts:
114
+ for m in Ms:
115
+ for n in Ns:
116
+ for k in Ks:
117
+ for beta in betas:
118
+ self.run_and_verify(m, n, k, batch_count, alpha, beta)
119
+
120
+
121
+ class gemm_test_bed_runner_transpose:
122
+ def __init__(self, dtype, device):
123
+ self.dtype = dtype
124
+ self.device = device
125
+
126
+ def alloc(self, m, n, k, batch_count):
127
+ rng = np.random.default_rng(42)
128
+ low = -4.5
129
+ high = 3.5
130
+ if batch_count == 1:
131
+ A = wp.array2d(
132
+ np.ceil(rng.uniform(low=low, high=high, size=(m, k))),
133
+ dtype=self.dtype,
134
+ device=self.device,
135
+ requires_grad=True,
136
+ )
137
+ B = wp.array2d(
138
+ np.ceil(rng.uniform(low=low, high=high, size=(k, n))),
139
+ dtype=self.dtype,
140
+ device=self.device,
141
+ requires_grad=True,
142
+ )
143
+ C = wp.array2d(
144
+ np.ceil(rng.uniform(low=low, high=high, size=(m, n))),
145
+ dtype=self.dtype,
146
+ device=self.device,
147
+ requires_grad=True,
148
+ )
149
+ D = wp.array2d(np.zeros((m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
150
+ AT = wp.array2d(A.numpy().transpose([1, 0]), dtype=self.dtype, device=self.device, requires_grad=True)
151
+ BT = wp.array2d(B.numpy().transpose([1, 0]), dtype=self.dtype, device=self.device, requires_grad=True)
152
+ else:
153
+ A = wp.array3d(
154
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, k))),
155
+ dtype=self.dtype,
156
+ device=self.device,
157
+ requires_grad=True,
158
+ )
159
+ B = wp.array3d(
160
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, k, n))),
161
+ dtype=self.dtype,
162
+ device=self.device,
163
+ requires_grad=True,
164
+ )
165
+ C = wp.array3d(
166
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, n))),
167
+ dtype=self.dtype,
168
+ device=self.device,
169
+ requires_grad=True,
170
+ )
171
+ D = wp.array3d(np.zeros((batch_count, m, n)), dtype=self.dtype, device=self.device, requires_grad=True)
172
+ AT = wp.array3d(A.numpy().transpose([0, 2, 1]), dtype=self.dtype, device=self.device, requires_grad=True)
173
+ BT = wp.array3d(B.numpy().transpose([0, 2, 1]), dtype=self.dtype, device=self.device, requires_grad=True)
174
+ return A, B, C, D, AT, BT
175
+
176
+ def run_and_verify(self, m, n, k, batch_count, alpha, beta):
177
+ A, B, C1, D1, AT1, BT1 = self.alloc(m, n, k, batch_count)
178
+ C2 = wp.clone(C1)
179
+ C3 = wp.clone(C1)
180
+ D2 = wp.clone(D1)
181
+ D3 = wp.clone(D1)
182
+ AT2 = wp.clone(AT1)
183
+ BT2 = wp.clone(BT1)
184
+ ones1 = wp.zeros_like(D1)
185
+ ones1.fill_(1.0)
186
+ ones2 = wp.zeros_like(D2)
187
+ ones2.fill_(1.0)
188
+ ones3 = wp.zeros_like(D3)
189
+ ones3.fill_(1.0)
190
+
191
+ if batch_count == 1:
192
+ ATT1 = AT1.transpose([1, 0])
193
+ BTT1 = BT1.transpose([1, 0])
194
+ ATT2 = AT2.transpose([1, 0])
195
+ BTT2 = BT2.transpose([1, 0])
196
+ tape = wp.Tape()
197
+ with tape:
198
+ wp.matmul(A, BTT1, C1, D1, alpha, beta, False)
199
+ wp.matmul(ATT1, B, C2, D2, alpha, beta, False)
200
+ wp.matmul(ATT2, BTT2, C3, D3, alpha, beta, False)
201
+ tape.backward(grads={D1: ones1, D2: ones2, D3: ones3})
202
+
203
+ D_np = alpha * (A.numpy() @ B.numpy()) + beta * C1.numpy()
204
+ assert_np_equal(D1.numpy(), D_np)
205
+ assert_np_equal(D2.numpy(), D_np)
206
+ assert_np_equal(D3.numpy(), D_np)
207
+
208
+ adj_A_np = alpha * (ones1.numpy() @ B.numpy().transpose())
209
+ adj_B_np = alpha * (A.numpy().transpose() @ ones1.numpy())
210
+ adj_C_np = beta * ones1.numpy()
211
+
212
+ else:
213
+ ATT1 = AT1.transpose([0, 2, 1])
214
+ BTT1 = BT1.transpose([0, 2, 1])
215
+ ATT2 = AT2.transpose([0, 2, 1])
216
+ BTT2 = BT2.transpose([0, 2, 1])
217
+ tape = wp.Tape()
218
+ with tape:
219
+ wp.batched_matmul(A, BTT1, C1, D1, alpha, beta, False)
220
+ wp.batched_matmul(ATT1, B, C2, D2, alpha, beta, False)
221
+ wp.batched_matmul(ATT2, BTT2, C3, D3, alpha, beta, False)
222
+ tape.backward(grads={D1: ones1, D2: ones2, D3: ones3})
223
+
224
+ D_np = alpha * np.matmul(A.numpy(), B.numpy()) + beta * C1.numpy()
225
+ assert_np_equal(D1.numpy(), D_np)
226
+ assert_np_equal(D2.numpy(), D_np)
227
+ assert_np_equal(D3.numpy(), D_np)
228
+
229
+ adj_A_np = alpha * np.matmul(ones1.numpy(), B.numpy().transpose((0, 2, 1)))
230
+ adj_B_np = alpha * np.matmul(A.numpy().transpose((0, 2, 1)), ones1.numpy())
231
+ adj_C_np = beta * ones1.numpy()
232
+
233
+ assert_np_equal(A.grad.numpy(), adj_A_np)
234
+ assert_np_equal(ATT1.grad.numpy(), adj_A_np)
235
+ assert_np_equal(ATT2.grad.numpy(), adj_A_np)
236
+ assert_np_equal(B.grad.numpy(), adj_B_np)
237
+ assert_np_equal(BTT1.grad.numpy(), adj_B_np)
238
+ assert_np_equal(BTT2.grad.numpy(), adj_B_np)
239
+ assert_np_equal(C1.grad.numpy(), adj_C_np)
240
+ assert_np_equal(C2.grad.numpy(), adj_C_np)
241
+ assert_np_equal(C3.grad.numpy(), adj_C_np)
242
+
243
+ def run(self):
244
+ m = 16
245
+ n = 32
246
+ k = 64
247
+ batch_counts = [1, 4]
248
+ beta = 1.0
249
+ alpha = 1.0
250
+
251
+ for batch_count in batch_counts:
252
+ self.run_and_verify(m, n, k, batch_count, alpha, beta)
253
+
254
+
255
+ # NOTE: F16 tests are slow due to the performance of the reference numpy F16 matmuls performed on CPU.
256
+ def test_f16(test, device):
257
+ gemm_test_bed_runner(wp.float16, device).run()
258
+ gemm_test_bed_runner_transpose(wp.float16, device).run()
259
+
260
+
261
+ @unittest.skipUnless(wp.context.runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
262
+ def test_f32(test, device):
263
+ gemm_test_bed_runner(wp.float32, device).run()
264
+ gemm_test_bed_runner_transpose(wp.float32, device).run()
265
+
266
+
267
+ @unittest.skipUnless(wp.context.runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
268
+ def test_f64(test, device):
269
+ gemm_test_bed_runner(wp.float64, device).run()
270
+ gemm_test_bed_runner_transpose(wp.float64, device).run()
271
+
272
+
273
+ @wp.kernel
274
+ def matrix_sum_kernel(arr: wp.array2d(dtype=float), loss: wp.array(dtype=float)):
275
+ i, j = wp.tid()
276
+ wp.atomic_add(loss, 0, arr[i, j])
277
+
278
+
279
+ @unittest.skipUnless(wp.context.runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
280
+ def test_tape(test, device):
281
+ rng = np.random.default_rng(42)
282
+ low = -4.5
283
+ high = 3.5
284
+ m = 64
285
+ n = 128
286
+ k = 256
287
+ A = wp.array2d(
288
+ np.ceil(rng.uniform(low=low, high=high, size=(m, k))), dtype=float, device=device, requires_grad=True
289
+ )
290
+ B = wp.array2d(
291
+ np.ceil(rng.uniform(low=low, high=high, size=(k, n))), dtype=float, device=device, requires_grad=True
292
+ )
293
+ C = wp.array2d(
294
+ np.ceil(rng.uniform(low=low, high=high, size=(m, n))), dtype=float, device=device, requires_grad=True
295
+ )
296
+ D = wp.array2d(np.zeros((m, n)), dtype=float, device=device, requires_grad=True)
297
+ loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
298
+
299
+ # test tape
300
+ tape = wp.Tape()
301
+ with tape:
302
+ wp.matmul(A, B, C, D)
303
+ wp.launch(matrix_sum_kernel, dim=(m, n), inputs=[D, loss], device=device)
304
+
305
+ tape.backward(loss=loss)
306
+ A_grad = A.grad.numpy()
307
+ tape.reset()
308
+
309
+ # test adjoint
310
+ D.grad = wp.ones((m, n), dtype=float, device=device)
311
+ wp.adj_matmul(A, B, C, A.grad, B.grad, C.grad, D.grad)
312
+ assert_np_equal(A_grad, A.grad.numpy())
313
+
314
+ # test zero
315
+ tape.zero()
316
+ assert_array_equal(A.grad, wp.zeros_like(A))
317
+
318
+
319
+ @unittest.skipUnless(wp.context.runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
320
+ def test_operator(test, device):
321
+ rng = np.random.default_rng(42)
322
+ low = -4.5
323
+ high = 3.5
324
+ m = 64
325
+ n = 128
326
+ k = 256
327
+ A = wp.array2d(
328
+ np.ceil(rng.uniform(low=low, high=high, size=(m, k))), dtype=float, device=device, requires_grad=True
329
+ )
330
+ B = wp.array2d(
331
+ np.ceil(rng.uniform(low=low, high=high, size=(k, n))), dtype=float, device=device, requires_grad=True
332
+ )
333
+ loss = wp.zeros(1, dtype=float, device=device, requires_grad=True)
334
+
335
+ # test tape
336
+ tape = wp.Tape()
337
+ with tape:
338
+ D = A @ B
339
+ wp.launch(matrix_sum_kernel, dim=(m, n), inputs=[D, loss], device=device)
340
+
341
+ tape.backward(loss=loss)
342
+
343
+ # test adjoint
344
+ D.grad = wp.ones((m, n), dtype=float, device=device)
345
+ B_transpose = wp.array2d(B.transpose().numpy(), dtype=float, device=device)
346
+
347
+ adj_A = D.grad @ B_transpose
348
+ assert_array_equal(adj_A, A.grad)
349
+
350
+ # test zero
351
+ tape.zero()
352
+ assert_array_equal(A.grad, wp.zeros_like(A))
353
+
354
+
355
+ @unittest.skipUnless(wp.context.runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
356
+ def test_large_batch_count(test, device):
357
+ rng = np.random.default_rng(42)
358
+ low = -4.5
359
+ high = 3.5
360
+ m = 2
361
+ n = 3
362
+ k = 4
363
+ batch_count = 65535 * 2 + int(65535 / 2)
364
+ A = wp.array3d(
365
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, k))),
366
+ dtype=float,
367
+ device=device,
368
+ requires_grad=True,
369
+ )
370
+ B = wp.array3d(
371
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, k, n))),
372
+ dtype=float,
373
+ device=device,
374
+ requires_grad=True,
375
+ )
376
+ C = wp.array3d(
377
+ np.ceil(rng.uniform(low=low, high=high, size=(batch_count, m, n))),
378
+ dtype=float,
379
+ device=device,
380
+ requires_grad=True,
381
+ )
382
+ D = wp.array3d(np.zeros((batch_count, m, n)), dtype=float, device=device, requires_grad=True)
383
+ ones = wp.zeros_like(D)
384
+ ones.fill_(1.0)
385
+
386
+ alpha = 1.0
387
+ beta = 1.0
388
+
389
+ tape = wp.Tape()
390
+ with tape:
391
+ wp.batched_matmul(A, B, C, D, alpha=alpha, beta=beta, allow_tf32x3_arith=False)
392
+ tape.backward(grads={D: ones})
393
+
394
+ D_np = alpha * np.matmul(A.numpy(), B.numpy()) + beta * C.numpy()
395
+ assert_np_equal(D.numpy(), D_np)
396
+
397
+ adj_A_np = alpha * np.matmul(ones.numpy(), B.numpy().transpose((0, 2, 1)))
398
+ adj_B_np = alpha * np.matmul(A.numpy().transpose((0, 2, 1)), ones.numpy())
399
+ adj_C_np = beta * ones.numpy()
400
+
401
+ assert_np_equal(A.grad.numpy(), adj_A_np)
402
+ assert_np_equal(B.grad.numpy(), adj_B_np)
403
+ assert_np_equal(C.grad.numpy(), adj_C_np)
404
+
405
+
406
+ @unittest.skipUnless(wp.context.runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
407
+ def test_adjoint_accumulation(test, device):
408
+ a_np = np.ones(shape=(2, 3))
409
+ b_np = np.ones(shape=(3, 2))
410
+ c_np = np.zeros(shape=(2, 2))
411
+ d_np = np.zeros(shape=(2, 2))
412
+
413
+ a_wp = wp.from_numpy(a_np, dtype=float, requires_grad=True, device=device)
414
+ b_wp = wp.from_numpy(b_np, dtype=float, requires_grad=True, device=device)
415
+ c_wp = wp.from_numpy(c_np, dtype=float, requires_grad=True, device=device)
416
+ d1_wp = wp.from_numpy(d_np, dtype=float, requires_grad=True, device=device)
417
+ d2_wp = wp.from_numpy(d_np, dtype=float, requires_grad=True, device=device)
418
+
419
+ tape = wp.Tape()
420
+
421
+ with tape:
422
+ wp.matmul(a_wp, b_wp, c_wp, d1_wp, alpha=1.0, beta=1.0)
423
+ wp.matmul(a_wp, b_wp, d1_wp, d2_wp, alpha=1.0, beta=1.0)
424
+
425
+ d_grad = wp.zeros_like(d2_wp, device=device)
426
+ d_grad.fill_(1.0)
427
+ grads = {d2_wp: d_grad}
428
+ tape.backward(grads=grads)
429
+
430
+ assert_np_equal(a_wp.grad.numpy(), 4.0 * np.ones(shape=(2, 3)))
431
+ assert_np_equal(b_wp.grad.numpy(), 4.0 * np.ones(shape=(3, 2)))
432
+ assert_np_equal(c_wp.grad.numpy(), np.ones(shape=(2, 2)))
433
+
434
+
435
+ @unittest.skipUnless(wp.context.runtime.core.is_cutlass_enabled(), "Warp was not built with CUTLASS support")
436
+ def test_cuda_graph_capture(test, device):
437
+ @wp.kernel
438
+ def mat_sum(mat: wp.array2d(dtype=Any), loss: wp.array(dtype=Any)):
439
+ i, j = wp.tid()
440
+ e = mat[i, j]
441
+ wp.atomic_add(loss, 0, e)
442
+
443
+ for T in [wp.float16, wp.float32, wp.float64]:
444
+ wp.overload(mat_sum, [wp.array2d(dtype=T), wp.array(dtype=T)])
445
+
446
+ wp.load_module(device=device)
447
+ wp.load_module(module="warp.utils", device=device)
448
+
449
+ for dtype in [wp.float16, wp.float32, wp.float64]:
450
+ m = 8
451
+ n = 8
452
+ k = 8
453
+
454
+ A = wp.ones((m, n), dtype=dtype, device=device, requires_grad=True)
455
+ B = wp.ones((n, k), dtype=dtype, device=device, requires_grad=True)
456
+ C = wp.zeros((m, k), dtype=dtype, device=device, requires_grad=True)
457
+ D = wp.zeros((m, k), dtype=dtype, device=device, requires_grad=True)
458
+
459
+ loss = wp.zeros(1, dtype=dtype, device=device, requires_grad=True)
460
+
461
+ wp.capture_begin(device, force_module_load=False)
462
+ try:
463
+ tape = wp.Tape()
464
+
465
+ with tape:
466
+ wp.matmul(A, B, C, D)
467
+ wp.launch(mat_sum, dim=(m, k), inputs=[D, loss], device=device)
468
+
469
+ tape.backward(loss=loss)
470
+ finally:
471
+ graph = wp.capture_end(device)
472
+
473
+ wp.capture_launch(graph)
474
+
475
+ assert_np_equal(A.grad.numpy(), 8.0 * np.ones((m, n), dtype=wp.types.warp_type_to_np_dtype[dtype]))
476
+
477
+
478
+ devices = get_test_devices()
479
+ cuda_devices = get_selected_cuda_test_devices()
480
+
481
+
482
+ class TestMatmul(unittest.TestCase):
483
+ pass
484
+
485
+
486
+ # add_function_test(TestMatmul, "test_f16", test_f16, devices=devices)
487
+ add_function_test(TestMatmul, "test_f32", test_f32, devices=devices)
488
+ add_function_test(TestMatmul, "test_f64", test_f64, devices=devices)
489
+ add_function_test(TestMatmul, "test_tape", test_tape, devices=devices)
490
+ add_function_test(TestMatmul, "test_operator", test_operator, devices=devices)
491
+ add_function_test(TestMatmul, "test_large_batch_count", test_large_batch_count, devices=devices)
492
+ add_function_test(TestMatmul, "test_adjoint_accumulation", test_adjoint_accumulation, devices=devices)
493
+ add_function_test(TestMatmul, "test_cuda_graph_capture", test_cuda_graph_capture, devices=cuda_devices)
494
+
495
+
496
+ if __name__ == "__main__":
497
+ wp.build.clear_kernel_cache()
498
+ unittest.main(verbosity=2, failfast=False)