warp-lang 1.9.1__py3-none-manylinux_2_34_aarch64.whl → 1.10.0__py3-none-manylinux_2_34_aarch64.whl

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

Potentially problematic release.


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

Files changed (346) hide show
  1. warp/__init__.py +301 -287
  2. warp/__init__.pyi +882 -305
  3. warp/_src/__init__.py +14 -0
  4. warp/_src/autograd.py +1077 -0
  5. warp/_src/build.py +620 -0
  6. warp/_src/build_dll.py +642 -0
  7. warp/{builtins.py → _src/builtins.py} +1435 -379
  8. warp/_src/codegen.py +4361 -0
  9. warp/{config.py → _src/config.py} +178 -169
  10. warp/_src/constants.py +59 -0
  11. warp/_src/context.py +8352 -0
  12. warp/_src/dlpack.py +464 -0
  13. warp/_src/fabric.py +362 -0
  14. warp/_src/fem/__init__.py +14 -0
  15. warp/_src/fem/adaptivity.py +510 -0
  16. warp/_src/fem/cache.py +689 -0
  17. warp/_src/fem/dirichlet.py +190 -0
  18. warp/{fem → _src/fem}/domain.py +42 -30
  19. warp/_src/fem/field/__init__.py +131 -0
  20. warp/_src/fem/field/field.py +703 -0
  21. warp/{fem → _src/fem}/field/nodal_field.py +32 -15
  22. warp/{fem → _src/fem}/field/restriction.py +3 -1
  23. warp/{fem → _src/fem}/field/virtual.py +55 -27
  24. warp/_src/fem/geometry/__init__.py +32 -0
  25. warp/{fem → _src/fem}/geometry/adaptive_nanogrid.py +79 -163
  26. warp/_src/fem/geometry/closest_point.py +99 -0
  27. warp/{fem → _src/fem}/geometry/deformed_geometry.py +16 -22
  28. warp/{fem → _src/fem}/geometry/element.py +34 -10
  29. warp/{fem → _src/fem}/geometry/geometry.py +50 -20
  30. warp/{fem → _src/fem}/geometry/grid_2d.py +14 -23
  31. warp/{fem → _src/fem}/geometry/grid_3d.py +14 -23
  32. warp/{fem → _src/fem}/geometry/hexmesh.py +42 -63
  33. warp/{fem → _src/fem}/geometry/nanogrid.py +256 -247
  34. warp/{fem → _src/fem}/geometry/partition.py +123 -63
  35. warp/{fem → _src/fem}/geometry/quadmesh.py +28 -45
  36. warp/{fem → _src/fem}/geometry/tetmesh.py +42 -63
  37. warp/{fem → _src/fem}/geometry/trimesh.py +28 -45
  38. warp/{fem → _src/fem}/integrate.py +166 -158
  39. warp/_src/fem/linalg.py +385 -0
  40. warp/_src/fem/operator.py +398 -0
  41. warp/_src/fem/polynomial.py +231 -0
  42. warp/{fem → _src/fem}/quadrature/pic_quadrature.py +17 -20
  43. warp/{fem → _src/fem}/quadrature/quadrature.py +97 -47
  44. warp/_src/fem/space/__init__.py +248 -0
  45. warp/{fem → _src/fem}/space/basis_function_space.py +22 -11
  46. warp/_src/fem/space/basis_space.py +681 -0
  47. warp/{fem → _src/fem}/space/dof_mapper.py +5 -3
  48. warp/{fem → _src/fem}/space/function_space.py +16 -13
  49. warp/{fem → _src/fem}/space/grid_2d_function_space.py +6 -7
  50. warp/{fem → _src/fem}/space/grid_3d_function_space.py +6 -4
  51. warp/{fem → _src/fem}/space/hexmesh_function_space.py +6 -10
  52. warp/{fem → _src/fem}/space/nanogrid_function_space.py +5 -9
  53. warp/{fem → _src/fem}/space/partition.py +119 -60
  54. warp/{fem → _src/fem}/space/quadmesh_function_space.py +6 -10
  55. warp/{fem → _src/fem}/space/restriction.py +68 -33
  56. warp/_src/fem/space/shape/__init__.py +152 -0
  57. warp/{fem → _src/fem}/space/shape/cube_shape_function.py +11 -9
  58. warp/{fem → _src/fem}/space/shape/shape_function.py +10 -9
  59. warp/{fem → _src/fem}/space/shape/square_shape_function.py +8 -6
  60. warp/{fem → _src/fem}/space/shape/tet_shape_function.py +5 -3
  61. warp/{fem → _src/fem}/space/shape/triangle_shape_function.py +5 -3
  62. warp/{fem → _src/fem}/space/tetmesh_function_space.py +5 -9
  63. warp/_src/fem/space/topology.py +461 -0
  64. warp/{fem → _src/fem}/space/trimesh_function_space.py +5 -9
  65. warp/_src/fem/types.py +114 -0
  66. warp/_src/fem/utils.py +488 -0
  67. warp/_src/jax.py +188 -0
  68. warp/_src/jax_experimental/__init__.py +14 -0
  69. warp/_src/jax_experimental/custom_call.py +389 -0
  70. warp/_src/jax_experimental/ffi.py +1286 -0
  71. warp/_src/jax_experimental/xla_ffi.py +658 -0
  72. warp/_src/marching_cubes.py +710 -0
  73. warp/_src/math.py +416 -0
  74. warp/_src/optim/__init__.py +14 -0
  75. warp/_src/optim/adam.py +165 -0
  76. warp/_src/optim/linear.py +1608 -0
  77. warp/_src/optim/sgd.py +114 -0
  78. warp/_src/paddle.py +408 -0
  79. warp/_src/render/__init__.py +14 -0
  80. warp/_src/render/imgui_manager.py +291 -0
  81. warp/_src/render/render_opengl.py +3638 -0
  82. warp/_src/render/render_usd.py +939 -0
  83. warp/_src/render/utils.py +162 -0
  84. warp/_src/sparse.py +2718 -0
  85. warp/_src/tape.py +1208 -0
  86. warp/{thirdparty → _src/thirdparty}/unittest_parallel.py +9 -2
  87. warp/_src/torch.py +393 -0
  88. warp/_src/types.py +5888 -0
  89. warp/_src/utils.py +1695 -0
  90. warp/autograd.py +12 -1054
  91. warp/bin/warp-clang.so +0 -0
  92. warp/bin/warp.so +0 -0
  93. warp/build.py +8 -588
  94. warp/build_dll.py +6 -721
  95. warp/codegen.py +6 -4251
  96. warp/constants.py +6 -39
  97. warp/context.py +12 -8062
  98. warp/dlpack.py +6 -444
  99. warp/examples/distributed/example_jacobi_mpi.py +4 -5
  100. warp/examples/fem/example_adaptive_grid.py +1 -1
  101. warp/examples/fem/example_apic_fluid.py +1 -1
  102. warp/examples/fem/example_burgers.py +8 -8
  103. warp/examples/fem/example_diffusion.py +1 -1
  104. warp/examples/fem/example_distortion_energy.py +1 -1
  105. warp/examples/fem/example_mixed_elasticity.py +2 -2
  106. warp/examples/fem/example_navier_stokes.py +1 -1
  107. warp/examples/fem/example_nonconforming_contact.py +7 -7
  108. warp/examples/fem/example_stokes.py +1 -1
  109. warp/examples/fem/example_stokes_transfer.py +1 -1
  110. warp/examples/fem/utils.py +2 -2
  111. warp/examples/interop/example_jax_callable.py +1 -1
  112. warp/examples/interop/example_jax_ffi_callback.py +1 -1
  113. warp/examples/interop/example_jax_kernel.py +1 -1
  114. warp/examples/tile/example_tile_mcgp.py +191 -0
  115. warp/fabric.py +6 -337
  116. warp/fem/__init__.py +159 -97
  117. warp/fem/adaptivity.py +7 -489
  118. warp/fem/cache.py +9 -648
  119. warp/fem/dirichlet.py +6 -184
  120. warp/fem/field/__init__.py +8 -109
  121. warp/fem/field/field.py +7 -652
  122. warp/fem/geometry/__init__.py +7 -18
  123. warp/fem/geometry/closest_point.py +11 -77
  124. warp/fem/linalg.py +18 -366
  125. warp/fem/operator.py +11 -369
  126. warp/fem/polynomial.py +9 -209
  127. warp/fem/space/__init__.py +5 -211
  128. warp/fem/space/basis_space.py +6 -662
  129. warp/fem/space/shape/__init__.py +41 -118
  130. warp/fem/space/topology.py +6 -437
  131. warp/fem/types.py +6 -81
  132. warp/fem/utils.py +11 -444
  133. warp/jax.py +8 -165
  134. warp/jax_experimental/__init__.py +14 -1
  135. warp/jax_experimental/custom_call.py +8 -365
  136. warp/jax_experimental/ffi.py +17 -873
  137. warp/jax_experimental/xla_ffi.py +5 -605
  138. warp/marching_cubes.py +5 -689
  139. warp/math.py +16 -393
  140. warp/native/array.h +385 -37
  141. warp/native/builtin.h +314 -37
  142. warp/native/bvh.cpp +43 -9
  143. warp/native/bvh.cu +62 -27
  144. warp/native/bvh.h +310 -309
  145. warp/native/clang/clang.cpp +102 -97
  146. warp/native/coloring.cpp +0 -1
  147. warp/native/crt.h +208 -0
  148. warp/native/exports.h +156 -0
  149. warp/native/hashgrid.cu +2 -0
  150. warp/native/intersect.h +24 -1
  151. warp/native/intersect_tri.h +44 -35
  152. warp/native/mat.h +1456 -276
  153. warp/native/mesh.cpp +4 -4
  154. warp/native/mesh.cu +4 -2
  155. warp/native/mesh.h +176 -61
  156. warp/native/quat.h +0 -52
  157. warp/native/scan.cu +2 -0
  158. warp/native/sparse.cu +7 -3
  159. warp/native/spatial.h +12 -0
  160. warp/native/tile.h +681 -89
  161. warp/native/tile_radix_sort.h +3 -3
  162. warp/native/tile_reduce.h +394 -46
  163. warp/native/tile_scan.h +4 -4
  164. warp/native/vec.h +469 -0
  165. warp/native/version.h +23 -0
  166. warp/native/volume.cpp +1 -1
  167. warp/native/volume.cu +1 -0
  168. warp/native/volume.h +1 -1
  169. warp/native/volume_builder.cu +2 -0
  170. warp/native/warp.cpp +57 -29
  171. warp/native/warp.cu +521 -250
  172. warp/native/warp.h +11 -8
  173. warp/optim/__init__.py +6 -3
  174. warp/optim/adam.py +6 -145
  175. warp/optim/linear.py +14 -1585
  176. warp/optim/sgd.py +6 -94
  177. warp/paddle.py +6 -388
  178. warp/render/__init__.py +8 -4
  179. warp/render/imgui_manager.py +7 -267
  180. warp/render/render_opengl.py +6 -3618
  181. warp/render/render_usd.py +6 -919
  182. warp/render/utils.py +6 -142
  183. warp/sparse.py +37 -2563
  184. warp/tape.py +6 -1188
  185. warp/tests/__main__.py +1 -1
  186. warp/tests/cuda/test_async.py +4 -4
  187. warp/tests/cuda/test_conditional_captures.py +1 -1
  188. warp/tests/cuda/test_multigpu.py +1 -1
  189. warp/tests/cuda/test_streams.py +58 -1
  190. warp/tests/geometry/test_bvh.py +157 -22
  191. warp/tests/geometry/test_marching_cubes.py +0 -1
  192. warp/tests/geometry/test_mesh.py +5 -3
  193. warp/tests/geometry/test_mesh_query_aabb.py +5 -12
  194. warp/tests/geometry/test_mesh_query_point.py +5 -2
  195. warp/tests/geometry/test_mesh_query_ray.py +15 -3
  196. warp/tests/geometry/test_volume_write.py +5 -5
  197. warp/tests/interop/test_dlpack.py +18 -17
  198. warp/tests/interop/test_jax.py +772 -49
  199. warp/tests/interop/test_paddle.py +1 -1
  200. warp/tests/test_adam.py +0 -1
  201. warp/tests/test_arithmetic.py +9 -9
  202. warp/tests/test_array.py +578 -100
  203. warp/tests/test_array_reduce.py +3 -3
  204. warp/tests/test_atomic.py +12 -8
  205. warp/tests/test_atomic_bitwise.py +209 -0
  206. warp/tests/test_atomic_cas.py +4 -4
  207. warp/tests/test_bool.py +2 -2
  208. warp/tests/test_builtins_resolution.py +5 -571
  209. warp/tests/test_codegen.py +33 -14
  210. warp/tests/test_conditional.py +1 -1
  211. warp/tests/test_context.py +6 -6
  212. warp/tests/test_copy.py +242 -161
  213. warp/tests/test_ctypes.py +3 -3
  214. warp/tests/test_devices.py +24 -2
  215. warp/tests/test_examples.py +16 -84
  216. warp/tests/test_fabricarray.py +35 -35
  217. warp/tests/test_fast_math.py +0 -2
  218. warp/tests/test_fem.py +56 -10
  219. warp/tests/test_fixedarray.py +3 -3
  220. warp/tests/test_func.py +8 -5
  221. warp/tests/test_generics.py +1 -1
  222. warp/tests/test_indexedarray.py +24 -24
  223. warp/tests/test_intersect.py +39 -9
  224. warp/tests/test_large.py +1 -1
  225. warp/tests/test_lerp.py +3 -1
  226. warp/tests/test_linear_solvers.py +1 -1
  227. warp/tests/test_map.py +35 -4
  228. warp/tests/test_mat.py +52 -62
  229. warp/tests/test_mat_constructors.py +4 -5
  230. warp/tests/test_mat_lite.py +1 -1
  231. warp/tests/test_mat_scalar_ops.py +121 -121
  232. warp/tests/test_math.py +34 -0
  233. warp/tests/test_module_aot.py +4 -4
  234. warp/tests/test_modules_lite.py +28 -2
  235. warp/tests/test_print.py +11 -11
  236. warp/tests/test_quat.py +93 -58
  237. warp/tests/test_runlength_encode.py +1 -1
  238. warp/tests/test_scalar_ops.py +38 -10
  239. warp/tests/test_smoothstep.py +1 -1
  240. warp/tests/test_sparse.py +126 -15
  241. warp/tests/test_spatial.py +105 -87
  242. warp/tests/test_special_values.py +6 -6
  243. warp/tests/test_static.py +7 -7
  244. warp/tests/test_struct.py +13 -2
  245. warp/tests/test_triangle_closest_point.py +48 -1
  246. warp/tests/test_types.py +27 -15
  247. warp/tests/test_utils.py +52 -52
  248. warp/tests/test_vec.py +29 -29
  249. warp/tests/test_vec_constructors.py +5 -5
  250. warp/tests/test_vec_scalar_ops.py +97 -97
  251. warp/tests/test_version.py +75 -0
  252. warp/tests/tile/test_tile.py +178 -0
  253. warp/tests/tile/test_tile_atomic_bitwise.py +403 -0
  254. warp/tests/tile/test_tile_cholesky.py +7 -4
  255. warp/tests/tile/test_tile_load.py +26 -2
  256. warp/tests/tile/test_tile_mathdx.py +3 -3
  257. warp/tests/tile/test_tile_matmul.py +1 -1
  258. warp/tests/tile/test_tile_mlp.py +2 -4
  259. warp/tests/tile/test_tile_reduce.py +214 -13
  260. warp/tests/unittest_suites.py +6 -14
  261. warp/tests/unittest_utils.py +10 -9
  262. warp/tests/walkthrough_debug.py +3 -1
  263. warp/torch.py +6 -373
  264. warp/types.py +29 -5764
  265. warp/utils.py +10 -1659
  266. {warp_lang-1.9.1.dist-info → warp_lang-1.10.0.dist-info}/METADATA +46 -99
  267. warp_lang-1.10.0.dist-info/RECORD +468 -0
  268. warp_lang-1.10.0.dist-info/licenses/licenses/Gaia-LICENSE.txt +6 -0
  269. warp_lang-1.10.0.dist-info/licenses/licenses/appdirs-LICENSE.txt +22 -0
  270. warp_lang-1.10.0.dist-info/licenses/licenses/asset_pixel_jpg-LICENSE.txt +3 -0
  271. warp_lang-1.10.0.dist-info/licenses/licenses/cuda-LICENSE.txt +1582 -0
  272. warp_lang-1.10.0.dist-info/licenses/licenses/dlpack-LICENSE.txt +201 -0
  273. warp_lang-1.10.0.dist-info/licenses/licenses/fp16-LICENSE.txt +28 -0
  274. warp_lang-1.10.0.dist-info/licenses/licenses/libmathdx-LICENSE.txt +220 -0
  275. warp_lang-1.10.0.dist-info/licenses/licenses/llvm-LICENSE.txt +279 -0
  276. warp_lang-1.10.0.dist-info/licenses/licenses/moller-LICENSE.txt +16 -0
  277. warp_lang-1.10.0.dist-info/licenses/licenses/nanovdb-LICENSE.txt +2 -0
  278. warp_lang-1.10.0.dist-info/licenses/licenses/nvrtc-LICENSE.txt +1592 -0
  279. warp_lang-1.10.0.dist-info/licenses/licenses/svd-LICENSE.txt +23 -0
  280. warp_lang-1.10.0.dist-info/licenses/licenses/unittest_parallel-LICENSE.txt +21 -0
  281. warp_lang-1.10.0.dist-info/licenses/licenses/usd-LICENSE.txt +213 -0
  282. warp_lang-1.10.0.dist-info/licenses/licenses/windingnumber-LICENSE.txt +21 -0
  283. warp/examples/assets/cartpole.urdf +0 -110
  284. warp/examples/assets/crazyflie.usd +0 -0
  285. warp/examples/assets/nv_ant.xml +0 -92
  286. warp/examples/assets/nv_humanoid.xml +0 -183
  287. warp/examples/assets/quadruped.urdf +0 -268
  288. warp/examples/optim/example_bounce.py +0 -266
  289. warp/examples/optim/example_cloth_throw.py +0 -228
  290. warp/examples/optim/example_drone.py +0 -870
  291. warp/examples/optim/example_inverse_kinematics.py +0 -182
  292. warp/examples/optim/example_inverse_kinematics_torch.py +0 -191
  293. warp/examples/optim/example_softbody_properties.py +0 -400
  294. warp/examples/optim/example_spring_cage.py +0 -245
  295. warp/examples/optim/example_trajectory.py +0 -227
  296. warp/examples/sim/example_cartpole.py +0 -143
  297. warp/examples/sim/example_cloth.py +0 -225
  298. warp/examples/sim/example_cloth_self_contact.py +0 -316
  299. warp/examples/sim/example_granular.py +0 -130
  300. warp/examples/sim/example_granular_collision_sdf.py +0 -202
  301. warp/examples/sim/example_jacobian_ik.py +0 -244
  302. warp/examples/sim/example_particle_chain.py +0 -124
  303. warp/examples/sim/example_quadruped.py +0 -203
  304. warp/examples/sim/example_rigid_chain.py +0 -203
  305. warp/examples/sim/example_rigid_contact.py +0 -195
  306. warp/examples/sim/example_rigid_force.py +0 -133
  307. warp/examples/sim/example_rigid_gyroscopic.py +0 -115
  308. warp/examples/sim/example_rigid_soft_contact.py +0 -140
  309. warp/examples/sim/example_soft_body.py +0 -196
  310. warp/examples/tile/example_tile_walker.py +0 -327
  311. warp/sim/__init__.py +0 -74
  312. warp/sim/articulation.py +0 -793
  313. warp/sim/collide.py +0 -2570
  314. warp/sim/graph_coloring.py +0 -307
  315. warp/sim/import_mjcf.py +0 -791
  316. warp/sim/import_snu.py +0 -227
  317. warp/sim/import_urdf.py +0 -579
  318. warp/sim/import_usd.py +0 -898
  319. warp/sim/inertia.py +0 -357
  320. warp/sim/integrator.py +0 -245
  321. warp/sim/integrator_euler.py +0 -2000
  322. warp/sim/integrator_featherstone.py +0 -2101
  323. warp/sim/integrator_vbd.py +0 -2487
  324. warp/sim/integrator_xpbd.py +0 -3295
  325. warp/sim/model.py +0 -4821
  326. warp/sim/particles.py +0 -121
  327. warp/sim/render.py +0 -431
  328. warp/sim/utils.py +0 -431
  329. warp/tests/sim/disabled_kinematics.py +0 -244
  330. warp/tests/sim/test_cloth.py +0 -863
  331. warp/tests/sim/test_collision.py +0 -743
  332. warp/tests/sim/test_coloring.py +0 -347
  333. warp/tests/sim/test_inertia.py +0 -161
  334. warp/tests/sim/test_model.py +0 -226
  335. warp/tests/sim/test_sim_grad.py +0 -287
  336. warp/tests/sim/test_sim_grad_bounce_linear.py +0 -212
  337. warp/tests/sim/test_sim_kinematics.py +0 -98
  338. warp/thirdparty/__init__.py +0 -0
  339. warp_lang-1.9.1.dist-info/RECORD +0 -456
  340. /warp/{fem → _src/fem}/quadrature/__init__.py +0 -0
  341. /warp/{tests/sim → _src/thirdparty}/__init__.py +0 -0
  342. /warp/{thirdparty → _src/thirdparty}/appdirs.py +0 -0
  343. /warp/{thirdparty → _src/thirdparty}/dlpack.py +0 -0
  344. {warp_lang-1.9.1.dist-info → warp_lang-1.10.0.dist-info}/WHEEL +0 -0
  345. {warp_lang-1.9.1.dist-info → warp_lang-1.10.0.dist-info}/licenses/LICENSE.md +0 -0
  346. {warp_lang-1.9.1.dist-info → warp_lang-1.10.0.dist-info}/top_level.txt +0 -0
