warp-lang 1.0.2__py3-none-manylinux2014_x86_64.whl → 1.1.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.
- warp/__init__.py +108 -97
- warp/__init__.pyi +1 -1
- warp/bin/warp-clang.so +0 -0
- warp/bin/warp.so +0 -0
- warp/build.py +115 -113
- warp/build_dll.py +383 -375
- warp/builtins.py +3425 -3354
- warp/codegen.py +2878 -2792
- warp/config.py +40 -36
- warp/constants.py +45 -45
- warp/context.py +5194 -5102
- warp/dlpack.py +442 -442
- warp/examples/__init__.py +16 -16
- warp/examples/assets/bear.usd +0 -0
- warp/examples/assets/bunny.usd +0 -0
- warp/examples/assets/cartpole.urdf +110 -110
- warp/examples/assets/crazyflie.usd +0 -0
- warp/examples/assets/cube.usd +0 -0
- warp/examples/assets/nv_ant.xml +92 -92
- warp/examples/assets/nv_humanoid.xml +183 -183
- warp/examples/assets/quadruped.urdf +267 -267
- warp/examples/assets/rocks.nvdb +0 -0
- warp/examples/assets/rocks.usd +0 -0
- warp/examples/assets/sphere.usd +0 -0
- warp/examples/benchmarks/benchmark_api.py +383 -383
- warp/examples/benchmarks/benchmark_cloth.py +278 -277
- warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
- warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
- warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
- warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
- warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
- warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
- warp/examples/benchmarks/benchmark_cloth_warp.py +146 -146
- warp/examples/benchmarks/benchmark_launches.py +295 -295
- warp/examples/browse.py +29 -29
- warp/examples/core/example_dem.py +234 -219
- warp/examples/core/example_fluid.py +293 -267
- warp/examples/core/example_graph_capture.py +144 -126
- warp/examples/core/example_marching_cubes.py +188 -174
- warp/examples/core/example_mesh.py +174 -155
- warp/examples/core/example_mesh_intersect.py +205 -193
- warp/examples/core/example_nvdb.py +176 -170
- warp/examples/core/example_raycast.py +105 -90
- warp/examples/core/example_raymarch.py +199 -178
- warp/examples/core/example_render_opengl.py +185 -141
- warp/examples/core/example_sph.py +405 -387
- warp/examples/core/example_torch.py +222 -181
- warp/examples/core/example_wave.py +263 -248
- warp/examples/fem/bsr_utils.py +378 -380
- warp/examples/fem/example_apic_fluid.py +407 -389
- warp/examples/fem/example_convection_diffusion.py +182 -168
- warp/examples/fem/example_convection_diffusion_dg.py +219 -209
- warp/examples/fem/example_convection_diffusion_dg0.py +204 -194
- warp/examples/fem/example_deformed_geometry.py +177 -159
- warp/examples/fem/example_diffusion.py +201 -173
- warp/examples/fem/example_diffusion_3d.py +177 -152
- warp/examples/fem/example_diffusion_mgpu.py +221 -214
- warp/examples/fem/example_mixed_elasticity.py +244 -222
- warp/examples/fem/example_navier_stokes.py +259 -243
- warp/examples/fem/example_stokes.py +220 -192
- warp/examples/fem/example_stokes_transfer.py +265 -249
- warp/examples/fem/mesh_utils.py +133 -109
- warp/examples/fem/plot_utils.py +292 -287
- warp/examples/optim/example_bounce.py +260 -246
- warp/examples/optim/example_cloth_throw.py +222 -209
- warp/examples/optim/example_diffray.py +566 -536
- warp/examples/optim/example_drone.py +864 -835
- warp/examples/optim/example_inverse_kinematics.py +176 -168
- warp/examples/optim/example_inverse_kinematics_torch.py +185 -169
- warp/examples/optim/example_spring_cage.py +239 -231
- warp/examples/optim/example_trajectory.py +223 -199
- warp/examples/optim/example_walker.py +306 -293
- warp/examples/sim/example_cartpole.py +139 -129
- warp/examples/sim/example_cloth.py +196 -186
- warp/examples/sim/example_granular.py +124 -111
- warp/examples/sim/example_granular_collision_sdf.py +197 -186
- warp/examples/sim/example_jacobian_ik.py +236 -214
- warp/examples/sim/example_particle_chain.py +118 -105
- warp/examples/sim/example_quadruped.py +193 -180
- warp/examples/sim/example_rigid_chain.py +197 -187
- warp/examples/sim/example_rigid_contact.py +189 -177
- warp/examples/sim/example_rigid_force.py +127 -125
- warp/examples/sim/example_rigid_gyroscopic.py +109 -95
- warp/examples/sim/example_rigid_soft_contact.py +134 -122
- warp/examples/sim/example_soft_body.py +190 -177
- warp/fabric.py +337 -335
- warp/fem/__init__.py +60 -27
- warp/fem/cache.py +401 -388
- warp/fem/dirichlet.py +178 -179
- warp/fem/domain.py +262 -263
- warp/fem/field/__init__.py +100 -101
- warp/fem/field/field.py +148 -149
- warp/fem/field/nodal_field.py +298 -299
- warp/fem/field/restriction.py +22 -21
- warp/fem/field/test.py +180 -181
- warp/fem/field/trial.py +183 -183
- warp/fem/geometry/__init__.py +15 -19
- warp/fem/geometry/closest_point.py +69 -70
- warp/fem/geometry/deformed_geometry.py +270 -271
- warp/fem/geometry/element.py +744 -744
- warp/fem/geometry/geometry.py +184 -186
- warp/fem/geometry/grid_2d.py +380 -373
- warp/fem/geometry/grid_3d.py +441 -435
- warp/fem/geometry/hexmesh.py +953 -953
- warp/fem/geometry/partition.py +374 -376
- warp/fem/geometry/quadmesh_2d.py +532 -532
- warp/fem/geometry/tetmesh.py +840 -840
- warp/fem/geometry/trimesh_2d.py +577 -577
- warp/fem/integrate.py +1630 -1615
- warp/fem/operator.py +190 -191
- warp/fem/polynomial.py +214 -213
- warp/fem/quadrature/__init__.py +2 -2
- warp/fem/quadrature/pic_quadrature.py +243 -245
- warp/fem/quadrature/quadrature.py +295 -294
- warp/fem/space/__init__.py +294 -292
- warp/fem/space/basis_space.py +488 -489
- warp/fem/space/collocated_function_space.py +100 -105
- warp/fem/space/dof_mapper.py +236 -236
- warp/fem/space/function_space.py +148 -145
- warp/fem/space/grid_2d_function_space.py +267 -267
- warp/fem/space/grid_3d_function_space.py +305 -306
- warp/fem/space/hexmesh_function_space.py +350 -352
- warp/fem/space/partition.py +350 -350
- warp/fem/space/quadmesh_2d_function_space.py +368 -369
- warp/fem/space/restriction.py +158 -160
- warp/fem/space/shape/__init__.py +13 -15
- warp/fem/space/shape/cube_shape_function.py +738 -738
- warp/fem/space/shape/shape_function.py +102 -103
- warp/fem/space/shape/square_shape_function.py +611 -611
- warp/fem/space/shape/tet_shape_function.py +565 -567
- warp/fem/space/shape/triangle_shape_function.py +429 -429
- warp/fem/space/tetmesh_function_space.py +294 -292
- warp/fem/space/topology.py +297 -295
- warp/fem/space/trimesh_2d_function_space.py +223 -221
- warp/fem/types.py +77 -77
- warp/fem/utils.py +495 -495
- warp/jax.py +166 -141
- warp/jax_experimental.py +341 -339
- warp/native/array.h +1072 -1025
- warp/native/builtin.h +1560 -1560
- warp/native/bvh.cpp +398 -398
- warp/native/bvh.cu +525 -525
- warp/native/bvh.h +429 -429
- warp/native/clang/clang.cpp +495 -464
- warp/native/crt.cpp +31 -31
- warp/native/crt.h +334 -334
- warp/native/cuda_crt.h +1049 -1049
- warp/native/cuda_util.cpp +549 -540
- warp/native/cuda_util.h +288 -203
- warp/native/cutlass_gemm.cpp +34 -34
- warp/native/cutlass_gemm.cu +372 -372
- warp/native/error.cpp +66 -66
- warp/native/error.h +27 -27
- warp/native/fabric.h +228 -228
- warp/native/hashgrid.cpp +301 -278
- warp/native/hashgrid.cu +78 -77
- warp/native/hashgrid.h +227 -227
- warp/native/initializer_array.h +32 -32
- warp/native/intersect.h +1204 -1204
- warp/native/intersect_adj.h +365 -365
- warp/native/intersect_tri.h +322 -322
- warp/native/marching.cpp +2 -2
- warp/native/marching.cu +497 -497
- warp/native/marching.h +2 -2
- warp/native/mat.h +1498 -1498
- warp/native/matnn.h +333 -333
- warp/native/mesh.cpp +203 -203
- warp/native/mesh.cu +293 -293
- warp/native/mesh.h +1887 -1887
- warp/native/nanovdb/NanoVDB.h +4782 -4782
- warp/native/nanovdb/PNanoVDB.h +2553 -2553
- warp/native/nanovdb/PNanoVDBWrite.h +294 -294
- warp/native/noise.h +850 -850
- warp/native/quat.h +1084 -1084
- warp/native/rand.h +299 -299
- warp/native/range.h +108 -108
- warp/native/reduce.cpp +156 -156
- warp/native/reduce.cu +348 -348
- warp/native/runlength_encode.cpp +61 -61
- warp/native/runlength_encode.cu +46 -46
- warp/native/scan.cpp +30 -30
- warp/native/scan.cu +36 -36
- warp/native/scan.h +7 -7
- warp/native/solid_angle.h +442 -442
- warp/native/sort.cpp +94 -94
- warp/native/sort.cu +97 -97
- warp/native/sort.h +14 -14
- warp/native/sparse.cpp +337 -337
- warp/native/sparse.cu +544 -544
- warp/native/spatial.h +630 -630
- warp/native/svd.h +562 -562
- warp/native/temp_buffer.h +30 -30
- warp/native/vec.h +1132 -1132
- warp/native/volume.cpp +297 -297
- warp/native/volume.cu +32 -32
- warp/native/volume.h +538 -538
- warp/native/volume_builder.cu +425 -425
- warp/native/volume_builder.h +19 -19
- warp/native/warp.cpp +1057 -1052
- warp/native/warp.cu +2943 -2828
- warp/native/warp.h +313 -305
- warp/optim/__init__.py +9 -9
- warp/optim/adam.py +120 -120
- warp/optim/linear.py +1104 -939
- warp/optim/sgd.py +104 -92
- warp/render/__init__.py +10 -10
- warp/render/render_opengl.py +3217 -3204
- warp/render/render_usd.py +768 -749
- warp/render/utils.py +152 -150
- warp/sim/__init__.py +52 -59
- warp/sim/articulation.py +685 -685
- warp/sim/collide.py +1594 -1590
- warp/sim/import_mjcf.py +489 -481
- warp/sim/import_snu.py +220 -221
- warp/sim/import_urdf.py +536 -516
- warp/sim/import_usd.py +887 -881
- warp/sim/inertia.py +316 -317
- warp/sim/integrator.py +234 -233
- warp/sim/integrator_euler.py +1956 -1956
- warp/sim/integrator_featherstone.py +1910 -1991
- warp/sim/integrator_xpbd.py +3294 -3312
- warp/sim/model.py +4473 -4314
- warp/sim/particles.py +113 -112
- warp/sim/render.py +417 -403
- warp/sim/utils.py +413 -410
- warp/sparse.py +1227 -1227
- warp/stubs.py +2109 -2469
- warp/tape.py +1162 -225
- warp/tests/__init__.py +1 -1
- warp/tests/__main__.py +4 -4
- warp/tests/assets/torus.usda +105 -105
- warp/tests/aux_test_class_kernel.py +26 -26
- warp/tests/aux_test_compile_consts_dummy.py +10 -10
- warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
- warp/tests/aux_test_dependent.py +22 -22
- warp/tests/aux_test_grad_customs.py +23 -23
- warp/tests/aux_test_reference.py +11 -11
- warp/tests/aux_test_reference_reference.py +10 -10
- warp/tests/aux_test_square.py +17 -17
- warp/tests/aux_test_unresolved_func.py +14 -14
- warp/tests/aux_test_unresolved_symbol.py +14 -14
- warp/tests/disabled_kinematics.py +239 -239
- warp/tests/run_coverage_serial.py +31 -31
- warp/tests/test_adam.py +157 -157
- warp/tests/test_arithmetic.py +1124 -1124
- warp/tests/test_array.py +2417 -2326
- warp/tests/test_array_reduce.py +150 -150
- warp/tests/test_async.py +668 -656
- warp/tests/test_atomic.py +141 -141
- warp/tests/test_bool.py +204 -149
- warp/tests/test_builtins_resolution.py +1292 -1292
- warp/tests/test_bvh.py +164 -171
- warp/tests/test_closest_point_edge_edge.py +228 -228
- warp/tests/test_codegen.py +566 -553
- warp/tests/test_compile_consts.py +97 -101
- warp/tests/test_conditional.py +246 -246
- warp/tests/test_copy.py +232 -215
- warp/tests/test_ctypes.py +632 -632
- warp/tests/test_dense.py +67 -67
- warp/tests/test_devices.py +91 -98
- warp/tests/test_dlpack.py +530 -529
- warp/tests/test_examples.py +400 -378
- warp/tests/test_fabricarray.py +955 -955
- warp/tests/test_fast_math.py +62 -54
- warp/tests/test_fem.py +1277 -1278
- warp/tests/test_fp16.py +130 -130
- warp/tests/test_func.py +338 -337
- warp/tests/test_generics.py +571 -571
- warp/tests/test_grad.py +746 -640
- warp/tests/test_grad_customs.py +333 -336
- warp/tests/test_hash_grid.py +210 -164
- warp/tests/test_import.py +39 -39
- warp/tests/test_indexedarray.py +1134 -1134
- warp/tests/test_intersect.py +67 -67
- warp/tests/test_jax.py +307 -307
- warp/tests/test_large.py +167 -164
- warp/tests/test_launch.py +354 -354
- warp/tests/test_lerp.py +261 -261
- warp/tests/test_linear_solvers.py +191 -171
- warp/tests/test_lvalue.py +421 -493
- warp/tests/test_marching_cubes.py +65 -65
- warp/tests/test_mat.py +1801 -1827
- warp/tests/test_mat_lite.py +115 -115
- warp/tests/test_mat_scalar_ops.py +2907 -2889
- warp/tests/test_math.py +126 -193
- warp/tests/test_matmul.py +500 -499
- warp/tests/test_matmul_lite.py +410 -410
- warp/tests/test_mempool.py +188 -190
- warp/tests/test_mesh.py +284 -324
- warp/tests/test_mesh_query_aabb.py +228 -241
- warp/tests/test_mesh_query_point.py +692 -702
- warp/tests/test_mesh_query_ray.py +292 -303
- warp/tests/test_mlp.py +276 -276
- warp/tests/test_model.py +110 -110
- warp/tests/test_modules_lite.py +39 -39
- warp/tests/test_multigpu.py +163 -163
- warp/tests/test_noise.py +248 -248
- warp/tests/test_operators.py +250 -250
- warp/tests/test_options.py +123 -125
- warp/tests/test_peer.py +133 -137
- warp/tests/test_pinned.py +78 -78
- warp/tests/test_print.py +54 -54
- warp/tests/test_quat.py +2086 -2086
- warp/tests/test_rand.py +288 -288
- warp/tests/test_reload.py +217 -217
- warp/tests/test_rounding.py +179 -179
- warp/tests/test_runlength_encode.py +190 -190
- warp/tests/test_sim_grad.py +243 -0
- warp/tests/test_sim_kinematics.py +91 -97
- warp/tests/test_smoothstep.py +168 -168
- warp/tests/test_snippet.py +305 -266
- warp/tests/test_sparse.py +468 -460
- warp/tests/test_spatial.py +2148 -2148
- warp/tests/test_streams.py +486 -473
- warp/tests/test_struct.py +710 -675
- warp/tests/test_tape.py +173 -148
- warp/tests/test_torch.py +743 -743
- warp/tests/test_transient_module.py +87 -87
- warp/tests/test_types.py +556 -659
- warp/tests/test_utils.py +490 -499
- warp/tests/test_vec.py +1264 -1268
- warp/tests/test_vec_lite.py +73 -73
- warp/tests/test_vec_scalar_ops.py +2099 -2099
- warp/tests/test_verify_fp.py +94 -94
- warp/tests/test_volume.py +737 -736
- warp/tests/test_volume_write.py +255 -265
- warp/tests/unittest_serial.py +37 -37
- warp/tests/unittest_suites.py +363 -359
- warp/tests/unittest_utils.py +603 -578
- warp/tests/unused_test_misc.py +71 -71
- warp/tests/walkthrough_debug.py +85 -85
- warp/thirdparty/appdirs.py +598 -598
- warp/thirdparty/dlpack.py +143 -143
- warp/thirdparty/unittest_parallel.py +566 -561
- warp/torch.py +321 -295
- warp/types.py +4504 -4450
- warp/utils.py +1008 -821
- {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/LICENSE.md +126 -126
- {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/METADATA +338 -400
- warp_lang-1.1.0.dist-info/RECORD +352 -0
- warp/examples/assets/cube.usda +0 -42
- warp/examples/assets/sphere.usda +0 -56
- warp/examples/assets/torus.usda +0 -105
- warp_lang-1.0.2.dist-info/RECORD +0 -352
- {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/WHEEL +0 -0
- {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/top_level.txt +0 -0
warp/tests/test_mat.py
CHANGED
|
@@ -1,1827 +1,1801 @@
|
|
|
1
|
-
# Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
|
|
2
|
-
# NVIDIA CORPORATION and its licensors retain all intellectual property
|
|
3
|
-
# and proprietary rights in and to this software, related documentation
|
|
4
|
-
# and any modifications thereto. Any use, reproduction, disclosure or
|
|
5
|
-
# distribution of this software and related documentation without an express
|
|
6
|
-
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
|
7
|
-
|
|
8
|
-
import unittest
|
|
9
|
-
|
|
10
|
-
import numpy as np
|
|
11
|
-
|
|
12
|
-
import warp as wp
|
|
13
|
-
from warp.tests.unittest_utils import *
|
|
14
|
-
|
|
15
|
-
wp.init()
|
|
16
|
-
|
|
17
|
-
np_signed_int_types = [
|
|
18
|
-
np.int8,
|
|
19
|
-
np.int16,
|
|
20
|
-
np.int32,
|
|
21
|
-
np.int64,
|
|
22
|
-
np.byte,
|
|
23
|
-
]
|
|
24
|
-
|
|
25
|
-
np_float_types = [np.float16, np.float32, np.float64]
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def randvals(rng, shape, dtype):
|
|
29
|
-
if dtype in np_float_types:
|
|
30
|
-
return rng.standard_normal(size=shape).astype(dtype)
|
|
31
|
-
elif dtype in [np.int8, np.uint8, np.byte, np.ubyte]:
|
|
32
|
-
return rng.integers(1, high=3, size=shape, dtype=dtype)
|
|
33
|
-
return rng.integers(1, high=5, size=shape, dtype=dtype)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
kernel_cache =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def getkernel(func, suffix=""):
|
|
40
|
-
key = func.__name__ + "_" + suffix
|
|
41
|
-
if key not in kernel_cache:
|
|
42
|
-
kernel_cache[key] = wp.Kernel(func=func, key=key)
|
|
43
|
-
return kernel_cache[key]
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def get_select_kernel(dtype):
|
|
47
|
-
def output_select_kernel_fn(
|
|
48
|
-
input: wp.array(dtype=dtype),
|
|
49
|
-
index: int,
|
|
50
|
-
out: wp.array(dtype=dtype),
|
|
51
|
-
):
|
|
52
|
-
out[0] = input[index]
|
|
53
|
-
|
|
54
|
-
return getkernel(output_select_kernel_fn, suffix=dtype.__name__)
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
def
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
def
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
def
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
def
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
def
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
def
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
m
|
|
252
|
-
test.assertSequenceEqual(
|
|
253
|
-
test.assertSequenceEqual(
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
if
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
wp.launch(
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
#
|
|
792
|
-
#
|
|
793
|
-
#
|
|
794
|
-
#
|
|
795
|
-
#
|
|
796
|
-
#
|
|
797
|
-
#
|
|
798
|
-
#
|
|
799
|
-
#
|
|
800
|
-
#
|
|
801
|
-
#
|
|
802
|
-
#
|
|
803
|
-
#
|
|
804
|
-
#
|
|
805
|
-
#
|
|
806
|
-
#
|
|
807
|
-
# )
|
|
808
|
-
#
|
|
809
|
-
#
|
|
810
|
-
#
|
|
811
|
-
#
|
|
812
|
-
#
|
|
813
|
-
#
|
|
814
|
-
#
|
|
815
|
-
#
|
|
816
|
-
#
|
|
817
|
-
#
|
|
818
|
-
#
|
|
819
|
-
#
|
|
820
|
-
#
|
|
821
|
-
#
|
|
822
|
-
#
|
|
823
|
-
#
|
|
824
|
-
#
|
|
825
|
-
#
|
|
826
|
-
#
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
def
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
assert_np_equal(2 * m[
|
|
957
|
-
assert_np_equal(-2 * m[0, 1] * m[
|
|
958
|
-
assert_np_equal(2 * m[
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
#
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
wp.launch(
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
assert_np_equal(2 * m[
|
|
975
|
-
assert_np_equal(-2 * m[
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
assert_np_equal(2 * m[0,
|
|
993
|
-
assert_np_equal(
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
wp.
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
)
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
)
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
)
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
)
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
)
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
m
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
for i in range(
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
add_kernel_test(
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
devices=devices,
|
|
1785
|
-
)
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
devices=devices,
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
TestMat, f"test_py_arithmetic_ops_{dtype.__name__}", test_py_arithmetic_ops, devices=None, dtype=dtype
|
|
1803
|
-
)
|
|
1804
|
-
add_function_test_register_kernel(
|
|
1805
|
-
TestMat, f"test_quat_constructor_{dtype.__name__}", test_quat_constructor, devices=devices, dtype=dtype
|
|
1806
|
-
)
|
|
1807
|
-
add_function_test_register_kernel(
|
|
1808
|
-
TestMat, f"test_inverse_{dtype.__name__}", test_inverse, devices=devices, dtype=dtype
|
|
1809
|
-
)
|
|
1810
|
-
add_function_test_register_kernel(TestMat, f"test_svd_{dtype.__name__}", test_svd, devices=devices, dtype=dtype)
|
|
1811
|
-
add_function_test_register_kernel(TestMat, f"test_qr_{dtype.__name__}", test_qr, devices=devices, dtype=dtype)
|
|
1812
|
-
add_function_test_register_kernel(TestMat, f"test_eig_{dtype.__name__}", test_eig, devices=devices, dtype=dtype)
|
|
1813
|
-
add_function_test_register_kernel(
|
|
1814
|
-
TestMat, f"test_transform_point_{dtype.__name__}", test_transform_point, devices=devices, dtype=dtype
|
|
1815
|
-
)
|
|
1816
|
-
add_function_test_register_kernel(
|
|
1817
|
-
TestMat, f"test_transform_vector_{dtype.__name__}", test_transform_vector, devices=devices, dtype=dtype
|
|
1818
|
-
)
|
|
1819
|
-
add_function_test_register_kernel(
|
|
1820
|
-
TestMat, f"test_determinant_{dtype.__name__}", test_determinant, devices=devices, dtype=dtype
|
|
1821
|
-
)
|
|
1822
|
-
add_function_test_register_kernel(TestMat, f"test_skew_{dtype.__name__}", test_skew, devices=devices, dtype=dtype)
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
if __name__ == "__main__":
|
|
1826
|
-
wp.build.clear_kernel_cache()
|
|
1827
|
-
unittest.main(verbosity=2, failfast=True)
|
|
1
|
+
# Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
|
|
2
|
+
# NVIDIA CORPORATION and its licensors retain all intellectual property
|
|
3
|
+
# and proprietary rights in and to this software, related documentation
|
|
4
|
+
# and any modifications thereto. Any use, reproduction, disclosure or
|
|
5
|
+
# distribution of this software and related documentation without an express
|
|
6
|
+
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
|
7
|
+
|
|
8
|
+
import unittest
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
import warp as wp
|
|
13
|
+
from warp.tests.unittest_utils import *
|
|
14
|
+
|
|
15
|
+
wp.init()
|
|
16
|
+
|
|
17
|
+
np_signed_int_types = [
|
|
18
|
+
np.int8,
|
|
19
|
+
np.int16,
|
|
20
|
+
np.int32,
|
|
21
|
+
np.int64,
|
|
22
|
+
np.byte,
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
np_float_types = [np.float16, np.float32, np.float64]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def randvals(rng, shape, dtype):
|
|
29
|
+
if dtype in np_float_types:
|
|
30
|
+
return rng.standard_normal(size=shape).astype(dtype)
|
|
31
|
+
elif dtype in [np.int8, np.uint8, np.byte, np.ubyte]:
|
|
32
|
+
return rng.integers(1, high=3, size=shape, dtype=dtype)
|
|
33
|
+
return rng.integers(1, high=5, size=shape, dtype=dtype)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
kernel_cache = {}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def getkernel(func, suffix=""):
|
|
40
|
+
key = func.__name__ + "_" + suffix
|
|
41
|
+
if key not in kernel_cache:
|
|
42
|
+
kernel_cache[key] = wp.Kernel(func=func, key=key)
|
|
43
|
+
return kernel_cache[key]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_select_kernel(dtype):
|
|
47
|
+
def output_select_kernel_fn(
|
|
48
|
+
input: wp.array(dtype=dtype),
|
|
49
|
+
index: int,
|
|
50
|
+
out: wp.array(dtype=dtype),
|
|
51
|
+
):
|
|
52
|
+
out[0] = input[index]
|
|
53
|
+
|
|
54
|
+
return getkernel(output_select_kernel_fn, suffix=dtype.__name__)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_anon_constructor_error_shape_keyword_missing(test, device):
|
|
58
|
+
@wp.kernel
|
|
59
|
+
def kernel():
|
|
60
|
+
wp.matrix(1.0, 2.0, 3.0)
|
|
61
|
+
|
|
62
|
+
with test.assertRaisesRegex(
|
|
63
|
+
RuntimeError,
|
|
64
|
+
r"shape keyword must be specified when calling matrix\(\) function$",
|
|
65
|
+
):
|
|
66
|
+
wp.launch(
|
|
67
|
+
kernel,
|
|
68
|
+
dim=1,
|
|
69
|
+
inputs=[],
|
|
70
|
+
device=device,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_anon_constructor_error_dtype_keyword_missing(test, device):
|
|
75
|
+
@wp.kernel
|
|
76
|
+
def kernel():
|
|
77
|
+
wp.matrix(shape=(3, 3))
|
|
78
|
+
|
|
79
|
+
with test.assertRaisesRegex(
|
|
80
|
+
RuntimeError,
|
|
81
|
+
r"matrix\(\) must have dtype as a keyword argument if it has no " r"positional arguments$",
|
|
82
|
+
):
|
|
83
|
+
wp.launch(
|
|
84
|
+
kernel,
|
|
85
|
+
dim=1,
|
|
86
|
+
inputs=[],
|
|
87
|
+
device=device,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_anon_constructor_error_shape_mismatch(test, device):
|
|
92
|
+
@wp.kernel
|
|
93
|
+
def kernel():
|
|
94
|
+
wp.matrix(
|
|
95
|
+
wp.matrix(shape=(1, 2), dtype=float),
|
|
96
|
+
shape=(3, 4),
|
|
97
|
+
dtype=float,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
with test.assertRaisesRegex(
|
|
101
|
+
RuntimeError,
|
|
102
|
+
r"Incompatible matrix sizes for casting copy constructor, " r"\(3, 4\) vs \(1, 2\)$",
|
|
103
|
+
):
|
|
104
|
+
wp.launch(
|
|
105
|
+
kernel,
|
|
106
|
+
dim=1,
|
|
107
|
+
inputs=[],
|
|
108
|
+
device=device,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_anon_constructor_error_invalid_arg_count(test, device):
|
|
113
|
+
@wp.kernel
|
|
114
|
+
def kernel():
|
|
115
|
+
wp.matrix(1.0, 2.0, 3.0, shape=(2, 2), dtype=float)
|
|
116
|
+
|
|
117
|
+
with test.assertRaisesRegex(
|
|
118
|
+
RuntimeError,
|
|
119
|
+
r"Wrong number of arguments for matrix\(\) function, must initialize "
|
|
120
|
+
r"with either a scalar value, or m\*n values$",
|
|
121
|
+
):
|
|
122
|
+
wp.launch(
|
|
123
|
+
kernel,
|
|
124
|
+
dim=1,
|
|
125
|
+
inputs=[],
|
|
126
|
+
device=device,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def test_tpl_constructor_error_incompatible_sizes(test, device):
|
|
131
|
+
@wp.kernel
|
|
132
|
+
def kernel():
|
|
133
|
+
wp.mat33(wp.mat22(1.0, 2.0, 3.0, 4.0))
|
|
134
|
+
|
|
135
|
+
with test.assertRaisesRegex(
|
|
136
|
+
RuntimeError,
|
|
137
|
+
r"Incompatible matrix sizes for casting copy constructor, " r"\(3, 3\) vs \(2, 2\)$",
|
|
138
|
+
):
|
|
139
|
+
wp.launch(
|
|
140
|
+
kernel,
|
|
141
|
+
dim=1,
|
|
142
|
+
inputs=[],
|
|
143
|
+
device=device,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_tpl_constructor_error_invalid_scalar_type(test, device):
|
|
148
|
+
@wp.kernel
|
|
149
|
+
def kernel():
|
|
150
|
+
wp.mat22(1, 2, 3, 4)
|
|
151
|
+
|
|
152
|
+
with test.assertRaisesRegex(
|
|
153
|
+
RuntimeError,
|
|
154
|
+
r"Wrong scalar type for mat 2,2,<class 'warp.types.float32'> constructor$",
|
|
155
|
+
):
|
|
156
|
+
wp.launch(
|
|
157
|
+
kernel,
|
|
158
|
+
dim=1,
|
|
159
|
+
inputs=[],
|
|
160
|
+
device=device,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def test_tpl_constructor_error_invalid_vector_count(test, device):
|
|
165
|
+
@wp.kernel
|
|
166
|
+
def kernel():
|
|
167
|
+
wp.mat22(wp.vec3(1.0, 2.0, 3.0))
|
|
168
|
+
|
|
169
|
+
with test.assertRaisesRegex(
|
|
170
|
+
RuntimeError,
|
|
171
|
+
r"Wrong number of vectors when attempting to construct a matrix " r"with column vectors$",
|
|
172
|
+
):
|
|
173
|
+
wp.launch(
|
|
174
|
+
kernel,
|
|
175
|
+
dim=1,
|
|
176
|
+
inputs=[],
|
|
177
|
+
device=device,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def test_tpl_constructor_error_invalid_vector_shape(test, device):
|
|
182
|
+
@wp.kernel
|
|
183
|
+
def kernel():
|
|
184
|
+
wp.mat22(wp.vec3(1.0, 2.0, 3.0), wp.vec3(4.0, 5.0, 6.0))
|
|
185
|
+
|
|
186
|
+
with test.assertRaisesRegex(
|
|
187
|
+
RuntimeError,
|
|
188
|
+
r"Wrong vector row count when attempting to construct a matrix " r"with column vectors$",
|
|
189
|
+
):
|
|
190
|
+
wp.launch(
|
|
191
|
+
kernel,
|
|
192
|
+
dim=1,
|
|
193
|
+
inputs=[],
|
|
194
|
+
device=device,
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def test_tpl_constructor_error_invalid_arg_count(test, device):
|
|
199
|
+
@wp.kernel
|
|
200
|
+
def kernel():
|
|
201
|
+
wp.mat22(1.0, 2.0, 3.0)
|
|
202
|
+
|
|
203
|
+
with test.assertRaisesRegex(
|
|
204
|
+
RuntimeError,
|
|
205
|
+
r"Wrong number of scalars when attempting to construct a matrix " r"from a list of components$",
|
|
206
|
+
):
|
|
207
|
+
wp.launch(
|
|
208
|
+
kernel,
|
|
209
|
+
dim=1,
|
|
210
|
+
inputs=[],
|
|
211
|
+
device=device,
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_py_arithmetic_ops(test, device, dtype):
|
|
216
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
217
|
+
|
|
218
|
+
def make_mat(*args):
|
|
219
|
+
if wptype in wp.types.int_types:
|
|
220
|
+
# Cast to the correct integer type to simulate wrapping.
|
|
221
|
+
return tuple(tuple(wptype._type_(x).value for x in row) for row in args)
|
|
222
|
+
|
|
223
|
+
return args
|
|
224
|
+
|
|
225
|
+
def make_vec(*args):
|
|
226
|
+
if wptype in wp.types.int_types:
|
|
227
|
+
# Cast to the correct integer type to simulate wrapping.
|
|
228
|
+
return tuple(wptype._type_(x).value for x in args)
|
|
229
|
+
|
|
230
|
+
return args
|
|
231
|
+
|
|
232
|
+
mat_cls = wp.mat((3, 3), wptype)
|
|
233
|
+
vec_cls = wp.vec(3, wptype)
|
|
234
|
+
|
|
235
|
+
m = mat_cls(((-1, 2, 3), (4, -5, 6), (7, 8, -9)))
|
|
236
|
+
test.assertSequenceEqual(+m, make_mat((-1, 2, 3), (4, -5, 6), (7, 8, -9)))
|
|
237
|
+
test.assertSequenceEqual(-m, make_mat((1, -2, -3), (-4, 5, -6), (-7, -8, 9)))
|
|
238
|
+
test.assertSequenceEqual(m + mat_cls((5, 5, 5) * 3), make_mat((4, 7, 8), (9, 0, 11), (12, 13, -4)))
|
|
239
|
+
test.assertSequenceEqual(m - mat_cls((5, 5, 5) * 3), make_mat((-6, -3, -2), (-1, -10, 1), (2, 3, -14)))
|
|
240
|
+
test.assertSequenceEqual(m * vec_cls(5, 5, 5), make_vec(20, 25, 30))
|
|
241
|
+
test.assertSequenceEqual(m @ vec_cls(5, 5, 5), make_vec(20, 25, 30))
|
|
242
|
+
test.assertSequenceEqual(vec_cls(5, 5, 5) * m, make_vec(50, 25, 0))
|
|
243
|
+
test.assertSequenceEqual(vec_cls(5, 5, 5) @ m, make_vec(50, 25, 0))
|
|
244
|
+
|
|
245
|
+
m = mat_cls(((2, 4, 6), (8, 10, 12), (14, 16, 18)))
|
|
246
|
+
test.assertSequenceEqual(m * wptype(2), make_mat((4, 8, 12), (16, 20, 24), (28, 32, 36)))
|
|
247
|
+
test.assertSequenceEqual(wptype(2) * m, make_mat((4, 8, 12), (16, 20, 24), (28, 32, 36)))
|
|
248
|
+
test.assertSequenceEqual(m / wptype(2), make_mat((1, 2, 3), (4, 5, 6), (7, 8, 9)))
|
|
249
|
+
test.assertSequenceEqual(wptype(5040) / m, make_mat((2520, 1260, 840), (630, 504, 420), (360, 315, 280)))
|
|
250
|
+
test.assertSequenceEqual(m * vec_cls(5, 5, 5), make_vec(60, 150, 240))
|
|
251
|
+
test.assertSequenceEqual(m @ vec_cls(5, 5, 5), make_vec(60, 150, 240))
|
|
252
|
+
test.assertSequenceEqual(vec_cls(5, 5, 5) * m, make_vec(120, 150, 180))
|
|
253
|
+
test.assertSequenceEqual(vec_cls(5, 5, 5) @ m, make_vec(120, 150, 180))
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def test_quat_constructor(test, device, dtype, register_kernels=False):
|
|
257
|
+
rng = np.random.default_rng(123)
|
|
258
|
+
|
|
259
|
+
tol = {
|
|
260
|
+
np.float16: 1.0e-3,
|
|
261
|
+
np.float32: 1.0e-6,
|
|
262
|
+
np.float64: 1.0e-8,
|
|
263
|
+
}.get(dtype, 0)
|
|
264
|
+
|
|
265
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
266
|
+
mat44 = wp.types.matrix(shape=(4, 4), dtype=wptype)
|
|
267
|
+
vec4 = wp.types.vector(length=4, dtype=wptype)
|
|
268
|
+
vec3 = wp.types.vector(length=3, dtype=wptype)
|
|
269
|
+
quat = wp.types.quaternion(dtype=wptype)
|
|
270
|
+
|
|
271
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
272
|
+
|
|
273
|
+
def check_mat_quat_constructor(
|
|
274
|
+
p: wp.array(dtype=vec3),
|
|
275
|
+
r: wp.array(dtype=quat),
|
|
276
|
+
s: wp.array(dtype=vec3),
|
|
277
|
+
outcomponents: wp.array(dtype=wptype),
|
|
278
|
+
outcomponents_alt: wp.array(dtype=wptype),
|
|
279
|
+
):
|
|
280
|
+
m = mat44(p[0], r[0], s[0])
|
|
281
|
+
|
|
282
|
+
R = wp.transpose(wp.quat_to_matrix(r[0]))
|
|
283
|
+
c0 = s[0][0] * R[0]
|
|
284
|
+
c1 = s[0][1] * R[1]
|
|
285
|
+
c2 = s[0][2] * R[2]
|
|
286
|
+
m_alt = mat44(
|
|
287
|
+
vec4(c0[0], c0[1], c0[2], wptype(0.0)),
|
|
288
|
+
vec4(c1[0], c1[1], c1[2], wptype(0.0)),
|
|
289
|
+
vec4(c2[0], c2[1], c2[2], wptype(0.0)),
|
|
290
|
+
vec4(p[0][0], p[0][1], p[0][2], wptype(1.0)),
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
idx = 0
|
|
294
|
+
for i in range(4):
|
|
295
|
+
for j in range(4):
|
|
296
|
+
outcomponents[idx] = m[i, j]
|
|
297
|
+
outcomponents_alt[idx] = m_alt[i, j]
|
|
298
|
+
idx = idx + 1
|
|
299
|
+
|
|
300
|
+
kernel = getkernel(check_mat_quat_constructor, suffix=dtype.__name__)
|
|
301
|
+
|
|
302
|
+
if register_kernels:
|
|
303
|
+
return
|
|
304
|
+
|
|
305
|
+
# translation:
|
|
306
|
+
p = wp.array(rng.standard_normal(size=(1, 3)).astype(dtype), dtype=vec3, requires_grad=True, device=device)
|
|
307
|
+
|
|
308
|
+
# generate a normalized quaternion for the rotation:
|
|
309
|
+
r = rng.standard_normal(size=(1, 4))
|
|
310
|
+
r /= np.linalg.norm(r)
|
|
311
|
+
r = wp.array(r.astype(dtype), dtype=quat, requires_grad=True, device=device)
|
|
312
|
+
|
|
313
|
+
# scale:
|
|
314
|
+
s = wp.array(rng.standard_normal(size=(1, 3)).astype(dtype), dtype=vec3, requires_grad=True, device=device)
|
|
315
|
+
|
|
316
|
+
# just going to generate the matrix using the constructor, then
|
|
317
|
+
# more manually, and make sure the values/gradients are the same:
|
|
318
|
+
outcomponents = wp.zeros(4 * 4, dtype=wptype, requires_grad=True, device=device)
|
|
319
|
+
outcomponents_alt = wp.zeros(4 * 4, dtype=wptype, requires_grad=True, device=device)
|
|
320
|
+
wp.launch(kernel, dim=1, inputs=[p, r, s], outputs=[outcomponents, outcomponents_alt], device=device)
|
|
321
|
+
assert_np_equal(outcomponents.numpy(), outcomponents_alt.numpy(), tol=1.0e-6)
|
|
322
|
+
|
|
323
|
+
idx = 0
|
|
324
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
325
|
+
out_alt = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
326
|
+
for _i in range(4):
|
|
327
|
+
for _j in range(4):
|
|
328
|
+
tape = wp.Tape()
|
|
329
|
+
with tape:
|
|
330
|
+
wp.launch(kernel, dim=1, inputs=[p, r, s], outputs=[outcomponents, outcomponents_alt], device=device)
|
|
331
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
332
|
+
wp.launch(
|
|
333
|
+
output_select_kernel, dim=1, inputs=[outcomponents_alt, idx], outputs=[out_alt], device=device
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
tape.backward(loss=out)
|
|
337
|
+
p_grad = 1.0 * tape.gradients[p].numpy()[0]
|
|
338
|
+
r_grad = 1.0 * tape.gradients[r].numpy()[0]
|
|
339
|
+
s_grad = 1.0 * tape.gradients[s].numpy()[0]
|
|
340
|
+
tape.zero()
|
|
341
|
+
|
|
342
|
+
tape.backward(loss=out_alt)
|
|
343
|
+
p_grad_alt = 1.0 * tape.gradients[p].numpy()[0]
|
|
344
|
+
r_grad_alt = 1.0 * tape.gradients[r].numpy()[0]
|
|
345
|
+
s_grad_alt = 1.0 * tape.gradients[s].numpy()[0]
|
|
346
|
+
tape.zero()
|
|
347
|
+
|
|
348
|
+
assert_np_equal(p_grad, p_grad_alt, tol=tol)
|
|
349
|
+
assert_np_equal(r_grad, r_grad_alt, tol=tol)
|
|
350
|
+
assert_np_equal(s_grad, s_grad_alt, tol=tol)
|
|
351
|
+
|
|
352
|
+
idx = idx + 1
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def test_negation(test, device, dtype, register_kernels=False):
|
|
356
|
+
rng = np.random.default_rng(123)
|
|
357
|
+
|
|
358
|
+
tol = {
|
|
359
|
+
np.float16: 1.0e-2,
|
|
360
|
+
np.float32: 1.0e-6,
|
|
361
|
+
np.float64: 1.0e-8,
|
|
362
|
+
}.get(dtype, 0)
|
|
363
|
+
|
|
364
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
365
|
+
mat22 = wp.types.matrix(shape=(2, 2), dtype=wptype)
|
|
366
|
+
mat33 = wp.types.matrix(shape=(3, 3), dtype=wptype)
|
|
367
|
+
mat44 = wp.types.matrix(shape=(4, 4), dtype=wptype)
|
|
368
|
+
mat55 = wp.types.matrix(shape=(5, 5), dtype=wptype)
|
|
369
|
+
|
|
370
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
371
|
+
|
|
372
|
+
def check_mat_negation(
|
|
373
|
+
m2: wp.array(dtype=mat22),
|
|
374
|
+
m3: wp.array(dtype=mat33),
|
|
375
|
+
m4: wp.array(dtype=mat44),
|
|
376
|
+
m5: wp.array(dtype=mat55),
|
|
377
|
+
outcomponents: wp.array(dtype=wptype),
|
|
378
|
+
):
|
|
379
|
+
mat2 = -m2[0]
|
|
380
|
+
mat3 = -m3[0]
|
|
381
|
+
mat4 = -m4[0]
|
|
382
|
+
mat5 = -m5[0]
|
|
383
|
+
|
|
384
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
385
|
+
idx = 0
|
|
386
|
+
for i in range(2):
|
|
387
|
+
for j in range(2):
|
|
388
|
+
outcomponents[idx] = wptype(2) * mat2[i, j]
|
|
389
|
+
idx = idx + 1
|
|
390
|
+
|
|
391
|
+
for i in range(3):
|
|
392
|
+
for j in range(3):
|
|
393
|
+
outcomponents[idx] = wptype(2) * mat3[i, j]
|
|
394
|
+
idx = idx + 1
|
|
395
|
+
|
|
396
|
+
for i in range(4):
|
|
397
|
+
for j in range(4):
|
|
398
|
+
outcomponents[idx] = wptype(2) * mat4[i, j]
|
|
399
|
+
idx = idx + 1
|
|
400
|
+
|
|
401
|
+
for i in range(5):
|
|
402
|
+
for j in range(5):
|
|
403
|
+
outcomponents[idx] = wptype(2) * mat5[i, j]
|
|
404
|
+
idx = idx + 1
|
|
405
|
+
|
|
406
|
+
kernel = getkernel(check_mat_negation, suffix=dtype.__name__)
|
|
407
|
+
|
|
408
|
+
if register_kernels:
|
|
409
|
+
return
|
|
410
|
+
|
|
411
|
+
m2 = wp.array(randvals(rng, [1, 2, 2], dtype), dtype=mat22, requires_grad=True, device=device)
|
|
412
|
+
m3 = wp.array(randvals(rng, [1, 3, 3], dtype), dtype=mat33, requires_grad=True, device=device)
|
|
413
|
+
m4 = wp.array(randvals(rng, [1, 4, 4], dtype), dtype=mat44, requires_grad=True, device=device)
|
|
414
|
+
m5 = wp.array(randvals(rng, [1, 5, 5], dtype), dtype=mat55, requires_grad=True, device=device)
|
|
415
|
+
outcomponents = wp.zeros(2 * 2 + 3 * 3 + 4 * 4 + 5 * 5, dtype=wptype, requires_grad=True, device=device)
|
|
416
|
+
|
|
417
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4, m5], outputs=[outcomponents], device=device)
|
|
418
|
+
|
|
419
|
+
assert_np_equal(outcomponents.numpy()[:4], -2 * m2.numpy().reshape(-1), tol=tol)
|
|
420
|
+
assert_np_equal(outcomponents.numpy()[4:13], -2 * m3.numpy().reshape(-1), tol=tol)
|
|
421
|
+
assert_np_equal(outcomponents.numpy()[13:29], -2 * m4.numpy().reshape(-1), tol=tol)
|
|
422
|
+
assert_np_equal(outcomponents.numpy()[29:54], -2 * m5.numpy().reshape(-1), tol=tol)
|
|
423
|
+
|
|
424
|
+
if dtype in np_float_types:
|
|
425
|
+
idx = 0
|
|
426
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
427
|
+
for dim, input in [(2, m2), (3, m3), (4, m4), (5, m5)]:
|
|
428
|
+
for i in range(dim):
|
|
429
|
+
for j in range(dim):
|
|
430
|
+
tape = wp.Tape()
|
|
431
|
+
with tape:
|
|
432
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4, m5], outputs=[outcomponents], device=device)
|
|
433
|
+
wp.launch(
|
|
434
|
+
output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device
|
|
435
|
+
)
|
|
436
|
+
tape.backward(loss=out)
|
|
437
|
+
expectedresult = np.zeros((dim, dim), dtype=dtype)
|
|
438
|
+
expectedresult[i, j] = -2
|
|
439
|
+
assert_np_equal(tape.gradients[input].numpy()[0], expectedresult)
|
|
440
|
+
tape.zero()
|
|
441
|
+
idx = idx + 1
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def test_subtraction(test, device, dtype, register_kernels=False):
|
|
445
|
+
rng = np.random.default_rng(123)
|
|
446
|
+
|
|
447
|
+
tol = {
|
|
448
|
+
np.float16: 5.0e-3,
|
|
449
|
+
np.float32: 1.0e-6,
|
|
450
|
+
np.float64: 1.0e-8,
|
|
451
|
+
}.get(dtype, 0)
|
|
452
|
+
|
|
453
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
454
|
+
mat22 = wp.types.matrix(shape=(2, 2), dtype=wptype)
|
|
455
|
+
mat33 = wp.types.matrix(shape=(3, 3), dtype=wptype)
|
|
456
|
+
mat44 = wp.types.matrix(shape=(4, 4), dtype=wptype)
|
|
457
|
+
mat55 = wp.types.matrix(shape=(5, 5), dtype=wptype)
|
|
458
|
+
|
|
459
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
460
|
+
|
|
461
|
+
def check_mat_sub(
|
|
462
|
+
s2: wp.array(dtype=mat22),
|
|
463
|
+
s3: wp.array(dtype=mat33),
|
|
464
|
+
s4: wp.array(dtype=mat44),
|
|
465
|
+
s5: wp.array(dtype=mat55),
|
|
466
|
+
v2: wp.array(dtype=mat22),
|
|
467
|
+
v3: wp.array(dtype=mat33),
|
|
468
|
+
v4: wp.array(dtype=mat44),
|
|
469
|
+
v5: wp.array(dtype=mat55),
|
|
470
|
+
outcomponents: wp.array(dtype=wptype),
|
|
471
|
+
):
|
|
472
|
+
v2result = v2[0] - s2[0]
|
|
473
|
+
v3result = v3[0] - s3[0]
|
|
474
|
+
v4result = v4[0] - s4[0]
|
|
475
|
+
v5result = v5[0] - s5[0]
|
|
476
|
+
|
|
477
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
478
|
+
idx = 0
|
|
479
|
+
for i in range(2):
|
|
480
|
+
for j in range(2):
|
|
481
|
+
outcomponents[idx] = wptype(2) * v2result[i, j]
|
|
482
|
+
idx = idx + 1
|
|
483
|
+
|
|
484
|
+
for i in range(3):
|
|
485
|
+
for j in range(3):
|
|
486
|
+
outcomponents[idx] = wptype(2) * v3result[i, j]
|
|
487
|
+
idx = idx + 1
|
|
488
|
+
|
|
489
|
+
for i in range(4):
|
|
490
|
+
for j in range(4):
|
|
491
|
+
outcomponents[idx] = wptype(2) * v4result[i, j]
|
|
492
|
+
idx = idx + 1
|
|
493
|
+
|
|
494
|
+
for i in range(5):
|
|
495
|
+
for j in range(5):
|
|
496
|
+
outcomponents[idx] = wptype(2) * v5result[i, j]
|
|
497
|
+
idx = idx + 1
|
|
498
|
+
|
|
499
|
+
kernel = getkernel(check_mat_sub, suffix=dtype.__name__)
|
|
500
|
+
|
|
501
|
+
if register_kernels:
|
|
502
|
+
return
|
|
503
|
+
|
|
504
|
+
s2 = wp.array(randvals(rng, [1, 2, 2], dtype), dtype=mat22, requires_grad=True, device=device)
|
|
505
|
+
s3 = wp.array(randvals(rng, [1, 3, 3], dtype), dtype=mat33, requires_grad=True, device=device)
|
|
506
|
+
s4 = wp.array(randvals(rng, [1, 4, 4], dtype), dtype=mat44, requires_grad=True, device=device)
|
|
507
|
+
s5 = wp.array(randvals(rng, [1, 5, 5], dtype), dtype=mat55, requires_grad=True, device=device)
|
|
508
|
+
v2 = wp.array(randvals(rng, [1, 2, 2], dtype), dtype=mat22, requires_grad=True, device=device)
|
|
509
|
+
v3 = wp.array(randvals(rng, [1, 3, 3], dtype), dtype=mat33, requires_grad=True, device=device)
|
|
510
|
+
v4 = wp.array(randvals(rng, [1, 4, 4], dtype), dtype=mat44, requires_grad=True, device=device)
|
|
511
|
+
v5 = wp.array(randvals(rng, [1, 5, 5], dtype), dtype=mat55, requires_grad=True, device=device)
|
|
512
|
+
outcomponents = wp.zeros(2 * 2 + 3 * 3 + 4 * 4 + 5 * 5, dtype=wptype, requires_grad=True, device=device)
|
|
513
|
+
|
|
514
|
+
wp.launch(
|
|
515
|
+
kernel,
|
|
516
|
+
dim=1,
|
|
517
|
+
inputs=[
|
|
518
|
+
s2,
|
|
519
|
+
s3,
|
|
520
|
+
s4,
|
|
521
|
+
s5,
|
|
522
|
+
v2,
|
|
523
|
+
v3,
|
|
524
|
+
v4,
|
|
525
|
+
v5,
|
|
526
|
+
],
|
|
527
|
+
outputs=[outcomponents],
|
|
528
|
+
device=device,
|
|
529
|
+
)
|
|
530
|
+
|
|
531
|
+
assert_np_equal(outcomponents.numpy()[:4], 2 * (v2.numpy() - s2.numpy()).reshape(-1), tol=tol)
|
|
532
|
+
assert_np_equal(outcomponents.numpy()[4:13], 2 * (v3.numpy() - s3.numpy()).reshape(-1), tol=tol)
|
|
533
|
+
assert_np_equal(outcomponents.numpy()[13:29], 2 * (v4.numpy() - s4.numpy()).reshape(-1), tol=tol)
|
|
534
|
+
assert_np_equal(outcomponents.numpy()[29:54], 2 * (v5.numpy() - s5.numpy()).reshape(-1), tol=10 * tol)
|
|
535
|
+
|
|
536
|
+
if dtype in np_float_types:
|
|
537
|
+
idx = 0
|
|
538
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
539
|
+
for dim, in1, in2 in [(2, s2, v2), (3, s3, v3), (4, s4, v4), (5, s5, v5)]:
|
|
540
|
+
for i in range(dim):
|
|
541
|
+
for j in range(dim):
|
|
542
|
+
tape = wp.Tape()
|
|
543
|
+
with tape:
|
|
544
|
+
wp.launch(
|
|
545
|
+
kernel,
|
|
546
|
+
dim=1,
|
|
547
|
+
inputs=[
|
|
548
|
+
s2,
|
|
549
|
+
s3,
|
|
550
|
+
s4,
|
|
551
|
+
s5,
|
|
552
|
+
v2,
|
|
553
|
+
v3,
|
|
554
|
+
v4,
|
|
555
|
+
v5,
|
|
556
|
+
],
|
|
557
|
+
outputs=[outcomponents],
|
|
558
|
+
device=device,
|
|
559
|
+
)
|
|
560
|
+
wp.launch(
|
|
561
|
+
output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device
|
|
562
|
+
)
|
|
563
|
+
tape.backward(loss=out)
|
|
564
|
+
expectedresult = np.zeros((dim, dim), dtype=dtype)
|
|
565
|
+
expectedresult[i, j] = 2
|
|
566
|
+
assert_np_equal(tape.gradients[in2].numpy()[0], expectedresult, tol=10 * tol)
|
|
567
|
+
expectedresult[i, j] = -2
|
|
568
|
+
assert_np_equal(tape.gradients[in1].numpy()[0], expectedresult, tol=10 * tol)
|
|
569
|
+
tape.zero()
|
|
570
|
+
|
|
571
|
+
idx = idx + 1
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
def test_determinant(test, device, dtype, register_kernels=False):
|
|
575
|
+
rng = np.random.default_rng(123)
|
|
576
|
+
|
|
577
|
+
tol = {
|
|
578
|
+
np.float16: 5.0e-3,
|
|
579
|
+
np.float32: 1.0e-6,
|
|
580
|
+
np.float64: 1.0e-8,
|
|
581
|
+
}.get(dtype, 0)
|
|
582
|
+
|
|
583
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
584
|
+
mat22 = wp.types.matrix(shape=(2, 2), dtype=wptype)
|
|
585
|
+
mat33 = wp.types.matrix(shape=(3, 3), dtype=wptype)
|
|
586
|
+
mat44 = wp.types.matrix(shape=(4, 4), dtype=wptype)
|
|
587
|
+
|
|
588
|
+
def check_mat_det(
|
|
589
|
+
v2: wp.array(dtype=mat22),
|
|
590
|
+
v3: wp.array(dtype=mat33),
|
|
591
|
+
v4: wp.array(dtype=mat44),
|
|
592
|
+
det2: wp.array(dtype=wptype),
|
|
593
|
+
det3: wp.array(dtype=wptype),
|
|
594
|
+
det4: wp.array(dtype=wptype),
|
|
595
|
+
):
|
|
596
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
597
|
+
det2[0] = wptype(2) * wp.determinant(v2[0])
|
|
598
|
+
det3[0] = wptype(2) * wp.determinant(v3[0])
|
|
599
|
+
det4[0] = wptype(2) * wp.determinant(v4[0])
|
|
600
|
+
|
|
601
|
+
kernel = getkernel(check_mat_det, suffix=dtype.__name__)
|
|
602
|
+
if register_kernels:
|
|
603
|
+
return
|
|
604
|
+
|
|
605
|
+
v2 = wp.array(randvals(rng, [1, 2, 2], dtype), dtype=mat22, requires_grad=True, device=device)
|
|
606
|
+
v3 = wp.array(randvals(rng, [1, 3, 3], dtype), dtype=mat33, requires_grad=True, device=device)
|
|
607
|
+
v4 = wp.array(randvals(rng, [1, 4, 4], dtype), dtype=mat44, requires_grad=True, device=device)
|
|
608
|
+
det2 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
609
|
+
det3 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
610
|
+
det4 = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
611
|
+
|
|
612
|
+
tape = wp.Tape()
|
|
613
|
+
with tape:
|
|
614
|
+
wp.launch(
|
|
615
|
+
kernel,
|
|
616
|
+
dim=1,
|
|
617
|
+
inputs=[
|
|
618
|
+
v2,
|
|
619
|
+
v3,
|
|
620
|
+
v4,
|
|
621
|
+
],
|
|
622
|
+
outputs=[
|
|
623
|
+
det2,
|
|
624
|
+
det3,
|
|
625
|
+
det4,
|
|
626
|
+
],
|
|
627
|
+
device=device,
|
|
628
|
+
)
|
|
629
|
+
|
|
630
|
+
if dtype in np_float_types:
|
|
631
|
+
assert_np_equal(det2.numpy()[0], 2 * np.linalg.det(v2.numpy()[0].astype(np.float64)), tol=100 * tol)
|
|
632
|
+
assert_np_equal(det3.numpy()[0], 2 * np.linalg.det(v3.numpy()[0].astype(np.float64)), tol=100 * tol)
|
|
633
|
+
assert_np_equal(det4.numpy()[0], 2 * np.linalg.det(v4.numpy()[0].astype(np.float64)), tol=420 * tol)
|
|
634
|
+
else:
|
|
635
|
+
assert_np_equal(det2.numpy()[0], 2 * np.around(np.linalg.det(v2.numpy()[0])).astype(int))
|
|
636
|
+
assert_np_equal(det3.numpy()[0], 2 * np.around(np.linalg.det(v3.numpy()[0])).astype(int))
|
|
637
|
+
assert_np_equal(det4.numpy()[0], 2 * np.around(np.linalg.det(v4.numpy()[0])).astype(int))
|
|
638
|
+
|
|
639
|
+
if dtype in np_float_types:
|
|
640
|
+
# determinant derivative formula is annoying so finite differences?
|
|
641
|
+
tape.backward(loss=det2)
|
|
642
|
+
v2grads = 1.0 * tape.gradients[v2].numpy()[0]
|
|
643
|
+
tape.zero()
|
|
644
|
+
|
|
645
|
+
tape.backward(loss=det3)
|
|
646
|
+
v3grads = 1.0 * tape.gradients[v3].numpy()[0]
|
|
647
|
+
tape.zero()
|
|
648
|
+
|
|
649
|
+
tape.backward(loss=det4)
|
|
650
|
+
v4grads = 1.0 * tape.gradients[v4].numpy()[0]
|
|
651
|
+
tape.zero()
|
|
652
|
+
|
|
653
|
+
# finite differences are also annoying hence the large tolerance...
|
|
654
|
+
# absolute nightmare in float16 too innit...
|
|
655
|
+
dx = 0.01 if dtype == np.float16 else 0.0001
|
|
656
|
+
fdtol = 2.0e-1 if dtype == np.float16 else 2.0e-3
|
|
657
|
+
for i in range(2):
|
|
658
|
+
for j in range(2):
|
|
659
|
+
v2test = v2.numpy()
|
|
660
|
+
v2test[0, i, j] += dx
|
|
661
|
+
wp.launch(
|
|
662
|
+
kernel,
|
|
663
|
+
dim=1,
|
|
664
|
+
inputs=[
|
|
665
|
+
wp.array(v2test, dtype=v2.dtype, requires_grad=True, device=device),
|
|
666
|
+
v3,
|
|
667
|
+
v4,
|
|
668
|
+
],
|
|
669
|
+
outputs=[
|
|
670
|
+
det2,
|
|
671
|
+
det3,
|
|
672
|
+
det4,
|
|
673
|
+
],
|
|
674
|
+
device=device,
|
|
675
|
+
)
|
|
676
|
+
dplus = det2.numpy()[0]
|
|
677
|
+
v2test[0, i, j] -= 2.0 * dx
|
|
678
|
+
wp.launch(
|
|
679
|
+
kernel,
|
|
680
|
+
dim=1,
|
|
681
|
+
inputs=[
|
|
682
|
+
wp.array(v2test, dtype=v2.dtype, requires_grad=True, device=device),
|
|
683
|
+
v3,
|
|
684
|
+
v4,
|
|
685
|
+
],
|
|
686
|
+
outputs=[
|
|
687
|
+
det2,
|
|
688
|
+
det3,
|
|
689
|
+
det4,
|
|
690
|
+
],
|
|
691
|
+
device=device,
|
|
692
|
+
)
|
|
693
|
+
dminus = det2.numpy()[0]
|
|
694
|
+
assert_np_equal((dplus - dminus) / (2.0 * dx * dplus), v2grads[i, j] / dplus, tol=fdtol)
|
|
695
|
+
|
|
696
|
+
for i in range(3):
|
|
697
|
+
for j in range(3):
|
|
698
|
+
v3test = v3.numpy()
|
|
699
|
+
v3test[0, i, j] += dx
|
|
700
|
+
wp.launch(
|
|
701
|
+
kernel,
|
|
702
|
+
dim=1,
|
|
703
|
+
inputs=[
|
|
704
|
+
v2,
|
|
705
|
+
wp.array(v3test, dtype=v3.dtype, requires_grad=True, device=device),
|
|
706
|
+
v4,
|
|
707
|
+
],
|
|
708
|
+
outputs=[
|
|
709
|
+
det2,
|
|
710
|
+
det3,
|
|
711
|
+
det4,
|
|
712
|
+
],
|
|
713
|
+
device=device,
|
|
714
|
+
)
|
|
715
|
+
dplus = det3.numpy()[0]
|
|
716
|
+
v3test[0, i, j] -= 2.0 * dx
|
|
717
|
+
wp.launch(
|
|
718
|
+
kernel,
|
|
719
|
+
dim=1,
|
|
720
|
+
inputs=[
|
|
721
|
+
v2,
|
|
722
|
+
wp.array(v3test, dtype=v3.dtype, requires_grad=True, device=device),
|
|
723
|
+
v4,
|
|
724
|
+
],
|
|
725
|
+
outputs=[
|
|
726
|
+
det2,
|
|
727
|
+
det3,
|
|
728
|
+
det4,
|
|
729
|
+
],
|
|
730
|
+
device=device,
|
|
731
|
+
)
|
|
732
|
+
dminus = det3.numpy()[0]
|
|
733
|
+
assert_np_equal((dplus - dminus) / (2.0 * dx * dplus), v3grads[i, j] / dplus, tol=fdtol)
|
|
734
|
+
|
|
735
|
+
for i in range(4):
|
|
736
|
+
for j in range(4):
|
|
737
|
+
v4test = v4.numpy()
|
|
738
|
+
v4test[0, i, j] += dx
|
|
739
|
+
wp.launch(
|
|
740
|
+
kernel,
|
|
741
|
+
dim=1,
|
|
742
|
+
inputs=[
|
|
743
|
+
v2,
|
|
744
|
+
v3,
|
|
745
|
+
wp.array(v4test, dtype=v4.dtype, requires_grad=True, device=device),
|
|
746
|
+
],
|
|
747
|
+
outputs=[
|
|
748
|
+
det2,
|
|
749
|
+
det3,
|
|
750
|
+
det4,
|
|
751
|
+
],
|
|
752
|
+
device=device,
|
|
753
|
+
)
|
|
754
|
+
dplus = det4.numpy()[0]
|
|
755
|
+
v4test[0, i, j] -= 2.0 * dx
|
|
756
|
+
wp.launch(
|
|
757
|
+
kernel,
|
|
758
|
+
dim=1,
|
|
759
|
+
inputs=[
|
|
760
|
+
v2,
|
|
761
|
+
v3,
|
|
762
|
+
wp.array(v4test, dtype=v4.dtype, requires_grad=True, device=device),
|
|
763
|
+
],
|
|
764
|
+
outputs=[
|
|
765
|
+
det2,
|
|
766
|
+
det3,
|
|
767
|
+
det4,
|
|
768
|
+
],
|
|
769
|
+
device=device,
|
|
770
|
+
)
|
|
771
|
+
dminus = det4.numpy()[0]
|
|
772
|
+
assert_np_equal((dplus - dminus) / (2.0 * dx * dplus), v4grads[i, j] / dplus, tol=fdtol)
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
# Unused. Why?
|
|
776
|
+
# def test_get_diag(test, device, dtype, register_kernels=False):
|
|
777
|
+
# tol = {
|
|
778
|
+
# np.float16: 1.0e-3,
|
|
779
|
+
# np.float32: 1.0e-6,
|
|
780
|
+
# np.float64: 1.0e-8,
|
|
781
|
+
# }.get(dtype, 0)
|
|
782
|
+
#
|
|
783
|
+
# wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
784
|
+
# mat55 = wp.types.vector(shape=(5, 5), dtype=wptype)
|
|
785
|
+
#
|
|
786
|
+
# output_select_kernel = get_select_kernel(wptype)
|
|
787
|
+
#
|
|
788
|
+
# def check_mat_diag(
|
|
789
|
+
# m55: wp.array(dtype=mat55),
|
|
790
|
+
# outcomponents: wp.array(dtype=wptype),
|
|
791
|
+
# ):
|
|
792
|
+
# # multiply outputs by 2 so we've got something to backpropagate:
|
|
793
|
+
# vec5result = wptype(2) * wp.get_diag(m55[0])
|
|
794
|
+
#
|
|
795
|
+
# idx = 0
|
|
796
|
+
# for i in range(5):
|
|
797
|
+
# outcomponents[idx] = vec5result[i]
|
|
798
|
+
# idx = idx + 1
|
|
799
|
+
#
|
|
800
|
+
# kernel = getkernel(check_mat_diag, suffix=dtype.__name__)
|
|
801
|
+
#
|
|
802
|
+
# if register_kernels:
|
|
803
|
+
# return
|
|
804
|
+
#
|
|
805
|
+
# m55 = wp.array(randvals((1, 5, 5), dtype), dtype=mat55, requires_grad=True, device=device)
|
|
806
|
+
# outcomponents = wp.zeros(5, dtype=wptype, requires_grad=True, device=device)
|
|
807
|
+
# out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
808
|
+
#
|
|
809
|
+
# wp.launch(kernel, dim=1, inputs=[m55], outputs=[outcomponents], device=device)
|
|
810
|
+
#
|
|
811
|
+
# assert_np_equal(outcomponents.numpy(), 2 * np.diag(m55.numpy()[0]), tol=tol)
|
|
812
|
+
#
|
|
813
|
+
# if dtype in np_float_types:
|
|
814
|
+
# idx = 0
|
|
815
|
+
# for i in range(5):
|
|
816
|
+
# tape = wp.Tape()
|
|
817
|
+
# with tape:
|
|
818
|
+
# wp.launch(kernel, dim=1, inputs=[m55], outputs=[outcomponents], device=device)
|
|
819
|
+
# wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
820
|
+
# tape.backward(loss=out)
|
|
821
|
+
# expectedresult = np.zeros((5, 5), dtype=dtype)
|
|
822
|
+
# expectedresult[i, i] = 2
|
|
823
|
+
# assert_np_equal(tape.gradients[m55].numpy()[0], expectedresult, tol=10 * tol)
|
|
824
|
+
# tape.zero()
|
|
825
|
+
#
|
|
826
|
+
# idx = idx + 1
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
def test_inverse(test, device, dtype, register_kernels=False):
|
|
830
|
+
rng = np.random.default_rng(123)
|
|
831
|
+
|
|
832
|
+
tol = {
|
|
833
|
+
np.float16: 5.0e-2,
|
|
834
|
+
np.float32: 1.0e-5,
|
|
835
|
+
np.float64: 1.0e-8,
|
|
836
|
+
}.get(dtype, 0)
|
|
837
|
+
|
|
838
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
839
|
+
mat22 = wp.types.matrix(shape=(2, 2), dtype=wptype)
|
|
840
|
+
mat33 = wp.types.matrix(shape=(3, 3), dtype=wptype)
|
|
841
|
+
mat44 = wp.types.matrix(shape=(4, 4), dtype=wptype)
|
|
842
|
+
|
|
843
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
844
|
+
|
|
845
|
+
def check_mat_inverse(
|
|
846
|
+
m2: wp.array(dtype=mat22),
|
|
847
|
+
m3: wp.array(dtype=mat33),
|
|
848
|
+
m4: wp.array(dtype=mat44),
|
|
849
|
+
outcomponents: wp.array(dtype=wptype),
|
|
850
|
+
):
|
|
851
|
+
m2result = wp.inverse(m2[0])
|
|
852
|
+
m3result = wp.inverse(m3[0])
|
|
853
|
+
m4result = wp.inverse(m4[0])
|
|
854
|
+
|
|
855
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
856
|
+
idx = 0
|
|
857
|
+
for i in range(2):
|
|
858
|
+
for j in range(2):
|
|
859
|
+
outcomponents[idx] = wptype(2) * m2result[i, j]
|
|
860
|
+
idx = idx + 1
|
|
861
|
+
|
|
862
|
+
for i in range(3):
|
|
863
|
+
for j in range(3):
|
|
864
|
+
outcomponents[idx] = wptype(2) * m3result[i, j]
|
|
865
|
+
idx = idx + 1
|
|
866
|
+
|
|
867
|
+
for i in range(4):
|
|
868
|
+
for j in range(4):
|
|
869
|
+
outcomponents[idx] = wptype(2) * m4result[i, j]
|
|
870
|
+
idx = idx + 1
|
|
871
|
+
|
|
872
|
+
kernel = getkernel(check_mat_inverse, suffix=dtype.__name__)
|
|
873
|
+
|
|
874
|
+
if register_kernels:
|
|
875
|
+
return
|
|
876
|
+
|
|
877
|
+
m2 = wp.array(
|
|
878
|
+
2 * (randvals(rng, [1, 2, 2], dtype) + 0.2 * np.eye(2)), dtype=mat22, requires_grad=True, device=device
|
|
879
|
+
)
|
|
880
|
+
m3 = wp.array(
|
|
881
|
+
2 * (randvals(rng, [1, 3, 3], dtype) + 0.2 * np.eye(3)), dtype=mat33, requires_grad=True, device=device
|
|
882
|
+
)
|
|
883
|
+
m4 = wp.array(
|
|
884
|
+
2 * (randvals(rng, [1, 4, 4], dtype) + 0.2 * np.eye(4)), dtype=mat44, requires_grad=True, device=device
|
|
885
|
+
)
|
|
886
|
+
|
|
887
|
+
outcomponents = wp.zeros(2 * 2 + 3 * 3 + 4 * 4, dtype=wptype, requires_grad=True, device=device)
|
|
888
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
889
|
+
|
|
890
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4], outputs=[outcomponents], device=device)
|
|
891
|
+
|
|
892
|
+
assert_np_equal(outcomponents.numpy()[:4], 2 * np.linalg.inv(m2.numpy()[0].astype(np.float64)), tol=tol)
|
|
893
|
+
assert_np_equal(outcomponents.numpy()[4:13], 2 * np.linalg.inv(m3.numpy()[0].astype(np.float64)), tol=5 * tol)
|
|
894
|
+
assert_np_equal(outcomponents.numpy()[13:], 2 * np.linalg.inv(m4.numpy()[0].astype(np.float64)), tol=5 * tol)
|
|
895
|
+
|
|
896
|
+
if dtype in np_float_types:
|
|
897
|
+
# check gradients:
|
|
898
|
+
idx = 0
|
|
899
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
900
|
+
for dim, input in [(2, m2), (3, m3), (4, m4)]:
|
|
901
|
+
minv = np.linalg.inv(input.numpy()[0].astype(np.float64))
|
|
902
|
+
for i in range(dim):
|
|
903
|
+
for j in range(dim):
|
|
904
|
+
tape = wp.Tape()
|
|
905
|
+
with tape:
|
|
906
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4], outputs=[outcomponents], device=device)
|
|
907
|
+
wp.launch(
|
|
908
|
+
output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device
|
|
909
|
+
)
|
|
910
|
+
tape.backward(loss=out)
|
|
911
|
+
d = np.zeros((dim, dim))
|
|
912
|
+
d[j, i] = 2
|
|
913
|
+
assert_np_equal(
|
|
914
|
+
tape.gradients[input].numpy()[0], -np.matmul(minv, np.matmul(d, minv)).T, tol=10 * tol
|
|
915
|
+
)
|
|
916
|
+
tape.zero()
|
|
917
|
+
|
|
918
|
+
idx = idx + 1
|
|
919
|
+
|
|
920
|
+
# let's check 2x2 using different formulae just for (in)sanity's sake:
|
|
921
|
+
m = m2.numpy()[0]
|
|
922
|
+
|
|
923
|
+
det = m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]
|
|
924
|
+
expected = 2 * np.array([[m[1, 1], -m[0, 1]], [-m[1, 0], m[0, 0]]], dtype=dtype) / det
|
|
925
|
+
assert_np_equal(expected, outcomponents.numpy()[:4], tol=tol)
|
|
926
|
+
|
|
927
|
+
# 0,0 component is this:
|
|
928
|
+
# 2 * m[1,1] / (m[0,0]*m[1,1] - m[1,0] * m[0,1])
|
|
929
|
+
assert_np_equal(2 * m[1, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]), outcomponents.numpy()[0], tol=tol)
|
|
930
|
+
|
|
931
|
+
tape = wp.Tape()
|
|
932
|
+
with tape:
|
|
933
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4], outputs=[outcomponents], device=device)
|
|
934
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, 0], outputs=[out], device=device)
|
|
935
|
+
|
|
936
|
+
if dtype in np_float_types:
|
|
937
|
+
tape.backward(loss=out)
|
|
938
|
+
g = tape.gradients[m2].numpy()[0]
|
|
939
|
+
assert_np_equal(-2 * m[1, 1] * m[1, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 0], tol=tol)
|
|
940
|
+
assert_np_equal(2 * m[1, 1] * m[0, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 0], tol=tol)
|
|
941
|
+
assert_np_equal(-2 * m[0, 1] * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 1], tol=tol)
|
|
942
|
+
assert_np_equal(2 * m[1, 1] * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 1], tol=tol)
|
|
943
|
+
tape.zero()
|
|
944
|
+
|
|
945
|
+
# 0,1 component is this:
|
|
946
|
+
# -2 * m[0,1] / (m[0,0]*m[1,1] - m[1,0] * m[0,1])
|
|
947
|
+
assert_np_equal(-2 * m[0, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]), outcomponents.numpy()[1], tol=tol)
|
|
948
|
+
|
|
949
|
+
tape = wp.Tape()
|
|
950
|
+
with tape:
|
|
951
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4], outputs=[outcomponents], device=device)
|
|
952
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, 1], outputs=[out], device=device)
|
|
953
|
+
if dtype in np_float_types:
|
|
954
|
+
tape.backward(loss=out)
|
|
955
|
+
g = tape.gradients[m2].numpy()[0]
|
|
956
|
+
assert_np_equal(2 * m[0, 1] * m[1, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 0], tol=tol)
|
|
957
|
+
assert_np_equal(-2 * m[0, 1] * m[0, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 0], tol=tol)
|
|
958
|
+
assert_np_equal(2 * m[0, 0] * m[0, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 1], tol=tol)
|
|
959
|
+
assert_np_equal(-2 * m[1, 1] * m[0, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 1], tol=tol)
|
|
960
|
+
tape.zero()
|
|
961
|
+
|
|
962
|
+
# 1,0 component is this:
|
|
963
|
+
# -2 * m[1,0] / (m[0,0]*m[1,1] - m[1,0] * m[0,1])
|
|
964
|
+
assert_np_equal(-2 * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]), outcomponents.numpy()[2], tol=tol)
|
|
965
|
+
|
|
966
|
+
tape = wp.Tape()
|
|
967
|
+
with tape:
|
|
968
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4], outputs=[outcomponents], device=device)
|
|
969
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, 2], outputs=[out], device=device)
|
|
970
|
+
|
|
971
|
+
if dtype in np_float_types:
|
|
972
|
+
tape.backward(loss=out)
|
|
973
|
+
g = tape.gradients[m2].numpy()[0]
|
|
974
|
+
assert_np_equal(2 * m[1, 1] * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 0], tol=tol)
|
|
975
|
+
assert_np_equal(-2 * m[0, 0] * m[1, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 0], tol=tol)
|
|
976
|
+
assert_np_equal(2 * m[0, 0] * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 1], tol=tol)
|
|
977
|
+
assert_np_equal(-2 * m[1, 0] * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 1], tol=tol)
|
|
978
|
+
tape.zero()
|
|
979
|
+
|
|
980
|
+
# 1,1 component is this:
|
|
981
|
+
# 2 * m[0,0] / (m[0,0]*m[1,1] - m[1,0] * m[0,1])
|
|
982
|
+
assert_np_equal(2 * m[0, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]), outcomponents.numpy()[3], tol=tol)
|
|
983
|
+
|
|
984
|
+
tape = wp.Tape()
|
|
985
|
+
with tape:
|
|
986
|
+
wp.launch(kernel, dim=1, inputs=[m2, m3, m4], outputs=[outcomponents], device=device)
|
|
987
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, 3], outputs=[out], device=device)
|
|
988
|
+
|
|
989
|
+
if dtype in np_float_types:
|
|
990
|
+
tape.backward(loss=out)
|
|
991
|
+
g = tape.gradients[m2].numpy()[0]
|
|
992
|
+
assert_np_equal(-2 * m[0, 1] * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 0], tol=tol)
|
|
993
|
+
assert_np_equal(2 * m[0, 0] * m[0, 1] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 0], tol=tol)
|
|
994
|
+
assert_np_equal(2 * m[0, 0] * m[1, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[0, 1], tol=tol)
|
|
995
|
+
assert_np_equal(-2 * m[0, 0] * m[0, 0] / (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]) ** 2, g[1, 1], tol=tol)
|
|
996
|
+
tape.zero()
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
def test_svd(test, device, dtype, register_kernels=False):
|
|
1000
|
+
rng = np.random.default_rng(123)
|
|
1001
|
+
|
|
1002
|
+
tol = {
|
|
1003
|
+
np.float16: 1.0e-3,
|
|
1004
|
+
np.float32: 1.0e-6,
|
|
1005
|
+
np.float64: 1.0e-6,
|
|
1006
|
+
}.get(dtype, 0)
|
|
1007
|
+
|
|
1008
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
1009
|
+
vec3 = wp.types.vector(length=3, dtype=wptype)
|
|
1010
|
+
mat33 = wp.types.matrix(shape=(3, 3), dtype=wptype)
|
|
1011
|
+
|
|
1012
|
+
def check_mat_svd(
|
|
1013
|
+
m3: wp.array(dtype=mat33),
|
|
1014
|
+
Uout: wp.array(dtype=mat33),
|
|
1015
|
+
sigmaout: wp.array(dtype=vec3),
|
|
1016
|
+
Vout: wp.array(dtype=mat33),
|
|
1017
|
+
outcomponents: wp.array(dtype=wptype),
|
|
1018
|
+
):
|
|
1019
|
+
U = mat33()
|
|
1020
|
+
sigma = vec3()
|
|
1021
|
+
V = mat33()
|
|
1022
|
+
|
|
1023
|
+
wp.svd3(m3[0], U, sigma, V)
|
|
1024
|
+
|
|
1025
|
+
Uout[0] = U
|
|
1026
|
+
sigmaout[0] = sigma
|
|
1027
|
+
Vout[0] = V
|
|
1028
|
+
|
|
1029
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
1030
|
+
idx = 0
|
|
1031
|
+
for i in range(3):
|
|
1032
|
+
for j in range(3):
|
|
1033
|
+
outcomponents[idx] = wptype(2) * U[i, j]
|
|
1034
|
+
idx = idx + 1
|
|
1035
|
+
|
|
1036
|
+
for i in range(3):
|
|
1037
|
+
outcomponents[idx] = wptype(2) * sigma[i]
|
|
1038
|
+
idx = idx + 1
|
|
1039
|
+
|
|
1040
|
+
for i in range(3):
|
|
1041
|
+
for j in range(3):
|
|
1042
|
+
outcomponents[idx] = wptype(2) * V[i, j]
|
|
1043
|
+
idx = idx + 1
|
|
1044
|
+
|
|
1045
|
+
kernel = getkernel(check_mat_svd, suffix=dtype.__name__)
|
|
1046
|
+
|
|
1047
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
1048
|
+
|
|
1049
|
+
if register_kernels:
|
|
1050
|
+
return
|
|
1051
|
+
|
|
1052
|
+
m3 = wp.array(randvals(rng, [1, 3, 3], dtype) + np.eye(3), dtype=mat33, requires_grad=True, device=device)
|
|
1053
|
+
|
|
1054
|
+
outcomponents = wp.zeros(2 * 3 * 3 + 3, dtype=wptype, requires_grad=True, device=device)
|
|
1055
|
+
Uout = wp.zeros(1, dtype=mat33, requires_grad=True, device=device)
|
|
1056
|
+
sigmaout = wp.zeros(1, dtype=vec3, requires_grad=True, device=device)
|
|
1057
|
+
Vout = wp.zeros(1, dtype=mat33, requires_grad=True, device=device)
|
|
1058
|
+
|
|
1059
|
+
wp.launch(kernel, dim=1, inputs=[m3], outputs=[Uout, sigmaout, Vout, outcomponents], device=device)
|
|
1060
|
+
|
|
1061
|
+
Uout_np = Uout.numpy()[0].astype(np.float64)
|
|
1062
|
+
sigmaout_np = np.diag(sigmaout.numpy()[0].astype(np.float64))
|
|
1063
|
+
Vout_np = Vout.numpy()[0].astype(np.float64)
|
|
1064
|
+
|
|
1065
|
+
assert_np_equal(
|
|
1066
|
+
np.matmul(Uout_np, np.matmul(sigmaout_np, Vout_np.T)), m3.numpy()[0].astype(np.float64), tol=30 * tol
|
|
1067
|
+
)
|
|
1068
|
+
|
|
1069
|
+
if dtype == np.float16:
|
|
1070
|
+
# I'm not even going to bother testing the gradients for float16
|
|
1071
|
+
# because the rounding errors are terrible...
|
|
1072
|
+
return
|
|
1073
|
+
|
|
1074
|
+
# check gradients:
|
|
1075
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
1076
|
+
idx = 0
|
|
1077
|
+
for idx in range(3 * 3 + 3 + 3 * 3):
|
|
1078
|
+
tape = wp.Tape()
|
|
1079
|
+
with tape:
|
|
1080
|
+
wp.launch(kernel, dim=1, inputs=[m3], outputs=[Uout, sigmaout, Vout, outcomponents], device=device)
|
|
1081
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1082
|
+
tape.backward(out)
|
|
1083
|
+
m3grads = 1.0 * tape.gradients[m3].numpy()[0]
|
|
1084
|
+
|
|
1085
|
+
tape.zero()
|
|
1086
|
+
|
|
1087
|
+
dx = 0.0001
|
|
1088
|
+
fdtol = 5.0e-4 if dtype == np.float64 else 2.0e-2
|
|
1089
|
+
for ii in range(3):
|
|
1090
|
+
for jj in range(3):
|
|
1091
|
+
m3test = 1.0 * m3.numpy()
|
|
1092
|
+
m3test[0, ii, jj] += dx
|
|
1093
|
+
wp.launch(
|
|
1094
|
+
kernel,
|
|
1095
|
+
dim=1,
|
|
1096
|
+
inputs=[wp.array(m3test, dtype=mat33, device=device)],
|
|
1097
|
+
outputs=[Uout, sigmaout, Vout, outcomponents],
|
|
1098
|
+
device=device,
|
|
1099
|
+
)
|
|
1100
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1101
|
+
plusval = out.numpy()[0]
|
|
1102
|
+
|
|
1103
|
+
m3test = 1.0 * m3.numpy()
|
|
1104
|
+
m3test[0, ii, jj] -= dx
|
|
1105
|
+
wp.launch(
|
|
1106
|
+
kernel,
|
|
1107
|
+
dim=1,
|
|
1108
|
+
inputs=[wp.array(m3test, dtype=mat33, device=device)],
|
|
1109
|
+
outputs=[Uout, sigmaout, Vout, outcomponents],
|
|
1110
|
+
device=device,
|
|
1111
|
+
)
|
|
1112
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1113
|
+
minusval = out.numpy()[0]
|
|
1114
|
+
|
|
1115
|
+
assert_np_equal((plusval - minusval) / (2 * dx), m3grads[ii, jj], tol=fdtol)
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
def test_qr(test, device, dtype, register_kernels=False):
|
|
1119
|
+
rng = np.random.default_rng(123)
|
|
1120
|
+
|
|
1121
|
+
tol = {
|
|
1122
|
+
np.float16: 2.0e-3,
|
|
1123
|
+
np.float32: 1.0e-6,
|
|
1124
|
+
np.float64: 1.0e-6,
|
|
1125
|
+
}.get(dtype, 0)
|
|
1126
|
+
|
|
1127
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
1128
|
+
mat33 = wp.types.matrix(shape=(3, 3), dtype=wptype)
|
|
1129
|
+
|
|
1130
|
+
def check_mat_qr(
|
|
1131
|
+
m3: wp.array(dtype=mat33),
|
|
1132
|
+
Qout: wp.array(dtype=mat33),
|
|
1133
|
+
Rout: wp.array(dtype=mat33),
|
|
1134
|
+
outcomponents: wp.array(dtype=wptype),
|
|
1135
|
+
):
|
|
1136
|
+
Q = mat33()
|
|
1137
|
+
R = mat33()
|
|
1138
|
+
|
|
1139
|
+
wp.qr3(m3[0], Q, R)
|
|
1140
|
+
|
|
1141
|
+
Qout[0] = Q
|
|
1142
|
+
Rout[0] = R
|
|
1143
|
+
|
|
1144
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
1145
|
+
idx = 0
|
|
1146
|
+
for i in range(3):
|
|
1147
|
+
for j in range(3):
|
|
1148
|
+
outcomponents[idx] = wptype(2) * Q[i, j]
|
|
1149
|
+
idx = idx + 1
|
|
1150
|
+
|
|
1151
|
+
for i in range(3):
|
|
1152
|
+
for j in range(3):
|
|
1153
|
+
outcomponents[idx] = wptype(2) * R[i, j]
|
|
1154
|
+
idx = idx + 1
|
|
1155
|
+
|
|
1156
|
+
kernel = getkernel(check_mat_qr, suffix=dtype.__name__)
|
|
1157
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
1158
|
+
|
|
1159
|
+
if register_kernels:
|
|
1160
|
+
return
|
|
1161
|
+
|
|
1162
|
+
m3 = wp.array(0.5 * (randvals(rng, [1, 3, 3], dtype) + np.eye(3)), dtype=mat33, requires_grad=True, device=device)
|
|
1163
|
+
|
|
1164
|
+
outcomponents = wp.zeros(2 * 3 * 3, dtype=wptype, requires_grad=True, device=device)
|
|
1165
|
+
Qout = wp.zeros(1, dtype=mat33, requires_grad=True, device=device)
|
|
1166
|
+
Rout = wp.zeros(1, dtype=mat33, requires_grad=True, device=device)
|
|
1167
|
+
|
|
1168
|
+
wp.launch(kernel, dim=1, inputs=[m3], outputs=[Qout, Rout, outcomponents], device=device)
|
|
1169
|
+
|
|
1170
|
+
Qout_np = Qout.numpy()[0].astype(np.float64)
|
|
1171
|
+
Rout_np = Rout.numpy()[0].astype(np.float64)
|
|
1172
|
+
|
|
1173
|
+
# check it's actually a q and an r:
|
|
1174
|
+
assert_np_equal(np.matmul(Qout_np.T, Qout_np), np.eye(3, dtype=np.float64), tol=tol)
|
|
1175
|
+
assert_np_equal(Rout_np[1, [0]], np.zeros(1, dtype=np.float64), tol=tol)
|
|
1176
|
+
assert_np_equal(Rout_np[2, [0, 1]], np.zeros(2, dtype=np.float64), tol=tol)
|
|
1177
|
+
|
|
1178
|
+
# check it's a factorization:
|
|
1179
|
+
assert_np_equal(np.matmul(Qout_np, Rout_np), m3.numpy()[0].astype(np.float64), tol=30 * tol)
|
|
1180
|
+
|
|
1181
|
+
if dtype == np.float16:
|
|
1182
|
+
# I'm not even going to bother testing the gradients for float16
|
|
1183
|
+
# because the rounding errors are terrible...
|
|
1184
|
+
return
|
|
1185
|
+
|
|
1186
|
+
# check gradients:
|
|
1187
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
1188
|
+
idx = 0
|
|
1189
|
+
for idx in range(len(outcomponents)):
|
|
1190
|
+
tape = wp.Tape()
|
|
1191
|
+
with tape:
|
|
1192
|
+
wp.launch(kernel, dim=1, inputs=[m3], outputs=[Qout, Rout, outcomponents], device=device)
|
|
1193
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1194
|
+
tape.backward(out)
|
|
1195
|
+
m3grads = 1.0 * tape.gradients[m3].numpy()[0]
|
|
1196
|
+
|
|
1197
|
+
tape.zero()
|
|
1198
|
+
|
|
1199
|
+
dx = 0.0001
|
|
1200
|
+
fdtol = 5.0e-4 if dtype == np.float64 else 2.0e-2
|
|
1201
|
+
for ii in range(3):
|
|
1202
|
+
for jj in range(3):
|
|
1203
|
+
m3test = 1.0 * m3.numpy()
|
|
1204
|
+
m3test[0, ii, jj] += dx
|
|
1205
|
+
wp.launch(
|
|
1206
|
+
kernel,
|
|
1207
|
+
dim=1,
|
|
1208
|
+
inputs=[wp.array(m3test, dtype=mat33, device=device)],
|
|
1209
|
+
outputs=[Qout, Rout, outcomponents],
|
|
1210
|
+
device=device,
|
|
1211
|
+
)
|
|
1212
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1213
|
+
plusval = out.numpy()[0]
|
|
1214
|
+
|
|
1215
|
+
m3test = 1.0 * m3.numpy()
|
|
1216
|
+
m3test[0, ii, jj] -= dx
|
|
1217
|
+
wp.launch(
|
|
1218
|
+
kernel,
|
|
1219
|
+
dim=1,
|
|
1220
|
+
inputs=[wp.array(m3test, dtype=mat33, device=device)],
|
|
1221
|
+
outputs=[Qout, Rout, outcomponents],
|
|
1222
|
+
device=device,
|
|
1223
|
+
)
|
|
1224
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1225
|
+
minusval = out.numpy()[0]
|
|
1226
|
+
|
|
1227
|
+
assert_np_equal((plusval - minusval) / (2 * dx), m3grads[ii, jj], tol=fdtol)
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
def test_eig(test, device, dtype, register_kernels=False):
|
|
1231
|
+
rng = np.random.default_rng(123)
|
|
1232
|
+
|
|
1233
|
+
tol = {
|
|
1234
|
+
np.float16: 4.0e-2,
|
|
1235
|
+
np.float32: 1.0e-5,
|
|
1236
|
+
np.float64: 1.0e-5,
|
|
1237
|
+
}.get(dtype, 0)
|
|
1238
|
+
|
|
1239
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
1240
|
+
vec3 = wp.types.vector(length=3, dtype=wptype)
|
|
1241
|
+
mat33 = wp.types.matrix(shape=(3, 3), dtype=wptype)
|
|
1242
|
+
|
|
1243
|
+
def check_mat_eig(
|
|
1244
|
+
m3: wp.array(dtype=mat33),
|
|
1245
|
+
Qout: wp.array(dtype=mat33),
|
|
1246
|
+
dout: wp.array(dtype=vec3),
|
|
1247
|
+
outcomponents: wp.array(dtype=wptype),
|
|
1248
|
+
):
|
|
1249
|
+
Q = mat33()
|
|
1250
|
+
d = vec3()
|
|
1251
|
+
|
|
1252
|
+
wp.eig3(m3[0] + wp.transpose(m3[0]), Q, d)
|
|
1253
|
+
|
|
1254
|
+
Qout[0] = Q
|
|
1255
|
+
dout[0] = d
|
|
1256
|
+
|
|
1257
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
1258
|
+
idx = 0
|
|
1259
|
+
for i in range(3):
|
|
1260
|
+
for j in range(3):
|
|
1261
|
+
outcomponents[idx] = wptype(2) * Q[i, j]
|
|
1262
|
+
idx = idx + 1
|
|
1263
|
+
|
|
1264
|
+
for i in range(3):
|
|
1265
|
+
outcomponents[idx] = wptype(2) * d[i]
|
|
1266
|
+
idx = idx + 1
|
|
1267
|
+
|
|
1268
|
+
kernel = getkernel(check_mat_eig, suffix=dtype.__name__)
|
|
1269
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
1270
|
+
|
|
1271
|
+
if register_kernels:
|
|
1272
|
+
return
|
|
1273
|
+
|
|
1274
|
+
m3_np = randvals(rng, [1, 3, 3], dtype) + np.eye(3, dtype=dtype)
|
|
1275
|
+
m3 = wp.array(m3_np, dtype=mat33, requires_grad=True, device=device)
|
|
1276
|
+
|
|
1277
|
+
outcomponents = wp.zeros(3 * 3 + 3, dtype=wptype, requires_grad=True, device=device)
|
|
1278
|
+
Qout = wp.zeros(1, dtype=mat33, requires_grad=True, device=device)
|
|
1279
|
+
dout = wp.zeros(1, dtype=vec3, requires_grad=True, device=device)
|
|
1280
|
+
|
|
1281
|
+
wp.launch(kernel, dim=1, inputs=[m3], outputs=[Qout, dout, outcomponents], device=device)
|
|
1282
|
+
|
|
1283
|
+
Qout_np = Qout.numpy()[0].astype(np.float64)
|
|
1284
|
+
dout_np = dout.numpy()[0].astype(np.float64)
|
|
1285
|
+
Dout_np = np.diag(dout_np)
|
|
1286
|
+
|
|
1287
|
+
# check Q is orthogonal:
|
|
1288
|
+
assert_np_equal(np.matmul(Qout_np.T, Qout_np), np.eye(3), tol=tol)
|
|
1289
|
+
|
|
1290
|
+
# check Q contains eigenvectors:
|
|
1291
|
+
assert_np_equal(np.matmul(Qout_np, np.matmul(Dout_np, Qout_np.T)), (m3_np[0] + m3_np[0].transpose()), tol=tol)
|
|
1292
|
+
|
|
1293
|
+
if dtype == np.float16:
|
|
1294
|
+
# I'm not even going to bother testing the gradients for float16
|
|
1295
|
+
# because the rounding errors are terrible...
|
|
1296
|
+
return
|
|
1297
|
+
|
|
1298
|
+
# check gradients:
|
|
1299
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
1300
|
+
idx = 0
|
|
1301
|
+
for idx in range(len(outcomponents)):
|
|
1302
|
+
tape = wp.Tape()
|
|
1303
|
+
with tape:
|
|
1304
|
+
wp.launch(kernel, dim=1, inputs=[m3], outputs=[Qout, dout, outcomponents], device=device)
|
|
1305
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1306
|
+
tape.backward(out)
|
|
1307
|
+
m3grads = 1.0 * tape.gradients[m3].numpy()[0]
|
|
1308
|
+
|
|
1309
|
+
tape.zero()
|
|
1310
|
+
|
|
1311
|
+
dx = 0.0001
|
|
1312
|
+
fdtol = 5.0e-4 if dtype == np.float64 else 2.0e-2
|
|
1313
|
+
for ii in range(3):
|
|
1314
|
+
for jj in range(3):
|
|
1315
|
+
m3test = 1.0 * m3.numpy()
|
|
1316
|
+
m3test[0, ii, jj] += dx
|
|
1317
|
+
wp.launch(
|
|
1318
|
+
kernel,
|
|
1319
|
+
dim=1,
|
|
1320
|
+
inputs=[wp.array(m3test, dtype=mat33, device=device)],
|
|
1321
|
+
outputs=[Qout, dout, outcomponents],
|
|
1322
|
+
device=device,
|
|
1323
|
+
)
|
|
1324
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1325
|
+
plusval = out.numpy()[0]
|
|
1326
|
+
|
|
1327
|
+
m3test = 1.0 * m3.numpy()
|
|
1328
|
+
m3test[0, ii, jj] -= dx
|
|
1329
|
+
wp.launch(
|
|
1330
|
+
kernel,
|
|
1331
|
+
dim=1,
|
|
1332
|
+
inputs=[wp.array(m3test, dtype=mat33, device=device)],
|
|
1333
|
+
outputs=[Qout, dout, outcomponents],
|
|
1334
|
+
device=device,
|
|
1335
|
+
)
|
|
1336
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1337
|
+
minusval = out.numpy()[0]
|
|
1338
|
+
|
|
1339
|
+
assert_np_equal((plusval - minusval) / (2 * dx), m3grads[ii, jj], tol=fdtol)
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
def test_skew(test, device, dtype, register_kernels=False):
|
|
1343
|
+
rng = np.random.default_rng(123)
|
|
1344
|
+
|
|
1345
|
+
tol = {
|
|
1346
|
+
np.float16: 1.0e-3,
|
|
1347
|
+
np.float32: 1.0e-6,
|
|
1348
|
+
np.float64: 1.0e-8,
|
|
1349
|
+
}.get(dtype, 0)
|
|
1350
|
+
|
|
1351
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
1352
|
+
vec3 = wp.types.vector(length=3, dtype=wptype)
|
|
1353
|
+
|
|
1354
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
1355
|
+
|
|
1356
|
+
def check_mat_skew(
|
|
1357
|
+
v3: wp.array(dtype=vec3),
|
|
1358
|
+
outcomponents: wp.array(dtype=wptype),
|
|
1359
|
+
):
|
|
1360
|
+
m3result = wp.skew(v3[0])
|
|
1361
|
+
|
|
1362
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
1363
|
+
idx = 0
|
|
1364
|
+
for i in range(3):
|
|
1365
|
+
for j in range(3):
|
|
1366
|
+
outcomponents[idx] = wptype(2) * m3result[i, j]
|
|
1367
|
+
idx = idx + 1
|
|
1368
|
+
|
|
1369
|
+
kernel = getkernel(check_mat_skew, suffix=dtype.__name__)
|
|
1370
|
+
|
|
1371
|
+
if register_kernels:
|
|
1372
|
+
return
|
|
1373
|
+
|
|
1374
|
+
v3 = wp.array(randvals(rng, [1, 3], dtype), dtype=vec3, requires_grad=True, device=device)
|
|
1375
|
+
|
|
1376
|
+
outcomponents = wp.zeros(3 * 3, dtype=wptype, requires_grad=True, device=device)
|
|
1377
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
1378
|
+
|
|
1379
|
+
wp.launch(kernel, dim=1, inputs=[v3], outputs=[outcomponents], device=device)
|
|
1380
|
+
|
|
1381
|
+
# make sure it gives you a cross product matrix:
|
|
1382
|
+
crossprodmat = outcomponents.numpy().reshape(3, 3)
|
|
1383
|
+
v = np.array([1, 0, 0])
|
|
1384
|
+
assert_np_equal(
|
|
1385
|
+
np.matmul(crossprodmat, np.array([1, 0, 0])).reshape(-1),
|
|
1386
|
+
2 * np.cross(v3.numpy()[0], np.array([1, 0, 0])),
|
|
1387
|
+
tol=tol,
|
|
1388
|
+
)
|
|
1389
|
+
assert_np_equal(
|
|
1390
|
+
np.matmul(crossprodmat, np.array([0, 1, 0])).reshape(-1),
|
|
1391
|
+
2 * np.cross(v3.numpy()[0], np.array([0, 1, 0])),
|
|
1392
|
+
tol=tol,
|
|
1393
|
+
)
|
|
1394
|
+
assert_np_equal(
|
|
1395
|
+
np.matmul(crossprodmat, np.array([0, 0, 1])).reshape(-1),
|
|
1396
|
+
2 * np.cross(v3.numpy()[0], np.array([0, 0, 1])),
|
|
1397
|
+
tol=tol,
|
|
1398
|
+
)
|
|
1399
|
+
|
|
1400
|
+
# check it another way:
|
|
1401
|
+
x0 = v3.numpy()[0, 0]
|
|
1402
|
+
x1 = v3.numpy()[0, 1]
|
|
1403
|
+
x2 = v3.numpy()[0, 2]
|
|
1404
|
+
crossprodmat_expected = np.array(
|
|
1405
|
+
[
|
|
1406
|
+
[0, -x2, x1],
|
|
1407
|
+
[x2, 0, -x0],
|
|
1408
|
+
[-x1, x0, 0],
|
|
1409
|
+
],
|
|
1410
|
+
dtype=dtype,
|
|
1411
|
+
)
|
|
1412
|
+
assert_np_equal(crossprodmat, 2 * crossprodmat_expected, tol=tol)
|
|
1413
|
+
|
|
1414
|
+
if dtype in np_float_types:
|
|
1415
|
+
idx = 0
|
|
1416
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
1417
|
+
|
|
1418
|
+
for i in range(3):
|
|
1419
|
+
for j in range(3):
|
|
1420
|
+
tape = wp.Tape()
|
|
1421
|
+
with tape:
|
|
1422
|
+
wp.launch(kernel, dim=1, inputs=[v3], outputs=[outcomponents], device=device)
|
|
1423
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, idx], outputs=[out], device=device)
|
|
1424
|
+
tape.backward(loss=out)
|
|
1425
|
+
if i == j:
|
|
1426
|
+
assert_np_equal(tape.gradients[v3].numpy()[0], np.zeros(3))
|
|
1427
|
+
elif [i, j] == [0, 1]:
|
|
1428
|
+
assert_np_equal(tape.gradients[v3].numpy()[0], np.array([0, 0, -2]))
|
|
1429
|
+
elif [i, j] == [1, 0]:
|
|
1430
|
+
assert_np_equal(tape.gradients[v3].numpy()[0], np.array([0, 0, 2]))
|
|
1431
|
+
elif [i, j] == [0, 2]:
|
|
1432
|
+
assert_np_equal(tape.gradients[v3].numpy()[0], np.array([0, 2, 0]))
|
|
1433
|
+
elif [i, j] == [2, 0]:
|
|
1434
|
+
assert_np_equal(tape.gradients[v3].numpy()[0], np.array([0, -2, 0]))
|
|
1435
|
+
elif [i, j] == [1, 2]:
|
|
1436
|
+
assert_np_equal(tape.gradients[v3].numpy()[0], np.array([-2, 0, 0]))
|
|
1437
|
+
elif [i, j] == [2, 1]:
|
|
1438
|
+
assert_np_equal(tape.gradients[v3].numpy()[0], np.array([2, 0, 0]))
|
|
1439
|
+
tape.zero()
|
|
1440
|
+
|
|
1441
|
+
idx = idx + 1
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
def test_transform_point(test, device, dtype, register_kernels=False):
|
|
1445
|
+
rng = np.random.default_rng(123)
|
|
1446
|
+
|
|
1447
|
+
tol = {
|
|
1448
|
+
np.float16: 5.0e-3,
|
|
1449
|
+
np.float32: 1.0e-6,
|
|
1450
|
+
np.float64: 1.0e-8,
|
|
1451
|
+
}.get(dtype, 0)
|
|
1452
|
+
|
|
1453
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
1454
|
+
vec3 = wp.types.vector(length=3, dtype=wptype)
|
|
1455
|
+
mat44 = wp.types.matrix(shape=(4, 4), dtype=wptype)
|
|
1456
|
+
|
|
1457
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
1458
|
+
|
|
1459
|
+
def check_mat_transform_point(
|
|
1460
|
+
v3: wp.array(dtype=vec3),
|
|
1461
|
+
m4: wp.array(dtype=mat44),
|
|
1462
|
+
outcomponents: wp.array(dtype=wptype),
|
|
1463
|
+
):
|
|
1464
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
1465
|
+
presult = wptype(2) * wp.transform_point(m4[0], v3[0])
|
|
1466
|
+
|
|
1467
|
+
outcomponents[0] = presult[0]
|
|
1468
|
+
outcomponents[1] = presult[1]
|
|
1469
|
+
outcomponents[2] = presult[2]
|
|
1470
|
+
|
|
1471
|
+
kernel = getkernel(check_mat_transform_point, suffix=dtype.__name__)
|
|
1472
|
+
|
|
1473
|
+
if register_kernels:
|
|
1474
|
+
return
|
|
1475
|
+
|
|
1476
|
+
v3 = wp.array(randvals(rng, [1, 3], dtype), dtype=vec3, requires_grad=True, device=device)
|
|
1477
|
+
m4 = wp.array(randvals(rng, [1, 4, 4], dtype), dtype=mat44, requires_grad=True, device=device)
|
|
1478
|
+
|
|
1479
|
+
outcomponents = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
|
|
1480
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
1481
|
+
|
|
1482
|
+
wp.launch(kernel, dim=1, inputs=[v3, m4], outputs=[outcomponents], device=device)
|
|
1483
|
+
|
|
1484
|
+
v3homog = np.ones(4, dtype=dtype)
|
|
1485
|
+
v3homog[:3] = v3.numpy()[0]
|
|
1486
|
+
assert_np_equal(outcomponents.numpy(), 2 * np.matmul(m4.numpy()[0], v3homog)[:3], tol=10 * tol)
|
|
1487
|
+
|
|
1488
|
+
if dtype in np_float_types:
|
|
1489
|
+
for j in range(3):
|
|
1490
|
+
tape = wp.Tape()
|
|
1491
|
+
with tape:
|
|
1492
|
+
wp.launch(kernel, dim=1, inputs=[v3, m4], outputs=[outcomponents], device=device)
|
|
1493
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, j], outputs=[out], device=device)
|
|
1494
|
+
tape.backward(loss=out)
|
|
1495
|
+
|
|
1496
|
+
assert_np_equal(2 * m4.numpy()[0, j, :3], tape.gradients[v3].numpy(), tol=tol)
|
|
1497
|
+
expected = np.zeros((4, 4), dtype=dtype)
|
|
1498
|
+
expected[j, :3] = 2 * v3.numpy()
|
|
1499
|
+
expected[j, 3] = 2
|
|
1500
|
+
assert_np_equal(tape.gradients[m4].numpy(), expected, tol=tol)
|
|
1501
|
+
|
|
1502
|
+
tape.zero()
|
|
1503
|
+
|
|
1504
|
+
|
|
1505
|
+
def test_transform_vector(test, device, dtype, register_kernels=False):
|
|
1506
|
+
rng = np.random.default_rng(123)
|
|
1507
|
+
|
|
1508
|
+
tol = {
|
|
1509
|
+
np.float16: 5.0e-3,
|
|
1510
|
+
np.float32: 1.0e-6,
|
|
1511
|
+
np.float64: 1.0e-8,
|
|
1512
|
+
}.get(dtype, 0)
|
|
1513
|
+
|
|
1514
|
+
wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)]
|
|
1515
|
+
vec3 = wp.types.vector(length=3, dtype=wptype)
|
|
1516
|
+
mat44 = wp.types.matrix(shape=(4, 4), dtype=wptype)
|
|
1517
|
+
|
|
1518
|
+
output_select_kernel = get_select_kernel(wptype)
|
|
1519
|
+
|
|
1520
|
+
def check_mat_transform_vector(
|
|
1521
|
+
v3: wp.array(dtype=vec3),
|
|
1522
|
+
m4: wp.array(dtype=mat44),
|
|
1523
|
+
outcomponents: wp.array(dtype=wptype),
|
|
1524
|
+
):
|
|
1525
|
+
# multiply outputs by 2 so we've got something to backpropagate:
|
|
1526
|
+
presult = wptype(2) * wp.transform_vector(m4[0], v3[0])
|
|
1527
|
+
|
|
1528
|
+
outcomponents[0] = presult[0]
|
|
1529
|
+
outcomponents[1] = presult[1]
|
|
1530
|
+
outcomponents[2] = presult[2]
|
|
1531
|
+
|
|
1532
|
+
kernel = getkernel(check_mat_transform_vector, suffix=dtype.__name__)
|
|
1533
|
+
|
|
1534
|
+
if register_kernels:
|
|
1535
|
+
return
|
|
1536
|
+
|
|
1537
|
+
v3 = wp.array(randvals(rng, [1, 3], dtype), dtype=vec3, requires_grad=True, device=device)
|
|
1538
|
+
m4 = wp.array(randvals(rng, [1, 4, 4], dtype), dtype=mat44, requires_grad=True, device=device)
|
|
1539
|
+
|
|
1540
|
+
outcomponents = wp.zeros(3, dtype=wptype, requires_grad=True, device=device)
|
|
1541
|
+
out = wp.zeros(1, dtype=wptype, requires_grad=True, device=device)
|
|
1542
|
+
|
|
1543
|
+
wp.launch(kernel, dim=1, inputs=[v3, m4], outputs=[outcomponents], device=device)
|
|
1544
|
+
|
|
1545
|
+
v3homog = np.zeros(4, dtype=dtype)
|
|
1546
|
+
v3homog[:3] = v3.numpy()[0]
|
|
1547
|
+
assert_np_equal(outcomponents.numpy(), 2 * np.matmul(m4.numpy()[0], v3homog)[:3], tol=10 * tol)
|
|
1548
|
+
|
|
1549
|
+
if dtype in np_float_types:
|
|
1550
|
+
for j in range(3):
|
|
1551
|
+
tape = wp.Tape()
|
|
1552
|
+
with tape:
|
|
1553
|
+
wp.launch(kernel, dim=1, inputs=[v3, m4], outputs=[outcomponents], device=device)
|
|
1554
|
+
wp.launch(output_select_kernel, dim=1, inputs=[outcomponents, j], outputs=[out], device=device)
|
|
1555
|
+
tape.backward(loss=out)
|
|
1556
|
+
|
|
1557
|
+
assert_np_equal(2 * m4.numpy()[0, j, :3], tape.gradients[v3].numpy(), tol=tol)
|
|
1558
|
+
expected = np.zeros((4, 4), dtype=dtype)
|
|
1559
|
+
expected[j, :3] = 2 * v3.numpy()
|
|
1560
|
+
assert_np_equal(tape.gradients[m4].numpy(), expected, tol=tol)
|
|
1561
|
+
|
|
1562
|
+
tape.zero()
|
|
1563
|
+
|
|
1564
|
+
|
|
1565
|
+
# Test matrix constructors using explicit type (float16)
|
|
1566
|
+
# note that these tests are specifically not using generics / closure
|
|
1567
|
+
# args to create kernels dynamically (like the rest of this file)
|
|
1568
|
+
# as those use different code paths to resolve arg types which
|
|
1569
|
+
# has lead to regressions.
|
|
1570
|
+
@wp.kernel
|
|
1571
|
+
def test_constructors_explicit_precision():
|
|
1572
|
+
# construction for custom matrix types
|
|
1573
|
+
eye = wp.identity(dtype=wp.float16, n=2)
|
|
1574
|
+
zeros = wp.matrix(shape=(2, 2), dtype=wp.float16)
|
|
1575
|
+
custom = wp.matrix(wp.float16(0.0), wp.float16(1.0), wp.float16(2.0), wp.float16(3.0), shape=(2, 2))
|
|
1576
|
+
|
|
1577
|
+
for i in range(2):
|
|
1578
|
+
for j in range(2):
|
|
1579
|
+
if i == j:
|
|
1580
|
+
wp.expect_eq(eye[i, j], wp.float16(1.0))
|
|
1581
|
+
else:
|
|
1582
|
+
wp.expect_eq(eye[i, j], wp.float16(0.0))
|
|
1583
|
+
|
|
1584
|
+
wp.expect_eq(zeros[i, j], wp.float16(0.0))
|
|
1585
|
+
wp.expect_eq(custom[i, j], wp.float16(i) * wp.float16(2.0) + wp.float16(j))
|
|
1586
|
+
|
|
1587
|
+
|
|
1588
|
+
mat32d = wp.mat(shape=(3, 2), dtype=wp.float64)
|
|
1589
|
+
|
|
1590
|
+
|
|
1591
|
+
@wp.kernel
|
|
1592
|
+
def test_matrix_constructor_value_func():
|
|
1593
|
+
a = wp.mat22()
|
|
1594
|
+
b = wp.matrix(a, shape=(2, 2))
|
|
1595
|
+
c = mat32d()
|
|
1596
|
+
d = mat32d(c, shape=(3, 2))
|
|
1597
|
+
e = mat32d(wp.float64(1.0), wp.float64(2.0), wp.float64(1.0), wp.float64(2.0), wp.float64(1.0), wp.float64(2.0))
|
|
1598
|
+
f = mat32d(
|
|
1599
|
+
wp.vec3d(wp.float64(1.0), wp.float64(2.0), wp.float64(3.0)),
|
|
1600
|
+
wp.vec3d(wp.float64(1.0), wp.float64(2.0), wp.float64(3.0)),
|
|
1601
|
+
)
|
|
1602
|
+
|
|
1603
|
+
|
|
1604
|
+
# Same as above but with a default (float/int) type
|
|
1605
|
+
# which tests some different code paths that
|
|
1606
|
+
# need to ensure types are correctly canonicalized
|
|
1607
|
+
# during codegen
|
|
1608
|
+
@wp.kernel
|
|
1609
|
+
def test_constructors_default_precision():
|
|
1610
|
+
# construction for default (float) matrix types
|
|
1611
|
+
eye = wp.identity(dtype=float, n=2)
|
|
1612
|
+
zeros = wp.matrix(shape=(2, 2), dtype=float)
|
|
1613
|
+
custom = wp.matrix(0.0, 1.0, 2.0, 3.0, shape=(2, 2))
|
|
1614
|
+
|
|
1615
|
+
for i in range(2):
|
|
1616
|
+
for j in range(2):
|
|
1617
|
+
if i == j:
|
|
1618
|
+
wp.expect_eq(eye[i, j], 1.0)
|
|
1619
|
+
else:
|
|
1620
|
+
wp.expect_eq(eye[i, j], 0.0)
|
|
1621
|
+
|
|
1622
|
+
wp.expect_eq(zeros[i, j], 0.0)
|
|
1623
|
+
wp.expect_eq(custom[i, j], float(i) * 2.0 + float(j))
|
|
1624
|
+
|
|
1625
|
+
|
|
1626
|
+
@wp.kernel
|
|
1627
|
+
def test_matrix_mutation(expected: wp.types.matrix(shape=(10, 3), dtype=float)):
|
|
1628
|
+
m = wp.matrix(shape=(10, 3), dtype=float)
|
|
1629
|
+
|
|
1630
|
+
# test direct element indexing
|
|
1631
|
+
m[0, 0] = 1.0
|
|
1632
|
+
m[0, 1] = 2.0
|
|
1633
|
+
m[0, 2] = 3.0
|
|
1634
|
+
|
|
1635
|
+
# The nested indexing (matrix->vector->scalar) below does not
|
|
1636
|
+
# currently modify m because m[0] returns row vector by
|
|
1637
|
+
# value rather than reference, this is different from NumPy
|
|
1638
|
+
# which always returns by ref. Not clear how we can support
|
|
1639
|
+
# this as well as auto-diff.
|
|
1640
|
+
|
|
1641
|
+
# m[0][1] = 2.0
|
|
1642
|
+
# m[0][2] = 3.0
|
|
1643
|
+
|
|
1644
|
+
# test setting rows
|
|
1645
|
+
for i in range(1, 10):
|
|
1646
|
+
m[i] = m[i - 1] + wp.vec3(1.0, 2.0, 3.0)
|
|
1647
|
+
|
|
1648
|
+
wp.expect_eq(m, expected)
|
|
1649
|
+
|
|
1650
|
+
|
|
1651
|
+
CONSTANT_SHAPE_ROWS = wp.constant(10)
|
|
1652
|
+
CONSTANT_SHAPE_COLS = wp.constant(10)
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
# tests that we can use global constants in shape keyword argument
|
|
1656
|
+
# for matrix constructor
|
|
1657
|
+
@wp.kernel
|
|
1658
|
+
def test_constructors_constant_shape():
|
|
1659
|
+
m = wp.matrix(shape=(CONSTANT_SHAPE_ROWS, CONSTANT_SHAPE_COLS), dtype=float)
|
|
1660
|
+
|
|
1661
|
+
for i in range(CONSTANT_SHAPE_ROWS):
|
|
1662
|
+
for j in range(CONSTANT_SHAPE_COLS):
|
|
1663
|
+
m[i, j] = float(i * j)
|
|
1664
|
+
|
|
1665
|
+
|
|
1666
|
+
devices = get_test_devices()
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
class TestMat(unittest.TestCase):
|
|
1670
|
+
def test_tpl_ops_with_anon(self):
|
|
1671
|
+
mat22f = wp.mat((2, 2), dtype=float)
|
|
1672
|
+
|
|
1673
|
+
m = wp.mat22f(1.0, 2.0, 3.0, 4.0)
|
|
1674
|
+
m += mat22f(2.0, 3.0, 4.0, 5.0)
|
|
1675
|
+
m -= mat22f(3.0, 4.0, 5.0, 6.0)
|
|
1676
|
+
self.assertSequenceEqual(m, ((0.0, 1.0), (2.0, 3.0)))
|
|
1677
|
+
|
|
1678
|
+
m = mat22f(1.0, 2.0, 3.0, 4.0)
|
|
1679
|
+
m += wp.mat22f(2.0, 3.0, 4.0, 5.0)
|
|
1680
|
+
m -= wp.mat22f(3.0, 4.0, 5.0, 6.0)
|
|
1681
|
+
self.assertSequenceEqual(m, ((0.0, 1.0), (2.0, 3.0)))
|
|
1682
|
+
|
|
1683
|
+
|
|
1684
|
+
add_kernel_test(TestMat, test_constructors_explicit_precision, dim=1, devices=devices)
|
|
1685
|
+
add_kernel_test(TestMat, test_constructors_default_precision, dim=1, devices=devices)
|
|
1686
|
+
add_kernel_test(TestMat, test_constructors_constant_shape, dim=1, devices=devices)
|
|
1687
|
+
add_kernel_test(TestMat, test_matrix_constructor_value_func, dim=1, devices=devices)
|
|
1688
|
+
|
|
1689
|
+
mat103 = wp.types.matrix(shape=(10, 3), dtype=float)
|
|
1690
|
+
add_kernel_test(
|
|
1691
|
+
TestMat,
|
|
1692
|
+
test_matrix_mutation,
|
|
1693
|
+
dim=1,
|
|
1694
|
+
inputs=[
|
|
1695
|
+
mat103(
|
|
1696
|
+
1.0, 2.0, 3.0,
|
|
1697
|
+
2.0, 4.0, 6.0,
|
|
1698
|
+
3.0, 6.0, 9.0,
|
|
1699
|
+
4.0, 8.0, 12.0,
|
|
1700
|
+
5.0, 10.0, 15.0,
|
|
1701
|
+
6.0, 12.0, 18.0,
|
|
1702
|
+
7.0, 14.0, 21.0,
|
|
1703
|
+
8.0, 16.0, 24.0,
|
|
1704
|
+
9.0, 18.0, 27.0,
|
|
1705
|
+
10.0, 20.0, 30.0,
|
|
1706
|
+
)
|
|
1707
|
+
],
|
|
1708
|
+
devices=devices,
|
|
1709
|
+
) # fmt: skip
|
|
1710
|
+
|
|
1711
|
+
for dtype in np_signed_int_types + np_float_types:
|
|
1712
|
+
add_function_test_register_kernel(
|
|
1713
|
+
TestMat, f"test_negation_{dtype.__name__}", test_negation, devices=devices, dtype=dtype
|
|
1714
|
+
)
|
|
1715
|
+
add_function_test_register_kernel(
|
|
1716
|
+
TestMat, f"test_subtraction_{dtype.__name__}", test_subtraction, devices=devices, dtype=dtype
|
|
1717
|
+
)
|
|
1718
|
+
|
|
1719
|
+
add_function_test(
|
|
1720
|
+
TestMat,
|
|
1721
|
+
"test_anon_constructor_error_shape_keyword_missing",
|
|
1722
|
+
test_anon_constructor_error_shape_keyword_missing,
|
|
1723
|
+
devices=devices,
|
|
1724
|
+
)
|
|
1725
|
+
add_function_test(
|
|
1726
|
+
TestMat,
|
|
1727
|
+
"test_anon_constructor_error_dtype_keyword_missing",
|
|
1728
|
+
test_anon_constructor_error_dtype_keyword_missing,
|
|
1729
|
+
devices=devices,
|
|
1730
|
+
)
|
|
1731
|
+
add_function_test(
|
|
1732
|
+
TestMat,
|
|
1733
|
+
"test_anon_constructor_error_shape_mismatch",
|
|
1734
|
+
test_anon_constructor_error_shape_mismatch,
|
|
1735
|
+
devices=devices,
|
|
1736
|
+
)
|
|
1737
|
+
add_function_test(
|
|
1738
|
+
TestMat,
|
|
1739
|
+
"test_anon_constructor_error_invalid_arg_count",
|
|
1740
|
+
test_anon_constructor_error_invalid_arg_count,
|
|
1741
|
+
devices=devices,
|
|
1742
|
+
)
|
|
1743
|
+
add_function_test(
|
|
1744
|
+
TestMat,
|
|
1745
|
+
"test_tpl_constructor_error_incompatible_sizes",
|
|
1746
|
+
test_tpl_constructor_error_incompatible_sizes,
|
|
1747
|
+
devices=devices,
|
|
1748
|
+
)
|
|
1749
|
+
add_function_test(
|
|
1750
|
+
TestMat,
|
|
1751
|
+
"test_tpl_constructor_error_invalid_scalar_type",
|
|
1752
|
+
test_tpl_constructor_error_invalid_scalar_type,
|
|
1753
|
+
devices=devices,
|
|
1754
|
+
)
|
|
1755
|
+
add_function_test(
|
|
1756
|
+
TestMat,
|
|
1757
|
+
"test_tpl_constructor_error_invalid_vector_count",
|
|
1758
|
+
test_tpl_constructor_error_invalid_vector_count,
|
|
1759
|
+
devices=devices,
|
|
1760
|
+
)
|
|
1761
|
+
add_function_test(
|
|
1762
|
+
TestMat,
|
|
1763
|
+
"test_tpl_constructor_error_invalid_vector_shape",
|
|
1764
|
+
test_tpl_constructor_error_invalid_vector_shape,
|
|
1765
|
+
devices=devices,
|
|
1766
|
+
)
|
|
1767
|
+
add_function_test(
|
|
1768
|
+
TestMat,
|
|
1769
|
+
"test_tpl_constructor_error_invalid_arg_count",
|
|
1770
|
+
test_tpl_constructor_error_invalid_arg_count,
|
|
1771
|
+
devices=devices,
|
|
1772
|
+
)
|
|
1773
|
+
|
|
1774
|
+
for dtype in np_float_types:
|
|
1775
|
+
add_function_test(
|
|
1776
|
+
TestMat, f"test_py_arithmetic_ops_{dtype.__name__}", test_py_arithmetic_ops, devices=None, dtype=dtype
|
|
1777
|
+
)
|
|
1778
|
+
add_function_test_register_kernel(
|
|
1779
|
+
TestMat, f"test_quat_constructor_{dtype.__name__}", test_quat_constructor, devices=devices, dtype=dtype
|
|
1780
|
+
)
|
|
1781
|
+
add_function_test_register_kernel(
|
|
1782
|
+
TestMat, f"test_inverse_{dtype.__name__}", test_inverse, devices=devices, dtype=dtype
|
|
1783
|
+
)
|
|
1784
|
+
add_function_test_register_kernel(TestMat, f"test_svd_{dtype.__name__}", test_svd, devices=devices, dtype=dtype)
|
|
1785
|
+
add_function_test_register_kernel(TestMat, f"test_qr_{dtype.__name__}", test_qr, devices=devices, dtype=dtype)
|
|
1786
|
+
add_function_test_register_kernel(TestMat, f"test_eig_{dtype.__name__}", test_eig, devices=devices, dtype=dtype)
|
|
1787
|
+
add_function_test_register_kernel(
|
|
1788
|
+
TestMat, f"test_transform_point_{dtype.__name__}", test_transform_point, devices=devices, dtype=dtype
|
|
1789
|
+
)
|
|
1790
|
+
add_function_test_register_kernel(
|
|
1791
|
+
TestMat, f"test_transform_vector_{dtype.__name__}", test_transform_vector, devices=devices, dtype=dtype
|
|
1792
|
+
)
|
|
1793
|
+
add_function_test_register_kernel(
|
|
1794
|
+
TestMat, f"test_determinant_{dtype.__name__}", test_determinant, devices=devices, dtype=dtype
|
|
1795
|
+
)
|
|
1796
|
+
add_function_test_register_kernel(TestMat, f"test_skew_{dtype.__name__}", test_skew, devices=devices, dtype=dtype)
|
|
1797
|
+
|
|
1798
|
+
|
|
1799
|
+
if __name__ == "__main__":
|
|
1800
|
+
wp.build.clear_kernel_cache()
|
|
1801
|
+
unittest.main(verbosity=2, failfast=True)
|