warp/_src/dlpack.py ADDED
@@ -0,0 +1,464 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Python specification for DLpack:
17
+ # https://dmlc.github.io/dlpack/latest/python_spec.html
18
+
19
+ import ctypes
20
+
21
+ import warp
22
+ from warp._src.thirdparty.dlpack import (
23
+ DLDataType,
24
+ DLDataTypeCode,
25
+ DLDevice,
26
+ DLDeviceType,
27
+ DLManagedTensor,
28
+ _c_str_dltensor,
29
+ )
30
+
31
+ _wp_module_name_ = "warp.dlpack"
32
+
33
+ _c_str_used_dltensor = b"used_dltensor"
34
+
35
+ PyMem_RawMalloc = ctypes.pythonapi.PyMem_RawMalloc
36
+ PyMem_RawMalloc.argtypes = [ctypes.c_size_t]
37
+ PyMem_RawMalloc.restype = ctypes.c_void_p
38
+
39
+ PyMem_RawFree = ctypes.pythonapi.PyMem_RawFree
40
+ PyMem_RawFree.argtypes = [ctypes.c_void_p]
41
+ PyMem_RawFree.restype = None
42
+
43
+ Py_IncRef = ctypes.pythonapi.Py_IncRef
44
+ Py_IncRef.argtypes = [ctypes.py_object]
45
+ Py_IncRef.restype = None
46
+
47
+ Py_DecRef = ctypes.pythonapi.Py_DecRef
48
+ Py_DecRef.argtypes = [ctypes.py_object]
49
+ Py_DecRef.restype = None
50
+
51
+ PyCapsule_Destructor = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
52
+
53
+ PyCapsule_IsValid = ctypes.pythonapi.PyCapsule_IsValid
54
+ PyCapsule_IsValid.argtypes = [ctypes.py_object, ctypes.c_char_p]
55
+ PyCapsule_IsValid.restype = ctypes.c_int
56
+
57
+ PyCapsule_GetPointer = ctypes.pythonapi.PyCapsule_GetPointer
58
+ PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
59
+ PyCapsule_GetPointer.restype = ctypes.c_void_p
60
+
61
+ PyCapsule_SetName = ctypes.pythonapi.PyCapsule_SetName
62
+ PyCapsule_SetName.argtypes = [ctypes.py_object, ctypes.c_char_p]
63
+ PyCapsule_SetName.restype = ctypes.c_int
64
+
65
+
66
+ class _DLPackTensorHolder:
67
+ """Class responsible for deleting DLManagedTensor memory after ownership is transferred from a capsule."""
68
+
69
+ def __new__(cls, *args, **kwargs):
70
+ instance = super().__new__(cls)
71
+ instance.mem_ptr = None
72
+ return instance
73
+
74
+ def __init__(self, mem_ptr):
75
+ self.mem_ptr = mem_ptr
76
+
77
+ def __del__(self):
78
+ if not self.mem_ptr:
79
+ return
80
+
81
+ managed_tensor = DLManagedTensor.from_address(self.mem_ptr)
82
+ if managed_tensor.deleter:
83
+ managed_tensor.deleter(self.mem_ptr)
84
+
85
+
86
+ @ctypes.CFUNCTYPE(None, ctypes.c_void_p)
87
+ def _dlpack_tensor_deleter(managed_ptr) -> None:
88
+ """A function to deallocate a DLManagedTensor."""
89
+
90
+ managed_tensor = DLManagedTensor.from_address(managed_ptr)
91
+
92
+ # unreference the source array
93
+ manager = ctypes.cast(managed_tensor.manager_ctx, ctypes.py_object)
94
+ ctypes.pythonapi.Py_DecRef(manager)
95
+
96
+ # free the DLManagedTensor memory, including shape and strides
97
+ PyMem_RawFree(ctypes.c_void_p(managed_ptr))
98
+
99
+
100
+ @PyCapsule_Destructor
101
+ def _dlpack_capsule_deleter(ptr) -> None:
102
+ """Destructor for a capsule holding a DLManagedTensor."""
103
+
104
+ capsule = ctypes.cast(ptr, ctypes.py_object)
105
+
106
+ if PyCapsule_IsValid(capsule, _c_str_dltensor):
107
+ managed_ptr = PyCapsule_GetPointer(capsule, _c_str_dltensor)
108
+ managed_tensor = DLManagedTensor.from_address(managed_ptr)
109
+ if managed_tensor.deleter:
110
+ managed_tensor.deleter(managed_ptr)
111
+
112
+
113
+ def _device_to_dlpack(wp_device: warp._src.context.Device) -> DLDevice:
114
+ dl_device = DLDevice()
115
+
116
+ if wp_device.is_cpu:
117
+ dl_device.device_type = DLDeviceType.kDLCPU
118
+ dl_device.device_id = 0
119
+ elif wp_device.is_cuda:
120
+ dl_device.device_type = DLDeviceType.kDLCUDA
121
+ dl_device.device_id = wp_device.ordinal
122
+ else:
123
+ raise RuntimeError(f"Invalid device type converting to DLPack: {wp_device}")
124
+
125
+ return dl_device
126
+
127
+
128
+ def device_to_dlpack(wp_device) -> DLDevice:
129
+ return _device_to_dlpack(warp.get_device(wp_device))
130
+
131
+
132
+ def dtype_to_dlpack(wp_dtype) -> DLDataType:
133
+ if wp_dtype == warp.bool:
134
+ return (DLDataTypeCode.kDLBool, 8, 1)
135
+ if wp_dtype == warp.int8:
136
+ return (DLDataTypeCode.kDLInt, 8, 1)
137
+ elif wp_dtype == warp.uint8:
138
+ return (DLDataTypeCode.kDLUInt, 8, 1)
139
+ elif wp_dtype == warp.int16:
140
+ return (DLDataTypeCode.kDLInt, 16, 1)
141
+ elif wp_dtype == warp.uint16:
142
+ return (DLDataTypeCode.kDLUInt, 16, 1)
143
+ elif wp_dtype == warp.int32:
144
+ return (DLDataTypeCode.kDLInt, 32, 1)
145
+ elif wp_dtype == warp.uint32:
146
+ return (DLDataTypeCode.kDLUInt, 32, 1)
147
+ elif wp_dtype == warp.int64:
148
+ return (DLDataTypeCode.kDLInt, 64, 1)
149
+ elif wp_dtype == warp.uint64:
150
+ return (DLDataTypeCode.kDLUInt, 64, 1)
151
+ elif wp_dtype == warp.float16:
152
+ return (DLDataTypeCode.kDLFloat, 16, 1)
153
+ elif wp_dtype == warp.float32:
154
+ return (DLDataTypeCode.kDLFloat, 32, 1)
155
+ elif wp_dtype == warp.float64:
156
+ return (DLDataTypeCode.kDLFloat, 64, 1)
157
+ else:
158
+ raise RuntimeError(f"No conversion from Warp type {wp_dtype} to DLPack type")
159
+
160
+
161
+ def dtype_from_dlpack(dl_dtype):
162
+ # unpack to tuple for easier comparison
163
+ dl_dtype = (dl_dtype.type_code.value, dl_dtype.bits)
164
+
165
+ if dl_dtype == (DLDataTypeCode.kDLUInt, 1):
166
+ raise RuntimeError("Warp does not support bit boolean types")
167
+ elif dl_dtype == (DLDataTypeCode.kDLInt, 8):
168
+ return warp._src.types.int8
169
+ elif dl_dtype == (DLDataTypeCode.kDLInt, 16):
170
+ return warp._src.types.int16
171
+ elif dl_dtype == (DLDataTypeCode.kDLInt, 32):
172
+ return warp._src.types.int32
173
+ elif dl_dtype == (DLDataTypeCode.kDLInt, 64):
174
+ return warp._src.types.int64
175
+ elif dl_dtype == (DLDataTypeCode.kDLUInt, 8):
176
+ return warp._src.types.uint8
177
+ elif dl_dtype == (DLDataTypeCode.kDLUInt, 16):
178
+ return warp._src.types.uint16
179
+ elif dl_dtype == (DLDataTypeCode.kDLUInt, 32):
180
+ return warp._src.types.uint32
181
+ elif dl_dtype == (DLDataTypeCode.kDLUInt, 64):
182
+ return warp._src.types.uint64
183
+ elif dl_dtype == (DLDataTypeCode.kDLFloat, 16):
184
+ return warp._src.types.float16
185
+ elif dl_dtype == (DLDataTypeCode.kDLFloat, 32):
186
+ return warp._src.types.float32
187
+ elif dl_dtype == (DLDataTypeCode.kDLFloat, 64):
188
+ return warp._src.types.float64
189
+ elif dl_dtype == (DLDataTypeCode.kDLComplex, 64):
190
+ raise RuntimeError("Warp does not support complex types")
191
+ elif dl_dtype == (DLDataTypeCode.kDLComplex, 128):
192
+ raise RuntimeError("Warp does not support complex types")
193
+ else:
194
+ raise RuntimeError(f"Unknown DLPack datatype {dl_dtype}")
195
+
196
+
197
+ def device_from_dlpack(dl_device):
198
+ assert warp._src.context.runtime is not None, "Warp not initialized, call wp.init() before use"
199
+
200
+ if dl_device.device_type.value == DLDeviceType.kDLCPU or dl_device.device_type.value == DLDeviceType.kDLCUDAHost:
201
+ return warp._src.context.runtime.cpu_device
202
+ elif (
203
+ dl_device.device_type.value == DLDeviceType.kDLCUDA
204
+ or dl_device.device_type.value == DLDeviceType.kDLCUDAManaged
205
+ ):
206
+ return warp._src.context.runtime.cuda_devices[dl_device.device_id]
207
+ else:
208
+ raise RuntimeError(f"Unknown device type from DLPack: {dl_device.device_type.value}")
209
+
210
+
211
+ def shape_to_dlpack(shape):
212
+ a = (ctypes.c_int64 * len(shape))(*shape)
213
+ return a
214
+
215
+
216
+ def strides_to_dlpack(strides, dtype):
217
+ # convert from byte count to element count
218
+ ndim = len(strides)
219
+ a = (ctypes.c_int64 * ndim)()
220
+ dtype_size = warp._src.types.type_size_in_bytes(dtype)
221
+ for i in range(ndim):
222
+ a[i] = strides[i] // dtype_size
223
+ return a
224
+
225
+
226
+ def to_dlpack(wp_array: warp.array):
227
+ """Convert a Warp array to another type of DLPack-compatible array.
228
+
229
+ Args:
230
+ wp_array: The source Warp array that will be converted.
231
+
232
+ Returns:
233
+ A capsule containing a DLManagedTensor that can be converted
234
+ to another array type without copying the underlying memory.
235
+ """
236
+
237
+ # DLPack does not support structured arrays
238
+ if isinstance(wp_array.dtype, warp._src.codegen.Struct):
239
+ raise RuntimeError("Cannot convert structured Warp arrays to DLPack.")
240
+
241
+ # handle vector types
242
+ if hasattr(wp_array.dtype, "_wp_scalar_type_"):
243
+ # vector type, flatten the dimensions into one tuple
244
+ target_dtype = wp_array.dtype._wp_scalar_type_
245
+ target_ndim = wp_array.ndim + len(wp_array.dtype._shape_)
246
+ target_shape = (*wp_array.shape, *wp_array.dtype._shape_)
247
+ dtype_strides = warp._src.types.strides_from_shape(wp_array.dtype._shape_, wp_array.dtype._wp_scalar_type_)
248
+ target_strides = (*wp_array.strides, *dtype_strides)
249
+ else:
250
+ # scalar type
251
+ target_dtype = wp_array.dtype
252
+ target_ndim = wp_array.ndim
253
+ target_shape = wp_array.shape
254
+ target_strides = wp_array.strides
255
+
256
+ if wp_array.pinned:
257
+ dl_device = DLDevice()
258
+ dl_device.device_type = DLDeviceType.kDLCUDAHost
259
+ dl_device.device_id = 0
260
+ else:
261
+ dl_device = _device_to_dlpack(wp_array.device)
262
+
263
+ # allocate DLManagedTensor, shape, and strides together
264
+ managed_tensor_size = ctypes.sizeof(DLManagedTensor)
265
+ padding = managed_tensor_size & 7
266
+ shape_size = target_ndim * 8
267
+ mem_size = managed_tensor_size + padding + 2 * shape_size
268
+ mem_ptr = PyMem_RawMalloc(mem_size)
269
+ assert mem_ptr, "Failed to allocate memory for DLManagedTensor"
270
+
271
+ # set managed tensor attributes
272
+ managed_tensor = DLManagedTensor.from_address(mem_ptr)
273
+ managed_tensor.dl_tensor.data = wp_array.ptr
274
+ managed_tensor.dl_tensor.device = dl_device
275
+ managed_tensor.dl_tensor.ndim = target_ndim
276
+ managed_tensor.dl_tensor.dtype = dtype_to_dlpack(target_dtype)
277
+ managed_tensor.dl_tensor.byte_offset = 0
278
+
279
+ # shape
280
+ shape_offset = managed_tensor_size + padding
281
+ shape_ptr = ctypes.cast(mem_ptr + shape_offset, ctypes.POINTER(ctypes.c_int64))
282
+ for i in range(target_ndim):
283
+ shape_ptr[i] = target_shape[i]
284
+ managed_tensor.dl_tensor.shape = shape_ptr
285
+
286
+ # strides, if not contiguous
287
+ if wp_array.is_contiguous:
288
+ managed_tensor.dl_tensor.strides = None
289
+ else:
290
+ stride_offset = shape_offset + shape_size
291
+ stride_ptr = ctypes.cast(mem_ptr + stride_offset, ctypes.POINTER(ctypes.c_int64))
292
+ dtype_size = warp._src.types.type_size_in_bytes(target_dtype)
293
+ for i in range(target_ndim):
294
+ stride_ptr[i] = target_strides[i] // dtype_size
295
+ managed_tensor.dl_tensor.strides = stride_ptr
296
+
297
+ # DLManagedTensor holds a reference to the source array
298
+ managed_tensor.manager_ctx = id(wp_array)
299
+ Py_IncRef(wp_array)
300
+
301
+ managed_tensor.deleter = _dlpack_tensor_deleter
302
+
303
+ # NOTE: jax.ffi.pycapsule() defines the PyCapsule_New() argtypes incorrectly, which causes problems.
304
+ # Here we make sure that the PyCapsule_Destructor callback is correctly defined.
305
+ PyCapsule_New = ctypes.pythonapi.PyCapsule_New
306
+ PyCapsule_New.argtypes = [ctypes.c_void_p, ctypes.c_char_p, PyCapsule_Destructor]
307
+ PyCapsule_New.restype = ctypes.py_object
308
+
309
+ capsule = PyCapsule_New(
310
+ ctypes.byref(managed_tensor),
311
+ _c_str_dltensor,
312
+ _dlpack_capsule_deleter,
313
+ )
314
+
315
+ return capsule
316
+
317
+
318
+ def dtype_is_compatible(dl_dtype, wp_dtype):
319
+ if dl_dtype.bits % 8 != 0:
320
+ raise RuntimeError("Data types with less than 8 bits are not supported")
321
+
322
+ if dl_dtype.type_code.value == DLDataTypeCode.kDLFloat:
323
+ if dl_dtype.bits == 16:
324
+ return wp_dtype == warp.float16
325
+ elif dl_dtype.bits == 32:
326
+ return wp_dtype == warp.float32
327
+ elif dl_dtype.bits == 64:
328
+ return wp_dtype == warp.float64
329
+ elif dl_dtype.type_code.value == DLDataTypeCode.kDLInt or dl_dtype.type_code.value == DLDataTypeCode.kDLUInt:
330
+ if dl_dtype.bits == 8:
331
+ return wp_dtype == warp.int8 or wp_dtype == warp.uint8
332
+ elif dl_dtype.bits == 16:
333
+ return wp_dtype == warp.int16 or wp_dtype == warp.uint16
334
+ elif dl_dtype.bits == 32:
335
+ return wp_dtype == warp.int32 or wp_dtype == warp.uint32
336
+ elif dl_dtype.bits == 64:
337
+ return wp_dtype == warp.int64 or wp_dtype == warp.uint64
338
+ elif dl_dtype.type_code.value == DLDataTypeCode.kDLBfloat:
339
+ raise RuntimeError("Bfloat data type is not supported")
340
+ elif dl_dtype.type_code.value == DLDataTypeCode.kDLComplex:
341
+ raise RuntimeError("Complex data types are not supported")
342
+ else:
343
+ raise RuntimeError(f"Unsupported DLPack dtype {(str(dl_dtype.type_code), dl_dtype.bits)}")
344
+
345
+
346
+ def _from_dlpack(capsule, dtype=None) -> warp.array:
347
+ """Convert a DLPack capsule into a Warp array without copying.
348
+
349
+ Args:
350
+ capsule: A DLPack capsule wrapping an external array or tensor.
351
+ dtype: An optional Warp data type to interpret the source data.
352
+
353
+ Returns:
354
+ A new Warp array that uses the same underlying memory as the input capsule.
355
+ """
356
+
357
+ assert PyCapsule_IsValid(capsule, _c_str_dltensor), "Invalid capsule"
358
+ mem_ptr = PyCapsule_GetPointer(capsule, _c_str_dltensor)
359
+ managed_tensor = DLManagedTensor.from_address(mem_ptr)
360
+
361
+ dlt = managed_tensor.dl_tensor
362
+
363
+ device = device_from_dlpack(dlt.device)
364
+ pinned = dlt.device.device_type.value == DLDeviceType.kDLCUDAHost
365
+ shape = tuple(dlt.shape[dim] for dim in range(dlt.ndim))
366
+
367
+ # strides, if not contiguous
368
+ itemsize = dlt.dtype.bits // 8
369
+ if dlt.strides:
370
+ strides = tuple(dlt.strides[dim] * itemsize for dim in range(dlt.ndim))
371
+ else:
372
+ strides = None
373
+
374
+ # handle multi-lane dtypes as another dimension
375
+ if dlt.dtype.lanes > 1:
376
+ shape = (*shape, dlt.dtype.lanes)
377
+ if strides is not None:
378
+ strides = (*strides, itemsize)
379
+
380
+ if dtype is None:
381
+ # automatically detect dtype
382
+ dtype = dtype_from_dlpack(dlt.dtype)
383
+
384
+ elif hasattr(dtype, "_wp_scalar_type_"):
385
+ # handle vector/matrix types
386
+
387
+ if not dtype_is_compatible(dlt.dtype, dtype._wp_scalar_type_):
388
+ raise RuntimeError(f"Incompatible data types: {dlt.dtype} and {dtype}")
389
+
390
+ dtype_shape = dtype._shape_
391
+ dtype_dims = len(dtype._shape_)
392
+ if dtype_dims > len(shape) or dtype_shape != shape[-dtype_dims:]:
393
+ raise RuntimeError(
394
+ f"Could not convert DLPack tensor with shape {shape} to Warp array with dtype={dtype}, ensure that source inner shape is {dtype_shape}"
395
+ )
396
+
397
+ if strides is not None:
398
+ # ensure the inner strides are contiguous
399
+ stride = itemsize
400
+ for i in range(dtype_dims):
401
+ if strides[-i - 1] != stride:
402
+ raise RuntimeError(
403
+ f"Could not convert DLPack tensor with shape {shape} to Warp array with dtype={dtype}, because the source inner strides are not contiguous"
404
+ )
405
+ stride *= dtype_shape[-i - 1]
406
+ strides = tuple(strides[:-dtype_dims]) or (itemsize,)
407
+
408
+ shape = tuple(shape[:-dtype_dims]) or (1,)
409
+
410
+ elif not dtype_is_compatible(dlt.dtype, dtype):
411
+ # incompatible dtype requested
412
+ raise RuntimeError(f"Incompatible data types: {dlt.dtype} and {dtype}")
413
+
414
+ a = warp._src.types.array(
415
+ ptr=dlt.data, dtype=dtype, shape=shape, strides=strides, copy=False, device=device, pinned=pinned
416
+ )
417
+
418
+ # take ownership of the DLManagedTensor
419
+ a._dlpack_tensor_holder = _DLPackTensorHolder(mem_ptr)
420
+
421
+ # rename the capsule so that it no longer owns the DLManagedTensor
422
+ PyCapsule_SetName(capsule, _c_str_used_dltensor)
423
+
424
+ return a
425
+
426
+
427
+ def from_dlpack(source, dtype=None) -> warp.array:
428
+ """Convert a source array or DLPack capsule into a Warp array without copying.
429
+
430
+ Args:
431
+ source: A DLPack-compatible array or PyCapsule
432
+ dtype: An optional Warp data type to interpret the source data.
433
+
434
+ Returns:
435
+ A new Warp array that uses the same underlying memory as the input
436
+ pycapsule.
437
+ """
438
+
439
+ # See https://data-apis.org/array-api/2022.12/API_specification/generated/array_api.array.__dlpack__.html
440
+
441
+ if hasattr(source, "__dlpack__"):
442
+ device_type, device_id = source.__dlpack_device__()
443
+ # Check if the source lives on a CUDA device
444
+ if device_type in (DLDeviceType.kDLCUDA, DLDeviceType.kDLCUDAManaged):
445
+ # Assume that the caller will use the array on its device's current stream.
446
+ # Note that we pass 1 for the null stream, per DLPack spec.
447
+ cuda_stream = warp.get_cuda_device(device_id).stream.cuda_stream or 1
448
+ elif device_type == DLDeviceType.kDLCPU:
449
+ # No stream sync for CPU arrays.
450
+ cuda_stream = None
451
+ elif device_type == DLDeviceType.kDLCUDAHost:
452
+ # For pinned memory, we sync with the current CUDA device's stream.
453
+ # Note that we pass 1 for the null stream, per DLPack spec.
454
+ cuda_stream = warp.get_cuda_device().stream.cuda_stream or 1
455
+ else:
456
+ raise TypeError("Unsupported source device")
457
+
458
+ capsule = source.__dlpack__(stream=cuda_stream)
459
+
460
+ else:
461
+ # legacy behaviour, assume source is a capsule
462
+ capsule = source
463
+
464
+ return _from_dlpack(capsule, dtype=dtype)