From 4003af42927f56801e9dae988e6dfed739d88025 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 1 May 2024 15:23:22 +1200 Subject: [PATCH 001/102] Initial tile script --- warp/tests/test_tile.py | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 warp/tests/test_tile.py diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py new file mode 100644 index 00000000..bc36d1fd --- /dev/null +++ b/warp/tests/test_tile.py @@ -0,0 +1,79 @@ +import numpy as np +import warp as wp + +wp.init() + +@wp.kernel +def gemm(A: wp.array2d(dtype=float), + B: wp.array2d(dtype=float), + C: wp.array2d(dtype=float)): + + # output index + i, j = wp.tid() + + sum = float(0.0) + + for k in range(0, A.shape[1]): + sum += A[i, k]*B[k, j] + + C[i, j] = sum + +TILE_M = wp.constant(16) +TILE_N = wp.constant(16) +TILE_K = wp.constant(8) + +@wp.kernel +def gemm_tiled(A: wp.array2d(dtype=float), + B: wp.array2d(dtype=float), + C: wp.array2d(dtype=float)): + + # output tile index + i, j = wp.tid() + + sum = wp.tile_zeros((TILE_M, TILE_N), dtype=wp.float32) + + M = A.shape[0] + N = A.shape[1] + K = B.shape[1] + + for k in range(0, K, TILE_K): + + a = wp.tile_load(A, i, j+k, TILE_M, TILE_K) + b = wp.tile_load(B, i+k, j, TILE_K, TILE_N) + + sum += wp.tile_matmul(a, b) + + wp.tile_store(C, i, j, TILE_M, TILE_N) + + +M = 240 +K = 80 +N = 350 + +rng = np.random.default_rng(42) +A = rng.random((M, K), dtype=np.float32) +B = rng.random((K, N), dtype=np.float32) +C = np.zeros((M, N), dtype=np.float32) + +A_wp = wp.array(A) +B_wp = wp.array(B) +C_wp = wp.array(C) + +iters = 100 + +with wp.ScopedTimer("NumPy"): + + for i in range(iters): + C = A@B + +wp.force_load() + +with wp.ScopedTimer("Warp", cuda_flags=wp.CUDA_TIMING_KERNEL): + + for i in range(iters): + wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) + + +print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) + + From fadd083dae036a91aa6eee126b7ae9931d56a30b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 3 May 2024 12:24:53 +1200 Subject: [PATCH 002/102] Working on tile tests + API --- warp/builtins.py | 164 ++++++++++++++++++++++++++++++++++++++++ warp/native/builtin.h | 1 + warp/native/tile.h | 92 ++++++++++++++++++++++ warp/tests/test_tile.py | 21 ++--- 4 files changed, 269 insertions(+), 9 deletions(-) create mode 100644 warp/native/tile.h diff --git a/warp/builtins.py b/warp/builtins.py index ea11e634..50d750e4 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1359,6 +1359,170 @@ def spatial_vector_constructor_value_func(arg_types, kwds, templates): group="Spatial Math", ) +# ------------------ +# Tile-based primitives +shared_memory_id = 0 + +def tile_zeros_value_func(arg_types, kwds, templates): + + # return generic type (for doc builds) + if arg_types is None: + return array_t(shape=(Any, Any), dtype=Scalar) + + if len(arg_types) > 0: + raise RuntimeError("tile_zero() args must be passed by keyword") + + if "m" not in kwds: + raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") + + if "n" not in kwds: + raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") + + if "dtype" not in kwds: + raise RuntimeError("'dtype' keyword argument must be specified when calling tile_zeros() function") + + m, n, dtype = kwds["m"], kwds["n"], kwds["dtype"] + + templates.append(m) + templates.append(n) + templates.append(dtype) + + global shared_memory_id + templates.append(shared_memory_id) + + shared_memory_id += 1 + + return array(dtype=dtype) + + + +add_builtin( + "tile_zeros", + input_types={"m": int, "n": int, "dtype": Scalar}, + value_func=tile_zeros_value_func, + variadic=True, + doc="Allocate a tile local block of zero'd memory", + group="Tile Primitives", + export=False, +) + +def tile_load_value_func(arg_types, kwds, templates): + + # return generic type (for doc builds) + if arg_types is None: + return array_t(shape=(Any, Any), dtype=Scalar) + + if len(arg_types) != 3: + raise RuntimeError("tile_load() requires 3 positional args") + + if not is_array(arg_types[0]): + raise RuntimeError("tile_load() argument 0 must be an array") + + if not type_is_int(arg_types[1]): + raise RuntimeError("tile_load() argument 1 must be an integer") + + if not type_is_int(arg_types[2]): + raise RuntimeError("tile_load() argument 1 must be an integer") + + if "m" not in kwds: + raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") + + if "n" not in kwds: + raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") + + m, n = kwds["m"], kwds["n"] + + templates.append(m) + templates.append(n) + templates.append(arg_types[0].dtype) + + global shared_memory_id + templates.append(shared_memory_id) + + shared_memory_id += 1 + + return array(dtype=arg_types[0].dtype) + + + +add_builtin( + "tile_load", + input_types={"a": array(dtype=Any), "x": int, "y": int, "m": int, "n": int}, + value_func=tile_load_value_func, + variadic=True, + doc="Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)", + group="Tile Primitives", + export=False, +) + +def tile_store_value_func(arg_types, kwds, templates): + + # return generic type (for doc builds) + if arg_types is None: + return None + + if len(arg_types) != 4: + raise RuntimeError("tile_store() requires 4 positional args") + + if not is_array(arg_types[0]): + raise RuntimeError("tile_store() argument 0 must be an array") + + if not type_is_int(arg_types[1]): + raise RuntimeError("tile_store() argument 1 must be an integer") + + if not type_is_int(arg_types[2]): + raise RuntimeError("tile_store() argument 2 must be an integer") + + if not is_array(arg_types[3]): + raise RuntimeError("tile_store() argument 3 must be an array") + + return None + + + +add_builtin( + "tile_store", + input_types={"a": array(dtype=Any), "x": int, "y": int, "m": int, "n": int}, + value_func=tile_store_value_func, + variadic=True, + doc="Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)", + group="Tile Primitives", + export=False, +) + + + +def tile_matmul_value_func(arg_types, kwds, templates): + + # return generic type (for doc builds) + if arg_types is None: + return None + + if len(arg_types) != 3: + raise RuntimeError("tile_matmul() requires 4 positional args") + + if not is_array(arg_types[0]): + raise RuntimeError("tile_matmul() argument 0 must be an array") + + if not is_array(arg_types[1]): + raise RuntimeError("tile_matmul() argument 1 must be an array") + + if not is_array(arg_types[2]): + raise RuntimeError("tile_matmul() argument 2 must be an array") + + return None + + +add_builtin( + "tile_matmul", + input_types={"a": array(dtype=Any), "b": array(dtype=Any), "out": array(dtype=Any)}, + value_func=tile_matmul_value_func, + variadic=True, + doc="Compute matrix product and accumulate out += a*b", + group="Tile Primitives", + export=False, +) + # --------------------------------- # Linear Algebra diff --git a/warp/native/builtin.h b/warp/native/builtin.h index b2865788..97737567 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1558,3 +1558,4 @@ inline CUDA_CALLABLE void adj_expect_near(const vec3& actual, const vec3& expect #include "rand.h" #include "noise.h" #include "matnn.h" +#include "tile.h" \ No newline at end of file diff --git a/warp/native/tile.h b/warp/native/tile.h new file mode 100644 index 00000000..434799ec --- /dev/null +++ b/warp/native/tile.h @@ -0,0 +1,92 @@ +#pragma once + +#include "builtin.h" + +// #define WP_CONCAT(x, y) x ## y +// #define WP_SHARED_MEM(name, id) WP_CONCAT(name, id) + +// #define zero(a) memset(a, 0, sizeof(a)); + +// #define tile_zeros(a, b, dtype) [](){\ +// static dtype WP_SHARED_MEM(data_, __LINE__)[a][b]; \ +// zero(WP_SHARED_MEM(data_, __LINE__)); \ +// return array_tWP_SHARED_MEM(data_, __LINE__; )}() + +#if !defined(__CUDA_ARCH__) +#define __shared__ static +#endif + +namespace wp +{ + +// 2D tile zero +template +inline CUDA_CALLABLE array_t tile_zeros() +{ + __shared__ T data[M*N]; + + return array_t(data, M, N, nullptr); +} + +// 2D tile load +template +inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) +{ + const int length = M*N; + + __shared__ T data[length]; + + // cooperatively load the tile, using a block-stride iterator + // todo: use cub::BlockLoad or cg::memcpy_async()? + for (int t=threadIdx.y; t < length; t += blockDim.y) + { + data[t] = index(src, i*M + t/N, j*N + t%N); + } + + return array_t(data, M, N, nullptr); +} + +// 2D tile store +template +inline CUDA_CALLABLE array_t tile_store(const array_t& dest, const array_t& src, int i, int j) +{ + const int length = src.shape[0]*src.shape[1]; + + // cooperatively store the tile, using a block-stride iterator + // todo: use cub::BlockStore or cg::memcpy_async()? + for (int t=threadIdx.y; t < length; t += blockDim.y) + { + index(dest, i*M + t/N, j*N + t%N, i) = src.data[t]; + } + + return array_t(data, M, N, nullptr); +} + + +// 2D gemm accumulate out += A*B +template +inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) +{ + const int length = out.shape[0]*out.shape[1]; + + for (int t=threadIdx.y; t < length; t += blockDim.y) + { + // compute output index + const int i = t%out.shape[0]; + const int j = t/out.shape[1]; + + T sum = T(0.0); + + for (int k=0; k < A.shape[1]; ++k) + { + sum += index(A, i, k)*index(B, k, j); + } + + index(out, i, j) += sum; + } +} + + + + +} // namespace wp \ No newline at end of file diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index bc36d1fd..137662f3 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -2,6 +2,8 @@ import warp as wp wp.init() +wp.set_module_options({"enable_backwards": False}) +wp.set_device("cuda:0") @wp.kernel def gemm(A: wp.array2d(dtype=float), @@ -30,20 +32,21 @@ def gemm_tiled(A: wp.array2d(dtype=float), # output tile index i, j = wp.tid() - sum = wp.tile_zeros((TILE_M, TILE_N), dtype=wp.float32) + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) M = A.shape[0] - N = A.shape[1] - K = B.shape[1] + N = B.shape[1] + K = A.shape[1] for k in range(0, K, TILE_K): - a = wp.tile_load(A, i, j+k, TILE_M, TILE_K) - b = wp.tile_load(B, i+k, j, TILE_K, TILE_N) + a = wp.tile_load(A, i, j+k, m=TILE_M, n=TILE_K) + b = wp.tile_load(B, i+k, j, m=TILE_K, n=TILE_N) - sum += wp.tile_matmul(a, b) + # sum += a*b + wp.tile_matmul(a, b, sum) - wp.tile_store(C, i, j, TILE_M, TILE_N) + wp.tile_store(C, i, j, sum) M = 240 @@ -66,9 +69,9 @@ def gemm_tiled(A: wp.array2d(dtype=float), for i in range(iters): C = A@B -wp.force_load() +#wp.force_load() -with wp.ScopedTimer("Warp", cuda_flags=wp.CUDA_TIMING_KERNEL): +with wp.ScopedTimer("Warp", cuda_flags=wp.TIMING_KERNEL): for i in range(iters): wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) From af9bae58a72f8874e3a0e86f18bd0daf5bac66d3 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 3 May 2024 16:15:49 +1200 Subject: [PATCH 003/102] Working tile-based GEMM, issues 1-block per-Warp logical thread, with tile size as an additional wp.launch() param --- warp/builtins.py | 4 +-- warp/codegen.py | 33 +++++++++++++++--- warp/context.py | 6 ++-- warp/native/array.h | 6 ++++ warp/native/tile.h | 50 ++++++++++++++++++--------- warp/native/warp.cu | 6 ++-- warp/native/warp.h | 2 +- warp/tests/test_tile.py | 76 ++++++++++++++++++++++++++++++++++------- 8 files changed, 142 insertions(+), 41 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 50d750e4..e1860363 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1383,9 +1383,9 @@ def tile_zeros_value_func(arg_types, kwds, templates): m, n, dtype = kwds["m"], kwds["n"], kwds["dtype"] + templates.append(dtype) templates.append(m) templates.append(n) - templates.append(dtype) global shared_memory_id templates.append(shared_memory_id) @@ -1432,9 +1432,9 @@ def tile_load_value_func(arg_types, kwds, templates): m, n = kwds["m"], kwds["n"] + templates.append(arg_types[0].dtype) templates.append(m) templates.append(n) - templates.append(arg_types[0].dtype) global shared_memory_id templates.append(shared_memory_id) diff --git a/warp/codegen.py b/warp/codegen.py index 1b9ccedb..a9972769 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -2289,14 +2289,38 @@ def get_node_source(adj, node): """ +# cuda_kernel_template = """ + +# extern "C" __global__ void {name}_cuda_kernel_forward( +# {forward_args}) +# {{ +# for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); +# _idx < dim.size; +# _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) +# {{ +# {forward_body} }} +# }} + +# extern "C" __global__ void {name}_cuda_kernel_backward( +# {reverse_args}) +# {{ +# for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); +# _idx < dim.size; +# _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) +# {{ +# {reverse_body} }} +# }} + +# """ + cuda_kernel_template = """ extern "C" __global__ void {name}_cuda_kernel_forward( {forward_args}) {{ - for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); + for (size_t _idx = static_cast(blockIdx.x); _idx < dim.size; - _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) + _idx += static_cast(gridDim.x)) {{ {forward_body} }} }} @@ -2304,15 +2328,16 @@ def get_node_source(adj, node): extern "C" __global__ void {name}_cuda_kernel_backward( {reverse_args}) {{ - for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); + for (size_t _idx = static_cast(blockIdx.x); _idx < dim.size; - _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) + _idx += static_cast(gridDim.x)) {{ {reverse_body} }} }} """ + cpu_kernel_template = """ void {name}_cpu_kernel_forward( diff --git a/warp/context.py b/warp/context.py index d9fca4d1..c590c822 100644 --- a/warp/context.py +++ b/warp/context.py @@ -2842,6 +2842,7 @@ def __init__(self): ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int, + ctypes.c_int, ctypes.POINTER(ctypes.c_void_p), ctypes.c_void_p, ] @@ -4232,6 +4233,7 @@ def launch( record_tape=True, record_cmd=False, max_blocks=0, + tile_size=1, ): """Launch a Warp kernel on the target device @@ -4352,7 +4354,7 @@ def pack_args(args, params, adjoint=False): ) runtime.core.cuda_launch_kernel( - device.context, hooks.backward, bounds.size, max_blocks, kernel_params, stream.cuda_stream + device.context, hooks.backward, bounds.size, max_blocks, tile_size, kernel_params, stream.cuda_stream ) else: @@ -4375,7 +4377,7 @@ def pack_args(args, params, adjoint=False): else: # launch runtime.core.cuda_launch_kernel( - device.context, hooks.forward, bounds.size, max_blocks, kernel_params, stream.cuda_stream + device.context, hooks.forward, bounds.size, max_blocks, tile_size, kernel_params, stream.cuda_stream ) try: diff --git a/warp/native/array.h b/warp/native/array.h index b0a43fc5..e9098c87 100644 --- a/warp/native/array.h +++ b/warp/native/array.h @@ -269,6 +269,12 @@ CUDA_CALLABLE inline size_t byte_offset(const array_t& arr, int i) template CUDA_CALLABLE inline size_t byte_offset(const array_t& arr, int i, int j) { + if (i < 0 || i >= arr.shape[0]) + printf("i: %d > arr.shape[0]: %d\n", i, arr.shape[0]); + + if (j < 0 || j >= arr.shape[1]) + printf("j: %d > arr.shape[1]: %d\n", j, arr.shape[1]); + assert(i >= 0 && i < arr.shape[0]); assert(j >= 0 && j < arr.shape[1]); diff --git a/warp/native/tile.h b/warp/native/tile.h index 434799ec..c6eab8f0 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -3,17 +3,21 @@ #include "builtin.h" // #define WP_CONCAT(x, y) x ## y -// #define WP_SHARED_MEM(name, id) WP_CONCAT(name, id) +// #define WP_TILE_SHARED_MEM(name, id) WP_CONCAT(name, id) // #define zero(a) memset(a, 0, sizeof(a)); // #define tile_zeros(a, b, dtype) [](){\ -// static dtype WP_SHARED_MEM(data_, __LINE__)[a][b]; \ -// zero(WP_SHARED_MEM(data_, __LINE__)); \ -// return array_tWP_SHARED_MEM(data_, __LINE__; )}() +// static dtype WP_TILE_SHARED_MEM(data_, __LINE__)[a][b]; \ +// zero(WP_TILE_SHARED_MEM(data_, __LINE__)); \ +// return array_tWP_TILE_SHARED_MEM(data_, __LINE__; )}() #if !defined(__CUDA_ARCH__) -#define __shared__ static +#define WP_TILE_SHARED static +#define WP_TILE_SYNC void +#else +#define WP_TILE_SHARED __shared__ +#define WP_TILE_SYNC __syncthreads #endif namespace wp @@ -23,8 +27,15 @@ namespace wp template inline CUDA_CALLABLE array_t tile_zeros() { - __shared__ T data[M*N]; + const int length = M*N; + + WP_TILE_SHARED T data[length]; + for (int t=threadIdx.x; t < length; t += blockDim.x) + { + data[t] = T(0.0); + } + return array_t(data, M, N, nullptr); } @@ -34,11 +45,11 @@ inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) { const int length = M*N; - __shared__ T data[length]; + WP_TILE_SHARED T data[length]; // cooperatively load the tile, using a block-stride iterator // todo: use cub::BlockLoad or cg::memcpy_async()? - for (int t=threadIdx.y; t < length; t += blockDim.y) + for (int t=threadIdx.x; t < length; t += blockDim.x) { data[t] = index(src, i*M + t/N, j*N + t%N); } @@ -48,18 +59,19 @@ inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) // 2D tile store template -inline CUDA_CALLABLE array_t tile_store(const array_t& dest, const array_t& src, int i, int j) +inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array_t& src) { - const int length = src.shape[0]*src.shape[1]; + const int M = src.shape[0]; + const int N = src.shape[1]; + + const int length = M*N; // cooperatively store the tile, using a block-stride iterator // todo: use cub::BlockStore or cg::memcpy_async()? - for (int t=threadIdx.y; t < length; t += blockDim.y) + for (int t=threadIdx.x; t < length; t += blockDim.x) { - index(dest, i*M + t/N, j*N + t%N, i) = src.data[t]; + index(dest, i*M + t/N, j*N + t%N) = src.data[t]; } - - return array_t(data, M, N, nullptr); } @@ -69,11 +81,13 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, { const int length = out.shape[0]*out.shape[1]; - for (int t=threadIdx.y; t < length; t += blockDim.y) + WP_TILE_SYNC(); + + for (int t=threadIdx.x; t < length; t += blockDim.x) { // compute output index - const int i = t%out.shape[0]; - const int j = t/out.shape[1]; + const int i = t/out.shape[1]; + const int j = t%out.shape[1]; T sum = T(0.0); @@ -84,6 +98,8 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, index(out, i, j) += sum; } + + WP_TILE_SYNC(); } diff --git a/warp/native/warp.cu b/warp/native/warp.cu index 07fa91de..f921a303 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -2787,14 +2787,14 @@ void* cuda_get_kernel(void* context, void* module, const char* name) return kernel; } -size_t cuda_launch_kernel(void* context, void* kernel, size_t dim, int max_blocks, void** args, void* stream) +size_t cuda_launch_kernel(void* context, void* kernel, size_t dim, int max_blocks, int tile_size, void** args, void* stream) { ContextGuard guard(context); - const int block_dim = 256; + const int block_dim = tile_size; // CUDA specs up to compute capability 9.0 says the max x-dim grid is 2**31-1, so // grid_dim is fine as an int for the near future - int grid_dim = (dim + block_dim - 1)/block_dim; + int grid_dim = dim; if (max_blocks <= 0) { max_blocks = 2147483647; diff --git a/warp/native/warp.h b/warp/native/warp.h index 2c072b61..58dc5f9b 100644 --- a/warp/native/warp.h +++ b/warp/native/warp.h @@ -294,7 +294,7 @@ extern "C" WP_API void* cuda_load_module(void* context, const char* ptx); WP_API void cuda_unload_module(void* context, void* module); WP_API void* cuda_get_kernel(void* context, void* module, const char* name); - WP_API size_t cuda_launch_kernel(void* context, void* kernel, size_t dim, int max_blocks, void** args, void* stream); + WP_API size_t cuda_launch_kernel(void* context, void* kernel, size_t dim, int max_blocks, int tile_size, void** args, void* stream); WP_API void cuda_set_context_restore_policy(bool always_restore); WP_API int cuda_get_context_restore_policy(); diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 137662f3..4383f428 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -1,10 +1,52 @@ import numpy as np import warp as wp +#wp.config.mode = "debug" + wp.init() -wp.set_module_options({"enable_backwards": False}) +wp.set_module_options({"enable_backward": False}) wp.set_device("cuda:0") + +wp.build.clear_kernel_cache() + +TILE_M = 8 +TILE_N = 4 + +@wp.kernel +def copy_tiled(A: wp.array2d(dtype=float), + B: wp.array2d(dtype=float)): + + # tile index + i, j = wp.tid() + + a = wp.tile_load(A, i, j, m=TILE_M, n=TILE_N) + wp.tile_store(B, i, j, a) + + +def test_copy_tiled(): + + rng = np.random.default_rng(42) + + M = TILE_M*7 + N = TILE_N*5 + + A = rng.random((M, N), dtype=np.float32) + B = rng.random((M, N), dtype=np.float32) + + A_wp = wp.array(A) + B_wp = wp.array(B) + + wp.launch(copy_tiled, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) + + assert(np.allclose(A, B_wp.numpy(), rtol=1.e-4)) + + print("Copy passed") + + +#test_copy_tiled() + + @wp.kernel def gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), @@ -20,6 +62,8 @@ def gemm(A: wp.array2d(dtype=float), C[i, j] = sum + + TILE_M = wp.constant(16) TILE_N = wp.constant(16) TILE_K = wp.constant(8) @@ -38,10 +82,12 @@ def gemm_tiled(A: wp.array2d(dtype=float), N = B.shape[1] K = A.shape[1] - for k in range(0, K, TILE_K): + count = int(K / 16) # TODO: code-gen bug if you use a constant before passing it to a kwd arg (in this case TILE_K) - a = wp.tile_load(A, i, j+k, m=TILE_M, n=TILE_K) - b = wp.tile_load(B, i+k, j, m=TILE_K, n=TILE_N) + for k in range(count): + + a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) + b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) # sum += a*b wp.tile_matmul(a, b, sum) @@ -49,9 +95,9 @@ def gemm_tiled(A: wp.array2d(dtype=float), wp.tile_store(C, i, j, sum) -M = 240 -K = 80 -N = 350 +M = TILE_M*21 +K = TILE_K*7 +N = TILE_M*12 rng = np.random.default_rng(42) A = rng.random((M, K), dtype=np.float32) @@ -62,21 +108,27 @@ def gemm_tiled(A: wp.array2d(dtype=float), B_wp = wp.array(B) C_wp = wp.array(C) -iters = 100 +iters = 10 with wp.ScopedTimer("NumPy"): for i in range(iters): C = A@B -#wp.force_load() +wp.force_load(device="cuda:0") -with wp.ScopedTimer("Warp", cuda_flags=wp.TIMING_KERNEL): +with wp.ScopedTimer("Warp", cuda_filter=wp.TIMING_KERNEL): for i in range(iters): wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) - -print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) + + print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) + + for i in range(iters): + wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=256) + + + print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) From b98b7069d2c095247a84c0082e785be0be99379d Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 3 May 2024 16:19:49 +1200 Subject: [PATCH 004/102] Fix typo --- warp/tests/test_tile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 4383f428..f5e768fe 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -82,7 +82,7 @@ def gemm_tiled(A: wp.array2d(dtype=float), N = B.shape[1] K = A.shape[1] - count = int(K / 16) # TODO: code-gen bug if you use a constant before passing it to a kwd arg (in this case TILE_K) + count = int(K / 8) # TODO: code-gen bug if you use a constant before passing it to a kwd arg (in this case TILE_K) for k in range(count): From 0c196e4a4828c17374bb7a9e3e9029d2b0ad3a5b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 10 May 2024 11:49:36 +1200 Subject: [PATCH 005/102] Working tile/partition based GEMM, experiments with CUTLASS/CuTe --- warp/build_dll.py | 8 +- warp/config.py | 2 +- warp/examples/benchmarks/benchmark_tile.py | 182 ++++++++++++ warp/native/array.h | 9 +- warp/native/builtin.h | 6 +- warp/native/mat.h | 11 +- warp/native/tile.h | 307 ++++++++++++++++++++- warp/native/warp.cu | 11 +- warp/tests/test_tile.py | 32 ++- 9 files changed, 538 insertions(+), 30 deletions(-) create mode 100644 warp/examples/benchmarks/benchmark_tile.py diff --git a/warp/build_dll.py b/warp/build_dll.py index 25692261..6810d9c7 100644 --- a/warp/build_dll.py +++ b/warp/build_dll.py @@ -275,10 +275,10 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None cu_out = cu_path + ".o" if mode == "debug": - cuda_cmd = f'"{cuda_home}/bin/nvcc" --compiler-options=/MT,/Zi,/Od -g -G -O0 -DNDEBUG -D_ITERATOR_DEBUG_LEVEL=0 -I"{native_dir}" -I"{nanovdb_home}" -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 --compiler-options=/MT,/Zi,/Od -g -G -O0 -DNDEBUG -D_ITERATOR_DEBUG_LEVEL=0 -I"{native_dir}" -I"{nanovdb_home}" -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' elif mode == "release": - cuda_cmd = f'"{cuda_home}/bin/nvcc" -O3 {" ".join(nvcc_opts)} -I"{native_dir}" -I"{nanovdb_home}" -DNDEBUG -DWP_ENABLE_CUDA=1 -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -O3 {" ".join(nvcc_opts)} -I"{native_dir}" -I"{nanovdb_home}" -DNDEBUG -DWP_ENABLE_CUDA=1 -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' with ScopedTimer("build_cuda", active=args.verbose): run_cmd(cuda_cmd) @@ -330,10 +330,10 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None cu_out = cu_path + ".o" if mode == "debug": - cuda_cmd = f'"{cuda_home}/bin/nvcc" -g -G -O0 --compiler-options -fPIC,-fvisibility=hidden -D_DEBUG -D_ITERATOR_DEBUG_LEVEL=0 -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -g -G -O0 --compiler-options -fPIC,-fvisibility=hidden -D_DEBUG -D_ITERATOR_DEBUG_LEVEL=0 -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' elif mode == "release": - cuda_cmd = f'"{cuda_home}/bin/nvcc" -O3 --compiler-options -fPIC,-fvisibility=hidden {" ".join(nvcc_opts)} -DNDEBUG -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -O3 --compiler-options -fPIC,-fvisibility=hidden {" ".join(nvcc_opts)} -DNDEBUG -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' with ScopedTimer("build_cuda", active=args.verbose): run_cmd(cuda_cmd) diff --git a/warp/config.py b/warp/config.py index 221fc252..ef16adf4 100644 --- a/warp/config.py +++ b/warp/config.py @@ -25,7 +25,7 @@ None # preferred CUDA output format for kernels ("ptx" or "cubin"), determined automatically if unspecified ) -ptx_target_arch: int = 70 # target architecture for PTX generation, defaults to the lowest architecture that supports all of Warp's features +ptx_target_arch: int = 80 # target architecture for PTX generation, defaults to the lowest architecture that supports all of Warp's features enable_backward: bool = True # whether to compiler the backward passes of the kernels diff --git a/warp/examples/benchmarks/benchmark_tile.py b/warp/examples/benchmarks/benchmark_tile.py new file mode 100644 index 00000000..1918684a --- /dev/null +++ b/warp/examples/benchmarks/benchmark_tile.py @@ -0,0 +1,182 @@ +import numpy as np +import warp as wp + +import torch + +wp.init() +wp.set_module_options({"enable_backward": False, "fast_math": True}) +wp.set_device("cuda:0") + +wp.build.clear_kernel_cache() + +@wp.kernel +def gemm(A: wp.array2d(dtype=float), + B: wp.array2d(dtype=float), + C: wp.array2d(dtype=float)): + + # output index + i, j = wp.tid() + + sum = float(0.0) + + for k in range(0, A.shape[1]): + sum += A[i, k]*B[k, j] + + C[i, j] = sum + + + +TILE_M = wp.constant(64) +TILE_N = wp.constant(64) +TILE_K = wp.constant(8) + +@wp.kernel +def gemm_tiled(A: wp.array2d(dtype=float), + B: wp.array2d(dtype=float), + C: wp.array2d(dtype=float)): + + # output tile index + i, j = wp.tid() + + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + + M = A.shape[0] + N = B.shape[1] + K = A.shape[1] + + count = int(K / 8) # TODO: code-gen bug if you use a constant before passing it to a kwd arg (in this case TILE_K) + + for k in range(count): + + a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) + b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) + + # sum += a*b + wp.tile_matmul(a, b, sum) + + wp.tile_store(C, i, j, sum) + + +def benchmark_numpy(A, B, C): + + timers = {} + iters = 10 + + # warm up + for i in range(10): + C = A@B + + with wp.ScopedTimer("NumPy", dict=timers): + + for i in range(iters): + C = A@B + + return min(timers["NumPy"]) + + +def benchmark_warp_simt(A, B, C): + + timers = {} + iters = 10 + + A_wp = wp.array(A) + B_wp = wp.array(B) + C_wp = wp.array(C) + + # warm up + for i in range(10): + wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) + + with wp.ScopedTimer("Warp (SIMT)", dict=timers, print=False, synchronize=True): + + for i in range(iters): + wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) + + return min(timers["Warp (SIMT)"]) + + +def benchmark_warp_tiled(A, B, C): + + timers = {} + iters = 10 + + num_threads = 256#TILE_M*TILE_N + + A_wp = wp.array(A) + B_wp = wp.array(B) + C_wp = wp.array(C) + + # warm up + for i in range(10): + wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) + + with wp.ScopedTimer("Warp (Tiled)", dict=timers, print=False, synchronize=True): + + for i in range(iters): + wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) + + wp.synchronize() + + return min(timers["Warp (Tiled)"]) + + +def benchmark_torch(A, B, C): + + A_tc = torch.from_numpy(A).to("cuda:0") + B_tc = torch.from_numpy(B).to("cuda:0") + C_tc = torch.from_numpy(C).to("cuda:0") + + # warm-up + for i in range(10): + torch.matmul(A_tc, B_tc, out=C_tc) + + timers = {} + iters = 10 + + torch.cuda.synchronize() + + with wp.ScopedTimer("Torch", dict=timers, print=False): + + for i in range(iters): + torch.matmul(A_tc, B_tc)#, out=C_tc) + + torch.cuda.synchronize() + + return min(timers["Torch"]) + + + +results_torch = [] +results_warp_simt = [] +results_warp_tiled = [] + +print("{:>8s} {:>8s} {:>8s} {:>8s} {:>8s} {:>8s}".format("M", "N", "K", "Torch", "Warp (SIMT)", "Warp (Tiled)")) +print("--------------------------------------------------------") + +for i in range(2, 33): + + M = i*128 + N = M + K = N + + # M = TILE_M*21 + # K = TILE_K*7 + # N = TILE_M*12 + + rng = np.random.default_rng(42) + + A = rng.random((M, K), dtype=np.float32) + B = rng.random((K, N), dtype=np.float32) + C = np.zeros((M, N), dtype=np.float32) + + results_torch.append(benchmark_torch(A, B, C)) + results_warp_simt.append(0.0)#benchmark_warp_simt(A, B, C)) + results_warp_tiled.append(benchmark_warp_tiled(A, B, C)) + + print("{:>8d} {:>8d} {:>8d} {:>8f} {:>8f} {:>8f}".format(M, N, K, results_torch[-1], results_warp_simt[-1], results_warp_tiled[-1])) + + + + + + diff --git a/warp/native/array.h b/warp/native/array.h index e9098c87..e1acebcf 100644 --- a/warp/native/array.h +++ b/warp/native/array.h @@ -269,11 +269,12 @@ CUDA_CALLABLE inline size_t byte_offset(const array_t& arr, int i) template CUDA_CALLABLE inline size_t byte_offset(const array_t& arr, int i, int j) { - if (i < 0 || i >= arr.shape[0]) - printf("i: %d > arr.shape[0]: %d\n", i, arr.shape[0]); + // if (i < 0 || i >= arr.shape[0]) + // printf("i: %d > arr.shape[0]: %d\n", i, arr.shape[0]); + + // if (j < 0 || j >= arr.shape[1]) + // printf("j: %d > arr.shape[1]: %d\n", j, arr.shape[1]); - if (j < 0 || j >= arr.shape[1]) - printf("j: %d > arr.shape[1]: %d\n", j, arr.shape[1]); assert(i >= 0 && i < arr.shape[0]); assert(j >= 0 && j < arr.shape[1]); diff --git a/warp/native/builtin.h b/warp/native/builtin.h index 97737567..682230dd 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1558,4 +1558,8 @@ inline CUDA_CALLABLE void adj_expect_near(const vec3& actual, const vec3& expect #include "rand.h" #include "noise.h" #include "matnn.h" -#include "tile.h" \ No newline at end of file + +// only include in kernels for now +#if defined(__CUDACC_RTC__) +#include "tile.h" +#endif \ No newline at end of file diff --git a/warp/native/mat.h b/warp/native/mat.h index f12733e9..56f86624 100644 --- a/warp/native/mat.h +++ b/warp/native/mat.h @@ -518,13 +518,18 @@ inline CUDA_CALLABLE mat_t mul(const mat_t& a { mat_t t(0); for (unsigned i=0; i < Rows; ++i) - { - for (unsigned j=0; j < ColsOut; ++j) + { + for (unsigned j=0; j < ColsOut; ++j) { + Type sum(0.0); + for (unsigned k=0; k < Cols; ++k) { - t.data[i][j] += a.data[i][k]*b.data[k][j]; + //t.data[i][j] += a.data[i][k]*b.data[k][j]; + sum = fmaf(a.data[i][k], b.data[k][j], sum); } + + t.data[i][j] = sum; } } diff --git a/warp/native/tile.h b/warp/native/tile.h index c6eab8f0..5becab3d 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -2,6 +2,10 @@ #include "builtin.h" +#include "cuda_pipeline_primitives.h" + +//#include "cutlass/include/cute/tensor.hpp" + // #define WP_CONCAT(x, y) x ## y // #define WP_TILE_SHARED_MEM(name, id) WP_CONCAT(name, id) @@ -20,17 +24,118 @@ #define WP_TILE_SYNC __syncthreads #endif + namespace wp { +// CUTLASS_PRAGMA_(UNROLL|NO_UNROLL) optimization directives for the CUDA compiler. +#if defined(__CUDA_ARCH__) && !defined(__INTELLISENSE__) + #if defined(__CUDACC_RTC__) || (defined(__clang__) && defined(__CUDA__)) + #define WP_PRAGMA_UNROLL _Pragma("unroll") + #define WP_PRAGMA_NO_UNROLL _Pragma("unroll 1") + #else + #define WP_PRAGMA_UNROLL #pragma unroll + #define WP_PRAGMA_NO_UNROLL #pragma unroll 1 + #endif + +#else + + #define WP_PRAGMA_UNROLL + #define WP_PRAGMA_NO_UNROLL + +#endif + +#if 0 +template + +CUDA_CALLABLE inline void +gemm_device(TA const* smemA, ASmemLayout sA_layout, AThreadLayout tA, + TB const* smemB, BSmemLayout sB_layout, BThreadLayout tB, + TC * smemC, CSmemLayout sC_layout, CThreadLayout tC) +{ + using namespace cute; + + static_assert(is_static::value); + static_assert(is_static::value); + static_assert(is_static::value); + + + static_assert(is_static::value); + static_assert(is_static::value); + static_assert(is_static::value); + + + Tensor sA = make_tensor(make_smem_ptr(smemA), sA_layout); // (BLK_M,BLK_K) + Tensor sB = make_tensor(make_smem_ptr(smemB), sB_layout); // (BLK_N,BLK_K) + Tensor sC = make_tensor(make_smem_ptr(smemC), sC_layout); // (BLK_M,BLK_K) + + + Tensor tAsA = local_partition(sA, tA, threadIdx.x); // (THR_M,THR_K) + Tensor tBsB = local_partition(sB, tB, threadIdx.x); // (THR_N,THR_K) + + + // Partition sA (M,K) by the rows of tC + Tensor tCsA = local_partition(sA, tC, threadIdx.x, Step{}); // (THR_M,BLK_K) + // Partition sB (K,M) by the rows of tC + Tensor tCsB = local_partition(sB, tC, threadIdx.x, Step<_1, X>{}); // (THR_N,BLK_K) + + // Partition gC (M,N) by the tile of tC + Tensor tCsC = local_partition(sC, tC, threadIdx.x, Step<_1,_1>{}); // (THR_M,THR_N) + + // Allocate the accumulators -- same shape/layout as the partitioned data + Tensor tCrC = make_tensor_like(tCsC); // (THR_M,THR_N) + + //******************* + // MM-QUESTION: this is not quite right, we need a 3d shape, but should we use local_partition or local_tile? + auto K_TILE_MAX = 1;//size<2>(tAsA); + + // ensure smem is ready + __syncthreads(); + + if (threadIdx.x == 0 && blockIdx.x == 0) + { + print(sA); printf("\n"); + print(sB); printf("\n"); + print(sC); printf("\n"); + + print(tCsA); printf("\n"); + print(tCsB); printf("\n"); + print(tCsC); printf("\n"); + } + + for (int k_tile = 0; k_tile < K_TILE_MAX; ++k_tile) + { + // Copy gmem to smem with tA|tB thread-partitioned tensors + // copy(tAgA(_,_,k_tile), tAsA); // A (THR_M,THR_K) -> (THR_M,THR_K) + // copy(tBgB(_,_,k_tile), tBsB); // B (THR_N,THR_K) -> (THR_N,THR_K) + + //******************* + // MM-QUESTION: how to 'advance' tCsA and tCsB to next tile in smem instead of above copy from global? + gemm(tCsA, tCsB, tCrC); + } + + CUTE_UNROLL + for (int i = 0; i < size(tCsA); ++i) { + tCsC(i) += tCrC(i); + } + + // ensure writes to shared are visible + __syncthreads(); +} + +#endif + // 2D tile zero template inline CUDA_CALLABLE array_t tile_zeros() { const int length = M*N; - WP_TILE_SHARED T data[length]; + WP_TILE_SHARED __align__(16) T data[length]; + WP_PRAGMA_UNROLL for (int t=threadIdx.x; t < length; t += blockDim.x) { data[t] = T(0.0); @@ -45,14 +150,30 @@ inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) { const int length = M*N; - WP_TILE_SHARED T data[length]; + WP_TILE_SHARED __align__(16) T data[length]; // cooperatively load the tile, using a block-stride iterator // todo: use cub::BlockLoad or cg::memcpy_async()? - for (int t=threadIdx.x; t < length; t += blockDim.x) + + // WP_PRAGMA_UNROLL + // for (int t=threadIdx.x; t < length; t += blockDim.x) + // { + // data[t] = index(src, i*M + t/N, j*N + t%N); + // } + + // // async copies + WP_PRAGMA_UNROLL + for (int t=threadIdx.x*4; t < length; t += blockDim.x*4) { - data[t] = index(src, i*M + t/N, j*N + t%N); + //data[t] = index(src, i*M + t/N, j*N + t%N); + __pipeline_memcpy_async(&data[t], + &index(src, i*M + t/N, j*N + t%N), + sizeof(T)*4); } + + __pipeline_commit(); + __pipeline_wait_prior(0); + return array_t(data, M, N, nullptr); } @@ -68,35 +189,202 @@ inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array // cooperatively store the tile, using a block-stride iterator // todo: use cub::BlockStore or cg::memcpy_async()? + WP_PRAGMA_UNROLL for (int t=threadIdx.x; t < length; t += blockDim.x) { index(dest, i*M + t/N, j*N + t%N) = src.data[t]; } } +// template +// inline CUDA_CALLABLE void tile_matmul_cute(const array_t& A, const array_t& B, const array_t& out) +// { +// using namespace cute; + +// // Define CTA matrix size (static) + +// auto bM = Int<64>{}; +// auto bN = Int<64>{}; +// auto bK = Int<8>{}; + +// auto cta_tiler = make_shape(bM, bN, bK); // (BLK_M, BLK_N, BLK_K) + +// // Define the smem layouts (static) +// auto sA = make_layout(make_shape(bM,bK), LayoutRight{}); +// auto sB = make_layout(make_shape(bN,bK)); +// auto sC = make_layout(make_shape(bM, bN), LayoutRight{}); + +// // Define the thread layouts (static) +// auto tA = make_layout(make_shape(Int<32>{}, Int< 8>{}), LayoutRight{}); +// auto tB = make_layout(make_shape(Int<32>{}, Int< 8>{}), LayoutRight{}); +// auto tC = make_layout(make_shape(Int<16>{}, Int<16>{}), LayoutRight{}); + +// gemm_device +// (A.data, sA, tA, +// B.data, sB, tB, +// out.data,sC, tC); +// } + + +template +inline CUDA_CALLABLE const T& index(const T* __restrict__ p, int i, int j, int stride) +{ + return p[i*stride + j]; +} + +template +inline CUDA_CALLABLE T& index(T* __restrict__ p, int i, int j, int stride) +{ + return p[i*stride + j]; +} + +template +struct partition_t +{ + partition_t(array_t A) + { + data = A; + + // todo: do ceil div for non-multiples of M,N + shape[0] = A.shape[0]/M; + shape[1] = A.shape[1]/N; + } + + // underlying data + array_t data; + + // partition dimensions + int shape[2]; +}; + +template +int partition_size(const partition_t& tile) +{ + return tile.shape[0]*tile.shape[1]; +} + +// returns the x, y coordinates of a tile given a linear index +template +void partition_coord(const partition_t& tile, const int t, int& i, int& j) +{ + i = t/tile.shape[1]; + j = t%tile.shape[1]; +} + +template +mat_t partition_load(const partition_t& tile, int i, int j) +{ + mat_t out; + + const int tile_i = i*M; + const int tile_j = j*N; + + // WP_PRAGMA_UNROLL + // for (int i=0; i < M; ++i) + // { + // WP_PRAGMA_UNROLL + // for (int j=0; j < N; ++j) + // { + // out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); + // } + // } + + + return out; +} + +template +void partition_store(const partition_t& tile, int i, int j, const mat_t& value) +{ + mat_t out; + + const int tile_i = M*i; + const int tile_j = N*j; + + WP_PRAGMA_UNROLL + for (int i=0; i < M; ++i) + { + WP_PRAGMA_UNROLL + for (int j=0; j < N; ++j) + { + index(tile.data, tile_i + i, tile_j + j) = value.data[i][j]; + } + } +} + -// 2D gemm accumulate out += A*B template inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) +{ + const int TILE_M = 4; + const int TILE_N = 4; + const int TILE_K = 4; + + partition_t A_tile = partition_t(A); + partition_t B_tile = partition_t(B); + partition_t C_tile = partition_t(out); + + const int length = partition_size(C_tile); + + WP_TILE_SYNC(); + + WP_PRAGMA_UNROLL + for (int t=threadIdx.x; t < length; t += blockDim.x) + { + int i, j; + partition_coord(C_tile, t, i, j); + + // accumulator + mat_t sum = partition_load(C_tile, i, j); + + WP_PRAGMA_UNROLL + for (int k=0; k < A_tile.shape[1]; ++k) + { + mat_t a = partition_load(A_tile, i, k); + mat_t b = partition_load(B_tile, k, j); + + sum += mul(a, b); + } + + partition_store(C_tile, i, j, sum); + } + + WP_TILE_SYNC(); +} + + + +// 2D gemm accumulate out += A*B +template +inline CUDA_CALLABLE void tile_matmul_scalar(const array_t& A, const array_t& B, const array_t& out) { const int length = out.shape[0]*out.shape[1]; WP_TILE_SYNC(); + const T* __restrict__ A_ptr = A.data; + const T* __restrict__ B_ptr = B.data; + T* __restrict__ C_ptr = out.data; + + WP_PRAGMA_UNROLL for (int t=threadIdx.x; t < length; t += blockDim.x) { // compute output index const int i = t/out.shape[1]; const int j = t%out.shape[1]; - T sum = T(0.0); + T sum(0.0); + WP_PRAGMA_UNROLL for (int k=0; k < A.shape[1]; ++k) { - sum += index(A, i, k)*index(B, k, j); - } + T a = index(A_ptr, i, k, A.shape[1]); + T b = index(B_ptr, k, j, B.shape[1]); - index(out, i, j) += sum; + sum = fmaf(a, b, sum); + } + + index(C_ptr, i, j, out.shape[1]) += sum; } WP_TILE_SYNC(); @@ -104,5 +392,4 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, - } // namespace wp \ No newline at end of file diff --git a/warp/native/warp.cu b/warp/native/warp.cu index f921a303..eac06ebc 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -2536,7 +2536,7 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ std::vector opts; opts.push_back(arch_opt); opts.push_back(include_opt); - opts.push_back("--std=c++11"); + opts.push_back("--std=c++17"); if (debug) { @@ -2556,6 +2556,15 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ if (fast_math) opts.push_back("--use_fast_math"); + char include_cutlass[max_path]; + sprintf(include_cutlass, "--include-path=%s/cutlass/include", include_dir); + opts.push_back(include_cutlass); + + //opts.push_back("--include-path=_build/target-deps/cuda/include"); + opts.push_back("--include-path=C:\\packman-repo\\chk\\cuda\\11.8.0_522.06-abe3d9d7-windows-x86_64\\include"); + + opts.push_back("--device-as-default-execution-space"); + nvrtcProgram prog; nvrtcResult res; diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index f5e768fe..921f269e 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -1,6 +1,8 @@ import numpy as np import warp as wp +import torch + #wp.config.mode = "debug" wp.init() @@ -64,8 +66,8 @@ def gemm(A: wp.array2d(dtype=float), -TILE_M = wp.constant(16) -TILE_N = wp.constant(16) +TILE_M = wp.constant(64) +TILE_N = wp.constant(64) TILE_K = wp.constant(8) @wp.kernel @@ -95,9 +97,9 @@ def gemm_tiled(A: wp.array2d(dtype=float), wp.tile_store(C, i, j, sum) -M = TILE_M*21 -K = TILE_K*7 -N = TILE_M*12 +M = TILE_M*7 +K = TILE_K*4 +N = TILE_N*6 rng = np.random.default_rng(42) A = rng.random((M, K), dtype=np.float32) @@ -126,9 +128,27 @@ def gemm_tiled(A: wp.array2d(dtype=float), print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) for i in range(iters): - wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=256) + wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=128) + wp.synchronize() print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) +A_tc = torch.from_numpy(A).to("cuda:0") +B_tc = torch.from_numpy(B).to("cuda:0") +C_tc = torch.from_numpy(C).to("cuda:0") + +for i in range(10): + torch.matmul(A_tc, B_tc, out=C_tc) + +with wp.ScopedTimer("Torch"): + + for i in range(iters): + torch.matmul(A_tc, B_tc, out=C_tc) + + torch.cuda.synchronize() + + + + From 054155b1c4b9b8b23380120103622195799f529e Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 10 May 2024 11:51:21 +1200 Subject: [PATCH 006/102] Re-enable partition load, currently at 2.8ms for 1024x1024 fp32 GEMM --- warp/native/tile.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index 5becab3d..7c1c45c0 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -279,15 +279,15 @@ mat_t partition_load(const partition_t& tile, int i, int j) const int tile_i = i*M; const int tile_j = j*N; - // WP_PRAGMA_UNROLL - // for (int i=0; i < M; ++i) - // { - // WP_PRAGMA_UNROLL - // for (int j=0; j < N; ++j) - // { - // out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); - // } - // } + WP_PRAGMA_UNROLL + for (int i=0; i < M; ++i) + { + WP_PRAGMA_UNROLL + for (int j=0; j < N; ++j) + { + out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); + } + } return out; From 560d19408e1fff47a4e791f4802bebc1dddc8654 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 13 May 2024 15:50:37 +1200 Subject: [PATCH 007/102] Some more experiments with coalesced loads from shared memory --- build_lib.py | 1 + warp/native/tile.h | 47 ++++++++++++++++++++++++++++++--------------- warp/native/warp.cu | 2 ++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/build_lib.py b/build_lib.py index 7a33bd8b..e781f025 100644 --- a/build_lib.py +++ b/build_lib.py @@ -52,6 +52,7 @@ parser.set_defaults(fast_math=False) parser.add_argument("--quick", action="store_true", help="Only generate PTX code, disable CUTLASS ops") +parser.set_defaults(quick=True) parser.add_argument("--build_llvm", action="store_true", help="Build Clang/LLVM compiler from source, default disabled") parser.add_argument("--no_build_llvm", dest="build_llvm", action="store_false") diff --git a/warp/native/tile.h b/warp/native/tile.h index 7c1c45c0..d2c55ff7 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -161,20 +161,21 @@ inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) // data[t] = index(src, i*M + t/N, j*N + t%N); // } - // // async copies + // // async copies (assumes row-major i.e.: stride 1 on y axis) + const int s = 4; + WP_PRAGMA_UNROLL - for (int t=threadIdx.x*4; t < length; t += blockDim.x*4) + for (int t=threadIdx.x*s; t < length; t += blockDim.x*s) { //data[t] = index(src, i*M + t/N, j*N + t%N); __pipeline_memcpy_async(&data[t], &index(src, i*M + t/N, j*N + t%N), - sizeof(T)*4); + sizeof(T)*s); } __pipeline_commit(); - __pipeline_wait_prior(0); - + return array_t(data, M, N, nullptr); } @@ -187,7 +188,7 @@ inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array const int length = M*N; - // cooperatively store the tile, using a block-stride iterator + // cooperatively store the tile, using a block-stride iterator // todo: use cub::BlockStore or cg::memcpy_async()? WP_PRAGMA_UNROLL for (int t=threadIdx.x; t < length; t += blockDim.x) @@ -241,7 +242,7 @@ inline CUDA_CALLABLE T& index(T* __restrict__ p, int i, int j, int stride) template struct partition_t { - partition_t(array_t A) + inline partition_t(array_t A) { data = A; @@ -258,21 +259,21 @@ struct partition_t }; template -int partition_size(const partition_t& tile) +inline int partition_size(const partition_t& tile) { return tile.shape[0]*tile.shape[1]; } // returns the x, y coordinates of a tile given a linear index template -void partition_coord(const partition_t& tile, const int t, int& i, int& j) +inline void partition_coord(const partition_t& tile, const int t, int& i, int& j) { i = t/tile.shape[1]; j = t%tile.shape[1]; } template -mat_t partition_load(const partition_t& tile, int i, int j) +inline mat_t partition_load(const partition_t& tile, int i, int j) { mat_t out; @@ -288,13 +289,28 @@ mat_t partition_load(const partition_t& tile, int i, int j) out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); } } + + // Specialization for when N = 4 and assumes data was swizzled into 4x4 blocks + // during tile_load(), this results in zero bank conflicts + 128 bit loads + + // const int tile_index = i*N + j; + // const int tile_count = partition_size(tile); + + // float4* out4 = (float4*)(&out.data[0][0]); + + // WP_PRAGMA_UNROLL + // for (int t=0; t < M; t += 4) + // { + // out4[t] = ((float4*)(tile.data.data))[t*tile_count + tile_index]; + // } + return out; } template -void partition_store(const partition_t& tile, int i, int j, const mat_t& value) +inline void partition_store(const partition_t& tile, int i, int j, const mat_t& value) { mat_t out; @@ -326,9 +342,10 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const int length = partition_size(C_tile); + __pipeline_wait_prior(0); + WP_TILE_SYNC(); - WP_PRAGMA_UNROLL for (int t=threadIdx.x; t < length; t += blockDim.x) { int i, j; @@ -338,10 +355,10 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, mat_t sum = partition_load(C_tile, i, j); WP_PRAGMA_UNROLL - for (int k=0; k < A_tile.shape[1]; ++k) + for (int k=0; k < A_tile.shape[1]; k++) { - mat_t a = partition_load(A_tile, i, k); - mat_t b = partition_load(B_tile, k, j); + const mat_t a = partition_load(A_tile, i, k); + const mat_t b = partition_load(B_tile, k, j); sum += mul(a, b); } diff --git a/warp/native/warp.cu b/warp/native/warp.cu index eac06ebc..90468965 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -2564,6 +2564,8 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ opts.push_back("--include-path=C:\\packman-repo\\chk\\cuda\\11.8.0_522.06-abe3d9d7-windows-x86_64\\include"); opts.push_back("--device-as-default-execution-space"); + opts.push_back("--extra-device-vectorization"); + opts.push_back("--restrict"); nvrtcProgram prog; From 8f1aed1b537dc4c267492c1c2b2f22d2de16c4d4 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 13 May 2024 15:51:06 +1200 Subject: [PATCH 008/102] Use CUDA graphs in benchmark_tile.py --- warp/examples/benchmarks/benchmark_tile.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/warp/examples/benchmarks/benchmark_tile.py b/warp/examples/benchmarks/benchmark_tile.py index 1918684a..fc5900fe 100644 --- a/warp/examples/benchmarks/benchmark_tile.py +++ b/warp/examples/benchmarks/benchmark_tile.py @@ -100,22 +100,31 @@ def benchmark_warp_tiled(A, B, C): timers = {} iters = 10 - num_threads = 256#TILE_M*TILE_N + # must match with the tile_matmul() partition size + SUB_TILE_M = 4 + SUB_TILE_N = 4 + + num_threads = int(TILE_M/SUB_TILE_M)*int(TILE_N/SUB_TILE_N); A_wp = wp.array(A) B_wp = wp.array(B) C_wp = wp.array(C) # warm up + wp.capture_begin() + for i in range(10): wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) + graph = wp.capture_end() + + with wp.ScopedTimer("Warp (Tiled)", dict=timers, print=False, synchronize=True): - for i in range(iters): - wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) + #for i in range(iters): + # wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) + wp.capture_launch(graph) - wp.synchronize() return min(timers["Warp (Tiled)"]) @@ -155,6 +164,8 @@ def benchmark_torch(A, B, C): for i in range(2, 33): +#for i in range(8,9): + M = i*128 N = M K = N From 58a834ce7ba6486b9e73aa1dafc71dc51dabf8da Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 22 May 2024 15:39:47 +1200 Subject: [PATCH 009/102] Remove some experiments with CuTe and clean up some dead code --- warp/native/tile.h | 279 ++++++++++------------------------------ warp/tests/test_tile.py | 2 +- 2 files changed, 71 insertions(+), 210 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index d2c55ff7..df1f8ff1 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -2,20 +2,9 @@ #include "builtin.h" +// todo: requires CTK, replace with inline ptx #include "cuda_pipeline_primitives.h" -//#include "cutlass/include/cute/tensor.hpp" - -// #define WP_CONCAT(x, y) x ## y -// #define WP_TILE_SHARED_MEM(name, id) WP_CONCAT(name, id) - -// #define zero(a) memset(a, 0, sizeof(a)); - -// #define tile_zeros(a, b, dtype) [](){\ -// static dtype WP_TILE_SHARED_MEM(data_, __LINE__)[a][b]; \ -// zero(WP_TILE_SHARED_MEM(data_, __LINE__)); \ -// return array_tWP_TILE_SHARED_MEM(data_, __LINE__; )}() - #if !defined(__CUDA_ARCH__) #define WP_TILE_SHARED static #define WP_TILE_SYNC void @@ -45,87 +34,6 @@ namespace wp #endif -#if 0 -template - -CUDA_CALLABLE inline void -gemm_device(TA const* smemA, ASmemLayout sA_layout, AThreadLayout tA, - TB const* smemB, BSmemLayout sB_layout, BThreadLayout tB, - TC * smemC, CSmemLayout sC_layout, CThreadLayout tC) -{ - using namespace cute; - - static_assert(is_static::value); - static_assert(is_static::value); - static_assert(is_static::value); - - - static_assert(is_static::value); - static_assert(is_static::value); - static_assert(is_static::value); - - - Tensor sA = make_tensor(make_smem_ptr(smemA), sA_layout); // (BLK_M,BLK_K) - Tensor sB = make_tensor(make_smem_ptr(smemB), sB_layout); // (BLK_N,BLK_K) - Tensor sC = make_tensor(make_smem_ptr(smemC), sC_layout); // (BLK_M,BLK_K) - - - Tensor tAsA = local_partition(sA, tA, threadIdx.x); // (THR_M,THR_K) - Tensor tBsB = local_partition(sB, tB, threadIdx.x); // (THR_N,THR_K) - - - // Partition sA (M,K) by the rows of tC - Tensor tCsA = local_partition(sA, tC, threadIdx.x, Step{}); // (THR_M,BLK_K) - // Partition sB (K,M) by the rows of tC - Tensor tCsB = local_partition(sB, tC, threadIdx.x, Step<_1, X>{}); // (THR_N,BLK_K) - - // Partition gC (M,N) by the tile of tC - Tensor tCsC = local_partition(sC, tC, threadIdx.x, Step<_1,_1>{}); // (THR_M,THR_N) - - // Allocate the accumulators -- same shape/layout as the partitioned data - Tensor tCrC = make_tensor_like(tCsC); // (THR_M,THR_N) - - //******************* - // MM-QUESTION: this is not quite right, we need a 3d shape, but should we use local_partition or local_tile? - auto K_TILE_MAX = 1;//size<2>(tAsA); - - // ensure smem is ready - __syncthreads(); - - if (threadIdx.x == 0 && blockIdx.x == 0) - { - print(sA); printf("\n"); - print(sB); printf("\n"); - print(sC); printf("\n"); - - print(tCsA); printf("\n"); - print(tCsB); printf("\n"); - print(tCsC); printf("\n"); - } - - for (int k_tile = 0; k_tile < K_TILE_MAX; ++k_tile) - { - // Copy gmem to smem with tA|tB thread-partitioned tensors - // copy(tAgA(_,_,k_tile), tAsA); // A (THR_M,THR_K) -> (THR_M,THR_K) - // copy(tBgB(_,_,k_tile), tBsB); // B (THR_N,THR_K) -> (THR_N,THR_K) - - //******************* - // MM-QUESTION: how to 'advance' tCsA and tCsB to next tile in smem instead of above copy from global? - gemm(tCsA, tCsB, tCrC); - } - - CUTE_UNROLL - for (int i = 0; i < size(tCsA); ++i) { - tCsC(i) += tCrC(i); - } - - // ensure writes to shared are visible - __syncthreads(); -} - -#endif // 2D tile zero template @@ -152,22 +60,22 @@ inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) WP_TILE_SHARED __align__(16) T data[length]; - // cooperatively load the tile, using a block-stride iterator - // todo: use cub::BlockLoad or cg::memcpy_async()? - + //--------------- + // naive-synchronous load + // // WP_PRAGMA_UNROLL // for (int t=threadIdx.x; t < length; t += blockDim.x) // { // data[t] = index(src, i*M + t/N, j*N + t%N); // } - // // async copies (assumes row-major i.e.: stride 1 on y axis) - const int s = 4; + //--------------- + // async 128 bit loads (assumes row-major i.e.: stride 1 on y axis and 4-element alignment on dimension) + const int s = 4; WP_PRAGMA_UNROLL for (int t=threadIdx.x*s; t < length; t += blockDim.x*s) { - //data[t] = index(src, i*M + t/N, j*N + t%N); __pipeline_memcpy_async(&data[t], &index(src, i*M + t/N, j*N + t%N), sizeof(T)*s); @@ -188,8 +96,7 @@ inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array const int length = M*N; - // cooperatively store the tile, using a block-stride iterator - // todo: use cub::BlockStore or cg::memcpy_async()? + // cooperatively store the tile, using a block-stride iterator WP_PRAGMA_UNROLL for (int t=threadIdx.x; t < length; t += blockDim.x) { @@ -197,36 +104,6 @@ inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array } } -// template -// inline CUDA_CALLABLE void tile_matmul_cute(const array_t& A, const array_t& B, const array_t& out) -// { -// using namespace cute; - -// // Define CTA matrix size (static) - -// auto bM = Int<64>{}; -// auto bN = Int<64>{}; -// auto bK = Int<8>{}; - -// auto cta_tiler = make_shape(bM, bN, bK); // (BLK_M, BLK_N, BLK_K) - -// // Define the smem layouts (static) -// auto sA = make_layout(make_shape(bM,bK), LayoutRight{}); -// auto sB = make_layout(make_shape(bN,bK)); -// auto sC = make_layout(make_shape(bM, bN), LayoutRight{}); - -// // Define the thread layouts (static) -// auto tA = make_layout(make_shape(Int<32>{}, Int< 8>{}), LayoutRight{}); -// auto tB = make_layout(make_shape(Int<32>{}, Int< 8>{}), LayoutRight{}); -// auto tC = make_layout(make_shape(Int<16>{}, Int<16>{}), LayoutRight{}); - -// gemm_device -// (A.data, sA, tA, -// B.data, sB, tB, -// out.data,sC, tC); -// } - - template inline CUDA_CALLABLE const T& index(const T* __restrict__ p, int i, int j, int stride) { @@ -242,103 +119,87 @@ inline CUDA_CALLABLE T& index(T* __restrict__ p, int i, int j, int stride) template struct partition_t { - inline partition_t(array_t A) - { - data = A; - - // todo: do ceil div for non-multiples of M,N - shape[0] = A.shape[0]/M; - shape[1] = A.shape[1]/N; - } - - // underlying data - array_t data; - - // partition dimensions - int shape[2]; + inline partition_t(array_t A) + { + data = A; + + // todo: do ceil div for non-multiples of M,N + shape[0] = A.shape[0]/M; + shape[1] = A.shape[1]/N; + } + + // underlying data + array_t data; + + // partition dimensions + int shape[2]; }; template inline int partition_size(const partition_t& tile) { - return tile.shape[0]*tile.shape[1]; + return tile.shape[0]*tile.shape[1]; } // returns the x, y coordinates of a tile given a linear index template inline void partition_coord(const partition_t& tile, const int t, int& i, int& j) { - i = t/tile.shape[1]; - j = t%tile.shape[1]; + i = t/tile.shape[1]; + j = t%tile.shape[1]; } template inline mat_t partition_load(const partition_t& tile, int i, int j) { - mat_t out; - - const int tile_i = i*M; - const int tile_j = j*N; - - WP_PRAGMA_UNROLL - for (int i=0; i < M; ++i) - { - WP_PRAGMA_UNROLL - for (int j=0; j < N; ++j) - { - out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); - } - } - - // Specialization for when N = 4 and assumes data was swizzled into 4x4 blocks - // during tile_load(), this results in zero bank conflicts + 128 bit loads - - // const int tile_index = i*N + j; - // const int tile_count = partition_size(tile); - - // float4* out4 = (float4*)(&out.data[0][0]); - - // WP_PRAGMA_UNROLL - // for (int t=0; t < M; t += 4) - // { - // out4[t] = ((float4*)(tile.data.data))[t*tile_count + tile_index]; - // } - - - - return out; + mat_t out; + + const int tile_i = i*M; + const int tile_j = j*N; + + WP_PRAGMA_UNROLL + for (int i=0; i < M; ++i) + { + WP_PRAGMA_UNROLL + for (int j=0; j < N; ++j) + { + out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); + } + } + + return out; } template inline void partition_store(const partition_t& tile, int i, int j, const mat_t& value) { - mat_t out; - - const int tile_i = M*i; - const int tile_j = N*j; - - WP_PRAGMA_UNROLL - for (int i=0; i < M; ++i) - { - WP_PRAGMA_UNROLL - for (int j=0; j < N; ++j) - { - index(tile.data, tile_i + i, tile_j + j) = value.data[i][j]; - } - } + mat_t out; + + const int tile_i = M*i; + const int tile_j = N*j; + + WP_PRAGMA_UNROLL + for (int i=0; i < M; ++i) + { + WP_PRAGMA_UNROLL + for (int j=0; j < N; ++j) + { + index(tile.data, tile_i + i, tile_j + j) = value.data[i][j]; + } + } } template inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) { - const int TILE_M = 4; - const int TILE_N = 4; - const int TILE_K = 4; + const int TILE_M = 4; + const int TILE_N = 4; + const int TILE_K = 4; - partition_t A_tile = partition_t(A); - partition_t B_tile = partition_t(B); - partition_t C_tile = partition_t(out); + partition_t A_tile = partition_t(A); + partition_t B_tile = partition_t(B); + partition_t C_tile = partition_t(out); const int length = partition_size(C_tile); @@ -348,22 +209,22 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, for (int t=threadIdx.x; t < length; t += blockDim.x) { - int i, j; - partition_coord(C_tile, t, i, j); + int i, j; + partition_coord(C_tile, t, i, j); - // accumulator - mat_t sum = partition_load(C_tile, i, j); + // accumulator + mat_t sum = partition_load(C_tile, i, j); WP_PRAGMA_UNROLL for (int k=0; k < A_tile.shape[1]; k++) { - const mat_t a = partition_load(A_tile, i, k); - const mat_t b = partition_load(B_tile, k, j); + const mat_t a = partition_load(A_tile, i, k); + const mat_t b = partition_load(B_tile, k, j); - sum += mul(a, b); + sum += mul(a, b); } - partition_store(C_tile, i, j, sum); + partition_store(C_tile, i, j, sum); } WP_TILE_SYNC(); @@ -390,7 +251,7 @@ inline CUDA_CALLABLE void tile_matmul_scalar(const array_t& A, const array_t< const int i = t/out.shape[1]; const int j = t%out.shape[1]; - T sum(0.0); + T sum(0.0); WP_PRAGMA_UNROLL for (int k=0; k < A.shape[1]; ++k) diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 921f269e..78fb7fc9 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -84,7 +84,7 @@ def gemm_tiled(A: wp.array2d(dtype=float), N = B.shape[1] K = A.shape[1] - count = int(K / 8) # TODO: code-gen bug if you use a constant before passing it to a kwd arg (in this case TILE_K) + count = int(K / 8) # todo: must be the same as TILE_K for k in range(count): From 3d92decbdbe907f5f966db1e6460adf710ced7b5 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 24 May 2024 13:30:51 +1200 Subject: [PATCH 010/102] Add CuTe implementation using `cute::cooperative_gem()` primitive --- warp/native/tile.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/warp/native/tile.h b/warp/native/tile.h index df1f8ff1..fa04f958 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -5,6 +5,13 @@ // todo: requires CTK, replace with inline ptx #include "cuda_pipeline_primitives.h" +#define USE_CUTE 0 + +#if USE_CUTE +#include "cutlass/include/cute/tensor.hpp" +#include "cutlass/include/cute/algorithm/cooperative_gemm.hpp" +#endif // USE_CUTE + #if !defined(__CUDA_ARCH__) #define WP_TILE_SHARED static #define WP_TILE_SYNC void @@ -190,6 +197,8 @@ inline void partition_store(const partition_t& tile, int i, int j, cons } +#if !USE_CUTE + template inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) { @@ -268,6 +277,51 @@ inline CUDA_CALLABLE void tile_matmul_scalar(const array_t& A, const array_t< WP_TILE_SYNC(); } +#else + + +template +inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) +{ + using namespace cute; + + __pipeline_wait_prior(0); + + // ensure smem tile is ready + WP_TILE_SYNC(); + + // Define CTA matrix size (static) + auto bM = Int<64>{}; + auto bN = Int<64>{}; + auto bK = Int<8>{}; + + // Define the smem layouts (static) + auto sA = make_layout(make_shape(bM, bK), LayoutRight{}); + auto sB = make_layout(make_shape(bN, bK)); + auto sC = make_layout(make_shape(bM, bN), LayoutRight{}); + + Tensor s_a_tensor = make_tensor(make_smem_ptr(A.data), sA); + Tensor s_b_tensor = make_tensor(make_smem_ptr(B.data), sB); + Tensor s_c_tensor = make_tensor(make_smem_ptr(out.data), sC); + + + TiledMMA tiled_mma = make_tiled_mma(UniversalFMA{}, + Layout>{}); // 16x8x1 UniversalFMA, assumes blockDim=128 + + + cooperative_gemm< AutoVectorizingCopyWithAssumedAlignment>, + AutoVectorizingCopyWithAssumedAlignment>, + AutoVectorizingCopyWithAssumedAlignment> + >( + threadIdx.x, tiled_mma, + 1.0f, s_a_tensor, s_b_tensor, 1.0f, s_c_tensor, + cute::identity(), cute::identity(), cute::identity(), cute::identity() + ); + + WP_TILE_SYNC(); + +} +#endif // USE_CUTE } // namespace wp \ No newline at end of file From f47d059d00e55714670861d477897bb77833c4ba Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 7 Jun 2024 08:02:19 +1200 Subject: [PATCH 011/102] First pass of Tile expressions working --- warp/builtins.py | 12 +- warp/codegen.py | 35 ++- warp/native/builtin.h | 1 + warp/native/tile.h | 474 +++++++++++++++++++++------------------- warp/native/tile_gemm.h | 310 ++++++++++++++++++++++++++ warp/tests/test_tile.py | 156 +++++++------ warp/types.py | 13 ++ 7 files changed, 700 insertions(+), 301 deletions(-) create mode 100644 warp/native/tile_gemm.h diff --git a/warp/builtins.py b/warp/builtins.py index e1860363..96dc4282 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1395,7 +1395,6 @@ def tile_zeros_value_func(arg_types, kwds, templates): return array(dtype=dtype) - add_builtin( "tile_zeros", input_types={"m": int, "n": int, "dtype": Scalar}, @@ -1431,17 +1430,18 @@ def tile_load_value_func(arg_types, kwds, templates): raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") m, n = kwds["m"], kwds["n"] + dtype = arg_types[0].dtype - templates.append(arg_types[0].dtype) + templates.append(dtype) templates.append(m) templates.append(n) global shared_memory_id - templates.append(shared_memory_id) + #templates.append(shared_memory_id) shared_memory_id += 1 - return array(dtype=arg_types[0].dtype) + return Tile(dtype, m, n, "load")#array(dtype=arg_types[0].dtype) @@ -1473,8 +1473,8 @@ def tile_store_value_func(arg_types, kwds, templates): if not type_is_int(arg_types[2]): raise RuntimeError("tile_store() argument 2 must be an integer") - if not is_array(arg_types[3]): - raise RuntimeError("tile_store() argument 3 must be an array") + if not is_tile(arg_types[3]): + raise RuntimeError("tile_store() argument 3 must be a tile") return None diff --git a/warp/codegen.py b/warp/codegen.py index a9972769..ae72c3ce 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -507,6 +507,8 @@ def type_to_ctype(t, value_type=False): dtypestr = f"wp::{t.dtype.__name__}" classstr = f"wp::{type(t).__name__}" return f"{classstr}_t<{dtypestr}>" + elif is_tile(t): + return "auto" elif isinstance(t, Struct): return make_full_qualified_name(t.cls) elif is_reference(t): @@ -1002,6 +1004,9 @@ def add_call(adj, func, args, min_outputs=None, templates=None, kwds=None): for i, a in enumerate(args) ] + # used to create an alias of the adjoint var to the primal var for tile ops + alias_call = None + if return_type is None: # handles expression (zero output) functions, e.g.: void do_something(); @@ -1024,10 +1029,16 @@ def add_call(adj, func, args, min_outputs=None, templates=None, kwds=None): output_list = [output] forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(args_var, use_initializer_list)});" + + # prepend auto if it is an anonymously typed var (e.g.: a tile op) + if output.ctype() == "auto": + forward_call = "auto " + forward_call + alias_call = f"auto& adj_{output} = var_{output};" + replay_call = forward_call if func.custom_replay_func is not None: replay_call = f"var_{output} = {func.namespace}replay_{func_name}({adj.format_forward_call_args(args_var, use_initializer_list)});" - + else: # handle multiple value functions @@ -1039,15 +1050,19 @@ def add_call(adj, func, args, min_outputs=None, templates=None, kwds=None): ) replay_call = forward_call + if func.skip_replay: adj.add_forward(forward_call, replay="// " + replay_call) else: adj.add_forward(forward_call, replay=replay_call) + if alias_call: + adj.add_forward(alias_call) + if not func.missing_grad and len(args): reverse_has_output_args = ( func.require_original_output_arg or len(output_list) > 1 - ) and func.custom_grad_func is None + ) and func.custom_grad_func is None arg_str = adj.format_reverse_call_args( args_var, args, @@ -2562,6 +2577,11 @@ def codegen_func_forward(adj, func_type="kernel", device="cpu"): lines += ["// primal vars\n"] for var in adj.variables: + + # do not predeclare vars with auto type + if var.ctype() == "auto": + continue + if var.constant is None: lines += [f"{var.ctype()} {var.emit()};\n"] else: @@ -2597,6 +2617,11 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): lines += ["// primal vars\n"] for var in adj.variables: + + # do not predeclare vars with auto type + if var.ctype() == "auto": + continue + if var.constant is None: lines += [f"{var.ctype()} {var.emit()};\n"] else: @@ -2607,7 +2632,11 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): lines += ["// dual vars\n"] for var in adj.variables: - lines += [f"{var.ctype(value_type=True)} {var.emit_adj()} = {{}};\n"] + name = var.emit_adj() + ctype = var.ctype(value_type=True) + + if ctype != "auto": + lines += [f"{ctype} {name} = {{}};\n"] # forward pass lines += ["//---------\n"] diff --git a/warp/native/builtin.h b/warp/native/builtin.h index 682230dd..a5788224 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1562,4 +1562,5 @@ inline CUDA_CALLABLE void adj_expect_near(const vec3& actual, const vec3& expect // only include in kernels for now #if defined(__CUDACC_RTC__) #include "tile.h" +//#include "tile_gemm.h" #endif \ No newline at end of file diff --git a/warp/native/tile.h b/warp/native/tile.h index fa04f958..618611f8 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -2,16 +2,6 @@ #include "builtin.h" -// todo: requires CTK, replace with inline ptx -#include "cuda_pipeline_primitives.h" - -#define USE_CUTE 0 - -#if USE_CUTE -#include "cutlass/include/cute/tensor.hpp" -#include "cutlass/include/cute/algorithm/cooperative_gemm.hpp" -#endif // USE_CUTE - #if !defined(__CUDA_ARCH__) #define WP_TILE_SHARED static #define WP_TILE_SYNC void @@ -20,10 +10,6 @@ #define WP_TILE_SYNC __syncthreads #endif - -namespace wp -{ - // CUTLASS_PRAGMA_(UNROLL|NO_UNROLL) optimization directives for the CUDA compiler. #if defined(__CUDA_ARCH__) && !defined(__INTELLISENSE__) #if defined(__CUDACC_RTC__) || (defined(__clang__) && defined(__CUDA__)) @@ -42,286 +28,328 @@ namespace wp #endif -// 2D tile zero -template -inline CUDA_CALLABLE array_t tile_zeros() +namespace wp { - const int length = M*N; - WP_TILE_SHARED __align__(16) T data[length]; - - WP_PRAGMA_UNROLL - for (int t=threadIdx.x; t < length; t += blockDim.x) - { - data[t] = T(0.0); - } +template +void print_tile(T& t) +{ + t.print(); - return array_t(data, M, N, nullptr); + printf("["); + for (int i=0; i < T::M; ++i) + { + printf("%*s[", i>0, ""); + for (int j=0; j < T::N; ++j) + { + printf("%5.2f ", t.fwd(i*T::N + j)); + } + + if (i == T::M-1) + printf("]]\n"); + else + printf("]\n"); + } } -// 2D tile load -template -inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) + +template +int size(Tile& t) { return Tile::M*Tile::N; } + + +template +struct tile_load_t { - const int length = M*N; + using Type = T; + static constexpr int M = M_; + static constexpr int N = N_; - WP_TILE_SHARED __align__(16) T data[length]; - - //--------------- - // naive-synchronous load - // - // WP_PRAGMA_UNROLL - // for (int t=threadIdx.x; t < length; t += blockDim.x) - // { - // data[t] = index(src, i*M + t/N, j*N + t%N); - // } - - //--------------- - // async 128 bit loads (assumes row-major i.e.: stride 1 on y axis and 4-element alignment on dimension) - const int s = 4; - - WP_PRAGMA_UNROLL - for (int t=threadIdx.x*s; t < length; t += blockDim.x*s) - { - __pipeline_memcpy_async(&data[t], - &index(src, i*M + t/N, j*N + t%N), - sizeof(T)*s); + array_t slice; + + tile_load_t(array_t& src, int x, int y) + { + assert(src.ndim == 2); + + // compute offsets into original array and store a view + const int i = x*M; + const int j = y*N; + + // slice into src + if (src.data) + slice.data = data_at_byte_offset(src, byte_offset(src, i, j)); + if (src.grad) + slice.grad = grad_at_byte_offset(src, byte_offset(src, i, j)); + + slice.shape[0] = M; + slice.shape[1] = N; + slice.strides[0] = src.strides[0]; + slice.strides[1] = src.strides[1]; + slice.ndim = 2; } - __pipeline_commit(); + Type fwd(int e) + { + int i = e/N; + int j = e%N; + return index(slice, i, j); + } - return array_t(data, M, N, nullptr); -} + void bwd(int e, const T& adj_ret) + { + int i = e/N; + int j = e%N; -// 2D tile store -template -inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array_t& src) -{ - const int M = src.shape[0]; - const int N = src.shape[1]; - - const int length = M*N; + if (slice.grad) + atomic_add(&index_grad(slice, i, j), adj_ret); + } - // cooperatively store the tile, using a block-stride iterator - WP_PRAGMA_UNROLL - for (int t=threadIdx.x; t < length; t += blockDim.x) - { - index(dest, i*M + t/N, j*N + t%N) = src.data[t]; + void print() + { + printf("tile_load_t<%d, %d>\n", M, N); } -} -template -inline CUDA_CALLABLE const T& index(const T* __restrict__ p, int i, int j, int stride) -{ - return p[i*stride + j]; -} +}; -template -inline CUDA_CALLABLE T& index(T* __restrict__ p, int i, int j, int stride) +template +struct tile_store_t { - return p[i*stride + j]; -} + using Tile = Tile_; + using Type = typename Tile_::Type; + static constexpr int M = Tile_::M; + static constexpr int N = Tile_::N; -template -struct partition_t -{ - inline partition_t(array_t A) + array_t slice; + + Tile& tile; + + tile_store_t(array_t& dest, int x, int y, Tile& t) : tile(t) { - data = A; - - // todo: do ceil div for non-multiples of M,N - shape[0] = A.shape[0]/M; - shape[1] = A.shape[1]/N; + assert(dest.ndim == 2); + + // compute offsets into original array and store a view + const int i = x*M; + const int j = y*N; + + // slice into dest + if (dest.data) + slice.data = data_at_byte_offset(dest, byte_offset(dest, i, j)); + if (dest.grad) + slice.grad = grad_at_byte_offset(dest, byte_offset(dest, i, j)); + + slice.shape[0] = M; + slice.shape[1] = N; + slice.strides[0] = dest.strides[0]; + slice.strides[1] = dest.strides[1]; + slice.ndim = 2; } - // underlying data - array_t data; - - // partition dimensions - int shape[2]; -}; + void fwd(int e) + { + int i = e/N; + int j = e%N; -template -inline int partition_size(const partition_t& tile) -{ - return tile.shape[0]*tile.shape[1]; -} + index(slice, i, j) = tile.fwd(e); + } -// returns the x, y coordinates of a tile given a linear index -template -inline void partition_coord(const partition_t& tile, const int t, int& i, int& j) -{ - i = t/tile.shape[1]; - j = t%tile.shape[1]; -} + void bwd(int e) + { + int i = e/N; + int j = e%N; -template -inline mat_t partition_load(const partition_t& tile, int i, int j) -{ - mat_t out; - - const int tile_i = i*M; - const int tile_j = j*N; + // materialize gradient (runs entire graph backward), reading incoming grads from the dest + if (slice.grad) + tile.bwd(e, index_grad(slice, i, j)); + } - WP_PRAGMA_UNROLL - for (int i=0; i < M; ++i) + void print() { - WP_PRAGMA_UNROLL - for (int j=0; j < N; ++j) - { - out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); - } + printf("tile_load_t<%d, %d>-+", M, N); + print(tile); } +}; - return out; -} -template -inline void partition_store(const partition_t& tile, int i, int j, const mat_t& value) +template +struct tile_constant_t { - mat_t out; + using Type = T; + static constexpr int M = M_; + static constexpr int N = N_; - const int tile_i = M*i; - const int tile_j = N*j; + T c; + T& adj_c; - WP_PRAGMA_UNROLL - for (int i=0; i < M; ++i) - { - WP_PRAGMA_UNROLL - for (int j=0; j < N; ++j) - { - index(tile.data, tile_i + i, tile_j + j) = value.data[i][j]; - } - } -} + tile_constant_t(const T& c, T& adj_c) : c(c), adj_c(adj_c) {} + Type fwd(int e) + { + return c; + } -#if !USE_CUTE + void bwd(int e, const T& adj_ret) + { + adj_c += adj_ret; + } -template -inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) -{ - const int TILE_M = 4; - const int TILE_N = 4; - const int TILE_K = 4; + void print() + { + printf("tile_constant_t<%d, %d>-+", M, N); + print(c); + printf("\n"); + } +}; - partition_t A_tile = partition_t(A); - partition_t B_tile = partition_t(B); - partition_t C_tile = partition_t(out); - const int length = partition_size(C_tile); - __pipeline_wait_prior(0); +template +struct tile_unary_map_t +{ + using Type = typename Tile::Type; + static constexpr int M = Tile::M; + static constexpr int N = Tile::N; - WP_TILE_SYNC(); + Tile& tile; + + FwdOp fwd_fn; + AdjOp adj_fn; - for (int t=threadIdx.x; t < length; t += blockDim.x) - { - int i, j; - partition_coord(C_tile, t, i, j); + tile_unary_map_t(Tile& t, FwdOp f, AdjOp a) : tile(t), fwd_fn(f), adj_fn(a) {} - // accumulator - mat_t sum = partition_load(C_tile, i, j); + Type fwd(int e) + { + return fwd_fn(tile.fwd(e)); + } - WP_PRAGMA_UNROLL - for (int k=0; k < A_tile.shape[1]; k++) - { - const mat_t a = partition_load(A_tile, i, k); - const mat_t b = partition_load(B_tile, k, j); + void bwd(int e, Type adj_ret) + { + Type adj_a = 0.0; - sum += mul(a, b); - } + adj_fn(tile.fwd(e), adj_a, adj_ret); - partition_store(C_tile, i, j, sum); + tile.bwd(e, adj_a); } - WP_TILE_SYNC(); -} + void print() + { + printf("tile_unary_map_t<%d, %d>-+", M, N); + tile.print(); + } +}; +template +struct tile_binary_map_t +{ + static_assert(TileA::Type == TileB::Type, "Error"); + static_assert(TileA::M == TileB::M, "Error"); + static_assert(TileA::N == TileB::N, "Error"); + using Type = typename TileA::Type; + static constexpr int M = TileA::M; + static constexpr int N = TileA::N; -// 2D gemm accumulate out += A*B -template -inline CUDA_CALLABLE void tile_matmul_scalar(const array_t& A, const array_t& B, const array_t& out) -{ - const int length = out.shape[0]*out.shape[1]; + const TileA& tile_a; + const TileB& tile_b; - WP_TILE_SYNC(); + FwdOp fwd_fn; + AdjOp adj_fn; - const T* __restrict__ A_ptr = A.data; - const T* __restrict__ B_ptr = B.data; - T* __restrict__ C_ptr = out.data; - WP_PRAGMA_UNROLL - for (int t=threadIdx.x; t < length; t += blockDim.x) - { - // compute output index - const int i = t/out.shape[1]; - const int j = t%out.shape[1]; + tile_binary_map_t(const TileA& a, TileB& b, FwdOp fwd_fn, AdjOp adj_fn) : tile_a(a), tile_b(b), fwd_fn(fwd_fn), adj_fn(adj_fn) {} - T sum(0.0); + Type fwd(int e) + { + Type a = tile_a.fwd(e); + Type b = tile_b.fwd(e); - WP_PRAGMA_UNROLL - for (int k=0; k < A.shape[1]; ++k) - { - T a = index(A_ptr, i, k, A.shape[1]); - T b = index(B_ptr, k, j, B.shape[1]); + return fwd_fn(a, b); + } - sum = fmaf(a, b, sum); - } - - index(C_ptr, i, j, out.shape[1]) += sum; + void bwd(int e, Type adj_ret) + { + Type a = tile_a.fwd(e); + Type b = tile_b.fwd(e); + + Type adj_a = 0.0; + Type adj_b = 0.0; + + adj_fn(a, b, adj_a, adj_b, adj_ret); + + // recurse + tile_a.bwd(e, adj_a); + tile_b.bwd(e, adj_b); } - WP_TILE_SYNC(); -} + void print() + { + printf("tile_binary_map_t<%d, %d>", M, N); + printf("\n -+"); + tile_a.print(); + printf("\n -+"); + tile_b.print(); -#else + } +}; -template -inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) + + +// entry point for load +template +tile_load_t tile_load(array_t& a, int x, int y) { - using namespace cute; + return tile_load_t(a, x, y); +} - __pipeline_wait_prior(0); +template +void adj_tile_load(array_t& a, int x, int y, array_t& adj_a, int adj_x, int adj_y, const tile_load_t& adj_ret) +{ + // nop +} - // ensure smem tile is ready - WP_TILE_SYNC(); - // Define CTA matrix size (static) - auto bM = Int<64>{}; - auto bN = Int<64>{}; - auto bK = Int<8>{}; +// entry point for store +template +void tile_store(array_t& dest, int x, int y, Tile& t) +{ + tile_store_t op(dest, x, y, t); + + // execute op + for (int i=threadIdx.x; i < size(op); i += blockDim.x) + op.fwd(i); +} - // Define the smem layouts (static) - auto sA = make_layout(make_shape(bM, bK), LayoutRight{}); - auto sB = make_layout(make_shape(bN, bK)); - auto sC = make_layout(make_shape(bM, bN), LayoutRight{}); - Tensor s_a_tensor = make_tensor(make_smem_ptr(A.data), sA); - Tensor s_b_tensor = make_tensor(make_smem_ptr(B.data), sB); - Tensor s_c_tensor = make_tensor(make_smem_ptr(out.data), sC); +template +void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, Tile& adj_t) +{ + tile_store_t op(dest, x, y, t); + for (int i=threadIdx.x; i < size(op); i += blockDim.x) + op.bwd(i); +} - TiledMMA tiled_mma = make_tiled_mma(UniversalFMA{}, - Layout>{}); // 16x8x1 UniversalFMA, assumes blockDim=128 - - cooperative_gemm< AutoVectorizingCopyWithAssumedAlignment>, - AutoVectorizingCopyWithAssumedAlignment>, - AutoVectorizingCopyWithAssumedAlignment> - >( - threadIdx.x, tiled_mma, - 1.0f, s_a_tensor, s_b_tensor, 1.0f, s_c_tensor, - cute::identity(), cute::identity(), cute::identity(), cute::identity() - ); - WP_TILE_SYNC(); +// unary map +template +tile_unary_map_t tile_map_impl(FwdOp fwd, AdjOp adj, Tile& t) +{ + return tile_unary_map_t(t, fwd, adj); +} +// binary map +template +tile_binary_map_t tile_map_impl(FwdOp op, AdjOp adj, TileA& a, TileB& b) +{ + return tile_binary_map_t(a, b); } -#endif // USE_CUTE +// use macro to capture adjoint operator +#define tile_map(op, ...) tile_map_impl(op, adj_##op, __VA_ARGS__) + +// use a macro to capture the adjoint var in the expression +#define tile_constant(T, M, N, var) tile_constant_t(var, adj_##var) + +} // namespace wp -} // namespace wp \ No newline at end of file diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h new file mode 100644 index 00000000..91ed329d --- /dev/null +++ b/warp/native/tile_gemm.h @@ -0,0 +1,310 @@ +#pragma once + +#include "builtin.h" + +// todo: requires CTK, replace with inline ptx +#include "cuda_pipeline_primitives.h" + +#define USE_CUTE 1 + +#if USE_CUTE +#include "cutlass/include/cute/tensor.hpp" +#include "cutlass/include/cute/algorithm/cooperative_gemm.hpp" +#endif // USE_CUTE + +namespace wp +{ + + +// 2D tile zero +template +inline CUDA_CALLABLE array_t tile_zeros() +{ + const int length = M*N; + + WP_TILE_SHARED __align__(16) T data[length]; + + WP_PRAGMA_UNROLL + for (int t=threadIdx.x; t < length; t += blockDim.x) + { + data[t] = T(0.0); + } + + return array_t(data, M, N, nullptr); +} + +// 2D tile load +template +inline CUDA_CALLABLE array_t tile_load(const array_t& src, int i, int j) +{ + const int length = M*N; + + WP_TILE_SHARED __align__(16) T data[length]; + + //--------------- + // naive-synchronous load + // + // WP_PRAGMA_UNROLL + // for (int t=threadIdx.x; t < length; t += blockDim.x) + // { + // data[t] = index(src, i*M + t/N, j*N + t%N); + // } + + //--------------- + // async 128 bit loads (assumes row-major i.e.: stride 1 on y axis and 4-element alignment on dimension) + const int s = 4; + + WP_PRAGMA_UNROLL + for (int t=threadIdx.x*s; t < length; t += blockDim.x*s) + { + __pipeline_memcpy_async(&data[t], + &index(src, i*M + t/N, j*N + t%N), + sizeof(T)*s); + } + + __pipeline_commit(); + + + return array_t(data, M, N, nullptr); +} + +// 2D tile store +template +inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array_t& src) +{ + const int M = src.shape[0]; + const int N = src.shape[1]; + + const int length = M*N; + + // cooperatively store the tile, using a block-stride iterator + WP_PRAGMA_UNROLL + for (int t=threadIdx.x; t < length; t += blockDim.x) + { + index(dest, i*M + t/N, j*N + t%N) = src.data[t]; + } +} + +template +inline CUDA_CALLABLE const T& index(const T* __restrict__ p, int i, int j, int stride) +{ + return p[i*stride + j]; +} + +template +inline CUDA_CALLABLE T& index(T* __restrict__ p, int i, int j, int stride) +{ + return p[i*stride + j]; +} + +template +struct partition_t +{ + inline partition_t(array_t A) + { + data = A; + + // todo: do ceil div for non-multiples of M,N + shape[0] = A.shape[0]/M; + shape[1] = A.shape[1]/N; + } + + // underlying data + array_t data; + + // partition dimensions + int shape[2]; +}; + +template +inline int partition_size(const partition_t& tile) +{ + return tile.shape[0]*tile.shape[1]; +} + +// returns the x, y coordinates of a tile given a linear index +template +inline void partition_coord(const partition_t& tile, const int t, int& i, int& j) +{ + i = t/tile.shape[1]; + j = t%tile.shape[1]; +} + +template +inline mat_t partition_load(const partition_t& tile, int i, int j) +{ + mat_t out; + + const int tile_i = i*M; + const int tile_j = j*N; + + WP_PRAGMA_UNROLL + for (int i=0; i < M; ++i) + { + WP_PRAGMA_UNROLL + for (int j=0; j < N; ++j) + { + out.data[i][j] = index(tile.data, tile_i + i, tile_j + j); + } + } + + return out; +} + +template +inline void partition_store(const partition_t& tile, int i, int j, const mat_t& value) +{ + mat_t out; + + const int tile_i = M*i; + const int tile_j = N*j; + + WP_PRAGMA_UNROLL + for (int i=0; i < M; ++i) + { + WP_PRAGMA_UNROLL + for (int j=0; j < N; ++j) + { + index(tile.data, tile_i + i, tile_j + j) = value.data[i][j]; + } + } +} + + +#if !USE_CUTE + +template +inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) +{ + const int TILE_M = 4; + const int TILE_N = 4; + const int TILE_K = 4; + + partition_t A_tile = partition_t(A); + partition_t B_tile = partition_t(B); + partition_t C_tile = partition_t(out); + + const int length = partition_size(C_tile); + + __pipeline_wait_prior(0); + + WP_TILE_SYNC(); + + for (int t=threadIdx.x; t < length; t += blockDim.x) + { + int i, j; + partition_coord(C_tile, t, i, j); + + // accumulator + mat_t sum = partition_load(C_tile, i, j); + + WP_PRAGMA_UNROLL + for (int k=0; k < A_tile.shape[1]; k++) + { + const mat_t a = partition_load(A_tile, i, k); + const mat_t b = partition_load(B_tile, k, j); + + sum += mul(a, b); + } + + partition_store(C_tile, i, j, sum); + } + + WP_TILE_SYNC(); +} + + + +// 2D gemm accumulate out += A*B +template +inline CUDA_CALLABLE void tile_matmul_scalar(const array_t& A, const array_t& B, const array_t& out) +{ + const int length = out.shape[0]*out.shape[1]; + + WP_TILE_SYNC(); + + const T* __restrict__ A_ptr = A.data; + const T* __restrict__ B_ptr = B.data; + T* __restrict__ C_ptr = out.data; + + WP_PRAGMA_UNROLL + for (int t=threadIdx.x; t < length; t += blockDim.x) + { + // compute output index + const int i = t/out.shape[1]; + const int j = t%out.shape[1]; + + T sum(0.0); + + WP_PRAGMA_UNROLL + for (int k=0; k < A.shape[1]; ++k) + { + T a = index(A_ptr, i, k, A.shape[1]); + T b = index(B_ptr, k, j, B.shape[1]); + + sum = fmaf(a, b, sum); + } + + index(C_ptr, i, j, out.shape[1]) += sum; + } + + WP_TILE_SYNC(); +} + +#else + + +template +inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) +{ + using namespace cute; + + __pipeline_wait_prior(0); + + // ensure smem tile is ready + WP_TILE_SYNC(); + + // Define CTA matrix size (static) + auto bM = Int<64>{}; + auto bN = Int<64>{}; + auto bK = Int<8>{}; + + // Define the smem layouts (static) + auto sA = make_layout(make_shape(bM, bK), LayoutRight{}); + auto sB = make_layout(make_shape(bN, bK)); + auto sC = make_layout(make_shape(bM, bN), LayoutRight{}); + + Tensor s_a_tensor = make_tensor(make_smem_ptr(A.data), sA); + Tensor s_b_tensor = make_tensor(make_smem_ptr(B.data), sB); + Tensor s_c_tensor = make_tensor(make_smem_ptr(out.data), sC); + + + // TiledMMA tiled_mma = make_tiled_mma(UniversalFMA{}, + // Layout>{}); // 16x8x1 UniversalFMA, assumes blockDim=128 + + + // TiledMMA tiled_mma = make_tiled_mma(UniversalFMA{}, + // Layout,Stride<_16,_1>>{}); // 8x16x1 UniversalFMA, assumes blockDim=128 + + + + TiledMMA tiled_mma = make_tiled_mma(UniversalFMA{}, + Layout,Stride<_64,_1>>{}); // 8x16x1 UniversalFMA, assumes blockDim=128 + + + cooperative_gemm< AutoVectorizingCopyWithAssumedAlignment>, + AutoVectorizingCopyWithAssumedAlignment>, + AutoVectorizingCopyWithAssumedAlignment> + >( + threadIdx.x, tiled_mma, + 1.0f, s_a_tensor, s_b_tensor, 1.0f, s_c_tensor, + cute::identity(), cute::identity(), cute::identity(), cute::identity() + ); + + WP_TILE_SYNC(); + +} + +#endif // USE_CUTE + +} // namespace wp \ No newline at end of file diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 78fb7fc9..4781d9ad 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -6,7 +6,7 @@ #wp.config.mode = "debug" wp.init() -wp.set_module_options({"enable_backward": False}) +wp.set_module_options({"enable_backward": True}) wp.set_device("cuda:0") @@ -36,118 +36,136 @@ def test_copy_tiled(): A = rng.random((M, N), dtype=np.float32) B = rng.random((M, N), dtype=np.float32) - A_wp = wp.array(A) - B_wp = wp.array(B) + A_wp = wp.array(A, requires_grad=True) + B_wp = wp.array(B, requires_grad=True) - wp.launch(copy_tiled, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) + with wp.Tape() as tape: + wp.launch(copy_tiled, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) + # verify forward pass assert(np.allclose(A, B_wp.numpy(), rtol=1.e-4)) - - print("Copy passed") + print("Copy forward passed") + # verify backward pass + B_wp.grad = wp.ones_like(B_wp) + tape.backward() -#test_copy_tiled() + assert(np.allclose(A_wp.grad.numpy(), B_wp.grad.numpy())) + print("Copy backward passed") -@wp.kernel -def gemm(A: wp.array2d(dtype=float), - B: wp.array2d(dtype=float), - C: wp.array2d(dtype=float)): +test_copy_tiled() - # output index - i, j = wp.tid() - sum = float(0.0) +# @wp.kernel +# def gemm(A: wp.array2d(dtype=float), +# B: wp.array2d(dtype=float), +# C: wp.array2d(dtype=float)): - for k in range(0, A.shape[1]): - sum += A[i, k]*B[k, j] +# # output index +# i, j = wp.tid() - C[i, j] = sum +# sum = float(0.0) +# for k in range(0, A.shape[1]): +# sum += A[i, k]*B[k, j] +# C[i, j] = sum -TILE_M = wp.constant(64) -TILE_N = wp.constant(64) -TILE_K = wp.constant(8) -@wp.kernel -def gemm_tiled(A: wp.array2d(dtype=float), - B: wp.array2d(dtype=float), - C: wp.array2d(dtype=float)): - # output tile index - i, j = wp.tid() +# TILE_M = wp.constant(64) +# TILE_N = wp.constant(64) +# TILE_K = wp.constant(8) + +# @wp.kernel +# def gemm_tiled(A: wp.array2d(dtype=float), +# B: wp.array2d(dtype=float), +# C: wp.array2d(dtype=float)): + +# # output tile index +# i, j = wp.tid() + +# sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + +# M = A.shape[0] +# N = B.shape[1] +# K = A.shape[1] + +# count = int(K / TILE_K) # todo: must be the same as TILE_K + +# for k in range(count): + +# a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) +# b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) - sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) +# # sum += a*b +# wp.tile_matmul(a, b, sum) - M = A.shape[0] - N = B.shape[1] - K = A.shape[1] +# wp.tile_store(C, i, j, sum) - count = int(K / 8) # todo: must be the same as TILE_K - for k in range(count): +# s = 0.0 - a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) - b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) +# for i, j in tile.shape: - # sum += a*b - wp.tile_matmul(a, b, sum) +# s += tile[i-1, i-1] +# s += tile[i, i-1] +# s += tile[i,] - wp.tile_store(C, i, j, sum) -M = TILE_M*7 -K = TILE_K*4 -N = TILE_N*6 +# M = TILE_M*7 +# K = TILE_K*4 +# N = TILE_N*6 -rng = np.random.default_rng(42) -A = rng.random((M, K), dtype=np.float32) -B = rng.random((K, N), dtype=np.float32) -C = np.zeros((M, N), dtype=np.float32) +# rng = np.random.default_rng(42) +# A = rng.random((M, K), dtype=np.float32) +# B = rng.random((K, N), dtype=np.float32) +# C = np.zeros((M, N), dtype=np.float32) -A_wp = wp.array(A) -B_wp = wp.array(B) -C_wp = wp.array(C) +# A_wp = wp.array(A) +# B_wp = wp.array(B) +# C_wp = wp.array(C) -iters = 10 +# iters = 10 -with wp.ScopedTimer("NumPy"): +# with wp.ScopedTimer("NumPy"): - for i in range(iters): - C = A@B +# for i in range(iters): +# C = A@B -wp.force_load(device="cuda:0") +# wp.force_load(device="cuda:0") -with wp.ScopedTimer("Warp", cuda_filter=wp.TIMING_KERNEL): +# with wp.ScopedTimer("Warp", cuda_filter=wp.TIMING_KERNEL): - for i in range(iters): - wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) +# for i in range(iters): +# wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) - print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) +# print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) - for i in range(iters): - wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=128) - wp.synchronize() +# for i in range(iters): +# wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=128) +# wp.synchronize() - print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) +# print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) -A_tc = torch.from_numpy(A).to("cuda:0") -B_tc = torch.from_numpy(B).to("cuda:0") -C_tc = torch.from_numpy(C).to("cuda:0") +# A_tc = torch.from_numpy(A).to("cuda:0") +# B_tc = torch.from_numpy(B).to("cuda:0") +# C_tc = torch.from_numpy(C).to("cuda:0") -for i in range(10): - torch.matmul(A_tc, B_tc, out=C_tc) +# for i in range(10): +# torch.matmul(A_tc, B_tc, out=C_tc) -with wp.ScopedTimer("Torch"): +# with wp.ScopedTimer("Torch"): - for i in range(iters): - torch.matmul(A_tc, B_tc, out=C_tc) +# for i in range(iters): +# torch.matmul(A_tc, B_tc, out=C_tc) - torch.cuda.synchronize() +# torch.cuda.synchronize() diff --git a/warp/types.py b/warp/types.py index 28c20fcd..11416bfd 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2861,6 +2861,19 @@ def array_type_id(a): raise ValueError("Invalid array type") +# tile expression objects +class Tile: + + def __init__(self, dtype, M, N, op): + self.dtype = dtype + self.M = M + self.N = N + self.op = op + +def is_tile(t): + return isinstance(t, Tile) + + class Bvh: def __init__(self, lowers, uppers): """Class representing a bounding volume hierarchy. From 9f1c428aacd2bdd68f906188547ea799eff8f20a Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 7 Jun 2024 08:20:34 +1200 Subject: [PATCH 012/102] Formatting fixes for CHANGELOG.md --- CHANGELOG.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5927f6ed..9ccb477c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,20 +9,20 @@ - Revised module compilation process to allow multiple processes to use the same kernel cache directory. Cached kernels will now be stored in hash-specific subdirectory. - Add runtime checks for `wp.MarchingCubes` on field dimensions and size -- Fix memory leak in mesh BVH ([GH-225](https://github.com/NVIDIA/warp/issues/225)) +- Fix memory leak in `wp.Mesh` BVH ([GH-225](https://github.com/NVIDIA/warp/issues/225)) - Use C++17 with NVCC when building the Warp library and user kernels - Increase PTX target architecture up to `sm_75` (from `sm_70`), enabling Turing ISA features - Extended NanoVDB support (see `warp.Volume`): - Add support for data-agnostic index grids, allocation at voxel granularity - - New `volume_lookup_index`, `volume_sample_index` and generic `volume_sample`/`volume_lookup`/`volume_store` kernel-level functions + - New `wp.volume_lookup_index()`, `wp.volume_sample_index()` and generic `wp.volume_sample()`/`wp.volume_lookup()`/`wp.volume_store()` kernel-level functions - Zero-copy aliasing of in-memory grids, support for multi-grid buffers - Grid introspection and blind data access capabilities - warp.fem can now work directly on NanoVDB grids using `warp.fem.Nanogrid` - - Fixed `volume_sample_v` and `volume_store_*` adjoints - - Prevent `volume_store` from overwriting grid background values + - Fixed `wp.volume_sample_v()` and `wp.volume_store_*()` adjoints + - Prevent `wp.volume_store()` from overwriting grid background values - Improve validation of user-provided fields and values in warp.fem -- Support headless rendering of `OpenGLRenderer` via `pyglet.options["headless"] = True` -- `RegisteredGLBuffer` can fall back to CPU-bound copying if CUDA/OpenGL interop is not available +- Support headless rendering of `wp.render.OpenGLRenderer` via `pyglet.options["headless"] = True` +- `wp.render.RegisteredGLBuffer` can fall back to CPU-bound copying if CUDA/OpenGL interop is not available - Fix to forward `wp.copy()` params to gradient and adjoint copy function calls. - Fix slicing of arrays with gradients in kernels From 209e3cae891bf98705bfdd8c0064ae57b96eb8f4 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 7 Jun 2024 10:42:31 +1200 Subject: [PATCH 013/102] Update CHANGELOG.md --- CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a12e527f..8916a63d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,16 +17,17 @@ - New `wp.volume_lookup_index()`, `wp.volume_sample_index()` and generic `wp.volume_sample()`/`wp.volume_lookup()`/`wp.volume_store()` kernel-level functions - Zero-copy aliasing of in-memory grids, support for multi-grid buffers - Grid introspection and blind data access capabilities - - warp.fem can now work directly on NanoVDB grids using `warp.fem.Nanogrid` + - `warp.fem` can now work directly on NanoVDB grids using `warp.fem.Nanogrid` - Fixed `wp.volume_sample_v()` and `wp.volume_store_*()` adjoints - Prevent `wp.volume_store()` from overwriting grid background values -- Improve validation of user-provided fields and values in warp.fem +- Improve validation of user-provided fields and values in `warp.fem` - Support headless rendering of `wp.render.OpenGLRenderer` via `pyglet.options["headless"] = True` - `wp.render.RegisteredGLBuffer` can fall back to CPU-bound copying if CUDA/OpenGL interop is not available - Fix to forward `wp.copy()` params to gradient and adjoint copy function calls. - Fix so that `wp.randn()` doesn't return inf - Fix slicing of arrays with gradients in kernels -- Fix function overload caching: ensure module is rebuilt if any function overloads are modified. +- Fix function overload caching: ensure module is rebuilt if any function overloads are modified +- Publish CUDA 12.5 binaries for Hopper support, see https://github.com/nvidia/warp?tab=readme-ov-file#installing for details ## [1.1.1] - 2024-05-24 From 15d76e0e994c215d26c8362045a05a18af92bbd7 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 8 Jul 2024 15:54:32 +1200 Subject: [PATCH 014/102] Working unary and binary tile_map() builtin --- warp/builtins.py | 30 +++++++++++ warp/native/tile.h | 47 +++++++++++------ warp/tests/test_tile.py | 113 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 170 insertions(+), 20 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 96dc4282..1f01df4d 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1523,6 +1523,36 @@ def tile_matmul_value_func(arg_types, kwds, templates): export=False, ) +# does type propagation for load() +def tile_map_value_func(arg_types, kwds, _): + + if arg_types is None: + return None + + dtype = arg_types[0] + for i in arg_types: + if arg_types[i].dtype != dtype: + raise RuntimeError("tile_map() arguments must all have the same type") + + input = arg_types[0] + + return Tile(dtype=input.dtype, + M=input.M, + N=input.N, + op="map") + + + +add_builtin( + "tile_map", + input_types={"op": Callable}, + value_func=tile_map_value_func, + variadic=True, + doc="Map the operation onto each element of the tile", + group="Tile Primitives", + export=False, +) + # --------------------------------- # Linear Algebra diff --git a/warp/native/tile.h b/warp/native/tile.h index 618611f8..b3be6d81 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -31,6 +31,18 @@ namespace wp { +// Primary template +template +struct is_same { + static constexpr bool value = false; +}; + +// Specialization for the case when T and U are the same type +template +struct is_same { + static constexpr bool value = true; +}; + template void print_tile(T& t) { @@ -87,7 +99,7 @@ struct tile_load_t slice.ndim = 2; } - Type fwd(int e) + Type fwd(int e) const { int i = e/N; int j = e%N; @@ -95,7 +107,7 @@ struct tile_load_t return index(slice, i, j); } - void bwd(int e, const T& adj_ret) + void bwd(int e, const T& adj_ret) const { int i = e/N; int j = e%N; @@ -144,7 +156,7 @@ struct tile_store_t slice.ndim = 2; } - void fwd(int e) + void fwd(int e) const { int i = e/N; int j = e%N; @@ -152,7 +164,7 @@ struct tile_store_t index(slice, i, j) = tile.fwd(e); } - void bwd(int e) + void bwd(int e) const { int i = e/N; int j = e%N; @@ -216,17 +228,17 @@ struct tile_unary_map_t tile_unary_map_t(Tile& t, FwdOp f, AdjOp a) : tile(t), fwd_fn(f), adj_fn(a) {} - Type fwd(int e) + Type fwd(int e) const { return fwd_fn(tile.fwd(e)); } - void bwd(int e, Type adj_ret) + void bwd(int e, Type adj_ret) const { Type adj_a = 0.0; adj_fn(tile.fwd(e), adj_a, adj_ret); - + tile.bwd(e, adj_a); } @@ -240,7 +252,7 @@ struct tile_unary_map_t template struct tile_binary_map_t { - static_assert(TileA::Type == TileB::Type, "Error"); + static_assert(wp::is_same::value, "Error"); static_assert(TileA::M == TileB::M, "Error"); static_assert(TileA::N == TileB::N, "Error"); @@ -257,7 +269,7 @@ struct tile_binary_map_t tile_binary_map_t(const TileA& a, TileB& b, FwdOp fwd_fn, AdjOp adj_fn) : tile_a(a), tile_b(b), fwd_fn(fwd_fn), adj_fn(adj_fn) {} - Type fwd(int e) + Type fwd(int e) const { Type a = tile_a.fwd(e); Type b = tile_b.fwd(e); @@ -265,7 +277,7 @@ struct tile_binary_map_t return fwd_fn(a, b); } - void bwd(int e, Type adj_ret) + void bwd(int e, Type adj_ret) const { Type a = tile_a.fwd(e); Type b = tile_b.fwd(e); @@ -287,7 +299,6 @@ struct tile_binary_map_t tile_a.print(); printf("\n -+"); tile_b.print(); - } }; @@ -333,20 +344,26 @@ void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_des // unary map template -tile_unary_map_t tile_map_impl(FwdOp fwd, AdjOp adj, Tile& t) +tile_unary_map_t tile_map_impl(FwdOp fwd, AdjOp adj, Tile& a) { - return tile_unary_map_t(t, fwd, adj); + return tile_unary_map_t(a, fwd, adj); } // binary map template -tile_binary_map_t tile_map_impl(FwdOp op, AdjOp adj, TileA& a, TileB& b) +tile_binary_map_t tile_map_impl(FwdOp fwd, AdjOp adj, TileA& a, TileB& b) { - return tile_binary_map_t(a, b); + return tile_binary_map_t(a, b, fwd, adj); } // use macro to capture adjoint operator #define tile_map(op, ...) tile_map_impl(op, adj_##op, __VA_ARGS__) +//#define tile_map(op, a) tile_map_impl(wp::##op, wp::##op, a) + + +// nop +void adj_tile_map_impl(void) {} +#define adj_tile_map(...) adj_tile_map_impl() // use a macro to capture the adjoint var in the expression #define tile_constant(T, M, N, var) tile_constant_t(var, adj_##var) diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 4781d9ad..beea0746 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -16,8 +16,8 @@ TILE_N = 4 @wp.kernel -def copy_tiled(A: wp.array2d(dtype=float), - B: wp.array2d(dtype=float)): +def tile_copy(A: wp.array2d(dtype=float), + B: wp.array2d(dtype=float)): # tile index i, j = wp.tid() @@ -26,7 +26,7 @@ def copy_tiled(A: wp.array2d(dtype=float), wp.tile_store(B, i, j, a) -def test_copy_tiled(): +def test_tile_copy(): rng = np.random.default_rng(42) @@ -40,7 +40,7 @@ def test_copy_tiled(): B_wp = wp.array(B, requires_grad=True) with wp.Tape() as tape: - wp.launch(copy_tiled, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) + wp.launch(tile_copy, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) # verify forward pass assert(np.allclose(A, B_wp.numpy(), rtol=1.e-4)) @@ -53,8 +53,111 @@ def test_copy_tiled(): assert(np.allclose(A_wp.grad.numpy(), B_wp.grad.numpy())) print("Copy backward passed") +@wp.func +def unary_func(x: float): + return wp.sin(x) -test_copy_tiled() +@wp.kernel +def tile_unary_map(input: wp.array2d(dtype=float), + output: wp.array2d(dtype=float)): + + # tile index + i, j = wp.tid() + + a = wp.tile_load(input, i, j, m=TILE_M, n=TILE_N) + + sa = wp.tile_map(unary_func, a) + + wp.tile_store(output, i, j, sa) + + +def test_tile_unary_map(): + + rng = np.random.default_rng(42) + + M = TILE_M*7 + N = TILE_N*5 + + A = rng.random((M, N), dtype=np.float32) + B = np.sin(A) + + A_grad = np.cos(A) + + A_wp = wp.array(A, requires_grad=True) + B_wp = wp.zeros_like(A_wp, requires_grad=True) + + with wp.Tape() as tape: + wp.launch(tile_unary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) + + # verify forward pass + assert(np.allclose(B, B_wp.numpy(), rtol=1.e-4)) + print("Unary map forward passed") + + # verify backward pass + B_wp.grad = wp.ones_like(B_wp) + tape.backward() + + assert(np.allclose(A_wp.grad.numpy(), A_grad)) + print("Unary map backward passed") + + +@wp.func +def binary_func(x: float, y: float): + return wp.sin(x) + y + +@wp.kernel +def tile_binary_map(input_a: wp.array2d(dtype=float), + input_b: wp.array2d(dtype=float), + output: wp.array2d(dtype=float)): + + # tile index + i, j = wp.tid() + + a = wp.tile_load(input_a, i, j, m=TILE_M, n=TILE_N) + b = wp.tile_load(input_b, i, j, m=TILE_M, n=TILE_N) + + sa = wp.tile_map(binary_func, a, b) + + wp.tile_store(output, i, j, sa) + + +def test_tile_binary_map(): + + rng = np.random.default_rng(42) + + M = TILE_M*7 + N = TILE_N*5 + + A = rng.random((M, N), dtype=np.float32) + B = rng.random((M, N), dtype=np.float32) + C = np.sin(A) + B + + A_grad = np.cos(A) + B_grad = np.ones_like(B) + + A_wp = wp.array(A, requires_grad=True) + B_wp = wp.array(B, requires_grad=True) + C_wp = wp.zeros_like(A_wp, requires_grad=True) + + with wp.Tape() as tape: + wp.launch(tile_binary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp, C_wp], tile_size=8) + + # verify forward pass + assert(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) + print("Binary map forward passed") + + # verify backward pass + C_wp.grad = wp.ones_like(C_wp) + tape.backward() + + assert(np.allclose(A_wp.grad.numpy(), A_grad)) + assert(np.allclose(B_wp.grad.numpy(), B_grad)) + + print("Binary map backward passed") + +test_tile_copy() +test_tile_unary_map() +test_tile_binary_map() # @wp.kernel From 7c2a365164e9203cee82fdf715f31e6b812a2324 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 9 Aug 2024 16:16:50 +1200 Subject: [PATCH 015/102] Update Tile Expressions branch to work with changes to codegen / builtins --- warp/builtins.py | 123 +++++++++++++++++++++++++++++++---------------- warp/codegen.py | 25 +++++----- warp/types.py | 2 + 3 files changed, 96 insertions(+), 54 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 6e40f95b..e81b7f6e 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1697,7 +1697,7 @@ def spatial_vector_dispatch_func(input_types: Mapping[str, type], return_type: A # Tile-based primitives shared_memory_id = 0 -def tile_zeros_value_func(arg_types, kwds, templates): +def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any]): # return generic type (for doc builds) if arg_types is None: @@ -1706,90 +1706,110 @@ def tile_zeros_value_func(arg_types, kwds, templates): if len(arg_types) > 0: raise RuntimeError("tile_zero() args must be passed by keyword") - if "m" not in kwds: + if "m" not in arg_values: raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") - if "n" not in kwds: + if "n" not in arg_values: raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") - if "dtype" not in kwds: + if "dtype" not in arg_values: raise RuntimeError("'dtype' keyword argument must be specified when calling tile_zeros() function") - m, n, dtype = kwds["m"], kwds["n"], kwds["dtype"] + dtype = arg_values["dtype"] + + return array(dtype=dtype) - templates.append(dtype) - templates.append(m) - templates.append(n) +def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): + + m, n, dtype = arg_values["m"], arg_values["n"], arg_values["dtype"] + + template_args = [] + template_args.append(dtype) + template_args.append(m) + template_args.append(n) global shared_memory_id - templates.append(shared_memory_id) + template_args.append(shared_memory_id) shared_memory_id += 1 - return array(dtype=dtype) + return ([], template_args) + add_builtin( "tile_zeros", input_types={"m": int, "n": int, "dtype": Scalar}, value_func=tile_zeros_value_func, + dispatch_func=tile_zeros_dispatch_func, variadic=True, doc="Allocate a tile local block of zero'd memory", group="Tile Primitives", export=False, ) -def tile_load_value_func(arg_types, kwds, templates): +def tile_load_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: return array_t(shape=(Any, Any), dtype=Scalar) - if len(arg_types) != 3: - raise RuntimeError("tile_load() requires 3 positional args") + # if len(arg_types) != 3: + # raise RuntimeError("tile_load() requires 3 positional args") - if not is_array(arg_types[0]): + if not is_array(arg_types["a"]): raise RuntimeError("tile_load() argument 0 must be an array") - if not type_is_int(arg_types[1]): + if not type_is_int(arg_types["x"]): raise RuntimeError("tile_load() argument 1 must be an integer") - if not type_is_int(arg_types[2]): + if not type_is_int(arg_types["y"]): raise RuntimeError("tile_load() argument 1 must be an integer") - if "m" not in kwds: + if "m" not in arg_values: raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") - if "n" not in kwds: + if "n" not in arg_values: raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") - m, n = kwds["m"], kwds["n"] - dtype = arg_types[0].dtype + m, n = arg_values["m"], arg_values["n"] + dtype = arg_types["a"].dtype - templates.append(dtype) - templates.append(m) - templates.append(n) + return Tile(dtype, m, n, "load") + + +def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): + + array = arg_values["a"] + x, y = arg_values["x"], arg_values["y"] + m, n = arg_values["m"].constant, arg_values["n"].constant + dtype = arg_values["a"].type.dtype + + template_args = [] + template_args.append(dtype) + template_args.append(m) + template_args.append(n) global shared_memory_id #templates.append(shared_memory_id) shared_memory_id += 1 - return Tile(dtype, m, n, "load")#array(dtype=arg_types[0].dtype) - + return ((array, x, y), template_args) add_builtin( "tile_load", input_types={"a": array(dtype=Any), "x": int, "y": int, "m": int, "n": int}, value_func=tile_load_value_func, + dispatch_func=tile_load_dispatch_func, variadic=True, doc="Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)", group="Tile Primitives", export=False, ) -def tile_store_value_func(arg_types, kwds, templates): +def tile_store_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -1798,16 +1818,16 @@ def tile_store_value_func(arg_types, kwds, templates): if len(arg_types) != 4: raise RuntimeError("tile_store() requires 4 positional args") - if not is_array(arg_types[0]): + if not is_array(arg_types["a"]): raise RuntimeError("tile_store() argument 0 must be an array") - if not type_is_int(arg_types[1]): + if not type_is_int(arg_types["x"]): raise RuntimeError("tile_store() argument 1 must be an integer") - if not type_is_int(arg_types[2]): + if not type_is_int(arg_types["y"]): raise RuntimeError("tile_store() argument 2 must be an integer") - if not is_tile(arg_types[3]): + if not is_tile(arg_types["t"]): raise RuntimeError("tile_store() argument 3 must be a tile") return None @@ -1816,7 +1836,7 @@ def tile_store_value_func(arg_types, kwds, templates): add_builtin( "tile_store", - input_types={"a": array(dtype=Any), "x": int, "y": int, "m": int, "n": int}, + input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, value_func=tile_store_value_func, variadic=True, doc="Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)", @@ -1826,7 +1846,7 @@ def tile_store_value_func(arg_types, kwds, templates): -def tile_matmul_value_func(arg_types, kwds, templates): +def tile_matmul_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -1858,29 +1878,48 @@ def tile_matmul_value_func(arg_types, kwds, templates): ) # does type propagation for load() -def tile_map_value_func(arg_types, kwds, _): +def tile_map_value_func(arg_types, arg_values): if arg_types is None: return None - dtype = arg_types[0] - for i in arg_types: - if arg_types[i].dtype != dtype: - raise RuntimeError("tile_map() arguments must all have the same type") + # check all args are tiles + for a in arg_types["args"]: + if not is_tile(a): + raise RuntimeError(f"tile_map() arguments must be tiles, got type {a}") + + # use first argument to define output type + first = arg_types["args"][0] + + # check all args have the same type and dimension + for a in arg_types["args"]: + if a.dtype != first.dtype: + raise RuntimeError(f"tile_map() arguments must all have the same type {first.dtype} != {a.dtype}") + + if a.M != first.M: + raise RuntimeError(f"tile_map() arguments must all have the same m dimension {first.M} != {a.M}") + + if a.N != first.N: + raise RuntimeError(f"tile_map() arguments must all have the same n dimension {first.N} != {a.N}") - input = arg_types[0] - return Tile(dtype=input.dtype, - M=input.M, - N=input.N, + return Tile(dtype=first.dtype, + M=first.M, + N=first.N, op="map") +def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, args: Mapping[str, Var]): + func_args = (args["op"], *args["args"]) + template_args = () + return (func_args, template_args) + add_builtin( "tile_map", - input_types={"op": Callable}, + input_types={"op": Callable, "*args": Any}, value_func=tile_map_value_func, + dispatch_func=tile_map_dispatch_func, variadic=True, doc="Map the operation onto each element of the tile", group="Tile Primitives", diff --git a/warp/codegen.py b/warp/codegen.py index 8870a196..5d017e54 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -1275,7 +1275,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): output = adj.add_var(return_type) output_list = [output] - forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(args_var, use_initializer_list)});" + forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" # prepend auto if it is an anonymously typed var (e.g.: a tile op) if output.ctype() == "auto": @@ -1284,7 +1284,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): replay_call = forward_call if func.custom_replay_func is not None: - replay_call = f"var_{output} = {func.namespace}replay_{func_name}({adj.format_forward_call_args(args_var, use_initializer_list)});" + replay_call = f"var_{output} = {func.namespace}replay_{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" else: # handle multiple value functions @@ -1307,6 +1307,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): adj.add_forward(alias_call) if not func.missing_grad and len(args): + adj_args = tuple(strip_reference(x) for x in func_args) reverse_has_output_args = ( func.require_original_output_arg or len(output_list) > 1 ) and func.custom_grad_func is None @@ -2611,10 +2612,10 @@ def get_constant_references(adj) -> Dict[str, Any]: #define int(x) cast_int(x) #define adj_int(x, adj_x, adj_ret) adj_cast_int(x, adj_x, adj_ret) -#define builtin_tid1d() wp::tid(task_index) -#define builtin_tid2d(x, y) wp::tid(x, y, task_index, dim) -#define builtin_tid3d(x, y, z) wp::tid(x, y, z, task_index, dim) -#define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, task_index, dim) +#define builtin_tid1d() wp::tid(_idx) +#define builtin_tid2d(x, y) wp::tid(x, y, _idx, dim) +#define builtin_tid3d(x, y, z) wp::tid(x, y, z, _idx, dim) +#define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, _idx, dim) """ @@ -2629,10 +2630,10 @@ def get_constant_references(adj) -> Dict[str, Any]: #define int(x) cast_int(x) #define adj_int(x, adj_x, adj_ret) adj_cast_int(x, adj_x, adj_ret) -#define builtin_tid1d() wp::tid(task_index) -#define builtin_tid2d(x, y) wp::tid(x, y, task_index, dim) -#define builtin_tid3d(x, y, z) wp::tid(x, y, z, task_index, dim) -#define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, task_index, dim) +#define builtin_tid1d() wp::tid(_idx) +#define builtin_tid2d(x, y) wp::tid(x, y, _idx, dim) +#define builtin_tid3d(x, y, z) wp::tid(x, y, z, _idx, dim) +#define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, _idx, dim) """ @@ -2770,7 +2771,7 @@ def get_constant_references(adj) -> Dict[str, Any]: WP_API void {name}_cpu_forward( {forward_args}) {{ - for (size_t task_index = 0; task_index < dim.size; ++task_index) + for (size_t _idx = 0; _idx < dim.size; ++_idx) {{ {name}_cpu_kernel_forward( {forward_params}); @@ -2780,7 +2781,7 @@ def get_constant_references(adj) -> Dict[str, Any]: WP_API void {name}_cpu_backward( {reverse_args}) {{ - for (size_t task_index = 0; task_index < dim.size; ++task_index) + for (size_t _idx = 0; _idx < dim.size; ++_idx) {{ {name}_cpu_kernel_backward( {reverse_params}); diff --git a/warp/types.py b/warp/types.py index 32d3ed90..03065d6d 100644 --- a/warp/types.py +++ b/warp/types.py @@ -1270,6 +1270,8 @@ def type_typestr(dtype): def type_repr(t): if is_array(t): return str(f"array(ndim={t.ndim}, dtype={t.dtype})") + if is_tile(t): + return str(f"tile(dtype={t.dtype}, m={t.M}, n={t.N})") if type_is_vector(t): return str(f"vector(length={t._shape_[0]}, dtype={t._wp_scalar_type_})") if type_is_matrix(t): From ceba991844ad2badec237b40aad854330a83892b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 26 Aug 2024 15:56:42 +1200 Subject: [PATCH 016/102] Working tile expressions with pre-declared tile operation objects --- warp/builtins.py | 86 ++++++++++------ warp/codegen.py | 2 +- warp/native/tile.h | 223 ++++++++++++++++++++++++++++++++++++---- warp/native/tile_gemm.h | 75 +++++++++++++- warp/tests/test_tile.py | 109 ++++++-------------- warp/types.py | 42 ++++++++ 6 files changed, 407 insertions(+), 130 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index e81b7f6e..b721dea1 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1703,8 +1703,8 @@ def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str if arg_types is None: return array_t(shape=(Any, Any), dtype=Scalar) - if len(arg_types) > 0: - raise RuntimeError("tile_zero() args must be passed by keyword") + # if len(arg_types) > 0: + # raise RuntimeError("tile_zero() args must be passed by keyword") if "m" not in arg_values: raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") @@ -1715,9 +1715,10 @@ def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str if "dtype" not in arg_values: raise RuntimeError("'dtype' keyword argument must be specified when calling tile_zeros() function") + m, n = arg_values["m"], arg_values["n"] dtype = arg_values["dtype"] - return array(dtype=dtype) + return TileZeros(dtype=dtype, M=m, N=n) def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): @@ -1725,13 +1726,13 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar template_args = [] template_args.append(dtype) - template_args.append(m) - template_args.append(n) + template_args.append(m.constant) + template_args.append(n.constant) - global shared_memory_id - template_args.append(shared_memory_id) + # global shared_memory_id + # template_args.append(shared_memory_id) - shared_memory_id += 1 + # shared_memory_id += 1 return ([], template_args) @@ -1772,10 +1773,10 @@ def tile_load_value_func(arg_types, arg_values): if "n" not in arg_values: raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") + a = arg_types["a"] m, n = arg_values["m"], arg_values["n"] - dtype = arg_types["a"].dtype - return Tile(dtype, m, n, "load") + return TileLoad(a, m, n) def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): @@ -1790,10 +1791,9 @@ def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg template_args.append(m) template_args.append(n) - global shared_memory_id + #global shared_memory_id #templates.append(shared_memory_id) - - shared_memory_id += 1 + #shared_memory_id += 1 return ((array, x, y), template_args) @@ -1845,6 +1845,31 @@ def tile_store_value_func(arg_types, arg_values): ) +def tile_realize_value_func(arg_types, arg_values): + + # return generic type (for doc builds) + if arg_types is None: + return None + + m, n = arg_values["t"].m, arg_values["n"].n + dtype = arg_values["t"].dtype + + return Tile(dtype, m, n, "realize") + + + +add_builtin( + "tile_realize", + input_types={"t": Tile}, + value_func=tile_realize_value_func, + variadic=True, + doc="Force evaluation of a tile expression tree into local memory", + group="Tile Primitives", + export=False, +) + + + def tile_matmul_value_func(arg_types, arg_values): @@ -1855,24 +1880,24 @@ def tile_matmul_value_func(arg_types, arg_values): if len(arg_types) != 3: raise RuntimeError("tile_matmul() requires 4 positional args") - if not is_array(arg_types[0]): - raise RuntimeError("tile_matmul() argument 0 must be an array") + if not is_tile(arg_types["a"]): + raise RuntimeError("tile_matmul() argument 0 must be a tile") - if not is_array(arg_types[1]): - raise RuntimeError("tile_matmul() argument 1 must be an array") + if not is_tile(arg_types["b"]): + raise RuntimeError("tile_matmul() argument 1 must be an tile") - if not is_array(arg_types[2]): - raise RuntimeError("tile_matmul() argument 2 must be an array") + if not is_tile(arg_types["out"]): + raise RuntimeError("tile_matmul() argument 2 must be an tile") return None add_builtin( "tile_matmul", - input_types={"a": array(dtype=Any), "b": array(dtype=Any), "out": array(dtype=Any)}, + input_types={"a": Tile, "b": Tile, "out": Tile}, value_func=tile_matmul_value_func, variadic=True, - doc="Compute matrix product and accumulate out += a*b", + doc="Compute matrix product and accumulate out += a*b, a and b will be realized before evaluation, and output must already be realized.", group="Tile Primitives", export=False, ) @@ -1883,16 +1908,18 @@ def tile_map_value_func(arg_types, arg_values): if arg_types is None: return None + tiles = arg_types["args"] + # check all args are tiles - for a in arg_types["args"]: + for a in tiles: if not is_tile(a): raise RuntimeError(f"tile_map() arguments must be tiles, got type {a}") # use first argument to define output type - first = arg_types["args"][0] + first = tiles[0] # check all args have the same type and dimension - for a in arg_types["args"]: + for a in tiles: if a.dtype != first.dtype: raise RuntimeError(f"tile_map() arguments must all have the same type {first.dtype} != {a.dtype}") @@ -1902,11 +1929,12 @@ def tile_map_value_func(arg_types, arg_values): if a.N != first.N: raise RuntimeError(f"tile_map() arguments must all have the same n dimension {first.N} != {a.N}") - - return Tile(dtype=first.dtype, - M=first.M, - N=first.N, - op="map") + if len(tiles) == 1: + return TileUnaryMap(tiles[0]) + elif len(tiles) == 2: + return TileBinaryMap(tiles[0], tiles[1]) + else: + raise RuntimeError(f"tile_map() must have or two tile arguments") def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, args: Mapping[str, Var]): diff --git a/warp/codegen.py b/warp/codegen.py index 5d017e54..6a9991af 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -607,7 +607,7 @@ def type_to_ctype(t, value_type=False): classstr = f"wp::{type(t).__name__}" return f"{classstr}_t<{dtypestr}>" elif is_tile(t): - return "auto" + return t.ctype() elif isinstance(t, Struct): return make_full_qualified_name(t.cls) elif is_reference(t): diff --git a/warp/native/tile.h b/warp/native/tile.h index b3be6d81..5174e140 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -27,6 +27,37 @@ #endif +/* Tile Expressions + +[x] Forward / Backward code-gen +[ ] wp.tile_map() + [ ] Support user functions + [ ] Support built-in functions + [ ] Support for lambda functions +[ ] wp.tile_matmul() + [ ] Forward + [ ] Reverse +[ ] Support for n-d shape tiles / broadcasting / slicing? +[ ] Compile-time block dimensions +[ ] Support for CUB reductions +[ ] Support for CUB sorts +[ ] Examples + [ ] GEMM + [ ] Batched MLP + [ ] Point cloud alignment + [ ] Layer norm + +*/ + +// wp.tile_load(A, offset, shape) +// wp.tile_load(A, (x, y), (16, 16)) +// wp.tile_load(A, (x, y, z), (3, 3, 3)) + +// wp.tile_load(A, index, shape) +// wp.tile_load(A, x, m) +// wp.tile_load(A, x, y, m, n) +// wp.tile_load(A, x, y, z, m, n, o) +// wp.tile_load(A, x, y, z, m, n, o, p) namespace wp { @@ -78,6 +109,7 @@ struct tile_load_t array_t slice; + tile_load_t() {} tile_load_t(array_t& src, int x, int y) { assert(src.ndim == 2); @@ -132,9 +164,9 @@ struct tile_store_t static constexpr int N = Tile_::N; array_t slice; + Tile tile; - Tile& tile; - + tile_store_t() {} tile_store_t(array_t& dest, int x, int y, Tile& t) : tile(t) { assert(dest.ndim == 2); @@ -190,9 +222,10 @@ struct tile_constant_t static constexpr int N = N_; T c; - T& adj_c; + T* adj_c; - tile_constant_t(const T& c, T& adj_c) : c(c), adj_c(adj_c) {} + tile_constant_t() {} + tile_constant_t(const T& c, T& adj_c) : c(c), adj_c(&adj_c) {} Type fwd(int e) { @@ -201,7 +234,7 @@ struct tile_constant_t void bwd(int e, const T& adj_ret) { - adj_c += adj_ret; + *adj_c += adj_ret; } void print() @@ -212,21 +245,71 @@ struct tile_constant_t } }; +template +struct tile_zeros_t +{ + using Type = T; + static constexpr int M = M_; + static constexpr int N = N_; + + tile_zeros_t() {} + + Type fwd(int e) + { + return Type(0.0); + } + + void bwd(int e, const T& adj_ret) {} + + void print() + { + printf("tile_zeros_t<%d, %d>-+", M, N); + print(c); + printf("\n"); + } +}; + +template +struct tile_ones_t +{ + using Type = T; + static constexpr int M = M_; + static constexpr int N = N_; + + tile_ones_t() {} + + Type fwd(int e) + { + return Type(1.0); + } + void bwd(int e, const T& adj_ret) {} -template + void print() + { + printf("tile_ones_t<%d, %d>-+", M, N); + print(c); + printf("\n"); + } +}; + +template struct tile_unary_map_t { using Type = typename Tile::Type; static constexpr int M = Tile::M; static constexpr int N = Tile::N; - Tile& tile; + using FwdOp = Type(*)(Type); + using AdjOp = void(*)(Type, Type&, Type&); + + Tile tile; FwdOp fwd_fn; AdjOp adj_fn; - tile_unary_map_t(Tile& t, FwdOp f, AdjOp a) : tile(t), fwd_fn(f), adj_fn(a) {} + tile_unary_map_t() {} + tile_unary_map_t(Tile& t, FwdOp fwd, AdjOp adj) : tile(t), fwd_fn(fwd), adj_fn(adj) {} Type fwd(int e) const { @@ -249,7 +332,7 @@ struct tile_unary_map_t } }; -template +template struct tile_binary_map_t { static_assert(wp::is_same::value, "Error"); @@ -260,14 +343,17 @@ struct tile_binary_map_t static constexpr int M = TileA::M; static constexpr int N = TileA::N; - const TileA& tile_a; - const TileB& tile_b; + using FwdOp = Type(*)(Type, Type); + using AdjOp = void(*)(Type, Type, Type&, Type&, Type&); + + TileA tile_a; + TileB tile_b; FwdOp fwd_fn; AdjOp adj_fn; - - tile_binary_map_t(const TileA& a, TileB& b, FwdOp fwd_fn, AdjOp adj_fn) : tile_a(a), tile_b(b), fwd_fn(fwd_fn), adj_fn(adj_fn) {} + tile_binary_map_t() {} + tile_binary_map_t(const TileA& a, TileB& b, FwdOp fwd, AdjOp adj) : tile_a(a), tile_b(b), fwd_fn(fwd), adj_fn(adj) {} Type fwd(int e) const { @@ -300,11 +386,20 @@ struct tile_binary_map_t printf("\n -+"); tile_b.print(); } - }; + +//----------------------------------------------------------------------------------------------------- +// High level entry points for each op (correspond to one Warp builtin) + +template +tile_zeros_t tile_zeros() { return tile_zeros_t(); } + +template +tile_ones_t tile_ones() { return tile_ones_t(); } + // entry point for load template tile_load_t tile_load(array_t& a, int x, int y) @@ -341,19 +436,18 @@ void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_des } - // unary map -template -tile_unary_map_t tile_map_impl(FwdOp fwd, AdjOp adj, Tile& a) +template +tile_unary_map_t tile_map_impl(typename tile_unary_map_t::FwdOp fwd, typename tile_unary_map_t::AdjOp adj, Tile& a) { - return tile_unary_map_t(a, fwd, adj); + return tile_unary_map_t(a, fwd, adj); } // binary map -template -tile_binary_map_t tile_map_impl(FwdOp fwd, AdjOp adj, TileA& a, TileB& b) +template +tile_binary_map_t tile_map_impl(typename tile_binary_map_t::FwdOp fwd, typename tile_binary_map_t::AdjOp adj, TileA& a, TileB& b) { - return tile_binary_map_t(a, b, fwd, adj); + return tile_binary_map_t(a, b, fwd, adj); } // use macro to capture adjoint operator @@ -370,3 +464,90 @@ void adj_tile_map_impl(void) {} } // namespace wp +#if 0 + +//----------------------------------------------------- +// c = a + b + +// forward +auto var_0 = wp::tile_load(var_A, x, y); +auto var_1 = wp::tile_load(var_B, x, y); +auto var_2 = wp::tile_add(var_0, var_1); +wp::tile_store(var_C, x, y, var_2) + +// reverse +wp::adj_store(var_C, x, y, var_2, adj_C, _, _, adj_2) +wp::adj_tile_add(var_0, var_1, adj_0, adj_1, adj_2) +wp::adj_tile_load(var_B, x, y, adj_B, _, _, adj_1); +wp::adj_tile_load(var_B, x, y, adj_B, _, _, adj_0); + + +//----------------------------------------------------- +// x = a[0] +// c = x*2.0 + x + +// forward +auto var_0 = wp::tile_load(var_A, x, y); +auto var_1 = wp::tile_mul(var_0, 2.0); +auto var_2 = wp::tile_add(var_0, var_1); +wp::tile_store(var_C, x, y, var_2) + +struct adj_store_t +{ + adj_store_t() + { + + } + + float bwd(int i, float adj_ret) + { + return array.grad[i]; + } +}; + +template +struct adj_add_t +{ + adj_add_t(P& parent) + { + + } + + float bwd(int i, float& adj_a, float& adj_b) + { + // evaluate parent + float adj_ret = parent.bwd(i); + + adj_a += adj_ret; + adj_b += adj_ret; + } +}; + +template +struct adj_tile +{ + adj_tile(T& parent) + { + + } + + + +}; + +void adj_tile_load(A, x, y, adj_A, adj_x, adj_y, adj_ret) +{ + for i in A(x,y): + adj_A[i] += adj_ret(i); +} + + + +// reverse +wp::adj_store(var_C, x, y, var_2, adj_C, _, _, adj_2) // adj_2->adj_C +wp::adj_tile_add(var_0, var_1, adj_0, adj_1, adj_2) // adj_0->adj_2->adj_C, adj_1->adj_2->adj_C +wp::adj_tile_mul(var_0, 2.0, adj_0, _, adj_1); // adj_0->adj_1->adj_2->adj_C +wp::adj_tile_load(var_A, x, y, adj_A, _, _, adj_0); // adj_A->adj_0->adj_1->adj_2->adj_C + + +#endif \ No newline at end of file diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index 91ed329d..15e22cbd 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -5,7 +5,7 @@ // todo: requires CTK, replace with inline ptx #include "cuda_pipeline_primitives.h" -#define USE_CUTE 1 +#define USE_CUTE 0 #if USE_CUTE #include "cutlass/include/cute/tensor.hpp" @@ -15,7 +15,7 @@ namespace wp { - +/* // 2D tile zero template inline CUDA_CALLABLE array_t tile_zeros() @@ -84,6 +84,7 @@ inline CUDA_CALLABLE void tile_store(array_t& dest, int i, int j, const array index(dest, i*M + t/N, j*N + t%N) = src.data[t]; } } +*/ template inline CUDA_CALLABLE const T& index(const T* __restrict__ p, int i, int j, int stride) @@ -174,7 +175,7 @@ inline void partition_store(const partition_t& tile, int i, int j, cons #if !USE_CUTE template -inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, const array_t& out) +inline CUDA_CALLABLE void gemm(const array_t& A, const array_t& B, const array_t& out) { const int TILE_M = 4; const int TILE_N = 4; @@ -307,4 +308,72 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, #endif // USE_CUTE +template +struct tile_matmul_t +{ + static_assert(wp::is_same::value, "Error"); + static_assert(TileA::N == TileB::M, "Error, inner dimensions must match"); + static_assert(TileC::M == TileA::M, "Error, first output dimension must match"); + static_assert(TileC::N == TileB::N, "Error, second output dimension must match"); + + using Type = typename TileA::Type; + static constexpr int M = TileC::M; + static constexpr int N = TileC::N; + + const TileA& tile_a; + const TileB& tile_b; + + tile_matmul_t(const TileA &a, TileB &b, TileC &b) : tile_a(a), + tile_b(b), + tile_c(c) {} + + Type fwd(int e) const + { + // load + + + } + + void bwd(int e, Type adj_ret) const + { + Type a = tile_a.fwd(e); + Type b = tile_b.fwd(e); + + Type adj_a = 0.0; + Type adj_b = 0.0; + + adj_fn(a, b, adj_a, adj_b, adj_ret); + + // recurse + tile_a.bwd(e, adj_a); + tile_b.bwd(e, adj_b); + } + + void print() + { + printf("tile_binary_map_t<%d, %d>", M, N); + printf("\n -+"); + tile_a.print(); + printf("\n -+"); + tile_b.print(); + } +}; + +template +void tile_matmul(TileA& a, TileB& b, TileC& c) +{ + // load a to shared + // load b to shared + +} + + +template +void adj_tile_matmul(TileA& a, TileB& b, TileC& c, + TileA& adj_a, TileB& adj_b, TileC& adj_c) +{ +} + + + } // namespace wp \ No newline at end of file diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index beea0746..cc1f4a3a 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -155,36 +155,31 @@ def test_tile_binary_map(): print("Binary map backward passed") -test_tile_copy() -test_tile_unary_map() -test_tile_binary_map() -# @wp.kernel -# def gemm(A: wp.array2d(dtype=float), -# B: wp.array2d(dtype=float), -# C: wp.array2d(dtype=float)): +TILE_M = wp.constant(64) +TILE_N = wp.constant(64) +TILE_K = wp.constant(8) -# # output index -# i, j = wp.tid() +# sum = wp.tile_zeros(M,N) -# sum = float(0.0) +# for i in range(5): -# for k in range(0, A.shape[1]): -# sum += A[i, k]*B[k, j] +# a = wp.tile_load(A) +# b = wp.tile_load(B) -# C[i, j] = sum +# a2 = a*2.0 + +# wp.tile_matmul(a2, b, sum) +# wp.tile_store(sum) -# TILE_M = wp.constant(64) -# TILE_N = wp.constant(64) -# TILE_K = wp.constant(8) # @wp.kernel -# def gemm_tiled(A: wp.array2d(dtype=float), -# B: wp.array2d(dtype=float), -# C: wp.array2d(dtype=float)): +# def tile_gemm(A: wp.array2d(dtype=float), +# B: wp.array2d(dtype=float), +# C: wp.array2d(dtype=float)): # # output tile index # i, j = wp.tid() @@ -197,7 +192,7 @@ def test_tile_binary_map(): # count = int(K / TILE_K) # todo: must be the same as TILE_K -# for k in range(count): +# for k in range(0, K, TILE_K): # a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) # b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) @@ -208,68 +203,30 @@ def test_tile_binary_map(): # wp.tile_store(C, i, j, sum) -# s = 0.0 - -# for i, j in tile.shape: - -# s += tile[i-1, i-1] -# s += tile[i, i-1] -# s += tile[i,] - - - -# M = TILE_M*7 -# K = TILE_K*4 -# N = TILE_N*6 - -# rng = np.random.default_rng(42) -# A = rng.random((M, K), dtype=np.float32) -# B = rng.random((K, N), dtype=np.float32) -# C = np.zeros((M, N), dtype=np.float32) - -# A_wp = wp.array(A) -# B_wp = wp.array(B) -# C_wp = wp.array(C) - -# iters = 10 +# def test_tile_gemm(): -# with wp.ScopedTimer("NumPy"): +# M = TILE_M*7 +# K = TILE_K*4 +# N = TILE_N*6 -# for i in range(iters): -# C = A@B +# rng = np.random.default_rng(42) +# A = rng.random((M, K), dtype=np.float32) +# B = rng.random((K, N), dtype=np.float32) +# C = np.zeros((M, N), dtype=np.float32) -# wp.force_load(device="cuda:0") +# A_wp = wp.array(A) +# B_wp = wp.array(B) +# C_wp = wp.array(C) -# with wp.ScopedTimer("Warp", cuda_filter=wp.TIMING_KERNEL): +# iters = 10 -# for i in range(iters): -# wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) +# wp.launch(tile_gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) +# assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) -# print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) - -# for i in range(iters): -# wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=128) -# wp.synchronize() - - -# print(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) - - -# A_tc = torch.from_numpy(A).to("cuda:0") -# B_tc = torch.from_numpy(B).to("cuda:0") -# C_tc = torch.from_numpy(C).to("cuda:0") - -# for i in range(10): -# torch.matmul(A_tc, B_tc, out=C_tc) - -# with wp.ScopedTimer("Torch"): - -# for i in range(iters): -# torch.matmul(A_tc, B_tc, out=C_tc) - -# torch.cuda.synchronize() - - +test_tile_copy() +test_tile_unary_map() +test_tile_binary_map() +#test_tile_gemm() \ No newline at end of file diff --git a/warp/types.py b/warp/types.py index 03065d6d..9d993169 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2869,6 +2869,48 @@ def __init__(self, dtype, M, N, op): self.N = N self.op = op +class TileZeros(Tile): + + def __init__(self, dtype, M, N): + Tile.__init__(self, dtype, M, N, "zeros") + + def ctype(self): + from warp.codegen import Var + return f"wp::tile_zeros_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + +class TileLoad(Tile): + + def __init__(self, array, M, N): + Tile.__init__(self, array.dtype, M, N, "load") + + def ctype(self): + from warp.codegen import Var + return f"wp::tile_load_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + +class TileUnaryMap(Tile): + + def __init__(self, t): + Tile.__init__(self, t.dtype, t.M, t.N, "unary_map") + + self.t = t + + def ctype(self): + from warp.codegen import Var + return f"wp::tile_unary_map_t<{self.t.ctype()}>" + +class TileBinaryMap(Tile): + + def __init__(self, a, b): + Tile.__init__(self, a.dtype, a.M, a.N, "binary_map") + + self.a = a + self.b = b + + def ctype(self): + from warp.codegen import Var + return f"wp::tile_binary_map_t<{self.a.ctype()}, {self.b.ctype()}>" + + def is_tile(t): return isinstance(t, Tile) From b61be8f0ef452056351204b6b074019c485ed6e7 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 27 Aug 2024 14:55:54 +1200 Subject: [PATCH 017/102] wp.tile_matmul() and wp.tile_eval() expressions forward mode working --- warp/builtins.py | 84 +++++++++++++++++++++++++++++------------ warp/native/builtin.h | 2 +- warp/native/tile.h | 49 ++++++++++++++++++++++-- warp/native/tile_gemm.h | 63 ++++++++++++++++--------------- warp/tests/test_tile.py | 65 +++++++++++++++---------------- warp/types.py | 12 ++++++ 6 files changed, 182 insertions(+), 93 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index b721dea1..c96b9f56 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1845,63 +1845,97 @@ def tile_store_value_func(arg_types, arg_values): ) -def tile_realize_value_func(arg_types, arg_values): + +def tile_matmul_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: return None - m, n = arg_values["t"].m, arg_values["n"].n - dtype = arg_values["t"].dtype + if len(arg_types) != 3: + raise RuntimeError("tile_matmul() requires 4 positional args") + + if not is_tile(arg_types["a"]): + raise RuntimeError("tile_matmul() argument 0 must be a tile") + + if not is_tile(arg_types["b"]): + raise RuntimeError("tile_matmul() argument 1 must be an tile") + + if not isinstance(arg_types["out"], TileShared): + raise RuntimeError("tile_matmul() output must be a fully evaluated tile, e.g.: created using tile_eval()") + + return None + +def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): - return Tile(dtype, m, n, "realize") + a = arg_values["a"] + b = arg_values["b"] + out = arg_values["out"] + + # template_args.append(dtype) + # template_args.append(m) + # template_args.append(n) + + global shared_memory_id + + template_args = [] + template_args.append(shared_memory_id) + # matmul makes two allocations (one for each of its arguments) + shared_memory_id += 1 + shared_memory_id += 1 + + return ((a, b, out), template_args) add_builtin( - "tile_realize", - input_types={"t": Tile}, - value_func=tile_realize_value_func, + "tile_matmul", + input_types={"a": Tile, "b": Tile, "out": Tile}, + value_func=tile_matmul_value_func, + dispatch_func=tile_matmul_dispatch_func, variadic=True, - doc="Force evaluation of a tile expression tree into local memory", + doc="Compute matrix product and accumulate out += a*b, a and b will be realized before evaluation, and output must already be realized.", group="Tile Primitives", export=False, ) - - - -def tile_matmul_value_func(arg_types, arg_values): +def tile_eval_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: return None - if len(arg_types) != 3: - raise RuntimeError("tile_matmul() requires 4 positional args") + if not is_tile(arg_types["t"]): + raise RuntimeError("tile_eval() argument must be a tile") - if not is_tile(arg_types["a"]): - raise RuntimeError("tile_matmul() argument 0 must be a tile") + return TileShared(arg_types["t"]) - if not is_tile(arg_types["b"]): - raise RuntimeError("tile_matmul() argument 1 must be an tile") +def tile_eval_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): - if not is_tile(arg_types["out"]): - raise RuntimeError("tile_matmul() argument 2 must be an tile") + t = arg_values["t"] - return None + global shared_memory_id + + template_args = [] + template_args.append(shared_memory_id) + + # matmul makes two allocations (one for each of its arguments) + shared_memory_id += 1 + return ((t,), template_args) add_builtin( - "tile_matmul", - input_types={"a": Tile, "b": Tile, "out": Tile}, - value_func=tile_matmul_value_func, + "tile_eval", + input_types={"t": Tile}, + value_func=tile_eval_value_func, + dispatch_func=tile_eval_dispatch_func, variadic=True, - doc="Compute matrix product and accumulate out += a*b, a and b will be realized before evaluation, and output must already be realized.", + doc="Force evaluation of a tile expression into shared memory", group="Tile Primitives", export=False, ) + # does type propagation for load() def tile_map_value_func(arg_types, arg_values): diff --git a/warp/native/builtin.h b/warp/native/builtin.h index 544d771d..a899d9a7 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1590,5 +1590,5 @@ inline CUDA_CALLABLE void adj_expect_near(const vec3& actual, const vec3& expect // only include in kernels for now #if defined(__CUDACC_RTC__) #include "tile.h" -//#include "tile_gemm.h" +#include "tile_gemm.h" #endif \ No newline at end of file diff --git a/warp/native/tile.h b/warp/native/tile.h index 5174e140..c7666513 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -227,12 +227,12 @@ struct tile_constant_t tile_constant_t() {} tile_constant_t(const T& c, T& adj_c) : c(c), adj_c(&adj_c) {} - Type fwd(int e) + Type fwd(int e) const { return c; } - void bwd(int e, const T& adj_ret) + void bwd(int e, const T& adj_ret) const { *adj_c += adj_ret; } @@ -254,12 +254,12 @@ struct tile_zeros_t tile_zeros_t() {} - Type fwd(int e) + Type fwd(int e) const { return Type(0.0); } - void bwd(int e, const T& adj_ret) {} + void bwd(int e, const T& adj_ret) const {} void print() { @@ -389,7 +389,30 @@ struct tile_binary_map_t }; +template +struct tile_shared_t +{ + using Type = T; + static constexpr int M = M_; + static constexpr int N = N_; + + T* data = NULL; + + tile_shared_t() {} + tile_shared_t(T* smem) : data(smem) + { + } + + T fwd(int e) const + { + return data[e]; + } + + void bwd(int e, T adj_ret) const + { + } +}; //----------------------------------------------------------------------------------------------------- // High level entry points for each op (correspond to one Warp builtin) @@ -407,6 +430,24 @@ tile_load_t tile_load(array_t& a, int x, int y) return tile_load_t(a, x, y); } +template +tile_shared_t tile_eval(Tile& t) +{ + WP_TILE_SHARED typename Tile::Type data[Tile::M*Tile::N]; + + // evaluate the input tile and store into shared memory + for (int i=threadIdx.x; i < size(t); i += blockDim.x) + data[i] = t.fwd(i); + + return tile_shared_t(data); +} + +template +void adj_tile_eval(Tile& t, Tile& adj_t, tile_shared_t& adj_ret) +{ + // nop +} + template void adj_tile_load(array_t& a, int x, int y, array_t& adj_a, int adj_x, int adj_y, const tile_load_t& adj_ret) { diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index 15e22cbd..27b5b852 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -215,15 +215,18 @@ inline CUDA_CALLABLE void gemm(const array_t& A, const array_t& B, const a } - // 2D gemm accumulate out += A*B -template -inline CUDA_CALLABLE void tile_matmul_scalar(const array_t& A, const array_t& B, const array_t& out) +template +inline CUDA_CALLABLE void tile_matmul_scalar(const TileA& A, + const TileB& B, + const TileC& out) { - const int length = out.shape[0]*out.shape[1]; + const int length = size(out); WP_TILE_SYNC(); + using T = typename TileA::Type; + const T* __restrict__ A_ptr = A.data; const T* __restrict__ B_ptr = B.data; T* __restrict__ C_ptr = out.data; @@ -232,21 +235,21 @@ inline CUDA_CALLABLE void tile_matmul_scalar(const array_t& A, const array_t< for (int t=threadIdx.x; t < length; t += blockDim.x) { // compute output index - const int i = t/out.shape[1]; - const int j = t%out.shape[1]; + const int i = t/out.N; + const int j = t%out.N; T sum(0.0); WP_PRAGMA_UNROLL - for (int k=0; k < A.shape[1]; ++k) + for (int k=0; k < A.N; ++k) { - T a = index(A_ptr, i, k, A.shape[1]); - T b = index(B_ptr, k, j, B.shape[1]); + T a = index(A_ptr, i, k, A.N); + T b = index(B_ptr, k, j, B.N); sum = fmaf(a, b, sum); } - index(C_ptr, i, j, out.shape[1]) += sum; + index(C_ptr, i, j, out.N) += sum; } WP_TILE_SYNC(); @@ -311,7 +314,7 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, template struct tile_matmul_t { - static_assert(wp::is_same::value, "Error"); + static_assert(wp::is_same::value, "Error, tile datatypes must match"); static_assert(TileA::N == TileB::M, "Error, inner dimensions must match"); static_assert(TileC::M == TileA::M, "Error, first output dimension must match"); static_assert(TileC::N == TileB::N, "Error, second output dimension must match"); @@ -320,12 +323,13 @@ struct tile_matmul_t static constexpr int M = TileC::M; static constexpr int N = TileC::N; - const TileA& tile_a; - const TileB& tile_b; + TileA tile_a; + TileB tile_b; + TileC tile_c; - tile_matmul_t(const TileA &a, TileB &b, TileC &b) : tile_a(a), - tile_b(b), - tile_c(c) {} + tile_matmul_t(TileA &a, TileB &b, TileC &c) : tile_a(a), + tile_b(b), + tile_c(c) {} Type fwd(int e) const { @@ -336,22 +340,11 @@ struct tile_matmul_t void bwd(int e, Type adj_ret) const { - Type a = tile_a.fwd(e); - Type b = tile_b.fwd(e); - - Type adj_a = 0.0; - Type adj_b = 0.0; - - adj_fn(a, b, adj_a, adj_b, adj_ret); - - // recurse - tile_a.bwd(e, adj_a); - tile_b.bwd(e, adj_b); } void print() { - printf("tile_binary_map_t<%d, %d>", M, N); + printf("tile_matmul_t<%d, %d>", M, N); printf("\n -+"); tile_a.print(); printf("\n -+"); @@ -359,12 +352,20 @@ struct tile_matmul_t } }; -template + +template void tile_matmul(TileA& a, TileB& b, TileC& c) { - // load a to shared - // load b to shared + static_assert(wp::is_same::value, "Error, tile datatypes must match"); + static_assert(TileA::N == TileB::M, "Error, inner dimensions must match"); + static_assert(TileC::M == TileA::M, "Error, first output dimension must match"); + static_assert(TileC::N == TileB::N, "Error, second output dimension must match"); + // load inputs to shared + auto a_shared = tile_eval(a); + auto b_shared = tile_eval(b); + + tile_matmul_scalar(a_shared, b_shared, c); } diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index cc1f4a3a..56a621f0 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -176,57 +176,58 @@ def test_tile_binary_map(): -# @wp.kernel -# def tile_gemm(A: wp.array2d(dtype=float), -# B: wp.array2d(dtype=float), -# C: wp.array2d(dtype=float)): +@wp.kernel +def tile_gemm(A: wp.array2d(dtype=float), + B: wp.array2d(dtype=float), + C: wp.array2d(dtype=float)): -# # output tile index -# i, j = wp.tid() + # output tile index + i, j = wp.tid() -# sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + sum = wp.tile_eval(wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32)) -# M = A.shape[0] -# N = B.shape[1] -# K = A.shape[1] + M = A.shape[0] + N = B.shape[1] + K = A.shape[1] -# count = int(K / TILE_K) # todo: must be the same as TILE_K + count = int(K / TILE_K) # todo: must be the same as TILE_K -# for k in range(0, K, TILE_K): + for k in range(0, count): -# a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) -# b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) + a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) + b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) -# # sum += a*b -# wp.tile_matmul(a, b, sum) + # sum += a*b + wp.tile_matmul(a, b, sum) -# wp.tile_store(C, i, j, sum) + wp.tile_store(C, i, j, sum) -# def test_tile_gemm(): +def test_tile_gemm(): -# M = TILE_M*7 -# K = TILE_K*4 -# N = TILE_N*6 + M = TILE_M*7 + K = TILE_K*4 + N = TILE_N*6 -# rng = np.random.default_rng(42) -# A = rng.random((M, K), dtype=np.float32) -# B = rng.random((K, N), dtype=np.float32) -# C = np.zeros((M, N), dtype=np.float32) + rng = np.random.default_rng(42) + A = rng.random((M, K), dtype=np.float32) + B = rng.random((K, N), dtype=np.float32) + C = np.zeros((M, N), dtype=np.float32) -# A_wp = wp.array(A) -# B_wp = wp.array(B) -# C_wp = wp.array(C) + A_wp = wp.array(A) + B_wp = wp.array(B) + C_wp = wp.array(C) -# iters = 10 + wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=8) -# wp.launch(tile_gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) + assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) -# assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) + # GEMM forward passed + print("Binary map backward passed") test_tile_copy() test_tile_unary_map() test_tile_binary_map() -#test_tile_gemm() \ No newline at end of file +test_tile_gemm() \ No newline at end of file diff --git a/warp/types.py b/warp/types.py index 9d993169..b5f02dba 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2911,6 +2911,18 @@ def ctype(self): return f"wp::tile_binary_map_t<{self.a.ctype()}, {self.b.ctype()}>" +class TileShared(Tile): + + def __init__(self, t): + Tile.__init__(self, t.dtype, t.M, t.N, "shared") + + self.t = t + + def ctype(self): + from warp.codegen import Var + return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + + def is_tile(t): return isinstance(t, Tile) From e3dfca85f7386de95a85cbb96460016e4bbc1af5 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 29 Aug 2024 14:50:31 +1200 Subject: [PATCH 018/102] Working on operator support --- warp/builtins.py | 91 ++++++++++++ warp/native/tile.h | 300 +++++++++++++++++++++++++++++++++++++++- warp/native/tile_gemm.h | 7 + warp/tests/test_tile.py | 98 +++++++++++-- warp/types.py | 17 ++- 5 files changed, 495 insertions(+), 18 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index c96b9f56..fc3438cb 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -4442,3 +4442,94 @@ def matmat_mul_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str add_builtin("unot", input_types={"a": array(dtype=Any)}, value_type=builtins.bool, doc="", group="Operators") + + +# Tile operators +def tile_unary_value_func(arg_types, arg_values): + + if arg_types is None: + return Tile(dtype=Any, M=Any, N=Any) + + t = arg_types["x"] + + if not is_tile(t): + raise RuntimeError("Expected tile for unary expression") + + return TileUnaryMap(t) + +def tile_scalar_mul_value_func(arg_types, arg_values): + + if arg_types is None: + return Tile(dtype=Any, M=Any, N=Any) + + x = arg_types["x"] + y = arg_types["y"] + + # tile*scalar + if is_tile(x): + if x.dtype != y: + raise RuntimeError("Scalar factor should have the same type as tile for tile*scalar, tile type: {x} scalar type: {y}") + + return TileBinaryMap(x, TileConstant(x.dtype, x.M, x.N)) + + # scalar*tile + if is_tile(y): + if y.dtype != x: + raise RuntimeError("Scalar factor should have the same type as tile for scalar*tile, tile type: {x} scalar type: {y}") + + return TileBinaryMap(TileConstant(x.dtype, x.M, x.N), y) + + + +# def tile_binary_value_func(arg_types, arg_values): + +# if arg_types is None: +# return Tile(dtype=Any, M=Any, N=Any) + +# a = arg_types[0] + + +# if not is_tile(t): +# raise RuntimeError("Expected tile for unary expression") + +# return TileUnaryMap(t.dtype, t.M, t.N) + +add_builtin( + "neg", + input_types={"x": Tile(dtype=Any, M=Any, N=Any)}, + value_func=tile_unary_value_func, + doc="", + export=False, + native_func="tile_neg", + group="Operators", +) + +add_builtin( + "mul", + input_types={"x": Tile(dtype=Any, M=Any, N=Any), "y": Scalar}, + value_func=tile_scalar_mul_value_func, + doc="", + export=False, + native_func="tile_mul", + group="Operators", +) + +add_builtin( + "mul", + input_types={"x": Scalar, "y": Tile(dtype=Any, M=Any, N=Any)}, + value_func=tile_scalar_mul_value_func, + doc="", + export=False, + native_func="tile_mul", + group="Operators", +) + +# add_builtin( +# "mul", +# input_types={"x": Tile, "s": Scalar}, +# value_func=tile_binary_value_func, +# doc="", +# group="Operators", +# ) + + diff --git a/warp/native/tile.h b/warp/native/tile.h index c7666513..009709a2 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -31,13 +31,13 @@ [x] Forward / Backward code-gen [ ] wp.tile_map() - [ ] Support user functions + [x] Support user functions [ ] Support built-in functions [ ] Support for lambda functions [ ] wp.tile_matmul() - [ ] Forward + [x] Forward [ ] Reverse -[ ] Support for n-d shape tiles / broadcasting / slicing? +[ ] Support for n-d shape tiles / broadcasting / slicing / transpose? [ ] Compile-time block dimensions [ ] Support for CUB reductions [ ] Support for CUB sorts @@ -46,7 +46,7 @@ [ ] Batched MLP [ ] Point cloud alignment [ ] Layer norm - + */ // wp.tile_load(A, offset, shape) @@ -388,7 +388,251 @@ struct tile_binary_map_t } }; +//----------------------------------------------- +// Operators + + +template +CUDA_CALLABLE inline tile_unary_map_t tile_pos(const Tile& t) +{ + return tile_unary_map_t(t, [](typename Tile::Type x) { return pos(x); } ); +} + +template +CUDA_CALLABLE inline tile_unary_map_t tile_neg(Tile& t) +{ + typedef tile_unary_map_t Op; + + typename Op::FwdOp fwd = [](typename Tile::Type x) { return neg(x); }; + typename Op::AdjOp adj = [](typename Tile::Type x, typename Tile::Type& adj_x, typename Tile::Type& adj_ret) { adj_neg(x, adj_x, adj_ret); }; + + return Op(t, fwd, adj); +} + +template +CUDA_CALLABLE inline void adj_tile_neg(const Tile& t, Tile& adj_t, tile_unary_map_t& adj_ret) +{ + // nop +} + + +/* + +template +CUDA_CALLABLE inline vec_t neg(const vec_t& x) +{ + return -x; +} + +template +CUDA_CALLABLE inline vec_t<3, Type> neg(const vec_t<3, Type>& x) +{ + return vec_t<3, Type>(-x.c[0], -x.c[1], -x.c[2]); +} + +template +CUDA_CALLABLE inline vec_t<2, Type> neg(const vec_t<2, Type>& x) +{ + return vec_t<2, Type>(-x.c[0], -x.c[1]); +} + +template +CUDA_CALLABLE inline void adj_neg(const vec_t& x, vec_t& adj_x, const vec_t& adj_ret) +{ + adj_x -= adj_ret; +} + +// equality: +template +inline CUDA_CALLABLE bool operator ==(const vec_t& a, const vec_t& b) +{ + for( unsigned i=0; i < Length; ++i ) + { + if(a[i] != b[i]) + { + return false; + } + } + return true; +} + +// scalar multiplication: +template +inline CUDA_CALLABLE vec_t mul(vec_t a, Type s) +{ + vec_t ret; + for( unsigned i=0; i < Length; ++i ) + { + ret[i] = a[i] * s; + } + return ret; +} + +template +inline CUDA_CALLABLE vec_t<3, Type> mul(vec_t<3, Type> a, Type s) +{ + return vec_t<3, Type>(a.c[0]*s,a.c[1]*s,a.c[2]*s); +} + +template +inline CUDA_CALLABLE vec_t<2, Type> mul(vec_t<2, Type> a, Type s) +{ + return vec_t<2, Type>(a.c[0]*s,a.c[1]*s); +} + +template +inline CUDA_CALLABLE vec_t mul(Type s, vec_t a) +{ + return mul(a, s); +} + +template +inline CUDA_CALLABLE vec_t operator*(Type s, vec_t a) +{ + return mul(a, s); +} + +template +inline CUDA_CALLABLE vec_t operator*(vec_t a, Type s) +{ + return mul(a, s); +} + + +// component wise multiplication: +template +inline CUDA_CALLABLE vec_t cw_mul(vec_t a, vec_t b) +{ + vec_t ret; + for( unsigned i=0; i < Length; ++i ) + { + ret[i] = a[i] * b[i]; + } + return ret; +} + +// division +template +inline CUDA_CALLABLE vec_t div(vec_t a, Type s) +{ + vec_t ret; + for( unsigned i=0; i < Length; ++i ) + { + ret[i] = a[i] / s; + } + return ret; +} + +template +inline CUDA_CALLABLE vec_t<3, Type> div(vec_t<3, Type> a, Type s) +{ + return vec_t<3, Type>(a.c[0]/s,a.c[1]/s,a.c[2]/s); +} + +template +inline CUDA_CALLABLE vec_t<2, Type> div(vec_t<2, Type> a, Type s) +{ + return vec_t<2, Type>(a.c[0]/s,a.c[1]/s); +} + +template +inline CUDA_CALLABLE vec_t div(Type s, vec_t a) +{ + vec_t ret; + for (unsigned i=0; i < Length; ++i) + { + ret[i] = s / a[i]; + } + return ret; +} + +template +inline CUDA_CALLABLE vec_t<3, Type> div(Type s, vec_t<3, Type> a) +{ + return vec_t<3, Type>(s/a.c[0],s/a.c[1],s/a.c[2]); +} + +template +inline CUDA_CALLABLE vec_t<2, Type> div(Type s, vec_t<2, Type> a) +{ + return vec_t<2, Type>(s/a.c[0],s/a.c[1]); +} + +template +inline CUDA_CALLABLE vec_t operator / (vec_t a, Type s) +{ + return div(a,s); +} + +template +inline CUDA_CALLABLE vec_t operator / (Type s, vec_t a) +{ + return div(s, a); +} + +// component wise division +template +inline CUDA_CALLABLE vec_t cw_div(vec_t a, vec_t b) +{ + vec_t ret; + for( unsigned i=0; i < Length; ++i ) + { + ret[i] = a[i] / b[i]; + } + return ret; +} + +// addition +template +inline CUDA_CALLABLE vec_t add(vec_t a, vec_t b) +{ + vec_t ret; + for( unsigned i=0; i < Length; ++i ) + { + ret[i] = a[i] + b[i]; + } + return ret; +} + +template +inline CUDA_CALLABLE vec_t<2, Type> add(vec_t<2, Type> a, vec_t<2, Type> b) +{ + return vec_t<2, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1]); +} + +template +inline CUDA_CALLABLE vec_t<3, Type> add(vec_t<3, Type> a, vec_t<3, Type> b) +{ + return vec_t<3, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1], a.c[2] + b.c[2]); +} + +// subtraction +template +inline CUDA_CALLABLE vec_t sub(vec_t a, vec_t b) +{ + vec_t ret; + for( unsigned i=0; i < Length; ++i ) + { + ret[i] = Type(a[i] - b[i]); + } + return ret; +} + +template +inline CUDA_CALLABLE vec_t<2, Type> sub(vec_t<2, Type> a, vec_t<2, Type> b) +{ + return vec_t<2, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1]); +} + +template +inline CUDA_CALLABLE vec_t<3, Type> sub(vec_t<3, Type> a, vec_t<3, Type> b) +{ + return vec_t<3, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1], a.c[2] - b.c[2]); +} +*/ + +// represents a fully evaluated tile in shared memory template struct tile_shared_t { @@ -495,7 +739,6 @@ tile_binary_map_t tile_map_impl(typename tile_binary_map_t(var, adj_##var) + +/* +// handle tile*scalar +template +CUDA_CALLABLE inline auto tile_mul_impl(Tile& t, typename Tile::Type s, + Tile& adj_t, typename Tile::Type adj_s) +{ + typedef typename Tile::Type T; + typedef tile_constant_t Constant; + + typedef tile_binary_map_t Op; + + typename Op::FwdOp fwd = [](T a, T b) { return mul(a, b); }; + typename Op::AdjOp adj = [](T a, T b, T& adj_a, T& adj_b, T& adj_ret) { adj_mul(a, b, adj_a, adj_b, adj_ret); }; + + // promote scalar to constant tile + Constant c(s, adj_s); + + return Op(t, c, fwd, adj); +} + +// handle scalar*tile +template +CUDA_CALLABLE inline auto tile_mul_impl(typename Tile::Type s, Tile& t, + typename Tile::Type adj_s, Tile& adj_t) +{ + typedef typename Tile::Type T; + typedef tile_constant_t Constant; + + typedef tile_binary_map_t Op; + + typename Op::FwdOp fwd = [](T a, T b) { return mul(a, b); }; + typename Op::AdjOp adj = [](T a, T b, T& adj_a, T& adj_b, T& adj_ret) { adj_mul(a, b, adj_a, adj_b, adj_ret); }; + + // promote scalar to constant tile + Constant c(s, adj_s); + + return Op(c, t, fwd, adj); + +} + + +#define tile_mul(a, b) tile_mul_impl(a, b adj_##a, adj_##b) +#define tile_add(a, b) tile_add_impl(a, b adj_##a, adj_##b) +*/ + + } // namespace wp #if 0 diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index 27b5b852..b1d3435e 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -373,6 +373,13 @@ template void adj_tile_matmul(TileA& a, TileB& b, TileC& c, TileA& adj_a, TileB& adj_b, TileC& adj_c) { + + // auto a_shared = tile_eval(a); + // auto b_shared = tile_eval(b); + // auto adj_c_shared = tile_eval(b); + + // tile_matmul_scalar(adj_c, wp.tile_transpose(b), adj_a); + // tile_matmul_scalar(wp.tile_transpose(a), adj_c, adj_b); } diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 56a621f0..e0c34de2 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -156,24 +156,98 @@ def test_tile_binary_map(): print("Binary map backward passed") +@wp.kernel +def tile_operators(input: wp.array3d(dtype=float), + output: wp.array3d(dtype=float)): + + # output tile index + i = wp.tid() + + a = wp.tile_load(input[i], 0, 0, m=32, n=8) + + # neg + b = -a + + # scalar multiply +# c = b*0.5 + + # # add tiles + # c = a + b + + wp.tile_store(output[i], 0, 0, b) + + +def test_tile_operators(): + + batch_count = 56 + + M = 32 + N = 8 + + rng = np.random.default_rng(42) + input = rng.random((batch_count, M, N), dtype=np.float32) + output = -input + + input_wp = wp.array(input) + output_wp = wp.zeros_like(input_wp) + + wp.launch(tile_operators, dim=batch_count, inputs=[input_wp, output_wp], tile_size=8) + + assert(np.allclose(output, output_wp.numpy(), rtol=1.e-4)) + + print("operators forward passed") + + TILE_M = wp.constant(64) TILE_N = wp.constant(64) TILE_K = wp.constant(8) -# sum = wp.tile_zeros(M,N) +@wp.kernel +def tile_grouped_gemm(A: wp.array3d(dtype=float), + B: wp.array3d(dtype=float), + C: wp.array3d(dtype=float)): -# for i in range(5): + # output tile index + i = wp.tid() -# a = wp.tile_load(A) -# b = wp.tile_load(B) + a = wp.tile_load(A[i], 0, 0, m=TILE_M, n=TILE_K) + b = wp.tile_load(B[i], 0, 0, m=TILE_K, n=TILE_N) -# a2 = a*2.0 - -# wp.tile_matmul(a2, b, sum) + sum = wp.tile_eval(wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32)) + + wp.tile_matmul(a, b, sum) + + wp.tile_store(C[i], 0, 0, sum) + + +def test_tile_batched_gemm(): + + batch_count = 56 + + M = TILE_M + N = TILE_N + K = TILE_K -# wp.tile_store(sum) + rng = np.random.default_rng(42) + A = rng.random((batch_count, M, K), dtype=np.float32) + B = rng.random((batch_count, K, N), dtype=np.float32) + C = np.zeros((batch_count, M, N), dtype=np.float32) + + A_wp = wp.array(A) + B_wp = wp.array(B) + C_wp = wp.array(C) + wp.launch(tile_grouped_gemm, dim=batch_count, inputs=[A_wp, B_wp, C_wp], tile_size=8) + + # bring back to host + C_wp = C_wp.numpy() + + for i in range(batch_count): + assert(np.allclose(A[i]@B[i], C_wp[i], rtol=1.e-4)) + + # GEMM forward passed + print("batched matmul forward passed") @wp.kernel @@ -190,7 +264,7 @@ def tile_gemm(A: wp.array2d(dtype=float), N = B.shape[1] K = A.shape[1] - count = int(K / TILE_K) # todo: must be the same as TILE_K + count = int(K / TILE_K) for k in range(0, count): @@ -223,11 +297,13 @@ def test_tile_gemm(): assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) # GEMM forward passed - print("Binary map backward passed") + print("matmul forward passed") test_tile_copy() test_tile_unary_map() test_tile_binary_map() -test_tile_gemm() \ No newline at end of file +test_tile_batched_gemm() +test_tile_gemm() +test_tile_operators() \ No newline at end of file diff --git a/warp/types.py b/warp/types.py index b5f02dba..1074b3df 100644 --- a/warp/types.py +++ b/warp/types.py @@ -1405,7 +1405,10 @@ def types_equal(a, b, match_generic=False): if is_array(a) and type(a) is type(b): return True - + + if is_tile(a) and is_tile(b): + return True + return scalars_equal(a, b, match_generic) @@ -2863,7 +2866,7 @@ def array_type_id(a): # tile expression objects class Tile: - def __init__(self, dtype, M, N, op): + def __init__(self, dtype, M, N, op=None): self.dtype = dtype self.M = M self.N = N @@ -2878,6 +2881,16 @@ def ctype(self): from warp.codegen import Var return f"wp::tile_zeros_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" +class TileConstant(Tile): + + def __init__(self, dtype, M, N): + Tile.__init__(self, dtype, M, N, "zeros") + + def ctype(self): + from warp.codegen import Var + return f"wp::tile_constant_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + + class TileLoad(Tile): def __init__(self, array, M, N): From 04fd859fd0a8036ad0cb612981bf7fccb101fcae Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 3 Sep 2024 15:23:46 +1200 Subject: [PATCH 019/102] Working implementation of register based tiles for most tests, added support for compile time block dimensions for tile kernels --- warp/builtins.py | 112 +++--- warp/codegen.py | 17 +- warp/context.py | 23 +- warp/native/tile.h | 749 +++++++++++----------------------------- warp/native/tile_gemm.h | 12 +- warp/tape.py | 10 +- warp/tests/test_tile.py | 20 +- warp/types.py | 57 ++- 8 files changed, 335 insertions(+), 665 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index fc3438cb..abe2d7b5 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1729,10 +1729,9 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar template_args.append(m.constant) template_args.append(n.constant) - # global shared_memory_id - # template_args.append(shared_memory_id) - - # shared_memory_id += 1 + global shared_memory_id + template_args.append(shared_memory_id) + shared_memory_id += 1 return ([], template_args) @@ -1791,9 +1790,9 @@ def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg template_args.append(m) template_args.append(n) - #global shared_memory_id - #templates.append(shared_memory_id) - #shared_memory_id += 1 + global shared_memory_id + template_args.append(shared_memory_id) + shared_memory_id += 1 return ((array, x, y), template_args) @@ -1861,8 +1860,12 @@ def tile_matmul_value_func(arg_types, arg_values): if not is_tile(arg_types["b"]): raise RuntimeError("tile_matmul() argument 1 must be an tile") - if not isinstance(arg_types["out"], TileShared): - raise RuntimeError("tile_matmul() output must be a fully evaluated tile, e.g.: created using tile_eval()") + if not isinstance(arg_types["out"], Tile): + raise RuntimeError("tile_matmul() output argument must be a tile") + + if arg_types["out"].storage != "shared": + raise RuntimeError("tile_matmul() output argument must have shared memory storage") + return None @@ -1876,14 +1879,14 @@ def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a # template_args.append(m) # template_args.append(n) - global shared_memory_id + # global shared_memory_id template_args = [] - template_args.append(shared_memory_id) + # template_args.append(shared_memory_id) - # matmul makes two allocations (one for each of its arguments) - shared_memory_id += 1 - shared_memory_id += 1 + # # matmul makes two allocations (one for each of its arguments) + # shared_memory_id += 1 + # shared_memory_id += 1 return ((a, b, out), template_args) @@ -1937,38 +1940,18 @@ def tile_eval_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg # does type propagation for load() -def tile_map_value_func(arg_types, arg_values): +def tile_unary_map_value_func(arg_types, arg_values): if arg_types is None: return None - tiles = arg_types["args"] + a = arg_types["a"] # check all args are tiles - for a in tiles: - if not is_tile(a): - raise RuntimeError(f"tile_map() arguments must be tiles, got type {a}") - - # use first argument to define output type - first = tiles[0] + if not is_tile(a): + raise RuntimeError(f"tile_map() arguments must be tiles, got type {a}") - # check all args have the same type and dimension - for a in tiles: - if a.dtype != first.dtype: - raise RuntimeError(f"tile_map() arguments must all have the same type {first.dtype} != {a.dtype}") - - if a.M != first.M: - raise RuntimeError(f"tile_map() arguments must all have the same m dimension {first.M} != {a.M}") - - if a.N != first.N: - raise RuntimeError(f"tile_map() arguments must all have the same n dimension {first.N} != {a.N}") - - if len(tiles) == 1: - return TileUnaryMap(tiles[0]) - elif len(tiles) == 2: - return TileBinaryMap(tiles[0], tiles[1]) - else: - raise RuntimeError(f"tile_map() must have or two tile arguments") + return TileUnaryMap(a) def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, args: Mapping[str, Var]): @@ -1979,10 +1962,51 @@ def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, ar add_builtin( "tile_map", - input_types={"op": Callable, "*args": Any}, - value_func=tile_map_value_func, - dispatch_func=tile_map_dispatch_func, - variadic=True, + input_types={"op": Callable, "a": Any}, + value_func=tile_unary_map_value_func, + #dispatch_func=tile_map_dispatch_func, + #variadic=True, + native_func="tile_unary_map", + doc="Map the operation onto each element of the tile", + group="Tile Primitives", + export=False, +) + +def tile_binary_map_value_func(arg_types, arg_values): + + if arg_types is None: + return None + + a = arg_types["a"] + b = arg_types["b"] + + # check all args are tiles + if not is_tile(a): + raise RuntimeError(f"tile_map() arguments must be tiles, got type {a}") + + if not is_tile(b): + raise RuntimeError(f"tile_map() arguments must be tiles, got type {b}") + + # use first argument to define output type + if a.dtype != b.dtype: + raise RuntimeError(f"tile_map() arguments must all have the same type {a.dtype} != {b.dtype}") + + if a.M != b.M: + raise RuntimeError(f"tile_map() arguments must all have the same m dimension {a.M} != {b.M}") + + if a.N != b.N: + raise RuntimeError(f"tile_map() arguments must all have the same n dimension {a.N} != {b.N}") + + return TileBinaryMap(a, b) + + +add_builtin( + "tile_map", + input_types={"op": Callable, "a": Any, "b": Any}, + value_func=tile_binary_map_value_func, + #dispatch_func=tile_map_dispatch_func, + #variadic=True, + native_func="tile_binary_map", doc="Map the operation onto each element of the tile", group="Tile Primitives", export=False, @@ -4464,7 +4488,7 @@ def tile_scalar_mul_value_func(arg_types, arg_values): x = arg_types["x"] y = arg_types["y"] - + # tile*scalar if is_tile(x): if x.dtype != y: diff --git a/warp/codegen.py b/warp/codegen.py index 6a9991af..9a38d7c1 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -942,9 +942,10 @@ def format_args(adj, prefix, args): if isinstance(a, warp.context.Function): # functions don't have a var_ prefix so strip it off here if prefix == "var": - arg_strs.append(a.key) + arg_strs.append(f"{a.namespace}{a.key}") else: - arg_strs.append(f"{prefix}_{a.key}") + arg_strs.append(f"{a.namespace}{prefix}_{a.key}") + elif is_reference(a.type): arg_strs.append(f"{prefix}_{a}") elif isinstance(a, Var): @@ -2602,6 +2603,7 @@ def get_constant_references(adj) -> Dict[str, Any]: # code generation cpu_module_header = """ +#define WP_TILE_BLOCK_DIM {tile_size} #define WP_NO_CRT #include "builtin.h" @@ -2620,6 +2622,7 @@ def get_constant_references(adj) -> Dict[str, Any]: """ cuda_module_header = """ +#define WP_TILE_BLOCK_DIM {tile_size} #define WP_NO_CRT #include "builtin.h" @@ -3013,10 +3016,6 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): for var in adj.variables: - # do not predeclare vars with auto type - if var.ctype() == "auto": - continue - if var.constant is None: lines += [f"{var.ctype()} {var.emit()};\n"] else: @@ -3029,8 +3028,10 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): for var in adj.variables: name = var.emit_adj() ctype = var.ctype(value_type=True) - - if ctype != "auto": + + if is_tile(var.type) and var.type.storage == "shared": + lines += [f"{ctype} {name} = wp::tile_alloc_shared<{Var.type_to_ctype(var.type.dtype)},{var.type.M},{var.type.N},{var.type.alloc()}>();\n"] + else: lines += [f"{ctype} {name} = {{}};\n"] # forward pass diff --git a/warp/context.py b/warp/context.py index 1d066f66..95f36afb 100644 --- a/warp/context.py +++ b/warp/context.py @@ -1404,9 +1404,9 @@ def codegen(self, device): # add headers if device == "cpu": - source = warp.codegen.cpu_module_header + source + source = warp.codegen.cpu_module_header.format(tile_size=self.options["tile_size"]) + source else: - source = warp.codegen.cuda_module_header + source + source = warp.codegen.cuda_module_header.format(tile_size=self.options["tile_size"]) + source return source @@ -1439,6 +1439,7 @@ def __init__(self, name, loader): "fast_math": False, "cuda_output": None, # supported values: "ptx", "cubin", or None (automatic) "mode": warp.config.mode, + "tile_size": 0 } # kernel hook lookup per device @@ -1682,11 +1683,18 @@ def hash_recursive(module, visited): return hash_recursive(self, visited=set()) - def load(self, device) -> bool: + def load(self, device, tile_size=0) -> bool: from warp.utils import ScopedTimer device = get_device(device) + # re-compile module if tile size (blockdim) changes + # todo: it would be better to have a method such as `module.get_kernel(tile_size=N)` + # that can return a single kernel instance with a given block size + if self.options["tile_size"] != tile_size: + self.unload() + self.options["tile_size"] = tile_size + if device.is_cpu: # check if already loaded if self.cpu_module: @@ -1695,7 +1703,7 @@ def load(self, device) -> bool: if self.cpu_build_failed: return False if not warp.is_cpu_available(): - raise RuntimeError("Failed to build CPU module because no CPU buildchain was found") + raise RuntimeError("Failed to build CPU module because no CPU build chain was found") else: # check if already loaded if device.context in self.cuda_modules: @@ -4630,7 +4638,7 @@ def launch( record_tape=True, record_cmd=False, max_blocks=0, - tile_size=1, + tile_size=0, ): """Launch a Warp kernel on the target device @@ -4650,6 +4658,7 @@ def launch( record_cmd: When True the launch will be returned as a ``Launch`` command object, the launch will not occur until the user calls ``cmd.launch()`` max_blocks: The maximum number of CUDA thread blocks to use. Only has an effect for CUDA kernel launches. If negative or zero, the maximum hardware value will be used. + tile_size: The number of threads per-program instance """ init() @@ -4704,7 +4713,7 @@ def pack_args(args, params, adjoint=False): # delay load modules, including new overload if needed module = kernel.module - if not module.load(device): + if not module.load(device, tile_size): return # late bind @@ -4788,7 +4797,7 @@ def pack_args(args, params, adjoint=False): # record file, lineno, func as metadata frame = inspect.currentframe().f_back caller = {"file": frame.f_code.co_filename, "lineno": frame.f_lineno, "func": frame.f_code.co_name} - runtime.tape.record_launch(kernel, dim, max_blocks, inputs, outputs, device, metadata={"caller": caller}) + runtime.tape.record_launch(kernel, dim, max_blocks, inputs, outputs, device, tile_size, metadata={"caller": caller}) # detect illegal inter-kernel read/write access patterns if verification flag is set if warp.config.verify_autograd_array_access: diff --git a/warp/native/tile.h b/warp/native/tile.h index 009709a2..4315eda7 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -27,18 +27,21 @@ #endif + + /* Tile Expressions [x] Forward / Backward code-gen [ ] wp.tile_map() - [x] Support user functions - [ ] Support built-in functions + [x] Support user functions + [x] Support built-in functions [ ] Support for lambda functions + [ ] Infer tile_map() output from operator type (e.g.: dot for each element) [ ] wp.tile_matmul() [x] Forward [ ] Reverse [ ] Support for n-d shape tiles / broadcasting / slicing / transpose? -[ ] Compile-time block dimensions +[x] Compile-time block dimensions [ ] Support for CUB reductions [ ] Support for CUB sorts [ ] Examples @@ -46,7 +49,10 @@ [ ] Batched MLP [ ] Point cloud alignment [ ] Layer norm - +[ ] Error checking + [ ] Ensure functions passed to tile_map() are compatible with tile type + [ ] Ensure that args passed to tile ops are compatible + */ // wp.tile_load(A, offset, shape) @@ -85,7 +91,7 @@ void print_tile(T& t) printf("%*s[", i>0, ""); for (int j=0; j < T::N; ++j) { - printf("%5.2f ", t.fwd(i*T::N + j)); + printf("%5.2f ", t.data[i*T::N + j]); } if (i == T::M-1) @@ -95,656 +101,293 @@ void print_tile(T& t) } } - template -int size(Tile& t) { return Tile::M*Tile::N; } +int tile_size(Tile& t) { return Tile::M*Tile::N; } +constexpr int tile_regcount(int m, int n) { + return (m*n + WP_TILE_BLOCK_DIM - 1) / WP_TILE_BLOCK_DIM; +} -template -struct tile_load_t +struct coord_t { - using Type = T; - static constexpr int M = M_; - static constexpr int N = N_; - - array_t slice; - - tile_load_t() {} - tile_load_t(array_t& src, int x, int y) - { - assert(src.ndim == 2); - - // compute offsets into original array and store a view - const int i = x*M; - const int j = y*N; - - // slice into src - if (src.data) - slice.data = data_at_byte_offset(src, byte_offset(src, i, j)); - if (src.grad) - slice.grad = grad_at_byte_offset(src, byte_offset(src, i, j)); - - slice.shape[0] = M; - slice.shape[1] = N; - slice.strides[0] = src.strides[0]; - slice.strides[1] = src.strides[1]; - slice.ndim = 2; - } - - Type fwd(int e) const - { - int i = e/N; - int j = e%N; - - return index(slice, i, j); - } - - void bwd(int e, const T& adj_ret) const - { - int i = e/N; - int j = e%N; - - if (slice.grad) - atomic_add(&index_grad(slice, i, j), adj_ret); - } - - void print() - { - printf("tile_load_t<%d, %d>\n", M, N); - } - + int i; + int j; }; -template -struct tile_store_t -{ - using Tile = Tile_; - using Type = typename Tile_::Type; - static constexpr int M = Tile_::M; - static constexpr int N = Tile_::N; - - array_t slice; - Tile tile; - - tile_store_t() {} - tile_store_t(array_t& dest, int x, int y, Tile& t) : tile(t) - { - assert(dest.ndim == 2); - - // compute offsets into original array and store a view - const int i = x*M; - const int j = y*N; - - // slice into dest - if (dest.data) - slice.data = data_at_byte_offset(dest, byte_offset(dest, i, j)); - if (dest.grad) - slice.grad = grad_at_byte_offset(dest, byte_offset(dest, i, j)); - - slice.shape[0] = M; - slice.shape[1] = N; - slice.strides[0] = dest.strides[0]; - slice.strides[1] = dest.strides[1]; - slice.ndim = 2; - } - - void fwd(int e) const - { - int i = e/N; - int j = e%N; - - index(slice, i, j) = tile.fwd(e); - } - - void bwd(int e) const - { - int i = e/N; - int j = e%N; - // materialize gradient (runs entire graph backward), reading incoming grads from the dest - if (slice.grad) - tile.bwd(e, index_grad(slice, i, j)); - } +template +inline CUDA_CALLABLE T* tile_alloc_shared() +{ + WP_TILE_SHARED __align__(16) T data[M*N]; - void print() - { - printf("tile_load_t<%d, %d>-+", M, N); - print(tile); - } -}; + for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) + data[i] = T(0); + return data; +} template -struct tile_constant_t +struct tile_shared_t { using Type = T; static constexpr int M = M_; static constexpr int N = N_; - T c; - T* adj_c; - - tile_constant_t() {} - tile_constant_t(const T& c, T& adj_c) : c(c), adj_c(&adj_c) {} + T* data = NULL; - Type fwd(int e) const + tile_shared_t() {} + tile_shared_t(T* smem) : data(smem) { - return c; } - void bwd(int e, const T& adj_ret) const + struct iterator { - *adj_c += adj_ret; - } + tile_shared_t& tile; + int offset; + + inline CUDA_CALLABLE iterator(tile_shared_t& t, int i) : tile(t), offset(i) {} + inline CUDA_CALLABLE T& operator*() const { return tile.data[offset]; } + inline CUDA_CALLABLE iterator& operator++() { offset += WP_TILE_BLOCK_DIM; return *this; } + inline CUDA_CALLABLE bool valid() const { return index() < tile_size(tile); } + + // linear index into the tile's data (assuming row-major layout) + inline CUDA_CALLABLE int index() const { return offset; } + inline CUDA_CALLABLE coord_t coord() const + { + int i = index(); + return {i/N, i%N}; + } + }; - void print() - { - printf("tile_constant_t<%d, %d>-+", M, N); - print(c); - printf("\n"); - } + iterator iter() { return iterator(*this, threadIdx.x); } }; -template -struct tile_zeros_t -{ - using Type = T; - static constexpr int M = M_; - static constexpr int N = N_; - - tile_zeros_t() {} - - Type fwd(int e) const - { - return Type(0.0); - } - - void bwd(int e, const T& adj_ret) const {} - - void print() - { - printf("tile_zeros_t<%d, %d>-+", M, N); - print(c); - printf("\n"); - } -}; template -struct tile_ones_t +struct tile_register_t { using Type = T; static constexpr int M = M_; static constexpr int N = N_; + static constexpr int NumRegs = tile_regcount(M, N); - tile_ones_t() {} - - Type fwd(int e) - { - return Type(1.0); - } - - void bwd(int e, const T& adj_ret) {} - - void print() - { - printf("tile_ones_t<%d, %d>-+", M, N); - print(c); - printf("\n"); - } -}; - -template -struct tile_unary_map_t -{ - using Type = typename Tile::Type; - static constexpr int M = Tile::M; - static constexpr int N = Tile::N; - - using FwdOp = Type(*)(Type); - using AdjOp = void(*)(Type, Type&, Type&); - - Tile tile; - - FwdOp fwd_fn; - AdjOp adj_fn; - - tile_unary_map_t() {} - tile_unary_map_t(Tile& t, FwdOp fwd, AdjOp adj) : tile(t), fwd_fn(fwd), adj_fn(adj) {} - - Type fwd(int e) const - { - return fwd_fn(tile.fwd(e)); - } - - void bwd(int e, Type adj_ret) const - { - Type adj_a = 0.0; - - adj_fn(tile.fwd(e), adj_a, adj_ret); - - tile.bwd(e, adj_a); - } - - void print() - { - printf("tile_unary_map_t<%d, %d>-+", M, N); - tile.print(); - } -}; - -template -struct tile_binary_map_t -{ - static_assert(wp::is_same::value, "Error"); - static_assert(TileA::M == TileB::M, "Error"); - static_assert(TileA::N == TileB::N, "Error"); - - using Type = typename TileA::Type; - static constexpr int M = TileA::M; - static constexpr int N = TileA::N; - - using FwdOp = Type(*)(Type, Type); - using AdjOp = void(*)(Type, Type, Type&, Type&, Type&); - - TileA tile_a; - TileB tile_b; - - FwdOp fwd_fn; - AdjOp adj_fn; - - tile_binary_map_t() {} - tile_binary_map_t(const TileA& a, TileB& b, FwdOp fwd, AdjOp adj) : tile_a(a), tile_b(b), fwd_fn(fwd), adj_fn(adj) {} - - Type fwd(int e) const + T data[NumRegs]; + + tile_register_t() { - Type a = tile_a.fwd(e); - Type b = tile_b.fwd(e); - - return fwd_fn(a, b); + // zero-initialize by default + // necessary for tile adjoints + // need to check if this results in worse codegen + for (int i=0; i < NumRegs; ++i) + data[i] = T(0); } - void bwd(int e, Type adj_ret) const + struct iterator { - Type a = tile_a.fwd(e); - Type b = tile_b.fwd(e); - - Type adj_a = 0.0; - Type adj_b = 0.0; + tile_register_t& tile; + int offset; + + inline CUDA_CALLABLE iterator(tile_register_t& t, int i) : tile(t), offset(i) {} - adj_fn(a, b, adj_a, adj_b, adj_ret); + inline CUDA_CALLABLE T& operator*() const { return tile.data[offset]; } + inline CUDA_CALLABLE iterator& operator++() { ++offset; return *this; } + inline CUDA_CALLABLE bool valid() const { return offset < NumRegs && index() < tile_size(tile); } - // recurse - tile_a.bwd(e, adj_a); - tile_b.bwd(e, adj_b); - } + // linear index into the tile's data (assuming row-major layout) + inline CUDA_CALLABLE int index() const { return threadIdx.x + offset*WP_TILE_BLOCK_DIM; } + inline CUDA_CALLABLE coord_t coord() const + { + int i = index(); + return {i/N, i%N}; + } + }; - void print() - { - printf("tile_binary_map_t<%d, %d>", M, N); - printf("\n -+"); - tile_a.print(); - printf("\n -+"); - tile_b.print(); - } + iterator iter() { return iterator(*this, 0); } }; -//----------------------------------------------- -// Operators -template -CUDA_CALLABLE inline tile_unary_map_t tile_pos(const Tile& t) -{ - return tile_unary_map_t(t, [](typename Tile::Type x) { return pos(x); } ); -} - -template -CUDA_CALLABLE inline tile_unary_map_t tile_neg(Tile& t) -{ - typedef tile_unary_map_t Op; - - typename Op::FwdOp fwd = [](typename Tile::Type x) { return neg(x); }; - typename Op::AdjOp adj = [](typename Tile::Type x, typename Tile::Type& adj_x, typename Tile::Type& adj_ret) { adj_neg(x, adj_x, adj_ret); }; - - return Op(t, fwd, adj); -} +//----------------------------------------------------------------------------------------------------- +// High level entry points for each op (correspond to one Warp builtin) -template -CUDA_CALLABLE inline void adj_tile_neg(const Tile& t, Tile& adj_t, tile_unary_map_t& adj_ret) +template +inline CUDA_CALLABLE auto tile_zeros() { - // nop -} - + const int length = M*N; -/* + WP_TILE_SHARED __align__(16) T data[length]; + + WP_PRAGMA_UNROLL + for (int t=threadIdx.x; t < length; t += WP_TILE_BLOCK_DIM) + { + data[t] = T(0.0); + } -template -CUDA_CALLABLE inline vec_t neg(const vec_t& x) -{ - return -x; + return tile_shared_t(data); } -template -CUDA_CALLABLE inline vec_t<3, Type> neg(const vec_t<3, Type>& x) -{ - return vec_t<3, Type>(-x.c[0], -x.c[1], -x.c[2]); -} -template -CUDA_CALLABLE inline vec_t<2, Type> neg(const vec_t<2, Type>& x) +// entry point for store +template +inline CUDA_CALLABLE auto tile_load(array_t& src, int x, int y) { - return vec_t<2, Type>(-x.c[0], -x.c[1]); -} + const int length = M*N; -template -CUDA_CALLABLE inline void adj_neg(const vec_t& x, vec_t& adj_x, const vec_t& adj_ret) -{ - adj_x -= adj_ret; -} + WP_TILE_SHARED __align__(16) T data[length]; -// equality: -template -inline CUDA_CALLABLE bool operator ==(const vec_t& a, const vec_t& b) -{ - for( unsigned i=0; i < Length; ++i ) - { - if(a[i] != b[i]) - { - return false; - } - } - return true; -} + tile_shared_t dest(data); + + WP_PRAGMA_UNROLL + for (auto dst_iter=dest.iter(); dst_iter.valid(); ++dst_iter) + { + coord_t c = dst_iter.coord(); -// scalar multiplication: -template -inline CUDA_CALLABLE vec_t mul(vec_t a, Type s) -{ - vec_t ret; - for( unsigned i=0; i < Length; ++i ) - { - ret[i] = a[i] * s; + *dst_iter = index(src, x*M + c.i, y*N + c.j); } - return ret; -} - -template -inline CUDA_CALLABLE vec_t<3, Type> mul(vec_t<3, Type> a, Type s) -{ - return vec_t<3, Type>(a.c[0]*s,a.c[1]*s,a.c[2]*s); -} -template -inline CUDA_CALLABLE vec_t<2, Type> mul(vec_t<2, Type> a, Type s) -{ - return vec_t<2, Type>(a.c[0]*s,a.c[1]*s); + return dest; } -template -inline CUDA_CALLABLE vec_t mul(Type s, vec_t a) -{ - return mul(a, s); -} - -template -inline CUDA_CALLABLE vec_t operator*(Type s, vec_t a) +// entry point for store +template +inline CUDA_CALLABLE void tile_store(array_t& dest, int x, int y, Tile& src) { - return mul(a, s); -} + const int M = src.M; + const int N = src.N; + + // cooperatively store the tile, using a block-stride iterator + WP_PRAGMA_UNROLL + for (auto src_iter=src.iter(); src_iter.valid(); ++src_iter) + { + coord_t c = src_iter.coord(); -template -inline CUDA_CALLABLE vec_t operator*(vec_t a, Type s) -{ - return mul(a, s); + index(dest, x*M + c.i, y*N + c.j) = *src_iter; + } } +//------------------------------------- +// Adjoints -// component wise multiplication: -template -inline CUDA_CALLABLE vec_t cw_mul(vec_t a, vec_t b) +template +inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, + array_t& adj_src, int adj_x, int adj_y, + AdjTile& adj_ret) { - vec_t ret; - for( unsigned i=0; i < Length; ++i ) - { - ret[i] = a[i] * b[i]; + // add gradients to src array + WP_PRAGMA_UNROLL + for (auto adj_iter=adj_ret.iter(); adj_iter.valid(); ++adj_iter) + { + coord_t c = adj_iter.coord(); + atomic_add(adj_src, x*adj_ret.M + c.i, y*adj_ret.N + c.j, *adj_iter); } - return ret; } -// division -template -inline CUDA_CALLABLE vec_t div(vec_t a, Type s) +template +inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, AdjTile& adj_t) { - vec_t ret; - for( unsigned i=0; i < Length; ++i ) - { - ret[i] = a[i] / s; + const int M = t.M; + const int N = t.N; + + // load gradients from output + WP_PRAGMA_UNROLL + for (auto adj_iter=adj_t.iter(); adj_iter.valid(); ++adj_iter) + { + coord_t c = adj_iter.coord(); + *adj_iter += index(adj_dest, x*M + c.i, y*N + c.j, *adj_iter); } - return ret; } -template -inline CUDA_CALLABLE vec_t<3, Type> div(vec_t<3, Type> a, Type s) +// unary map +template +auto tile_map(Fwd op, + Tile &a) { - return vec_t<3, Type>(a.c[0]/s,a.c[1]/s,a.c[2]/s); -} + auto out = tile_register_t(); -template -inline CUDA_CALLABLE vec_t<2, Type> div(vec_t<2, Type> a, Type s) -{ - return vec_t<2, Type>(a.c[0]/s,a.c[1]/s); -} + auto out_iter = out.iter(); + auto a_iter = a.iter(); -template -inline CUDA_CALLABLE vec_t div(Type s, vec_t a) -{ - vec_t ret; - for (unsigned i=0; i < Length; ++i) + for (; out_iter.valid(); ++out_iter, ++a_iter) { - ret[i] = s / a[i]; + *out_iter = op(*a_iter); } - return ret; -} - -template -inline CUDA_CALLABLE vec_t<3, Type> div(Type s, vec_t<3, Type> a) -{ - return vec_t<3, Type>(s/a.c[0],s/a.c[1],s/a.c[2]); -} -template -inline CUDA_CALLABLE vec_t<2, Type> div(Type s, vec_t<2, Type> a) -{ - return vec_t<2, Type>(s/a.c[0],s/a.c[1]); + return out; } -template -inline CUDA_CALLABLE vec_t operator / (vec_t a, Type s) +template +void adj_tile_map(Fwd op, + Tile &a, + Adj adj_op, + Tile &adj_a, + AdjTile &adj_ret) { - return div(a,s); -} + auto a_iter = a.iter(); + auto adj_a_iter = adj_a.iter(); + auto adj_ret_iter = adj_ret.iter(); -template -inline CUDA_CALLABLE vec_t operator / (Type s, vec_t a) -{ - return div(s, a); -} - -// component wise division -template -inline CUDA_CALLABLE vec_t cw_div(vec_t a, vec_t b) -{ - vec_t ret; - for( unsigned i=0; i < Length; ++i ) + for (; a_iter.valid(); ++a_iter, ++adj_a_iter, ++adj_ret_iter) { - ret[i] = a[i] / b[i]; + adj_op(*a_iter, *adj_a_iter, *adj_ret_iter); } - return ret; } -// addition -template -inline CUDA_CALLABLE vec_t add(vec_t a, vec_t b) +// binary map +template +auto tile_map(Fwd op, + TileA &a, + TileB &b) { - vec_t ret; - for( unsigned i=0; i < Length; ++i ) - { - ret[i] = a[i] + b[i]; - } - return ret; -} + auto out = tile_register_t(); -template -inline CUDA_CALLABLE vec_t<2, Type> add(vec_t<2, Type> a, vec_t<2, Type> b) -{ - return vec_t<2, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1]); -} + auto out_iter = out.iter(); + auto a_iter = a.iter(); + auto b_iter = b.iter(); -template -inline CUDA_CALLABLE vec_t<3, Type> add(vec_t<3, Type> a, vec_t<3, Type> b) -{ - return vec_t<3, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1], a.c[2] + b.c[2]); -} - -// subtraction -template -inline CUDA_CALLABLE vec_t sub(vec_t a, vec_t b) -{ - vec_t ret; - for( unsigned i=0; i < Length; ++i ) + for (; out_iter.valid(); ++out_iter, ++a_iter, ++b_iter) { - ret[i] = Type(a[i] - b[i]); + *out_iter = op(*a_iter, *b_iter); } - return ret; -} -template -inline CUDA_CALLABLE vec_t<2, Type> sub(vec_t<2, Type> a, vec_t<2, Type> b) -{ - return vec_t<2, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1]); + return out; } -template -inline CUDA_CALLABLE vec_t<3, Type> sub(vec_t<3, Type> a, vec_t<3, Type> b) +template +void adj_tile_map(Fwd op, + TileA &a, + TileB &b, + Adj adj_op, + TileA &adj_a, + TileB &adj_b, + AdjTile &adj_ret) { - return vec_t<3, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1], a.c[2] - b.c[2]); -} -*/ - - -// represents a fully evaluated tile in shared memory -template -struct tile_shared_t -{ - using Type = T; - static constexpr int M = M_; - static constexpr int N = N_; - - T* data = NULL; - - tile_shared_t() {} - tile_shared_t(T* smem) : data(smem) - { - } + auto a_iter = a.iter(); + auto b_iter = b.iter(); + auto adj_a_iter = adj_a.iter(); + auto adj_b_iter = adj_b.iter(); + auto adj_ret_iter = adj_ret.iter(); - T fwd(int e) const + for (; a_iter.valid(); ++a_iter, ++b_iter, ++adj_a_iter, ++adj_b_iter, ++adj_ret_iter) { - return data[e]; + adj_op(*a_iter, *b_iter, *adj_a_iter, *adj_b_iter, *adj_ret_iter); } - - void bwd(int e, T adj_ret) const - { - - } -}; - -//----------------------------------------------------------------------------------------------------- -// High level entry points for each op (correspond to one Warp builtin) - -template -tile_zeros_t tile_zeros() { return tile_zeros_t(); } - -template -tile_ones_t tile_ones() { return tile_ones_t(); } - -// entry point for load -template -tile_load_t tile_load(array_t& a, int x, int y) -{ - return tile_load_t(a, x, y); } -template -tile_shared_t tile_eval(Tile& t) -{ - WP_TILE_SHARED typename Tile::Type data[Tile::M*Tile::N]; - - // evaluate the input tile and store into shared memory - for (int i=threadIdx.x; i < size(t); i += blockDim.x) - data[i] = t.fwd(i); +// wrap the operator in a lambda so that we don't have to do overload resolution for things like e.g.: wp.sin() +// this is important because many of the builtin operators don't follow particular conventions on references for +// the `adj_ret` parameter, which means it's not possible to figure out the overload we need using simple casting +#define tile_unary_map(op, a) tile_map([](auto x) { return op(x);}, a) +#define adj_tile_unary_map(op, a, adj_op, adj_a, adj_ret) adj_tile_map([](auto x) { return op(x);}, a, [](auto x, auto& adj_x, auto adj_ret) { adj_op(x, adj_x, adj_ret);}, adj_a, adj_ret) - return tile_shared_t(data); -} +#define tile_binary_map(op, a, b) tile_map([](auto x, auto y) { return op(x, y);}, a, b) +#define adj_tile_binary_map(op, a, b, adj_op, adj_a, adj_b, adj_ret) adj_tile_map([](auto x, auto y) { return op(x, y);}, a, b, [](auto x, auto y, auto& adj_x, auto& adj_y, auto adj_ret) { adj_op(x, y, adj_x, adj_y, adj_ret);}, adj_a, adj_b, adj_ret) +// unary neg template -void adj_tile_eval(Tile& t, Tile& adj_t, tile_shared_t& adj_ret) -{ - // nop -} - -template -void adj_tile_load(array_t& a, int x, int y, array_t& adj_a, int adj_x, int adj_y, const tile_load_t& adj_ret) -{ - // nop -} - - -// entry point for store -template -void tile_store(array_t& dest, int x, int y, Tile& t) -{ - tile_store_t op(dest, x, y, t); - - // execute op - for (int i=threadIdx.x; i < size(op); i += blockDim.x) - op.fwd(i); -} - - -template -void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, Tile& adj_t) -{ - tile_store_t op(dest, x, y, t); - - for (int i=threadIdx.x; i < size(op); i += blockDim.x) - op.bwd(i); -} - - -// unary map -template -tile_unary_map_t tile_map_impl(typename tile_unary_map_t::FwdOp fwd, typename tile_unary_map_t::AdjOp adj, Tile& a) -{ - return tile_unary_map_t(a, fwd, adj); -} - -// binary map -template -tile_binary_map_t tile_map_impl(typename tile_binary_map_t::FwdOp fwd, typename tile_binary_map_t::AdjOp adj, TileA& a, TileB& b) -{ - return tile_binary_map_t(a, b, fwd, adj); -} - -// use macro to capture adjoint operator -#define tile_map(op, ...) tile_map_impl(op, adj_##op, __VA_ARGS__) -//#define tile_map(op, a) tile_map_impl(wp::##op, wp::##op, a) - -// nop -void adj_tile_map_impl(void) {} -#define adj_tile_map(...) adj_tile_map_impl() +auto tile_neg(Tile& a) { return tile_unary_map(wp::neg, a); } -// use a macro to capture the adjoint var in the expression -#define tile_constant(T, M, N, var) tile_constant_t(var, adj_##var) +template +void adj_tile_neg(Tile& a, Tile& adj_a, AdjTile& adj_ret) { adj_tile_unary_map(wp::neg, a, wp::adj_neg, adj_a, adj_ret); } /* diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index b1d3435e..fca527d0 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -221,7 +221,7 @@ inline CUDA_CALLABLE void tile_matmul_scalar(const TileA& A, const TileB& B, const TileC& out) { - const int length = size(out); + const int length = tile_size(out); WP_TILE_SYNC(); @@ -353,19 +353,15 @@ struct tile_matmul_t }; -template +template void tile_matmul(TileA& a, TileB& b, TileC& c) { static_assert(wp::is_same::value, "Error, tile datatypes must match"); static_assert(TileA::N == TileB::M, "Error, inner dimensions must match"); static_assert(TileC::M == TileA::M, "Error, first output dimension must match"); static_assert(TileC::N == TileB::N, "Error, second output dimension must match"); - - // load inputs to shared - auto a_shared = tile_eval(a); - auto b_shared = tile_eval(b); - - tile_matmul_scalar(a_shared, b_shared, c); + + tile_matmul_scalar(a, b, c); } diff --git a/warp/tape.py b/warp/tape.py index 8c3cc103..15aebf81 100644 --- a/warp/tape.py +++ b/warp/tape.py @@ -129,7 +129,8 @@ def backward(self, loss: wp.array = None, grads: dict = None): inputs = launch[3] outputs = launch[4] device = launch[5] - + tile_size = launch[6] + adj_inputs = [] adj_outputs = [] @@ -151,13 +152,14 @@ def backward(self, loss: wp.array = None, grads: dict = None): device=device, adjoint=True, max_blocks=max_blocks, + tile_size=tile_size ) # record a kernel launch on the tape - def record_launch(self, kernel, dim, max_blocks, inputs, outputs, device, metadata=None): + def record_launch(self, kernel, dim, max_blocks, inputs, outputs, device, tile_size=0, metadata=None): if metadata is None: metadata = {} - self.launches.append([kernel, dim, max_blocks, inputs, outputs, device, metadata]) + self.launches.append([kernel, dim, max_blocks, inputs, outputs, device, tile_size, metadata]) def record_func(self, backward, arrays): """ @@ -612,7 +614,7 @@ def emit_kernel_launch_node( self.array_grad_stats.insert(0, grad_stats) -Launch = namedtuple("Launch", ["id", "kernel", "dim", "max_blocks", "inputs", "outputs", "device", "metadata"]) +Launch = namedtuple("Launch", ["id", "kernel", "dim", "max_blocks", "inputs", "outputs", "device", "tile_size", "metadata"]) RepeatedSequence = namedtuple("RepeatedSequence", ["start", "end", "repetitions"]) diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index e0c34de2..02fc9870 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -9,11 +9,13 @@ wp.set_module_options({"enable_backward": True}) wp.set_device("cuda:0") +wp.config.verify_cuda = True wp.build.clear_kernel_cache() -TILE_M = 8 -TILE_N = 4 +TILE_M = wp.constant(32) +TILE_N = wp.constant(32) +TILE_K = wp.constant(8) @wp.kernel def tile_copy(A: wp.array2d(dtype=float), @@ -66,7 +68,7 @@ def tile_unary_map(input: wp.array2d(dtype=float), a = wp.tile_load(input, i, j, m=TILE_M, n=TILE_N) - sa = wp.tile_map(unary_func, a) + sa = wp.tile_map(wp.sin, a) wp.tile_store(output, i, j, sa) @@ -199,10 +201,6 @@ def test_tile_operators(): -TILE_M = wp.constant(64) -TILE_N = wp.constant(64) -TILE_K = wp.constant(8) - @wp.kernel def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), @@ -214,7 +212,7 @@ def tile_grouped_gemm(A: wp.array3d(dtype=float), a = wp.tile_load(A[i], 0, 0, m=TILE_M, n=TILE_K) b = wp.tile_load(B[i], 0, 0, m=TILE_K, n=TILE_N) - sum = wp.tile_eval(wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32)) + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) wp.tile_matmul(a, b, sum) @@ -258,7 +256,7 @@ def tile_gemm(A: wp.array2d(dtype=float), # output tile index i, j = wp.tid() - sum = wp.tile_eval(wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32)) + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) M = A.shape[0] N = B.shape[1] @@ -304,6 +302,6 @@ def test_tile_gemm(): test_tile_copy() test_tile_unary_map() test_tile_binary_map() -test_tile_batched_gemm() -test_tile_gemm() +# test_tile_batched_gemm() +# test_tile_gemm() test_tile_operators() \ No newline at end of file diff --git a/warp/types.py b/warp/types.py index 1074b3df..9bc6f7d7 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2865,75 +2865,72 @@ def array_type_id(a): # tile expression objects class Tile: - - def __init__(self, dtype, M, N, op=None): + + allocation = 0 + + def __init__(self, dtype, M, N, op=None, storage="register"): self.dtype = dtype self.M = M self.N = N self.op = op + self.storage = storage + + def ctype(self): + from warp.codegen import Var + + if self.storage == "register": + return f"wp::tile_register_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + elif self.storage == "shared": + return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + + # generate a unique allocation index for shared memory + @classmethod + def alloc(cls): + index = cls.allocation + cls.allocation += 1 + return index class TileZeros(Tile): def __init__(self, dtype, M, N): - Tile.__init__(self, dtype, M, N, "zeros") + Tile.__init__(self, dtype, M, N, op="zeros", storage="shared") - def ctype(self): - from warp.codegen import Var - return f"wp::tile_zeros_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" class TileConstant(Tile): def __init__(self, dtype, M, N): - Tile.__init__(self, dtype, M, N, "zeros") + Tile.__init__(self, dtype, M, N, op="constant", storage="register") - def ctype(self): - from warp.codegen import Var - return f"wp::tile_constant_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" - class TileLoad(Tile): def __init__(self, array, M, N): - Tile.__init__(self, array.dtype, M, N, "load") + Tile.__init__(self, array.dtype, M, N, op="load", storage="shared") - def ctype(self): - from warp.codegen import Var - return f"wp::tile_load_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" class TileUnaryMap(Tile): def __init__(self, t): - Tile.__init__(self, t.dtype, t.M, t.N, "unary_map") + Tile.__init__(self, t.dtype, t.M, t.N, op="unary_map", storage="register") self.t = t - - def ctype(self): - from warp.codegen import Var - return f"wp::tile_unary_map_t<{self.t.ctype()}>" + class TileBinaryMap(Tile): def __init__(self, a, b): - Tile.__init__(self, a.dtype, a.M, a.N, "binary_map") + Tile.__init__(self, a.dtype, a.M, a.N, op="binary_map", storage="register") self.a = a self.b = b - - def ctype(self): - from warp.codegen import Var - return f"wp::tile_binary_map_t<{self.a.ctype()}, {self.b.ctype()}>" class TileShared(Tile): def __init__(self, t): - Tile.__init__(self, t.dtype, t.M, t.N, "shared") + Tile.__init__(self, t.dtype, t.M, t.N, "shared", storage="shared") self.t = t - - def ctype(self): - from warp.codegen import Var - return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" def is_tile(t): From 5fce6ced169526fdae66ed8874f1c48a94ee1c02 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 9 Sep 2024 15:17:05 +1200 Subject: [PATCH 020/102] wp.tile_matmul() reverse mode working, added support for strided shared memory tiles --- warp/codegen.py | 5 +- warp/native/tile.h | 112 +++++++++++++++++++++++++++++++++------- warp/native/tile_gemm.h | 62 +++------------------- warp/tests/test_tile.py | 49 ++++++++++-------- 4 files changed, 132 insertions(+), 96 deletions(-) diff --git a/warp/codegen.py b/warp/codegen.py index 9a38d7c1..88c12d8a 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -1424,7 +1424,10 @@ def end_for(adj, iter): # zero adjoints for i in body_block.vars: - reverse.append(adj.indentation + f"\t{i.emit_adj()} = {{}};") + if is_tile(i.type): + reverse.append(adj.indentation + f"\t{i.emit_adj()}.zero();") + else: + reverse.append(adj.indentation + f"\t{i.emit_adj()} = {{}};") # replay for i in body_block.body_replay: diff --git a/warp/native/tile.h b/warp/native/tile.h index 4315eda7..5111d958 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -37,9 +37,10 @@ [x] Support built-in functions [ ] Support for lambda functions [ ] Infer tile_map() output from operator type (e.g.: dot for each element) -[ ] wp.tile_matmul() +[x] wp.tile_matmul() [x] Forward - [ ] Reverse + [x] Reverse +[ ] wp.tile_atomic_add() [ ] Support for n-d shape tiles / broadcasting / slicing / transpose? [x] Compile-time block dimensions [ ] Support for CUB reductions @@ -49,6 +50,17 @@ [ ] Batched MLP [ ] Point cloud alignment [ ] Layer norm + [ ] Convolution: https://github.com/NVIDIA/MinkowskiEngine/blob/master/src/convolution_kernel.cu#L123 + [ ] MeshCNN (Modulus, Oliver) + [ ] BioNemo (Ali) + [ ] Skinning (David/Or/Vismay) + [ ] warp.sim (VBD) + [ ] warp.sim (CRBA) + [ ] Point clustering + [ ] GEMM + [ ] MLP + [ ] LayerNorm + [ ] SoftMax [ ] Error checking [ ] Ensure functions passed to tile_map() are compatible with tile type [ ] Ensure that args passed to tile ops are compatible @@ -126,12 +138,15 @@ inline CUDA_CALLABLE T* tile_alloc_shared() return data; } -template + +template struct tile_shared_t { using Type = T; static constexpr int M = M_; static constexpr int N = N_; + static constexpr int StrideM = StrideM_; + static constexpr int StrideN = StrideN_; T* data = NULL; @@ -140,13 +155,36 @@ struct tile_shared_t { } + inline T& operator()(int i, int j) + { + assert(i < M); + assert(j < N); + + return data[i*StrideM + j*StrideN]; + } + + inline const T& operator()(int i, int j) const + { + assert(i < M); + assert(j < N); + + return data[i*StrideM + j*StrideN]; + } + struct iterator { - tile_shared_t& tile; + tile_shared_t& tile; int offset; - inline CUDA_CALLABLE iterator(tile_shared_t& t, int i) : tile(t), offset(i) {} - inline CUDA_CALLABLE T& operator*() const { return tile.data[offset]; } + template + inline CUDA_CALLABLE iterator(Tile& t, int i) : tile(t), offset(i) {} + inline CUDA_CALLABLE T& operator*() const + { + assert(offset < tile_size(tile)); + + return tile.data[offset]; + } + inline CUDA_CALLABLE iterator& operator++() { offset += WP_TILE_BLOCK_DIM; return *this; } inline CUDA_CALLABLE bool valid() const { return index() < tile_size(tile); } @@ -160,8 +198,22 @@ struct tile_shared_t }; iterator iter() { return iterator(*this, threadIdx.x); } + + void zero() + { + // todo: make this subtile (stride aware)? + for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) + data[i] = T(0); + } }; +template +auto tile_transpose(Tile& t) +{ + // alias incoming tile + return tile_shared_t(t.data); +} + template struct tile_register_t @@ -175,9 +227,11 @@ struct tile_register_t tile_register_t() { - // zero-initialize by default - // necessary for tile adjoints + // zero-initialize by default necessary for tile adjoints // need to check if this results in worse codegen + // than doing adj_var = tile_zeros() explicitly + // in backwards pass and letting default constructor + // avoid initialization for (int i=0; i < NumRegs; ++i) data[i] = T(0); } @@ -187,14 +241,19 @@ struct tile_register_t tile_register_t& tile; int offset; - inline CUDA_CALLABLE iterator(tile_register_t& t, int i) : tile(t), offset(i) {} + inline CUDA_CALLABLE iterator(tile_register_t& t) : tile(t), offset(0) { } - inline CUDA_CALLABLE T& operator*() const { return tile.data[offset]; } + inline CUDA_CALLABLE T& operator*() const + { + assert(offset < NumRegs); + + return tile.data[offset]; + } inline CUDA_CALLABLE iterator& operator++() { ++offset; return *this; } - inline CUDA_CALLABLE bool valid() const { return offset < NumRegs && index() < tile_size(tile); } + inline CUDA_CALLABLE bool valid() const { return index() < tile_size(tile); } // linear index into the tile's data (assuming row-major layout) - inline CUDA_CALLABLE int index() const { return threadIdx.x + offset*WP_TILE_BLOCK_DIM; } + inline CUDA_CALLABLE int index() const { return threadIdx.x + offset*WP_TILE_BLOCK_DIM;} inline CUDA_CALLABLE coord_t coord() const { int i = index(); @@ -202,7 +261,7 @@ struct tile_register_t } }; - iterator iter() { return iterator(*this, 0); } + iterator iter() { return iterator(*this); } }; @@ -272,19 +331,27 @@ template inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, array_t& adj_src, int adj_x, int adj_y, AdjTile& adj_ret) -{ - // add gradients to src array - WP_PRAGMA_UNROLL +{ + // add gradients to src array for (auto adj_iter=adj_ret.iter(); adj_iter.valid(); ++adj_iter) { coord_t c = adj_iter.coord(); - atomic_add(adj_src, x*adj_ret.M + c.i, y*adj_ret.N + c.j, *adj_iter); + + int i = x*adj_ret.M + c.i; + int j = y*adj_ret.N + c.j; + + auto grad = *adj_iter; + + if (adj_src.data) + adj_atomic_add(&index(adj_src, i, j), grad); + else if (src.grad) + adj_atomic_add(&index_grad(src, i, j), grad); } } template inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, AdjTile& adj_t) -{ +{ const int M = t.M; const int N = t.N; @@ -293,7 +360,14 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t for (auto adj_iter=adj_t.iter(); adj_iter.valid(); ++adj_iter) { coord_t c = adj_iter.coord(); - *adj_iter += index(adj_dest, x*M + c.i, y*N + c.j, *adj_iter); + + int i = x*M + c.i; + int j = y*N + c.j; + + if (adj_dest.data) + *adj_iter += index(adj_dest, i, j); + else if (dest.grad) + *adj_iter += index_grad(dest, i, j); } } diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index fca527d0..5cf8ba04 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -219,7 +219,7 @@ inline CUDA_CALLABLE void gemm(const array_t& A, const array_t& B, const a template inline CUDA_CALLABLE void tile_matmul_scalar(const TileA& A, const TileB& B, - const TileC& out) + TileC& out) { const int length = tile_size(out); @@ -227,10 +227,6 @@ inline CUDA_CALLABLE void tile_matmul_scalar(const TileA& A, using T = typename TileA::Type; - const T* __restrict__ A_ptr = A.data; - const T* __restrict__ B_ptr = B.data; - T* __restrict__ C_ptr = out.data; - WP_PRAGMA_UNROLL for (int t=threadIdx.x; t < length; t += blockDim.x) { @@ -243,13 +239,13 @@ inline CUDA_CALLABLE void tile_matmul_scalar(const TileA& A, WP_PRAGMA_UNROLL for (int k=0; k < A.N; ++k) { - T a = index(A_ptr, i, k, A.N); - T b = index(B_ptr, k, j, B.N); + T a = A(i,k); + T b = B(k,j); - sum = fmaf(a, b, sum); + sum += a*b; // todo: use fmaf() } - index(C_ptr, i, j, out.N) += sum; + out(i,j) += sum; } WP_TILE_SYNC(); @@ -311,46 +307,6 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, #endif // USE_CUTE -template -struct tile_matmul_t -{ - static_assert(wp::is_same::value, "Error, tile datatypes must match"); - static_assert(TileA::N == TileB::M, "Error, inner dimensions must match"); - static_assert(TileC::M == TileA::M, "Error, first output dimension must match"); - static_assert(TileC::N == TileB::N, "Error, second output dimension must match"); - - using Type = typename TileA::Type; - static constexpr int M = TileC::M; - static constexpr int N = TileC::N; - - TileA tile_a; - TileB tile_b; - TileC tile_c; - - tile_matmul_t(TileA &a, TileB &b, TileC &c) : tile_a(a), - tile_b(b), - tile_c(c) {} - - Type fwd(int e) const - { - // load - - - } - - void bwd(int e, Type adj_ret) const - { - } - - void print() - { - printf("tile_matmul_t<%d, %d>", M, N); - printf("\n -+"); - tile_a.print(); - printf("\n -+"); - tile_b.print(); - } -}; template @@ -370,12 +326,8 @@ void adj_tile_matmul(TileA& a, TileB& b, TileC& c, TileA& adj_a, TileB& adj_b, TileC& adj_c) { - // auto a_shared = tile_eval(a); - // auto b_shared = tile_eval(b); - // auto adj_c_shared = tile_eval(b); - - // tile_matmul_scalar(adj_c, wp.tile_transpose(b), adj_a); - // tile_matmul_scalar(wp.tile_transpose(a), adj_c, adj_b); + tile_matmul_scalar(adj_c, wp::tile_transpose(b), adj_a); + tile_matmul_scalar(wp::tile_transpose(a), adj_c, adj_b); } diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 02fc9870..6365d91d 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -3,18 +3,16 @@ import torch -#wp.config.mode = "debug" - wp.init() wp.set_module_options({"enable_backward": True}) wp.set_device("cuda:0") - +wp.config.mode = "debug" wp.config.verify_cuda = True wp.build.clear_kernel_cache() TILE_M = wp.constant(32) -TILE_N = wp.constant(32) +TILE_N = wp.constant(16) TILE_K = wp.constant(8) @wp.kernel @@ -232,17 +230,15 @@ def test_tile_batched_gemm(): B = rng.random((batch_count, K, N), dtype=np.float32) C = np.zeros((batch_count, M, N), dtype=np.float32) - A_wp = wp.array(A) - B_wp = wp.array(B) - C_wp = wp.array(C) + A_wp = wp.array(A, requires_grad=True) + B_wp = wp.array(B, requires_grad=True) + C_wp = wp.array(C, requires_grad=True) - wp.launch(tile_grouped_gemm, dim=batch_count, inputs=[A_wp, B_wp, C_wp], tile_size=8) + with wp.Tape() as tape: + wp.launch(tile_grouped_gemm, dim=batch_count, inputs=[A_wp, B_wp, C_wp], tile_size=8) # bring back to host - C_wp = C_wp.numpy() - - for i in range(batch_count): - assert(np.allclose(A[i]@B[i], C_wp[i], rtol=1.e-4)) + C_host = C_wp.numpy() # GEMM forward passed print("batched matmul forward passed") @@ -263,7 +259,7 @@ def tile_gemm(A: wp.array2d(dtype=float), K = A.shape[1] count = int(K / TILE_K) - + for k in range(0, count): a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) @@ -278,30 +274,41 @@ def tile_gemm(A: wp.array2d(dtype=float), def test_tile_gemm(): M = TILE_M*7 - K = TILE_K*4 - N = TILE_N*6 + K = TILE_K*5 + N = TILE_N*2 rng = np.random.default_rng(42) A = rng.random((M, K), dtype=np.float32) B = rng.random((K, N), dtype=np.float32) C = np.zeros((M, N), dtype=np.float32) - A_wp = wp.array(A) - B_wp = wp.array(B) - C_wp = wp.array(C) + A_wp = wp.array(A, requires_grad=True) + B_wp = wp.array(B, requires_grad=True) + C_wp = wp.array(C, requires_grad=True) - wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=8) + with wp.Tape() as tape: + wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=32) assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) # GEMM forward passed print("matmul forward passed") + adj_C = np.ones_like(C) + + tape.backward(grads={C_wp: wp.array(adj_C)}) + + assert(np.allclose(adj_C@B.T, A_wp.grad.numpy(), rtol=1.e-4)) + assert(np.allclose(A.T@adj_C, B_wp.grad.numpy(), rtol=1.e-4)) + + print("matmul backward passed") + + test_tile_copy() test_tile_unary_map() test_tile_binary_map() -# test_tile_batched_gemm() -# test_tile_gemm() +test_tile_batched_gemm() +test_tile_gemm() test_tile_operators() \ No newline at end of file From defbe87a4326acdf67ce703f46cc0126e095a69e Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 9 Sep 2024 17:22:02 +1200 Subject: [PATCH 021/102] Add support for scalar*tile, tile*scalar operators --- warp/builtins.py | 16 +++++- warp/native/tile.h | 83 +++++++++++++++++++++++++++--- warp/native/tile_gemm.h | 2 +- warp/tests/test_tile.py | 109 +++++++++++++++++++++------------------- 4 files changed, 149 insertions(+), 61 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index abe2d7b5..7e3c5722 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -2012,6 +2012,18 @@ def tile_binary_map_value_func(arg_types, arg_values): export=False, ) +add_builtin( + "add", + input_types={"a": Tile(dtype=Any, M=Any, N=Any), "b": Tile(dtype=Any, M=Any, N=Any)}, + value_func=tile_binary_map_value_func, + #dispatch_func=tile_map_dispatch_func, + #variadic=True, + native_func="tile_add", + doc="Add each element of two tiles together", + group="Tile Primitives", + export=False, +) + # --------------------------------- # Linear Algebra @@ -4494,14 +4506,14 @@ def tile_scalar_mul_value_func(arg_types, arg_values): if x.dtype != y: raise RuntimeError("Scalar factor should have the same type as tile for tile*scalar, tile type: {x} scalar type: {y}") - return TileBinaryMap(x, TileConstant(x.dtype, x.M, x.N)) + return TileBinaryMap(x, TileConstant(y, x.M, x.N)) # scalar*tile if is_tile(y): if y.dtype != x: raise RuntimeError("Scalar factor should have the same type as tile for scalar*tile, tile type: {x} scalar type: {y}") - return TileBinaryMap(TileConstant(x.dtype, x.M, x.N), y) + return TileBinaryMap(TileConstant(x, y.M, y.N), y) diff --git a/warp/native/tile.h b/warp/native/tile.h index 5111d958..b08cdf4c 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -64,6 +64,7 @@ [ ] Error checking [ ] Ensure functions passed to tile_map() are compatible with tile type [ ] Ensure that args passed to tile ops are compatible + [ ] Ensure tile load/store operations don't go out of bounds of arrays in debug mode */ @@ -225,7 +226,7 @@ struct tile_register_t T data[NumRegs]; - tile_register_t() + tile_register_t(T value=T(0.0)) { // zero-initialize by default necessary for tile adjoints // need to check if this results in worse codegen @@ -233,7 +234,7 @@ struct tile_register_t // in backwards pass and letting default constructor // avoid initialization for (int i=0; i < NumRegs; ++i) - data[i] = T(0); + data[i] = value; } struct iterator @@ -381,10 +382,13 @@ auto tile_map(Fwd op, auto out_iter = out.iter(); auto a_iter = a.iter(); + WP_PRAGMA_UNROLL for (; out_iter.valid(); ++out_iter, ++a_iter) - { *out_iter = op(*a_iter); - } + + // WP_PRAGMA_UNROLL + // for (int i=0; i < Tile::NumRegs; ++i) + // out.data[i] = op(a.data[i]); return out; } @@ -400,6 +404,7 @@ void adj_tile_map(Fwd op, auto adj_a_iter = adj_a.iter(); auto adj_ret_iter = adj_ret.iter(); + WP_PRAGMA_UNROLL for (; a_iter.valid(); ++a_iter, ++adj_a_iter, ++adj_ret_iter) { adj_op(*a_iter, *adj_a_iter, *adj_ret_iter); @@ -418,10 +423,14 @@ auto tile_map(Fwd op, auto a_iter = a.iter(); auto b_iter = b.iter(); + WP_PRAGMA_UNROLL for (; out_iter.valid(); ++out_iter, ++a_iter, ++b_iter) - { *out_iter = op(*a_iter, *b_iter); - } + + // WP_PRAGMA_UNROLL + // for (int i=0; i < TileA::NumRegs; ++i) + // out.data[i] = op(a.data[i], b.data[i]); + return out; } @@ -441,6 +450,7 @@ void adj_tile_map(Fwd op, auto adj_b_iter = adj_b.iter(); auto adj_ret_iter = adj_ret.iter(); + WP_PRAGMA_UNROLL for (; a_iter.valid(); ++a_iter, ++b_iter, ++adj_a_iter, ++adj_b_iter, ++adj_ret_iter) { adj_op(*a_iter, *b_iter, *adj_a_iter, *adj_b_iter, *adj_ret_iter); @@ -509,6 +519,67 @@ CUDA_CALLABLE inline auto tile_mul_impl(typename Tile::Type s, Tile& t, #define tile_add(a, b) tile_add_impl(a, b adj_##a, adj_##b) */ +template +auto tile_add(TileA& a, TileB& b) +{ + return tile_binary_map(add, a, b); +} + +template +void adj_tile_add(TileA& a, TileB& b, TileA& adj_a, TileB& adj_b, AdjTile& adj_c) +{ + adj_tile_binary_map(add, a, b, adj_add, adj_a, adj_b, adj_c); +} + +// tile*scalar +template +auto tile_mul(Tile& a, const typename Tile::Type& s) +{ + // promote scalar to a constant tile + auto s_tile = tile_register_t(s); + + return tile_binary_map(mul, a, s_tile); +} + +template +void adj_tile_mul(Tile& a, const typename Tile::Type& s, + Tile& adj_a, typename Tile::Type& adj_s, + AdjTile& adj_c) +{ + // auto s_tile = tile_register_t(s); + // auto adj_s_tile = tile_register_t(); + + // adj_tile_binary_map(mul, a, s_tile, adj_mul, adj_a, adj_s_tile, adj_c); + + // todo: sum up contribution from all adj_s_tile onto original scalar + //adj_tile_sum() +} + + +// scalar*tile +template +auto tile_mul(const typename Tile::Type& s, Tile& a) +{ + // promote scalar to a constant tile + auto s_tile = tile_register_t(s); + + return tile_binary_map(mul, s_tile, a); +} + +template +void adj_tile_mul(const typename Tile::Type& s, Tile& a, + typename Tile::Type& adj_s, Tile& adj_a, + AdjTile& adj_c) +{ + // auto s_tile = tile_register_t(s); + // auto adj_s_tile = tile_register_t(); + + // adj_tile_binary_map(mul, a, s_tile, adj_mul, adj_a, adj_s_tile, adj_c); + + // todo: sum up contribution from all adj_s_tile onto original scalar + //adj_tile_sum() +} + } // namespace wp diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index 5cf8ba04..faf807ad 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -228,7 +228,7 @@ inline CUDA_CALLABLE void tile_matmul_scalar(const TileA& A, using T = typename TileA::Type; WP_PRAGMA_UNROLL - for (int t=threadIdx.x; t < length; t += blockDim.x) + for (int t=threadIdx.x; t < length; t += WP_TILE_BLOCK_DIM) { // compute output index const int i = t/out.N; diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 6365d91d..2a025362 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -6,15 +6,18 @@ wp.init() wp.set_module_options({"enable_backward": True}) wp.set_device("cuda:0") -wp.config.mode = "debug" -wp.config.verify_cuda = True +#wp.config.mode = "debug" +#wp.config.verify_cuda = True wp.build.clear_kernel_cache() -TILE_M = wp.constant(32) -TILE_N = wp.constant(16) +TILE_M = wp.constant(16) +TILE_N = wp.constant(8) TILE_K = wp.constant(8) +# num threads per-tile +TILE_DIM = 64 + @wp.kernel def tile_copy(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): @@ -40,7 +43,7 @@ def test_tile_copy(): B_wp = wp.array(B, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_copy, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) + wp.launch(tile_copy, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=TILE_DIM) # verify forward pass assert(np.allclose(A, B_wp.numpy(), rtol=1.e-4)) @@ -87,7 +90,7 @@ def test_tile_unary_map(): B_wp = wp.zeros_like(A_wp, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_unary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=8) + wp.launch(tile_unary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=TILE_DIM) # verify forward pass assert(np.allclose(B, B_wp.numpy(), rtol=1.e-4)) @@ -140,7 +143,7 @@ def test_tile_binary_map(): C_wp = wp.zeros_like(A_wp, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_binary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp, C_wp], tile_size=8) + wp.launch(tile_binary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp, C_wp], tile_size=TILE_DIM) # verify forward pass assert(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) @@ -156,49 +159,6 @@ def test_tile_binary_map(): print("Binary map backward passed") -@wp.kernel -def tile_operators(input: wp.array3d(dtype=float), - output: wp.array3d(dtype=float)): - - # output tile index - i = wp.tid() - - a = wp.tile_load(input[i], 0, 0, m=32, n=8) - - # neg - b = -a - - # scalar multiply -# c = b*0.5 - - # # add tiles - # c = a + b - - wp.tile_store(output[i], 0, 0, b) - - -def test_tile_operators(): - - batch_count = 56 - - M = 32 - N = 8 - - rng = np.random.default_rng(42) - input = rng.random((batch_count, M, N), dtype=np.float32) - output = -input - - input_wp = wp.array(input) - output_wp = wp.zeros_like(input_wp) - - wp.launch(tile_operators, dim=batch_count, inputs=[input_wp, output_wp], tile_size=8) - - assert(np.allclose(output, output_wp.numpy(), rtol=1.e-4)) - - print("operators forward passed") - - - @wp.kernel def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), @@ -235,7 +195,7 @@ def test_tile_batched_gemm(): C_wp = wp.array(C, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_grouped_gemm, dim=batch_count, inputs=[A_wp, B_wp, C_wp], tile_size=8) + wp.launch(tile_grouped_gemm, dim=batch_count, inputs=[A_wp, B_wp, C_wp], tile_size=TILE_DIM) # bring back to host C_host = C_wp.numpy() @@ -287,7 +247,7 @@ def test_tile_gemm(): C_wp = wp.array(C, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=32) + wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=TILE_DIM) assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) @@ -305,6 +265,51 @@ def test_tile_gemm(): +@wp.kernel +def tile_operators(input: wp.array3d(dtype=float), + output: wp.array3d(dtype=float)): + + # output tile index + i = wp.tid() + + a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) + + # neg + b = -a + + # right scalar multiply + c = b*0.5 + + # left scalar multiply + d = 0.5*c + + # add tiles + e = a + d + + wp.tile_store(output[i], 0, 0, e) + + +def test_tile_operators(): + + batch_count = 56 + + M = TILE_M + N = TILE_N + + rng = np.random.default_rng(42) + input = rng.random((batch_count, M, N), dtype=np.float32) + output = input*0.75 + + input_wp = wp.array(input) + output_wp = wp.zeros_like(input_wp) + + wp.launch(tile_operators, dim=batch_count, inputs=[input_wp, output_wp], tile_size=TILE_DIM) + + assert(np.allclose(output, output_wp.numpy(), rtol=1.e-4)) + + print("operators forward passed") + + test_tile_copy() test_tile_unary_map() From 77172ad297ab76dc5d8c99871dfece72218abbba Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 11 Sep 2024 12:55:28 +1200 Subject: [PATCH 022/102] Code-gen improvements, force ops. to promote shared memory tiles to registers before execution --- warp/native/tile.h | 485 ++++++++++++++++++++++++---------------- warp/tests/test_tile.py | 7 +- 2 files changed, 302 insertions(+), 190 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index b08cdf4c..b271ccd3 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -93,29 +93,9 @@ struct is_same { static constexpr bool value = true; }; -template -void print_tile(T& t) -{ - t.print(); - - printf("["); - for (int i=0; i < T::M; ++i) - { - printf("%*s[", i>0, ""); - for (int j=0; j < T::N; ++j) - { - printf("%5.2f ", t.data[i*T::N + j]); - } - - if (i == T::M-1) - printf("]]\n"); - else - printf("]\n"); - } -} template -int tile_size(Tile& t) { return Tile::M*Tile::N; } +constexpr int tile_size(Tile& t) { return Tile::M*Tile::N; } constexpr int tile_regcount(int m, int n) { return (m*n + WP_TILE_BLOCK_DIM - 1) / WP_TILE_BLOCK_DIM; @@ -140,23 +120,96 @@ inline CUDA_CALLABLE T* tile_alloc_shared() } +template +struct tile_register_t +{ + using Type = T; + static constexpr int M = M_; + static constexpr int N = N_; + static constexpr int Size = M*N; + + static constexpr int NumRegs = tile_regcount(M, N); + + T data[NumRegs]; + + inline CUDA_CALLABLE tile_register_t(T value=T(0.0)) + { + // zero-initialize by default necessary for tile adjoints + // need to check if this results in worse codegen + // than doing adj_var = tile_zeros() explicitly + // in backwards pass and letting default constructor + // avoid initialization + + for (int i=0; i < NumRegs; ++i) + data[i] = value; + } + + // compute linear tile index from a local register index + inline CUDA_CALLABLE int index(int reg) const + { + return threadIdx.x + reg*WP_TILE_BLOCK_DIM; + } + + // compute tile coordinate from linear index + inline CUDA_CALLABLE coord_t coord(int index) const + { + return {index/N, index%N}; + } + + // Returns the number of valid registers for this tile + // i.e.: how many registers map to a valid coordinate. + // When a tile's size is not aligned to the block dimension + // some of the trailing registers may lie outside the valid range + inline CUDA_CALLABLE int valid() const + { + return (Size - threadIdx.x)/WP_TILE_BLOCK_DIM; + } + + // return the in-register version of this tile (nop) + inline CUDA_CALLABLE auto& get() { return *this; } + + inline CUDA_CALLABLE void assign(const tile_register_t& tile) + { + for (int i=0; i < NumRegs; ++i) + data[i] = tile.data[i]; + } + + + inline CUDA_CALLABLE void print() + { + printf("tid: %d ", threadIdx.x); + + for (int i=0; i < NumRegs; ++i) + { + printf("%f ", data[i]); + } + + printf("\n"); + } + +}; + + + template struct tile_shared_t { using Type = T; static constexpr int M = M_; static constexpr int N = N_; + static constexpr int Size = M*N; + static constexpr int StrideM = StrideM_; static constexpr int StrideN = StrideN_; T* data = NULL; - tile_shared_t() {} - tile_shared_t(T* smem) : data(smem) + inline CUDA_CALLABLE tile_shared_t() {} + inline CUDA_CALLABLE tile_shared_t(T* smem) : data(smem) { } - inline T& operator()(int i, int j) + inline CUDA_CALLABLE T& operator()(int i, int j) { assert(i < M); assert(j < N); @@ -164,7 +217,7 @@ struct tile_shared_t return data[i*StrideM + j*StrideN]; } - inline const T& operator()(int i, int j) const + inline CUDA_CALLABLE const T& operator()(int i, int j) const { assert(i < M); assert(j < N); @@ -172,99 +225,116 @@ struct tile_shared_t return data[i*StrideM + j*StrideN]; } - struct iterator + inline CUDA_CALLABLE T& operator()(int index) { - tile_shared_t& tile; - int offset; - - template - inline CUDA_CALLABLE iterator(Tile& t, int i) : tile(t), offset(i) {} - inline CUDA_CALLABLE T& operator*() const - { - assert(offset < tile_size(tile)); + assert(index < M*N); - return tile.data[offset]; - } + // unravel + int i = index/N; + int j = index%N; - inline CUDA_CALLABLE iterator& operator++() { offset += WP_TILE_BLOCK_DIM; return *this; } - inline CUDA_CALLABLE bool valid() const { return index() < tile_size(tile); } + return (*this)(i,j); + } - // linear index into the tile's data (assuming row-major layout) - inline CUDA_CALLABLE int index() const { return offset; } - inline CUDA_CALLABLE coord_t coord() const - { - int i = index(); - return {i/N, i%N}; - } - }; + inline CUDA_CALLABLE const T& operator()(int index) const + { + assert(index < M*N); - iterator iter() { return iterator(*this, threadIdx.x); } + // unravel + int i = index/N; + int j = index%N; - void zero() + return (*this)(i,j); + } + + // in-place zero + inline CUDA_CALLABLE void zero() { - // todo: make this subtile (stride aware)? + // todo: make this subtile (stride aware) for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) data[i] = T(0); } -}; - -template -auto tile_transpose(Tile& t) -{ - // alias incoming tile - return tile_shared_t(t.data); -} - - -template -struct tile_register_t -{ - using Type = T; - static constexpr int M = M_; - static constexpr int N = N_; - static constexpr int NumRegs = tile_regcount(M, N); - T data[NumRegs]; - - tile_register_t(T value=T(0.0)) + // compute linear tile index from a local register index + inline CUDA_CALLABLE int index(int reg) const { - // zero-initialize by default necessary for tile adjoints - // need to check if this results in worse codegen - // than doing adj_var = tile_zeros() explicitly - // in backwards pass and letting default constructor - // avoid initialization - for (int i=0; i < NumRegs; ++i) - data[i] = value; + return threadIdx.x + reg*WP_TILE_BLOCK_DIM; } - struct iterator + // compute tile coordinate from linear index + inline CUDA_CALLABLE coord_t coord(int index) const { - tile_register_t& tile; - int offset; - - inline CUDA_CALLABLE iterator(tile_register_t& t) : tile(t), offset(0) { } + return {index/N, index%N}; + } - inline CUDA_CALLABLE T& operator*() const + // copy shared tile to register + inline CUDA_CALLABLE tile_register_t get() + { + tile_register_t out; + + WP_PRAGMA_UNROLL + for (int i=0; i < out.NumRegs; ++i) { - assert(offset < NumRegs); + const int linear = out.index(i); + + // handle case where tile size is not + // aligned to block dimensions + if (linear > Size) + break; - return tile.data[offset]; + out.data[i] = (*this)(linear); } - inline CUDA_CALLABLE iterator& operator++() { ++offset; return *this; } - inline CUDA_CALLABLE bool valid() const { return index() < tile_size(tile); } - // linear index into the tile's data (assuming row-major layout) - inline CUDA_CALLABLE int index() const { return threadIdx.x + offset*WP_TILE_BLOCK_DIM;} - inline CUDA_CALLABLE coord_t coord() const + return out; + } + + // copy register tile to shared + inline CUDA_CALLABLE void assign(const tile_register_t& tile) + { + WP_PRAGMA_UNROLL + for (int i=0; i < tile.NumRegs; ++i) { - int i = index(); - return {i/N, i%N}; + const int linear = tile.index(i); + + // handle case where tile size is not + // aligned to block dimensions + if (linear > Size) + break; + + // todo: should use coord here to handle cases where + // shared tile is a slice? + data[linear] = tile.data[i]; } - }; + } - iterator iter() { return iterator(*this); } + inline CUDA_CALLABLE void print() + { + if (threadIdx.x == 0) + { + printf("["); + for (int i=0; i < M; ++i) + { + printf("%*s[", i>0, ""); + for (int j=0; j < N; ++j) + { + printf("%5.2f ", data(i, j)); + } + + if (i == M-1) + printf("]]\n"); + else + printf("]\n"); + } + } + } }; +template +inline CUDA_CALLABLE auto tile_transpose(Tile& t) +{ + // alias incoming tile + return tile_shared_t(t.data); +} //----------------------------------------------------------------------------------------------------- @@ -287,7 +357,7 @@ inline CUDA_CALLABLE auto tile_zeros() } -// entry point for store +// entry point for load template inline CUDA_CALLABLE auto tile_load(array_t& src, int x, int y) { @@ -297,12 +367,21 @@ inline CUDA_CALLABLE auto tile_load(array_t& src, int x, int y) tile_shared_t dest(data); + const int tile_i = x*M; + const int tile_j = y*N; + + // wp.array() indexing generates poor code due to char* casting + // here we unroll some of the ops, note this assumes byte strides are + // aligned to the element size + T* ptr = &index(src, tile_i, tile_j); + const int stride_i = src.strides[0]/sizeof(T); + const int stride_j = src.strides[1]/sizeof(T); + WP_PRAGMA_UNROLL - for (auto dst_iter=dest.iter(); dst_iter.valid(); ++dst_iter) + for (int i=threadIdx.x; i < length; i += WP_TILE_BLOCK_DIM) { - coord_t c = dst_iter.coord(); - - *dst_iter = index(src, x*M + c.i, y*N + c.j); + coord_t c = dest.coord(i); + dest.data[i] = ptr[c.i*stride_i + c.j*stride_j]; //index(src, tile_i + c.i, tile_j + c.j); } return dest; @@ -312,19 +391,34 @@ inline CUDA_CALLABLE auto tile_load(array_t& src, int x, int y) template inline CUDA_CALLABLE void tile_store(array_t& dest, int x, int y, Tile& src) { - const int M = src.M; - const int N = src.N; - - // cooperatively store the tile, using a block-stride iterator - WP_PRAGMA_UNROLL - for (auto src_iter=src.iter(); src_iter.valid(); ++src_iter) - { - coord_t c = src_iter.coord(); + auto src_reg = src.get(); - index(dest, x*M + c.i, y*N + c.j) = *src_iter; + const int tile_i = x*src.M; + const int tile_j = y*src.N; + + // wp.array() indexing generates poor code due to char* casting + // here we unroll some of the ops, note this assumes byte strides are + // aligned to the element size + T* ptr = &index(dest, tile_i, tile_j); + const int stride_i = dest.strides[0]/sizeof(T); + const int stride_j = dest.strides[1]/sizeof(T); + + WP_PRAGMA_UNROLL + for (int i=0; i < src_reg.NumRegs; ++i) + { + // handle case where tile size is not + // aligned to block dimensions + int index = src_reg.index(i); + if (index > src_reg.Size) + break; + + coord_t c = src_reg.coord(index); + ptr[c.i*stride_i + c.j*stride_j] = src_reg.data[i]; //index(dest, tile_i + c.i, tile_j + c.j); } } + + //------------------------------------- // Adjoints @@ -332,129 +426,146 @@ template inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, array_t& adj_src, int adj_x, int adj_y, AdjTile& adj_ret) -{ - // add gradients to src array - for (auto adj_iter=adj_ret.iter(); adj_iter.valid(); ++adj_iter) +{ + // early out + // if (!src.grad) + // return; + + auto adj_reg = adj_ret.get(); + + const int tile_i = x*adj_reg.M; + const int tile_j = y*adj_reg.N; + + // add gradients to src array + WP_PRAGMA_UNROLL + for (int i=0; i < adj_reg.NumRegs; ++i) { - coord_t c = adj_iter.coord(); + int linear = adj_reg.index(i); + if (linear > adj_reg.Size) + break; - int i = x*adj_ret.M + c.i; - int j = y*adj_ret.N + c.j; + coord_t coord = adj_reg.coord(linear); - auto grad = *adj_iter; + auto grad = adj_reg.data[i]; if (adj_src.data) - adj_atomic_add(&index(adj_src, i, j), grad); + adj_atomic_add(&index(adj_src, tile_i + coord.i, tile_j + coord.j), grad); else if (src.grad) - adj_atomic_add(&index_grad(src, i, j), grad); + adj_atomic_add(&index_grad(src, tile_i + coord.i, tile_j + coord.j), grad); } } template inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, AdjTile& adj_t) { - const int M = t.M; - const int N = t.N; + // if (!dest.grad) + // return; + + // convert to register if necessary + auto adj_reg = adj_t.get(); + + const int tile_i = x*adj_reg.M; + const int tile_j = y*adj_reg.N; // load gradients from output WP_PRAGMA_UNROLL - for (auto adj_iter=adj_t.iter(); adj_iter.valid(); ++adj_iter) + for (int i=0; i < adj_reg.NumRegs; ++i) { - coord_t c = adj_iter.coord(); + int linear = adj_reg.index(i); + if (linear > adj_reg.Size) + break; - int i = x*M + c.i; - int j = y*N + c.j; + coord_t coord = adj_reg.coord(linear); - if (adj_dest.data) - *adj_iter += index(adj_dest, i, j); + if (adj_dest.data) + adj_reg.data[i] += index(adj_dest, tile_i + coord.i, tile_j + coord.j); else if (dest.grad) - *adj_iter += index_grad(dest, i, j); + adj_reg.data[i] += index_grad(dest, tile_i + coord.i, tile_j + coord.j); } + + // store adjoint back to tile + adj_t.assign(adj_reg); } // unary map template -auto tile_map(Fwd op, - Tile &a) +inline CUDA_CALLABLE auto tile_map(Fwd op, + Tile &a) { auto out = tile_register_t(); - - auto out_iter = out.iter(); - auto a_iter = a.iter(); - + auto a_reg = a.get(); + WP_PRAGMA_UNROLL - for (; out_iter.valid(); ++out_iter, ++a_iter) - *out_iter = op(*a_iter); - - // WP_PRAGMA_UNROLL - // for (int i=0; i < Tile::NumRegs; ++i) - // out.data[i] = op(a.data[i]); + for (int i=0; i < out.NumRegs; ++i) + { + out.data[i] = op(a_reg.data[i]); + } return out; } template -void adj_tile_map(Fwd op, - Tile &a, - Adj adj_op, - Tile &adj_a, - AdjTile &adj_ret) +inline CUDA_CALLABLE void adj_tile_map(Fwd op, + Tile& a, + Adj adj_op, + Tile& adj_a, + AdjTile& adj_ret) { - auto a_iter = a.iter(); - auto adj_a_iter = adj_a.iter(); - auto adj_ret_iter = adj_ret.iter(); + auto a_reg = a.get(); + auto adj_a_reg = adj_a.get(); + auto adj_ret_reg = adj_ret.get(); WP_PRAGMA_UNROLL - for (; a_iter.valid(); ++a_iter, ++adj_a_iter, ++adj_ret_iter) - { - adj_op(*a_iter, *adj_a_iter, *adj_ret_iter); + for (int i=0; i < a_reg.NumRegs; ++i) + { + adj_op(a_reg.data[i], adj_a_reg.data[i], adj_ret_reg.data[i]); } + + // write adjoints back + adj_a.assign(adj_a_reg); } // binary map template -auto tile_map(Fwd op, - TileA &a, - TileB &b) +inline CUDA_CALLABLE auto tile_map(Fwd op, + TileA& a, + TileB& b) { auto out = tile_register_t(); - auto out_iter = out.iter(); - auto a_iter = a.iter(); - auto b_iter = b.iter(); + auto a_reg = a.get(); + auto b_reg = b.get(); WP_PRAGMA_UNROLL - for (; out_iter.valid(); ++out_iter, ++a_iter, ++b_iter) - *out_iter = op(*a_iter, *b_iter); - - // WP_PRAGMA_UNROLL - // for (int i=0; i < TileA::NumRegs; ++i) - // out.data[i] = op(a.data[i], b.data[i]); - + for (int i=0; i < out.NumRegs; ++i) + out.data[i] = op(a_reg.data[i], b_reg.data[i]); return out; } template -void adj_tile_map(Fwd op, - TileA &a, - TileB &b, - Adj adj_op, - TileA &adj_a, - TileB &adj_b, - AdjTile &adj_ret) +inline CUDA_CALLABLE void adj_tile_map(Fwd op, + TileA &a, + TileB &b, + Adj adj_op, + TileA &adj_a, + TileB &adj_b, + AdjTile &adj_ret) { - auto a_iter = a.iter(); - auto b_iter = b.iter(); - auto adj_a_iter = adj_a.iter(); - auto adj_b_iter = adj_b.iter(); - auto adj_ret_iter = adj_ret.iter(); + auto a_reg = a.get(); + auto b_reg = b.get(); + auto adj_a_reg = adj_a.get(); + auto adj_b_reg = adj_b.get(); + auto adj_ret_reg = adj_ret.get(); WP_PRAGMA_UNROLL - for (; a_iter.valid(); ++a_iter, ++b_iter, ++adj_a_iter, ++adj_b_iter, ++adj_ret_iter) + for (int i=0; i < a_reg.NumRegs; ++i) { - adj_op(*a_iter, *b_iter, *adj_a_iter, *adj_b_iter, *adj_ret_iter); + adj_op(a_reg.data[i], b_reg.data[i], adj_a_reg.data[i], adj_b_reg.data[i], adj_ret_reg.data[i]); } + + adj_a.assign(adj_a_reg); + adj_b.assign(adj_b_reg); } // wrap the operator in a lambda so that we don't have to do overload resolution for things like e.g.: wp.sin() @@ -468,10 +579,10 @@ void adj_tile_map(Fwd op, // unary neg template -auto tile_neg(Tile& a) { return tile_unary_map(wp::neg, a); } +inline CUDA_CALLABLE auto tile_neg(Tile& a) { return tile_unary_map(wp::neg, a); } template -void adj_tile_neg(Tile& a, Tile& adj_a, AdjTile& adj_ret) { adj_tile_unary_map(wp::neg, a, wp::adj_neg, adj_a, adj_ret); } +inline CUDA_CALLABLE void adj_tile_neg(Tile& a, Tile& adj_a, AdjTile& adj_ret) { adj_tile_unary_map(wp::neg, a, wp::adj_neg, adj_a, adj_ret); } /* @@ -520,20 +631,20 @@ CUDA_CALLABLE inline auto tile_mul_impl(typename Tile::Type s, Tile& t, */ template -auto tile_add(TileA& a, TileB& b) +inline CUDA_CALLABLE auto tile_add(TileA& a, TileB& b) { return tile_binary_map(add, a, b); } template -void adj_tile_add(TileA& a, TileB& b, TileA& adj_a, TileB& adj_b, AdjTile& adj_c) +inline CUDA_CALLABLE void adj_tile_add(TileA& a, TileB& b, TileA& adj_a, TileB& adj_b, AdjTile& adj_c) { adj_tile_binary_map(add, a, b, adj_add, adj_a, adj_b, adj_c); } // tile*scalar template -auto tile_mul(Tile& a, const typename Tile::Type& s) +inline CUDA_CALLABLE auto tile_mul(Tile& a, const typename Tile::Type& s) { // promote scalar to a constant tile auto s_tile = tile_register_t(s); @@ -542,9 +653,9 @@ auto tile_mul(Tile& a, const typename Tile::Type& s) } template -void adj_tile_mul(Tile& a, const typename Tile::Type& s, - Tile& adj_a, typename Tile::Type& adj_s, - AdjTile& adj_c) +inline CUDA_CALLABLE void adj_tile_mul(Tile& a, const typename Tile::Type& s, + Tile& adj_a, typename Tile::Type& adj_s, + AdjTile& adj_c) { // auto s_tile = tile_register_t(s); // auto adj_s_tile = tile_register_t(); @@ -558,7 +669,7 @@ void adj_tile_mul(Tile& a, const typename Tile::Type& s, // scalar*tile template -auto tile_mul(const typename Tile::Type& s, Tile& a) +inline CUDA_CALLABLE auto tile_mul(const typename Tile::Type& s, Tile& a) { // promote scalar to a constant tile auto s_tile = tile_register_t(s); @@ -567,9 +678,9 @@ auto tile_mul(const typename Tile::Type& s, Tile& a) } template -void adj_tile_mul(const typename Tile::Type& s, Tile& a, - typename Tile::Type& adj_s, Tile& adj_a, - AdjTile& adj_c) +inline CUDA_CALLABLE void adj_tile_mul(const typename Tile::Type& s, Tile& a, + typename Tile::Type& adj_s, Tile& adj_a, + AdjTile& adj_c) { // auto s_tile = tile_register_t(s); // auto adj_s_tile = tile_register_t(); diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 2a025362..e52e0b10 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -6,13 +6,14 @@ wp.init() wp.set_module_options({"enable_backward": True}) wp.set_device("cuda:0") +wp.set_module_options({"fast_math": True}) #wp.config.mode = "debug" #wp.config.verify_cuda = True wp.build.clear_kernel_cache() -TILE_M = wp.constant(16) -TILE_N = wp.constant(8) +TILE_M = wp.constant(32) +TILE_N = wp.constant(16) TILE_K = wp.constant(8) # num threads per-tile @@ -93,7 +94,7 @@ def test_tile_unary_map(): wp.launch(tile_unary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=TILE_DIM) # verify forward pass - assert(np.allclose(B, B_wp.numpy(), rtol=1.e-4)) + assert(np.allclose(B, B_wp.numpy(), atol=1.e-4)) print("Unary map forward passed") # verify backward pass From 7d8ddbc003dc8d264da3c86111b8a86a63cae5e1 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 16 Sep 2024 13:03:06 +1200 Subject: [PATCH 023/102] Automatically set tile storage to shared based on GEMM usage --- warp/builtins.py | 5 + warp/codegen.py | 2 +- warp/native/tile.h | 371 ++++++++++++++++++++++++++-------------- warp/native/tile_gemm.h | 6 +- warp/tests/test_tile.py | 12 +- warp/types.py | 11 +- 6 files changed, 264 insertions(+), 143 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 7e3c5722..1757d469 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1875,6 +1875,11 @@ def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a b = arg_values["b"] out = arg_values["out"] + # set the storage type to the inputs to shared + a.type.storage = "shared" + b.type.storage = "shared" + out.type.storage = "shared" + # template_args.append(dtype) # template_args.append(m) # template_args.append(n) diff --git a/warp/codegen.py b/warp/codegen.py index 88c12d8a..93997b07 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -3033,7 +3033,7 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): ctype = var.ctype(value_type=True) if is_tile(var.type) and var.type.storage == "shared": - lines += [f"{ctype} {name} = wp::tile_alloc_shared<{Var.type_to_ctype(var.type.dtype)},{var.type.M},{var.type.N},{var.type.alloc()}>();\n"] + lines += [f"{ctype} {name} = {{0}};\n"] else: lines += [f"{ctype} {name} = {{}};\n"] diff --git a/warp/native/tile.h b/warp/native/tile.h index b271ccd3..5df1e670 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -119,7 +119,23 @@ inline CUDA_CALLABLE T* tile_alloc_shared() return data; } +// represents a tile stored in global memory with dynamic strides +// only used to represent the source for tile loads to register/shared +template +struct tile_global_t +{ + using Type = T; + array_t data; + int x; + int y; + + tile_global_t(array_t& a, int x, int y) : data(a), x(x), y(y) + { + } +}; + +// represents a tile stored in registers across a block template struct tile_register_t { @@ -130,6 +146,8 @@ struct tile_register_t static constexpr int NumRegs = tile_regcount(M, N); + static constexpr bool Aligned = Size%WP_TILE_BLOCK_DIM == 0; + T data[NumRegs]; inline CUDA_CALLABLE tile_register_t(T value=T(0.0)) @@ -144,6 +162,33 @@ struct tile_register_t data[i] = value; } + inline CUDA_CALLABLE tile_register_t(tile_global_t& t) + { + // construct from a global tile + copy_from_global(t.data, t.x, t.y); + } + + + inline CUDA_CALLABLE auto& operator=(const tile_global_t& t) + { + // assign from a global tile + copy_from_global(t.data, t.x, t.y); + return *this; + } + + + inline CUDA_CALLABLE T& operator()(int index) + { + assert(index < NumRegs); + return data[index]; + } + + inline CUDA_CALLABLE const T& operator()(int index) const + { + assert(index < NumRegs); + return data[index]; + } + // compute linear tile index from a local register index inline CUDA_CALLABLE int index(int reg) const { @@ -165,50 +210,133 @@ struct tile_register_t return (Size - threadIdx.x)/WP_TILE_BLOCK_DIM; } - // return the in-register version of this tile (nop) - inline CUDA_CALLABLE auto& get() { return *this; } - inline CUDA_CALLABLE void assign(const tile_register_t& tile) { for (int i=0; i < NumRegs; ++i) data[i] = tile.data[i]; } - - inline CUDA_CALLABLE void print() + // return the in-register version of this tile (nop) + inline CUDA_CALLABLE auto& copy_to_register() { return *this; } + + + void copy_to_global(array_t dest, int x, int y) { - printf("tid: %d ", threadIdx.x); + const int tile_i = x*M; + const int tile_j = y*N; + // wp.array() indexing generates poor code due to char* casting + // here we unroll some of the ops, note this assumes byte strides are + // aligned to the element size + T* ptr = &wp::index(dest, tile_i, tile_j); + const int stride_i = dest.strides[0]/sizeof(T); + const int stride_j = dest.strides[1]/sizeof(T); + + WP_PRAGMA_UNROLL for (int i=0; i < NumRegs; ++i) { - printf("%f ", data[i]); + // handle case where tile size is not + // aligned to block dimensions + int linear = index(i); + if (!Aligned && linear >= Size) + break; + + coord_t c = coord(linear); + ptr[c.i*stride_i + c.j*stride_j] = data[i]; } + } + + inline CUDA_CALLABLE void copy_from_global(const array_t& src, int x, int y) + { + // todo: use async pipelines or TMA here + const int tile_i = x*M; + const int tile_j = y*N; + + // wp.array() indexing generates poor code due to char* casting + // here we unroll some of the ops, note this assumes array byte strides are + // aligned to the element size + const T* ptr = &wp::index(src, tile_i, tile_j); + + assert(src.strides[0]%sizeof(T) == 0); + assert(src.strides[1]%sizeof(T) == 0); - printf("\n"); + const int stride_i = src.strides[0]/sizeof(T); + const int stride_j = src.strides[1]/sizeof(T); + + WP_PRAGMA_UNROLL + for (int i=0; i < NumRegs; ++i) + { + int linear = index(i); + if (!Aligned && linear >= Size) + break; + + coord_t c = coord(linear); + data[i] = ptr[c.i*stride_i + c.j*stride_j]; + } } }; -template +template struct tile_shared_t { using Type = T; static constexpr int M = M_; static constexpr int N = N_; static constexpr int Size = M*N; + static constexpr int Alloc = Alloc_; static constexpr int StrideM = StrideM_; static constexpr int StrideN = StrideN_; + static constexpr bool Aligned = Size%WP_TILE_BLOCK_DIM == 0; + T* data = NULL; - inline CUDA_CALLABLE tile_shared_t() {} + // default initialization (non-initialized) + inline CUDA_CALLABLE tile_shared_t() + { + data = tile_alloc_shared(); + } + + // zero initialization, handles adj_tile = {0} syntax + inline CUDA_CALLABLE tile_shared_t(int nil) + { + data = tile_alloc_shared(); + zero(); + } + + // initialize from an existing tile's memory inline CUDA_CALLABLE tile_shared_t(T* smem) : data(smem) { + } + + // construct from a global tile + inline CUDA_CALLABLE tile_shared_t(tile_global_t& t) + { + copy_from_global(t.array, t.x, t.y); + } + + // assign from a global tile + inline CUDA_CALLABLE auto& operator=(const tile_global_t& t) + { + copy_from_global(t.data, t.x, t.y); + return *this; } + // assign from a constant value + inline CUDA_CALLABLE auto& operator=(const T& x) + { + // todo: make this subtile (stride aware) + for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) + data[i] = x; + + return *this; + } + + inline CUDA_CALLABLE T& operator()(int i, int j) { assert(i < M); @@ -247,45 +375,18 @@ struct tile_shared_t return (*this)(i,j); } - // in-place zero - inline CUDA_CALLABLE void zero() - { - // todo: make this subtile (stride aware) - for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) - data[i] = T(0); - } - - // compute linear tile index from a local register index - inline CUDA_CALLABLE int index(int reg) const - { - return threadIdx.x + reg*WP_TILE_BLOCK_DIM; - } - // compute tile coordinate from linear index inline CUDA_CALLABLE coord_t coord(int index) const { return {index/N, index%N}; } - // copy shared tile to register - inline CUDA_CALLABLE tile_register_t get() - { - tile_register_t out; - - WP_PRAGMA_UNROLL - for (int i=0; i < out.NumRegs; ++i) - { - const int linear = out.index(i); - - // handle case where tile size is not - // aligned to block dimensions - if (linear > Size) - break; - - out.data[i] = (*this)(linear); - } - - return out; + // in-place zero + inline CUDA_CALLABLE void zero() + { + // todo: make this subtile (stride aware) + for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) + data[i] = T(0); } // copy register tile to shared @@ -298,12 +399,10 @@ struct tile_shared_t // handle case where tile size is not // aligned to block dimensions - if (linear > Size) + if (!Aligned && linear >= Size) break; - // todo: should use coord here to handle cases where - // shared tile is a slice? - data[linear] = tile.data[i]; + (*this)(linear) = tile.data[i]; } } @@ -317,7 +416,7 @@ struct tile_shared_t printf("%*s[", i>0, ""); for (int j=0; j < N; ++j) { - printf("%5.2f ", data(i, j)); + printf("%5.2f ", (*this)(i, j)); } if (i == M-1) @@ -327,33 +426,91 @@ struct tile_shared_t } } } + + // copy shared tile to register + inline CUDA_CALLABLE tile_register_t copy_to_register() + { + tile_register_t out; + + WP_PRAGMA_UNROLL + for (int i=0; i < out.NumRegs; ++i) + { + const int linear = out.index(i); + + // handle case where tile size is not + // aligned to block dimensions + if (!Aligned && linear >= Size) + break; + + out(i) = (*this)(linear); + } + + return out; + } + + inline CUDA_CALLABLE void copy_to_global(array_t dest, int x, int y) + { + // todo: use TMA here + const int tile_i = x*M; + const int tile_j = y*N; + + // wp.array() indexing generates poor code due to char* casting + // here we unroll some of the ops, note this assumes byte strides are + // aligned to the element size + T* ptr = &wp::index(dest, tile_i, tile_j); + const int stride_i = dest.strides[0]/sizeof(T); + const int stride_j = dest.strides[1]/sizeof(T); + + WP_PRAGMA_UNROLL + for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM) + { + coord_t c = coord(i); + ptr[c.i*stride_i + c.j*stride_j] = (*this)(c.i, c.j); + } + } + + inline CUDA_CALLABLE void copy_from_global(const array_t& src, int x, int y) + { + // todo: use async pipelines or TMA here + const int tile_i = x*M; + const int tile_j = y*N; + + // wp.array() indexing generates poor code due to char* casting + // here we unroll some of the ops, note this assumes array byte strides are + // aligned to the element size + const T* ptr = &wp::index(src, tile_i, tile_j); + + assert(src.strides[0]%sizeof(T) == 0); + assert(src.strides[1]%sizeof(T) == 0); + + const int stride_i = src.strides[0]/sizeof(T); + const int stride_j = src.strides[1]/sizeof(T); + + WP_PRAGMA_UNROLL + for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM) + { + coord_t c = coord(i); + (*this)(c.i, c.j) = ptr[c.i*stride_i + c.j*stride_j]; + } + } }; template inline CUDA_CALLABLE auto tile_transpose(Tile& t) -{ +{ // alias incoming tile - return tile_shared_t(t.data); + return tile_shared_t(t.data); } //----------------------------------------------------------------------------------------------------- // High level entry points for each op (correspond to one Warp builtin) -template +template inline CUDA_CALLABLE auto tile_zeros() { - const int length = M*N; - - WP_TILE_SHARED __align__(16) T data[length]; - - WP_PRAGMA_UNROLL - for (int t=threadIdx.x; t < length; t += WP_TILE_BLOCK_DIM) - { - data[t] = T(0.0); - } - - return tile_shared_t(data); + // tile variable assignment operator will handle initialization + return T(0.0); } @@ -361,64 +518,20 @@ inline CUDA_CALLABLE auto tile_zeros() template inline CUDA_CALLABLE auto tile_load(array_t& src, int x, int y) { - const int length = M*N; - - WP_TILE_SHARED __align__(16) T data[length]; - - tile_shared_t dest(data); - - const int tile_i = x*M; - const int tile_j = y*N; - - // wp.array() indexing generates poor code due to char* casting - // here we unroll some of the ops, note this assumes byte strides are - // aligned to the element size - T* ptr = &index(src, tile_i, tile_j); - const int stride_i = src.strides[0]/sizeof(T); - const int stride_j = src.strides[1]/sizeof(T); - - WP_PRAGMA_UNROLL - for (int i=threadIdx.x; i < length; i += WP_TILE_BLOCK_DIM) - { - coord_t c = dest.coord(i); - dest.data[i] = ptr[c.i*stride_i + c.j*stride_j]; //index(src, tile_i + c.i, tile_j + c.j); - } - - return dest; + // just return a ref. to the global memory + // it will be loaded to shared or registers + // on assignment to the variable + return tile_global_t(src, x, y); } // entry point for store template inline CUDA_CALLABLE void tile_store(array_t& dest, int x, int y, Tile& src) { - auto src_reg = src.get(); - - const int tile_i = x*src.M; - const int tile_j = y*src.N; - - // wp.array() indexing generates poor code due to char* casting - // here we unroll some of the ops, note this assumes byte strides are - // aligned to the element size - T* ptr = &index(dest, tile_i, tile_j); - const int stride_i = dest.strides[0]/sizeof(T); - const int stride_j = dest.strides[1]/sizeof(T); - - WP_PRAGMA_UNROLL - for (int i=0; i < src_reg.NumRegs; ++i) - { - // handle case where tile size is not - // aligned to block dimensions - int index = src_reg.index(i); - if (index > src_reg.Size) - break; - - coord_t c = src_reg.coord(index); - ptr[c.i*stride_i + c.j*stride_j] = src_reg.data[i]; //index(dest, tile_i + c.i, tile_j + c.j); - } + // dispatch to tile type + src.copy_to_global(dest, x, y); } - - //------------------------------------- // Adjoints @@ -431,7 +544,7 @@ inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, // if (!src.grad) // return; - auto adj_reg = adj_ret.get(); + auto adj_reg = adj_ret.copy_to_register(); const int tile_i = x*adj_reg.M; const int tile_j = y*adj_reg.N; @@ -441,7 +554,7 @@ inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, for (int i=0; i < adj_reg.NumRegs; ++i) { int linear = adj_reg.index(i); - if (linear > adj_reg.Size) + if (!adj_reg.Aligned && linear >= adj_reg.Size) break; coord_t coord = adj_reg.coord(linear); @@ -449,7 +562,7 @@ inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, auto grad = adj_reg.data[i]; if (adj_src.data) - adj_atomic_add(&index(adj_src, tile_i + coord.i, tile_j + coord.j), grad); + adj_atomic_add(&index(adj_src, tile_i + coord.i, tile_j + coord.j), grad); else if (src.grad) adj_atomic_add(&index_grad(src, tile_i + coord.i, tile_j + coord.j), grad); } @@ -462,7 +575,7 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t // return; // convert to register if necessary - auto adj_reg = adj_t.get(); + auto adj_reg = adj_t.copy_to_register(); const int tile_i = x*adj_reg.M; const int tile_j = y*adj_reg.N; @@ -472,13 +585,13 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t for (int i=0; i < adj_reg.NumRegs; ++i) { int linear = adj_reg.index(i); - if (linear > adj_reg.Size) + if (!adj_reg.Aligned && linear >= adj_reg.Size) break; coord_t coord = adj_reg.coord(linear); if (adj_dest.data) - adj_reg.data[i] += index(adj_dest, tile_i + coord.i, tile_j + coord.j); + adj_reg.data[i] += index(adj_dest, tile_i + coord.i, tile_j + coord.j); else if (dest.grad) adj_reg.data[i] += index_grad(dest, tile_i + coord.i, tile_j + coord.j); } @@ -493,7 +606,7 @@ inline CUDA_CALLABLE auto tile_map(Fwd op, Tile &a) { auto out = tile_register_t(); - auto a_reg = a.get(); + auto a_reg = a.copy_to_register(); WP_PRAGMA_UNROLL for (int i=0; i < out.NumRegs; ++i) @@ -511,9 +624,9 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op, Tile& adj_a, AdjTile& adj_ret) { - auto a_reg = a.get(); - auto adj_a_reg = adj_a.get(); - auto adj_ret_reg = adj_ret.get(); + auto a_reg = a.copy_to_register(); + auto adj_a_reg = adj_a.copy_to_register(); + auto adj_ret_reg = adj_ret.copy_to_register(); WP_PRAGMA_UNROLL for (int i=0; i < a_reg.NumRegs; ++i) @@ -533,8 +646,8 @@ inline CUDA_CALLABLE auto tile_map(Fwd op, { auto out = tile_register_t(); - auto a_reg = a.get(); - auto b_reg = b.get(); + auto a_reg = a.copy_to_register(); + auto b_reg = b.copy_to_register(); WP_PRAGMA_UNROLL for (int i=0; i < out.NumRegs; ++i) @@ -552,11 +665,11 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op, TileB &adj_b, AdjTile &adj_ret) { - auto a_reg = a.get(); - auto b_reg = b.get(); - auto adj_a_reg = adj_a.get(); - auto adj_b_reg = adj_b.get(); - auto adj_ret_reg = adj_ret.get(); + auto a_reg = a.copy_to_register(); + auto b_reg = b.copy_to_register(); + auto adj_a_reg = adj_a.copy_to_register(); + auto adj_b_reg = adj_b.copy_to_register(); + auto adj_ret_reg = adj_ret.copy_to_register(); WP_PRAGMA_UNROLL for (int i=0; i < a_reg.NumRegs; ++i) diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index faf807ad..1ca668d3 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -321,11 +321,11 @@ void tile_matmul(TileA& a, TileB& b, TileC& c) } -template +template void adj_tile_matmul(TileA& a, TileB& b, TileC& c, - TileA& adj_a, TileB& adj_b, TileC& adj_c) + AdjTileA& adj_a, AdjTileB& adj_b, AdjTileC& adj_c) { - tile_matmul_scalar(adj_c, wp::tile_transpose(b), adj_a); tile_matmul_scalar(wp::tile_transpose(a), adj_c, adj_b); } diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index e52e0b10..9f9b079b 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -12,8 +12,8 @@ wp.build.clear_kernel_cache() -TILE_M = wp.constant(32) -TILE_N = wp.constant(16) +TILE_M = wp.constant(8) +TILE_N = wp.constant(4) TILE_K = wp.constant(8) # num threads per-tile @@ -154,8 +154,8 @@ def test_tile_binary_map(): C_wp.grad = wp.ones_like(C_wp) tape.backward() - assert(np.allclose(A_wp.grad.numpy(), A_grad)) - assert(np.allclose(B_wp.grad.numpy(), B_grad)) + assert(np.allclose(A_wp.grad.numpy(), A_grad, rtol=1.e-2)) + assert(np.allclose(B_wp.grad.numpy(), B_grad, rtol=1.e-2)) print("Binary map backward passed") @@ -235,8 +235,8 @@ def tile_gemm(A: wp.array2d(dtype=float), def test_tile_gemm(): M = TILE_M*7 - K = TILE_K*5 - N = TILE_N*2 + K = TILE_K*6 + N = TILE_N*5 rng = np.random.default_rng(42) A = rng.random((M, K), dtype=np.float32) diff --git a/warp/types.py b/warp/types.py index 9bc6f7d7..f990a49e 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2881,13 +2881,16 @@ def ctype(self): if self.storage == "register": return f"wp::tile_register_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" elif self.storage == "shared": - return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + + # every shared memory tile will create a new static shared memory allocation + # this just needs to be a unique-id for templated allocation functions + return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>" # generate a unique allocation index for shared memory @classmethod def alloc(cls): - index = cls.allocation - cls.allocation += 1 + index = Tile.allocation + Tile.allocation += 1 return index class TileZeros(Tile): @@ -2905,7 +2908,7 @@ def __init__(self, dtype, M, N): class TileLoad(Tile): def __init__(self, array, M, N): - Tile.__init__(self, array.dtype, M, N, op="load", storage="shared") + Tile.__init__(self, array.dtype, M, N, op="load", storage="register") class TileUnaryMap(Tile): From f31bef61c3d1ac4e93defa86d7a6c1af2529738b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 16 Sep 2024 15:59:02 +1200 Subject: [PATCH 024/102] Implement operator backward pass --- warp/native/tile.h | 72 ++++++------------------ warp/tests/test_tile.py | 118 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 60 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index 5df1e670..c3d0e965 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -690,7 +690,7 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op, #define tile_binary_map(op, a, b) tile_map([](auto x, auto y) { return op(x, y);}, a, b) #define adj_tile_binary_map(op, a, b, adj_op, adj_a, adj_b, adj_ret) adj_tile_map([](auto x, auto y) { return op(x, y);}, a, b, [](auto x, auto y, auto& adj_x, auto& adj_y, auto adj_ret) { adj_op(x, y, adj_x, adj_y, adj_ret);}, adj_a, adj_b, adj_ret) -// unary neg +// -tile (unary neg) template inline CUDA_CALLABLE auto tile_neg(Tile& a) { return tile_unary_map(wp::neg, a); } @@ -698,51 +698,7 @@ template inline CUDA_CALLABLE void adj_tile_neg(Tile& a, Tile& adj_a, AdjTile& adj_ret) { adj_tile_unary_map(wp::neg, a, wp::adj_neg, adj_a, adj_ret); } -/* -// handle tile*scalar -template -CUDA_CALLABLE inline auto tile_mul_impl(Tile& t, typename Tile::Type s, - Tile& adj_t, typename Tile::Type adj_s) -{ - typedef typename Tile::Type T; - typedef tile_constant_t Constant; - - typedef tile_binary_map_t Op; - - typename Op::FwdOp fwd = [](T a, T b) { return mul(a, b); }; - typename Op::AdjOp adj = [](T a, T b, T& adj_a, T& adj_b, T& adj_ret) { adj_mul(a, b, adj_a, adj_b, adj_ret); }; - - // promote scalar to constant tile - Constant c(s, adj_s); - - return Op(t, c, fwd, adj); -} - -// handle scalar*tile -template -CUDA_CALLABLE inline auto tile_mul_impl(typename Tile::Type s, Tile& t, - typename Tile::Type adj_s, Tile& adj_t) -{ - typedef typename Tile::Type T; - typedef tile_constant_t Constant; - - typedef tile_binary_map_t Op; - - typename Op::FwdOp fwd = [](T a, T b) { return mul(a, b); }; - typename Op::AdjOp adj = [](T a, T b, T& adj_a, T& adj_b, T& adj_ret) { adj_mul(a, b, adj_a, adj_b, adj_ret); }; - - // promote scalar to constant tile - Constant c(s, adj_s); - - return Op(c, t, fwd, adj); - -} - - -#define tile_mul(a, b) tile_mul_impl(a, b adj_##a, adj_##b) -#define tile_add(a, b) tile_add_impl(a, b adj_##a, adj_##b) -*/ - +// tile + tile template inline CUDA_CALLABLE auto tile_add(TileA& a, TileB& b) { @@ -770,13 +726,15 @@ inline CUDA_CALLABLE void adj_tile_mul(Tile& a, const typename Tile::Type& s, Tile& adj_a, typename Tile::Type& adj_s, AdjTile& adj_c) { - // auto s_tile = tile_register_t(s); - // auto adj_s_tile = tile_register_t(); + auto s_tile = tile_register_t(s); + auto adj_s_tile = tile_register_t(); - // adj_tile_binary_map(mul, a, s_tile, adj_mul, adj_a, adj_s_tile, adj_c); + adj_tile_binary_map(mul, a, s_tile, adj_mul, adj_a, adj_s_tile, adj_c); - // todo: sum up contribution from all adj_s_tile onto original scalar - //adj_tile_sum() + for (int i=0; i < adj_s_tile.NumRegs; ++i) + { + adj_s += adj_s_tile.data[i]; + } } @@ -795,13 +753,15 @@ inline CUDA_CALLABLE void adj_tile_mul(const typename Tile::Type& s, Tile& a, typename Tile::Type& adj_s, Tile& adj_a, AdjTile& adj_c) { - // auto s_tile = tile_register_t(s); - // auto adj_s_tile = tile_register_t(); + auto s_tile = tile_register_t(s); + auto adj_s_tile = tile_register_t(); - // adj_tile_binary_map(mul, a, s_tile, adj_mul, adj_a, adj_s_tile, adj_c); + adj_tile_binary_map(mul, s_tile, a, adj_mul, adj_s_tile, adj_a, adj_c); - // todo: sum up contribution from all adj_s_tile onto original scalar - //adj_tile_sum() + for (int i=0; i < adj_s_tile.NumRegs; ++i) + { + adj_s += adj_s_tile.data[i]; + } } diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 9f9b079b..90bd3d66 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -301,15 +301,24 @@ def test_tile_operators(): input = rng.random((batch_count, M, N), dtype=np.float32) output = input*0.75 - input_wp = wp.array(input) - output_wp = wp.zeros_like(input_wp) + input_wp = wp.array(input, requires_grad=True) + output_wp = wp.zeros_like(input_wp, requires_grad=True) - wp.launch(tile_operators, dim=batch_count, inputs=[input_wp, output_wp], tile_size=TILE_DIM) + with wp.Tape() as tape: + wp.launch(tile_operators, dim=batch_count, inputs=[input_wp, output_wp], tile_size=TILE_DIM) assert(np.allclose(output, output_wp.numpy(), rtol=1.e-4)) print("operators forward passed") + output_wp.grad.fill_(1.0) + + tape.backward() + + assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.75, rtol=1.e-4)) + + print("operators backward passed") + test_tile_copy() @@ -317,4 +326,105 @@ def test_tile_operators(): test_tile_binary_map() test_tile_batched_gemm() test_tile_gemm() -test_tile_operators() \ No newline at end of file +test_tile_operators() + + +# #----------------------------------------- +# # center of mass computation + +# start = offset[i] +# end = offset[i+1] + +# com = wp.tile_zeros(dtype=wp.vec3, M=1) + +# # load chunks of indices +# for i in range(start, end, N): + +# count = wp.min(N, end-i) + +# idx = wp.tile_load(indices, i, N, max_col=count) +# p = wp.tile_load(points, idx, max_col=count) + +# com += wp.tile_sum(p) + + +# wp.tile_store(out[i], com) + + + +# #------------------------------------------- +# # compute deformation gradient + +# i = +# j = +# k = +# l = + +# f = wp.tile(F) # generate a block size tile of feature vectors + +# # layer 1 +# w1 = wp.tile_load(weights) +# b1 = wp.tile_load(bias) + +# z = wp.tile_matmul(w1, f) + b1 +# z = wp.tile_map(relu, z) + +# # layer 2 +# w2 = wp.tile_load(weights) +# b2 = wp.tile_load(bias) + +# z = wp.tile_matmul(w2, z) + b2 +# z = wp.tile_map(relu, z) + +# o = wp.untile(f) + + +# #---------------------------------- +# # MLP with helper function for linear layers +# # where shape is only partially known +# # at compile time, and the other dims +# # are inferred from the input vector + +# f = wp.tile(F) + +# z = wp.tile_linear(weights1, bias1, f, hidden=16) +# z = wp.tile_map(relu, z) + +# z = wp.tile_linear(weights2, bias2, f, hidden=8) +# z = wp.tile_map(relu, z) + +# z = wp.tile_linear(weights3, bias3, f, hidden=4) +# z = wp.tile_map(relu, z) + +# o = wp.untile(z) + + + +# #---------------------------------- +# # softmax + +# def softmax(z: Any): + +# e = wp.tile_map(wp.exp, z) +# s = wp.tile_sum(e, dim=0) + +# return z/s[0] + + + + + + + + + + + + + + + + + + + From 5e18a5f964515ec0caf02a61e1e6cf33a0e245f2 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 17 Sep 2024 15:59:53 +1200 Subject: [PATCH 025/102] Added `wp.tile_sum()` for whole tile reductions --- warp/builtins.py | 43 +++++++++---- warp/native/builtin.h | 1 + warp/native/tile_reduce.h | 129 ++++++++++++++++++++++++++++++++++++++ warp/tests/test_tile.py | 46 ++++++++++++++ 4 files changed, 206 insertions(+), 13 deletions(-) create mode 100644 warp/native/tile_reduce.h diff --git a/warp/builtins.py b/warp/builtins.py index 1757d469..e10204e5 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1880,19 +1880,7 @@ def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a b.type.storage = "shared" out.type.storage = "shared" - # template_args.append(dtype) - # template_args.append(m) - # template_args.append(n) - - # global shared_memory_id - template_args = [] - # template_args.append(shared_memory_id) - - # # matmul makes two allocations (one for each of its arguments) - # shared_memory_id += 1 - # shared_memory_id += 1 - return ((a, b, out), template_args) @@ -1902,11 +1890,40 @@ def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a value_func=tile_matmul_value_func, dispatch_func=tile_matmul_dispatch_func, variadic=True, - doc="Compute matrix product and accumulate out += a*b, a and b will be realized before evaluation, and output must already be realized.", + doc="Compute matrix product and accumulate out += a*b.", + group="Tile Primitives", + export=False, +) + +def tile_sum_value_func(arg_types, arg_values): + + # return generic type (for doc builds) + if arg_types is None: + return None + + if len(arg_types) != 2: + raise RuntimeError("tile_sum() requires 2 positional args") + + a = arg_types["a"] + + if not is_tile(a): + raise RuntimeError("tile_sum() argument 0 must be a tile") + + return Tile(dtype=a.dtype, M=1, N=1, op="sum") + + +add_builtin( + "tile_sum", + input_types={"a": Tile, "axis": Any}, + value_func=tile_sum_value_func, + variadic=True, + doc="Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored", group="Tile Primitives", export=False, ) + + def tile_eval_value_func(arg_types, arg_values): # return generic type (for doc builds) diff --git a/warp/native/builtin.h b/warp/native/builtin.h index a899d9a7..8409b810 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1591,4 +1591,5 @@ inline CUDA_CALLABLE void adj_expect_near(const vec3& actual, const vec3& expect #if defined(__CUDACC_RTC__) #include "tile.h" #include "tile_gemm.h" +#include "tile_reduce.h" #endif \ No newline at end of file diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h new file mode 100644 index 00000000..f9cfd23d --- /dev/null +++ b/warp/native/tile_reduce.h @@ -0,0 +1,129 @@ +#pragma once + +#include "tile.h" + +#define WP_TILE_WARP_SIZE 32 + +namespace wp +{ + +template +inline CUDA_CALLABLE T warp_shuffle_down(T val, int offset) +{ + typedef unsigned int Word; + + union + { + T output; + Word output_storage; + }; + + union + { + T input; + Word input_storage; + }; + + input = val; + + Word* dest = reinterpret_cast(&output); + Word* src = reinterpret_cast(&input); + + unsigned int shuffle_word; + unsigned int mask = 0xffffffff; + + constexpr int word_count = (sizeof(T) + sizeof(Word) - 1) / sizeof(Word); + + WP_PRAGMA_UNROLL + for (int i=0; i < word_count; ++i) + { + shuffle_word = __shfl_down_sync(mask, src[i], offset, WP_TILE_WARP_SIZE); + dest[i] = shuffle_word; + } + + return output; +} + +template +inline CUDA_CALLABLE T warp_reduce(T val) +{ + T sum = val; + + for (int offset=WP_TILE_WARP_SIZE/2; offset > 0; offset /= 2) + { + sum += warp_shuffle_down(sum, offset); + } + + return sum; +} + + +// non-axis version which computes sum +// across the entire tile using the whole block +template +auto tile_sum(Tile& t, int axis) +{ + using T = typename Tile::Type; + + auto input = t.copy_to_register(); + auto output = tile_register_t(); + + const int warp_count = (WP_TILE_BLOCK_DIM + WP_TILE_WARP_SIZE - 1)/WP_TILE_WARP_SIZE; + const int warp_index = threadIdx.x/WP_TILE_WARP_SIZE; + const int lane_index = threadIdx.x%WP_TILE_WARP_SIZE; + + T thread_sum = input.data[0]; + + // thread reduction + WP_PRAGMA_UNROLL + for (int i=1; i < input.NumRegs; ++i) + thread_sum += input.data[i]; + + // warp reduction + T warp_sum = warp_reduce(thread_sum); + + // fixed size scratch pad for partial results + __shared__ T partials[warp_count]; + + if (lane_index == 0) + { + partials[warp_index] = warp_sum; + } + + __syncthreads(); + + // reduce across block, todo: use warp_reduce() here + if (threadIdx.x == 0) + { + T block_sum = partials[0]; + + WP_PRAGMA_UNROLL + for (int i=1; i < warp_count; ++i) + block_sum += partials[i]; + + output.data[0] = block_sum; + } + + return output; +} + +template +void adj_tile_sum(Tile& t, int axis, Tile& adj_t, int adj_axis, AdjTile& adj_ret) +{ + using T = typename Tile::Type; + + // broadcast incoming adjoint to block + __shared__ T scratch; + if (threadIdx.x == 0) + scratch = adj_ret.data[0]; + + __syncthreads(); + + auto adj_t_reg = adj_t.copy_to_register(); + auto adj_ret_reg = tile_shared_t(&scratch).copy_to_register(); + + adj_t.assign(tile_add(adj_t_reg, adj_ret_reg)); +} + + +} // namespace wp \ No newline at end of file diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 90bd3d66..f6aa9188 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -320,6 +320,51 @@ def test_tile_operators(): print("operators backward passed") +@wp.kernel +def tile_sum_kernel(input: wp.array3d(dtype=float), + output: wp.array(dtype=float)): + + # output tile index + i = wp.tid() + + a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) + s = wp.tile_sum(a, axis=-1)*0.5 + wp.tile_store(output, i, 0, s) + +def test_tile_sum(): + + batch_count = 2 + + M = TILE_M + N = TILE_N + + rng = np.random.default_rng(42) + input = rng.random((batch_count, M, N), dtype=np.float32) + + input_wp = wp.array(input, requires_grad=True) + output_wp = wp.zeros(batch_count, requires_grad=True) + + with wp.Tape() as tape: + wp.launch(tile_sum_kernel, dim=batch_count, inputs=[input_wp, output_wp], tile_size=TILE_DIM) + + + for i in range(batch_count): + sum_np = np.sum(input[i])*0.5 + sum_wp = output_wp.numpy()[i] + + assert(np.allclose(sum_np, sum_wp, rtol=1.e-4)) + + print("Sum forward passed") + + output_wp.grad.fill_(1.0) + + tape.backward() + + assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.5, rtol=1.e-4)) + + print("Sum backward passed") + + test_tile_copy() test_tile_unary_map() @@ -327,6 +372,7 @@ def test_tile_operators(): test_tile_batched_gemm() test_tile_gemm() test_tile_operators() +test_tile_sum() # #----------------------------------------- From 670445d73326101b5b508aaed14f799f77ccaca0 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 19 Sep 2024 15:54:39 +1200 Subject: [PATCH 026/102] Add support for extracting tile elements to regular Warp types, remove axis parameter from tile_sum() --- warp/builtins.py | 119 +++++++++---------------- warp/codegen.py | 10 ++- warp/native/tile.h | 181 ++++++++++++++++++++------------------ warp/native/tile_reduce.h | 97 ++++++++++++++++++-- warp/tests/test_tile.py | 62 ++++++++++--- 5 files changed, 286 insertions(+), 183 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index e10204e5..077d6cd0 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1844,6 +1844,31 @@ def tile_store_value_func(arg_types, arg_values): ) +def tile_extract_value_func(arg_types, arg_values): + + # return generic type (for doc builds) + if arg_types is None: + return None + + if len(arg_types) != 3: + raise RuntimeError("tile_extract() requires 3 positional args") + + if not is_tile(arg_types["a"]): + raise RuntimeError("tile_extract() argument 0 must be a tile") + + return arg_types["a"].dtype + + +add_builtin( + "tile_extract", + input_types={"a": Tile(dtype=Any, M=Any, N=Any), "i": int, "j": int}, + value_func=tile_extract_value_func, + variadic=True, + doc="Extract element at index (i, j) of the tile and return the native type", + group="Tile Primitives", + export=False, +) + def tile_matmul_value_func(arg_types, arg_values): @@ -1901,8 +1926,8 @@ def tile_sum_value_func(arg_types, arg_values): if arg_types is None: return None - if len(arg_types) != 2: - raise RuntimeError("tile_sum() requires 2 positional args") + if len(arg_types) != 1: + raise RuntimeError("tile_sum() requires 1 positional args") a = arg_types["a"] @@ -1914,7 +1939,7 @@ def tile_sum_value_func(arg_types, arg_values): add_builtin( "tile_sum", - input_types={"a": Tile, "axis": Any}, + input_types={"a": Tile}, value_func=tile_sum_value_func, variadic=True, doc="Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored", @@ -1924,43 +1949,6 @@ def tile_sum_value_func(arg_types, arg_values): -def tile_eval_value_func(arg_types, arg_values): - - # return generic type (for doc builds) - if arg_types is None: - return None - - if not is_tile(arg_types["t"]): - raise RuntimeError("tile_eval() argument must be a tile") - - return TileShared(arg_types["t"]) - -def tile_eval_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): - - t = arg_values["t"] - - global shared_memory_id - - template_args = [] - template_args.append(shared_memory_id) - - # matmul makes two allocations (one for each of its arguments) - shared_memory_id += 1 - - return ((t,), template_args) - -add_builtin( - "tile_eval", - input_types={"t": Tile}, - value_func=tile_eval_value_func, - dispatch_func=tile_eval_dispatch_func, - variadic=True, - doc="Force evaluation of a tile expression into shared memory", - group="Tile Primitives", - export=False, -) - - # does type propagation for load() def tile_unary_map_value_func(arg_types, arg_values): @@ -2034,17 +2022,6 @@ def tile_binary_map_value_func(arg_types, arg_values): export=False, ) -add_builtin( - "add", - input_types={"a": Tile(dtype=Any, M=Any, N=Any), "b": Tile(dtype=Any, M=Any, N=Any)}, - value_func=tile_binary_map_value_func, - #dispatch_func=tile_map_dispatch_func, - #variadic=True, - native_func="tile_add", - doc="Add each element of two tiles together", - group="Tile Primitives", - export=False, -) # --------------------------------- # Linear Algebra @@ -4538,35 +4515,33 @@ def tile_scalar_mul_value_func(arg_types, arg_values): return TileBinaryMap(TileConstant(x, y.M, y.N), y) - -# def tile_binary_value_func(arg_types, arg_values): - -# if arg_types is None: -# return Tile(dtype=Any, M=Any, N=Any) - -# a = arg_types[0] - - -# if not is_tile(t): -# raise RuntimeError("Expected tile for unary expression") - -# return TileUnaryMap(t.dtype, t.M, t.N) - add_builtin( "neg", input_types={"x": Tile(dtype=Any, M=Any, N=Any)}, value_func=tile_unary_value_func, - doc="", + doc="Negate each element of a tile", export=False, native_func="tile_neg", group="Operators", ) +add_builtin( + "add", + input_types={"a": Tile(dtype=Any, M=Any, N=Any), "b": Tile(dtype=Any, M=Any, N=Any)}, + value_func=tile_binary_map_value_func, + #dispatch_func=tile_map_dispatch_func, + #variadic=True, + native_func="tile_add", + doc="Add each element of two tiles together", + group="Tile Primitives", + export=False, +) + add_builtin( "mul", input_types={"x": Tile(dtype=Any, M=Any, N=Any), "y": Scalar}, value_func=tile_scalar_mul_value_func, - doc="", + doc="Multiply each element of a tile by a scalar", export=False, native_func="tile_mul", group="Operators", @@ -4576,18 +4551,10 @@ def tile_scalar_mul_value_func(arg_types, arg_values): "mul", input_types={"x": Scalar, "y": Tile(dtype=Any, M=Any, N=Any)}, value_func=tile_scalar_mul_value_func, - doc="", + doc="Multiply each element of a tile by a scalar", export=False, native_func="tile_mul", group="Operators", ) -# add_builtin( -# "mul", -# input_types={"x": Tile, "s": Scalar}, -# value_func=tile_binary_value_func, -# doc="", -# group="Operators", -# ) - diff --git a/warp/codegen.py b/warp/codegen.py index 93997b07..941a8f4b 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -2187,8 +2187,8 @@ def emit_Subscript(adj, node): return var target, indices = adj.eval_subscript(node) - target_type = strip_reference(target.type) + if is_array(target_type): if len(indices) == target_type.ndim: # handles array loads (where each dimension has an index specified) @@ -2209,6 +2209,14 @@ def emit_Subscript(adj, node): out.is_read = target.is_read out.is_write = target.is_write + elif is_tile(target_type): + if len(indices) == 2: + # handles extracting a single element from a tile + out = adj.add_builtin_call("tile_extract", [target, *indices]) + else: + # handles tile views + out = adj.add_builtin_call("tile_view", [target, *indices]) + else: # handles non-array type indexing, e.g: vec3, mat33, etc out = adj.add_builtin_call("extract", [target, *indices]) diff --git a/warp/native/tile.h b/warp/native/tile.h index c3d0e965..3f3845c9 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -31,20 +31,42 @@ /* Tile Expressions -[x] Forward / Backward code-gen -[ ] wp.tile_map() +[ ] Tiles + [x] Register, Shared, Global + [ ] Layouts + [x] Simple + [ ] Cute + [ ] Remove Alloc type from tile_shared_t + +[ ] Load/Store + [ ] 1D load/store variants + [ ] max_coord option for non-aligned loads + [ ] Indexed load + [ ] wp.tile_atomic_add() +[ ] Maps [x] Support user functions [x] Support built-in functions [ ] Support for lambda functions [ ] Infer tile_map() output from operator type (e.g.: dot for each element) -[x] wp.tile_matmul() +[ ] Reductions + [x] Sum + [x] Forward + [x] Reverse + [ ] Min + [ ] Max + [ ] Custom +[x] MatMul [x] Forward [x] Reverse -[ ] wp.tile_atomic_add() -[ ] Support for n-d shape tiles / broadcasting / slicing / transpose? -[x] Compile-time block dimensions -[ ] Support for CUB reductions -[ ] Support for CUB sorts +[ ] Reshape + [ ] Broadcasting + [ ] Transpose + [x] Shared + [ ] Register + [ ] Slice +[ ] Runtime + [x] Compile-time block dimensions + [ ] Switch between SIMT / Tile based execution if `tile_dim` not provided to wp.launch() [ ] Examples [ ] GEMM [ ] Batched MLP @@ -216,6 +238,44 @@ struct tile_register_t data[i] = tile.data[i]; } + // extract a single tile element to a native type + inline CUDA_CALLABLE Type extract(int i, int j) + { + // map from logical coords (i, j) -> (thread, reg) + const int linear = i*N + j; + + const int thread = linear/NumRegs; + const int reg = linear%NumRegs; + + WP_TILE_SHARED Type scratch; + + if (threadIdx.x == thread) + { + scratch = data[reg]; + } + + WP_TILE_SYNC(); + + return scratch; + } + + + // backward version of scalar extract + inline CUDA_CALLABLE void adj_extract(int i, int j, Type adj_ret) + { + // map from logical coords (i, j) -> (thread, reg) + const int linear = i*N + j; + + const int thread = linear/NumRegs; + const int reg = linear%NumRegs; + + if (threadIdx.x == thread) + { + data[reg] += adj_ret; + } + } + + // return the in-register version of this tile (nop) inline CUDA_CALLABLE auto& copy_to_register() { return *this; } @@ -389,6 +449,20 @@ struct tile_shared_t data[i] = T(0); } + // extract a single tile element to a native type + inline CUDA_CALLABLE Type extract(int i, int j) + { + return (*this)(i, j); + } + + // backward of scalar extraction + inline CUDA_CALLABLE void adj_extract(int i, int j, Type adj_ret) + { + if (threadIdx.x == 0) + (*this)(i, j) += adj_ret; + } + + // copy register tile to shared inline CUDA_CALLABLE void assign(const tile_register_t& tile) { @@ -765,92 +839,25 @@ inline CUDA_CALLABLE void adj_tile_mul(const typename Tile::Type& s, Tile& a, } -} // namespace wp - -#if 0 - -//----------------------------------------------------- -// c = a + b - -// forward -auto var_0 = wp::tile_load(var_A, x, y); -auto var_1 = wp::tile_load(var_B, x, y); -auto var_2 = wp::tile_add(var_0, var_1); -wp::tile_store(var_C, x, y, var_2) - -// reverse -wp::adj_store(var_C, x, y, var_2, adj_C, _, _, adj_2) -wp::adj_tile_add(var_0, var_1, adj_0, adj_1, adj_2) -wp::adj_tile_load(var_B, x, y, adj_B, _, _, adj_1); -wp::adj_tile_load(var_B, x, y, adj_B, _, _, adj_0); - -//----------------------------------------------------- -// x = a[0] -// c = x*2.0 + x - -// forward -auto var_0 = wp::tile_load(var_A, x, y); -auto var_1 = wp::tile_mul(var_0, 2.0); -auto var_2 = wp::tile_add(var_0, var_1); -wp::tile_store(var_C, x, y, var_2) - -struct adj_store_t -{ - adj_store_t() - { - - } - - float bwd(int i, float adj_ret) - { - return array.grad[i]; - } -}; - -template -struct adj_add_t +template +typename Tile::Type tile_extract(Tile& t, int i, int j) { - adj_add_t(P& parent) - { - - } - - float bwd(int i, float& adj_a, float& adj_b) - { - // evaluate parent - float adj_ret = parent.bwd(i); + assert(i < Tile::M); + assert(j < Tile::N); - adj_a += adj_ret; - adj_b += adj_ret; - } -}; + return t.extract(i, j); +} -template -struct adj_tile +template +void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_j, typename Tile::Type adj_ret) { - adj_tile(T& parent) - { - - } + assert(i < Tile::M); + assert(j < Tile::N); - - -}; - -void adj_tile_load(A, x, y, adj_A, adj_x, adj_y, adj_ret) -{ - for i in A(x,y): - adj_A[i] += adj_ret(i); + adj_t.adj_extract(i, j, adj_ret); } +} // namespace wp -// reverse -wp::adj_store(var_C, x, y, var_2, adj_C, _, _, adj_2) // adj_2->adj_C -wp::adj_tile_add(var_0, var_1, adj_0, adj_1, adj_2) // adj_0->adj_2->adj_C, adj_1->adj_2->adj_C -wp::adj_tile_mul(var_0, 2.0, adj_0, _, adj_1); // adj_0->adj_1->adj_2->adj_C -wp::adj_tile_load(var_A, x, y, adj_A, _, _, adj_0); // adj_A->adj_0->adj_1->adj_2->adj_C - - -#endif \ No newline at end of file diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index f9cfd23d..5a5b4d81 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -45,7 +45,7 @@ inline CUDA_CALLABLE T warp_shuffle_down(T val, int offset) } template -inline CUDA_CALLABLE T warp_reduce(T val) +inline CUDA_CALLABLE T warp_reduce_sum(T val) { T sum = val; @@ -57,11 +57,24 @@ inline CUDA_CALLABLE T warp_reduce(T val) return sum; } +template +inline CUDA_CALLABLE T warp_reduce(T val, Op op) +{ + T sum = val; + + for (int offset=WP_TILE_WARP_SIZE/2; offset > 0; offset /= 2) + { + sum = op(sum, warp_shuffle_down(sum, offset)); + } + + return sum; +} + // non-axis version which computes sum // across the entire tile using the whole block template -auto tile_sum(Tile& t, int axis) +auto tile_sum(Tile& t) { using T = typename Tile::Type; @@ -80,17 +93,18 @@ auto tile_sum(Tile& t, int axis) thread_sum += input.data[i]; // warp reduction - T warp_sum = warp_reduce(thread_sum); + T warp_sum = warp_reduce_sum(thread_sum); - // fixed size scratch pad for partial results - __shared__ T partials[warp_count]; + // fixed size scratch pad for partial results in shared memory + WP_TILE_SHARED T partials[warp_count]; if (lane_index == 0) { partials[warp_index] = warp_sum; } - __syncthreads(); + // ensure partials are ready + WP_TILE_SYNC(); // reduce across block, todo: use warp_reduce() here if (threadIdx.x == 0) @@ -108,16 +122,16 @@ auto tile_sum(Tile& t, int axis) } template -void adj_tile_sum(Tile& t, int axis, Tile& adj_t, int adj_axis, AdjTile& adj_ret) +void adj_tile_sum(Tile& t, Tile& adj_t, AdjTile& adj_ret) { using T = typename Tile::Type; // broadcast incoming adjoint to block - __shared__ T scratch; + WP_TILE_SHARED T scratch; if (threadIdx.x == 0) scratch = adj_ret.data[0]; - __syncthreads(); + WP_TILE_SYNC(); auto adj_t_reg = adj_t.copy_to_register(); auto adj_ret_reg = tile_shared_t(&scratch).copy_to_register(); @@ -126,4 +140,69 @@ void adj_tile_sum(Tile& t, int axis, Tile& adj_t, int adj_axis, AdjTile& adj_ret } +template +auto tile_reduce(Fwd op, Tile& t, int axis) +{ + using T = typename Tile::Type; + + auto input = t.copy_to_register(); + auto output = tile_register_t(); + + const int warp_count = (WP_TILE_BLOCK_DIM + WP_TILE_WARP_SIZE - 1)/WP_TILE_WARP_SIZE; + const int warp_index = threadIdx.x/WP_TILE_WARP_SIZE; + const int lane_index = threadIdx.x%WP_TILE_WARP_SIZE; + + T thread_sum = input.data[0]; + + // thread reduction + WP_PRAGMA_UNROLL + for (int i=1; i < input.NumRegs; ++i) + thread_sum = op(thread_sum, input.data[i]); + + // warp reduction + T warp_sum = warp_reduce(thread_sum, op); + + // fixed size scratch pad for partial results + WP_TILE_SHARED T partials[warp_count]; + + if (lane_index == 0) + { + partials[warp_index] = warp_sum; + } + + WP_TILE_SYNC(); + + // reduce across block, todo: use warp_reduce() here + if (threadIdx.x == 0) + { + T block_sum = partials[0]; + + WP_PRAGMA_UNROLL + for (int i=1; i < warp_count; ++i) + block_sum = op(block_sum, partials[i]); + + output.data[0] = block_sum; + } + + return output; +} + +template +void adj_tile_reduce(Tile& t, int axis, Tile& adj_t, int adj_axis, AdjTile& adj_ret) +{ + using T = typename Tile::Type; + + // broadcast incoming adjoint to block + WP_TILE_SHARED T scratch; + if (threadIdx.x == 0) + scratch = adj_ret.data[0]; + + WP_TILE_SYNC(); + + auto adj_t_reg = adj_t.copy_to_register(); + auto adj_ret_reg = tile_shared_t(&scratch).copy_to_register(); + + adj_t.assign(tile_add(adj_t_reg, adj_ret_reg)); +} + } // namespace wp \ No newline at end of file diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index f6aa9188..3153ac1b 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -178,7 +178,7 @@ def tile_grouped_gemm(A: wp.array3d(dtype=float), wp.tile_store(C[i], 0, 0, sum) -def test_tile_batched_gemm(): +def test_tile_grouped_gemm(): batch_count = 56 @@ -202,7 +202,7 @@ def test_tile_batched_gemm(): C_host = C_wp.numpy() # GEMM forward passed - print("batched matmul forward passed") + print("Batched matmul forward passed") @wp.kernel @@ -253,7 +253,7 @@ def test_tile_gemm(): assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) # GEMM forward passed - print("matmul forward passed") + print("Tiled matmul forward passed") adj_C = np.ones_like(C) @@ -262,7 +262,7 @@ def test_tile_gemm(): assert(np.allclose(adj_C@B.T, A_wp.grad.numpy(), rtol=1.e-4)) assert(np.allclose(A.T@adj_C, B_wp.grad.numpy(), rtol=1.e-4)) - print("matmul backward passed") + print("Tiled matmul backward passed") @@ -309,7 +309,7 @@ def test_tile_operators(): assert(np.allclose(output, output_wp.numpy(), rtol=1.e-4)) - print("operators forward passed") + print("Operators forward passed") output_wp.grad.fill_(1.0) @@ -317,7 +317,7 @@ def test_tile_operators(): assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.75, rtol=1.e-4)) - print("operators backward passed") + print("Operators backward passed") @wp.kernel @@ -328,12 +328,13 @@ def tile_sum_kernel(input: wp.array3d(dtype=float), i = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) - s = wp.tile_sum(a, axis=-1)*0.5 + s = wp.tile_sum(a)*0.5 + wp.tile_store(output, i, 0, s) def test_tile_sum(): - batch_count = 2 + batch_count = 56 M = TILE_M N = TILE_N @@ -365,15 +366,56 @@ def test_tile_sum(): print("Sum backward passed") +@wp.kernel +def tile_extract_kernel(input: wp.array2d(dtype=float), + output: wp.array2d(dtype=float)): + + # output tile index + i = wp.tid() + + t = wp.tile_load(input, 0, 0, m=TILE_M, n=TILE_N) + + # perform a scalar copy, extracting each + # tile element individually + for i in range(TILE_M): + for j in range(TILE_N): + output[i,j] = t[i,j] + +def test_tile_extract(): + + M = TILE_M + N = TILE_N + + rng = np.random.default_rng(42) + input = rng.random((M, N), dtype=np.float32) + + input_wp = wp.array(input, requires_grad=True) + output_wp = wp.zeros_like(input_wp, requires_grad=True) + + with wp.Tape() as tape: + wp.launch(tile_extract_kernel, dim=1, inputs=[input_wp, output_wp], tile_size=TILE_DIM) + + assert(np.allclose(input_wp.numpy(), output_wp.numpy(), rtol=1.e-4)) + + print("Extract forward passed") + + output_wp.grad.fill_(1.0) + + tape.backward() + + assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input), rtol=1.e-4)) + + print("Extract backward passed") + test_tile_copy() test_tile_unary_map() test_tile_binary_map() -test_tile_batched_gemm() +test_tile_grouped_gemm() test_tile_gemm() test_tile_operators() test_tile_sum() - +test_tile_extract() # #----------------------------------------- # # center of mass computation From b57ff025c3bbef871351fd40651e48ef155d1b29 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 19 Sep 2024 16:43:29 +1200 Subject: [PATCH 027/102] Remove Alloc parameter from tile_shared_t --- warp/builtins.py | 8 ------- warp/codegen.py | 14 +++++++---- warp/native/tile.h | 49 +++++++++++++++++++-------------------- warp/native/tile_reduce.h | 2 +- warp/types.py | 21 ++++++++++++++--- 5 files changed, 52 insertions(+), 42 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 077d6cd0..fbb526fe 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1729,10 +1729,6 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar template_args.append(m.constant) template_args.append(n.constant) - global shared_memory_id - template_args.append(shared_memory_id) - shared_memory_id += 1 - return ([], template_args) @@ -1790,10 +1786,6 @@ def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg template_args.append(m) template_args.append(n) - global shared_memory_id - template_args.append(shared_memory_id) - shared_memory_id += 1 - return ((array, x, y), template_args) diff --git a/warp/codegen.py b/warp/codegen.py index 941a8f4b..5bbaef68 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -2991,7 +2991,9 @@ def codegen_func_forward(adj, func_type="kernel", device="cpu"): if var.ctype() == "auto": continue - if var.constant is None: + if is_tile(var.type): + lines += [f"{var.ctype()} {var.emit()} = {var.type.cinit()};\n"] + elif var.constant is None: lines += [f"{var.ctype()} {var.emit()};\n"] else: lines += [f"const {var.ctype()} {var.emit()} = {constant_str(var.constant)};\n"] @@ -3027,8 +3029,10 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): for var in adj.variables: - if var.constant is None: - lines += [f"{var.ctype()} {var.emit()};\n"] + if is_tile(var.type): + lines += [f"{var.ctype()} {var.emit()} = {var.type.cinit()};\n"] + elif var.constant is None: + lines += [f"{var.ctype()} {var.emit()};\n"] else: lines += [f"const {var.ctype()} {var.emit()} = {constant_str(var.constant)};\n"] @@ -3040,8 +3044,8 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): name = var.emit_adj() ctype = var.ctype(value_type=True) - if is_tile(var.type) and var.type.storage == "shared": - lines += [f"{ctype} {name} = {{0}};\n"] + if is_tile(var.type): + lines += [f"{ctype} {name} = {var.type.cinit(adjoint=True)};\n"] else: lines += [f"{ctype} {name} = {{}};\n"] diff --git a/warp/native/tile.h b/warp/native/tile.h index 3f3845c9..6a65481e 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -130,17 +130,6 @@ struct coord_t }; -template -inline CUDA_CALLABLE T* tile_alloc_shared() -{ - WP_TILE_SHARED __align__(16) T data[M*N]; - - for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) - data[i] = T(0); - - return data; -} - // represents a tile stored in global memory with dynamic strides // only used to represent the source for tile loads to register/shared template @@ -339,15 +328,14 @@ struct tile_register_t -template +template struct tile_shared_t { using Type = T; static constexpr int M = M_; static constexpr int N = N_; static constexpr int Size = M*N; - static constexpr int Alloc = Alloc_; - + static constexpr int StrideM = StrideM_; static constexpr int StrideN = StrideN_; @@ -358,16 +346,8 @@ struct tile_shared_t // default initialization (non-initialized) inline CUDA_CALLABLE tile_shared_t() { - data = tile_alloc_shared(); } - // zero initialization, handles adj_tile = {0} syntax - inline CUDA_CALLABLE tile_shared_t(int nil) - { - data = tile_alloc_shared(); - zero(); - } - // initialize from an existing tile's memory inline CUDA_CALLABLE tile_shared_t(T* smem) : data(smem) { @@ -569,18 +549,37 @@ struct tile_shared_t } }; +// helpers to allocate shared tiles +template +inline CUDA_CALLABLE auto tile_alloc_empty() +{ + WP_TILE_SHARED __align__(16) T data[M*N]; + return tile_shared_t(data); +} + +template +inline CUDA_CALLABLE auto tile_alloc_zeros() +{ + WP_TILE_SHARED __align__(16) T data[M*N]; + + for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) + data[i] = T(0); + + return tile_shared_t(data); +} + template inline CUDA_CALLABLE auto tile_transpose(Tile& t) { // alias incoming tile - return tile_shared_t(t.data); + return tile_shared_t(t.data); } //----------------------------------------------------------------------------------------------------- // High level entry points for each op (correspond to one Warp builtin) -template +template inline CUDA_CALLABLE auto tile_zeros() { // tile variable assignment operator will handle initialization @@ -589,7 +588,7 @@ inline CUDA_CALLABLE auto tile_zeros() // entry point for load -template +template inline CUDA_CALLABLE auto tile_load(array_t& src, int x, int y) { // just return a ref. to the global memory diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index 5a5b4d81..1f618f6d 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -134,7 +134,7 @@ void adj_tile_sum(Tile& t, Tile& adj_t, AdjTile& adj_ret) WP_TILE_SYNC(); auto adj_t_reg = adj_t.copy_to_register(); - auto adj_ret_reg = tile_shared_t(&scratch).copy_to_register(); + auto adj_ret_reg = tile_shared_t(&scratch).copy_to_register(); adj_t.assign(tile_add(adj_t_reg, adj_ret_reg)); } diff --git a/warp/types.py b/warp/types.py index f990a49e..ec36adc3 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2875,16 +2875,31 @@ def __init__(self, dtype, M, N, op=None, storage="register"): self.op = op self.storage = storage + # generates C-type string def ctype(self): from warp.codegen import Var if self.storage == "register": return f"wp::tile_register_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + elif self.storage == "shared": + return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + + # generates C-initializer string + def cinit(self, adjoint=False): + from warp.codegen import Var + + if self.storage == "register": + return f"{0}" elif self.storage == "shared": + + if adjoint: + # backward pass requires zeroed memory + return f"wp::tile_alloc_zeros<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" + else: + # forward mode can be uninitialized until first used by the kernel + return f"wp::tile_alloc_empty<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" - # every shared memory tile will create a new static shared memory allocation - # this just needs to be a unique-id for templated allocation functions - return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>" + # generate a unique allocation index for shared memory @classmethod From 1e039fd3fa4bae7e52ed4dbc2b4b4f9a0172ecf9 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 24 Sep 2024 16:10:03 +1200 Subject: [PATCH 028/102] Modify the way tiled kernels are launched, this change makes it so the block dimension is inserted as an optional additional launch dimension. This makes it so regular Warp kernels behavior is unchanged, and they can still use tile*() primitives. --- warp/codegen.py | 32 ++++---------------------------- warp/context.py | 16 +++++++++------- warp/tape.py | 10 +++++----- warp/tests/test_tile.py | 30 +++++++++++++++--------------- 4 files changed, 33 insertions(+), 55 deletions(-) diff --git a/warp/codegen.py b/warp/codegen.py index 5bbaef68..fb8dcce2 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -2714,38 +2714,14 @@ def get_constant_references(adj) -> Dict[str, Any]: """ -# cuda_kernel_template = """ - -# extern "C" __global__ void {name}_cuda_kernel_forward( -# {forward_args}) -# {{ -# for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); -# _idx < dim.size; -# _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) -# {{ -# {forward_body} }} -# }} - -# extern "C" __global__ void {name}_cuda_kernel_backward( -# {reverse_args}) -# {{ -# for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); -# _idx < dim.size; -# _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) -# {{ -# {reverse_body} }} -# }} - -# """ - cuda_kernel_template = """ extern "C" __global__ void {name}_cuda_kernel_forward( {forward_args}) {{ - for (size_t _idx = static_cast(blockIdx.x); + for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); _idx < dim.size; - _idx += static_cast(gridDim.x)) + _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) {{ {forward_body} }} }} @@ -2753,9 +2729,9 @@ def get_constant_references(adj) -> Dict[str, Any]: extern "C" __global__ void {name}_cuda_kernel_backward( {reverse_args}) {{ - for (size_t _idx = static_cast(blockIdx.x); + for (size_t _idx = static_cast(blockDim.x) * static_cast(blockIdx.x) + static_cast(threadIdx.x); _idx < dim.size; - _idx += static_cast(gridDim.x)) + _idx += static_cast(blockDim.x) * static_cast(gridDim.x)) {{ {reverse_body} }} }} diff --git a/warp/context.py b/warp/context.py index 95f36afb..9d13059e 100644 --- a/warp/context.py +++ b/warp/context.py @@ -4638,7 +4638,7 @@ def launch( record_tape=True, record_cmd=False, max_blocks=0, - tile_size=0, + block_dim=0, ): """Launch a Warp kernel on the target device @@ -4658,7 +4658,7 @@ def launch( record_cmd: When True the launch will be returned as a ``Launch`` command object, the launch will not occur until the user calls ``cmd.launch()`` max_blocks: The maximum number of CUDA thread blocks to use. Only has an effect for CUDA kernel launches. If negative or zero, the maximum hardware value will be used. - tile_size: The number of threads per-program instance + block_dim: The number of threads per-block """ init() @@ -4713,7 +4713,7 @@ def pack_args(args, params, adjoint=False): # delay load modules, including new overload if needed module = kernel.module - if not module.load(device, tile_size): + if not module.load(device, block_dim): return # late bind @@ -4760,7 +4760,7 @@ def pack_args(args, params, adjoint=False): ) runtime.core.cuda_launch_kernel( - device.context, hooks.backward, bounds.size, max_blocks, tile_size, kernel_params, stream.cuda_stream + device.context, hooks.backward, bounds.size, max_blocks, block_dim, kernel_params, stream.cuda_stream ) else: @@ -4783,7 +4783,7 @@ def pack_args(args, params, adjoint=False): else: # launch runtime.core.cuda_launch_kernel( - device.context, hooks.forward, bounds.size, max_blocks, tile_size, kernel_params, stream.cuda_stream + device.context, hooks.forward, bounds.size, max_blocks, block_dim, kernel_params, stream.cuda_stream ) try: @@ -4797,7 +4797,7 @@ def pack_args(args, params, adjoint=False): # record file, lineno, func as metadata frame = inspect.currentframe().f_back caller = {"file": frame.f_code.co_filename, "lineno": frame.f_lineno, "func": frame.f_code.co_name} - runtime.tape.record_launch(kernel, dim, max_blocks, inputs, outputs, device, tile_size, metadata={"caller": caller}) + runtime.tape.record_launch(kernel, dim, max_blocks, inputs, outputs, device, block_dim, metadata={"caller": caller}) # detect illegal inter-kernel read/write access patterns if verification flag is set if warp.config.verify_autograd_array_access: @@ -5348,7 +5348,9 @@ def type_str(t): elif typing.get_origin(t) in (List, Mapping, Sequence, Union, Tuple): args_repr = ", ".join(type_str(x) for x in typing.get_args(t)) return f"{t.__name__}[{args_repr}]" - + elif warp.types.is_tile(t): + return "Tile" + return t.__name__ diff --git a/warp/tape.py b/warp/tape.py index 15aebf81..9905a2cd 100644 --- a/warp/tape.py +++ b/warp/tape.py @@ -129,7 +129,7 @@ def backward(self, loss: wp.array = None, grads: dict = None): inputs = launch[3] outputs = launch[4] device = launch[5] - tile_size = launch[6] + block_dim = launch[6] adj_inputs = [] adj_outputs = [] @@ -152,14 +152,14 @@ def backward(self, loss: wp.array = None, grads: dict = None): device=device, adjoint=True, max_blocks=max_blocks, - tile_size=tile_size + block_dim=block_dim ) # record a kernel launch on the tape - def record_launch(self, kernel, dim, max_blocks, inputs, outputs, device, tile_size=0, metadata=None): + def record_launch(self, kernel, dim, max_blocks, inputs, outputs, device, block_dim=0, metadata=None): if metadata is None: metadata = {} - self.launches.append([kernel, dim, max_blocks, inputs, outputs, device, tile_size, metadata]) + self.launches.append([kernel, dim, max_blocks, inputs, outputs, device, block_dim, metadata]) def record_func(self, backward, arrays): """ @@ -614,7 +614,7 @@ def emit_kernel_launch_node( self.array_grad_stats.insert(0, grad_stats) -Launch = namedtuple("Launch", ["id", "kernel", "dim", "max_blocks", "inputs", "outputs", "device", "tile_size", "metadata"]) +Launch = namedtuple("Launch", ["id", "kernel", "dim", "max_blocks", "inputs", "outputs", "device", "block_dim", "metadata"]) RepeatedSequence = namedtuple("RepeatedSequence", ["start", "end", "repetitions"]) diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 3153ac1b..e1bfd21b 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -24,7 +24,7 @@ def tile_copy(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): # tile index - i, j = wp.tid() + i, j, _ = wp.tid() a = wp.tile_load(A, i, j, m=TILE_M, n=TILE_N) wp.tile_store(B, i, j, a) @@ -44,7 +44,7 @@ def test_tile_copy(): B_wp = wp.array(B, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_copy, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=TILE_DIM) + wp.launch(tile_copy, dim=[int(M/TILE_M), int(N/TILE_N), TILE_DIM], inputs=[A_wp, B_wp], block_dim=TILE_DIM) # verify forward pass assert(np.allclose(A, B_wp.numpy(), rtol=1.e-4)) @@ -66,7 +66,7 @@ def tile_unary_map(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): # tile index - i, j = wp.tid() + i, j, _ = wp.tid() a = wp.tile_load(input, i, j, m=TILE_M, n=TILE_N) @@ -91,7 +91,7 @@ def test_tile_unary_map(): B_wp = wp.zeros_like(A_wp, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_unary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp], tile_size=TILE_DIM) + wp.launch(tile_unary_map, dim=[int(M/TILE_M), int(N/TILE_N), TILE_DIM], inputs=[A_wp, B_wp], block_dim=TILE_DIM) # verify forward pass assert(np.allclose(B, B_wp.numpy(), atol=1.e-4)) @@ -115,7 +115,7 @@ def tile_binary_map(input_a: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): # tile index - i, j = wp.tid() + i, j, _= wp.tid() a = wp.tile_load(input_a, i, j, m=TILE_M, n=TILE_N) b = wp.tile_load(input_b, i, j, m=TILE_M, n=TILE_N) @@ -144,7 +144,7 @@ def test_tile_binary_map(): C_wp = wp.zeros_like(A_wp, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_binary_map, dim=[int(M/TILE_M), int(N/TILE_N)], inputs=[A_wp, B_wp, C_wp], tile_size=TILE_DIM) + wp.launch(tile_binary_map, dim=[int(M/TILE_M), int(N/TILE_N), TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) # verify forward pass assert(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) @@ -196,7 +196,7 @@ def test_tile_grouped_gemm(): C_wp = wp.array(C, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_grouped_gemm, dim=batch_count, inputs=[A_wp, B_wp, C_wp], tile_size=TILE_DIM) + wp.launch(tile_grouped_gemm, dim=[batch_count, TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) # bring back to host C_host = C_wp.numpy() @@ -211,7 +211,7 @@ def tile_gemm(A: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): # output tile index - i, j = wp.tid() + i, j, _= wp.tid() sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) @@ -248,7 +248,7 @@ def test_tile_gemm(): C_wp = wp.array(C, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=TILE_DIM) + wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N), TILE_DIM), inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) @@ -271,7 +271,7 @@ def tile_operators(input: wp.array3d(dtype=float), output: wp.array3d(dtype=float)): # output tile index - i = wp.tid() + i, _ = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) @@ -305,7 +305,7 @@ def test_tile_operators(): output_wp = wp.zeros_like(input_wp, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_operators, dim=batch_count, inputs=[input_wp, output_wp], tile_size=TILE_DIM) + wp.launch(tile_operators, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) assert(np.allclose(output, output_wp.numpy(), rtol=1.e-4)) @@ -325,7 +325,7 @@ def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): # output tile index - i = wp.tid() + i, _ = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) s = wp.tile_sum(a)*0.5 @@ -346,7 +346,7 @@ def test_tile_sum(): output_wp = wp.zeros(batch_count, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_sum_kernel, dim=batch_count, inputs=[input_wp, output_wp], tile_size=TILE_DIM) + wp.launch(tile_sum_kernel, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) for i in range(batch_count): @@ -371,7 +371,7 @@ def tile_extract_kernel(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): # output tile index - i = wp.tid() + i, _ = wp.tid() t = wp.tile_load(input, 0, 0, m=TILE_M, n=TILE_N) @@ -393,7 +393,7 @@ def test_tile_extract(): output_wp = wp.zeros_like(input_wp, requires_grad=True) with wp.Tape() as tape: - wp.launch(tile_extract_kernel, dim=1, inputs=[input_wp, output_wp], tile_size=TILE_DIM) + wp.launch(tile_extract_kernel, dim=[1, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) assert(np.allclose(input_wp.numpy(), output_wp.numpy(), rtol=1.e-4)) From 394b2b036ad7ab54ef1336bedd0cc322716c99e3 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 24 Sep 2024 16:18:48 +1200 Subject: [PATCH 029/102] Fix for shared memory race condition when extracting elements from register tiles --- warp/native/tile.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/warp/native/tile.h b/warp/native/tile.h index 6a65481e..e7808f41 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -238,11 +238,15 @@ struct tile_register_t WP_TILE_SHARED Type scratch; + // ensure any prevoiusly scheduled threads have finished reading from scratch + WP_TILE_SYNC(); + if (threadIdx.x == thread) { scratch = data[reg]; } + // ensure extraction thread has updated smem WP_TILE_SYNC(); return scratch; From 3ac843167216126fe92fd7c310692b921b8c154b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 24 Sep 2024 16:29:51 +1200 Subject: [PATCH 030/102] Fix for regular Warp kernel code-gen on CPU, set default block_dim=256 explicitly --- warp/codegen.py | 12 ++++++------ warp/context.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/warp/codegen.py b/warp/codegen.py index fb8dcce2..6eeba5fd 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -2625,10 +2625,10 @@ def get_constant_references(adj) -> Dict[str, Any]: #define int(x) cast_int(x) #define adj_int(x, adj_x, adj_ret) adj_cast_int(x, adj_x, adj_ret) -#define builtin_tid1d() wp::tid(_idx) -#define builtin_tid2d(x, y) wp::tid(x, y, _idx, dim) -#define builtin_tid3d(x, y, z) wp::tid(x, y, z, _idx, dim) -#define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, _idx, dim) +#define builtin_tid1d() wp::tid(task_index) +#define builtin_tid2d(x, y) wp::tid(x, y, task_index, dim) +#define builtin_tid3d(x, y, z) wp::tid(x, y, z, task_index, dim) +#define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, task_index, dim) """ @@ -2761,7 +2761,7 @@ def get_constant_references(adj) -> Dict[str, Any]: WP_API void {name}_cpu_forward( {forward_args}) {{ - for (size_t _idx = 0; _idx < dim.size; ++_idx) + for (size_t task_index = 0; task_index < dim.size; ++task_index) {{ {name}_cpu_kernel_forward( {forward_params}); @@ -2771,7 +2771,7 @@ def get_constant_references(adj) -> Dict[str, Any]: WP_API void {name}_cpu_backward( {reverse_args}) {{ - for (size_t _idx = 0; _idx < dim.size; ++_idx) + for (size_t task_index = 0; task_index < dim.size; ++task_index) {{ {name}_cpu_kernel_backward( {reverse_params}); diff --git a/warp/context.py b/warp/context.py index 9d13059e..59356599 100644 --- a/warp/context.py +++ b/warp/context.py @@ -4638,7 +4638,7 @@ def launch( record_tape=True, record_cmd=False, max_blocks=0, - block_dim=0, + block_dim=256, ): """Launch a Warp kernel on the target device From 38a27d45352f277fdcd45ec3945a03db53094839 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Tue, 24 Sep 2024 17:40:18 -0700 Subject: [PATCH 031/102] MathDx support in Warp + Tile --- build_lib.py | 2 +- examples/tile_fft.py | 34 +++++ examples/tile_matmul.py | 35 +++++ warp/build.py | 18 ++- warp/build_dll.py | 8 +- warp/builtins.py | 249 ++++++++++++++++++++++++++++++ warp/codegen.py | 28 ++-- warp/context.py | 67 ++++++++- warp/native/tile.h | 52 +++++++ warp/native/warp.cpp | 2 +- warp/native/warp.cu | 326 ++++++++++++++++++++++++++++++++++++++-- warp/native/warp.h | 4 +- warp/tests/test_tile.py | 77 ++++++++++ warp/types.py | 4 +- 14 files changed, 867 insertions(+), 39 deletions(-) create mode 100644 examples/tile_fft.py create mode 100644 examples/tile_matmul.py diff --git a/build_lib.py b/build_lib.py index 436c9cde..3827c4cd 100644 --- a/build_lib.py +++ b/build_lib.py @@ -52,7 +52,7 @@ parser.set_defaults(fast_math=False) parser.add_argument("--quick", action="store_true", help="Only generate PTX code, disable CUTLASS ops") -parser.set_defaults(quick=True) +parser.set_defaults(quick=False) parser.add_argument("--build_llvm", action="store_true", help="Build Clang/LLVM compiler from source, default disabled") parser.add_argument("--no_build_llvm", dest="build_llvm", action="store_false") diff --git a/examples/tile_fft.py b/examples/tile_fft.py new file mode 100644 index 00000000..f6cf23f9 --- /dev/null +++ b/examples/tile_fft.py @@ -0,0 +1,34 @@ +import numpy as np +import warp as wp +import numpy as np + +wp.init() +wp.set_module_options({"enable_backward": False}) +wp.set_device("cuda:0") +wp.build.clear_kernel_cache() + +BLOCK_DIM = 8 +TILE_M = 1 +TILE_N = 32 + +@wp.kernel +def fft_tiled(x: wp.array2d(dtype=wp.vec2d), + y: wp.array2d(dtype=wp.vec2d)): + + i, j, _ = wp.tid() + a = wp.tile_load(x, i, j, m=TILE_M, n=TILE_N) + wp.tile_fft_dx(a) + wp.tile_ifft_dx(a) + wp.tile_store(y, i, j, a) + + +x_h = np.ones((TILE_M, TILE_N, 2), dtype=np.float64) +x_h[:,:,1] = 0 +y_h = 3 * np.ones((TILE_M, TILE_N, 2), dtype=np.float64) +x_wp = wp.array2d(x_h, dtype=wp.vec2d) +y_wp = wp.array2d(y_h, dtype=wp.vec2d) + +wp.launch(fft_tiled, dim=[1, 1, BLOCK_DIM], inputs=[x_wp, y_wp], block_dim=BLOCK_DIM) + +print("inputs:\n", x_wp) # [1+0i, 1+0i, 1+0i, ...] +print("output:\n", y_wp) # [32+0i, 0, 0, ...] diff --git a/examples/tile_matmul.py b/examples/tile_matmul.py new file mode 100644 index 00000000..3d980592 --- /dev/null +++ b/examples/tile_matmul.py @@ -0,0 +1,35 @@ +import numpy as np +import warp as wp + +wp.init() +wp.build.clear_kernel_cache() + +BLOCK_DIM = 32 +M, N, K = 4, 8, 16 + +@wp.kernel +def matmul_tiled(ga: wp.array2d(dtype=wp.float64), + gb: wp.array2d(dtype=wp.float64), + gc: wp.array2d(dtype=wp.float64)): + + i, j, _ = wp.tid() + a = wp.tile_load(ga, i, j, m=M, n=K) + b = wp.tile_load(gb, i, j, m=K, n=N) + c = wp.tile_zeros(m=M, n=N, dtype=wp.float64) + wp.tile_matmul_dx(a, b, c) + wp.tile_store(gc, i, j, c) + + +A = np.ones((M, K), dtype=np.float64) +B = 3 * np.ones((K, N), dtype=np.float64) +C = np.zeros((M, N), dtype=np.float64) + +A_wp = wp.array2d(A, dtype=wp.float64) +B_wp = wp.array2d(B, dtype=wp.float64) +C_wp = wp.array2d(C, dtype=wp.float64) + +wp.launch(matmul_tiled, dim=[1, 1, BLOCK_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=BLOCK_DIM) +wp.synchronize() + +print("inputs:\n", A, '\n', B) +print("output (should be = 48 * np.ones(4, 8)):\n", C_wp) diff --git a/warp/build.py b/warp/build.py index 7eee8e29..024e5ebc 100644 --- a/warp/build.py +++ b/warp/build.py @@ -9,22 +9,36 @@ import warp.config from warp.thirdparty import appdirs +import ctypes +def get_mathdx_include_dirs(): + return (os.environ['MATHDX_HOME'] + '/include').encode("utf-8") + +def get_cuda_include_dirs(): + cuda_inc_path = (os.environ['CUDA_HOME'] + '/include').encode("utf-8") + include_dirs = [cuda_inc_path] + arr_include_dirs = (ctypes.c_char_p * len(include_dirs))() + arr_include_dirs[:] = include_dirs + return arr_include_dirs # builds cuda source to PTX or CUBIN using NVRTC (output type determined by output_path extension) -def build_cuda(cu_path, arch, output_path, config="release", verify_fp=False, fast_math=False): +def build_cuda(cu_path, arch, output_path, config="release", verify_fp=False, fast_math=False, ltoirs=[]): with open(cu_path, "rb") as src_file: src = src_file.read() cu_path = cu_path.encode("utf-8") inc_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "native").encode("utf-8") output_path = output_path.encode("utf-8") + cuda_include_dirs = get_cuda_include_dirs() if warp.config.llvm_cuda: warp.context.runtime.llvm.compile_cuda(src, cu_path, inc_path, output_path, False) else: + num_ltoirs = len(ltoirs) + arr_lroirs = (ctypes.c_char_p * num_ltoirs)(*ltoirs) + arr_lroir_sizes = (ctypes.c_size_t * num_ltoirs)(*[len(l) for l in ltoirs]) err = warp.context.runtime.core.cuda_compile_program( - src, arch, inc_path, config == "debug", warp.config.verbose, verify_fp, fast_math, output_path + src, arch, inc_path, len(cuda_include_dirs), cuda_include_dirs, config == "debug", warp.config.verbose, verify_fp, fast_math, output_path, num_ltoirs, arr_lroirs, arr_lroir_sizes ) if err != 0: raise Exception(f"CUDA kernel build failed with error code {err}") diff --git a/warp/build_dll.py b/warp/build_dll.py index 6810d9c7..cecfc105 100644 --- a/warp/build_dll.py +++ b/warp/build_dll.py @@ -292,6 +292,8 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None run_cmd(link_cmd) else: + libmathdx_home = os.environ['LIBMATHDX_HOME'] + libmathdx_includes = f'-I{libmathdx_home}/include' cpp_includes = f' -I"{warp_home_path.parent}/external/llvm-project/out/install/{mode}-{arch}/include"' cpp_includes += f' -I"{warp_home_path.parent}/_build/host-deps/llvm-project/release-{arch}/include"' cuda_includes = f' -I"{cuda_home}/include"' if cu_path else "" @@ -330,17 +332,17 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None cu_out = cu_path + ".o" if mode == "debug": - cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -g -G -O0 --compiler-options -fPIC,-fvisibility=hidden -D_DEBUG -D_ITERATOR_DEBUG_LEVEL=0 -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -g -G -O0 --compiler-options -fPIC,-fvisibility=hidden -D_DEBUG -D_ITERATOR_DEBUG_LEVEL=0 -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} {libmathdx_includes} -o "{cu_out}" -c "{cu_path}"' elif mode == "release": - cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -O3 --compiler-options -fPIC,-fvisibility=hidden {" ".join(nvcc_opts)} -DNDEBUG -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -O3 --compiler-options -fPIC,-fvisibility=hidden {" ".join(nvcc_opts)} -DNDEBUG -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} {libmathdx_includes} -o "{cu_out}" -c "{cu_path}"' with ScopedTimer("build_cuda", active=args.verbose): run_cmd(cuda_cmd) ld_inputs.append(quote(cu_out)) ld_inputs.append( - f'-L"{cuda_home}/lib64" -lcudart_static -lnvrtc_static -lnvrtc-builtins_static -lnvptxcompiler_static -lpthread -ldl -lrt' + f'-L"{cuda_home}/lib64" -L{libmathdx_home}/lib -lcudart_static -lnvrtc_static -lnvrtc-builtins_static -lnvptxcompiler_static -lnvJitLink_static -lpthread -ldl -lrt -lmathdx_static' ) if sys.platform == "darwin": diff --git a/warp/builtins.py b/warp/builtins.py index fbb526fe..5aa0cee6 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5,12 +5,16 @@ # distribution of this software and related documentation without an express # license agreement from NVIDIA CORPORATION is strictly prohibited. import builtins +import tempfile +import functools +import os from typing import Any, Callable, Mapping, Sequence from warp.codegen import Reference, Var, strip_reference from warp.types import * from .context import add_builtin +from .build import get_cuda_include_dirs, get_mathdx_include_dirs def seq_check_equal(seq_1, seq_2): @@ -4550,3 +4554,248 @@ def tile_scalar_mul_value_func(arg_types, arg_values): ) +## +## MathDx, LTOIR-based, Tile functions +## + +## +## Matmul +## +def tile_matmul_generic_value_func(arg_types, arg_values): + + # return generic type (for doc builds) + if arg_types is None: + return None + + if len(arg_types) != 3: + raise RuntimeError("tile_matmul() requires 4 positional args") + + if not is_tile(arg_types["a"]): + raise RuntimeError("tile_matmul() argument 0 must be a tile") + + if not is_tile(arg_types["b"]): + raise RuntimeError("tile_matmul() argument 1 must be an tile") + + if not isinstance(arg_types["out"], Tile): + raise RuntimeError("tile_matmul() output argument must be a tile") + + if arg_types["out"].storage != "shared": + raise RuntimeError("tile_matmul() output argument must have shared memory storage") + + + return None + +def tile_matmul_generic_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any]): + + a = arg_values["a"] + b = arg_values["b"] + out = arg_values["out"] + + if any(not is_tile(arg.type) for arg in [a, b, out]): + raise RuntimeError(f"tile_matmul() requires three Tile arguments") + + if any(arg.type.dtype not in [float16, float32, float64, vec2h, vec2f, vec2d] for arg in [a, b, out]): + raise RuntimeError(f"tile_matmul() arguments must be tiles of float16, float32 or float64, vec2h, vec2f, vec2d entries") + + if any(arg.type.dtype != out.type.dtype for arg in [a, b]): + raise RuntimeError(f"tile_matmul() arguments must have the same type") + + if (a.type.N != b.type.M) or (a.type.M != out.type.M) or (b.type.N != out.type.N): + raise RuntimeError(f"tile_matmul(A, B, C) requires sizes of A, B and C to be consistent for a matmul") + + # set the storage type to the inputs to shared + a.type.storage = "shared" + b.type.storage = "shared" + out.type.storage = "shared" + template_args = [] + + # Real + if out.type.dtype == float16: + dtype = "wp::float16" + precision = 2 # COMMONDX_PRECISION_F16 + element_type = 0 # CUBLASDX_TYPE_REAL + elif out.type.dtype == float32: + dtype = "wp::float32" + precision = 3 # COMMONDX_PRECISION_F32 + element_type = 0 # CUBLASDX_TYPE_REAL + elif out.type.dtype == float64: + dtype = "wp::float64" + precision = 4 # COMMONDX_PRECISION_F64 + element_type = 0 # CUBLASDX_TYPE_REAL + # Complex + elif out.type.dtype == vec2h: + dtype = "wp::vec2h" + precision = 2 # COMMONDX_PRECISION_F16 + element_type = 1 # CUBLASDX_TYPE_COMPLEX + elif out.type.dtype == vec2f: + dtype = "wp::vec2f" + precision = 3 # COMMONDX_PRECISION_F32 + element_type = 1 # CUBLASDX_TYPE_COMPLEX + elif out.type.dtype == vec2d: + dtype = "wp::vec2d" + precision = 4 # COMMONDX_PRECISION_F64 + element_type = 1 # CUBLASDX_TYPE_COMPLEX + else: + raise RuntimeError("Unsupported datatype") + + # generate the LTO + M, K = a.type.M, a.type.N + _, N = b.type.M, b.type.N + num_threads = options['tile_size'] + arch = options['output_arch'] + + def make_function(M, N, K, tA, tB): + # Warp follows Numpy: matrices are row-major + # But cuBLASDx follows BLAS: matrices are col-major + # So we have to flip M <-> N and A <-> B + def make_transpose(t): + if t == 'N': + return 0 # CUBLASDX_TRANSPOSE_MODE_NON_TRANSPOSED + elif t == 'T': + return 1 # CUBLASDX_TRANSPOSE_MODE_TRANSPOSED + raise RuntimeError("Invalid transpose mode") + lto_symbol = f"dot_{M}_{N}_{K}_{tA}_{tB}_{precision}_{element_type}" + lto_code = tempfile.NamedTemporaryFile() + include_dirs = get_cuda_include_dirs() + result = warp.context.runtime.core.cuda_compile_dot( + lto_code.name.encode("utf-8"), lto_symbol.encode("utf-8"), + len(include_dirs), include_dirs, get_mathdx_include_dirs(), + arch, N, M, K, precision, element_type, make_transpose(tB), make_transpose(tA), num_threads) + if not result: + raise RuntimeError("Failed to compile tile_matmul") + else: + with open(lto_code.name, 'rb') as f: + lto_code = f.read() + return lto_symbol, lto_code + + (fun_forward, lto_forward) = make_function(M, N, K, 'N', 'N') # C += A * B + (fun_backward_A, lto_backward_A) = make_function(M, K, N, 'N', 'T') # adjA += adjC * B^T + (fun_backward_B, lto_backward_B) = make_function(K, N, M, 'T', 'N') # adjB += A^T * adjC + + return ((Var(fun_forward, str, False, True, False), + Var(fun_backward_A, str, False, True, False), + Var(fun_backward_B, str, False, True, False), + Var(dtype, str, False, True, False), + a, + b, + out), + template_args, + [lto_forward, lto_backward_A, lto_backward_B]) + +add_builtin( + "tile_matmul_dx", + input_types={"a": Tile, "b": Tile, "out": Tile}, + value_func=tile_matmul_generic_value_func, + lto_dispatch_func=tile_matmul_generic_dispatch_func, + variadic=True, + doc="Compute matrix product and accumulate out += a*b.", + group="Tile Primitives", + export=False, + namespace="", +) + +## +## FFT +## +def tile_fft_generic_value_func(arg_types, arg_values): + + if arg_types is None: + return None + + if len(arg_types) != 1: + raise RuntimeError("tile_fft() requires 1 positional args") + + if not is_tile(arg_types["inout"]): + raise RuntimeError("tile_fft() argument 0 must be a tile") + + if arg_types["inout"].storage != "register": + raise RuntimeError("tile_fft() input/output argument must have register memory storage") + + return None + +def tile_fft_generic_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any], direction:str = None): + + inout = arg_values["inout"] + inout.type.storage = "register" + + if (not is_tile(inout.type)): + raise RuntimeError(f"tile_fft() arguments must be a single tile with register storage") + + if (inout.type.dtype not in [vec2f, vec2d]): + raise RuntimeError(f"tile_fft() argument must be a tile of vec2f or vec2d (interpreted as complex) entries") + + # see libcufftdx.hpp + if direction == 'forward': + dir = 0 # CUFFTDX_DIRECTION_FORWARD + elif direction == 'inverse': + dir = 1 # CUFFTDX_DIRECTION_INVERSE + else: + raise RuntimeError("Invalid direction") + + if inout.type.dtype == vec2f: + dtype = "wp::vec2f" + precision = 3 # COMMONDX_PRECISION_F32 + elif inout.type.dtype == vec2d: + dtype = "wp::vec2d" + precision = 4 # COMMONDX_PRECISION_F64 + else: + raise RuntimeError("Unsupported datatype") + + # M FFTs of size N each + batch, size = inout.type.M, inout.type.N + num_threads = options['tile_size'] + arch = options['output_arch'] + ept = size // num_threads + lto_symbol = f"fft_{size}_{ept}_{arch}_{direction}_{precision}" + + lto_code = tempfile.NamedTemporaryFile() + shared_memory_size = ctypes.c_int(0) + + include_dirs = get_cuda_include_dirs() + + result = warp.context.runtime.core.cuda_compile_fft( + lto_code.name.encode("utf-8"), + lto_symbol.encode("utf-8"), + len(include_dirs), include_dirs, + get_mathdx_include_dirs(), + arch, size, ept, dir, precision, ctypes.byref(shared_memory_size) + ) + + if not result: + raise RuntimeError("Failed to compile tile_matmul") + + with open(lto_code.name, 'rb') as f: + lto_code = f.read() + + return ((Var(lto_symbol, str, False, True, False), + Var(dtype, str, False, True, False), + Var(str(shared_memory_size.value), str, False, True, False), + Var(str(batch), str, False, True, False), + Var(str(ept), str, False, True, False), + inout), + [], + [lto_code]) + +add_builtin( + "tile_fft_dx", + input_types={"inout": Tile}, + value_func=tile_fft_generic_value_func, + lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction='forward'), + variadic=True, + doc="Compute the FFT along the second dimension of a 2D tile of data.", + group="Tile Primitives", + export=False, + namespace="", +) + +add_builtin( + "tile_ifft_dx", + input_types={"inout": Tile}, + value_func=tile_fft_generic_value_func, + lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction='inverse'), + variadic=True, + doc="Compute the inverse FFT along the second dimension of a 2D tile of data.", + group="Tile Primitives", + export=False, + namespace="", +) diff --git a/warp/codegen.py b/warp/codegen.py index 6eeba5fd..7336ac5e 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -865,6 +865,9 @@ def __init__( # for unit testing errors being spit out from kernels. adj.skip_build = False + # Collect the LTOIR required at link-time + adj.ltoirs = [] + # generate function ssa form and adjoint def build(adj, builder, default_builder_options=None): # arg Var read/write flags are held during module rebuilds, so we reset here even when skipping a build @@ -901,6 +904,9 @@ def build(adj, builder, default_builder_options=None): # used to generate new label indices adj.label_count = 0 + # collect ltoirs + adj.ltoirs = [] + # update symbol map for each argument for a in adj.args: adj.symbols[a.label] = a @@ -926,6 +932,8 @@ def build(adj, builder, default_builder_options=None): elif isinstance(a.type, warp.types.array) and isinstance(a.type.dtype, Struct): builder.build_struct_recursive(a.type.dtype) + builder.ltoirs.extend(adj.ltoirs) + # code generation methods def format_template(adj, template, input_vars, output_var): # output var is always the 0th index @@ -1227,15 +1235,17 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): bound_arg_values, ) - if func.dispatch_func is not None: - # If we have a built-in that requires special handling to dispatch - # the arguments to the underlying C++ function, then we can resolve - # these using the `dispatch_func`. Since this is only called from - # within codegen, we pass it directly `codegen.Var` objects, - # which allows for some more advanced resolution to be performed, - # for example by checking whether an argument corresponds to - # a literal value or references a variable. - + # If we have a built-in that requires special handling to dispatch + # the arguments to the underlying C++ function, then we can resolve + # these using the `dispatch_func`. Since this is only called from + # within codegen, we pass it directly `codegen.Var` objects, + # which allows for some more advanced resolution to be performed, + # for example by checking whether an argument corresponds to + # a literal value or references a variable. + if func.lto_dispatch_func is not None: + func_args, template_args, ltoirs = func.lto_dispatch_func(func.input_types, return_type, bound_args, options=adj.builder_options) + adj.ltoirs.extend(ltoirs) + elif func.dispatch_func is not None: func_args, template_args = func.dispatch_func(func.input_types, return_type, bound_args) else: func_args = tuple(bound_args.values()) diff --git a/warp/context.py b/warp/context.py index 59356599..a07b9f1d 100644 --- a/warp/context.py +++ b/warp/context.py @@ -66,6 +66,7 @@ def __init__( value_func=None, export_func=None, dispatch_func=None, + lto_dispatch_func=None, module=None, variadic=False, initializer_list_func=None, @@ -101,6 +102,7 @@ def __init__( self.value_func = value_func # a function that takes a list of args and a list of templates and returns the value type, e.g.: load(array, index) returns the type of value being loaded self.export_func = export_func self.dispatch_func = dispatch_func + self.lto_dispatch_func = lto_dispatch_func self.input_types = {} self.export = export self.doc = doc @@ -1012,6 +1014,7 @@ def add_builtin( value_func=None, export_func=None, dispatch_func=None, + lto_dispatch_func=None, doc="", namespace="wp::", variadic=False, @@ -1052,6 +1055,9 @@ def add_builtin( The arguments returned must be of type `codegen.Var`. If not provided, all arguments passed by the users when calling the built-in are passed as-is as runtime arguments to the C++ function. + lto_dispatch_func (Callable): Same as dispatch_func, but takes an 'option' dict + as extra argument (indicating tile_size and target architecture) and returns + an LTO-IR buffer as extra return value doc (str): Used to generate the Python's docstring and the HTML documentation. namespace: Namespace for the underlying C++ function. variadic (bool): Whether the function declares variadic arguments. @@ -1190,6 +1196,7 @@ def initializer_list_func(args, return_type): value_type=return_type, export_func=export_func, dispatch_func=dispatch_func, + lto_dispatch_func=lto_dispatch_func, doc=doc, namespace=namespace, variadic=variadic, @@ -1212,6 +1219,7 @@ def initializer_list_func(args, return_type): value_func=value_func, export_func=export_func, dispatch_func=dispatch_func, + lto_dispatch_func=lto_dispatch_func, variadic=variadic, initializer_list_func=initializer_list_func, export=export, @@ -1296,6 +1304,7 @@ def __init__(self, module, options): self.options = options self.module = module self.deferred_functions = [] + self.ltoirs = [] # build all functions declared in the module for func in module.functions.values(): @@ -1750,6 +1759,9 @@ def load(self, device, tile_size=0) -> bool: output_arch = device.arch output_name = f"module_codegen.sm{output_arch}.cubin" + # Some of the Tile codegen, such as cuFFTDx and cuBLASDx, requires knowledge of the target arch + self.options["output_arch"] = output_arch + # final object binary path binary_path = os.path.join(module_dir, output_name) @@ -1828,6 +1840,7 @@ def load(self, device, tile_size=0) -> bool: config=self.options["mode"], fast_math=self.options["fast_math"], verify_fp=warp.config.verify_fp, + ltoirs=builder.ltoirs, ) except Exception as e: @@ -3069,17 +3082,55 @@ def __init__(self): self.core.cuda_graph_destroy.restype = ctypes.c_bool self.core.cuda_compile_program.argtypes = [ - ctypes.c_char_p, - ctypes.c_int, - ctypes.c_char_p, - ctypes.c_bool, - ctypes.c_bool, - ctypes.c_bool, - ctypes.c_bool, - ctypes.c_char_p, + ctypes.c_char_p, # cuda_src + ctypes.c_int, # arch + ctypes.c_char_p, # include_dir + ctypes.c_int, # num_cuda_include_dirs + ctypes.POINTER(ctypes.c_char_p), # cuda include dirs + ctypes.c_bool, # debug + ctypes.c_bool, # verbose + ctypes.c_bool, # verify_fp + ctypes.c_bool, # fast_math + ctypes.c_char_p, # output_path + ctypes.c_size_t, # num_ltoirs + ctypes.POINTER(ctypes.c_char_p), # ltoirs + ctypes.POINTER(ctypes.c_size_t), # ltoir_sizes ] self.core.cuda_compile_program.restype = ctypes.c_size_t + self.core.cuda_compile_fft.argtypes = [ + ctypes.c_char_p, # lto + ctypes.c_char_p, # function name + ctypes.c_int, # num include dirs + ctypes.POINTER(ctypes.c_char_p), # include dirs + ctypes.c_char_p, # mathdx include dir + ctypes.c_int, # arch + ctypes.c_int, # size + ctypes.c_int, # ept + ctypes.c_int, # direction + ctypes.c_int, # precision + ctypes.POINTER(ctypes.c_int) # smem (out) + ] + self.core.cuda_compile_fft.restype = ctypes.c_bool + + self.core.cuda_compile_dot.argtypes = [ + ctypes.c_char_p, # lto + ctypes.c_char_p, # function name + ctypes.c_int, # num include dirs + ctypes.POINTER(ctypes.c_char_p), # include dirs + ctypes.c_char_p, # mathdx include dir + ctypes.c_int, # arch + ctypes.c_int, # M + ctypes.c_int, # N + ctypes.c_int, # K + ctypes.c_int, # precision + ctypes.c_int, # type + ctypes.c_int, # tA + ctypes.c_int, # tB + ctypes.c_int # num threads + ] + self.core.cuda_compile_dot.restype = ctypes.c_bool + self.core.cuda_load_module.argtypes = [ctypes.c_void_p, ctypes.c_char_p] self.core.cuda_load_module.restype = ctypes.c_void_p diff --git a/warp/native/tile.h b/warp/native/tile.h index e7808f41..10ea0830 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -861,6 +861,58 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ adj_t.adj_extract(i, j, adj_ret); } +// But cuBLASDx follows the BLAS convention: matrices are col-major, so we swap A & B in the code below + +#define tile_matmul_dx(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C) \ + do { \ + void fun_forward(dtype, dtype*, dtype*, dtype, dtype*); \ + WP_TILE_SYNC(); \ + fun_forward(dtype(1.0), B.data, A.data, dtype(1.0), C.data); \ + WP_TILE_SYNC(); \ + } while (0) + +// adj_fun_forward, adj_fun_backward_A, adj_fun_backward_B, adj_dtype are in practice ignored +// but are here because builtins.py creates them even though those are effectively compile time constants +#define adj_tile_matmul_dx(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C, \ + adj_fun_forward, adj_fun_backward_A, adj_fun_backward_B, adj_dtype, \ + adjA, adjB, adjC) \ + do { \ + void fun_backward_A(dtype, dtype*, dtype*, dtype, dtype*); \ + void fun_backward_B(dtype, dtype*, dtype*, dtype, dtype*); \ + WP_TILE_SYNC(); \ + fun_backward_A(dtype(1.0), B.data, adjC.data, dtype(1.0), adjA.data); \ + fun_backward_B(dtype(1.0), adjC.data, A.data, dtype(1.0), adjB.data); \ + WP_TILE_SYNC(); \ + } while (0) + +#define tile_fft_dx(function_name, dtype, shared_memory_size, batch_size, ept, Xinout) \ + do { \ + void function_name(dtype*, dtype*); \ + WP_TILE_SHARED __align__(16) char buffer[shared_memory_size]; \ + WP_TILE_SYNC(); \ + for(int b = 0; b < (int)batch_size; b++) { \ + function_name(Xinout.data + (int)b * (int)ept, (dtype*)buffer); \ + WP_TILE_SYNC(); \ + } \ + } while (0) + +#define tile_ifft_dx tile_fft_dx + +// adj_function_name, adj_dtype, adj_shared_memory_size, adj_batch_size, adj_ept are all ignored + +#define adj_tile_fft_dx(function_name, dtype, shared_memory_size, batch_size, ept, Xinout, \ + adj_function_name, adj_dtype, adj_shared_memory_size, adj_batch_size, adj_ept, \ + adj_Xinout) \ + do { \ + tile_ifft_dx(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \ + } while (0) + +#define adj_tile_ifft_dx(function_name, dtype, shared_memory_size, batch_size, ept, Xinout, \ + adj_function_name, adj_dtype, adj_shared_memory_size, adj_batch_size, adj_ept, \ + adj_Xinout) \ + do { \ + tile_fft_dx(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \ + } while (0) } // namespace wp diff --git a/warp/native/warp.cpp b/warp/native/warp.cpp index b7ad19a3..697e4dcf 100644 --- a/warp/native/warp.cpp +++ b/warp/native/warp.cpp @@ -1031,7 +1031,7 @@ WP_API bool cuda_graph_end_capture(void* context, void* stream, void** graph_ret WP_API bool cuda_graph_launch(void* graph, void* stream) { return false; } WP_API bool cuda_graph_destroy(void* context, void* graph) { return false; } -WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_file) { return 0; } +WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes) { return 0; } WP_API void* cuda_load_module(void* context, const char* ptx) { return NULL; } WP_API void cuda_unload_module(void* context, void* module) {} diff --git a/warp/native/warp.cu b/warp/native/warp.cu index 30c8f512..8268ae3b 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -11,9 +11,14 @@ #include "cuda_util.h" #include "error.h" +#include +#include #include +#include #include +#include +#include #include #include #include @@ -23,8 +28,39 @@ #include #include +#define check_any(result) (check_generic(result, __FILE__, __LINE__)) #define check_nvrtc(code) (check_nvrtc_result(code, __FILE__, __LINE__)) #define check_nvptx(code) (check_nvptx_result(code, __FILE__, __LINE__)) +#define check_nvjitlink(handle, code) (check_nvjitlink_result(handle, code, __FILE__, __LINE__)) +#define check_cufftdx(code) (check_cufftdx_result(code, __FILE__, __LINE__)) +#define check_cublasdx(code) (check_cublasdx_result(code, __FILE__, __LINE__)) +#define CHECK_ANY(code) \ +{ \ + do { \ + bool out = (check_any(code)); \ + if(!out) { \ + return out; \ + } \ + } while(0); \ +} +#define CHECK_CUFFTDX(code) \ +{ \ + do { \ + bool out = (check_cufftdx(code)); \ + if(!out) { \ + return out; \ + } \ + } while(0); \ +} +#define CHECK_CUBLASDX(code) \ +{ \ + do { \ + bool out = (check_cufftdx(code)); \ + if(!out) { \ + return out; \ + } \ + } while(0); \ +} bool check_nvrtc_result(nvrtcResult result, const char* file, int line) { @@ -74,6 +110,54 @@ bool check_nvptx_result(nvPTXCompileResult result, const char* file, int line) return false; } +bool check_nvjitlink_result(nvJitLinkHandle handle, nvJitLinkResult result, const char* file, int line) +{ + if (result != NVJITLINK_SUCCESS) { + fprintf(stderr, "nvJitLink error: %d on %s:%d\n", (int)result, file, line); + size_t lsize; + result = nvJitLinkGetErrorLogSize(handle, &lsize); + if (result == NVJITLINK_SUCCESS && lsize > 0) { + std::vector log(lsize); + result = nvJitLinkGetErrorLog(handle, log.data()); + if (result == NVJITLINK_SUCCESS) { + fprintf(stderr, "%s\n", log.data()); + } + } + return false; + } else { + return true; + } +} + +bool check_cufftdx_result(commonDxStatusType result, const char* file, int line) +{ + if (result != commonDxStatusType::COMMONDX_SUCCESS) { + fprintf(stderr, "libmathdx cuFFTDx error: %d on %s:%d\n", (int)result, file, line); + return false; + } else { + return true; + } +} + +bool check_cublasdx_result(commonDxStatusType result, const char* file, int line) +{ + if (result != commonDxStatusType::COMMONDX_SUCCESS) { + fprintf(stderr, "libmathdx cuBLASDx error: %d on %s:%d\n", (int)result, file, line); + return false; + } else { + return true; + } +} + +bool check_generic(int result, const char* file, int line) +{ + if (!result) { + fprintf(stderr, "Error %d on %s:%d\n", (int)result, file, line); + return false; + } else { + return true; + } +} struct DeviceInfo { @@ -2508,11 +2592,134 @@ bool cuda_graph_destroy(void* context, void* graph_exec) return check_cuda(cudaGraphExecDestroy((cudaGraphExec_t)graph_exec)); } -size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path) +bool write_file(const char* data, size_t size, std::string filename, const char* mode) +{ + const bool print_debug = (std::getenv("WARP_DEBUG") != nullptr); + if (print_debug) + { + printf("Writing %zu B to %s (%s)\n", size, filename.c_str(), mode); + } + FILE* file = fopen(filename.c_str(), mode); + if (file) + { + if (fwrite(data, 1, size, file) != size) { + fprintf(stderr, "Warp error: Failed to write to output file '%s'\n", filename.c_str()); + return false; + } + fclose(file); + return true; + } + else + { + fprintf(stderr, "Warp error: Failed to open file '%s'\n", filename.c_str()); + return false; + } +} + +bool cuda_compile_fft(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int size, int elements_per_thread, int direction, int precision, int* shared_memory_size) +{ + + CHECK_ANY(ltoir_output_path != nullptr); + CHECK_ANY(symbol_name != nullptr); + CHECK_ANY(mathdx_include_dir != nullptr); + CHECK_ANY(shared_memory_size != nullptr); + CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); + + bool res = true; + cufftdxHandle h; + CHECK_CUFFTDX(cufftDxCreate(&h)); + + // CUFFTDX_API_BLOCK_LMEM means each thread starts with a subset of the data + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_API, cufftDxApi::CUFFTDX_API_BLOCK_LMEM)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_EXECUTION, commonDxExecution::COMMONDX_EXECUTION_BLOCK)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_SIZE, (long long)size)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_DIRECTION, (cufftDxDirection)direction)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_PRECISION, (commonDxPrecision)precision)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_SM, (long long)(arch * 10))); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_ELEMENTS_PER_THREAD, (long long)(elements_per_thread))); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_FFTS_PER_BLOCK, 1)); + + CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); + for(int dir = 0; dir < num_include_dirs; dir++) + { + CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); + } + CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); + + size_t lto_size = 0; + CHECK_CUFFTDX(cufftDxGetLTOIRSize(h, <o_size)); + + std::vector lto(lto_size); + CHECK_CUFFTDX(cufftDxGetLTOIR(h, lto.size(), lto.data())); + + long long int smem = 0; + CHECK_CUFFTDX(cufftDxGetTraitInt64(h, cufftDxTraitType::CUFFTDX_TRAIT_SHARED_MEMORY_SIZE, &smem)); + *shared_memory_size = (int)smem; + + if(!write_file(lto.data(), lto.size(), ltoir_output_path, "wb")) { + res = false; + } + + CHECK_CUFFTDX(cufftDxDestroy(h)); + + return res; +} + +bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision, int type, int tA, int tB, int num_threads) +{ + + CHECK_ANY(ltoir_output_path != nullptr); + CHECK_ANY(symbol_name != nullptr); + CHECK_ANY(mathdx_include_dir != nullptr); + CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); + + bool res = true; + cublasdxHandle h; + CHECK_CUBLASDX(cublasDxCreate(&h)); + + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_FUNCTION, cublasDxFunction::CUBLASDX_FUNCTION_MM)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_EXECUTION, commonDxExecution::COMMONDX_EXECUTION_BLOCK)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_API, cublasDxApi::CUBLASDX_API_BLOCK_SMEM)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_PRECISION, (commonDxPrecision)precision)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SM, (long long)(arch * 10))); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TYPE, (cublasDxType)type)); + std::array block_dim = {num_threads, 1, 1}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_BLOCK_DIM, block_dim.size(), block_dim.data())); + std::array size = {M, N, K}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SIZE, size.size(), size.data())); + std::array transpose_mode = {(cublasDxTransposeMode_t)tA, (cublasDxTransposeMode_t)tB}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TRANSPOSE_MODE, transpose_mode.size(), transpose_mode.data())); + + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); + for(int dir = 0; dir < num_include_dirs; dir++) + { + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); + } + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/cublasdx/include").c_str())); + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/../external/cutlass/include").c_str())); + + size_t lto_size = 0; + CHECK_CUBLASDX(cublasDxGetLTOIRSize(h, <o_size)); + + std::vector lto(lto_size); + CHECK_CUBLASDX(cublasDxGetLTOIR(h, lto.size(), lto.data())); + + if(!write_file(lto.data(), lto.size(), ltoir_output_path, "wb")) { + res = false; + } + + CHECK_CUBLASDX(cublasDxDestroy(h)); + + return res; +} + +size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes) { // use file extension to determine whether to output PTX or CUBIN const char* output_ext = strrchr(output_path, '.'); bool use_ptx = output_ext && strcmp(output_ext + 1, "ptx") == 0; + const bool print_debug = (std::getenv("WARP_DEBUG") != nullptr); // check include dir path len (path + option) const int max_path = 4096 + 16; @@ -2522,17 +2729,37 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ return size_t(-1); } + if (print_debug) + { + // Not available in all nvJitLink versions + // unsigned major = 0; + // unsigned minor = 0; + // nvJitLinkVersion(&major, &minor); + // printf("nvJitLink version %d.%d\n", major, minor); + int major = 0; + int minor = 0; + nvrtcVersion(&major, &minor); + printf("NVRTC version %d.%d\n", major, minor); + } + char include_opt[max_path]; strcpy(include_opt, "--include-path="); strcat(include_opt, include_dir); const int max_arch = 128; char arch_opt[max_arch]; + char arch_opt_lto[max_arch]; if (use_ptx) + { snprintf(arch_opt, max_arch, "--gpu-architecture=compute_%d", arch); + snprintf(arch_opt_lto, max_arch, "-arch=compute_%d", arch); + } else + { snprintf(arch_opt, max_arch, "--gpu-architecture=sm_%d", arch); + snprintf(arch_opt_lto, max_arch, "-arch=sm_%d", arch); + } std::vector opts; opts.push_back(arch_opt); @@ -2561,13 +2788,22 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ sprintf(include_cutlass, "--include-path=%s/cutlass/include", include_dir); opts.push_back(include_cutlass); - //opts.push_back("--include-path=_build/target-deps/cuda/include"); - opts.push_back("--include-path=C:\\packman-repo\\chk\\cuda\\11.8.0_522.06-abe3d9d7-windows-x86_64\\include"); + std::vector cuda_include_opt; + for(int i = 0; i < num_cuda_include_dirs; i++) + { + cuda_include_opt.push_back(std::string("--include-path=") + cuda_include_dirs[i]); + opts.push_back(cuda_include_opt.back().c_str()); + } opts.push_back("--device-as-default-execution-space"); opts.push_back("--extra-device-vectorization"); opts.push_back("--restrict"); + if (num_ltoirs > 0) + { + opts.push_back("-dlto"); + opts.push_back("--relocatable-device-code=true"); + } nvrtcProgram prog; nvrtcResult res; @@ -2583,6 +2819,13 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ if (!check_nvrtc(res)) return size_t(res); + if (print_debug) + { + printf("NVRTC options:\n"); + for(auto o: opts) { + printf("%s\n", o); + } + } res = nvrtcCompileProgram(prog, int(opts.size()), opts.data()); if (!check_nvrtc(res) || verbose) @@ -2612,7 +2855,12 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ nvrtcResult (*get_output_size)(nvrtcProgram, size_t*); nvrtcResult (*get_output_data)(nvrtcProgram, char*); const char* output_mode; - if (use_ptx) + if(num_ltoirs > 0) { + get_output_size = nvrtcGetLTOIRSize; + get_output_data = nvrtcGetLTOIR; + output_mode = "wb"; + } + else if (use_ptx) { get_output_size = nvrtcGetPTXSize; get_output_data = nvrtcGetPTX; @@ -2634,19 +2882,73 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ res = get_output_data(prog, output.data()); if (check_nvrtc(res)) { - FILE* file = fopen(output_path, output_mode); - if (file) + + // LTOIR case - need an extra step + if (num_ltoirs > 0) { - if (fwrite(output.data(), 1, output_size, file) != output_size) + nvJitLinkHandle handle; + std::vector lopts = {"-dlto", arch_opt_lto}; + if (use_ptx) { + lopts.push_back("-ptx"); + } + if (print_debug) + { + printf("nvJitLink options:\n"); + for(auto o: lopts) { + printf("%s\n", o); + } + } + if(!check_nvjitlink(handle, nvJitLinkCreate(&handle, lopts.size(), lopts.data()))) { - fprintf(stderr, "Warp error: Failed to write output file '%s'\n", output_path); res = nvrtcResult(-1); } - fclose(file); + // Links + if(std::getenv("WARP_DUMP_LTOIR")) + { + write_file(output.data(), output.size(), "nvrtc_output.ltoir", "wb"); + } + if(!check_nvjitlink(handle, nvJitLinkAddData(handle, NVJITLINK_INPUT_LTOIR, output.data(), output.size(), "nvrtc_output"))) // NVRTC business + { + res = nvrtcResult(-1); + } + for(size_t ltoidx = 0; ltoidx < num_ltoirs; ltoidx++) + { + if(std::getenv("WARP_DUMP_LTOIR")) + { + write_file(ltoirs[ltoidx], ltoir_sizes[ltoidx], std::string("lto_online_") + std::to_string(ltoidx) + ".ltoir", "wb"); + } + if(!check_nvjitlink(handle, nvJitLinkAddData(handle, NVJITLINK_INPUT_LTOIR, ltoirs[ltoidx], ltoir_sizes[ltoidx], "lto_online"))) // External LTOIR + { + res = nvrtcResult(-1); + } + } + if(!check_nvjitlink(handle, nvJitLinkComplete(handle))) + { + res = nvrtcResult(-1); + } + else + { + if(use_ptx) + { + size_t ptx_size = 0; + check_nvjitlink(handle, nvJitLinkGetLinkedPtxSize(handle, &ptx_size)); + std::vector ptx(ptx_size); + check_nvjitlink(handle, nvJitLinkGetLinkedPtx(handle, ptx.data())); + output = ptx; + } + else + { + size_t cubin_size = 0; + check_nvjitlink(handle, nvJitLinkGetLinkedCubinSize(handle, &cubin_size)); + std::vector cubin(cubin_size); + check_nvjitlink(handle, nvJitLinkGetLinkedCubin(handle, cubin.data())); + output = cubin; + } + } + check_nvjitlink(handle, nvJitLinkDestroy(&handle)); } - else - { - fprintf(stderr, "Warp error: Failed to open output file '%s'\n", output_path); + + if(!write_file(output.data(), output.size(), output_path, output_mode)) { res = nvrtcResult(-1); } } diff --git a/warp/native/warp.h b/warp/native/warp.h index 52f67664..a089a6c8 100644 --- a/warp/native/warp.h +++ b/warp/native/warp.h @@ -307,7 +307,9 @@ extern "C" WP_API bool cuda_graph_launch(void* graph, void* stream); WP_API bool cuda_graph_destroy(void* context, void* graph); - WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_file); + WP_API bool cuda_compile_fft(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int size, int elements_per_thread, int direction, int precision, int* shared_memory_size); + WP_API bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision, int type, int tA, int tB, int num_threads); + WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes); WP_API void* cuda_load_module(void* context, const char* ptx); WP_API void cuda_unload_module(void* context, void* module); diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index e1bfd21b..cab691ac 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -407,6 +407,81 @@ def test_tile_extract(): print("Extract backward passed") +@wp.kernel() +def tile_matmul_dx_kernel(ga: wp.array2d(dtype=wp.float64), + gb: wp.array2d(dtype=wp.float64), + gc: wp.array2d(dtype=wp.float64)): + i, j, _ = wp.tid() + a = wp.tile_load(ga, i, j, m=TILE_M, n=TILE_K) + b = wp.tile_load(gb, i, j, m=TILE_K, n=TILE_N) + c = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float64) + wp.tile_matmul_dx(a, b, c) + wp.tile_store(gc, i, j, c) + +def test_tile_matmul_dx(): + + rng = np.random.default_rng(42) + + A = rng.random((TILE_M, TILE_K), dtype=np.float64) + B = rng.random((TILE_K, TILE_N), dtype=np.float64) + C = np.zeros((TILE_M, TILE_N), dtype=np.float64) + + A_wp = wp.array(A, requires_grad=True) + B_wp = wp.array(B, requires_grad=True) + C_wp = wp.array(C, requires_grad=True) + + with wp.Tape() as tape: + wp.launch(tile_matmul_dx_kernel, dim=[1, 1, TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) + + # verify forward pass + assert(np.allclose(A @ B, C_wp.numpy(), rtol=1.e-4)) + + print("Matmul (Dx) forward passed") + + adj_C = np.ones_like(C) + + tape.backward(grads={C_wp: wp.array(adj_C)}) + + assert(np.allclose(adj_C@B.T, A_wp.grad.numpy(), rtol=1.e-4)) + assert(np.allclose(A.T@adj_C, B_wp.grad.numpy(), rtol=1.e-4)) + + print("Matmul (Dx) backward passed") + +N_FFT = 128 + +@wp.kernel() +def tile_fft_dx_kernel(gx: wp.array2d(dtype=wp.vec2f), + gy: wp.array2d(dtype=wp.vec2f)): + i, j, _ = wp.tid() + xy = wp.tile_load(gx, i, j, m=N_FFT, n=N_FFT) + wp.tile_fft_dx(xy) + wp.tile_store(gy, i, j, xy) + +def test_tile_fft_dx(): + + rng = np.random.default_rng(42) + + # Warp doesn't really have a complex64 type, + # so we use 2 float32 to represent a single complex64 number and then convert it to vec2f + + X = rng.random((N_FFT, 2*N_FFT), dtype=np.float32) + Y = np.zeros_like(X) + + X_wp = wp.array2d(X, requires_grad=True, dtype=wp.vec2f) + Y_wp = wp.array2d(Y, requires_grad=True, dtype=wp.vec2f) + + X_c64 = X.view(np.complex64).reshape(N_FFT, N_FFT) + Y_c64 = np.fft.fft(X_c64, axis=-1) + + with wp.Tape() as tape: + wp.launch(tile_fft_dx_kernel, dim=[1, 1, TILE_DIM], inputs=[X_wp, Y_wp], block_dim=TILE_DIM) + + Y_wp_c64 = Y_wp.numpy().view(np.complex64).reshape(N_FFT, N_FFT) + assert(np.allclose(Y_c64, Y_wp_c64, rtol=1.e-4)) + + print("FFT (Dx) forward passed") + + # TODO: implement and test backward pass test_tile_copy() test_tile_unary_map() @@ -416,6 +491,8 @@ def test_tile_extract(): test_tile_operators() test_tile_sum() test_tile_extract() +test_tile_matmul_dx() +test_tile_fft_dx() # #----------------------------------------- # # center of mass computation diff --git a/warp/types.py b/warp/types.py index ec36adc3..6cc00d90 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2888,8 +2888,8 @@ def ctype(self): def cinit(self, adjoint=False): from warp.codegen import Var - if self.storage == "register": - return f"{0}" + if self.storage == "register": + return self.ctype() + "(0.0)" elif self.storage == "shared": if adjoint: From f0d9abfec26bea3c34c0358dd5d945cfb3dd9f34 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 25 Sep 2024 07:54:26 +0000 Subject: [PATCH 032/102] Add support for constructing tiles from thread local values using wp.tile() Add support for wp.tile_atomic_add() to global memory Add support for wp.print() on tiles --- warp/builtins.py | 74 ++++++++++++++++++- warp/codegen.py | 6 ++ warp/native/tile.h | 127 +++++++++++++++++++++++++++++++-- warp/tests/test_tile_reduce.py | 122 +++++++++++++++++++++++++++++++ 4 files changed, 320 insertions(+), 9 deletions(-) create mode 100644 warp/tests/test_tile_reduce.py diff --git a/warp/builtins.py b/warp/builtins.py index 23742e97..3b74d18d 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1712,7 +1712,7 @@ def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str # return generic type (for doc builds) if arg_types is None: - return array_t(shape=(Any, Any), dtype=Scalar) + return Tile(dtype=Any, M=Any, N=Any) # if len(arg_types) > 0: # raise RuntimeError("tile_zero() args must be passed by keyword") @@ -1759,7 +1759,7 @@ def tile_load_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: - return array_t(shape=(Any, Any), dtype=Scalar) + return Tile(dtype=Any, M=Any, N=Any) # if len(arg_types) != 3: # raise RuntimeError("tile_load() requires 3 positional args") @@ -1841,7 +1841,75 @@ def tile_store_value_func(arg_types, arg_values): input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, value_func=tile_store_value_func, variadic=True, - doc="Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)", + doc="Store tile `t` to an array `a` at offset `(i=x*m, j=y*n)`", + group="Tile Primitives", + export=False, +) + +def tile_atomic_add_value_func(arg_types, arg_values): + + # return generic type (for doc builds) + if arg_types is None: + return Tile(dtype=Any, M=Any, N=Any) + + if len(arg_types) != 4: + raise RuntimeError("tile_atomic_add() requires 4 positional args") + + if not is_array(arg_types["a"]): + raise RuntimeError("tile_atomic_add() argument 0 must be an array") + + if not type_is_int(arg_types["x"]): + raise RuntimeError("tile_atomic_add() argument 1 must be an integer") + + if not type_is_int(arg_types["y"]): + raise RuntimeError("tile_atomic_add() argument 2 must be an integer") + + if not is_tile(arg_types["t"]): + raise RuntimeError("tile_atomic_add() argument 3 must be a tile") + + if arg_types["a"].dtype != arg_types["t"].dtype: + raise RuntimeError("tile_atomic_add() tile dtype and array dtype must match") + + return Tile(dtype=arg_types["t"].dtype, + M=arg_types["t"].M, + N=arg_types["t"].N) + + + +add_builtin( + "tile_atomic_add", + input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, + value_func=tile_atomic_add_value_func, + variadic=True, + doc="Atomically add a tile `t` worth of data to array `a` at offset `(i=x*m, j=y*n)`", + group="Tile Primitives", + export=False, +) + + +def tile_value_func(arg_types, arg_values): + + # return generic type (for doc builds) + if arg_types is None: + return Tile + + if len(arg_types) != 1: + raise RuntimeError("tile() requires 1 positional arg") + + # todo: we need a way to pass things like current compiler options + # into the value_func, for now we use a single global options dictionary + # we should ideally pass in the Adjoint object if it exists + + return Tile(dtype=arg_types["x"], M=1, N=warp.codegen.options["block_dim"], op="Tile") + + + +add_builtin( + "tile", + input_types={"x": Any}, + value_func=tile_value_func, + variadic=True, + doc="Construct a Tile from a per-thread kernel value, returns a tile with dimensions of `(1, block_dim)` where block_dim is the number of threads specified in `wp.launch()`", group="Tile Primitives", export=False, ) diff --git a/warp/codegen.py b/warp/codegen.py index e5a21a83..fc2da1f6 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -23,6 +23,9 @@ import warp.config from warp.types import * +# used as a globally accessible copy +# of current compile options (block_dim) etc +options = {} class WarpCodegenError(RuntimeError): def __init__(self, message): @@ -917,6 +920,9 @@ def build(adj, builder, default_builder_options=None): else: adj.builder_options = default_builder_options + global options + options = adj.builder_options + adj.symbols = {} # map from symbols to adjoint variables adj.variables = [] # list of local variables (in order) diff --git a/warp/native/tile.h b/warp/native/tile.h index 10ea0830..7563e0d9 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -268,6 +268,8 @@ struct tile_register_t } } + inline CUDA_CALLABLE void print(); + // return the in-register version of this tile (nop) inline CUDA_CALLABLE auto& copy_to_register() { return *this; } @@ -327,7 +329,6 @@ struct tile_register_t data[i] = ptr[c.i*stride_i + c.j*stride_j]; } } - }; @@ -466,15 +467,17 @@ struct tile_shared_t inline CUDA_CALLABLE void print() { + WP_TILE_SYNC(); + if (threadIdx.x == 0) { - printf("["); + printf("Tile(M=%d, N=%d, storage=shared) = [\n", M, N); for (int i=0; i < M; ++i) { printf("%*s[", i>0, ""); for (int j=0; j < N; ++j) { - printf("%5.2f ", (*this)(i, j)); + printf("%g ", double((*this)(i, j))); } if (i == M-1) @@ -553,6 +556,52 @@ struct tile_shared_t } }; +template +void tile_register_t::print() +{ + // create a temporary shared tile so that + // we can print it deterministically + WP_TILE_SHARED T smem[M*N]; + + tile_shared_t scratch(smem); + scratch.assign(*this); + + WP_TILE_SYNC(); + + if (threadIdx.x == 0) + { + printf("Tile(M=%d, N=%d, storage=register) = [\n", M, N); + for (int i=0; i < M; ++i) + { + printf("%*s[", i>0, ""); + for (int j=0; j < N; ++j) + { + printf("%g ", double(scratch(i, j))); + } + + if (i == M-1) + printf("]]\n"); + else + printf("]\n"); + } + } + + WP_TILE_SYNC(); +} + +template +inline CUDA_CALLABLE void print(Tile& t) +{ + t.print(); +} + +template +inline CUDA_CALLABLE void adj_print(Tile& t, AdjTile& a) +{ + a.print(); +} + + // helpers to allocate shared tiles template inline CUDA_CALLABLE auto tile_alloc_empty() @@ -579,14 +628,44 @@ inline CUDA_CALLABLE auto tile_transpose(Tile& t) return tile_shared_t(t.data); } - //----------------------------------------------------------------------------------------------------- // High level entry points for each op (correspond to one Warp builtin) +// construct a tile from a local SIMT value (one per-thread) +template +inline CUDA_CALLABLE auto tile(const T& x) +{ + tile_register_t result; + + // code-gen should have set the tile to + // have exactly the block dimension so + // there is exactly one value per-thread + static_assert(result.NumRegs == 1); + + result.data[0] = x; + return result; +} + +// construct a tile from a local SIMT value (one per-thread) +template +inline CUDA_CALLABLE void adj_tile(const T& x, T& adj_x, const AdjTile& adj_ret) +{ + static_assert(AdjTile::M == 1); + static_assert(AdjTile::N == WP_TILE_BLOCK_DIM); + + // code-gen should have set the tile to + // have exactly the block dimension so + // there is exactly one value per-thread + static_assert(AdjTile::NumRegs == 1); + + adj_x += adj_ret.data[0]; +} + +// zero initialized tile template inline CUDA_CALLABLE auto tile_zeros() { - // tile variable assignment operator will handle initialization + // tile variable assignment operator will handle initialization (since lhs could be shared/register tile) return T(0.0); } @@ -609,6 +688,35 @@ inline CUDA_CALLABLE void tile_store(array_t& dest, int x, int y, Tile& src) src.copy_to_global(dest, x, y); } +// entry point for store +template +inline CUDA_CALLABLE auto tile_atomic_add(array_t& dest, int x, int y, Tile& src) +{ + auto src_reg = src.copy_to_register(); + + const int tile_i = x*src_reg.M; + const int tile_j = y*src_reg.N; + + tile_register_t previous; + + WP_PRAGMA_UNROLL + for (int i=0; i < src_reg.NumRegs; ++i) + { + // handle case where tile size is not + // aligned to block dimensions + int linear = src_reg.index(i); + if (!src_reg.Aligned && linear >= src_reg.Size) + break; + + coord_t c = src_reg.coord(linear); + previous.data[i] = atomic_add(dest, tile_i + c.i, tile_j + c.j, src_reg.data[i]); + } + + return previous; +} + + + //------------------------------------- // Adjoints @@ -674,9 +782,16 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t } // store adjoint back to tile - adj_t.assign(adj_reg); + adj_t.assign(adj_reg); } +template +inline CUDA_CALLABLE void adj_tile_atomic_add(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, AdjTile& adj_t, AdjRet& adj_ret) +{ + adj_tile_store(dest, x, y, t, adj_dest, adj_x, adj_y, adj_t); +} + + // unary map template inline CUDA_CALLABLE auto tile_map(Fwd op, diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py new file mode 100644 index 00000000..a71e08d3 --- /dev/null +++ b/warp/tests/test_tile_reduce.py @@ -0,0 +1,122 @@ +import numpy as np +import warp as wp + +wp.init() +wp.set_module_options({"enable_backward": True}) +wp.set_device("cuda:0") +wp.set_module_options({"fast_math": True}) +#wp.config.mode = "debug" +#wp.config.verify_cuda = True + +wp.build.clear_kernel_cache() + +TILE_M = wp.constant(8) +TILE_N = wp.constant(4) +TILE_K = wp.constant(8) + +# num threads per-tile +TILE_DIM = 64 + + +@wp.kernel +def tile_sum_kernel(input: wp.array3d(dtype=float), + output: wp.array(dtype=float)): + + # output tile index + i, _ = wp.tid() + + a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) + s = wp.tile_sum(a)*0.5 + + wp.tile_store(output, i, 0, s) + +def test_tile_sum(): + + batch_count = 56 + + M = TILE_M + N = TILE_N + + rng = np.random.default_rng(42) + input = rng.random((batch_count, M, N), dtype=np.float32) + + input_wp = wp.array(input, requires_grad=True) + output_wp = wp.zeros(batch_count, requires_grad=True) + + with wp.Tape() as tape: + wp.launch(tile_sum_kernel, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) + + + for i in range(batch_count): + sum_np = np.sum(input[i])*0.5 + sum_wp = output_wp.numpy()[i] + + assert(np.allclose(sum_np, sum_wp, rtol=1.e-4)) + + print("Sum forward passed") + + output_wp.grad.fill_(1.0) + + tape.backward() + + assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.5, rtol=1.e-4)) + + print("Sum backward passed") + + + +@wp.kernel +def tile_reduce_1d_kernel(output: wp.array(dtype=int)): + + # output tile index + i = wp.tid() + + t = wp.tile(i) # convert to block wide tile + s = wp.tile_sum(t) # sum over block + + # update global sum + wp.tile_atomic_add(output, i, 0, s) + +def test_tile_reduce_1d(): + + N = int(TILE_DIM*3/2) + + output = wp.zeros(shape=1, dtype=int, requires_grad=True) + + with wp.Tape() as tape: + wp.launch(tile_reduce_1d_kernel, dim=[N], inputs=[output], block_dim=TILE_DIM) + + assert(np.sum(np.arange(N)), output.numpy()) + + print("Sum 1D forward passed") + + # output_wp.grad.fill_(1.0) + + # tape.backward() + + # assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.5, rtol=1.e-4)) + + # print("Sum backward passed") + + +test_tile_sum() +test_tile_reduce_1d() + + + + + + + + + + + + + + + + + + + From 22a843cf1b0a352ae2bd9e024f9d538a87a97f21 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 25 Sep 2024 16:38:15 -0700 Subject: [PATCH 033/102] Build-related updates for mathdx+tile --- .gitlab-ci.yml | 27 ++ .gitlab/ci/common.yml | 1 + .gitlab/ci/cuda-11-build-and-test.yml | 2 +- .gitlab/ci/debug-build-and-test.yml | 2 +- .gitlab/ci/mathdx-support.yml | 169 +++++++ UNKNOWN.egg-info/PKG-INFO | 11 - UNKNOWN.egg-info/SOURCES.txt | 8 - UNKNOWN.egg-info/dependency_links.txt | 1 - UNKNOWN.egg-info/top_level.txt | 1 - build_lib.py | 24 +- deps/cuda-toolkit-deps.packman.xml | 6 +- docs/modules/functions.rst | 99 ++++ examples/tile_fft.py | 13 +- examples/tile_matmul.py | 9 +- tools/ci/building/build-linux-x86_64/build.sh | 2 +- warp/build.py | 34 +- warp/build_dll.py | 24 +- warp/builtins.py | 295 ++++++------ warp/codegen.py | 16 +- warp/context.py | 110 +++-- warp/examples/benchmarks/benchmark_tile.py | 85 ++-- warp/mathdx.py | 151 ++++++ warp/native/mathdx.cpp | 56 +++ warp/native/tile_gemm.h | 5 +- warp/native/warp.cpp | 7 +- warp/native/warp.cu | 243 +++++----- warp/native/warp.h | 4 +- warp/stubs.py | 102 ++++ warp/tape.py | 15 +- warp/tests/test_mat_scalar_ops.py | 2 +- warp/tests/test_spatial.py | 2 +- warp/tests/test_tile.py | 444 +++++++----------- warp/tests/test_tile_mathdx.py | 116 +++++ warp/tests/test_tile_reduce.py | 107 ++--- warp/tests/unittest_utils.py | 4 + warp/types.py | 23 +- 36 files changed, 1431 insertions(+), 789 deletions(-) create mode 100644 .gitlab/ci/mathdx-support.yml delete mode 100644 UNKNOWN.egg-info/PKG-INFO delete mode 100644 UNKNOWN.egg-info/SOURCES.txt delete mode 100644 UNKNOWN.egg-info/dependency_links.txt delete mode 100644 UNKNOWN.egg-info/top_level.txt create mode 100644 warp/mathdx.py create mode 100644 warp/native/mathdx.cpp create mode 100644 warp/tests/test_tile_mathdx.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7a5e2012..5eb130f6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -30,6 +30,7 @@ variables: PIP_CACHE_DIR: '$CI_PROJECT_DIR/.cache/pip' CUDA_BIN: '$CI_PROJECT_DIR/_build/target-deps/cuda/bin' CUDA: '$CI_PROJECT_DIR/_build/target-deps/cuda' + CUDA_HOME: '$CI_PROJECT_DIR/_build/target-deps/cuda' PYTHON: '$CI_PROJECT_DIR/_build/target-deps/python/python' LINBUILD: '$CI_PROJECT_DIR/_build/host-deps/linbuild/linbuild.sh' WARP_CACHE_ROOT: '$CI_PROJECT_DIR/.cache/warp' # Used by the parallel test runner @@ -493,6 +494,32 @@ debug build and test: extends: - .trigger_common +trigger mathdx support pipeline: + stage: test + image: busybox + extends: + - .runner-utility-linux-x86_64 + needs: [] + rules: + - if: $CI_PIPELINE_SOURCE == "schedule" + - if: $CI_COMMIT_TAG + - if: $CI_COMMIT_BRANCH =~ /^release-.*/ + - when: manual # Can be triggered in all other scenarios + allow_failure: true + variables: + GIT_STRATEGY: none + script: + - echo "Run this job to test Warp compiled with mathdx support." + +# Uses the same Python version as the main pipeline. +mathdx support: + stage: child pipelines + needs: [trigger mathdx support pipeline] + trigger: + include: /.gitlab/ci/mathdx-support.yml + extends: + - .trigger_common + # Trigger CUDA 11 pipelines # Workaround from https://gitlab.com/gitlab-org/gitlab/-/issues/284086 trigger cuda 11 pipeline: diff --git a/.gitlab/ci/common.yml b/.gitlab/ci/common.yml index f0d6463a..e8b8c5ee 100644 --- a/.gitlab/ci/common.yml +++ b/.gitlab/ci/common.yml @@ -106,6 +106,7 @@ include: PIP_CACHE_DIR: '$PARENT_PROJECT_DIR/.cache/pip' CUDA_BIN: '$PARENT_PROJECT_DIR/_build/target-deps/cuda/bin' CUDA: '$PARENT_PROJECT_DIR/_build/target-deps/cuda' + CUDA_HOME: '$CI_PROJECT_DIR/_build/target-deps/cuda' PYTHON: '$PARENT_PROJECT_DIR/_build/target-deps/python/python' LINBUILD: '$PARENT_PROJECT_DIR/_build/host-deps/linbuild/linbuild.sh' WARP_CACHE_ROOT: '$PARENT_PROJECT_DIR/.cache/warp' # Used by the parallel test runner diff --git a/.gitlab/ci/cuda-11-build-and-test.yml b/.gitlab/ci/cuda-11-build-and-test.yml index 483a66bd..735104ea 100644 --- a/.gitlab/ci/cuda-11-build-and-test.yml +++ b/.gitlab/ci/cuda-11-build-and-test.yml @@ -187,7 +187,7 @@ create pypi wheels: - find . -type d -exec chmod 775 {} + artifacts: name: $PARENT_COMMIT_REF_SLUG-$PARENT_COMMIT_SHORT_SHA - expose_as: "Python Wheels" + expose_as: "Python Wheels Cu11" paths: - "dist/" when: always diff --git a/.gitlab/ci/debug-build-and-test.yml b/.gitlab/ci/debug-build-and-test.yml index e86f553d..3ebeeade 100644 --- a/.gitlab/ci/debug-build-and-test.yml +++ b/.gitlab/ci/debug-build-and-test.yml @@ -196,7 +196,7 @@ create pypi wheels: - find . -type d -exec chmod 775 {} + artifacts: name: $CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA - expose_as: "Python Wheels" + expose_as: "Python Wheels Debug" paths: - "dist/" when: always diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml new file mode 100644 index 00000000..5bea3383 --- /dev/null +++ b/.gitlab/ci/mathdx-support.yml @@ -0,0 +1,169 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +# ============================================================================== +# CI/CD Pipeline Configuration +# ============================================================================== + +include: /.gitlab/ci/common.yml + +workflow: + rules: + - if: $CI_PIPELINE_SOURCE == "parent_pipeline" + +stages: + - build + - test + - package + - deploy + +# ============================================================================== +# Build Jobs (Release) +# ============================================================================== + +linux-x86_64 build: + stage: build + image: ubuntu:20.04 + extends: + - .save_warp_bin_artifact + - .runner-build-linux-x86_64 + before_script: + - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" + - apt-get update && apt-get install build-essential curl wget --no-install-recommends -y + - > + wget --header="X-JFrog-Art-Api:$ARTIFACTORY_API_KEY" -nv --no-check-certificate + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/30/libmathdx_build_x86_64_ubuntu20.04_cuda12.0.0_release.tar.gz + -O libmathdx.tar.gz + - mkdir -p _build/target-deps + - tar -xzf libmathdx.tar.gz -C _build/target-deps + - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" + - gcc --version + - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" + script: + - ./tools/ci/building/build-linux-x86_64/build.sh --no-docker # We are already using the builder image + - mkdir -p warp/bin/linux-x86_64 + - mv warp/bin/warp.so warp/bin/linux-x86_64 + - mv warp/bin/warp-clang.so warp/bin/linux-x86_64 + +linux-aarch64 build: + stage: build + image: ubuntu:20.04 + extends: + - .save_warp_bin_artifact + before_script: + - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" + - apt-get update && apt-get install build-essential curl wget --no-install-recommends -y + - > + wget --header="X-JFrog-Art-Api:$ARTIFACTORY_API_KEY" -nv --no-check-certificate + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/30/libmathdx_build_aarch64_ubuntu20.04_cuda12.0.0_release.tar.gz + -O libmathdx.tar.gz + - mkdir -p _build/target-deps + - tar -xzf libmathdx.tar.gz -C _build/target-deps + - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" + - gcc --version + - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" + script: + - ./tools/ci/building/build-linux-x86_64/build.sh --no-docker # We are already using the builder image + - mkdir -p warp/bin/linux-aarch64 + - mv warp/bin/warp.so warp/bin/linux-aarch64 + - mv warp/bin/warp-clang.so warp/bin/linux-aarch64 + tags: + - arch/arm + +# ============================================================================== +# Unit Testing Jobs (MathDx Support) +# +# Unlike the main testing jobs defined in /.gitlab-ci.yml, the jobs don't +# generate code coverage reports. +# ============================================================================== + +linux-x86_64 test: + stage: test + needs: [linux-x86_64 build] + extends: + - .omni_nvks_gpu_2x + - .save_test_report_artifact + before_script: + - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" + - df -h + # Move compiled binaries out of platform-specific directory + - mv warp/bin/linux-x86_64/warp.so warp/bin/ + - mv warp/bin/linux-x86_64/warp-clang.so warp/bin/ + - tools/packman/packman install -l _build/target-deps/python python ${DEFAULT_PYTHON}-linux-x86_64 + - export PATH="$CUDA_BIN:$PATH" + - $PYTHON -m venv _venv + - source _venv/bin/activate + - python -m pip install --upgrade pip + - python -m pip install --upgrade usd-core + - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 + - python -m pip install --upgrade "jax[cuda12_pip]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html + - python -m pip install --upgrade nvidia-mathdx==24.4.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 + - python -m pip install -e . + - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" + # HACK: disable P2P tests due to misbehaving agents + - export WARP_DISABLE_P2P_TESTS=1 + script: + - python -m warp.tests --junit-report-xml rspec.xml -s autodetect --failfast + +linux-aarch64 test jetson: + image: ubuntu:22.04 + needs: [linux-aarch64 build] + extends: + - .save_test_report_artifact + before_script: + - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" + - !reference [.snippets, install-python+warp-aarch64] + - python -m pip install --upgrade nvidia-mathdx==24.4.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 + - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" + script: + - python -m warp.tests --junit-report-xml rspec.xml -s autodetect --failfast + tags: + - gpu/orin + +# ============================================================================== +# Packaging Jobs +# ============================================================================== + +# Creates wheel files for PyPI +create pypi wheels: + stage: package + needs: + - linux-aarch64 build + - linux-x86_64 build + extends: + - .runner-utility-linux-x86_64 + before_script: + - python3 -m pip install --upgrade pip + - python3 -m pip install build + script: + - sed -i 's/dependencies = \["numpy"\]/dependencies = \["numpy", "nvidia-mathdx==24.4.0", "nvidia-cuda-cccl-cu12", "nvidia-cuda-runtime-cu12"\]/' pyproject.toml + - sed -i "s/^\(.*\)$/\1+tile/" VERSION.md # Modify VERSION.md with +tile + - python3 -m build --wheel -C--build-option=-Plinux-x86_64 + - python3 -m build --wheel -C--build-option=-Plinux-aarch64 + - find . -type f -exec chmod 664 {} + + - find . -type d -exec chmod 775 {} + + artifacts: + name: $CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA + expose_as: "Python Wheels MathDx" + paths: + - "dist/" + when: always + +publish wheels to gitlab pypi registry: + stage: deploy + image: python:3.11-slim + needs: ["create pypi wheels"] + extends: + - .runner-utility-linux-x86_64 + rules: + - when: manual + allow_failure: true + before_script: + - python3 -m pip install --upgrade pip + - python3 -m pip install --upgrade build twine + script: + - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python3 -m twine upload --verbose --skip-existing --non-interactive --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/* diff --git a/UNKNOWN.egg-info/PKG-INFO b/UNKNOWN.egg-info/PKG-INFO deleted file mode 100644 index 9f5ddb3d..00000000 --- a/UNKNOWN.egg-info/PKG-INFO +++ /dev/null @@ -1,11 +0,0 @@ -Metadata-Version: 2.1 -Name: UNKNOWN -Version: 0.0.0 -Summary: UNKNOWN -Home-page: UNKNOWN -License: UNKNOWN -Platform: UNKNOWN -License-File: LICENSE.md - -UNKNOWN - diff --git a/UNKNOWN.egg-info/SOURCES.txt b/UNKNOWN.egg-info/SOURCES.txt deleted file mode 100644 index 0bbe9384..00000000 --- a/UNKNOWN.egg-info/SOURCES.txt +++ /dev/null @@ -1,8 +0,0 @@ -LICENSE.md -README.md -pyproject.toml -setup.py -UNKNOWN.egg-info/PKG-INFO -UNKNOWN.egg-info/SOURCES.txt -UNKNOWN.egg-info/dependency_links.txt -UNKNOWN.egg-info/top_level.txt \ No newline at end of file diff --git a/UNKNOWN.egg-info/dependency_links.txt b/UNKNOWN.egg-info/dependency_links.txt deleted file mode 100644 index 8b137891..00000000 --- a/UNKNOWN.egg-info/dependency_links.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/UNKNOWN.egg-info/top_level.txt b/UNKNOWN.egg-info/top_level.txt deleted file mode 100644 index 8b137891..00000000 --- a/UNKNOWN.egg-info/top_level.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/build_lib.py b/build_lib.py index 3827c4cd..7aea44fe 100644 --- a/build_lib.py +++ b/build_lib.py @@ -17,6 +17,7 @@ import argparse import glob import os +import platform import shutil from warp.build_dll import build_dll, find_host_compiler, set_msvc_env, verbose_cmd @@ -26,6 +27,7 @@ parser.add_argument("--msvc_path", type=str, help="Path to MSVC compiler (optional if already on PATH)") parser.add_argument("--sdk_path", type=str, help="Path to WinSDK (optional if already on PATH)") parser.add_argument("--cuda_path", type=str, help="Path to CUDA SDK") +parser.add_argument("--libmathdx_path", type=str, help="Path to libmathdx (optional if LIBMATHDX_HOME is defined)") parser.add_argument( "--mode", type=str, @@ -70,6 +72,7 @@ parser.add_argument("--no_standalone", dest="standalone", action="store_false") parser.set_defaults(standalone=True) + args = parser.parse_args() # set build output path off this file @@ -97,7 +100,7 @@ def find_cuda_sdk(): return cuda_sdk # check default paths - if os.name == "nt": + if platform.system() == "Windows": cuda_paths = glob.glob("C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v*.*") if len(cuda_paths) >= 1: cuda_sdk = cuda_paths[0] @@ -115,16 +118,21 @@ def find_cuda_sdk(): # setup CUDA Toolkit path -if sys.platform == "darwin": +if platform.system() == "Darwin": args.cuda_path = None - else: if not args.cuda_path: args.cuda_path = find_cuda_sdk() + if not args.libmathdx_path: + libmathdx_path = os.environ.get("LIBMATHDX_HOME") + + if libmathdx_path: + print(f"Using libmathdx path '{libmathdx_path}' provided through the 'LIBMATHDX_HOME' environment variable") + args.libmathdx_path = libmathdx_path # setup MSVC and WinSDK paths -if os.name == "nt": +if platform.system() == "Windows": if args.msvc_path or args.sdk_path: # user provided MSVC and Windows SDK assert args.msvc_path and args.sdk_path, "--msvc_path and --sdk_path must be used together." @@ -141,9 +149,9 @@ def find_cuda_sdk(): # return platform specific shared library name def lib_name(name): - if sys.platform == "win32": + if platform.system() == "Windows": return f"{name}.dll" - elif sys.platform == "darwin": + elif platform.system() == "Darwin": return f"lib{name}.dylib" else: return f"{name}.so" @@ -189,6 +197,7 @@ def generate_exports_header_file(): "native/volume.cpp", "native/marching.cpp", "native/cutlass_gemm.cpp", + "native/mathdx.cpp", ] warp_cpp_paths = [os.path.join(build_path, cpp) for cpp in cpp_sources] @@ -198,6 +207,9 @@ def generate_exports_header_file(): else: warp_cu_path = os.path.join(build_path, "native/warp.cu") + if args.libmathdx_path is None: + print("Warning: libmathdx not found, building without MathDx support") + warp_dll_path = os.path.join(build_path, f"bin/{lib_name('warp')}") build_dll(args, dll_path=warp_dll_path, cpp_paths=warp_cpp_paths, cu_path=warp_cu_path) diff --git a/deps/cuda-toolkit-deps.packman.xml b/deps/cuda-toolkit-deps.packman.xml index 7b3b4e6d..0024ee86 100644 --- a/deps/cuda-toolkit-deps.packman.xml +++ b/deps/cuda-toolkit-deps.packman.xml @@ -7,9 +7,9 @@ - - - + + + diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 080a6e7d..bcd18cc9 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -800,6 +800,77 @@ Spatial Math +Tile Primitives +--------------- +.. py:function:: tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile + + Allocate a tile local block of zero'd memory + + +.. py:function:: tile_load(a: Array[Any], x: int32, y: int32, m: int32, n: int32) -> Tile + + Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n) + + +.. py:function:: tile_store(a: Array[Any], x: int32, y: int32, t: Any) -> None + + Store tile `t` to an array `a` at offset `(i=x*m, j=y*n)` + + +.. py:function:: tile_atomic_add(a: Array[Any], x: int32, y: int32, t: Any) -> Tile + + Atomically add a tile `t` worth of data to array `a` at offset `(i=x*m, j=y*n)` + + +.. py:function:: tile(x: Any) -> Tile + + Construct a Tile from a per-thread kernel value, returns a tile with dimensions of `(1, block_dim)` where block_dim is the number of threads specified in `wp.launch()` + + +.. py:function:: tile_extract(a: Tile, i: int32, j: int32) -> None + + Extract element at index (i, j) of the tile and return the native type + + +.. py:function:: tile_matmul(a: Tile, b: Tile, out: Tile) -> None + + Compute matrix product and accumulate out += a*b. + + +.. py:function:: tile_sum(a: Tile) -> None + + Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored + + +.. py:function:: tile_map(op: Callable, a: Any) -> None + + Map the operation onto each element of the tile + + +.. py:function:: tile_map(op: Callable, a: Any, b: Any) -> None + :noindex: + :nocontentsentry: + + Map the operation onto each element of the tile + + +.. py:function:: tile_matmul_dx(a: Tile, b: Tile, out: Tile) -> None + + Compute matrix product and accumulate out += a*b. + + +.. py:function:: tile_fft_dx(inout: Tile) -> None + + Compute the FFT along the second dimension of a 2D tile of data. + + +.. py:function:: tile_ifft_dx(inout: Tile) -> None + + Compute the inverse FFT along the second dimension of a 2D tile of data. + + + + Utility --------------- .. py:function:: mlp(weights: Array[float32], bias: Array[float32], activation: Callable, index: int32, x: Array[float32], out: Array[float32]) -> None @@ -1961,6 +2032,13 @@ Operators :nocontentsentry: +.. py:function:: add(a: Tile, b: Tile) -> None + :noindex: + :nocontentsentry: + + Add each element of two tiles together + + .. py:function:: sub(a: Scalar, b: Scalar) -> Scalar @@ -2052,6 +2130,20 @@ Operators :nocontentsentry: +.. py:function:: mul(x: Tile, y: Scalar) -> Tile + :noindex: + :nocontentsentry: + + Multiply each element of a tile by a scalar + + +.. py:function:: mul(x: Scalar, y: Tile) -> Tile + :noindex: + :nocontentsentry: + + Multiply each element of a tile by a scalar + + .. py:function:: mod(a: Scalar, b: Scalar) -> Scalar Modulo operation using truncated division. @@ -2136,6 +2228,13 @@ Operators :nocontentsentry: +.. py:function:: neg(x: Tile) -> Tile + :noindex: + :nocontentsentry: + + Negate each element of a tile + + .. py:function:: unot(a: bool) -> bool diff --git a/examples/tile_fft.py b/examples/tile_fft.py index f6cf23f9..edc6c101 100644 --- a/examples/tile_fft.py +++ b/examples/tile_fft.py @@ -1,6 +1,6 @@ import numpy as np + import warp as wp -import numpy as np wp.init() wp.set_module_options({"enable_backward": False}) @@ -11,10 +11,9 @@ TILE_M = 1 TILE_N = 32 + @wp.kernel -def fft_tiled(x: wp.array2d(dtype=wp.vec2d), - y: wp.array2d(dtype=wp.vec2d)): - +def fft_tiled(x: wp.array2d(dtype=wp.vec2d), y: wp.array2d(dtype=wp.vec2d)): i, j, _ = wp.tid() a = wp.tile_load(x, i, j, m=TILE_M, n=TILE_N) wp.tile_fft_dx(a) @@ -23,12 +22,12 @@ def fft_tiled(x: wp.array2d(dtype=wp.vec2d), x_h = np.ones((TILE_M, TILE_N, 2), dtype=np.float64) -x_h[:,:,1] = 0 +x_h[:, :, 1] = 0 y_h = 3 * np.ones((TILE_M, TILE_N, 2), dtype=np.float64) x_wp = wp.array2d(x_h, dtype=wp.vec2d) y_wp = wp.array2d(y_h, dtype=wp.vec2d) wp.launch(fft_tiled, dim=[1, 1, BLOCK_DIM], inputs=[x_wp, y_wp], block_dim=BLOCK_DIM) -print("inputs:\n", x_wp) # [1+0i, 1+0i, 1+0i, ...] -print("output:\n", y_wp) # [32+0i, 0, 0, ...] +print("inputs:\n", x_wp) # [1+0i, 1+0i, 1+0i, ...] +print("output:\n", y_wp) # [32+0i, 0, 0, ...] diff --git a/examples/tile_matmul.py b/examples/tile_matmul.py index 3d980592..faedbee6 100644 --- a/examples/tile_matmul.py +++ b/examples/tile_matmul.py @@ -1,4 +1,5 @@ import numpy as np + import warp as wp wp.init() @@ -7,11 +8,9 @@ BLOCK_DIM = 32 M, N, K = 4, 8, 16 + @wp.kernel -def matmul_tiled(ga: wp.array2d(dtype=wp.float64), - gb: wp.array2d(dtype=wp.float64), - gc: wp.array2d(dtype=wp.float64)): - +def matmul_tiled(ga: wp.array2d(dtype=wp.float64), gb: wp.array2d(dtype=wp.float64), gc: wp.array2d(dtype=wp.float64)): i, j, _ = wp.tid() a = wp.tile_load(ga, i, j, m=M, n=K) b = wp.tile_load(gb, i, j, m=K, n=N) @@ -31,5 +30,5 @@ def matmul_tiled(ga: wp.array2d(dtype=wp.float64), wp.launch(matmul_tiled, dim=[1, 1, BLOCK_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=BLOCK_DIM) wp.synchronize() -print("inputs:\n", A, '\n', B) +print("inputs:\n", A, "\n", B) print("output (should be = 48 * np.ones(4, 8)):\n", C_wp) diff --git a/tools/ci/building/build-linux-x86_64/build.sh b/tools/ci/building/build-linux-x86_64/build.sh index 51940183..e9af605d 100755 --- a/tools/ci/building/build-linux-x86_64/build.sh +++ b/tools/ci/building/build-linux-x86_64/build.sh @@ -74,7 +74,7 @@ CUDA="$SCRIPT_DIR/../../../../_build/target-deps/cuda" # pip deps $PYTHON -m pip install --upgrade pip -$PYTHON -m pip install --upgrade numpy gitpython cmake ninja +$PYTHON -m pip install --upgrade numpy gitpython cmake ninja nvidia-mathdx==24.4.0 if [ "$GITLAB_CI" = "true" ]; then echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" diff --git a/warp/build.py b/warp/build.py index 024e5ebc..8655201c 100644 --- a/warp/build.py +++ b/warp/build.py @@ -5,40 +5,48 @@ # distribution of this software and related documentation without an express # license agreement from NVIDIA CORPORATION is strictly prohibited. +import ctypes import os import warp.config +from warp.mathdx import get_cuda_include_dirs from warp.thirdparty import appdirs -import ctypes - -def get_mathdx_include_dirs(): - return (os.environ['MATHDX_HOME'] + '/include').encode("utf-8") -def get_cuda_include_dirs(): - cuda_inc_path = (os.environ['CUDA_HOME'] + '/include').encode("utf-8") - include_dirs = [cuda_inc_path] - arr_include_dirs = (ctypes.c_char_p * len(include_dirs))() - arr_include_dirs[:] = include_dirs - return arr_include_dirs # builds cuda source to PTX or CUBIN using NVRTC (output type determined by output_path extension) -def build_cuda(cu_path, arch, output_path, config="release", verify_fp=False, fast_math=False, ltoirs=[]): +def build_cuda(cu_path, arch, output_path, config="release", verify_fp=False, fast_math=False, ltoirs=None): with open(cu_path, "rb") as src_file: src = src_file.read() cu_path = cu_path.encode("utf-8") inc_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "native").encode("utf-8") output_path = output_path.encode("utf-8") - cuda_include_dirs = get_cuda_include_dirs() if warp.config.llvm_cuda: warp.context.runtime.llvm.compile_cuda(src, cu_path, inc_path, output_path, False) else: + cuda_include_dirs = get_cuda_include_dirs() + + if ltoirs is None: + ltoirs = [] + num_ltoirs = len(ltoirs) arr_lroirs = (ctypes.c_char_p * num_ltoirs)(*ltoirs) arr_lroir_sizes = (ctypes.c_size_t * num_ltoirs)(*[len(l) for l in ltoirs]) err = warp.context.runtime.core.cuda_compile_program( - src, arch, inc_path, len(cuda_include_dirs), cuda_include_dirs, config == "debug", warp.config.verbose, verify_fp, fast_math, output_path, num_ltoirs, arr_lroirs, arr_lroir_sizes + src, + arch, + inc_path, + len(cuda_include_dirs), + cuda_include_dirs, + config == "debug", + warp.config.verbose, + verify_fp, + fast_math, + output_path, + num_ltoirs, + arr_lroirs, + arr_lroir_sizes, ) if err != 0: raise Exception(f"CUDA kernel build failed with error code {err}") diff --git a/warp/build_dll.py b/warp/build_dll.py index b6dfc81d..b860c146 100644 --- a/warp/build_dll.py +++ b/warp/build_dll.py @@ -244,7 +244,7 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None iter_dbg = "_ITERATOR_DEBUG_LEVEL=2" debug = "_DEBUG" - cpp_flags = f'/nologo /std:c++17 /GR- {runtime} /D "{debug}" /D "{cuda_enabled}" /D "{cutlass_enabled}" /D "{cuda_compat_enabled}" /D "{iter_dbg}" /I"{native_dir}" {includes} ' + cpp_flags = f'/nologo /std:c++17 /GR- {runtime} /D "{debug}" /D "{cuda_enabled}" /D "{cutlass_enabled}" /D "WP_ENABLE_MATHDX=0" /D "{cuda_compat_enabled}" /D "{iter_dbg}" /I"{native_dir}" {includes} ' if args.mode == "debug": cpp_flags += "/Zi /Od /D WP_ENABLE_DEBUG=1" @@ -282,7 +282,7 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None run_cmd(cuda_cmd) linkopts.append(quote(cu_out)) linkopts.append( - f'cudart_static.lib nvrtc_static.lib nvrtc-builtins_static.lib nvptxcompiler_static.lib ws2_32.lib user32.lib /LIBPATH:"{cuda_home}/lib/x64"' + f'cudart_static.lib nvrtc_static.lib nvrtc-builtins_static.lib nvptxcompiler_static.lib ws2_32.lib user32.lib nvJitLink_static.lib /LIBPATH:"{cuda_home}/lib/x64"' ) with ScopedTimer("link", active=args.verbose): @@ -290,19 +290,24 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None run_cmd(link_cmd) else: - libmathdx_home = os.environ['LIBMATHDX_HOME'] - libmathdx_includes = f'-I{libmathdx_home}/include' cpp_includes = f' -I"{warp_home_path.parent}/external/llvm-project/out/install/{mode}-{arch}/include"' cpp_includes += f' -I"{warp_home_path.parent}/_build/host-deps/llvm-project/release-{arch}/include"' cuda_includes = f' -I"{cuda_home}/include"' if cu_path else "" includes = cpp_includes + cuda_includes + if args.libmathdx_path: + libmathdx_includes = f' -I"{args.libmathdx_path}/include"' + mathdx_enabled = "WP_ENABLE_MATHDX=1" + else: + libmathdx_includes = "" + mathdx_enabled = "WP_ENABLE_MATHDX=0" + if sys.platform == "darwin": version = f"--target={arch}-apple-macos11" else: version = "-fabi-version=13" # GCC 8.2+ - cpp_flags = f'{version} --std=c++17 -fno-rtti -D{cuda_enabled} -D{cutlass_enabled} -D{cuda_compat_enabled} -fPIC -fvisibility=hidden -D_GLIBCXX_USE_CXX11_ABI=0 -I"{native_dir}" {includes} ' + cpp_flags = f'{version} --std=c++17 -fno-rtti -D{cuda_enabled} -D{cutlass_enabled} -D{mathdx_enabled} -D{cuda_compat_enabled} -fPIC -fvisibility=hidden -D_GLIBCXX_USE_CXX11_ABI=0 -I"{native_dir}" {includes} ' if mode == "debug": cpp_flags += "-O0 -g -D_DEBUG -DWP_ENABLE_DEBUG=1 -fkeep-inline-functions" @@ -330,19 +335,22 @@ def build_dll_for_arch(args, dll_path, cpp_paths, cu_path, libs, arch, mode=None cu_out = cu_path + ".o" if mode == "debug": - cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -g -G -O0 --compiler-options -fPIC,-fvisibility=hidden -D_DEBUG -D_ITERATOR_DEBUG_LEVEL=0 -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} {libmathdx_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -g -G -O0 --compiler-options -fPIC,-fvisibility=hidden -D_DEBUG -D_ITERATOR_DEBUG_LEVEL=0 -line-info {" ".join(nvcc_opts)} -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -D{mathdx_enabled} {libmathdx_includes} -o "{cu_out}" -c "{cu_path}"' elif mode == "release": - cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -O3 --compiler-options -fPIC,-fvisibility=hidden {" ".join(nvcc_opts)} -DNDEBUG -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} {libmathdx_includes} -o "{cu_out}" -c "{cu_path}"' + cuda_cmd = f'"{cuda_home}/bin/nvcc" --std=c++17 -O3 --compiler-options -fPIC,-fvisibility=hidden {" ".join(nvcc_opts)} -DNDEBUG -DWP_ENABLE_CUDA=1 -I"{native_dir}" -D{cutlass_enabled} {cutlass_includes} -D{mathdx_enabled} {libmathdx_includes} -o "{cu_out}" -c "{cu_path}"' with ScopedTimer("build_cuda", active=args.verbose): run_cmd(cuda_cmd) ld_inputs.append(quote(cu_out)) ld_inputs.append( - f'-L"{cuda_home}/lib64" -L{libmathdx_home}/lib -lcudart_static -lnvrtc_static -lnvrtc-builtins_static -lnvptxcompiler_static -lnvJitLink_static -lpthread -ldl -lrt -lmathdx_static' + f'-L"{cuda_home}/lib64" -lcudart_static -lnvrtc_static -lnvrtc-builtins_static -lnvptxcompiler_static -lnvJitLink_static -lpthread -ldl -lrt' ) + if args.libmathdx_path: + ld_inputs.append(f"-L{args.libmathdx_path}/lib -lmathdx_static") + if sys.platform == "darwin": opt_no_undefined = "-Wl,-undefined,error" opt_exclude_libs = "" diff --git a/warp/builtins.py b/warp/builtins.py index 3b74d18d..2286b7fc 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5,16 +5,15 @@ # distribution of this software and related documentation without an express # license agreement from NVIDIA CORPORATION is strictly prohibited. import builtins -import tempfile import functools -import os +import tempfile from typing import Any, Callable, Mapping, Sequence from warp.codegen import Reference, Var, strip_reference +from warp.mathdx import get_cuda_include_dirs, get_mathdx_include_dirs from warp.types import * from .context import add_builtin -from .build import get_cuda_include_dirs, get_mathdx_include_dirs def seq_check_equal(seq_1, seq_2): @@ -1708,8 +1707,8 @@ def spatial_vector_dispatch_func(input_types: Mapping[str, type], return_type: A # Tile-based primitives shared_memory_id = 0 + def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any]): - # return generic type (for doc builds) if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) @@ -1731,8 +1730,8 @@ def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str return TileZeros(dtype=dtype, M=m, N=n) -def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): +def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): m, n, dtype = arg_values["m"], arg_values["n"], arg_values["dtype"] template_args = [] @@ -1743,7 +1742,6 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar return ([], template_args) - add_builtin( "tile_zeros", input_types={"m": int, "n": int, "dtype": Scalar}, @@ -1755,13 +1753,13 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar export=False, ) + def tile_load_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) - # if len(arg_types) != 3: + # if len(arg_types) != 3: # raise RuntimeError("tile_load() requires 3 positional args") if not is_array(arg_types["a"]): @@ -1786,7 +1784,6 @@ def tile_load_value_func(arg_types, arg_values): def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): - array = arg_values["a"] x, y = arg_values["x"], arg_values["y"] m, n = arg_values["m"].constant, arg_values["n"].constant @@ -1811,13 +1808,13 @@ def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg export=False, ) + def tile_store_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: return None - if len(arg_types) != 4: + if len(arg_types) != 4: raise RuntimeError("tile_store() requires 4 positional args") if not is_array(arg_types["a"]): @@ -1835,7 +1832,6 @@ def tile_store_value_func(arg_types, arg_values): return None - add_builtin( "tile_store", input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, @@ -1846,13 +1842,13 @@ def tile_store_value_func(arg_types, arg_values): export=False, ) + def tile_atomic_add_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) - if len(arg_types) != 4: + if len(arg_types) != 4: raise RuntimeError("tile_atomic_add() requires 4 positional args") if not is_array(arg_types["a"]): @@ -1870,10 +1866,7 @@ def tile_atomic_add_value_func(arg_types, arg_values): if arg_types["a"].dtype != arg_types["t"].dtype: raise RuntimeError("tile_atomic_add() tile dtype and array dtype must match") - return Tile(dtype=arg_types["t"].dtype, - M=arg_types["t"].M, - N=arg_types["t"].N) - + return Tile(dtype=arg_types["t"].dtype, M=arg_types["t"].M, N=arg_types["t"].N) add_builtin( @@ -1888,12 +1881,11 @@ def tile_atomic_add_value_func(arg_types, arg_values): def tile_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: return Tile - if len(arg_types) != 1: + if len(arg_types) != 1: raise RuntimeError("tile() requires 1 positional arg") # todo: we need a way to pass things like current compiler options @@ -1903,7 +1895,6 @@ def tile_value_func(arg_types, arg_values): return Tile(dtype=arg_types["x"], M=1, N=warp.codegen.options["block_dim"], op="Tile") - add_builtin( "tile", input_types={"x": Any}, @@ -1916,17 +1907,16 @@ def tile_value_func(arg_types, arg_values): def tile_extract_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: - return None - - if len(arg_types) != 3: + return None + + if len(arg_types) != 3: raise RuntimeError("tile_extract() requires 3 positional args") if not is_tile(arg_types["a"]): raise RuntimeError("tile_extract() argument 0 must be a tile") - + return arg_types["a"].dtype @@ -1942,12 +1932,11 @@ def tile_extract_value_func(arg_types, arg_values): def tile_matmul_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: return None - if len(arg_types) != 3: + if len(arg_types) != 3: raise RuntimeError("tile_matmul() requires 4 positional args") if not is_tile(arg_types["a"]): @@ -1962,11 +1951,10 @@ def tile_matmul_value_func(arg_types, arg_values): if arg_types["out"].storage != "shared": raise RuntimeError("tile_matmul() output argument must have shared memory storage") - return None + def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): - a = arg_values["a"] b = arg_values["b"] out = arg_values["out"] @@ -1986,13 +1974,13 @@ def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a value_func=tile_matmul_value_func, dispatch_func=tile_matmul_dispatch_func, variadic=True, - doc="Compute matrix product and accumulate out += a*b.", + doc="Compute matrix product and accumulate out += a*b.", group="Tile Primitives", export=False, ) + def tile_sum_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: return None @@ -2013,16 +2001,14 @@ def tile_sum_value_func(arg_types, arg_values): input_types={"a": Tile}, value_func=tile_sum_value_func, variadic=True, - doc="Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored", + doc="Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored", group="Tile Primitives", export=False, ) - # does type propagation for load() def tile_unary_map_value_func(arg_types, arg_values): - if arg_types is None: return None @@ -2045,16 +2031,16 @@ def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, ar "tile_map", input_types={"op": Callable, "a": Any}, value_func=tile_unary_map_value_func, - #dispatch_func=tile_map_dispatch_func, - #variadic=True, + # dispatch_func=tile_map_dispatch_func, + # variadic=True, native_func="tile_unary_map", - doc="Map the operation onto each element of the tile", + doc="Map the operation onto each element of the tile", group="Tile Primitives", export=False, ) -def tile_binary_map_value_func(arg_types, arg_values): +def tile_binary_map_value_func(arg_types, arg_values): if arg_types is None: return None @@ -2085,10 +2071,10 @@ def tile_binary_map_value_func(arg_types, arg_values): "tile_map", input_types={"op": Callable, "a": Any, "b": Any}, value_func=tile_binary_map_value_func, - #dispatch_func=tile_map_dispatch_func, - #variadic=True, + # dispatch_func=tile_map_dispatch_func, + # variadic=True, native_func="tile_binary_map", - doc="Map the operation onto each element of the tile", + doc="Map the operation onto each element of the tile", group="Tile Primitives", export=False, ) @@ -4705,7 +4691,6 @@ def matmat_mul_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str # Tile operators def tile_unary_value_func(arg_types, arg_values): - if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) @@ -4713,29 +4698,33 @@ def tile_unary_value_func(arg_types, arg_values): if not is_tile(t): raise RuntimeError("Expected tile for unary expression") - + return TileUnaryMap(t) -def tile_scalar_mul_value_func(arg_types, arg_values): +def tile_scalar_mul_value_func(arg_types, arg_values): if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) x = arg_types["x"] y = arg_types["y"] - + # tile*scalar if is_tile(x): if x.dtype != y: - raise RuntimeError("Scalar factor should have the same type as tile for tile*scalar, tile type: {x} scalar type: {y}") - + raise RuntimeError( + "Scalar factor should have the same type as tile for tile*scalar, tile type: {x} scalar type: {y}" + ) + return TileBinaryMap(x, TileConstant(y, x.M, x.N)) - + # scalar*tile if is_tile(y): if y.dtype != x: - raise RuntimeError("Scalar factor should have the same type as tile for scalar*tile, tile type: {x} scalar type: {y}") - + raise RuntimeError( + "Scalar factor should have the same type as tile for scalar*tile, tile type: {x} scalar type: {y}" + ) + return TileBinaryMap(TileConstant(x, y.M, y.N), y) @@ -4753,10 +4742,10 @@ def tile_scalar_mul_value_func(arg_types, arg_values): "add", input_types={"a": Tile(dtype=Any, M=Any, N=Any), "b": Tile(dtype=Any, M=Any, N=Any)}, value_func=tile_binary_map_value_func, - #dispatch_func=tile_map_dispatch_func, - #variadic=True, + # dispatch_func=tile_map_dispatch_func, + # variadic=True, native_func="tile_add", - doc="Add each element of two tiles together", + doc="Add each element of two tiles together", group="Tile Primitives", export=False, ) @@ -4785,17 +4774,17 @@ def tile_scalar_mul_value_func(arg_types, arg_values): ## ## MathDx, LTOIR-based, Tile functions ## - + + ## ## Matmul ## def tile_matmul_generic_value_func(arg_types, arg_values): - # return generic type (for doc builds) if arg_types is None: return None - if len(arg_types) != 3: + if len(arg_types) != 3: raise RuntimeError("tile_matmul() requires 4 positional args") if not is_tile(arg_types["a"]): @@ -4810,26 +4799,29 @@ def tile_matmul_generic_value_func(arg_types, arg_values): if arg_types["out"].storage != "shared": raise RuntimeError("tile_matmul() output argument must have shared memory storage") - return None -def tile_matmul_generic_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any]): - + +def tile_matmul_generic_dispatch_func( + arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any] +): a = arg_values["a"] b = arg_values["b"] out = arg_values["out"] if any(not is_tile(arg.type) for arg in [a, b, out]): - raise RuntimeError(f"tile_matmul() requires three Tile arguments") - + raise RuntimeError("tile_matmul() requires three Tile arguments") + if any(arg.type.dtype not in [float16, float32, float64, vec2h, vec2f, vec2d] for arg in [a, b, out]): - raise RuntimeError(f"tile_matmul() arguments must be tiles of float16, float32 or float64, vec2h, vec2f, vec2d entries") - + raise RuntimeError( + "tile_matmul() arguments must be tiles of float16, float32 or float64, vec2h, vec2f, vec2d entries" + ) + if any(arg.type.dtype != out.type.dtype for arg in [a, b]): - raise RuntimeError(f"tile_matmul() arguments must have the same type") + raise RuntimeError("tile_matmul() arguments must have the same type") if (a.type.N != b.type.M) or (a.type.M != out.type.M) or (b.type.N != out.type.N): - raise RuntimeError(f"tile_matmul(A, B, C) requires sizes of A, B and C to be consistent for a matmul") + raise RuntimeError("tile_matmul(A, B, C) requires sizes of A, B and C to be consistent for a matmul") # set the storage type to the inputs to shared a.type.storage = "shared" @@ -4840,75 +4832,93 @@ def tile_matmul_generic_dispatch_func(arg_types: Mapping[str, type], return_type # Real if out.type.dtype == float16: dtype = "wp::float16" - precision = 2 # COMMONDX_PRECISION_F16 - element_type = 0 # CUBLASDX_TYPE_REAL + precision = 2 # COMMONDX_PRECISION_F16 + element_type = 0 # CUBLASDX_TYPE_REAL elif out.type.dtype == float32: dtype = "wp::float32" - precision = 3 # COMMONDX_PRECISION_F32 - element_type = 0 # CUBLASDX_TYPE_REAL + precision = 3 # COMMONDX_PRECISION_F32 + element_type = 0 # CUBLASDX_TYPE_REAL elif out.type.dtype == float64: dtype = "wp::float64" - precision = 4 # COMMONDX_PRECISION_F64 - element_type = 0 # CUBLASDX_TYPE_REAL + precision = 4 # COMMONDX_PRECISION_F64 + element_type = 0 # CUBLASDX_TYPE_REAL # Complex elif out.type.dtype == vec2h: dtype = "wp::vec2h" - precision = 2 # COMMONDX_PRECISION_F16 - element_type = 1 # CUBLASDX_TYPE_COMPLEX + precision = 2 # COMMONDX_PRECISION_F16 + element_type = 1 # CUBLASDX_TYPE_COMPLEX elif out.type.dtype == vec2f: dtype = "wp::vec2f" - precision = 3 # COMMONDX_PRECISION_F32 - element_type = 1 # CUBLASDX_TYPE_COMPLEX + precision = 3 # COMMONDX_PRECISION_F32 + element_type = 1 # CUBLASDX_TYPE_COMPLEX elif out.type.dtype == vec2d: dtype = "wp::vec2d" - precision = 4 # COMMONDX_PRECISION_F64 - element_type = 1 # CUBLASDX_TYPE_COMPLEX + precision = 4 # COMMONDX_PRECISION_F64 + element_type = 1 # CUBLASDX_TYPE_COMPLEX else: raise RuntimeError("Unsupported datatype") # generate the LTO M, K = a.type.M, a.type.N _, N = b.type.M, b.type.N - num_threads = options['block_dim'] - arch = options['output_arch'] + num_threads = options["block_dim"] + arch = options["output_arch"] def make_function(M, N, K, tA, tB): # Warp follows Numpy: matrices are row-major # But cuBLASDx follows BLAS: matrices are col-major # So we have to flip M <-> N and A <-> B def make_transpose(t): - if t == 'N': - return 0 # CUBLASDX_TRANSPOSE_MODE_NON_TRANSPOSED - elif t == 'T': - return 1 # CUBLASDX_TRANSPOSE_MODE_TRANSPOSED + if t == "N": + return 0 # CUBLASDX_TRANSPOSE_MODE_NON_TRANSPOSED + elif t == "T": + return 1 # CUBLASDX_TRANSPOSE_MODE_TRANSPOSED raise RuntimeError("Invalid transpose mode") + lto_symbol = f"dot_{M}_{N}_{K}_{tA}_{tB}_{precision}_{element_type}" lto_code = tempfile.NamedTemporaryFile() include_dirs = get_cuda_include_dirs() result = warp.context.runtime.core.cuda_compile_dot( - lto_code.name.encode("utf-8"), lto_symbol.encode("utf-8"), - len(include_dirs), include_dirs, get_mathdx_include_dirs(), - arch, N, M, K, precision, element_type, make_transpose(tB), make_transpose(tA), num_threads) + lto_code.name.encode("utf-8"), + lto_symbol.encode("utf-8"), + len(include_dirs), + include_dirs, + get_mathdx_include_dirs(), + arch, + N, + M, + K, + precision, + element_type, + make_transpose(tB), + make_transpose(tA), + num_threads, + ) if not result: raise RuntimeError("Failed to compile tile_matmul") else: - with open(lto_code.name, 'rb') as f: + with open(lto_code.name, "rb") as f: lto_code = f.read() return lto_symbol, lto_code - (fun_forward, lto_forward) = make_function(M, N, K, 'N', 'N') # C += A * B - (fun_backward_A, lto_backward_A) = make_function(M, K, N, 'N', 'T') # adjA += adjC * B^T - (fun_backward_B, lto_backward_B) = make_function(K, N, M, 'T', 'N') # adjB += A^T * adjC + (fun_forward, lto_forward) = make_function(M, N, K, "N", "N") # C += A * B + (fun_backward_A, lto_backward_A) = make_function(M, K, N, "N", "T") # adjA += adjC * B^T + (fun_backward_B, lto_backward_B) = make_function(K, N, M, "T", "N") # adjB += A^T * adjC + + return ( + ( + Var(fun_forward, str, False, True, False), + Var(fun_backward_A, str, False, True, False), + Var(fun_backward_B, str, False, True, False), + Var(dtype, str, False, True, False), + a, + b, + out, + ), + template_args, + [lto_forward, lto_backward_A, lto_backward_B], + ) - return ((Var(fun_forward, str, False, True, False), - Var(fun_backward_A, str, False, True, False), - Var(fun_backward_B, str, False, True, False), - Var(dtype, str, False, True, False), - a, - b, - out), - template_args, - [lto_forward, lto_backward_A, lto_backward_B]) add_builtin( "tile_matmul_dx", @@ -4916,21 +4926,21 @@ def make_transpose(t): value_func=tile_matmul_generic_value_func, lto_dispatch_func=tile_matmul_generic_dispatch_func, variadic=True, - doc="Compute matrix product and accumulate out += a*b.", + doc="Compute matrix product and accumulate out += a*b.", group="Tile Primitives", export=False, namespace="", ) + ## ## FFT ## def tile_fft_generic_value_func(arg_types, arg_values): - if arg_types is None: return None - if len(arg_types) != 1: + if len(arg_types) != 1: raise RuntimeError("tile_fft() requires 1 positional args") if not is_tile(arg_types["inout"]): @@ -4941,38 +4951,44 @@ def tile_fft_generic_value_func(arg_types, arg_values): return None -def tile_fft_generic_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any], direction:str = None): - + +def tile_fft_generic_dispatch_func( + arg_types: Mapping[str, type], + return_type: Any, + arg_values: Mapping[str, Var], + options: Mapping[str, Any], + direction: str = None, +): inout = arg_values["inout"] inout.type.storage = "register" - if (not is_tile(inout.type)): - raise RuntimeError(f"tile_fft() arguments must be a single tile with register storage") + if not is_tile(inout.type): + raise RuntimeError("tile_fft() arguments must be a single tile with register storage") - if (inout.type.dtype not in [vec2f, vec2d]): - raise RuntimeError(f"tile_fft() argument must be a tile of vec2f or vec2d (interpreted as complex) entries") + if inout.type.dtype not in [vec2f, vec2d]: + raise RuntimeError("tile_fft() argument must be a tile of vec2f or vec2d (interpreted as complex) entries") # see libcufftdx.hpp - if direction == 'forward': - dir = 0 # CUFFTDX_DIRECTION_FORWARD - elif direction == 'inverse': - dir = 1 # CUFFTDX_DIRECTION_INVERSE + if direction == "forward": + dir = 0 # CUFFTDX_DIRECTION_FORWARD + elif direction == "inverse": + dir = 1 # CUFFTDX_DIRECTION_INVERSE else: raise RuntimeError("Invalid direction") - + if inout.type.dtype == vec2f: dtype = "wp::vec2f" - precision = 3 # COMMONDX_PRECISION_F32 + precision = 3 # COMMONDX_PRECISION_F32 elif inout.type.dtype == vec2d: dtype = "wp::vec2d" - precision = 4 # COMMONDX_PRECISION_F64 + precision = 4 # COMMONDX_PRECISION_F64 else: raise RuntimeError("Unsupported datatype") # M FFTs of size N each batch, size = inout.type.M, inout.type.N - num_threads = options['block_dim'] - arch = options['output_arch'] + num_threads = options["block_dim"] + arch = options["output_arch"] ept = size // num_threads lto_symbol = f"fft_{size}_{ept}_{arch}_{direction}_{precision}" @@ -4982,35 +4998,46 @@ def tile_fft_generic_dispatch_func(arg_types: Mapping[str, type], return_type: A include_dirs = get_cuda_include_dirs() result = warp.context.runtime.core.cuda_compile_fft( - lto_code.name.encode("utf-8"), + lto_code.name.encode("utf-8"), lto_symbol.encode("utf-8"), - len(include_dirs), include_dirs, + len(include_dirs), + include_dirs, get_mathdx_include_dirs(), - arch, size, ept, dir, precision, ctypes.byref(shared_memory_size) + arch, + size, + ept, + dir, + precision, + ctypes.byref(shared_memory_size), ) if not result: raise RuntimeError("Failed to compile tile_matmul") - with open(lto_code.name, 'rb') as f: + with open(lto_code.name, "rb") as f: lto_code = f.read() - return ((Var(lto_symbol, str, False, True, False), - Var(dtype, str, False, True, False), - Var(str(shared_memory_size.value), str, False, True, False), - Var(str(batch), str, False, True, False), - Var(str(ept), str, False, True, False), - inout), - [], - [lto_code]) + return ( + ( + Var(lto_symbol, str, False, True, False), + Var(dtype, str, False, True, False), + Var(str(shared_memory_size.value), str, False, True, False), + Var(str(batch), str, False, True, False), + Var(str(ept), str, False, True, False), + inout, + ), + [], + [lto_code], + ) + add_builtin( "tile_fft_dx", input_types={"inout": Tile}, value_func=tile_fft_generic_value_func, - lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction='forward'), + lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction="forward"), variadic=True, - doc="Compute the FFT along the second dimension of a 2D tile of data.", + doc="Compute the FFT along the second dimension of a 2D tile of data.", group="Tile Primitives", export=False, namespace="", @@ -5020,9 +5047,9 @@ def tile_fft_generic_dispatch_func(arg_types: Mapping[str, type], return_type: A "tile_ifft_dx", input_types={"inout": Tile}, value_func=tile_fft_generic_value_func, - lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction='inverse'), + lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction="inverse"), variadic=True, - doc="Compute the inverse FFT along the second dimension of a 2D tile of data.", + doc="Compute the inverse FFT along the second dimension of a 2D tile of data.", group="Tile Primitives", export=False, namespace="", diff --git a/warp/codegen.py b/warp/codegen.py index fc2da1f6..f9a47f25 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -27,6 +27,7 @@ # of current compile options (block_dim) etc options = {} + class WarpCodegenError(RuntimeError): def __init__(self, message): super().__init__(message) @@ -1278,7 +1279,9 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): # for example by checking whether an argument corresponds to # a literal value or references a variable. if func.lto_dispatch_func is not None: - func_args, template_args, ltoirs = func.lto_dispatch_func(func.input_types, return_type, bound_args, options=adj.builder_options) + func_args, template_args, ltoirs = func.lto_dispatch_func( + func.input_types, return_type, bound_args, options=adj.builder_options + ) adj.ltoirs.extend(ltoirs) elif func.dispatch_func is not None: func_args, template_args = func.dispatch_func(func.input_types, return_type, bound_args) @@ -1335,7 +1338,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): replay_call = forward_call if func.custom_replay_func is not None: replay_call = f"var_{output} = {func.namespace}replay_{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" - + else: # handle multiple value functions @@ -1347,7 +1350,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): ) replay_call = forward_call - if func.skip_replay: adj.add_forward(forward_call, replay="// " + replay_call) else: @@ -1360,7 +1362,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): adj_args = tuple(strip_reference(x) for x in func_args) reverse_has_output_args = ( func.require_original_output_arg or len(output_list) > 1 - ) and func.custom_grad_func is None + ) and func.custom_grad_func is None arg_str = adj.format_reverse_call_args( fwd_args, adj_args, @@ -3094,7 +3096,6 @@ def codegen_func_forward(adj, func_type="kernel", device="cpu"): lines += ["// primal vars\n"] for var in adj.variables: - # do not predeclare vars with auto type if var.ctype() == "auto": continue @@ -3136,11 +3137,10 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): lines += ["// primal vars\n"] for var in adj.variables: - if is_tile(var.type): lines += [f"{var.ctype()} {var.emit()} = {var.type.cinit()};\n"] elif var.constant is None: - lines += [f"{var.ctype()} {var.emit()};\n"] + lines += [f"{var.ctype()} {var.emit()};\n"] else: lines += [f"const {var.ctype()} {var.emit()} = {constant_str(var.constant)};\n"] @@ -3151,7 +3151,7 @@ def codegen_func_reverse(adj, func_type="kernel", device="cpu"): for var in adj.variables: name = var.emit_adj() ctype = var.ctype(value_type=True) - + if is_tile(var.type): lines += [f"{ctype} {name} = {var.type.cinit(adjoint=True)};\n"] else: diff --git a/warp/context.py b/warp/context.py index a28d3f5a..9f0617b1 100644 --- a/warp/context.py +++ b/warp/context.py @@ -1751,7 +1751,7 @@ def __init__(self, name, loader): "fast_math": False, "cuda_output": None, # supported values: "ptx", "cubin", or None (automatic) "mode": warp.config.mode, - "block_dim": 0 + "block_dim": 0, } # Module dependencies are determined by scanning each function @@ -1888,7 +1888,7 @@ def load(self, device, block_dim=None) -> ModuleExec: # re-compile module if tile size (blockdim) changes # todo: it would be better to have a method such as `module.get_kernel(block_dim=N)` # that can return a single kernel instance with a given block size - if block_dim != None: + if block_dim is not None: if self.options["block_dim"] != block_dim: self.unload() self.options["block_dim"] = block_dim @@ -3220,6 +3220,8 @@ def __init__(self): self.core.is_cuda_compatibility_enabled.restype = ctypes.c_int self.core.is_cutlass_enabled.argtypes = None self.core.is_cutlass_enabled.restype = ctypes.c_int + self.core.is_mathdx_enabled.argtypes = None + self.core.is_mathdx_enabled.restype = ctypes.c_int self.core.cuda_driver_version.argtypes = None self.core.cuda_driver_version.restype = ctypes.c_int @@ -3344,52 +3346,52 @@ def __init__(self): self.core.cuda_graph_destroy.restype = ctypes.c_bool self.core.cuda_compile_program.argtypes = [ - ctypes.c_char_p, # cuda_src - ctypes.c_int, # arch - ctypes.c_char_p, # include_dir - ctypes.c_int, # num_cuda_include_dirs - ctypes.POINTER(ctypes.c_char_p), # cuda include dirs - ctypes.c_bool, # debug - ctypes.c_bool, # verbose - ctypes.c_bool, # verify_fp - ctypes.c_bool, # fast_math - ctypes.c_char_p, # output_path - ctypes.c_size_t, # num_ltoirs - ctypes.POINTER(ctypes.c_char_p), # ltoirs - ctypes.POINTER(ctypes.c_size_t), # ltoir_sizes + ctypes.c_char_p, # cuda_src + ctypes.c_int, # arch + ctypes.c_char_p, # include_dir + ctypes.c_int, # num_cuda_include_dirs + ctypes.POINTER(ctypes.c_char_p), # cuda include dirs + ctypes.c_bool, # debug + ctypes.c_bool, # verbose + ctypes.c_bool, # verify_fp + ctypes.c_bool, # fast_math + ctypes.c_char_p, # output_path + ctypes.c_size_t, # num_ltoirs + ctypes.POINTER(ctypes.c_char_p), # ltoirs + ctypes.POINTER(ctypes.c_size_t), # ltoir_sizes ] self.core.cuda_compile_program.restype = ctypes.c_size_t self.core.cuda_compile_fft.argtypes = [ - ctypes.c_char_p, # lto - ctypes.c_char_p, # function name - ctypes.c_int, # num include dirs - ctypes.POINTER(ctypes.c_char_p), # include dirs - ctypes.c_char_p, # mathdx include dir - ctypes.c_int, # arch - ctypes.c_int, # size - ctypes.c_int, # ept - ctypes.c_int, # direction - ctypes.c_int, # precision - ctypes.POINTER(ctypes.c_int) # smem (out) + ctypes.c_char_p, # lto + ctypes.c_char_p, # function name + ctypes.c_int, # num include dirs + ctypes.POINTER(ctypes.c_char_p), # include dirs + ctypes.c_char_p, # mathdx include dir + ctypes.c_int, # arch + ctypes.c_int, # size + ctypes.c_int, # ept + ctypes.c_int, # direction + ctypes.c_int, # precision + ctypes.POINTER(ctypes.c_int), # smem (out) ] self.core.cuda_compile_fft.restype = ctypes.c_bool self.core.cuda_compile_dot.argtypes = [ - ctypes.c_char_p, # lto - ctypes.c_char_p, # function name - ctypes.c_int, # num include dirs - ctypes.POINTER(ctypes.c_char_p), # include dirs - ctypes.c_char_p, # mathdx include dir - ctypes.c_int, # arch - ctypes.c_int, # M - ctypes.c_int, # N - ctypes.c_int, # K - ctypes.c_int, # precision - ctypes.c_int, # type - ctypes.c_int, # tA - ctypes.c_int, # tB - ctypes.c_int # num threads + ctypes.c_char_p, # lto + ctypes.c_char_p, # function name + ctypes.c_int, # num include dirs + ctypes.POINTER(ctypes.c_char_p), # include dirs + ctypes.c_char_p, # mathdx include dir + ctypes.c_int, # arch + ctypes.c_int, # M + ctypes.c_int, # N + ctypes.c_int, # K + ctypes.c_int, # precision + ctypes.c_int, # type + ctypes.c_int, # tA + ctypes.c_int, # tB + ctypes.c_int, # num threads ] self.core.cuda_compile_dot.restype = ctypes.c_bool @@ -4845,7 +4847,9 @@ def pack_arg(kernel, arg_type, arg_name, value, device, adjoint=False): # represents all data required for a kernel launch # so that launches can be replayed quickly, use `wp.launch(..., record_cmd=True)` class Launch: - def __init__(self, kernel, device, hooks=None, params=None, params_addr=None, bounds=None, max_blocks=0): + def __init__( + self, kernel, device, hooks=None, params=None, params_addr=None, bounds=None, max_blocks=0, block_dim=256 + ): # retain the module executable so it doesn't get unloaded self.module_exec = kernel.module.load(device) if not self.module_exec: @@ -4884,6 +4888,7 @@ def __init__(self, kernel, device, hooks=None, params=None, params_addr=None, bo self.device = device self.bounds = bounds self.max_blocks = max_blocks + self.block_dim = block_dim def set_dim(self, dim): self.bounds = warp.types.launch_bounds_t(dim) @@ -4965,6 +4970,7 @@ def launch(self, stream=None) -> Any: self.hooks.forward, self.bounds.size, self.max_blocks, + self.block_dim, self.params_addr, stream.cuda_stream, ) @@ -5113,7 +5119,13 @@ def pack_args(args, params, adjoint=False): ) runtime.core.cuda_launch_kernel( - device.context, hooks.backward, bounds.size, max_blocks, block_dim, kernel_params, stream.cuda_stream + device.context, + hooks.backward, + bounds.size, + max_blocks, + block_dim, + kernel_params, + stream.cuda_stream, ) else: @@ -5136,7 +5148,13 @@ def pack_args(args, params, adjoint=False): else: # launch runtime.core.cuda_launch_kernel( - device.context, hooks.forward, bounds.size, max_blocks, block_dim, kernel_params, stream.cuda_stream + device.context, + hooks.forward, + bounds.size, + max_blocks, + block_dim, + kernel_params, + stream.cuda_stream, ) try: @@ -5150,7 +5168,9 @@ def pack_args(args, params, adjoint=False): # record file, lineno, func as metadata frame = inspect.currentframe().f_back caller = {"file": frame.f_code.co_filename, "lineno": frame.f_lineno, "func": frame.f_code.co_name} - runtime.tape.record_launch(kernel, dim, max_blocks, inputs, outputs, device, block_dim, metadata={"caller": caller}) + runtime.tape.record_launch( + kernel, dim, max_blocks, inputs, outputs, device, block_dim, metadata={"caller": caller} + ) # detect illegal inter-kernel read/write access patterns if verification flag is set if warp.config.verify_autograd_array_access: @@ -5724,7 +5744,7 @@ def type_str(t): return f"{t.__name__}[{args_repr}]" elif warp.types.is_tile(t): return "Tile" - + return t.__name__ diff --git a/warp/examples/benchmarks/benchmark_tile.py b/warp/examples/benchmarks/benchmark_tile.py index fc5900fe..54fec3f9 100644 --- a/warp/examples/benchmarks/benchmark_tile.py +++ b/warp/examples/benchmarks/benchmark_tile.py @@ -1,53 +1,47 @@ import numpy as np -import warp as wp - import torch +import warp as wp + wp.init() wp.set_module_options({"enable_backward": False, "fast_math": True}) wp.set_device("cuda:0") wp.build.clear_kernel_cache() -@wp.kernel -def gemm(A: wp.array2d(dtype=float), - B: wp.array2d(dtype=float), - C: wp.array2d(dtype=float)): +@wp.kernel +def gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): # output index i, j = wp.tid() sum = float(0.0) for k in range(0, A.shape[1]): - sum += A[i, k]*B[k, j] + sum += A[i, k] * B[k, j] C[i, j] = sum - TILE_M = wp.constant(64) TILE_N = wp.constant(64) TILE_K = wp.constant(8) -@wp.kernel -def gemm_tiled(A: wp.array2d(dtype=float), - B: wp.array2d(dtype=float), - C: wp.array2d(dtype=float)): +@wp.kernel +def gemm_tiled(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): # output tile index i, j = wp.tid() sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) - M = A.shape[0] - N = B.shape[1] + _M = A.shape[0] + _N = B.shape[1] K = A.shape[1] - count = int(K / 8) # TODO: code-gen bug if you use a constant before passing it to a kwd arg (in this case TILE_K) + count = int(K / 8) # TODO: code-gen bug if you use a constant before passing it to a kwd arg (in this case TILE_K) for k in range(count): - a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) @@ -58,24 +52,21 @@ def gemm_tiled(A: wp.array2d(dtype=float), def benchmark_numpy(A, B, C): - timers = {} iters = 10 # warm up - for i in range(10): - C = A@B + for _i in range(10): + _C = A @ B with wp.ScopedTimer("NumPy", dict=timers): - - for i in range(iters): - C = A@B + for _i in range(iters): + _C = A @ B return min(timers["NumPy"]) def benchmark_warp_simt(A, B, C): - timers = {} iters = 10 @@ -84,19 +75,17 @@ def benchmark_warp_simt(A, B, C): C_wp = wp.array(C) # warm up - for i in range(10): + for _i in range(10): wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) with wp.ScopedTimer("Warp (SIMT)", dict=timers, print=False, synchronize=True): - - for i in range(iters): + for _i in range(iters): wp.launch(gemm, dim=(M, N), inputs=[A_wp, B_wp, C_wp]) return min(timers["Warp (SIMT)"]) def benchmark_warp_tiled(A, B, C): - timers = {} iters = 10 @@ -104,8 +93,7 @@ def benchmark_warp_tiled(A, B, C): SUB_TILE_M = 4 SUB_TILE_N = 4 - num_threads = int(TILE_M/SUB_TILE_M)*int(TILE_N/SUB_TILE_N); - + num_threads = int(TILE_M / SUB_TILE_M) * int(TILE_N / SUB_TILE_N) A_wp = wp.array(A) B_wp = wp.array(B) C_wp = wp.array(C) @@ -113,48 +101,42 @@ def benchmark_warp_tiled(A, B, C): # warm up wp.capture_begin() - for i in range(10): - wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) + for _i in range(iters): + wp.launch(gemm_tiled, dim=(int(M / TILE_M), int(N / TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) graph = wp.capture_end() - with wp.ScopedTimer("Warp (Tiled)", dict=timers, print=False, synchronize=True): - - #for i in range(iters): + # for i in range(iters): # wp.launch(gemm_tiled, dim=(int(M/TILE_M), int(N/TILE_N)), inputs=[A_wp, B_wp, C_wp], tile_size=num_threads) wp.capture_launch(graph) - return min(timers["Warp (Tiled)"]) def benchmark_torch(A, B, C): - A_tc = torch.from_numpy(A).to("cuda:0") B_tc = torch.from_numpy(B).to("cuda:0") C_tc = torch.from_numpy(C).to("cuda:0") # warm-up - for i in range(10): + for _i in range(10): torch.matmul(A_tc, B_tc, out=C_tc) timers = {} iters = 10 - + torch.cuda.synchronize() with wp.ScopedTimer("Torch", dict=timers, print=False): - - for i in range(iters): - torch.matmul(A_tc, B_tc)#, out=C_tc) + for _i in range(iters): + torch.matmul(A_tc, B_tc) # , out=C_tc) torch.cuda.synchronize() return min(timers["Torch"]) - results_torch = [] results_warp_simt = [] results_warp_tiled = [] @@ -163,10 +145,9 @@ def benchmark_torch(A, B, C): print("--------------------------------------------------------") for i in range(2, 33): + # for i in range(8,9): -#for i in range(8,9): - - M = i*128 + M = i * 128 N = M K = N @@ -181,13 +162,11 @@ def benchmark_torch(A, B, C): C = np.zeros((M, N), dtype=np.float32) results_torch.append(benchmark_torch(A, B, C)) - results_warp_simt.append(0.0)#benchmark_warp_simt(A, B, C)) + results_warp_simt.append(0.0) # benchmark_warp_simt(A, B, C)) results_warp_tiled.append(benchmark_warp_tiled(A, B, C)) - print("{:>8d} {:>8d} {:>8d} {:>8f} {:>8f} {:>8f}".format(M, N, K, results_torch[-1], results_warp_simt[-1], results_warp_tiled[-1])) - - - - - - + print( + "{:>8d} {:>8d} {:>8d} {:>8f} {:>8f} {:>8f}".format( + M, N, K, results_torch[-1], results_warp_simt[-1], results_warp_tiled[-1] + ) + ) diff --git a/warp/mathdx.py b/warp/mathdx.py new file mode 100644 index 00000000..dab9fbc8 --- /dev/null +++ b/warp/mathdx.py @@ -0,0 +1,151 @@ +# Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +import ctypes +import os +import platform +import re +import sys +import warnings +from importlib.metadata import PackageNotFoundError, files + +CUDA_HOME = None +MATHDX_HOME = None +CUTLASS_HOME = None + + +PLATFORM_LINUX = sys.platform.startswith("linux") +PLATFORM_WIN = sys.platform.startswith("win32") + + +def _conda_get_target_name(): + if PLATFORM_LINUX: + plat = platform.processor() + if plat == "aarch64": + return "sbsa-linux" + else: + return f"{plat}-linux" + elif PLATFORM_WIN: + return "x64" + else: + raise AssertionError + + +def _check_cuda_home(): + # We need some CUDA headers for compiling mathDx headers. + # We assume users properly managing their local envs (ex: no mix-n-match). + global CUDA_HOME + + # Try wheel + try: + # We need CUDA 12+ for device API support + cudart = files("nvidia-cuda-runtime-cu12") + cccl = files("nvidia-cuda-cccl-cu12") + # use cuda_fp16.h (which we need) as a proxy + cudart = [f for f in cudart if "cuda_fp16.h" in str(f)][0] + cudart = os.path.join(os.path.dirname(cudart.locate()), "..") + # use cuda/std/type_traits as a proxy + cccl = min([f for f in cccl if re.match(".*cuda\\/std\\/type_traits.*", str(f))], key=lambda x: len(str(x))) + cccl = os.path.join(os.path.dirname(cccl.locate()), "../../..") + except PackageNotFoundError: + pass + except ValueError: + # cccl wheel is buggy (headers missing), skip using wheels + pass + else: + CUDA_HOME = (cudart, cccl) + return + + # Try conda + if "CONDA_PREFIX" in os.environ: + if PLATFORM_LINUX: + conda_include = os.path.join( + os.environ["CONDA_PREFIX"], "targets", f"{_conda_get_target_name()}", "include" + ) + elif PLATFORM_WIN: + conda_include = os.path.join(os.environ["CONDA_PREFIX"], "Library", "include") + else: + assert AssertionError + if os.path.isfile(os.path.join(conda_include, "cuda_fp16.h")) and os.path.isfile( + os.path.join(conda_include, "cuda/std/type_traits") + ): + CUDA_HOME = (os.path.join(conda_include, ".."),) + return + + # Try local + CUDA_PATH = os.environ.get("CUDA_PATH", None) + CUDA_HOME = os.environ.get("CUDA_HOME", None) + if CUDA_PATH is None and CUDA_HOME is None: + raise RuntimeError( + "cudart headers not found. Depending on how you install nvmath-python and other CUDA packages,\n" + "you may need to perform one of the steps below:\n" + " - conda install -c conda-forge cuda-cudart-dev cuda-cccl cuda-version=12\n" + " - export CUDA_HOME=/path/to/CUDA/Toolkit" + ) + elif CUDA_PATH is not None and CUDA_HOME is None: + CUDA_HOME = CUDA_PATH + elif CUDA_PATH is not None and CUDA_HOME is not None: + if CUDA_HOME != CUDA_PATH: + warnings.warn( + "Both CUDA_HOME and CUDA_PATH are set but not consistent. " "Ignoring CUDA_PATH...", stacklevel=2 + ) + CUDA_HOME = (CUDA_HOME,) + + +def _check_mathdx_home(): + # Find mathDx headers + global MATHDX_HOME + + # Try wheel + try: + MATHDX_HOME = files("nvidia-mathdx") + except PackageNotFoundError: + pass + else: + # use cufftdx.hpp as a proxy + MATHDX_HOME = [f for f in MATHDX_HOME if "cufftdx.hpp" in str(f)][0] + MATHDX_HOME = os.path.join(os.path.dirname(MATHDX_HOME.locate()), "..") + return + + # Try conda + if "CONDA_PREFIX" in os.environ: + if PLATFORM_LINUX: + conda_include = os.path.join(os.environ["CONDA_PREFIX"], "include") + elif PLATFORM_WIN: + conda_include = os.path.join(os.environ["CONDA_PREFIX"], "Library", "include") + if os.path.isfile(os.path.join(conda_include, "cufftdx.hpp")): + MATHDX_HOME = os.path.join(conda_include, "..") + return + + # Try local + if "MATHDX_HOME" not in os.environ: + raise RuntimeError( + "mathDx headers not found. Depending on how you install nvmath-python and other CUDA packages, " + "you may need to perform one of the steps below:\n" + " - pip install nvidia-mathdx\n" + " - conda install -c conda-forge mathdx\n" + " - export MATHDX_HOME=/path/to/mathdx" + ) + else: + MATHDX_HOME = os.environ["MATHDX_HOME"] + + +def get_mathdx_include_dirs(): + _check_mathdx_home() + + global MATHDX_HOME + return (MATHDX_HOME + "/include").encode("utf-8") + + +def get_cuda_include_dirs(): + _check_cuda_home() + + global CUDA_HOME + include_dirs = [(f"{h}" + "/include").encode("utf-8") for h in CUDA_HOME] + arr_include_dirs = (ctypes.c_char_p * len(include_dirs))() + arr_include_dirs[:] = include_dirs + return arr_include_dirs diff --git a/warp/native/mathdx.cpp b/warp/native/mathdx.cpp new file mode 100644 index 00000000..1dca0afa --- /dev/null +++ b/warp/native/mathdx.cpp @@ -0,0 +1,56 @@ +/** Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + */ + +#include "builtin.h" + +// stubs for platforms where there is no CUDA +#if !WP_ENABLE_CUDA || !WP_ENABLE_MATHDX + +extern "C" +{ + +WP_API +bool cuda_compile_fft( + const char* ltoir_output_path, + const char* symbol_name, int num_include_dirs, + const char** include_dirs, + const char* mathdx_include_dir, + int arch, + int size, + int elements_per_thread, + int direction, + int precision, + int* shared_memory_size) +{ + printf("CUDA is disabled and/or Warp was not compiled with MathDx support.\n"); + return false; +} + +WP_API bool cuda_compile_dot( + const char* ltoir_output_path, + const char* symbol_name, + int num_include_dirs, + const char** include_dirs, + const char* mathdx_include_dir, + int arch, + int M, + int N, + int K, + int precision, + int type, + int tA, + int tB, + int num_threads) +{ + printf("CUDA is disabled and/or Warp was not compiled with MathDx support.\n"); + return false; +} + +} // extern "C" + +#endif // !WP_ENABLE_CUDA || !WP_ENABLE_MATHDX diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index 1ca668d3..3aa3dbe7 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -2,9 +2,6 @@ #include "builtin.h" -// todo: requires CTK, replace with inline ptx -#include "cuda_pipeline_primitives.h" - #define USE_CUTE 0 #if USE_CUTE @@ -332,4 +329,4 @@ void adj_tile_matmul(TileA& a, TileB& b, TileC& c, -} // namespace wp \ No newline at end of file +} // namespace wp diff --git a/warp/native/warp.cpp b/warp/native/warp.cpp index ed3efbc4..2fd64562 100644 --- a/warp/native/warp.cpp +++ b/warp/native/warp.cpp @@ -147,6 +147,11 @@ int is_cutlass_enabled() return int(WP_ENABLE_CUTLASS); } +int is_mathdx_enabled() +{ + return int(WP_ENABLE_MATHDX); +} + int is_debug_enabled() { return int(WP_ENABLE_DEBUG); @@ -1038,7 +1043,7 @@ WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* i WP_API void* cuda_load_module(void* context, const char* ptx) { return NULL; } WP_API void cuda_unload_module(void* context, void* module) {} WP_API void* cuda_get_kernel(void* context, void* module, const char* name) { return NULL; } -WP_API size_t cuda_launch_kernel(void* context, void* kernel, size_t dim, int max_blocks, void** args, void* stream) { return 0; } +WP_API size_t cuda_launch_kernel(void* context, void* kernel, size_t dim, int max_blocks, int tile_size, void** args, void* stream) { return 0; } WP_API void cuda_set_context_restore_policy(bool always_restore) {} WP_API int cuda_get_context_restore_policy() { return false; } diff --git a/warp/native/warp.cu b/warp/native/warp.cu index b2554ed4..76f7b97f 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -16,7 +16,9 @@ #include #include #include -#include +#if WP_ENABLE_MATHDX + #include +#endif #include #include @@ -129,26 +131,6 @@ bool check_nvjitlink_result(nvJitLinkHandle handle, nvJitLinkResult result, cons } } -bool check_cufftdx_result(commonDxStatusType result, const char* file, int line) -{ - if (result != commonDxStatusType::COMMONDX_SUCCESS) { - fprintf(stderr, "libmathdx cuFFTDx error: %d on %s:%d\n", (int)result, file, line); - return false; - } else { - return true; - } -} - -bool check_cublasdx_result(commonDxStatusType result, const char* file, int line) -{ - if (result != commonDxStatusType::COMMONDX_SUCCESS) { - fprintf(stderr, "libmathdx cuBLASDx error: %d on %s:%d\n", (int)result, file, line); - return false; - } else { - return true; - } -} - bool check_generic(int result, const char* file, int line) { if (!result) { @@ -2628,104 +2610,6 @@ bool write_file(const char* data, size_t size, std::string filename, const char* } } -bool cuda_compile_fft(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int size, int elements_per_thread, int direction, int precision, int* shared_memory_size) -{ - - CHECK_ANY(ltoir_output_path != nullptr); - CHECK_ANY(symbol_name != nullptr); - CHECK_ANY(mathdx_include_dir != nullptr); - CHECK_ANY(shared_memory_size != nullptr); - CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); - - bool res = true; - cufftdxHandle h; - CHECK_CUFFTDX(cufftDxCreate(&h)); - - // CUFFTDX_API_BLOCK_LMEM means each thread starts with a subset of the data - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_API, cufftDxApi::CUFFTDX_API_BLOCK_LMEM)); - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_EXECUTION, commonDxExecution::COMMONDX_EXECUTION_BLOCK)); - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_SIZE, (long long)size)); - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_DIRECTION, (cufftDxDirection)direction)); - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_PRECISION, (commonDxPrecision)precision)); - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_SM, (long long)(arch * 10))); - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_ELEMENTS_PER_THREAD, (long long)(elements_per_thread))); - CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_FFTS_PER_BLOCK, 1)); - - CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); - for(int dir = 0; dir < num_include_dirs; dir++) - { - CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); - } - CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); - - size_t lto_size = 0; - CHECK_CUFFTDX(cufftDxGetLTOIRSize(h, <o_size)); - - std::vector lto(lto_size); - CHECK_CUFFTDX(cufftDxGetLTOIR(h, lto.size(), lto.data())); - - long long int smem = 0; - CHECK_CUFFTDX(cufftDxGetTraitInt64(h, cufftDxTraitType::CUFFTDX_TRAIT_SHARED_MEMORY_SIZE, &smem)); - *shared_memory_size = (int)smem; - - if(!write_file(lto.data(), lto.size(), ltoir_output_path, "wb")) { - res = false; - } - - CHECK_CUFFTDX(cufftDxDestroy(h)); - - return res; -} - -bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision, int type, int tA, int tB, int num_threads) -{ - - CHECK_ANY(ltoir_output_path != nullptr); - CHECK_ANY(symbol_name != nullptr); - CHECK_ANY(mathdx_include_dir != nullptr); - CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); - - bool res = true; - cublasdxHandle h; - CHECK_CUBLASDX(cublasDxCreate(&h)); - - CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_FUNCTION, cublasDxFunction::CUBLASDX_FUNCTION_MM)); - CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_EXECUTION, commonDxExecution::COMMONDX_EXECUTION_BLOCK)); - CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_API, cublasDxApi::CUBLASDX_API_BLOCK_SMEM)); - CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_PRECISION, (commonDxPrecision)precision)); - CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SM, (long long)(arch * 10))); - CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TYPE, (cublasDxType)type)); - std::array block_dim = {num_threads, 1, 1}; - CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_BLOCK_DIM, block_dim.size(), block_dim.data())); - std::array size = {M, N, K}; - CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SIZE, size.size(), size.data())); - std::array transpose_mode = {(cublasDxTransposeMode_t)tA, (cublasDxTransposeMode_t)tB}; - CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TRANSPOSE_MODE, transpose_mode.size(), transpose_mode.data())); - - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); - for(int dir = 0; dir < num_include_dirs; dir++) - { - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); - } - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/cublasdx/include").c_str())); - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/../external/cutlass/include").c_str())); - - size_t lto_size = 0; - CHECK_CUBLASDX(cublasDxGetLTOIRSize(h, <o_size)); - - std::vector lto(lto_size); - CHECK_CUBLASDX(cublasDxGetLTOIR(h, lto.size(), lto.data())); - - if(!write_file(lto.data(), lto.size(), ltoir_output_path, "wb")) { - res = false; - } - - CHECK_CUBLASDX(cublasDxDestroy(h)); - - return res; -} - size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes) { // use file extension to determine whether to output PTX or CUBIN @@ -2971,6 +2855,126 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ return res; } +#if WP_ENABLE_MATHDX + bool check_cufftdx_result(commonDxStatusType result, const char* file, int line) + { + if (result != commonDxStatusType::COMMONDX_SUCCESS) { + fprintf(stderr, "libmathdx cuFFTDx error: %d on %s:%d\n", (int)result, file, line); + return false; + } else { + return true; + } + } + + bool check_cublasdx_result(commonDxStatusType result, const char* file, int line) + { + if (result != commonDxStatusType::COMMONDX_SUCCESS) { + fprintf(stderr, "libmathdx cuBLASDx error: %d on %s:%d\n", (int)result, file, line); + return false; + } else { + return true; + } + } + + bool cuda_compile_fft(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int size, int elements_per_thread, int direction, int precision, int* shared_memory_size) + { + + CHECK_ANY(ltoir_output_path != nullptr); + CHECK_ANY(symbol_name != nullptr); + CHECK_ANY(mathdx_include_dir != nullptr); + CHECK_ANY(shared_memory_size != nullptr); + CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); + + bool res = true; + cufftdxHandle h; + CHECK_CUFFTDX(cufftDxCreate(&h)); + + // CUFFTDX_API_BLOCK_LMEM means each thread starts with a subset of the data + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_API, cufftDxApi::CUFFTDX_API_BLOCK_LMEM)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_EXECUTION, commonDxExecution::COMMONDX_EXECUTION_BLOCK)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_SIZE, (long long)size)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_DIRECTION, (cufftDxDirection)direction)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_PRECISION, (commonDxPrecision)precision)); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_SM, (long long)(arch * 10))); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_ELEMENTS_PER_THREAD, (long long)(elements_per_thread))); + CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_FFTS_PER_BLOCK, 1)); + + CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); + for(int dir = 0; dir < num_include_dirs; dir++) + { + CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); + } + CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); + + size_t lto_size = 0; + CHECK_CUFFTDX(cufftDxGetLTOIRSize(h, <o_size)); + + std::vector lto(lto_size); + CHECK_CUFFTDX(cufftDxGetLTOIR(h, lto.size(), lto.data())); + + long long int smem = 0; + CHECK_CUFFTDX(cufftDxGetTraitInt64(h, cufftDxTraitType::CUFFTDX_TRAIT_SHARED_MEMORY_SIZE, &smem)); + *shared_memory_size = (int)smem; + + if(!write_file(lto.data(), lto.size(), ltoir_output_path, "wb")) { + res = false; + } + + CHECK_CUFFTDX(cufftDxDestroy(h)); + + return res; + } + + bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision, int type, int tA, int tB, int num_threads) + { + + CHECK_ANY(ltoir_output_path != nullptr); + CHECK_ANY(symbol_name != nullptr); + CHECK_ANY(mathdx_include_dir != nullptr); + CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); + + bool res = true; + cublasdxHandle h; + CHECK_CUBLASDX(cublasDxCreate(&h)); + + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_FUNCTION, cublasDxFunction::CUBLASDX_FUNCTION_MM)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_EXECUTION, commonDxExecution::COMMONDX_EXECUTION_BLOCK)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_API, cublasDxApi::CUBLASDX_API_BLOCK_SMEM)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_PRECISION, (commonDxPrecision)precision)); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SM, (long long)(arch * 10))); + CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TYPE, (cublasDxType)type)); + std::array block_dim = {num_threads, 1, 1}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_BLOCK_DIM, block_dim.size(), block_dim.data())); + std::array size = {M, N, K}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SIZE, size.size(), size.data())); + std::array transpose_mode = {(cublasDxTransposeMode_t)tA, (cublasDxTransposeMode_t)tB}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TRANSPOSE_MODE, transpose_mode.size(), transpose_mode.data())); + + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); + for(int dir = 0; dir < num_include_dirs; dir++) + { + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); + } + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/cublasdx/include").c_str())); + CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/../external/cutlass/include").c_str())); + + size_t lto_size = 0; + CHECK_CUBLASDX(cublasDxGetLTOIRSize(h, <o_size)); + + std::vector lto(lto_size); + CHECK_CUBLASDX(cublasDxGetLTOIR(h, lto.size(), lto.data())); + + if(!write_file(lto.data(), lto.size(), ltoir_output_path, "wb")) { + res = false; + } + + CHECK_CUBLASDX(cublasDxDestroy(h)); + + return res; + } +#endif + void* cuda_load_module(void* context, const char* path) { ContextGuard guard(context); @@ -3253,7 +3257,6 @@ void cuda_timing_end(timing_result_t* results, int size) g_cuda_timing_state = parent_state; } - // impl. files #include "bvh.cu" #include "mesh.cu" diff --git a/warp/native/warp.h b/warp/native/warp.h index 1a90e0d6..045d5f0a 100644 --- a/warp/native/warp.h +++ b/warp/native/warp.h @@ -34,6 +34,8 @@ extern "C" WP_API int is_cuda_compatibility_enabled(); // whether Warp was compiled with CUTLASS support WP_API int is_cutlass_enabled(); + // whether Warp was compiled with MathDx support + WP_API int is_mathdx_enabled(); // whether Warp was compiled with debug support WP_API int is_debug_enabled(); @@ -315,9 +317,9 @@ extern "C" WP_API bool cuda_graph_launch(void* graph, void* stream); WP_API bool cuda_graph_destroy(void* context, void* graph); + WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes); WP_API bool cuda_compile_fft(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int size, int elements_per_thread, int direction, int precision, int* shared_memory_size); WP_API bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision, int type, int tA, int tB, int num_threads); - WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes); WP_API void* cuda_load_module(void* context, const char* ptx); WP_API void cuda_unload_module(void* context, void* module); diff --git a/warp/stubs.py b/warp/stubs.py index f9d7be6b..1a41fd5f 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -886,6 +886,66 @@ def spatial_mass( ... +@over +def tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile: + """Allocate a tile local block of zero'd memory""" + ... + + +@over +def tile_load(a: Array[Any], x: int32, y: int32, m: int32, n: int32) -> Tile: + """Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)""" + ... + + +@over +def tile_store(a: Array[Any], x: int32, y: int32, t: Any): + """Store tile `t` to an array `a` at offset `(i=x*m, j=y*n)`""" + ... + + +@over +def tile_atomic_add(a: Array[Any], x: int32, y: int32, t: Any) -> Tile: + """Atomically add a tile `t` worth of data to array `a` at offset `(i=x*m, j=y*n)`""" + ... + + +@over +def tile(x: Any) -> Tile: + """Construct a Tile from a per-thread kernel value, returns a tile with dimensions of `(1, block_dim)` where block_dim is the number of threads specified in `wp.launch()`""" + ... + + +@over +def tile_extract(a: Tile, i: int32, j: int32): + """Extract element at index (i, j) of the tile and return the native type""" + ... + + +@over +def tile_matmul(a: Tile, b: Tile, out: Tile): + """Compute matrix product and accumulate out += a*b.""" + ... + + +@over +def tile_sum(a: Tile): + """Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored""" + ... + + +@over +def tile_map(op: Callable, a: Any): + """Map the operation onto each element of the tile""" + ... + + +@over +def tile_map(op: Callable, a: Any, b: Any): + """Map the operation onto each element of the tile""" + ... + + @over def mlp( weights: Array[float32], @@ -2083,6 +2143,12 @@ def add(a: Transformation[Scalar], b: Transformation[Scalar]) -> Transformation[ ... +@over +def add(a: Tile, b: Tile): + """Add each element of two tiles together""" + ... + + @over def sub(a: Scalar, b: Scalar) -> Scalar: """ """ @@ -2233,6 +2299,18 @@ def mul(a: Transformation[Scalar], b: Scalar) -> Transformation[Scalar]: ... +@over +def mul(x: Tile, y: Scalar) -> Tile: + """Multiply each element of a tile by a scalar""" + ... + + +@over +def mul(x: Scalar, y: Tile) -> Tile: + """Multiply each element of a tile by a scalar""" + ... + + @over def mod(a: Scalar, b: Scalar) -> Scalar: """Modulo operation using truncated division.""" @@ -2341,6 +2419,12 @@ def neg(x: Matrix[Any, Any, Scalar]) -> Matrix[Any, Any, Scalar]: ... +@over +def neg(x: Tile) -> Tile: + """Negate each element of a tile""" + ... + + @over def unot(a: bool) -> bool: """ """ @@ -2399,3 +2483,21 @@ def unot(a: uint64) -> bool: def unot(a: Array[Any]) -> bool: """ """ ... + + +@over +def tile_matmul_dx(a: Tile, b: Tile, out: Tile): + """Compute matrix product and accumulate out += a*b.""" + ... + + +@over +def tile_fft_dx(inout: Tile): + """Compute the FFT along the second dimension of a 2D tile of data.""" + ... + + +@over +def tile_ifft_dx(inout: Tile): + """Compute the inverse FFT along the second dimension of a 2D tile of data.""" + ... diff --git a/warp/tape.py b/warp/tape.py index 2aef1b0e..6df7c21b 100644 --- a/warp/tape.py +++ b/warp/tape.py @@ -130,7 +130,7 @@ def backward(self, loss: wp.array = None, grads: dict = None): outputs = launch[4] device = launch[5] block_dim = launch[6] - + adj_inputs = [] adj_outputs = [] @@ -152,7 +152,7 @@ def backward(self, loss: wp.array = None, grads: dict = None): device=device, adjoint=True, max_blocks=max_blocks, - block_dim=block_dim + block_dim=block_dim, ) # record a kernel launch on the tape @@ -614,7 +614,9 @@ def emit_kernel_launch_node( self.array_grad_stats.insert(0, grad_stats) -Launch = namedtuple("Launch", ["id", "kernel", "dim", "max_blocks", "inputs", "outputs", "device", "block_dim", "metadata"]) +Launch = namedtuple( + "Launch", ["id", "kernel", "dim", "max_blocks", "inputs", "outputs", "device", "block_dim", "metadata"] +) RepeatedSequence = namedtuple("RepeatedSequence", ["start", "end", "repetitions"]) @@ -645,8 +647,8 @@ def visit_tape( def get_launch_id(launch): kernel = launch[0] suffix = "" - if len(launch) > 6: - metadata = launch[6] + if len(launch) > 7: + metadata = launch[7] # calling function helps to identify unique launches if "caller" in metadata: caller = metadata["caller"] @@ -680,7 +682,8 @@ def get_launch_id(launch): inputs=launch[3], outputs=launch[4], device=launch[5], - metadata=launch[6] if len(launch) > 6 else {}, + block_dim=launch[6], + metadata=launch[7] if len(launch) > 7 else {}, ) for launch in kernel_launches ] diff --git a/warp/tests/test_mat_scalar_ops.py b/warp/tests/test_mat_scalar_ops.py index 67b6c0c7..61df6c38 100644 --- a/warp/tests/test_mat_scalar_ops.py +++ b/warp/tests/test_mat_scalar_ops.py @@ -1501,7 +1501,7 @@ def test_matmat_multiplication(test, device, dtype, register_kernels=False): tol = { np.float16: 2.0e-2, np.float32: 5.0e-6, - np.float64: 1.0e-8, + np.float64: 5.0e-7, }.get(dtype, 0) wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)] diff --git a/warp/tests/test_spatial.py b/warp/tests/test_spatial.py index 4eb21a8c..7449d71c 100644 --- a/warp/tests/test_spatial.py +++ b/warp/tests/test_spatial.py @@ -1611,7 +1611,7 @@ def test_spatial_matmat_multiplication(test, device, dtype, register_kernels=Fal tol = { np.float16: 2.0e-2, np.float32: 5.0e-6, - np.float64: 1.0e-8, + np.float64: 5.0e-7, }.get(dtype, 0) wptype = wp.types.np_dtype_to_warp_type[np.dtype(dtype)] diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index d95b1f6d..ed47b4a3 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -1,14 +1,16 @@ -import numpy as np -import warp as wp +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +import unittest -wp.init() -wp.set_module_options({"enable_backward": True}) -wp.set_device("cuda:0") -wp.set_module_options({"fast_math": True}) -#wp.config.mode = "debug" -#wp.config.verify_cuda = True +import numpy as np -wp.build.clear_kernel_cache() +import warp as wp +from warp.tests.unittest_utils import * TILE_M = wp.constant(8) TILE_N = wp.constant(4) @@ -17,118 +19,122 @@ # num threads per-tile TILE_DIM = 64 + @wp.kernel -def tile_copy(A: wp.array2d(dtype=float), - B: wp.array2d(dtype=float)): - +def tile_copy(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): # tile index - i, j, _ = wp.tid() - + i, j, _ = wp.tid() + a = wp.tile_load(A, i, j, m=TILE_M, n=TILE_N) wp.tile_store(B, i, j, a) -def test_tile_copy(): - +def test_tile_copy(test, device): rng = np.random.default_rng(42) - M = TILE_M*7 - N = TILE_N*5 + M = TILE_M * 7 + N = TILE_N * 5 A = rng.random((M, N), dtype=np.float32) B = rng.random((M, N), dtype=np.float32) - A_wp = wp.array(A, requires_grad=True) - B_wp = wp.array(B, requires_grad=True) + A_wp = wp.array(A, requires_grad=True, device=device) + B_wp = wp.array(B, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_copy, dim=[int(M/TILE_M), int(N/TILE_N), TILE_DIM], inputs=[A_wp, B_wp], block_dim=TILE_DIM) + wp.launch( + tile_copy, + dim=[int(M / TILE_M), int(N / TILE_N), TILE_DIM], + inputs=[A_wp, B_wp], + block_dim=TILE_DIM, + device=device, + ) # verify forward pass - assert(np.allclose(A, B_wp.numpy(), rtol=1.e-4)) - print("Copy forward passed") + assert_array_equal(B_wp, A_wp) # verify backward pass - B_wp.grad = wp.ones_like(B_wp) + B_wp.grad = wp.ones_like(B_wp, device=device) tape.backward() - assert(np.allclose(A_wp.grad.numpy(), B_wp.grad.numpy())) - print("Copy backward passed") + assert_array_equal(B_wp.grad, A_wp.grad) + @wp.func def unary_func(x: float): return wp.sin(x) + @wp.kernel -def tile_unary_map(input: wp.array2d(dtype=float), - output: wp.array2d(dtype=float)): - +def tile_unary_map(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): # tile index - i, j, _ = wp.tid() - + i, j, _ = wp.tid() + a = wp.tile_load(input, i, j, m=TILE_M, n=TILE_N) - + sa = wp.tile_map(wp.sin, a) - - wp.tile_store(output, i, j, sa) + wp.tile_store(output, i, j, sa) -def test_tile_unary_map(): +def test_tile_unary_map(test, device): rng = np.random.default_rng(42) - M = TILE_M*7 - N = TILE_N*5 + M = TILE_M * 7 + N = TILE_N * 5 A = rng.random((M, N), dtype=np.float32) B = np.sin(A) A_grad = np.cos(A) - A_wp = wp.array(A, requires_grad=True) - B_wp = wp.zeros_like(A_wp, requires_grad=True) + A_wp = wp.array(A, requires_grad=True, device=device) + B_wp = wp.zeros_like(A_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_unary_map, dim=[int(M/TILE_M), int(N/TILE_N), TILE_DIM], inputs=[A_wp, B_wp], block_dim=TILE_DIM) + wp.launch( + tile_unary_map, + dim=[int(M / TILE_M), int(N / TILE_N), TILE_DIM], + inputs=[A_wp, B_wp], + block_dim=TILE_DIM, + device=device, + ) # verify forward pass - assert(np.allclose(B, B_wp.numpy(), atol=1.e-4)) - print("Unary map forward passed") + assert_np_equal(B_wp.numpy(), B, tol=1.0e-4) # verify backward pass - B_wp.grad = wp.ones_like(B_wp) + B_wp.grad = wp.ones_like(B_wp, device=device) tape.backward() - assert(np.allclose(A_wp.grad.numpy(), A_grad)) - print("Unary map backward passed") + assert_np_equal(A_wp.grad.numpy(), A_grad, tol=1.0e-6) @wp.func def binary_func(x: float, y: float): return wp.sin(x) + y + @wp.kernel -def tile_binary_map(input_a: wp.array2d(dtype=float), - input_b: wp.array2d(dtype=float), - output: wp.array2d(dtype=float)): - +def tile_binary_map( + input_a: wp.array2d(dtype=float), input_b: wp.array2d(dtype=float), output: wp.array2d(dtype=float) +): # tile index - i, j, _= wp.tid() - + i, j, _ = wp.tid() + a = wp.tile_load(input_a, i, j, m=TILE_M, n=TILE_N) b = wp.tile_load(input_b, i, j, m=TILE_M, n=TILE_N) - + sa = wp.tile_map(binary_func, a, b) - - wp.tile_store(output, i, j, sa) + wp.tile_store(output, i, j, sa) -def test_tile_binary_map(): +def test_tile_binary_map(test, device): rng = np.random.default_rng(42) - M = TILE_M*7 - N = TILE_N*5 + M = TILE_M * 7 + N = TILE_N * 5 A = rng.random((M, N), dtype=np.float32) B = rng.random((M, N), dtype=np.float32) @@ -137,32 +143,32 @@ def test_tile_binary_map(): A_grad = np.cos(A) B_grad = np.ones_like(B) - A_wp = wp.array(A, requires_grad=True) - B_wp = wp.array(B, requires_grad=True) - C_wp = wp.zeros_like(A_wp, requires_grad=True) + A_wp = wp.array(A, requires_grad=True, device=device) + B_wp = wp.array(B, requires_grad=True, device=device) + C_wp = wp.zeros_like(A_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_binary_map, dim=[int(M/TILE_M), int(N/TILE_N), TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) + wp.launch( + tile_binary_map, + dim=[int(M / TILE_M), int(N / TILE_N), TILE_DIM], + inputs=[A_wp, B_wp, C_wp], + block_dim=TILE_DIM, + device=device, + ) # verify forward pass - assert(np.allclose(C, C_wp.numpy(), rtol=1.e-4)) - print("Binary map forward passed") + assert_np_equal(C_wp.numpy(), C, tol=1.0e-6) # verify backward pass - C_wp.grad = wp.ones_like(C_wp) + C_wp.grad = wp.ones_like(C_wp, device=device) tape.backward() - assert(np.allclose(A_wp.grad.numpy(), A_grad, rtol=1.e-2)) - assert(np.allclose(B_wp.grad.numpy(), B_grad, rtol=1.e-2)) - - print("Binary map backward passed") + assert_np_equal(A_wp.grad.numpy(), A_grad, tol=1.0e-6) + assert_np_equal(B_wp.grad.numpy(), B_grad) @wp.kernel -def tile_grouped_gemm(A: wp.array3d(dtype=float), - B: wp.array3d(dtype=float), - C: wp.array3d(dtype=float)): - +def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), C: wp.array3d(dtype=float)): # output tile index i = wp.tid() @@ -176,8 +182,8 @@ def tile_grouped_gemm(A: wp.array3d(dtype=float), wp.tile_store(C[i], 0, 0, sum) -def test_tile_grouped_gemm(): - +@unittest.expectedFailure +def test_tile_grouped_gemm(test, device): batch_count = 56 M = TILE_M @@ -187,29 +193,25 @@ def test_tile_grouped_gemm(): rng = np.random.default_rng(42) A = rng.random((batch_count, M, K), dtype=np.float32) B = rng.random((batch_count, K, N), dtype=np.float32) - C = np.zeros((batch_count, M, N), dtype=np.float32) + C = A @ B - A_wp = wp.array(A, requires_grad=True) - B_wp = wp.array(B, requires_grad=True) - C_wp = wp.array(C, requires_grad=True) + A_wp = wp.array(A, requires_grad=True, device=device) + B_wp = wp.array(B, requires_grad=True, device=device) + C_wp = wp.array(C, requires_grad=True, device=device) - with wp.Tape() as tape: - wp.launch(tile_grouped_gemm, dim=[batch_count, TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) - - # bring back to host - C_host = C_wp.numpy() + with wp.Tape() as tape: + wp.launch( + tile_grouped_gemm, dim=[batch_count, TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, device=device + ) - # GEMM forward passed - print("Batched matmul forward passed") + # TODO: 32 mismatched elements + assert_np_equal(C_wp.numpy(), C) @wp.kernel -def tile_gemm(A: wp.array2d(dtype=float), - B: wp.array2d(dtype=float), - C: wp.array2d(dtype=float)): - +def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): # output tile index - i, j, _= wp.tid() + i, j, _ = wp.tid() sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) @@ -217,10 +219,9 @@ def tile_gemm(A: wp.array2d(dtype=float), N = B.shape[1] K = A.shape[1] - count = int(K / TILE_K) - - for k in range(0, count): + count = int(K / TILE_K) + for k in range(0, count): a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) @@ -230,66 +231,62 @@ def tile_gemm(A: wp.array2d(dtype=float), wp.tile_store(C, i, j, sum) -def test_tile_gemm(): - - M = TILE_M*7 - K = TILE_K*6 - N = TILE_N*5 +def test_tile_gemm(test, device): + M = TILE_M * 7 + K = TILE_K * 6 + N = TILE_N * 5 rng = np.random.default_rng(42) A = rng.random((M, K), dtype=np.float32) B = rng.random((K, N), dtype=np.float32) C = np.zeros((M, N), dtype=np.float32) - A_wp = wp.array(A, requires_grad=True) - B_wp = wp.array(B, requires_grad=True) - C_wp = wp.array(C, requires_grad=True) - - with wp.Tape() as tape: - wp.launch(tile_gemm, dim=(int(M/TILE_M), int(N/TILE_N), TILE_DIM), inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) + A_wp = wp.array(A, requires_grad=True, device=device) + B_wp = wp.array(B, requires_grad=True, device=device) + C_wp = wp.array(C, requires_grad=True, device=device) - assert(np.allclose(A@B, C_wp.numpy(), rtol=1.e-4)) + with wp.Tape() as tape: + wp.launch( + tile_gemm, + dim=(int(M / TILE_M), int(N / TILE_N), TILE_DIM), + inputs=[A_wp, B_wp, C_wp], + block_dim=TILE_DIM, + device=device, + ) - # GEMM forward passed - print("Tiled matmul forward passed") + assert_np_equal(C_wp.numpy(), A @ B, tol=1.0e-5) adj_C = np.ones_like(C) - tape.backward(grads={C_wp: wp.array(adj_C)}) - - assert(np.allclose(adj_C@B.T, A_wp.grad.numpy(), rtol=1.e-4)) - assert(np.allclose(A.T@adj_C, B_wp.grad.numpy(), rtol=1.e-4)) - - print("Tiled matmul backward passed") + tape.backward(grads={C_wp: wp.array(adj_C, device=device)}) + assert_np_equal(A_wp.grad.numpy(), adj_C @ B.T, tol=1.0e-5) + assert_np_equal(B_wp.grad.numpy(), A.T @ adj_C, 1.0e-5) @wp.kernel -def tile_operators(input: wp.array3d(dtype=float), - output: wp.array3d(dtype=float)): - +def tile_operators(input: wp.array3d(dtype=float), output: wp.array3d(dtype=float)): # output tile index i, _ = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) - + # neg b = -a # right scalar multiply - c = b*0.5 + c = b * 0.5 # left scalar multiply - d = 0.5*c + d = 0.5 * c # add tiles e = a + d - - wp.tile_store(output[i], 0, 0, e) + wp.tile_store(output[i], 0, 0, e) -def test_tile_operators(): +def test_tile_operators(test, device): batch_count = 56 M = TILE_M @@ -297,41 +294,37 @@ def test_tile_operators(): rng = np.random.default_rng(42) input = rng.random((batch_count, M, N), dtype=np.float32) - output = input*0.75 + output = input * 0.75 - input_wp = wp.array(input, requires_grad=True) - output_wp = wp.zeros_like(input_wp, requires_grad=True) + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros_like(input_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_operators, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) + wp.launch( + tile_operators, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device + ) - assert(np.allclose(output, output_wp.numpy(), rtol=1.e-4)) - - print("Operators forward passed") + assert_np_equal(output_wp.numpy(), output) output_wp.grad.fill_(1.0) tape.backward() - assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.75, rtol=1.e-4)) - - print("Operators backward passed") + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.75) @wp.kernel -def tile_sum_kernel(input: wp.array3d(dtype=float), - output: wp.array(dtype=float)): - +def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): # output tile index i, _ = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) - s = wp.tile_sum(a)*0.5 + s = wp.tile_sum(a) * 0.5 wp.tile_store(output, i, 0, s) -def test_tile_sum(): +def test_tile_sum(test, device): batch_count = 56 M = TILE_M @@ -340,34 +333,33 @@ def test_tile_sum(): rng = np.random.default_rng(42) input = rng.random((batch_count, M, N), dtype=np.float32) - input_wp = wp.array(input, requires_grad=True) - output_wp = wp.zeros(batch_count, requires_grad=True) + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros(batch_count, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_sum_kernel, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) + wp.launch( + tile_sum_kernel, + dim=[batch_count, TILE_DIM], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device, + ) + sum_wp = output_wp.numpy() for i in range(batch_count): - sum_np = np.sum(input[i])*0.5 - sum_wp = output_wp.numpy()[i] - - assert(np.allclose(sum_np, sum_wp, rtol=1.e-4)) - - print("Sum forward passed") + sum_np = np.sum(input[i]) * 0.5 + test.assertAlmostEqual(sum_wp[i], sum_np, places=5) output_wp.grad.fill_(1.0) tape.backward() - assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.5, rtol=1.e-4)) - - print("Sum backward passed") + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5) @wp.kernel -def tile_extract_kernel(input: wp.array2d(dtype=float), - output: wp.array2d(dtype=float)): - +def tile_extract_kernel(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): # output tile index i, _ = wp.tid() @@ -377,126 +369,38 @@ def tile_extract_kernel(input: wp.array2d(dtype=float), # tile element individually for i in range(TILE_M): for j in range(TILE_N): - output[i,j] = t[i,j] + output[i, j] = t[i, j] -def test_tile_extract(): +def test_tile_extract(test, device): M = TILE_M N = TILE_N rng = np.random.default_rng(42) input = rng.random((M, N), dtype=np.float32) - input_wp = wp.array(input, requires_grad=True) - output_wp = wp.zeros_like(input_wp, requires_grad=True) + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros_like(input_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_extract_kernel, dim=[1, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) - - assert(np.allclose(input_wp.numpy(), output_wp.numpy(), rtol=1.e-4)) + wp.launch( + tile_extract_kernel, dim=[1, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device + ) - print("Extract forward passed") + assert_array_equal(output_wp, input_wp) output_wp.grad.fill_(1.0) tape.backward() - assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input), rtol=1.e-4)) + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input)) - print("Extract backward passed") - -@wp.kernel() -def tile_matmul_dx_kernel(ga: wp.array2d(dtype=wp.float64), - gb: wp.array2d(dtype=wp.float64), - gc: wp.array2d(dtype=wp.float64)): - i, j, _ = wp.tid() - a = wp.tile_load(ga, i, j, m=TILE_M, n=TILE_K) - b = wp.tile_load(gb, i, j, m=TILE_K, n=TILE_N) - c = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float64) - wp.tile_matmul_dx(a, b, c) - wp.tile_store(gc, i, j, c) - -def test_tile_matmul_dx(): - - rng = np.random.default_rng(42) - - A = rng.random((TILE_M, TILE_K), dtype=np.float64) - B = rng.random((TILE_K, TILE_N), dtype=np.float64) - C = np.zeros((TILE_M, TILE_N), dtype=np.float64) - - A_wp = wp.array(A, requires_grad=True) - B_wp = wp.array(B, requires_grad=True) - C_wp = wp.array(C, requires_grad=True) - - with wp.Tape() as tape: - wp.launch(tile_matmul_dx_kernel, dim=[1, 1, TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM) - - # verify forward pass - assert(np.allclose(A @ B, C_wp.numpy(), rtol=1.e-4)) - - print("Matmul (Dx) forward passed") - - adj_C = np.ones_like(C) - - tape.backward(grads={C_wp: wp.array(adj_C)}) - - assert(np.allclose(adj_C@B.T, A_wp.grad.numpy(), rtol=1.e-4)) - assert(np.allclose(A.T@adj_C, B_wp.grad.numpy(), rtol=1.e-4)) - - print("Matmul (Dx) backward passed") - -N_FFT = 128 - -@wp.kernel() -def tile_fft_dx_kernel(gx: wp.array2d(dtype=wp.vec2f), - gy: wp.array2d(dtype=wp.vec2f)): - i, j, _ = wp.tid() - xy = wp.tile_load(gx, i, j, m=N_FFT, n=N_FFT) - wp.tile_fft_dx(xy) - wp.tile_store(gy, i, j, xy) - -def test_tile_fft_dx(): - - rng = np.random.default_rng(42) - - # Warp doesn't really have a complex64 type, - # so we use 2 float32 to represent a single complex64 number and then convert it to vec2f - - X = rng.random((N_FFT, 2*N_FFT), dtype=np.float32) - Y = np.zeros_like(X) - - X_wp = wp.array2d(X, requires_grad=True, dtype=wp.vec2f) - Y_wp = wp.array2d(Y, requires_grad=True, dtype=wp.vec2f) - - X_c64 = X.view(np.complex64).reshape(N_FFT, N_FFT) - Y_c64 = np.fft.fft(X_c64, axis=-1) - - with wp.Tape() as tape: - wp.launch(tile_fft_dx_kernel, dim=[1, 1, TILE_DIM], inputs=[X_wp, Y_wp], block_dim=TILE_DIM) - - Y_wp_c64 = Y_wp.numpy().view(np.complex64).reshape(N_FFT, N_FFT) - assert(np.allclose(Y_c64, Y_wp_c64, rtol=1.e-4)) - - print("FFT (Dx) forward passed") - - # TODO: implement and test backward pass - -test_tile_copy() -test_tile_unary_map() -test_tile_binary_map() -test_tile_grouped_gemm() -test_tile_gemm() -test_tile_operators() -test_tile_sum() -test_tile_extract() -test_tile_matmul_dx() -test_tile_fft_dx() # #----------------------------------------- # # center of mass computation -# start = offset[i] -# end = offset[i+1] +# start = offset[i] +# end = offset[i+1] # com = wp.tile_zeros(dtype=wp.vec3, M=1) @@ -504,7 +408,7 @@ def test_tile_fft_dx(): # for i in range(start, end, N): # count = wp.min(N, end-i) - + # idx = wp.tile_load(indices, i, N, max_col=count) # p = wp.tile_load(points, idx, max_col=count) @@ -514,13 +418,12 @@ def test_tile_fft_dx(): # wp.tile_store(out[i], com) - # #------------------------------------------- # # compute deformation gradient -# i = +# i = # j = -# k = +# k = # l = # f = wp.tile(F) # generate a block size tile of feature vectors @@ -545,7 +448,7 @@ def test_tile_fft_dx(): # #---------------------------------- # # MLP with helper function for linear layers # # where shape is only partially known -# # at compile time, and the other dims +# # at compile time, and the other dims # # are inferred from the input vector # f = wp.tile(F) @@ -562,32 +465,33 @@ def test_tile_fft_dx(): # o = wp.untile(z) - # #---------------------------------- # # softmax # def softmax(z: Any): - + # e = wp.tile_map(wp.exp, z) # s = wp.tile_sum(e, dim=0) # return z/s[0] +devices = get_cuda_test_devices() +class TestTile(unittest.TestCase): + pass +add_function_test(TestTile, "test_tile_copy", test_tile_copy, devices=devices) +add_function_test(TestTile, "test_tile_unary_map", test_tile_unary_map, devices=devices) +add_function_test(TestTile, "test_tile_binary_map", test_tile_binary_map, devices=devices) +add_function_test(TestTile, "test_tile_grouped_gemm", test_tile_grouped_gemm, devices=devices) # FAILS +add_function_test(TestTile, "test_tile_gemm", test_tile_gemm, devices=devices) +add_function_test(TestTile, "test_tile_operators", test_tile_operators, devices=devices) +add_function_test(TestTile, "test_tile_sum", test_tile_sum, devices=devices) +add_function_test(TestTile, "test_tile_extract", test_tile_extract, devices=devices) - - - - - - - - - - - - +if __name__ == "__main__": + wp.clear_kernel_cache() + unittest.main(verbosity=2) diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py new file mode 100644 index 00000000..6cf4b7c1 --- /dev/null +++ b/warp/tests/test_tile_mathdx.py @@ -0,0 +1,116 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +import unittest + +import numpy as np + +import warp as wp +from warp.tests.unittest_utils import * + +wp.init() # For wp.context.runtime.core.is_mathdx_enabled() + +TILE_M = wp.constant(8) +TILE_N = wp.constant(4) +TILE_K = wp.constant(8) + +N_FFT = wp.constant(128) + +# num threads per-tile +TILE_DIM = 64 + + +@wp.kernel() +def tile_math_dx_matmul_kernel( + ga: wp.array2d(dtype=wp.float64), gb: wp.array2d(dtype=wp.float64), gc: wp.array2d(dtype=wp.float64) +): + i, j, _ = wp.tid() + a = wp.tile_load(ga, i, j, m=TILE_M, n=TILE_K) + b = wp.tile_load(gb, i, j, m=TILE_K, n=TILE_N) + c = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float64) + wp.tile_matmul_dx(a, b, c) + wp.tile_store(gc, i, j, c) + + +def test_tile_math_dx_matmul(test, device): + rng = np.random.default_rng(42) + + A = rng.random((TILE_M, TILE_K), dtype=np.float64) + B = rng.random((TILE_K, TILE_N), dtype=np.float64) + C = np.zeros((TILE_M, TILE_N), dtype=np.float64) + + A_wp = wp.array(A, requires_grad=True, device=device) + B_wp = wp.array(B, requires_grad=True, device=device) + C_wp = wp.array(C, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch( + tile_math_dx_matmul_kernel, + dim=[1, 1, TILE_DIM], + inputs=[A_wp, B_wp, C_wp], + block_dim=TILE_DIM, + device=device, + ) + + # verify forward pass + assert_np_equal(C_wp.numpy(), A @ B) + + adj_C = np.ones_like(C) + + tape.backward(grads={C_wp: wp.array(adj_C, device=device)}) + + assert_np_equal(A_wp.grad.numpy(), adj_C @ B.T) + assert_np_equal(B_wp.grad.numpy(), A.T @ adj_C) + + +@wp.kernel() +def tile_math_dx_fft_kernel(gx: wp.array2d(dtype=wp.vec2f), gy: wp.array2d(dtype=wp.vec2f)): + i, j, _ = wp.tid() + xy = wp.tile_load(gx, i, j, m=N_FFT, n=N_FFT) + wp.tile_fft_dx(xy) + wp.tile_store(gy, i, j, xy) + + +def test_tile_math_dx_fft(test, device): + rng = np.random.default_rng(42) + + # Warp doesn't really have a complex64 type, + # so we use 2 float32 to represent a single complex64 number and then convert it to vec2f + + X = rng.random((N_FFT, 2 * N_FFT), dtype=np.float32) + Y = np.zeros_like(X) + + X_wp = wp.array2d(X, requires_grad=True, dtype=wp.vec2f, device=device) + Y_wp = wp.array2d(Y, requires_grad=True, dtype=wp.vec2f, device=device) + + X_c64 = X.view(np.complex64).reshape(N_FFT, N_FFT) + Y_c64 = np.fft.fft(X_c64, axis=-1) + + with wp.Tape() as tape: + wp.launch(tile_math_dx_fft_kernel, dim=[1, 1, TILE_DIM], inputs=[X_wp, Y_wp], block_dim=TILE_DIM, device=device) + + Y_wp_c64 = Y_wp.numpy().view(np.complex64).reshape(N_FFT, N_FFT) + + assert_np_equal(Y_wp_c64, Y_c64, tol=1.0e-4) + + # TODO: implement and test backward pass + + +devices = get_cuda_test_devices() + + +@unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") +class TestTileMathDx(unittest.TestCase): + pass + + +add_function_test(TestTileMathDx, "test_tile_math_dx_matmul", test_tile_math_dx_matmul, devices=devices) +add_function_test(TestTileMathDx, "test_tile_math_dx_fft", test_tile_math_dx_fft, devices=devices) + +if __name__ == "__main__": + wp.clear_kernel_cache() + unittest.main(verbosity=2) diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index a71e08d3..81491878 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -1,14 +1,16 @@ -import numpy as np -import warp as wp +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. -wp.init() -wp.set_module_options({"enable_backward": True}) -wp.set_device("cuda:0") -wp.set_module_options({"fast_math": True}) -#wp.config.mode = "debug" -#wp.config.verify_cuda = True +import unittest -wp.build.clear_kernel_cache() +import numpy as np + +import warp as wp +from warp.tests.unittest_utils import * TILE_M = wp.constant(8) TILE_N = wp.constant(4) @@ -19,19 +21,17 @@ @wp.kernel -def tile_sum_kernel(input: wp.array3d(dtype=float), - output: wp.array(dtype=float)): - +def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): # output tile index i, _ = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) - s = wp.tile_sum(a)*0.5 + s = wp.tile_sum(a) * 0.5 wp.tile_store(output, i, 0, s) -def test_tile_sum(): +def test_tile_reduce_sum(test, device): batch_count = 56 M = TILE_M @@ -40,83 +40,64 @@ def test_tile_sum(): rng = np.random.default_rng(42) input = rng.random((batch_count, M, N), dtype=np.float32) - input_wp = wp.array(input, requires_grad=True) - output_wp = wp.zeros(batch_count, requires_grad=True) + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros(batch_count, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_sum_kernel, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM) - - + wp.launch( + tile_sum_kernel, + dim=[batch_count, TILE_DIM], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device, + ) + + sum_wp = output_wp.numpy() for i in range(batch_count): - sum_np = np.sum(input[i])*0.5 - sum_wp = output_wp.numpy()[i] - - assert(np.allclose(sum_np, sum_wp, rtol=1.e-4)) - - print("Sum forward passed") + sum_np = np.sum(input[i]) * 0.5 + test.assertAlmostEqual(sum_wp[i], sum_np, places=5) output_wp.grad.fill_(1.0) tape.backward() - assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.5, rtol=1.e-4)) - - print("Sum backward passed") - + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5) @wp.kernel def tile_reduce_1d_kernel(output: wp.array(dtype=int)): - # output tile index i = wp.tid() - - t = wp.tile(i) # convert to block wide tile + + t = wp.tile(i) # convert to block wide tile s = wp.tile_sum(t) # sum over block # update global sum wp.tile_atomic_add(output, i, 0, s) -def test_tile_reduce_1d(): - N = int(TILE_DIM*3/2) +@unittest.expectedFailure +def test_tile_reduce_1d(test, device): + N = int(TILE_DIM * 3 / 2) - output = wp.zeros(shape=1, dtype=int, requires_grad=True) + output = wp.zeros(shape=1, dtype=int, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_reduce_1d_kernel, dim=[N], inputs=[output], block_dim=TILE_DIM) - - assert(np.sum(np.arange(N)), output.numpy()) - - print("Sum 1D forward passed") - - # output_wp.grad.fill_(1.0) - - # tape.backward() - - # assert(np.allclose(input_wp.grad.numpy(), np.ones_like(input)*0.5, rtol=1.e-4)) - - # print("Sum backward passed") - - -test_tile_sum() -test_tile_reduce_1d() - - - - - - - - - - - + wp.launch(tile_reduce_1d_kernel, dim=[N], inputs=[output], block_dim=TILE_DIM, device=device) + test.assertAlmostEqual(output.numpy()[0], np.sum(np.arange(N))) +devices = get_cuda_test_devices() +class TestTileReduce(unittest.TestCase): + pass +add_function_test(TestTileReduce, "test_tile_reduce_sum", test_tile_reduce_sum, devices=devices) +add_function_test(TestTileReduce, "test_tile_reduce_1d", test_tile_reduce_1d, devices=devices) # FAILS +if __name__ == "__main__": + wp.clear_kernel_cache() + unittest.main(verbosity=2) diff --git a/warp/tests/unittest_utils.py b/warp/tests/unittest_utils.py index 83e6ab2f..a94e6a36 100644 --- a/warp/tests/unittest_utils.py +++ b/warp/tests/unittest_utils.py @@ -232,6 +232,10 @@ def test_func(self): else: func(self, device, **kwargs) + # Copy the __unittest_expecting_failure__ attribute from func to test_func + if hasattr(func, "__unittest_expecting_failure__"): + test_func.__unittest_expecting_failure__ = func.__unittest_expecting_failure__ + return test_func diff --git a/warp/types.py b/warp/types.py index c346a044..9dbab2a4 100644 --- a/warp/types.py +++ b/warp/types.py @@ -1492,10 +1492,10 @@ def types_equal(a, b, match_generic=False): if is_array(a) and type(a) is type(b): return True - + if is_tile(a) and is_tile(b): return True - + return scalars_equal(a, b, match_generic) @@ -2957,7 +2957,6 @@ def array_type_id(a): # tile expression objects class Tile: - allocation = 0 def __init__(self, dtype, M, N, op=None, storage="register"): @@ -2973,7 +2972,7 @@ def ctype(self): if self.storage == "register": return f"wp::tile_register_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" - elif self.storage == "shared": + elif self.storage == "shared": return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" # generates C-initializer string @@ -2983,15 +2982,12 @@ def cinit(self, adjoint=False): if self.storage == "register": return self.ctype() + "(0.0)" elif self.storage == "shared": - if adjoint: # backward pass requires zeroed memory return f"wp::tile_alloc_zeros<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" else: # forward mode can be uninitialized until first used by the kernel return f"wp::tile_alloc_empty<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" - - # generate a unique allocation index for shared memory @classmethod @@ -3000,26 +2996,23 @@ def alloc(cls): Tile.allocation += 1 return index -class TileZeros(Tile): +class TileZeros(Tile): def __init__(self, dtype, M, N): Tile.__init__(self, dtype, M, N, op="zeros", storage="shared") - -class TileConstant(Tile): +class TileConstant(Tile): def __init__(self, dtype, M, N): Tile.__init__(self, dtype, M, N, op="constant", storage="register") - -class TileLoad(Tile): +class TileLoad(Tile): def __init__(self, array, M, N): Tile.__init__(self, array.dtype, M, N, op="load", storage="register") - -class TileUnaryMap(Tile): +class TileUnaryMap(Tile): def __init__(self, t): Tile.__init__(self, t.dtype, t.M, t.N, op="unary_map", storage="register") @@ -3027,7 +3020,6 @@ def __init__(self, t): class TileBinaryMap(Tile): - def __init__(self, a, b): Tile.__init__(self, a.dtype, a.M, a.N, op="binary_map", storage="register") @@ -3036,7 +3028,6 @@ def __init__(self, a, b): class TileShared(Tile): - def __init__(self, t): Tile.__init__(self, t.dtype, t.M, t.N, "shared", storage="shared") From 1c415a4bb6bc691409c0ad40305ccfca570a8547 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 26 Sep 2024 01:40:38 +0000 Subject: [PATCH 034/102] Update some docstrings --- warp/builtins.py | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 3b74d18d..e24e0d76 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1750,7 +1750,7 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar value_func=tile_zeros_value_func, dispatch_func=tile_zeros_dispatch_func, variadic=True, - doc="Allocate a tile local block of zero'd memory", + doc="Allocate a tile of zero initialized items", group="Tile Primitives", export=False, ) @@ -1909,8 +1909,27 @@ def tile_value_func(arg_types, arg_values): input_types={"x": Any}, value_func=tile_value_func, variadic=True, - doc="Construct a Tile from a per-thread kernel value, returns a tile with dimensions of `(1, block_dim)` where block_dim is the number of threads specified in `wp.launch()`", - group="Tile Primitives", + doc="""Construct a Tile from a per-thread kernel value. + + Args: + x (Any): A per-thread local value, e.g.: scalar, vector, or matrix. + + Returns: + Tile: A tile with dimensions of ``(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch().`` + + Examples: + This example shows how to create a linear sequence from thread variables: + + .. code-block:: python + + # get thread id + i = wp.tid() + + # convert to block wide tile + t = wp.tile(i*2) + """, + + group="Tile Primitives""", export=False, ) @@ -1919,7 +1938,7 @@ def tile_extract_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: - return None + return Scalar if len(arg_types) != 3: raise RuntimeError("tile_extract() requires 3 positional args") @@ -1945,7 +1964,7 @@ def tile_matmul_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: - return None + return Tile(dtype=Any, M=Any, N=Any) if len(arg_types) != 3: raise RuntimeError("tile_matmul() requires 4 positional args") @@ -1971,7 +1990,7 @@ def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a b = arg_values["b"] out = arg_values["out"] - # set the storage type to the inputs to shared + # force the storage type of the input variables to shared memory a.type.storage = "shared" b.type.storage = "shared" out.type.storage = "shared" @@ -1995,7 +2014,7 @@ def tile_sum_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: - return None + return Tile(dtype=Any, M=1, N=1) if len(arg_types) != 1: raise RuntimeError("tile_sum() requires 1 positional args") @@ -2024,7 +2043,7 @@ def tile_sum_value_func(arg_types, arg_values): def tile_unary_map_value_func(arg_types, arg_values): if arg_types is None: - return None + return Tile(dtype=Any, M=Any, N=Any) a = arg_types["a"] @@ -2048,7 +2067,7 @@ def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, ar #dispatch_func=tile_map_dispatch_func, #variadic=True, native_func="tile_unary_map", - doc="Map the operation onto each element of the tile", + doc="Unary map the operation onto each element of the tile.", group="Tile Primitives", export=False, ) @@ -2056,7 +2075,7 @@ def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, ar def tile_binary_map_value_func(arg_types, arg_values): if arg_types is None: - return None + return Tile(dtype=Any, M=Any, N=Any) a = arg_types["a"] b = arg_types["b"] @@ -2088,7 +2107,7 @@ def tile_binary_map_value_func(arg_types, arg_values): #dispatch_func=tile_map_dispatch_func, #variadic=True, native_func="tile_binary_map", - doc="Map the operation onto each element of the tile", + doc="Apply the binary map operation onto each corresponding pair of elements from each the tile.", group="Tile Primitives", export=False, ) @@ -4793,7 +4812,7 @@ def tile_matmul_generic_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: - return None + return Tile(dtype=Any, M=Any, N=Any) if len(arg_types) != 3: raise RuntimeError("tile_matmul() requires 4 positional args") @@ -4928,7 +4947,7 @@ def make_transpose(t): def tile_fft_generic_value_func(arg_types, arg_values): if arg_types is None: - return None + return Tile(dtype=Any, M=Any, N=Any) if len(arg_types) != 1: raise RuntimeError("tile_fft() requires 1 positional args") From 53968c6d86747d37c6e70af718eb6f709d524682 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 26 Sep 2024 10:15:32 +0000 Subject: [PATCH 035/102] Add wp.tile_ones() Add wp.tile_arange() Add detailed docstrings for most tile methods --- warp/builtins.py | 292 +++++++++++++++++++++++++++------ warp/native/tile.h | 39 ++++- warp/tests/test_tile_reduce.py | 60 ++++++- warp/types.py | 11 ++ 4 files changed, 347 insertions(+), 55 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index cbad03b5..e400c364 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1713,9 +1713,6 @@ def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) - # if len(arg_types) > 0: - # raise RuntimeError("tile_zero() args must be passed by keyword") - if "m" not in arg_values: raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") @@ -1748,12 +1745,138 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar value_func=tile_zeros_value_func, dispatch_func=tile_zeros_dispatch_func, variadic=True, - doc="Allocate a tile of zero initialized items", + doc="""Allocates a tile of zero initialized items. + + :param m: Size of the first dimension of the output tile + :param n: Size of the second dimension of the output tile + :param dtype: Datatype of output tile's elements + :returns: A zero initialized tile with ``shape=(m,n)`` and the specified datatype""", + group="Tile Primitives", + export=False, +) + +def tile_ones_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any]): + # return generic type (for doc builds) + if arg_types is None: + return Tile(dtype=Any, M=Any, N=Any) + + if "m" not in arg_values: + raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") + + if "n" not in arg_values: + raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") + + if "dtype" not in arg_values: + raise RuntimeError("'dtype' keyword argument must be specified when calling tile_zeros() function") + + m, n = arg_values["m"], arg_values["n"] + dtype = arg_values["dtype"] + + return TileZeros(dtype=dtype, M=m, N=n) + + +def tile_ones_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): + m, n, dtype = arg_values["m"], arg_values["n"], arg_values["dtype"] + + template_args = [] + template_args.append(dtype) + template_args.append(m.constant) + template_args.append(n.constant) + + return ([], template_args) + + +add_builtin( + "tile_ones", + input_types={"m": int, "n": int, "dtype": Scalar}, + value_func=tile_ones_value_func, + dispatch_func=tile_ones_dispatch_func, + variadic=True, + doc="""Allocates a tile of one initialized items. + + :param m: Size of the first dimension of the output tile + :param n: Size of the second dimension of the output tile + :param dtype: Datatype of output tile's elements + :returns: A one initialized tile with ``shape=(m,n)`` and the specified dtype""", + group="Tile Primitives", + export=False, +) + +def tile_arange_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any]): + # return generic type (for doc builds) + if arg_types is None: + return Tile(dtype=Any, M=Any, N=Any) + + start = 0 + stop = 0 + step = 1 + dtype = int + + args = arg_values["args"] + + if len(args) == 1: + start = 0 + stop = args[0] + + elif len(args) == 2: + start = args[0] + stop = args[1] + + elif len(args) == 3: + start = args[0] + stop = args[1] + step = args[2] + + if start == None or stop == None or step == None: + raise RuntimeError("wp.tile_arange() arguments must be compile time constants") + + if arg_values["dtype"] is not None: + dtype = arg_values["dtype"] + + return TileRange(dtype=dtype, start=start, stop=stop, step=step) + + +def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): + m, n, dtype = return_type.M, return_type.N, return_type.dtype + + template_args = [] + template_args.append(dtype) + template_args.append(m) + template_args.append(n) + + # take dtype from stop value + t = return_type.dtype + + start = warp.codegen.Var(label=None, type=t, constant=return_type.start) + stop = warp.codegen.Var(label=None, type=t, constant=return_type.stop) + step = warp.codegen.Var(label=None, type=t, constant=return_type.step) + + return ([start, stop, step], template_args) + + +add_builtin( + "tile_arange", + input_types={"*args": Scalar, "dtype": Scalar}, + defaults={"dtype": None}, + value_func=tile_arange_value_func, + dispatch_func=tile_arange_dispatch_func, + variadic=True, + doc="""Generates a tile of linearly spaced elements. + + :param args: Variable length positional arguments, interpreted as: + + - ``(stop,)``: Generates values from ``0`` to ``stop - 1`` + - ``(start, stop)``: Generates values from ``start`` to ``stop - 1`` + - ``(start, stop, step)``: Generates values from ``start`` to ``stop - 1`` with a step size + + :param dtype: Datatype of output tile's elements (optional, default: int) + :returns: A tile with ``shape=(1,n)`` with linearly spaced elements of specified dtype""", group="Tile Primitives", export=False, ) + def tile_load_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -1803,7 +1926,16 @@ def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg value_func=tile_load_value_func, dispatch_func=tile_load_dispatch_func, variadic=True, - doc="Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)", + doc="""Loads a tile from a global memory array. + + This method will cooperatively load a tile from global memory using all threads in the block. + + :param a: The source array in global memory + :param x: Offset in the source array measured in multiples of ``m``, i.e.: ``i=x*m`` + :param y: Offset in the source array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param m: The size of the tile's first dimension + :param n: The size of the tile's second dimensions + :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array""", group="Tile Primitives", export=False, ) @@ -1829,6 +1961,9 @@ def tile_store_value_func(arg_types, arg_values): if not is_tile(arg_types["t"]): raise RuntimeError("tile_store() argument 3 must be a tile") + if not types_equal(arg_types["a"].dtype, arg_types["t"].dtype): + raise RuntimeError("tile_store() destination array must have same type as source tile") + return None @@ -1837,7 +1972,14 @@ def tile_store_value_func(arg_types, arg_values): input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, value_func=tile_store_value_func, variadic=True, - doc="Store tile `t` to an array `a` at offset `(i=x*m, j=y*n)`", + doc="""Stores a tile to a global memory array. + + This method will cooperatively store a tile to global memory using all threads in the block. + + :param a: The destination array in global memory + :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*m`` + :param y: Offset in the destination array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param t: The source tile to store data from, must have the same dtype as the destination array""", group="Tile Primitives", export=False, ) @@ -1874,7 +2016,13 @@ def tile_atomic_add_value_func(arg_types, arg_values): input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, value_func=tile_atomic_add_value_func, variadic=True, - doc="Atomically add a tile `t` worth of data to array `a` at offset `(i=x*m, j=y*n)`", + doc="""Atomically add a tile to the array `a`, each element will be updated atomically. + + :param a: Array in global memory, should have the same ``dtype`` as the input tile + :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*M`` where ``M`` is the first tile dimension + :param y: Offset in the destination array measured in multiples of ``n``, i.e.: ``j=y*N`` where ``N`` is the second tile dimension + :param t: Source tile to add to the desination array + :returns: A tile with the same dimensions and type as the source tile, holding the original value of the destination elements""", group="Tile Primitives", export=False, ) @@ -1900,24 +2048,30 @@ def tile_value_func(arg_types, arg_values): input_types={"x": Any}, value_func=tile_value_func, variadic=True, - doc="""Construct a Tile from a per-thread kernel value. + doc="""Constructs a new Tile from a per-thread kernel values. - Args: - x (Any): A per-thread local value, e.g.: scalar, vector, or matrix. + This function converts values computed using scalar kernel code to a tile representation for input into collective operations. - Returns: - Tile: A tile with dimensions of ``(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch().`` + :param x: A per-thread local value, e.g.: scalar, vector, or matrix. + :returns: A tile with ``shape=(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch()``. - Examples: - This example shows how to create a linear sequence from thread variables: + This example shows how to create a linear sequence from thread variables: - .. code-block:: python - - # get thread id + .. code-block:: python + + @wp.kernel + def compute(): i = wp.tid() - - # convert to block wide tile t = wp.tile(i*2) + print(t) + + wp.launch(compute, dim=16, inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + tile(m=1, n=16, storage=register) = [[0 2 4 6 8 10 12 14...]] """, group="Tile Primitives""", @@ -1944,7 +2098,14 @@ def tile_extract_value_func(arg_types, arg_values): input_types={"a": Tile(dtype=Any, M=Any, N=Any), "i": int, "j": int}, value_func=tile_extract_value_func, variadic=True, - doc="Extract element at index (i, j) of the tile and return the native type", + doc="""Extracts a single element from the tile and returns it as a scalar type. + + This function will extract an element from the tile and broadcast its value to all threads in the block, note that this may incur additional synchronization if the source tile is a register tile. + + :param a: Tile to extract the element from + :param i: Coordinate of element on first dimension + :param j: Coordinate of element on the second dimension + :returns: The value of the element at the specified tile location, with the same type as the input tile's per-element dtype""", group="Tile Primitives", export=False, ) @@ -1988,13 +2149,14 @@ def tile_matmul_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a add_builtin( - "tile_matmul", + "tile_matmul_scalar", input_types={"a": Tile, "b": Tile, "out": Tile}, value_func=tile_matmul_value_func, dispatch_func=tile_matmul_dispatch_func, variadic=True, doc="Compute matrix product and accumulate out += a*b.", group="Tile Primitives", + hidden=True, export=False, ) @@ -2020,7 +2182,32 @@ def tile_sum_value_func(arg_types, arg_values): input_types={"a": Tile}, value_func=tile_sum_value_func, variadic=True, - doc="Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored", + doc="""Cooperatively compute the sum the tile elements using all threads in the block. + + :param a: The tile to compute the sum of + :returns: A single element tile with dimensions of (1,1) holding the sum + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_ones(dtype=float, m=16, n=16) + s = wp.tile_sum(t) + + print(t) + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[256]] + + """, group="Tile Primitives", export=False, ) @@ -2053,7 +2240,34 @@ def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, ar # dispatch_func=tile_map_dispatch_func, # variadic=True, native_func="tile_unary_map", - doc="Unary map the operation onto each element of the tile.", + doc="""Apply a unary function onto the tile. + + This function cooperatively applies a unary function to each element of the tile using all threads in the block. + + :param op: A callable function that accepts one argument and returns one argument, may be a user function or builtin + :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A tile with the same dimensions as the input tile, currently output tiles must have the same dtype as the input. + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_arange(0.0, 1.0, 0.1, dtype=float) + s = wp.tile_map(wp.sin, t) + + print(s) + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=10, storage=register) = [[0 0.0998334 0.198669 0.29552 ...]] + """, group="Tile Primitives", export=False, ) @@ -3871,9 +4085,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) @@ -3882,9 +4094,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) @@ -3893,9 +4103,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) @@ -3904,9 +4112,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) @@ -3916,9 +4122,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) @@ -3927,9 +4131,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) @@ -3938,9 +4140,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) @@ -3949,9 +4149,7 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + doc="""Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""", group="Utility", skip_replay=True, ) diff --git a/warp/native/tile.h b/warp/native/tile.h index 7563e0d9..4f562e15 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -471,7 +471,7 @@ struct tile_shared_t if (threadIdx.x == 0) { - printf("Tile(M=%d, N=%d, storage=shared) = [\n", M, N); + printf("tile(m=%d, n=%d, storage=shared) = [", M, N); for (int i=0; i < M; ++i) { printf("%*s[", i>0, ""); @@ -570,7 +570,7 @@ void tile_register_t::print() if (threadIdx.x == 0) { - printf("Tile(M=%d, N=%d, storage=register) = [\n", M, N); + printf("tile(m=%d, n=%d, storage=register) = [", M, N); for (int i=0; i < M; ++i) { printf("%*s[", i>0, ""); @@ -666,9 +666,42 @@ template inline CUDA_CALLABLE auto tile_zeros() { // tile variable assignment operator will handle initialization (since lhs could be shared/register tile) - return T(0.0); + return T(0); } +// zero initialized tile +template +inline CUDA_CALLABLE auto tile_ones() +{ + // tile variable assignment operator will handle initialization (since lhs could be shared/register tile) + return T(1); +} + +// zero initialized tile +template +inline CUDA_CALLABLE auto tile_arange(T start, T stop, T step) +{ + tile_register_t out; + + WP_PRAGMA_UNROLL + for (int i=0; i < out.NumRegs; ++i) + { + const int linear = out.index(i); + + // handle case where tile size is not + // aligned to block dimensions + if (!out.Aligned && linear >= out.Size) + break; + + out.data[i] = start + linear*step; + } + + return out; +} + +template +inline CUDA_CALLABLE void adj_tile_arange(int start, int stop, int step, + int adj_start, int adj_stop, int adj_step, const tile_register_t& adj_ret) {} // entry point for load template diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index 81491878..00b8b301 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -71,23 +71,71 @@ def tile_reduce_1d_kernel(output: wp.array(dtype=int)): t = wp.tile(i) # convert to block wide tile s = wp.tile_sum(t) # sum over block - + # update global sum - wp.tile_atomic_add(output, i, 0, s) + wp.tile_atomic_add(output, 0, 0, s) -@unittest.expectedFailure def test_tile_reduce_1d(test, device): + + # use an unaligned grid dimension N = int(TILE_DIM * 3 / 2) output = wp.zeros(shape=1, dtype=int, requires_grad=True, device=device) with wp.Tape() as tape: wp.launch(tile_reduce_1d_kernel, dim=[N], inputs=[output], block_dim=TILE_DIM, device=device) - + test.assertAlmostEqual(output.numpy()[0], np.sum(np.arange(N))) + +@wp.kernel +def tile_ones_kernel(out: wp.array(dtype=float)): + i = wp.tid() + + t = wp.tile_ones(dtype=float, m=16, n=16) + s = wp.tile_sum(t) + + wp.tile_store(out, 0, 0, s) + +def test_tile_ones(test, device): + + output = wp.zeros(shape=1, dtype=float, device=device) + + with wp.Tape() as tape: + wp.launch(tile_ones_kernel, dim=[1, TILE_DIM], inputs=[output], block_dim=TILE_DIM, device=device) + wp.synchronize() + + test.assertAlmostEqual(output.numpy()[0], 256.0) + + +@wp.kernel +def tile_arange_kernel(out: wp.array2d(dtype=int)): + i = wp.tid() + + a = wp.tile_arange(17, dtype=int) + b = wp.tile_arange(5, 23, dtype=int) + c = wp.tile_arange(0, 34, 2, dtype=int) + + wp.tile_store(out, 0, 0, a) + wp.tile_store(out, 1, 0, b) + wp.tile_store(out, 2, 0, c) + +def test_tile_arange(test, device): + + N = 17 + + output = wp.zeros(shape=(3, N), dtype=int, device=device) + + with wp.Tape() as tape: + wp.launch(tile_arange_kernel, dim=[1, N], inputs=[output], block_dim=TILE_DIM, device=device) + + assert_np_equal(output.numpy()[0], np.arange(17)) + assert_np_equal(output.numpy()[1], np.arange(5, 22)) + assert_np_equal(output.numpy()[2], np.arange(0, 34, 2)) + + devices = get_cuda_test_devices() @@ -96,7 +144,9 @@ class TestTileReduce(unittest.TestCase): add_function_test(TestTileReduce, "test_tile_reduce_sum", test_tile_reduce_sum, devices=devices) -add_function_test(TestTileReduce, "test_tile_reduce_1d", test_tile_reduce_1d, devices=devices) # FAILS +add_function_test(TestTileReduce, "test_tile_reduce_1d", test_tile_reduce_1d, devices=devices) +add_function_test(TestTileReduce, "test_tile_ones", test_tile_ones, devices=devices) +add_function_test(TestTileReduce, "test_tile_arange", test_tile_arange, devices=devices) if __name__ == "__main__": wp.clear_kernel_cache() diff --git a/warp/types.py b/warp/types.py index 9dbab2a4..e099119d 100644 --- a/warp/types.py +++ b/warp/types.py @@ -3001,6 +3001,17 @@ class TileZeros(Tile): def __init__(self, dtype, M, N): Tile.__init__(self, dtype, M, N, op="zeros", storage="shared") +class TileRange(Tile): + def __init__(self, dtype, start, stop, step): + + self.start = start + self.stop = stop + self.step = step + + M = 1 + N = int((stop-start)/step) + + Tile.__init__(self, dtype, M, N, op="arange", storage="register") class TileConstant(Tile): def __init__(self, dtype, M, N): From 83b1ed3d48925e311d2be39baf2ab039657659ce Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Thu, 26 Sep 2024 13:48:06 -0700 Subject: [PATCH 036/102] Fix various issues with tile branch tests --- .gitlab-ci.yml | 3 +- .gitlab/ci/additional-tests.yml | 2 +- .gitlab/ci/cuda-11-build-and-test.yml | 2 +- .gitlab/ci/debug-build-and-test.yml | 2 +- .gitlab/ci/mathdx-support.yml | 3 +- docs/modules/functions.rst | 215 ++++++++++++------ warp/autograd.py | 51 ++++- warp/builtins.py | 56 ++--- warp/context.py | 4 +- warp/jax_experimental.py | 4 +- warp/native/builtin.h | 5 +- warp/native/tile.h | 3 +- warp/native/warp.cu | 11 +- warp/stubs.py | 303 ++++++++++++++++---------- warp/tests/test_tile.py | 1 - warp/tests/test_tile_reduce.py | 19 +- warp/types.py | 5 +- 17 files changed, 443 insertions(+), 246 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5eb130f6..ea8ae21c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -207,6 +207,7 @@ linux-aarch64 test jetson: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - !reference [.snippets, install-python+warp-aarch64] - python -m pip install coverage[toml] + - python -m pip install -U "jax[cuda12]" - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - python -m warp.tests --junit-report-xml rspec.xml --coverage --coverage-xml coverage.xml -s autodetect --failfast @@ -231,7 +232,7 @@ linux-x86_64 test: - python -m pip install --upgrade pip - python -m pip install --upgrade usd-core coverage[toml] - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - - python -m pip install --upgrade "jax[cuda12_pip]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html + - python -m pip install -U "jax[cuda12]" - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" # HACK: disable P2P tests due to misbehaving agents diff --git a/.gitlab/ci/additional-tests.yml b/.gitlab/ci/additional-tests.yml index 10c19889..aba4a45d 100644 --- a/.gitlab/ci/additional-tests.yml +++ b/.gitlab/ci/additional-tests.yml @@ -43,7 +43,7 @@ linux-x86_64 test: - python -m pip install --upgrade pip - python -m pip install --upgrade usd-core - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - - python -m pip install --upgrade "jax[cuda12_pip]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html + - python -m pip install -U "jax[cuda12]" - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" # HACK: disable P2P tests due to misbehaving agents diff --git a/.gitlab/ci/cuda-11-build-and-test.yml b/.gitlab/ci/cuda-11-build-and-test.yml index 735104ea..7282d9e8 100644 --- a/.gitlab/ci/cuda-11-build-and-test.yml +++ b/.gitlab/ci/cuda-11-build-and-test.yml @@ -122,7 +122,7 @@ linux-x86_64 test: - python -m pip install --upgrade pip - python -m pip install --upgrade usd-core - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - - python -m pip install --upgrade "jax[cuda12_pip]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html + - python -m pip install -U "jax[cuda12]" - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" # HACK: disable P2P tests due to misbehaving agents diff --git a/.gitlab/ci/debug-build-and-test.yml b/.gitlab/ci/debug-build-and-test.yml index 3ebeeade..e041739a 100644 --- a/.gitlab/ci/debug-build-and-test.yml +++ b/.gitlab/ci/debug-build-and-test.yml @@ -114,7 +114,7 @@ linux-x86_64 test: - python -m pip install --upgrade pip - python -m pip install --upgrade usd-core - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - - python -m pip install --upgrade "jax[cuda12_pip]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html + - python -m pip install -U "jax[cuda12]" - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" # HACK: disable P2P tests due to misbehaving agents diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index 5bea3383..b6fff5b3 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -100,7 +100,7 @@ linux-x86_64 test: - python -m pip install --upgrade pip - python -m pip install --upgrade usd-core - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - - python -m pip install --upgrade "jax[cuda12_pip]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html + - python -m pip install -U "jax[cuda12]" - python -m pip install --upgrade nvidia-mathdx==24.4.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" @@ -117,6 +117,7 @@ linux-aarch64 test jetson: before_script: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - !reference [.snippets, install-python+warp-aarch64] + - python -m pip install -U "jax[cuda12]" - python -m pip install --upgrade nvidia-mathdx==24.4.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index bcd18cc9..5d2bc605 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -804,67 +804,196 @@ Tile Primitives --------------- .. py:function:: tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile - Allocate a tile local block of zero'd memory + Allocates a tile of zero initialized items. + + :param m: Size of the first dimension of the output tile + :param n: Size of the second dimension of the output tile + :param dtype: Datatype of output tile's elements + :returns: A zero initialized tile with ``shape=(m,n)`` and the specified datatype + + +.. py:function:: tile_ones(m: int32, n: int32, dtype: Scalar) -> Tile + + Allocates a tile of one initialized items. + + :param m: Size of the first dimension of the output tile + :param n: Size of the second dimension of the output tile + :param dtype: Datatype of output tile's elements + :returns: A one initialized tile with ``shape=(m,n)`` and the specified dtype + + +.. py:function:: tile_arange(*args: Scalar, dtype: Scalar) -> Tile + + Generates a tile of linearly spaced elements. + + :param args: Variable length positional arguments, interpreted as: + + - ``(stop,)``: Generates values from ``0`` to ``stop - 1`` + - ``(start, stop)``: Generates values from ``start`` to ``stop - 1`` + - ``(start, stop, step)``: Generates values from ``start`` to ``stop - 1`` with a step size + + :param dtype: Datatype of output tile's elements (optional, default: int) + :returns: A tile with ``shape=(1,n)`` with linearly spaced elements of specified dtype .. py:function:: tile_load(a: Array[Any], x: int32, y: int32, m: int32, n: int32) -> Tile - Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n) + Loads a tile from a global memory array. + + This method will cooperatively load a tile from global memory using all threads in the block. + + :param a: The source array in global memory + :param x: Offset in the source array measured in multiples of ``m``, i.e.: ``i=x*m`` + :param y: Offset in the source array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param m: The size of the tile's first dimension + :param n: The size of the tile's second dimensions + :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array .. py:function:: tile_store(a: Array[Any], x: int32, y: int32, t: Any) -> None - Store tile `t` to an array `a` at offset `(i=x*m, j=y*n)` + Stores a tile to a global memory array. + + This method will cooperatively store a tile to global memory using all threads in the block. + + :param a: The destination array in global memory + :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*m`` + :param y: Offset in the destination array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param t: The source tile to store data from, must have the same dtype as the destination array .. py:function:: tile_atomic_add(a: Array[Any], x: int32, y: int32, t: Any) -> Tile - Atomically add a tile `t` worth of data to array `a` at offset `(i=x*m, j=y*n)` + Atomically add a tile to the array `a`, each element will be updated atomically. + + :param a: Array in global memory, should have the same ``dtype`` as the input tile + :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*M`` where ``M`` is the first tile dimension + :param y: Offset in the destination array measured in multiples of ``n``, i.e.: ``j=y*N`` where ``N`` is the second tile dimension + :param t: Source tile to add to the destination array + :returns: A tile with the same dimensions and type as the source tile, holding the original value of the destination elements .. py:function:: tile(x: Any) -> Tile - Construct a Tile from a per-thread kernel value, returns a tile with dimensions of `(1, block_dim)` where block_dim is the number of threads specified in `wp.launch()` + Constructs a new Tile from a per-thread kernel values. + This function converts values computed using scalar kernel code to a tile representation for input into collective operations. -.. py:function:: tile_extract(a: Tile, i: int32, j: int32) -> None + :param x: A per-thread local value, e.g.: scalar, vector, or matrix. + :returns: A tile with ``shape=(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch()``. - Extract element at index (i, j) of the tile and return the native type + This example shows how to create a linear sequence from thread variables: + .. code-block:: python -.. py:function:: tile_matmul(a: Tile, b: Tile, out: Tile) -> None + @wp.kernel + def compute(): + i = wp.tid() + t = wp.tile(i*2) + print(t) - Compute matrix product and accumulate out += a*b. + wp.launch(compute, dim=16, inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + tile(m=1, n=16, storage=register) = [[0 2 4 6 8 10 12 14...]] + + + +.. py:function:: tile_extract(a: Tile, i: int32, j: int32) -> Scalar + + Extracts a single element from the tile and returns it as a scalar type. + + This function will extract an element from the tile and broadcast its value to all threads in the block, note that this may incur additional synchronization if the source tile is a register tile. + + :param a: Tile to extract the element from + :param i: Coordinate of element on first dimension + :param j: Coordinate of element on the second dimension + :returns: The value of the element at the specified tile location, with the same type as the input tile's per-element dtype + + +.. py:function:: tile_sum(a: Tile) -> Tile + + Cooperatively compute the sum the tile elements using all threads in the block. + + :param a: The tile to compute the sum of + :returns: A single element tile with dimensions of (1,1) holding the sum + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_ones(dtype=float, m=16, n=16) + s = wp.tile_sum(t) + + print(t) + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + .. code-block:: text -.. py:function:: tile_sum(a: Tile) -> None + tile(m=1, n=1, storage=register) = [[256]] - Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored + + + +.. py:function:: tile_map(op: Callable, a: Any) -> Tile + + Apply a unary function onto the tile. + + This function cooperatively applies a unary function to each element of the tile using all threads in the block. + + :param op: A callable function that accepts one argument and returns one argument, may be a user function or builtin + :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A tile with the same dimensions as the input tile, currently output tiles must have the same dtype as the input. + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_arange(0.0, 1.0, 0.1, dtype=float) + s = wp.tile_map(wp.sin, t) + print(s) -.. py:function:: tile_map(op: Callable, a: Any) -> None + wp.launch(compute, dim=[64], inputs=[]) - Map the operation onto each element of the tile + Prints: + .. code-block:: text -.. py:function:: tile_map(op: Callable, a: Any, b: Any) -> None + tile(m=1, n=10, storage=register) = [[0 0.0998334 0.198669 0.29552 ...]] + + + +.. py:function:: tile_map(op: Callable, a: Any, b: Any) -> Tile :noindex: :nocontentsentry: - Map the operation onto each element of the tile + Apply the binary map operation onto each corresponding pair of elements from each the tile. -.. py:function:: tile_matmul_dx(a: Tile, b: Tile, out: Tile) -> None +.. py:function:: tile_matmul_dx(a: Tile, b: Tile, out: Tile) -> Tile Compute matrix product and accumulate out += a*b. -.. py:function:: tile_fft_dx(inout: Tile) -> None +.. py:function:: tile_fft_dx(inout: Tile) -> Tile Compute the FFT along the second dimension of a 2D tile of data. -.. py:function:: tile_ifft_dx(inout: Tile) -> None +.. py:function:: tile_ifft_dx(inout: Tile) -> Tile Compute the inverse FFT along the second dimension of a 2D tile of data. @@ -1183,8 +1312,6 @@ Utility Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1192,8 +1319,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1201,8 +1326,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1210,8 +1333,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1219,8 +1340,6 @@ Utility Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1228,8 +1347,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1237,8 +1354,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1246,8 +1361,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1255,8 +1368,6 @@ Utility Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1264,8 +1375,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1273,8 +1382,6 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1282,15 +1389,11 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: Array[Any], i: int32, value: Any) -> Any Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1298,8 +1401,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1307,8 +1408,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1316,8 +1415,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1325,8 +1422,6 @@ Utility Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1334,8 +1429,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1343,8 +1436,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1352,8 +1443,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1361,8 +1450,6 @@ Utility Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1370,8 +1457,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1379,8 +1464,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1388,8 +1471,6 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - .. py:function:: lerp(a: Float, b: Float, t: Float) -> Float @@ -2032,7 +2113,7 @@ Operators :nocontentsentry: -.. py:function:: add(a: Tile, b: Tile) -> None +.. py:function:: add(a: Tile, b: Tile) -> Tile :noindex: :nocontentsentry: diff --git a/warp/autograd.py b/warp/autograd.py index 8f884f04..9b2eea47 100644 --- a/warp/autograd.py +++ b/warp/autograd.py @@ -34,6 +34,7 @@ def gradcheck( input_output_mask: List[Tuple[Union[str, int], Union[str, int]]] = None, device: wp.context.Devicelike = None, max_blocks=0, + block_dim=256, max_inputs_per_var=-1, max_outputs_per_var=-1, plot_relative_error=False, @@ -44,7 +45,8 @@ def gradcheck( Checks whether the autodiff gradient of a Warp kernel matches finite differences. Fails if the relative or absolute errors between the autodiff and finite difference gradients exceed the specified tolerance, or if the autodiff gradients contain NaN values. - The kernel function and its adjoint version are launched with the given inputs and outputs, as well as the provided ``dim`` and ``max_blocks`` arguments (see :func:`warp.launch` for more details). + The kernel function and its adjoint version are launched with the given inputs and outputs, as well as the provided + ``dim``, ``max_blocks``, and ``block_dim`` arguments (see :func:`warp.launch` for more details). Note: This function only supports Warp kernels whose input arguments precede the output arguments. @@ -65,6 +67,7 @@ def gradcheck( input_output_mask: List of tuples specifying the input-output pairs to compute the Jacobian for. Inputs and outputs can be identified either by their integer indices of where they appear in the kernel input/output arguments, or by the respective argument names as strings. If None, computes the Jacobian for all input-output pairs. device: The device to launch on (optional) max_blocks: The maximum number of CUDA thread blocks to use. + block_dim: The number of threads per block. max_inputs_per_var: Maximum number of input dimensions over which to evaluate the Jacobians for the input-output pairs. Evaluates all input dimensions if value <= 0. max_outputs_per_var: Maximum number of output dimensions over which to evaluate the Jacobians for the input-output pairs. Evaluates all output dimensions if value <= 0. plot_relative_error: If True, visualizes the relative error of the Jacobians in a plot (requires ``matplotlib``). @@ -85,6 +88,7 @@ def gradcheck( input_output_mask=input_output_mask, device=device, max_blocks=max_blocks, + block_dim=block_dim, max_inputs_per_var=max_inputs_per_var, eps=eps, plot_jacobians=False, @@ -98,6 +102,7 @@ def gradcheck( input_output_mask=input_output_mask, device=device, max_blocks=max_blocks, + block_dim=block_dim, max_outputs_per_var=max_outputs_per_var, plot_jacobians=False, ) @@ -237,7 +242,6 @@ def gradcheck_tape( input_output_masks: Dictionary of input-output masks for each kernel in the tape, mapping from kernel keys to input-output masks. Inputs and outputs can be identified either by their integer indices of where they appear in the kernel input/output arguments, or by the respective argument names as strings. If None, computes the Jacobian for all input-output pairs. blacklist_kernels: List of kernel keys to exclude from the gradient check. whitelist_kernels: List of kernel keys to include in the gradient check. If not empty or None, only kernels in this list are checked. - max_blocks: The maximum number of CUDA thread blocks to use. max_inputs_per_var: Maximum number of input dimensions over which to evaluate the Jacobians for the input-output pairs. Evaluates all input dimensions if value <= 0. max_outputs_per_var: Maximum number of output dimensions over which to evaluate the Jacobians for the input-output pairs. Evaluates all output dimensions if value <= 0. plot_relative_error: If True, visualizes the relative error of the Jacobians in a plot (requires ``matplotlib``). @@ -262,7 +266,7 @@ def gradcheck_tape( for launch in tape.launches: if not isinstance(launch[0], wp.Kernel): continue - kernel, dim, max_blocks, inputs, outputs, device = launch[:6] + kernel, dim, max_blocks, inputs, outputs, device, block_dim = launch[:7] if len(whitelist_kernels) > 0 and kernel.key not in whitelist_kernels: continue if kernel.key in blacklist_kernels: @@ -280,6 +284,7 @@ def gradcheck_tape( input_output_mask=input_output_mask, device=device, max_blocks=max_blocks, + block_dim=block_dim, max_inputs_per_var=max_inputs_per_var, max_outputs_per_var=max_outputs_per_var, plot_relative_error=plot_relative_error, @@ -611,13 +616,15 @@ def jacobian( input_output_mask: List[Tuple[Union[str, int], Union[str, int]]] = None, device: wp.context.Devicelike = None, max_blocks=0, + block_dim=256, max_outputs_per_var=-1, plot_jacobians=False, ) -> Dict[Tuple[int, int], wp.array]: """ Computes the Jacobians of a Warp kernel launch for the provided selection of differentiable inputs to differentiable outputs. - The kernel adjoint function is launched with the given inputs and outputs, as well as the provided ``dim`` and ``max_blocks`` arguments (see :func:`warp.launch` for more details). + The kernel adjoint function is launched with the given inputs and outputs, as well as the provided ``dim``, + ``max_blocks``, and ``block_dim`` arguments (see :func:`warp.launch` for more details). Note: This function only supports Warp kernels whose input arguments precede the output arguments. @@ -634,6 +641,7 @@ def jacobian( input_output_mask: List of tuples specifying the input-output pairs to compute the Jacobian for. Inputs and outputs can be identified either by their integer indices of where they appear in the kernel input/output arguments, or by the respective argument names as strings. If None, computes the Jacobian for all input-output pairs. device: The device to launch on (optional) max_blocks: The maximum number of CUDA thread blocks to use. + block_dim: The number of threads per block. max_outputs_per_var: Maximum number of output dimensions over which to evaluate the Jacobians for the input-output pairs. Evaluates all output dimensions if value <= 0. plot_jacobians: If True, visualizes the computed Jacobians in a plot (requires ``matplotlib``). @@ -661,7 +669,15 @@ def resolve_arg(name, offset: int = 0): device = infer_device(inputs + outputs) tape = wp.Tape() - tape.record_launch(kernel=kernel, dim=dim, max_blocks=max_blocks, inputs=inputs, outputs=outputs, device=device) + tape.record_launch( + kernel=kernel, + dim=dim, + inputs=inputs, + outputs=outputs, + device=device, + max_blocks=max_blocks, + block_dim=block_dim, + ) jacobians = {} @@ -709,6 +725,7 @@ def jacobian_fd( input_output_mask: List[Tuple[Union[str, int], Union[str, int]]] = None, device: wp.context.Devicelike = None, max_blocks=0, + block_dim=256, max_inputs_per_var=-1, eps=1e-4, plot_jacobians=False, @@ -717,7 +734,8 @@ def jacobian_fd( Computes the finite-difference Jacobian of a Warp kernel launch for the provided selection of differentiable inputs to differentiable outputs. The method uses a central difference scheme to approximate the Jacobian. - The kernel is launched multiple times in forward-only mode with the given inputs and outputs, as well as the provided ``dim`` and ``max_blocks`` arguments (see :func:`warp.launch` for more details). + The kernel is launched multiple times in forward-only mode with the given inputs and outputs, as well as the + provided ``dim``, ``max_blocks``, and ``block_dim`` arguments (see :func:`warp.launch` for more details). Note: This function only supports Warp kernels whose input arguments precede the output arguments. @@ -734,6 +752,7 @@ def jacobian_fd( input_output_mask: List of tuples specifying the input-output pairs to compute the Jacobian for. Inputs and outputs can be identified either by their integer indices of where they appear in the kernel input/output arguments, or by the respective argument names as strings. If None, computes the Jacobian for all input-output pairs. device: The device to launch on (optional) max_blocks: The maximum number of CUDA thread blocks to use. + block_dim: The number of threads per block. max_inputs_per_var: Maximum number of input dimensions over which to evaluate the Jacobians for the input-output pairs. Evaluates all input dimensions if value <= 0. eps: The finite-difference step size. plot_jacobians: If True, visualizes the computed Jacobians in a plot (requires ``matplotlib``). @@ -793,10 +812,26 @@ def resolve_arg(name, offset: int = 0): input_num = min(input_num, max_inputs_per_var) for i in range(input_num): set_element(flat_input, i, -eps, relative=True) - wp.launch(kernel, dim=dim, max_blocks=max_blocks, inputs=inputs, outputs=left_outputs, device=device) + wp.launch( + kernel, + dim=dim, + inputs=inputs, + outputs=left_outputs, + device=device, + max_blocks=max_blocks, + block_dim=block_dim, + ) set_element(flat_input, i, 2 * eps, relative=True) - wp.launch(kernel, dim=dim, max_blocks=max_blocks, inputs=inputs, outputs=right_outputs, device=device) + wp.launch( + kernel, + dim=dim, + inputs=inputs, + outputs=right_outputs, + device=device, + max_blocks=max_blocks, + block_dim=block_dim, + ) set_element(flat_input, i, -eps, relative=True) diff --git a/warp/builtins.py b/warp/builtins.py index e400c364..da5463ec 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1746,7 +1746,7 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar dispatch_func=tile_zeros_dispatch_func, variadic=True, doc="""Allocates a tile of zero initialized items. - + :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements @@ -1755,6 +1755,7 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar export=False, ) + def tile_ones_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any]): # return generic type (for doc builds) if arg_types is None: @@ -1793,7 +1794,7 @@ def tile_ones_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg dispatch_func=tile_ones_dispatch_func, variadic=True, doc="""Allocates a tile of one initialized items. - + :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements @@ -1802,6 +1803,7 @@ def tile_ones_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg export=False, ) + def tile_arange_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any]): # return generic type (for doc builds) if arg_types is None: @@ -1827,7 +1829,7 @@ def tile_arange_value_func(arg_types: Mapping[str, type], arg_values: Mapping[st stop = args[1] step = args[2] - if start == None or stop == None or step == None: + if start is None or stop is None or step is None: raise RuntimeError("wp.tile_arange() arguments must be compile time constants") if arg_values["dtype"] is not None: @@ -1862,7 +1864,7 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a dispatch_func=tile_arange_dispatch_func, variadic=True, doc="""Generates a tile of linearly spaced elements. - + :param args: Variable length positional arguments, interpreted as: - ``(stop,)``: Generates values from ``0`` to ``stop - 1`` @@ -1876,7 +1878,6 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a ) - def tile_load_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -1927,7 +1928,7 @@ def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg dispatch_func=tile_load_dispatch_func, variadic=True, doc="""Loads a tile from a global memory array. - + This method will cooperatively load a tile from global memory using all threads in the block. :param a: The source array in global memory @@ -1973,7 +1974,7 @@ def tile_store_value_func(arg_types, arg_values): value_func=tile_store_value_func, variadic=True, doc="""Stores a tile to a global memory array. - + This method will cooperatively store a tile to global memory using all threads in the block. :param a: The destination array in global memory @@ -2017,11 +2018,11 @@ def tile_atomic_add_value_func(arg_types, arg_values): value_func=tile_atomic_add_value_func, variadic=True, doc="""Atomically add a tile to the array `a`, each element will be updated atomically. - + :param a: Array in global memory, should have the same ``dtype`` as the input tile :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*M`` where ``M`` is the first tile dimension :param y: Offset in the destination array measured in multiples of ``n``, i.e.: ``j=y*N`` where ``N`` is the second tile dimension - :param t: Source tile to add to the desination array + :param t: Source tile to add to the destination array :returns: A tile with the same dimensions and type as the source tile, holding the original value of the destination elements""", group="Tile Primitives", export=False, @@ -2049,7 +2050,7 @@ def tile_value_func(arg_types, arg_values): value_func=tile_value_func, variadic=True, doc="""Constructs a new Tile from a per-thread kernel values. - + This function converts values computed using scalar kernel code to a tile representation for input into collective operations. :param x: A per-thread local value, e.g.: scalar, vector, or matrix. @@ -2068,13 +2069,12 @@ def compute(): wp.launch(compute, dim=16, inputs=[], block_dim=16) Prints: - + .. code-block:: text tile(m=1, n=16, storage=register) = [[0 2 4 6 8 10 12 14...]] """, - - group="Tile Primitives""", + group="Tile Primitives" "", export=False, ) @@ -2082,9 +2082,9 @@ def compute(): def tile_extract_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: - return Scalar - - if len(arg_types) != 3: + return Scalar + + if len(arg_types) != 3: raise RuntimeError("tile_extract() requires 3 positional args") if not is_tile(arg_types["a"]): @@ -2099,7 +2099,7 @@ def tile_extract_value_func(arg_types, arg_values): value_func=tile_extract_value_func, variadic=True, doc="""Extracts a single element from the tile and returns it as a scalar type. - + This function will extract an element from the tile and broadcast its value to all threads in the block, note that this may incur additional synchronization if the source tile is a register tile. :param a: Tile to extract the element from @@ -2183,17 +2183,17 @@ def tile_sum_value_func(arg_types, arg_values): value_func=tile_sum_value_func, variadic=True, doc="""Cooperatively compute the sum the tile elements using all threads in the block. - + :param a: The tile to compute the sum of :returns: A single element tile with dimensions of (1,1) holding the sum - + Example: .. code-block:: python @wp.kernel def compute(): - + t = wp.tile_ones(dtype=float, m=16, n=16) s = wp.tile_sum(t) @@ -2202,11 +2202,11 @@ def compute(): wp.launch(compute, dim=[64], inputs=[]) Prints: - + .. code-block:: text tile(m=1, n=1, storage=register) = [[256]] - + """, group="Tile Primitives", export=False, @@ -2241,13 +2241,13 @@ def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, ar # variadic=True, native_func="tile_unary_map", doc="""Apply a unary function onto the tile. - + This function cooperatively applies a unary function to each element of the tile using all threads in the block. - + :param op: A callable function that accepts one argument and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype :returns: A tile with the same dimensions as the input tile, currently output tiles must have the same dtype as the input. - + Example: .. code-block:: python @@ -2263,11 +2263,11 @@ def compute(): wp.launch(compute, dim=[64], inputs=[]) Prints: - + .. code-block:: text tile(m=1, n=10, storage=register) = [[0 0.0998334 0.198669 0.29552 ...]] - """, + """, group="Tile Primitives", export=False, ) @@ -2307,7 +2307,7 @@ def tile_binary_map_value_func(arg_types, arg_values): # dispatch_func=tile_map_dispatch_func, # variadic=True, native_func="tile_binary_map", - doc="Apply the binary map operation onto each corresponding pair of elements from each the tile.", + doc="Apply the binary map operation onto each corresponding pair of elements from each the tile.", group="Tile Primitives", export=False, ) diff --git a/warp/context.py b/warp/context.py index 9f0617b1..55b42f3d 100644 --- a/warp/context.py +++ b/warp/context.py @@ -1751,7 +1751,7 @@ def __init__(self, name, loader): "fast_math": False, "cuda_output": None, # supported values: "ptx", "cubin", or None (automatic) "mode": warp.config.mode, - "block_dim": 0, + "block_dim": 256, } # Module dependencies are determined by scanning each function @@ -5009,7 +5009,7 @@ def launch( record_cmd: When True the launch will be returned as a ``Launch`` command object, the launch will not occur until the user calls ``cmd.launch()`` max_blocks: The maximum number of CUDA thread blocks to use. Only has an effect for CUDA kernel launches. If negative or zero, the maximum hardware value will be used. - block_dim: The number of threads per-block + block_dim: The number of threads per block. """ init() diff --git a/warp/jax_experimental.py b/warp/jax_experimental.py index 8e78ab26..5f62f953 100644 --- a/warp/jax_experimental.py +++ b/warp/jax_experimental.py @@ -102,7 +102,9 @@ def _warp_custom_callback(stream, buffers, opaque, opaque_len): assert hooks.forward, "Failed to find kernel entry point" # Launch the kernel. - wp.context.runtime.core.cuda_launch_kernel(device.context, hooks.forward, bounds.size, 0, kernel_params, stream) + wp.context.runtime.core.cuda_launch_kernel( + device.context, hooks.forward, bounds.size, 0, 256, kernel_params, stream + ) # TODO: is there a simpler way of getting the Jax "current" device? diff --git a/warp/native/builtin.h b/warp/native/builtin.h index 91701a89..7d1ac8d9 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1575,11 +1575,14 @@ inline CUDA_CALLABLE void print(transform_t t) printf("(%g %g %g) (%g %g %g %g)\n", float(t.p[0]), float(t.p[1]), float(t.p[2]), float(t.q.x), float(t.q.y), float(t.q.z), float(t.q.w)); } +inline CUDA_CALLABLE void adj_print(bool i, bool adj_i) { printf("%d adj: %d\n", i, adj_i); } +inline CUDA_CALLABLE void adj_print(int8 i, int8 adj_i) { printf("%hhd adj: %hhd\n", i, adj_i); } inline CUDA_CALLABLE void adj_print(int i, int adj_i) { printf("%d adj: %d\n", i, adj_i); } inline CUDA_CALLABLE void adj_print(float f, float adj_f) { printf("%g adj: %g\n", f, adj_f); } inline CUDA_CALLABLE void adj_print(short f, short adj_f) { printf("%hd adj: %hd\n", f, adj_f); } inline CUDA_CALLABLE void adj_print(long f, long adj_f) { printf("%ld adj: %ld\n", f, adj_f); } inline CUDA_CALLABLE void adj_print(long long f, long long adj_f) { printf("%lld adj: %lld\n", f, adj_f); } +inline CUDA_CALLABLE void adj_print(uint8 i, uint8 adj_i) { printf("%hhu adj: %hhu\n", i, adj_i); } inline CUDA_CALLABLE void adj_print(unsigned f, unsigned adj_f) { printf("%u adj: %u\n", f, adj_f); } inline CUDA_CALLABLE void adj_print(unsigned short f, unsigned short adj_f) { printf("%hu adj: %hu\n", f, adj_f); } inline CUDA_CALLABLE void adj_print(unsigned long f, unsigned long adj_f) { printf("%lu adj: %lu\n", f, adj_f); } @@ -1689,4 +1692,4 @@ inline CUDA_CALLABLE void adj_expect_near(const vec3& actual, const vec3& expect #include "tile.h" #include "tile_gemm.h" #include "tile_reduce.h" -#endif \ No newline at end of file +#endif diff --git a/warp/native/tile.h b/warp/native/tile.h index 4f562e15..4252bc97 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -238,7 +238,7 @@ struct tile_register_t WP_TILE_SHARED Type scratch; - // ensure any prevoiusly scheduled threads have finished reading from scratch + // ensure any previously scheduled threads have finished reading from scratch WP_TILE_SYNC(); if (threadIdx.x == thread) @@ -1063,4 +1063,3 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ } while (0) } // namespace wp - diff --git a/warp/native/warp.cu b/warp/native/warp.cu index 76f7b97f..7ae7b634 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -3121,10 +3121,19 @@ size_t cuda_launch_kernel(void* context, void* kernel, size_t dim, int max_block { ContextGuard guard(context); + if (tile_size <= 0) + { +#if defined(_DEBUG) + fprintf(stderr, "Warp warning: Got tile_size %d. Setting to 256.\n", dim, tile_size); +#endif + tile_size = 256; + } + const int block_dim = tile_size; + // CUDA specs up to compute capability 9.0 says the max x-dim grid is 2**31-1, so // grid_dim is fine as an int for the near future - int grid_dim = dim; + int grid_dim = (dim + block_dim - 1)/block_dim; if (max_blocks <= 0) { max_blocks = 2147483647; diff --git a/warp/stubs.py b/warp/stubs.py index 1a41fd5f..b2511920 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -888,61 +888,202 @@ def spatial_mass( @over def tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile: - """Allocate a tile local block of zero'd memory""" + """Allocates a tile of zero initialized items. + + :param m: Size of the first dimension of the output tile + :param n: Size of the second dimension of the output tile + :param dtype: Datatype of output tile's elements + :returns: A zero initialized tile with ``shape=(m,n)`` and the specified datatype + """ + ... + + +@over +def tile_ones(m: int32, n: int32, dtype: Scalar) -> Tile: + """Allocates a tile of one initialized items. + + :param m: Size of the first dimension of the output tile + :param n: Size of the second dimension of the output tile + :param dtype: Datatype of output tile's elements + :returns: A one initialized tile with ``shape=(m,n)`` and the specified dtype + """ + ... + + +@over +def tile_arange(*args: Scalar, dtype: Scalar) -> Tile: + """Generates a tile of linearly spaced elements. + + :param args: Variable length positional arguments, interpreted as: + + - ``(stop,)``: Generates values from ``0`` to ``stop - 1`` + - ``(start, stop)``: Generates values from ``start`` to ``stop - 1`` + - ``(start, stop, step)``: Generates values from ``start`` to ``stop - 1`` with a step size + + :param dtype: Datatype of output tile's elements (optional, default: int) + :returns: A tile with ``shape=(1,n)`` with linearly spaced elements of specified dtype + """ ... @over def tile_load(a: Array[Any], x: int32, y: int32, m: int32, n: int32) -> Tile: - """Load a tile of size (m, n) worth of data from array a from offset (i=x*m, j=y*n)""" + """Loads a tile from a global memory array. + + This method will cooperatively load a tile from global memory using all threads in the block. + + :param a: The source array in global memory + :param x: Offset in the source array measured in multiples of ``m``, i.e.: ``i=x*m`` + :param y: Offset in the source array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param m: The size of the tile's first dimension + :param n: The size of the tile's second dimensions + :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array + """ ... @over def tile_store(a: Array[Any], x: int32, y: int32, t: Any): - """Store tile `t` to an array `a` at offset `(i=x*m, j=y*n)`""" + """Stores a tile to a global memory array. + + This method will cooperatively store a tile to global memory using all threads in the block. + + :param a: The destination array in global memory + :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*m`` + :param y: Offset in the destination array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param t: The source tile to store data from, must have the same dtype as the destination array + """ ... @over def tile_atomic_add(a: Array[Any], x: int32, y: int32, t: Any) -> Tile: - """Atomically add a tile `t` worth of data to array `a` at offset `(i=x*m, j=y*n)`""" + """Atomically add a tile to the array `a`, each element will be updated atomically. + + :param a: Array in global memory, should have the same ``dtype`` as the input tile + :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*M`` where ``M`` is the first tile dimension + :param y: Offset in the destination array measured in multiples of ``n``, i.e.: ``j=y*N`` where ``N`` is the second tile dimension + :param t: Source tile to add to the destination array + :returns: A tile with the same dimensions and type as the source tile, holding the original value of the destination elements + """ ... @over def tile(x: Any) -> Tile: - """Construct a Tile from a per-thread kernel value, returns a tile with dimensions of `(1, block_dim)` where block_dim is the number of threads specified in `wp.launch()`""" - ... + """Constructs a new Tile from a per-thread kernel values. + This function converts values computed using scalar kernel code to a tile representation for input into collective operations. -@over -def tile_extract(a: Tile, i: int32, j: int32): - """Extract element at index (i, j) of the tile and return the native type""" + :param x: A per-thread local value, e.g.: scalar, vector, or matrix. + :returns: A tile with ``shape=(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch()``. + + This example shows how to create a linear sequence from thread variables: + + .. code-block:: python + + @wp.kernel + def compute(): + i = wp.tid() + t = wp.tile(i * 2) + print(t) + + + wp.launch(compute, dim=16, inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + tile(m=1, n=16, storage=register) = [[0 2 4 6 8 10 12 14...]] + + """ ... @over -def tile_matmul(a: Tile, b: Tile, out: Tile): - """Compute matrix product and accumulate out += a*b.""" +def tile_extract(a: Tile, i: int32, j: int32) -> Scalar: + """Extracts a single element from the tile and returns it as a scalar type. + + This function will extract an element from the tile and broadcast its value to all threads in the block, note that this may incur additional synchronization if the source tile is a register tile. + + :param a: Tile to extract the element from + :param i: Coordinate of element on first dimension + :param j: Coordinate of element on the second dimension + :returns: The value of the element at the specified tile location, with the same type as the input tile's per-element dtype + """ ... @over -def tile_sum(a: Tile): - """Computes the sum of all elements in the tile, returns a 1x1 tile, axis is currently ignored""" +def tile_sum(a: Tile) -> Tile: + """Cooperatively compute the sum the tile elements using all threads in the block. + + :param a: The tile to compute the sum of + :returns: A single element tile with dimensions of (1,1) holding the sum + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + t = wp.tile_ones(dtype=float, m=16, n=16) + s = wp.tile_sum(t) + + print(t) + + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[256]] + + + """ ... @over -def tile_map(op: Callable, a: Any): - """Map the operation onto each element of the tile""" +def tile_map(op: Callable, a: Any) -> Tile: + """Apply a unary function onto the tile. + + This function cooperatively applies a unary function to each element of the tile using all threads in the block. + + :param op: A callable function that accepts one argument and returns one argument, may be a user function or builtin + :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A tile with the same dimensions as the input tile, currently output tiles must have the same dtype as the input. + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + t = wp.tile_arange(0.0, 1.0, 0.1, dtype=float) + s = wp.tile_map(wp.sin, t) + + print(s) + + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=10, storage=register) = [[0 0.0998334 0.198669 0.29552 ...]] + + """ ... @over -def tile_map(op: Callable, a: Any, b: Any): - """Map the operation onto each element of the tile""" +def tile_map(op: Callable, a: Any, b: Any) -> Tile: + """Apply the binary map operation onto each corresponding pair of elements from each the tile.""" ... @@ -1837,217 +1978,145 @@ def atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: in @over def atomic_min(arr: Array[Any], i: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: FabricArray[Any], i: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: Array[Any], i: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: FabricArray[Any], i: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. - """ + """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" ... @@ -2144,7 +2213,7 @@ def add(a: Transformation[Scalar], b: Transformation[Scalar]) -> Transformation[ @over -def add(a: Tile, b: Tile): +def add(a: Tile, b: Tile) -> Tile: """Add each element of two tiles together""" ... @@ -2486,18 +2555,18 @@ def unot(a: Array[Any]) -> bool: @over -def tile_matmul_dx(a: Tile, b: Tile, out: Tile): +def tile_matmul_dx(a: Tile, b: Tile, out: Tile) -> Tile: """Compute matrix product and accumulate out += a*b.""" ... @over -def tile_fft_dx(inout: Tile): +def tile_fft_dx(inout: Tile) -> Tile: """Compute the FFT along the second dimension of a 2D tile of data.""" ... @over -def tile_ifft_dx(inout: Tile): +def tile_ifft_dx(inout: Tile) -> Tile: """Compute the inverse FFT along the second dimension of a 2D tile of data.""" ... diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index ed47b4a3..bc991c77 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -182,7 +182,6 @@ def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), C: wp.tile_store(C[i], 0, 0, sum) -@unittest.expectedFailure def test_tile_grouped_gemm(test, device): batch_count = 56 diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index 00b8b301..c343e353 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -71,13 +71,12 @@ def tile_reduce_1d_kernel(output: wp.array(dtype=int)): t = wp.tile(i) # convert to block wide tile s = wp.tile_sum(t) # sum over block - + # update global sum wp.tile_atomic_add(output, 0, 0, s) def test_tile_reduce_1d(test, device): - # use an unaligned grid dimension N = int(TILE_DIM * 3 / 2) @@ -85,27 +84,25 @@ def test_tile_reduce_1d(test, device): with wp.Tape() as tape: wp.launch(tile_reduce_1d_kernel, dim=[N], inputs=[output], block_dim=TILE_DIM, device=device) - - test.assertAlmostEqual(output.numpy()[0], np.sum(np.arange(N))) + test.assertEqual(output.numpy()[0], np.sum(np.arange(N))) @wp.kernel def tile_ones_kernel(out: wp.array(dtype=float)): i = wp.tid() - + t = wp.tile_ones(dtype=float, m=16, n=16) s = wp.tile_sum(t) wp.tile_store(out, 0, 0, s) + def test_tile_ones(test, device): - output = wp.zeros(shape=1, dtype=float, device=device) with wp.Tape() as tape: wp.launch(tile_ones_kernel, dim=[1, TILE_DIM], inputs=[output], block_dim=TILE_DIM, device=device) - wp.synchronize() test.assertAlmostEqual(output.numpy()[0], 256.0) @@ -113,7 +110,7 @@ def test_tile_ones(test, device): @wp.kernel def tile_arange_kernel(out: wp.array2d(dtype=int)): i = wp.tid() - + a = wp.tile_arange(17, dtype=int) b = wp.tile_arange(5, 23, dtype=int) c = wp.tile_arange(0, 34, 2, dtype=int) @@ -122,15 +119,15 @@ def tile_arange_kernel(out: wp.array2d(dtype=int)): wp.tile_store(out, 1, 0, b) wp.tile_store(out, 2, 0, c) + def test_tile_arange(test, device): - N = 17 output = wp.zeros(shape=(3, N), dtype=int, device=device) with wp.Tape() as tape: wp.launch(tile_arange_kernel, dim=[1, N], inputs=[output], block_dim=TILE_DIM, device=device) - + assert_np_equal(output.numpy()[0], np.arange(17)) assert_np_equal(output.numpy()[1], np.arange(5, 22)) assert_np_equal(output.numpy()[2], np.arange(0, 34, 2)) @@ -144,7 +141,7 @@ class TestTileReduce(unittest.TestCase): add_function_test(TestTileReduce, "test_tile_reduce_sum", test_tile_reduce_sum, devices=devices) -add_function_test(TestTileReduce, "test_tile_reduce_1d", test_tile_reduce_1d, devices=devices) +add_function_test(TestTileReduce, "test_tile_reduce_1d", test_tile_reduce_1d, devices=devices) add_function_test(TestTileReduce, "test_tile_ones", test_tile_ones, devices=devices) add_function_test(TestTileReduce, "test_tile_arange", test_tile_arange, devices=devices) diff --git a/warp/types.py b/warp/types.py index e099119d..7dc725fb 100644 --- a/warp/types.py +++ b/warp/types.py @@ -3001,18 +3001,19 @@ class TileZeros(Tile): def __init__(self, dtype, M, N): Tile.__init__(self, dtype, M, N, op="zeros", storage="shared") + class TileRange(Tile): def __init__(self, dtype, start, stop, step): - self.start = start self.stop = stop self.step = step M = 1 - N = int((stop-start)/step) + N = int((stop - start) / step) Tile.__init__(self, dtype, M, N, op="arange", storage="register") + class TileConstant(Tile): def __init__(self, dtype, M, N): Tile.__init__(self, dtype, M, N, op="constant", storage="register") From c51349af6ebab9eeb9e6dc05d575a1e2c7b23e87 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 27 Sep 2024 01:13:49 +0000 Subject: [PATCH 037/102] Cosmetic change to tile_arange() --- warp/builtins.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index e400c364..1caccac6 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1844,14 +1844,18 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a template_args.append(m) template_args.append(n) - # take dtype from stop value - t = return_type.dtype - - start = warp.codegen.Var(label=None, type=t, constant=return_type.start) - stop = warp.codegen.Var(label=None, type=t, constant=return_type.stop) - step = warp.codegen.Var(label=None, type=t, constant=return_type.step) - - return ([start, stop, step], template_args) + # todo: it is somewhat redundant to create new vars here since some of start,stop,step + # already exist depending on which form the function was called by the user + start = warp.codegen.Var(label=None, type=return_type.dtype, constant=return_type.start) + stop = warp.codegen.Var(label=None, type=return_type.dtype, constant=return_type.stop) + step = warp.codegen.Var(label=None, type=return_type.dtype, constant=return_type.step) + + function_args = [] + function_args.append(start) + function_args.append(stop) + function_args.append(step) + + return (function_args, template_args) add_builtin( From 3f31e2c5073d17b48ddc2e926ff7e80ff881b834 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 27 Sep 2024 03:49:42 +0000 Subject: [PATCH 038/102] Fix for test_tile_grouped_gemm Fix for duplicate LTO symbols when using the same GEMM multiple times in a module Remove *_dx() suffix and make them the default, disable scalar tile_matmul() Update docstrings for all tile functions --- docs/modules/functions.rst | 71 ++++++++++++++++++++--- warp/builtins.py | 102 ++++++++++++++++++++++++++++----- warp/codegen.py | 12 +--- warp/context.py | 4 +- warp/native/builtin.h | 85 ++++++++++++++++++--------- warp/native/tile.h | 34 +++++++---- warp/native/tile_gemm.h | 3 +- warp/stubs.py | 77 ++++++++++++++++++++++--- warp/tests/test_tile.py | 10 ++-- warp/tests/test_tile_mathdx.py | 20 +++---- 10 files changed, 322 insertions(+), 96 deletions(-) diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 5d2bc605..061fb5f6 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -952,7 +952,7 @@ Tile Primitives :param op: A callable function that accepts one argument and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype - :returns: A tile with the same dimensions as the input tile, currently output tiles must have the same dtype as the input. + :returns: A tile with the same dimensions and datatype as the input tile. Example: @@ -966,7 +966,7 @@ Tile Primitives print(s) - wp.launch(compute, dim=[64], inputs=[]) + wp.launch(compute, dim=[16], inputs=[]) Prints: @@ -980,22 +980,77 @@ Tile Primitives :noindex: :nocontentsentry: - Apply the binary map operation onto each corresponding pair of elements from each the tile. + Apply a binary function onto the tile. + + This function cooperatively applies a binary function to each element of the tiles using all threads in the block. + Both input tiles must have the same dimensions and datatype. + + :param op: A callable function that accepts two arguments and returns one argument, all of the same type, may be a user function or builtin + :param a: The first input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :param b: The second input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A tile with the same dimensions and datatype as the input tiles. + + Example: + .. code-block:: python -.. py:function:: tile_matmul_dx(a: Tile, b: Tile, out: Tile) -> Tile + @wp.kernel + def compute(): - Compute matrix product and accumulate out += a*b. + a = wp.tile_arange(0.0, 1.0, 0.1, dtype=float) + b = wp.tile_ones(m=1, n=10, dtype=float) + s = wp.tile_map(wp.add, a, b) + + print(s) + + wp.launch(compute, dim=[16], inputs=[]) + + Prints: + + .. code-block:: text -.. py:function:: tile_fft_dx(inout: Tile) -> Tile + tile(m=1, n=10, storage=register) = [[1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]] - Compute the FFT along the second dimension of a 2D tile of data. +.. py:function:: tile_matmul(a: Tile, b: Tile, out: Tile) -> Tile -.. py:function:: tile_ifft_dx(inout: Tile) -> Tile + Computes the matrix product and accumulates ``out += a*b``. + + Supported datatypes are: + * fp16, fp32, fp64 (real) + * vec2h, vec2f, vec2d (complex) + + All input and output tiles must have the same datatype, and will be automatically be migrated to shared memory if necessary. + + :param a: A tile with ``shape=(M, K)`` + :param b: A tile with ``shape=(K, N)`` + :param out: A tile with ``shape=(M, N)`` + + + +.. py:function:: tile_fft(inout: Tile) -> Tile + + Compute the forward FFT along the second dimension of a 2D tile of data. + + This function cooperatively computes the forward FFT on a tile of data inplace, treating each row individually. + + Supported datatypes are: + * vec2f, vec2d + + :param inout: The input/output tile + + +.. py:function:: tile_ifft(inout: Tile) -> Tile Compute the inverse FFT along the second dimension of a 2D tile of data. + + This function cooperatively computes the inverse FFT on a tile of data inplace, treating each row individually. + + Supported datatypes are: + * vec2f, vec2d + + :param inout: The input/output tile diff --git a/warp/builtins.py b/warp/builtins.py index 8fe51981..bcc3a573 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -2250,7 +2250,7 @@ def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, ar :param op: A callable function that accepts one argument and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype - :returns: A tile with the same dimensions as the input tile, currently output tiles must have the same dtype as the input. + :returns: A tile with the same dimensions and datatype as the input tile. Example: @@ -2264,7 +2264,7 @@ def compute(): print(s) - wp.launch(compute, dim=[64], inputs=[]) + wp.launch(compute, dim=[16], inputs=[]) Prints: @@ -2311,7 +2311,37 @@ def tile_binary_map_value_func(arg_types, arg_values): # dispatch_func=tile_map_dispatch_func, # variadic=True, native_func="tile_binary_map", - doc="Apply the binary map operation onto each corresponding pair of elements from each the tile.", + doc="""Apply a binary function onto the tile. + + This function cooperatively applies a binary function to each element of the tiles using all threads in the block. + Both input tiles must have the same dimensions and datatype. + + :param op: A callable function that accepts two arguments and returns one argument, all of the same type, may be a user function or builtin + :param a: The first input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :param b: The second input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A tile with the same dimensions and datatype as the input tiles. + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + a = wp.tile_arange(0.0, 1.0, 0.1, dtype=float) + b = wp.tile_ones(m=1, n=10, dtype=float) + + s = wp.tile_map(wp.add, a, b) + + print(s) + + wp.launch(compute, dim=[16], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=10, storage=register) = [[1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]]""", group="Tile Primitives", export=False, ) @@ -5023,8 +5053,8 @@ def tile_matmul_generic_value_func(arg_types, arg_values): return None -def tile_matmul_generic_dispatch_func( - arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any] +def tile_matmul_generic_lto_dispatch_func( + arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any], builder: warp.context.ModuleBuilder ): a = arg_values["a"] b = arg_values["b"] @@ -5097,6 +5127,12 @@ def make_transpose(t): raise RuntimeError("Invalid transpose mode") lto_symbol = f"dot_{M}_{N}_{K}_{tA}_{tB}_{precision}_{element_type}" + + # early out if LTO for this combination already exists for this module + if lto_symbol in builder.ltoirs: + return lto_symbol, builder.ltoirs[lto_symbol] + + # otherwise compile LTO lto_code = tempfile.NamedTemporaryFile() include_dirs = get_cuda_include_dirs() result = warp.context.runtime.core.cuda_compile_dot( @@ -5120,6 +5156,8 @@ def make_transpose(t): else: with open(lto_code.name, "rb") as f: lto_code = f.read() + + builder.ltoirs[lto_symbol] = lto_code return lto_symbol, lto_code (fun_forward, lto_forward) = make_function(M, N, K, "N", "N") # C += A * B @@ -5142,12 +5180,24 @@ def make_transpose(t): add_builtin( - "tile_matmul_dx", + "tile_matmul", input_types={"a": Tile, "b": Tile, "out": Tile}, value_func=tile_matmul_generic_value_func, - lto_dispatch_func=tile_matmul_generic_dispatch_func, + lto_dispatch_func=tile_matmul_generic_lto_dispatch_func, variadic=True, - doc="Compute matrix product and accumulate out += a*b.", + doc="""Computes the matrix product and accumulates ``out += a*b``. + + Supported datatypes are: + * fp16, fp32, fp64 (real) + * vec2h, vec2f, vec2d (complex) + + All input and output tiles must have the same datatype. Tile data will be automatically be migrated + to shared memory if necessary and will use TensoreCore operations when available. + + :param a: A tile with ``shape=(M, K)`` + :param b: A tile with ``shape=(K, N)`` + :param out: A tile with ``shape=(M, N)`` + """, group="Tile Primitives", export=False, namespace="", @@ -5173,11 +5223,12 @@ def tile_fft_generic_value_func(arg_types, arg_values): return None -def tile_fft_generic_dispatch_func( +def tile_fft_generic_lto_dispatch_func( arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any], + builder: warp.context.ModuleBuilder, direction: str = None, ): inout = arg_values["inout"] @@ -5213,6 +5264,11 @@ def tile_fft_generic_dispatch_func( ept = size // num_threads lto_symbol = f"fft_{size}_{ept}_{arch}_{direction}_{precision}" + # early out if LTO for this combination already exists for this module + if lto_symbol in builder.ltoirs: + return lto_symbol, builder.ltoirs[lto_symbol] + + # otherwise compile LTO lto_code = tempfile.NamedTemporaryFile() shared_memory_size = ctypes.c_int(0) @@ -5238,6 +5294,8 @@ def tile_fft_generic_dispatch_func( with open(lto_code.name, "rb") as f: lto_code = f.read() + builder.ltoirs[lto_symbol] = lto_code + return ( ( Var(lto_symbol, str, False, True, False), @@ -5253,24 +5311,38 @@ def tile_fft_generic_dispatch_func( add_builtin( - "tile_fft_dx", + "tile_fft", input_types={"inout": Tile}, value_func=tile_fft_generic_value_func, - lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction="forward"), + lto_dispatch_func=functools.partial(tile_fft_generic_lto_dispatch_func, direction="forward"), variadic=True, - doc="Compute the FFT along the second dimension of a 2D tile of data.", + doc="""Compute the forward FFT along the second dimension of a 2D tile of data. + + This function cooperatively computes the forward FFT on a tile of data inplace, treating each row individually. + + Supported datatypes are: + * vec2f, vec2d + + :param inout: The input/output tile""", group="Tile Primitives", export=False, namespace="", ) add_builtin( - "tile_ifft_dx", + "tile_ifft", input_types={"inout": Tile}, value_func=tile_fft_generic_value_func, - lto_dispatch_func=functools.partial(tile_fft_generic_dispatch_func, direction="inverse"), + lto_dispatch_func=functools.partial(tile_fft_generic_lto_dispatch_func, direction="inverse"), variadic=True, - doc="Compute the inverse FFT along the second dimension of a 2D tile of data.", + doc="""Compute the inverse FFT along the second dimension of a 2D tile of data. + + This function cooperatively computes the inverse FFT on a tile of data inplace, treating each row individually. + + Supported datatypes are: + * vec2f, vec2d + + :param inout: The input/output tile""", group="Tile Primitives", export=False, namespace="", diff --git a/warp/codegen.py b/warp/codegen.py index f9a47f25..0eb26c1b 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -940,9 +940,6 @@ def build(adj, builder, default_builder_options=None): # used to generate new label indices adj.label_count = 0 - # collect ltoirs - adj.ltoirs = [] - # update symbol map for each argument for a in adj.args: adj.symbols[a.label] = a @@ -968,8 +965,6 @@ def build(adj, builder, default_builder_options=None): elif isinstance(a.type, warp.types.array) and isinstance(a.type.dtype, Struct): builder.build_struct_recursive(a.type.dtype) - builder.ltoirs.extend(adj.ltoirs) - # code generation methods def format_template(adj, template, input_vars, output_var): # output var is always the 0th index @@ -1280,9 +1275,8 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): # a literal value or references a variable. if func.lto_dispatch_func is not None: func_args, template_args, ltoirs = func.lto_dispatch_func( - func.input_types, return_type, bound_args, options=adj.builder_options + func.input_types, return_type, bound_args, options=adj.builder_options, builder=adj.builder ) - adj.ltoirs.extend(ltoirs) elif func.dispatch_func is not None: func_args, template_args = func.dispatch_func(func.input_types, return_type, bound_args) else: @@ -2759,7 +2753,7 @@ def get_references(adj) -> Dict[str, Any]: #define int(x) cast_int(x) #define adj_int(x, adj_x, adj_ret) adj_cast_int(x, adj_x, adj_ret) -#define builtin_tid1d() wp::tid(task_index) +#define builtin_tid1d() wp::tid(task_index, dim) #define builtin_tid2d(x, y) wp::tid(x, y, task_index, dim) #define builtin_tid3d(x, y, z) wp::tid(x, y, z, task_index, dim) #define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, task_index, dim) @@ -2778,7 +2772,7 @@ def get_references(adj) -> Dict[str, Any]: #define int(x) cast_int(x) #define adj_int(x, adj_x, adj_ret) adj_cast_int(x, adj_x, adj_ret) -#define builtin_tid1d() wp::tid(_idx) +#define builtin_tid1d() wp::tid(_idx, dim) #define builtin_tid2d(x, y) wp::tid(x, y, _idx, dim) #define builtin_tid3d(x, y, z) wp::tid(x, y, z, _idx, dim) #define builtin_tid4d(x, y, z, w) wp::tid(x, y, z, w, _idx, dim) diff --git a/warp/context.py b/warp/context.py index 55b42f3d..ad18d270 100644 --- a/warp/context.py +++ b/warp/context.py @@ -1541,7 +1541,7 @@ def __init__(self, module, options, hasher=None): self.options = options self.module = module self.deferred_functions = [] - self.ltoirs = [] + self.ltoirs = {} # map from lto symbol to lto binary if hasher is None: hasher = ModuleHasher(module) @@ -2024,7 +2024,7 @@ def load(self, device, block_dim=None) -> ModuleExec: config=self.options["mode"], fast_math=self.options["fast_math"], verify_fp=warp.config.verify_fp, - ltoirs=builder.ltoirs, + ltoirs=builder.ltoirs.values(), ) except Exception as e: diff --git a/warp/native/builtin.h b/warp/native/builtin.h index 7d1ac8d9..bf12b765 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1145,7 +1145,47 @@ struct launch_bounds_t size_t size; // total number of threads }; -inline CUDA_CALLABLE int tid(size_t index) +// represents coordinate in the launch grid +struct launch_coord_t +{ + int i; + int j; + int k; + int l; +}; + +// unravels a linear thread index to the corresponding launch grid coord (up to 4d) +inline CUDA_CALLABLE launch_coord_t launch_coord(size_t linear, const launch_bounds_t& bounds) +{ + launch_coord_t coord = {0, 0, 0, 0}; + + if (bounds.ndim > 3) + { + coord.l = linear%bounds.shape[3]; + linear /= bounds.shape[3]; + } + + if (bounds.ndim > 2) + { + coord.k = linear%bounds.shape[2]; + linear /= bounds.shape[2]; + } + + if (bounds.ndim > 1) + { + coord.j = linear%bounds.shape[1]; + linear /= bounds.shape[1]; + } + + if (bounds.ndim > 0) + { + coord.i = linear; + } + + return coord; +} + +inline CUDA_CALLABLE int tid(size_t index, const launch_bounds_t& bounds) { // For the 1-D tid() we need to warn the user if we're about to provide a truncated index // Only do this in _DEBUG when called from device to avoid excessive register allocation @@ -1154,40 +1194,33 @@ inline CUDA_CALLABLE int tid(size_t index) printf("Warp warning: tid() is returning an overflowed int\n"); } #endif - return static_cast(index); + + launch_coord_t c = launch_coord(index, bounds); + return static_cast(c.i); } -inline CUDA_CALLABLE_DEVICE void tid(int& i, int& j, size_t index, const launch_bounds_t& launch_bounds) +inline CUDA_CALLABLE_DEVICE void tid(int& i, int& j, size_t index, const launch_bounds_t& bounds) { - const size_t n = launch_bounds.shape[1]; - - // convert to work item - i = index/n; - j = index%n; + launch_coord_t c = launch_coord(index, bounds); + i = c.i; + j = c.j; } -inline CUDA_CALLABLE_DEVICE void tid(int& i, int& j, int& k, size_t index, const launch_bounds_t& launch_bounds) +inline CUDA_CALLABLE_DEVICE void tid(int& i, int& j, int& k, size_t index, const launch_bounds_t& bounds) { - const size_t n = launch_bounds.shape[1]; - const size_t o = launch_bounds.shape[2]; - - // convert to work item - i = index/(n*o); - j = index%(n*o)/o; - k = index%o; + launch_coord_t c = launch_coord(index, bounds); + i = c.i; + j = c.j; + k = c.k; } -inline CUDA_CALLABLE_DEVICE void tid(int& i, int& j, int& k, int& l, size_t index, const launch_bounds_t& launch_bounds) +inline CUDA_CALLABLE_DEVICE void tid(int& i, int& j, int& k, int& l, size_t index, const launch_bounds_t& bounds) { - const size_t n = launch_bounds.shape[1]; - const size_t o = launch_bounds.shape[2]; - const size_t p = launch_bounds.shape[3]; - - // convert to work item - i = index/(n*o*p); - j = index%(n*o*p)/(o*p); - k = index%(o*p)/p; - l = index%p; + launch_coord_t c = launch_coord(index, bounds); + i = c.i; + j = c.j; + k = c.k; + l = c.l; } template diff --git a/warp/native/tile.h b/warp/native/tile.h index 4252bc97..f3b1eea5 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -36,13 +36,20 @@ [ ] Layouts [x] Simple [ ] Cute - [ ] Remove Alloc type from tile_shared_t - + [x] Remove Alloc type from tile_shared_t + [ ] wp.launch_tiled() helper +[ ] Creation + [x] zeros + [x] ones + [x] arange + [x] tile() + [ ] untile() + [ ] explicit storage [ ] Load/Store [ ] 1D load/store variants [ ] max_coord option for non-aligned loads [ ] Indexed load - [ ] wp.tile_atomic_add() + [x] wp.tile_atomic_add() [ ] Maps [x] Support user functions [x] Support built-in functions @@ -58,6 +65,9 @@ [x] MatMul [x] Forward [x] Reverse +[ ] Operators + [ ] +, -, *, /, @? + [ ] += for matmul, e.g.: c += a@b, or c = a@b [ ] Reshape [ ] Broadcasting [ ] Transpose @@ -66,7 +76,7 @@ [ ] Slice [ ] Runtime [x] Compile-time block dimensions - [ ] Switch between SIMT / Tile based execution if `tile_dim` not provided to wp.launch() + [x] Switch between SIMT / Tile based execution if `tile_dim` not provided to wp.launch() [ ] Examples [ ] GEMM [ ] Batched MLP @@ -1011,7 +1021,7 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ // But cuBLASDx follows the BLAS convention: matrices are col-major, so we swap A & B in the code below -#define tile_matmul_dx(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C) \ +#define tile_matmul(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C) \ do { \ void fun_forward(dtype, dtype*, dtype*, dtype, dtype*); \ WP_TILE_SYNC(); \ @@ -1021,7 +1031,7 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ // adj_fun_forward, adj_fun_backward_A, adj_fun_backward_B, adj_dtype are in practice ignored // but are here because builtins.py creates them even though those are effectively compile time constants -#define adj_tile_matmul_dx(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C, \ +#define adj_tile_matmul(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C, \ adj_fun_forward, adj_fun_backward_A, adj_fun_backward_B, adj_dtype, \ adjA, adjB, adjC) \ do { \ @@ -1033,7 +1043,7 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ WP_TILE_SYNC(); \ } while (0) -#define tile_fft_dx(function_name, dtype, shared_memory_size, batch_size, ept, Xinout) \ +#define tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, Xinout) \ do { \ void function_name(dtype*, dtype*); \ WP_TILE_SHARED __align__(16) char buffer[shared_memory_size]; \ @@ -1044,22 +1054,22 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ } \ } while (0) -#define tile_ifft_dx tile_fft_dx +#define tile_ifft tile_fft // adj_function_name, adj_dtype, adj_shared_memory_size, adj_batch_size, adj_ept are all ignored -#define adj_tile_fft_dx(function_name, dtype, shared_memory_size, batch_size, ept, Xinout, \ +#define adj_tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, Xinout, \ adj_function_name, adj_dtype, adj_shared_memory_size, adj_batch_size, adj_ept, \ adj_Xinout) \ do { \ - tile_ifft_dx(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \ + tile_ifft(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \ } while (0) -#define adj_tile_ifft_dx(function_name, dtype, shared_memory_size, batch_size, ept, Xinout, \ +#define adj_tile_ifft(function_name, dtype, shared_memory_size, batch_size, ept, Xinout, \ adj_function_name, adj_dtype, adj_shared_memory_size, adj_batch_size, adj_ept, \ adj_Xinout) \ do { \ - tile_fft_dx(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \ + tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \ } while (0) } // namespace wp diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index 3aa3dbe7..c033330a 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -305,6 +305,7 @@ inline CUDA_CALLABLE void tile_matmul(const array_t& A, const array_t& B, #endif // USE_CUTE +#if 0 template void tile_matmul(TileA& a, TileB& b, TileC& c) @@ -327,6 +328,6 @@ void adj_tile_matmul(TileA& a, TileB& b, TileC& c, tile_matmul_scalar(wp::tile_transpose(a), adj_c, adj_b); } - +#endif // 0 } // namespace wp diff --git a/warp/stubs.py b/warp/stubs.py index b2511920..c5b6fbf3 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -1055,7 +1055,7 @@ def tile_map(op: Callable, a: Any) -> Tile: :param op: A callable function that accepts one argument and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype - :returns: A tile with the same dimensions as the input tile, currently output tiles must have the same dtype as the input. + :returns: A tile with the same dimensions and datatype as the input tile. Example: @@ -1069,7 +1069,7 @@ def compute(): print(s) - wp.launch(compute, dim=[64], inputs=[]) + wp.launch(compute, dim=[16], inputs=[]) Prints: @@ -1083,7 +1083,38 @@ def compute(): @over def tile_map(op: Callable, a: Any, b: Any) -> Tile: - """Apply the binary map operation onto each corresponding pair of elements from each the tile.""" + """Apply a binary function onto the tile. + + This function cooperatively applies a binary function to each element of the tiles using all threads in the block. + Both input tiles must have the same dimensions and datatype. + + :param op: A callable function that accepts two arguments and returns one argument, all of the same type, may be a user function or builtin + :param a: The first input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :param b: The second input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A tile with the same dimensions and datatype as the input tiles. + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + a = wp.tile_arange(0.0, 1.0, 0.1, dtype=float) + b = wp.tile_ones(m=1, n=10, dtype=float) + + s = wp.tile_map(wp.add, a, b) + + print(s) + + + wp.launch(compute, dim=[16], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=10, storage=register) = [[1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]] + """ ... @@ -2555,18 +2586,46 @@ def unot(a: Array[Any]) -> bool: @over -def tile_matmul_dx(a: Tile, b: Tile, out: Tile) -> Tile: - """Compute matrix product and accumulate out += a*b.""" +def tile_matmul(a: Tile, b: Tile, out: Tile) -> Tile: + """Computes the matrix product and accumulates ``out += a*b``. + + Supported datatypes are: + * fp16, fp32, fp64 (real) + * vec2h, vec2f, vec2d (complex) + + All input and output tiles must have the same datatype, and will be automatically be migrated to shared memory if necessary. + + :param a: A tile with ``shape=(M, K)`` + :param b: A tile with ``shape=(K, N)`` + :param out: A tile with ``shape=(M, N)`` + + """ ... @over -def tile_fft_dx(inout: Tile) -> Tile: - """Compute the FFT along the second dimension of a 2D tile of data.""" +def tile_fft(inout: Tile) -> Tile: + """Compute the forward FFT along the second dimension of a 2D tile of data. + + This function cooperatively computes the forward FFT on a tile of data inplace, treating each row individually. + + Supported datatypes are: + * vec2f, vec2d + + :param inout: The input/output tile + """ ... @over -def tile_ifft_dx(inout: Tile) -> Tile: - """Compute the inverse FFT along the second dimension of a 2D tile of data.""" +def tile_ifft(inout: Tile) -> Tile: + """Compute the inverse FFT along the second dimension of a 2D tile of data. + + This function cooperatively computes the inverse FFT on a tile of data inplace, treating each row individually. + + Supported datatypes are: + * vec2f, vec2d + + :param inout: The input/output tile + """ ... diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index bc991c77..f6822a0e 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -196,12 +196,14 @@ def test_tile_grouped_gemm(test, device): A_wp = wp.array(A, requires_grad=True, device=device) B_wp = wp.array(B, requires_grad=True, device=device) - C_wp = wp.array(C, requires_grad=True, device=device) + C_wp = wp.zeros((batch_count, TILE_M, TILE_N), requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( - tile_grouped_gemm, dim=[batch_count, TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, device=device - ) + wp.launch(tile_grouped_gemm, + dim=[batch_count, TILE_DIM], + inputs=[A_wp, B_wp, C_wp], + block_dim=TILE_DIM, + device=device) # TODO: 32 mismatched elements assert_np_equal(C_wp.numpy(), C) diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py index 6cf4b7c1..229ce074 100644 --- a/warp/tests/test_tile_mathdx.py +++ b/warp/tests/test_tile_mathdx.py @@ -25,18 +25,18 @@ @wp.kernel() -def tile_math_dx_matmul_kernel( +def tile_math_matmul_kernel( ga: wp.array2d(dtype=wp.float64), gb: wp.array2d(dtype=wp.float64), gc: wp.array2d(dtype=wp.float64) ): i, j, _ = wp.tid() a = wp.tile_load(ga, i, j, m=TILE_M, n=TILE_K) b = wp.tile_load(gb, i, j, m=TILE_K, n=TILE_N) c = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float64) - wp.tile_matmul_dx(a, b, c) + wp.tile_matmul(a, b, c) wp.tile_store(gc, i, j, c) -def test_tile_math_dx_matmul(test, device): +def test_tile_math_matmul(test, device): rng = np.random.default_rng(42) A = rng.random((TILE_M, TILE_K), dtype=np.float64) @@ -49,7 +49,7 @@ def test_tile_math_dx_matmul(test, device): with wp.Tape() as tape: wp.launch( - tile_math_dx_matmul_kernel, + tile_math_matmul_kernel, dim=[1, 1, TILE_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, @@ -68,14 +68,14 @@ def test_tile_math_dx_matmul(test, device): @wp.kernel() -def tile_math_dx_fft_kernel(gx: wp.array2d(dtype=wp.vec2f), gy: wp.array2d(dtype=wp.vec2f)): +def tile_math_fft_kernel(gx: wp.array2d(dtype=wp.vec2f), gy: wp.array2d(dtype=wp.vec2f)): i, j, _ = wp.tid() xy = wp.tile_load(gx, i, j, m=N_FFT, n=N_FFT) - wp.tile_fft_dx(xy) + wp.tile_fft(xy) wp.tile_store(gy, i, j, xy) -def test_tile_math_dx_fft(test, device): +def test_tile_math_fft(test, device): rng = np.random.default_rng(42) # Warp doesn't really have a complex64 type, @@ -91,7 +91,7 @@ def test_tile_math_dx_fft(test, device): Y_c64 = np.fft.fft(X_c64, axis=-1) with wp.Tape() as tape: - wp.launch(tile_math_dx_fft_kernel, dim=[1, 1, TILE_DIM], inputs=[X_wp, Y_wp], block_dim=TILE_DIM, device=device) + wp.launch(tile_math_fft_kernel, dim=[1, 1, TILE_DIM], inputs=[X_wp, Y_wp], block_dim=TILE_DIM, device=device) Y_wp_c64 = Y_wp.numpy().view(np.complex64).reshape(N_FFT, N_FFT) @@ -108,8 +108,8 @@ class TestTileMathDx(unittest.TestCase): pass -add_function_test(TestTileMathDx, "test_tile_math_dx_matmul", test_tile_math_dx_matmul, devices=devices) -add_function_test(TestTileMathDx, "test_tile_math_dx_fft", test_tile_math_dx_fft, devices=devices) +add_function_test(TestTileMathDx, "test_tile_math_matmul", test_tile_math_matmul, devices=devices) +add_function_test(TestTileMathDx, "test_tile_math_fft", test_tile_math_fft, devices=devices) if __name__ == "__main__": wp.clear_kernel_cache() From b3a409263d9fe4ff29b21c7c395be807d7c423b2 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 27 Sep 2024 04:57:59 +0000 Subject: [PATCH 039/102] Add a wp.tiled_launch() helper to launch grids with a trailing tile dim --- docs/modules/functions.rst | 3 +- docs/modules/runtime.rst | 2 ++ warp/__init__.py | 1 + warp/context.py | 41 ++++++++++++++++++++++ warp/stubs.py | 4 ++- warp/tests/test_misc.py | 63 ++++++++++++++++++++++++++++++++++ warp/tests/test_tile.py | 56 ++++++++++++++++-------------- warp/tests/test_tile_mathdx.py | 15 +++++--- warp/tests/test_tile_reduce.py | 27 ++++++--------- 9 files changed, 164 insertions(+), 48 deletions(-) create mode 100644 warp/tests/test_misc.py diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 061fb5f6..4f328880 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -1021,7 +1021,8 @@ Tile Primitives * fp16, fp32, fp64 (real) * vec2h, vec2f, vec2d (complex) - All input and output tiles must have the same datatype, and will be automatically be migrated to shared memory if necessary. + All input and output tiles must have the same datatype. Tile data will be automatically be migrated + to shared memory if necessary and will use TensoreCore operations when available. :param a: A tile with ``shape=(M, K)`` :param b: A tile with ``shape=(K, N)`` diff --git a/docs/modules/runtime.rst b/docs/modules/runtime.rst index 4d0d4fad..05c63d43 100644 --- a/docs/modules/runtime.rst +++ b/docs/modules/runtime.rst @@ -38,6 +38,8 @@ The location of the kernel cache is printed when Warp is initialized. generated compilation artifacts as Warp does not automatically try to keep the cache below a certain size. .. autofunction:: launch +.. autofunction:: launch_tiled + .. autofunction:: clear_kernel_cache .. _Runtime Kernel Creation: diff --git a/warp/__init__.py b/warp/__init__.py index 76672327..8ecda0c1 100644 --- a/warp/__init__.py +++ b/warp/__init__.py @@ -58,6 +58,7 @@ copy, from_numpy, launch, + launch_tiled, synchronize, force_load, load_module, diff --git a/warp/context.py b/warp/context.py index ad18d270..efcf7fd6 100644 --- a/warp/context.py +++ b/warp/context.py @@ -5176,6 +5176,47 @@ def pack_args(args, params, adjoint=False): if warp.config.verify_autograd_array_access: runtime.tape._check_kernel_array_access(kernel, fwd_args) +def launch_tiled(*args, **kwargs): + """A helper method for launching a grid with an extra trailing dimension equal to the block size. + + For example, to launch a 2D grid, where each element has 64 threads assigned you would use the following: + + .. code-block:: python + + wp.launch_tiled(kernel, [M, N], inputs=[...], block_dim=64) + + Which is equivalent to the following: + + .. code-block:: python + + wp.launch(kernel, [M, N, 64], inputs=[...], block_dim=64) + + Inside your kernel code you can retrieve the first two indices of the thread as usual, ignoring the implicit third dimension if desired: + + .. code-block:: python + + @wp.kernel + def compute() + + i, j = wp.tid() + + ... + """ + + if len(kwargs["dim"]) > 3: + raise RuntimeError("wp.launch_tiled() requires a grid with fewer than 4 dimensions") + + # promote dim to a list in case it was passed as a scalar or tuple + dim = kwargs["dim"] + if not isinstance(dim, list): + dim = list(dim) if isinstance(dim, tuple) else [dim] + + # add trailing dimension + kwargs["dim"] = dim + [kwargs["block_dim"]] + + # forward to original launch method + launch(*args, **kwargs) + def synchronize(): """Manually synchronize the calling CPU thread with any outstanding CUDA work on all devices diff --git a/warp/stubs.py b/warp/stubs.py index c5b6fbf3..a0e38e91 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -67,6 +67,7 @@ copy, from_numpy, launch, + launch_tiled, synchronize, force_load, load_module, @@ -2593,7 +2594,8 @@ def tile_matmul(a: Tile, b: Tile, out: Tile) -> Tile: * fp16, fp32, fp64 (real) * vec2h, vec2f, vec2d (complex) - All input and output tiles must have the same datatype, and will be automatically be migrated to shared memory if necessary. + All input and output tiles must have the same datatype. Tile data will be automatically be migrated + to shared memory if necessary and will use TensoreCore operations when available. :param a: A tile with ``shape=(M, K)`` :param b: A tile with ``shape=(K, N)`` diff --git a/warp/tests/test_misc.py b/warp/tests/test_misc.py new file mode 100644 index 00000000..de9e5fc4 --- /dev/null +++ b/warp/tests/test_misc.py @@ -0,0 +1,63 @@ +import numpy as np +import warp as wp + +wp.clear_kernel_cache() + +TILE_M = wp.constant(8) +TILE_N = wp.constant(4) +TILE_K = wp.constant(8) + +# num threads per-tile +TILE_DIM = 64 + + +@wp.kernel +def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), C: wp.array3d(dtype=float)): + # output tile index + i = wp.tid() + + a = wp.tile_load(A[i], 0, 0, m=TILE_M, n=TILE_K) + b = wp.tile_load(B[i], 0, 0, m=TILE_K, n=TILE_N) + + print(a) + print(b) + + # sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + + # wp.tile_matmul(a, b, sum) + + # print(sum) + + # wp.tile_store(C[i], 0, 0, sum) + + +batch_count = 1 + +M = TILE_M +N = TILE_N +K = TILE_K + +device = "cuda:0" + +rng = np.random.default_rng(42) +A = rng.random((batch_count, M, K), dtype=np.float32) +B = rng.random((batch_count, K, N), dtype=np.float32) +C = A @ B + +A_wp = wp.array(A, requires_grad=True, device=device) +B_wp = wp.array(B, requires_grad=True, device=device) +C_wp = wp.zeros((batch_count, TILE_M, TILE_N), requires_grad=True, device=device) + +with wp.Tape() as tape: + wp.launch(tile_grouped_gemm, + dim=[batch_count, TILE_DIM], + inputs=[A_wp, B_wp, C_wp], + block_dim=TILE_DIM, + device=device) + +wp.synchronize() + +# TODO: 32 mismatched elements +#assert_np_equal(C_wp.numpy(), C) +#print(C_wp.numpy()) + diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index f6822a0e..f757be22 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -23,7 +23,7 @@ @wp.kernel def tile_copy(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): # tile index - i, j, _ = wp.tid() + i, j = wp.tid() a = wp.tile_load(A, i, j, m=TILE_M, n=TILE_N) wp.tile_store(B, i, j, a) @@ -42,9 +42,9 @@ def test_tile_copy(test, device): B_wp = wp.array(B, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( + wp.launch_tiled( tile_copy, - dim=[int(M / TILE_M), int(N / TILE_N), TILE_DIM], + dim=[int(M / TILE_M), int(N / TILE_N)], inputs=[A_wp, B_wp], block_dim=TILE_DIM, device=device, @@ -68,7 +68,7 @@ def unary_func(x: float): @wp.kernel def tile_unary_map(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): # tile index - i, j, _ = wp.tid() + i, j = wp.tid() a = wp.tile_load(input, i, j, m=TILE_M, n=TILE_N) @@ -92,9 +92,9 @@ def test_tile_unary_map(test, device): B_wp = wp.zeros_like(A_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( + wp.launch_tiled( tile_unary_map, - dim=[int(M / TILE_M), int(N / TILE_N), TILE_DIM], + dim=[int(M / TILE_M), int(N / TILE_N)], inputs=[A_wp, B_wp], block_dim=TILE_DIM, device=device, @@ -120,7 +120,7 @@ def tile_binary_map( input_a: wp.array2d(dtype=float), input_b: wp.array2d(dtype=float), output: wp.array2d(dtype=float) ): # tile index - i, j, _ = wp.tid() + i, j = wp.tid() a = wp.tile_load(input_a, i, j, m=TILE_M, n=TILE_N) b = wp.tile_load(input_b, i, j, m=TILE_M, n=TILE_N) @@ -148,9 +148,9 @@ def test_tile_binary_map(test, device): C_wp = wp.zeros_like(A_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( + wp.launch_tiled( tile_binary_map, - dim=[int(M / TILE_M), int(N / TILE_N), TILE_DIM], + dim=[int(M / TILE_M), int(N / TILE_N)], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, device=device, @@ -199,8 +199,8 @@ def test_tile_grouped_gemm(test, device): C_wp = wp.zeros((batch_count, TILE_M, TILE_N), requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_grouped_gemm, - dim=[batch_count, TILE_DIM], + wp.launch_tiled(tile_grouped_gemm, + dim=[batch_count], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, device=device) @@ -212,7 +212,7 @@ def test_tile_grouped_gemm(test, device): @wp.kernel def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): # output tile index - i, j, _ = wp.tid() + i, j = wp.tid() sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) @@ -247,9 +247,9 @@ def test_tile_gemm(test, device): C_wp = wp.array(C, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( + wp.launch_tiled( tile_gemm, - dim=(int(M / TILE_M), int(N / TILE_N), TILE_DIM), + dim=(int(M / TILE_M), int(N / TILE_N)), inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, device=device, @@ -268,7 +268,7 @@ def test_tile_gemm(test, device): @wp.kernel def tile_operators(input: wp.array3d(dtype=float), output: wp.array3d(dtype=float)): # output tile index - i, _ = wp.tid() + i = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) @@ -301,9 +301,12 @@ def test_tile_operators(test, device): output_wp = wp.zeros_like(input_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( - tile_operators, dim=[batch_count, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device - ) + wp.launch_tiled( + tile_operators, + dim=[batch_count], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device) assert_np_equal(output_wp.numpy(), output) @@ -317,7 +320,7 @@ def test_tile_operators(test, device): @wp.kernel def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): # output tile index - i, _ = wp.tid() + i = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) s = wp.tile_sum(a) * 0.5 @@ -338,9 +341,9 @@ def test_tile_sum(test, device): output_wp = wp.zeros(batch_count, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( + wp.launch_tiled( tile_sum_kernel, - dim=[batch_count, TILE_DIM], + dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device, @@ -362,7 +365,7 @@ def test_tile_sum(test, device): @wp.kernel def tile_extract_kernel(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): # output tile index - i, _ = wp.tid() + i = wp.tid() t = wp.tile_load(input, 0, 0, m=TILE_M, n=TILE_N) @@ -384,9 +387,12 @@ def test_tile_extract(test, device): output_wp = wp.zeros_like(input_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( - tile_extract_kernel, dim=[1, TILE_DIM], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device - ) + wp.launch_tiled( + tile_extract_kernel, + dim=[1], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device) assert_array_equal(output_wp, input_wp) diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py index 229ce074..92e97ff0 100644 --- a/warp/tests/test_tile_mathdx.py +++ b/warp/tests/test_tile_mathdx.py @@ -28,7 +28,7 @@ def tile_math_matmul_kernel( ga: wp.array2d(dtype=wp.float64), gb: wp.array2d(dtype=wp.float64), gc: wp.array2d(dtype=wp.float64) ): - i, j, _ = wp.tid() + i, j = wp.tid() a = wp.tile_load(ga, i, j, m=TILE_M, n=TILE_K) b = wp.tile_load(gb, i, j, m=TILE_K, n=TILE_N) c = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float64) @@ -48,9 +48,9 @@ def test_tile_math_matmul(test, device): C_wp = wp.array(C, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( + wp.launch_tiled( tile_math_matmul_kernel, - dim=[1, 1, TILE_DIM], + dim=[1, 1], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, device=device, @@ -69,7 +69,7 @@ def test_tile_math_matmul(test, device): @wp.kernel() def tile_math_fft_kernel(gx: wp.array2d(dtype=wp.vec2f), gy: wp.array2d(dtype=wp.vec2f)): - i, j, _ = wp.tid() + i, j = wp.tid() xy = wp.tile_load(gx, i, j, m=N_FFT, n=N_FFT) wp.tile_fft(xy) wp.tile_store(gy, i, j, xy) @@ -91,7 +91,12 @@ def test_tile_math_fft(test, device): Y_c64 = np.fft.fft(X_c64, axis=-1) with wp.Tape() as tape: - wp.launch(tile_math_fft_kernel, dim=[1, 1, TILE_DIM], inputs=[X_wp, Y_wp], block_dim=TILE_DIM, device=device) + wp.launch_tiled( + tile_math_fft_kernel, + dim=[1, 1], + inputs=[X_wp, Y_wp], + block_dim=TILE_DIM, + device=device) Y_wp_c64 = Y_wp.numpy().view(np.complex64).reshape(N_FFT, N_FFT) diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index c343e353..3f65b7cf 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -21,9 +21,9 @@ @wp.kernel -def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): +def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): # output tile index - i, _ = wp.tid() + i = wp.tid() a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) s = wp.tile_sum(a) * 0.5 @@ -44,13 +44,7 @@ def test_tile_reduce_sum(test, device): output_wp = wp.zeros(batch_count, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch( - tile_sum_kernel, - dim=[batch_count, TILE_DIM], - inputs=[input_wp, output_wp], - block_dim=TILE_DIM, - device=device, - ) + wp.launch_tiled(tile_sum_kernel, dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device) sum_wp = output_wp.numpy() for i in range(batch_count): @@ -65,8 +59,9 @@ def test_tile_reduce_sum(test, device): @wp.kernel -def tile_reduce_1d_kernel(output: wp.array(dtype=int)): - # output tile index +def tile_reduce_simt_kernel(output: wp.array(dtype=int)): + + # thread index i = wp.tid() t = wp.tile(i) # convert to block wide tile @@ -76,14 +71,14 @@ def tile_reduce_1d_kernel(output: wp.array(dtype=int)): wp.tile_atomic_add(output, 0, 0, s) -def test_tile_reduce_1d(test, device): +def test_tile_reduce_simt(test, device): # use an unaligned grid dimension N = int(TILE_DIM * 3 / 2) output = wp.zeros(shape=1, dtype=int, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch(tile_reduce_1d_kernel, dim=[N], inputs=[output], block_dim=TILE_DIM, device=device) + wp.launch(tile_reduce_simt_kernel, dim=N, inputs=[output], block_dim=TILE_DIM, device=device) test.assertEqual(output.numpy()[0], np.sum(np.arange(N))) @@ -102,7 +97,7 @@ def test_tile_ones(test, device): output = wp.zeros(shape=1, dtype=float, device=device) with wp.Tape() as tape: - wp.launch(tile_ones_kernel, dim=[1, TILE_DIM], inputs=[output], block_dim=TILE_DIM, device=device) + wp.launch_tiled(tile_ones_kernel, dim=[1], inputs=[output], block_dim=TILE_DIM, device=device) test.assertAlmostEqual(output.numpy()[0], 256.0) @@ -126,7 +121,7 @@ def test_tile_arange(test, device): output = wp.zeros(shape=(3, N), dtype=int, device=device) with wp.Tape() as tape: - wp.launch(tile_arange_kernel, dim=[1, N], inputs=[output], block_dim=TILE_DIM, device=device) + wp.launch_tiled(tile_arange_kernel, dim=[1], inputs=[output], block_dim=TILE_DIM, device=device) assert_np_equal(output.numpy()[0], np.arange(17)) assert_np_equal(output.numpy()[1], np.arange(5, 22)) @@ -141,7 +136,7 @@ class TestTileReduce(unittest.TestCase): add_function_test(TestTileReduce, "test_tile_reduce_sum", test_tile_reduce_sum, devices=devices) -add_function_test(TestTileReduce, "test_tile_reduce_1d", test_tile_reduce_1d, devices=devices) +add_function_test(TestTileReduce, "test_tile_reduce_simt", test_tile_reduce_simt, devices=devices) add_function_test(TestTileReduce, "test_tile_ones", test_tile_ones, devices=devices) add_function_test(TestTileReduce, "test_tile_arange", test_tile_arange, devices=devices) From 0d795e76f0bf86dddac25f107cc7981594a23dd0 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 27 Sep 2024 10:07:59 +0000 Subject: [PATCH 040/102] Add first pass at documentation section for tiles --- docs/index.rst | 1 + docs/modules/functions.rst | 2 +- docs/modules/tiles.rst | 165 +++++++++++++++++++++++++++++++++++++ warp/builtins.py | 2 +- warp/stubs.py | 2 +- 5 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 docs/modules/tiles.rst diff --git a/docs/index.rst b/docs/index.rst index e3f45fa0..ac324f32 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -351,6 +351,7 @@ Full Table of Contents modules/devices modules/differentiability modules/generics + modules/tiles modules/interoperability configuration debugging diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 4f328880..45a79f07 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -1022,7 +1022,7 @@ Tile Primitives * vec2h, vec2f, vec2d (complex) All input and output tiles must have the same datatype. Tile data will be automatically be migrated - to shared memory if necessary and will use TensoreCore operations when available. + to shared memory if necessary and will use TensorCore operations when available. :param a: A tile with ``shape=(M, K)`` :param b: A tile with ``shape=(K, N)`` diff --git a/docs/modules/tiles.rst b/docs/modules/tiles.rst new file mode 100644 index 00000000..bf7f40bb --- /dev/null +++ b/docs/modules/tiles.rst @@ -0,0 +1,165 @@ +Tiles (Preview) +=============== + +Block-based programming models such as those in OpenAI Triton have proved to be effective ways of expressing high performance kernels that can leverage cooperative operations on modern GPUs. + +Warp 1.4.0 introduces tile extensions that expose a block-based programming to Warp kernels. + +Execution Model +--------------- + +Warp's execution model allows users to specify an up to 4-dimensional grid of logical threads for kernel execution at launch time. With the introduction of tiles, users can also specify a block size, which partitions the grid into smaller sets of threads that are executed on a single compute unit. + +Inside kernels, tile operations are executed cooperatively across each block of threads, allowing them to take advantage of efficient memory access, local memory, and dedicated hardware units like TensorCores. + +As an example, consider the following kernel: + +.. code:: python + + TILE_SIZE = wp.constant(256) + TILE_THREADS = 64 + + @wp.kernel + def compute(a: array(dtype=float)) + i = wp.tid()/TILE_SIZE + + t = wp.tile_load(array, x=i, n=TILE_SIZE) + ... + + wp.launch(compute, dim=[len(a)], inputs=[a], block_dim=TILE_THREADS) + +Here, we load a 1D tile of 256 values from a global memory array ``a``, where the load operation is performed cooperatively by all 64 threads in the block, as specified by the ``block_dim`` argument to :func:`warp.launch`. In this case each thread is responsible for loading 4 values from global memory, which may then be stored in registers, or shared memory across the block. + +Tile Properties +--------------- + +In Warp, tile objects are 2D arrays of data where the tile elements may be scalars, vectors, matrices, or user defined structures. + +In a more complex example, we launch a grid of threads where each block is responsible for loading a row of data from a 2D array and computing its sum: + +.. code:: python + + TILE_SIZE = wp.constant(256) + TILE_THREADS = 64 + + @wp.kernel + def compute(a: array2d(dtype=float)) + i, _= wp.tid() + + # load a row from global memory + t = wp.tile_load(array, i, TILE_SIZE) + s = wp.sum(t) + ... + + wp.launch(compute, dim=[a.shape[0], TILE_THREADS], inputs=[a], block_dim=TILE_THREADS) + +Here, we launch a 2D grid of threads where the trailing dimension is equal to the block size. This ensures we have an entire block of threads dedicated to each row. Each block then loads an entire row of 256 values from the global memory array and computes its sum. + +To streamline this common pattern Warp provides a helper ``wp.tiled_launch()`` which takes care of adding the trailing tile dimension to the thread grid, for example, to assign a block of 64 threads to load and sum a 2D array of values we can do the following: + +.. code:: python + + TILE_M = wp.constant(16) + TILE_N = wp.constant(16) + TILE_THREADS = 64 + + @wp.kernel + def compute(a: array2d(dtype=float)) + i, j = wp.tid() + + # load a row from global memory + t = wp.tile_load(array, i, j, TILE_M, TILE_N) + s = wp.sum(t) + ... + + wp.launch_tiled(compute, dim=[a.shape[0]/TILE_M, a.shape[1]/TILE_N], inputs=[a], block_dim=TILE_THREADS) + +In this example, we use :func:`warp.launch_tiled` to automatically insert the trailing dimension, and assign ``TILE_THREADS`` to each 2D tile of the array. Each tile consists of ``16*16=256`` values, which are loaded cooperatively by the 64 threads in each block. + +Tile Storage +------------ + +When tiles are created they are placed in either `register` or `shared` memory. In general Warp tries to determine the best storage for each, the default is generally for register storage, although some operations such as matrix multiplies may migrate data from register to shared as necessary. + +Register Tiles +++++++++++++++ + +Values in register tiles are stored across the entire block, for example, if the block dimension at launch is set to 64, a register tile with ``shape=(1, 256)`` will result in each thread storing 4 elements. Reigster based storage is the fastest storage on most hardware, however, because the tile storage is spread across the threads in the block, an individual thread cannot randomly access data that is assigned to another thread efficiently. For this reason operations on tiles tend to expressed as higher level maps, reductions, and reshaping operations that may transfer values through shared memory. + +Shared Memory Tiles ++++++++++++++++++++ + +Some operations like matrix multiplication, require access to an entire tile of values. In this case the tile data may stored in shared memory, which allows efficient random access. Warp will automatically migrate tiles to shared memory as necessary for specific operations. Shared memory is a limited resource, and so tile size must be set appropriately to avoid exceeding the hardware limitations, otherwise kernel compilation may fail. + +Tile Operations +--------------- + +Creation +++++++++ + +* :func:`warp.tile_zeros` +* :func:`warp.tile_ones` +* :func:`warp.tile_arange` + +Conversion +++++++++++ + +* :func:`warp.tile` +* :func:`warp.untile` + + +Load/Store +++++++++++ + +* :func:`warp.tile_load` +* :func:`warp.tile_store` +* :func:`warp.tile_atomic_add` + +Maps/Reductions ++++++++++++++++ + +* :func:`warp.tile_map` +* :func:`warp.tile_sum` + +Linear Algebra +++++++++++++++ + +* :func:`warp.tile_matmul` +* :func:`warp.tile_fft` +* :func:`warp.tile_ifft` + +Tiles and SIMT Code ++++++++++++++++++++ + +Warp kernels are primarily written in the SIMT programming model in mind, where each thread's execution happens completely independently. Tiles on the other hand allow threads to work cooperatively to perform operations. + +Warp aims to give users a way to seamlessly integrate tile operations with existing SIMT code. To this end, we expose two operations, :func:`warp.tile`, and :func:`warp.untile` which can be used as follows: + +.. code:: python + + TILE_THREADS = 64 + + @wp.kernel + def compute() + i = wp.tid() + + # perform some per-thread computation + x = i*2.0 + wp.sin(float(i)) + + # tile the value x across the block + # returns a tile with shape=(1, TILE_THREADS) + t = wp.tile(x) + ... + + # launch as regular SIMT kernel + wp.launch(compute, dim=[N], inputs=[], block_dim=TILE_THREADS) + +In this example we perform some per-thread computations, and then convert the scalar ``x`` value into a tile object using the :func:`warp.tile` function. This function takes a single value as input, and returns a tile with the same dimensions as the number of threads in the block. From here, the tile can used in other regular cooperative operations such as reductions, GEMMs, etc. + +Similarly, we can `untile` tile objects back to their per-thread scalar equivalent values. + + + + + + diff --git a/warp/builtins.py b/warp/builtins.py index bcc3a573..b91f6dd2 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5192,7 +5192,7 @@ def make_transpose(t): * vec2h, vec2f, vec2d (complex) All input and output tiles must have the same datatype. Tile data will be automatically be migrated - to shared memory if necessary and will use TensoreCore operations when available. + to shared memory if necessary and will use TensorCore operations when available. :param a: A tile with ``shape=(M, K)`` :param b: A tile with ``shape=(K, N)`` diff --git a/warp/stubs.py b/warp/stubs.py index a0e38e91..2e5b4bf9 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -2595,7 +2595,7 @@ def tile_matmul(a: Tile, b: Tile, out: Tile) -> Tile: * vec2h, vec2f, vec2d (complex) All input and output tiles must have the same datatype. Tile data will be automatically be migrated - to shared memory if necessary and will use TensoreCore operations when available. + to shared memory if necessary and will use TensorCore operations when available. :param a: A tile with ``shape=(M, K)`` :param b: A tile with ``shape=(K, N)`` From ebd29e03ef5e2a79b20777fd6ac260849bf74ce6 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Fri, 27 Sep 2024 09:54:33 -0700 Subject: [PATCH 041/102] Various formatting and test updates --- docs/modules/functions.rst | 8 ++-- warp/builtins.py | 16 ++++--- warp/context.py | 5 ++- warp/tests/test_misc.py | 63 -------------------------- warp/tests/test_tile.py | 80 +++++++++++++++------------------- warp/tests/test_tile_mathdx.py | 7 +-- warp/tests/test_tile_reduce.py | 7 +-- 7 files changed, 58 insertions(+), 128 deletions(-) delete mode 100644 warp/tests/test_misc.py diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 45a79f07..0e3ec3de 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -1021,9 +1021,9 @@ Tile Primitives * fp16, fp32, fp64 (real) * vec2h, vec2f, vec2d (complex) - All input and output tiles must have the same datatype. Tile data will be automatically be migrated + All input and output tiles must have the same datatype. Tile data will be automatically be migrated to shared memory if necessary and will use TensorCore operations when available. - + :param a: A tile with ``shape=(M, K)`` :param b: A tile with ``shape=(K, N)`` :param out: A tile with ``shape=(M, N)`` @@ -1033,7 +1033,7 @@ Tile Primitives .. py:function:: tile_fft(inout: Tile) -> Tile Compute the forward FFT along the second dimension of a 2D tile of data. - + This function cooperatively computes the forward FFT on a tile of data inplace, treating each row individually. Supported datatypes are: @@ -1045,7 +1045,7 @@ Tile Primitives .. py:function:: tile_ifft(inout: Tile) -> Tile Compute the inverse FFT along the second dimension of a 2D tile of data. - + This function cooperatively computes the inverse FFT on a tile of data inplace, treating each row individually. Supported datatypes are: diff --git a/warp/builtins.py b/warp/builtins.py index b91f6dd2..b5994685 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5054,7 +5054,11 @@ def tile_matmul_generic_value_func(arg_types, arg_values): def tile_matmul_generic_lto_dispatch_func( - arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var], options: Mapping[str, Any], builder: warp.context.ModuleBuilder + arg_types: Mapping[str, type], + return_type: Any, + arg_values: Mapping[str, Var], + options: Mapping[str, Any], + builder: warp.context.ModuleBuilder, ): a = arg_values["a"] b = arg_values["b"] @@ -5157,7 +5161,7 @@ def make_transpose(t): with open(lto_code.name, "rb") as f: lto_code = f.read() - builder.ltoirs[lto_symbol] = lto_code + builder.ltoirs[lto_symbol] = lto_code return lto_symbol, lto_code (fun_forward, lto_forward) = make_function(M, N, K, "N", "N") # C += A * B @@ -5191,9 +5195,9 @@ def make_transpose(t): * fp16, fp32, fp64 (real) * vec2h, vec2f, vec2d (complex) - All input and output tiles must have the same datatype. Tile data will be automatically be migrated + All input and output tiles must have the same datatype. Tile data will be automatically be migrated to shared memory if necessary and will use TensorCore operations when available. - + :param a: A tile with ``shape=(M, K)`` :param b: A tile with ``shape=(K, N)`` :param out: A tile with ``shape=(M, N)`` @@ -5317,7 +5321,7 @@ def tile_fft_generic_lto_dispatch_func( lto_dispatch_func=functools.partial(tile_fft_generic_lto_dispatch_func, direction="forward"), variadic=True, doc="""Compute the forward FFT along the second dimension of a 2D tile of data. - + This function cooperatively computes the forward FFT on a tile of data inplace, treating each row individually. Supported datatypes are: @@ -5336,7 +5340,7 @@ def tile_fft_generic_lto_dispatch_func( lto_dispatch_func=functools.partial(tile_fft_generic_lto_dispatch_func, direction="inverse"), variadic=True, doc="""Compute the inverse FFT along the second dimension of a 2D tile of data. - + This function cooperatively computes the inverse FFT on a tile of data inplace, treating each row individually. Supported datatypes are: diff --git a/warp/context.py b/warp/context.py index efcf7fd6..ff92f0e3 100644 --- a/warp/context.py +++ b/warp/context.py @@ -1541,7 +1541,7 @@ def __init__(self, module, options, hasher=None): self.options = options self.module = module self.deferred_functions = [] - self.ltoirs = {} # map from lto symbol to lto binary + self.ltoirs = {} # map from lto symbol to lto binary if hasher is None: hasher = ModuleHasher(module) @@ -5176,6 +5176,7 @@ def pack_args(args, params, adjoint=False): if warp.config.verify_autograd_array_access: runtime.tape._check_kernel_array_access(kernel, fwd_args) + def launch_tiled(*args, **kwargs): """A helper method for launching a grid with an extra trailing dimension equal to the block size. @@ -5216,7 +5217,7 @@ def compute() # forward to original launch method launch(*args, **kwargs) - + def synchronize(): """Manually synchronize the calling CPU thread with any outstanding CUDA work on all devices diff --git a/warp/tests/test_misc.py b/warp/tests/test_misc.py deleted file mode 100644 index de9e5fc4..00000000 --- a/warp/tests/test_misc.py +++ /dev/null @@ -1,63 +0,0 @@ -import numpy as np -import warp as wp - -wp.clear_kernel_cache() - -TILE_M = wp.constant(8) -TILE_N = wp.constant(4) -TILE_K = wp.constant(8) - -# num threads per-tile -TILE_DIM = 64 - - -@wp.kernel -def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), C: wp.array3d(dtype=float)): - # output tile index - i = wp.tid() - - a = wp.tile_load(A[i], 0, 0, m=TILE_M, n=TILE_K) - b = wp.tile_load(B[i], 0, 0, m=TILE_K, n=TILE_N) - - print(a) - print(b) - - # sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) - - # wp.tile_matmul(a, b, sum) - - # print(sum) - - # wp.tile_store(C[i], 0, 0, sum) - - -batch_count = 1 - -M = TILE_M -N = TILE_N -K = TILE_K - -device = "cuda:0" - -rng = np.random.default_rng(42) -A = rng.random((batch_count, M, K), dtype=np.float32) -B = rng.random((batch_count, K, N), dtype=np.float32) -C = A @ B - -A_wp = wp.array(A, requires_grad=True, device=device) -B_wp = wp.array(B, requires_grad=True, device=device) -C_wp = wp.zeros((batch_count, TILE_M, TILE_N), requires_grad=True, device=device) - -with wp.Tape() as tape: - wp.launch(tile_grouped_gemm, - dim=[batch_count, TILE_DIM], - inputs=[A_wp, B_wp, C_wp], - block_dim=TILE_DIM, - device=device) - -wp.synchronize() - -# TODO: 32 mismatched elements -#assert_np_equal(C_wp.numpy(), C) -#print(C_wp.numpy()) - diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index f757be22..8aba083e 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -12,6 +12,8 @@ import warp as wp from warp.tests.unittest_utils import * +wp.init() # For wp.context.runtime.core.is_mathdx_enabled() + TILE_M = wp.constant(8) TILE_N = wp.constant(4) TILE_K = wp.constant(8) @@ -167,22 +169,22 @@ def test_tile_binary_map(test, device): assert_np_equal(B_wp.grad.numpy(), B_grad) -@wp.kernel -def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), C: wp.array3d(dtype=float)): - # output tile index - i = wp.tid() - - a = wp.tile_load(A[i], 0, 0, m=TILE_M, n=TILE_K) - b = wp.tile_load(B[i], 0, 0, m=TILE_K, n=TILE_N) +@unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") +def test_tile_grouped_gemm(test, device): + @wp.kernel + def tile_grouped_gemm(A: wp.array3d(dtype=float), B: wp.array3d(dtype=float), C: wp.array3d(dtype=float)): + # output tile index + i = wp.tid() - sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + a = wp.tile_load(A[i], 0, 0, m=TILE_M, n=TILE_K) + b = wp.tile_load(B[i], 0, 0, m=TILE_K, n=TILE_N) - wp.tile_matmul(a, b, sum) + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) - wp.tile_store(C[i], 0, 0, sum) + wp.tile_matmul(a, b, sum) + wp.tile_store(C[i], 0, 0, sum) -def test_tile_grouped_gemm(test, device): batch_count = 56 M = TILE_M @@ -199,40 +201,38 @@ def test_tile_grouped_gemm(test, device): C_wp = wp.zeros((batch_count, TILE_M, TILE_N), requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch_tiled(tile_grouped_gemm, - dim=[batch_count], - inputs=[A_wp, B_wp, C_wp], - block_dim=TILE_DIM, - device=device) + wp.launch_tiled( + tile_grouped_gemm, dim=[batch_count], inputs=[A_wp, B_wp, C_wp], block_dim=TILE_DIM, device=device + ) # TODO: 32 mismatched elements assert_np_equal(C_wp.numpy(), C) -@wp.kernel -def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): - # output tile index - i, j = wp.tid() +@unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") +def test_tile_gemm(test, device): + @wp.kernel + def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): + # output tile index + i, j = wp.tid() - sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) - M = A.shape[0] - N = B.shape[1] - K = A.shape[1] + M = A.shape[0] + N = B.shape[1] + K = A.shape[1] - count = int(K / TILE_K) + count = int(K / TILE_K) - for k in range(0, count): - a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) - b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) + for k in range(0, count): + a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) + b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) - # sum += a*b - wp.tile_matmul(a, b, sum) + # sum += a*b + wp.tile_matmul(a, b, sum) - wp.tile_store(C, i, j, sum) + wp.tile_store(C, i, j, sum) - -def test_tile_gemm(test, device): M = TILE_M * 7 K = TILE_K * 6 N = TILE_N * 5 @@ -302,11 +302,8 @@ def test_tile_operators(test, device): with wp.Tape() as tape: wp.launch_tiled( - tile_operators, - dim=[batch_count], - inputs=[input_wp, output_wp], - block_dim=TILE_DIM, - device=device) + tile_operators, dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device + ) assert_np_equal(output_wp.numpy(), output) @@ -387,12 +384,7 @@ def test_tile_extract(test, device): output_wp = wp.zeros_like(input_wp, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch_tiled( - tile_extract_kernel, - dim=[1], - inputs=[input_wp, output_wp], - block_dim=TILE_DIM, - device=device) + wp.launch_tiled(tile_extract_kernel, dim=[1], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device) assert_array_equal(output_wp, input_wp) diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py index 92e97ff0..50b71404 100644 --- a/warp/tests/test_tile_mathdx.py +++ b/warp/tests/test_tile_mathdx.py @@ -91,12 +91,7 @@ def test_tile_math_fft(test, device): Y_c64 = np.fft.fft(X_c64, axis=-1) with wp.Tape() as tape: - wp.launch_tiled( - tile_math_fft_kernel, - dim=[1, 1], - inputs=[X_wp, Y_wp], - block_dim=TILE_DIM, - device=device) + wp.launch_tiled(tile_math_fft_kernel, dim=[1, 1], inputs=[X_wp, Y_wp], block_dim=TILE_DIM, device=device) Y_wp_c64 = Y_wp.numpy().view(np.complex64).reshape(N_FFT, N_FFT) diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index 3f65b7cf..22578de8 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -21,7 +21,7 @@ @wp.kernel -def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): +def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): # output tile index i = wp.tid() @@ -44,7 +44,9 @@ def test_tile_reduce_sum(test, device): output_wp = wp.zeros(batch_count, requires_grad=True, device=device) with wp.Tape() as tape: - wp.launch_tiled(tile_sum_kernel, dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device) + wp.launch_tiled( + tile_sum_kernel, dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device + ) sum_wp = output_wp.numpy() for i in range(batch_count): @@ -60,7 +62,6 @@ def test_tile_reduce_sum(test, device): @wp.kernel def tile_reduce_simt_kernel(output: wp.array(dtype=int)): - # thread index i = wp.tid() From ecb578f8bca1062a5bb36c64c11c375e91ad0bed Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 27 Sep 2024 21:10:39 +0000 Subject: [PATCH 042/102] Fix for partial warps using `wp.tile_sum()` --- warp/native/tile_reduce.h | 2 +- warp/tests/test_tile_reduce.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index 1f618f6d..047177b9 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -30,7 +30,7 @@ inline CUDA_CALLABLE T warp_shuffle_down(T val, int offset) Word* src = reinterpret_cast(&input); unsigned int shuffle_word; - unsigned int mask = 0xffffffff; + unsigned int mask = __activemask(); constexpr int word_count = (sizeof(T) + sizeof(Word) - 1) / sizeof(Word); diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index 3f65b7cf..9d868be2 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -72,8 +72,9 @@ def tile_reduce_simt_kernel(output: wp.array(dtype=int)): def test_tile_reduce_simt(test, device): + # use an unaligned grid dimension - N = int(TILE_DIM * 3 / 2) + N = TILE_DIM*4 + 5 output = wp.zeros(shape=1, dtype=int, requires_grad=True, device=device) From c913ad79366448f34becdc1f85ebbff27b98e9e6 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 27 Sep 2024 23:46:43 +0000 Subject: [PATCH 043/102] Handle partial warp case for tile_sum --- warp/native/tile_reduce.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index 047177b9..efa6ab4f 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -8,7 +8,7 @@ namespace wp { template -inline CUDA_CALLABLE T warp_shuffle_down(T val, int offset) +inline CUDA_CALLABLE T warp_shuffle_down(T val, int offset, int mask) { typedef unsigned int Word; @@ -30,7 +30,6 @@ inline CUDA_CALLABLE T warp_shuffle_down(T val, int offset) Word* src = reinterpret_cast(&input); unsigned int shuffle_word; - unsigned int mask = __activemask(); constexpr int word_count = (sizeof(T) + sizeof(Word) - 1) / sizeof(Word); @@ -49,9 +48,25 @@ inline CUDA_CALLABLE T warp_reduce_sum(T val) { T sum = val; - for (int offset=WP_TILE_WARP_SIZE/2; offset > 0; offset /= 2) + unsigned int mask = __activemask(); + + if (mask == 0xFFFFFFFF) + { + // handle case where entire warp is active + for (int offset=WP_TILE_WARP_SIZE/2; offset > 0; offset /= 2) + { + sum += warp_shuffle_down(sum, offset, mask); + } + } + else { - sum += warp_shuffle_down(sum, offset); + // handle partial warp case + for (int offset=WP_TILE_WARP_SIZE/2; offset > 0; offset /= 2) + { + T shfl_val = warp_shuffle_down(sum, offset, mask); + if ((mask & (1 << ((threadIdx.x + offset)%WP_TILE_WARP_SIZE))) != 0) + sum += shfl_val; + } } return sum; From d5909f69e1d12689e8c8a5c166942f178eee19eb Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Sat, 28 Sep 2024 02:07:03 +0000 Subject: [PATCH 044/102] Fix for uninitialized partial reduction results --- warp/native/tile_reduce.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index efa6ab4f..f14ff70c 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -113,9 +113,18 @@ auto tile_sum(Tile& t) // fixed size scratch pad for partial results in shared memory WP_TILE_SHARED T partials[warp_count]; + // count of active warps + WP_TILE_SHARED int active_warps; + if (threadIdx.x == 0) + active_warps = 0; + + // ensure active_warps is initialized + WP_TILE_SYNC(); + if (lane_index == 0) { partials[warp_index] = warp_sum; + atomicAdd(&active_warps, 1); } // ensure partials are ready @@ -127,7 +136,7 @@ auto tile_sum(Tile& t) T block_sum = partials[0]; WP_PRAGMA_UNROLL - for (int i=1; i < warp_count; ++i) + for (int i=1; i < active_warps; ++i) block_sum += partials[i]; output.data[0] = block_sum; From 114a6b501bd77d1ec2037f60d557ea4c657f6300 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Sat, 28 Sep 2024 09:40:16 +0000 Subject: [PATCH 045/102] Add wp.untile() Make wp.tile_zeros() use register storage by default --- docs/modules/functions.rst | 40 +++++++++++++++++++ warp/builtins.py | 73 +++++++++++++++++++++++++++++++--- warp/native/tile.h | 23 ++++++++--- warp/stubs.py | 43 ++++++++++++++++++++ warp/tests/test_tile_reduce.py | 28 ++++++++++++- warp/types.py | 2 +- 6 files changed, 195 insertions(+), 14 deletions(-) diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 0e3ec3de..f33d4339 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -902,6 +902,46 @@ Tile Primitives +.. py:function:: untile(a: Any) -> Scalar + + Convert a Tile back to per-thread values. + + This function converts a block-wide tile back to per-thread values. + + :param a: A tile with dimensions ``shape=(1, block_dim)`` + :returns: A single value per-thread with the same dtype as the tile + + This example shows how to create a linear sequence from thread variables: + + .. code-block:: python + + @wp.kernel + def compute(): + i = wp.tid() + + # create block-wide tile + t = wp.tile(i)*2 + + # convert back to per-thread values + s = wp.untile() + + print(s) + + wp.launch(compute, dim=16, inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + 0 + 2 + 4 + 6 + 8 + ... + + + .. py:function:: tile_extract(a: Tile, i: int32, j: int32) -> Scalar Extracts a single element from the tile and returns it as a scalar type. diff --git a/warp/builtins.py b/warp/builtins.py index b5994685..063fbbd6 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -2083,6 +2083,73 @@ def compute(): ) +def untile_value_func(arg_types, arg_values): + # return generic type (for doc builds) + if arg_types is None: + return Scalar + + if len(arg_types) != 1: + raise RuntimeError("untile() requires 1 positional arg") + + t = arg_types["a"] + + if not is_tile(t): + raise RuntimeError(f"untile() accepts arguments of type tile only, got {arg_types[0]}") + + if t.N != warp.codegen.options["block_dim"]: + raise RuntimeError( + f"until() argument must have the same length as the block width, got {t.N}, expected {warp.codegen.options['block_dim']}" + ) + + return t.dtype + + +add_builtin( + "untile", + input_types={"a": Any}, + value_func=untile_value_func, + variadic=True, + doc="""Convert a Tile back to per-thread values. + + This function converts a block-wide tile back to per-thread values. + + :param a: A tile with dimensions ``shape=(M, block_dim)`` + :returns: A single value per-thread with the same dtype as the tile + + This example shows how to create a linear sequence from thread variables: + + .. code-block:: python + + @wp.kernel + def compute(): + i = wp.tid() + + # create block-wide tile + t = wp.tile(i)*2 + + # convert back to per-thread values + s = wp.untile() + + print(s) + + wp.launch(compute, dim=16, inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + 0 + 2 + 4 + 6 + 8 + ... + """, + group="Tile Primitives" "", + export=False, +) + + def tile_extract_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -2132,9 +2199,6 @@ def tile_matmul_value_func(arg_types, arg_values): if not isinstance(arg_types["out"], Tile): raise RuntimeError("tile_matmul() output argument must be a tile") - if arg_types["out"].storage != "shared": - raise RuntimeError("tile_matmul() output argument must have shared memory storage") - return None @@ -5047,9 +5111,6 @@ def tile_matmul_generic_value_func(arg_types, arg_values): if not isinstance(arg_types["out"], Tile): raise RuntimeError("tile_matmul() output argument must be a tile") - if arg_types["out"].storage != "shared": - raise RuntimeError("tile_matmul() output argument must have shared memory storage") - return None diff --git a/warp/native/tile.h b/warp/native/tile.h index f3b1eea5..6f6ad654 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -647,28 +647,41 @@ inline CUDA_CALLABLE auto tile(const T& x) { tile_register_t result; - // code-gen should have set the tile to - // have exactly the block dimension so - // there is exactly one value per-thread static_assert(result.NumRegs == 1); result.data[0] = x; return result; } + // construct a tile from a local SIMT value (one per-thread) template inline CUDA_CALLABLE void adj_tile(const T& x, T& adj_x, const AdjTile& adj_ret) { static_assert(AdjTile::M == 1); static_assert(AdjTile::N == WP_TILE_BLOCK_DIM); + static_assert(AdjTile::NumRegs == 1); + + adj_x += adj_ret.data[0]; +} +template +inline CUDA_CALLABLE auto untile(Tile& tile) +{ // code-gen should have set the tile to // have exactly the block dimension so // there is exactly one value per-thread - static_assert(AdjTile::NumRegs == 1); + static_assert(Tile::NumRegs == 1); - adj_x += adj_ret.data[0]; + return tile.copy_to_register().data[0]; +} + +template +inline CUDA_CALLABLE void adj_untile(Tile& tile, Tile& adj_tile, typename Tile::Type& adj_ret) +{ + auto adj = adj_tile.copy_to_register(); + adj.data[0] += adj_ret; + adj_tile.assign(adj); } // zero initialized tile diff --git a/warp/stubs.py b/warp/stubs.py index 2e5b4bf9..fc642827 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -1002,6 +1002,49 @@ def compute(): ... +@over +def untile(a: Any) -> Scalar: + """Convert a Tile back to per-thread values. + + This function converts a block-wide tile back to per-thread values. + + :param a: A tile with dimensions ``shape=(M, block_dim)`` + :returns: A single value per-thread with the same dtype as the tile + + This example shows how to create a linear sequence from thread variables: + + .. code-block:: python + + @wp.kernel + def compute(): + i = wp.tid() + + # create block-wide tile + t = wp.tile(i) * 2 + + # convert back to per-thread values + s = wp.untile() + + print(s) + + + wp.launch(compute, dim=16, inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + 0 + 2 + 4 + 6 + 8 + ... + + """ + ... + + @over def tile_extract(a: Tile, i: int32, j: int32) -> Scalar: """Extracts a single element from the tile and returns it as a scalar type. diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index 84be9e6b..723ab12f 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -73,9 +73,8 @@ def tile_reduce_simt_kernel(output: wp.array(dtype=int)): def test_tile_reduce_simt(test, device): - # use an unaligned grid dimension - N = TILE_DIM*4 + 5 + N = TILE_DIM * 4 + 5 output = wp.zeros(shape=1, dtype=int, requires_grad=True, device=device) @@ -85,6 +84,30 @@ def test_tile_reduce_simt(test, device): test.assertEqual(output.numpy()[0], np.sum(np.arange(N))) +@wp.kernel +def tile_untile_kernel(output: wp.array(dtype=int)): + # thread index + i = wp.tid() + + # convert to block wide tile + t = wp.tile(i) * 2 + s = wp.untile(t) + + output[i] = s + + +def test_tile_untile(test, device): + # use an unaligned grid dimension + N = TILE_DIM * 4 + 5 + + output = wp.zeros(shape=N, dtype=int, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch(tile_untile_kernel, dim=N, inputs=[output], block_dim=TILE_DIM, device=device) + + assert_np_equal(output.numpy(), np.arange(N) * 2) + + @wp.kernel def tile_ones_kernel(out: wp.array(dtype=float)): i = wp.tid() @@ -141,6 +164,7 @@ class TestTileReduce(unittest.TestCase): add_function_test(TestTileReduce, "test_tile_reduce_simt", test_tile_reduce_simt, devices=devices) add_function_test(TestTileReduce, "test_tile_ones", test_tile_ones, devices=devices) add_function_test(TestTileReduce, "test_tile_arange", test_tile_arange, devices=devices) +add_function_test(TestTileReduce, "test_tile_untile", test_tile_untile, devices=devices) if __name__ == "__main__": wp.clear_kernel_cache() diff --git a/warp/types.py b/warp/types.py index 7dc725fb..3cae1be3 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2999,7 +2999,7 @@ def alloc(cls): class TileZeros(Tile): def __init__(self, dtype, M, N): - Tile.__init__(self, dtype, M, N, op="zeros", storage="shared") + Tile.__init__(self, dtype, M, N, op="zeros", storage="register") class TileRange(Tile): From 6e9cee09a12ac31c080d1ef9508905b197bd6e83 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Sun, 29 Sep 2024 04:00:22 +0000 Subject: [PATCH 046/102] Add wp.tile_min() Add wp.tile_max() Add wp.tile_reduce() --- warp/builtins.py | 188 ++++++++++++++++++++++++++++++++- warp/codegen.py | 14 --- warp/native/tile.h | 26 +++-- warp/native/tile_reduce.h | 135 ++++++++++------------- warp/tests/test_tile_reduce.py | 180 ++++++++++++++++++++++++++++++- 5 files changed, 430 insertions(+), 113 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 063fbbd6..6771d22a 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -2280,6 +2280,167 @@ def compute(): export=False, ) +def tile_min_value_func(arg_types, arg_values): + # return generic type (for doc builds) + if arg_types is None: + return Tile(dtype=Any, M=1, N=1) + + if len(arg_types) != 1: + raise RuntimeError("tile_min() requires 1 positional args") + + a = arg_types["a"] + + if not is_tile(a): + raise RuntimeError("tile_min() argument 0 must be a tile") + + return Tile(dtype=a.dtype, M=1, N=1, op="min") + + +add_builtin( + "tile_min", + input_types={"a": Tile}, + value_func=tile_min_value_func, + variadic=True, + doc="""Cooperatively compute the minimum of the tile elements using all threads in the block. + + :param a: The tile to compute the minimum of + :returns: A single element tile with dimensions of (1,1) holding the minimum value + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_arange(start=--10, stop=10, dtype=float) + s = wp.tile_min(t) + + print(t) + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[-10]] + + """, + group="Tile Primitives", + export=False, +) + +def tile_max_value_func(arg_types, arg_values): + # return generic type (for doc builds) + if arg_types is None: + return Tile(dtype=Any, M=1, N=1) + + if len(arg_types) != 1: + raise RuntimeError("tile_max() requires 1 positional args") + + a = arg_types["a"] + + if not is_tile(a): + raise RuntimeError("tile_max() argument 0 must be a tile") + + return Tile(dtype=a.dtype, M=1, N=1, op="min") + + +add_builtin( + "tile_max", + input_types={"a": Tile}, + value_func=tile_max_value_func, + variadic=True, + doc="""Cooperatively compute the maximum of the tile elements using all threads in the block. + + :param a: The tile to compute the maximum from + :returns: A single element tile with dimensions of (1,1) holding the maximum value + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_arange(start=--10, stop=10, dtype=float) + s = wp.tile_min(t) + + print(t) + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[10]] + + """, + group="Tile Primitives", + export=False, +) + +# does type propagation for load() +def tile_reduce_value_func(arg_types, arg_values): + if arg_types is None: + return Tile(dtype=Any, M=Any, N=Any) + + a = arg_types["a"] + + # check all args are tiles + if not is_tile(a): + raise RuntimeError(f"tile_reduce() arguments must be tiles, got type {a}") + + return Tile(dtype=a.dtype, M=1, N=1, op="reduce") + + +def tile_reduce_dispatch_func(input_types: Mapping[str, type], return_type: Any, args: Mapping[str, Var]): + func_args = (args["op"], *args["args"]) + template_args = () + return (func_args, template_args) + + +add_builtin( + "tile_reduce", + input_types={"op": Callable, "a": Any}, + value_func=tile_reduce_value_func, + native_func="tile_reduce", + doc="""Apply a custom reduction operator across the tile. + + This function cooperatively performs a reduction using the provided operator across the tile. + + :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin + :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A single element tile with ``shape=(1,1)`` with the same datatype as the input tile. + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_arange(1, 10, dtype=int) + s = wp.tile_reduce(wp.prod, t) + + print(s) + + wp.launch(compute, dim=[16], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[362880]] + """, + group="Tile Primitives", + export=False, +) + +# maps + # does type propagation for load() def tile_unary_map_value_func(arg_types, arg_values): @@ -2356,7 +2517,7 @@ def tile_binary_map_value_func(arg_types, arg_values): raise RuntimeError(f"tile_map() arguments must be tiles, got type {b}") # use first argument to define output type - if a.dtype != b.dtype: + if not types_equal(a.dtype, b.dtype): raise RuntimeError(f"tile_map() arguments must all have the same type {a.dtype} != {b.dtype}") if a.M != b.M: @@ -5108,7 +5269,7 @@ def tile_matmul_generic_value_func(arg_types, arg_values): if not is_tile(arg_types["b"]): raise RuntimeError("tile_matmul() argument 1 must be an tile") - if not isinstance(arg_types["out"], Tile): + if not is_tile(arg_types["out"]): raise RuntimeError("tile_matmul() output argument must be a tile") return None @@ -5268,6 +5429,29 @@ def make_transpose(t): namespace="", ) +add_builtin( + "tile_matmul", + input_types={"a": Tile, "b": Tile}, + value_func=tile_matmul_generic_value_func, + lto_dispatch_func=tile_matmul_generic_lto_dispatch_func, + variadic=True, + doc="""Computes the matrix product ``out = a*b``. + + Supported datatypes are: + * fp16, fp32, fp64 (real) + * vec2h, vec2f, vec2d (complex) + + Both input tiles must have the same datatype. Tile data will be automatically be migrated + to shared memory if necessary and will use TensorCore operations when available. + + :param a: A tile with ``shape=(M, K)`` + :param b: A tile with ``shape=(K, N)`` + :returns: A tile with ``shape=(M, N)`` + """, + group="Tile Primitives", + export=False, + namespace="", +) ## ## FFT diff --git a/warp/codegen.py b/warp/codegen.py index 0eb26c1b..9628f795 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -1298,9 +1298,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): fwd_args.append(strip_reference(func_arg)) - # used to create an alias of the adjoint var to the primal var for tile ops - alias_call = None - if return_type is None: # handles expression (zero output) functions, e.g.: void do_something(); @@ -1324,11 +1321,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" - # prepend auto if it is an anonymously typed var (e.g.: a tile op) - if output.ctype() == "auto": - forward_call = "auto " + forward_call - alias_call = f"auto& adj_{output} = var_{output};" - replay_call = forward_call if func.custom_replay_func is not None: replay_call = f"var_{output} = {func.namespace}replay_{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" @@ -1349,9 +1341,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): else: adj.add_forward(forward_call, replay=replay_call) - if alias_call: - adj.add_forward(alias_call) - if not func.missing_grad and len(args): adj_args = tuple(strip_reference(x) for x in func_args) reverse_has_output_args = ( @@ -3090,9 +3079,6 @@ def codegen_func_forward(adj, func_type="kernel", device="cpu"): lines += ["// primal vars\n"] for var in adj.variables: - # do not predeclare vars with auto type - if var.ctype() == "auto": - continue if is_tile(var.type): lines += [f"{var.ctype()} {var.emit()} = {var.type.cinit()};\n"] diff --git a/warp/native/tile.h b/warp/native/tile.h index 6f6ad654..cdc338a2 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -37,13 +37,14 @@ [x] Simple [ ] Cute [x] Remove Alloc type from tile_shared_t - [ ] wp.launch_tiled() helper + [x] wp.launch_tiled() helper [ ] Creation [x] zeros [x] ones [x] arange [x] tile() - [ ] untile() + [x] untile() + [ ] fromfunction() [ ] explicit storage [ ] Load/Store [ ] 1D load/store variants @@ -76,23 +77,22 @@ [ ] Slice [ ] Runtime [x] Compile-time block dimensions - [x] Switch between SIMT / Tile based execution if `tile_dim` not provided to wp.launch() + [x] Switch between SIMT / Tile based execution if `block_dim` not provided to wp.launch() [ ] Examples + [ ] Point registration [ ] GEMM + [ ] MLP + [ ] LayerNorm + [ ] SoftMax + [ ] GEMM + [ ] warp.sim (CRBA) [ ] Batched MLP - [ ] Point cloud alignment [ ] Layer norm [ ] Convolution: https://github.com/NVIDIA/MinkowskiEngine/blob/master/src/convolution_kernel.cu#L123 [ ] MeshCNN (Modulus, Oliver) [ ] BioNemo (Ali) [ ] Skinning (David/Or/Vismay) [ ] warp.sim (VBD) - [ ] warp.sim (CRBA) - [ ] Point clustering - [ ] GEMM - [ ] MLP - [ ] LayerNorm - [ ] SoftMax [ ] Error checking [ ] Ensure functions passed to tile_map() are compatible with tile type [ ] Ensure that args passed to tile ops are compatible @@ -237,6 +237,12 @@ struct tile_register_t data[i] = tile.data[i]; } + inline CUDA_CALLABLE void zero() + { + for (int i=0; i < NumRegs; ++i) + data[i] = T(0); + } + // extract a single tile element to a native type inline CUDA_CALLABLE Type extract(int i, int j) { diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index f14ff70c..35107f35 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -43,19 +43,17 @@ inline CUDA_CALLABLE T warp_shuffle_down(T val, int offset, int mask) return output; } -template -inline CUDA_CALLABLE T warp_reduce_sum(T val) +template +inline CUDA_CALLABLE T warp_reduce(T val, Op f, unsigned int mask) { T sum = val; - unsigned int mask = __activemask(); - if (mask == 0xFFFFFFFF) { // handle case where entire warp is active for (int offset=WP_TILE_WARP_SIZE/2; offset > 0; offset /= 2) { - sum += warp_shuffle_down(sum, offset, mask); + sum = f(sum, warp_shuffle_down(sum, offset, mask)); } } else @@ -65,31 +63,17 @@ inline CUDA_CALLABLE T warp_reduce_sum(T val) { T shfl_val = warp_shuffle_down(sum, offset, mask); if ((mask & (1 << ((threadIdx.x + offset)%WP_TILE_WARP_SIZE))) != 0) - sum += shfl_val; + sum = f(sum, shfl_val); } } return sum; } -template -inline CUDA_CALLABLE T warp_reduce(T val, Op op) -{ - T sum = val; - - for (int offset=WP_TILE_WARP_SIZE/2; offset > 0; offset /= 2) - { - sum = op(sum, warp_shuffle_down(sum, offset)); - } - - return sum; -} - - // non-axis version which computes sum // across the entire tile using the whole block -template -auto tile_sum(Tile& t) +template +auto tile_reduce_impl(Op f, Tile& t) { using T = typename Tile::Type; @@ -105,10 +89,19 @@ auto tile_sum(Tile& t) // thread reduction WP_PRAGMA_UNROLL for (int i=1; i < input.NumRegs; ++i) - thread_sum += input.data[i]; + { + int linear = t.index(i); + if (!Tile::Aligned && linear >= Tile::Size) + break; + + thread_sum = f(thread_sum, input.data[i]); + } + + // ensure that only threads with at least one valid item participate in the reduction + unsigned int mask = __ballot_sync(__activemask(), t.index(0) < Tile::Size); // warp reduction - T warp_sum = warp_reduce_sum(thread_sum); + T warp_sum = warp_reduce(thread_sum, f, mask); // fixed size scratch pad for partial results in shared memory WP_TILE_SHARED T partials[warp_count]; @@ -137,7 +130,7 @@ auto tile_sum(Tile& t) WP_PRAGMA_UNROLL for (int i=1; i < active_warps; ++i) - block_sum += partials[i]; + block_sum = f(block_sum, partials[i]); output.data[0] = block_sum; } @@ -145,6 +138,24 @@ auto tile_sum(Tile& t) return output; } +void adj_tile_reduce_impl() +{ + // todo: general purpose reduction gradients not implemented +} + +// entry point for Python code-gen, wraps op in a lambda to perform overload resolution +#define tile_reduce(op, t) tile_reduce_impl([](auto x, auto y) { return op(x, y);}, t) +#define adj_tile_reduce(op, a, adj_op, adj_a, adj_ret) adj_tile_reduce_impl() + +// convenience methods for specific reductions + +template +auto tile_sum(Tile& t) +{ + return tile_reduce(add, t); +} + +// special case adjoint for summation template void adj_tile_sum(Tile& t, Tile& adj_t, AdjTile& adj_ret) { @@ -163,70 +174,30 @@ void adj_tile_sum(Tile& t, Tile& adj_t, AdjTile& adj_ret) adj_t.assign(tile_add(adj_t_reg, adj_ret_reg)); } - -template -auto tile_reduce(Fwd op, Tile& t, int axis) +template +auto tile_max(Tile& t) { - using T = typename Tile::Type; - - auto input = t.copy_to_register(); - auto output = tile_register_t(); - - const int warp_count = (WP_TILE_BLOCK_DIM + WP_TILE_WARP_SIZE - 1)/WP_TILE_WARP_SIZE; - const int warp_index = threadIdx.x/WP_TILE_WARP_SIZE; - const int lane_index = threadIdx.x%WP_TILE_WARP_SIZE; - - T thread_sum = input.data[0]; - - // thread reduction - WP_PRAGMA_UNROLL - for (int i=1; i < input.NumRegs; ++i) - thread_sum = op(thread_sum, input.data[i]); - - // warp reduction - T warp_sum = warp_reduce(thread_sum, op); - - // fixed size scratch pad for partial results - WP_TILE_SHARED T partials[warp_count]; - - if (lane_index == 0) - { - partials[warp_index] = warp_sum; - } - - WP_TILE_SYNC(); - - // reduce across block, todo: use warp_reduce() here - if (threadIdx.x == 0) - { - T block_sum = partials[0]; - - WP_PRAGMA_UNROLL - for (int i=1; i < warp_count; ++i) - block_sum = op(block_sum, partials[i]); - - output.data[0] = block_sum; - } - - return output; + return tile_reduce(max, t); } -template -void adj_tile_reduce(Tile& t, int axis, Tile& adj_t, int adj_axis, AdjTile& adj_ret) +template +void adj_tile_max(Tile& t, Tile& adj_t, AdjTile& adj_ret) { - using T = typename Tile::Type; + // todo: not implemented +} - // broadcast incoming adjoint to block - WP_TILE_SHARED T scratch; - if (threadIdx.x == 0) - scratch = adj_ret.data[0]; +template +auto tile_min(Tile& t) +{ + return tile_reduce(min, t); +} - WP_TILE_SYNC(); +template +void adj_tile_min(Tile& t, Tile& adj_t, AdjTile& adj_ret) +{ + // todo: not implemented +} - auto adj_t_reg = adj_t.copy_to_register(); - auto adj_ret_reg = tile_shared_t(&scratch).copy_to_register(); - adj_t.assign(tile_add(adj_t_reg, adj_ret_reg)); -} } // namespace wp \ No newline at end of file diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index 723ab12f..ff040d23 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -19,19 +19,184 @@ # num threads per-tile TILE_DIM = 64 +@wp.kernel +def tile_sum_kernel(input: wp.array2d(dtype=float), + output: wp.array(dtype=float)): + + # output tile index + i = wp.tid() + + n = input.shape[1] + count = int(n / TILE_DIM) + + s = wp.tile_zeros(m=1, n=1, dtype=float) + + for j in range(count): + a = wp.tile_load(input, i, j, m=1, n=TILE_DIM) + s += wp.tile_sum(a) * 0.5 + + wp.tile_store(output, i, 0, s) + + +def test_tile_reduce_sum(test, device): + batch_count = 56 + + N = TILE_DIM*3 + + rng = np.random.default_rng(42) + input = rng.random((batch_count, N), dtype=np.float32) + + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros(batch_count, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch_tiled( + tile_sum_kernel, + dim=[batch_count], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device) + + sum_wp = output_wp.numpy() + for i in range(batch_count): + sum_np = np.sum(input[i]) * 0.5 + test.assertAlmostEqual(sum_wp[i], sum_np, places=4) + + output_wp.grad.fill_(1.0) + + tape.backward() + + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5, tol=1.e-4) + + +@wp.kernel +def tile_min_kernel(input: wp.array2d(dtype=float), + output: wp.array(dtype=float)): + + # output tile index + i = wp.tid() + + a = wp.tile_load(input, i, 0, m=1, n=TILE_DIM) + m = wp.tile_min(a) + + wp.tile_store(output, i, 0, m) + + +def test_tile_reduce_min(test, device): + batch_count = 56 + + N = TILE_DIM + + rng = np.random.default_rng(42) + input = rng.random((batch_count, N), dtype=np.float32) + + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros(batch_count, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch_tiled( + tile_min_kernel, + dim=[batch_count], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device) + + min_wp = output_wp.numpy() + for i in range(batch_count): + min_np = np.min(input[i]) + test.assertAlmostEqual(min_wp[i], min_np, places=4) + @wp.kernel -def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): +def tile_max_kernel(input: wp.array2d(dtype=float), + output: wp.array(dtype=float)): + # output tile index i = wp.tid() - a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) + a = wp.tile_load(input, i, 0, m=1, n=TILE_DIM) + m = wp.tile_max(a) + + wp.tile_store(output, i, 0, m) + + +def test_tile_reduce_max(test, device): + batch_count = 56 + + N = TILE_DIM + + rng = np.random.default_rng(42) + input = rng.random((batch_count, N), dtype=np.float32) + + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros(batch_count, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch_tiled( + tile_max_kernel, + dim=[batch_count], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device) + + max_wp = output_wp.numpy() + for i in range(batch_count): + max_np = np.max(input[i]) + test.assertAlmostEqual(max_wp[i], max_np, places=4) + + +@wp.kernel +def tile_reduce_custom_kernel(input: wp.array2d(dtype=float), + output: wp.array(dtype=float)): + + # output tile index + i = wp.tid() + + a = wp.tile_load(input, i, 0, m=1, n=TILE_DIM) + m = wp.tile_reduce(wp.mul, a) + + wp.tile_store(output, i, 0, m) + + +def test_tile_reduce_custom(test, device): + batch_count = 56 + + N = TILE_DIM + + rng = np.random.default_rng(42) + input = rng.random((batch_count, N), dtype=np.float32) + + input_wp = wp.array(input, requires_grad=True, device=device) + output_wp = wp.zeros(batch_count, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch_tiled( + tile_reduce_custom_kernel, + dim=[batch_count], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device) + + prod_wp = output_wp.numpy() + for i in range(batch_count): + prod_np = np.prod(input[i]) + test.assertAlmostEqual(prod_wp[i], prod_np, places=4) + + + + +@wp.kernel +def tile_grouped_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): + # output tile index + i = wp.tid() + + a = wp.tile_load(input, i, 0, m=TILE_M, n=TILE_N) s = wp.tile_sum(a) * 0.5 wp.tile_store(output, i, 0, s) -def test_tile_reduce_sum(test, device): +def test_tile_reduce_grouped_sum(test, device): batch_count = 56 M = TILE_M @@ -51,13 +216,13 @@ def test_tile_reduce_sum(test, device): sum_wp = output_wp.numpy() for i in range(batch_count): sum_np = np.sum(input[i]) * 0.5 - test.assertAlmostEqual(sum_wp[i], sum_np, places=5) + test.assertAlmostEqual(sum_wp[i], sum_np, places=4) output_wp.grad.fill_(1.0) tape.backward() - assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5) + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5, tol=1.e-4) @wp.kernel @@ -160,7 +325,12 @@ class TestTileReduce(unittest.TestCase): pass + add_function_test(TestTileReduce, "test_tile_reduce_sum", test_tile_reduce_sum, devices=devices) +add_function_test(TestTileReduce, "test_tile_reduce_min", test_tile_reduce_min, devices=devices) +add_function_test(TestTileReduce, "test_tile_reduce_max", test_tile_reduce_max, devices=devices) +add_function_test(TestTileReduce, "test_tile_reduce_custom", test_tile_reduce_custom, devices=devices) +add_function_test(TestTileReduce, "test_tile_reduce_grouped_sum", test_tile_reduce_sum, devices=devices) add_function_test(TestTileReduce, "test_tile_reduce_simt", test_tile_reduce_simt, devices=devices) add_function_test(TestTileReduce, "test_tile_ones", test_tile_ones, devices=devices) add_function_test(TestTileReduce, "test_tile_arange", test_tile_arange, devices=devices) From d0e4eca9ab3b8b84f18f240e786f1ea427a123be Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 30 Sep 2024 00:07:37 +0000 Subject: [PATCH 047/102] Add support for wp.tile_transpose() --- docs/modules/differentiability.rst | 2 + docs/modules/functions.rst | 123 ++++++++++++++++++++++++++- docs/modules/tiles.rst | 41 +++++---- warp/builtins.py | 79 ++++++++++++++++-- warp/codegen.py | 1 - warp/native/tile.h | 46 ++++++++-- warp/stubs.py | 129 +++++++++++++++++++++++++++++ warp/tests/test_tile.py | 41 +++++++++ warp/tests/test_tile_reduce.py | 60 +++++--------- warp/types.py | 22 ++++- 10 files changed, 468 insertions(+), 76 deletions(-) diff --git a/docs/modules/differentiability.rst b/docs/modules/differentiability.rst index 72436104..81145d8d 100644 --- a/docs/modules/differentiability.rst +++ b/docs/modules/differentiability.rst @@ -1,3 +1,5 @@ +.. _differentiability: + Differentiability ================= diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index f33d4339..ffd87dc9 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -908,7 +908,7 @@ Tile Primitives This function converts a block-wide tile back to per-thread values. - :param a: A tile with dimensions ``shape=(1, block_dim)`` + :param a: A tile with dimensions ``shape=(M, block_dim)`` :returns: A single value per-thread with the same dtype as the tile This example shows how to create a linear sequence from thread variables: @@ -954,6 +954,16 @@ Tile Primitives :returns: The value of the element at the specified tile location, with the same type as the input tile's per-element dtype +.. py:function:: tile_transpose(a: Tile) -> Tile + + Transpose a tile. + + For shared memory tiles this operation will alias the input tile, register tiles will first be transferred to shared memory before transposition. + + :param a: Tile to transpose with ``shape=(M,N)`` + :returns: Tile with ``shape=(N,M)`` + + .. py:function:: tile_sum(a: Tile) -> Tile Cooperatively compute the sum the tile elements using all threads in the block. @@ -984,6 +994,98 @@ Tile Primitives +.. py:function:: tile_min(a: Tile) -> Tile + + Cooperatively compute the minimum of the tile elements using all threads in the block. + + :param a: The tile to compute the minimum of + :returns: A single element tile with dimensions of (1,1) holding the minimum value + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_arange(start=--10, stop=10, dtype=float) + s = wp.tile_min(t) + + print(t) + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[-10]] + + + + +.. py:function:: tile_max(a: Tile) -> Tile + + Cooperatively compute the maximum of the tile elements using all threads in the block. + + :param a: The tile to compute the maximum from + :returns: A single element tile with dimensions of (1,1) holding the maximum value + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + + t = wp.tile_arange(start=--10, stop=10, dtype=float) + s = wp.tile_min(t) + + print(t) + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[10]] + + + + +.. py:function:: tile_reduce(op: Callable, a: Any) -> Tile + + Apply a custom reduction operator across the tile. + + This function cooperatively performs a reduction using the provided operator across the tile. + + :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin + :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A single element tile with ``shape=(1,1)`` with the same datatype as the input tile. + + Example: + + .. code-block:: python + + @wp.kernel + def factorial(): + + t = wp.tile_arange(1, 10, dtype=int) + s = wp.tile_reduce(wp.mul, t) + + print(s) + + wp.launch(factorial, dim=[16], inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[362880]] + + + .. py:function:: tile_map(op: Callable, a: Any) -> Tile Apply a unary function onto the tile. @@ -1070,6 +1172,25 @@ Tile Primitives +.. py:function:: tile_matmul(a: Tile, b: Tile) -> Tile + :noindex: + :nocontentsentry: + + Computes the matrix product ``out = a*b``. + + Supported datatypes are: + * fp16, fp32, fp64 (real) + * vec2h, vec2f, vec2d (complex) + + Both input tiles must have the same datatype. Tile data will be automatically be migrated + to shared memory if necessary and will use TensorCore operations when available. + + :param a: A tile with ``shape=(M, K)`` + :param b: A tile with ``shape=(K, N)`` + :returns: A tile with ``shape=(M, N)`` + + + .. py:function:: tile_fft(inout: Tile) -> Tile Compute the forward FFT along the second dimension of a 2D tile of data. diff --git a/docs/modules/tiles.rst b/docs/modules/tiles.rst index bf7f40bb..48d2c788 100644 --- a/docs/modules/tiles.rst +++ b/docs/modules/tiles.rst @@ -1,5 +1,7 @@ -Tiles (Preview) -=============== +Tiles +===== + +.. warning:: Tile-based operations in Warp are under preview, APIs are subject to change. Block-based programming models such as those in OpenAI Triton have proved to be effective ways of expressing high performance kernels that can leverage cooperative operations on modern GPUs. @@ -8,7 +10,7 @@ Warp 1.4.0 introduces tile extensions that expose a block-based programming to W Execution Model --------------- -Warp's execution model allows users to specify an up to 4-dimensional grid of logical threads for kernel execution at launch time. With the introduction of tiles, users can also specify a block size, which partitions the grid into smaller sets of threads that are executed on a single compute unit. +Warp's execution model allows users to specify an up to 4-dimensional grid of logical threads for kernel execution at launch time. With the introduction of tile primitives, users can additionally specify a block size, which partitions the thread grid into smaller sets of threads that are executed on a single compute unit. Inside kernels, tile operations are executed cooperatively across each block of threads, allowing them to take advantage of efficient memory access, local memory, and dedicated hardware units like TensorCores. @@ -23,19 +25,19 @@ As an example, consider the following kernel: def compute(a: array(dtype=float)) i = wp.tid()/TILE_SIZE - t = wp.tile_load(array, x=i, n=TILE_SIZE) + t = wp.tile_load(array, i, TILE_SIZE) ... wp.launch(compute, dim=[len(a)], inputs=[a], block_dim=TILE_THREADS) -Here, we load a 1D tile of 256 values from a global memory array ``a``, where the load operation is performed cooperatively by all 64 threads in the block, as specified by the ``block_dim`` argument to :func:`warp.launch`. In this case each thread is responsible for loading 4 values from global memory, which may then be stored in registers, or shared memory across the block. +Here, each block loads a 1D tile of 256 values from a global memory array ``a``, where the load operation is performed cooperatively by all 64 threads in the block, as specified by the ``block_dim`` argument to :func:`warp.launch`. In this case, each thread is responsible for loading 4 values from global memory, which may then be stored in registers, or shared memory across the block. Tile Properties --------------- In Warp, tile objects are 2D arrays of data where the tile elements may be scalars, vectors, matrices, or user defined structures. -In a more complex example, we launch a grid of threads where each block is responsible for loading a row of data from a 2D array and computing its sum: +In the following example, we launch a grid of threads where each block is responsible for loading a row of data from a 2D array and computing its sum: .. code:: python @@ -44,10 +46,10 @@ In a more complex example, we launch a grid of threads where each block is respo @wp.kernel def compute(a: array2d(dtype=float)) - i, _= wp.tid() + i, _ = wp.tid() # load a row from global memory - t = wp.tile_load(array, i, TILE_SIZE) + t = wp.tile_load(array, i, 0, 1, TILE_SIZE) s = wp.sum(t) ... @@ -79,7 +81,7 @@ In this example, we use :func:`warp.launch_tiled` to automatically insert the tr Tile Storage ------------ -When tiles are created they are placed in either `register` or `shared` memory. In general Warp tries to determine the best storage for each, the default is generally for register storage, although some operations such as matrix multiplies may migrate data from register to shared as necessary. +When tiles are created they are placed in either `register` or `shared` memory. In general Warp tries to determine the best storage for each, by default tiles are allocated in register storage, however some operations such as matrix multiplies may migrate data from register to shared as necessary. Register Tiles ++++++++++++++ @@ -94,20 +96,15 @@ Some operations like matrix multiplication, require access to an entire tile of Tile Operations --------------- -Creation -++++++++ +Construction +++++++++++++ * :func:`warp.tile_zeros` * :func:`warp.tile_ones` * :func:`warp.tile_arange` - -Conversion -++++++++++ - * :func:`warp.tile` * :func:`warp.untile` - Load/Store ++++++++++ @@ -119,17 +116,21 @@ Maps/Reductions +++++++++++++++ * :func:`warp.tile_map` +* :func:`warp.tile_reduce` * :func:`warp.tile_sum` +* :func:`warp.tile_min` +* :func:`warp.tile_max` Linear Algebra ++++++++++++++ * :func:`warp.tile_matmul` +* :func:`warp.tile_transpose` * :func:`warp.tile_fft` * :func:`warp.tile_ifft` Tiles and SIMT Code -+++++++++++++++++++ +------------------- Warp kernels are primarily written in the SIMT programming model in mind, where each thread's execution happens completely independently. Tiles on the other hand allow threads to work cooperatively to perform operations. @@ -158,6 +159,12 @@ In this example we perform some per-thread computations, and then convert the sc Similarly, we can `untile` tile objects back to their per-thread scalar equivalent values. +.. Note:: All threads in a block must execute tile operations, however code surrounding tile operations may contain arbitrary conditional logic. + +Automatic Differentiation +------------------------- + +Warp can automatically generate the backward version of tile-based programs, in general tile programs must obey the same rules for auto-diff as regular Warp programs, e.g.: avoiding in-place operations, etc. Please see the :ref:`differentiability` section for more details. diff --git a/warp/builtins.py b/warp/builtins.py index 6771d22a..02d31d31 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -2182,6 +2182,49 @@ def tile_extract_value_func(arg_types, arg_values): ) +def tile_transpose_value_func(arg_types, arg_values): + # return generic type (for doc builds) + if arg_types is None: + return Tile + + if len(arg_types) != 1: + raise RuntimeError("tile_transpose() requires 1 positional args") + + t = arg_types["a"] + + if not is_tile(t): + raise RuntimeError("tile_transpose() argument 0 must be a tile") + + layout = None + + # flip layout + if t.layout == "rowmajor": + layout = "colmajor" + elif t.layout == "colmajor": + layout = "rowmajor" + + # force the input tile to shared memory + t.storage = "shared" + + return Tile(dtype=t.dtype, M=t.N, N=t.M, op="transpose", storage=t.storage, layout=layout, owner=False) + + +add_builtin( + "tile_transpose", + input_types={"a": Tile(dtype=Any, M=Any, N=Any)}, + value_func=tile_transpose_value_func, + variadic=True, + doc="""Transpose a tile. + + For shared memory tiles this operation will alias the input tile, register tiles will first be transferred to shared memory before transposition. + + :param a: Tile to transpose with ``shape=(M,N)`` + :returns: Tile with ``shape=(N,M)``""", + group="Tile Primitives", + export=False, +) + + def tile_matmul_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -2280,6 +2323,7 @@ def compute(): export=False, ) + def tile_min_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -2331,6 +2375,7 @@ def compute(): export=False, ) + def tile_max_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -2382,6 +2427,7 @@ def compute(): export=False, ) + # does type propagation for load() def tile_reduce_value_func(arg_types, arg_values): if arg_types is None: @@ -2420,14 +2466,14 @@ def tile_reduce_dispatch_func(input_types: Mapping[str, type], return_type: Any, .. code-block:: python @wp.kernel - def compute(): + def factorial(): t = wp.tile_arange(1, 10, dtype=int) - s = wp.tile_reduce(wp.prod, t) + s = wp.tile_reduce(wp.mul, t) print(s) - wp.launch(compute, dim=[16], inputs=[]) + wp.launch(factorial, dim=[16], inputs=[], block_dim=16) Prints: @@ -5386,9 +5432,29 @@ def make_transpose(t): builder.ltoirs[lto_symbol] = lto_code return lto_symbol, lto_code - (fun_forward, lto_forward) = make_function(M, N, K, "N", "N") # C += A * B - (fun_backward_A, lto_backward_A) = make_function(M, K, N, "N", "T") # adjA += adjC * B^T - (fun_backward_B, lto_backward_B) = make_function(K, N, M, "T", "N") # adjB += A^T * adjC + def tile_layout_mode(tile): + if tile.layout == "rowmajor": + return "N" + if tile.layout == "colmajor": + return "T" + + def tile_flip_layout(layout): + if layout == "N": + return "T" + elif layout == "T": + return "N" + + a_layout = tile_layout_mode(a.type) + b_layout = tile_layout_mode(b.type) + c_layout = tile_layout_mode(out.type) + + (fun_forward, lto_forward) = make_function(M, N, K, a_layout, b_layout) # C += A * B + (fun_backward_A, lto_backward_A) = make_function( + M, K, N, c_layout, tile_flip_layout(b_layout) + ) # adjA += adjC * B^T + (fun_backward_B, lto_backward_B) = make_function( + K, N, M, tile_flip_layout(a_layout), c_layout + ) # adjB += A^T * adjC return ( ( @@ -5453,6 +5519,7 @@ def make_transpose(t): namespace="", ) + ## ## FFT ## diff --git a/warp/codegen.py b/warp/codegen.py index 9628f795..697f3d33 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -3079,7 +3079,6 @@ def codegen_func_forward(adj, func_type="kernel", device="cpu"): lines += ["// primal vars\n"] for var in adj.variables: - if is_tile(var.type): lines += [f"{var.ctype()} {var.emit()} = {var.type.cinit()};\n"] elif var.constant is None: diff --git a/warp/native/tile.h b/warp/native/tile.h index cdc338a2..a856b643 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -60,9 +60,9 @@ [x] Sum [x] Forward [x] Reverse - [ ] Min - [ ] Max - [ ] Custom + [x] Min + [x] Max + [x] Custom [x] MatMul [x] Forward [x] Reverse @@ -380,6 +380,22 @@ struct tile_shared_t copy_from_global(t.array, t.x, t.y); } + // construct from another shared tile, this constructor + // is invoked for reshape operations like `wp.tile_transpose()` + template + inline CUDA_CALLABLE auto& operator=(const tile_shared_t& stile) + { + using OtherTile = tile_shared_t; + + // check dimensions are compatible + static_assert(Size == OtherTile::Size); + + // alias tile directly + data = stile.data; + + return *this; + } + // assign from a global tile inline CUDA_CALLABLE auto& operator=(const tile_global_t& t) { @@ -637,12 +653,6 @@ inline CUDA_CALLABLE auto tile_alloc_zeros() return tile_shared_t(data); } -template -inline CUDA_CALLABLE auto tile_transpose(Tile& t) -{ - // alias incoming tile - return tile_shared_t(t.data); -} //----------------------------------------------------------------------------------------------------- // High level entry points for each op (correspond to one Warp builtin) @@ -1091,4 +1101,22 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \ } while (0) + +template +inline CUDA_CALLABLE auto tile_transpose(Tile& t) +{ + // alias incoming tile + return tile_shared_t(t.data); +} + +template +inline CUDA_CALLABLE void adj_tile_transpose(Tile& t, Tile& adj_t, AdjTile& adj_ret) +{ + auto a = adj_t.copy_to_register(); + auto b = t.copy_to_register(); + + adj_t.assign(tile_add(a,b)); +} + + } // namespace wp diff --git a/warp/stubs.py b/warp/stubs.py index fc642827..301b056b 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -1059,6 +1059,18 @@ def tile_extract(a: Tile, i: int32, j: int32) -> Scalar: ... +@over +def tile_transpose(a: Tile) -> Tile: + """Transpose a tile. + + For shared memory tiles this operation will alias the input tile, register tiles will first be transferred to shared memory before transposition. + + :param a: Tile to transpose with ``shape=(M,N)`` + :returns: Tile with ``shape=(N,M)`` + """ + ... + + @over def tile_sum(a: Tile) -> Tile: """Cooperatively compute the sum the tile elements using all threads in the block. @@ -1091,6 +1103,104 @@ def compute(): ... +@over +def tile_min(a: Tile) -> Tile: + """Cooperatively compute the minimum of the tile elements using all threads in the block. + + :param a: The tile to compute the minimum of + :returns: A single element tile with dimensions of (1,1) holding the minimum value + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + t = wp.tile_arange(start=--10, stop=10, dtype=float) + s = wp.tile_min(t) + + print(t) + + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[-10]] + + + """ + ... + + +@over +def tile_max(a: Tile) -> Tile: + """Cooperatively compute the maximum of the tile elements using all threads in the block. + + :param a: The tile to compute the maximum from + :returns: A single element tile with dimensions of (1,1) holding the maximum value + + Example: + + .. code-block:: python + + @wp.kernel + def compute(): + t = wp.tile_arange(start=--10, stop=10, dtype=float) + s = wp.tile_min(t) + + print(t) + + + wp.launch(compute, dim=[64], inputs=[]) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[10]] + + + """ + ... + + +@over +def tile_reduce(op: Callable, a: Any) -> Tile: + """Apply a custom reduction operator across the tile. + + This function cooperatively performs a reduction using the provided operator across the tile. + + :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin + :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype + :returns: A single element tile with ``shape=(1,1)`` with the same datatype as the input tile. + + Example: + + .. code-block:: python + + @wp.kernel + def factorial(): + t = wp.tile_arange(1, 10, dtype=int) + s = wp.tile_reduce(wp.mul, t) + + print(s) + + + wp.launch(factorial, dim=[16], inputs=[], block_dim=16) + + Prints: + + .. code-block:: text + + tile(m=1, n=1, storage=register) = [[362880]] + + """ + ... + + @over def tile_map(op: Callable, a: Any) -> Tile: """Apply a unary function onto the tile. @@ -2648,6 +2758,25 @@ def tile_matmul(a: Tile, b: Tile, out: Tile) -> Tile: ... +@over +def tile_matmul(a: Tile, b: Tile) -> Tile: + """Computes the matrix product ``out = a*b``. + + Supported datatypes are: + * fp16, fp32, fp64 (real) + * vec2h, vec2f, vec2d (complex) + + Both input tiles must have the same datatype. Tile data will be automatically be migrated + to shared memory if necessary and will use TensorCore operations when available. + + :param a: A tile with ``shape=(M, K)`` + :param b: A tile with ``shape=(K, N)`` + :returns: A tile with ``shape=(M, N)`` + + """ + ... + + @over def tile_fft(inout: Tile) -> Tile: """Compute the forward FFT along the second dimension of a 2D tile of data. diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 8aba083e..fcd394aa 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -395,6 +395,45 @@ def test_tile_extract(test, device): assert_np_equal(input_wp.grad.numpy(), np.ones_like(input)) +@wp.kernel +def test_tile_transpose_kernel(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): + x = wp.tile_load(input, 0, 0, m=TILE_M, n=TILE_N) + y = wp.tile_transpose(x) + + wp.tile_store(output, 0, 0, y) + + +def test_tile_transpose(test, device): + rng = np.random.default_rng(42) + input = wp.array(rng.random((TILE_M, TILE_N), dtype=np.float32), device=device) + output = wp.zeros_like(input.transpose(), device=device) + + wp.launch_tiled(test_tile_transpose_kernel, dim=[1], inputs=[input, output], block_dim=32, device=device) + + assert_np_equal(output.numpy(), input.numpy().T) + + +@wp.kernel +def test_tile_transpose_matmul_kernel(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): + x = wp.tile_load(input, 0, 0, m=TILE_M, n=TILE_N) + y = wp.tile_transpose(x) + + z = wp.tile_zeros(dtype=float, m=TILE_N, n=TILE_N) + wp.tile_matmul(y, x, z) + + wp.tile_store(output, 0, 0, z) + + +def test_tile_transpose_matmul(test, device): + rng = np.random.default_rng(42) + input = wp.array(rng.random((TILE_M, TILE_N), dtype=np.float32), device=device) + output = wp.zeros((TILE_N, TILE_N), dtype=float, device=device) + + wp.launch_tiled(test_tile_transpose_matmul_kernel, dim=[1], inputs=[input, output], block_dim=32, device=device) + + assert_np_equal(output.numpy(), input.numpy().T @ input.numpy()) + + # #----------------------------------------- # # center of mass computation @@ -486,6 +525,8 @@ class TestTile(unittest.TestCase): add_function_test(TestTile, "test_tile_binary_map", test_tile_binary_map, devices=devices) add_function_test(TestTile, "test_tile_grouped_gemm", test_tile_grouped_gemm, devices=devices) # FAILS add_function_test(TestTile, "test_tile_gemm", test_tile_gemm, devices=devices) +add_function_test(TestTile, "test_tile_transpose", test_tile_transpose, devices=devices) # FAILS +add_function_test(TestTile, "test_tile_transpose_matmul", test_tile_transpose_matmul, devices=devices) add_function_test(TestTile, "test_tile_operators", test_tile_operators, devices=devices) add_function_test(TestTile, "test_tile_sum", test_tile_sum, devices=devices) add_function_test(TestTile, "test_tile_extract", test_tile_extract, devices=devices) diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index ff040d23..f0b60d86 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -19,10 +19,9 @@ # num threads per-tile TILE_DIM = 64 + @wp.kernel -def tile_sum_kernel(input: wp.array2d(dtype=float), - output: wp.array(dtype=float)): - +def tile_sum_kernel(input: wp.array2d(dtype=float), output: wp.array(dtype=float)): # output tile index i = wp.tid() @@ -33,7 +32,7 @@ def tile_sum_kernel(input: wp.array2d(dtype=float), for j in range(count): a = wp.tile_load(input, i, j, m=1, n=TILE_DIM) - s += wp.tile_sum(a) * 0.5 + s += wp.tile_sum(a) * 0.5 wp.tile_store(output, i, 0, s) @@ -41,7 +40,7 @@ def tile_sum_kernel(input: wp.array2d(dtype=float), def test_tile_reduce_sum(test, device): batch_count = 56 - N = TILE_DIM*3 + N = TILE_DIM * 3 rng = np.random.default_rng(42) input = rng.random((batch_count, N), dtype=np.float32) @@ -51,11 +50,8 @@ def test_tile_reduce_sum(test, device): with wp.Tape() as tape: wp.launch_tiled( - tile_sum_kernel, - dim=[batch_count], - inputs=[input_wp, output_wp], - block_dim=TILE_DIM, - device=device) + tile_sum_kernel, dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device + ) sum_wp = output_wp.numpy() for i in range(batch_count): @@ -66,13 +62,11 @@ def test_tile_reduce_sum(test, device): tape.backward() - assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5, tol=1.e-4) + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5, tol=1.0e-4) @wp.kernel -def tile_min_kernel(input: wp.array2d(dtype=float), - output: wp.array(dtype=float)): - +def tile_min_kernel(input: wp.array2d(dtype=float), output: wp.array(dtype=float)): # output tile index i = wp.tid() @@ -95,11 +89,8 @@ def test_tile_reduce_min(test, device): with wp.Tape() as tape: wp.launch_tiled( - tile_min_kernel, - dim=[batch_count], - inputs=[input_wp, output_wp], - block_dim=TILE_DIM, - device=device) + tile_min_kernel, dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device + ) min_wp = output_wp.numpy() for i in range(batch_count): @@ -108,9 +99,7 @@ def test_tile_reduce_min(test, device): @wp.kernel -def tile_max_kernel(input: wp.array2d(dtype=float), - output: wp.array(dtype=float)): - +def tile_max_kernel(input: wp.array2d(dtype=float), output: wp.array(dtype=float)): # output tile index i = wp.tid() @@ -133,11 +122,8 @@ def test_tile_reduce_max(test, device): with wp.Tape() as tape: wp.launch_tiled( - tile_max_kernel, - dim=[batch_count], - inputs=[input_wp, output_wp], - block_dim=TILE_DIM, - device=device) + tile_max_kernel, dim=[batch_count], inputs=[input_wp, output_wp], block_dim=TILE_DIM, device=device + ) max_wp = output_wp.numpy() for i in range(batch_count): @@ -146,9 +132,7 @@ def test_tile_reduce_max(test, device): @wp.kernel -def tile_reduce_custom_kernel(input: wp.array2d(dtype=float), - output: wp.array(dtype=float)): - +def tile_reduce_custom_kernel(input: wp.array2d(dtype=float), output: wp.array(dtype=float)): # output tile index i = wp.tid() @@ -171,11 +155,12 @@ def test_tile_reduce_custom(test, device): with wp.Tape() as tape: wp.launch_tiled( - tile_reduce_custom_kernel, - dim=[batch_count], - inputs=[input_wp, output_wp], - block_dim=TILE_DIM, - device=device) + tile_reduce_custom_kernel, + dim=[batch_count], + inputs=[input_wp, output_wp], + block_dim=TILE_DIM, + device=device, + ) prod_wp = output_wp.numpy() for i in range(batch_count): @@ -183,8 +168,6 @@ def test_tile_reduce_custom(test, device): test.assertAlmostEqual(prod_wp[i], prod_np, places=4) - - @wp.kernel def tile_grouped_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float)): # output tile index @@ -222,7 +205,7 @@ def test_tile_reduce_grouped_sum(test, device): tape.backward() - assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5, tol=1.e-4) + assert_np_equal(input_wp.grad.numpy(), np.ones_like(input) * 0.5, tol=1.0e-4) @wp.kernel @@ -325,7 +308,6 @@ class TestTileReduce(unittest.TestCase): pass - add_function_test(TestTileReduce, "test_tile_reduce_sum", test_tile_reduce_sum, devices=devices) add_function_test(TestTileReduce, "test_tile_reduce_min", test_tile_reduce_min, devices=devices) add_function_test(TestTileReduce, "test_tile_reduce_max", test_tile_reduce_max, devices=devices) diff --git a/warp/types.py b/warp/types.py index 3cae1be3..7e244863 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2959,12 +2959,21 @@ def array_type_id(a): class Tile: allocation = 0 - def __init__(self, dtype, M, N, op=None, storage="register"): - self.dtype = dtype + def __init__(self, dtype, M, N, op=None, storage="register", layout="rowmajor", owner=True): + self.dtype = type_to_warp(dtype) self.M = M self.N = N self.op = op self.storage = storage + self.layout = layout + + # default to row major layout + if layout == "rowmajor": + self.strides = (N, 1) + elif layout == "colmajor": + self.strides = (1, M) + + self.owner = owner # generates C-type string def ctype(self): @@ -2973,7 +2982,9 @@ def ctype(self): if self.storage == "register": return f"wp::tile_register_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" elif self.storage == "shared": - return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" + return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}, {self.strides[0]}, {self.strides[1]}>" + else: + raise RuntimeError(f"Unrecognized tile storage type {self.storage}") # generates C-initializer string def cinit(self, adjoint=False): @@ -2982,6 +2993,11 @@ def cinit(self, adjoint=False): if self.storage == "register": return self.ctype() + "(0.0)" elif self.storage == "shared": + # if this is a reference to another tile + # then don't allocate any memory + if self.owner == False: + return "NULL" + if adjoint: # backward pass requires zeroed memory return f"wp::tile_alloc_zeros<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" From a1b79ccff6968a34583d921fab54774fd3e4eec6 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 30 Sep 2024 20:48:03 +0000 Subject: [PATCH 048/102] Add wp.tile_broadcast() Add wp.tile_matmul() overload with no explicit output variable --- warp/builtins.py | 112 +++++++++++++++++++++++++++++++++++----- warp/codegen.py | 33 ++++++------ warp/context.py | 9 +++- warp/native/tile.h | 87 ++++++++++++++++++++++--------- warp/tests/test_tile.py | 2 +- 5 files changed, 187 insertions(+), 56 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 02d31d31..5491e3de 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1832,8 +1832,10 @@ def tile_arange_value_func(arg_types: Mapping[str, type], arg_values: Mapping[st if start is None or stop is None or step is None: raise RuntimeError("wp.tile_arange() arguments must be compile time constants") - if arg_values["dtype"] is not None: + if "dtype" in arg_values: dtype = arg_values["dtype"] + else: + dtype = float return TileRange(dtype=dtype, start=start, stop=stop, step=step) @@ -2224,6 +2226,76 @@ def tile_transpose_value_func(arg_types, arg_values): export=False, ) +def tile_broadcast_value_func(arg_types, arg_values): + # return generic type (for doc builds) + if arg_types is None: + return Tile + + if len(arg_types) != 3: + raise RuntimeError("tile_broadcast() requires 1 positional args") + + t = arg_types["a"] + m = arg_values["m"] + n = arg_values["n"] + + if not is_tile(t): + raise RuntimeError("tile_transpose() argument 0 must be a tile") + + # try to broadcast last dimension + if t.N == 1: + stride_n = 0 + elif t.N == n: + stride_n = t.strides[1] + else: + raise RuntimeError(f"Broadcast dimension must be 1 or match destination, shape(src) = {t.m, t.n}, shape(dest) = {m, n}") + + # try to broadcast first dimension + if t.M == 1: + stride_m = 0 + elif t.M == m: + stride_m = t.strides[0] + else: + raise RuntimeError(f"Broadcast dimension must be 1 or match destination, shape(src) = {t.m, t.n}, shape(dest) = {m, n}") + + # force the input tile to shared memory + t.storage = "shared" + + tile_type = Tile(dtype=t.dtype, M=m, N=n, op="broadcast", storage=t.storage, owner=False) + tile_type.strides = (stride_m, stride_n) + + return tile_type + +def tile_broadcast_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): + + tile = arg_values["a"] + + template_args = [] + template_args.append(return_type.M) + template_args.append(return_type.N) + template_args.append(return_type.strides[0]) + template_args.append(return_type.strides[1]) + + return ((tile,), template_args) + + +add_builtin( + "tile_broadcast", + input_types={"a": Tile(dtype=Any, M=Any, N=Any), "m": int, "n": int}, + value_func=tile_broadcast_value_func, + dispatch_func=tile_broadcast_dispatch_func, + variadic=True, + doc="""Broadcast a tile. + + This method will attempt to broadcast the input tile ``a`` to the destination shape (m, n), broadcasting follows NumPy broadcast rules. + + :param a: Tile to broadcast + :returns: Tile with broadcast ``shape=(m, n)``""", + group="Tile Primitives", + export=False, +) + + + def tile_matmul_value_func(arg_types, arg_values): # return generic type (for doc builds) @@ -5306,17 +5378,22 @@ def tile_matmul_generic_value_func(arg_types, arg_values): if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) - if len(arg_types) != 3: - raise RuntimeError("tile_matmul() requires 4 positional args") + a = arg_types["a"] + b = arg_types["b"] - if not is_tile(arg_types["a"]): + if not is_tile(a): raise RuntimeError("tile_matmul() argument 0 must be a tile") - - if not is_tile(arg_types["b"]): + if not is_tile(b): raise RuntimeError("tile_matmul() argument 1 must be an tile") - if not is_tile(arg_types["out"]): - raise RuntimeError("tile_matmul() output argument must be a tile") + # out = wp.tile_matmul(a, b) + if len(arg_types) == 2: + return Tile(dtype=a.dtype, M=a.M, N=b.N, storage="shared") + + # wp.tile_matmul(a, b, out) + elif len(arg_types) == 3: + if not is_tile(arg_types["out"]): + raise RuntimeError("tile_matmul() output argument must be a tile") return None @@ -5324,13 +5401,18 @@ def tile_matmul_generic_value_func(arg_types, arg_values): def tile_matmul_generic_lto_dispatch_func( arg_types: Mapping[str, type], return_type: Any, + return_values: List[Var], arg_values: Mapping[str, Var], options: Mapping[str, Any], builder: warp.context.ModuleBuilder, ): a = arg_values["a"] b = arg_values["b"] - out = arg_values["out"] + + if len(return_values) > 0: + out = return_values[0] + else: + out = arg_values["out"] if any(not is_tile(arg.type) for arg in [a, b, out]): raise RuntimeError("tile_matmul() requires three Tile arguments") @@ -5430,6 +5512,8 @@ def make_transpose(t): lto_code = f.read() builder.ltoirs[lto_symbol] = lto_code + builder.ltoirs_decl[lto_symbol] = f"void {lto_symbol}({dtype}, {dtype}*, {dtype}*, {dtype}, {dtype}*);" + return lto_symbol, lto_code def tile_layout_mode(tile): @@ -5461,7 +5545,6 @@ def tile_flip_layout(layout): Var(fun_forward, str, False, True, False), Var(fun_backward_A, str, False, True, False), Var(fun_backward_B, str, False, True, False), - Var(dtype, str, False, True, False), a, b, out, @@ -5473,10 +5556,10 @@ def tile_flip_layout(layout): add_builtin( "tile_matmul", - input_types={"a": Tile, "b": Tile, "out": Tile}, + input_types={"a": Tile(dtype=Any, M=Any, N=Any), "b": Tile(dtype=Any, M=Any, N=Any), "out": Tile(dtype=Any, M=Any, N=Any)}, value_func=tile_matmul_generic_value_func, lto_dispatch_func=tile_matmul_generic_lto_dispatch_func, - variadic=True, + variadic=False, doc="""Computes the matrix product and accumulates ``out += a*b``. Supported datatypes are: @@ -5497,10 +5580,10 @@ def tile_flip_layout(layout): add_builtin( "tile_matmul", - input_types={"a": Tile, "b": Tile}, + input_types={"a": Tile(dtype=Any, M=Any, N=Any), "b": Tile(dtype=Any, M=Any, N=Any)}, value_func=tile_matmul_generic_value_func, lto_dispatch_func=tile_matmul_generic_lto_dispatch_func, - variadic=True, + variadic=False, doc="""Computes the matrix product ``out = a*b``. Supported datatypes are: @@ -5542,6 +5625,7 @@ def tile_fft_generic_value_func(arg_types, arg_values): def tile_fft_generic_lto_dispatch_func( arg_types: Mapping[str, type], return_type: Any, + return_values: List[Var], arg_values: Mapping[str, Var], options: Mapping[str, Any], builder: warp.context.ModuleBuilder, diff --git a/warp/codegen.py b/warp/codegen.py index 697f3d33..f347c2fc 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -1266,6 +1266,23 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): bound_arg_values, ) + # immediately allocate output variables so we can pass them into the dispatch method + if return_type is None: + # void function + output = None + output_list = [] + elif not isinstance(return_type, Sequence) or len(return_type) == 1: + # single return value function + if isinstance(return_type, Sequence): + return_type = return_type[0] + output = adj.add_var(return_type) + output_list = [output] + else: + # multiple return value function + output = [adj.add_var(v) for v in return_type] + output_list = output + + # If we have a built-in that requires special handling to dispatch # the arguments to the underlying C++ function, then we can resolve # these using the `dispatch_func`. Since this is only called from @@ -1275,7 +1292,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): # a literal value or references a variable. if func.lto_dispatch_func is not None: func_args, template_args, ltoirs = func.lto_dispatch_func( - func.input_types, return_type, bound_args, options=adj.builder_options, builder=adj.builder + func.input_types, return_type, output_list, bound_args, options=adj.builder_options, builder=adj.builder ) elif func.dispatch_func is not None: func_args, template_args = func.dispatch_func(func.input_types, return_type, bound_args) @@ -1300,10 +1317,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): if return_type is None: # handles expression (zero output) functions, e.g.: void do_something(); - - output = None - output_list = [] - forward_call = ( f"{func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" ) @@ -1313,12 +1326,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): elif not isinstance(return_type, Sequence) or len(return_type) == 1: # handle simple function (one output) - - if isinstance(return_type, Sequence): - return_type = return_type[0] - output = adj.add_var(return_type) - output_list = [output] - forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" replay_call = forward_call @@ -1327,10 +1334,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): else: # handle multiple value functions - - output = [adj.add_var(v) for v in return_type] - output_list = output - forward_call = ( f"{func.namespace}{func_name}({adj.format_forward_call_args(fwd_args + output, use_initializer_list)});" ) diff --git a/warp/context.py b/warp/context.py index ff92f0e3..a66577fd 100644 --- a/warp/context.py +++ b/warp/context.py @@ -1541,7 +1541,8 @@ def __init__(self, module, options, hasher=None): self.options = options self.module = module self.deferred_functions = [] - self.ltoirs = {} # map from lto symbol to lto binary + self.ltoirs = {} # map from lto symbol to lto binary + self.ltoirs_decl = {} # map from lto symbol to lto forward declaration if hasher is None: hasher = ModuleHasher(module) @@ -1612,6 +1613,12 @@ def value_type(arg_types, arg_values): def codegen(self, device): source = "" + # code-gen LTO forward declarations + source += 'extern "C" {\n' + for fwd in self.ltoirs_decl.values(): + source += fwd + "\n" + source += '}\n' + # code-gen structs visited_structs = set() for struct in self.structs.keys(): diff --git a/warp/native/tile.h b/warp/native/tile.h index a856b643..7910c21d 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -380,6 +380,14 @@ struct tile_shared_t copy_from_global(t.array, t.x, t.y); } + // assign from a register tile + template + inline CUDA_CALLABLE auto& operator=(const Tile& t) + { + assign(t); + return *this; + } + // construct from another shared tile, this constructor // is invoked for reshape operations like `wp.tile_transpose()` template @@ -738,9 +746,9 @@ inline CUDA_CALLABLE auto tile_arange(T start, T stop, T step) return out; } -template +template inline CUDA_CALLABLE void adj_tile_arange(int start, int stop, int step, - int adj_start, int adj_stop, int adj_step, const tile_register_t& adj_ret) {} + int adj_start, int adj_stop, int adj_step, AdjTile& adj_ret) {} // entry point for load template @@ -1048,29 +1056,45 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ adj_t.adj_extract(i, j, adj_ret); } -// But cuBLASDx follows the BLAS convention: matrices are col-major, so we swap A & B in the code below +// cuBLASDx follows the BLAS convention: matrices are col-major, so we swap A & B in the code below +template +TileC& tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C) +{ + using T = typename TileA::Type; -#define tile_matmul(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C) \ - do { \ - void fun_forward(dtype, dtype*, dtype*, dtype, dtype*); \ - WP_TILE_SYNC(); \ - fun_forward(dtype(1.0), B.data, A.data, dtype(1.0), C.data); \ - WP_TILE_SYNC(); \ - } while (0) + WP_TILE_SYNC(); + fun_forward(T(1.0), B.data, A.data, T(1.0), C.data); + WP_TILE_SYNC(); + + return C; +} + +// backward for the wp.tile_matmul(a, b, out) syntax +template +void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C, + Fwd adj_fun_forward, AdjA adj_fun_backward_A, AdjB adj_fun_backward_B, TileA& adj_A, TileB& adj_B, TileC& adj_C) +{ + using T = typename TileA::Type; + + WP_TILE_SYNC(); + fun_backward_A(T(1.0), B.data, adj_C.data, T(1.0), adj_A.data); + fun_backward_B(T(1.0), adj_C.data, A.data, T(1.0), adj_B.data); + WP_TILE_SYNC(); +} + +// backward for the out = wp.tile_matmul(a, b) syntax +template +void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C, + Fwd adj_fun_forward, AdjA adj_fun_backward_A, AdjB adj_fun_backward_B, TileA& adj_A, TileB& adj_B, TileC& adj_C, TileC& adj_ret) +{ + using T = typename TileA::Type; + + WP_TILE_SYNC(); + fun_backward_A(T(1.0), B.data, adj_C.data, T(1.0), adj_A.data); + fun_backward_B(T(1.0), adj_C.data, A.data, T(1.0), adj_B.data); + WP_TILE_SYNC(); +} -// adj_fun_forward, adj_fun_backward_A, adj_fun_backward_B, adj_dtype are in practice ignored -// but are here because builtins.py creates them even though those are effectively compile time constants -#define adj_tile_matmul(fun_forward, fun_backward_A, fun_backward_B, dtype, A, B, C, \ - adj_fun_forward, adj_fun_backward_A, adj_fun_backward_B, adj_dtype, \ - adjA, adjB, adjC) \ - do { \ - void fun_backward_A(dtype, dtype*, dtype*, dtype, dtype*); \ - void fun_backward_B(dtype, dtype*, dtype*, dtype, dtype*); \ - WP_TILE_SYNC(); \ - fun_backward_A(dtype(1.0), B.data, adjC.data, dtype(1.0), adjA.data); \ - fun_backward_B(dtype(1.0), adjC.data, A.data, dtype(1.0), adjB.data); \ - WP_TILE_SYNC(); \ - } while (0) #define tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, Xinout) \ do { \ @@ -1112,11 +1136,24 @@ inline CUDA_CALLABLE auto tile_transpose(Tile& t) template inline CUDA_CALLABLE void adj_tile_transpose(Tile& t, Tile& adj_t, AdjTile& adj_ret) { - auto a = adj_t.copy_to_register(); - auto b = t.copy_to_register(); + auto a = tile_transpose(adj_ret); + auto b = adj_t; adj_t.assign(tile_add(a,b)); } +template +inline CUDA_CALLABLE auto tile_broadcast(Tile& t) +{ + // alias incoming tile with new strides + return tile_shared_t(t.data); +} + +template +inline CUDA_CALLABLE void adj_tile_broadcast(Tile& t, Tile& adj_t, AdjTile& adj_ret) +{ + // todo: +} + } // namespace wp diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index fcd394aa..e94521e0 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -534,4 +534,4 @@ class TestTile(unittest.TestCase): if __name__ == "__main__": wp.clear_kernel_cache() - unittest.main(verbosity=2) + unittest.main(verbosity=2, failfast=True) From f92d9e9396b08fa4bff8bfa427588e77076639fe Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 1 Oct 2024 02:30:25 +0000 Subject: [PATCH 049/102] Add tests for wp.tile_broadcast() Add support for 1D tile loads --- warp/builtins.py | 148 +++++++++++++++++++++++++------ warp/native/tile.h | 190 ++++++++++++++++++++++++++++++++++------ warp/tests/test_tile.py | 80 +++++++++++++++-- 3 files changed, 356 insertions(+), 62 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 5491e3de..23dbfcca 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1883,29 +1883,82 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a export=False, ) - -def tile_load_value_func(arg_types, arg_values): +def tile_load_1d_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: return Tile(dtype=Any, M=Any, N=Any) - # if len(arg_types) != 3: - # raise RuntimeError("tile_load() requires 3 positional args") + if not is_array(arg_types["a"]): + raise RuntimeError("tile_load() argument 0 must be an array") + + if arg_types["a"].ndim != 1: + raise RuntimeError("tile_load() argument 0 must be 1-dimensional if using the ``wp.tile_load(array, i, n)`` syntax.") + + if not type_is_int(arg_types["i"]): + raise RuntimeError("tile_load() argument 1 must be an integer") + + if "n" not in arg_values: + raise RuntimeError("'n' keyword argument must be specified when calling tile_load() function") + + a = arg_types["a"] + m, n = 1, arg_values["n"] + + return TileLoad(a, 1, n) + + +def tile_load_1d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): + array = arg_values["a"] + i = arg_values["i"] + n = arg_values["n"].constant + dtype = arg_values["a"].type.dtype + + template_args = [] + template_args.append(dtype) + template_args.append(n) + + return ((array, i), template_args) + +add_builtin( + "tile_load", + input_types={"a": array(dtype=Any), "i": int, "n": int}, + value_func=tile_load_1d_value_func, + dispatch_func=tile_load_1d_dispatch_func, + variadic=False, + doc="""Loads a 1D tile from a global memory array. + + This method will cooperatively load a tile from global memory using all threads in the block. + + :param a: The source array in global memory + :param i: Offset in the source array measured in multiples of ``n``, i.e.: ``offset=i*n`` + :param n: The number of elements in the tile + :returns: A tile with ``shape=(1,n)`` and dtype the same as the source array""", + group="Tile Primitives", + export=False, +) + + +def tile_load_2d_value_func(arg_types, arg_values): + # return generic type (for doc builds) + if arg_types is None: + return Tile(dtype=Any, M=Any, N=Any) if not is_array(arg_types["a"]): raise RuntimeError("tile_load() argument 0 must be an array") - if not type_is_int(arg_types["x"]): + if arg_types["a"].ndim != 2: + raise RuntimeError("tile_load() argument 0 must be 2-dimensional if using the ``wp.tile_load(array, i, j, m, n)`` syntax.") + + if not type_is_int(arg_types["i"]): raise RuntimeError("tile_load() argument 1 must be an integer") - if not type_is_int(arg_types["y"]): + if not type_is_int(arg_types["j"]): raise RuntimeError("tile_load() argument 1 must be an integer") if "m" not in arg_values: - raise RuntimeError("'m' keyword argument must be specified when calling tile_zeros() function") + raise RuntimeError("'m' keyword argument must be specified when calling tile_load() function") if "n" not in arg_values: - raise RuntimeError("'n' keyword argument must be specified when calling tile_zeros() function") + raise RuntimeError("'n' keyword argument must be specified when calling tile_load() function") a = arg_types["a"] m, n = arg_values["m"], arg_values["n"] @@ -1913,9 +1966,9 @@ def tile_load_value_func(arg_types, arg_values): return TileLoad(a, m, n) -def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): +def tile_load_2d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): array = arg_values["a"] - x, y = arg_values["x"], arg_values["y"] + i, j = arg_values["i"], arg_values["j"] m, n = arg_values["m"].constant, arg_values["n"].constant dtype = arg_values["a"].type.dtype @@ -1924,31 +1977,70 @@ def tile_load_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg template_args.append(m) template_args.append(n) - return ((array, x, y), template_args) + return ((array, i, j), template_args) add_builtin( "tile_load", - input_types={"a": array(dtype=Any), "x": int, "y": int, "m": int, "n": int}, - value_func=tile_load_value_func, - dispatch_func=tile_load_dispatch_func, - variadic=True, - doc="""Loads a tile from a global memory array. + input_types={"a": array(dtype=Any), "i": int, "j": int, "m": int, "n": int}, + value_func=tile_load_2d_value_func, + dispatch_func=tile_load_2d_dispatch_func, + variadic=False, + doc="""Loads a 2D tile from a global memory array. This method will cooperatively load a tile from global memory using all threads in the block. :param a: The source array in global memory - :param x: Offset in the source array measured in multiples of ``m``, i.e.: ``i=x*m`` - :param y: Offset in the source array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param i: Offset in the source array measured in multiples of ``m``, i.e.: ``row=i*m`` + :param j: Offset in the source array measured in multiples of ``n``, i.e.; ``col=j*n`` :param m: The size of the tile's first dimension - :param n: The size of the tile's second dimensions + :param n: The size of the tile's second dimension :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array""", group="Tile Primitives", export=False, ) -def tile_store_value_func(arg_types, arg_values): +def tile_store_1d_value_func(arg_types, arg_values): + # return generic type (for doc builds) + if arg_types is None: + return None + + if len(arg_types) != 3: + raise RuntimeError("tile_store() requires 3 positional args") + + if not is_array(arg_types["a"]): + raise RuntimeError("tile_store() argument 0 must be an array") + + if not type_is_int(arg_types["i"]): + raise RuntimeError("tile_store() argument 1 must be an integer") + + if not is_tile(arg_types["t"]): + raise RuntimeError("tile_store() argument 2 must be a tile") + + if not types_equal(arg_types["a"].dtype, arg_types["t"].dtype): + raise RuntimeError("tile_store() destination array must have same type as source tile") + + return None + + +add_builtin( + "tile_store", + input_types={"a": array(dtype=Any), "i": int, "t": Any}, + value_func=tile_store_1d_value_func, + variadic=False, + doc="""Stores a 1D tile to a global memory array. + + This method will cooperatively store a tile to global memory using all threads in the block. + + :param a: The destination array in global memory + :param i: Offset in the destination array measured in multiples of ``n``, i.e.: ``offset=i*n`` + :param t: The source tile to store data from, must have the same dtype as the destination array""", + group="Tile Primitives", + export=False, +) + +def tile_store_2d_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: return None @@ -1959,10 +2051,10 @@ def tile_store_value_func(arg_types, arg_values): if not is_array(arg_types["a"]): raise RuntimeError("tile_store() argument 0 must be an array") - if not type_is_int(arg_types["x"]): + if not type_is_int(arg_types["i"]): raise RuntimeError("tile_store() argument 1 must be an integer") - if not type_is_int(arg_types["y"]): + if not type_is_int(arg_types["j"]): raise RuntimeError("tile_store() argument 2 must be an integer") if not is_tile(arg_types["t"]): @@ -1976,16 +2068,16 @@ def tile_store_value_func(arg_types, arg_values): add_builtin( "tile_store", - input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, - value_func=tile_store_value_func, - variadic=True, + input_types={"a": array(dtype=Any), "i": int, "j": int, "t": Any}, + value_func=tile_store_2d_value_func, + variadic=False, doc="""Stores a tile to a global memory array. This method will cooperatively store a tile to global memory using all threads in the block. :param a: The destination array in global memory - :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*m`` - :param y: Offset in the destination array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param i: Offset in the destination array measured in multiples of ``m``, i.e.: ``row=i*m`` + :param j: Offset in the destination array measured in multiples of ``n``, i.e.; ``col=j*n`` :param t: The source tile to store data from, must have the same dtype as the destination array""", group="Tile Primitives", export=False, @@ -2239,7 +2331,7 @@ def tile_broadcast_value_func(arg_types, arg_values): n = arg_values["n"] if not is_tile(t): - raise RuntimeError("tile_transpose() argument 0 must be a tile") + raise RuntimeError("tile_broadcast() argument 0 must be a tile") # try to broadcast last dimension if t.N == 1: diff --git a/warp/native/tile.h b/warp/native/tile.h index 7910c21d..1f3b5119 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -88,6 +88,8 @@ [ ] warp.sim (CRBA) [ ] Batched MLP [ ] Layer norm + [ ] FNO + Burgers equation + [ ] Stochastic financial modeling [ ] Convolution: https://github.com/NVIDIA/MinkowskiEngine/blob/master/src/convolution_kernel.cu#L123 [ ] MeshCNN (Modulus, Oliver) [ ] BioNemo (Ali) @@ -142,7 +144,7 @@ struct coord_t // represents a tile stored in global memory with dynamic strides // only used to represent the source for tile loads to register/shared -template +template struct tile_global_t { using Type = T; @@ -183,20 +185,16 @@ struct tile_register_t data[i] = value; } - inline CUDA_CALLABLE tile_register_t(tile_global_t& t) - { - // construct from a global tile - copy_from_global(t.data, t.x, t.y); - } - - - inline CUDA_CALLABLE auto& operator=(const tile_global_t& t) + inline CUDA_CALLABLE auto& operator=(const tile_global_t& t) { - // assign from a global tile - copy_from_global(t.data, t.x, t.y); + if (t.data.ndim == 1) + copy_from_global(t.data, t.x); // 1d load + else + copy_from_global(t.data, t.x, t.y); // 2d load + return *this; - } + } inline CUDA_CALLABLE T& operator()(int index) { @@ -288,11 +286,34 @@ struct tile_register_t // return the in-register version of this tile (nop) - inline CUDA_CALLABLE auto& copy_to_register() { return *this; } + inline CUDA_CALLABLE auto& copy_to_register() + { + return *this; + } + + void copy_to_global(array_t dest, int x) + { + assert(dest.ndim == 1); + const int tile_i = x*N; + + WP_PRAGMA_UNROLL + for (int i=0; i < NumRegs; ++i) + { + // handle case where tile size is not + // aligned to block dimensions + int linear = index(i); + if (!Aligned && linear >= Size) + break; + + wp::index(dest, tile_i + linear) = data[i]; + } + } void copy_to_global(array_t dest, int x, int y) { + assert(dest.ndim == 2); + const int tile_i = x*M; const int tile_j = y*N; @@ -317,6 +338,22 @@ struct tile_register_t } } + inline CUDA_CALLABLE void copy_from_global(const array_t& src, int x) + { + // todo: use async pipelines or TMA here + const int tile_i = x*N; + + WP_PRAGMA_UNROLL + for (int i=0; i < NumRegs; ++i) + { + int linear = index(i); + if (!Aligned && linear >= Size) + break; + + data[i] = wp::index(src, tile_i + linear); + } + } + inline CUDA_CALLABLE void copy_from_global(const array_t& src, int x, int y) { // todo: use async pipelines or TMA here @@ -374,12 +411,6 @@ struct tile_shared_t { } - // construct from a global tile - inline CUDA_CALLABLE tile_shared_t(tile_global_t& t) - { - copy_from_global(t.array, t.x, t.y); - } - // assign from a register tile template inline CUDA_CALLABLE auto& operator=(const Tile& t) @@ -405,9 +436,13 @@ struct tile_shared_t } // assign from a global tile - inline CUDA_CALLABLE auto& operator=(const tile_global_t& t) - { - copy_from_global(t.data, t.x, t.y); + inline CUDA_CALLABLE auto& operator=(const tile_global_t& t) + { + if (t.data.ndim == 1) + copy_from_global(t.data, t.x); // 1d load + else + copy_from_global(t.data, t.x, t.y); // 2d load + return *this; } @@ -549,6 +584,21 @@ struct tile_shared_t return out; } + inline CUDA_CALLABLE void copy_to_global(array_t dest, int x) + { + assert(dest.ndim == 1); + + // todo: use TMA here + const int tile_i = x*N; + + WP_PRAGMA_UNROLL + for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM) + { + coord_t c = coord(i); + wp::index(dest, tile_i + linear) = (*this)(c.i, c.j); + } + } + inline CUDA_CALLABLE void copy_to_global(array_t dest, int x, int y) { // todo: use TMA here @@ -570,6 +620,18 @@ struct tile_shared_t } } + inline CUDA_CALLABLE void copy_from_global(const array_t& src, int x) + { + // todo: use async pipelines or TMA here + const int tile_i = x*N; + + WP_PRAGMA_UNROLL + for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM) + { + (*this)(i) = wp::index(src, tile_i + i); + } + } + inline CUDA_CALLABLE void copy_from_global(const array_t& src, int x, int y) { // todo: use async pipelines or TMA here @@ -750,17 +812,29 @@ template inline CUDA_CALLABLE void adj_tile_arange(int start, int stop, int step, int adj_start, int adj_stop, int adj_step, AdjTile& adj_ret) {} -// entry point for load +// entry point for 1d load +template +inline CUDA_CALLABLE auto tile_load(array_t& src, int x) +{ + return tile_global_t(src, x, 0); +} + +// entry point for 2d load template inline CUDA_CALLABLE auto tile_load(array_t& src, int x, int y) { - // just return a ref. to the global memory - // it will be loaded to shared or registers - // on assignment to the variable - return tile_global_t(src, x, y); + return tile_global_t(src, x, y); } -// entry point for store +// entry point for 1d store +template +inline CUDA_CALLABLE void tile_store(array_t& dest, int x, Tile& src) +{ + // dispatch to tile type + src.copy_to_global(dest, x); +} + +// entry point for 2d store template inline CUDA_CALLABLE void tile_store(array_t& dest, int x, int y, Tile& src) { @@ -800,6 +874,36 @@ inline CUDA_CALLABLE auto tile_atomic_add(array_t& dest, int x, int y, Tile& //------------------------------------- // Adjoints +template +inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, + array_t& adj_src, int adj_x, + AdjTile& adj_ret) +{ + // early out + // if (!src.grad) + // return; + + auto adj_reg = adj_ret.copy_to_register(); + + const int tile_i = x*adj_reg.N; + + // add gradients to src array + WP_PRAGMA_UNROLL + for (int i=0; i < adj_reg.NumRegs; ++i) + { + int linear = adj_reg.index(i); + if (!adj_reg.Aligned && linear >= adj_reg.Size) + break; + + auto grad = adj_reg.data[i]; + + if (adj_src.data) + adj_atomic_add(&index(adj_src, tile_i + linear), grad); + else if (src.grad) + adj_atomic_add(&index_grad(src, tile_i + linear), grad); + } +} + template inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, array_t& adj_src, int adj_x, int adj_y, @@ -833,6 +937,36 @@ inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, } } + +template +inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, Tile& t, array_t& adj_dest, int adj_x, AdjTile& adj_t) +{ + // if (!dest.grad) + // return; + + // convert to register if necessary + auto adj_reg = adj_t.copy_to_register(); + + const int tile_i = x*adj_reg.N; + + // load gradients from output + WP_PRAGMA_UNROLL + for (int i=0; i < adj_reg.NumRegs; ++i) + { + int linear = adj_reg.index(i); + if (!adj_reg.Aligned && linear >= adj_reg.Size) + break; + + if (adj_dest.data) + adj_reg.data[i] += index(adj_dest, tile_i + linear); + else if (dest.grad) + adj_reg.data[i] += index_grad(dest, tile_i + linear); + } + + // store adjoint back to tile + adj_t.assign(adj_reg); +} + template inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, AdjTile& adj_t) { diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index e94521e0..aceff12e 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -21,9 +21,46 @@ # num threads per-tile TILE_DIM = 64 +@wp.kernel +def tile_copy_1d_kernel(A: wp.array(dtype=float), B: wp.array(dtype=float)): + # tile index + i = wp.tid() + + a = wp.tile_load(A, i, n=TILE_N) + wp.tile_store(B, i, a) + + +def test_tile_copy_1d(test, device): + rng = np.random.default_rng(42) + + N = TILE_N * 5 + + A = rng.random((N), dtype=np.float32) + B = rng.random((N), dtype=np.float32) + + A_wp = wp.array(A, requires_grad=True, device=device) + B_wp = wp.array(B, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch_tiled( + tile_copy_1d_kernel, + dim=[int(N / TILE_N)], + inputs=[A_wp, B_wp], + block_dim=TILE_DIM, + device=device, + ) + + # verify forward pass + assert_array_equal(B_wp, A_wp) + + # verify backward pass + B_wp.grad = wp.ones_like(B_wp, device=device) + tape.backward() + + assert_array_equal(B_wp.grad, A_wp.grad) @wp.kernel -def tile_copy(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): +def tile_copy_2d_kernel(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): # tile index i, j = wp.tid() @@ -31,7 +68,7 @@ def tile_copy(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): wp.tile_store(B, i, j, a) -def test_tile_copy(test, device): +def test_tile_copy_2d(test, device): rng = np.random.default_rng(42) M = TILE_M * 7 @@ -45,7 +82,7 @@ def test_tile_copy(test, device): with wp.Tape() as tape: wp.launch_tiled( - tile_copy, + tile_copy_2d_kernel, dim=[int(M / TILE_M), int(N / TILE_N)], inputs=[A_wp, B_wp], block_dim=TILE_DIM, @@ -434,6 +471,35 @@ def test_tile_transpose_matmul(test, device): assert_np_equal(output.numpy(), input.numpy().T @ input.numpy()) +@wp.kernel +def test_tile_broadcast_kernel( + input_a: wp.array2d(dtype=float), + input_b: wp.array(dtype=float), + output: wp.array2d(dtype=float)): + + a = wp.tile_load(input_a, 0, 0, m=10, n=10) + b = wp.tile_load(input_b, 0, n=10) + + c = wp.tile_broadcast(b, 10, 10) + d = a + c + + wp.tile_store(output, 0, 0, d) + +def test_tile_broadcast(test, device): + + M = 10 + N = 10 + + a = wp.array(np.ones((M,N), dtype=np.float32), device=device) + b = wp.array(np.arange(0, N, dtype=np.float32), device=device) + out = wp.zeros((M,N), dtype=float, device=device) + + wp.launch_tiled(test_tile_broadcast_kernel, dim=[1], inputs=[a, b, out], block_dim=32) + + assert_np_equal(out.numpy(), a.numpy() + b.numpy()) + + + # #----------------------------------------- # # center of mass computation @@ -520,16 +586,18 @@ class TestTile(unittest.TestCase): pass -add_function_test(TestTile, "test_tile_copy", test_tile_copy, devices=devices) +add_function_test(TestTile, "test_tile_copy_1d", test_tile_copy_1d, devices=devices) +add_function_test(TestTile, "test_tile_copy_2d", test_tile_copy_2d, devices=devices) add_function_test(TestTile, "test_tile_unary_map", test_tile_unary_map, devices=devices) add_function_test(TestTile, "test_tile_binary_map", test_tile_binary_map, devices=devices) -add_function_test(TestTile, "test_tile_grouped_gemm", test_tile_grouped_gemm, devices=devices) # FAILS +add_function_test(TestTile, "test_tile_grouped_gemm", test_tile_grouped_gemm, devices=devices) add_function_test(TestTile, "test_tile_gemm", test_tile_gemm, devices=devices) -add_function_test(TestTile, "test_tile_transpose", test_tile_transpose, devices=devices) # FAILS +add_function_test(TestTile, "test_tile_transpose", test_tile_transpose, devices=devices) add_function_test(TestTile, "test_tile_transpose_matmul", test_tile_transpose_matmul, devices=devices) add_function_test(TestTile, "test_tile_operators", test_tile_operators, devices=devices) add_function_test(TestTile, "test_tile_sum", test_tile_sum, devices=devices) add_function_test(TestTile, "test_tile_extract", test_tile_extract, devices=devices) +add_function_test(TestTile, "test_tile_broadcast", test_tile_broadcast, devices=devices) if __name__ == "__main__": From 226770e438d54344d3dcbbc32ff53f83b8ab22be Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 1 Oct 2024 04:34:51 +0000 Subject: [PATCH 050/102] Add support for gradients through broadcasting --- warp/builtins.py | 9 ++++ warp/native/tile.h | 101 ++++++++++++++++++++++++++++++---------- warp/tests/test_tile.py | 36 ++++++++++++-- warp/types.py | 14 +++--- 4 files changed, 124 insertions(+), 36 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 23dbfcca..3747777c 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -2012,6 +2012,9 @@ def tile_store_1d_value_func(arg_types, arg_values): if not is_array(arg_types["a"]): raise RuntimeError("tile_store() argument 0 must be an array") + if arg_types["a"].ndim != 1: + raise RuntimeError("tile_load() argument 0 must be a 1-dimensional array if using the ``wp.tile_store(array, i, t)`` syntax.") + if not type_is_int(arg_types["i"]): raise RuntimeError("tile_store() argument 1 must be an integer") @@ -2029,6 +2032,7 @@ def tile_store_1d_value_func(arg_types, arg_values): input_types={"a": array(dtype=Any), "i": int, "t": Any}, value_func=tile_store_1d_value_func, variadic=False, + skip_replay=True, doc="""Stores a 1D tile to a global memory array. This method will cooperatively store a tile to global memory using all threads in the block. @@ -2051,6 +2055,9 @@ def tile_store_2d_value_func(arg_types, arg_values): if not is_array(arg_types["a"]): raise RuntimeError("tile_store() argument 0 must be an array") + if arg_types["a"].ndim != 2: + raise RuntimeError("tile_load() argument 0 must be a 2-dimensional array if using the ``wp.tile_store(array, i, j, t)`` syntax.") + if not type_is_int(arg_types["i"]): raise RuntimeError("tile_store() argument 1 must be an integer") @@ -2071,6 +2078,7 @@ def tile_store_2d_value_func(arg_types, arg_values): input_types={"a": array(dtype=Any), "i": int, "j": int, "t": Any}, value_func=tile_store_2d_value_func, variadic=False, + skip_replay=True, doc="""Stores a tile to a global memory array. This method will cooperatively store a tile to global memory using all threads in the block. @@ -2115,6 +2123,7 @@ def tile_atomic_add_value_func(arg_types, arg_values): input_types={"a": array(dtype=Any), "x": int, "y": int, "t": Any}, value_func=tile_atomic_add_value_func, variadic=True, + skip_replay=True, doc="""Atomically add a tile to the array `a`, each element will be updated atomically. :param a: Array in global memory, should have the same ``dtype`` as the input tile diff --git a/warp/native/tile.h b/warp/native/tile.h index 1f3b5119..e8b9bd8d 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -235,6 +235,12 @@ struct tile_register_t data[i] = tile.data[i]; } + inline CUDA_CALLABLE void add(const tile_register_t& tile) + { + for (int i=0; i < NumRegs; ++i) + data[i] += tile.data[i]; + } + inline CUDA_CALLABLE void zero() { for (int i=0; i < NumRegs; ++i) @@ -384,6 +390,15 @@ struct tile_register_t } }; +// helper to allocate a register tile like another tile +template +auto tile_register_like() +{ + using T = typename Tile::Type; + + return tile_register_t(T(0.0)); +} + template @@ -540,6 +555,25 @@ struct tile_shared_t } } + inline CUDA_CALLABLE void add(const tile_register_t& tile) + { + WP_PRAGMA_UNROLL + for (int i=0; i < tile.NumRegs; ++i) + { + const int linear = tile.index(i); + + // handle case where tile size is not + // aligned to block dimensions + if (!Aligned && linear >= Size) + break; + + // use shared memory atomics to accumulate gradients + // since for broadcast tiles multiple incoming values + // may map to a single location in shared memory + atomic_add(&(*this)(linear), tile.data[i]); + } + } + inline CUDA_CALLABLE void print() { WP_TILE_SYNC(); @@ -594,8 +628,7 @@ struct tile_shared_t WP_PRAGMA_UNROLL for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM) { - coord_t c = coord(i); - wp::index(dest, tile_i + linear) = (*this)(c.i, c.j); + wp::index(dest, tile_i + i) = (*this)(i); } } @@ -712,15 +745,18 @@ inline CUDA_CALLABLE auto tile_alloc_empty() return tile_shared_t(data); } -template +template inline CUDA_CALLABLE auto tile_alloc_zeros() { - WP_TILE_SHARED __align__(16) T data[M*N]; + // compute the total storage required for the tile (may be different from M*N) for broadcast tiles + constexpr int Len = (M-1)*StrideM + (N-1)*StrideN + 1; + + WP_TILE_SHARED __align__(16) T data[Len]; - for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) + for (int i=threadIdx.x; i < Len; i+= WP_TILE_BLOCK_DIM) data[i] = T(0); - return tile_shared_t(data); + return tile_shared_t(data); } @@ -808,9 +844,9 @@ inline CUDA_CALLABLE auto tile_arange(T start, T stop, T step) return out; } -template -inline CUDA_CALLABLE void adj_tile_arange(int start, int stop, int step, - int adj_start, int adj_stop, int adj_step, AdjTile& adj_ret) {} +template +inline CUDA_CALLABLE void adj_tile_arange(T start, T stop, T step, + T& adj_start, T& adj_stop, T& adj_step, AdjTile& adj_ret) {} // entry point for 1d load template @@ -945,7 +981,7 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, Tile& t, array // return; // convert to register if necessary - auto adj_reg = adj_t.copy_to_register(); + tile_register_t adj_reg; const int tile_i = x*adj_reg.N; @@ -958,13 +994,13 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, Tile& t, array break; if (adj_dest.data) - adj_reg.data[i] += index(adj_dest, tile_i + linear); + adj_reg.data[i] = index(adj_dest, tile_i + linear); else if (dest.grad) - adj_reg.data[i] += index_grad(dest, tile_i + linear); + adj_reg.data[i] = index_grad(dest, tile_i + linear); } // store adjoint back to tile - adj_t.assign(adj_reg); + adj_t.add(adj_reg); } template @@ -974,7 +1010,7 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t // return; // convert to register if necessary - auto adj_reg = adj_t.copy_to_register(); + tile_register_t adj_reg; const int tile_i = x*adj_reg.M; const int tile_j = y*adj_reg.N; @@ -990,13 +1026,13 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t coord_t coord = adj_reg.coord(linear); if (adj_dest.data) - adj_reg.data[i] += index(adj_dest, tile_i + coord.i, tile_j + coord.j); + adj_reg.data[i] = index(adj_dest, tile_i + coord.i, tile_j + coord.j); else if (dest.grad) - adj_reg.data[i] += index_grad(dest, tile_i + coord.i, tile_j + coord.j); + adj_reg.data[i] = index_grad(dest, tile_i + coord.i, tile_j + coord.j); } // store adjoint back to tile - adj_t.assign(adj_reg); + adj_t.add(adj_reg); } template @@ -1023,6 +1059,7 @@ inline CUDA_CALLABLE auto tile_map(Fwd op, return out; } + template inline CUDA_CALLABLE void adj_tile_map(Fwd op, Tile& a, @@ -1031,7 +1068,7 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op, AdjTile& adj_ret) { auto a_reg = a.copy_to_register(); - auto adj_a_reg = adj_a.copy_to_register(); + auto adj_a_reg = tile_register_like(); auto adj_ret_reg = adj_ret.copy_to_register(); WP_PRAGMA_UNROLL @@ -1041,7 +1078,7 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op, } // write adjoints back - adj_a.assign(adj_a_reg); + adj_a.add(adj_a_reg); } // binary map @@ -1062,6 +1099,7 @@ inline CUDA_CALLABLE auto tile_map(Fwd op, return out; } + template inline CUDA_CALLABLE void adj_tile_map(Fwd op, TileA &a, @@ -1073,8 +1111,11 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op, { auto a_reg = a.copy_to_register(); auto b_reg = b.copy_to_register(); - auto adj_a_reg = adj_a.copy_to_register(); - auto adj_b_reg = adj_b.copy_to_register(); + + // allocate storage for adjoints + auto adj_a_reg = tile_register_like(); + auto adj_b_reg = tile_register_like(); + auto adj_ret_reg = adj_ret.copy_to_register(); WP_PRAGMA_UNROLL @@ -1083,8 +1124,8 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op, adj_op(a_reg.data[i], b_reg.data[i], adj_a_reg.data[i], adj_b_reg.data[i], adj_ret_reg.data[i]); } - adj_a.assign(adj_a_reg); - adj_b.assign(adj_b_reg); + adj_a.add(adj_a_reg); + adj_b.add(adj_b_reg); } // wrap the operator in a lambda so that we don't have to do overload resolution for things like e.g.: wp.sin() @@ -1286,8 +1327,18 @@ inline CUDA_CALLABLE auto tile_broadcast(Tile& t) template inline CUDA_CALLABLE void adj_tile_broadcast(Tile& t, Tile& adj_t, AdjTile& adj_ret) { - // todo: -} + constexpr int LenTile = (Tile::M-1)*Tile::StrideM + (Tile::N-1)*Tile::StrideN + 1; + constexpr int LenAdjTile = (AdjTile::M-1)*AdjTile::StrideM + (AdjTile::N-1)*AdjTile::StrideN + 1; + static_assert(LenTile == LenAdjTile); + + // since the incoming adjoint will have the same physical storage + // as the original tile (just with different strides and expanded dimensions), + // we can simply update the gradient element by element + for (int i=threadIdx.x; i < LenTile; i+=WP_TILE_BLOCK_DIM) + { + adj_t.data[i] += adj_ret.data[i]; + } +} } // namespace wp diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index aceff12e..8b1d3157 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -359,7 +359,7 @@ def tile_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dtype=float a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) s = wp.tile_sum(a) * 0.5 - wp.tile_store(output, i, 0, s) + wp.tile_store(output, i, s) def test_tile_sum(test, device): @@ -472,7 +472,7 @@ def test_tile_transpose_matmul(test, device): @wp.kernel -def test_tile_broadcast_kernel( +def test_tile_broadcast_add_kernel( input_a: wp.array2d(dtype=float), input_b: wp.array(dtype=float), output: wp.array2d(dtype=float)): @@ -485,7 +485,7 @@ def test_tile_broadcast_kernel( wp.tile_store(output, 0, 0, d) -def test_tile_broadcast(test, device): +def test_tile_broadcast_add(test, device): M = 10 N = 10 @@ -494,11 +494,36 @@ def test_tile_broadcast(test, device): b = wp.array(np.arange(0, N, dtype=np.float32), device=device) out = wp.zeros((M,N), dtype=float, device=device) - wp.launch_tiled(test_tile_broadcast_kernel, dim=[1], inputs=[a, b, out], block_dim=32) + wp.launch_tiled(test_tile_broadcast_add_kernel, dim=[1], inputs=[a, b, out], block_dim=32) assert_np_equal(out.numpy(), a.numpy() + b.numpy()) +@wp.kernel +def test_tile_broadcast_grad_kernel( + a: wp.array(dtype=float), + b: wp.array2d(dtype=float)): + + x = wp.tile_load(a, i=0, n=5) + y = wp.tile_broadcast(x, m=5, n=5) + + w = wp.tile_ones(dtype=float, m=5, n=5) + z = w + y + + wp.tile_store(b, 0, 0, z) + +def test_tile_broadcast_grad(test, device): + + a = wp.array(np.arange(0, 5, dtype=np.float32), requires_grad=True) + b = wp.array(np.ones((5, 5), dtype=np.float32), requires_grad=True) + + with wp.Tape() as tape: + wp.launch_tiled(test_tile_broadcast_grad_kernel, dim=[1], inputs=[a, b], block_dim=32) + + b.grad = wp.ones_like(b) + tape.backward() + + assert_np_equal(a.grad.numpy(), np.ones(5)*5.0) # #----------------------------------------- # # center of mass computation @@ -597,7 +622,8 @@ class TestTile(unittest.TestCase): add_function_test(TestTile, "test_tile_operators", test_tile_operators, devices=devices) add_function_test(TestTile, "test_tile_sum", test_tile_sum, devices=devices) add_function_test(TestTile, "test_tile_extract", test_tile_extract, devices=devices) -add_function_test(TestTile, "test_tile_broadcast", test_tile_broadcast, devices=devices) +add_function_test(TestTile, "test_tile_broadcast_add", test_tile_broadcast_add, devices=devices) +add_function_test(TestTile, "test_tile_broadcast_grad", test_tile_broadcast_grad, devices=devices) if __name__ == "__main__": diff --git a/warp/types.py b/warp/types.py index 7e244863..9a0aa8a0 100644 --- a/warp/types.py +++ b/warp/types.py @@ -2982,7 +2982,7 @@ def ctype(self): if self.storage == "register": return f"wp::tile_register_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}>" elif self.storage == "shared": - return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N}, {self.strides[0]}, {self.strides[1]}>" + return f"wp::tile_shared_t<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{self.strides[0]}, {self.strides[1]}>" else: raise RuntimeError(f"Unrecognized tile storage type {self.storage}") @@ -2995,15 +2995,17 @@ def cinit(self, adjoint=False): elif self.storage == "shared": # if this is a reference to another tile # then don't allocate any memory - if self.owner == False: - return "NULL" if adjoint: # backward pass requires zeroed memory - return f"wp::tile_alloc_zeros<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" + return f"wp::tile_alloc_zeros<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{self.strides[0]}, {self.strides[1]}, {Tile.alloc()}>()" else: - # forward mode can be uninitialized until first used by the kernel - return f"wp::tile_alloc_empty<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" + if self.owner == False: + # will be initialized by subsequent call, e.g.: t = tile_broadcast(a) + return "NULL" + else: + # forward mode can be uninitialized until first used by the kernel + return f"wp::tile_alloc_empty<{Var.type_to_ctype(self.dtype)},{self.M},{self.N},{Tile.alloc()}>()" # generate a unique allocation index for shared memory @classmethod From deafbe9c5db6f463f37de2af8941fe5713026882 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 1 Oct 2024 07:49:58 +0000 Subject: [PATCH 051/102] Add support for tiling vectors with gradients --- warp/builtins.py | 36 ++++++++++++++++------- warp/native/tile.h | 73 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 90 insertions(+), 19 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 3747777c..19b1254c 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -2144,11 +2144,17 @@ def tile_value_func(arg_types, arg_values): if len(arg_types) != 1: raise RuntimeError("tile() requires 1 positional arg") - # todo: we need a way to pass things like current compiler options - # into the value_func, for now we use a single global options dictionary - # we should ideally pass in the Adjoint object if it exists + dtype = None + length = None - return Tile(dtype=arg_types["x"], M=1, N=warp.codegen.options["block_dim"], op="Tile") + if type_is_vector(arg_types["x"]): + dtype = arg_types["x"]._wp_scalar_type_ + length = arg_types["x"]._shape_[0] + else: + dtype = arg_types["x"] + length = 1 + + return Tile(dtype=dtype, M=length, N=warp.codegen.options["block_dim"], op="tile") add_builtin( @@ -2160,8 +2166,11 @@ def tile_value_func(arg_types, arg_values): This function converts values computed using scalar kernel code to a tile representation for input into collective operations. + * If the input value is a scalar then the resulting tile has ``shape=(1, block_dim)`` + * If the input value is a vector then the resulting tile has ``shape=(length(vector), block_dim)`` + :param x: A per-thread local value, e.g.: scalar, vector, or matrix. - :returns: A tile with ``shape=(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch()``. + :returns: A tile with first dimension according to the value type length and a second dimension equal to ``block_dim`` This example shows how to create a linear sequence from thread variables: @@ -2179,9 +2188,10 @@ def compute(): .. code-block:: text - tile(m=1, n=16, storage=register) = [[0 2 4 6 8 10 12 14...]] + tile(m=1, n=16, storage=register) = [[0 2 4 6 8 ...]] + """, - group="Tile Primitives" "", + group="Tile Primitives", export=False, ) @@ -2201,10 +2211,13 @@ def untile_value_func(arg_types, arg_values): if t.N != warp.codegen.options["block_dim"]: raise RuntimeError( - f"until() argument must have the same length as the block width, got {t.N}, expected {warp.codegen.options['block_dim']}" + f"untile() argument must have the same length as the block width, got {t.N}, expected {warp.codegen.options['block_dim']}" ) - return t.dtype + if t.M == 1: + return t.dtype + elif t.M > 1: + return warp.types.vector(t.M, t.dtype) add_builtin( @@ -2216,6 +2229,9 @@ def untile_value_func(arg_types, arg_values): This function converts a block-wide tile back to per-thread values. + * If the input tile is 1-dimensional then the resulting value will be a per-thread scalar + * If the input tile is 2-dimensional then the the resulting value will be a per-thread vector of length M + :param a: A tile with dimensions ``shape=(M, block_dim)`` :returns: A single value per-thread with the same dtype as the tile @@ -2248,7 +2264,7 @@ def compute(): 8 ... """, - group="Tile Primitives" "", + group="Tile Primitives", export=False, ) diff --git a/warp/native/tile.h b/warp/native/tile.h index e8b9bd8d..cd25c674 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -775,16 +775,42 @@ inline CUDA_CALLABLE auto tile(const T& x) return result; } +// overload for constructing a tile from a per-thread vector +template +inline CUDA_CALLABLE auto tile(const wp::vec_t& x) +{ + tile_register_t result; + + static_assert(result.NumRegs == Length); + + for (int i=0; i < Length; ++i) + result.data[i] = x[i]; + + return result; +} // construct a tile from a local SIMT value (one per-thread) template -inline CUDA_CALLABLE void adj_tile(const T& x, T& adj_x, const AdjTile& adj_ret) +inline CUDA_CALLABLE void adj_tile(const T& x, T& adj_x, AdjTile& adj_ret) { static_assert(AdjTile::M == 1); static_assert(AdjTile::N == WP_TILE_BLOCK_DIM); - static_assert(AdjTile::NumRegs == 1); + + auto adj_reg = adj_ret.copy_to_register(); + + adj_x += adj_reg.data[0]; +} + +template +inline CUDA_CALLABLE void adj_tile(const wp::vec_t& x, wp::vec_t& adj_x, AdjTile& adj_ret) +{ + static_assert(AdjTile::M == Length); + static_assert(AdjTile::N == WP_TILE_BLOCK_DIM); + + auto adj_reg = adj_ret.copy_to_register(); - adj_x += adj_ret.data[0]; + for (int i=0; i < Length; ++i) + adj_x[i] += adj_reg.data[i]; } template @@ -793,16 +819,45 @@ inline CUDA_CALLABLE auto untile(Tile& tile) // code-gen should have set the tile to // have exactly the block dimension so // there is exactly one value per-thread - static_assert(Tile::NumRegs == 1); + auto reg = tile.copy_to_register(); - return tile.copy_to_register().data[0]; + // scalar case + if constexpr(Tile::M == 1) + { + return reg.data[0]; + } + + // vector case + if constexpr(Tile::M > 1) + { + wp::vec_t v; + for (int i=0; i < Tile::M; ++i) + v[i] = reg.data[i]; + + return v; + } } -template -inline CUDA_CALLABLE void adj_untile(Tile& tile, Tile& adj_tile, typename Tile::Type& adj_ret) + + +template +inline CUDA_CALLABLE void adj_untile(Tile& tile, Tile& adj_tile, Value& adj_ret) { - auto adj = adj_tile.copy_to_register(); - adj.data[0] += adj_ret; + auto adj = adj_tile.copy_to_register(); + + // scalar case + if constexpr(Tile::M == 1) + { + adj.data[0] += adj_ret; + } + + // vector case + if constexpr(Tile::M > 1) + { + for (int i=0; i < Tile::M; ++i) + adj.data[i] = adj_ret[i]; + } + adj_tile.assign(adj); } From a947d5b657a327e7a04816cd329873598ed528f8 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 1 Oct 2024 08:42:52 +0000 Subject: [PATCH 052/102] Fix up tile reduce unit tests --- warp/tests/test_tile_reduce.py | 76 +++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index f0b60d86..5e48b62f 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -34,7 +34,7 @@ def tile_sum_kernel(input: wp.array2d(dtype=float), output: wp.array(dtype=float a = wp.tile_load(input, i, j, m=1, n=TILE_DIM) s += wp.tile_sum(a) * 0.5 - wp.tile_store(output, i, 0, s) + wp.tile_store(output, i, s) def test_tile_reduce_sum(test, device): @@ -73,7 +73,7 @@ def tile_min_kernel(input: wp.array2d(dtype=float), output: wp.array(dtype=float a = wp.tile_load(input, i, 0, m=1, n=TILE_DIM) m = wp.tile_min(a) - wp.tile_store(output, i, 0, m) + wp.tile_store(output, i, m) def test_tile_reduce_min(test, device): @@ -106,7 +106,7 @@ def tile_max_kernel(input: wp.array2d(dtype=float), output: wp.array(dtype=float a = wp.tile_load(input, i, 0, m=1, n=TILE_DIM) m = wp.tile_max(a) - wp.tile_store(output, i, 0, m) + wp.tile_store(output, i, m) def test_tile_reduce_max(test, device): @@ -139,7 +139,7 @@ def tile_reduce_custom_kernel(input: wp.array2d(dtype=float), output: wp.array(d a = wp.tile_load(input, i, 0, m=1, n=TILE_DIM) m = wp.tile_reduce(wp.mul, a) - wp.tile_store(output, i, 0, m) + wp.tile_store(output, i, m) def test_tile_reduce_custom(test, device): @@ -173,10 +173,10 @@ def tile_grouped_sum_kernel(input: wp.array3d(dtype=float), output: wp.array(dty # output tile index i = wp.tid() - a = wp.tile_load(input, i, 0, m=TILE_M, n=TILE_N) + a = wp.tile_load(input[i], 0, 0, m=TILE_M, n=TILE_N) s = wp.tile_sum(a) * 0.5 - wp.tile_store(output, i, 0, s) + wp.tile_store(output, i, s) def test_tile_reduce_grouped_sum(test, device): @@ -256,6 +256,60 @@ def test_tile_untile(test, device): assert_np_equal(output.numpy(), np.arange(N) * 2) +@wp.kernel +def tile_untile_scalar_kernel(output: wp.array(dtype=int)): + # thread index + i = wp.tid() + + # convert to block wide tile + t = wp.tile(i) * 2 + s = wp.untile(t) + + output[i] = s + + +def test_tile_untile_scalar(test, device): + # use an unaligned grid dimension + N = TILE_DIM * 4 + 5 + + output = wp.zeros(shape=N, dtype=int, requires_grad=True, device=device) + + with wp.Tape() as tape: + wp.launch(tile_untile_kernel, dim=N, inputs=[output], block_dim=TILE_DIM, device=device) + + assert_np_equal(output.numpy(), np.arange(N) * 2) + + + +@wp.kernel +def test_untile_vector_kernel( + input: wp.array(dtype=wp.vec3), + output: wp.array(dtype=wp.vec3)): + + i = wp.tid() + + v = input[i]*0.5 + + t = wp.tile(v) + u = wp.untile(t) + + output[i] = u*2.0 + +def test_tile_untile_vector(test, device): + + input = wp.full(16, wp.vec3(1.0, 2.0, 3.0), requires_grad=True) + output = wp.zeros_like(input) + + with wp.Tape() as tape: + wp.launch(test_untile_vector_kernel, dim=16, inputs=[input, output], block_dim=16) + + output.grad = wp.ones_like(output) + tape.backward() + + assert_np_equal(output.numpy(), input.numpy()) + assert_np_equal(input.grad.numpy(), np.ones((16, 3))) + + @wp.kernel def tile_ones_kernel(out: wp.array(dtype=float)): i = wp.tid() @@ -263,11 +317,12 @@ def tile_ones_kernel(out: wp.array(dtype=float)): t = wp.tile_ones(dtype=float, m=16, n=16) s = wp.tile_sum(t) - wp.tile_store(out, 0, 0, s) + wp.tile_store(out, 0, s) def test_tile_ones(test, device): - output = wp.zeros(shape=1, dtype=float, device=device) + + output = wp.zeros(1, dtype=float, device=device) with wp.Tape() as tape: wp.launch_tiled(tile_ones_kernel, dim=[1], inputs=[output], block_dim=TILE_DIM, device=device) @@ -316,8 +371,9 @@ class TestTileReduce(unittest.TestCase): add_function_test(TestTileReduce, "test_tile_reduce_simt", test_tile_reduce_simt, devices=devices) add_function_test(TestTileReduce, "test_tile_ones", test_tile_ones, devices=devices) add_function_test(TestTileReduce, "test_tile_arange", test_tile_arange, devices=devices) -add_function_test(TestTileReduce, "test_tile_untile", test_tile_untile, devices=devices) +add_function_test(TestTileReduce, "test_tile_untile_scalar", test_tile_untile_scalar, devices=devices) +add_function_test(TestTileReduce, "test_tile_untile_vector", test_tile_untile_vector, devices=devices) if __name__ == "__main__": wp.clear_kernel_cache() - unittest.main(verbosity=2) + unittest.main(verbosity=2, failfast=True) From 34f44c0f470160d3d212a46b434202bdbafb5f2c Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 1 Oct 2024 19:05:45 +0000 Subject: [PATCH 053/102] Update GEMM example --- warp/examples/tile/example_tile_matmul.py | 80 +++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 warp/examples/tile/example_tile_matmul.py diff --git a/warp/examples/tile/example_tile_matmul.py b/warp/examples/tile/example_tile_matmul.py new file mode 100644 index 00000000..881396f9 --- /dev/null +++ b/warp/examples/tile/example_tile_matmul.py @@ -0,0 +1,80 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +########################################################################### +# Example Tile MatMul +# +# Shows how to write a simple GEMM kernel using Warp tile primitives. +# +########################################################################### + +import numpy as np +import warp as wp + +# tile size +TILE_M = wp.constant(8) +TILE_N = wp.constant(4) +TILE_K = wp.constant(8) + +# num threads per-tile +TILE_THREADS = 64 + +@wp.kernel +def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): + + # output tile index + i, j = wp.tid() + + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + + M = A.shape[0] + N = B.shape[1] + K = A.shape[1] + + count = int(K / TILE_K) + + for k in range(0, count): + a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) + b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) + + # sum += a*b + wp.tile_matmul(a, b, sum) + + wp.tile_store(C, i, j, sum) + + + +if __name__ == "__main__": + + wp.set_device("cuda:0") + + # generate some tile aligned matrix dimensions + M = TILE_M * 7 + K = TILE_K * 6 + N = TILE_N * 5 + + rng = np.random.default_rng(42) + A = rng.random((M, K), dtype=np.float32) + B = rng.random((K, N), dtype=np.float32) + C = np.zeros((M, N), dtype=np.float32) + + A_wp = wp.array(A, requires_grad=True) + B_wp = wp.array(B, requires_grad=True) + C_wp = wp.array(C, requires_grad=True) + + with wp.Tape() as tape: + wp.launch_tiled( + tile_gemm, + dim=(int(M / TILE_M), int(N / TILE_N)), + inputs=[A_wp, B_wp, C_wp], + block_dim=TILE_THREADS) + + assert(np.allclose(C_wp.numpy(), A@B)) + + print("Example matrix multiplication passed") + + From 8caa97b590afecfdfe53afca952b2915e4875bc5 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 1 Oct 2024 12:05:57 -0700 Subject: [PATCH 054/102] Update tile branch with Warp 1.4.0 --- .github/workflows/ci.yml | 8 +- .github/workflows/sphinx.yml | 6 +- .gitlab-ci.yml | 14 +- .pre-commit-config.yaml | 2 +- CHANGELOG.md | 46 +- README.md | 11 +- VERSION.md | 2 +- docs/basics.rst | 4 + docs/codegen.rst | 1047 +++++++++++++++++++ docs/configuration.rst | 2 + docs/index.rst | 7 +- docs/installation.rst | 7 +- docs/modules/contribution_guide.rst | 333 ++++++ docs/modules/differentiability.rst | 1 + docs/modules/functions.rst | 129 ++- docs/modules/interoperability.rst | 181 ++++ docs/modules/runtime.rst | 78 +- docs/modules/sim.rst | 3 + docs/requirements.txt | 8 +- exts/omni.warp.core/config/extension.toml | 3 +- exts/omni.warp.core/docs/CHANGELOG.md | 68 ++ exts/omni.warp/config/extension.toml | 4 +- exts/omni.warp/docs/CHANGELOG.md | 68 ++ warp/__init__.py | 6 + warp/builtins.py | 107 +- warp/codegen.py | 280 ++++- warp/config.py | 2 +- warp/context.py | 14 +- warp/dlpack.py | 2 + warp/examples/benchmarks/benchmark.bat | 2 + warp/examples/benchmarks/benchmark.sh | 2 + warp/examples/benchmarks/benchmark_cloth.py | 10 + warp/examples/sim/example_cloth.py | 56 +- warp/native/mat.h | 6 + warp/native/quat.h | 8 + warp/native/spatial.h | 6 + warp/paddle.py | 382 +++++++ warp/sim/integrator_euler.py | 20 +- warp/sim/integrator_featherstone.py | 4 +- warp/sim/integrator_vbd.py | 435 +++++++- warp/sim/model.py | 14 +- warp/stubs.py | 207 +++- warp/tape.py | 2 + warp/tests/test_array.py | 20 + warp/tests/test_codegen.py | 33 +- warp/tests/test_dlpack.py | 118 +++ warp/tests/test_implicit_init.py | 49 + warp/tests/test_paddle.py | 852 +++++++++++++++ warp/tests/test_static.py | 412 ++++++++ warp/tests/test_tile.py | 66 +- warp/tests/test_tile_reduce.py | 13 +- warp/tests/test_torch.py | 24 + warp/tests/test_types.py | 2 +- warp/thirdparty/dlpack.py | 4 +- warp/types.py | 18 +- 55 files changed, 4904 insertions(+), 304 deletions(-) create mode 100644 docs/codegen.rst create mode 100644 docs/modules/contribution_guide.rst create mode 100644 warp/paddle.py create mode 100644 warp/tests/test_paddle.py create mode 100644 warp/tests/test_static.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0e4a87d4..2a05aa0e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -232,6 +232,7 @@ jobs: pull-request-docs: runs-on: ubuntu-latest + needs: build-warp-ubuntu if: ${{ github.event_name == 'pull_request' }} outputs: artifact-url: ${{ steps.build-docs-output.outputs.artifact-url }} @@ -242,10 +243,15 @@ jobs: uses: actions/setup-python@v5 with: python-version: "3.10" + - name: Download Warp binaries + uses: actions/download-artifact@v4 + with: + name: build-artifact-ubuntu + path: warp/bin/ - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -r docs/requirements.txt + pip install --no-cache-dir -r docs/requirements.txt - name: Build Sphinx documentation run: python build_docs.py - name: Upload artifacts diff --git a/.github/workflows/sphinx.yml b/.github/workflows/sphinx.yml index f649cd39..18413d26 100644 --- a/.github/workflows/sphinx.yml +++ b/.github/workflows/sphinx.yml @@ -29,7 +29,9 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -r docs/requirements.txt + pip install --no-cache-dir -r docs/requirements.txt + - name: Build Warp without CUDA Support + run: python build_lib.py - name: Build Sphinx documentation run: python build_docs.py - name: Upload artifacts @@ -46,7 +48,7 @@ jobs: mv docs/_build/html/* . mv docs/_build/html/.nojekyll . mv docs/_build/html/.buildinfo . - rm -rf docs warp + rm -rf docs warp _build __pycache__ git add . .nojekyll .buildinfo git commit -m "Deploy Sphinx documentation" git push -f origin gh-pages diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ea8ae21c..554b9273 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -119,7 +119,7 @@ ruff lint: - .runner-utility-linux-x86_64 before_script: - python -m pip install --upgrade pip - - pip install --upgrade ruff==0.5.5 + - pip install --upgrade ruff==0.6.8 script: - ruff check --output-format full --exit-zero # Just to get something in the log - ruff check --output-format gitlab > gl-code-quality-report.json @@ -135,7 +135,7 @@ ruff format: - .runner-utility-linux-x86_64 before_script: - python -m pip install --upgrade pip - - pip install --upgrade ruff==0.5.5 + - pip install --upgrade ruff==0.6.8 script: - ruff format --diff @@ -397,7 +397,7 @@ linux-x86_64 test warp-init: # artifacts. windows-x86_64 docs: stage: test - needs: [] + needs: [windows-x86_64 build] extends: - .runner-utility-windows-x86_64 artifacts: @@ -407,7 +407,7 @@ windows-x86_64 docs: - !reference [.snippets, define-powershell-GetTime] - Write-Output "$([char]27)[0Ksection_start:$(GetTime):install_dependencies[collapsed=true]$([char]13)$([char]27)[0KInstalling dependencies" - powershell -command "Get-Volume | Format-Table -AutoSize" - - $python_name = $DEFAULT_PYTHON + "-windows-x86_64" + - $python_name = "3.12.6+nv1-windows-x86_64" - tools/packman/packman.cmd install -l _build/target-deps/python python $python_name - '& $env:CI_PROJECT_DIR\_build\target-deps\python\python.exe -m venv _venv' - .\_venv\Scripts\Activate.ps1 @@ -696,7 +696,7 @@ publish wheels to gitlab package registry: .build-docs-common: stage: deploy image: python:3.11-slim - needs: [] + needs: [linux-x86_64 build] extends: - .runner-utility-linux-x86_64 artifacts: @@ -704,7 +704,11 @@ publish wheels to gitlab package registry: - public before_script: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KSet up docs environment" + - df -h - apt-get update && apt-get install make --no-install-recommends -y + # Move compiled binaries out of platform-specific directory + - mv warp/bin/linux-x86_64/warp.so warp/bin/ + - mv warp/bin/linux-x86_64/warp-clang.so warp/bin/ - python -m pip install --upgrade pip - python -m pip install -r docs/requirements.txt - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1b263a09..fd7faf1d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,7 @@ ci: repos: - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.5.0 + rev: v0.6.8 hooks: # Run the linter. - id: ruff diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d8450ce..1ac1e54d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,61 +1,72 @@ # CHANGELOG -## [Unreleased] - 2024-?? +## [1.4.0] - 2024-10-01 ### Added -- Support for fp64 `atomic_add`, `atomic_max`, and `atomic_min` ([GH-284](https://github.com/NVIDIA/warp/issues/284)). +- Support for a new `wp.static(expr)` function that allows arbitrary Python expressions to be evaluated at the time of + function/kernel definition ([docs](https://nvidia.github.io/warp/codegen.html#static-expressions)). - Support for stream priorities to hint to the device that it should process pending work in high-priority streams over pending work in low-priority streams when possible ([docs](https://nvidia.github.io/warp/modules/concurrency.html#stream-priorities)). +- Adaptive sparse grid geometry to `warp.fem` ([docs](https://nvidia.github.io/warp/modules/fem.html#adaptivity)). +- Support for defining `wp.kernel` and `wp.func` objects from within closures. +- Support for defining multiple versions of kernels, functions, and structs without manually assigning unique keys. +- Support for default argument values for user functions decorated with `wp.func`. +- Allow passing custom launch dimensions to `jax_kernel()` ([GH-310](https://github.com/NVIDIA/warp/pull/310)). +- JAX interoperability examples for sharding and matrix multiplication ([docs](https://nvidia.github.io/warp/modules/interoperability.html#using-shardmap-for-distributed-computation)). +- Interoperability support for the PaddlePaddle ML framework ([GH-318](https://github.com/NVIDIA/warp/pull/318)). - Support `wp.mod()` for vector types ([GH-282](https://github.com/NVIDIA/warp/issues/282)). - Expose the modulo operator `%` to Python's runtime scalar and vector types. -- Support for local vec/mat/quat component gradient tracking in backwards mode. +- Support for fp64 `atomic_add`, `atomic_max`, and `atomic_min` ([GH-284](https://github.com/NVIDIA/warp/issues/284)). - Support for quaternion indexing (e.g. `q.w`). -- Support for default argument values for user functions decorated with `wp.func`. - Support shadowing builtin functions ([GH-308](https://github.com/NVIDIA/warp/issues/308)). -- Allow passing custom launch dimensions to `jax_kernel()` ([GH-310](https://github.com/NVIDIA/warp/pull/310)). -- Jax interoperability examples for sharding and matrix multiplication (see Interoperability documentation). -- Include all non-hidden builtins in the stub file. -- Adaptive sparse grid geometry to `warp.fem` ([docs](https://nvidia.github.io/warp/modules/fem.html#adaptivity)). -- Improve accuracy of symmetric eigenvalues routine in `warp.fem`. -- Support for `wp.kernel` and `wp.func` closures. -- Support for defining multiple versions of kernels, functions, and structs without manually assigning unique keys. - Support for redefining function overloads. - Add an ocean sample to the `omni.warp` extension. +- `warp.sim.VBDIntegrator` now supports body-particle collision. +- Add a [contributing guide](https://nvidia.github.io/warp/modules/contribution_guide.html) to the Sphinx docs . +- Add documentation for dynamic code generation ([docs](https://nvidia.github.io/warp/codegen.html#dynamic-kernel-creation)). ### Changed -- **Breaking:** Rename function `plot_kernel_jacobians` to `jacobian_plot` in `autograd` module. - `wp.sim.Model.edge_indices` now includes boundary edges. - Unexposed `wp.rand*()`, `wp.sample*()`, and `wp.poisson()` from the Python scope. - Skip unused functions in module code generation, improving performance. - Avoid reloading modules if their content does not change, improving performance. - `wp.Mesh.points` is now a property instead of a raw data member, its reference can be changed after the mesh is initialized. +- Improve error message when invalid objects are referenced in a Warp kernel. +- `if`/`else`/`elif` statements with constant conditions are resolved at compile time with no branches being inserted in the generated code. +- Include all non-hidden builtins in the stub file. +- Improve accuracy of symmetric eigenvalues routine in `warp.fem`. ### Fixed - Fix for `wp.func` erroring out when defining a `Tuple` as a return type hint ([GH-302](https://github.com/NVIDIA/warp/issues/302)). -- Fix array in-place op (`+=`, `-=`) adjoints to compute gradients correctly in the backwards pass. +- Fix array in-place op (`+=`, `-=`) adjoints to compute gradients correctly in the backwards pass +- Fix vector, matrix in-place assignment adjoints to compute gradients correctly in the backwards pass, e.g.: `v[1] = x` - Fix a bug in which Python docstrings would be created as local function variables in generated code. -- Fix a rare crash during error reporting on some systems. - Fix a bug with autograd array access validation in functions from different modules. - Fix a rare crash during error reporting on some systems due to glibc mismatches. - Handle `--num_tiles 1` in `example_render_opengl.py` ([GH-306](https://github.com/NVIDIA/warp/issues/306)). +- Fix the computation of body contact forces in `FeatherstoneIntegrator` when bodies and particles collide. - Fix bug in `FeatherstoneIntegrator` where `eval_rigid_jacobian` could give incorrect results or reach an infinite loop when the body and joint indices were not in the same order. Added `Model.joint_ancestor` to fix the indexing from a joint to its parent joint in the articulation. -- Add a workaround for `__threadfence()` issues in the Compute Sanitizer initcheck tool. +- Fix wrong vertex index passed to `add_edges()` called from `ModelBuilder.add_cloth_mesh()` ([GH-319](https://github.com/NVIDIA/warp/issues/319)). +- Add a workaround for uninitialized memory read warning in the `compute-sanitizer` initcheck tool when using `wp.Mesh`. - Fix name clashes when Warp functions and structs are returned from Python functions multiple times. - Fix name clashes between Warp functions and structs defined in different modules. - Fix code generation errors when overloading generic kernels defined in a Python function. -- Fix some bugs related to module hashing and caching. - Fix issues with unrelated functions being treated as overloads (e.g., closures). - Fix handling of `stream` argument in `array.__dlpack__()`. - Fix a bug related to reloading CPU modules. - Fix a crash when kernel functions are not found in CPU modules. - Fix conditions not being evaluated as expected in `while` statements. - Fix printing Boolean and 8-bit integer values. +- Fix array interface type strings used for Boolean and 8-bit integer values. +- Fix initialization error when setting struct members. +- Fix Warp not being initialized upon entering a `wp.Tape` context. +- Use `kDLBool` instead of `kDLUInt` for DLPack interop of Booleans. ## [1.3.3] - 2024-09-04 @@ -1119,7 +1130,8 @@ - Initial publish for alpha testing -[Unreleased]: https://github.com/NVIDIA/warp/compare/v1.3.3...HEAD +[Unreleased]: https://github.com/NVIDIA/warp/compare/v1.4.0...HEAD +[1.4.0]: https://github.com/NVIDIA/warp/releases/tag/v1.4.0 [1.3.3]: https://github.com/NVIDIA/warp/releases/tag/v1.3.3 [1.3.2]: https://github.com/NVIDIA/warp/releases/tag/v1.3.2 [1.3.1]: https://github.com/NVIDIA/warp/releases/tag/v1.3.1 diff --git a/README.md b/README.md index 44c5d326..54c1bbfd 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ regular Python functions and JIT compiles them to efficient kernel code that can Warp is designed for [spatial computing](https://en.wikipedia.org/wiki/Spatial_computing) and comes with a rich set of primitives that make it easy to write programs for physics simulation, perception, robotics, and geometry processing. In addition, Warp kernels -are differentiable and can be used as part of machine-learning pipelines with frameworks such as PyTorch and JAX. +are differentiable and can be used as part of machine-learning pipelines with frameworks such as PyTorch, JAX and Paddle. Please refer to the project [Documentation](https://nvidia.github.io/warp/) for API and language reference and [CHANGELOG.md](./CHANGELOG.md) for release history. @@ -45,9 +45,9 @@ the `pip install` command, e.g. | Platform | Install Command | | --------------- | ----------------------------------------------------------------------------------------------------------------------------- | -| Linux aarch64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.3.3/warp_lang-1.3.3+cu11-py3-none-manylinux2014_aarch64.whl` | -| Linux x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.3.3/warp_lang-1.3.3+cu11-py3-none-manylinux2014_x86_64.whl` | -| Windows x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.3.3/warp_lang-1.3.3+cu11-py3-none-win_amd64.whl` | +| Linux aarch64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_aarch64.whl` | +| Linux x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_x86_64.whl` | +| Windows x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-win_amd64.whl` | The `--force-reinstall` option may need to be used to overwrite a previous installation. @@ -372,7 +372,8 @@ Warp is provided under the NVIDIA Software License, please see [LICENSE.md](./LI Contributions and pull requests from the community are welcome and are taken under the terms described in the **Feedback** section of [LICENSE.md](LICENSE.md#9-feedback). -[CONTRIBUTING.md](./CONTRIBUTING.md) provides additional information on how to open a pull request for Warp. +Please see the [Contribution Guide](https://nvidia.github.io/warp/modules/contribution_guide.html) for more +information on contributing to the development of Warp. ## Citing diff --git a/VERSION.md b/VERSION.md index 31e5c843..88c5fb89 100644 --- a/VERSION.md +++ b/VERSION.md @@ -1 +1 @@ -1.3.3 +1.4.0 diff --git a/docs/basics.rst b/docs/basics.rst index 2b042edf..2fb1b880 100644 --- a/docs/basics.rst +++ b/docs/basics.rst @@ -3,6 +3,8 @@ Basics .. currentmodule:: warp +.. _warp-initialization: + Initialization -------------- @@ -273,6 +275,8 @@ less time to load since code compilation is skipped: step took 0.04 ms render took 5.05 ms +For more information, see the :doc:`codegen` section. + Language Details ---------------- diff --git a/docs/codegen.rst b/docs/codegen.rst new file mode 100644 index 00000000..fe5ed81b --- /dev/null +++ b/docs/codegen.rst @@ -0,0 +1,1047 @@ +.. _code_generation: + +Code Generation +=============== + +Overview +-------- + +Warp kernels are grouped together by Python module. Before they can run on a device, they must be translated and compiled for the device architecture. All kernels in a module are compiled together, which is faster than compiling each one individually. When a kernel is launched, Warp checks if the module is up-to-date and will compile it if needed. Adding new kernels to a module at runtime modifies the module, which means that it will need to be reloaded on next launch. + +.. code:: python + + @wp.kernel + def kernel_foo(): + print("foo") + + wp.launch(kernel_foo, dim=1) + + @wp.kernel + def kernel_bar(): + print("bar") + + wp.launch(kernel_bar, dim=1) + +In the snippet above, kernel definitions are interspersed with kernel launches. To execute ``kernel_foo``, the module is compiled during the first launch. Defining ``kernel_bar`` modifies the module, so it needs to be recompiled during the second launch: + +.. code:: text + + Module __main__ 6cd1d53 load on device 'cuda:0' took 168.19 ms (compiled) + foo + Module __main__ c7c0e9a load on device 'cuda:0' took 160.35 ms (compiled) + bar + +The compilation can take a long time for modules with numerous complex kernels, so Warp caches the compiled modules and can reuse them on the next run of the program: + +.. code:: text + + Module __main__ 6cd1d53 load on device 'cuda:0' took 4.97 ms (cached) + foo + Module __main__ c7c0e9a load on device 'cuda:0' took 0.40 ms (cached) + bar + +Loading cached modules is much faster, but it's not free. In addition, module reloading can cause problems during CUDA graph capture, so there are good reasons to try to avoid it. + +The best way to avoid module reloading is to define all the kernels before launching any of them. This way, the module will be compiled only once: + +.. code:: python + + @wp.kernel + def kernel_foo(): + print("foo") + + @wp.kernel + def kernel_bar(): + print("bar") + + wp.launch(kernel_foo, dim=1) + wp.launch(kernel_bar, dim=1) + +.. code:: text + + Module __main__ c7c0e9a load on device 'cuda:0' took 174.57 ms (compiled) + foo + bar + +On subsequent runs it will be loaded from the kernel cache only once: + +.. code:: text + + Module __main__ c7c0e9a load on device 'cuda:0' took 4.96 ms (cached) + foo + bar + +Warp tries to recognize duplicate kernels to avoid unnecessary module reloading. For example, this program creates kernels in a loop, but they are always identical, so the module does not need to be recompiled on every launch: + +.. code:: python + + for i in range(3): + + @wp.kernel + def kernel_hello(): + print("hello") + + wp.launch(kernel_hello, dim=1) + +Warp filters out the duplicate kernels, so the module is only loaded once: + +.. code:: text + + Module __main__ 8194f57 load on device 'cuda:0' took 178.24 ms (compiled) + hello + hello + hello + + +Warp generates C++/CUDA source code for CPU/GPU and stores the .cpp/.cu source files under the module directories of the kernel cache. +The kernel cache folder path is printed during the :ref:`Warp initialization ` and +can be retrieved after Warp has been initialized from the ``warp.config.kernel_cache_dir`` :ref:`configuration setting `. + +Consider the following example: + +.. code:: python + + @wp.func + def my_func(a: float, b: float): + c = wp.sin(b) * a + return c + +The resulting CUDA code looks similar to this: + +.. code:: cpp + + // example.py:5 + static CUDA_CALLABLE wp::float32 my_func_0( + wp::float32 var_a, + wp::float32 var_b) + { + //--------- + // primal vars + wp::float32 var_0; + wp::float32 var_1; + //--------- + // forward + // def my_func(a: float, b: float): + // c = wp.sin(b) * a + var_0 = wp::sin(var_b); + var_1 = wp::mul(var_0, var_a); + // return c + return var_1; + } + +The generated code follows `static-single-assignment (SSA) form `__. +To ease the readability, comments referring to the original Python source code lines are inserted. +Besides the forward pass, the gradient function is also generated, and, +if a :ref:`custom replay function ` is provided, the replay function is generated as well. + +Warp passes the generated source code to native compilers (e.g., LLVM for CPU and NVRTC for CUDA) to produce executable code that is invoked when launching kernels. + +.. _external_references: + +External References and Constants +--------------------------------- + +A Warp kernel can access regular Python variables defined outside of the kernel itself, as long as those variables are of a supported type. Such external references are treated as compile-time constants in the kernel. It's not possible for code running on a different device to access the state of the Python interpreter, so these variables are folded into the kernels by value: + +.. code:: python + + C = 42 + + @wp.kernel + def k(): + print(C) + + wp.launch(k, dim=1) + +During code generation, the external variable ``C`` becomes a constant: + +.. code:: c++ + + { + //--------- + // primal vars + const wp::int32 var_0 = 42; + //--------- + // forward + // def k(): + // print(C) + wp::print(var_0); + } + + +Supported Constant Types +~~~~~~~~~~~~~~~~~~~~~~~~ + +Only value types can be used as constants in Warp kernels. This includes integers, floating point numbers, vectors (``wp.vec*``), matrices (``wp.mat*``) and other built-in math types. Attempting to capture other variables types will result in an exception: + +.. code:: python + + global_array = wp.zeros(5, dtype=int) + + @wp.kernel + def k(): + tid = wp.tid() + global_array[tid] = 42 # referencing external arrays is not allowed! + + wp.launch(k, dim=global_array.shape, inputs=[]) + +Output: + +.. code:: text + + TypeError: Invalid external reference type: + +The reason why arrays cannot be captured is because they exist on a particular device and contain pointers to the device memory, which would make the kernel not portable across different devices. Arrays should always be passed as kernel inputs. + + +Usage of ``wp.constant()`` +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In older versions of Warp, ``wp.constant()`` was required to declare constants that can be used in a kernel. This is no longer necessary, but the old syntax is still supported for backward compatibility. ``wp.constant()`` can still be used to check if a value can be referenced in a kernel: + +.. code:: python + + x = wp.constant(17.0) # ok + v = wp.constant(wp.vec3(1.0, 2.0, 3.0)) # ok + a = wp.constant(wp.zeros(n=5, dtype=int)) # error, invalid constant type + + @wp.kernel + def k(): + tid = wp.tid() + a[tid] = x * v + +In this snippet, a ``TypeError`` will be raised when declaring the array with ``wp.constant()``. If ``wp.constant()`` was omitted, the error would be raised later during code generation, which might be slightly harder to debug. + + +Updating Constants +~~~~~~~~~~~~~~~~~~ + +One limitation of using external variables in Warp kernels is that Warp doesn't know when the value is modified: + +.. code:: python + + C = 17 + + @wp.kernel + def k(): + print(C) + + wp.launch(k, dim=1) + + # redefine constant + C = 42 + + wp.launch(k, dim=1) + +This prints: + +.. code:: text + + Module __main__ 4494df2 load on device 'cuda:0' took 163.54 ms (compiled) + 17 + 17 + +During the first launch of kernel ``k``, the kernel is compiled using the existing value of ``C`` (17). Since ``C`` is just a plain Python variable, Warp has no way of detecting when it is modified. Thus on the second launch the old value is printed again. + +One way to get around this limitation is to tell Warp that the module was modified: + +.. code:: python + + C = 17 + + @wp.kernel + def k(): + print(C) + + wp.launch(k, dim=1) + + # redefine constant + C = 42 + + # tell Warp that the module was modified + k.module.mark_modified() + + wp.launch(k, dim=1) + +This produces the updated output: + +.. code:: text + + Module __main__ 4494df2 load on device 'cuda:0' took 167.92 ms (compiled) + 17 + Module __main__ 9a0664f load on device 'cuda:0' took 164.83 ms (compiled) + 42 + +Notice that calling ``module.mark_modified()`` caused the module to be recompiled on the second launch using the latest value of ``C``. + +.. note:: + The ``Module`` class and the ``mark_modified()`` method are considered internal. A public API for working with modules is planned, but currently it is subject to change without notice. Programs should not overly rely on the ``mark_modified()`` method, but it can be used in a pinch. + + +.. _static_expressions: + +Static Expressions +------------------ + +We often encounter situations where a kernel needs to be specialized for a given input or where certain parts of the code are static by the time the code is executed. +With static expressions, we can write Python expressions to be evaluated at the time of declaring a Warp function or kernel. + +``wp.static(...)`` expressions allow the user to run arbitrary Python code at the time the Warp function or kernel containing the expression is defined. +:func:`wp.static(expr) ` accepts a Python expression and replaces it with the result. +Note that the expression can only access variables that can be evaluated at the time the expression is declared. +This includes global variables and variables captured in a closure in which the Warp function or kernel is defined. +Additionally, Warp constants from within the kernel or function can be accessed, such as the constant iteration variable for static for-loops (i.e. when the range is known at the time of code generation). + +The result from ``wp.static()`` must be a non-null value of one of the following types: + +- A Warp function +- A string +- Any type that is supported by Warp inside kernels (e.g. scalars, structs, matrices, vectors, etc.), excluding Warp arrays or structs containing Warp arrays + +Example: Static Math Expressions +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: python + + import warp as wp + import scipy.linalg + + @wp.kernel + def my_kernel(): + static_var = wp.static(3 + 2) + # we can call arbitrary Python code inside wp.static() + static_norm = wp.static(wp.float64(scipy.linalg.norm([3, 4]))) + wp.printf("static_var = %i\n", static_var) + wp.printf("static_norm = %f\n", static_norm) + + wp.launch(my_kernel, 1) + +The static expressions are evaluated at the time of when the ``@wp.kernel`` decorator is evaluated and replaced in the code by their respective constant result values. The generated code will therefore contain the results of the expressions hard-coded in the source file (shown an abbreviated version): + +.. code:: cpp + + const wp::int32 var_0 = 5; + const wp::float64 var_1 = 5.0; + const wp::str var_2 = "static_var = %i\n"; + const wp::str var_3 = "static_norm = %f\n"; + + // wp.printf("static_var = %i\n", static_var) + printf(var_2, var_0); + // wp.printf("static_norm = %f\n", static_norm) + printf(var_3, var_1); + + +Example: Static Conditionals +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If/else/elif conditions that are constant can be eliminated from the generated code by using ``wp.static()`` inside the branch condition to yield a constant boolean. +This can provide improved performance by avoiding branching and can be useful for generating specialized kernels: + +.. code:: python + + import warp as wp + + available_colors = {"red", "green", "blue"} + + @wp.kernel + def my_kernel(): + if wp.static("red" in available_colors): + print("red is available") + else: + print("red is not available") + +The global variable ``available_colors`` is known at the time of declaring the kernel and the generated code will contain only the branch that is taken: + +.. code:: cpp + + const wp::str var_1 = "red is available"; + wp::print(var_1); + +Example: Static Loop Unrolling +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Static expressions can be used to unroll for-loops during code generation. We place ``wp.static()`` expressions inside the loop's ``range`` to yield static for-loops that can be unrolled. The iteration variable becomes a constant and can therefore be accessed from within a static expression in the loop body: + +.. code:: python + + import warp as wp + + def loop_limit(): + return 3 + + @wp.kernel + def my_kernel(): + for i in range(wp.static(loop_limit())): + static_i = wp.static(i) + wp.printf("i = %i\n", static_i) + + wp.launch(my_kernel, 1) + +The generated code will not contain the for-loop but instead the loop body will be repeated three times: + +.. code:: cpp + + const wp::int32 var_0 = 3; + const wp::int32 var_1 = 0; + const wp::int32 var_2 = 0; + const wp::str var_3 = "i = %i\n"; + const wp::int32 var_4 = 1; + const wp::int32 var_5 = 1; + const wp::str var_6 = "i = %i\n"; + const wp::int32 var_7 = 2; + const wp::int32 var_8 = 2; + const wp::str var_9 = "i = %i\n"; + printf(var_3, var_2); + printf(var_6, var_5); + printf(var_9, var_8); + +Example: Function Pointers +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``wp.static(...)`` may also return a Warp function. This can be useful to specialize a kernel or function based on information available at the time of declaring the Warp function or kernel, or to automatically generate overloads for different types. + +.. code:: python + + import warp as wp + + @wp.func + def do_add(a: float, b: float): + return a + b + + @wp.func + def do_sub(a: float, b: float): + return a - b + + @wp.func + def do_mul(a: float, b: float): + return a * b + + op_handlers = { + "add": do_add, + "sub": do_sub, + "mul": do_mul, + } + + inputs = wp.array([[1, 2], [3, 0]], dtype=wp.float32) + outputs = wp.empty(2, dtype=wp.float32) + + for op in op_handlers.keys(): + + @wp.kernel + def operate(input: wp.array(dtype=inputs.dtype, ndim=2), output: wp.array(dtype=wp.float32)): + tid = wp.tid() + a, b = input[tid, 0], input[tid, 1] + # retrieve the right function to use for the captured dtype variable + output[tid] = wp.static(op_handlers[op])(a, b) + + wp.launch(operate, dim=2, inputs=[inputs], outputs=[outputs]) + print(outputs.numpy()) + +The above program uses a static expression to select the right function given the captured ``op`` variable and prints the following output while compiling the module containing the ``operate`` kernel three times: + +.. code:: text + + [3. 3.] + [-1. 3.] + [2. 0.] + + +.. _dynamic_generation: + +Dynamic Kernel Creation +----------------------- + +It is often desirable to dynamically customize kernels with different constants, types, or functions. We can achieve this through runtime kernel specialization using Python closures. + +Kernel Closures +~~~~~~~~~~~~~~~ + +Constants +^^^^^^^^^ + +Warp allows references to external constants in kernels: + +.. code:: python + + def create_kernel_with_constant(constant): + @wp.kernel + def k(a: wp.array(dtype=float)): + tid = wp.tid() + a[tid] += constant + return k + + k1 = create_kernel_with_constant(17.0) + k2 = create_kernel_with_constant(42.0) + + a = wp.zeros(5, dtype=float) + + wp.launch(k1, dim=a.shape, inputs=[a]) + wp.launch(k2, dim=a.shape, inputs=[a]) + + print(a) + +Output: + +.. code:: text + + [59. 59. 59. 59. 59.] + + +Data Types +^^^^^^^^^^ + +Warp data types can also be captured in a closure. Here is an example of creating kernels that work with different vector dimensions: + +.. code:: python + + def create_kernel_with_dtype(vec_type): + @wp.kernel + def k(a: wp.array(dtype=vec_type)): + tid = wp.tid() + a[tid] += float(tid) * vec_type(1.0) + return k + + k2 = create_kernel_with_dtype(wp.vec2) + k4 = create_kernel_with_dtype(wp.vec4) + + a2 = wp.ones(3, dtype=wp.vec2) + a4 = wp.ones(3, dtype=wp.vec4) + + wp.launch(k2, dim=a2.shape, inputs=[a2]) + wp.launch(k4, dim=a4.shape, inputs=[a4]) + + print(a2) + print(a4) + +Output: + +.. code:: text + + [[1. 1.] + [2. 2.] + [3. 3.]] + [[1. 1. 1. 1.] + [2. 2. 2. 2.] + [3. 3. 3. 3.]] + + +Functions +^^^^^^^^^ + +Here's a kernel generator that's parameterized using different functions: + +.. code:: python + + def create_kernel_with_function(f): + @wp.kernel + def k(a: wp.array(dtype=float)): + tid = wp.tid() + a[tid] = f(a[tid]) + return k + + @wp.func + def square(x: float): + return x * x + + @wp.func + def cube(x: float): + return x * x * x + + k1 = create_kernel_with_function(square) + k2 = create_kernel_with_function(cube) + + a1 = wp.array([1, 2, 3, 4, 5], dtype=float) + a2 = wp.array([1, 2, 3, 4, 5], dtype=float) + + wp.launch(k1, dim=a1.shape, inputs=[a1]) + wp.launch(k2, dim=a2.shape, inputs=[a2]) + + print(a1) + print(a2) + +Output: + +.. code:: text + + [ 1. 4. 9. 16. 25.] + [ 1. 8. 27. 64. 125.] + + +Function Closures +~~~~~~~~~~~~~~~~~ + +Warp functions (``@wp.func``) also support closures, just like kernels: + +.. code:: python + + def create_function_with_constant(constant): + @wp.func + def f(x: float): + return constant * x + return f + + f1 = create_function_with_constant(2.0) + f2 = create_function_with_constant(3.0) + + @wp.kernel + def k(a: wp.array(dtype=float)): + tid = wp.tid() + x = float(tid) + a[tid] = f1(x) + f2(x) + + a = wp.ones(5, dtype=float) + + wp.launch(k, dim=a.shape, inputs=[a]) + + print(a) + +Output: + +.. code:: text + + [ 0. 5. 10. 15. 20.] + + +We can also create related function and kernel closures together like this: + +.. code:: python + + def create_fk(a, b): + @wp.func + def f(x: float): + return a * x + + @wp.kernel + def k(a: wp.array(dtype=float)): + tid = wp.tid() + a[tid] = f(a[tid]) + b + + return f, k + + # create related function and kernel closures + f1, k1 = create_fk(2.0, 3.0) + f2, k2 = create_fk(4.0, 5.0) + + # use the functions separately in a new kernel + @wp.kernel + def kk(a: wp.array(dtype=float)): + tid = wp.tid() + a[tid] = f1(a[tid]) + f2(a[tid]) + + a1 = wp.array([1, 2, 3, 4, 5], dtype=float) + a2 = wp.array([1, 2, 3, 4, 5], dtype=float) + ak = wp.array([1, 2, 3, 4, 5], dtype=float) + + wp.launch(k1, dim=a1.shape, inputs=[a1]) + wp.launch(k2, dim=a2.shape, inputs=[a2]) + wp.launch(kk, dim=ak.shape, inputs=[ak]) + + print(a1) + print(a2) + print(ak) + +Output: + +.. code:: text + + [ 5. 7. 9. 11. 13.] + [ 9. 13. 17. 21. 25.] + [ 6. 12. 18. 24. 30.] + + +Dynamic Structs +~~~~~~~~~~~~~~~ + +Sometimes it's useful to customize Warp structs with different data types. + +Customize Precision +^^^^^^^^^^^^^^^^^^^ + +For example, we can create structs with different floating point precision: + +.. code:: python + + def create_struct_with_precision(dtype): + @wp.struct + class S: + a: dtype + b: dtype + return S + + # create structs with different floating point precision + S16 = create_struct_with_precision(wp.float16) + S32 = create_struct_with_precision(wp.float32) + S64 = create_struct_with_precision(wp.float64) + + s16 = S16() + s32 = S32() + s64 = S64() + + s16.a, s16.b = 2.0001, 3.0000002 + s32.a, s32.b = 2.0001, 3.0000002 + s64.a, s64.b = 2.0001, 3.0000002 + + # create a generic kernel that works with the different types + @wp.kernel + def k(s: Any, output: wp.array(dtype=Any)): + tid = wp.tid() + x = output.dtype(tid) + output[tid] = x * s.a + s.b + + a16 = wp.empty(5, dtype=wp.float16) + a32 = wp.empty(5, dtype=wp.float32) + a64 = wp.empty(5, dtype=wp.float64) + + wp.launch(k, dim=a16.shape, inputs=[s16, a16]) + wp.launch(k, dim=a32.shape, inputs=[s32, a32]) + wp.launch(k, dim=a64.shape, inputs=[s64, a64]) + + print(a16) + print(a32) + print(a64) + +We can see the effect of using different floating point precision in the output: + +.. code:: text + + [ 3. 5. 7. 9. 11.] + [ 3.0000002 5.0001 7.0002003 9.000299 11.0004 ] + [ 3.0000002 5.0001002 7.0002002 9.0003002 11.0004002] + + +Customize Dimensions +^^^^^^^^^^^^^^^^^^^^ + +Another useful application of dynamic structs is the ability to customize dimensionality. Here, we create structs that work with 2D and 3D data: + +.. code:: python + + # create struct with different vectors and matrix dimensions + def create_struct_nd(dim): + @wp.struct + class S: + v: wp.types.vector(dim, float) + m: wp.types.matrix((dim, dim), float) + return S + + S2 = create_struct_nd(2) + S3 = create_struct_nd(3) + + s2 = S2() + s2.v = (1.0, 2.0) + s2.m = ((2.0, 0.0), + (0.0, 0.5)) + + s3 = S3() + s3.v = (1.0, 2.0, 3.0) + s3.m = ((2.0, 0.0, 0.0), + (0.0, 0.5, 0.0), + (0.0, 0.0, 1.0)) + + # create a generic kernel that works with the different types + @wp.kernel + def k(s: Any, output: wp.array(dtype=Any)): + tid = wp.tid() + x = float(tid) + output[tid] = x * s.v * s.m + + a2 = wp.empty(5, dtype=wp.vec2) + a3 = wp.empty(5, dtype=wp.vec3) + + wp.launch(k, dim=a2.shape, inputs=[s2, a2]) + wp.launch(k, dim=a3.shape, inputs=[s3, a3]) + + print(a2) + print(a3) + +Output: + +.. code:: text + + [[0. 0.] + [2. 1.] + [4. 2.] + [6. 3.] + [8. 4.]] + [[ 0. 0. 0.] + [ 2. 1. 3.] + [ 4. 2. 6.] + [ 6. 3. 9.] + [ 8. 4. 12.]] + + +Module Reloading +~~~~~~~~~~~~~~~~ + +Frequent recompilation can add overhead to a program, especially if the program is creating kernels at runtime. Consider this program: + +.. code:: python + + def create_kernel_with_constant(constant): + @wp.kernel + def k(a: wp.array(dtype=float)): + tid = wp.tid() + a[tid] += constant + return k + + a = wp.zeros(5, dtype=float) + + k1 = create_kernel_with_constant(17.0) + wp.launch(k1, dim=a.shape, inputs=[a]) + print(a) + + k2 = create_kernel_with_constant(42.0) + wp.launch(k2, dim=a.shape, inputs=[a]) + print(a) + + k3 = create_kernel_with_constant(-9.0) + wp.launch(k3, dim=a.shape, inputs=[a]) + print(a) + +Kernel creation is interspersed with kernel launches, which forces reloading on each kernel launch: + +.. code:: text + + Module __main__ 96db544 load on device 'cuda:0' took 165.46 ms (compiled) + [17. 17. 17. 17. 17.] + Module __main__ 9f609a4 load on device 'cuda:0' took 151.69 ms (compiled) + [59. 59. 59. 59. 59.] + Module __main__ e93fbb9 load on device 'cuda:0' took 167.84 ms (compiled) + [50. 50. 50. 50. 50.] + +To avoid reloading, all kernels should be created before launching them: + +.. code:: python + + def create_kernel_with_constant(constant): + @wp.kernel + def k(a: wp.array(dtype=float)): + tid = wp.tid() + a[tid] += constant + return k + + k1 = create_kernel_with_constant(17.0) + k2 = create_kernel_with_constant(42.0) + k3 = create_kernel_with_constant(-9.0) + + a = wp.zeros(5, dtype=float) + + wp.launch(k1, dim=a.shape, inputs=[a]) + print(a) + + wp.launch(k2, dim=a.shape, inputs=[a]) + print(a) + + wp.launch(k3, dim=a.shape, inputs=[a]) + print(a) + +.. code:: text + + Module __main__ e93fbb9 load on device 'cuda:0' took 164.87 ms (compiled) + [17. 17. 17. 17. 17.] + [59. 59. 59. 59. 59.] + [50. 50. 50. 50. 50.] + +Redefining identical kernels, functions, and structs should not cause module reloading, since Warp is able to detect duplicates: + +.. code:: python + + def create_struct(dtype): + @wp.struct + class S: + a: dtype + b: dtype + return S + + def create_function(dtype, S): + @wp.func + def f(s: S): + return s.a * s.b + return f + + def create_kernel(dtype, S, f, C): + @wp.kernel + def k(a: wp.array(dtype=dtype)): + tid = wp.tid() + s = S(a[tid], C) + a[tid] = f(s) + return k + + # create identical struct, function, and kernel in a loop + for i in range(3): + S = create_struct(float) + f = create_function(float, S) + k = create_kernel(float, S, f, 3.0) + + a = wp.array([1, 2, 3, 4, 5], dtype=float) + + wp.launch(k, dim=a.shape, inputs=[a]) + print(a) + +Even though struct ``S``, function ``f``, and kernel ``k`` are re-created in each iteration of the loop, they are duplicates so the module is only loaded once: + +.. code:: text + + Module __main__ 4af2d60 load on device 'cuda:0' took 181.34 ms (compiled) + [ 3. 6. 9. 12. 15.] + [ 3. 6. 9. 12. 15.] + [ 3. 6. 9. 12. 15.] + + +.. _late_binding: + +Late Binding and Static Expressions +----------------------------------- + +Python uses late binding, which means that variables can be referenced in a function before they are defined: + +.. code:: python + + def k(): + # Function f() and constant C are not defined yet. + # They will be resolved when k() is called. + print(f() + C) + + def f(): + return 42 + + C = 17 + + # late binding occurs in this call + k() + +Warp follows this convention by default, because it's the Pythonic way. Here is a similar program written in Warp: + +.. code:: python + + @wp.kernel + def k(): + # Function f() and constant C are not defined yet. + # They will be resolved when k() is called. + print(f() + C) + + @wp.func + def f(): + return 42 + + C = 17 + + # late binding occurs in this launch, when the module is compiled + wp.launch(k, dim=1) + + # wait for the output + wp.synchronize_device() + +Late binding is often convenient, but it can sometimes lead to surprising results. Consider this snippet, which creates kernels in a loop. The kernels reference the loop variable as a constant. + +.. code:: python + + # create a list of kernels that use the loop variable + kernels = [] + for i in range(3): + @wp.kernel + def k(): + print(i) + kernels.append(k) + + # launch the kernels + for k in kernels: + wp.launch(k, dim=1) + + wp.synchronize_device() + +This prints: + +.. code:: text + + 2 + 2 + 2 + +This might be surprising, but creating a similar program in pure Python would lead to the same results. Because of late binding, the captured loop variable ``i`` is not evaluated until the kernels are launched. At that moment, the value of ``i`` is 2 and we see the same output from each kernel. + +In Warp, ``wp.static()`` can be used to get around this problem: + +.. code:: python + + # create a list of kernels that use the loop variable + kernels = [] + for i in range(3): + @wp.kernel + def k(): + print(wp.static(i)) # wp.static() for the win + kernels.append(k) + + # launch the kernels + for k in kernels: + wp.launch(k, dim=1) + + wp.synchronize_device() + +Warp replaces the call to ``wp.static()`` with the value of the expression passed as its argument. The expression is evaluated immediately at the time of kernel definition. This is similar to static binding used by languages like C++, which means that all variables referenced by the static expression must already be defined. + +To further illustrate the difference between the default late binding behavior and static expressions, consider this program: + +.. code:: python + + C = 17 + + @wp.kernel + def k1(): + print(C) + + @wp.kernel + def k2(): + print(wp.static(C)) + + # redefine constant + C = 42 + + wp.launch(k1, dim=1) + wp.launch(k2, dim=1) + + wp.synchronize_device() + +Output: + +.. code:: text + + 42 + 17 + +Kernel ``k1`` uses late binding of ``C``. This means that it captures the latest value of ``C``, determined when the module is built during the launch. Kernel ``k2`` consumes ``C`` in a static expression, thus it captures the value of ``C`` when the kernel is defined. + +The same rules apply to resolving Warp functions: + +.. code:: python + + @wp.func + def f(): + return 17 + + @wp.kernel + def k1(): + print(f()) + + @wp.kernel + def k2(): + print(wp.static(f)()) + + # redefine function + @wp.func + def f(): + return 42 + + wp.launch(k1, dim=1) + wp.launch(k2, dim=1) + + wp.synchronize_device() + +Output: + +.. code:: text + + 42 + 17 + +Kernel ``k1`` uses the latest definition of function ``f``, while kernel ``k2`` uses the definition of ``f`` when the kernel was declared. diff --git a/docs/configuration.rst b/docs/configuration.rst index 04ac2d2c..b054d5d8 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -7,6 +7,8 @@ Warp has settings at the global, module, and kernel level that can be used to fi of Warp programs. In cases in which a setting can be changed at multiple levels (e.g.: ``enable_backward``), the setting at the more-specific scope takes precedence. +.. _global-settings: + Global Settings --------------- diff --git a/docs/index.rst b/docs/index.rst index ac324f32..4338cb9f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -7,7 +7,7 @@ regular Python functions and JIT compiles them to efficient kernel code that can Warp is designed for `spatial computing `_ and comes with a rich set of primitives that make it easy to write programs for physics simulation, perception, robotics, and geometry processing. In addition, Warp kernels -are differentiable and can be used as part of machine-learning pipelines with frameworks such as PyTorch and JAX. +are differentiable and can be used as part of machine-learning pipelines with frameworks such as PyTorch, JAX and Paddle. Below are some examples of simulations implemented using Warp: @@ -320,8 +320,7 @@ Contributing Contributions and pull requests from the community are welcome and are taken under the terms described in the **Feedback** section of `LICENSE.md `__. -`CONTRIBUTING.md `_ provides additional information on -how to open a pull request for Warp. +Please see the :doc:`modules/contribution_guide` for more information on contributing to the development of Warp. Citing ------ @@ -356,12 +355,14 @@ Full Table of Contents configuration debugging limitations + modules/contribution_guide faq .. toctree:: :maxdepth: 2 :caption: Advanced Topics + codegen modules/allocators modules/concurrency profiling diff --git a/docs/installation.rst b/docs/installation.rst index 016109f0..b432a326 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -25,11 +25,11 @@ the ``pip install`` command, e.g. * - Platform - Install Command * - Linux aarch64 - - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.3.3/warp_lang-1.3.3+cu11-py3-none-manylinux2014_aarch64.whl`` + - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_aarch64.whl`` * - Linux x86-64 - - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.3.3/warp_lang-1.3.3+cu11-py3-none-manylinux2014_x86_64.whl`` + - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_x86_64.whl`` * - Windows x86-64 - - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.3.3/warp_lang-1.3.3+cu11-py3-none-win_amd64.whl`` + - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-win_amd64.whl`` The ``--force-reinstall`` option may need to be used to overwrite a previous installation. @@ -76,6 +76,7 @@ The following optional dependencies are required to support certain features: * `usd-core `_: Required for some Warp examples, ``warp.sim.parse_usd()``, and ``warp.render.UsdRenderer``. * `JAX `_: Required for JAX interoperability (see :ref:`jax-interop`). * `PyTorch `_: Required for PyTorch interoperability (see :ref:`pytorch-interop`). +* `Paddle `_: Required for Paddle interoperability (see :ref:`paddle-interop`). * `NVTX for Python `_: Required to use :class:`wp.ScopedTimer(use_nvtx=True) `. Building the Warp documentation requires: diff --git a/docs/modules/contribution_guide.rst b/docs/modules/contribution_guide.rst new file mode 100644 index 00000000..905de2c3 --- /dev/null +++ b/docs/modules/contribution_guide.rst @@ -0,0 +1,333 @@ +Contribution Guide +================== + +Some ways to contribute to the development of Warp include: + +* Reporting bugs and requesting new features on `GitHub `__. +* Asking questions, sharing your work, or participating in discussion threads on + `GitHub `__ (preferred) or + `Discord `__. +* Adding new examples to the Warp repository. +* Documentation improvements. +* Contributing bug fixes or new features. + +Code Contributions +------------------ + +Code contributions from the community are welcome and are taken under the +terms described in the **Feedback** section of `LICENSE.md `__. + +Contributors are encouraged to first open an issue on GitHub to discuss proposed feature contributions and gauge +potential interest. + +Overview +^^^^^^^^ + +#. Create a fork of the Warp GitHub repository by visiting https://github.com/NVIDIA/warp/fork +#. Clone your fork on your local machine, e.g. ``git clone git@github.com:username/warp.git``. +#. Create a branch to develop your contribution on, e.g. ``git checkout -b mmacklin/cuda-bvh-optimizations``. + + Use the following naming conventions for the branch name: + + * New features: ``username/feature-name`` + * Bug fixes: ``bugfix/feature-name`` + +#. Make your desired changes. + + * Please familiarize yourself with the :ref:`coding-guidelines`. + * Ensure that code changes pass :ref:`linting and formatting checks `. + * Test cases should be written to verify correctness (:ref:`testing-warp`). + * Documentation should be added for new features (:ref:`building-docs`). + * Add an entry to the unreleased section at the top of the + `CHANGELOG.md `__ describing the changes. + +#. Push your branch to your GitHub fork, e.g. ``git push origin username/feature-name``. +#. Submit a pull request on GitHub to the ``main`` branch (:ref:`pull-requests`). + Work with reviewers to ensure the pull request is in a state suitable for merging. + +.. _coding-guidelines: + +General Coding Guidelines +^^^^^^^^^^^^^^^^^^^^^^^^^ + +* Follow `PEP 8 `__ as the baseline for coding style, but prioritize matching the + existing style and conventions of the file being modified to maintain consistency. +* Use `snake case `__ for all function names. +* Use `Google-style docstrings `__ + for Python code. +* Include the NVIDIA copyright header on all newly created files, updating the year to current year at the time of + the initial file creation. +* Aim for consistency in variable and function names. + + * Use the existing terminology when possible when naming new functions (e.g. use ``points`` instead of ``vertex_buffer``). + * Don't introduce new abbreviations if one already exists in the code base. + * Also be mindful of consistency and clarity when naming local function variables. + +* Avoid generic function names like ``get_data()``. +* Follow the existing style conventions in any CUDA C++ files being modified. +* Use both ``inputs`` and ``outputs`` parameters in ``wp.launch()`` in functions that are expected to be used in + differentiable programming applications to aid in visualization and debugging tools. + +.. _linting-and-formatting: + +Linting and Formatting +^^^^^^^^^^^^^^^^^^^^^^ + +`Ruff `__ is used as the linter and code formatter for Python code in the Warp repository. +The contents of pull requests will automatically be checked to ensure adherence to our formatting and linting standards. + +We recommend first running Ruff locally on your branch prior to opening a pull request. +From the project root, run: + +.. code-block:: bash + + pip install pre-commit + pre-commit run --all + +This command will attempt to fix any lint violations and then format the code. + +To run Ruff checks at the same time as ``git commit``, pre-commit hooks can be installed by running this command in the project root: + +.. code-block:: bash + + pre-commit install + +.. _building-docs: + +Building the Documentation +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The Sphinx documentation can be built by running the following from the project root: + +.. code-block:: bash + + pip install -r docs/requirements.txt + python build_docs.py + +This command also regenerates the stub file (``warp/stubs.py``) and the reStructuredText file for the +:doc:`functions` page. After building the documentation, it is recommended to run a ``git status`` to +check if your changes have modified these files. If so, please commit the modified files to your branch. + +.. note:: In the future, Warp needs to be built at least once prior to building the documentation. + +.. _pull-requests: + +Pull Request Guidelines +^^^^^^^^^^^^^^^^^^^^^^^ + +* Ensure your pull request has a descriptive title that clearly states the purpose of the changes. +* Include a brief description covering: + + * Summary of changes. + * Areas affected by the changes. + * The problem being solved. + * Any limitations or non-handled areas in the changes. + * Any existing GitHub issues being addressed by the changes. + +.. _testing-warp: + +Testing Warp +------------ + +Running the Test Suite +^^^^^^^^^^^^^^^^^^^^^^ + +Warp's test suite uses the `unittest `__ unit testing framework, +along with `unittest-parallel `__ to run tests in parallel. + +The majority of the Warp tests are located in the `warp/tests `__ +directory. As part of the test suite, most examples in the ``warp/examples`` subdirectories are tested via +`test_examples.py `__. + +After building and installing Warp (``pip install -e .`` from the project root), run the test suite using +``python -m warp.tests``. The tests should take 5–10 minutes to run. By default, only the test modules +defined in ``default_suite()`` (in ``warp/tests/unittest_suites.py``) are run. To run the test suite +using `test discovery `__, use +``python -m warp.tests -s autodetect``, which will discover tests in modules matching the path +``warp/tests/test*.py``. + +Running a subset of tests +""""""""""""""""""""""""" + +Instead of running the full test suite, there are two main ways to select a subset of tests to run. +These options must be used with the ``-s autodetect`` option. + +Use ``-p PATTERN`` to define a pattern to match test files. +For example, to run only tests that have ``mesh`` in the file name, use: + +.. code-block:: bash + + python -m warp.tests -s autodetect -p '*mesh*.py' + +Use ``-k TESTNAMEPATTERNS`` to define `wildcard test name patterns `__. +This option can be used multiple times. +For example, to run only tests that have either ``mgpu`` or ``cuda`` in their name, use: + +.. code-block:: bash + + python -m warp.tests -s autodetect -k 'mgpu' -k 'cuda' + +Adding New Tests +^^^^^^^^^^^^^^^^ + +For tests that should be run on multiple devices, e.g. ``"cpu"``, ``"cuda:0"``, and ``"cuda:1"``, we recommend +first defining a test function at the module scope and then using ``add_function_test()`` to add multiple +test methods (a separate method for each device) to a test class. + +.. code-block:: python + + # Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. + # NVIDIA CORPORATION and its licensors retain all intellectual property + # and proprietary rights in and to this software, related documentation + # and any modifications thereto. Any use, reproduction, disclosure or + # distribution of this software and related documentation without an express + # license agreement from NVIDIA CORPORATION is strictly prohibited. + + import unittest + + import warp as wp + from warp.tests.unittest_utils import * + + + def test_amazing_code_test_one(test, device): + pass + + devices = get_test_devices() + + + class TestAmazingCode(unittest.TestCase): + pass + + add_function_test(TestAmazingCode, "test_amazing_code_test_one", test_amazing_code_test_one, devices=devices) + + + if __name__ == "__main__": + wp.clear_kernel_cache() + unittest.main(verbosity=2) + +If we directly run this module, we get the following output: + +.. code-block:: bash + + python test_amazing_code.py + Warp 1.3.1 initialized: + CUDA Toolkit 12.6, Driver 12.6 + Devices: + "cpu" : "x86_64" + "cuda:0" : "NVIDIA GeForce RTX 3090" (24 GiB, sm_86, mempool enabled) + "cuda:1" : "NVIDIA GeForce RTX 3090" (24 GiB, sm_86, mempool enabled) + CUDA peer access: + Supported fully (all-directional) + Kernel cache: + /home/nvidia/.cache/warp/1.3.1 + test_amazing_code_test_one_cpu (__main__.TestAmazingCode) ... ok + test_amazing_code_test_one_cuda_0 (__main__.TestAmazingCode) ... ok + test_amazing_code_test_one_cuda_1 (__main__.TestAmazingCode) ... ok + + ---------------------------------------------------------------------- + Ran 3 tests in 0.001s + + OK + +Note that the output indicated that three tests were run, despite us only writing a single test function called +``test_amazing_code_test_one()``. +A closer inspection reveals that the test function was run on three separate devices: ``"cpu"``, ``"cuda:0"``, and +``cuda:1``. This is a result of calling ``add_function_test()`` in our test script with the `devices=devices` argument. +``add_function_test()`` is defined in ``warp/tests/unittest_utils.py``. + +A caveat of using ``add_function_test()`` is that this by itself is not sufficient to ensure that the registered test +function (e.g. `test_amazing_code_test_one()`) is run on different devices. It is up to the body of the test to make use +of the ``device`` argument in ensuring that data is allocated on and kernels are run on the intended ``device`` for the +test, e.g. + +.. code-block:: python + + def test_amazing_code_test_one(test, device): + with wp.ScopedDevice(device): + score = wp.zeros(1, dtype=float, requires_grad=True) + +or + +.. code-block:: python + + def test_amazing_code_test_one(test, device): + score = wp.zeros(1, dtype=float, requires_grad=True, device=device) + +Checking for Expected Behaviors +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Due to the use of the test-registration function ``add_function_test()``, the ``test`` parameter actually refers to the +instance of the test class, which always subclasses ``unittest.TestCase``. + +The ``unittest`` library also provides methods to check that assertions are raised, as it is also important to test code +paths that trigger errors. The `assertRaises() `__ +and `assertRaisesRegex() `__ +methods can be used to test that a block of code correctly raises an exception. + +Sometimes we need to compare the contents of a Warp array with an expected result. +Some functions that are helpful include: + +* ``assert_np_equal()``: Accepts two NumPy arrays as input parameters along with an optional absolute tolerance ``tol`` + defaulted to 0. If the tolerance is 0, the arrays are compared using ``np.testing.assert_array_equal()``. Otherwise, + both NumPy arrays are flattened and compared with ``np.testing.assert_allclose()``. +* ``assert_array_equal()``: Accepts two Warp arrays as input parameters, converts each array to a NumPy array on the + CPU, and then compares the arrays using ``np.testing.assert_equal()``. +* ``wp.expect_eq()``: Unlike the previous two functions, the array(s) are to be compared by running a Warp kernel + so the data can remain in the GPU. This is important if the array is particularly large that an element-wise + comparison on the CPU would be prohibitively slow. + +Skipping Tests +^^^^^^^^^^^^^^ + +Warp needs to be tested on multiple operating systems including macOS, on which NVIDIA GPUs are not supported. +When it is not possible for a particular test to be executed on *any* devices, there are some mechanisms to mark the +test as *skipped*. + +``unittest`` provides some `methods `__ +to skip a test. + +If the test function is added to a test class using ``add_function_test()``, we can pass an empty list as the argument +to the ``device`` parameter. + +The final common technique is to avoid calling ``add_function_test`` on a test function in order to skip it. +Examples are `test_torch.py `__, +`test_jax.py `__, and +`test_dlpack.py `__. +This technique is discouraged because the test is not marked as skipped in the ``unittest`` framework. +Instead, the test is treated as if it does not exist. +This can create a situation in which we are unaware that a test is being skipped because it does not show up under the +skipped tests count (it doesn't show up under the passed tests count, either). + +Besides the situation in which a test requires CUDA, some examples for skipping tests are: + +* ``usd-core`` is not installed in the current environment. +* The installed JAX version is too old. +* Warp was not built with CUTLASS support (e.g. `python build_lib.py --quick`). +* The system does not have at least two CUDA devices available (e.g. required for a multi-GPU test). + +Tests Without a Device +^^^^^^^^^^^^^^^^^^^^^^ + +Recall that we previously discussed the use of ``add_function_test()`` to register a test function so that it can be +run on different devices (e.g. ``"cpu"`` and ``"cuda:0"``). +Sometimes, a test function doesn't make use of a specific device and we only want to run it a single time. + +If we still want to use ``add_function_test()`` to register the test, we can pass ``devices=None`` to indicate that the +function does not make use of devices. In this case, the function will be registered only a single time to the test +class passed to ``add_function_test()``. + +An alternative is to avoid the use of ``add_function_test()`` altogether and define the test function inside the +test class *directly*. +Taking our previous example with ``TestAmazingCode``, instead of the class body simply being +``pass``, we can add a device-agnostic function: + +.. code-block:: python + + class TestAmazingCode(unittest.TestCase): + def test_amazing_code_no_device(self): + self.assertEqual(True, True) + +This technique can be more readable to some developers because it avoids the obfuscation of +``add_function_test(..., device=None)``. +After all, ``add_function_test()`` is used to facilitate the execution of a single test function on different devices +instead of having to define a separate function for each device. diff --git a/docs/modules/differentiability.rst b/docs/modules/differentiability.rst index 81145d8d..3f1b8243 100644 --- a/docs/modules/differentiability.rst +++ b/docs/modules/differentiability.rst @@ -176,6 +176,7 @@ When we run simulations independently in parallel, the Jacobian corresponding to tape.zero() +.. _custom-gradient-functions: Custom Gradient Functions ######################### diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index ffd87dc9..30a9fd80 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -836,29 +836,56 @@ Tile Primitives :returns: A tile with ``shape=(1,n)`` with linearly spaced elements of specified dtype -.. py:function:: tile_load(a: Array[Any], x: int32, y: int32, m: int32, n: int32) -> Tile +.. py:function:: tile_load(a: Array[Any], i: int32, n: int32) -> Tile - Loads a tile from a global memory array. + Loads a 1D tile from a global memory array. This method will cooperatively load a tile from global memory using all threads in the block. :param a: The source array in global memory - :param x: Offset in the source array measured in multiples of ``m``, i.e.: ``i=x*m`` - :param y: Offset in the source array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param i: Offset in the source array measured in multiples of ``n``, i.e.: ``offset=i*n`` + :param n: The number of elements in the tile + :returns: A tile with ``shape=(1,n)`` and dtype the same as the source array + + +.. py:function:: tile_load(a: Array[Any], i: int32, j: int32, m: int32, n: int32) -> Tile + :noindex: + :nocontentsentry: + + Loads a 2D tile from a global memory array. + + This method will cooperatively load a tile from global memory using all threads in the block. + + :param a: The source array in global memory + :param i: Offset in the source array measured in multiples of ``m``, i.e.: ``row=i*m`` + :param j: Offset in the source array measured in multiples of ``n``, i.e.; ``col=j*n`` :param m: The size of the tile's first dimension - :param n: The size of the tile's second dimensions + :param n: The size of the tile's second dimension :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array -.. py:function:: tile_store(a: Array[Any], x: int32, y: int32, t: Any) -> None +.. py:function:: tile_store(a: Array[Any], i: int32, t: Any) -> None + + Stores a 1D tile to a global memory array. + + This method will cooperatively store a tile to global memory using all threads in the block. + + :param a: The destination array in global memory + :param i: Offset in the destination array measured in multiples of ``n``, i.e.: ``offset=i*n`` + :param t: The source tile to store data from, must have the same dtype as the destination array + + +.. py:function:: tile_store(a: Array[Any], i: int32, j: int32, t: Any) -> None + :noindex: + :nocontentsentry: Stores a tile to a global memory array. This method will cooperatively store a tile to global memory using all threads in the block. :param a: The destination array in global memory - :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*m`` - :param y: Offset in the destination array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param i: Offset in the destination array measured in multiples of ``m``, i.e.: ``row=i*m`` + :param j: Offset in the destination array measured in multiples of ``n``, i.e.; ``col=j*n`` :param t: The source tile to store data from, must have the same dtype as the destination array @@ -879,8 +906,11 @@ Tile Primitives This function converts values computed using scalar kernel code to a tile representation for input into collective operations. + * If the input value is a scalar then the resulting tile has ``shape=(1, block_dim)`` + * If the input value is a vector then the resulting tile has ``shape=(length(vector), block_dim)`` + :param x: A per-thread local value, e.g.: scalar, vector, or matrix. - :returns: A tile with ``shape=(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch()``. + :returns: A tile with first dimension according to the value type length and a second dimension equal to ``block_dim`` This example shows how to create a linear sequence from thread variables: @@ -898,7 +928,8 @@ Tile Primitives .. code-block:: text - tile(m=1, n=16, storage=register) = [[0 2 4 6 8 10 12 14...]] + tile(m=1, n=16, storage=register) = [[0 2 4 6 8 ...]] + @@ -908,6 +939,9 @@ Tile Primitives This function converts a block-wide tile back to per-thread values. + * If the input tile is 1-dimensional then the resulting value will be a per-thread scalar + * If the input tile is 2-dimensional then the the resulting value will be a per-thread vector of length M + :param a: A tile with dimensions ``shape=(M, block_dim)`` :returns: A single value per-thread with the same dtype as the tile @@ -964,6 +998,16 @@ Tile Primitives :returns: Tile with ``shape=(N,M)`` +.. py:function:: tile_broadcast(a: Tile, m: int32, n: int32) -> Tile + + Broadcast a tile. + + This method will attempt to broadcast the input tile ``a`` to the destination shape (m, n), broadcasting follows NumPy broadcast rules. + + :param a: Tile to broadcast + :returns: Tile with broadcast ``shape=(m, n)`` + + .. py:function:: tile_sum(a: Tile) -> Tile Cooperatively compute the sum the tile elements using all threads in the block. @@ -1529,6 +1573,8 @@ Utility Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1536,6 +1582,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1543,6 +1591,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1550,6 +1600,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1557,6 +1609,8 @@ Utility Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1564,6 +1618,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1571,6 +1627,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1578,6 +1636,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1585,6 +1645,8 @@ Utility Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1592,6 +1654,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1599,6 +1663,8 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1606,11 +1672,15 @@ Utility Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: Array[Any], i: int32, value: Any) -> Any Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1618,6 +1688,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1625,6 +1697,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1632,6 +1706,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1639,6 +1715,8 @@ Utility Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1646,6 +1724,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1653,6 +1733,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1660,6 +1742,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any :noindex: @@ -1667,6 +1751,8 @@ Utility Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any :noindex: @@ -1674,6 +1760,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any :noindex: @@ -1681,6 +1769,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any :noindex: @@ -1688,6 +1778,8 @@ Utility Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + .. py:function:: lerp(a: Float, b: Float, t: Float) -> Float @@ -2581,5 +2673,22 @@ Operators :nocontentsentry: + + +Code Generation +--------------- +.. py:function:: static(expr: Any) -> Any + + Evaluates a static Python expression and replaces it with its result. + + See the `codegen.html#static-expressions
`_ for more details. + + Note: + The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, + which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. + The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions + (excluding Warp arrays since they cannot be created in a Warp kernel at the moment). + + .. rubric:: Footnotes .. [1] Function gradients have not been implemented for backpropagation. diff --git a/docs/modules/interoperability.rst b/docs/modules/interoperability.rst index 800d4e79..ef215f7c 100644 --- a/docs/modules/interoperability.rst +++ b/docs/modules/interoperability.rst @@ -709,6 +709,7 @@ The canonical way to export a Warp array to an external framework is to use the jax_array = jax.dlpack.from_dlpack(warp_array) torch_tensor = torch.utils.dlpack.from_dlpack(warp_array) + paddle_tensor = paddle.utils.dlpack.from_dlpack(warp_array) For CUDA arrays, this will synchronize the current stream of the consumer framework with the current Warp stream on the array's device. Thus it should be safe to use the wrapped array in the consumer framework, even if the array was previously used in a Warp kernel @@ -719,9 +720,11 @@ This approach may be used for older versions of frameworks that do not support t warp_array1 = wp.from_dlpack(jax.dlpack.to_dlpack(jax_array)) warp_array2 = wp.from_dlpack(torch.utils.dlpack.to_dlpack(torch_tensor)) + warp_array3 = wp.from_dlpack(paddle.utils.dlpack.to_dlpack(paddle_tensor)) jax_array = jax.dlpack.from_dlpack(wp.to_dlpack(warp_array)) torch_tensor = torch.utils.dlpack.from_dlpack(wp.to_dlpack(warp_array)) + paddle_tensor = paddle.utils.dlpack.from_dlpack(wp.to_dlpack(warp_array)) This approach is generally faster because it skips any stream synchronization, but another solution must be used to ensure correct ordering of operations. In situations where no synchronization is required, using this approach can yield better performance. @@ -733,3 +736,181 @@ This may be a good choice in situations like these: .. autofunction:: warp.from_dlpack .. autofunction:: warp.to_dlpack + +.. _paddle-interop: + +Paddle +------ + +Warp provides helper functions to convert arrays to/from Paddle:: + + w = wp.array([1.0, 2.0, 3.0], dtype=float, device="cpu") + + # convert to Paddle tensor + t = wp.to_paddle(w) + + # convert from Paddle tensor + w = wp.from_paddle(t) + +These helper functions allow the conversion of Warp arrays to/from Paddle tensors without copying the underlying data. +At the same time, if available, gradient arrays and tensors are converted to/from Paddle autograd tensors, allowing the use of Warp arrays +in Paddle autograd computations. + +.. autofunction:: warp.from_paddle +.. autofunction:: warp.to_paddle +.. autofunction:: warp.device_from_paddle +.. autofunction:: warp.device_to_paddle +.. autofunction:: warp.dtype_from_paddle +.. autofunction:: warp.dtype_to_paddle + +To convert a Paddle CUDA stream to a Warp CUDA stream and vice versa, Warp provides the following functions: + +.. autofunction:: warp.stream_from_paddle + +Example: Optimization using ``warp.from_paddle()`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +An example usage of minimizing a loss function over an array of 2D points written in Warp via Paddle's Adam optimizer +using :func:`warp.from_paddle` is as follows:: + + import warp as wp + import paddle + + # init warp context at beginning + wp.context.init() + + @wp.kernel() + def loss(xs: wp.array(dtype=float, ndim=2), l: wp.array(dtype=float)): + tid = wp.tid() + wp.atomic_add(l, 0, xs[tid, 0] ** 2.0 + xs[tid, 1] ** 2.0) + + # indicate requires_grad so that Warp can accumulate gradients in the grad buffers + xs = paddle.randn([100, 2]) + xs.stop_gradient = False + l = paddle.zeros([1]) + l.stop_gradient = False + opt = paddle.optimizer.Adam(learning_rate=0.1, parameters=[xs]) + + wp_xs = wp.from_paddle(xs) + wp_l = wp.from_paddle(l) + + tape = wp.Tape() + with tape: + # record the loss function kernel launch on the tape + wp.launch(loss, dim=len(xs), inputs=[wp_xs], outputs=[wp_l], device=wp_xs.device) + + for i in range(500): + tape.zero() + tape.backward(loss=wp_l) # compute gradients + # now xs.grad will be populated with the gradients computed by Warp + opt.step() # update xs (and thereby wp_xs) + + # these lines are only needed for evaluating the loss + # (the optimization just needs the gradient, not the loss value) + wp_l.zero_() + wp.launch(loss, dim=len(xs), inputs=[wp_xs], outputs=[wp_l], device=wp_xs.device) + print(f"{i}\tloss: {l.item()}") + +Example: Optimization using ``warp.to_paddle`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Less code is needed when we declare the optimization variables directly in Warp and use :func:`warp.to_paddle` to convert them to Paddle tensors. +Here, we revisit the same example from above where now only a single conversion to a paddle tensor is needed to supply Adam with the optimization variables:: + + import warp as wp + import numpy as np + import paddle + + # init warp context at beginning + wp.context.init() + + @wp.kernel() + def loss(xs: wp.array(dtype=float, ndim=2), l: wp.array(dtype=float)): + tid = wp.tid() + wp.atomic_add(l, 0, xs[tid, 0] ** 2.0 + xs[tid, 1] ** 2.0) + + # initialize the optimization variables in Warp + xs = wp.array(np.random.randn(100, 2), dtype=wp.float32, requires_grad=True) + l = wp.zeros(1, dtype=wp.float32, requires_grad=True) + # just a single wp.to_paddle call is needed, Adam optimizes using the Warp array gradients + opt = paddle.optimizer.Adam(learning_rate=0.1, parameters=[wp.to_paddle(xs)]) + + tape = wp.Tape() + with tape: + wp.launch(loss, dim=len(xs), inputs=[xs], outputs=[l], device=xs.device) + + for i in range(500): + tape.zero() + tape.backward(loss=l) + opt.step() + + l.zero_() + wp.launch(loss, dim=len(xs), inputs=[xs], outputs=[l], device=xs.device) + print(f"{i}\tloss: {l.numpy()[0]}") + +Performance Notes +^^^^^^^^^^^^^^^^^ + +The ``wp.from_paddle()`` function creates a Warp array object that shares data with a Paddle tensor. Although this function does not copy the data, there is always some CPU overhead during the conversion. If these conversions happen frequently, the overall program performance may suffer. As a general rule, it's good to avoid repeated conversions of the same tensor. Instead of: + +.. code:: python + + x_t = paddle.arange(n, dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + y_t = paddle.ones([n], dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + + for i in range(10): + x_w = wp.from_paddle(x_t) + y_w = wp.from_paddle(y_t) + wp.launch(saxpy, dim=n, inputs=[x_w, y_w, 1.0], device=device) + +Try converting the arrays only once and reuse them: + +.. code:: python + + x_t = paddle.arange(n, dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + y_t = paddle.ones([n], dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + + x_w = wp.from_paddle(x_t) + y_w = wp.from_paddle(y_t) + + for i in range(10): + wp.launch(saxpy, dim=n, inputs=[x_w, y_w, 1.0], device=device) + +If reusing arrays is not possible (e.g., a new Paddle tensor is constructed on every iteration), passing ``return_ctype=True`` to ``wp.from_paddle()`` should yield faster performance. Setting this argument to True avoids constructing a ``wp.array`` object and instead returns a low-level array descriptor. This descriptor is a simple C structure that can be passed to Warp kernels instead of a ``wp.array``, but cannot be used in other places that require a ``wp.array``. + +.. code:: python + + for n in range(1, 10): + # get Paddle tensors for this iteration + x_t = paddle.arange(n, dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + y_t = paddle.ones([n], dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + + # get Warp array descriptors + x_ctype = wp.from_paddle(x_t, return_ctype=True) + y_ctype = wp.from_paddle(y_t, return_ctype=True) + + wp.launch(saxpy, dim=n, inputs=[x_ctype, y_ctype, 1.0], device=device) + +An alternative approach is to pass the Paddle tensors to Warp kernels directly. This avoids constructing temporary Warp arrays by leveraging standard array interfaces (like ``__cuda_array_interface__``) supported by both Paddle and Warp. The main advantage of this approach is convenience, since there is no need to call any conversion functions. The main limitation is that it does not handle gradients, because gradient information is not included in the standard array interfaces. This technique is therefore most suitable for algorithms that do not involve differentiation. + +.. code:: python + + x = paddle.arange(n, dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + y = paddle.ones([n], dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + + for i in range(10): + wp.launch(saxpy, dim=n, inputs=[x, y, 1.0], device=device) + +.. code:: shell + + python -m warp.examples.benchmarks.benchmark_interop_paddle + +Sample output: + +.. code:: + + 13990 ms from_paddle(...) + 5990 ms from_paddle(..., return_ctype=True) + 35167 ms direct from paddle + +The default ``wp.from_paddle()`` conversion is the slowest. Passing ``return_ctype=True`` is the fastest, because it skips creating temporary Warp array objects. Passing Paddle tensors to Warp kernels directly falls somewhere in between. It skips creating temporary Warp arrays, but accessing the ``__cuda_array_interface__`` attributes of Paddle tensors adds overhead because they are initialized on-demand. diff --git a/docs/modules/runtime.rst b/docs/modules/runtime.rst index 05c63d43..aa628608 100644 --- a/docs/modules/runtime.rst +++ b/docs/modules/runtime.rst @@ -47,67 +47,7 @@ generated compilation artifacts as Warp does not automatically try to keep the c Runtime Kernel Creation ####################### -It is often desirable to specialize kernels for different types, constants, or functions at runtime. -We can achieve this through the use of runtime kernel specialization using Python closures. - -For example, we might require a variety of kernels that execute particular functions for each item in an array. -We might also want this function call to be valid for a variety of data types. Making use of closure and generics, we can generate -these kernels using a single kernel definition:: - - def make_kernel(func, dtype): - def closure_kernel_fn(data: wp.array(dtype=dtype), out: wp.array(dtype=dtype)): - tid = wp.tid() - out[tid] = func(data[tid]) - - return wp.Kernel(closure_kernel_fn) - -In practice, we might use our kernel generator, ``make_kernel()`` as follows:: - - @wp.func - def sqr(x: Any) -> Any: - return x * x - - @wp.func - def cube(x: Any) -> Any: - return sqr(x) * x - - sqr_float = make_kernel(sqr, wp.float32) - cube_double = make_kernel(cube, wp.float64) - - arr = [1.0, 2.0, 3.0] - N = len(arr) - - data_float = wp.array(arr, dtype=wp.float32, device=device) - data_double = wp.array(arr, dtype=wp.float64, device=device) - - out_float = wp.zeros(N, dtype=wp.float32, device=device) - out_double = wp.zeros(N, dtype=wp.float64, device=device) - - wp.launch(sqr_float, dim=N, inputs=[data_float], outputs=[out_float], device=device) - wp.launch(cube_double, dim=N, inputs=[data_double], outputs=[out_double], device=device) - -We can specialize kernel definitions over Warp constants similarly. The following generates kernels that add a specified constant -to a generic-typed array value:: - - def make_add_kernel(key, constant): - def closure_kernel_fn(data: wp.array(dtype=Any), out: wp.array(dtype=Any)): - tid = wp.tid() - out[tid] = data[tid] + constant - - return wp.Kernel(closure_kernel_fn, key=key) - - add_ones_int = make_add_kernel("add_one", wp.constant(1)) - add_ones_vec3 = make_add_kernel("add_ones_vec3", wp.constant(wp.vec3(1.0, 1.0, 1.0))) - - a = wp.zeros(2, dtype=int) - b = wp.zeros(2, dtype=wp.vec3) - - a_out = wp.zeros_like(a) - b_out = wp.zeros_like(b) - - wp.launch(add_ones_int, dim=a.size, inputs=[a], outputs=[a_out], device=device) - wp.launch(add_ones_vec3, dim=b.size, inputs=[b], outputs=[b_out], device=device) - +Warp allows generating kernels on-the-fly with various customizations, including closure support. Refer to the :ref:`Code Generation` section for the latest features. .. _Arrays: @@ -684,12 +624,15 @@ This can be surprising for users that are accustomed to C-style conversions but Users should explicitly cast variables to compatible types using constructors like ``int()``, ``float()``, ``wp.float16()``, ``wp.uint8()``, etc. +.. note:: + For performance reasons, Warp relies on native compilers to perform numeric conversions (e.g., LLVM for CPU and NVRTC for CUDA). This is generally not a problem, but in some cases the results may vary on different devices. For example, the conversion ``wp.uint8(-1.0)`` results in undefined behavior, since the floating point value -1.0 is out of range for unsigned integer types. C++ compilers are free to handle such cases as they see fit. Numeric conversions are only guaranteed to produce correct results when the value being converted is in the range supported by the target data type. + Constants --------- -In general, Warp kernels cannot access variables in the global Python interpreter state. One exception to this is for compile-time constants, which may be declared globally (or as class attributes) and folded into the kernel definition. +A Warp kernel can access Python variables defined outside of the kernel, which are treated as compile-time constants inside of the kernel. -Constants are defined using the ``wp.constant()`` function. An example is shown below:: +.. code:: python TYPE_SPHERE = wp.constant(0) TYPE_CUBE = wp.constant(1) @@ -700,15 +643,16 @@ Constants are defined using the ``wp.constant()`` function. An example is shown t = geometry[wp.tid()] - if (t == TYPE_SPHERE): + if t == TYPE_SPHERE: print("sphere") - if (t == TYPE_CUBE): + elif t == TYPE_CUBE: print("cube") - if (t == TYPE_CAPSULE): + elif t == TYPE_CAPSULE: print("capsule") +Note that using ``wp.constant()`` is no longer required, but it performs some type checking and can serve as a reminder that the variables are meant to be used as Warp constants. -.. autoclass:: constant +The behavior is simple and intuitive when the referenced Python variables never change. For details and more complex scenarios, refer to :ref:`External References and Constants`. The :ref:`Code Generation` section contains additional information and tips for advanced usage. Predefined Constants #################### diff --git a/docs/modules/sim.rst b/docs/modules/sim.rst index f6d6ce06..973401ad 100644 --- a/docs/modules/sim.rst +++ b/docs/modules/sim.rst @@ -163,6 +163,9 @@ Integrators .. autoclass:: FeatherstoneIntegrator :members: +.. autoclass:: VBDIntegrator + :members: + Importers --------- diff --git a/docs/requirements.txt b/docs/requirements.txt index d63c016a..b8b6bd59 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,5 +1,5 @@ -furo==2024.7.18 -sphinx==7.4.7 +furo==2024.8.6 +sphinx==8.0.2 sphinx_copybutton==0.5.2 -numpy==1.26.4 -ruff==0.5.5 +numpy==2.1.1 +ruff==0.6.8 diff --git a/exts/omni.warp.core/config/extension.toml b/exts/omni.warp.core/config/extension.toml index 8b80e91f..841caf50 100644 --- a/exts/omni.warp.core/config/extension.toml +++ b/exts/omni.warp.core/config/extension.toml @@ -1,6 +1,6 @@ [package] # Semantic Versioning is used: https://semver.org/ -version = "1.3.3" +version = "1.4.0" authors = ["NVIDIA"] title = "Warp Core" description="The core Warp Python module" @@ -38,6 +38,7 @@ pyCoverageOmit = [ "warp/stubs.py", "warp/jax.py", "warp/torch.py", + "warp/paddle.py", "warp/build.py", "warp/build_dll.py", "warp/sim/**", diff --git a/exts/omni.warp.core/docs/CHANGELOG.md b/exts/omni.warp.core/docs/CHANGELOG.md index 73426dca..82fb2e73 100644 --- a/exts/omni.warp.core/docs/CHANGELOG.md +++ b/exts/omni.warp.core/docs/CHANGELOG.md @@ -1,5 +1,73 @@ # CHANGELOG +## [1.4.0] - 2024-10-01 + +### Added + +- Support for a new `wp.static(expr)` function that allows arbitrary Python expressions to be evaluated at the time of + function/kernel definition ([docs](https://nvidia.github.io/warp/codegen.html#static-expressions)). +- Support for stream priorities to hint to the device that it should process pending work + in high-priority streams over pending work in low-priority streams when possible + ([docs](https://nvidia.github.io/warp/modules/concurrency.html#stream-priorities)). +- Adaptive sparse grid geometry to `warp.fem` ([docs](https://nvidia.github.io/warp/modules/fem.html#adaptivity)). +- Support for defining `wp.kernel` and `wp.func` objects from within closures. +- Support for defining multiple versions of kernels, functions, and structs without manually assigning unique keys. +- Support for default argument values for user functions decorated with `wp.func`. +- Allow passing custom launch dimensions to `jax_kernel()` ([GH-310](https://github.com/NVIDIA/warp/pull/310)). +- JAX interoperability examples for sharding and matrix multiplication ([docs](https://nvidia.github.io/warp/modules/interoperability.html#using-shardmap-for-distributed-computation)). +- Interoperability support for the PaddlePaddle ML framework ([GH-318](https://github.com/NVIDIA/warp/pull/318)). +- Support `wp.mod()` for vector types ([GH-282](https://github.com/NVIDIA/warp/issues/282)). +- Expose the modulo operator `%` to Python's runtime scalar and vector types. +- Support for fp64 `atomic_add`, `atomic_max`, and `atomic_min` ([GH-284](https://github.com/NVIDIA/warp/issues/284)). +- Support for quaternion indexing (e.g. `q.w`). +- Support shadowing builtin functions ([GH-308](https://github.com/NVIDIA/warp/issues/308)). +- Support for redefining function overloads. +- Add an ocean sample to the `omni.warp` extension. +- `warp.sim.VBDIntegrator` now supports body-particle collision. +- Add a [contributing guide](https://nvidia.github.io/warp/modules/contribution_guide.html) to the Sphinx docs . +- Add documentation for dynamic code generation ([docs](https://nvidia.github.io/warp/codegen.html#dynamic-kernel-creation)). + +### Changed + +- `wp.sim.Model.edge_indices` now includes boundary edges. +- Unexposed `wp.rand*()`, `wp.sample*()`, and `wp.poisson()` from the Python scope. +- Skip unused functions in module code generation, improving performance. +- Avoid reloading modules if their content does not change, improving performance. +- `wp.Mesh.points` is now a property instead of a raw data member, its reference can be changed after the mesh is initialized. +- Improve error message when invalid objects are referenced in a Warp kernel. +- `if`/`else`/`elif` statements with constant conditions are resolved at compile time with no branches being inserted in the generated code. +- Include all non-hidden builtins in the stub file. +- Improve accuracy of symmetric eigenvalues routine in `warp.fem`. + +### Fixed + +- Fix for `wp.func` erroring out when defining a `Tuple` as a return type hint ([GH-302](https://github.com/NVIDIA/warp/issues/302)). +- Fix array in-place op (`+=`, `-=`) adjoints to compute gradients correctly in the backwards pass +- Fix vector, matrix in-place assignment adjoints to compute gradients correctly in the backwards pass, e.g.: `v[1] = x` +- Fix a bug in which Python docstrings would be created as local function variables in generated code. +- Fix a bug with autograd array access validation in functions from different modules. +- Fix a rare crash during error reporting on some systems due to glibc mismatches. +- Handle `--num_tiles 1` in `example_render_opengl.py` ([GH-306](https://github.com/NVIDIA/warp/issues/306)). +- Fix the computation of body contact forces in `FeatherstoneIntegrator` when bodies and particles collide. +- Fix bug in `FeatherstoneIntegrator` where `eval_rigid_jacobian` could give incorrect results or reach an infinite + loop when the body and joint indices were not in the same order. Added `Model.joint_ancestor` to fix the indexing + from a joint to its parent joint in the articulation. +- Fix wrong vertex index passed to `add_edges()` called from `ModelBuilder.add_cloth_mesh()` ([GH-319](https://github.com/NVIDIA/warp/issues/319)). +- Add a workaround for uninitialized memory read warning in the `compute-sanitizer` initcheck tool when using `wp.Mesh`. +- Fix name clashes when Warp functions and structs are returned from Python functions multiple times. +- Fix name clashes between Warp functions and structs defined in different modules. +- Fix code generation errors when overloading generic kernels defined in a Python function. +- Fix issues with unrelated functions being treated as overloads (e.g., closures). +- Fix handling of `stream` argument in `array.__dlpack__()`. +- Fix a bug related to reloading CPU modules. +- Fix a crash when kernel functions are not found in CPU modules. +- Fix conditions not being evaluated as expected in `while` statements. +- Fix printing Boolean and 8-bit integer values. +- Fix array interface type strings used for Boolean and 8-bit integer values. +- Fix initialization error when setting struct members. +- Fix Warp not being initialized upon entering a `wp.Tape` context. +- Use `kDLBool` instead of `kDLUInt` for DLPack interop of Booleans. + ## [1.3.3] - 2024-09-04 - Bug fixes diff --git a/exts/omni.warp/config/extension.toml b/exts/omni.warp/config/extension.toml index 8e80c45d..cfebd3b6 100644 --- a/exts/omni.warp/config/extension.toml +++ b/exts/omni.warp/config/extension.toml @@ -1,6 +1,6 @@ [package] # Semantic Versioning is used: https://semver.org/ -version = "1.3.3" +version = "1.4.0" authors = ["NVIDIA"] title = "Warp" description="Warp OmniGraph Nodes and Sample Scenes" @@ -35,7 +35,7 @@ exclude = ["Ogn*Database.py", "*/ogn*"] "omni.timeline" = {} "omni.ui" = {optional = true} "omni.usd" = {} -"omni.warp.core" = {version = "1.3.3", exact = true} +"omni.warp.core" = {version = "1.4.0", exact = true} [[python.module]] name = "omni.warp._extension" diff --git a/exts/omni.warp/docs/CHANGELOG.md b/exts/omni.warp/docs/CHANGELOG.md index 73426dca..82fb2e73 100644 --- a/exts/omni.warp/docs/CHANGELOG.md +++ b/exts/omni.warp/docs/CHANGELOG.md @@ -1,5 +1,73 @@ # CHANGELOG +## [1.4.0] - 2024-10-01 + +### Added + +- Support for a new `wp.static(expr)` function that allows arbitrary Python expressions to be evaluated at the time of + function/kernel definition ([docs](https://nvidia.github.io/warp/codegen.html#static-expressions)). +- Support for stream priorities to hint to the device that it should process pending work + in high-priority streams over pending work in low-priority streams when possible + ([docs](https://nvidia.github.io/warp/modules/concurrency.html#stream-priorities)). +- Adaptive sparse grid geometry to `warp.fem` ([docs](https://nvidia.github.io/warp/modules/fem.html#adaptivity)). +- Support for defining `wp.kernel` and `wp.func` objects from within closures. +- Support for defining multiple versions of kernels, functions, and structs without manually assigning unique keys. +- Support for default argument values for user functions decorated with `wp.func`. +- Allow passing custom launch dimensions to `jax_kernel()` ([GH-310](https://github.com/NVIDIA/warp/pull/310)). +- JAX interoperability examples for sharding and matrix multiplication ([docs](https://nvidia.github.io/warp/modules/interoperability.html#using-shardmap-for-distributed-computation)). +- Interoperability support for the PaddlePaddle ML framework ([GH-318](https://github.com/NVIDIA/warp/pull/318)). +- Support `wp.mod()` for vector types ([GH-282](https://github.com/NVIDIA/warp/issues/282)). +- Expose the modulo operator `%` to Python's runtime scalar and vector types. +- Support for fp64 `atomic_add`, `atomic_max`, and `atomic_min` ([GH-284](https://github.com/NVIDIA/warp/issues/284)). +- Support for quaternion indexing (e.g. `q.w`). +- Support shadowing builtin functions ([GH-308](https://github.com/NVIDIA/warp/issues/308)). +- Support for redefining function overloads. +- Add an ocean sample to the `omni.warp` extension. +- `warp.sim.VBDIntegrator` now supports body-particle collision. +- Add a [contributing guide](https://nvidia.github.io/warp/modules/contribution_guide.html) to the Sphinx docs . +- Add documentation for dynamic code generation ([docs](https://nvidia.github.io/warp/codegen.html#dynamic-kernel-creation)). + +### Changed + +- `wp.sim.Model.edge_indices` now includes boundary edges. +- Unexposed `wp.rand*()`, `wp.sample*()`, and `wp.poisson()` from the Python scope. +- Skip unused functions in module code generation, improving performance. +- Avoid reloading modules if their content does not change, improving performance. +- `wp.Mesh.points` is now a property instead of a raw data member, its reference can be changed after the mesh is initialized. +- Improve error message when invalid objects are referenced in a Warp kernel. +- `if`/`else`/`elif` statements with constant conditions are resolved at compile time with no branches being inserted in the generated code. +- Include all non-hidden builtins in the stub file. +- Improve accuracy of symmetric eigenvalues routine in `warp.fem`. + +### Fixed + +- Fix for `wp.func` erroring out when defining a `Tuple` as a return type hint ([GH-302](https://github.com/NVIDIA/warp/issues/302)). +- Fix array in-place op (`+=`, `-=`) adjoints to compute gradients correctly in the backwards pass +- Fix vector, matrix in-place assignment adjoints to compute gradients correctly in the backwards pass, e.g.: `v[1] = x` +- Fix a bug in which Python docstrings would be created as local function variables in generated code. +- Fix a bug with autograd array access validation in functions from different modules. +- Fix a rare crash during error reporting on some systems due to glibc mismatches. +- Handle `--num_tiles 1` in `example_render_opengl.py` ([GH-306](https://github.com/NVIDIA/warp/issues/306)). +- Fix the computation of body contact forces in `FeatherstoneIntegrator` when bodies and particles collide. +- Fix bug in `FeatherstoneIntegrator` where `eval_rigid_jacobian` could give incorrect results or reach an infinite + loop when the body and joint indices were not in the same order. Added `Model.joint_ancestor` to fix the indexing + from a joint to its parent joint in the articulation. +- Fix wrong vertex index passed to `add_edges()` called from `ModelBuilder.add_cloth_mesh()` ([GH-319](https://github.com/NVIDIA/warp/issues/319)). +- Add a workaround for uninitialized memory read warning in the `compute-sanitizer` initcheck tool when using `wp.Mesh`. +- Fix name clashes when Warp functions and structs are returned from Python functions multiple times. +- Fix name clashes between Warp functions and structs defined in different modules. +- Fix code generation errors when overloading generic kernels defined in a Python function. +- Fix issues with unrelated functions being treated as overloads (e.g., closures). +- Fix handling of `stream` argument in `array.__dlpack__()`. +- Fix a bug related to reloading CPU modules. +- Fix a crash when kernel functions are not found in CPU modules. +- Fix conditions not being evaluated as expected in `while` statements. +- Fix printing Boolean and 8-bit integer values. +- Fix array interface type strings used for Boolean and 8-bit integer values. +- Fix initialization error when setting struct members. +- Fix Warp not being initialized upon entering a `wp.Tape` context. +- Use `kDLBool` instead of `kDLUInt` for DLPack interop of Booleans. + ## [1.3.3] - 2024-09-04 - Bug fixes diff --git a/warp/__init__.py b/warp/__init__.py index 8ecda0c1..b051f837 100644 --- a/warp/__init__.py +++ b/warp/__init__.py @@ -100,11 +100,17 @@ from warp.dlpack import from_dlpack, to_dlpack +from warp.paddle import from_paddle, to_paddle +from warp.paddle import dtype_from_paddle, dtype_to_paddle +from warp.paddle import device_from_paddle, device_to_paddle +from warp.paddle import stream_from_paddle + from warp.build import clear_kernel_cache from warp.constants import * from . import builtins +from warp.builtins import static import warp.config as config diff --git a/warp/builtins.py b/warp/builtins.py index 19b1254c..1a940161 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1883,6 +1883,7 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a export=False, ) + def tile_load_1d_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -1892,7 +1893,9 @@ def tile_load_1d_value_func(arg_types, arg_values): raise RuntimeError("tile_load() argument 0 must be an array") if arg_types["a"].ndim != 1: - raise RuntimeError("tile_load() argument 0 must be 1-dimensional if using the ``wp.tile_load(array, i, n)`` syntax.") + raise RuntimeError( + "tile_load() argument 0 must be 1-dimensional if using the ``wp.tile_load(array, i, n)`` syntax." + ) if not type_is_int(arg_types["i"]): raise RuntimeError("tile_load() argument 1 must be an integer") @@ -1901,7 +1904,7 @@ def tile_load_1d_value_func(arg_types, arg_values): raise RuntimeError("'n' keyword argument must be specified when calling tile_load() function") a = arg_types["a"] - m, n = 1, arg_values["n"] + _m, n = 1, arg_values["n"] return TileLoad(a, 1, n) @@ -1918,6 +1921,7 @@ def tile_load_1d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, return ((array, i), template_args) + add_builtin( "tile_load", input_types={"a": array(dtype=Any), "i": int, "n": int}, @@ -1946,7 +1950,9 @@ def tile_load_2d_value_func(arg_types, arg_values): raise RuntimeError("tile_load() argument 0 must be an array") if arg_types["a"].ndim != 2: - raise RuntimeError("tile_load() argument 0 must be 2-dimensional if using the ``wp.tile_load(array, i, j, m, n)`` syntax.") + raise RuntimeError( + "tile_load() argument 0 must be 2-dimensional if using the ``wp.tile_load(array, i, j, m, n)`` syntax." + ) if not type_is_int(arg_types["i"]): raise RuntimeError("tile_load() argument 1 must be an integer") @@ -2013,7 +2019,9 @@ def tile_store_1d_value_func(arg_types, arg_values): raise RuntimeError("tile_store() argument 0 must be an array") if arg_types["a"].ndim != 1: - raise RuntimeError("tile_load() argument 0 must be a 1-dimensional array if using the ``wp.tile_store(array, i, t)`` syntax.") + raise RuntimeError( + "tile_load() argument 0 must be a 1-dimensional array if using the ``wp.tile_store(array, i, t)`` syntax." + ) if not type_is_int(arg_types["i"]): raise RuntimeError("tile_store() argument 1 must be an integer") @@ -2044,6 +2052,7 @@ def tile_store_1d_value_func(arg_types, arg_values): export=False, ) + def tile_store_2d_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -2056,7 +2065,9 @@ def tile_store_2d_value_func(arg_types, arg_values): raise RuntimeError("tile_store() argument 0 must be an array") if arg_types["a"].ndim != 2: - raise RuntimeError("tile_load() argument 0 must be a 2-dimensional array if using the ``wp.tile_store(array, i, j, t)`` syntax.") + raise RuntimeError( + "tile_load() argument 0 must be a 2-dimensional array if using the ``wp.tile_store(array, i, j, t)`` syntax." + ) if not type_is_int(arg_types["i"]): raise RuntimeError("tile_store() argument 1 must be an integer") @@ -2343,6 +2354,7 @@ def tile_transpose_value_func(arg_types, arg_values): export=False, ) + def tile_broadcast_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -2364,7 +2376,9 @@ def tile_broadcast_value_func(arg_types, arg_values): elif t.N == n: stride_n = t.strides[1] else: - raise RuntimeError(f"Broadcast dimension must be 1 or match destination, shape(src) = {t.m, t.n}, shape(dest) = {m, n}") + raise RuntimeError( + f"Broadcast dimension must be 1 or match destination, shape(src) = {t.m, t.n}, shape(dest) = {m, n}" + ) # try to broadcast first dimension if t.M == 1: @@ -2372,7 +2386,9 @@ def tile_broadcast_value_func(arg_types, arg_values): elif t.M == m: stride_m = t.strides[0] else: - raise RuntimeError(f"Broadcast dimension must be 1 or match destination, shape(src) = {t.m, t.n}, shape(dest) = {m, n}") + raise RuntimeError( + f"Broadcast dimension must be 1 or match destination, shape(src) = {t.m, t.n}, shape(dest) = {m, n}" + ) # force the input tile to shared memory t.storage = "shared" @@ -2382,8 +2398,8 @@ def tile_broadcast_value_func(arg_types, arg_values): return tile_type -def tile_broadcast_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): +def tile_broadcast_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): tile = arg_values["a"] template_args = [] @@ -2412,8 +2428,6 @@ def tile_broadcast_dispatch_func(arg_types: Mapping[str, type], return_type: Any ) - - def tile_matmul_value_func(arg_types, arg_values): # return generic type (for doc builds) if arg_types is None: @@ -4579,7 +4593,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""", + doc="""Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4588,7 +4604,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""", + doc="""Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4597,7 +4615,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""", + doc="""Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4606,7 +4626,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""", + doc="""Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4616,7 +4638,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""", + doc="""Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4625,7 +4649,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""", + doc="""Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4634,7 +4660,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""", + doc="""Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4643,7 +4671,9 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, hidden=hidden, input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, value_func=atomic_op_value_func, - doc="""Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""", + doc="""Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -5504,7 +5534,7 @@ def tile_matmul_generic_value_func(arg_types, arg_values): raise RuntimeError("tile_matmul() argument 1 must be an tile") # out = wp.tile_matmul(a, b) - if len(arg_types) == 2: + if len(arg_types) == 2: return Tile(dtype=a.dtype, M=a.M, N=b.N, storage="shared") # wp.tile_matmul(a, b, out) @@ -5673,7 +5703,11 @@ def tile_flip_layout(layout): add_builtin( "tile_matmul", - input_types={"a": Tile(dtype=Any, M=Any, N=Any), "b": Tile(dtype=Any, M=Any, N=Any), "out": Tile(dtype=Any, M=Any, N=Any)}, + input_types={ + "a": Tile(dtype=Any, M=Any, N=Any), + "b": Tile(dtype=Any, M=Any, N=Any), + "out": Tile(dtype=Any, M=Any, N=Any), + }, value_func=tile_matmul_generic_value_func, lto_dispatch_func=tile_matmul_generic_lto_dispatch_func, variadic=False, @@ -5864,3 +5898,36 @@ def tile_fft_generic_lto_dispatch_func( export=False, namespace="", ) + +# --------------------------------- +# Code Generation + +add_builtin( + "static", + input_types={"expr": Any}, + value_type=Any, + doc="""Evaluates a static Python expression and replaces it with its result. + + See the `codegen.html#static-expressions
`_ for more details. + + Note: + The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, + which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. + The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions + (excluding Warp arrays since they cannot be created in a Warp kernel at the moment).""", + group="Code Generation", +) + + +def static(expr): + """ + Evaluates a static expression and replaces the expression with its result. + + Args: + expr: A Python expression to evaluate. Must return a non-null value which must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions (excluding Warp arrays since they cannot be created in a Warp kernel at the moment). + + Note: + The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, + which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. + """ + return expr diff --git a/warp/codegen.py b/warp/codegen.py index f347c2fc..50288e05 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -237,8 +237,11 @@ def __init__(self, cls: Struct, ctype): def __getattribute__(self, name): cls = super().__getattribute__("_cls") - if name in cls.vars: - var = cls.vars[name] + if name == "native_name": + return cls.native_name + + var = cls.vars.get(name) + if var is not None: if isinstance(var.type, type) and issubclass(var.type, ctypes.Array): # Each field stored in a `StructInstance` is exposed as # a standard Python attribute but also has a `ctypes` @@ -413,6 +416,9 @@ def __init__(self, cls, key, module): elif issubclass(var.type, ctypes.Array): fields.append((label, var.type)) else: + # HACK: fp16 requires conversion functions from warp.so + if var.type is warp.float16: + warp.init() fields.append((label, var.type._type_)) class StructType(ctypes.Structure): @@ -490,6 +496,10 @@ class NewStructInstance(self.cls, StructInstance): def __init__(inst): StructInstance.__init__(inst, self, None) + # make sure warp.types.get_type_code works with this StructInstance + NewStructInstance.cls = self.cls + NewStructInstance.native_name = self.native_name + return NewStructInstance() def initializer(self): @@ -635,6 +645,9 @@ def type_to_ctype(t, value_type=False): return t.ctype() elif isinstance(t, Struct): return t.native_name + elif isinstance(t, type) and issubclass(t, StructInstance): + # ensure the actual Struct name is used instead of "NewStructInstance" + return t.native_name elif is_reference(t): if not value_type: return Var.type_to_ctype(t.value_type) + "*" @@ -890,6 +903,12 @@ def __init__( # this is to avoid registering false references to overshadowed modules adj.symbols[name] = arg + # try to replace static expressions by their constant result if the + # expression can be evaluated at declaration time + adj.static_expressions: Dict[str, Any] = {} + if "static" in adj.source: + adj.replace_static_expressions() + # There are cases where a same module might be rebuilt multiple times, # for example when kernels are nested inside of functions, or when # a kernel's launch raises an exception. Ideally we'd always want to @@ -929,6 +948,7 @@ def build(adj, builder, default_builder_options=None): adj.return_var = None # return type for function or kernel adj.loop_symbols = [] # symbols at the start of each loop + adj.loop_const_iter_symbols = set() # iteration variables (constant) for static loops # blocks adj.blocks = [Block()] @@ -1268,7 +1288,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): # immediately allocate output variables so we can pass them into the dispatch method if return_type is None: - # void function + # void function output = None output_list = [] elif not isinstance(return_type, Sequence) or len(return_type) == 1: @@ -1282,7 +1302,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): output = [adj.add_var(v) for v in return_type] output_list = output - # If we have a built-in that requires special handling to dispatch # the arguments to the underlying C++ function, then we can resolve # these using the `dispatch_func`. Since this is only called from @@ -1575,6 +1594,16 @@ def emit_If(adj, node): # eval condition cond = adj.eval(node.test) + if cond.constant is not None: + # resolve constant condition + if cond.constant: + for stmt in node.body: + adj.eval(stmt) + else: + for stmt in node.orelse: + adj.eval(stmt) + return None + # save symbol map symbols_prev = adj.symbols.copy() @@ -1670,7 +1699,7 @@ def emit_Name(adj, node): if isinstance(obj, types.ModuleType): return obj - raise RuntimeError("Cannot reference a global variable from a kernel unless `wp.constant()` is being used") + raise TypeError(f"Invalid external reference type: {type(obj)}") @staticmethod def resolve_type_attribute(var_type: type, attr: str): @@ -1784,7 +1813,7 @@ def emit_Ellipsis(adj, node): def emit_NameConstant(adj, node): if node.value: - return adj.add_constant(True) + return adj.add_constant(node.value) elif node.value is None: raise WarpCodegenTypeError("None type unsupported") else: @@ -1798,7 +1827,7 @@ def emit_Constant(adj, node): elif isinstance(node, ast.Ellipsis): return adj.emit_Ellipsis(node) else: - assert isinstance(node, ast.NameConstant) + assert isinstance(node, ast.NameConstant) or isinstance(node, ast.Constant) return adj.emit_NameConstant(node) def emit_BinOp(adj, node): @@ -1839,6 +1868,11 @@ def materialize_redefinitions(adj, symbols): # detect symbols with conflicting definitions (assigned inside the for loop) for items in symbols.items(): sym = items[0] + if adj.loop_const_iter_symbols is not None and sym in adj.loop_const_iter_symbols: + # ignore constant overwriting in for-loops if it is a loop iterator + # (it is no problem to unroll static loops multiple times in sequence) + continue + var1 = items[1] var2 = adj.symbols[sym] @@ -1985,15 +2019,27 @@ def get_unroll_range(adj, loop): ) return range_call + def begin_record_constant_iter_symbols(adj): + if adj.loop_const_iter_symbols is None: + adj.loop_const_iter_symbols = set() + + def end_record_constant_iter_symbols(adj): + adj.loop_const_iter_symbols = None + def emit_For(adj, node): # try and unroll simple range() statements that use constant args unroll_range = adj.get_unroll_range(node) if isinstance(unroll_range, range): + const_iter_sym = node.target.id + if adj.loop_const_iter_symbols is not None: + # prevent constant conflicts in `materialize_redefinitions()` + adj.loop_const_iter_symbols.add(const_iter_sym) + + # unroll static for-loop for i in unroll_range: const_iter = adj.add_constant(i) - var_iter = adj.add_builtin_call("int", [const_iter]) - adj.symbols[node.target.id] = var_iter + adj.symbols[const_iter_sym] = const_iter # eval body for s in node.body: @@ -2009,6 +2055,7 @@ def emit_For(adj, node): iter = adj.eval(node.iter) adj.symbols[node.target.id] = adj.begin_for(iter) + adj.begin_record_constant_iter_symbols() # for loops should be side-effect free, here we store a copy adj.loop_symbols.append(adj.symbols.copy()) @@ -2019,6 +2066,7 @@ def emit_For(adj, node): adj.materialize_redefinitions(adj.loop_symbols[-1]) adj.loop_symbols.pop() + adj.end_record_constant_iter_symbols() adj.end_for(iter) @@ -2075,13 +2123,28 @@ def emit_Call(adj, node): # try and lookup function in globals by # resolving path (e.g.: module.submodule.attr) - func, path = adj.resolve_static_expression(node.func) + if hasattr(node.func, "warp_func"): + func = node.func.warp_func + path = [] + else: + func, path = adj.resolve_static_expression(node.func) if func is None: func = adj.eval(node.func) + if adj.is_static_expression(func): + # try to evaluate wp.static() expressions + obj, _ = adj.evaluate_static_expression(node) + if obj is not None: + if isinstance(obj, warp.context.Function): + # special handling for wp.static() evaluating to a function + return obj + else: + out = adj.add_constant(obj) + return out + type_args = {} - if not isinstance(func, warp.context.Function): + if len(path) > 0 and not isinstance(func, warp.context.Function): attr = path[-1] caller = func func = None @@ -2610,6 +2673,190 @@ def resolve_path(adj, path): return expr + # retrieves a dictionary of all closure and global variables and their values + # to be used in the evaluation context of wp.static() expressions + def get_static_evaluation_context(adj): + closure_vars = dict( + zip( + adj.func.__code__.co_freevars, + [c.cell_contents for c in (adj.func.__closure__ or [])], + ) + ) + + vars_dict = {} + vars_dict.update(adj.func.__globals__) + # variables captured in closure have precedence over global vars + vars_dict.update(closure_vars) + + return vars_dict + + def is_static_expression(adj, func): + return ( + isinstance(func, types.FunctionType) + and func.__module__ == "warp.builtins" + and func.__qualname__ == "static" + ) + + # verify the return type of a wp.static() expression is supported inside a Warp kernel + def verify_static_return_value(adj, value): + if value is None: + raise ValueError("None is returned") + if warp.types.is_value(value): + return True + if warp.types.is_array(value): + # more useful explanation for the common case of creating a Warp array + raise ValueError("a Warp array cannot be created inside Warp kernels") + if isinstance(value, str): + # we want to support cases such as `print(wp.static("test"))` + return True + if isinstance(value, warp.context.Function): + return True + + def verify_struct(s: StructInstance, attr_path: List[str]): + for key in s._cls.vars.keys(): + v = getattr(s, key) + if issubclass(type(v), StructInstance): + verify_struct(v, attr_path + [key]) + else: + try: + adj.verify_static_return_value(v) + except ValueError as e: + raise ValueError( + f"the returned Warp struct contains a data type that cannot be constructed inside Warp kernels: {e} at {value._cls.key}.{'.'.join(attr_path)}" + ) from e + + if issubclass(type(value), StructInstance): + return verify_struct(value, []) + + raise ValueError(f"value of type {type(value)} cannot be constructed inside Warp kernels") + + # find the source code string of an AST node + def extract_node_source(adj, node) -> Optional[str]: + if not hasattr(node, "lineno") or not hasattr(node, "col_offset"): + return None + + start_line = node.lineno - 1 # line numbers start at 1 + start_col = node.col_offset + + if hasattr(node, "end_lineno") and hasattr(node, "end_col_offset"): + end_line = node.end_lineno - 1 + end_col = node.end_col_offset + else: + # fallback for Python versions before 3.8 + # we have to find the end line and column manually + end_line = start_line + end_col = start_col + parenthesis_count = 1 + for lineno in range(start_line, len(adj.source_lines)): + if lineno == start_line: + c_start = start_col + else: + c_start = 0 + line = adj.source_lines[lineno] + for i in range(c_start, len(line)): + c = line[i] + if c == "(": + parenthesis_count += 1 + elif c == ")": + parenthesis_count -= 1 + if parenthesis_count == 0: + end_col = i + end_line = lineno + break + if parenthesis_count == 0: + break + + if start_line == end_line: + # single-line expression + return adj.source_lines[start_line][start_col:end_col] + else: + # multi-line expression + lines = [] + # first line (from start_col to the end) + lines.append(adj.source_lines[start_line][start_col:]) + # middle lines (entire lines) + lines.extend(adj.source_lines[start_line + 1 : end_line]) + # last line (from the start to end_col) + lines.append(adj.source_lines[end_line][:end_col]) + return "\n".join(lines).strip() + + # handles a wp.static() expression and returns the resulting object and a string representing the code + # of the static expression + def evaluate_static_expression(adj, node) -> Tuple[Any, str]: + if len(node.args) == 1: + static_code = adj.extract_node_source(node.args[0]) + elif len(node.keywords) == 1: + static_code = adj.extract_node_source(node.keywords[0]) + else: + raise WarpCodegenError("warp.static() requires a single argument or keyword") + if static_code is None: + raise WarpCodegenError("Error extracting source code from wp.static() expression") + + vars_dict = adj.get_static_evaluation_context() + # add constant variables to the static call context + constant_vars = {k: v.constant for k, v in adj.symbols.items() if isinstance(v, Var) and v.constant is not None} + vars_dict.update(constant_vars) + + try: + value = eval(static_code, vars_dict) + if warp.config.verbose: + print(f"Evaluated static command: {static_code} = {value}") + except NameError as e: + raise WarpCodegenError( + f"Error evaluating static expression: {e}. Make sure all variables used in the static expression are constant." + ) from e + except Exception as e: + raise WarpCodegenError( + f"Error evaluating static expression: {e} while evaluating the following code generated from the static expression:\n{static_code}" + ) from e + + try: + adj.verify_static_return_value(value) + except ValueError as e: + raise WarpCodegenError( + f"Static expression returns an unsupported value: {e} while evaluating the following code generated from the static expression:\n{static_code}" + ) from e + + return value, static_code + + # try to replace wp.static() expressions by their evaluated value if the + # expression can be evaluated + def replace_static_expressions(adj): + class StaticExpressionReplacer(ast.NodeTransformer): + def visit_Call(self, node): + func, _ = adj.resolve_static_expression(node.func, eval_types=False) + if adj.is_static_expression(func): + try: + # the static expression will execute as long as the static expression is valid and + # only depends on global or captured variables + obj, code = adj.evaluate_static_expression(node) + if code is not None: + adj.static_expressions[code] = obj + if isinstance(obj, warp.context.Function): + name_node = ast.Name("__warp_func__") + # we add a pointer to the Warp function here so that we can refer to it later at + # codegen time (note that the function key itself is not sufficient to uniquely + # identify the function, as the function may be redefined between the current time + # of wp.static() declaration and the time of codegen during module building) + name_node.warp_func = obj + return ast.copy_location(name_node, node) + else: + return ast.copy_location(ast.Constant(value=obj), node) + except Exception: + # Ignoring failing static expressions should generally not be an issue because only + # one of these cases should be possible: + # 1) the static expression itself is invalid code, in which case the module cannot be + # built all, + # 2) the static expression contains a reference to a local (even if constant) variable + # (and is therefore not executable and raises this exception), in which + # case changing the constant, or the code affecting this constant, would lead to + # a different module hash anyway. + pass + + return self.generic_visit(node) + + adj.tree = StaticExpressionReplacer().visit(adj.tree) + # Evaluates a static expression that does not depend on runtime values # if eval_types is True, try resolving the path using evaluated type information as well def resolve_static_expression(adj, root_node, eval_types=True): @@ -2684,7 +2931,7 @@ def get_node_source(adj, node): # return the Python code corresponding to the given AST node return ast.get_source_segment(adj.source, node) - def get_references(adj) -> Dict[str, Any]: + def get_references(adj) -> Tuple[Dict[str, Any], Dict[Any, Any], Dict[warp.context.Function, Any]]: """Traverses ``adj.tree`` and returns referenced constants, types, and user-defined functions.""" local_variables = set() # Track local variables appearing on the LHS so we know when variables are shadowed @@ -2976,6 +3223,15 @@ def scalar_value(x): # make sure we emit the value of objects, e.g. uint32 return str(value.value) + elif issubclass(value_type, warp.codegen.StructInstance): + # constant struct instance + arg_strs = [] + for key, var in value._cls.vars.items(): + attr = getattr(value, key) + arg_strs.append(f"{Var.type_to_ctype(var.type)}({constant_str(attr)})") + arg_str = ", ".join(arg_strs) + return f"{value.native_name}({arg_str})" + elif value == math.inf: return "INFINITY" diff --git a/warp/config.py b/warp/config.py index e732f71b..49df51ea 100644 --- a/warp/config.py +++ b/warp/config.py @@ -7,7 +7,7 @@ from typing import Optional -version: str = "1.3.3" +version: str = "1.4.0" """Warp version string""" verify_fp: bool = False diff --git a/warp/context.py b/warp/context.py index a66577fd..281a6009 100644 --- a/warp/context.py +++ b/warp/context.py @@ -1450,9 +1450,9 @@ def hash_function(self, func): # custom bits if ovl.custom_grad_func: - ch.update(bytes(ovl.custom_grad_func.adj.source, "utf-8")) + ch.update(self.hash_adjoint(ovl.custom_grad_func.adj)) if ovl.custom_replay_func: - ch.update(bytes(ovl.custom_replay_func.adj.source, "utf-8")) + ch.update(self.hash_adjoint(ovl.custom_replay_func.adj)) if ovl.replay_snippet: ch.update(bytes(ovl.replay_snippet, "utf-8")) if ovl.native_snippet: @@ -1516,6 +1516,10 @@ def hash_adjoint(self, adj): else: raise RuntimeError(f"Invalid constant type: {type(value)}") + # hash wp.static() expressions that were evaluated at declaration time + for k, v in adj.static_expressions.items(): + ch.update(bytes(f"{k} = {v}", "utf-8")) + # hash referenced types for t in types.keys(): ch.update(bytes(warp.types.get_type_code(t), "utf-8")) @@ -1541,8 +1545,8 @@ def __init__(self, module, options, hasher=None): self.options = options self.module = module self.deferred_functions = [] - self.ltoirs = {} # map from lto symbol to lto binary - self.ltoirs_decl = {} # map from lto symbol to lto forward declaration + self.ltoirs = {} # map from lto symbol to lto binary + self.ltoirs_decl = {} # map from lto symbol to lto forward declaration if hasher is None: hasher = ModuleHasher(module) @@ -1617,7 +1621,7 @@ def codegen(self, device): source += 'extern "C" {\n' for fwd in self.ltoirs_decl.values(): source += fwd + "\n" - source += '}\n' + source += "}\n" # code-gen structs visited_structs = set() diff --git a/warp/dlpack.py b/warp/dlpack.py index 34de4264..20860c6e 100644 --- a/warp/dlpack.py +++ b/warp/dlpack.py @@ -124,6 +124,8 @@ def device_to_dlpack(wp_device) -> DLDevice: def dtype_to_dlpack(wp_dtype) -> DLDataType: + if wp_dtype == warp.bool: + return (DLDataTypeCode.kDLBool, 8, 1) if wp_dtype == warp.int8: return (DLDataTypeCode.kDLInt, 8, 1) elif wp_dtype == warp.uint8: diff --git a/warp/examples/benchmarks/benchmark.bat b/warp/examples/benchmarks/benchmark.bat index 9edec17d..66a5dab3 100644 --- a/warp/examples/benchmarks/benchmark.bat +++ b/warp/examples/benchmarks/benchmark.bat @@ -11,3 +11,5 @@ python benchmark_cloth.py numpy @REM python benchmark_cloth.py numba @REM python benchmark_cloth.py jax_cpu @REM python benchmark_cloth.py jax_gpu +@REM python benchmark_cloth.py paddle_cpu +@REM python benchmark_cloth.py paddle_gpu diff --git a/warp/examples/benchmarks/benchmark.sh b/warp/examples/benchmarks/benchmark.sh index f82289a6..a4d54386 100755 --- a/warp/examples/benchmarks/benchmark.sh +++ b/warp/examples/benchmarks/benchmark.sh @@ -11,3 +11,5 @@ python3 benchmark_cloth.py numpy # python3 benchmark_cloth.py jax_cpu # python3 benchmark_cloth.py jax_gpu # python3 benchmark_cloth.py numba +# python3 benchmark_cloth.py paddle_cpu +# python3 benchmark_cloth.py paddle_gpu diff --git a/warp/examples/benchmarks/benchmark_cloth.py b/warp/examples/benchmarks/benchmark_cloth.py index d28213da..3fc6a740 100644 --- a/warp/examples/benchmarks/benchmark_cloth.py +++ b/warp/examples/benchmarks/benchmark_cloth.py @@ -219,6 +219,16 @@ def run_benchmark(mode, dim, timers, render=False): integrator = benchmark_cloth_jax.JxIntegrator(cloth) + elif mode == "paddle_cpu": + import benchmark_cloth_paddle + + integrator = benchmark_cloth_paddle.TrIntegrator(cloth, "cpu") + + elif mode == "paddle_gpu": + import benchmark_cloth_paddle + + integrator = benchmark_cloth_paddle.TrIntegrator(cloth, "gpu") + else: raise RuntimeError("Unknown simulation backend") diff --git a/warp/examples/sim/example_cloth.py b/warp/examples/sim/example_cloth.py index 13ea6860..1a93763d 100644 --- a/warp/examples/sim/example_cloth.py +++ b/warp/examples/sim/example_cloth.py @@ -26,9 +26,33 @@ import warp.sim.render +def color_lattice_grid(num_x, num_y): + colors = [] + for _i in range(4): + colors.append([]) + + for xi in range(num_x + 1): + for yi in range(num_y + 1): + vId = xi * (num_y + 1) + yi + + a = 1 if xi % 2 else 0 + b = 1 if yi % 2 else 0 + + c = a * 2 + b + + colors[c].append(vId) + + colors_wp = [] + for i_color in range(len(colors)): + colors_wp.append(wp.array(colors[i_color], dtype=wp.int32)) + + return colors_wp + + class IntegratorType(Enum): EULER = "euler" XPBD = "xpbd" + VBD = "vbd" def __str__(self): return self.value @@ -67,7 +91,7 @@ def __init__( tri_ka=1.0e3, tri_kd=1.0e1, ) - else: + elif self.integrator_type == IntegratorType.XPBD: builder.add_cloth_grid( pos=wp.vec3(0.0, 4.0, 0.0), rot=wp.quat_from_axis_angle(wp.vec3(1.0, 0.0, 0.0), math.pi * 0.5), @@ -83,6 +107,22 @@ def __init__( spring_ke=1.0e3, spring_kd=0.0, ) + else: + # VBD + builder.add_cloth_grid( + pos=wp.vec3(0.0, 4.0, 0.0), + rot=wp.quat_from_axis_angle(wp.vec3(1.0, 0.0, 0.0), math.pi * 0.5), + vel=wp.vec3(0.0, 0.0, 0.0), + dim_x=self.sim_width, + dim_y=self.sim_height, + cell_x=0.1, + cell_y=0.1, + mass=0.1, + fix_left=True, + tri_ke=1e4, + tri_ka=1e4, + tri_kd=1e-5, + ) usd_stage = Usd.Stage.Open(os.path.join(warp.examples.get_asset_directory(), "bunny.usd")) usd_geom = UsdGeom.Mesh(usd_stage.GetPrimAtPath("/root/bunny")) @@ -103,16 +143,20 @@ def __init__( kf=1.0e1, ) - if self.integrator_type == IntegratorType.EULER: - self.integrator = wp.sim.SemiImplicitIntegrator() - else: - self.integrator = wp.sim.XPBDIntegrator(iterations=1) - self.model = builder.finalize() self.model.ground = True self.model.soft_contact_ke = 1.0e4 self.model.soft_contact_kd = 1.0e2 + if self.integrator_type == IntegratorType.EULER: + self.integrator = wp.sim.SemiImplicitIntegrator() + elif self.integrator_type == IntegratorType.XPBD: + self.integrator = wp.sim.XPBDIntegrator(iterations=1) + else: + self.integrator = wp.sim.VBDIntegrator(self.model, iterations=1) + # we need to give VBD coloring information + self.model.particle_coloring = color_lattice_grid(width, height) + self.state_0 = self.model.state() self.state_1 = self.model.state() diff --git a/warp/native/mat.h b/warp/native/mat.h index 0327a07a..ee084d60 100644 --- a/warp/native/mat.h +++ b/warp/native/mat.h @@ -210,6 +210,12 @@ inline CUDA_CALLABLE mat_t identity() return m; } +template +inline CUDA_CALLABLE void adj_identity(const mat_t& adj_ret) +{ + // nop +} + template inline CUDA_CALLABLE bool operator==(const mat_t& a, const mat_t& b) { diff --git a/warp/native/quat.h b/warp/native/quat.h index 3896c029..90f9c556 100644 --- a/warp/native/quat.h +++ b/warp/native/quat.h @@ -29,6 +29,14 @@ struct quat_t w = static_cast(other.w); } + inline CUDA_CALLABLE quat_t(const initializer_array<4, Type> &l) + { + x = l[0]; + y = l[1]; + z = l[2]; + w = l[3]; + } + // imaginary part Type x; Type y; diff --git a/warp/native/spatial.h b/warp/native/spatial.h index 6e0d27da..48261536 100644 --- a/warp/native/spatial.h +++ b/warp/native/spatial.h @@ -127,6 +127,12 @@ struct transform_t CUDA_CALLABLE inline transform_t(vec_t<3,Type> p=vec_t<3,Type>(), quat_t q=quat_t()) : p(p), q(q) {} CUDA_CALLABLE inline transform_t(Type) {} // helps uniform initialization + CUDA_CALLABLE inline transform_t(const initializer_array<7, Type> &l) + { + p = vec_t<3,Type>(l[0], l[1], l[2]); + q = quat_t(l[3], l[4], l[5], l[6]); + } + CUDA_CALLABLE inline Type operator[](int index) const { assert(index < 7); diff --git a/warp/paddle.py b/warp/paddle.py new file mode 100644 index 00000000..65dcf17f --- /dev/null +++ b/warp/paddle.py @@ -0,0 +1,382 @@ +# Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +from __future__ import annotations + +import ctypes +from typing import TYPE_CHECKING, Optional, Union + +import numpy + +import warp +import warp.context + +if TYPE_CHECKING: + import paddle + + +# return the warp device corresponding to a paddle device +def device_from_paddle(paddle_device: Union[paddle.base.libpaddle.Place, str]) -> warp.context.Device: + """Return the Warp device corresponding to a Paddle device. + + Args: + paddle_device (`paddle.base.libpaddle.Place` or `str`): Paddle device identifier + + Raises: + RuntimeError: Paddle device does not have a corresponding Warp device + """ + if type(paddle_device) is str: + warp_device = warp.context.runtime.device_map.get(paddle_device) + if warp_device is not None: + return warp_device + elif paddle_device.startswith("gpu"): + return warp.context.runtime.get_current_cuda_device() + else: + raise RuntimeError(f"Unsupported Paddle device {paddle_device}") + else: + import paddle + + try: + if paddle_device.is_gpu_place(): + return warp.context.runtime.cuda_devices[paddle_device.gpu_device_id()] + elif paddle_device.is_cpu_place(): + return warp.context.runtime.cpu_device + else: + raise RuntimeError(f"Unsupported Paddle device type {paddle_device}") + except Exception as e: + import paddle + + if not isinstance(paddle_device, paddle.base.libpaddle.Place): + raise ValueError("Argument must be a paddle.base.libpaddle.Place object or a string") from e + raise + + +def device_to_paddle(warp_device: warp.context.Devicelike) -> str: + """Return the Paddle device string corresponding to a Warp device. + + Args: + warp_device: An identifier that can be resolved to a :class:`warp.context.Device`. + + Raises: + RuntimeError: The Warp device is not compatible with PyPaddle. + """ + device = warp.get_device(warp_device) + if device.is_cpu or device.is_primary: + return str(device).replace("cuda", "gpu") + elif device.is_cuda and device.is_uva: + # it's not a primary context, but paddle can access the data ptr directly thanks to UVA + return f"gpu:{device.ordinal}" + raise RuntimeError(f"Warp device {device} is not compatible with paddle") + + +def dtype_to_paddle(warp_dtype): + """Return the Paddle dtype corresponding to a Warp dtype. + + Args: + warp_dtype: A Warp data type that has a corresponding ``paddle.dtype``. + ``warp.uint16``, ``warp.uint32``, and ``warp.uint64`` are mapped + to the signed integer ``paddle.dtype`` of the same width. + Raises: + TypeError: Unable to find a corresponding PyPaddle data type. + """ + # initialize lookup table on first call to defer paddle import + if dtype_to_paddle.type_map is None: + import paddle + + dtype_to_paddle.type_map = { + warp.float16: paddle.float16, + warp.float32: paddle.float32, + warp.float64: paddle.float64, + warp.int8: paddle.int8, + warp.int16: paddle.int16, + warp.int32: paddle.int32, + warp.int64: paddle.int64, + warp.uint8: paddle.uint8, + warp.bool: paddle.bool, + # paddle doesn't support unsigned ints bigger than 8 bits + warp.uint16: paddle.int16, + warp.uint32: paddle.int32, + warp.uint64: paddle.int64, + } + + paddle_dtype = dtype_to_paddle.type_map.get(warp_dtype) + if paddle_dtype is not None: + return paddle_dtype + else: + raise TypeError(f"Cannot convert {warp_dtype} to a Paddle type") + + +def dtype_from_paddle(paddle_dtype): + """Return the Warp dtype corresponding to a Paddle dtype. + + Args: + paddle_dtype: A ``paddle.dtype`` that has a corresponding Warp data type. + Currently ``paddle.bfloat16``, ``paddle.complex64``, and + ``paddle.complex128`` are not supported. + + Raises: + TypeError: Unable to find a corresponding Warp data type. + """ + # initialize lookup table on first call to defer paddle import + if dtype_from_paddle.type_map is None: + import paddle + + dtype_from_paddle.type_map = { + paddle.float16: warp.float16, + paddle.float32: warp.float32, + paddle.float64: warp.float64, + paddle.int8: warp.int8, + paddle.int16: warp.int16, + paddle.int32: warp.int32, + paddle.int64: warp.int64, + paddle.uint8: warp.uint8, + paddle.bool: warp.bool, + # currently unsupported by Warp + # paddle.bfloat16: + # paddle.complex64: + # paddle.complex128: + } + + warp_dtype = dtype_from_paddle.type_map.get(paddle_dtype) + + if warp_dtype is not None: + return warp_dtype + else: + raise TypeError(f"Cannot convert {paddle_dtype} to a Warp type") + + +def dtype_is_compatible(paddle_dtype: paddle.dtype, warp_dtype) -> bool: + """Evaluates whether the given paddle dtype is compatible with the given Warp dtype.""" + # initialize lookup table on first call to defer paddle import + if dtype_is_compatible.compatible_sets is None: + import paddle + + dtype_is_compatible.compatible_sets = { + paddle.float64: {warp.float64}, + paddle.float32: {warp.float32}, + paddle.float16: {warp.float16}, + # allow aliasing integer tensors as signed or unsigned integer arrays + paddle.int64: {warp.int64, warp.uint64}, + paddle.int32: {warp.int32, warp.uint32}, + paddle.int16: {warp.int16, warp.uint16}, + paddle.int8: {warp.int8, warp.uint8}, + paddle.uint8: {warp.uint8, warp.int8}, + paddle.bool: {warp.bool, warp.uint8, warp.int8}, + # currently unsupported by Warp + # paddle.bfloat16: + # paddle.complex64: + # paddle.complex128: + } + + compatible_set = dtype_is_compatible.compatible_sets.get(paddle_dtype) + + if compatible_set is not None: + if warp_dtype in compatible_set: + return True + # check if it's a vector or matrix type + if hasattr(warp_dtype, "_wp_scalar_type_"): + return warp_dtype._wp_scalar_type_ in compatible_set + + return False + + +# lookup tables initialized when needed +dtype_from_paddle.type_map = None +dtype_to_paddle.type_map = None +dtype_is_compatible.compatible_sets = None + + +# wrap a paddle tensor to a wp array, data is not copied +def from_paddle( + t: paddle.Tensor, + dtype: Optional[paddle.dtype] = None, + requires_grad: Optional[bool] = None, + grad: Optional[paddle.Tensor] = None, + return_ctype: bool = False, +) -> warp.array: + """Convert a Paddle tensor to a Warp array without copying the data. + + Args: + t (paddle.Tensor): The paddle tensor to wrap. + dtype (warp.dtype, optional): The target data type of the resulting Warp array. Defaults to the tensor value type mapped to a Warp array value type. + requires_grad (bool, optional): Whether the resulting array should wrap the tensor's gradient, if it exists (the grad tensor will be allocated otherwise). Defaults to the tensor's `requires_grad` value. + grad (paddle.Tensor, optional): The grad attached to given tensor. Defaults to None. + return_ctype (bool, optional): Whether to return a low-level array descriptor instead of a ``wp.array`` object (faster). The descriptor can be passed to Warp kernels. + + Returns: + warp.array: The wrapped array or array descriptor. + """ + if dtype is None: + dtype = dtype_from_paddle(t.dtype) + elif not dtype_is_compatible(t.dtype, dtype): + raise RuntimeError(f"Cannot convert Paddle type {t.dtype} to Warp type {dtype}") + + # get size of underlying data type to compute strides + ctype_size = ctypes.sizeof(dtype._type_) + + shape = tuple(t.shape) + strides = tuple(s * ctype_size for s in t.strides) + + # if target is a vector or matrix type + # then check if trailing dimensions match + # the target type and update the shape + if hasattr(dtype, "_shape_"): + dtype_shape = dtype._shape_ + dtype_dims = len(dtype._shape_) + # ensure inner shape matches + if dtype_dims > len(shape) or dtype_shape != shape[-dtype_dims:]: + raise RuntimeError( + f"Could not convert Paddle tensor with shape {shape} to Warp array with dtype={dtype}, ensure that source inner shape is {dtype_shape}" + ) + # ensure inner strides are contiguous + if strides[-1] != ctype_size or (dtype_dims > 1 and strides[-2] != ctype_size * dtype_shape[-1]): + raise RuntimeError( + f"Could not convert Paddle tensor with shape {shape} to Warp array with dtype={dtype}, because the source inner strides are not contiguous" + ) + # trim shape and strides + shape = tuple(shape[:-dtype_dims]) or (1,) + strides = tuple(strides[:-dtype_dims]) or (ctype_size,) + + # gradient + # - if return_ctype is False, we set `grad` to a wp.array or None + # - if return_ctype is True, we set `grad_ptr` and set `grad` as the owner (wp.array or paddle.Tensor) + requires_grad = (not t.stop_gradient) if requires_grad is None else requires_grad + grad_ptr = 0 + if grad is not None: + if isinstance(grad, warp.array): + if return_ctype: + if grad.strides != strides: + raise RuntimeError( + f"Gradient strides must match array strides, expected {strides} but got {grad.strides}" + ) + grad_ptr = grad.ptr + else: + # assume grad is a paddle.Tensor + if return_ctype: + if t.strides != grad.strides: + raise RuntimeError( + f"Gradient strides must match array strides, expected {t.strides} but got {grad.strides}" + ) + grad_ptr = grad.data_ptr() + else: + grad = from_paddle(grad, dtype=dtype, requires_grad=False) + elif requires_grad: + # wrap the tensor gradient, allocate if necessary + if t.grad is not None: + if return_ctype: + grad = t.grad + if t.strides != grad.strides: + raise RuntimeError( + f"Gradient strides must match array strides, expected {t.strides} but got {grad.strides}" + ) + grad_ptr = grad.data_ptr() + else: + grad = from_paddle(t.grad, dtype=dtype, requires_grad=False) + else: + # allocate a zero-filled gradient if it doesn't exist + # Note: we use Warp to allocate the shared gradient with compatible strides + grad = warp.zeros(dtype=dtype, shape=shape, strides=strides, device=device_from_paddle(t.place)) + # use .grad_ for zero-copy + t.grad_ = to_paddle(grad, requires_grad=False) + grad_ptr = grad.ptr + + if return_ctype: + ptr = t.data_ptr() + + # create array descriptor + array_ctype = warp.types.array_t(ptr, grad_ptr, len(shape), shape, strides) + + # keep data and gradient alive + array_ctype._ref = t + array_ctype._gradref = grad + + return array_ctype + + else: + a = warp.array( + ptr=t.data_ptr(), + dtype=dtype, + shape=shape, + strides=strides, + device=device_from_paddle(t.place), + copy=False, + grad=grad, + requires_grad=requires_grad, + ) + + # save a reference to the source tensor, otherwise it may get deallocated + a._tensor = t + + return a + + +def to_paddle(a: warp.array, requires_grad: bool = None) -> paddle.Tensor: + """ + Convert a Warp array to a Paddle tensor without copying the data. + + Args: + a (warp.array): The Warp array to convert. + requires_grad (bool, optional): Whether the resulting tensor should convert the array's gradient, if it exists, to a grad tensor. Defaults to the array's `requires_grad` value. + + Returns: + paddle.Tensor: The converted tensor. + """ + import paddle + import paddle.utils.dlpack + + if requires_grad is None: + requires_grad = a.requires_grad + + # Paddle does not support structured arrays + if isinstance(a.dtype, warp.codegen.Struct): + raise RuntimeError("Cannot convert structured Warp arrays to Paddle.") + + if a.device.is_cpu: + # Paddle has an issue wrapping CPU objects + # that support the __array_interface__ protocol + # in this case we need to workaround by going + # to an ndarray first, see https://pearu.github.io/array_interface_pypaddle.html + t = paddle.to_tensor(numpy.asarray(a), place="cpu") + t.stop_gradient = not requires_grad + if requires_grad and a.requires_grad: + # use .grad_ for zero-copy + t.grad_ = paddle.to_tensor(numpy.asarray(a.grad), place="cpu") + return t + + elif a.device.is_cuda: + # Paddle does support the __cuda_array_interface__ + # correctly, but we must be sure to maintain a reference + # to the owning object to prevent memory allocs going out of scope + t = paddle.utils.dlpack.from_dlpack(warp.to_dlpack(a)).to(device=device_to_paddle(a.device)) + t.stop_gradient = not requires_grad + if requires_grad and a.requires_grad: + # use .grad_ for zero-copy + t.grad_ = paddle.utils.dlpack.from_dlpack(warp.to_dlpack(a.grad)).to(device=device_to_paddle(a.device)) + return t + + else: + raise RuntimeError("Unsupported device") + + +def stream_from_paddle(stream_or_device=None): + """Convert from a Paddle CUDA stream to a Warp CUDA stream.""" + import paddle + + if isinstance(stream_or_device, paddle.device.Stream): + stream = stream_or_device + else: + # assume arg is a paddle device + stream = paddle.device.current_stream(stream_or_device) + + device = device_from_paddle(stream.device) + + warp_stream = warp.Stream(device, cuda_stream=stream.stream_base.cuda_stream) + + # save a reference to the source stream, otherwise it may be destroyed + warp_stream._paddle_stream = stream + + return warp_stream diff --git a/warp/sim/integrator_euler.py b/warp/sim/integrator_euler.py index 40db2813..624e644a 100644 --- a/warp/sim/integrator_euler.py +++ b/warp/sim/integrator_euler.py @@ -761,6 +761,7 @@ def eval_particle_contacts( contact_body_vel: wp.array(dtype=wp.vec3), contact_normal: wp.array(dtype=wp.vec3), contact_max: int, + body_f_in_world_frame: bool, # outputs particle_f: wp.array(dtype=wp.vec3), body_f: wp.array(dtype=wp.spatial_vector), @@ -809,7 +810,11 @@ def eval_particle_contacts( body_v = wp.spatial_bottom(body_v_s) # compute the body velocity at the particle position - bv = body_v + wp.cross(body_w, r) + wp.transform_vector(X_wb, contact_body_vel[tid]) + bv = body_v + wp.transform_vector(X_wb, contact_body_vel[tid]) + if body_f_in_world_frame: + bv += wp.cross(body_w, bx) + else: + bv += wp.cross(body_w, r) # relative velocity v = pv - bv @@ -840,12 +845,14 @@ def eval_particle_contacts( ft = wp.normalize(vt) * wp.min(kf * wp.length(vt), abs(mu * c * ke)) f_total = fn + (fd + ft) - t_total = wp.cross(r, f_total) wp.atomic_sub(particle_f, particle_index, f_total) if body_index >= 0: - wp.atomic_add(body_f, body_index, wp.spatial_vector(t_total, f_total)) + if body_f_in_world_frame: + wp.atomic_sub(body_f, body_index, wp.spatial_vector(wp.cross(bx, f_total), f_total)) + else: + wp.atomic_add(body_f, body_index, wp.spatial_vector(wp.cross(r, f_total), f_total)) @wp.kernel @@ -1814,7 +1821,9 @@ def eval_body_joint_forces(model: Model, state: State, control: Control, body_f: ) -def eval_particle_body_contact_forces(model: Model, state: State, particle_f: wp.array, body_f: wp.array): +def eval_particle_body_contact_forces( + model: Model, state: State, particle_f: wp.array, body_f: wp.array, body_f_in_world_frame: bool = False +): if model.particle_count and model.shape_count > 1: wp.launch( kernel=eval_particle_contacts, @@ -1841,6 +1850,7 @@ def eval_particle_body_contact_forces(model: Model, state: State, particle_f: wp model.soft_contact_body_vel, model.soft_contact_normal, model.soft_contact_max, + body_f_in_world_frame, ], # outputs outputs=[particle_f, body_f], @@ -1897,7 +1907,7 @@ def compute_forces(model: Model, state: State, control: Control, particle_f: wp. eval_body_contact_forces(model, state, particle_f) # particle shape contact - eval_particle_body_contact_forces(model, state, particle_f, body_f) + eval_particle_body_contact_forces(model, state, particle_f, body_f, body_f_in_world_frame=False) # muscles if False: diff --git a/warp/sim/integrator_featherstone.py b/warp/sim/integrator_featherstone.py index 2bc3e61f..6b240870 100644 --- a/warp/sim/integrator_featherstone.py +++ b/warp/sim/integrator_featherstone.py @@ -592,7 +592,7 @@ def jcalc_integrate( p_s = wp.vec3(joint_q[coord_start + 0], joint_q[coord_start + 1], joint_q[coord_start + 2]) # linear vel of origin (note q/qd switch order of linear angular elements) - # note we are converting the body twist in the space frame (w_s, v_s) to compute center of mass velcity + # note we are converting the body twist in the space frame (w_s, v_s) to compute center of mass velocity dpdt_s = v_s + wp.cross(w_s, p_s) # quat and quat derivative @@ -1621,7 +1621,7 @@ def simulate(self, model: Model, state_in: State, state_out: State, dt: float, c eval_particle_ground_contact_forces(model, state_in, particle_f) # particle shape contact - eval_particle_body_contact_forces(model, state_in, particle_f, body_f) + eval_particle_body_contact_forces(model, state_in, particle_f, body_f, body_f_in_world_frame=True) # muscles if False: diff --git a/warp/sim/integrator_vbd.py b/warp/sim/integrator_vbd.py index 23ea7631..f6ced884 100644 --- a/warp/sim/integrator_vbd.py +++ b/warp/sim/integrator_vbd.py @@ -10,7 +10,7 @@ from ..types import float32, matrix from .integrator import Integrator -from .model import PARTICLE_FLAG_ACTIVE, Control, Model, State +from .model import PARTICLE_FLAG_ACTIVE, Control, Model, ModelShapeMaterials, State class mat66(matrix(shape=(6, 6), dtype=float32)): @@ -110,6 +110,39 @@ def _test_compute_force_element_adjacency( ) +@wp.func +def build_orthonormal_basis(n: wp.vec3): + """ + Builds an orthonormal basis given a normal vector `n`. Return the two axes that is perpendicular to `n`. + + :param n: A 3D vector (list or array-like) representing the normal vector + """ + b1 = wp.vec3() + b2 = wp.vec3() + if n[2] < 0.0: + a = 1.0 / (1.0 - n[2]) + b = n[0] * n[1] * a + b1[0] = 1.0 - n[0] * n[0] * a + b1[1] = -b + b1[2] = n[0] + + b2[0] = b + b2[1] = n[1] * n[1] * a - 1.0 + b2[2] = -n[1] + else: + a = 1.0 / (1.0 + n[2]) + b = -n[0] * n[1] * a + b1[0] = 1.0 - n[0] * n[0] * a + b1[1] = b + b1[2] = -n[0] + + b2[0] = b + b2[1] = 1.0 - n[1] * n[1] * a + b2[2] = -n[1] + + return b1, b2 + + @wp.func def calculate_triangle_deformation_gradient( face: int, tri_indices: wp.array(dtype=wp.int32, ndim=2), pos: wp.array(dtype=wp.vec3), tri_pose: wp.mat22 @@ -288,6 +321,157 @@ def evaluate_stvk_force_hessian( return f, h +@wp.func +def evaluate_ground_contact_force_hessian( + vertex_pos: wp.vec3, + vertex_prev_pos: wp.vec3, + particle_radius: float, + ground_normal: wp.vec3, + ground_level: float, + soft_contact_ke: float, + friction_mu: float, + friction_epsilon: float, + dt: float, +): + penetration_depth = -(wp.dot(ground_normal, vertex_pos) + ground_level - particle_radius) + + if penetration_depth > 0: + ground_contact_force_norm = penetration_depth * soft_contact_ke + ground_contact_force = ground_normal * ground_contact_force_norm + ground_contact_hessian = soft_contact_ke * wp.outer(ground_normal, ground_normal) + + dx = vertex_pos - vertex_prev_pos + + # friction + e0, e1 = build_orthonormal_basis(ground_normal) + + T = mat32(e0[0], e1[0], e0[1], e1[1], e0[2], e1[2]) + + relative_translation = dx + u = wp.transpose(T) * relative_translation + eps_u = friction_epsilon * dt + + friction_force, friction_hessian = compute_friction(friction_mu, ground_contact_force_norm, T, u, eps_u) + ground_contact_force = ground_contact_force + friction_force + ground_contact_hessian = ground_contact_hessian + friction_hessian + else: + ground_contact_force = wp.vec3(0.0, 0.0, 0.0) + ground_contact_hessian = wp.mat33(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) + + return ground_contact_force, ground_contact_hessian + + +@wp.func +def evaluate_body_particle_contact( + particle_index: int, + particle_pos: wp.vec3, + particle_prev_pos: wp.vec3, + contact_index: int, + soft_contact_ke: float, + friction_mu: float, + friction_epsilon: float, + particle_radius: wp.array(dtype=float), + shape_materials: ModelShapeMaterials, + shape_body: wp.array(dtype=int), + body_q: wp.array(dtype=wp.transform), + body_qd: wp.array(dtype=wp.spatial_vector), + body_com: wp.array(dtype=wp.vec3), + contact_shape: wp.array(dtype=int), + contact_body_pos: wp.array(dtype=wp.vec3), + contact_body_vel: wp.array(dtype=wp.vec3), + contact_normal: wp.array(dtype=wp.vec3), + dt: float, +): + shape_index = contact_shape[contact_index] + body_index = shape_body[shape_index] + + X_wb = wp.transform_identity() + X_com = wp.vec3() + if body_index >= 0: + X_wb = body_q[body_index] + X_com = body_com[body_index] + + # body position in world space + bx = wp.transform_point(X_wb, contact_body_pos[contact_index]) + r = bx - wp.transform_point(X_wb, X_com) + + n = contact_normal[contact_index] + + penetration_depth = -(wp.dot(n, particle_pos - bx) - particle_radius[particle_index]) + if penetration_depth > 0: + body_contact_force_norm = penetration_depth * soft_contact_ke + body_contact_force = n * body_contact_force_norm + body_contact_hessian = soft_contact_ke * wp.outer(n, n) + + mu = 0.5 * (friction_mu + shape_materials.mu[shape_index]) + + dx = particle_pos - particle_prev_pos + + # body velocity + body_v_s = wp.spatial_vector() + if body_index >= 0: + body_v_s = body_qd[body_index] + + body_w = wp.spatial_top(body_v_s) + body_v = wp.spatial_bottom(body_v_s) + + # compute the body velocity at the particle position + bv = body_v + wp.cross(body_w, r) + wp.transform_vector(X_wb, contact_body_vel[contact_index]) + + relative_translation = dx - bv * dt + + # friction + e0, e1 = build_orthonormal_basis(n) + + T = mat32(e0[0], e1[0], e0[1], e1[1], e0[2], e1[2]) + + u = wp.transpose(T) * relative_translation + eps_u = friction_epsilon * dt + + friction_force, friction_hessian = compute_friction(mu, body_contact_force_norm, T, u, eps_u) + body_contact_force = body_contact_force + friction_force + body_contact_hessian = body_contact_hessian + friction_hessian + else: + body_contact_force = wp.vec3(0.0, 0.0, 0.0) + body_contact_hessian = wp.mat33(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) + + return body_contact_force, body_contact_hessian + + +@wp.func +def compute_friction(mu: float, normal_contact_force: float, T: mat32, u: wp.vec2, eps_u: float): + """ + Returns the friction force and hessian. + Args: + mu: Friction coefficient. + normal_contact_force: normal contact force. + T: Transformation matrix (3x2 matrix). + u: 2D displacement vector. + """ + # Friction + u_norm = wp.length(u) + + if u_norm > 0.0: + # IPC friction + if u_norm > eps_u: + # constant stage + f1_SF_over_x = 1.0 / u_norm + else: + # smooth transition + f1_SF_over_x = (-u_norm / eps_u + 2.0) / eps_u + + force = -mu * normal_contact_force * T * (f1_SF_over_x * u) + + # Different from IPC, we treat the contact normal as constant + # this significantly improves the stability + hessian = mu * normal_contact_force * T * (f1_SF_over_x * wp.identity(2, float)) * wp.transpose(T) + else: + force = wp.vec3(0.0, 0.0, 0.0) + hessian = wp.mat33(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) + + return force, hessian + + @wp.kernel def forward_step( dt: float, @@ -300,21 +484,21 @@ def forward_step( particle_flags: wp.array(dtype=wp.uint32), inertia: wp.array(dtype=wp.vec3), ): - vertex = wp.tid() + particle = wp.tid() - prev_pos[vertex] = pos[vertex] - if not particle_flags[vertex] & PARTICLE_FLAG_ACTIVE: - inertia[vertex] = prev_pos[vertex] + prev_pos[particle] = pos[particle] + if not particle_flags[particle] & PARTICLE_FLAG_ACTIVE: + inertia[particle] = prev_pos[particle] return - vel_new = vel[vertex] + (gravity + external_force[vertex] * inv_mass[vertex]) * dt - pos[vertex] = pos[vertex] + vel_new * dt - inertia[vertex] = pos[vertex] + vel_new = vel[particle] + (gravity + external_force[particle] * inv_mass[particle]) * dt + pos[particle] = pos[particle] + vel_new * dt + inertia[particle] = pos[particle] @wp.kernel def VBD_solve_trimesh( dt: float, - vertex_ids_in_color: wp.array(dtype=wp.int32), + particle_ids_in_color: wp.array(dtype=wp.int32), prev_pos: wp.array(dtype=wp.vec3), pos: wp.array(dtype=wp.vec3), pos_new: wp.array(dtype=wp.vec3), @@ -328,32 +512,57 @@ def VBD_solve_trimesh( tri_areas: wp.array(dtype=float), edge_indices: wp.array(dtype=wp.int32, ndim=2), adjacency: ForceElementAdjacencyInfo, + # contact info + # self contact + soft_contact_ke: float, + friction_mu: float, + friction_epsilon: float, + # body-particle contact + particle_radius: wp.array(dtype=float), + body_particle_contact_buffer_pre_alloc: int, + body_particle_contact_buffer: wp.array(dtype=int), + body_particle_contact_count: wp.array(dtype=int), + shape_materials: ModelShapeMaterials, + shape_body: wp.array(dtype=int), + body_q: wp.array(dtype=wp.transform), + body_qd: wp.array(dtype=wp.spatial_vector), + body_com: wp.array(dtype=wp.vec3), + contact_shape: wp.array(dtype=int), + contact_body_pos: wp.array(dtype=wp.vec3), + contact_body_vel: wp.array(dtype=wp.vec3), + contact_normal: wp.array(dtype=wp.vec3), + # ground-particle contact + has_ground: bool, + ground: wp.array(dtype=float), ): - t_id = wp.tid() + tid = wp.tid() - vertex = vertex_ids_in_color[t_id] - # wp.printf("vId: %d\n", vertex) + particle_index = particle_ids_in_color[tid] + # wp.printf("vId: %d\n", particle) - if not particle_flags[vertex] & PARTICLE_FLAG_ACTIVE: + if not particle_flags[particle_index] & PARTICLE_FLAG_ACTIVE: return - dtSqrReciprocal = 1.0 / (dt * dt) + particle_pos = pos[particle_index] + particle_prev_pos = pos[particle_index] + + dt_sqr_reciprocal = 1.0 / (dt * dt) # inertia force and hessian - f = mass[vertex] * (inertia[vertex] - pos[vertex]) * (dtSqrReciprocal) - h = mass[vertex] * dtSqrReciprocal * wp.identity(n=3, dtype=float) + f = mass[particle_index] * (inertia[particle_index] - pos[particle_index]) * (dt_sqr_reciprocal) + h = mass[particle_index] * dt_sqr_reciprocal * wp.identity(n=3, dtype=float) # elastic force and hessian - for i_adj_tri in range(get_vertex_num_adjacent_faces(vertex, adjacency)): - # wp.printf("vertex: %d | num_adj_faces: %d | ", vertex, get_vertex_num_adjacent_faces(vertex, adjacency)) - tri_id, vertex_order = get_vertex_adjacent_face_id_order(vertex, i_adj_tri, adjacency) + for i_adj_tri in range(get_vertex_num_adjacent_faces(particle_index, adjacency)): + # wp.printf("particle: %d | num_adj_faces: %d | ", particle, get_particle_num_adjacent_faces(particle, adjacency)) + tri_id, particle_order = get_vertex_adjacent_face_id_order(particle_index, i_adj_tri, adjacency) - # wp.printf("i_face: %d | face id: %d | v_order: %d | ", i_adj_tri, tri_id, vertex_order) + # wp.printf("i_face: %d | face id: %d | v_order: %d | ", i_adj_tri, tri_id, particle_order) # wp.printf("face: %d %d %d\n", tri_indices[tri_id, 0], tri_indices[tri_id, 1], tri_indices[tri_id, 2], ) f_tri, h_tri = evaluate_stvk_force_hessian( tri_id, - vertex_order, + particle_order, pos, tri_indices, tri_poses[tri_id], @@ -366,46 +575,152 @@ def VBD_solve_trimesh( k_d = tri_materials[tri_id, 2] h_d = h_tri * (k_d / dt) - f_d = h_d * (prev_pos[vertex] - pos[vertex]) + f_d = h_d * (prev_pos[particle_index] - pos[particle_index]) f = f + f_tri + f_d h = h + h_tri + h_d - # wp.printf("vertex: %d, i_adj_tri: %d, vertex_order: %d, \nforce:\n %f %f %f, \nhessian:, \n%f %f %f, \n%f %f %f, \n%f %f %f\n", - # vertex, i_adj_tri, vertex_order, + # wp.printf("particle: %d, i_adj_tri: %d, particle_order: %d, \nforce:\n %f %f %f, \nhessian:, \n%f %f %f, \n%f %f %f, \n%f %f %f\n", + # particle, i_adj_tri, particle_order, # f[0], f[1], f[2], # h[0, 0], h[0, 1], h[0, 2], # h[1, 0], h[1, 1], h[1, 2], # h[2, 0], h[2, 1], h[2, 2], # ) + # body-particle contact + particle_contact_count = min(body_particle_contact_count[particle_index], body_particle_contact_buffer_pre_alloc) + + offset = body_particle_contact_buffer_pre_alloc * particle_index + for contact_counter in range(particle_contact_count): + # the index to access body-particle data, which is size-variable and only contains active contact + contact_index = body_particle_contact_buffer[offset + contact_counter] + + body_contact_force, body_contact_hessian = evaluate_body_particle_contact( + particle_index, + particle_pos, + particle_prev_pos, + contact_index, + soft_contact_ke, + friction_mu, + friction_epsilon, + particle_radius, + shape_materials, + shape_body, + body_q, + body_qd, + body_com, + contact_shape, + contact_body_pos, + contact_body_vel, + contact_normal, + dt, + ) + + f = f + body_contact_force + h = h + body_contact_hessian + + if has_ground: + ground_normal = wp.vec3(ground[0], ground[1], ground[2]) + ground_level = ground[3] + ground_contact_force, ground_contact_hessian = evaluate_ground_contact_force_hessian( + particle_pos, + particle_prev_pos, + particle_radius[particle_index], + ground_normal, + ground_level, + soft_contact_ke, + friction_mu, + friction_epsilon, + dt, + ) + + f = f + ground_contact_force + h = h + ground_contact_hessian + if abs(wp.determinant(h)) > 1e-5: hInv = wp.inverse(h) - pos_new[vertex] = pos[vertex] + hInv * f + pos_new[particle_index] = particle_pos + hInv * f @wp.kernel def VBD_copy_particle_positions_back( - vertex_ids_in_color: wp.array(dtype=wp.int32), + particle_ids_in_color: wp.array(dtype=wp.int32), pos: wp.array(dtype=wp.vec3), pos_new: wp.array(dtype=wp.vec3), ): - t_id = wp.tid() - vertex = vertex_ids_in_color[t_id] + tid = wp.tid() + particle = particle_ids_in_color[tid] - pos[vertex] = pos_new[vertex] + pos[particle] = pos_new[particle] @wp.kernel def update_velocity( dt: float, prev_pos: wp.array(dtype=wp.vec3), pos: wp.array(dtype=wp.vec3), vel: wp.array(dtype=wp.vec3) ): - vertex = wp.tid() - vel[vertex] = (pos[vertex] - prev_pos[vertex]) / dt + particle = wp.tid() + vel[particle] = (pos[particle] - prev_pos[particle]) / dt + + +@wp.kernel +def convert_body_particle_contact_data_kernel( + # inputs + body_particle_contact_buffer_pre_alloc: int, + soft_contact_particle: wp.array(dtype=int), + contact_count: wp.array(dtype=int), + contact_max: int, + # outputs + body_particle_contact_buffer: wp.array(dtype=int), + body_particle_contact_count: wp.array(dtype=int), +): + contact_index = wp.tid() + count = min(contact_max, contact_count[0]) + if contact_index >= count: + return + + particle_index = soft_contact_particle[contact_index] + offset = particle_index * body_particle_contact_buffer_pre_alloc + + contact_counter = wp.atomic_add(body_particle_contact_count, particle_index, 1) + if contact_counter < body_particle_contact_buffer_pre_alloc: + body_particle_contact_buffer[offset + contact_counter] = contact_index class VBDIntegrator(Integrator): - def __init__(self, model: Model, iterations=10): + """An implicit integrator using Vertex Block Descent (VBD) for cloth simulation. + + References: + - Anka He Chen, Ziheng Liu, Yin Yang, and Cem Yuksel. 2024. Vertex Block Descent. ACM Trans. Graph. 43, 4, Article 116 (July 2024), 16 pages. https://doi.org/10.1145/3658179 + + Note that VBDIntegrator's constructor requires a :class:`Model` object as input, so that it can do some precomputation and preallocate the space. + After construction, you must provide the same :class:`Model` object that you used that was used during construction. + Currently, you must manually provide particle coloring and assign it to `model.particle_coloring` to make VBD work. + + VBDIntegrator.simulate accepts three arguments: class:`Model`, :class:`State`, and :class:`Control` (optional) objects, this time-integrator + may be used to advance the simulation state forward in time. + + Example + ------- + + .. code-block:: python + + model.particle_coloring = # load or generate particle coloring + integrator = wp.VBDIntegrator(model) + + # simulation loop + for i in range(100): + state = integrator.simulate(model, state_in, state_out, dt, control) + + """ + + def __init__( + self, + model: Model, + iterations=10, + body_particle_contact_buffer_pre_alloc=4, + friction_epsilon=1e-2, + ): self.device = model.device self.model = model self.iterations = iterations @@ -416,6 +731,15 @@ def __init__(self, model: Model, iterations=10): self.adjacency = self.compute_force_element_adjacency(model).to(self.device) + self.body_particle_contact_buffer_pre_alloc = body_particle_contact_buffer_pre_alloc + self.body_particle_contact_buffer = wp.zeros( + (self.body_particle_contact_buffer_pre_alloc * model.particle_count,), + dtype=wp.int32, + device=self.device, + ) + self.body_particle_contact_count = wp.zeros((model.particle_count,), dtype=wp.int32, device=self.device) + self.friction_epsilon = friction_epsilon + # tests # wp.launch(kernel=_test_compute_force_element_adjacency, # inputs=[self.adjacency, model.edge_indices, model.tri_indices], @@ -507,6 +831,8 @@ def simulate(self, model: Model, state_in: State, state_out: State, dt: float, c if model is not self.model: raise ValueError("model must be the one used to initialize VBDIntegrator") + self.convert_body_particle_contact_data() + wp.launch( kernel=forward_step, inputs=[ @@ -525,12 +851,12 @@ def simulate(self, model: Model, state_in: State, state_out: State, dt: float, c ) for _iter in range(self.iterations): - for i_color in range(len(self.model.coloring)): + for color_counter in range(len(self.model.particle_coloring)): wp.launch( kernel=VBD_solve_trimesh, inputs=[ dt, - self.model.coloring[i_color], + self.model.particle_coloring[color_counter], self.particle_q_prev, state_in.particle_q, state_out.particle_q, @@ -544,15 +870,34 @@ def simulate(self, model: Model, state_in: State, state_out: State, dt: float, c self.model.tri_areas, self.model.edge_indices, self.adjacency, + self.model.soft_contact_ke, + self.model.soft_contact_mu, + self.friction_epsilon, + # body-particle contact + self.model.particle_radius, + self.body_particle_contact_buffer_pre_alloc, + self.body_particle_contact_buffer, + self.body_particle_contact_count, + self.model.shape_materials, + self.model.shape_body, + self.model.body_q, + self.model.body_qd, + self.model.body_com, + self.model.soft_contact_shape, + self.model.soft_contact_body_pos, + self.model.soft_contact_body_vel, + self.model.soft_contact_normal, + self.model.ground, + self.model.ground_plane, ], - dim=self.model.coloring[i_color].size, + dim=self.model.particle_coloring[color_counter].size, device=self.device, ) wp.launch( kernel=VBD_copy_particle_positions_back, - inputs=[self.model.coloring[i_color], state_in.particle_q, state_out.particle_q], - dim=self.model.coloring[i_color].size, + inputs=[self.model.particle_coloring[color_counter], state_in.particle_q, state_out.particle_q], + dim=self.model.particle_coloring[color_counter].size, device=self.device, ) @@ -563,6 +908,22 @@ def simulate(self, model: Model, state_in: State, state_out: State, dt: float, c device=self.device, ) + def convert_body_particle_contact_data(self): + self.body_particle_contact_count.zero_() + + wp.launch( + kernel=convert_body_particle_contact_data_kernel, + inputs=[ + self.body_particle_contact_buffer_pre_alloc, + self.model.soft_contact_particle, + self.model.soft_contact_count, + self.model.soft_contact_max, + ], + outputs=[self.body_particle_contact_buffer, self.body_particle_contact_count], + dim=self.model.soft_contact_max, + device=self.device, + ) + @wp.kernel def count_num_adjacent_edges( edges_array: wp.array(dtype=wp.int32, ndim=2), num_vertex_adjacent_edges: wp.array(dtype=wp.int32) diff --git a/warp/sim/model.py b/warp/sim/model.py index 2842bef7..4d9df0fb 100644 --- a/warp/sim/model.py +++ b/warp/sim/model.py @@ -641,6 +641,8 @@ class Model: joint_dof_count (int): Total number of velocity degrees of freedom of all joints in the system joint_coord_count (int): Total number of position degrees of freedom of all joints in the system + particle_coloring (list of array): The coloring of all the particles, used for VBD's Gauss-Seidel interation. + device (wp.Device): Device on which the Model was allocated Note: @@ -810,6 +812,8 @@ def __init__(self, device=None): self.joint_dof_count = 0 self.joint_coord_count = 0 + self.particle_coloring = [] + self.device = wp.get_device(device) def state(self, requires_grad=None) -> State: @@ -3858,16 +3862,22 @@ def grid_index(x, y, dim_x): p = wp.quat_rotate(rot, g) + pos m = mass + particle_flag = PARTICLE_FLAG_ACTIVE + if x == 0 and fix_left: m = 0.0 + particle_flag = wp.uint32(int(particle_flag) & ~int(PARTICLE_FLAG_ACTIVE)) elif x == dim_x and fix_right: m = 0.0 + particle_flag = wp.uint32(int(particle_flag) & ~int(PARTICLE_FLAG_ACTIVE)) elif y == 0 and fix_bottom: m = 0.0 + particle_flag = wp.uint32(int(particle_flag) & ~int(PARTICLE_FLAG_ACTIVE)) elif y == dim_y and fix_top: m = 0.0 + particle_flag = wp.uint32(int(particle_flag) & ~int(PARTICLE_FLAG_ACTIVE)) - self.add_particle(p, vel, m) + self.add_particle(p, vel, m, flags=particle_flag) if x > 0 and y > 0: if reverse_winding: @@ -4015,7 +4025,7 @@ def add_cloth_mesh( edgeinds[:, 0], edgeinds[:, 1], edgeinds[:, 2], - edgeinds[:, 0], + edgeinds[:, 3], edge_ke=[edge_ke] * len(edgeinds), edge_kd=[edge_kd] * len(edgeinds), ) diff --git a/warp/stubs.py b/warp/stubs.py index 301b056b..01c8234d 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -109,11 +109,17 @@ from warp.dlpack import from_dlpack, to_dlpack +from warp.paddle import from_paddle, to_paddle +from warp.paddle import dtype_from_paddle, dtype_to_paddle +from warp.paddle import device_from_paddle, device_to_paddle +from warp.paddle import stream_from_paddle + from warp.build import clear_kernel_cache from warp.constants import * from . import builtins +from warp.builtins import static import warp.config as config @@ -928,30 +934,57 @@ def tile_arange(*args: Scalar, dtype: Scalar) -> Tile: @over -def tile_load(a: Array[Any], x: int32, y: int32, m: int32, n: int32) -> Tile: - """Loads a tile from a global memory array. +def tile_load(a: Array[Any], i: int32, n: int32) -> Tile: + """Loads a 1D tile from a global memory array. This method will cooperatively load a tile from global memory using all threads in the block. :param a: The source array in global memory - :param x: Offset in the source array measured in multiples of ``m``, i.e.: ``i=x*m`` - :param y: Offset in the source array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param i: Offset in the source array measured in multiples of ``n``, i.e.: ``offset=i*n`` + :param n: The number of elements in the tile + :returns: A tile with ``shape=(1,n)`` and dtype the same as the source array + """ + ... + + +@over +def tile_load(a: Array[Any], i: int32, j: int32, m: int32, n: int32) -> Tile: + """Loads a 2D tile from a global memory array. + + This method will cooperatively load a tile from global memory using all threads in the block. + + :param a: The source array in global memory + :param i: Offset in the source array measured in multiples of ``m``, i.e.: ``row=i*m`` + :param j: Offset in the source array measured in multiples of ``n``, i.e.; ``col=j*n`` :param m: The size of the tile's first dimension - :param n: The size of the tile's second dimensions + :param n: The size of the tile's second dimension :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array """ ... @over -def tile_store(a: Array[Any], x: int32, y: int32, t: Any): +def tile_store(a: Array[Any], i: int32, t: Any): + """Stores a 1D tile to a global memory array. + + This method will cooperatively store a tile to global memory using all threads in the block. + + :param a: The destination array in global memory + :param i: Offset in the destination array measured in multiples of ``n``, i.e.: ``offset=i*n`` + :param t: The source tile to store data from, must have the same dtype as the destination array + """ + ... + + +@over +def tile_store(a: Array[Any], i: int32, j: int32, t: Any): """Stores a tile to a global memory array. This method will cooperatively store a tile to global memory using all threads in the block. :param a: The destination array in global memory - :param x: Offset in the destination array measured in multiples of ``m``, i.e.: ``i=x*m`` - :param y: Offset in the destination array measured in multiples of ``n``, i.e.; ``j=y*n`` + :param i: Offset in the destination array measured in multiples of ``m``, i.e.: ``row=i*m`` + :param j: Offset in the destination array measured in multiples of ``n``, i.e.; ``col=j*n`` :param t: The source tile to store data from, must have the same dtype as the destination array """ ... @@ -976,8 +1009,11 @@ def tile(x: Any) -> Tile: This function converts values computed using scalar kernel code to a tile representation for input into collective operations. + * If the input value is a scalar then the resulting tile has ``shape=(1, block_dim)`` + * If the input value is a vector then the resulting tile has ``shape=(length(vector), block_dim)`` + :param x: A per-thread local value, e.g.: scalar, vector, or matrix. - :returns: A tile with ``shape=(1, block_dim)`` where ``block_dim`` is the number of threads specified in ``wp.launch()``. + :returns: A tile with first dimension according to the value type length and a second dimension equal to ``block_dim`` This example shows how to create a linear sequence from thread variables: @@ -996,7 +1032,8 @@ def compute(): .. code-block:: text - tile(m=1, n=16, storage=register) = [[0 2 4 6 8 10 12 14...]] + tile(m=1, n=16, storage=register) = [[0 2 4 6 8 ...]] + """ ... @@ -1008,6 +1045,9 @@ def untile(a: Any) -> Scalar: This function converts a block-wide tile back to per-thread values. + * If the input tile is 1-dimensional then the resulting value will be a per-thread scalar + * If the input tile is 2-dimensional then the the resulting value will be a per-thread vector of length M + :param a: A tile with dimensions ``shape=(M, block_dim)`` :returns: A single value per-thread with the same dtype as the tile @@ -1071,6 +1111,18 @@ def tile_transpose(a: Tile) -> Tile: ... +@over +def tile_broadcast(a: Tile, m: int32, n: int32) -> Tile: + """Broadcast a tile. + + This method will attempt to broadcast the input tile ``a`` to the destination shape (m, n), broadcasting follows NumPy broadcast rules. + + :param a: Tile to broadcast + :returns: Tile with broadcast ``shape=(m, n)`` + """ + ... + + @over def tile_sum(a: Tile) -> Tile: """Cooperatively compute the sum the tile elements using all threads in the block. @@ -2163,145 +2215,217 @@ def atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: in @over def atomic_min(arr: Array[Any], i: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: FabricArray[Any], i: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" + """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: Array[Any], i: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: FabricArray[Any], i: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @over def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: - """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value.""" + """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. + + .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + """ ... @@ -2803,3 +2927,18 @@ def tile_ifft(inout: Tile) -> Tile: :param inout: The input/output tile """ ... + + +@over +def static(expr: Any) -> Any: + """Evaluates a static Python expression and replaces it with its result. + + See the `codegen.html#static-expressions
`_ for more details. + + Note: + The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, + which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. + The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions + (excluding Warp arrays since they cannot be created in a Warp kernel at the moment). + """ + ... diff --git a/warp/tape.py b/warp/tape.py index 6df7c21b..67e9bc9f 100644 --- a/warp/tape.py +++ b/warp/tape.py @@ -50,6 +50,8 @@ def __init__(self): self.loss = None def __enter__(self): + wp.context.init() + if wp.context.runtime.tape is not None: raise RuntimeError("Warp: Error, entering a tape while one is already active") diff --git a/warp/tests/test_array.py b/warp/tests/test_array.py index 97bcf208..77721ca5 100644 --- a/warp/tests/test_array.py +++ b/warp/tests/test_array.py @@ -2590,6 +2590,25 @@ def test_array_from_int64_domain(test, device): wp.zeros(np.array([1504, 1080, 520], dtype=np.int64), dtype=wp.float32, device=device) +def test_numpy_array_interface(test, device): + # We should be able to convert between NumPy and Warp arrays using __array_interface__ on CPU. + # This tests all scalar types supported by both. + + n = 10 + + scalar_types = wp.types.scalar_types + + for dtype in scalar_types: + # test round trip + a1 = wp.zeros(n, dtype=dtype, device="cpu") + na = np.array(a1) + a2 = wp.array(na, device="cpu") + + assert a1.dtype == a2.dtype + assert a1.shape == a2.shape + assert a1.strides == a2.strides + + devices = get_test_devices() @@ -2648,6 +2667,7 @@ def test_array_new_del(self): add_function_test(TestArray, "test_array_of_structs_roundtrip", test_array_of_structs_roundtrip, devices=devices) add_function_test(TestArray, "test_array_from_numpy", test_array_from_numpy, devices=devices) add_function_test(TestArray, "test_array_aliasing_from_numpy", test_array_aliasing_from_numpy, devices=["cpu"]) +add_function_test(TestArray, "test_numpy_array_interface", test_numpy_array_interface, devices=["cpu"]) add_function_test(TestArray, "test_array_inplace_ops", test_array_inplace_ops, devices=devices) add_function_test(TestArray, "test_direct_from_numpy", test_direct_from_numpy, devices=["cpu"]) diff --git a/warp/tests/test_codegen.py b/warp/tests/test_codegen.py index beb0cf03..e3552ad2 100644 --- a/warp/tests/test_codegen.py +++ b/warp/tests/test_codegen.py @@ -405,22 +405,22 @@ def kernel_3_fn( kernel = wp.Kernel(func=kernel_1_fn) with test.assertRaisesRegex( - RuntimeError, - r"Cannot reference a global variable from a kernel unless `wp.constant\(\)` is being used", + TypeError, + r"Invalid external reference type: ", ): wp.launch(kernel, dim=out.shape, inputs=(), outputs=(out,), device=device) kernel = wp.Kernel(func=kernel_2_fn) with test.assertRaisesRegex( - RuntimeError, - r"Cannot reference a global variable from a kernel unless `wp.constant\(\)` is being used", + TypeError, + r"Invalid external reference type: ", ): wp.launch(kernel, dim=out.shape, inputs=(), outputs=(out,), device=device) kernel = wp.Kernel(func=kernel_3_fn) with test.assertRaisesRegex( - RuntimeError, - r"Cannot reference a global variable from a kernel unless `wp.constant\(\)` is being used", + TypeError, + r"Invalid external reference type: ", ): wp.launch(kernel, dim=out.shape, inputs=(), outputs=(out,), device=device) @@ -489,6 +489,21 @@ def kernel_2_fn(): wp.launch(kernel, dim=1, device=device) +def test_error_mutating_constant_in_dynamic_loop(test, device): + @wp.kernel + def dynamic_loop_kernel(n: int, input: wp.array(dtype=float)): + my_constant = 0.0 + for i in range(n): + my_constant += input[i] + + inputs = wp.array([1.0, 2.0, 3.0], dtype=float, device=device) + with test.assertRaisesRegex( + wp.codegen.WarpCodegenError, + r"Error mutating a constant my_constant inside a dynamic loop, use the following syntax\: pi = float\(3\.141\) to declare a dynamic variable", + ): + wp.launch(dynamic_loop_kernel, dim=1, inputs=[3, inputs], device=device) + + @wp.kernel def test_call_syntax(): expected_pow = 16.0 @@ -667,6 +682,12 @@ class TestCodeGen(unittest.TestCase): add_function_test( TestCodeGen, func=test_error_unmatched_arguments, name="test_error_unmatched_arguments", devices=devices ) +add_function_test( + TestCodeGen, + func=test_error_mutating_constant_in_dynamic_loop, + name="test_error_mutating_constant_in_dynamic_loop", + devices=devices, +) add_kernel_test(TestCodeGen, name="test_call_syntax", kernel=test_call_syntax, dim=1, devices=devices) add_kernel_test(TestCodeGen, name="test_shadow_builtin", kernel=test_shadow_builtin, dim=1, devices=devices) diff --git a/warp/tests/test_dlpack.py b/warp/tests/test_dlpack.py index 45fbef13..30ef693a 100644 --- a/warp/tests/test_dlpack.py +++ b/warp/tests/test_dlpack.py @@ -350,6 +350,34 @@ def test_dlpack_torch_to_warp_v2(test, device): assert_np_equal(a.numpy(), t.cpu().numpy()) +def test_dlpack_paddle_to_warp(test, device): + import paddle + import paddle.utils.dlpack + + t = paddle.arange(N, dtype=paddle.float32).to(device=wp.device_to_paddle(device)) + + # paddle do not implement __dlpack__ yet, so only test to_dlpack here + a = wp.from_dlpack(paddle.utils.dlpack.to_dlpack(t)) + + item_size = wp.types.type_size_in_bytes(a.dtype) + + test.assertEqual(a.ptr, t.data_ptr()) + test.assertEqual(a.device, wp.device_from_paddle(t.place)) + test.assertEqual(a.dtype, wp.dtype_from_paddle(t.dtype)) + test.assertEqual(a.shape, tuple(t.shape)) + test.assertEqual(a.strides, tuple(s * item_size for s in t.strides)) + + assert_np_equal(a.numpy(), t.numpy()) + + wp.launch(inc, dim=a.size, inputs=[a], device=device) + + assert_np_equal(a.numpy(), t.numpy()) + + paddle.assign(t + 1, t) + + assert_np_equal(a.numpy(), t.numpy()) + + def test_dlpack_warp_to_jax(test, device): import jax import jax.dlpack @@ -421,6 +449,61 @@ def test_dlpack_warp_to_jax_v2(test, device): assert_np_equal(a.numpy(), np.asarray(j2)) +def test_dlpack_warp_to_paddle(test, device): + import paddle.utils.dlpack + + a = wp.array(data=np.arange(N, dtype=np.float32), device=device) + + t = paddle.utils.dlpack.from_dlpack(wp.to_dlpack(a)) + + item_size = wp.types.type_size_in_bytes(a.dtype) + + test.assertEqual(a.ptr, t.data_ptr()) + test.assertEqual(a.device, wp.device_from_paddle(t.place)) + test.assertEqual(a.dtype, wp.dtype_from_paddle(t.dtype)) + test.assertEqual(a.shape, tuple(t.shape)) + test.assertEqual(a.strides, tuple(s * item_size for s in t.strides)) + + assert_np_equal(a.numpy(), t.cpu().numpy()) + + wp.launch(inc, dim=a.size, inputs=[a], device=device) + + assert_np_equal(a.numpy(), t.cpu().numpy()) + + paddle.assign(t + 1, t) + + assert_np_equal(a.numpy(), t.cpu().numpy()) + + +def test_dlpack_warp_to_paddle_v2(test, device): + # same as original test, but uses newer __dlpack__() method + + import paddle.utils.dlpack + + a = wp.array(data=np.arange(N, dtype=np.float32), device=device) + + # pass the array directly + t = paddle.utils.dlpack.from_dlpack(a) + + item_size = wp.types.type_size_in_bytes(a.dtype) + + test.assertEqual(a.ptr, t.data_ptr()) + test.assertEqual(a.device, wp.device_from_paddle(t.place)) + test.assertEqual(a.dtype, wp.dtype_from_paddle(t.dtype)) + test.assertEqual(a.shape, tuple(t.shape)) + test.assertEqual(a.strides, tuple(s * item_size for s in t.strides)) + + assert_np_equal(a.numpy(), t.numpy()) + + wp.launch(inc, dim=a.size, inputs=[a], device=device) + + assert_np_equal(a.numpy(), t.numpy()) + + paddle.assign(t + 1, t) + + assert_np_equal(a.numpy(), t.numpy()) + + def test_dlpack_jax_to_warp(test, device): import jax import jax.dlpack @@ -575,6 +658,41 @@ class TestDLPack(unittest.TestCase): print(f"Skipping Jax DLPack tests due to exception: {e}") +# paddle interop via dlpack +try: + import paddle + import paddle.utils.dlpack + + # check which Warp devices work with paddle + # CUDA devices may fail if paddle was not compiled with CUDA support + test_devices = get_test_devices() + paddle_compatible_devices = [] + for d in test_devices: + try: + t = paddle.arange(10).to(device=wp.device_to_paddle(d)) + paddle.assign(t + 1, t) + paddle_compatible_devices.append(d) + except Exception as e: + print(f"Skipping paddle DLPack tests on device '{d}' due to exception: {e}") + + if paddle_compatible_devices: + add_function_test( + TestDLPack, "test_dlpack_warp_to_paddle", test_dlpack_warp_to_paddle, devices=paddle_compatible_devices + ) + add_function_test( + TestDLPack, + "test_dlpack_warp_to_paddle_v2", + test_dlpack_warp_to_paddle_v2, + devices=paddle_compatible_devices, + ) + add_function_test( + TestDLPack, "test_dlpack_paddle_to_warp", test_dlpack_paddle_to_warp, devices=paddle_compatible_devices + ) + +except Exception as e: + print(f"Skipping Paddle DLPack tests due to exception: {e}") + + if __name__ == "__main__": wp.clear_kernel_cache() unittest.main(verbosity=2) diff --git a/warp/tests/test_implicit_init.py b/warp/tests/test_implicit_init.py index e9daef58..e926397d 100644 --- a/warp/tests/test_implicit_init.py +++ b/warp/tests/test_implicit_init.py @@ -347,6 +347,55 @@ class TestImplicitInitIsPeerAccessSupported(unittest.TestCase): ) +# Structs +# ------------------------------------------------------------------------------ + + +def test_struct_member_init(test, device): + @wp.struct + class S: + # fp16 requires conversion functions from warp.so + x: wp.float16 + v: wp.vec3h + + s = S() + s.x = 42.0 + s.v = wp.vec3h(1.0, 2.0, 3.0) + + +class TestImplicitInitStructMemberInit(unittest.TestCase): + pass + + +add_function_test( + TestImplicitInitStructMemberInit, + "test_struct_member_init", + test_struct_member_init, + check_output=False, +) + + +# Tape +# ------------------------------------------------------------------------------ + + +def test_tape(test, device): + with wp.Tape(): + pass + + +class TestImplicitInitTape(unittest.TestCase): + pass + + +add_function_test( + TestImplicitInitTape, + "test_tape", + test_tape, + check_output=False, +) + + if __name__ == "__main__": # Do not clear the kernel cache or call anything that would initialize Warp # since these tests are specifically aiming to catch issues where Warp isn't diff --git a/warp/tests/test_paddle.py b/warp/tests/test_paddle.py new file mode 100644 index 00000000..53db028e --- /dev/null +++ b/warp/tests/test_paddle.py @@ -0,0 +1,852 @@ +# Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +import unittest + +import numpy as np + +import warp as wp +from warp.tests.unittest_utils import * + + +@wp.kernel +def op_kernel(x: wp.array(dtype=float), y: wp.array(dtype=float)): + tid = wp.tid() + y[tid] = 0.5 - x[tid] * 2.0 + + +@wp.kernel +def inc(a: wp.array(dtype=float)): + tid = wp.tid() + a[tid] = a[tid] + 1.0 + + +@wp.kernel +def inc_vector(a: wp.array(dtype=wp.vec3f)): + tid = wp.tid() + a[tid] = a[tid] + wp.vec3f(1.0) + + +@wp.kernel +def inc_matrix(a: wp.array(dtype=wp.mat22f)): + tid = wp.tid() + a[tid] = a[tid] + wp.mat22f(1.0) + + +@wp.kernel +def arange(start: int, step: int, a: wp.array(dtype=int)): + tid = wp.tid() + a[tid] = start + step * tid + + +# copy elements between non-contiguous 1d arrays of float +@wp.kernel +def copy1d_float_kernel(dst: wp.array(dtype=float), src: wp.array(dtype=float)): + i = wp.tid() + dst[i] = src[i] + + +# copy elements between non-contiguous 2d arrays of float +@wp.kernel +def copy2d_float_kernel(dst: wp.array2d(dtype=float), src: wp.array2d(dtype=float)): + i, j = wp.tid() + dst[i, j] = src[i, j] + + +# copy elements between non-contiguous 3d arrays of float +@wp.kernel +def copy3d_float_kernel(dst: wp.array3d(dtype=float), src: wp.array3d(dtype=float)): + i, j, k = wp.tid() + dst[i, j, k] = src[i, j, k] + + +# copy elements between non-contiguous 2d arrays of vec3 +@wp.kernel +def copy2d_vec3_kernel(dst: wp.array2d(dtype=wp.vec3), src: wp.array2d(dtype=wp.vec3)): + i, j = wp.tid() + dst[i, j] = src[i, j] + + +# copy elements between non-contiguous 2d arrays of mat22 +@wp.kernel +def copy2d_mat22_kernel(dst: wp.array2d(dtype=wp.mat22), src: wp.array2d(dtype=wp.mat22)): + i, j = wp.tid() + dst[i, j] = src[i, j] + + +def test_dtype_from_paddle(test, device): + import paddle + + def test_conversions(paddle_type, warp_type): + test.assertEqual(wp.dtype_from_paddle(paddle_type), warp_type) + + test_conversions(paddle.float16, wp.float16) + test_conversions(paddle.float32, wp.float32) + test_conversions(paddle.float64, wp.float64) + test_conversions(paddle.int8, wp.int8) + test_conversions(paddle.int16, wp.int16) + test_conversions(paddle.int32, wp.int32) + test_conversions(paddle.int64, wp.int64) + test_conversions(paddle.uint8, wp.uint8) + test_conversions(paddle.bool, wp.bool) + + +def test_dtype_to_paddle(test, device): + import paddle + + def test_conversions(warp_type, paddle_type): + test.assertEqual(wp.dtype_to_paddle(warp_type), paddle_type) + + test_conversions(wp.float16, paddle.float16) + test_conversions(wp.float32, paddle.float32) + test_conversions(wp.float64, paddle.float64) + test_conversions(wp.int8, paddle.int8) + test_conversions(wp.int16, paddle.int16) + test_conversions(wp.int32, paddle.int32) + test_conversions(wp.int64, paddle.int64) + test_conversions(wp.uint8, paddle.uint8) + test_conversions(wp.uint16, paddle.int16) + test_conversions(wp.uint32, paddle.int32) + test_conversions(wp.uint64, paddle.int64) + test_conversions(wp.bool, paddle.bool) + + +def test_device_conversion(test, device): + paddle_device = wp.device_to_paddle(device) + warp_device = wp.device_from_paddle(paddle_device) + test.assertEqual(warp_device, device) + + +def test_paddle_zerocopy(test, device): + import paddle + + a = wp.zeros(10, dtype=wp.float32, device=device) + t = wp.to_paddle(a) + assert a.ptr == t.data_ptr() + + paddle_device = wp.device_to_paddle(device) + + t = paddle.zeros([10], dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + + +def test_from_paddle(test, device): + import paddle + + paddle_device = wp.device_to_paddle(device) + + # automatically determine warp dtype + def wrap_scalar_tensor_implicit(paddle_dtype, expected_warp_dtype): + t = paddle.zeros([10], dtype=paddle_dtype).to(device=paddle_device) + a = wp.from_paddle(t) + assert a.dtype == expected_warp_dtype + assert a.shape == tuple(t.shape) + + wrap_scalar_tensor_implicit(paddle.float64, wp.float64) + wrap_scalar_tensor_implicit(paddle.float32, wp.float32) + wrap_scalar_tensor_implicit(paddle.float16, wp.float16) + wrap_scalar_tensor_implicit(paddle.int64, wp.int64) + wrap_scalar_tensor_implicit(paddle.int32, wp.int32) + wrap_scalar_tensor_implicit(paddle.int16, wp.int16) + wrap_scalar_tensor_implicit(paddle.int8, wp.int8) + wrap_scalar_tensor_implicit(paddle.uint8, wp.uint8) + wrap_scalar_tensor_implicit(paddle.bool, wp.bool) + + # explicitly specify warp dtype + def wrap_scalar_tensor_explicit(paddle_dtype, expected_warp_dtype): + t = paddle.zeros([10], dtype=paddle_dtype).to(device=paddle_device) + a = wp.from_paddle(t, expected_warp_dtype) + assert a.dtype == expected_warp_dtype + assert a.shape == tuple(t.shape) + + wrap_scalar_tensor_explicit(paddle.float64, wp.float64) + wrap_scalar_tensor_explicit(paddle.float32, wp.float32) + wrap_scalar_tensor_explicit(paddle.float16, wp.float16) + wrap_scalar_tensor_explicit(paddle.int64, wp.int64) + wrap_scalar_tensor_explicit(paddle.int64, wp.uint64) + wrap_scalar_tensor_explicit(paddle.int32, wp.int32) + wrap_scalar_tensor_explicit(paddle.int32, wp.uint32) + wrap_scalar_tensor_explicit(paddle.int16, wp.int16) + wrap_scalar_tensor_explicit(paddle.int16, wp.uint16) + wrap_scalar_tensor_explicit(paddle.int8, wp.int8) + wrap_scalar_tensor_explicit(paddle.int8, wp.uint8) + wrap_scalar_tensor_explicit(paddle.uint8, wp.uint8) + wrap_scalar_tensor_explicit(paddle.uint8, wp.int8) + wrap_scalar_tensor_explicit(paddle.bool, wp.uint8) + wrap_scalar_tensor_explicit(paddle.bool, wp.int8) + wrap_scalar_tensor_explicit(paddle.bool, wp.bool) + + def wrap_vec_tensor(n, desired_warp_dtype): + t = paddle.zeros((10, n), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, desired_warp_dtype) + assert a.dtype == desired_warp_dtype + assert a.shape == (10,) + + wrap_vec_tensor(2, wp.vec2) + wrap_vec_tensor(3, wp.vec3) + wrap_vec_tensor(4, wp.vec4) + wrap_vec_tensor(6, wp.spatial_vector) + wrap_vec_tensor(7, wp.transform) + + def wrap_mat_tensor(n, m, desired_warp_dtype): + t = paddle.zeros((10, n, m), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, desired_warp_dtype) + assert a.dtype == desired_warp_dtype + assert a.shape == (10,) + + wrap_mat_tensor(2, 2, wp.mat22) + wrap_mat_tensor(3, 3, wp.mat33) + wrap_mat_tensor(4, 4, wp.mat44) + wrap_mat_tensor(6, 6, wp.spatial_matrix) + + def wrap_vec_tensor_with_grad(n, desired_warp_dtype): + t = paddle.zeros((10, n), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, desired_warp_dtype) + a.reuqires_grad = True + assert a.dtype == desired_warp_dtype + assert a.shape == (10,) + + wrap_vec_tensor_with_grad(2, wp.vec2) + wrap_vec_tensor_with_grad(3, wp.vec3) + wrap_vec_tensor_with_grad(4, wp.vec4) + wrap_vec_tensor_with_grad(6, wp.spatial_vector) + wrap_vec_tensor_with_grad(7, wp.transform) + + def wrap_mat_tensor_with_grad(n, m, desired_warp_dtype): + t = paddle.zeros((10, n, m), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, desired_warp_dtype, requires_grad=True) + assert a.dtype == desired_warp_dtype + assert a.shape == (10,) + + wrap_mat_tensor_with_grad(2, 2, wp.mat22) + wrap_mat_tensor_with_grad(3, 3, wp.mat33) + wrap_mat_tensor_with_grad(4, 4, wp.mat44) + wrap_mat_tensor_with_grad(6, 6, wp.spatial_matrix) + + +def test_array_ctype_from_paddle(test, device): + import paddle + + paddle_device = wp.device_to_paddle(device) + + # automatically determine warp dtype + def wrap_scalar_tensor_implicit(paddle_dtype): + t = paddle.zeros([10], dtype=paddle_dtype).to(device=paddle_device) + a = wp.from_paddle(t, return_ctype=True) + warp_dtype = wp.dtype_from_paddle(paddle_dtype) + ctype_size = ctypes.sizeof(warp_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == 0 + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_scalar_tensor_implicit(paddle.float64) + wrap_scalar_tensor_implicit(paddle.float32) + wrap_scalar_tensor_implicit(paddle.float16) + wrap_scalar_tensor_implicit(paddle.int64) + wrap_scalar_tensor_implicit(paddle.int32) + wrap_scalar_tensor_implicit(paddle.int16) + wrap_scalar_tensor_implicit(paddle.int8) + wrap_scalar_tensor_implicit(paddle.uint8) + wrap_scalar_tensor_implicit(paddle.bool) + + # explicitly specify warp dtype + def wrap_scalar_tensor_explicit(paddle_dtype, warp_dtype): + t = paddle.zeros([10], dtype=paddle_dtype).to(device=paddle_device) + a = wp.from_paddle(t, dtype=warp_dtype, return_ctype=True) + ctype_size = ctypes.sizeof(warp_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == 0 + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_scalar_tensor_explicit(paddle.float64, wp.float64) + wrap_scalar_tensor_explicit(paddle.float32, wp.float32) + wrap_scalar_tensor_explicit(paddle.float16, wp.float16) + wrap_scalar_tensor_explicit(paddle.int64, wp.int64) + wrap_scalar_tensor_explicit(paddle.int64, wp.uint64) + wrap_scalar_tensor_explicit(paddle.int32, wp.int32) + wrap_scalar_tensor_explicit(paddle.int32, wp.uint32) + wrap_scalar_tensor_explicit(paddle.int16, wp.int16) + wrap_scalar_tensor_explicit(paddle.int16, wp.uint16) + wrap_scalar_tensor_explicit(paddle.int8, wp.int8) + wrap_scalar_tensor_explicit(paddle.int8, wp.uint8) + wrap_scalar_tensor_explicit(paddle.uint8, wp.uint8) + wrap_scalar_tensor_explicit(paddle.uint8, wp.int8) + wrap_scalar_tensor_explicit(paddle.bool, wp.uint8) + wrap_scalar_tensor_explicit(paddle.bool, wp.int8) + wrap_scalar_tensor_explicit(paddle.bool, wp.bool) + + def wrap_vec_tensor(vec_dtype): + t = paddle.zeros((10, vec_dtype._length_), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, dtype=vec_dtype, return_ctype=True) + ctype_size = ctypes.sizeof(vec_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == 0 + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_vec_tensor(wp.vec2) + wrap_vec_tensor(wp.vec3) + wrap_vec_tensor(wp.vec4) + wrap_vec_tensor(wp.spatial_vector) + wrap_vec_tensor(wp.transform) + + def wrap_mat_tensor(mat_dtype): + t = paddle.zeros((10, *mat_dtype._shape_), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, dtype=mat_dtype, return_ctype=True) + ctype_size = ctypes.sizeof(mat_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == 0 + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_mat_tensor(wp.mat22) + wrap_mat_tensor(wp.mat33) + wrap_mat_tensor(wp.mat44) + wrap_mat_tensor(wp.spatial_matrix) + + def wrap_vec_tensor_with_existing_grad(vec_dtype): + t = paddle.zeros((10, vec_dtype._length_), dtype=paddle.float32).to(device=paddle_device) + t.stop_gradient = False + t.grad_ = paddle.zeros((10, vec_dtype._length_), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, dtype=vec_dtype, return_ctype=True) + ctype_size = ctypes.sizeof(vec_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == t.grad.data_ptr() + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_vec_tensor_with_existing_grad(wp.vec2) + wrap_vec_tensor_with_existing_grad(wp.vec3) + wrap_vec_tensor_with_existing_grad(wp.vec4) + wrap_vec_tensor_with_existing_grad(wp.spatial_vector) + wrap_vec_tensor_with_existing_grad(wp.transform) + + def wrap_vec_tensor_with_new_grad(vec_dtype): + t = paddle.zeros((10, vec_dtype._length_), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, dtype=vec_dtype, requires_grad=True, return_ctype=True) + ctype_size = ctypes.sizeof(vec_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == t.grad.data_ptr() + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_vec_tensor_with_new_grad(wp.vec2) + wrap_vec_tensor_with_new_grad(wp.vec3) + wrap_vec_tensor_with_new_grad(wp.vec4) + wrap_vec_tensor_with_new_grad(wp.spatial_vector) + wrap_vec_tensor_with_new_grad(wp.transform) + + def wrap_vec_tensor_with_paddle_grad(vec_dtype): + t = paddle.zeros((10, vec_dtype._length_), dtype=paddle.float32).to(device=paddle_device) + grad = paddle.zeros((10, vec_dtype._length_), dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t, dtype=vec_dtype, grad=grad, return_ctype=True) + ctype_size = ctypes.sizeof(vec_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == grad.data_ptr() + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_vec_tensor_with_paddle_grad(wp.vec2) + wrap_vec_tensor_with_paddle_grad(wp.vec3) + wrap_vec_tensor_with_paddle_grad(wp.vec4) + wrap_vec_tensor_with_paddle_grad(wp.spatial_vector) + wrap_vec_tensor_with_paddle_grad(wp.transform) + + def wrap_vec_tensor_with_warp_grad(vec_dtype): + t = paddle.zeros((10, vec_dtype._length_), dtype=paddle.float32).to(device=paddle_device) + grad = wp.zeros(10, dtype=vec_dtype, device=device) + a = wp.from_paddle(t, dtype=vec_dtype, grad=grad, return_ctype=True) + ctype_size = ctypes.sizeof(vec_dtype._type_) + assert a.data == t.data_ptr() + assert a.grad == grad.ptr + assert a.ndim == 1 + assert a.shape[0] == t.shape[0] + assert a.strides[0] == t.strides[0] * ctype_size + + wrap_vec_tensor_with_warp_grad(wp.vec2) + wrap_vec_tensor_with_warp_grad(wp.vec3) + wrap_vec_tensor_with_warp_grad(wp.vec4) + wrap_vec_tensor_with_warp_grad(wp.spatial_vector) + wrap_vec_tensor_with_warp_grad(wp.transform) + + +def test_to_paddle(test, device): + import paddle + + def wrap_scalar_array(warp_dtype, expected_paddle_dtype): + a = wp.zeros(10, dtype=warp_dtype, device=device) + t = wp.to_paddle(a) + assert t.dtype == expected_paddle_dtype + assert tuple(t.shape) == a.shape + + wrap_scalar_array(wp.float64, paddle.float64) + wrap_scalar_array(wp.float32, paddle.float32) + wrap_scalar_array(wp.float16, paddle.float16) + wrap_scalar_array(wp.int64, paddle.int64) + wrap_scalar_array(wp.int32, paddle.int32) + wrap_scalar_array(wp.int16, paddle.int16) + wrap_scalar_array(wp.int8, paddle.int8) + wrap_scalar_array(wp.uint8, paddle.uint8) + wrap_scalar_array(wp.bool, paddle.bool) + + # not supported by paddle + # wrap_scalar_array(wp.uint64, paddle.int64) + # wrap_scalar_array(wp.uint32, paddle.int32) + # wrap_scalar_array(wp.uint16, paddle.int16) + + def wrap_vec_array(n, warp_dtype): + a = wp.zeros(10, dtype=warp_dtype, device=device) + t = wp.to_paddle(a) + assert t.dtype == paddle.float32 + assert tuple(t.shape) == (10, n) + + wrap_vec_array(2, wp.vec2) + wrap_vec_array(3, wp.vec3) + wrap_vec_array(4, wp.vec4) + wrap_vec_array(6, wp.spatial_vector) + wrap_vec_array(7, wp.transform) + + def wrap_mat_array(n, m, warp_dtype): + a = wp.zeros(10, dtype=warp_dtype, device=device) + t = wp.to_paddle(a) + assert t.dtype == paddle.float32 + assert tuple(t.shape) == (10, n, m) + + wrap_mat_array(2, 2, wp.mat22) + wrap_mat_array(3, 3, wp.mat33) + wrap_mat_array(4, 4, wp.mat44) + wrap_mat_array(6, 6, wp.spatial_matrix) + + +def test_from_paddle_slices(test, device): + import paddle + + paddle_device = wp.device_to_paddle(device) + + # 1D slice, contiguous + t_base = paddle.arange(10, dtype=paddle.float32).to(device=paddle_device) + t = t_base[2:9] + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + assert a.is_contiguous + assert a.shape == tuple(t.shape) + assert_np_equal(a.numpy(), t.cpu().numpy()) + + # 1D slice with non-contiguous stride + t_base = paddle.arange(10, dtype=paddle.float32).to(device=paddle_device) + t = t_base[2:9:2] + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + assert not a.is_contiguous + assert a.shape == tuple(t.shape) + # copy contents to contiguous array + a_contiguous = wp.empty_like(a) + wp.launch(copy1d_float_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + # 2D slices (non-contiguous) + t_base = paddle.arange(24, dtype=paddle.float32).to(device=paddle_device).reshape((4, 6)) + t = t_base[1:3, 2:5] + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + assert not a.is_contiguous + assert a.shape == tuple(t.shape) + # copy contents to contiguous array + a_contiguous = wp.empty_like(a) + wp.launch(copy2d_float_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + # 3D slices (non-contiguous) + t_base = paddle.arange(36, dtype=paddle.float32).to(device=paddle_device).reshape((4, 3, 3)) + t = t_base[::2, 0:1, 1:2] + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + assert not a.is_contiguous + assert a.shape == tuple(t.shape) + # copy contents to contiguous array + a_contiguous = wp.empty_like(a) + wp.launch(copy3d_float_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + # 2D slices of vec3 (inner contiguous, outer non-contiguous) + t_base = paddle.arange(150, dtype=paddle.float32).to(device=paddle_device).reshape((10, 5, 3)) + t = t_base[1:7:2, 2:5] + a = wp.from_paddle(t, dtype=wp.vec3) + assert a.ptr == t.data_ptr() + assert not a.is_contiguous + assert a.shape == tuple(t.shape[:-1]) + # copy contents to contiguous array + a_contiguous = wp.empty_like(a) + wp.launch(copy2d_vec3_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + # 2D slices of mat22 (inner contiguous, outer non-contiguous) + t_base = paddle.arange(200, dtype=paddle.float32).to(device=paddle_device).reshape((10, 5, 2, 2)) + t = t_base[1:7:2, 2:5] + a = wp.from_paddle(t, dtype=wp.mat22) + assert a.ptr == t.data_ptr() + assert not a.is_contiguous + assert a.shape == tuple(t.shape[:-2]) + # copy contents to contiguous array + a_contiguous = wp.empty_like(a) + wp.launch(copy2d_mat22_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + +def test_from_paddle_zero_strides(test, device): + import paddle + + paddle_device = wp.device_to_paddle(device) + + t_base = paddle.arange(9, dtype=paddle.float32).to(device=paddle_device).reshape((3, 3)) + + # expand outermost dimension + t = t_base.unsqueeze(0).expand([3, -1, -1]) + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + assert a.is_contiguous + assert a.shape == tuple(t.shape) + a_contiguous = wp.empty_like(a) + wp.launch(copy3d_float_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + # expand middle dimension + t = t_base.unsqueeze(1).expand([-1, 3, -1]) + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + assert a.is_contiguous + assert a.shape == tuple(t.shape) + a_contiguous = wp.empty_like(a) + wp.launch(copy3d_float_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + # expand innermost dimension + t = t_base.unsqueeze(2).expand([-1, -1, 3]) + a = wp.from_paddle(t) + assert a.ptr == t.data_ptr() + assert a.is_contiguous + assert a.shape == tuple(t.shape) + a_contiguous = wp.empty_like(a) + wp.launch(copy3d_float_kernel, dim=a.shape, inputs=[a_contiguous, a], device=device) + assert_np_equal(a_contiguous.numpy(), t.cpu().numpy()) + + +def test_paddle_mgpu_from_paddle(test, device): + import paddle + + n = 32 + + t0 = paddle.arange(0, n, 1, dtype=paddle.int32).to(device="gpu:0") + t1 = paddle.arange(0, n * 2, 2, dtype=paddle.int32).to(device="gpu:1") + + a0 = wp.from_paddle(t0, dtype=wp.int32) + a1 = wp.from_paddle(t1, dtype=wp.int32) + + assert a0.device == "gpu:0" + assert a1.device == "gpu:1" + + expected0 = np.arange(0, n, 1) + expected1 = np.arange(0, n * 2, 2) + + assert_np_equal(a0.numpy(), expected0) + assert_np_equal(a1.numpy(), expected1) + + +def test_paddle_mgpu_to_paddle(test, device): + n = 32 + + with wp.ScopedDevice("gpu:0"): + a0 = wp.empty(n, dtype=wp.int32) + wp.launch(arange, dim=a0.size, inputs=[0, 1, a0]) + + with wp.ScopedDevice("gpu:1"): + a1 = wp.empty(n, dtype=wp.int32) + wp.launch(arange, dim=a1.size, inputs=[0, 2, a1]) + + t0 = wp.to_paddle(a0) + t1 = wp.to_paddle(a1) + + assert str(t0.device) == "gpu:0" + assert str(t1.device) == "gpu:1" + + expected0 = np.arange(0, n, 1, dtype=np.int32) + expected1 = np.arange(0, n * 2, 2, dtype=np.int32) + + assert_np_equal(t0.cpu().numpy(), expected0) + assert_np_equal(t1.cpu().numpy(), expected1) + + +def test_paddle_mgpu_interop(test, device): + import paddle + + n = 1024 * 1024 + + with paddle.cuda.device(0): + t0 = paddle.arange(n, dtype=paddle.float32).to(device="gpu") + a0 = wp.from_paddle(t0) + wp.launch(inc, dim=a0.size, inputs=[a0], stream=wp.stream_from_paddle()) + + with paddle.cuda.device(1): + t1 = paddle.arange(n, dtype=paddle.float32).to(device="gpu") + a1 = wp.from_paddle(t1) + wp.launch(inc, dim=a1.size, inputs=[a1], stream=wp.stream_from_paddle()) + + assert a0.device == "gpu:0" + assert a1.device == "gpu:1" + + expected = np.arange(n, dtype=int) + 1 + + # ensure the paddle tensors were modified by warp + assert_np_equal(t0.cpu().numpy(), expected) + assert_np_equal(t1.cpu().numpy(), expected) + + +def test_paddle_autograd(test, device): + """Test paddle autograd with a custom Warp op""" + + import paddle + + # custom autograd op + class TestFunc(paddle.autograd.PyLayer): + @staticmethod + def forward(ctx, x): + # allocate output array + y = paddle.empty_like(x) + + ctx.x = x + ctx.y = y + + wp.launch(kernel=op_kernel, dim=len(x), inputs=[wp.from_paddle(x)], outputs=[wp.from_paddle(y)]) + + return y + + @staticmethod + def backward(ctx, adj_y): + # adjoints should be allocated as zero initialized + adj_x = paddle.zeros_like(ctx.x).contiguous() + adj_y = adj_y.contiguous() + + wp_x = wp.from_paddle(ctx.x, grad=adj_x) + wp_y = wp.from_paddle(ctx.y, grad=adj_y) + + wp.launch( + kernel=op_kernel, + dim=len(ctx.x), + # fwd inputs + inputs=[wp_x], + outputs=[wp_y], + # adj inputs (already stored in input/output arrays, passing null pointers) + adj_inputs=[None], + adj_outputs=[None], + adjoint=True, + ) + + return adj_x + + # run autograd on given device + with wp.ScopedDevice(device): + paddle_device = wp.device_to_paddle(device) + + # input data + x = paddle.ones(16, dtype=paddle.float32).to(device=paddle_device) + x.stop_gradient = False + + # execute op + y = TestFunc.apply(x) + + # compute grads + l = y.sum() + l.backward() + + passed = (x.grad == -2.0).all() + assert passed.item() + + +def test_warp_graph_warp_stream(test, device): + """Capture Warp graph on Warp stream""" + + import paddle + + paddle_device = wp.device_to_paddle(device) + + n = 1024 * 1024 + t = paddle.zeros(n, dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t) + + # make paddle use the warp stream from the given device + paddle_stream = wp.stream_to_paddle(device) + + # capture graph + with wp.ScopedDevice(device), paddle.device.stream(paddle_stream): + wp.capture_begin(force_module_load=False) + try: + t += 1.0 + wp.launch(inc, dim=n, inputs=[a]) + t += 1.0 + wp.launch(inc, dim=n, inputs=[a]) + finally: + g = wp.capture_end() + + # replay graph + num_iters = 10 + for _i in range(num_iters): + wp.capture_launch(g) + + passed = (t == num_iters * 4.0).all() + assert passed.item() + + +def test_warp_graph_paddle_stream(test, device): + """Capture Warp graph on Paddle stream""" + + wp.load_module(device=device) + + import paddle + + paddle_device = wp.device_to_paddle(device) + + n = 1024 * 1024 + t = paddle.zeros(n, dtype=paddle.float32).to(device=paddle_device) + a = wp.from_paddle(t) + + # create a device-specific paddle stream to use for capture + # (the default paddle stream is not suitable for graph capture) + paddle_stream = paddle.device.Stream(device=paddle_device) + + # make warp use the same stream + warp_stream = wp.stream_from_paddle(paddle_stream) + + # capture graph + with wp.ScopedStream(warp_stream): + wp.capture_begin(force_module_load=False) + try: + t += 1.0 + wp.launch(inc, dim=n, inputs=[a]) + t += 1.0 + wp.launch(inc, dim=n, inputs=[a]) + finally: + g = wp.capture_end() + + # replay graph + num_iters = 10 + for _i in range(num_iters): + wp.capture_launch(g) + + passed = (t == num_iters * 4.0).all() + assert passed.item() + + +def test_direct(test, device): + """Pass Paddle tensors to Warp kernels directly""" + + import paddle + + paddle_device = wp.device_to_paddle(device) + n = 12 + + s = paddle.arange(n, dtype=paddle.float32).to(device=paddle_device) + v = paddle.arange(n, dtype=paddle.float32).to(device=paddle_device).reshape((n // 3, 3)) + m = paddle.arange(n, dtype=paddle.float32).to(device=paddle_device).reshape((n // 4, 2, 2)) + + wp.launch(inc, dim=n, inputs=[s], device=device) + wp.launch(inc_vector, dim=n // 3, inputs=[v], device=device) + wp.launch(inc_matrix, dim=n // 4, inputs=[m], device=device) + + expected = paddle.arange(1, n + 1, dtype=paddle.float32).to(device=paddle_device) + + assert paddle.equal_all(s, expected).item() + assert paddle.equal_all(v.reshape([n]), expected).item() + assert paddle.equal_all(m.reshape([n]), expected).item() + + +class TestPaddle(unittest.TestCase): + pass + + +test_devices = get_test_devices() + +try: + import paddle + + # check which Warp devices work with Paddle + # CUDA devices may fail if Paddle was not compiled with CUDA support + paddle_compatible_devices = [] + paddle_compatible_cuda_devices = [] + + for d in test_devices: + try: + t = paddle.arange(10).to(device=wp.device_to_paddle(d)) + t += 1 + paddle_compatible_devices.append(d) + if d.is_cuda: + paddle_compatible_cuda_devices.append(d) + except Exception as e: + print(f"Skipping Paddle tests on device '{d}' due to exception: {e}") + + add_function_test(TestPaddle, "test_dtype_from_paddle", test_dtype_from_paddle, devices=None) + add_function_test(TestPaddle, "test_dtype_to_paddle", test_dtype_to_paddle, devices=None) + + if paddle_compatible_devices: + add_function_test( + TestPaddle, "test_device_conversion", test_device_conversion, devices=paddle_compatible_devices + ) + add_function_test(TestPaddle, "test_from_paddle", test_from_paddle, devices=paddle_compatible_devices) + add_function_test( + TestPaddle, "test_from_paddle_slices", test_from_paddle_slices, devices=paddle_compatible_devices + ) + add_function_test( + TestPaddle, "test_array_ctype_from_paddle", test_array_ctype_from_paddle, devices=paddle_compatible_devices + ) + add_function_test( + TestPaddle, + "test_from_paddle_zero_strides", + test_from_paddle_zero_strides, + devices=paddle_compatible_devices, + ) + add_function_test(TestPaddle, "test_to_paddle", test_to_paddle, devices=paddle_compatible_devices) + add_function_test(TestPaddle, "test_paddle_zerocopy", test_paddle_zerocopy, devices=paddle_compatible_devices) + add_function_test(TestPaddle, "test_paddle_autograd", test_paddle_autograd, devices=paddle_compatible_devices) + add_function_test(TestPaddle, "test_direct", test_direct, devices=paddle_compatible_devices) + + # NOTE: Graph not supported now + # if paddle_compatible_cuda_devices: + # add_function_test( + # TestPaddle, + # "test_warp_graph_warp_stream", + # test_warp_graph_warp_stream, + # devices=paddle_compatible_cuda_devices, + # ) + # add_function_test( + # TestPaddle, + # "test_warp_graph_paddle_stream", + # test_warp_graph_paddle_stream, + # devices=paddle_compatible_cuda_devices, + # ) + + # multi-GPU tests + if len(paddle_compatible_cuda_devices) > 1: + add_function_test(TestPaddle, "test_paddle_mgpu_from_paddle", test_paddle_mgpu_from_paddle) + add_function_test(TestPaddle, "test_paddle_mgpu_to_paddle", test_paddle_mgpu_to_paddle) + add_function_test(TestPaddle, "test_paddle_mgpu_interop", test_paddle_mgpu_interop) + +except Exception as e: + print(f"Skipping Paddle tests due to exception: {e}") + + +if __name__ == "__main__": + wp.clear_kernel_cache() + unittest.main(verbosity=2) diff --git a/warp/tests/test_static.py b/warp/tests/test_static.py new file mode 100644 index 00000000..d816af4f --- /dev/null +++ b/warp/tests/test_static.py @@ -0,0 +1,412 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +import unittest +from typing import Dict, List + +import numpy as np + +import warp +import warp as wp +from warp.tests.unittest_utils import * + +global_variable = 3 + + +@wp.func +def static_global_variable_func(): + static_var = warp.static(global_variable + 2) + return static_var + + +@wp.kernel +def static_global_variable_kernel(results: wp.array(dtype=int)): + # evaluate a constant expression at codegen time + static_var = static_global_variable_func() + const_var = 3 + # call a function at codegen time + static_func_result = wp.static(static_global_variable_func() + const_var) + results[0] = static_var + results[1] = static_func_result + + +@wp.struct +class StaticallyConstructableStruct: + mat: wp.mat33 + vec: wp.vec3 + i: int + + +@wp.struct +class StaticallyConstructableNestedStruct: + s: StaticallyConstructableStruct + tf: wp.transform + quat: wp.quat + + +@wp.func +def construct_struct(mat: wp.mat33, vec: wp.vec3, i: int): + s = StaticallyConstructableStruct() + s.mat = mat + s.vec = vec + s.i = i + return s + + +@wp.func +def construct_nested_struct(mat: wp.mat33, vec: wp.vec3, i: int, tf: wp.transform, quat: wp.quat): + n = StaticallyConstructableNestedStruct() + n.s = construct_struct(mat, vec, i) + n.tf = tf + n.quat = quat + return n + + +@wp.kernel +def construct_static_struct_kernel(results: wp.array(dtype=StaticallyConstructableStruct)): + static_struct = wp.static( + construct_struct( + wp.mat33(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0), + wp.vec3(1.0, 2.0, 3.0), + 1, + ) + ) + results[0] = static_struct + + +@wp.kernel +def construct_static_nested_struct_kernel(results: wp.array(dtype=StaticallyConstructableNestedStruct)): + static_struct = wp.static( + construct_nested_struct( + wp.mat33(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0), + wp.vec3(1.0, 2.0, 3.0), + 1, + wp.transform(wp.vec3(1.0, 2.0, 3.0), wp.quat_from_axis_angle(wp.vec3(0.0, 1.0, 0.0), wp.pi / 2.0)), + wp.quat_from_axis_angle(wp.normalize(wp.vec3(1.0, 2.0, 3.0)), wp.pi / 2.0), + ) + ) + results[0] = static_struct + + +def test_static_global_variable(test, device): + results = wp.zeros(2, dtype=int, device=device) + wp.launch(static_global_variable_kernel, 1, [results], device=device) + assert_np_equal(results.numpy(), np.array([5, 8], dtype=int)) + + +def test_construct_static_struct(test, device): + results = wp.zeros(1, dtype=StaticallyConstructableStruct, device=device) + wp.launch(construct_static_struct_kernel, 1, [results], device=device) + results = results.numpy() + assert_np_equal(results[0][0], np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])) + assert_np_equal(results[0][1], np.array([1.0, 2.0, 3.0])) + assert_np_equal(results[0][2], 1) + + +def test_construct_static_nested_struct(test, device): + results = wp.zeros(1, dtype=StaticallyConstructableNestedStruct, device=device) + wp.launch(construct_static_nested_struct_kernel, 1, [results], device=device) + results = results.numpy() + + tf = wp.transform(wp.vec3(1.0, 2.0, 3.0), wp.quat_from_axis_angle(wp.vec3(0.0, 1.0, 0.0), wp.pi / 2.0)) + quat = wp.quat_from_axis_angle(wp.normalize(wp.vec3(1.0, 2.0, 3.0)), wp.pi / 2.0) + + assert_np_equal(results[0][0][0], np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])) + assert_np_equal(results[0][0][1], np.array([1.0, 2.0, 3.0])) + assert_np_equal(results[0][0][2], 1) + assert_np_equal(results[0][1], np.array(tf)) + assert_np_equal(results[0][2], np.array(quat)) + + +def test_invalid_static_expression(test, device): + @wp.kernel + def invalid_kernel(): + wp.static(1.0 / 0.0) + + with test.assertRaisesRegex( + warp.codegen.WarpCodegenError, r"Error evaluating static expression\: float division by zero" + ): + wp.launch(invalid_kernel, 1, device=device) + + @wp.kernel + def invalid_kernel(i: int): + wp.static(i * 2) + + with test.assertRaisesRegex( + wp.codegen.WarpCodegenError, + r"Error evaluating static expression\: name 'i' is not defined\. Make sure all variables used in the static expression are constant\.", + ): + wp.launch(invalid_kernel, 1, device=device, inputs=[3]) + + +def test_static_expression_return_types(test, device): + @wp.kernel + def invalid_kernel(): + wp.static(wp.zeros(3, device=device)) + + with test.assertRaisesRegex( + warp.codegen.WarpCodegenError, + r"Static expression returns an unsupported value\: a Warp array cannot be created inside Warp kernels", + ): + wp.launch(invalid_kernel, 1, device=device) + + @wp.struct + class Baz: + data: wp.array(dtype=int) + z: wp.vec3 + + @wp.struct + class Bar: + baz: Baz + y: float + + @wp.struct + class Foo: + bar: Bar + x: int + + def create_struct(): + foo = Foo() + foo.bar = Bar() + foo.bar.baz = Baz() + foo.bar.baz.data = wp.zeros(3, dtype=int, device=device) + foo.bar.baz.z = wp.vec3(1, 2, 3) + foo.bar.y = 1.23 + foo.x = 123 + return foo + + @wp.kernel + def invalid_kernel(): + wp.static(create_struct()) + + with test.assertRaisesRegex( + warp.codegen.WarpCodegenError, + r"Static expression returns an unsupported value: the returned Warp struct contains a data type that cannot be constructed inside Warp kernels\: a Warp array cannot be created inside Warp kernels at .*?Foo\.bar\.baz", + ): + wp.launch(invalid_kernel, 1, device=device) + + def function_with_no_return_value(): + pass + + @wp.kernel + def invalid_kernel(): + wp.static(function_with_no_return_value()) + + with test.assertRaisesRegex( + warp.codegen.WarpCodegenError, + r"Static expression returns an unsupported value\: None is returned", + ): + wp.launch(invalid_kernel, 1, device=device) + + class MyClass: + pass + + @wp.kernel + def invalid_kernel(): + wp.static(MyClass()) + + with test.assertRaisesRegex( + warp.codegen.WarpCodegenError, + r"Static expression returns an unsupported value\: value of type .*?MyClass", + ): + wp.launch(invalid_kernel, 1, device=device) + + +def test_function_variable(test, device): + # create a function and pass it in as a static variable to the kernel + @wp.func + def func1(a: int, b: int): + return a + b + + @wp.func + def func2(a: int, b: int): + return a - b + + for func in [func1, func2]: + # note that this example also works without using wp.static() + + @wp.kernel + def function_variable_kernel(results: wp.array(dtype=int)): + results[0] = wp.static(func)(3, 2) # noqa: B023 + + results = wp.zeros(1, dtype=int, device=device) + # note that the kernel has to be recompiled everytime the value of func changes + wp.launch(function_variable_kernel, 1, [results], device=device) + assert_np_equal(results.numpy(), np.array([func(3, 2)], dtype=int)) + + +def test_function_lookup(test, device): + @wp.func + def do_add(a: float, b: float): + return a + b + + @wp.func + def do_sub(a: float, b: float): + return a - b + + @wp.func + def do_mul(a: float, b: float): + return a * b + + op_handlers = { + "add": do_add, + "sub": do_sub, + "mul": do_mul, + } + + inputs = wp.array([[1, 2], [3, 0]], dtype=wp.float32) + + outputs = wp.empty(2, dtype=wp.float32) + + for op in op_handlers.keys(): + + @wp.kernel + def operate(input: wp.array(dtype=inputs.dtype, ndim=2), output: wp.array(dtype=wp.float32)): + tid = wp.tid() + a, b = input[tid, 0], input[tid, 1] + # retrieve the right function to use for the captured dtype variable + output[tid] = wp.static(op_handlers[op])(a, b) # noqa: B023 + + wp.launch(operate, dim=2, inputs=[inputs], outputs=[outputs]) + outputs_np = outputs.numpy() + inputs_np = inputs.numpy() + for i in range(len(outputs_np)): + test.assertEqual(outputs_np[i], op_handlers[op](float(inputs_np[i][0]), float(inputs_np[i][1]))) + + +def count_ssa_occurrences(kernel: wp.Kernel, ssas: List[str]) -> Dict[str, int]: + # analyze the generated code + counts = {ssa: 0 for ssa in ssas} + for line in kernel.adj.blocks[0].body_forward: + for ssa in ssas: + if ssa in line: + counts[ssa] += 1 + return counts + + +def test_static_for_loop(test, device): + @wp.kernel + def static_loop_variable(results: wp.array(dtype=int)): + s = 0 + for i in range(wp.static(static_global_variable_func())): + s += wp.static(i) + results[0] = s + + wp.set_module_options( + options={"max_unroll": static_global_variable_func()}, + ) + + results = wp.zeros(1, dtype=int, device=device) + wp.launch(static_loop_variable, 1, [results], device=device) + results = results.numpy() + + s = 0 + for i in range(wp.static(static_global_variable_func())): + s += wp.static(i) + + test.assertEqual(results[0], s, "Static for loop has to compute the correct solution") + + # analyze the generated code + if hasattr(static_loop_variable.adj, "blocks"): + counts = count_ssa_occurrences(static_loop_variable, ["add", "for"]) + + test.assertEqual(counts["add"], static_global_variable_func(), "Static for loop must be unrolled") + # there is just one occurrence of "for" in the comment referring to the original Python code + test.assertEqual(counts["for"], 1, "Static for loop must be unrolled") + + +def test_static_if_else_elif(test, device): + @wp.kernel + def static_condition1(results: wp.array(dtype=int)): + if wp.static(static_global_variable_func() in {2, 3, 5}): + results[0] = 1 + elif wp.static(static_global_variable_func() in {0, 1}): + results[0] = 2 + else: + results[0] = 3 + + results = wp.zeros(1, dtype=int, device=device) + wp.launch(static_condition1, 1, [results], device=device) + results = results.numpy() + assert_np_equal(results[0], 1) + # TODO this needs fixing to ensure we can run these tests multiple times + if hasattr(static_condition1.adj, "blocks"): + counts = count_ssa_occurrences(static_condition1, ["if", "else"]) + + # if, else, elif can appear as comments but the generated code must not contain + # such keywords since the conditions are resolved at the time of code generation + assert_np_equal(counts["if"], 1) + assert_np_equal(counts["else"], 0) + + captured_var = "hello" + + @wp.kernel + def static_condition2(results: wp.array(dtype=int)): + if wp.static(captured_var == "world"): + results[0] = 1 + else: + results[0] = 2 + + results = wp.zeros(1, dtype=int, device=device) + wp.launch(static_condition2, 1, [results], device=device) + results = results.numpy() + assert_np_equal(results[0], 2) + if hasattr(static_condition2.adj, "blocks"): + counts = count_ssa_occurrences(static_condition2, ["if", "else"]) + assert_np_equal(counts["if"], 1) + assert_np_equal(counts["else"], 0) + + my_list = [1, 2, 3] + + @wp.kernel + def static_condition3(results: wp.array(dtype=int)): + if wp.static(len(my_list) == 0): + results[0] = 0 + elif wp.static(len(my_list) == 1): + results[0] = 1 + elif wp.static(len(my_list) == 2): + results[0] = 2 + elif wp.static(len(my_list) == 3): + results[0] = 3 + + results = wp.zeros(1, dtype=int, device=device) + wp.launch(static_condition3, 1, [results], device=device) + results = results.numpy() + assert_np_equal(results[0], 3) + if hasattr(static_condition3.adj, "blocks"): + counts = count_ssa_occurrences(static_condition3, ["if", "else"]) + assert_np_equal(counts["if"], 4) + assert_np_equal(counts["else"], 0) + + +devices = get_test_devices() + + +class TestStatic(unittest.TestCase): + def test_static_python_call(self): + # ensure wp.static() works from a Python context + self.assertEqual(static_global_variable_func(), 5) + + +add_function_test(TestStatic, "test_static_global_variable", test_static_global_variable, devices=devices) +add_function_test(TestStatic, "test_construct_static_struct", test_construct_static_struct, devices=devices) +add_function_test( + TestStatic, "test_construct_static_nested_struct", test_construct_static_nested_struct, devices=devices +) +add_function_test(TestStatic, "test_function_variable", test_function_variable, devices=devices) +add_function_test(TestStatic, "test_function_lookup", test_function_lookup, devices=devices) +add_function_test(TestStatic, "test_invalid_static_expression", test_invalid_static_expression, devices=devices) +add_function_test( + TestStatic, "test_static_expression_return_types", test_static_expression_return_types, devices=devices +) +add_function_test(TestStatic, "test_static_for_loop", test_static_for_loop, devices=devices) +add_function_test(TestStatic, "test_static_if_else_elif", test_static_if_else_elif, devices=devices) + + +if __name__ == "__main__": + wp.clear_kernel_cache() + unittest.main(verbosity=2) diff --git a/warp/tests/test_tile.py b/warp/tests/test_tile.py index 8b1d3157..51cf7307 100644 --- a/warp/tests/test_tile.py +++ b/warp/tests/test_tile.py @@ -21,6 +21,7 @@ # num threads per-tile TILE_DIM = 64 + @wp.kernel def tile_copy_1d_kernel(A: wp.array(dtype=float), B: wp.array(dtype=float)): # tile index @@ -59,6 +60,7 @@ def test_tile_copy_1d(test, device): assert_array_equal(B_wp.grad, A_wp.grad) + @wp.kernel def tile_copy_2d_kernel(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float)): # tile index @@ -450,18 +452,18 @@ def test_tile_transpose(test, device): assert_np_equal(output.numpy(), input.numpy().T) -@wp.kernel -def test_tile_transpose_matmul_kernel(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): - x = wp.tile_load(input, 0, 0, m=TILE_M, n=TILE_N) - y = wp.tile_transpose(x) - - z = wp.tile_zeros(dtype=float, m=TILE_N, n=TILE_N) - wp.tile_matmul(y, x, z) +@unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") +def test_tile_transpose_matmul(test, device): + @wp.kernel + def test_tile_transpose_matmul_kernel(input: wp.array2d(dtype=float), output: wp.array2d(dtype=float)): + x = wp.tile_load(input, 0, 0, m=TILE_M, n=TILE_N) + y = wp.tile_transpose(x) - wp.tile_store(output, 0, 0, z) + z = wp.tile_zeros(dtype=float, m=TILE_N, n=TILE_N) + wp.tile_matmul(y, x, z) + wp.tile_store(output, 0, 0, z) -def test_tile_transpose_matmul(test, device): rng = np.random.default_rng(42) input = wp.array(rng.random((TILE_M, TILE_N), dtype=np.float32), device=device) output = wp.zeros((TILE_N, TILE_N), dtype=float, device=device) @@ -473,57 +475,53 @@ def test_tile_transpose_matmul(test, device): @wp.kernel def test_tile_broadcast_add_kernel( - input_a: wp.array2d(dtype=float), - input_b: wp.array(dtype=float), - output: wp.array2d(dtype=float)): - + input_a: wp.array2d(dtype=float), input_b: wp.array(dtype=float), output: wp.array2d(dtype=float) +): a = wp.tile_load(input_a, 0, 0, m=10, n=10) b = wp.tile_load(input_b, 0, n=10) c = wp.tile_broadcast(b, 10, 10) d = a + c - wp.tile_store(output, 0, 0, d) + wp.tile_store(output, 0, 0, d) -def test_tile_broadcast_add(test, device): +def test_tile_broadcast_add(test, device): M = 10 N = 10 - - a = wp.array(np.ones((M,N), dtype=np.float32), device=device) + + a = wp.array(np.ones((M, N), dtype=np.float32), device=device) b = wp.array(np.arange(0, N, dtype=np.float32), device=device) - out = wp.zeros((M,N), dtype=float, device=device) + out = wp.zeros((M, N), dtype=float, device=device) + + wp.launch_tiled(test_tile_broadcast_add_kernel, dim=[1], inputs=[a, b, out], block_dim=32, device=device) - wp.launch_tiled(test_tile_broadcast_add_kernel, dim=[1], inputs=[a, b, out], block_dim=32) - assert_np_equal(out.numpy(), a.numpy() + b.numpy()) @wp.kernel -def test_tile_broadcast_grad_kernel( - a: wp.array(dtype=float), - b: wp.array2d(dtype=float)): - +def test_tile_broadcast_grad_kernel(a: wp.array(dtype=float), b: wp.array2d(dtype=float)): x = wp.tile_load(a, i=0, n=5) y = wp.tile_broadcast(x, m=5, n=5) w = wp.tile_ones(dtype=float, m=5, n=5) z = w + y - + wp.tile_store(b, 0, 0, z) + def test_tile_broadcast_grad(test, device): - - a = wp.array(np.arange(0, 5, dtype=np.float32), requires_grad=True) - b = wp.array(np.ones((5, 5), dtype=np.float32), requires_grad=True) + a = wp.array(np.arange(0, 5, dtype=np.float32), requires_grad=True, device=device) + b = wp.array(np.ones((5, 5), dtype=np.float32), requires_grad=True, device=device) - with wp.Tape() as tape: - wp.launch_tiled(test_tile_broadcast_grad_kernel, dim=[1], inputs=[a, b], block_dim=32) + with wp.Tape() as tape: + wp.launch_tiled(test_tile_broadcast_grad_kernel, dim=[1], inputs=[a, b], block_dim=32, device=device) - b.grad = wp.ones_like(b) + b.grad = wp.ones_like(b, device=device) tape.backward() - assert_np_equal(a.grad.numpy(), np.ones(5)*5.0) + assert_np_equal(a.grad.numpy(), np.ones(5) * 5.0) + # #----------------------------------------- # # center of mass computation @@ -615,9 +613,9 @@ class TestTile(unittest.TestCase): add_function_test(TestTile, "test_tile_copy_2d", test_tile_copy_2d, devices=devices) add_function_test(TestTile, "test_tile_unary_map", test_tile_unary_map, devices=devices) add_function_test(TestTile, "test_tile_binary_map", test_tile_binary_map, devices=devices) -add_function_test(TestTile, "test_tile_grouped_gemm", test_tile_grouped_gemm, devices=devices) +add_function_test(TestTile, "test_tile_grouped_gemm", test_tile_grouped_gemm, devices=devices) add_function_test(TestTile, "test_tile_gemm", test_tile_gemm, devices=devices) -add_function_test(TestTile, "test_tile_transpose", test_tile_transpose, devices=devices) +add_function_test(TestTile, "test_tile_transpose", test_tile_transpose, devices=devices) add_function_test(TestTile, "test_tile_transpose_matmul", test_tile_transpose_matmul, devices=devices) add_function_test(TestTile, "test_tile_operators", test_tile_operators, devices=devices) add_function_test(TestTile, "test_tile_sum", test_tile_sum, devices=devices) diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index 5e48b62f..fdf59259 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -280,23 +280,19 @@ def test_tile_untile_scalar(test, device): assert_np_equal(output.numpy(), np.arange(N) * 2) - @wp.kernel -def test_untile_vector_kernel( - input: wp.array(dtype=wp.vec3), - output: wp.array(dtype=wp.vec3)): - +def test_untile_vector_kernel(input: wp.array(dtype=wp.vec3), output: wp.array(dtype=wp.vec3)): i = wp.tid() - v = input[i]*0.5 + v = input[i] * 0.5 t = wp.tile(v) u = wp.untile(t) - output[i] = u*2.0 + output[i] = u * 2.0 -def test_tile_untile_vector(test, device): +def test_tile_untile_vector(test, device): input = wp.full(16, wp.vec3(1.0, 2.0, 3.0), requires_grad=True) output = wp.zeros_like(input) @@ -321,7 +317,6 @@ def tile_ones_kernel(out: wp.array(dtype=float)): def test_tile_ones(test, device): - output = wp.zeros(1, dtype=float, device=device) with wp.Tape() as tape: diff --git a/warp/tests/test_torch.py b/warp/tests/test_torch.py index 1a4bd0b1..40c77899 100644 --- a/warp/tests/test_torch.py +++ b/warp/tests/test_torch.py @@ -382,6 +382,27 @@ def wrap_vec_tensor_with_warp_grad(vec_dtype): wrap_vec_tensor_with_warp_grad(wp.transform) +def test_cuda_array_interface(test, device): + # We should be able to construct Torch tensors from Warp arrays via __cuda_array_interface__ on GPU. + # Note that Torch does not support __array_interface__ on CPU. + + torch_device = wp.device_to_torch(device) + n = 10 + + # test the types supported by both Warp and Torch + scalar_types = [wp.float16, wp.float32, wp.float64, wp.int8, wp.int16, wp.int32, wp.int64, wp.uint8] + + for dtype in scalar_types: + # test round trip + a1 = wp.zeros(n, dtype=dtype, device=device) + t = torch.tensor(a1, device=torch_device) + a2 = wp.array(t, device=device) + + assert a1.dtype == a2.dtype + assert a1.shape == a2.shape + assert a1.strides == a2.strides + + def test_to_torch(test, device): import torch @@ -918,6 +939,9 @@ class TestTorch(unittest.TestCase): test_warp_graph_torch_stream, devices=torch_compatible_cuda_devices, ) + add_function_test( + TestTorch, "test_cuda_array_interface", test_cuda_array_interface, devices=torch_compatible_cuda_devices + ) # multi-GPU tests if len(torch_compatible_cuda_devices) > 1: diff --git a/warp/tests/test_types.py b/warp/tests/test_types.py index 51f5f99b..bf859d1b 100644 --- a/warp/tests/test_types.py +++ b/warp/tests/test_types.py @@ -215,7 +215,7 @@ def test_constant(self): self.assertEqual(const, wp.vec3i(1, 2, 3)) def test_constant_error_invalid_type(self): - with self.assertRaisesRegex(RuntimeError, r"Invalid constant type: $"): + with self.assertRaisesRegex(TypeError, r"Invalid constant type: $"): wp.constant((1, 2, 3)) def test_vector_assign(self): diff --git a/warp/thirdparty/dlpack.py b/warp/thirdparty/dlpack.py index 0634474b..399e0002 100644 --- a/warp/thirdparty/dlpack.py +++ b/warp/thirdparty/dlpack.py @@ -58,6 +58,7 @@ class DLDataTypeCode(ctypes.c_uint8): kDLOpaquePointer = 3 kDLBfloat = 4 kDLComplex = 5 + kDLBool = 6 def __str__(self): return { @@ -66,6 +67,7 @@ def __str__(self): self.kDLFloat: "float", self.kDLBfloat: "bfloat", self.kDLComplex: "complex", + self.kDLBool: "bool", self.kDLOpaquePointer: "void_p", }[self.value] @@ -85,7 +87,7 @@ class DLDataType(ctypes.Structure): ("lanes", ctypes.c_uint16), ] TYPE_MAP = { - "bool": (DLDataTypeCode.kDLUInt, 1, 1), + "bool": (DLDataTypeCode.kDLBool, 8, 1), "int8": (DLDataTypeCode.kDLInt, 8, 1), "int16": (DLDataTypeCode.kDLInt, 16, 1), "int32": (DLDataTypeCode.kDLInt, 32, 1), diff --git a/warp/types.py b/warp/types.py index 9a0aa8a0..94fee051 100644 --- a/warp/types.py +++ b/warp/types.py @@ -66,8 +66,8 @@ def constant(x): x: Compile-time constant value, can be any of the built-in math types. """ - if not isinstance(x, (builtins.bool, int, float, tuple(scalar_and_bool_types), ctypes.Array)): - raise RuntimeError(f"Invalid constant type: {type(x)}") + if not is_value(x): + raise TypeError(f"Invalid constant type: {type(x)}") return x @@ -1302,7 +1302,7 @@ def type_to_warp(dtype): def type_typestr(dtype): if dtype == bool: - return "?" + return "|b1" elif dtype == float16: return "()" else: - if self.owner == False: + if not self.owner: # will be initialized by subsequent call, e.g.: t = tile_broadcast(a) return "NULL" else: From 6d8d54289abe142db87aa6a95e259c5ca3285b3c Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 1 Oct 2024 14:27:16 -0700 Subject: [PATCH 055/102] Use intended device in test_tile_untile_vector --- warp/tests/test_tile_reduce.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/warp/tests/test_tile_reduce.py b/warp/tests/test_tile_reduce.py index fdf59259..bf8650e8 100644 --- a/warp/tests/test_tile_reduce.py +++ b/warp/tests/test_tile_reduce.py @@ -293,13 +293,13 @@ def test_untile_vector_kernel(input: wp.array(dtype=wp.vec3), output: wp.array(d def test_tile_untile_vector(test, device): - input = wp.full(16, wp.vec3(1.0, 2.0, 3.0), requires_grad=True) - output = wp.zeros_like(input) + input = wp.full(16, wp.vec3(1.0, 2.0, 3.0), requires_grad=True, device=device) + output = wp.zeros_like(input, device=device) with wp.Tape() as tape: - wp.launch(test_untile_vector_kernel, dim=16, inputs=[input, output], block_dim=16) + wp.launch(test_untile_vector_kernel, dim=16, inputs=[input, output], block_dim=16, device=device) - output.grad = wp.ones_like(output) + output.grad = wp.ones_like(output, device=device) tape.backward() assert_np_equal(output.numpy(), input.numpy()) From bddf3d25bee75d3c803fb3aca42d43f769dd107c Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 1 Oct 2024 21:33:36 +0000 Subject: [PATCH 056/102] Tile documentation fixes --- docs/modules/tiles.rst | 127 +++++++++++++++++++++++++++++------------ 1 file changed, 90 insertions(+), 37 deletions(-) diff --git a/docs/modules/tiles.rst b/docs/modules/tiles.rst index 48d2c788..27706423 100644 --- a/docs/modules/tiles.rst +++ b/docs/modules/tiles.rst @@ -10,33 +10,10 @@ Warp 1.4.0 introduces tile extensions that expose a block-based programming to W Execution Model --------------- -Warp's execution model allows users to specify an up to 4-dimensional grid of logical threads for kernel execution at launch time. With the introduction of tile primitives, users can additionally specify a block size, which partitions the thread grid into smaller sets of threads that are executed on a single compute unit. +Warp's execution model allows users to specify an up to 4-dimensional grid of logical threads for kernel execution at launch time. With the introduction of tile primitives, users can now specify the block size for kernel launches, which partitions the thread grid into smaller sets of threads that are executed on a single compute unit. Inside kernels, tile operations are executed cooperatively across each block of threads, allowing them to take advantage of efficient memory access, local memory, and dedicated hardware units like TensorCores. -As an example, consider the following kernel: - -.. code:: python - - TILE_SIZE = wp.constant(256) - TILE_THREADS = 64 - - @wp.kernel - def compute(a: array(dtype=float)) - i = wp.tid()/TILE_SIZE - - t = wp.tile_load(array, i, TILE_SIZE) - ... - - wp.launch(compute, dim=[len(a)], inputs=[a], block_dim=TILE_THREADS) - -Here, each block loads a 1D tile of 256 values from a global memory array ``a``, where the load operation is performed cooperatively by all 64 threads in the block, as specified by the ``block_dim`` argument to :func:`warp.launch`. In this case, each thread is responsible for loading 4 values from global memory, which may then be stored in registers, or shared memory across the block. - -Tile Properties ---------------- - -In Warp, tile objects are 2D arrays of data where the tile elements may be scalars, vectors, matrices, or user defined structures. - In the following example, we launch a grid of threads where each block is responsible for loading a row of data from a 2D array and computing its sum: .. code:: python @@ -46,18 +23,24 @@ In the following example, we launch a grid of threads where each block is respon @wp.kernel def compute(a: array2d(dtype=float)) - i, _ = wp.tid() + + # obtain our block index + i = wp.tid() # load a row from global memory - t = wp.tile_load(array, i, 0, 1, TILE_SIZE) + t = wp.tile_load(array[i], i, TILE_SIZE) s = wp.sum(t) ... - wp.launch(compute, dim=[a.shape[0], TILE_THREADS], inputs=[a], block_dim=TILE_THREADS) + wp.launch_tiled(compute, dim=[a.shape[0]], inputs=[a], block_dim=TILE_THREADS) -Here, we launch a 2D grid of threads where the trailing dimension is equal to the block size. This ensures we have an entire block of threads dedicated to each row. Each block then loads an entire row of 256 values from the global memory array and computes its sum. +Here, we have used the new :func:`warp.launch_tiled` function which assigns ``TILE_THREADS`` to each of the elements in the launch grid. Each block then loads an entire row of 256 values from the global memory array, computes its sum (cooperatively), and then stores the result back to global memory. + + +Tile Properties +--------------- -To streamline this common pattern Warp provides a helper ``wp.tiled_launch()`` which takes care of adding the trailing tile dimension to the thread grid, for example, to assign a block of 64 threads to load and sum a 2D array of values we can do the following: +In Warp, tile objects are 2D arrays of data where the tile elements may be scalars, vectors, matrices, or user defined structures. We can load 2D tiles directly from 2D global memory arrays as follows: .. code:: python @@ -67,16 +50,18 @@ To streamline this common pattern Warp provides a helper ``wp.tiled_launch()`` w @wp.kernel def compute(a: array2d(dtype=float)) + + # obtain our 2d block index i, j = wp.tid() - # load a row from global memory + # load a 2d tile from global memory t = wp.tile_load(array, i, j, TILE_M, TILE_N) s = wp.sum(t) ... wp.launch_tiled(compute, dim=[a.shape[0]/TILE_M, a.shape[1]/TILE_N], inputs=[a], block_dim=TILE_THREADS) -In this example, we use :func:`warp.launch_tiled` to automatically insert the trailing dimension, and assign ``TILE_THREADS`` to each 2D tile of the array. Each tile consists of ``16*16=256`` values, which are loaded cooperatively by the 64 threads in each block. +Here we divide the array ``a`` into 2d tiles of shape 16x16, each block cooperatively loads tile from the input array and computes its sum before returning the result. Tile Storage ------------ @@ -86,16 +71,86 @@ When tiles are created they are placed in either `register` or `shared` memory. Register Tiles ++++++++++++++ -Values in register tiles are stored across the entire block, for example, if the block dimension at launch is set to 64, a register tile with ``shape=(1, 256)`` will result in each thread storing 4 elements. Reigster based storage is the fastest storage on most hardware, however, because the tile storage is spread across the threads in the block, an individual thread cannot randomly access data that is assigned to another thread efficiently. For this reason operations on tiles tend to expressed as higher level maps, reductions, and reshaping operations that may transfer values through shared memory. +Values in register tiles are stored across the entire block, for example, if the block dimension at launch is set to 64, a register tile with ``shape=(1, 256)`` will result in each thread storing 4 elements. Register based storage is the fastest storage on most hardware, however, because the tile storage is spread across the threads in the block, an individual thread cannot randomly access data that is assigned to another thread efficiently. For this reason operations on tiles tend to expressed as higher level maps, reductions, and reshaping operations that may transfer values through shared memory. Shared Memory Tiles +++++++++++++++++++ -Some operations like matrix multiplication, require access to an entire tile of values. In this case the tile data may stored in shared memory, which allows efficient random access. Warp will automatically migrate tiles to shared memory as necessary for specific operations. Shared memory is a limited resource, and so tile size must be set appropriately to avoid exceeding the hardware limitations, otherwise kernel compilation may fail. +Some operations like matrix multiplication, require access to an entire tile of values. In this case the tile data may be stored in shared memory, which allows efficient random access. Warp will automatically migrate tiles to shared memory as necessary for specific operations. Shared memory is a limited resource, and so tile size must be set appropriately to avoid exceeding the hardware limitations, otherwise kernel compilation may fail. + +Example: GEMM +------------- + +.. code:: python + + import numpy as np + import warp as wp + + # tile size + TILE_M = wp.constant(8) + TILE_N = wp.constant(4) + TILE_K = wp.constant(8) + + # num threads per-tile + TILE_THREADS = 64 + + @wp.kernel + def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): + + # output tile index + i, j = wp.tid() + + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + + M = A.shape[0] + N = B.shape[1] + K = A.shape[1] + + count = int(K / TILE_K) + + for k in range(0, count): + a = wp.tile_load(A, i, k, m=TILE_M, n=TILE_K) + b = wp.tile_load(B, k, j, m=TILE_K, n=TILE_N) + + # sum += a*b + wp.tile_matmul(a, b, sum) + + wp.tile_store(C, i, j, sum) + + + + if __name__ == "__main__": + + # generate some tile aligned matrix dimensions + M = TILE_M * 7 + K = TILE_K * 6 + N = TILE_N * 5 + + rng = np.random.default_rng(42) + A = rng.random((M, K), dtype=np.float32) + B = rng.random((K, N), dtype=np.float32) + C = np.zeros((M, N), dtype=np.float32) + + A_wp = wp.array(A) + B_wp = wp.array(B) + C_wp = wp.array(C) + + with wp.Tape() as tape: + wp.launch_tiled( + tile_gemm, + dim=(int(M / TILE_M), int(N / TILE_N)), + inputs=[A_wp, B_wp, C_wp], + block_dim=TILE_THREADS) + + assert(np.allclose(C_wp.numpy(), A@B)) + + print("Example matrix multiplication passed") + Tile Operations --------------- + Construction ++++++++++++ @@ -132,9 +187,7 @@ Linear Algebra Tiles and SIMT Code ------------------- -Warp kernels are primarily written in the SIMT programming model in mind, where each thread's execution happens completely independently. Tiles on the other hand allow threads to work cooperatively to perform operations. - -Warp aims to give users a way to seamlessly integrate tile operations with existing SIMT code. To this end, we expose two operations, :func:`warp.tile`, and :func:`warp.untile` which can be used as follows: +Traditionally Warp kernels are primarily written in the SIMT programming model, where each thread's execution happens independently. Tiles on the other hand allow threads to work cooperatively to perform operations. Warp exposes :func:`warp.tile`, and :func:`warp.untile` methods to convert data between per-thread value types and the equivalent tile representation. For example: .. code:: python @@ -155,7 +208,7 @@ Warp aims to give users a way to seamlessly integrate tile operations with exist # launch as regular SIMT kernel wp.launch(compute, dim=[N], inputs=[], block_dim=TILE_THREADS) -In this example we perform some per-thread computations, and then convert the scalar ``x`` value into a tile object using the :func:`warp.tile` function. This function takes a single value as input, and returns a tile with the same dimensions as the number of threads in the block. From here, the tile can used in other regular cooperative operations such as reductions, GEMMs, etc. +In this example we have launched a regular SIMT grid using ``wp.launch()``, with ``N`` logical threads. The kernel performs some per-thread computations, and then converts the scalar ``x`` value into a tile object using the :func:`warp.tile` function. This function takes a single value as input, and returns a tile with the same dimensions as the number of threads in the block. From here, the tile can used in other regular cooperative operations such as reductions, GEMMs, etc. Similarly, we can `untile` tile objects back to their per-thread scalar equivalent values. From 68c0b68ad4e009a39c526ebe1721663e09a40708 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Thu, 3 Oct 2024 15:37:10 -0700 Subject: [PATCH 057/102] Minor adjustments to docs and docstrings --- docs/modules/functions.rst | 32 +++++++++++--------- docs/modules/runtime.rst | 2 +- docs/modules/tiles.rst | 62 +++++++++++++++++++++----------------- warp/builtins.py | 32 +++++++++++--------- warp/stubs.py | 32 +++++++++++--------- warp/types.py | 2 +- 6 files changed, 88 insertions(+), 74 deletions(-) diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 30a9fd80..8fcc6f83 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -804,29 +804,29 @@ Tile Primitives --------------- .. py:function:: tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile - Allocates a tile of zero initialized items. + Allocates a tile of zero-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements - :returns: A zero initialized tile with ``shape=(m,n)`` and the specified datatype + :returns: A zero-initialized tile with ``shape=(m,n)`` and the specified datatype .. py:function:: tile_ones(m: int32, n: int32, dtype: Scalar) -> Tile - Allocates a tile of one initialized items. + Allocates a tile of one-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements - :returns: A one initialized tile with ``shape=(m,n)`` and the specified dtype + :returns: A one-initialized tile with ``shape=(m,n)`` and the specified dtype .. py:function:: tile_arange(*args: Scalar, dtype: Scalar) -> Tile Generates a tile of linearly spaced elements. - :param args: Variable length positional arguments, interpreted as: + :param args: Variable-length positional arguments, interpreted as: - ``(stop,)``: Generates values from ``0`` to ``stop - 1`` - ``(start, stop)``: Generates values from ``start`` to ``stop - 1`` @@ -902,12 +902,12 @@ Tile Primitives .. py:function:: tile(x: Any) -> Tile - Constructs a new Tile from a per-thread kernel values. + Constructs a new Tile from per-thread kernel values. This function converts values computed using scalar kernel code to a tile representation for input into collective operations. - * If the input value is a scalar then the resulting tile has ``shape=(1, block_dim)`` - * If the input value is a vector then the resulting tile has ``shape=(length(vector), block_dim)`` + * If the input value is a scalar, then the resulting tile has ``shape=(1, block_dim)`` + * If the input value is a vector, then the resulting tile has ``shape=(length(vector), block_dim)`` :param x: A per-thread local value, e.g.: scalar, vector, or matrix. :returns: A tile with first dimension according to the value type length and a second dimension equal to ``block_dim`` @@ -940,7 +940,7 @@ Tile Primitives This function converts a block-wide tile back to per-thread values. * If the input tile is 1-dimensional then the resulting value will be a per-thread scalar - * If the input tile is 2-dimensional then the the resulting value will be a per-thread vector of length M + * If the input tile is 2-dimensional then the resulting value will be a per-thread vector of length M :param a: A tile with dimensions ``shape=(M, block_dim)`` :returns: A single value per-thread with the same dtype as the tile @@ -980,7 +980,9 @@ Tile Primitives Extracts a single element from the tile and returns it as a scalar type. - This function will extract an element from the tile and broadcast its value to all threads in the block, note that this may incur additional synchronization if the source tile is a register tile. + This function will extract an element from the tile and broadcast its value to all threads in the block. + + Note that this may incur additional synchronization if the source tile is a register tile. :param a: Tile to extract the element from :param i: Coordinate of element on first dimension @@ -1010,10 +1012,10 @@ Tile Primitives .. py:function:: tile_sum(a: Tile) -> Tile - Cooperatively compute the sum the tile elements using all threads in the block. + Cooperatively compute the sum of the tile elements using all threads in the block. :param a: The tile to compute the sum of - :returns: A single element tile with dimensions of (1,1) holding the sum + :returns: A single-element tile with dimensions of (1,1) holding the sum Example: @@ -1043,7 +1045,7 @@ Tile Primitives Cooperatively compute the minimum of the tile elements using all threads in the block. :param a: The tile to compute the minimum of - :returns: A single element tile with dimensions of (1,1) holding the minimum value + :returns: A single-element tile with dimensions of (1,1) holding the minimum value Example: @@ -1073,7 +1075,7 @@ Tile Primitives Cooperatively compute the maximum of the tile elements using all threads in the block. :param a: The tile to compute the maximum from - :returns: A single element tile with dimensions of (1,1) holding the maximum value + :returns: A single-element tile with dimensions of (1,1) holding the maximum value Example: @@ -1106,7 +1108,7 @@ Tile Primitives :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype - :returns: A single element tile with ``shape=(1,1)`` with the same datatype as the input tile. + :returns: A single-element tile with ``shape=(1,1)`` with the same datatype as the input tile. Example: diff --git a/docs/modules/runtime.rst b/docs/modules/runtime.rst index aa628608..3c96be47 100644 --- a/docs/modules/runtime.rst +++ b/docs/modules/runtime.rst @@ -819,7 +819,7 @@ To record a series of kernel launches use the :func:`wp.capture_begin() ` gets called, even if an exception occurs during capture, which would otherwise trap the stream in a capturing state. diff --git a/docs/modules/tiles.rst b/docs/modules/tiles.rst index 27706423..044bfa32 100644 --- a/docs/modules/tiles.rst +++ b/docs/modules/tiles.rst @@ -3,16 +3,16 @@ Tiles .. warning:: Tile-based operations in Warp are under preview, APIs are subject to change. -Block-based programming models such as those in OpenAI Triton have proved to be effective ways of expressing high performance kernels that can leverage cooperative operations on modern GPUs. +Block-based programming models such as those in OpenAI Triton have proved to be effective ways of expressing high-performance kernels that can leverage cooperative operations on modern GPUs. Warp 1.4.0 introduces tile extensions that expose a block-based programming to Warp kernels. Execution Model --------------- -Warp's execution model allows users to specify an up to 4-dimensional grid of logical threads for kernel execution at launch time. With the introduction of tile primitives, users can now specify the block size for kernel launches, which partitions the thread grid into smaller sets of threads that are executed on a single compute unit. +Warp's execution model allows users to specify a grid of logical threads with up to 4 dimensions for kernel execution at launch time. With the introduction of tile primitives, users can now specify the *block size* for kernel launches, which partitions the thread grid into smaller sets of threads that are executed on a single compute unit. -Inside kernels, tile operations are executed cooperatively across each block of threads, allowing them to take advantage of efficient memory access, local memory, and dedicated hardware units like TensorCores. +Inside kernels, tile operations are executed cooperatively across each block of threads, allowing them to take advantage of efficient memory access, local memory, and dedicated hardware units like `Tensor Cores `__. In the following example, we launch a grid of threads where each block is responsible for loading a row of data from a 2D array and computing its sum: @@ -34,13 +34,13 @@ In the following example, we launch a grid of threads where each block is respon wp.launch_tiled(compute, dim=[a.shape[0]], inputs=[a], block_dim=TILE_THREADS) -Here, we have used the new :func:`warp.launch_tiled` function which assigns ``TILE_THREADS`` to each of the elements in the launch grid. Each block then loads an entire row of 256 values from the global memory array, computes its sum (cooperatively), and then stores the result back to global memory. +Here, we have used the new :func:`warp.launch_tiled` function which assigns ``TILE_THREADS`` threads to each of the elements in the launch grid. Each block of ``TILE_THREADS`` threads then loads an entire row of 256 values from the global memory array and computes its sum (cooperatively). Tile Properties --------------- -In Warp, tile objects are 2D arrays of data where the tile elements may be scalars, vectors, matrices, or user defined structures. We can load 2D tiles directly from 2D global memory arrays as follows: +In Warp, tile objects are 2D arrays of data where the tile elements may be scalars, vectors, matrices, or user-defined structures. We can load 2D tiles directly from 2D global memory arrays as follows: .. code:: python @@ -55,31 +55,42 @@ In Warp, tile objects are 2D arrays of data where the tile elements may be scala i, j = wp.tid() # load a 2d tile from global memory - t = wp.tile_load(array, i, j, TILE_M, TILE_N) + t = wp.tile_load(array, i, j, m=TILE_M, n=TILE_N) s = wp.sum(t) ... wp.launch_tiled(compute, dim=[a.shape[0]/TILE_M, a.shape[1]/TILE_N], inputs=[a], block_dim=TILE_THREADS) -Here we divide the array ``a`` into 2d tiles of shape 16x16, each block cooperatively loads tile from the input array and computes its sum before returning the result. +Here, we divide the array ``a`` into 2D tiles of shape 16 x 16. +Each block cooperatively loads a tile from the input array and computes its sum. Tile Storage ------------ -When tiles are created they are placed in either `register` or `shared` memory. In general Warp tries to determine the best storage for each, by default tiles are allocated in register storage, however some operations such as matrix multiplies may migrate data from register to shared as necessary. +When tiles are created, they are placed in either *register* or *shared* memory. +In general, Warp tries to determine the best storage location for tiles. +By default, tiles are allocated in register storage, but some operations such as matrix multiplication may migrate data from register to shared as necessary. Register Tiles -++++++++++++++ +^^^^^^^^^^^^^^ -Values in register tiles are stored across the entire block, for example, if the block dimension at launch is set to 64, a register tile with ``shape=(1, 256)`` will result in each thread storing 4 elements. Register based storage is the fastest storage on most hardware, however, because the tile storage is spread across the threads in the block, an individual thread cannot randomly access data that is assigned to another thread efficiently. For this reason operations on tiles tend to expressed as higher level maps, reductions, and reshaping operations that may transfer values through shared memory. +Values in register tiles are stored across the entire block. +For example, if the block dimension at launch is set to 64, a register tile with ``shape=(1, 256)`` will result in each thread storing 4 elements. +Register-based storage is the fastest storage on most hardware, but an individual thread cannot randomly access data that is assigned to another thread efficiently +because the tile storage is spread across the threads in the block. +For this reason, operations on tiles tend to be expressed as higher-level maps, reductions, and reshaping operations that may transfer values through shared memory. Shared Memory Tiles -+++++++++++++++++++ +^^^^^^^^^^^^^^^^^^^ -Some operations like matrix multiplication, require access to an entire tile of values. In this case the tile data may be stored in shared memory, which allows efficient random access. Warp will automatically migrate tiles to shared memory as necessary for specific operations. Shared memory is a limited resource, and so tile size must be set appropriately to avoid exceeding the hardware limitations, otherwise kernel compilation may fail. +Some operations like matrix multiplication require access to an entire tile of values. +In this case, the tile data may be stored in shared memory, which allows efficient random access. +Warp will automatically migrate tiles to shared memory as necessary for specific operations. +Shared memory is a limited resource, and so the tile size must be set appropriately to avoid exceeding the hardware limitations. +Otherwise, kernel compilation may fail. -Example: GEMM -------------- +Example: General Matrix Multiply (GEMM) +--------------------------------------- .. code:: python @@ -152,7 +163,7 @@ Tile Operations Construction -++++++++++++ +^^^^^^^^^^^^ * :func:`warp.tile_zeros` * :func:`warp.tile_ones` @@ -161,14 +172,14 @@ Construction * :func:`warp.untile` Load/Store -++++++++++ +^^^^^^^^^^ * :func:`warp.tile_load` * :func:`warp.tile_store` * :func:`warp.tile_atomic_add` Maps/Reductions -+++++++++++++++ +^^^^^^^^^^^^^^^ * :func:`warp.tile_map` * :func:`warp.tile_reduce` @@ -177,7 +188,7 @@ Maps/Reductions * :func:`warp.tile_max` Linear Algebra -++++++++++++++ +^^^^^^^^^^^^^^ * :func:`warp.tile_matmul` * :func:`warp.tile_transpose` @@ -187,7 +198,7 @@ Linear Algebra Tiles and SIMT Code ------------------- -Traditionally Warp kernels are primarily written in the SIMT programming model, where each thread's execution happens independently. Tiles on the other hand allow threads to work cooperatively to perform operations. Warp exposes :func:`warp.tile`, and :func:`warp.untile` methods to convert data between per-thread value types and the equivalent tile representation. For example: +Traditionally, Warp kernels are primarily written in the SIMT programming model, where each thread's execution happens independently. Tiles, on the other hand, allow threads to work **cooperatively** to perform operations. Warp exposes the :func:`warp.tile`, and :func:`warp.untile` methods to convert data between per-thread value types and the equivalent tile representation. For example: .. code:: python @@ -208,18 +219,15 @@ Traditionally Warp kernels are primarily written in the SIMT programming model, # launch as regular SIMT kernel wp.launch(compute, dim=[N], inputs=[], block_dim=TILE_THREADS) -In this example we have launched a regular SIMT grid using ``wp.launch()``, with ``N`` logical threads. The kernel performs some per-thread computations, and then converts the scalar ``x`` value into a tile object using the :func:`warp.tile` function. This function takes a single value as input, and returns a tile with the same dimensions as the number of threads in the block. From here, the tile can used in other regular cooperative operations such as reductions, GEMMs, etc. +In this example, we have launched a regular SIMT grid with ``N`` logical threads using ``wp.launch()``. The kernel performs some per-thread computations and then converts the scalar ``x`` value into a tile object using :func:`warp.tile`. This function takes a single value as input and returns a tile with the same dimensions as the number of threads in the block. From here, the tile can be used in other regular cooperative operations such as reductions, GEMMs, etc. Similarly, we can `untile` tile objects back to their per-thread scalar equivalent values. -.. Note:: All threads in a block must execute tile operations, however code surrounding tile operations may contain arbitrary conditional logic. +.. Note:: All threads in a block must execute tile operations, but code surrounding tile operations may contain arbitrary conditional logic. Automatic Differentiation ------------------------- -Warp can automatically generate the backward version of tile-based programs, in general tile programs must obey the same rules for auto-diff as regular Warp programs, e.g.: avoiding in-place operations, etc. Please see the :ref:`differentiability` section for more details. - - - - - +Warp can automatically generate the backward version of tile-based programs. +In general, tile programs must obey the same rules for auto-diff as regular Warp programs, e.g. avoiding in-place operations, etc. +Please see the :ref:`differentiability` section for more details. diff --git a/warp/builtins.py b/warp/builtins.py index 1a940161..fa7e8a5b 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1745,12 +1745,12 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar value_func=tile_zeros_value_func, dispatch_func=tile_zeros_dispatch_func, variadic=True, - doc="""Allocates a tile of zero initialized items. + doc="""Allocates a tile of zero-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements - :returns: A zero initialized tile with ``shape=(m,n)`` and the specified datatype""", + :returns: A zero-initialized tile with ``shape=(m,n)`` and the specified datatype""", group="Tile Primitives", export=False, ) @@ -1793,12 +1793,12 @@ def tile_ones_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg value_func=tile_ones_value_func, dispatch_func=tile_ones_dispatch_func, variadic=True, - doc="""Allocates a tile of one initialized items. + doc="""Allocates a tile of one-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements - :returns: A one initialized tile with ``shape=(m,n)`` and the specified dtype""", + :returns: A one-initialized tile with ``shape=(m,n)`` and the specified dtype""", group="Tile Primitives", export=False, ) @@ -1871,7 +1871,7 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a variadic=True, doc="""Generates a tile of linearly spaced elements. - :param args: Variable length positional arguments, interpreted as: + :param args: Variable-length positional arguments, interpreted as: - ``(stop,)``: Generates values from ``0`` to ``stop - 1`` - ``(start, stop)``: Generates values from ``start`` to ``stop - 1`` @@ -2173,12 +2173,12 @@ def tile_value_func(arg_types, arg_values): input_types={"x": Any}, value_func=tile_value_func, variadic=True, - doc="""Constructs a new Tile from a per-thread kernel values. + doc="""Constructs a new Tile from per-thread kernel values. This function converts values computed using scalar kernel code to a tile representation for input into collective operations. - * If the input value is a scalar then the resulting tile has ``shape=(1, block_dim)`` - * If the input value is a vector then the resulting tile has ``shape=(length(vector), block_dim)`` + * If the input value is a scalar, then the resulting tile has ``shape=(1, block_dim)`` + * If the input value is a vector, then the resulting tile has ``shape=(length(vector), block_dim)`` :param x: A per-thread local value, e.g.: scalar, vector, or matrix. :returns: A tile with first dimension according to the value type length and a second dimension equal to ``block_dim`` @@ -2241,7 +2241,7 @@ def untile_value_func(arg_types, arg_values): This function converts a block-wide tile back to per-thread values. * If the input tile is 1-dimensional then the resulting value will be a per-thread scalar - * If the input tile is 2-dimensional then the the resulting value will be a per-thread vector of length M + * If the input tile is 2-dimensional then the resulting value will be a per-thread vector of length M :param a: A tile with dimensions ``shape=(M, block_dim)`` :returns: A single value per-thread with the same dtype as the tile @@ -2301,7 +2301,9 @@ def tile_extract_value_func(arg_types, arg_values): variadic=True, doc="""Extracts a single element from the tile and returns it as a scalar type. - This function will extract an element from the tile and broadcast its value to all threads in the block, note that this may incur additional synchronization if the source tile is a register tile. + This function will extract an element from the tile and broadcast its value to all threads in the block. + + Note that this may incur additional synchronization if the source tile is a register tile. :param a: Tile to extract the element from :param i: Coordinate of element on first dimension @@ -2496,10 +2498,10 @@ def tile_sum_value_func(arg_types, arg_values): input_types={"a": Tile}, value_func=tile_sum_value_func, variadic=True, - doc="""Cooperatively compute the sum the tile elements using all threads in the block. + doc="""Cooperatively compute the sum of the tile elements using all threads in the block. :param a: The tile to compute the sum of - :returns: A single element tile with dimensions of (1,1) holding the sum + :returns: A single-element tile with dimensions of (1,1) holding the sum Example: @@ -2551,7 +2553,7 @@ def tile_min_value_func(arg_types, arg_values): doc="""Cooperatively compute the minimum of the tile elements using all threads in the block. :param a: The tile to compute the minimum of - :returns: A single element tile with dimensions of (1,1) holding the minimum value + :returns: A single-element tile with dimensions of (1,1) holding the minimum value Example: @@ -2603,7 +2605,7 @@ def tile_max_value_func(arg_types, arg_values): doc="""Cooperatively compute the maximum of the tile elements using all threads in the block. :param a: The tile to compute the maximum from - :returns: A single element tile with dimensions of (1,1) holding the maximum value + :returns: A single-element tile with dimensions of (1,1) holding the maximum value Example: @@ -2662,7 +2664,7 @@ def tile_reduce_dispatch_func(input_types: Mapping[str, type], return_type: Any, :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype - :returns: A single element tile with ``shape=(1,1)`` with the same datatype as the input tile. + :returns: A single-element tile with ``shape=(1,1)`` with the same datatype as the input tile. Example: diff --git a/warp/stubs.py b/warp/stubs.py index 01c8234d..3b7f8823 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -895,24 +895,24 @@ def spatial_mass( @over def tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile: - """Allocates a tile of zero initialized items. + """Allocates a tile of zero-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements - :returns: A zero initialized tile with ``shape=(m,n)`` and the specified datatype + :returns: A zero-initialized tile with ``shape=(m,n)`` and the specified datatype """ ... @over def tile_ones(m: int32, n: int32, dtype: Scalar) -> Tile: - """Allocates a tile of one initialized items. + """Allocates a tile of one-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements - :returns: A one initialized tile with ``shape=(m,n)`` and the specified dtype + :returns: A one-initialized tile with ``shape=(m,n)`` and the specified dtype """ ... @@ -921,7 +921,7 @@ def tile_ones(m: int32, n: int32, dtype: Scalar) -> Tile: def tile_arange(*args: Scalar, dtype: Scalar) -> Tile: """Generates a tile of linearly spaced elements. - :param args: Variable length positional arguments, interpreted as: + :param args: Variable-length positional arguments, interpreted as: - ``(stop,)``: Generates values from ``0`` to ``stop - 1`` - ``(start, stop)``: Generates values from ``start`` to ``stop - 1`` @@ -1005,12 +1005,12 @@ def tile_atomic_add(a: Array[Any], x: int32, y: int32, t: Any) -> Tile: @over def tile(x: Any) -> Tile: - """Constructs a new Tile from a per-thread kernel values. + """Constructs a new Tile from per-thread kernel values. This function converts values computed using scalar kernel code to a tile representation for input into collective operations. - * If the input value is a scalar then the resulting tile has ``shape=(1, block_dim)`` - * If the input value is a vector then the resulting tile has ``shape=(length(vector), block_dim)`` + * If the input value is a scalar, then the resulting tile has ``shape=(1, block_dim)`` + * If the input value is a vector, then the resulting tile has ``shape=(length(vector), block_dim)`` :param x: A per-thread local value, e.g.: scalar, vector, or matrix. :returns: A tile with first dimension according to the value type length and a second dimension equal to ``block_dim`` @@ -1046,7 +1046,7 @@ def untile(a: Any) -> Scalar: This function converts a block-wide tile back to per-thread values. * If the input tile is 1-dimensional then the resulting value will be a per-thread scalar - * If the input tile is 2-dimensional then the the resulting value will be a per-thread vector of length M + * If the input tile is 2-dimensional then the resulting value will be a per-thread vector of length M :param a: A tile with dimensions ``shape=(M, block_dim)`` :returns: A single value per-thread with the same dtype as the tile @@ -1089,7 +1089,9 @@ def compute(): def tile_extract(a: Tile, i: int32, j: int32) -> Scalar: """Extracts a single element from the tile and returns it as a scalar type. - This function will extract an element from the tile and broadcast its value to all threads in the block, note that this may incur additional synchronization if the source tile is a register tile. + This function will extract an element from the tile and broadcast its value to all threads in the block. + + Note that this may incur additional synchronization if the source tile is a register tile. :param a: Tile to extract the element from :param i: Coordinate of element on first dimension @@ -1125,10 +1127,10 @@ def tile_broadcast(a: Tile, m: int32, n: int32) -> Tile: @over def tile_sum(a: Tile) -> Tile: - """Cooperatively compute the sum the tile elements using all threads in the block. + """Cooperatively compute the sum of the tile elements using all threads in the block. :param a: The tile to compute the sum of - :returns: A single element tile with dimensions of (1,1) holding the sum + :returns: A single-element tile with dimensions of (1,1) holding the sum Example: @@ -1160,7 +1162,7 @@ def tile_min(a: Tile) -> Tile: """Cooperatively compute the minimum of the tile elements using all threads in the block. :param a: The tile to compute the minimum of - :returns: A single element tile with dimensions of (1,1) holding the minimum value + :returns: A single-element tile with dimensions of (1,1) holding the minimum value Example: @@ -1192,7 +1194,7 @@ def tile_max(a: Tile) -> Tile: """Cooperatively compute the maximum of the tile elements using all threads in the block. :param a: The tile to compute the maximum from - :returns: A single element tile with dimensions of (1,1) holding the maximum value + :returns: A single-element tile with dimensions of (1,1) holding the maximum value Example: @@ -1227,7 +1229,7 @@ def tile_reduce(op: Callable, a: Any) -> Tile: :param op: A callable function that accepts two arguments and returns one argument, may be a user function or builtin :param a: The input tile, the operator (or one of its overloads) must be able to accept the tile's dtype - :returns: A single element tile with ``shape=(1,1)`` with the same datatype as the input tile. + :returns: A single-element tile with ``shape=(1,1)`` with the same datatype as the input tile. Example: diff --git a/warp/types.py b/warp/types.py index 94fee051..454f7cc0 100644 --- a/warp/types.py +++ b/warp/types.py @@ -3583,7 +3583,7 @@ def get_feature_array_info(self, feature_index: int) -> Volume.FeatureArrayInfo: ) def feature_array(self, feature_index: int, dtype=None) -> array: - """Returns one the the grid's feature data arrays as a Warp array + """Returns one the grid's feature data arrays as a Warp array Args: feature_index: Index of the supplemental data array in the grid From e5129c353614f66a7e1a505945ea5d572debdd5c Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 8 Oct 2024 04:33:17 +0000 Subject: [PATCH 058/102] Working MLP + grad test --- warp/examples/assets/pixel.jpg | Bin 0 -> 33802 bytes warp/tests/test_tile_mlp.py | 327 +++++++++++++++++++++++++++++++++ 2 files changed, 327 insertions(+) create mode 100644 warp/examples/assets/pixel.jpg create mode 100644 warp/tests/test_tile_mlp.py diff --git a/warp/examples/assets/pixel.jpg b/warp/examples/assets/pixel.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b784f952919e2ebe4c8aa1eb41979fa2f0cbb51d GIT binary patch literal 33802 zcmeFXcRXBQ_dhz(qDJqcjL{ipbRpUdM(;g@F&MoENeI#V=snSil3++kBsznr5j_!| zh!7$~a3^^_pU?BX_w{^#_jUii&pwXxK6|gV_TKNk_L;r*IhV7S%K#c}4TJ{Z%9Ses zE&Ktv`~+x#2cw+<00cr9Knef=t^(+;5CX2?(eIP(3h|$-BObH=h28L2@GltxJce8$ z08rtte)toL#|-%E1N@1OCHkYAiO0VazPdm6e_u5b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mo37U}Nuk@9EAt%g ztOvNz{T4kswS0czvONd^p!gs!O1@vHgMkJeK4NodG^HE2}>aX(Fi!p?Xrm6V1Un8_bQG`w~-lE!llyJKWMZ{Rs~bOJItb0nGVJv zrNYnv@%ffG7B&}zue`ACw>G8Um?K93igg1)0kyX!u@!AaJu3eIQxH{&0nc~^259BE z-sJ;1`b4WpGGW}Mrc4iLIV&(sY>sLy40>7i6FOk2c?m;W+UK^RJy<~-qVsSbIkCLf zeV|5Nly8RaG>j-!);sr>n69hWm|LV$@q{4xHx1TGh(r)*7N Date: Tue, 8 Oct 2024 04:39:11 +0000 Subject: [PATCH 059/102] Use scalar loss function for comparison against Torch --- warp/tests/test_tile_mlp.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 9d4e67ef..b8e34452 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -140,7 +140,7 @@ def compute(input: wp.array2d(dtype=float), output[1] - reference[1,linear], output[2] - reference[2,linear]) - wp.atomic_add(loss, 0, wp.length_sq(error)/float(IMG_WIDTH*IMG_HEIGHT)) + wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*IMG_WIDTH*IMG_HEIGHT)) for i in range(DIM_OUT): @@ -190,10 +190,10 @@ def compute(input: wp.array2d(dtype=float), print(loss.numpy()) - output.grad = wp.ones_like(output) - tape.backward() + # output.grad = wp.ones_like(output) + # tape.backward() - #tape.backward(loss) + tape.backward(loss) # optimizer.step(optimizer_grads) @@ -240,10 +240,12 @@ def compute(input: wp.array2d(dtype=float), z_tc = tc.clamp(weights_2_tc@z_tc + bias_2_tc, min=0.0) ref_tc = tc.from_numpy(reference.numpy()).requires_grad_(True) - #l_tc = tc.mean((z_tc - ref_tc)**2) - #l_tc.backward() + + + l_tc = tc.mean((z_tc - ref_tc)**2) + l_tc.backward() - z_tc.backward(tc.ones_like(z_tc)) + #z_tc.backward(tc.ones_like(z_tc)) # test torch print("Torch output close: ", assert_equal(z_tc.cpu().detach().numpy(), output.numpy())) From c93b16ed277c9c7891f568039211fbf2bd35f4d7 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Mon, 7 Oct 2024 16:19:57 -0700 Subject: [PATCH 060/102] Allow tile storage to be specified in API --- docs/modules/functions.rst | 20 +++++-- warp/builtins.py | 72 ++++++++++++++++++----- warp/codegen.py | 3 + warp/examples/tile/example_tile_matmul.py | 19 +++--- warp/stubs.py | 20 +++++-- warp/types.py | 20 +++---- 6 files changed, 107 insertions(+), 47 deletions(-) diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index 8fcc6f83..ca1bea38 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -802,27 +802,31 @@ Spatial Math Tile Primitives --------------- -.. py:function:: tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile +.. py:function:: tile_zeros(m: int32, n: int32, dtype: Scalar, storage: str) -> Tile Allocates a tile of zero-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A zero-initialized tile with ``shape=(m,n)`` and the specified datatype -.. py:function:: tile_ones(m: int32, n: int32, dtype: Scalar) -> Tile +.. py:function:: tile_ones(m: int32, n: int32, dtype: Scalar, storage: str) -> Tile Allocates a tile of one-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A one-initialized tile with ``shape=(m,n)`` and the specified dtype -.. py:function:: tile_arange(*args: Scalar, dtype: Scalar) -> Tile +.. py:function:: tile_arange(*args: Scalar, dtype: Scalar, storage: str) -> Tile Generates a tile of linearly spaced elements. @@ -833,10 +837,12 @@ Tile Primitives - ``(start, stop, step)``: Generates values from ``start`` to ``stop - 1`` with a step size :param dtype: Datatype of output tile's elements (optional, default: int) + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(1,n)`` with linearly spaced elements of specified dtype -.. py:function:: tile_load(a: Array[Any], i: int32, n: int32) -> Tile +.. py:function:: tile_load(a: Array[Any], i: int32, n: int32, storage: str) -> Tile Loads a 1D tile from a global memory array. @@ -845,10 +851,12 @@ Tile Primitives :param a: The source array in global memory :param i: Offset in the source array measured in multiples of ``n``, i.e.: ``offset=i*n`` :param n: The number of elements in the tile + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(1,n)`` and dtype the same as the source array -.. py:function:: tile_load(a: Array[Any], i: int32, j: int32, m: int32, n: int32) -> Tile +.. py:function:: tile_load(a: Array[Any], i: int32, j: int32, m: int32, n: int32, storage: str) -> Tile :noindex: :nocontentsentry: @@ -861,6 +869,8 @@ Tile Primitives :param j: Offset in the source array measured in multiples of ``n``, i.e.; ``col=j*n`` :param m: The size of the tile's first dimension :param n: The size of the tile's second dimension + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array diff --git a/warp/builtins.py b/warp/builtins.py index fa7e8a5b..0bf4c2a5 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1722,10 +1722,18 @@ def tile_zeros_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str if "dtype" not in arg_values: raise RuntimeError("'dtype' keyword argument must be specified when calling tile_zeros() function") + if "storage" not in arg_values: + raise ValueError("'storage' keyword not provided for tile_zeros") + + if arg_values["storage"] not in {"shared", "register"}: + raise ValueError( + f"'storage' keyword argument must be either 'shared' or 'register', got {arg_values['storage']}" + ) + m, n = arg_values["m"], arg_values["n"] dtype = arg_values["dtype"] - return TileZeros(dtype=dtype, M=m, N=n) + return TileZeros(dtype=dtype, M=m, N=n, storage=arg_values["storage"]) def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): @@ -1741,7 +1749,8 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar add_builtin( "tile_zeros", - input_types={"m": int, "n": int, "dtype": Scalar}, + input_types={"m": int, "n": int, "dtype": Scalar, "storage": str}, + defaults={"storage": "register"}, value_func=tile_zeros_value_func, dispatch_func=tile_zeros_dispatch_func, variadic=True, @@ -1750,6 +1759,8 @@ def tile_zeros_dispatch_func(arg_types: Mapping[str, type], return_type: Any, ar :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A zero-initialized tile with ``shape=(m,n)`` and the specified datatype""", group="Tile Primitives", export=False, @@ -1770,10 +1781,15 @@ def tile_ones_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, if "dtype" not in arg_values: raise RuntimeError("'dtype' keyword argument must be specified when calling tile_zeros() function") + if arg_values["storage"] not in {"shared", "register"}: + raise ValueError( + f"'storage' keyword argument must be either 'shared' or 'register', got {arg_values['storage']}" + ) + m, n = arg_values["m"], arg_values["n"] dtype = arg_values["dtype"] - return TileZeros(dtype=dtype, M=m, N=n) + return TileZeros(dtype=dtype, M=m, N=n, storage=arg_values["storage"]) def tile_ones_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): @@ -1789,7 +1805,8 @@ def tile_ones_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg add_builtin( "tile_ones", - input_types={"m": int, "n": int, "dtype": Scalar}, + input_types={"m": int, "n": int, "dtype": Scalar, "storage": str}, + defaults={"storage": "register"}, value_func=tile_ones_value_func, dispatch_func=tile_ones_dispatch_func, variadic=True, @@ -1798,6 +1815,8 @@ def tile_ones_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A one-initialized tile with ``shape=(m,n)`` and the specified dtype""", group="Tile Primitives", export=False, @@ -1837,7 +1856,12 @@ def tile_arange_value_func(arg_types: Mapping[str, type], arg_values: Mapping[st else: dtype = float - return TileRange(dtype=dtype, start=start, stop=stop, step=step) + if arg_values["storage"] not in {"shared", "register"}: + raise ValueError( + f"'storage' keyword argument must be either 'shared' or 'register', got {arg_values['storage']}" + ) + + return TileRange(dtype=dtype, start=start, stop=stop, step=step, storage=arg_values["storage"]) def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): @@ -1864,8 +1888,8 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a add_builtin( "tile_arange", - input_types={"*args": Scalar, "dtype": Scalar}, - defaults={"dtype": None}, + input_types={"*args": Scalar, "dtype": Scalar, "storage": str}, + defaults={"dtype": None, "storage": "register"}, value_func=tile_arange_value_func, dispatch_func=tile_arange_dispatch_func, variadic=True, @@ -1878,6 +1902,8 @@ def tile_arange_dispatch_func(arg_types: Mapping[str, type], return_type: Any, a - ``(start, stop, step)``: Generates values from ``start`` to ``stop - 1`` with a step size :param dtype: Datatype of output tile's elements (optional, default: int) + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(1,n)`` with linearly spaced elements of specified dtype""", group="Tile Primitives", export=False, @@ -1903,10 +1929,15 @@ def tile_load_1d_value_func(arg_types, arg_values): if "n" not in arg_values: raise RuntimeError("'n' keyword argument must be specified when calling tile_load() function") + if arg_values["storage"] not in {"shared", "register"}: + raise ValueError( + f"'storage' keyword argument must be either 'shared' or 'register', got {arg_values['storage']}" + ) + a = arg_types["a"] _m, n = 1, arg_values["n"] - return TileLoad(a, 1, n) + return TileLoad(a, 1, n, arg_values["storage"]) def tile_load_1d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): @@ -1924,7 +1955,8 @@ def tile_load_1d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, add_builtin( "tile_load", - input_types={"a": array(dtype=Any), "i": int, "n": int}, + input_types={"a": array(dtype=Any), "i": int, "n": int, "storage": str}, + defaults={"storage": "register"}, value_func=tile_load_1d_value_func, dispatch_func=tile_load_1d_dispatch_func, variadic=False, @@ -1935,6 +1967,8 @@ def tile_load_1d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, :param a: The source array in global memory :param i: Offset in the source array measured in multiples of ``n``, i.e.: ``offset=i*n`` :param n: The number of elements in the tile + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(1,n)`` and dtype the same as the source array""", group="Tile Primitives", export=False, @@ -1966,10 +2000,15 @@ def tile_load_2d_value_func(arg_types, arg_values): if "n" not in arg_values: raise RuntimeError("'n' keyword argument must be specified when calling tile_load() function") + if arg_values["storage"] not in {"shared", "register"}: + raise ValueError( + f"'storage' keyword argument must be either 'shared' or 'register', got {arg_values['storage']}" + ) + a = arg_types["a"] m, n = arg_values["m"], arg_values["n"] - return TileLoad(a, m, n) + return TileLoad(a, m, n, arg_values["storage"]) def tile_load_2d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, arg_values: Mapping[str, Var]): @@ -1988,7 +2027,8 @@ def tile_load_2d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, add_builtin( "tile_load", - input_types={"a": array(dtype=Any), "i": int, "j": int, "m": int, "n": int}, + input_types={"a": array(dtype=Any), "i": int, "j": int, "m": int, "n": int, "storage": str}, + defaults={"storage": "register"}, value_func=tile_load_2d_value_func, dispatch_func=tile_load_2d_dispatch_func, variadic=False, @@ -2001,6 +2041,8 @@ def tile_load_2d_dispatch_func(arg_types: Mapping[str, type], return_type: Any, :param j: Offset in the source array measured in multiples of ``n``, i.e.; ``col=j*n`` :param m: The size of the tile's first dimension :param n: The size of the tile's second dimension + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array""", group="Tile Primitives", export=False, @@ -2707,10 +2749,10 @@ def tile_unary_map_value_func(arg_types, arg_values): return TileUnaryMap(a) -def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, args: Mapping[str, Var]): - func_args = (args["op"], *args["args"]) - template_args = () - return (func_args, template_args) +# def tile_map_dispatch_func(input_types: Mapping[str, type], return_type: Any, args: Mapping[str, Var]): +# func_args = (args["op"], *args["args"]) +# template_args = () +# return (func_args, template_args) add_builtin( diff --git a/warp/codegen.py b/warp/codegen.py index 50288e05..53519521 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -783,6 +783,9 @@ def func_match_args(func, arg_types, kwarg_types): def get_arg_type(arg: Union[Var, Any]): + if isinstance(arg, str): + return str + if isinstance(arg, Sequence): return tuple(get_arg_type(x) for x in arg) diff --git a/warp/examples/tile/example_tile_matmul.py b/warp/examples/tile/example_tile_matmul.py index 881396f9..b8ee510c 100644 --- a/warp/examples/tile/example_tile_matmul.py +++ b/warp/examples/tile/example_tile_matmul.py @@ -13,6 +13,7 @@ ########################################################################### import numpy as np + import warp as wp # tile size @@ -23,16 +24,16 @@ # num threads per-tile TILE_THREADS = 64 + @wp.kernel def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): - # output tile index i, j = wp.tid() sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) - M = A.shape[0] - N = B.shape[1] + _M = A.shape[0] + _N = B.shape[1] K = A.shape[1] count = int(K / TILE_K) @@ -47,9 +48,7 @@ def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.arra wp.tile_store(C, i, j, sum) - if __name__ == "__main__": - wp.set_device("cuda:0") # generate some tile aligned matrix dimensions @@ -68,13 +67,9 @@ def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.arra with wp.Tape() as tape: wp.launch_tiled( - tile_gemm, - dim=(int(M / TILE_M), int(N / TILE_N)), - inputs=[A_wp, B_wp, C_wp], - block_dim=TILE_THREADS) + tile_gemm, dim=(int(M / TILE_M), int(N / TILE_N)), inputs=[A_wp, B_wp, C_wp], block_dim=TILE_THREADS + ) - assert(np.allclose(C_wp.numpy(), A@B)) + assert np.allclose(C_wp.numpy(), A @ B) print("Example matrix multiplication passed") - - diff --git a/warp/stubs.py b/warp/stubs.py index 3b7f8823..77e1c548 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -894,31 +894,35 @@ def spatial_mass( @over -def tile_zeros(m: int32, n: int32, dtype: Scalar) -> Tile: +def tile_zeros(m: int32, n: int32, dtype: Scalar, storage: str) -> Tile: """Allocates a tile of zero-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A zero-initialized tile with ``shape=(m,n)`` and the specified datatype """ ... @over -def tile_ones(m: int32, n: int32, dtype: Scalar) -> Tile: +def tile_ones(m: int32, n: int32, dtype: Scalar, storage: str) -> Tile: """Allocates a tile of one-initialized items. :param m: Size of the first dimension of the output tile :param n: Size of the second dimension of the output tile :param dtype: Datatype of output tile's elements + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A one-initialized tile with ``shape=(m,n)`` and the specified dtype """ ... @over -def tile_arange(*args: Scalar, dtype: Scalar) -> Tile: +def tile_arange(*args: Scalar, dtype: Scalar, storage: str) -> Tile: """Generates a tile of linearly spaced elements. :param args: Variable-length positional arguments, interpreted as: @@ -928,13 +932,15 @@ def tile_arange(*args: Scalar, dtype: Scalar) -> Tile: - ``(start, stop, step)``: Generates values from ``start`` to ``stop - 1`` with a step size :param dtype: Datatype of output tile's elements (optional, default: int) + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(1,n)`` with linearly spaced elements of specified dtype """ ... @over -def tile_load(a: Array[Any], i: int32, n: int32) -> Tile: +def tile_load(a: Array[Any], i: int32, n: int32, storage: str) -> Tile: """Loads a 1D tile from a global memory array. This method will cooperatively load a tile from global memory using all threads in the block. @@ -942,13 +948,15 @@ def tile_load(a: Array[Any], i: int32, n: int32) -> Tile: :param a: The source array in global memory :param i: Offset in the source array measured in multiples of ``n``, i.e.: ``offset=i*n`` :param n: The number of elements in the tile + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(1,n)`` and dtype the same as the source array """ ... @over -def tile_load(a: Array[Any], i: int32, j: int32, m: int32, n: int32) -> Tile: +def tile_load(a: Array[Any], i: int32, j: int32, m: int32, n: int32, storage: str) -> Tile: """Loads a 2D tile from a global memory array. This method will cooperatively load a tile from global memory using all threads in the block. @@ -958,6 +966,8 @@ def tile_load(a: Array[Any], i: int32, j: int32, m: int32, n: int32) -> Tile: :param j: Offset in the source array measured in multiples of ``n``, i.e.; ``col=j*n`` :param m: The size of the tile's first dimension :param n: The size of the tile's second dimension + :param storage: The storage location for the tile: ``"register"`` for registers + (default) or ``"shared"`` for shared memory. :returns: A tile with ``shape=(m,n)`` and dtype the same as the source array """ ... diff --git a/warp/types.py b/warp/types.py index 454f7cc0..ea9604e4 100644 --- a/warp/types.py +++ b/warp/types.py @@ -3020,12 +3020,12 @@ def alloc(cls): class TileZeros(Tile): - def __init__(self, dtype, M, N): - Tile.__init__(self, dtype, M, N, op="zeros", storage="register") + def __init__(self, dtype, M, N, storage="register"): + Tile.__init__(self, dtype, M, N, op="zeros", storage=storage) class TileRange(Tile): - def __init__(self, dtype, start, stop, step): + def __init__(self, dtype, start, stop, step, storage="register"): self.start = start self.stop = stop self.step = step @@ -3033,7 +3033,7 @@ def __init__(self, dtype, start, stop, step): M = 1 N = int((stop - start) / step) - Tile.__init__(self, dtype, M, N, op="arange", storage="register") + Tile.__init__(self, dtype, M, N, op="arange", storage=storage) class TileConstant(Tile): @@ -3042,20 +3042,20 @@ def __init__(self, dtype, M, N): class TileLoad(Tile): - def __init__(self, array, M, N): - Tile.__init__(self, array.dtype, M, N, op="load", storage="register") + def __init__(self, array, M, N, storage="register"): + Tile.__init__(self, array.dtype, M, N, op="load", storage=storage) class TileUnaryMap(Tile): - def __init__(self, t): - Tile.__init__(self, t.dtype, t.M, t.N, op="unary_map", storage="register") + def __init__(self, t, storage="register"): + Tile.__init__(self, t.dtype, t.M, t.N, op="unary_map", storage=storage) self.t = t class TileBinaryMap(Tile): - def __init__(self, a, b): - Tile.__init__(self, a.dtype, a.M, a.N, op="binary_map", storage="register") + def __init__(self, a, b, storage="register"): + Tile.__init__(self, a.dtype, a.M, a.N, op="binary_map", storage=storage) self.a = a self.b = b From d32c9043016e4c5b4b4f85280eeee5666d52099b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 8 Oct 2024 21:35:09 +0000 Subject: [PATCH 061/102] Testing different dimensions --- warp/tests/test_tile_mlp.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index b8e34452..04bdec85 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -9,9 +9,6 @@ from PIL import Image -TILE_M = wp.constant(4) -TILE_N = wp.constant(2) - #wp.clear_kernel_cache() #wp.config.mode = "debug" #wp.config.verify_cuda = True @@ -23,7 +20,7 @@ def assert_equal(result: np.ndarray, expect: np.ndarray, tol=1.e-2): if tol != 0.0: # TODO: Get all tests working without the .flatten() - np.testing.assert_allclose(result.flatten(), expect.flatten(), rtol=tol, atol=0, equal_nan=True) + np.testing.assert_allclose(result.flatten(), expect.flatten(), rtol=tol, atol=1.e-2, equal_nan=True) else: # TODO: Get all tests working with strict=True np.testing.assert_array_equal(result, expect) @@ -188,7 +185,7 @@ def compute(input: wp.array2d(dtype=float), output], block_dim=NUM_THREADS) - print(loss.numpy()) + print(f"Iter: {i} Loss: {loss.numpy()}") # output.grad = wp.ones_like(output) # tape.backward() From 77e83e3cc57481e264fcfc365438a2caaccbb12c Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 8 Oct 2024 21:41:02 +0000 Subject: [PATCH 062/102] Clean up some comments --- warp/tests/test_tile_mlp.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 04bdec85..30b2245e 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -76,16 +76,13 @@ def compute(input: wp.array2d(dtype=float), row, col = wp.tid() linear = row*IMG_WIDTH + col - # linear = wp.tid() - # row = linear/IMG_WIDTH - # col = linear%IMG_WIDTH - - # # normalize input coordinates to [-1, 1] + # normalize input coordinates to [-1, 1] x = (float(row)/float(IMG_WIDTH) - 0.5)*2.0 y = (float(col)/float(IMG_HEIGHT) - 0.5)*2.0 local = wp.vector(dtype=float, length=DIM_IN) + # construct positional encoding for s in range(NUM_FREQ): scale = wp.pow(2.0, float(s))*wp.pi @@ -98,48 +95,43 @@ def compute(input: wp.array2d(dtype=float), local[s*4 + 2] = wp.sin(y * scale) local[s*4 + 3] = wp.cos(y * scale) - # write input back to array so that torch can use it input[s*4 + 0, linear] = local[s*4 + 0] input[s*4 + 1, linear] = local[s*4 + 1] input[s*4 + 2, linear] = local[s*4 + 2] input[s*4 + 3, linear] = local[s*4 + 3] - ## load from input array - # local = wp.vector(dtype=float, length=DIM_IN) - # for i in range(DIM_IN): - # local[i] = input[i, linear] - + # tile feature vectors across the block, returns [dim(f), NUM_THREADS] f = wp.tile(local) - # input layer w0 = wp.tile_load(weights_0, 0, 0, m=DIM_HID, n=DIM_IN) b0 = wp.tile_load(bias_0, 0, 0, m=DIM_HID, n=1) z = wp.tile_map(relu, wp.tile_matmul(w0, f) + wp.tile_broadcast(b0, m=DIM_HID, n=NUM_THREADS)) - # output layer + # hidden layer w1 = wp.tile_load(weights_1, 0, 0, m=DIM_HID, n=DIM_HID) b1 = wp.tile_load(bias_1, 0, 0, m=DIM_HID, n=1) z = wp.tile_map(relu, wp.tile_matmul(w1, z) + wp.tile_broadcast(b1, m=DIM_HID, n=NUM_THREADS)) - + # output layer w2 = wp.tile_load(weights_2, 0, 0, m=DIM_OUT, n=DIM_HID) b2 = wp.tile_load(bias_2, 0, 0, m=DIM_OUT, n=1) o = wp.tile_map(relu, wp.tile_matmul(w2, z) + wp.tile_broadcast(b2, m=DIM_OUT, n=NUM_THREADS)) - #wp.tile_store(out, 0, i, o) - + # until back to SIMT output = wp.untile(o) + # compute error error = wp.vec3(output[0] - reference[0,linear], output[1] - reference[1,linear], output[2] - reference[2,linear]) + # write MSE loss wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*IMG_WIDTH*IMG_HEIGHT)) - + # image output for i in range(DIM_OUT): out[i, linear] = output[i] From 29b277df436a370faf414ecced7a54df40af021b Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 8 Oct 2024 15:17:36 -0700 Subject: [PATCH 063/102] Use Artifactory access key --- .gitlab/ci/mathdx-support.yml | 12 ++++++------ warp/examples/tile/example_tile_matmul.py | 19 +++++++------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index b6fff5b3..bfca61fe 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -33,11 +33,11 @@ linux-x86_64 build: - .runner-build-linux-x86_64 before_script: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - - apt-get update && apt-get install build-essential curl wget --no-install-recommends -y + - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > - wget --header="X-JFrog-Art-Api:$ARTIFACTORY_API_KEY" -nv --no-check-certificate + curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/30/libmathdx_build_x86_64_ubuntu20.04_cuda12.0.0_release.tar.gz - -O libmathdx.tar.gz + -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" @@ -56,11 +56,11 @@ linux-aarch64 build: - .save_warp_bin_artifact before_script: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - - apt-get update && apt-get install build-essential curl wget --no-install-recommends -y + - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > - wget --header="X-JFrog-Art-Api:$ARTIFACTORY_API_KEY" -nv --no-check-certificate + curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/30/libmathdx_build_aarch64_ubuntu20.04_cuda12.0.0_release.tar.gz - -O libmathdx.tar.gz + -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" diff --git a/warp/examples/tile/example_tile_matmul.py b/warp/examples/tile/example_tile_matmul.py index 881396f9..b8ee510c 100644 --- a/warp/examples/tile/example_tile_matmul.py +++ b/warp/examples/tile/example_tile_matmul.py @@ -13,6 +13,7 @@ ########################################################################### import numpy as np + import warp as wp # tile size @@ -23,16 +24,16 @@ # num threads per-tile TILE_THREADS = 64 + @wp.kernel def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): - # output tile index i, j = wp.tid() sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) - M = A.shape[0] - N = B.shape[1] + _M = A.shape[0] + _N = B.shape[1] K = A.shape[1] count = int(K / TILE_K) @@ -47,9 +48,7 @@ def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.arra wp.tile_store(C, i, j, sum) - if __name__ == "__main__": - wp.set_device("cuda:0") # generate some tile aligned matrix dimensions @@ -68,13 +67,9 @@ def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.arra with wp.Tape() as tape: wp.launch_tiled( - tile_gemm, - dim=(int(M / TILE_M), int(N / TILE_N)), - inputs=[A_wp, B_wp, C_wp], - block_dim=TILE_THREADS) + tile_gemm, dim=(int(M / TILE_M), int(N / TILE_N)), inputs=[A_wp, B_wp, C_wp], block_dim=TILE_THREADS + ) - assert(np.allclose(C_wp.numpy(), A@B)) + assert np.allclose(C_wp.numpy(), A @ B) print("Example matrix multiplication passed") - - From 132c8d64f8e95ea311408ab3adbb692cfa4799ef Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 9 Oct 2024 04:23:21 +0000 Subject: [PATCH 064/102] Add batching support --- warp/tests/test_tile_mlp.py | 87 ++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 29 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 30b2245e..47748110 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -13,8 +13,11 @@ #wp.config.mode = "debug" #wp.config.verify_cuda = True +wp.set_device("cuda:0") wp.set_module_options({"fast_math": False}) +#wp.clear_kernel_cache() + rng = np.random.default_rng(45) def assert_equal(result: np.ndarray, expect: np.ndarray, tol=1.e-2): @@ -53,10 +56,11 @@ def create_array(dim_in, dim_hid, dtype=float): DIM_OUT = 3 NUM_THREADS = 32 -NUM_BLOCKS = 36 -IMG_WIDTH = NUM_THREADS*2 -IMG_HEIGHT = NUM_THREADS*2 +IMG_WIDTH = NUM_THREADS*8 +IMG_HEIGHT = NUM_THREADS*8 + +BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) def test_multi_layer_nn(): @@ -64,8 +68,17 @@ def test_multi_layer_nn(): def relu(x: float): return wp.max(x, 0.0) + @wp.func + def sigmoid(x: float): + return 1.0 / (1.0 + wp.exp(-x)) + @wp.kernel - def compute(input: wp.array2d(dtype=float), + def zero(loss: wp.array(dtype=float)): + loss[0] = 0.0 + + @wp.kernel + def compute(batches: wp.array(dtype=int), + input: wp.array2d(dtype=float), weights_0: wp.array2d(dtype=float), bias_0: wp.array2d(dtype=float), weights_1: wp.array2d(dtype=float), bias_1: wp.array2d(dtype=float), weights_2: wp.array2d(dtype=float), bias_2: wp.array2d(dtype=float), @@ -73,8 +86,12 @@ def compute(input: wp.array2d(dtype=float), loss: wp.array1d(dtype=float), out: wp.array2d(dtype=float)): - row, col = wp.tid() - linear = row*IMG_WIDTH + col + # row, col = wp.tid() + # linear = row*IMG_WIDTH + col + + linear = batches[wp.tid()] + row = linear/IMG_WIDTH + col = linear%IMG_WIDTH # normalize input coordinates to [-1, 1] x = (float(row)/float(IMG_WIDTH) - 0.5)*2.0 @@ -118,7 +135,7 @@ def compute(input: wp.array2d(dtype=float), # output layer w2 = wp.tile_load(weights_2, 0, 0, m=DIM_OUT, n=DIM_HID) b2 = wp.tile_load(bias_2, 0, 0, m=DIM_OUT, n=1) - o = wp.tile_map(relu, wp.tile_matmul(w2, z) + wp.tile_broadcast(b2, m=DIM_OUT, n=NUM_THREADS)) + o = wp.tile_map(sigmoid, wp.tile_matmul(w2, z) + wp.tile_broadcast(b2, m=DIM_OUT, n=NUM_THREADS)) # until back to SIMT output = wp.untile(o) @@ -129,7 +146,7 @@ def compute(input: wp.array2d(dtype=float), output[2] - reference[2,linear]) # write MSE loss - wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*IMG_WIDTH*IMG_HEIGHT)) + wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*BATCH_SIZE)) # image output for i in range(DIM_OUT): @@ -160,34 +177,43 @@ def compute(input: wp.array2d(dtype=float), optimizer_inputs = [p.flatten() for p in params] optimizer = warp.optim.Adam(optimizer_inputs, lr=0.001) - for i in range(1): + # create shuffled batch indices + indices = np.arange(0, IMG_WIDTH*IMG_HEIGHT) + np.random.shuffle(indices) + batches = wp.array(indices, dtype=int) - loss.zero_() + for i in range(32): - with wp.Tape() as tape: - wp.launch( - compute, - dim=[IMG_WIDTH, IMG_HEIGHT], - inputs=[input, - weights_0, bias_0, - weights_1, bias_1, - weights_2, bias_2, - reference, - loss, - output], - block_dim=NUM_THREADS) + for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): - print(f"Iter: {i} Loss: {loss.numpy()}") + loss.zero_() - # output.grad = wp.ones_like(output) - # tape.backward() - - tape.backward(loss) + with wp.Tape() as tape: + wp.launch( + compute, + dim=[BATCH_SIZE], + inputs=[batches[b:b+BATCH_SIZE], + input, + weights_0, bias_0, + weights_1, bias_1, + weights_2, bias_2, + reference, + loss, + output], + block_dim=NUM_THREADS) + + print(f"Iter: {i} Loss: {loss.numpy()}") + + tape.backward(loss) - # optimizer.step(optimizer_grads) + optimizer.step(optimizer_grads) - # tape.zero() + tape.zero() + # uncommenting this line fixes convergence + # wp.synchronize() + + predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) predicted_image = (predicted_image * 255).astype(np.uint8) @@ -195,6 +221,9 @@ def compute(input: wp.array2d(dtype=float), predicted_image_pil = Image.fromarray(predicted_image) predicted_image_pil.save("test_tile_mlp_wp.jpg") + return + + # print(input) # print(output) From 9e8dad930e84fa9f7e71e561e7b193683676fcc8 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 9 Oct 2024 22:30:12 +0000 Subject: [PATCH 065/102] Increase layers, use cosine weighted learning rate --- warp/tests/test_tile_mlp.py | 43 ++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 47748110..5e3616a8 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -5,6 +5,7 @@ import torch as tc +import math import os from PIL import Image @@ -49,7 +50,7 @@ def create_array(dim_in, dim_hid, dtype=float): return a -NUM_FREQ = wp.constant(4) +NUM_FREQ = wp.constant(8) DIM_IN = wp.constant(4*NUM_FREQ) # sin,cos for both x,y at each frequenecy DIM_HID = 16 @@ -57,8 +58,8 @@ def create_array(dim_in, dim_hid, dtype=float): NUM_THREADS = 32 -IMG_WIDTH = NUM_THREADS*8 -IMG_HEIGHT = NUM_THREADS*8 +IMG_WIDTH = NUM_THREADS*16 +IMG_HEIGHT = NUM_THREADS*16 BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) @@ -82,6 +83,7 @@ def compute(batches: wp.array(dtype=int), weights_0: wp.array2d(dtype=float), bias_0: wp.array2d(dtype=float), weights_1: wp.array2d(dtype=float), bias_1: wp.array2d(dtype=float), weights_2: wp.array2d(dtype=float), bias_2: wp.array2d(dtype=float), + weights_3: wp.array2d(dtype=float), bias_3: wp.array2d(dtype=float), reference: wp.array2d(dtype=float), loss: wp.array1d(dtype=float), out: wp.array2d(dtype=float)): @@ -132,12 +134,16 @@ def compute(batches: wp.array(dtype=int), b1 = wp.tile_load(bias_1, 0, 0, m=DIM_HID, n=1) z = wp.tile_map(relu, wp.tile_matmul(w1, z) + wp.tile_broadcast(b1, m=DIM_HID, n=NUM_THREADS)) + w2 = wp.tile_load(weights_2, 0, 0, m=DIM_HID, n=DIM_HID) + b2 = wp.tile_load(bias_2, 0, 0, m=DIM_HID, n=1) + z = wp.tile_map(relu, wp.tile_matmul(w2, z) + wp.tile_broadcast(b2, m=DIM_HID, n=NUM_THREADS)) + # output layer - w2 = wp.tile_load(weights_2, 0, 0, m=DIM_OUT, n=DIM_HID) - b2 = wp.tile_load(bias_2, 0, 0, m=DIM_OUT, n=1) - o = wp.tile_map(sigmoid, wp.tile_matmul(w2, z) + wp.tile_broadcast(b2, m=DIM_OUT, n=NUM_THREADS)) + w3 = wp.tile_load(weights_3, 0, 0, m=DIM_OUT, n=DIM_HID) + b3 = wp.tile_load(bias_3, 0, 0, m=DIM_OUT, n=1) + o = wp.tile_map(sigmoid, wp.tile_matmul(w3, z) + wp.tile_broadcast(b3, m=DIM_OUT, n=NUM_THREADS)) - # until back to SIMT + # untile back to SIMT output = wp.untile(o) # compute error @@ -156,7 +162,8 @@ def compute(batches: wp.array(dtype=int), weights_0, bias_0 = create_layer(DIM_IN, DIM_HID, dtype=float) weights_1, bias_1 = create_layer(DIM_HID, DIM_HID, dtype=float) - weights_2, bias_2 = create_layer(DIM_HID, DIM_OUT, dtype=float) + weights_2, bias_2 = create_layer(DIM_HID, DIM_HID, dtype=float) + weights_3, bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=float) input = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_IN) output = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_OUT) @@ -171,18 +178,20 @@ def compute(batches: wp.array(dtype=int), params = [weights_0, bias_0, weights_1, bias_1, - weights_2, bias_2] + weights_2, bias_2, + weights_3, bias_3] optimizer_grads = [p.grad.flatten() for p in params] optimizer_inputs = [p.flatten() for p in params] optimizer = warp.optim.Adam(optimizer_inputs, lr=0.001) - # create shuffled batch indices - indices = np.arange(0, IMG_WIDTH*IMG_HEIGHT) - np.random.shuffle(indices) - batches = wp.array(indices, dtype=int) + max_iters = 500 + + for i in range(max_iters): - for i in range(32): + # create randomized batch indices + batches = wp.array(rng.integers(low=0, high=IMG_WIDTH*IMG_HEIGHT, size=IMG_WIDTH*IMG_HEIGHT, dtype=np.int32)) + for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): @@ -197,15 +206,19 @@ def compute(batches: wp.array(dtype=int), weights_0, bias_0, weights_1, bias_1, weights_2, bias_2, + weights_3, bias_3, reference, loss, output], block_dim=NUM_THREADS) - print(f"Iter: {i} Loss: {loss.numpy()}") + if b == 0: + print(f"Iter: {i} Loss: {loss.numpy()}") tape.backward(loss) + # cosine weighted decay + optimizer.lr = 0.5*0.01*(1.0 + math.cos(float(i)/float(max_iters)*math.pi)) optimizer.step(optimizer_grads) tape.zero() From 477a30c382de806cdfabcab41c406a04e855498c Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 10 Oct 2024 04:31:07 +0000 Subject: [PATCH 066/102] Add fp16 support, fix for uninitialized output --- warp/builtins.py | 6 +- warp/native/tile.h | 16 ++- warp/optim/adam.py | 43 ++++++- warp/tests/test_tile_mlp.py | 240 ++++++++++++++++++------------------ 4 files changed, 177 insertions(+), 128 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index fa7e8a5b..d8a38a5e 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5559,8 +5559,10 @@ def tile_matmul_generic_lto_dispatch_func( b = arg_values["b"] if len(return_values) > 0: + accumulate = 0 # for c = tile_matmul(a,b) case we want to overwrite c value out = return_values[0] else: + accumulate = 1 # for tile_matmul(a,b,c) case we want to add to c value out = arg_values["out"] if any(not is_tile(arg.type) for arg in [a, b, out]): @@ -5581,7 +5583,7 @@ def tile_matmul_generic_lto_dispatch_func( a.type.storage = "shared" b.type.storage = "shared" out.type.storage = "shared" - template_args = [] + template_args = [accumulate] # Real if out.type.dtype == float16: @@ -5728,7 +5730,6 @@ def tile_flip_layout(layout): """, group="Tile Primitives", export=False, - namespace="", ) add_builtin( @@ -5752,7 +5753,6 @@ def tile_flip_layout(layout): """, group="Tile Primitives", export=False, - namespace="", ) diff --git a/warp/native/tile.h b/warp/native/tile.h index cd25c674..8df8e202 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -742,6 +742,16 @@ template inline CUDA_CALLABLE auto tile_alloc_empty() { WP_TILE_SHARED __align__(16) T data[M*N]; + +#if FP_CHECK + + for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) + data[i] = T(nanf("")); + + WP_TILE_SYNC(); + +#endif // FP_CHECK + return tile_shared_t(data); } @@ -1287,13 +1297,13 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ } // cuBLASDx follows the BLAS convention: matrices are col-major, so we swap A & B in the code below -template +template TileC& tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C) { using T = typename TileA::Type; WP_TILE_SYNC(); - fun_forward(T(1.0), B.data, A.data, T(1.0), C.data); + fun_forward(T(1.0), B.data, A.data, T(Add), C.data); WP_TILE_SYNC(); return C; @@ -1314,7 +1324,7 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, // backward for the out = wp.tile_matmul(a, b) syntax template -void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C, +void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C, Fwd adj_fun_forward, AdjA adj_fun_backward_A, AdjB adj_fun_backward_B, TileA& adj_A, TileB& adj_B, TileC& adj_C, TileC& adj_ret) { using T = typename TileA::Type; diff --git a/warp/optim/adam.py b/warp/optim/adam.py index cce2eff6..fb2d0064 100644 --- a/warp/optim/adam.py +++ b/warp/optim/adam.py @@ -50,6 +50,26 @@ def adam_step_kernel_float( params[i] = params[i] - lr * mhat / (wp.sqrt(vhat) + eps) +@wp.kernel +def adam_step_kernel_half( + g: wp.array(dtype=wp.float16), + m: wp.array(dtype=float), + v: wp.array(dtype=float), + lr: float, + beta1: float, + beta2: float, + t: float, + eps: float, + params: wp.array(dtype=wp.float16), +): + i = wp.tid() + m[i] = beta1 * m[i] + (1.0 - beta1) * float(g[i]) + v[i] = beta2 * v[i] + (1.0 - beta2) * float(g[i]) * float(g[i]) + mhat = m[i] / (1.0 - wp.pow(beta1, (t + 1.0))) + vhat = v[i] / (1.0 - wp.pow(beta2, (t + 1.0))) + params[i] = params[i] - wp.float16(lr * mhat / (wp.sqrt(vhat) + eps)) + + class Adam: """An implementation of the Adam Optimizer It is designed to mimic Pytorch's version. @@ -75,10 +95,20 @@ def set_params(self, params): self.v = [None] * len(params) # reset second moment for i in range(len(params)): param = params[i] + + if param.dtype == wp.vec3: + dtype = wp.vec3 + elif param.dtype == wp.float32: + dtype = wp.float32 + elif param.dtype == wp.float16: + dtype = wp.float32 # we always use fp32 for moments, even if params are fp16 + else: + raise RuntimeError(f"Unsupported dtype for Warp Adam optimizer: {param.dtype}") + if self.m[i] is None or self.m[i].shape != param.shape or self.m[i].dtype != param.dtype: - self.m[i] = wp.zeros_like(param) + self.m[i] = wp.zeros(shape=param.shape, dtype=dtype, device=param.device) if self.v[i] is None or self.v[i].shape != param.shape or self.v[i].dtype != param.dtype: - self.v[i] = wp.zeros_like(param) + self.v[i] = wp.zeros(shape=param.shape, dtype=dtype, device=param.device) def reset_internal_state(self): for m_i in self.m: @@ -98,8 +128,6 @@ def step(self, grad): @staticmethod def step_detail(g, m, v, lr, beta1, beta2, t, eps, params): assert params.dtype == g.dtype - assert params.dtype == m.dtype - assert params.dtype == v.dtype assert params.shape == g.shape kernel_inputs = [g, m, v, lr, beta1, beta2, t, eps, params] if params.dtype == wp.types.float32: @@ -109,6 +137,13 @@ def step_detail(g, m, v, lr, beta1, beta2, t, eps, params): inputs=kernel_inputs, device=params.device, ) + elif params.dtype == wp.types.float16: + wp.launch( + kernel=adam_step_kernel_half, + dim=len(params), + inputs=kernel_inputs, + device=params.device, + ) elif params.dtype == wp.types.vec3: wp.launch( kernel=adam_step_kernel_vec3, diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 5e3616a8..34f5ff60 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -10,8 +10,8 @@ from PIL import Image -#wp.clear_kernel_cache() #wp.config.mode = "debug" +#wp.config.verify_fp = True #wp.config.verify_cuda = True wp.set_device("cuda:0") @@ -53,7 +53,7 @@ def create_array(dim_in, dim_hid, dtype=float): NUM_FREQ = wp.constant(8) DIM_IN = wp.constant(4*NUM_FREQ) # sin,cos for both x,y at each frequenecy -DIM_HID = 16 +DIM_HID = 32 DIM_OUT = 3 NUM_THREADS = 32 @@ -63,15 +63,17 @@ def create_array(dim_in, dim_hid, dtype=float): BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) +dtype = wp.float16 + def test_multi_layer_nn(): @wp.func - def relu(x: float): - return wp.max(x, 0.0) + def relu(x: dtype): + return wp.max(x, dtype(0.0)) @wp.func - def sigmoid(x: float): - return 1.0 / (1.0 + wp.exp(-x)) + def sigmoid(x: dtype): + return dtype(1.0 / (1.0 + wp.exp(-float(x)))) @wp.kernel def zero(loss: wp.array(dtype=float)): @@ -79,11 +81,11 @@ def zero(loss: wp.array(dtype=float)): @wp.kernel def compute(batches: wp.array(dtype=int), - input: wp.array2d(dtype=float), - weights_0: wp.array2d(dtype=float), bias_0: wp.array2d(dtype=float), - weights_1: wp.array2d(dtype=float), bias_1: wp.array2d(dtype=float), - weights_2: wp.array2d(dtype=float), bias_2: wp.array2d(dtype=float), - weights_3: wp.array2d(dtype=float), bias_3: wp.array2d(dtype=float), + input: wp.array2d(dtype=dtype), + weights_0: wp.array2d(dtype=dtype), bias_0: wp.array2d(dtype=dtype), + weights_1: wp.array2d(dtype=dtype), bias_1: wp.array2d(dtype=dtype), + weights_2: wp.array2d(dtype=dtype), bias_2: wp.array2d(dtype=dtype), + weights_3: wp.array2d(dtype=dtype), bias_3: wp.array2d(dtype=dtype), reference: wp.array2d(dtype=float), loss: wp.array1d(dtype=float), out: wp.array2d(dtype=float)): @@ -99,7 +101,7 @@ def compute(batches: wp.array(dtype=int), x = (float(row)/float(IMG_WIDTH) - 0.5)*2.0 y = (float(col)/float(IMG_HEIGHT) - 0.5)*2.0 - local = wp.vector(dtype=float, length=DIM_IN) + local = wp.vector(dtype=dtype, length=DIM_IN) # construct positional encoding for s in range(NUM_FREQ): @@ -107,14 +109,14 @@ def compute(batches: wp.array(dtype=int), scale = wp.pow(2.0, float(s))*wp.pi # x-coord - local[s*4 + 0] = wp.sin(x * scale) - local[s*4 + 1] = wp.cos(x * scale) + local[s*4 + 0] = dtype(wp.sin(x * scale)) + local[s*4 + 1] = dtype(wp.cos(x * scale)) # y-coord - local[s*4 + 2] = wp.sin(y * scale) - local[s*4 + 3] = wp.cos(y * scale) + local[s*4 + 2] = dtype(wp.sin(y * scale)) + local[s*4 + 3] = dtype(wp.cos(y * scale)) - # write input back to array so that torch can use it + # # write input back to array so that torch can use it input[s*4 + 0, linear] = local[s*4 + 0] input[s*4 + 1, linear] = local[s*4 + 1] input[s*4 + 2, linear] = local[s*4 + 2] @@ -141,31 +143,32 @@ def compute(batches: wp.array(dtype=int), # output layer w3 = wp.tile_load(weights_3, 0, 0, m=DIM_OUT, n=DIM_HID) b3 = wp.tile_load(bias_3, 0, 0, m=DIM_OUT, n=1) - o = wp.tile_map(sigmoid, wp.tile_matmul(w3, z) + wp.tile_broadcast(b3, m=DIM_OUT, n=NUM_THREADS)) + o = wp.tile_map(relu, wp.tile_matmul(w3, z) + wp.tile_broadcast(b3, m=DIM_OUT, n=NUM_THREADS)) # untile back to SIMT output = wp.untile(o) # compute error - error = wp.vec3(output[0] - reference[0,linear], - output[1] - reference[1,linear], - output[2] - reference[2,linear]) + error = wp.vec3(float(output[0]) - reference[0,linear], + float(output[1]) - reference[1,linear], + float(output[2]) - reference[2,linear]) # write MSE loss wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*BATCH_SIZE)) + # image output for i in range(DIM_OUT): - out[i, linear] = output[i] + out[i, linear] = float(output[i]) - weights_0, bias_0 = create_layer(DIM_IN, DIM_HID, dtype=float) - weights_1, bias_1 = create_layer(DIM_HID, DIM_HID, dtype=float) - weights_2, bias_2 = create_layer(DIM_HID, DIM_HID, dtype=float) - weights_3, bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=float) + weights_0, bias_0 = create_layer(DIM_IN, DIM_HID, dtype=dtype) + weights_1, bias_1 = create_layer(DIM_HID, DIM_HID, dtype=dtype) + weights_2, bias_2 = create_layer(DIM_HID, DIM_HID, dtype=dtype) + weights_3, bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=dtype) - input = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_IN) + input = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_IN, dtype=dtype) output = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_OUT) # # reference @@ -185,48 +188,98 @@ def compute(batches: wp.array(dtype=int), optimizer_inputs = [p.flatten() for p in params] optimizer = warp.optim.Adam(optimizer_inputs, lr=0.001) - max_iters = 500 - - for i in range(max_iters): - - # create randomized batch indices - batches = wp.array(rng.integers(low=0, high=IMG_WIDTH*IMG_HEIGHT, size=IMG_WIDTH*IMG_HEIGHT, dtype=np.int32)) - - - for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): - - loss.zero_() - - with wp.Tape() as tape: - wp.launch( - compute, - dim=[BATCH_SIZE], - inputs=[batches[b:b+BATCH_SIZE], - input, - weights_0, bias_0, - weights_1, bias_1, - weights_2, bias_2, - weights_3, bias_3, - reference, - loss, - output], - block_dim=NUM_THREADS) - - if b == 0: - print(f"Iter: {i} Loss: {loss.numpy()}") - - tape.backward(loss) - - # cosine weighted decay - optimizer.lr = 0.5*0.01*(1.0 + math.cos(float(i)/float(max_iters)*math.pi)) - optimizer.step(optimizer_grads) - - tape.zero() - - # uncommenting this line fixes convergence - # wp.synchronize() - - + num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) + max_iters = 5000 + max_epochs = int(max_iters/num_batches) + + # create randomized batch indices + batches = np.arange(0, IMG_WIDTH*IMG_HEIGHT, dtype=np.int32) + rng.shuffle(batches) + batches = wp.array(batches) + + with wp.ScopedTimer("Training"): + + for i in range(max_epochs): + + for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): + + loss.zero_() + + with wp.Tape() as tape: + wp.launch( + compute, + dim=[BATCH_SIZE], + inputs=[batches[b:b+BATCH_SIZE], + input, + weights_0, bias_0, + weights_1, bias_1, + weights_2, bias_2, + weights_3, bias_3, + reference, + loss, + output], + block_dim=NUM_THREADS) + + tape.backward(loss) + + verify = False + if verify: + + indices = batches[b:b+BATCH_SIZE].numpy() + + z_np = np.maximum(weights_0.numpy()@input.numpy()[:,indices] + bias_0.numpy(), 0.0) + z_np = np.maximum(weights_1.numpy()@z_np + bias_1.numpy(), 0.0) + z_np = np.maximum(weights_2.numpy()@z_np + bias_2.numpy(), 0.0) + z_np = np.maximum(weights_3.numpy()@z_np + bias_3.numpy(), 0.0) + + # test numpy foward + assert_equal(output.numpy()[:,indices], z_np) + + # torch + input_tc = tc.from_numpy(input.numpy()[:, indices]).requires_grad_(True) + + weights_0_tc = tc.from_numpy(weights_0.numpy()).requires_grad_(True) + bias_0_tc = tc.from_numpy(bias_0.numpy()).requires_grad_(True) + + weights_1_tc = tc.from_numpy(weights_1.numpy()).requires_grad_(True) + bias_1_tc = tc.from_numpy(bias_1.numpy()).requires_grad_(True) + + weights_2_tc = tc.from_numpy(weights_2.numpy()).requires_grad_(True) + bias_2_tc = tc.from_numpy(bias_2.numpy()).requires_grad_(True) + + weights_3_tc = tc.from_numpy(weights_3.numpy()).requires_grad_(True) + bias_3_tc = tc.from_numpy(bias_3.numpy()).requires_grad_(True) + + z_tc = tc.clamp(weights_0_tc@input_tc + bias_0_tc, min=0.0) + z_tc = tc.clamp(weights_1_tc@z_tc + bias_1_tc, min=0.0) + z_tc = tc.clamp(weights_2_tc@z_tc + bias_2_tc, min=0.0) + z_tc = tc.clamp(weights_3_tc@z_tc + bias_3_tc, min=0.0) + + ref_tc = tc.from_numpy(reference.numpy()[:, indices]).requires_grad_(True) + + l_tc = tc.mean((z_tc - ref_tc)**2) + l_tc.backward() + + # test torch + assert_equal(z_tc.cpu().detach().numpy(), output.numpy()[:, indices]) + assert_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy()) + assert_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy()) + assert_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy()) + assert_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy()) + assert_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy()) + assert_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy()) + assert_equal(weights_3.grad.numpy(), weights_3_tc.grad.cpu().detach().numpy()) + assert_equal(bias_3.grad.numpy(), bias_3_tc.grad.cpu().detach().numpy()) + + # cosine weighted decay + optimizer.lr = 0.5*0.01*(1.0 + math.cos(float(i)/float(max_iters)*math.pi)) + optimizer.step(optimizer_grads) + + tape.zero() + + print(f"Epoch: {i} Loss: {loss.numpy()}") + + predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) predicted_image = (predicted_image * 255).astype(np.uint8) @@ -241,56 +294,7 @@ def compute(batches: wp.array(dtype=int), # print(output) # numpy - z_np = np.maximum(weights_0.numpy()@input.numpy() + bias_0.numpy(), 0.0) - z_np = np.maximum(weights_1.numpy()@z_np + bias_1.numpy(), 0.0) - z_np = np.maximum(weights_2.numpy()@z_np + bias_2.numpy(), 0.0) - predicted_image = z_np.T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) - predicted_image = (predicted_image * 255).astype(np.uint8) - - predicted_image_pil = Image.fromarray(predicted_image) - predicted_image_pil.save("test_tile_mlp_np.jpg") - - # test numpy foward - print("NumPy output close: ", assert_equal(output.numpy(), z_np)) - - # torch - input_tc = tc.from_numpy(input.numpy()).requires_grad_(True) - - weights_0_tc = tc.from_numpy(weights_0.numpy()).requires_grad_(True) - bias_0_tc = tc.from_numpy(bias_0.numpy()).requires_grad_(True) - - weights_1_tc = tc.from_numpy(weights_1.numpy()).requires_grad_(True) - bias_1_tc = tc.from_numpy(bias_1.numpy()).requires_grad_(True) - - weights_2_tc = tc.from_numpy(weights_2.numpy()).requires_grad_(True) - bias_2_tc = tc.from_numpy(bias_2.numpy()).requires_grad_(True) - - z_tc = tc.clamp(weights_0_tc@input_tc + bias_0_tc, min=0.0) - z_tc = tc.clamp(weights_1_tc@z_tc + bias_1_tc, min=0.0) - z_tc = tc.clamp(weights_2_tc@z_tc + bias_2_tc, min=0.0) - - ref_tc = tc.from_numpy(reference.numpy()).requires_grad_(True) - - - l_tc = tc.mean((z_tc - ref_tc)**2) - l_tc.backward() - - #z_tc.backward(tc.ones_like(z_tc)) - - # test torch - print("Torch output close: ", assert_equal(z_tc.cpu().detach().numpy(), output.numpy())) - #print("Torch loss close: ", assert_equal(l_tc.cpu().detach().numpy(), loss.numpy())) - #print("Torch input.grad close: ", assert_equal(input.grad.numpy(), input_tc.grad.cpu().detach().numpy())) - - print("Torch weights0.grad close: ", assert_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy())) - print("Torch bias0.grad close: ", assert_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy())) - - print("Torch weights1.grad close: ", assert_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy())) - print("Torch bias1.grad close: ", assert_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy())) - - print("Torch weights2.grad close: ", assert_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy())) - print("Torch bias2.grad close: ", assert_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy())) From e1d1012621f12b58bc52b0ff8944d2cd086a048e Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Thu, 10 Oct 2024 16:07:07 -0700 Subject: [PATCH 067/102] MathDx 24.08 support in Warp + Tile --- .gitlab/ci/mathdx-support.yml | 8 ++-- examples/tile_fft.py | 4 +- examples/tile_matmul.py | 12 ++--- warp/builtins.py | 83 +++++++++++++++++----------------- warp/native/mathdx.cpp | 4 +- warp/native/warp.cu | 6 ++- warp/native/warp.h | 2 +- warp/tests/test_tile_mathdx.py | 12 ++--- 8 files changed, 67 insertions(+), 64 deletions(-) diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index bfca61fe..d7879267 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -36,7 +36,7 @@ linux-x86_64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/30/libmathdx_build_x86_64_ubuntu20.04_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/54/libmathdx_build_x86_64_ubuntu20.04_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -59,7 +59,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/30/libmathdx_build_aarch64_ubuntu20.04_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/54/libmathdx_build_aarch64_ubuntu20.04_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -101,7 +101,7 @@ linux-x86_64 test: - python -m pip install --upgrade usd-core - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - python -m pip install -U "jax[cuda12]" - - python -m pip install --upgrade nvidia-mathdx==24.4.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 + - python -m pip install --upgrade nvidia-mathdx==24.8.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" # HACK: disable P2P tests due to misbehaving agents @@ -118,7 +118,7 @@ linux-aarch64 test jetson: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - !reference [.snippets, install-python+warp-aarch64] - python -m pip install -U "jax[cuda12]" - - python -m pip install --upgrade nvidia-mathdx==24.4.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 + - python -m pip install --upgrade nvidia-mathdx==24.8.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - python -m warp.tests --junit-report-xml rspec.xml -s autodetect --failfast diff --git a/examples/tile_fft.py b/examples/tile_fft.py index edc6c101..f47e0b4a 100644 --- a/examples/tile_fft.py +++ b/examples/tile_fft.py @@ -16,8 +16,8 @@ def fft_tiled(x: wp.array2d(dtype=wp.vec2d), y: wp.array2d(dtype=wp.vec2d)): i, j, _ = wp.tid() a = wp.tile_load(x, i, j, m=TILE_M, n=TILE_N) - wp.tile_fft_dx(a) - wp.tile_ifft_dx(a) + wp.tile_fft(a) + wp.tile_ifft(a) wp.tile_store(y, i, j, a) diff --git a/examples/tile_matmul.py b/examples/tile_matmul.py index faedbee6..57b94bbc 100644 --- a/examples/tile_matmul.py +++ b/examples/tile_matmul.py @@ -10,21 +10,21 @@ @wp.kernel -def matmul_tiled(ga: wp.array2d(dtype=wp.float64), gb: wp.array2d(dtype=wp.float64), gc: wp.array2d(dtype=wp.float64)): +def matmul_tiled(ga: wp.array2d(dtype=wp.float32), gb: wp.array2d(dtype=wp.float16), gc: wp.array2d(dtype=wp.float64)): i, j, _ = wp.tid() a = wp.tile_load(ga, i, j, m=M, n=K) b = wp.tile_load(gb, i, j, m=K, n=N) c = wp.tile_zeros(m=M, n=N, dtype=wp.float64) - wp.tile_matmul_dx(a, b, c) + wp.tile_matmul(a, b, c) wp.tile_store(gc, i, j, c) -A = np.ones((M, K), dtype=np.float64) -B = 3 * np.ones((K, N), dtype=np.float64) +A = np.ones((M, K), dtype=np.float32) +B = 3 * np.ones((K, N), dtype=np.float16) C = np.zeros((M, N), dtype=np.float64) -A_wp = wp.array2d(A, dtype=wp.float64) -B_wp = wp.array2d(B, dtype=wp.float64) +A_wp = wp.array2d(A, dtype=wp.float32) +B_wp = wp.array2d(B, dtype=wp.float16) C_wp = wp.array2d(C, dtype=wp.float64) wp.launch(matmul_tiled, dim=[1, 1, BLOCK_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=BLOCK_DIM) diff --git a/warp/builtins.py b/warp/builtins.py index d8a38a5e..d3a9de7f 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5573,9 +5573,6 @@ def tile_matmul_generic_lto_dispatch_func( "tile_matmul() arguments must be tiles of float16, float32 or float64, vec2h, vec2f, vec2d entries" ) - if any(arg.type.dtype != out.type.dtype for arg in [a, b]): - raise RuntimeError("tile_matmul() arguments must have the same type") - if (a.type.N != b.type.M) or (a.type.M != out.type.M) or (b.type.N != out.type.N): raise RuntimeError("tile_matmul(A, B, C) requires sizes of A, B and C to be consistent for a matmul") @@ -5585,34 +5582,21 @@ def tile_matmul_generic_lto_dispatch_func( out.type.storage = "shared" template_args = [accumulate] - # Real - if out.type.dtype == float16: - dtype = "wp::float16" - precision = 2 # COMMONDX_PRECISION_F16 - element_type = 0 # CUBLASDX_TYPE_REAL - elif out.type.dtype == float32: - dtype = "wp::float32" - precision = 3 # COMMONDX_PRECISION_F32 - element_type = 0 # CUBLASDX_TYPE_REAL - elif out.type.dtype == float64: - dtype = "wp::float64" - precision = 4 # COMMONDX_PRECISION_F64 - element_type = 0 # CUBLASDX_TYPE_REAL - # Complex - elif out.type.dtype == vec2h: - dtype = "wp::vec2h" - precision = 2 # COMMONDX_PRECISION_F16 - element_type = 1 # CUBLASDX_TYPE_COMPLEX - elif out.type.dtype == vec2f: - dtype = "wp::vec2f" - precision = 3 # COMMONDX_PRECISION_F32 - element_type = 1 # CUBLASDX_TYPE_COMPLEX - elif out.type.dtype == vec2d: - dtype = "wp::vec2d" - precision = 4 # COMMONDX_PRECISION_F64 - element_type = 1 # CUBLASDX_TYPE_COMPLEX - else: - raise RuntimeError("Unsupported datatype") + def cublasdx_type_map(dtype): + if dtype == float16: + return ("wp::float16", 3, 0) + if dtype == float32: + return ("wp::float32", 5, 0) + if dtype == float64: + return ("wp::float64", 6, 0) + if dtype == vec2h: + return ("wp::vec2h", 3, 1) + if dtype == vec2f: + return ("wp::vec2f", 5, 1) + if dtype == vec2d: + return ("wp::vec2d", 6, 1) + raise RuntimeError("Unsupported input type in tile_matmul") + # generate the LTO M, K = a.type.M, a.type.N @@ -5620,7 +5604,17 @@ def tile_matmul_generic_lto_dispatch_func( num_threads = options["block_dim"] arch = options["output_arch"] - def make_function(M, N, K, tA, tB): + def make_function(M, N, K, adtype, bdtype, cdtype, tA, tB): + + (a_dtype, a_prec, a_type) = cublasdx_type_map(adtype) + (b_dtype, b_prec, b_type) = cublasdx_type_map(bdtype) + (c_dtype, c_prec, c_type) = cublasdx_type_map(cdtype) + + if (a_type != b_type or a_type != c_type): + raise RuntimeError("time_matmul(A, B, C) requires all inputs to be real or complex") + + element_type = a_type + # Warp follows Numpy: matrices are row-major # But cuBLASDx follows BLAS: matrices are col-major # So we have to flip M <-> N and A <-> B @@ -5631,7 +5625,7 @@ def make_transpose(t): return 1 # CUBLASDX_TRANSPOSE_MODE_TRANSPOSED raise RuntimeError("Invalid transpose mode") - lto_symbol = f"dot_{M}_{N}_{K}_{tA}_{tB}_{precision}_{element_type}" + lto_symbol = f"dot_{M}_{N}_{K}_{tA}_{tB}_{a_prec}_{b_prec}_{c_prec}_{element_type}" # early out if LTO for this combination already exists for this module if lto_symbol in builder.ltoirs: @@ -5650,7 +5644,9 @@ def make_transpose(t): N, M, K, - precision, + b_prec, + a_prec, + c_prec, element_type, make_transpose(tB), make_transpose(tA), @@ -5663,7 +5659,7 @@ def make_transpose(t): lto_code = f.read() builder.ltoirs[lto_symbol] = lto_code - builder.ltoirs_decl[lto_symbol] = f"void {lto_symbol}({dtype}, {dtype}*, {dtype}*, {dtype}, {dtype}*);" + builder.ltoirs_decl[lto_symbol] = f"void {lto_symbol}({c_dtype}, {b_dtype}*, {a_dtype}*, {c_dtype}, {c_dtype}*);" return lto_symbol, lto_code @@ -5683,13 +5679,16 @@ def tile_flip_layout(layout): b_layout = tile_layout_mode(b.type) c_layout = tile_layout_mode(out.type) - (fun_forward, lto_forward) = make_function(M, N, K, a_layout, b_layout) # C += A * B + # C += A * B + (fun_forward, lto_forward) = make_function(M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a_layout, b_layout) + # adjA += adjC * B^T (fun_backward_A, lto_backward_A) = make_function( - M, K, N, c_layout, tile_flip_layout(b_layout) - ) # adjA += adjC * B^T + M, K, N, out.type.dtype, b.type.dtype, a.type.dtype, c_layout, tile_flip_layout(b_layout) + ) + # adjB += A^T * adjC (fun_backward_B, lto_backward_B) = make_function( - K, N, M, tile_flip_layout(a_layout), c_layout - ) # adjB += A^T * adjC + K, N, M, a.type.dtype, out.type.dtype, b.type.dtype, tile_flip_layout(a_layout), c_layout + ) return ( ( @@ -5803,10 +5802,10 @@ def tile_fft_generic_lto_dispatch_func( if inout.type.dtype == vec2f: dtype = "wp::vec2f" - precision = 3 # COMMONDX_PRECISION_F32 + precision = 5 # COMMONDX_PRECISION_F32 elif inout.type.dtype == vec2d: dtype = "wp::vec2d" - precision = 4 # COMMONDX_PRECISION_F64 + precision = 6 # COMMONDX_PRECISION_F64 else: raise RuntimeError("Unsupported datatype") diff --git a/warp/native/mathdx.cpp b/warp/native/mathdx.cpp index 1dca0afa..75a83e3d 100644 --- a/warp/native/mathdx.cpp +++ b/warp/native/mathdx.cpp @@ -41,7 +41,9 @@ WP_API bool cuda_compile_dot( int M, int N, int K, - int precision, + int precision_A, + int precision_B, + int precision_C, int type, int tA, int tB, diff --git a/warp/native/warp.cu b/warp/native/warp.cu index 7ae7b634..bb6bb8e7 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -2905,6 +2905,7 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); } CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); + CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/../external/cutlass/include").c_str())); size_t lto_size = 0; CHECK_CUFFTDX(cufftDxGetLTOIRSize(h, <o_size)); @@ -2925,7 +2926,7 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ return res; } - bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision, int type, int tA, int tB, int num_threads) + bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision_A, int precision_B, int precision_C, int type, int tA, int tB, int num_threads) { CHECK_ANY(ltoir_output_path != nullptr); @@ -2940,7 +2941,8 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_FUNCTION, cublasDxFunction::CUBLASDX_FUNCTION_MM)); CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_EXECUTION, commonDxExecution::COMMONDX_EXECUTION_BLOCK)); CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_API, cublasDxApi::CUBLASDX_API_BLOCK_SMEM)); - CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_PRECISION, (commonDxPrecision)precision)); + std::array precisions = {precision_A, precision_B, precision_C}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_PRECISION, 3, precisions.data())); CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SM, (long long)(arch * 10))); CHECK_CUBLASDX(cublasDxSetOperatorInt64(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TYPE, (cublasDxType)type)); std::array block_dim = {num_threads, 1, 1}; diff --git a/warp/native/warp.h b/warp/native/warp.h index 045d5f0a..f913c006 100644 --- a/warp/native/warp.h +++ b/warp/native/warp.h @@ -319,7 +319,7 @@ extern "C" WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes); WP_API bool cuda_compile_fft(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int size, int elements_per_thread, int direction, int precision, int* shared_memory_size); - WP_API bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision, int type, int tA, int tB, int num_threads); + WP_API bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision_A, int precision_B, int precision_C, int type, int tA, int tB, int num_threads); WP_API void* cuda_load_module(void* context, const char* ptx); WP_API void cuda_unload_module(void* context, void* module); diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py index 50b71404..2c8d7180 100644 --- a/warp/tests/test_tile_mathdx.py +++ b/warp/tests/test_tile_mathdx.py @@ -26,7 +26,7 @@ @wp.kernel() def tile_math_matmul_kernel( - ga: wp.array2d(dtype=wp.float64), gb: wp.array2d(dtype=wp.float64), gc: wp.array2d(dtype=wp.float64) + ga: wp.array2d(dtype=wp.float16), gb: wp.array2d(dtype=wp.float32), gc: wp.array2d(dtype=wp.float64) ): i, j = wp.tid() a = wp.tile_load(ga, i, j, m=TILE_M, n=TILE_K) @@ -39,8 +39,8 @@ def tile_math_matmul_kernel( def test_tile_math_matmul(test, device): rng = np.random.default_rng(42) - A = rng.random((TILE_M, TILE_K), dtype=np.float64) - B = rng.random((TILE_K, TILE_N), dtype=np.float64) + A = rng.random((TILE_M, TILE_K), dtype=np.float64).astype(np.float16) + B = rng.random((TILE_K, TILE_N), dtype=np.float32) C = np.zeros((TILE_M, TILE_N), dtype=np.float64) A_wp = wp.array(A, requires_grad=True, device=device) @@ -57,14 +57,14 @@ def test_tile_math_matmul(test, device): ) # verify forward pass - assert_np_equal(C_wp.numpy(), A @ B) + assert_np_equal(C_wp.numpy(), A @ B, tol=1e-2) adj_C = np.ones_like(C) tape.backward(grads={C_wp: wp.array(adj_C, device=device)}) - assert_np_equal(A_wp.grad.numpy(), adj_C @ B.T) - assert_np_equal(B_wp.grad.numpy(), A.T @ adj_C) + assert_np_equal(A_wp.grad.numpy(), adj_C @ B.T, tol=1e-2) + assert_np_equal(B_wp.grad.numpy(), A.T @ adj_C, tol=1e-2) @wp.kernel() From 241fd3e217adf55e7d41cdef35c3cc836e306409 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 15 Oct 2024 08:08:55 +0000 Subject: [PATCH 068/102] Fix for tile MLP unit test --- warp/tests/assets/pixel.npy | Bin 0 -> 196736 bytes warp/tests/test_tile_mlp.py | 229 +++++++++++++++++++----------------- 2 files changed, 120 insertions(+), 109 deletions(-) create mode 100644 warp/tests/assets/pixel.npy diff --git a/warp/tests/assets/pixel.npy b/warp/tests/assets/pixel.npy new file mode 100644 index 0000000000000000000000000000000000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||c?23XHA2?5NAteOlK zOj3c}>;1fFV1Od>PE;gS*tCx57a?E*1+QoK!A)4cbxfghwniY3N0YGZO7Mtb>68Kn zu3LohOk?5LAE`YAlS@I#&a~|GRl)e<8ApK-a*o#drcq%f&NWeUI8x!7sP#iTkP(hX z>)aDauLunaKcXPGCh9;A9gbAJ7`XYg@7nVR*Fi4_hTP>LC7MM*?)$ z&V%}cabi@%jcb`uJzQhB2u$2k%G2T6pz=yT9}HF}m5hTa)&%m|X-qqf)2dy7Ec$T5updn>yn^ckt8CtYtNqA)ZU6Qf|`!qQY>0op4d5h{3j;Co&!2*MJi zd#XBVru?VlLDWLYtq7tPdPED2ve7kM=N&;j@A)jJrM`REmN9^bf;{T`EILtJRFT{R zx8hRlv%A+kV|a*v*n#=o^WH;IRnkN$%y6FdeV$U`vKnG!8kLljj~-N{l*D%{LYM-a zgB4&6K>YirDzkdR@eC`fz`7?9r%SCc-N1bZ+H)|DgkXU94}d6=5WHd>Ob3t!aM$2` zZ@31Ug-1#%FLoVG;-B`yVgmszUFTR`K#B>g@E#E&B*TCV4qmYx^oxNC$0!1ZBbW{n z8Vba}XR0243Ml~JmV)?qP1dAsuz@cGr0p@bYvdgKYcMCU+fI)n_Xh&!8B`6 M%ydauUK!{A1EZB6BLDyZ literal 0 HcmV?d00001 diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 34f5ff60..ac661549 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -3,36 +3,15 @@ import warp.examples import warp.optim -import torch as tc +from warp.tests.unittest_utils import * import math import os -from PIL import Image - -#wp.config.mode = "debug" -#wp.config.verify_fp = True -#wp.config.verify_cuda = True - -wp.set_device("cuda:0") -wp.set_module_options({"fast_math": False}) - -#wp.clear_kernel_cache() - -rng = np.random.default_rng(45) - -def assert_equal(result: np.ndarray, expect: np.ndarray, tol=1.e-2): - if tol != 0.0: - # TODO: Get all tests working without the .flatten() - np.testing.assert_allclose(result.flatten(), expect.flatten(), rtol=tol, atol=1.e-2, equal_nan=True) - else: - # TODO: Get all tests working with strict=True - np.testing.assert_array_equal(result, expect) - - return True - +# needs to be constant for the whole module +NUM_THREADS = 32 -def create_layer(dim_in, dim_hid, dtype=float): +def create_layer(rng, dim_in, dim_hid, dtype=float): w = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) b = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, 1)) @@ -42,7 +21,7 @@ def create_layer(dim_in, dim_hid, dtype=float): return (weights, bias) -def create_array(dim_in, dim_hid, dtype=float): +def create_array(rng, dim_in, dim_hid, dtype=float): s = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) a = wp.array(s, dtype=dtype, requires_grad=True) @@ -50,22 +29,22 @@ def create_array(dim_in, dim_hid, dtype=float): return a -NUM_FREQ = wp.constant(8) +def test_multi_layer_nn(test, device): -DIM_IN = wp.constant(4*NUM_FREQ) # sin,cos for both x,y at each frequenecy -DIM_HID = 32 -DIM_OUT = 3 + import torch as tc -NUM_THREADS = 32 + NUM_FREQ = wp.constant(8) -IMG_WIDTH = NUM_THREADS*16 -IMG_HEIGHT = NUM_THREADS*16 + DIM_IN = wp.constant(4*NUM_FREQ) # sin,cos for both x,y at each frequenecy + DIM_HID = 32 + DIM_OUT = 3 -BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) + IMG_WIDTH = NUM_THREADS*8 + IMG_HEIGHT = NUM_THREADS*8 -dtype = wp.float16 + BATCH_SIZE = min(512, int((IMG_WIDTH*IMG_HEIGHT)/8)) -def test_multi_layer_nn(): + dtype = wp.float16 @wp.func def relu(x: dtype): @@ -90,9 +69,6 @@ def compute(batches: wp.array(dtype=int), loss: wp.array1d(dtype=float), out: wp.array2d(dtype=float)): - # row, col = wp.tid() - # linear = row*IMG_WIDTH + col - linear = batches[wp.tid()] row = linear/IMG_WIDTH col = linear%IMG_WIDTH @@ -116,7 +92,7 @@ def compute(batches: wp.array(dtype=int), local[s*4 + 2] = dtype(wp.sin(y * scale)) local[s*4 + 3] = dtype(wp.cos(y * scale)) - # # write input back to array so that torch can use it + # write input back to array so that torch can use it input[s*4 + 0, linear] = local[s*4 + 0] input[s*4 + 1, linear] = local[s*4 + 1] input[s*4 + 2, linear] = local[s*4 + 2] @@ -148,6 +124,7 @@ def compute(batches: wp.array(dtype=int), # untile back to SIMT output = wp.untile(o) + # compute error error = wp.vec3(float(output[0]) - reference[0,linear], float(output[1]) - reference[1,linear], @@ -162,20 +139,26 @@ def compute(batches: wp.array(dtype=int), out[i, linear] = float(output[i]) + rng = np.random.default_rng(45) - weights_0, bias_0 = create_layer(DIM_IN, DIM_HID, dtype=dtype) - weights_1, bias_1 = create_layer(DIM_HID, DIM_HID, dtype=dtype) - weights_2, bias_2 = create_layer(DIM_HID, DIM_HID, dtype=dtype) - weights_3, bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=dtype) + weights_0, bias_0 = create_layer(rng, DIM_IN, DIM_HID, dtype=dtype) + weights_1, bias_1 = create_layer(rng, DIM_HID, DIM_HID, dtype=dtype) + weights_2, bias_2 = create_layer(rng, DIM_HID, DIM_HID, dtype=dtype) + weights_3, bias_3 = create_layer(rng, DIM_HID, DIM_OUT, dtype=dtype) - input = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_IN, dtype=dtype) - output = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_OUT) + input = create_array(rng, IMG_WIDTH*IMG_HEIGHT, DIM_IN, dtype=dtype) + output = create_array(rng, IMG_WIDTH*IMG_HEIGHT, DIM_OUT) - # # reference + # generate reference image + from PIL import Image reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") with Image.open(reference_path) as im: - reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) / 255.0 - reference = wp.array(reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T, dtype=float) + reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) + reference_np = reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T + np.save(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), reference_np, allow_pickle=True) + + reference_np = np.load(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), allow_pickle=True)/255.0 + reference = wp.array(reference_np, dtype=float) loss = wp.zeros(1, dtype=float, requires_grad=True) @@ -186,20 +169,19 @@ def compute(batches: wp.array(dtype=int), optimizer_grads = [p.grad.flatten() for p in params] optimizer_inputs = [p.flatten() for p in params] - optimizer = warp.optim.Adam(optimizer_inputs, lr=0.001) + optimizer = warp.optim.Adam(optimizer_inputs, lr=0.01) num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) - max_iters = 5000 - max_epochs = int(max_iters/num_batches) + max_epochs = 30 # create randomized batch indices batches = np.arange(0, IMG_WIDTH*IMG_HEIGHT, dtype=np.int32) rng.shuffle(batches) batches = wp.array(batches) - with wp.ScopedTimer("Training"): + with wp.ScopedTimer("Training", active=False): - for i in range(max_epochs): + for epoch in range(max_epochs): for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): @@ -222,8 +204,10 @@ def compute(batches: wp.array(dtype=int), tape.backward(loss) - verify = False - if verify: + # check outputs + grads on the first few epoch only + # since this is a relatively slow operation + verify = True + if verify and epoch < 3: indices = batches[b:b+BATCH_SIZE].numpy() @@ -233,7 +217,7 @@ def compute(batches: wp.array(dtype=int), z_np = np.maximum(weights_3.numpy()@z_np + bias_3.numpy(), 0.0) # test numpy foward - assert_equal(output.numpy()[:,indices], z_np) + assert_np_equal(output.numpy()[:,indices], z_np, tol=1.e-2) # torch input_tc = tc.from_numpy(input.numpy()[:, indices]).requires_grad_(True) @@ -261,47 +245,42 @@ def compute(batches: wp.array(dtype=int), l_tc.backward() # test torch - assert_equal(z_tc.cpu().detach().numpy(), output.numpy()[:, indices]) - assert_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy()) - assert_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy()) - assert_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy()) - assert_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy()) - assert_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy()) - assert_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy()) - assert_equal(weights_3.grad.numpy(), weights_3_tc.grad.cpu().detach().numpy()) - assert_equal(bias_3.grad.numpy(), bias_3_tc.grad.cpu().detach().numpy()) - - # cosine weighted decay - optimizer.lr = 0.5*0.01*(1.0 + math.cos(float(i)/float(max_iters)*math.pi)) - optimizer.step(optimizer_grads) + assert_np_equal(z_tc.cpu().detach().numpy(), output.numpy()[:, indices], tol=1.e-2) + assert_np_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(weights_3.grad.numpy(), weights_3_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(bias_3.grad.numpy(), bias_3_tc.grad.cpu().detach().numpy(), tol=1.e-2) + optimizer.step(optimizer_grads) tape.zero() - print(f"Epoch: {i} Loss: {loss.numpy()}") - - - - predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) - predicted_image = (predicted_image * 255).astype(np.uint8) + #print(f"Epoch: {epoch} Loss: {loss.numpy()}") - predicted_image_pil = Image.fromarray(predicted_image) - predicted_image_pil.save("test_tile_mlp_wp.jpg") + # predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) + # predicted_image = (predicted_image * 255).astype(np.uint8) - return + # predicted_image_pil = Image.fromarray(predicted_image) + # predicted_image_pil.save("test_tile_mlp_wp.jpg") + # initial loss is ~0.061 + assert loss.numpy()[0] < 0.002 - # print(input) - # print(output) - # numpy - +def test_single_layer_nn(test, device): + import torch as tc + DIM_IN = 8 + DIM_HID = 32 + DIM_OUT = 16 - -def test_single_layer_nn(): + NUM_BLOCKS = 56 @wp.func def relu(x: float): @@ -325,40 +304,72 @@ def compute(input: wp.array2d(dtype=float), wp.tile_store(out, 0, i, o) - weights, bias = create_layer(DIM_IN, DIM_OUT, dtype=float) + with wp.ScopedDevice(device): + + rng = np.random.default_rng(45) + + # single layer weights, bias + weights, bias = create_layer(rng, DIM_IN, DIM_OUT, dtype=float) + + input = create_array(rng, NUM_THREADS*NUM_BLOCKS, DIM_IN) + output = create_array(rng, NUM_THREADS*NUM_BLOCKS, DIM_OUT) + + with wp.Tape() as tape: + wp.launch_tiled(compute, dim=[NUM_BLOCKS], inputs=[input, weights, bias, output], block_dim=NUM_THREADS) + + output.grad = wp.ones_like(output) + tape.backward() + + # numpy + output_np = np.maximum(weights.numpy()@input.numpy() + bias.numpy(), 0.0) + + # test numpy foward + assert_np_equal(output.numpy(), output_np, tol=1.e-2) + - input = create_array(NUM_THREADS*NUM_BLOCKS, DIM_IN) - output = create_array(NUM_THREADS*NUM_BLOCKS, DIM_OUT) + # torch + weights_tc = tc.from_numpy(weights.numpy()).requires_grad_(True) # use .numpy() to avoid any memory aliasing + input_tc = tc.from_numpy(input.numpy()).requires_grad_(True) + bias_tc = tc.from_numpy(bias.numpy()).requires_grad_(True) - with wp.Tape() as tape: - wp.launch_tiled(compute, dim=[NUM_BLOCKS], inputs=[input, weights, bias, output], block_dim=NUM_THREADS) + output_tc = tc.clamp(weights_tc@input_tc + bias_tc, min=0.0) + output_tc.backward(tc.ones_like(output_tc)) - output.grad = wp.ones_like(output) - tape.backward() + # test torch + assert_np_equal(output_tc.detach().numpy(), output.numpy(), tol=1.e-2) + assert_np_equal(input.grad.numpy(), input_tc.grad.detach().numpy(), tol=1.e-2) - # print(input) - # print(output) +class TestTileMLP(unittest.TestCase): + pass - # numpy - output_np = np.maximum(weights.numpy()@input.numpy() + bias.numpy(), 0.0) +test_devices = get_test_devices() - # test numpy foward - print(np.allclose(output.numpy(), output_np)) +try: + import torch + # check which Warp devices work with Torch + # CUDA devices may fail if Torch was not compiled with CUDA support + torch_compatible_devices = [] + torch_compatible_cuda_devices = [] - # torch - weights_tc = tc.from_numpy(weights.numpy()).requires_grad_(True) # use .numpy() to avoid any memory aliasing - input_tc = tc.from_numpy(input.numpy()).requires_grad_(True) - bias_tc = tc.from_numpy(bias.numpy()).requires_grad_(True) + for d in test_devices: + try: + t = torch.arange(10, device=wp.device_to_torch(d)) + t += 1 + torch_compatible_devices.append(d) + if d.is_cuda: + torch_compatible_cuda_devices.append(d) + except Exception as e: + print(f"Skipping Torch tests on device '{d}' due to exception: {e}") - output_tc = tc.clamp(weights_tc@input_tc + bias_tc, min=0.0) - output_tc.backward(tc.ones_like(output_tc)) + add_function_test(TestTileMLP, "test_single_layer_nn", test_single_layer_nn, check_output=False, devices=torch_compatible_cuda_devices) + add_function_test(TestTileMLP, "test_multi_layer_nn", test_multi_layer_nn, check_output=False, devices=torch_compatible_cuda_devices) - # test torch - print(np.allclose(output_tc.detach().numpy(), output.numpy())) - print(np.allclose(input.grad.numpy(), input_tc.grad.detach().numpy())) +except Exception as e: + print(f"Skipping Torch tests due to exception: {e}") -#test_single_layer_nn() -test_multi_layer_nn() \ No newline at end of file +if __name__ == "__main__": +# wp.clear_kernel_cache() + unittest.main(verbosity=2, failfast=True) From a3a5c63e688ed191f2356102b85f5f6ceacd99e7 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Tue, 15 Oct 2024 08:10:15 +0000 Subject: [PATCH 069/102] Disable reference image loading in MLP unit test --- warp/tests/test_tile_mlp.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index ac661549..693dffe3 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -149,13 +149,13 @@ def compute(batches: wp.array(dtype=int), input = create_array(rng, IMG_WIDTH*IMG_HEIGHT, DIM_IN, dtype=dtype) output = create_array(rng, IMG_WIDTH*IMG_HEIGHT, DIM_OUT) - # generate reference image - from PIL import Image - reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") - with Image.open(reference_path) as im: - reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) - reference_np = reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T - np.save(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), reference_np, allow_pickle=True) + # # generate reference image + # from PIL import Image + # reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") + # with Image.open(reference_path) as im: + # reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) + # reference_np = reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T + # np.save(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), reference_np, allow_pickle=True) reference_np = np.load(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), allow_pickle=True)/255.0 reference = wp.array(reference_np, dtype=float) From fcc95c072c3e48d7f6df72d460c1909dbc4cf8c6 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Tue, 15 Oct 2024 13:00:44 -0700 Subject: [PATCH 070/102] Tile/Mathdx: simplifying matmul implementation using arrangement --- .gitlab/ci/mathdx-support.yml | 4 +-- warp/builtins.py | 66 +++++++++++++++-------------------- warp/context.py | 9 +++-- warp/native/mathdx.cpp | 5 +-- warp/native/tile.h | 11 +++--- warp/native/warp.cu | 6 ++-- warp/native/warp.h | 2 +- 7 files changed, 48 insertions(+), 55 deletions(-) diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index d7879267..3b78b4d5 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -36,7 +36,7 @@ linux-x86_64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/54/libmathdx_build_x86_64_ubuntu20.04_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/68/libmathdx_build_x86_64_ubuntu20.04_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -59,7 +59,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/54/libmathdx_build_aarch64_ubuntu20.04_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/68/libmathdx_build_aarch64_ubuntu20.04_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps diff --git a/warp/builtins.py b/warp/builtins.py index 1c9d5ecc..b34533f0 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5624,6 +5624,7 @@ def tile_matmul_generic_lto_dispatch_func( out.type.storage = "shared" template_args = [accumulate] + # Maps Python/Warp types to C++ types and enums def cublasdx_type_map(dtype): if dtype == float16: return ("wp::float16", 3, 0) @@ -5638,7 +5639,13 @@ def cublasdx_type_map(dtype): if dtype == vec2d: return ("wp::vec2d", 6, 1) raise RuntimeError("Unsupported input type in tile_matmul") - + + def cublasdx_arrangement_map(layout): + if layout == "colmajor": + return 0 # CUBLASDX_ARRANGEMENT_COL_MAJOR + if layout == "rowmajor": + return 1 # CUBLASDX_ARRANGEMENT_ROW_MAJOR + raise RuntimeError("Unsupported layout in tile_matmul") # generate the LTO M, K = a.type.M, a.type.N @@ -5646,28 +5653,20 @@ def cublasdx_type_map(dtype): num_threads = options["block_dim"] arch = options["output_arch"] - def make_function(M, N, K, adtype, bdtype, cdtype, tA, tB): + def make_function(M, N, K, adtype, bdtype, cdtype, alayout, blayout, clayout): (a_dtype, a_prec, a_type) = cublasdx_type_map(adtype) (b_dtype, b_prec, b_type) = cublasdx_type_map(bdtype) (c_dtype, c_prec, c_type) = cublasdx_type_map(cdtype) + a_arrangement = cublasdx_arrangement_map(alayout) + b_arrangement = cublasdx_arrangement_map(blayout) + c_arrangement = cublasdx_arrangement_map(clayout) if (a_type != b_type or a_type != c_type): raise RuntimeError("time_matmul(A, B, C) requires all inputs to be real or complex") - element_type = a_type - # Warp follows Numpy: matrices are row-major - # But cuBLASDx follows BLAS: matrices are col-major - # So we have to flip M <-> N and A <-> B - def make_transpose(t): - if t == "N": - return 0 # CUBLASDX_TRANSPOSE_MODE_NON_TRANSPOSED - elif t == "T": - return 1 # CUBLASDX_TRANSPOSE_MODE_TRANSPOSED - raise RuntimeError("Invalid transpose mode") - - lto_symbol = f"dot_{M}_{N}_{K}_{tA}_{tB}_{a_prec}_{b_prec}_{c_prec}_{element_type}" + lto_symbol = f"dot_{M}_{N}_{K}_{a_arrangement}_{b_arrangement}_{c_arrangement}_{a_prec}_{b_prec}_{c_prec}_{element_type}" # early out if LTO for this combination already exists for this module if lto_symbol in builder.ltoirs: @@ -5683,15 +5682,16 @@ def make_transpose(t): include_dirs, get_mathdx_include_dirs(), arch, - N, M, + N, K, - b_prec, a_prec, + b_prec, c_prec, element_type, - make_transpose(tB), - make_transpose(tA), + a_arrangement, + b_arrangement, + c_arrangement, num_threads, ) if not result: @@ -5701,35 +5701,25 @@ def make_transpose(t): lto_code = f.read() builder.ltoirs[lto_symbol] = lto_code - builder.ltoirs_decl[lto_symbol] = f"void {lto_symbol}({c_dtype}, {b_dtype}*, {a_dtype}*, {c_dtype}, {c_dtype}*);" + builder.ltoirs_decl[lto_symbol] = f"void {lto_symbol}({c_dtype}, {a_dtype}*, {b_dtype}*, {c_dtype}, {c_dtype}*);" return lto_symbol, lto_code - def tile_layout_mode(tile): - if tile.layout == "rowmajor": - return "N" - if tile.layout == "colmajor": - return "T" - def tile_flip_layout(layout): - if layout == "N": - return "T" - elif layout == "T": - return "N" - - a_layout = tile_layout_mode(a.type) - b_layout = tile_layout_mode(b.type) - c_layout = tile_layout_mode(out.type) + if layout == "rowmajor": + return "colmajor" + elif layout == "colmajor": + return "rowmajor" # C += A * B - (fun_forward, lto_forward) = make_function(M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a_layout, b_layout) - # adjA += adjC * B^T + (fun_forward, lto_forward) = make_function(M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a.type.layout, b.type.layout, out.type.layout) + # adjA += adjC * B^T - Tranpose ~= flipped layout (fun_backward_A, lto_backward_A) = make_function( - M, K, N, out.type.dtype, b.type.dtype, a.type.dtype, c_layout, tile_flip_layout(b_layout) + M, K, N, out.type.dtype, b.type.dtype, a.type.dtype, out.type.layout, tile_flip_layout(b.type.layout), a.type.layout ) - # adjB += A^T * adjC + # adjB += A^T * adjC - Tranpose ~= flipped layout (fun_backward_B, lto_backward_B) = make_function( - K, N, M, a.type.dtype, out.type.dtype, b.type.dtype, tile_flip_layout(a_layout), c_layout + K, N, M, a.type.dtype, out.type.dtype, b.type.dtype, tile_flip_layout(a.type.layout), out.type.layout, b.type.layout ) return ( diff --git a/warp/context.py b/warp/context.py index 281a6009..65ddeebe 100644 --- a/warp/context.py +++ b/warp/context.py @@ -3398,10 +3398,13 @@ def __init__(self): ctypes.c_int, # M ctypes.c_int, # N ctypes.c_int, # K - ctypes.c_int, # precision + ctypes.c_int, # a_precision + ctypes.c_int, # b_precision + ctypes.c_int, # c_precision ctypes.c_int, # type - ctypes.c_int, # tA - ctypes.c_int, # tB + ctypes.c_int, # a_arrangement + ctypes.c_int, # b_arrangement + ctypes.c_int, # c_arrangement ctypes.c_int, # num threads ] self.core.cuda_compile_dot.restype = ctypes.c_bool diff --git a/warp/native/mathdx.cpp b/warp/native/mathdx.cpp index 75a83e3d..c540c873 100644 --- a/warp/native/mathdx.cpp +++ b/warp/native/mathdx.cpp @@ -45,8 +45,9 @@ WP_API bool cuda_compile_dot( int precision_B, int precision_C, int type, - int tA, - int tB, + int a_arrangement, + int b_arrangement, + int c_arrangement, int num_threads) { printf("CUDA is disabled and/or Warp was not compiled with MathDx support.\n"); diff --git a/warp/native/tile.h b/warp/native/tile.h index 8df8e202..a8c3534d 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -1296,14 +1296,13 @@ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_ adj_t.adj_extract(i, j, adj_ret); } -// cuBLASDx follows the BLAS convention: matrices are col-major, so we swap A & B in the code below template TileC& tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C) { using T = typename TileA::Type; WP_TILE_SYNC(); - fun_forward(T(1.0), B.data, A.data, T(Add), C.data); + fun_forward(T(1.0), A.data, B.data, T(Add), C.data); WP_TILE_SYNC(); return C; @@ -1317,8 +1316,8 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, using T = typename TileA::Type; WP_TILE_SYNC(); - fun_backward_A(T(1.0), B.data, adj_C.data, T(1.0), adj_A.data); - fun_backward_B(T(1.0), adj_C.data, A.data, T(1.0), adj_B.data); + fun_backward_A(T(1.0), adj_C.data, B.data, T(1.0), adj_A.data); + fun_backward_B(T(1.0), A.data, adj_C.data, T(1.0), adj_B.data); WP_TILE_SYNC(); } @@ -1330,8 +1329,8 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, using T = typename TileA::Type; WP_TILE_SYNC(); - fun_backward_A(T(1.0), B.data, adj_C.data, T(1.0), adj_A.data); - fun_backward_B(T(1.0), adj_C.data, A.data, T(1.0), adj_B.data); + fun_backward_A(T(1.0), adj_C.data, B.data, T(1.0), adj_A.data); + fun_backward_B(T(1.0), A.data, adj_C.data, T(1.0), adj_B.data); WP_TILE_SYNC(); } diff --git a/warp/native/warp.cu b/warp/native/warp.cu index bb6bb8e7..b043aeba 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -2926,7 +2926,7 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ return res; } - bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision_A, int precision_B, int precision_C, int type, int tA, int tB, int num_threads) + bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision_A, int precision_B, int precision_C, int type, int arrangement_A, int arrangement_B, int arrangement_C, int num_threads) { CHECK_ANY(ltoir_output_path != nullptr); @@ -2949,8 +2949,8 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_BLOCK_DIM, block_dim.size(), block_dim.data())); std::array size = {M, N, K}; CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_SIZE, size.size(), size.data())); - std::array transpose_mode = {(cublasDxTransposeMode_t)tA, (cublasDxTransposeMode_t)tB}; - CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_TRANSPOSE_MODE, transpose_mode.size(), transpose_mode.data())); + std::array arrangement = {arrangement_A, arrangement_B, arrangement_C}; + CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_ARRANGEMENT, arrangement.size(), arrangement.data())); CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); for(int dir = 0; dir < num_include_dirs; dir++) diff --git a/warp/native/warp.h b/warp/native/warp.h index f913c006..33c878d2 100644 --- a/warp/native/warp.h +++ b/warp/native/warp.h @@ -319,7 +319,7 @@ extern "C" WP_API size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_dir, int num_cuda_include_dirs, const char** cuda_include_dirs, bool debug, bool verbose, bool verify_fp, bool fast_math, const char* output_path, size_t num_ltoirs, char** ltoirs, size_t* ltoir_sizes); WP_API bool cuda_compile_fft(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int size, int elements_per_thread, int direction, int precision, int* shared_memory_size); - WP_API bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision_A, int precision_B, int precision_C, int type, int tA, int tB, int num_threads); + WP_API bool cuda_compile_dot(const char* ltoir_output_path, const char* symbol_name, int num_include_dirs, const char** include_dirs, const char* mathdx_include_dir, int arch, int M, int N, int K, int precision_A, int precision_B, int precision_C, int type, int arrangement_A, int arrangement_B, int arrangement_C, int num_threads); WP_API void* cuda_load_module(void* context, const char* ptx); WP_API void cuda_unload_module(void* context, void* module); From 90bc5353a4ea4572c060a722ff4ba56baaf85df0 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Tue, 15 Oct 2024 14:16:22 -0700 Subject: [PATCH 071/102] Update libmathdx artifactory paths + typo --- .gitlab/ci/mathdx-support.yml | 4 ++-- warp/builtins.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index 3b78b4d5..4b85d124 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -36,7 +36,7 @@ linux-x86_64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/68/libmathdx_build_x86_64_ubuntu20.04_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/69/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -59,7 +59,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/68/libmathdx_build_aarch64_ubuntu20.04_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/69/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps diff --git a/warp/builtins.py b/warp/builtins.py index b34533f0..e733a7c3 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5713,11 +5713,11 @@ def tile_flip_layout(layout): # C += A * B (fun_forward, lto_forward) = make_function(M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a.type.layout, b.type.layout, out.type.layout) - # adjA += adjC * B^T - Tranpose ~= flipped layout + # adjA += adjC * B^T - Transpose ~= flipped layout (fun_backward_A, lto_backward_A) = make_function( M, K, N, out.type.dtype, b.type.dtype, a.type.dtype, out.type.layout, tile_flip_layout(b.type.layout), a.type.layout ) - # adjB += A^T * adjC - Tranpose ~= flipped layout + # adjB += A^T * adjC - Transpose ~= flipped layout (fun_backward_B, lto_backward_B) = make_function( K, N, M, a.type.dtype, out.type.dtype, b.type.dtype, tile_flip_layout(a.type.layout), out.type.layout, b.type.layout ) From a54851c19033dea8d723afa918f86f0922222a0b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 17 Oct 2024 03:02:03 +0000 Subject: [PATCH 072/102] Skip MLP unit tests on non-math DX platforms --- warp/tests/test_tile_mlp.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 693dffe3..d79ce897 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -8,6 +8,8 @@ import math import os +wp.init() + # needs to be constant for the whole module NUM_THREADS = 32 @@ -28,7 +30,7 @@ def create_array(rng, dim_in, dim_hid, dtype=float): return a - +@unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") def test_multi_layer_nn(test, device): import torch as tc @@ -271,7 +273,7 @@ def compute(batches: wp.array(dtype=int), - +@unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") def test_single_layer_nn(test, device): import torch as tc From e577c7d98c3b825bf7edce4db6d6a2de638b872d Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Thu, 17 Oct 2024 08:59:25 +0000 Subject: [PATCH 073/102] Change to make all shared tile write operations synchronize. --- warp/native/tile.h | 54 +++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index 8df8e202..f4d8871c 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -102,15 +102,25 @@ */ -// wp.tile_load(A, offset, shape) -// wp.tile_load(A, (x, y), (16, 16)) -// wp.tile_load(A, (x, y, z), (3, 3, 3)) - -// wp.tile_load(A, index, shape) -// wp.tile_load(A, x, m) -// wp.tile_load(A, x, y, m, n) -// wp.tile_load(A, x, y, z, m, n, o) -// wp.tile_load(A, x, y, z, m, n, o, p) +// Notes on shared memory synchronization +// ====================================== +// +// Currently operations that wite to shared memory tiles (e.g.: tile_load()) +// must synchronize before they return through WP_TILE_SYNC(), this +// ensures subsequent read operations from the tile do not cause a race condition. +// +// For tile_shared_t adjoints, the gradient accumulation is done through shared +// memory atomics, i.e.: atomic_add(), so explicit synchronization is not +// required, with the exception of some operations like GEMMs, which use +// standard shared memory loads and stores to compute and accumulate gradients. +// +// The current synchronization strategy is conservative, can lead to more +// synchronization than necessary. A more sophisticated strategy would be +// to track the 'dirty' state of shared tiles, and synchronize only when +// necessary. In addition, custom synchronization for e.g.: tile_load() +// operations could be added through a SyncProvider template parameter on +// the tile_shared_t type, for example to support barrier synchronization +// for asynchronous global to shared loads. namespace wp { @@ -458,6 +468,8 @@ struct tile_shared_t else copy_from_global(t.data, t.x, t.y); // 2d load + // synchronization happens in copy functions + return *this; } @@ -468,6 +480,7 @@ struct tile_shared_t for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) data[i] = x; + WP_TILE_SYNC(); return *this; } @@ -522,6 +535,8 @@ struct tile_shared_t // todo: make this subtile (stride aware) for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM) data[i] = T(0); + + WP_TILE_SYNC(); } // extract a single tile element to a native type @@ -553,6 +568,8 @@ struct tile_shared_t (*this)(linear) = tile.data[i]; } + + WP_TILE_SYNC(); } inline CUDA_CALLABLE void add(const tile_register_t& tile) @@ -576,8 +593,6 @@ struct tile_shared_t inline CUDA_CALLABLE void print() { - WP_TILE_SYNC(); - if (threadIdx.x == 0) { printf("tile(m=%d, n=%d, storage=shared) = [", M, N); @@ -663,6 +678,8 @@ struct tile_shared_t { (*this)(i) = wp::index(src, tile_i + i); } + + WP_TILE_SYNC(); } inline CUDA_CALLABLE void copy_from_global(const array_t& src, int x, int y) @@ -688,6 +705,8 @@ struct tile_shared_t coord_t c = coord(i); (*this)(c.i, c.j) = ptr[c.i*stride_i + c.j*stride_j]; } + + WP_TILE_SYNC(); } }; @@ -766,6 +785,8 @@ inline CUDA_CALLABLE auto tile_alloc_zeros() for (int i=threadIdx.x; i < Len; i+= WP_TILE_BLOCK_DIM) data[i] = T(0); + WP_TILE_SYNC(); + return tile_shared_t(data); } @@ -1302,7 +1323,6 @@ TileC& tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, Ti { using T = typename TileA::Type; - WP_TILE_SYNC(); fun_forward(T(1.0), B.data, A.data, T(Add), C.data); WP_TILE_SYNC(); @@ -1316,6 +1336,8 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, { using T = typename TileA::Type; + // need to sync here because previous operations + // may still be performing atomic adds onto adj_A, adj_B, adjC WP_TILE_SYNC(); fun_backward_A(T(1.0), B.data, adj_C.data, T(1.0), adj_A.data); fun_backward_B(T(1.0), adj_C.data, A.data, T(1.0), adj_B.data); @@ -1329,6 +1351,8 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, { using T = typename TileA::Type; + // need to sync here because previous operations + // may still be performing atomic adds onto adj_A, adj_B, adjC WP_TILE_SYNC(); fun_backward_A(T(1.0), B.data, adj_C.data, T(1.0), adj_A.data); fun_backward_B(T(1.0), adj_C.data, A.data, T(1.0), adj_B.data); @@ -1340,7 +1364,6 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, do { \ void function_name(dtype*, dtype*); \ WP_TILE_SHARED __align__(16) char buffer[shared_memory_size]; \ - WP_TILE_SYNC(); \ for(int b = 0; b < (int)batch_size; b++) { \ function_name(Xinout.data + (int)b * (int)ept, (dtype*)buffer); \ WP_TILE_SYNC(); \ @@ -1397,13 +1420,14 @@ inline CUDA_CALLABLE void adj_tile_broadcast(Tile& t, Tile& adj_t, AdjTile& adj_ static_assert(LenTile == LenAdjTile); - // since the incoming adjoint will have the same physical storage + // since the incoming adjoint will have the same sized physical storage // as the original tile (just with different strides and expanded dimensions), // we can simply update the gradient element by element for (int i=threadIdx.x; i < LenTile; i+=WP_TILE_BLOCK_DIM) { - adj_t.data[i] += adj_ret.data[i]; + atomic_add(&adj_t.data[i], adj_ret.data[i]); } } + } // namespace wp From 9ee677568f96190ea0cbd265804273b9c6c3d6a4 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Thu, 17 Oct 2024 08:23:10 -0700 Subject: [PATCH 074/102] Fix Ruff errors --- warp/builtins.py | 16 +-- warp/optim/adam.py | 4 +- warp/tests/test_tile_mlp.py | 242 +++++++++++++++++++----------------- 3 files changed, 136 insertions(+), 126 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 1c9d5ecc..b50705a7 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5601,10 +5601,10 @@ def tile_matmul_generic_lto_dispatch_func( b = arg_values["b"] if len(return_values) > 0: - accumulate = 0 # for c = tile_matmul(a,b) case we want to overwrite c value + accumulate = 0 # for c = tile_matmul(a,b) case we want to overwrite c value out = return_values[0] else: - accumulate = 1 # for tile_matmul(a,b,c) case we want to add to c value + accumulate = 1 # for tile_matmul(a,b,c) case we want to add to c value out = arg_values["out"] if any(not is_tile(arg.type) for arg in [a, b, out]): @@ -5639,7 +5639,6 @@ def cublasdx_type_map(dtype): return ("wp::vec2d", 6, 1) raise RuntimeError("Unsupported input type in tile_matmul") - # generate the LTO M, K = a.type.M, a.type.N _, N = b.type.M, b.type.N @@ -5647,12 +5646,11 @@ def cublasdx_type_map(dtype): arch = options["output_arch"] def make_function(M, N, K, adtype, bdtype, cdtype, tA, tB): - (a_dtype, a_prec, a_type) = cublasdx_type_map(adtype) (b_dtype, b_prec, b_type) = cublasdx_type_map(bdtype) (c_dtype, c_prec, c_type) = cublasdx_type_map(cdtype) - if (a_type != b_type or a_type != c_type): + if a_type != b_type or a_type != c_type: raise RuntimeError("time_matmul(A, B, C) requires all inputs to be real or complex") element_type = a_type @@ -5701,7 +5699,9 @@ def make_transpose(t): lto_code = f.read() builder.ltoirs[lto_symbol] = lto_code - builder.ltoirs_decl[lto_symbol] = f"void {lto_symbol}({c_dtype}, {b_dtype}*, {a_dtype}*, {c_dtype}, {c_dtype}*);" + builder.ltoirs_decl[lto_symbol] = ( + f"void {lto_symbol}({c_dtype}, {b_dtype}*, {a_dtype}*, {c_dtype}, {c_dtype}*);" + ) return lto_symbol, lto_code @@ -5722,7 +5722,7 @@ def tile_flip_layout(layout): c_layout = tile_layout_mode(out.type) # C += A * B - (fun_forward, lto_forward) = make_function(M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a_layout, b_layout) + (fun_forward, lto_forward) = make_function(M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a_layout, b_layout) # adjA += adjC * B^T (fun_backward_A, lto_backward_A) = make_function( M, K, N, out.type.dtype, b.type.dtype, a.type.dtype, c_layout, tile_flip_layout(b_layout) @@ -5730,7 +5730,7 @@ def tile_flip_layout(layout): # adjB += A^T * adjC (fun_backward_B, lto_backward_B) = make_function( K, N, M, a.type.dtype, out.type.dtype, b.type.dtype, tile_flip_layout(a_layout), c_layout - ) + ) return ( ( diff --git a/warp/optim/adam.py b/warp/optim/adam.py index fb2d0064..a235432a 100644 --- a/warp/optim/adam.py +++ b/warp/optim/adam.py @@ -101,7 +101,7 @@ def set_params(self, params): elif param.dtype == wp.float32: dtype = wp.float32 elif param.dtype == wp.float16: - dtype = wp.float32 # we always use fp32 for moments, even if params are fp16 + dtype = wp.float32 # we always use fp32 for moments, even if params are fp16 else: raise RuntimeError(f"Unsupported dtype for Warp Adam optimizer: {param.dtype}") @@ -143,7 +143,7 @@ def step_detail(g, m, v, lr, beta1, beta2, t, eps, params): dim=len(params), inputs=kernel_inputs, device=params.device, - ) + ) elif params.dtype == wp.types.vec3: wp.launch( kernel=adam_step_kernel_vec3, diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index d79ce897..36915535 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -1,20 +1,19 @@ +import os + import numpy as np + import warp as wp import warp.examples import warp.optim - from warp.tests.unittest_utils import * -import math -import os - wp.init() # needs to be constant for the whole module NUM_THREADS = 32 -def create_layer(rng, dim_in, dim_hid, dtype=float): +def create_layer(rng, dim_in, dim_hid, dtype=float): w = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) b = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, 1)) @@ -23,28 +22,28 @@ def create_layer(rng, dim_in, dim_hid, dtype=float): return (weights, bias) -def create_array(rng, dim_in, dim_hid, dtype=float): +def create_array(rng, dim_in, dim_hid, dtype=float): s = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) a = wp.array(s, dtype=dtype, requires_grad=True) return a + @unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") def test_multi_layer_nn(test, device): - import torch as tc NUM_FREQ = wp.constant(8) - DIM_IN = wp.constant(4*NUM_FREQ) # sin,cos for both x,y at each frequenecy + DIM_IN = wp.constant(4 * NUM_FREQ) # sin,cos for both x,y at each frequenecy DIM_HID = 32 DIM_OUT = 3 - IMG_WIDTH = NUM_THREADS*8 - IMG_HEIGHT = NUM_THREADS*8 + IMG_WIDTH = NUM_THREADS * 8 + IMG_HEIGHT = NUM_THREADS * 8 - BATCH_SIZE = min(512, int((IMG_WIDTH*IMG_HEIGHT)/8)) + BATCH_SIZE = min(512, int((IMG_WIDTH * IMG_HEIGHT) / 8)) dtype = wp.float16 @@ -61,49 +60,52 @@ def zero(loss: wp.array(dtype=float)): loss[0] = 0.0 @wp.kernel - def compute(batches: wp.array(dtype=int), - input: wp.array2d(dtype=dtype), - weights_0: wp.array2d(dtype=dtype), bias_0: wp.array2d(dtype=dtype), - weights_1: wp.array2d(dtype=dtype), bias_1: wp.array2d(dtype=dtype), - weights_2: wp.array2d(dtype=dtype), bias_2: wp.array2d(dtype=dtype), - weights_3: wp.array2d(dtype=dtype), bias_3: wp.array2d(dtype=dtype), - reference: wp.array2d(dtype=float), - loss: wp.array1d(dtype=float), - out: wp.array2d(dtype=float)): - + def compute( + batches: wp.array(dtype=int), + input: wp.array2d(dtype=dtype), + weights_0: wp.array2d(dtype=dtype), + bias_0: wp.array2d(dtype=dtype), + weights_1: wp.array2d(dtype=dtype), + bias_1: wp.array2d(dtype=dtype), + weights_2: wp.array2d(dtype=dtype), + bias_2: wp.array2d(dtype=dtype), + weights_3: wp.array2d(dtype=dtype), + bias_3: wp.array2d(dtype=dtype), + reference: wp.array2d(dtype=float), + loss: wp.array1d(dtype=float), + out: wp.array2d(dtype=float), + ): linear = batches[wp.tid()] - row = linear/IMG_WIDTH - col = linear%IMG_WIDTH + row = linear / IMG_WIDTH + col = linear % IMG_WIDTH # normalize input coordinates to [-1, 1] - x = (float(row)/float(IMG_WIDTH) - 0.5)*2.0 - y = (float(col)/float(IMG_HEIGHT) - 0.5)*2.0 + x = (float(row) / float(IMG_WIDTH) - 0.5) * 2.0 + y = (float(col) / float(IMG_HEIGHT) - 0.5) * 2.0 local = wp.vector(dtype=dtype, length=DIM_IN) # construct positional encoding for s in range(NUM_FREQ): - - scale = wp.pow(2.0, float(s))*wp.pi + scale = wp.pow(2.0, float(s)) * wp.pi # x-coord - local[s*4 + 0] = dtype(wp.sin(x * scale)) - local[s*4 + 1] = dtype(wp.cos(x * scale)) + local[s * 4 + 0] = dtype(wp.sin(x * scale)) + local[s * 4 + 1] = dtype(wp.cos(x * scale)) # y-coord - local[s*4 + 2] = dtype(wp.sin(y * scale)) - local[s*4 + 3] = dtype(wp.cos(y * scale)) + local[s * 4 + 2] = dtype(wp.sin(y * scale)) + local[s * 4 + 3] = dtype(wp.cos(y * scale)) # write input back to array so that torch can use it - input[s*4 + 0, linear] = local[s*4 + 0] - input[s*4 + 1, linear] = local[s*4 + 1] - input[s*4 + 2, linear] = local[s*4 + 2] - input[s*4 + 3, linear] = local[s*4 + 3] - + input[s * 4 + 0, linear] = local[s * 4 + 0] + input[s * 4 + 1, linear] = local[s * 4 + 1] + input[s * 4 + 2, linear] = local[s * 4 + 2] + input[s * 4 + 3, linear] = local[s * 4 + 3] # tile feature vectors across the block, returns [dim(f), NUM_THREADS] f = wp.tile(local) - + # input layer w0 = wp.tile_load(weights_0, 0, 0, m=DIM_HID, n=DIM_IN) b0 = wp.tile_load(bias_0, 0, 0, m=DIM_HID, n=1) @@ -126,20 +128,19 @@ def compute(batches: wp.array(dtype=int), # untile back to SIMT output = wp.untile(o) - # compute error - error = wp.vec3(float(output[0]) - reference[0,linear], - float(output[1]) - reference[1,linear], - float(output[2]) - reference[2,linear]) + error = wp.vec3( + float(output[0]) - reference[0, linear], + float(output[1]) - reference[1, linear], + float(output[2]) - reference[2, linear], + ) # write MSE loss - wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*BATCH_SIZE)) - + wp.atomic_add(loss, 0, wp.length_sq(error) / float(3 * BATCH_SIZE)) # image output for i in range(DIM_OUT): out[i, linear] = float(output[i]) - rng = np.random.default_rng(45) @@ -148,8 +149,8 @@ def compute(batches: wp.array(dtype=int), weights_2, bias_2 = create_layer(rng, DIM_HID, DIM_HID, dtype=dtype) weights_3, bias_3 = create_layer(rng, DIM_HID, DIM_OUT, dtype=dtype) - input = create_array(rng, IMG_WIDTH*IMG_HEIGHT, DIM_IN, dtype=dtype) - output = create_array(rng, IMG_WIDTH*IMG_HEIGHT, DIM_OUT) + input = create_array(rng, IMG_WIDTH * IMG_HEIGHT, DIM_IN, dtype=dtype) + output = create_array(rng, IMG_WIDTH * IMG_HEIGHT, DIM_OUT) # # generate reference image # from PIL import Image @@ -159,50 +160,51 @@ def compute(batches: wp.array(dtype=int), # reference_np = reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T # np.save(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), reference_np, allow_pickle=True) - reference_np = np.load(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), allow_pickle=True)/255.0 + reference_np = np.load(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), allow_pickle=True) / 255.0 reference = wp.array(reference_np, dtype=float) loss = wp.zeros(1, dtype=float, requires_grad=True) - params = [weights_0, bias_0, - weights_1, bias_1, - weights_2, bias_2, - weights_3, bias_3] + params = [weights_0, bias_0, weights_1, bias_1, weights_2, bias_2, weights_3, bias_3] optimizer_grads = [p.grad.flatten() for p in params] optimizer_inputs = [p.flatten() for p in params] optimizer = warp.optim.Adam(optimizer_inputs, lr=0.01) - num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) + num_batches = int((IMG_WIDTH * IMG_HEIGHT) / BATCH_SIZE) max_epochs = 30 # create randomized batch indices - batches = np.arange(0, IMG_WIDTH*IMG_HEIGHT, dtype=np.int32) + batches = np.arange(0, IMG_WIDTH * IMG_HEIGHT, dtype=np.int32) rng.shuffle(batches) batches = wp.array(batches) - - with wp.ScopedTimer("Training", active=False): + with wp.ScopedTimer("Training", active=False): for epoch in range(max_epochs): - - for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): - + for b in range(0, IMG_WIDTH * IMG_HEIGHT, BATCH_SIZE): loss.zero_() with wp.Tape() as tape: wp.launch( - compute, + compute, dim=[BATCH_SIZE], - inputs=[batches[b:b+BATCH_SIZE], - input, - weights_0, bias_0, - weights_1, bias_1, - weights_2, bias_2, - weights_3, bias_3, - reference, - loss, - output], - block_dim=NUM_THREADS) + inputs=[ + batches[b : b + BATCH_SIZE], + input, + weights_0, + bias_0, + weights_1, + bias_1, + weights_2, + bias_2, + weights_3, + bias_3, + reference, + loss, + output, + ], + block_dim=NUM_THREADS, + ) tape.backward(loss) @@ -210,16 +212,15 @@ def compute(batches: wp.array(dtype=int), # since this is a relatively slow operation verify = True if verify and epoch < 3: + indices = batches[b : b + BATCH_SIZE].numpy() - indices = batches[b:b+BATCH_SIZE].numpy() - - z_np = np.maximum(weights_0.numpy()@input.numpy()[:,indices] + bias_0.numpy(), 0.0) - z_np = np.maximum(weights_1.numpy()@z_np + bias_1.numpy(), 0.0) - z_np = np.maximum(weights_2.numpy()@z_np + bias_2.numpy(), 0.0) - z_np = np.maximum(weights_3.numpy()@z_np + bias_3.numpy(), 0.0) + z_np = np.maximum(weights_0.numpy() @ input.numpy()[:, indices] + bias_0.numpy(), 0.0) + z_np = np.maximum(weights_1.numpy() @ z_np + bias_1.numpy(), 0.0) + z_np = np.maximum(weights_2.numpy() @ z_np + bias_2.numpy(), 0.0) + z_np = np.maximum(weights_3.numpy() @ z_np + bias_3.numpy(), 0.0) # test numpy foward - assert_np_equal(output.numpy()[:,indices], z_np, tol=1.e-2) + assert_np_equal(output.numpy()[:, indices], z_np, tol=1.0e-2) # torch input_tc = tc.from_numpy(input.numpy()[:, indices]).requires_grad_(True) @@ -234,33 +235,33 @@ def compute(batches: wp.array(dtype=int), bias_2_tc = tc.from_numpy(bias_2.numpy()).requires_grad_(True) weights_3_tc = tc.from_numpy(weights_3.numpy()).requires_grad_(True) - bias_3_tc = tc.from_numpy(bias_3.numpy()).requires_grad_(True) + bias_3_tc = tc.from_numpy(bias_3.numpy()).requires_grad_(True) + + z_tc = tc.clamp(weights_0_tc @ input_tc + bias_0_tc, min=0.0) + z_tc = tc.clamp(weights_1_tc @ z_tc + bias_1_tc, min=0.0) + z_tc = tc.clamp(weights_2_tc @ z_tc + bias_2_tc, min=0.0) + z_tc = tc.clamp(weights_3_tc @ z_tc + bias_3_tc, min=0.0) - z_tc = tc.clamp(weights_0_tc@input_tc + bias_0_tc, min=0.0) - z_tc = tc.clamp(weights_1_tc@z_tc + bias_1_tc, min=0.0) - z_tc = tc.clamp(weights_2_tc@z_tc + bias_2_tc, min=0.0) - z_tc = tc.clamp(weights_3_tc@z_tc + bias_3_tc, min=0.0) - ref_tc = tc.from_numpy(reference.numpy()[:, indices]).requires_grad_(True) - - l_tc = tc.mean((z_tc - ref_tc)**2) + + l_tc = tc.mean((z_tc - ref_tc) ** 2) l_tc.backward() # test torch - assert_np_equal(z_tc.cpu().detach().numpy(), output.numpy()[:, indices], tol=1.e-2) - assert_np_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy(), tol=1.e-2) - assert_np_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy(), tol=1.e-2) - assert_np_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy(), tol=1.e-2) - assert_np_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy(), tol=1.e-2) - assert_np_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy(), tol=1.e-2) - assert_np_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy(), tol=1.e-2) - assert_np_equal(weights_3.grad.numpy(), weights_3_tc.grad.cpu().detach().numpy(), tol=1.e-2) - assert_np_equal(bias_3.grad.numpy(), bias_3_tc.grad.cpu().detach().numpy(), tol=1.e-2) + assert_np_equal(z_tc.cpu().detach().numpy(), output.numpy()[:, indices], tol=1.0e-2) + assert_np_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(weights_3.grad.numpy(), weights_3_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_3.grad.numpy(), bias_3_tc.grad.cpu().detach().numpy(), tol=1.0e-2) optimizer.step(optimizer_grads) tape.zero() - #print(f"Epoch: {epoch} Loss: {loss.numpy()}") + # print(f"Epoch: {epoch} Loss: {loss.numpy()}") # predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) # predicted_image = (predicted_image * 255).astype(np.uint8) @@ -272,10 +273,8 @@ def compute(batches: wp.array(dtype=int), assert loss.numpy()[0] < 0.002 - @unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") def test_single_layer_nn(test, device): - import torch as tc DIM_IN = 8 @@ -289,11 +288,12 @@ def relu(x: float): return wp.max(x, 0.0) @wp.kernel - def compute(input: wp.array2d(dtype=float), - weights: wp.array2d(dtype=float), - bias: wp.array2d(dtype=float), - out: wp.array2d(dtype=float)): - + def compute( + input: wp.array2d(dtype=float), + weights: wp.array2d(dtype=float), + bias: wp.array2d(dtype=float), + out: wp.array2d(dtype=float), + ): i = wp.tid() f = wp.tile_load(input, 0, i, m=DIM_IN, n=NUM_THREADS) @@ -305,46 +305,44 @@ def compute(input: wp.array2d(dtype=float), wp.tile_store(out, 0, i, o) - with wp.ScopedDevice(device): - rng = np.random.default_rng(45) # single layer weights, bias weights, bias = create_layer(rng, DIM_IN, DIM_OUT, dtype=float) - input = create_array(rng, NUM_THREADS*NUM_BLOCKS, DIM_IN) - output = create_array(rng, NUM_THREADS*NUM_BLOCKS, DIM_OUT) + input = create_array(rng, NUM_THREADS * NUM_BLOCKS, DIM_IN) + output = create_array(rng, NUM_THREADS * NUM_BLOCKS, DIM_OUT) with wp.Tape() as tape: wp.launch_tiled(compute, dim=[NUM_BLOCKS], inputs=[input, weights, bias, output], block_dim=NUM_THREADS) output.grad = wp.ones_like(output) - tape.backward() + tape.backward() # numpy - output_np = np.maximum(weights.numpy()@input.numpy() + bias.numpy(), 0.0) + output_np = np.maximum(weights.numpy() @ input.numpy() + bias.numpy(), 0.0) # test numpy foward - assert_np_equal(output.numpy(), output_np, tol=1.e-2) - + assert_np_equal(output.numpy(), output_np, tol=1.0e-2) # torch - weights_tc = tc.from_numpy(weights.numpy()).requires_grad_(True) # use .numpy() to avoid any memory aliasing + weights_tc = tc.from_numpy(weights.numpy()).requires_grad_(True) # use .numpy() to avoid any memory aliasing input_tc = tc.from_numpy(input.numpy()).requires_grad_(True) bias_tc = tc.from_numpy(bias.numpy()).requires_grad_(True) - output_tc = tc.clamp(weights_tc@input_tc + bias_tc, min=0.0) + output_tc = tc.clamp(weights_tc @ input_tc + bias_tc, min=0.0) output_tc.backward(tc.ones_like(output_tc)) # test torch - assert_np_equal(output_tc.detach().numpy(), output.numpy(), tol=1.e-2) - assert_np_equal(input.grad.numpy(), input_tc.grad.detach().numpy(), tol=1.e-2) + assert_np_equal(output_tc.detach().numpy(), output.numpy(), tol=1.0e-2) + assert_np_equal(input.grad.numpy(), input_tc.grad.detach().numpy(), tol=1.0e-2) class TestTileMLP(unittest.TestCase): pass + test_devices = get_test_devices() try: @@ -365,13 +363,25 @@ class TestTileMLP(unittest.TestCase): except Exception as e: print(f"Skipping Torch tests on device '{d}' due to exception: {e}") - add_function_test(TestTileMLP, "test_single_layer_nn", test_single_layer_nn, check_output=False, devices=torch_compatible_cuda_devices) - add_function_test(TestTileMLP, "test_multi_layer_nn", test_multi_layer_nn, check_output=False, devices=torch_compatible_cuda_devices) + add_function_test( + TestTileMLP, + "test_single_layer_nn", + test_single_layer_nn, + check_output=False, + devices=torch_compatible_cuda_devices, + ) + add_function_test( + TestTileMLP, + "test_multi_layer_nn", + test_multi_layer_nn, + check_output=False, + devices=torch_compatible_cuda_devices, + ) except Exception as e: print(f"Skipping Torch tests due to exception: {e}") if __name__ == "__main__": -# wp.clear_kernel_cache() + # wp.clear_kernel_cache() unittest.main(verbosity=2, failfast=True) From f275782d825b9e6e3cb6aa9cb4196b983409c9ec Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Thu, 17 Oct 2024 08:35:19 -0700 Subject: [PATCH 075/102] Fix Ruff issues --- warp/builtins.py | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/warp/builtins.py b/warp/builtins.py index 168a78f7..87ad6815 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -5639,12 +5639,12 @@ def cublasdx_type_map(dtype): if dtype == vec2d: return ("wp::vec2d", 6, 1) raise RuntimeError("Unsupported input type in tile_matmul") - + def cublasdx_arrangement_map(layout): if layout == "colmajor": - return 0 # CUBLASDX_ARRANGEMENT_COL_MAJOR + return 0 # CUBLASDX_ARRANGEMENT_COL_MAJOR if layout == "rowmajor": - return 1 # CUBLASDX_ARRANGEMENT_ROW_MAJOR + return 1 # CUBLASDX_ARRANGEMENT_ROW_MAJOR raise RuntimeError("Unsupported layout in tile_matmul") # generate the LTO @@ -5654,7 +5654,6 @@ def cublasdx_arrangement_map(layout): arch = options["output_arch"] def make_function(M, N, K, adtype, bdtype, cdtype, alayout, blayout, clayout): - (a_dtype, a_prec, a_type) = cublasdx_type_map(adtype) (b_dtype, b_prec, b_type) = cublasdx_type_map(bdtype) (c_dtype, c_prec, c_type) = cublasdx_type_map(cdtype) @@ -5666,7 +5665,9 @@ def make_function(M, N, K, adtype, bdtype, cdtype, alayout, blayout, clayout): raise RuntimeError("time_matmul(A, B, C) requires all inputs to be real or complex") element_type = a_type - lto_symbol = f"dot_{M}_{N}_{K}_{a_arrangement}_{b_arrangement}_{c_arrangement}_{a_prec}_{b_prec}_{c_prec}_{element_type}" + lto_symbol = ( + f"dot_{M}_{N}_{K}_{a_arrangement}_{b_arrangement}_{c_arrangement}_{a_prec}_{b_prec}_{c_prec}_{element_type}" + ) # early out if LTO for this combination already exists for this module if lto_symbol in builder.ltoirs: @@ -5701,7 +5702,9 @@ def make_function(M, N, K, adtype, bdtype, cdtype, alayout, blayout, clayout): lto_code = f.read() builder.ltoirs[lto_symbol] = lto_code - builder.ltoirs_decl[lto_symbol] = f"void {lto_symbol}({c_dtype}, {a_dtype}*, {b_dtype}*, {c_dtype}, {c_dtype}*);" + builder.ltoirs_decl[lto_symbol] = ( + f"void {lto_symbol}({c_dtype}, {a_dtype}*, {b_dtype}*, {c_dtype}, {c_dtype}*);" + ) return lto_symbol, lto_code @@ -5712,15 +5715,33 @@ def tile_flip_layout(layout): return "rowmajor" # C += A * B - (fun_forward, lto_forward) = make_function(M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a.type.layout, b.type.layout, out.type.layout) + (fun_forward, lto_forward) = make_function( + M, N, K, a.type.dtype, b.type.dtype, out.type.dtype, a.type.layout, b.type.layout, out.type.layout + ) # adjA += adjC * B^T - Transpose ~= flipped layout (fun_backward_A, lto_backward_A) = make_function( - M, K, N, out.type.dtype, b.type.dtype, a.type.dtype, out.type.layout, tile_flip_layout(b.type.layout), a.type.layout + M, + K, + N, + out.type.dtype, + b.type.dtype, + a.type.dtype, + out.type.layout, + tile_flip_layout(b.type.layout), + a.type.layout, ) # adjB += A^T * adjC - Transpose ~= flipped layout (fun_backward_B, lto_backward_B) = make_function( - K, N, M, a.type.dtype, out.type.dtype, b.type.dtype, tile_flip_layout(a.type.layout), out.type.layout, b.type.layout - ) + K, + N, + M, + a.type.dtype, + out.type.dtype, + b.type.dtype, + tile_flip_layout(a.type.layout), + out.type.layout, + b.type.layout, + ) return ( ( From 9c2c8e3b7739bab550e1579cdb7bc3e1fb4e6887 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Thu, 17 Oct 2024 08:46:02 -0700 Subject: [PATCH 076/102] Add missing wp.ScopedDevice to test_tile_mlp --- warp/tests/test_tile_mlp.py | 266 +++++++++++++++++++----------------- 1 file changed, 137 insertions(+), 129 deletions(-) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 36915535..89fcf052 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -1,3 +1,10 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + import os import numpy as np @@ -142,135 +149,136 @@ def compute( for i in range(DIM_OUT): out[i, linear] = float(output[i]) - rng = np.random.default_rng(45) - - weights_0, bias_0 = create_layer(rng, DIM_IN, DIM_HID, dtype=dtype) - weights_1, bias_1 = create_layer(rng, DIM_HID, DIM_HID, dtype=dtype) - weights_2, bias_2 = create_layer(rng, DIM_HID, DIM_HID, dtype=dtype) - weights_3, bias_3 = create_layer(rng, DIM_HID, DIM_OUT, dtype=dtype) - - input = create_array(rng, IMG_WIDTH * IMG_HEIGHT, DIM_IN, dtype=dtype) - output = create_array(rng, IMG_WIDTH * IMG_HEIGHT, DIM_OUT) - - # # generate reference image - # from PIL import Image - # reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") - # with Image.open(reference_path) as im: - # reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) - # reference_np = reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T - # np.save(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), reference_np, allow_pickle=True) - - reference_np = np.load(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), allow_pickle=True) / 255.0 - reference = wp.array(reference_np, dtype=float) - - loss = wp.zeros(1, dtype=float, requires_grad=True) - - params = [weights_0, bias_0, weights_1, bias_1, weights_2, bias_2, weights_3, bias_3] - - optimizer_grads = [p.grad.flatten() for p in params] - optimizer_inputs = [p.flatten() for p in params] - optimizer = warp.optim.Adam(optimizer_inputs, lr=0.01) - - num_batches = int((IMG_WIDTH * IMG_HEIGHT) / BATCH_SIZE) - max_epochs = 30 - - # create randomized batch indices - batches = np.arange(0, IMG_WIDTH * IMG_HEIGHT, dtype=np.int32) - rng.shuffle(batches) - batches = wp.array(batches) - - with wp.ScopedTimer("Training", active=False): - for epoch in range(max_epochs): - for b in range(0, IMG_WIDTH * IMG_HEIGHT, BATCH_SIZE): - loss.zero_() - - with wp.Tape() as tape: - wp.launch( - compute, - dim=[BATCH_SIZE], - inputs=[ - batches[b : b + BATCH_SIZE], - input, - weights_0, - bias_0, - weights_1, - bias_1, - weights_2, - bias_2, - weights_3, - bias_3, - reference, - loss, - output, - ], - block_dim=NUM_THREADS, - ) - - tape.backward(loss) - - # check outputs + grads on the first few epoch only - # since this is a relatively slow operation - verify = True - if verify and epoch < 3: - indices = batches[b : b + BATCH_SIZE].numpy() - - z_np = np.maximum(weights_0.numpy() @ input.numpy()[:, indices] + bias_0.numpy(), 0.0) - z_np = np.maximum(weights_1.numpy() @ z_np + bias_1.numpy(), 0.0) - z_np = np.maximum(weights_2.numpy() @ z_np + bias_2.numpy(), 0.0) - z_np = np.maximum(weights_3.numpy() @ z_np + bias_3.numpy(), 0.0) - - # test numpy foward - assert_np_equal(output.numpy()[:, indices], z_np, tol=1.0e-2) - - # torch - input_tc = tc.from_numpy(input.numpy()[:, indices]).requires_grad_(True) - - weights_0_tc = tc.from_numpy(weights_0.numpy()).requires_grad_(True) - bias_0_tc = tc.from_numpy(bias_0.numpy()).requires_grad_(True) - - weights_1_tc = tc.from_numpy(weights_1.numpy()).requires_grad_(True) - bias_1_tc = tc.from_numpy(bias_1.numpy()).requires_grad_(True) - - weights_2_tc = tc.from_numpy(weights_2.numpy()).requires_grad_(True) - bias_2_tc = tc.from_numpy(bias_2.numpy()).requires_grad_(True) - - weights_3_tc = tc.from_numpy(weights_3.numpy()).requires_grad_(True) - bias_3_tc = tc.from_numpy(bias_3.numpy()).requires_grad_(True) - - z_tc = tc.clamp(weights_0_tc @ input_tc + bias_0_tc, min=0.0) - z_tc = tc.clamp(weights_1_tc @ z_tc + bias_1_tc, min=0.0) - z_tc = tc.clamp(weights_2_tc @ z_tc + bias_2_tc, min=0.0) - z_tc = tc.clamp(weights_3_tc @ z_tc + bias_3_tc, min=0.0) - - ref_tc = tc.from_numpy(reference.numpy()[:, indices]).requires_grad_(True) - - l_tc = tc.mean((z_tc - ref_tc) ** 2) - l_tc.backward() - - # test torch - assert_np_equal(z_tc.cpu().detach().numpy(), output.numpy()[:, indices], tol=1.0e-2) - assert_np_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - assert_np_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - assert_np_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - assert_np_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - assert_np_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - assert_np_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - assert_np_equal(weights_3.grad.numpy(), weights_3_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - assert_np_equal(bias_3.grad.numpy(), bias_3_tc.grad.cpu().detach().numpy(), tol=1.0e-2) - - optimizer.step(optimizer_grads) - tape.zero() - - # print(f"Epoch: {epoch} Loss: {loss.numpy()}") - - # predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) - # predicted_image = (predicted_image * 255).astype(np.uint8) - - # predicted_image_pil = Image.fromarray(predicted_image) - # predicted_image_pil.save("test_tile_mlp_wp.jpg") - - # initial loss is ~0.061 - assert loss.numpy()[0] < 0.002 + with wp.ScopedDevice(device): + rng = np.random.default_rng(45) + + weights_0, bias_0 = create_layer(rng, DIM_IN, DIM_HID, dtype=dtype) + weights_1, bias_1 = create_layer(rng, DIM_HID, DIM_HID, dtype=dtype) + weights_2, bias_2 = create_layer(rng, DIM_HID, DIM_HID, dtype=dtype) + weights_3, bias_3 = create_layer(rng, DIM_HID, DIM_OUT, dtype=dtype) + + input = create_array(rng, IMG_WIDTH * IMG_HEIGHT, DIM_IN, dtype=dtype) + output = create_array(rng, IMG_WIDTH * IMG_HEIGHT, DIM_OUT) + + # # generate reference image + # from PIL import Image + # reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") + # with Image.open(reference_path) as im: + # reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) + # reference_np = reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T + # np.save(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), reference_np, allow_pickle=True) + + reference_np = np.load(os.path.join(os.path.dirname(__file__), "assets/pixel.npy"), allow_pickle=True) / 255.0 + reference = wp.array(reference_np, dtype=float) + + loss = wp.zeros(1, dtype=float, requires_grad=True) + + params = [weights_0, bias_0, weights_1, bias_1, weights_2, bias_2, weights_3, bias_3] + + optimizer_grads = [p.grad.flatten() for p in params] + optimizer_inputs = [p.flatten() for p in params] + optimizer = warp.optim.Adam(optimizer_inputs, lr=0.01) + + num_batches = int((IMG_WIDTH * IMG_HEIGHT) / BATCH_SIZE) + max_epochs = 30 + + # create randomized batch indices + batches = np.arange(0, IMG_WIDTH * IMG_HEIGHT, dtype=np.int32) + rng.shuffle(batches) + batches = wp.array(batches) + + with wp.ScopedTimer("Training", active=False): + for epoch in range(max_epochs): + for b in range(0, IMG_WIDTH * IMG_HEIGHT, BATCH_SIZE): + loss.zero_() + + with wp.Tape() as tape: + wp.launch( + compute, + dim=[BATCH_SIZE], + inputs=[ + batches[b : b + BATCH_SIZE], + input, + weights_0, + bias_0, + weights_1, + bias_1, + weights_2, + bias_2, + weights_3, + bias_3, + reference, + loss, + output, + ], + block_dim=NUM_THREADS, + ) + + tape.backward(loss) + + # check outputs + grads on the first few epoch only + # since this is a relatively slow operation + verify = True + if verify and epoch < 3: + indices = batches[b : b + BATCH_SIZE].numpy() + + z_np = np.maximum(weights_0.numpy() @ input.numpy()[:, indices] + bias_0.numpy(), 0.0) + z_np = np.maximum(weights_1.numpy() @ z_np + bias_1.numpy(), 0.0) + z_np = np.maximum(weights_2.numpy() @ z_np + bias_2.numpy(), 0.0) + z_np = np.maximum(weights_3.numpy() @ z_np + bias_3.numpy(), 0.0) + + # test numpy foward + assert_np_equal(output.numpy()[:, indices], z_np, tol=1.0e-2) + + # torch + input_tc = tc.from_numpy(input.numpy()[:, indices]).requires_grad_(True) + + weights_0_tc = tc.from_numpy(weights_0.numpy()).requires_grad_(True) + bias_0_tc = tc.from_numpy(bias_0.numpy()).requires_grad_(True) + + weights_1_tc = tc.from_numpy(weights_1.numpy()).requires_grad_(True) + bias_1_tc = tc.from_numpy(bias_1.numpy()).requires_grad_(True) + + weights_2_tc = tc.from_numpy(weights_2.numpy()).requires_grad_(True) + bias_2_tc = tc.from_numpy(bias_2.numpy()).requires_grad_(True) + + weights_3_tc = tc.from_numpy(weights_3.numpy()).requires_grad_(True) + bias_3_tc = tc.from_numpy(bias_3.numpy()).requires_grad_(True) + + z_tc = tc.clamp(weights_0_tc @ input_tc + bias_0_tc, min=0.0) + z_tc = tc.clamp(weights_1_tc @ z_tc + bias_1_tc, min=0.0) + z_tc = tc.clamp(weights_2_tc @ z_tc + bias_2_tc, min=0.0) + z_tc = tc.clamp(weights_3_tc @ z_tc + bias_3_tc, min=0.0) + + ref_tc = tc.from_numpy(reference.numpy()[:, indices]).requires_grad_(True) + + l_tc = tc.mean((z_tc - ref_tc) ** 2) + l_tc.backward() + + # test torch + assert_np_equal(z_tc.cpu().detach().numpy(), output.numpy()[:, indices], tol=1.0e-2) + assert_np_equal(weights_0.grad.numpy(), weights_0_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_0.grad.numpy(), bias_0_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(weights_1.grad.numpy(), weights_1_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_1.grad.numpy(), bias_1_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(weights_2.grad.numpy(), weights_2_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_2.grad.numpy(), bias_2_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(weights_3.grad.numpy(), weights_3_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + assert_np_equal(bias_3.grad.numpy(), bias_3_tc.grad.cpu().detach().numpy(), tol=1.0e-2) + + optimizer.step(optimizer_grads) + tape.zero() + + # print(f"Epoch: {epoch} Loss: {loss.numpy()}") + + # predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) + # predicted_image = (predicted_image * 255).astype(np.uint8) + + # predicted_image_pil = Image.fromarray(predicted_image) + # predicted_image_pil.save("test_tile_mlp_wp.jpg") + + # initial loss is ~0.061 + test.assertLess(loss.numpy()[0], 0.002) @unittest.skipUnless(wp.context.runtime.core.is_mathdx_enabled(), "Warp was not built with MathDx support") From e095a66dcbfff2a2930ca31b8b9b08cc6358b98c Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 18 Oct 2024 02:55:05 +0000 Subject: [PATCH 077/102] Fixes for backward smem synchronization. --- warp/native/tile.h | 31 +++++++++++++------------------ warp/native/tile_reduce.h | 2 ++ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index 3f995221..bb911a17 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -110,11 +110,12 @@ // ensures subsequent read operations from the tile do not cause a race condition. // // For tile_shared_t adjoints, the gradient accumulation is done through shared -// memory atomics, i.e.: atomic_add(), so explicit synchronization is not -// required, with the exception of some operations like GEMMs, which use -// standard shared memory loads and stores to compute and accumulate gradients. +// memory atomics, i.e.: atomic_add() since for broadcast tiles multiple threads +// may map to the same location. Synchronization is still required after these +// updates, since subsequent operations e.g.: adj_tile_load() will store the +// gradients to memory, and all updates must be visible at that point. // -// The current synchronization strategy is conservative, can lead to more +// The current synchronization strategy is conservative, and can lead to more // synchronization than necessary. A more sophisticated strategy would be // to track the 'dirty' state of shared tiles, and synchronize only when // necessary. In addition, custom synchronization for e.g.: tile_load() @@ -550,6 +551,8 @@ struct tile_shared_t { if (threadIdx.x == 0) (*this)(i, j) += adj_ret; + + WP_TILE_SYNC(); } @@ -585,10 +588,12 @@ struct tile_shared_t break; // use shared memory atomics to accumulate gradients - // since for broadcast tiles multiple incoming values + // since for broadcast tiles multiple incoming threads // may map to a single location in shared memory atomic_add(&(*this)(linear), tile.data[i]); } + + WP_TILE_SYNC(); } inline CUDA_CALLABLE void print() @@ -1063,9 +1068,6 @@ inline CUDA_CALLABLE void adj_tile_load(array_t& src, int x, int y, template inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, Tile& t, array_t& adj_dest, int adj_x, AdjTile& adj_t) { - // if (!dest.grad) - // return; - // convert to register if necessary tile_register_t adj_reg; @@ -1092,10 +1094,7 @@ inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, Tile& t, array template inline CUDA_CALLABLE void adj_tile_store(array_t& dest, int x, int y, Tile& t, array_t& adj_dest, int adj_x, int adj_y, AdjTile& adj_t) { - // if (!dest.grad) - // return; - - // convert to register if necessary + // allocate register tile to load grads into tile_register_t adj_reg; const int tile_i = x*adj_reg.M; @@ -1335,9 +1334,6 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, { using T = typename TileA::Type; - // need to sync here because previous operations - // may still be performing atomic adds onto adj_A, adj_B, adjC - WP_TILE_SYNC(); fun_backward_A(T(1.0), adj_C.data, B.data, T(1.0), adj_A.data); fun_backward_B(T(1.0), A.data, adj_C.data, T(1.0), adj_B.data); WP_TILE_SYNC(); @@ -1350,9 +1346,6 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, { using T = typename TileA::Type; - // need to sync here because previous operations - // may still be performing atomic adds onto adj_A, adj_B, adjC - WP_TILE_SYNC(); fun_backward_A(T(1.0), adj_C.data, B.data, T(1.0), adj_A.data); fun_backward_B(T(1.0), A.data, adj_C.data, T(1.0), adj_B.data); WP_TILE_SYNC(); @@ -1426,6 +1419,8 @@ inline CUDA_CALLABLE void adj_tile_broadcast(Tile& t, Tile& adj_t, AdjTile& adj_ { atomic_add(&adj_t.data[i], adj_ret.data[i]); } + + WP_TILE_SYNC(); } diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index 35107f35..3b5da6d9 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -168,7 +168,9 @@ void adj_tile_sum(Tile& t, Tile& adj_t, AdjTile& adj_ret) WP_TILE_SYNC(); + // convert the destination adjoint to a register auto adj_t_reg = adj_t.copy_to_register(); + // broadcast scalar across input dimensions (note zero strides) auto adj_ret_reg = tile_shared_t(&scratch).copy_to_register(); adj_t.assign(tile_add(adj_t_reg, adj_ret_reg)); From 4eeec1642e4eaf8dce65eb83975dc381bf76d67c Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Fri, 18 Oct 2024 03:00:30 +0000 Subject: [PATCH 078/102] Update docs on adjoint synchronization. --- warp/native/tile.h | 60 ++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index bb911a17..6d164d7f 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -102,26 +102,46 @@ */ -// Notes on shared memory synchronization -// ====================================== -// -// Currently operations that wite to shared memory tiles (e.g.: tile_load()) -// must synchronize before they return through WP_TILE_SYNC(), this -// ensures subsequent read operations from the tile do not cause a race condition. -// -// For tile_shared_t adjoints, the gradient accumulation is done through shared -// memory atomics, i.e.: atomic_add() since for broadcast tiles multiple threads -// may map to the same location. Synchronization is still required after these -// updates, since subsequent operations e.g.: adj_tile_load() will store the -// gradients to memory, and all updates must be visible at that point. -// -// The current synchronization strategy is conservative, and can lead to more -// synchronization than necessary. A more sophisticated strategy would be -// to track the 'dirty' state of shared tiles, and synchronize only when -// necessary. In addition, custom synchronization for e.g.: tile_load() -// operations could be added through a SyncProvider template parameter on -// the tile_shared_t type, for example to support barrier synchronization -// for asynchronous global to shared loads. +/* +Notes on shared memory synchronization +====================================== + +Currently operations that wite to shared memory tiles (e.g.: tile_load()) +must synchronize before they return through WP_TILE_SYNC(), this +ensures subsequent read operations from the tile do not cause a race condition. + +For tile_shared_t adjoints, the gradient accumulation is done through shared +memory atomics, i.e.: atomic_add(), since for broadcast tiles multiple threads +may map to the same location. Synchronization is still required after these +updates, since subsequent operations e.g.: adj_tile_load() will store the +gradients to memory, and all updates must be visible at that point, e.g.: + + a = wp.tile_load(...) + b = wp.tile_load(...) + c = wp.tile_matmul(a, b) + wp.tile_store(c) + + // loads incoming adjoints from global -> shared + wp.adj_tile_store(c, adj_c) + // consumes adj_c, requires synchronization + wp.adj_tile_matmul(a, b, adj_a, adj_b, adj_c) + // consumes adj_b, requires synchronization + wp.adj_tile_load(..., adj_b) + // consumes adj_b, requires synchronization + wp.adj_tile_load(..., adj_a) + +Generally synchronization to adjoint tiles will happen through the +tile_shared_t::add() and tile_shared_t::assign() function automatically, +but in some cases e.g.: tile_matmul() it is done manually. + +The current synchronization strategy is conservative, and can lead to more +synchronization than necessary. A more sophisticated strategy would be +to track the 'dirty' state of shared tiles, and synchronize only when +necessary. In addition, custom synchronization for e.g.: tile_load() +operations could be added through a SyncProvider template parameter on +the tile_shared_t type, for example to support barrier synchronization +for asynchronous global to shared loads. +*/ namespace wp { From 3abf3a81dc1a4e37d11e304ef2955f67e4c26c57 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Mon, 21 Oct 2024 08:44:09 +0000 Subject: [PATCH 079/102] Clean up example_tile_mlp.py --- warp/examples/tile/example_tile_mlp.py | 254 +++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 warp/examples/tile/example_tile_mlp.py diff --git a/warp/examples/tile/example_tile_mlp.py b/warp/examples/tile/example_tile_mlp.py new file mode 100644 index 00000000..893b344c --- /dev/null +++ b/warp/examples/tile/example_tile_mlp.py @@ -0,0 +1,254 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +########################################################################### +# Example Image Multilayer Perceptron (MLP) +# +# Shows how to train a coordinate-based MLP on an image to predict the RGB +# color at a given input position. By default, a positional encoding is +# applied to the input coordinates to improve the ability of the MLP to +# represent higher-frequency content. This can be disabled by passing the +# '--no_encoding' option. +# +# References: +# Ben Mildenhall et al. 2021. NeRF: representing scenes +# as neural radiance fields for view synthesis. Commun. ACM 65, 1 +# (January 2022), 99–106. https://doi.org/10.1145/3503250 +# +########################################################################### + +import numpy as np +import warp as wp +import warp.examples +import warp.optim + +import math +import os + +from PIL import Image + +rng = np.random.default_rng(45) + +def create_layer(dim_in, dim_hid, dtype=float): + + w = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) + b = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, 1)) + + weights = wp.array(w, dtype=dtype, requires_grad=True) + bias = wp.array(b, dtype=dtype, requires_grad=True) + + return (weights, bias) + +def create_array(dim_in, dim_hid, dtype=float): + + s = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) + a = wp.array(s, dtype=dtype, requires_grad=True) + + return a + + +# number of frequencies for the positional encoding +NUM_FREQ = wp.constant(8) + +DIM_IN = wp.constant(4*NUM_FREQ) # sin,cos for both x,y at each frequenecy +DIM_HID = 32 +DIM_OUT = 3 + +# threads per-block +NUM_THREADS = 32 + +IMG_WIDTH = NUM_THREADS*16 +IMG_HEIGHT = NUM_THREADS*16 + +BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) + +# dtype for our weights and bias matrices +dtype = wp.float16 + +@wp.func +def relu(x: dtype): + return wp.max(x, dtype(0.0)) + +@wp.kernel +def compute(indices: wp.array(dtype=int), + weights_0: wp.array2d(dtype=dtype), bias_0: wp.array2d(dtype=dtype), + weights_1: wp.array2d(dtype=dtype), bias_1: wp.array2d(dtype=dtype), + weights_2: wp.array2d(dtype=dtype), bias_2: wp.array2d(dtype=dtype), + weights_3: wp.array2d(dtype=dtype), bias_3: wp.array2d(dtype=dtype), + reference: wp.array2d(dtype=float), + loss: wp.array1d(dtype=float), + out: wp.array2d(dtype=float)): + + if indices: + # use batch indices if provided + linear = indices[wp.tid()] + else: + linear = wp.tid() + + row = linear/IMG_WIDTH + col = linear%IMG_WIDTH + + # normalize input coordinates to [-1, 1] + x = (float(row)/float(IMG_WIDTH) - 0.5)*2.0 + y = (float(col)/float(IMG_HEIGHT) - 0.5)*2.0 + + local = wp.vector(dtype=dtype, length=DIM_IN) + + # construct positional encoding + for s in range(NUM_FREQ): + + scale = wp.pow(2.0, float(s))*wp.pi + + # x-coord + local[s*4 + 0] = dtype(wp.sin(x * scale)) + local[s*4 + 1] = dtype(wp.cos(x * scale)) + + # y-coord + local[s*4 + 2] = dtype(wp.sin(y * scale)) + local[s*4 + 3] = dtype(wp.cos(y * scale)) + + + # tile feature vectors across the block, returns [dim(f), NUM_THREADS] + f = wp.tile(local) + + # input layer + w0 = wp.tile_load(weights_0, 0, 0, m=DIM_HID, n=DIM_IN) + b0 = wp.tile_load(bias_0, 0, 0, m=DIM_HID, n=1) + z = wp.tile_map(relu, wp.tile_matmul(w0, f) + wp.tile_broadcast(b0, m=DIM_HID, n=NUM_THREADS)) + + # hidden layer + w1 = wp.tile_load(weights_1, 0, 0, m=DIM_HID, n=DIM_HID) + b1 = wp.tile_load(bias_1, 0, 0, m=DIM_HID, n=1) + z = wp.tile_map(relu, wp.tile_matmul(w1, z) + wp.tile_broadcast(b1, m=DIM_HID, n=NUM_THREADS)) + + w2 = wp.tile_load(weights_2, 0, 0, m=DIM_HID, n=DIM_HID) + b2 = wp.tile_load(bias_2, 0, 0, m=DIM_HID, n=1) + z = wp.tile_map(relu, wp.tile_matmul(w2, z) + wp.tile_broadcast(b2, m=DIM_HID, n=NUM_THREADS)) + + # output layer + w3 = wp.tile_load(weights_3, 0, 0, m=DIM_OUT, n=DIM_HID) + b3 = wp.tile_load(bias_3, 0, 0, m=DIM_OUT, n=1) + o = wp.tile_map(relu, wp.tile_matmul(w3, z) + wp.tile_broadcast(b3, m=DIM_OUT, n=NUM_THREADS)) + + # untile back to SIMT + output = wp.untile(o) + + # compute error + error = wp.vec3(float(output[0]) - reference[0,linear], + float(output[1]) - reference[1,linear], + float(output[2]) - reference[2,linear]) + + # write MSE loss + if loss: + wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*BATCH_SIZE)) + + # write image output + if out: + for i in range(DIM_OUT): + out[i, linear] = float(output[i]) + + +class Example: + + def __init__(self): + pass + + def train(self): + + weights_0, bias_0 = create_layer(DIM_IN, DIM_HID, dtype=dtype) + weights_1, bias_1 = create_layer(DIM_HID, DIM_HID, dtype=dtype) + weights_2, bias_2 = create_layer(DIM_HID, DIM_HID, dtype=dtype) + weights_3, bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=dtype) + + input = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_IN, dtype=dtype) + output = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_OUT) + + # reference + reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") + with Image.open(reference_path) as im: + reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) / 255.0 + reference = wp.array(reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T, dtype=float) + + loss = wp.zeros(1, dtype=float, requires_grad=True) + + params = [weights_0, bias_0, + weights_1, bias_1, + weights_2, bias_2, + weights_3, bias_3] + + optimizer_grads = [p.grad.flatten() for p in params] + optimizer_inputs = [p.flatten() for p in params] + optimizer = warp.optim.Adam(optimizer_inputs, lr=0.01) + + num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) + max_iters = 20000 + max_epochs = int(max_iters/num_batches) + + # create randomized batch indices + indices = np.arange(0, IMG_WIDTH*IMG_HEIGHT, dtype=np.int32) + rng.shuffle(indices) + indices = wp.array(indices) + + with wp.ScopedTimer("Training"): + + for i in range(max_epochs): + + for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): + + loss.zero_() + + with wp.Tape() as tape: + wp.launch( + compute, + dim=[BATCH_SIZE], + inputs=[indices[b:b+BATCH_SIZE], + weights_0, bias_0, + weights_1, bias_1, + weights_2, bias_2, + weights_3, bias_3, + reference, + loss, + None], + block_dim=NUM_THREADS) + + tape.backward(loss) + + optimizer.step(optimizer_grads) + + tape.zero() + + print(f"Epoch: {i} Loss: {loss.numpy()}") + + + # evaluate full image + wp.launch( + compute, + dim=[IMG_WIDTH*IMG_HEIGHT], + inputs=[None, + weights_0, bias_0, + weights_1, bias_1, + weights_2, bias_2, + weights_3, bias_3, + reference, + loss, + output], + block_dim=NUM_THREADS) + + predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) + predicted_image = (predicted_image * 255).astype(np.uint8) + + predicted_image_pil = Image.fromarray(predicted_image) + predicted_image_pil.save("example_tile_mlp.jpg") + + + +if __name__ == "__main__": + + with wp.ScopedDevice("cuda:0"): + + example = Example() + example.train() From b155a70a7393cc66c1529db39c45fdf569c99b72 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 23 Oct 2024 02:18:49 +0000 Subject: [PATCH 080/102] Add Torch impl. to MLP example + CUDA graph support --- warp/examples/tile/example_tile_mlp.py | 240 +++++++++++++++++++------ 1 file changed, 185 insertions(+), 55 deletions(-) diff --git a/warp/examples/tile/example_tile_mlp.py b/warp/examples/tile/example_tile_mlp.py index 893b344c..abac75d4 100644 --- a/warp/examples/tile/example_tile_mlp.py +++ b/warp/examples/tile/example_tile_mlp.py @@ -66,6 +66,8 @@ def create_array(dim_in, dim_hid, dtype=float): BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) +wp.set_module_options({"fast_math": True}) + # dtype for our weights and bias matrices dtype = wp.float16 @@ -75,6 +77,7 @@ def relu(x: dtype): @wp.kernel def compute(indices: wp.array(dtype=int), + encoding: wp.array2d(dtype=dtype), weights_0: wp.array2d(dtype=dtype), bias_0: wp.array2d(dtype=dtype), weights_1: wp.array2d(dtype=dtype), bias_1: wp.array2d(dtype=dtype), weights_2: wp.array2d(dtype=dtype), bias_2: wp.array2d(dtype=dtype), @@ -106,11 +109,17 @@ def compute(indices: wp.array(dtype=int), # x-coord local[s*4 + 0] = dtype(wp.sin(x * scale)) local[s*4 + 1] = dtype(wp.cos(x * scale)) - # y-coord local[s*4 + 2] = dtype(wp.sin(y * scale)) local[s*4 + 3] = dtype(wp.cos(y * scale)) + # if requested then write the encoding back to device memory + if encoding: + encoding[s*4 + 0, linear] = local[s*4 + 0] + encoding[s*4 + 1, linear] = local[s*4 + 1] + encoding[s*4 + 2, linear] = local[s*4 + 2] + encoding[s*4 + 3, linear] = local[s*4 + 3] + # tile feature vectors across the block, returns [dim(f), NUM_THREADS] f = wp.tile(local) @@ -155,73 +164,77 @@ def compute(indices: wp.array(dtype=int), class Example: def __init__(self): - pass - - def train(self): - weights_0, bias_0 = create_layer(DIM_IN, DIM_HID, dtype=dtype) - weights_1, bias_1 = create_layer(DIM_HID, DIM_HID, dtype=dtype) - weights_2, bias_2 = create_layer(DIM_HID, DIM_HID, dtype=dtype) - weights_3, bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=dtype) - - input = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_IN, dtype=dtype) - output = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_OUT) + self.weights_0, self.bias_0 = create_layer(DIM_IN, DIM_HID, dtype=dtype) + self.weights_1, self.bias_1 = create_layer(DIM_HID, DIM_HID, dtype=dtype) + self.weights_2, self.bias_2 = create_layer(DIM_HID, DIM_HID, dtype=dtype) + self.weights_3, self.bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=dtype) # reference reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") with Image.open(reference_path) as im: reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) / 255.0 - reference = wp.array(reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T, dtype=float) + self.reference = wp.array(reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T, dtype=float) - loss = wp.zeros(1, dtype=float, requires_grad=True) + # create randomized batch indices + indices = np.arange(0, IMG_WIDTH*IMG_HEIGHT, dtype=np.int32) + rng.shuffle(indices) + self.indices = wp.array(indices) + + self.num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) + self.max_iters = 20000 + self.max_epochs = int(self.max_iters/self.num_batches) + + def train_warp(self): - params = [weights_0, bias_0, - weights_1, bias_1, - weights_2, bias_2, - weights_3, bias_3] + params = [self.weights_0, self.bias_0, + self.weights_1, self.bias_1, + self.weights_2, self.bias_2, + self.weights_3, self.bias_3] optimizer_grads = [p.grad.flatten() for p in params] optimizer_inputs = [p.flatten() for p in params] optimizer = warp.optim.Adam(optimizer_inputs, lr=0.01) + + loss = wp.zeros(1, dtype=float, requires_grad=True) + output = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_OUT) - num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) - max_iters = 20000 - max_epochs = int(max_iters/num_batches) - - # create randomized batch indices - indices = np.arange(0, IMG_WIDTH*IMG_HEIGHT, dtype=np.int32) - rng.shuffle(indices) - indices = wp.array(indices) - - with wp.ScopedTimer("Training"): + # capture graph for whole epoch + wp.capture_begin() + + for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): - for i in range(max_epochs): + loss.zero_() - for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): + with wp.Tape() as tape: + wp.launch( + compute, + dim=[BATCH_SIZE], + inputs=[self.indices[b:b+BATCH_SIZE], + None, + self.weights_0, self.bias_0, + self.weights_1, self.bias_1, + self.weights_2, self.bias_2, + self.weights_3, self.bias_3, + self.reference, + loss, + None], + block_dim=NUM_THREADS) - loss.zero_() + tape.backward(loss) + optimizer.step(optimizer_grads) + tape.zero() - with wp.Tape() as tape: - wp.launch( - compute, - dim=[BATCH_SIZE], - inputs=[indices[b:b+BATCH_SIZE], - weights_0, bias_0, - weights_1, bias_1, - weights_2, bias_2, - weights_3, bias_3, - reference, - loss, - None], - block_dim=NUM_THREADS) + graph = wp.capture_end() - tape.backward(loss) - optimizer.step(optimizer_grads) + with wp.ScopedTimer("Training"): - tape.zero() + for i in range(self.max_epochs): - print(f"Epoch: {i} Loss: {loss.numpy()}") + with wp.ScopedTimer("Epoch"): + wp.capture_launch(graph) + print(f"Epoch: {i} Loss: {loss.numpy()}") # evaluate full image @@ -229,16 +242,131 @@ def train(self): compute, dim=[IMG_WIDTH*IMG_HEIGHT], inputs=[None, - weights_0, bias_0, - weights_1, bias_1, - weights_2, bias_2, - weights_3, bias_3, - reference, + None, + self.weights_0, self.bias_0, + self.weights_1, self.bias_1, + self.weights_2, self.bias_2, + self.weights_3, self.bias_3, + self.reference, loss, output], - block_dim=NUM_THREADS) + block_dim=NUM_THREADS) + + + self.save_image(output.numpy()) + + + + def train_torch(self): + + import torch as tc + + weights_0 = tc.nn.Parameter(wp.to_torch(self.weights_0)) + weights_1 = tc.nn.Parameter(wp.to_torch(self.weights_1)) + weights_2 = tc.nn.Parameter(wp.to_torch(self.weights_2)) + weights_3 = tc.nn.Parameter(wp.to_torch(self.weights_3)) + + bias_0 = tc.nn.Parameter(wp.to_torch(self.bias_0)) + bias_1 = tc.nn.Parameter(wp.to_torch(self.bias_1)) + bias_2 = tc.nn.Parameter(wp.to_torch(self.bias_2)) + bias_3 = tc.nn.Parameter(wp.to_torch(self.bias_3)) + + indices = wp.to_torch(self.indices) + reference = wp.to_torch(self.reference) + + optimizer = tc.optim.Adam([weights_0, + bias_0, + weights_1, + bias_1, + weights_2, + bias_2, + weights_3, + bias_3], capturable=True, lr=0.0001, betas=(0.9, 0.95), eps=1.e-6) + + + # generate frequency space encoding of pixels + # based on their linear index in the image + def encode(linear): + + row = (linear // IMG_WIDTH).float() + col = (linear % IMG_WIDTH).float() + + x = (row / float(IMG_WIDTH) - 0.5) * 2.0 + y = (col / float(IMG_HEIGHT) - 0.5) * 2.0 + + encoding = tc.zeros((NUM_FREQ * 4, len(linear)), dtype=tc.float16, device="cuda") + + for s in range(NUM_FREQ): + scale = math.pow(2.0, float(s)) * math.pi + + # Directly write the computed values into the encoding tensor + encoding[s * 4 + 0, :] = tc.sin(scale * x) + encoding[s * 4 + 1, :] = tc.cos(scale * x) + encoding[s * 4 + 2, :] = tc.sin(scale * y) + encoding[s * 4 + 3, :] = tc.cos(scale * y) + + return encoding + + + stream = tc.cuda.Stream() + graph = tc.cuda.CUDAGraph() + + # warm-up + with tc.cuda.stream(stream): + f = tc.rand((NUM_FREQ*4, BATCH_SIZE), dtype=tc.float16, device="cuda") + z = tc.relu(weights_0 @ f + bias_0) + z = tc.relu(weights_1 @ z + bias_1) + z = tc.relu(weights_2 @ z + bias_2) + z = tc.relu(weights_3 @ z + bias_3) + ref = tc.rand((3, BATCH_SIZE), dtype=tc.float16, device="cuda") + loss = tc.mean((z - ref) ** 2) + optimizer.zero_grad() + loss.backward() + optimizer.step() + + with tc.cuda.graph(graph): + + for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): + + linear = indices[b:b+BATCH_SIZE] + + f = encode(linear) + + z = tc.relu(weights_0 @ f + bias_0) + z = tc.relu(weights_1 @ z + bias_1) + z = tc.relu(weights_2 @ z + bias_2) + z = tc.relu(weights_3 @ z + bias_3) + + ref = reference[:, linear] + loss = tc.mean((z - ref) ** 2) - predicted_image = output.numpy().T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) + optimizer.zero_grad() + loss.backward() + optimizer.step() + + + with wp.ScopedTimer("Training (Torch)"): + + for i in range(self.max_epochs): + + with wp.ScopedTimer("Epoch"): + graph.replay() + + print(loss) + + + f = encode(tc.arange(0, IMG_WIDTH*IMG_HEIGHT)) + z = tc.relu(weights_0 @ f + bias_0) + z = tc.relu(weights_1 @ z + bias_1) + z = tc.relu(weights_2 @ z + bias_2) + z = tc.relu(weights_3 @ z + bias_3) + + self.save_image(z.detach().cpu().numpy()) + + + def save_image(self, output): + + predicted_image = output.T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) predicted_image = (predicted_image * 255).astype(np.uint8) predicted_image_pil = Image.fromarray(predicted_image) @@ -246,9 +374,11 @@ def train(self): + if __name__ == "__main__": with wp.ScopedDevice("cuda:0"): example = Example() - example.train() + #example.train_warp() + example.train_torch() From 89256ad6c086820372962be59ea71034673e8ce6 Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 23 Oct 2024 02:54:06 +0000 Subject: [PATCH 081/102] Add support for specifying max iterations (useful for profiling) --- warp/examples/tile/example_tile_mlp.py | 40 +++++++++++--------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/warp/examples/tile/example_tile_mlp.py b/warp/examples/tile/example_tile_mlp.py index abac75d4..18614bfe 100644 --- a/warp/examples/tile/example_tile_mlp.py +++ b/warp/examples/tile/example_tile_mlp.py @@ -77,7 +77,6 @@ def relu(x: dtype): @wp.kernel def compute(indices: wp.array(dtype=int), - encoding: wp.array2d(dtype=dtype), weights_0: wp.array2d(dtype=dtype), bias_0: wp.array2d(dtype=dtype), weights_1: wp.array2d(dtype=dtype), bias_1: wp.array2d(dtype=dtype), weights_2: wp.array2d(dtype=dtype), bias_2: wp.array2d(dtype=dtype), @@ -86,11 +85,8 @@ def compute(indices: wp.array(dtype=int), loss: wp.array1d(dtype=float), out: wp.array2d(dtype=float)): - if indices: - # use batch indices if provided - linear = indices[wp.tid()] - else: - linear = wp.tid() + # batch indices + linear = indices[wp.tid()] row = linear/IMG_WIDTH col = linear%IMG_WIDTH @@ -113,13 +109,6 @@ def compute(indices: wp.array(dtype=int), local[s*4 + 2] = dtype(wp.sin(y * scale)) local[s*4 + 3] = dtype(wp.cos(y * scale)) - # if requested then write the encoding back to device memory - if encoding: - encoding[s*4 + 0, linear] = local[s*4 + 0] - encoding[s*4 + 1, linear] = local[s*4 + 1] - encoding[s*4 + 2, linear] = local[s*4 + 2] - encoding[s*4 + 3, linear] = local[s*4 + 3] - # tile feature vectors across the block, returns [dim(f), NUM_THREADS] f = wp.tile(local) @@ -163,7 +152,7 @@ def compute(indices: wp.array(dtype=int), class Example: - def __init__(self): + def __init__(self, train_iters): self.weights_0, self.bias_0 = create_layer(DIM_IN, DIM_HID, dtype=dtype) self.weights_1, self.bias_1 = create_layer(DIM_HID, DIM_HID, dtype=dtype) @@ -182,8 +171,8 @@ def __init__(self): self.indices = wp.array(indices) self.num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) - self.max_iters = 20000 - self.max_epochs = int(self.max_iters/self.num_batches) + self.max_iters = train_iters + self.max_epochs = max(1, int(self.max_iters/self.num_batches)) def train_warp(self): @@ -202,7 +191,7 @@ def train_warp(self): # capture graph for whole epoch wp.capture_begin() - for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): + for b in range(0, min(self.max_iters, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE)): loss.zero_() @@ -211,7 +200,6 @@ def train_warp(self): compute, dim=[BATCH_SIZE], inputs=[self.indices[b:b+BATCH_SIZE], - None, self.weights_0, self.bias_0, self.weights_1, self.bias_1, self.weights_2, self.bias_2, @@ -241,8 +229,7 @@ def train_warp(self): wp.launch( compute, dim=[IMG_WIDTH*IMG_HEIGHT], - inputs=[None, - None, + inputs=[self.indices, self.weights_0, self.bias_0, self.weights_1, self.bias_1, self.weights_2, self.bias_2, @@ -377,8 +364,15 @@ def save_image(self, output): if __name__ == "__main__": + import argparse + + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument("--train_iters", type=int, default=20000, help="Total number of training iterations.") + + args = parser.parse_known_args()[0] + with wp.ScopedDevice("cuda:0"): - example = Example() - #example.train_warp() - example.train_torch() + example = Example(args.train_iters) + example.train_warp() + #example.train_torch() From a19a67939a81b715fd9103cce565304bc3c7494b Mon Sep 17 00:00:00 2001 From: Miles Macklin Date: Wed, 23 Oct 2024 05:07:03 +0000 Subject: [PATCH 082/102] Fix for typo batch iteration --- warp/examples/tile/example_tile_mlp.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/warp/examples/tile/example_tile_mlp.py b/warp/examples/tile/example_tile_mlp.py index 18614bfe..ef0f49e4 100644 --- a/warp/examples/tile/example_tile_mlp.py +++ b/warp/examples/tile/example_tile_mlp.py @@ -66,8 +66,6 @@ def create_array(dim_in, dim_hid, dtype=float): BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) -wp.set_module_options({"fast_math": True}) - # dtype for our weights and bias matrices dtype = wp.float16 @@ -191,7 +189,7 @@ def train_warp(self): # capture graph for whole epoch wp.capture_begin() - for b in range(0, min(self.max_iters, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE)): + for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): loss.zero_() @@ -238,9 +236,8 @@ def train_warp(self): loss, output], block_dim=NUM_THREADS) - - self.save_image(output.numpy()) + self.save_image(f"example_tile_mlp.jpg", output.numpy()) @@ -348,16 +345,16 @@ def encode(linear): z = tc.relu(weights_2 @ z + bias_2) z = tc.relu(weights_3 @ z + bias_3) - self.save_image(z.detach().cpu().numpy()) + self.save_image("example_tile_mlp_torch.jpg", z.detach().cpu().numpy()) - def save_image(self, output): + def save_image(self, name, output): predicted_image = output.T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) predicted_image = (predicted_image * 255).astype(np.uint8) predicted_image_pil = Image.fromarray(predicted_image) - predicted_image_pil.save("example_tile_mlp.jpg") + predicted_image_pil.save(name) From 1521890a5794105205d7e024b6aa3c4e3a6a85c8 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Wed, 23 Oct 2024 17:10:56 -0700 Subject: [PATCH 083/102] Use libmathdx with embedded headers --- .gitlab/ci/mathdx-support.yml | 8 +++--- warp/builtins.py | 16 +++++------- warp/mathdx.py | 47 ----------------------------------- warp/native/warp.cu | 27 +++++++------------- 4 files changed, 19 insertions(+), 79 deletions(-) diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index 4b85d124..b5ef9e4a 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -36,7 +36,7 @@ linux-x86_64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/69/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/88/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -59,7 +59,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/69/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/88/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -101,7 +101,7 @@ linux-x86_64 test: - python -m pip install --upgrade usd-core - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - python -m pip install -U "jax[cuda12]" - - python -m pip install --upgrade nvidia-mathdx==24.8.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 + - python -m pip install --upgrade nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" # HACK: disable P2P tests due to misbehaving agents @@ -118,7 +118,7 @@ linux-aarch64 test jetson: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - !reference [.snippets, install-python+warp-aarch64] - python -m pip install -U "jax[cuda12]" - - python -m pip install --upgrade nvidia-mathdx==24.8.0 nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 + - python -m pip install --upgrade nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - python -m warp.tests --junit-report-xml rspec.xml -s autodetect --failfast diff --git a/warp/builtins.py b/warp/builtins.py index 87ad6815..0d2d51b2 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -10,7 +10,6 @@ from typing import Any, Callable, Mapping, Sequence from warp.codegen import Reference, Var, strip_reference -from warp.mathdx import get_cuda_include_dirs, get_mathdx_include_dirs from warp.types import * from .context import add_builtin @@ -5675,13 +5674,12 @@ def make_function(M, N, K, adtype, bdtype, cdtype, alayout, blayout, clayout): # otherwise compile LTO lto_code = tempfile.NamedTemporaryFile() - include_dirs = get_cuda_include_dirs() result = warp.context.runtime.core.cuda_compile_dot( lto_code.name.encode("utf-8"), lto_symbol.encode("utf-8"), - len(include_dirs), - include_dirs, - get_mathdx_include_dirs(), + 0, + None, + None, arch, M, N, @@ -5877,14 +5875,12 @@ def tile_fft_generic_lto_dispatch_func( lto_code = tempfile.NamedTemporaryFile() shared_memory_size = ctypes.c_int(0) - include_dirs = get_cuda_include_dirs() - result = warp.context.runtime.core.cuda_compile_fft( lto_code.name.encode("utf-8"), lto_symbol.encode("utf-8"), - len(include_dirs), - include_dirs, - get_mathdx_include_dirs(), + 0, + None, + None, arch, size, ept, diff --git a/warp/mathdx.py b/warp/mathdx.py index dab9fbc8..e71faf06 100644 --- a/warp/mathdx.py +++ b/warp/mathdx.py @@ -14,8 +14,6 @@ from importlib.metadata import PackageNotFoundError, files CUDA_HOME = None -MATHDX_HOME = None -CUTLASS_HOME = None PLATFORM_LINUX = sys.platform.startswith("linux") @@ -96,51 +94,6 @@ def _check_cuda_home(): CUDA_HOME = (CUDA_HOME,) -def _check_mathdx_home(): - # Find mathDx headers - global MATHDX_HOME - - # Try wheel - try: - MATHDX_HOME = files("nvidia-mathdx") - except PackageNotFoundError: - pass - else: - # use cufftdx.hpp as a proxy - MATHDX_HOME = [f for f in MATHDX_HOME if "cufftdx.hpp" in str(f)][0] - MATHDX_HOME = os.path.join(os.path.dirname(MATHDX_HOME.locate()), "..") - return - - # Try conda - if "CONDA_PREFIX" in os.environ: - if PLATFORM_LINUX: - conda_include = os.path.join(os.environ["CONDA_PREFIX"], "include") - elif PLATFORM_WIN: - conda_include = os.path.join(os.environ["CONDA_PREFIX"], "Library", "include") - if os.path.isfile(os.path.join(conda_include, "cufftdx.hpp")): - MATHDX_HOME = os.path.join(conda_include, "..") - return - - # Try local - if "MATHDX_HOME" not in os.environ: - raise RuntimeError( - "mathDx headers not found. Depending on how you install nvmath-python and other CUDA packages, " - "you may need to perform one of the steps below:\n" - " - pip install nvidia-mathdx\n" - " - conda install -c conda-forge mathdx\n" - " - export MATHDX_HOME=/path/to/mathdx" - ) - else: - MATHDX_HOME = os.environ["MATHDX_HOME"] - - -def get_mathdx_include_dirs(): - _check_mathdx_home() - - global MATHDX_HOME - return (MATHDX_HOME + "/include").encode("utf-8") - - def get_cuda_include_dirs(): _check_cuda_home() diff --git a/warp/native/warp.cu b/warp/native/warp.cu index b043aeba..c930a913 100644 --- a/warp/native/warp.cu +++ b/warp/native/warp.cu @@ -17,7 +17,7 @@ #include #include #if WP_ENABLE_MATHDX - #include + #include #endif #include @@ -2881,9 +2881,11 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ CHECK_ANY(ltoir_output_path != nullptr); CHECK_ANY(symbol_name != nullptr); - CHECK_ANY(mathdx_include_dir != nullptr); CHECK_ANY(shared_memory_size != nullptr); - CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); + // Includes currently unused + CHECK_ANY(include_dirs == nullptr); + CHECK_ANY(mathdx_include_dir == nullptr); + CHECK_ANY(num_include_dirs == 0); bool res = true; cufftdxHandle h; @@ -2900,12 +2902,6 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ CHECK_CUFFTDX(cufftDxSetOperatorInt64(h, cufftDxOperatorType::CUFFTDX_OPERATOR_FFTS_PER_BLOCK, 1)); CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); - for(int dir = 0; dir < num_include_dirs; dir++) - { - CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); - } - CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); - CHECK_CUFFTDX(cufftDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/../external/cutlass/include").c_str())); size_t lto_size = 0; CHECK_CUFFTDX(cufftDxGetLTOIRSize(h, <o_size)); @@ -2931,8 +2927,10 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ CHECK_ANY(ltoir_output_path != nullptr); CHECK_ANY(symbol_name != nullptr); - CHECK_ANY(mathdx_include_dir != nullptr); - CHECK_ANY(num_include_dirs == 0 || include_dirs != nullptr); + // Includes currently unused + CHECK_ANY(include_dirs == nullptr); + CHECK_ANY(mathdx_include_dir == nullptr); + CHECK_ANY(num_include_dirs == 0); bool res = true; cublasdxHandle h; @@ -2953,13 +2951,6 @@ size_t cuda_compile_program(const char* cuda_src, int arch, const char* include_ CHECK_CUBLASDX(cublasDxSetOperatorInt64Array(h, cublasDxOperatorType::CUBLASDX_OPERATOR_ARRANGEMENT, arrangement.size(), arrangement.data())); CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_SYMBOL_NAME, symbol_name)); - for(int dir = 0; dir < num_include_dirs; dir++) - { - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, include_dirs[dir])); - } - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, mathdx_include_dir)); - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/cublasdx/include").c_str())); - CHECK_CUBLASDX(cublasDxSetOptionStr(h, commonDxOption::COMMONDX_OPTION_INCLUDE, (std::string(mathdx_include_dir) + "/../external/cutlass/include").c_str())); size_t lto_size = 0; CHECK_CUBLASDX(cublasDxGetLTOIRSize(h, <o_size)); From bac57c4f9af4c510b911bd6be019c5507aef34e1 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Mon, 28 Oct 2024 15:12:03 -0700 Subject: [PATCH 084/102] Removing previous CCCL/CUDA runtime wheels + updating to new libmathdx_static --- .gitlab/ci/mathdx-support.yml | 7 +-- warp/build.py | 7 +-- warp/mathdx.py | 104 ---------------------------------- 3 files changed, 4 insertions(+), 114 deletions(-) delete mode 100644 warp/mathdx.py diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index b5ef9e4a..d13873e9 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -36,7 +36,7 @@ linux-x86_64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/88/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/92/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -59,7 +59,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/88/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/92/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -101,7 +101,6 @@ linux-x86_64 test: - python -m pip install --upgrade usd-core - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - python -m pip install -U "jax[cuda12]" - - python -m pip install --upgrade nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - python -m pip install -e . - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" # HACK: disable P2P tests due to misbehaving agents @@ -118,7 +117,6 @@ linux-aarch64 test jetson: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - !reference [.snippets, install-python+warp-aarch64] - python -m pip install -U "jax[cuda12]" - - python -m pip install --upgrade nvidia-cuda-cccl-cu12 nvidia-cuda-runtime-cu12 - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - python -m warp.tests --junit-report-xml rspec.xml -s autodetect --failfast @@ -141,7 +139,6 @@ create pypi wheels: - python3 -m pip install --upgrade pip - python3 -m pip install build script: - - sed -i 's/dependencies = \["numpy"\]/dependencies = \["numpy", "nvidia-mathdx==24.4.0", "nvidia-cuda-cccl-cu12", "nvidia-cuda-runtime-cu12"\]/' pyproject.toml - sed -i "s/^\(.*\)$/\1+tile/" VERSION.md # Modify VERSION.md with +tile - python3 -m build --wheel -C--build-option=-Plinux-x86_64 - python3 -m build --wheel -C--build-option=-Plinux-aarch64 diff --git a/warp/build.py b/warp/build.py index 8655201c..d5193ad0 100644 --- a/warp/build.py +++ b/warp/build.py @@ -9,7 +9,6 @@ import os import warp.config -from warp.mathdx import get_cuda_include_dirs from warp.thirdparty import appdirs @@ -25,8 +24,6 @@ def build_cuda(cu_path, arch, output_path, config="release", verify_fp=False, fa warp.context.runtime.llvm.compile_cuda(src, cu_path, inc_path, output_path, False) else: - cuda_include_dirs = get_cuda_include_dirs() - if ltoirs is None: ltoirs = [] @@ -37,8 +34,8 @@ def build_cuda(cu_path, arch, output_path, config="release", verify_fp=False, fa src, arch, inc_path, - len(cuda_include_dirs), - cuda_include_dirs, + 0, + None, config == "debug", warp.config.verbose, verify_fp, diff --git a/warp/mathdx.py b/warp/mathdx.py deleted file mode 100644 index e71faf06..00000000 --- a/warp/mathdx.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved. -# NVIDIA CORPORATION and its licensors retain all intellectual property -# and proprietary rights in and to this software, related documentation -# and any modifications thereto. Any use, reproduction, disclosure or -# distribution of this software and related documentation without an express -# license agreement from NVIDIA CORPORATION is strictly prohibited. - -import ctypes -import os -import platform -import re -import sys -import warnings -from importlib.metadata import PackageNotFoundError, files - -CUDA_HOME = None - - -PLATFORM_LINUX = sys.platform.startswith("linux") -PLATFORM_WIN = sys.platform.startswith("win32") - - -def _conda_get_target_name(): - if PLATFORM_LINUX: - plat = platform.processor() - if plat == "aarch64": - return "sbsa-linux" - else: - return f"{plat}-linux" - elif PLATFORM_WIN: - return "x64" - else: - raise AssertionError - - -def _check_cuda_home(): - # We need some CUDA headers for compiling mathDx headers. - # We assume users properly managing their local envs (ex: no mix-n-match). - global CUDA_HOME - - # Try wheel - try: - # We need CUDA 12+ for device API support - cudart = files("nvidia-cuda-runtime-cu12") - cccl = files("nvidia-cuda-cccl-cu12") - # use cuda_fp16.h (which we need) as a proxy - cudart = [f for f in cudart if "cuda_fp16.h" in str(f)][0] - cudart = os.path.join(os.path.dirname(cudart.locate()), "..") - # use cuda/std/type_traits as a proxy - cccl = min([f for f in cccl if re.match(".*cuda\\/std\\/type_traits.*", str(f))], key=lambda x: len(str(x))) - cccl = os.path.join(os.path.dirname(cccl.locate()), "../../..") - except PackageNotFoundError: - pass - except ValueError: - # cccl wheel is buggy (headers missing), skip using wheels - pass - else: - CUDA_HOME = (cudart, cccl) - return - - # Try conda - if "CONDA_PREFIX" in os.environ: - if PLATFORM_LINUX: - conda_include = os.path.join( - os.environ["CONDA_PREFIX"], "targets", f"{_conda_get_target_name()}", "include" - ) - elif PLATFORM_WIN: - conda_include = os.path.join(os.environ["CONDA_PREFIX"], "Library", "include") - else: - assert AssertionError - if os.path.isfile(os.path.join(conda_include, "cuda_fp16.h")) and os.path.isfile( - os.path.join(conda_include, "cuda/std/type_traits") - ): - CUDA_HOME = (os.path.join(conda_include, ".."),) - return - - # Try local - CUDA_PATH = os.environ.get("CUDA_PATH", None) - CUDA_HOME = os.environ.get("CUDA_HOME", None) - if CUDA_PATH is None and CUDA_HOME is None: - raise RuntimeError( - "cudart headers not found. Depending on how you install nvmath-python and other CUDA packages,\n" - "you may need to perform one of the steps below:\n" - " - conda install -c conda-forge cuda-cudart-dev cuda-cccl cuda-version=12\n" - " - export CUDA_HOME=/path/to/CUDA/Toolkit" - ) - elif CUDA_PATH is not None and CUDA_HOME is None: - CUDA_HOME = CUDA_PATH - elif CUDA_PATH is not None and CUDA_HOME is not None: - if CUDA_HOME != CUDA_PATH: - warnings.warn( - "Both CUDA_HOME and CUDA_PATH are set but not consistent. " "Ignoring CUDA_PATH...", stacklevel=2 - ) - CUDA_HOME = (CUDA_HOME,) - - -def get_cuda_include_dirs(): - _check_cuda_home() - - global CUDA_HOME - include_dirs = [(f"{h}" + "/include").encode("utf-8") for h in CUDA_HOME] - arr_include_dirs = (ctypes.c_char_p * len(include_dirs))() - arr_include_dirs[:] = include_dirs - return arr_include_dirs From 6195e930666cff0135de21db3aa1312710531910 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Mon, 28 Oct 2024 16:35:00 -0700 Subject: [PATCH 085/102] Bump libmathdx build --- .gitlab/ci/mathdx-support.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml index d13873e9..bc711297 100644 --- a/.gitlab/ci/mathdx-support.yml +++ b/.gitlab/ci/mathdx-support.yml @@ -36,7 +36,7 @@ linux-x86_64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/92/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/93/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -59,7 +59,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/92/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/93/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps From 5fa11a75540834c5a12749a49d399c37d21e907f Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 29 Oct 2024 10:29:20 -0700 Subject: [PATCH 086/102] Fix Ruff errors --- warp/examples/tile/example_tile_mlp.py | 230 ++++++++++++------------- 1 file changed, 115 insertions(+), 115 deletions(-) diff --git a/warp/examples/tile/example_tile_mlp.py b/warp/examples/tile/example_tile_mlp.py index ef0f49e4..b5e4f82e 100644 --- a/warp/examples/tile/example_tile_mlp.py +++ b/warp/examples/tile/example_tile_mlp.py @@ -21,20 +21,20 @@ # ########################################################################### -import numpy as np -import warp as wp -import warp.examples -import warp.optim - import math import os +import numpy as np from PIL import Image +import warp as wp +import warp.examples +import warp.optim + rng = np.random.default_rng(45) -def create_layer(dim_in, dim_hid, dtype=float): +def create_layer(dim_in, dim_hid, dtype=float): w = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) b = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, 1)) @@ -43,8 +43,8 @@ def create_layer(dim_in, dim_hid, dtype=float): return (weights, bias) -def create_array(dim_in, dim_hid, dtype=float): +def create_array(dim_in, dim_hid, dtype=float): s = rng.uniform(-1.0 / np.sqrt(dim_in), 1.0 / np.sqrt(dim_in), (dim_hid, dim_in)) a = wp.array(s, dtype=dtype, requires_grad=True) @@ -54,63 +54,68 @@ def create_array(dim_in, dim_hid, dtype=float): # number of frequencies for the positional encoding NUM_FREQ = wp.constant(8) -DIM_IN = wp.constant(4*NUM_FREQ) # sin,cos for both x,y at each frequenecy +DIM_IN = wp.constant(4 * NUM_FREQ) # sin,cos for both x,y at each frequenecy DIM_HID = 32 DIM_OUT = 3 # threads per-block NUM_THREADS = 32 -IMG_WIDTH = NUM_THREADS*16 -IMG_HEIGHT = NUM_THREADS*16 +IMG_WIDTH = NUM_THREADS * 16 +IMG_HEIGHT = NUM_THREADS * 16 -BATCH_SIZE = min(1024, int((IMG_WIDTH*IMG_HEIGHT)/8)) +BATCH_SIZE = min(1024, int((IMG_WIDTH * IMG_HEIGHT) / 8)) # dtype for our weights and bias matrices dtype = wp.float16 + @wp.func def relu(x: dtype): return wp.max(x, dtype(0.0)) -@wp.kernel -def compute(indices: wp.array(dtype=int), - weights_0: wp.array2d(dtype=dtype), bias_0: wp.array2d(dtype=dtype), - weights_1: wp.array2d(dtype=dtype), bias_1: wp.array2d(dtype=dtype), - weights_2: wp.array2d(dtype=dtype), bias_2: wp.array2d(dtype=dtype), - weights_3: wp.array2d(dtype=dtype), bias_3: wp.array2d(dtype=dtype), - reference: wp.array2d(dtype=float), - loss: wp.array1d(dtype=float), - out: wp.array2d(dtype=float)): +@wp.kernel +def compute( + indices: wp.array(dtype=int), + weights_0: wp.array2d(dtype=dtype), + bias_0: wp.array2d(dtype=dtype), + weights_1: wp.array2d(dtype=dtype), + bias_1: wp.array2d(dtype=dtype), + weights_2: wp.array2d(dtype=dtype), + bias_2: wp.array2d(dtype=dtype), + weights_3: wp.array2d(dtype=dtype), + bias_3: wp.array2d(dtype=dtype), + reference: wp.array2d(dtype=float), + loss: wp.array1d(dtype=float), + out: wp.array2d(dtype=float), +): # batch indices linear = indices[wp.tid()] - row = linear/IMG_WIDTH - col = linear%IMG_WIDTH + row = linear / IMG_WIDTH + col = linear % IMG_WIDTH # normalize input coordinates to [-1, 1] - x = (float(row)/float(IMG_WIDTH) - 0.5)*2.0 - y = (float(col)/float(IMG_HEIGHT) - 0.5)*2.0 + x = (float(row) / float(IMG_WIDTH) - 0.5) * 2.0 + y = (float(col) / float(IMG_HEIGHT) - 0.5) * 2.0 local = wp.vector(dtype=dtype, length=DIM_IN) # construct positional encoding for s in range(NUM_FREQ): - - scale = wp.pow(2.0, float(s))*wp.pi + scale = wp.pow(2.0, float(s)) * wp.pi # x-coord - local[s*4 + 0] = dtype(wp.sin(x * scale)) - local[s*4 + 1] = dtype(wp.cos(x * scale)) + local[s * 4 + 0] = dtype(wp.sin(x * scale)) + local[s * 4 + 1] = dtype(wp.cos(x * scale)) # y-coord - local[s*4 + 2] = dtype(wp.sin(y * scale)) - local[s*4 + 3] = dtype(wp.cos(y * scale)) - + local[s * 4 + 2] = dtype(wp.sin(y * scale)) + local[s * 4 + 3] = dtype(wp.cos(y * scale)) # tile feature vectors across the block, returns [dim(f), NUM_THREADS] f = wp.tile(local) - + # input layer w0 = wp.tile_load(weights_0, 0, 0, m=DIM_HID, n=DIM_IN) b0 = wp.tile_load(bias_0, 0, 0, m=DIM_HID, n=1) @@ -134,78 +139,89 @@ def compute(indices: wp.array(dtype=int), output = wp.untile(o) # compute error - error = wp.vec3(float(output[0]) - reference[0,linear], - float(output[1]) - reference[1,linear], - float(output[2]) - reference[2,linear]) + error = wp.vec3( + float(output[0]) - reference[0, linear], + float(output[1]) - reference[1, linear], + float(output[2]) - reference[2, linear], + ) # write MSE loss if loss: - wp.atomic_add(loss, 0, wp.length_sq(error)/float(3*BATCH_SIZE)) + wp.atomic_add(loss, 0, wp.length_sq(error) / float(3 * BATCH_SIZE)) # write image output if out: for i in range(DIM_OUT): out[i, linear] = float(output[i]) - -class Example: +class Example: def __init__(self, train_iters): - self.weights_0, self.bias_0 = create_layer(DIM_IN, DIM_HID, dtype=dtype) self.weights_1, self.bias_1 = create_layer(DIM_HID, DIM_HID, dtype=dtype) self.weights_2, self.bias_2 = create_layer(DIM_HID, DIM_HID, dtype=dtype) self.weights_3, self.bias_3 = create_layer(DIM_HID, DIM_OUT, dtype=dtype) - # reference + # reference reference_path = os.path.join(wp.examples.get_asset_directory(), "pixel.jpg") with Image.open(reference_path) as im: - reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) / 255.0 - self.reference = wp.array(reference_image.reshape(IMG_WIDTH*IMG_HEIGHT, 3).T, dtype=float) + reference_image = np.asarray(im.resize((IMG_WIDTH, IMG_HEIGHT)).convert("RGB")) / 255.0 + self.reference = wp.array(reference_image.reshape(IMG_WIDTH * IMG_HEIGHT, 3).T, dtype=float) # create randomized batch indices - indices = np.arange(0, IMG_WIDTH*IMG_HEIGHT, dtype=np.int32) + indices = np.arange(0, IMG_WIDTH * IMG_HEIGHT, dtype=np.int32) rng.shuffle(indices) self.indices = wp.array(indices) - self.num_batches = int((IMG_WIDTH*IMG_HEIGHT)/BATCH_SIZE) + self.num_batches = int((IMG_WIDTH * IMG_HEIGHT) / BATCH_SIZE) self.max_iters = train_iters - self.max_epochs = max(1, int(self.max_iters/self.num_batches)) + self.max_epochs = max(1, int(self.max_iters / self.num_batches)) def train_warp(self): - - params = [self.weights_0, self.bias_0, - self.weights_1, self.bias_1, - self.weights_2, self.bias_2, - self.weights_3, self.bias_3] + params = [ + self.weights_0, + self.bias_0, + self.weights_1, + self.bias_1, + self.weights_2, + self.bias_2, + self.weights_3, + self.bias_3, + ] optimizer_grads = [p.grad.flatten() for p in params] optimizer_inputs = [p.flatten() for p in params] optimizer = warp.optim.Adam(optimizer_inputs, lr=0.01) - + loss = wp.zeros(1, dtype=float, requires_grad=True) - output = create_array(IMG_WIDTH*IMG_HEIGHT, DIM_OUT) + output = create_array(IMG_WIDTH * IMG_HEIGHT, DIM_OUT) # capture graph for whole epoch wp.capture_begin() - - for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): + for b in range(0, IMG_WIDTH * IMG_HEIGHT, BATCH_SIZE): loss.zero_() with wp.Tape() as tape: wp.launch( - compute, + compute, dim=[BATCH_SIZE], - inputs=[self.indices[b:b+BATCH_SIZE], - self.weights_0, self.bias_0, - self.weights_1, self.bias_1, - self.weights_2, self.bias_2, - self.weights_3, self.bias_3, - self.reference, - loss, - None], - block_dim=NUM_THREADS) + inputs=[ + self.indices[b : b + BATCH_SIZE], + self.weights_0, + self.bias_0, + self.weights_1, + self.bias_1, + self.weights_2, + self.bias_2, + self.weights_3, + self.bias_3, + self.reference, + loss, + None, + ], + block_dim=NUM_THREADS, + ) tape.backward(loss) optimizer.step(optimizer_grads) @@ -213,36 +229,36 @@ def train_warp(self): graph = wp.capture_end() - with wp.ScopedTimer("Training"): - for i in range(self.max_epochs): - with wp.ScopedTimer("Epoch"): wp.capture_launch(graph) print(f"Epoch: {i} Loss: {loss.numpy()}") - # evaluate full image wp.launch( - compute, - dim=[IMG_WIDTH*IMG_HEIGHT], - inputs=[self.indices, - self.weights_0, self.bias_0, - self.weights_1, self.bias_1, - self.weights_2, self.bias_2, - self.weights_3, self.bias_3, - self.reference, - loss, - output], - block_dim=NUM_THREADS) - - self.save_image(f"example_tile_mlp.jpg", output.numpy()) - - + compute, + dim=[IMG_WIDTH * IMG_HEIGHT], + inputs=[ + self.indices, + self.weights_0, + self.bias_0, + self.weights_1, + self.bias_1, + self.weights_2, + self.bias_2, + self.weights_3, + self.bias_3, + self.reference, + loss, + output, + ], + block_dim=NUM_THREADS, + ) + + self.save_image("example_tile_mlp.jpg", output.numpy()) def train_torch(self): - import torch as tc weights_0 = tc.nn.Parameter(wp.to_torch(self.weights_0)) @@ -258,20 +274,17 @@ def train_torch(self): indices = wp.to_torch(self.indices) reference = wp.to_torch(self.reference) - optimizer = tc.optim.Adam([weights_0, - bias_0, - weights_1, - bias_1, - weights_2, - bias_2, - weights_3, - bias_3], capturable=True, lr=0.0001, betas=(0.9, 0.95), eps=1.e-6) - + optimizer = tc.optim.Adam( + [weights_0, bias_0, weights_1, bias_1, weights_2, bias_2, weights_3, bias_3], + capturable=True, + lr=0.0001, + betas=(0.9, 0.95), + eps=1.0e-6, + ) # generate frequency space encoding of pixels # based on their linear index in the image def encode(linear): - row = (linear // IMG_WIDTH).float() col = (linear % IMG_WIDTH).float() @@ -287,17 +300,16 @@ def encode(linear): encoding[s * 4 + 0, :] = tc.sin(scale * x) encoding[s * 4 + 1, :] = tc.cos(scale * x) encoding[s * 4 + 2, :] = tc.sin(scale * y) - encoding[s * 4 + 3, :] = tc.cos(scale * y) + encoding[s * 4 + 3, :] = tc.cos(scale * y) return encoding - stream = tc.cuda.Stream() graph = tc.cuda.CUDAGraph() # warm-up with tc.cuda.stream(stream): - f = tc.rand((NUM_FREQ*4, BATCH_SIZE), dtype=tc.float16, device="cuda") + f = tc.rand((NUM_FREQ * 4, BATCH_SIZE), dtype=tc.float16, device="cuda") z = tc.relu(weights_0 @ f + bias_0) z = tc.relu(weights_1 @ z + bias_1) z = tc.relu(weights_2 @ z + bias_2) @@ -309,10 +321,8 @@ def encode(linear): optimizer.step() with tc.cuda.graph(graph): - - for b in range(0, IMG_WIDTH*IMG_HEIGHT, BATCH_SIZE): - - linear = indices[b:b+BATCH_SIZE] + for b in range(0, IMG_WIDTH * IMG_HEIGHT, BATCH_SIZE): + linear = indices[b : b + BATCH_SIZE] f = encode(linear) @@ -323,23 +333,19 @@ def encode(linear): ref = reference[:, linear] loss = tc.mean((z - ref) ** 2) - + optimizer.zero_grad() loss.backward() optimizer.step() - with wp.ScopedTimer("Training (Torch)"): - - for i in range(self.max_epochs): - + for _i in range(self.max_epochs): with wp.ScopedTimer("Epoch"): graph.replay() print(loss) - - f = encode(tc.arange(0, IMG_WIDTH*IMG_HEIGHT)) + f = encode(tc.arange(0, IMG_WIDTH * IMG_HEIGHT)) z = tc.relu(weights_0 @ f + bias_0) z = tc.relu(weights_1 @ z + bias_1) z = tc.relu(weights_2 @ z + bias_2) @@ -347,9 +353,7 @@ def encode(linear): self.save_image("example_tile_mlp_torch.jpg", z.detach().cpu().numpy()) - def save_image(self, name, output): - predicted_image = output.T.reshape(IMG_WIDTH, IMG_HEIGHT, 3) predicted_image = (predicted_image * 255).astype(np.uint8) @@ -357,10 +361,7 @@ def save_image(self, name, output): predicted_image_pil.save(name) - - if __name__ == "__main__": - import argparse parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) @@ -369,7 +370,6 @@ def save_image(self, name, output): args = parser.parse_known_args()[0] with wp.ScopedDevice("cuda:0"): - example = Example(args.train_iters) example.train_warp() - #example.train_torch() + # example.train_torch() From a14bac183b863e2ff1518158176c6bb88c84ed45 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 29 Oct 2024 11:41:40 -0700 Subject: [PATCH 087/102] Merge with main --- .gitlab-ci.yml | 24 ++- .gitlab/ci/additional-tests.yml | 6 +- .gitlab/ci/cuda-11-build-and-test.yml | 10 +- .gitlab/ci/debug-build-and-test.yml | 6 +- CHANGELOG.md | 144 +++++++++++------- README.md | 6 +- VERSION.md | 2 +- docs/changelog.md | 8 + docs/codegen.rst | 148 +++++++++++++++++- docs/conf.py | 6 + docs/index.rst | 1 + docs/installation.rst | 6 +- docs/modules/differentiability.rst | 8 +- docs/modules/functions.rst | 168 +++++++++++---------- docs/modules/sim.rst | 2 + docs/requirements.txt | 1 + exts/omni.warp.core/config/extension.toml | 2 +- exts/omni.warp.core/docs/CHANGELOG.md | 125 +++++++++------- exts/omni.warp/config/extension.toml | 4 +- exts/omni.warp/docs/CHANGELOG.md | 125 +++++++++------- warp/__init__.py | 3 + warp/builtins.py | 140 +++++++++-------- warp/codegen.py | 43 +++--- warp/config.py | 2 +- warp/context.py | 52 +++---- warp/examples/fem/utils.py | 3 +- warp/examples/optim/example_walker.py | 4 +- warp/fem/utils.py | 13 +- warp/native/array.h | 80 +++++----- warp/native/builtin.h | 78 +++++++--- warp/native/bvh.cu | 4 +- warp/native/bvh.h | 4 + warp/native/exports.h | 17 +++ warp/native/hashgrid.h | 4 + warp/native/mesh.cu | 4 +- warp/native/mesh.h | 4 + warp/native/range.h | 17 ++- warp/sim/integrator_xpbd.py | 8 +- warp/sim/model.py | 5 +- warp/sparse.py | 16 +- warp/stubs.py | 174 +++++++++++----------- warp/tests/test_array.py | 82 ++++++++++ warp/tests/test_codegen.py | 70 +++++++++ warp/tests/test_fabricarray.py | 33 ++++ warp/tests/test_fem.py | 18 ++- warp/tests/test_func.py | 36 ++++- warp/tests/test_generics.py | 52 +++++++ warp/tests/test_iter.py | 68 +++++++++ warp/tests/test_model.py | 13 ++ warp/tests/test_print.py | 135 +++++++++++++++++ warp/tests/test_static.py | 158 +++++++++++++++++++- warp/tests/unittest_suites.py | 4 + warp/types.py | 37 +++-- 53 files changed, 1603 insertions(+), 580 deletions(-) create mode 100644 docs/changelog.md create mode 100644 warp/tests/test_iter.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 554b9273..566a12bc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,11 @@ # CI/CD Pipeline Configuration # ============================================================================== -include: /.gitlab/ci/common.yml +include: + - local: /.gitlab/ci/common.yml + - project: "omniverse/devplat/gitlab/templates/common/compliance" + file: "modules/omniverse-repo-compliance.gitlab-ci.yml" + ref: v1_latest workflow: rules: @@ -66,7 +70,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - - ./tools/ci/building/build-linux-x86_64/build.sh --no-docker # We are already using the builder image + - ./tools/ci/building/build-linux-aarch64/build.sh --no-docker # We are already using the builder image - mkdir -p warp/bin/linux-aarch64 - mv warp/bin/warp.so warp/bin/linux-aarch64 - mv warp/bin/warp-clang.so warp/bin/linux-aarch64 @@ -139,6 +143,12 @@ ruff format: script: - ruff format --diff +osec:sonarqube: + variables: + # Disable C/C++ analyzer until project specific work is done to enable it. + # See: https://confluence.nvidia.com/display/OMNIVERSE/SonarQube+Gitlab+CI+Integration#C+Project+Enablement+Additions + SONAR_EXTRA_ARGS: "-Dsonar.c.file.suffixes=- -Dsonar.cpp.file.suffixes=- -Dsonar.objc.file.suffixes=-" + # ============================================================================== # Main Unit Testing Jobs # @@ -340,11 +350,7 @@ windows-x86_64 test mgpu: - tools/packman/packman.cmd install -l _build/target-deps/python python $python_name - '& $env:CI_PROJECT_DIR\_build\target-deps\python\python.exe -m venv _venv' - .\_venv\Scripts\Activate.ps1 - - python -m pip install --upgrade pip - - python -m pip install --upgrade usd-core - # Temporary HACK: use NumPy < 2.0 on Windows due to issues with Torch wheels that are not compatible - # https://github.com/pytorch/pytorch/issues/128860 - - python -m pip install "numpy<2" + - python -m pip install --upgrade pip usd-core numpy - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - python -m pip install -e . - Write-Output "$([char]27)[0Ksection_end:$(GetTime):install_dependencies$([char]13)$([char]27)[0K" @@ -377,6 +383,7 @@ linux-x86_64 test warp-init: - build_llvm.py - when: manual # If not auto-triggered, allow any pipeline to run this job manually allow_failure: true + timeout: 10m before_script: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - df -h @@ -728,6 +735,7 @@ merge request docs: - public rules: - if: $CI_PIPELINE_SOURCE == 'merge_request_event' + timeout: 10m environment: name: review/$CI_MERGE_REQUEST_IID url: https://$CI_PROJECT_ROOT_NAMESPACE.$CI_PAGES_DOMAIN/-/$CI_PROJECT_NAME/-/jobs/$CI_JOB_ID/artifacts/public/index.html @@ -752,6 +760,7 @@ check generated files: - docs/modules/functions.rst rules: - if: $CI_PIPELINE_SOURCE == 'merge_request_event' + timeout: 10m extends: - .runner-utility-linux-x86_64 script: @@ -769,6 +778,7 @@ pages: - .build-docs-common rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + timeout: 10m environment: name: GitLab Pages deployment_tier: staging diff --git a/.gitlab/ci/additional-tests.yml b/.gitlab/ci/additional-tests.yml index aba4a45d..7a59be88 100644 --- a/.gitlab/ci/additional-tests.yml +++ b/.gitlab/ci/additional-tests.yml @@ -67,11 +67,7 @@ windows-x86_64 test: - tools/packman/packman.cmd install -l _build/target-deps/python python $python_name - '& $env:CI_PROJECT_DIR\_build\target-deps\python\python.exe -m venv _venv' - .\_venv\Scripts\Activate.ps1 - - python -m pip install --upgrade pip - - python -m pip install --upgrade usd-core - # Temporary HACK: use NumPy < 2.0 on Windows due to issues with Torch wheels that are not compatible - # https://github.com/pytorch/pytorch/issues/128860 - - python -m pip install "numpy<2" + - python -m pip install --upgrade pip usd-core numpy - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - python -m pip install -e . - Write-Output "$([char]27)[0Ksection_end:$(GetTime):install_dependencies$([char]13)$([char]27)[0K" diff --git a/.gitlab/ci/cuda-11-build-and-test.yml b/.gitlab/ci/cuda-11-build-and-test.yml index 7282d9e8..3f5cd25d 100644 --- a/.gitlab/ci/cuda-11-build-and-test.yml +++ b/.gitlab/ci/cuda-11-build-and-test.yml @@ -25,6 +25,7 @@ include: - "templates/v3/windows/codesign.gitlab-ci.yml" - "templates/v3/linux/packman_s3.gitlab-ci.yml" - "templates/v3/windows/packman_s3.gitlab-ci.yml" + - "templates/v3/linux/nucleus/kit-extensions.ov.nvidia.com/kit-extension-svc.gitlab-ci.yml" ref: v1_latest @@ -52,7 +53,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - - ./tools/ci/building/build-linux-x86_64/build.sh --cuda 11 --no-docker # We are already using the builder image + - ./tools/ci/building/build-linux-aarch64/build.sh --cuda 11 --no-docker # We are already using the builder image - mkdir -p warp/bin/linux-aarch64 - mv warp/bin/warp.so warp/bin/linux-aarch64 - mv warp/bin/warp-clang.so warp/bin/linux-aarch64 @@ -144,11 +145,7 @@ windows-x86_64 test: - tools/packman/packman.cmd install -l _build/target-deps/python python $python_name - '& $env:CI_PROJECT_DIR\_build\target-deps\python\python.exe -m venv _venv' - .\_venv\Scripts\Activate.ps1 - - python -m pip install --upgrade pip - - python -m pip install --upgrade usd-core - # Temporary HACK: use NumPy < 2.0 on Windows due to issues with Torch wheels that are not compatible - # https://github.com/pytorch/pytorch/issues/128860 - - python -m pip install "numpy<2" + - python -m pip install --upgrade pip usd-core numpy - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - python -m pip install -e . - Write-Output "$([char]27)[0Ksection_end:$(GetTime):install_dependencies$([char]13)$([char]27)[0K" @@ -314,4 +311,5 @@ publish extensions to packman: script: - !reference [.osec:vault:v3:linux, codesign:perform_vault_requests] - !reference [.osec:vault:v3:linux, packman_s3:perform_vault_requests] + - !reference [.osec:vault:v3:linux, nucleus:kit-extensions.ov.nvidia.com:kit-extension-svc:perform_vault_requests] - tools/repo.sh publish_exts --publish-all diff --git a/.gitlab/ci/debug-build-and-test.yml b/.gitlab/ci/debug-build-and-test.yml index e041739a..d028af2e 100644 --- a/.gitlab/ci/debug-build-and-test.yml +++ b/.gitlab/ci/debug-build-and-test.yml @@ -136,11 +136,7 @@ windows-x86_64 test: - tools/packman/packman.cmd install -l _build/target-deps/python python $python_name - '& $env:CI_PROJECT_DIR\_build\target-deps\python\python.exe -m venv _venv' - .\_venv\Scripts\Activate.ps1 - - python -m pip install --upgrade pip - - python -m pip install --upgrade usd-core - # Temporary HACK: use NumPy < 2.0 on Windows due to issues with Torch wheels that are not compatible - # https://github.com/pytorch/pytorch/issues/128860 - - python -m pip install "numpy<2" + - python -m pip install --upgrade pip usd-core numpy - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - python -m pip install -e . - Write-Output "$([char]27)[0Ksection_end:$(GetTime):install_dependencies$([char]13)$([char]27)[0K" diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ac1e54d..fae03b44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,35 @@ -# CHANGELOG +# Changelog + +## [1.4.0] - 2024-10-01 + +### Added + +- Expose a `reversed()` built-in for iterators to test ([GH-311](https://github.com/NVIDIA/warp/issues/311)). + +### Changed + +- Promote the `wp.Int`, `wp.Float`, and `wp.Scalar` generic annotation types to the public API. +- Make the output of `wp.print()` in backward kernels consistent for all supported data types. + +### Fixed + +- Fix to relax the integer types expected when indexing arrays (regression in 1.3.0). +- Fix printing vector and matrix adjoints in backward kernels. +- Fix kernel compile error when printing structs. +- Fix an incorrect user function being sometimes resolved when multiple overloads are available with array parameters with different `dtype` values. +- Fix error being raised when static and dynamic for-loops are written in sequence with the same iteration variable names ([GH-331](https://github.com/NVIDIA/warp/issues/331)). + +## [1.4.1] - 2024-10-15 + +### Fixed + +- Fix `iter_reverse()` not working as expected for ranges with steps other than 1 ([GH-311](https://github.com/NVIDIA/warp/issues/311)). +- Fix potential out-of-bounds memory access when a `wp.sparse.BsrMatrix` object is reused for storing matrices of different shapes. +- Fix robustness to very low desired tolerance in `wp.fem.utils.symmetric_eigenvalues_qr`. +- Fix invalid code generation error messages when nesting dynamic and static for-loops. +- Fix caching of kernels with static expressions. +- Fix `ModelBuilder.add_builder(builder)` to correctly update `articulation_start` and thereby `articulation_count` when `builder` contains more than one articulation. +- Re-introduced the `wp.rand*()`, `wp.sample*()`, and `wp.poisson()` onto the Python scope to revert a breaking change. ## [1.4.0] - 2024-10-01 @@ -204,7 +235,7 @@ - Fix for handling of `bool` types in generic kernels - Publish CUDA 12.5 binaries for Hopper support, see https://github.com/nvidia/warp?tab=readme-ov-file#installing for details -## [1.1.1] - 2024-05-24 +## 1.1.1 - 2024-05-24 - `wp.init()` is no longer required to be called explicitly and will be performed on first call to the API - Speed up `omni.warp.core`'s startup time @@ -239,7 +270,7 @@ - Support gradient propagation for indexing sliced multi-dimensional arrays, i.e. `a[i][j]` vs. `a[i, j]` - Provide an informative message if setting DLL C-types failed, instructing to try rebuilding the library -## [1.0.3] - 2024-04-17 +## 1.0.3 - 2024-04-17 - Add a `support_level` entry to the configuration file of the extensions @@ -317,7 +348,7 @@ - Added `wp.ones()` to efficiently create one-initialized arrays - Rename `wp.config.graph_capture_module_load_default` to `wp.config.enable_graph_capture_module_load_by_default` -## [0.14.0] - 2024-02-19 +## 0.14.0 - 2024-02-19 - Add support for CUDA pooled (stream-ordered) allocators - Support memory allocation during graph capture @@ -354,7 +385,7 @@ - Fixed a small CPU memory leak related to DLPack interop - Improved performance of creating arrays -## [0.13.1] - 2024-02-22 +## 0.13.1 - 2024-02-22 - Ensure that the results from the `Noise Deform` are deterministic across different Kit sessions @@ -367,7 +398,7 @@ - Add missing `.py` extension to `warp/tests/walkthrough_debug` - Allow `wp.bool` usage in vector and matrix types -## [0.12.0] - 2024-02-05 +## 0.12.0 - 2024-02-05 - Add a warning when the `enable_backward` setting is set to `False` upon calling `wp.Tape.backward()` - Fix kernels not being recompiled as expected when defined using a closure @@ -383,7 +414,7 @@ - Point releases (if any) go on the same minor release branch and only contain bug fixes, not new features. - The `public` branch, previously used to merge releases into and corresponding with the GitHub `main` branch, is retired. -## [1.0.0-beta.7] - 2024-01-23 +## 1.0.0-beta.7 - 2024-01-23 - Ensure captures are always enclosed in `try`/`finally` - Only include .py files from the warp subdirectory into wheel packages @@ -445,7 +476,7 @@ - Documentation update for `wp.BVH` - Documentation and simplified API for runtime kernel specialization `wp.Kernel` -## [1.0.0-beta.4] - 2023-11-01 +## 1.0.0-beta.4 - 2023-11-01 - Add `wp.cbrt()` for cube root calculation - Add `wp.mesh_furthest_point_no_sign()` to compute furthest point on a surface from a query point @@ -457,7 +488,7 @@ - Fix for `wp.utils.array_sum()` output initialization when used with vector types - Coverage and documentation updates -## [1.0.0-beta.3] - 2023-10-19 +## 1.0.0-beta.3 - 2023-10-19 - Add support for code coverage scans (test_coverage.py), coverage at 85% in `omni.warp.core` - Add support for named component access for vector types, e.g.: `a = v.x` @@ -479,13 +510,13 @@ - To support grid-stride kernels, `wp.tid()` can no longer be called inside `wp.func` functions. -## [1.0.0-beta.2] - 2023-09-01 +## 1.0.0-beta.2 - 2023-09-01 - Fix for passing bool into `wp.func` functions - Fix for deprecation warnings appearing on `stderr`, now redirected to `stdout` - Fix for using `for i in wp.hash_grid_query(..)` syntax -## [1.0.0-beta.1] - 2023-08-29 +## 1.0.0-beta.1 - 2023-08-29 - Fix for `wp.float16` being passed as kernel arguments - Fix for compile errors with kernels using structs in backward pass @@ -524,7 +555,7 @@ - Update margin used by for mesh queries when using `wp.sim.create_soft_body_contacts()` - Improvements to gradient handling with `wp.from_torch()`, `wp.to_torch()` plus documentation -## [0.10.0] - 2023-07-05 +## 0.10.0 - 2023-07-05 - Add support for macOS universal binaries (x86 + aarch64) for M1+ support - Add additional methods for SDF generation please see the following new methods: @@ -600,7 +631,7 @@ - Deprecate `wp.Model.soft_contact_distance` which is now replaced by `wp.Model.particle_radius` - Deprecate single scalar particle radius (should be a per-particle array) -## [0.8.2] - 2023-04-21 +## 0.8.2 - 2023-04-21 - Add `ModelBuilder.soft_contact_max` to control the maximum number of soft contacts that can be registered. Use `Model.allocate_soft_contacts(new_count)` to change count on existing `Model` objects. - Add support for `bool` parameters @@ -611,12 +642,12 @@ - Add sign determination using winding number of `wp.mesh_query_point()` as `wp.mesh_query_sign_winding_number()` - Add query point without sign determination `wp.mesh_query_no_sign()` -## [0.8.1] - 2023-04-13 +## 0.8.1 - 2023-04-13 - Fix for regression when passing flattened numeric lists as matrix arguments to kernels - Fix for regressions when passing `wp.struct` types with uninitialized (`None`) member attributes -## [0.8.0] - 2023-04-05 +## 0.8.0 - 2023-04-05 - Add `Texture Write` node for updating dynamic RTX textures from Warp kernels / nodes - Add multi-dimensional kernel support to Warp Kernel Node @@ -660,14 +691,14 @@ - `wp.sim.model.ground_plane` is now a `wp.array` to support gradient, users should call `builder.set_ground_plane()` to create the ground - `wp.sim` capsule, cones, and cylinders are now aligned with the default USD up-axis -## [0.7.2] - 2023-02-15 +## 0.7.2 - 2023-02-15 - Reduce test time for vec/math types - Clean-up CUDA disabled build pipeline - Remove extension.gen.toml to make Kit packages Python version independent - Handle additional cases for array indexing inside Python -## [0.7.1] - 2023-02-14 +## 0.7.1 - 2023-02-14 - Disabling some slow tests for Kit - Make unit tests run on first GPU only by default @@ -684,13 +715,13 @@ - Add security pop-up for Kernel Node - Improve error handling for kernel return values -## [0.6.3] - 2023-01-31 +## 0.6.3 - 2023-01-31 - Add DLPack utilities, see `wp.from_dlpack()`, `wp.to_dlpack()` - Add Jax utilities, see `wp.from_jax()`, `wp.to_jax()`, `wp.device_from_jax()`, `wp.device_to_jax()` - Fix for Linux Kit extensions OM-80132, OM-80133 -## [0.6.2] - 2023-01-19 +## 0.6.2 - 2023-01-19 - Updated `wp.from_torch()` to support more data types - Updated `wp.from_torch()` to automatically determine the target Warp data type if not specified @@ -705,14 +736,14 @@ - Replace Python `imp` package with `importlib` - Fix for quaternion slerp gradients (`wp.quat_slerp()`) -## [0.6.1] - 2022-12-05 +## 0.6.1 - 2022-12-05 - Fix for non-CUDA builds - Fix strides computation in array_t constructor, fixes a bug with accessing mesh indices through mesh.indices[] - Disable backward pass code generation for kernel node (4-6x faster compilation) - Switch to linbuild for universal Linux binaries (affects TeamCity builds only) -## [0.6.0] - 2022-11-28 +## 0.6.0 - 2022-11-28 - Add support for CUDA streams, see `wp.Stream`, `wp.get_stream()`, `wp.set_stream()`, `wp.synchronize_stream()`, `wp.ScopedStream` - Add support for CUDA events, see `wp.Event`, `wp.record_event()`, `wp.wait_event()`, `wp.wait_stream()`, `wp.Stream.record_event()`, `wp.Stream.wait_event()`, `wp.Stream.wait_stream()` @@ -737,7 +768,7 @@ - Fix various deployment issues by statically linking with all CUDA libs - Update warp.so/warp.dll to CUDA Toolkit 11.5 -## [0.5.1] - 2022-11-01 +## 0.5.1 - 2022-11-01 - Fix for unit tests in Kit @@ -774,14 +805,14 @@ - Fix for arrays > 2GB in length - Add support for per-vertex USD mesh colors with `wp.render` class -## [0.4.2] - 2022-09-07 +## 0.4.2 - 2022-09-07 - Register Warp samples to the sample browser in Kit - Add NDEBUG flag to release mode kernel builds - Fix for particle solver node when using a large number of particles - Fix for broken cameras in Warp sample scenes -## [0.4.1] - 2022-08-30 +## 0.4.1 - 2022-08-30 - Add geometry sampling methods, see `wp.sample_unit_cube()`, `wp.sample_unit_disk()`, etc - Add `wp.lower_bound()` for searching sorted arrays @@ -791,7 +822,7 @@ - Fix for debug flags not being set correctly on CUDA when `wp.config.mode == "debug"`, this enables bounds checking on CUDA kernels in debug mode - Fix for code gen of functions that do not return a value -## [0.4.0] - 2022-08-09 +## 0.4.0 - 2022-08-09 - Fix for FP16 conversions on GPUs without hardware support - Fix for `runtime = None` errors when reloading the Warp module @@ -808,7 +839,7 @@ - Removed `wp.runtime` reference from the top-level module, as it should be considered private -## [0.3.2] - 2022-07-19 +## 0.3.2 - 2022-07-19 - Remove Torch import from `__init__.py`, defer import to `wp.from_torch()`, `wp.to_torch()` @@ -830,7 +861,7 @@ - `wp.synchronize()` now synchronizes all devices; for finer-grained control, use `wp.synchronize_device()` - Device alias `"cuda"` now refers to the current CUDA context, rather than a specific device like `"cuda:0"` or `"cuda:1"` -## [0.3.0] - 2022-07-08 +## 0.3.0 - 2022-07-08 - Add support for FP16 storage type, see `wp.float16` - Add support for per-dimension byte strides, see `wp.array.strides` @@ -867,7 +898,7 @@ - Tape `capture` option has been removed, users can now capture tapes inside existing CUDA graphs (e.g.: inside Torch) - Scalar loss arrays should now explicitly set `requires_grad=True` at creation time -## [0.2.2] - 2022-05-30 +## 0.2.2 - 2022-05-30 - Fix for `from import *` inside Warp initialization - Fix for body space velocity when using deforming Mesh objects with scale @@ -891,7 +922,7 @@ - Local `@wp.func` functions should not be namespaced when called, e.g.: previously `wp.myfunc()` would work even if `myfunc()` was not a builtin - Removed `wp.rpy2quat()`, please use `wp.quat_rpy()` instead -## [0.2.1] - 2022-05-11 +## 0.2.1 - 2022-05-11 - Fix for unit tests in Kit @@ -940,7 +971,7 @@ - `wp.array.length` member has been removed, please use `wp.array.shape` to access array dimensions, or use `wp.array.size` to get total element count - Marking `dense_gemm()`, `dense_chol()`, etc methods as experimental until we revisit them -## [0.1.25] - 2022-03-20 +## 0.1.25 - 2022-03-20 - Add support for class methods to be Warp kernels - Add HashGrid reserve() so it can be used with CUDA graphs @@ -950,7 +981,7 @@ - Add support for floored division on integer types - Move tests into core library so they can be run in Kit environment -## [0.1.24] - 2022-03-03 +## 0.1.24 - 2022-03-03 ### Warp Core @@ -966,7 +997,7 @@ - Fix for ranged for loops with negative step sizes - Fix for 3d and 4d spherical gradient distributions -## [0.1.23] - 2022-02-17 +## 0.1.23 - 2022-02-17 ### Warp Core @@ -976,7 +1007,7 @@ - Add procedural noise primitives, see `wp.noise()`, `wp.pnoise()`, `wp.curlnoise()` - Move simulation helpers our of test into `wp.sim` module -## [0.1.22] - 2022-02-14 +## 0.1.22 - 2022-02-14 ### Warp Core @@ -990,7 +1021,7 @@ - Add support for universal and compound joint types -## [0.1.21] - 2022-01-19 +## 0.1.21 - 2022-01-19 ### Warp Core @@ -1010,19 +1041,19 @@ - New OgnParticleVolume node for sampling shapes -> particles - New OgnParticleSolver node for DEM style granular materials -## [0.1.20] - 2021-11-02 +## 0.1.20 - 2021-11-02 - Updates to the ripple solver for GTC (support for multiple colliders, buoyancy, etc) -## [0.1.19] - 2021-10-15 +## 0.1.19 - 2021-10-15 - Publish from 2021.3 to avoid omni.graph database incompatibilities -## [0.1.18] - 2021-10-08 +## 0.1.18 - 2021-10-08 - Enable Linux support (tested on 20.04) -## [0.1.17] - 2021-09-30 +## 0.1.17 - 2021-09-30 - Fix for 3x3 SVD adjoint - Fix for A6000 GPU (bump compute model to sm_52 minimum) @@ -1031,12 +1062,12 @@ - Rename spatial_transform -> transform - Documentation update -## [0.1.16] - 2021-09-06 +## 0.1.16 - 2021-09-06 - Fix for case where simple assignments (a = b) incorrectly generated reference rather than value copy - Handle passing zero-length (empty) arrays to kernels -## [0.1.15] - 2021-09-03 +## 0.1.15 - 2021-09-03 - Add additional math library functions (asin, etc) - Add builtin 3x3 SVD support @@ -1049,62 +1080,62 @@ - Removes the need to transfer array to CPU before numpy conversion (will be done implicitly) - Update the example OgnRipple wave equation solver to use bundles -## [0.1.14] - 2021-08-09 +## 0.1.14 - 2021-08-09 - Fix for out-of-bounds memory access in CUDA BVH - Better error checking after kernel launches (use `wp.config.verify_cuda=True`) - Fix for vec3 normalize adjoint code -## [0.1.13] - 2021-07-29 +## 0.1.13 - 2021-07-29 - Remove OgnShrinkWrap.py test node -## [0.1.12] - 2021-07-29 +## 0.1.12 - 2021-07-29 - Switch to Woop et al.'s watertight ray-tri intersection test - Disable --fast-math in CUDA compilation step for improved precision -## [0.1.11] - 2021-07-28 +## 0.1.11 - 2021-07-28 - Fix for `wp.mesh_query_ray()` returning incorrect t-value -## [0.1.10] - 2021-07-28 +## 0.1.10 - 2021-07-28 - Fix for OV extension fwatcher filters to avoid hot-reload loop due to OGN regeneration -## [0.1.9] - 2021-07-21 +## 0.1.9 - 2021-07-21 - Fix for loading sibling DLL paths - Better type checking for built-in function arguments - Added runtime docs, can now list all builtins using `wp.print_builtins()` -## [0.1.8] - 2021-07-14 +## 0.1.8 - 2021-07-14 - Fix for hot-reload of CUDA kernels - Add Tape object for replaying differentiable kernels - Add helpers for Torch interop (convert `torch.Tensor` to `wp.Array`) -## [0.1.7] - 2021-07-05 +## 0.1.7 - 2021-07-05 - Switch to NVRTC for CUDA runtime - Allow running without host compiler - Disable asserts in kernel release mode (small perf. improvement) -## [0.1.6] - 2021-06-14 +## 0.1.6 - 2021-06-14 - Look for CUDA toolchain in target-deps -## [0.1.5] - 2021-06-14 +## 0.1.5 - 2021-06-14 - Rename OgLang -> Warp - Improve CUDA environment error checking - Clean-up some logging, add verbose mode (`wp.config.verbose`) -## [0.1.4] - 2021-06-10 +## 0.1.4 - 2021-06-10 - Add support for mesh raycast -## [0.1.3] - 2021-06-09 +## 0.1.3 - 2021-06-09 - Add support for unary negation operator - Add support for mutating variables during dynamic loops (non-differentiable) @@ -1112,7 +1143,7 @@ - Improve kernel cache start up times (avoids adjointing before cache check) - Update README.md with requirements / examples -## [0.1.2] - 2021-06-03 +## 0.1.2 - 2021-06-03 - Add support for querying mesh velocities - Add CUDA graph support, see `wp.capture_begin()`, `wp.capture_end()`, `wp.capture_launch()` @@ -1122,15 +1153,16 @@ - Fix for Linux/macOS support -## [0.1.1] - 2021-05-18 +## 0.1.1 - 2021-05-18 - Fix bug with conflicting CUDA contexts -## [0.1.0] - 2021-05-17 +## 0.1.0 - 2021-05-17 - Initial publish for alpha testing -[Unreleased]: https://github.com/NVIDIA/warp/compare/v1.4.0...HEAD +[Unreleased]: https://github.com/NVIDIA/warp/compare/v1.4.1...HEAD +[1.4.1]: https://github.com/NVIDIA/warp/releases/tag/v1.4.1 [1.4.0]: https://github.com/NVIDIA/warp/releases/tag/v1.4.0 [1.3.3]: https://github.com/NVIDIA/warp/releases/tag/v1.3.3 [1.3.2]: https://github.com/NVIDIA/warp/releases/tag/v1.3.2 diff --git a/README.md b/README.md index 54c1bbfd..ac8a11dc 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,9 @@ the `pip install` command, e.g. | Platform | Install Command | | --------------- | ----------------------------------------------------------------------------------------------------------------------------- | -| Linux aarch64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_aarch64.whl` | -| Linux x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_x86_64.whl` | -| Windows x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-win_amd64.whl` | +| Linux aarch64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.1/warp_lang-1.4.1+cu11-py3-none-manylinux2014_aarch64.whl` | +| Linux x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.1/warp_lang-1.4.1+cu11-py3-none-manylinux2014_x86_64.whl` | +| Windows x86-64 | `pip install https://github.com/NVIDIA/warp/releases/download/v1.4.1/warp_lang-1.4.1+cu11-py3-none-win_amd64.whl` | The `--force-reinstall` option may need to be used to overwrite a previous installation. diff --git a/VERSION.md b/VERSION.md index 88c5fb89..347f5833 100644 --- a/VERSION.md +++ b/VERSION.md @@ -1 +1 @@ -1.4.0 +1.4.1 diff --git a/docs/changelog.md b/docs/changelog.md new file mode 100644 index 00000000..4e68f707 --- /dev/null +++ b/docs/changelog.md @@ -0,0 +1,8 @@ +--- +tocdepth: 2 +--- + + + +```{include} ../CHANGELOG.md +``` diff --git a/docs/codegen.rst b/docs/codegen.rst index fe5ed81b..b4984781 100644 --- a/docs/codegen.rst +++ b/docs/codegen.rst @@ -446,6 +446,153 @@ The above program uses a static expression to select the right function given th [2. 0.] +Advanced Example: Branching Elimination with Static Loop Unrolling +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +In computational simulations, it's common to apply different operations or boundary conditions based on runtime variables. However, conditional branching using runtime variables often leads to performance issues due to register pressure, as the GPU may allocate resources for all branches even if some of them are never taken. To tackle this, we can utilize static loop unrolling via ``wp.static(...)``, which helps eliminate unnecessary branching at compile-time and improve parallel execution. + +**Scenario:** + +Suppose we have three different functions ``apply_func_a``, ``apply_func_b``, and ``apply_func_c`` that perform different mathematical operations. + +We are currently interested in applying only two of these functions (``apply_func_a`` and ``apply_func_b``) on a given dataset. Which function we apply to each data point is determined by a runtime variable ``func_id``, which is provided as an array to the kernel called ``func_field``. + +In practice, ``func_field`` represents a mapping of which operation should be applied to each data point, and is particularly useful when dealing with boundary conditions or different regions of a physical simulation. For example, in a fluid simulation, different regions of the fluid might require different updates based on pre-defined boundary conditions. + +**Naive Approach Implementation** + +To start, let us first consider a naive approach to implement this, which involves straightforward runtime branching based on the value of func_id. This approach will highlight why we need to optimize further. + +.. code:: python + + import warp as wp + import numpy as np + + # Define three functions that perform different operations + @wp.func + def apply_func_a(x: float) -> float: + return x + 10.0 + + @wp.func + def apply_func_b(x: float) -> float: + return x * 2.0 + + @wp.func + def apply_func_c(x: float) -> float: + return x - 5.0 + + # Assign static IDs to represent each function + func_id_a = 0 + func_id_b = 1 + func_id_c = 2 # Not used in this kernel + + # Kernel that applies the correct function to each element of the input array + @wp.kernel + def apply_func_conditions_naive(x: wp.array(dtype=wp.float32), func_field: wp.array(dtype=wp.int8)): + tid = wp.tid() + value = x[tid] + result = value + func_id = func_field[tid] # Get the function ID for this element + + # Apply the corresponding function based on func_id + if func_id == func_id_a: + result = apply_func_a(value) + elif func_id == func_id_b: + result = apply_func_b(value) + elif func_id == func_id_c: + result = apply_func_c(value) + + x[tid] = result + + # Example usage + data = wp.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=wp.float32) + + # Create an array that specifies which function to apply to each element + func_field = wp.array([func_id_a, func_id_b, func_id_b, func_id_a, func_id_b], dtype=wp.int8) + + # Launch the kernel + wp.launch(apply_func_conditions_naive, inputs=[data, func_field], dim=data.size) + + print(data.numpy()) + +**Output:** + +.. code:: python + + [11. 4. 6. 14. 10.] + +Since ``func_id`` is not static, the compiler cannot eliminate the unused function at compile time. Looking at the generated CUDA code, we can see the kernel includes an extra branching for the unused ``apply_func_c``: + +.. code:: cpp + + //... + var_11 = wp::select(var_9, var_4, var_10); + if (!var_9) { + var_13 = (var_7 == var_12); + if (var_13) { + var_14 = apply_func_b_0(var_3); + } + var_15 = wp::select(var_13, var_11, var_14); + if (!var_13) { + var_17 = (var_7 == var_16); + if (var_17) { + var_18 = apply_func_c_0(var_3); + } + var_19 = wp::select(var_17, var_15, var_18); + } + var_20 = wp::select(var_13, var_19, var_15); + } + //... + +**Optimization** + +To avoid the extra branching, we can use the static loop unrolling via ``wp.static(...)`` to effectively "compile out" the unnecessary branches and only keep the operations that are relevant. + +**Implementation:** + +.. code:: python + + funcs = [apply_func_a, apply_func_b, apply_func_c] + + # Assign static IDs to represent each function + func_id_a = 0 + func_id_b = 1 + func_id_c = 2 # Not used in this kernel + + # Define which function IDs are actually used in this kernel + used_func_ids = (func_id_a, func_id_b) + + @wp.kernel + def apply_func_conditions(x: wp.array(dtype=wp.float32), func_field: wp.array(dtype=wp.int8)): + tid = wp.tid() + value = x[tid] + result = value + func_id = func_field[tid] # Get the function ID for this element + + # Unroll the loop over the used function IDs + for i in range(wp.static(len(used_func_ids))): + func_static_id = wp.static(used_func_ids[i]) + if func_id == func_static_id: + result = wp.static(funcs[i])(value) + + x[tid] = result + + +In the generated CUDA code, we can see that the optimized code does not branch for the unused function. + +.. code:: cpp + + //... + var_10 = (var_7 == var_9); + if (var_10) { + var_11 = apply_func_a_1(var_3); + } + var_12 = wp::select(var_10, var_4, var_11); + var_15 = (var_7 == var_14); + if (var_15) { + var_16 = apply_func_b_1(var_3); + } + //... + .. _dynamic_generation: Dynamic Kernel Creation @@ -566,7 +713,6 @@ Output: [ 1. 4. 9. 16. 25.] [ 1. 8. 27. 64. 125.] - Function Closures ~~~~~~~~~~~~~~~~~ diff --git a/docs/conf.py b/docs/conf.py index 400d0c77..f77e02ff 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -34,6 +34,7 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ + "myst_parser", # Parse markdown files "sphinx.ext.autodoc", "sphinx.ext.napoleon", # Convert docstrings to reStructuredText "sphinx.ext.intersphinx", @@ -74,6 +75,11 @@ "github": ("https://github.com/NVIDIA/warp/blob/main/%s", "%s"), } +source_suffix = { + ".rst": "restructuredtext", + ".md": "markdown", +} + def linkcode_resolve(domain, info): """Tries to generate external links to code hosted on the Warp GitHub diff --git a/docs/index.rst b/docs/index.rst index 4338cb9f..135d0871 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -357,6 +357,7 @@ Full Table of Contents limitations modules/contribution_guide faq + changelog .. toctree:: :maxdepth: 2 diff --git a/docs/installation.rst b/docs/installation.rst index b432a326..3e2e6354 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -25,11 +25,11 @@ the ``pip install`` command, e.g. * - Platform - Install Command * - Linux aarch64 - - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_aarch64.whl`` + - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.1/warp_lang-1.4.1+cu11-py3-none-manylinux2014_aarch64.whl`` * - Linux x86-64 - - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-manylinux2014_x86_64.whl`` + - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.1/warp_lang-1.4.1+cu11-py3-none-manylinux2014_x86_64.whl`` * - Windows x86-64 - - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.0/warp_lang-1.4.0+cu11-py3-none-win_amd64.whl`` + - ``pip install https://github.com/NVIDIA/warp/releases/download/v1.4.1/warp_lang-1.4.1+cu11-py3-none-win_amd64.whl`` The ``--force-reinstall`` option may need to be used to overwrite a previous installation. diff --git a/docs/modules/differentiability.rst b/docs/modules/differentiability.rst index 3f1b8243..d3db5c53 100644 --- a/docs/modules/differentiability.rst +++ b/docs/modules/differentiability.rst @@ -778,9 +778,11 @@ In the example above we can see that the array ``c`` does not have its ``require Array Overwrite Tracking ^^^^^^^^^^^^^^^^^^^^^^^^^ -It is a common mistake to inadvertently overwrite an array that participates in the computation graph. For example:: +It is a common mistake to inadvertently overwrite an array that participates in the computation graph. For example: - with tape as wp.Tape(): +.. code-block:: python + + with wp.Tape() as tape: # step 1 wp.launch(compute_forces, dim=n, inputs=[pos0, vel0], outputs=[force]) @@ -791,7 +793,7 @@ It is a common mistake to inadvertently overwrite an array that participates in wp.launch(simulate, dim=n, inputs=[pos1, vel1, force], outputs=[pos2, vel2]) # compute loss - wp.launch(loss, dim=n, inputs=[pos2]) + wp.launch(compute_loss, dim=n, inputs=[pos2], outputs=[loss]) tape.backward(loss) diff --git a/docs/modules/functions.rst b/docs/modules/functions.rst index ca1bea38..7982952d 100644 --- a/docs/modules/functions.rst +++ b/docs/modules/functions.rst @@ -712,7 +712,8 @@ Transformations Apply the transform to a point ``point`` treating the homogeneous coordinate as w=1. The transformation is applied treating ``point`` as a column vector, e.g.: ``y = mat*point``. - Note this is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = point^T*mat^T``. + + This is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = point^T*mat^T``. If the transform is coming from a library that uses row-vectors, then users should transpose the transformation matrix before calling this method. @@ -728,8 +729,9 @@ Transformations Apply the transform to a vector ``vec`` treating the homogeneous coordinate as w=0. - The transformation is applied treating ``vec`` as a column vector, e.g.: ``y = mat*vec`` - note this is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = vec^T*mat^T``. + The transformation is applied treating ``vec`` as a column vector, e.g.: ``y = mat*vec``. + + This is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = vec^T*mat^T``. If the transform is coming from a library that uses row-vectors, then users should transpose the transformation matrix before calling this method. @@ -1291,6 +1293,11 @@ Utility All matrices are assumed to be stored in flattened row-major memory layout (NumPy default). +.. py:function:: reversed(range: range_t) -> range_t + + Returns the range in reversed order. + + .. py:function:: printf(fmt: str, *args: Any) -> None Allows printing formatted strings using C-style format specifiers. @@ -1417,380 +1424,380 @@ Utility Select between two arguments, if ``arr`` is null then return ``value_if_false``, otherwise return ``value_if_true`` -.. py:function:: atomic_add(arr: Array[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: Array[Any], i: Int, value: Any) -> Any Atomically add ``value`` onto ``arr[i]`` and return the old value. -.. py:function:: atomic_add(arr: Array[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: Array[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j]`` and return the old value. -.. py:function:: atomic_add(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j,k]`` and return the old value. -.. py:function:: atomic_add(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j,k,l]`` and return the old value. -.. py:function:: atomic_add(arr: FabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: FabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i]`` and return the old value. -.. py:function:: atomic_add(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j]`` and return the old value. -.. py:function:: atomic_add(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j,k]`` and return the old value. -.. py:function:: atomic_add(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j,k,l]`` and return the old value. -.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i]`` and return the old value. -.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j]`` and return the old value. -.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j,k]`` and return the old value. -.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_add(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically add ``value`` onto ``arr[i,j,k,l]`` and return the old value. -.. py:function:: atomic_sub(arr: Array[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: Array[Any], i: Int, value: Any) -> Any Atomically subtract ``value`` onto ``arr[i]`` and return the old value. -.. py:function:: atomic_sub(arr: Array[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: Array[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j]`` and return the old value. -.. py:function:: atomic_sub(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j,k]`` and return the old value. -.. py:function:: atomic_sub(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j,k,l]`` and return the old value. -.. py:function:: atomic_sub(arr: FabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: FabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i]`` and return the old value. -.. py:function:: atomic_sub(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j]`` and return the old value. -.. py:function:: atomic_sub(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j,k]`` and return the old value. -.. py:function:: atomic_sub(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j,k,l]`` and return the old value. -.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i]`` and return the old value. -.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j]`` and return the old value. -.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j,k]`` and return the old value. -.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_sub(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Atomically subtract ``value`` onto ``arr[i,j,k,l]`` and return the old value. -.. py:function:: atomic_min(arr: Array[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: Array[Any], i: Int, value: Any) -> Any Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: Array[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: FabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: FabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_min(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: Array[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: Array[Any], i: Int, value: Any) -> Any Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: Array[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: FabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: FabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. -.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any +.. py:function:: atomic_max(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any :noindex: :nocontentsentry: Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. .. py:function:: lerp(a: Float, b: Float, t: Float) -> Float @@ -2693,13 +2700,12 @@ Code Generation Evaluates a static Python expression and replaces it with its result. - See the `codegen.html#static-expressions

`_ for more details. + See the :ref:`code generation guide ` for more details. - Note: - The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, - which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. - The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions - (excluding Warp arrays since they cannot be created in a Warp kernel at the moment). + The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, + which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. + The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions + (excluding Warp arrays since they cannot be created in a Warp kernel at the moment). .. rubric:: Footnotes diff --git a/docs/modules/sim.rst b/docs/modules/sim.rst index 973401ad..eebd37ec 100644 --- a/docs/modules/sim.rst +++ b/docs/modules/sim.rst @@ -1,3 +1,5 @@ +:tocdepth: 3 + warp.sim ======== diff --git a/docs/requirements.txt b/docs/requirements.txt index b8b6bd59..c8626adc 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -3,3 +3,4 @@ sphinx==8.0.2 sphinx_copybutton==0.5.2 numpy==2.1.1 ruff==0.6.8 +myst-parser==4.0.0 diff --git a/exts/omni.warp.core/config/extension.toml b/exts/omni.warp.core/config/extension.toml index 841caf50..04df653c 100644 --- a/exts/omni.warp.core/config/extension.toml +++ b/exts/omni.warp.core/config/extension.toml @@ -1,6 +1,6 @@ [package] # Semantic Versioning is used: https://semver.org/ -version = "1.4.0" +version = "1.4.1" authors = ["NVIDIA"] title = "Warp Core" description="The core Warp Python module" diff --git a/exts/omni.warp.core/docs/CHANGELOG.md b/exts/omni.warp.core/docs/CHANGELOG.md index 82fb2e73..94d42d6e 100644 --- a/exts/omni.warp.core/docs/CHANGELOG.md +++ b/exts/omni.warp.core/docs/CHANGELOG.md @@ -1,5 +1,17 @@ # CHANGELOG +## [1.4.1] - 2024-10-15 + +### Fixed + +- Fix `iter_reverse()` not working as expected for ranges with steps other than 1 ([GH-311](https://github.com/NVIDIA/warp/issues/311)). +- Fix potential out-of-bounds memory access when a `wp.sparse.BsrMatrix` object is reused for storing matrices of different shapes. +- Fix robustness to very low desired tolerance in `wp.fem.utils.symmetric_eigenvalues_qr`. +- Fix invalid code generation error messages when nesting dynamic and static for-loops. +- Fix caching of kernels with static expressions. +- Fix `ModelBuilder.add_builder(builder)` to correctly update `articulation_start` and thereby `articulation_count` when `builder` contains more than one articulation. +- Re-introduced the `wp.rand*()`, `wp.sample*()`, and `wp.poisson()` onto the Python scope to revert a breaking change. + ## [1.4.0] - 2024-10-01 ### Added @@ -72,15 +84,14 @@ - Bug fixes - Fix an aliasing issue with zero-copy array initialization from NumPy introduced in Warp 1.3.0. - - Fix `wp.Volume.load_from_numpy()` behavior when `bg_value` is a sequence of values. + - Fix `wp.Volume.load_from_numpy()` behavior when `bg_value` is a sequence of values ([GH-312](https://github.com/NVIDIA/warp/pull/312)). ## [1.3.2] - 2024-08-30 - Bug fixes - Fix accuracy of 3x3 SVD ``wp.svd3`` with fp64 numbers ([GH-281](https://github.com/NVIDIA/warp/issues/281)). - Fix module hashing when a kernel argument contained a struct array ([GH-287](https://github.com/NVIDIA/warp/issues/287)). - - Fix a bug in `wp.bvh_query_ray()` where the direction instead of the reciprocal direction was used - ([GH-288](https://github.com/NVIDIA/warp/issues/288)). + - Fix a bug in `wp.bvh_query_ray()` where the direction instead of the reciprocal direction was used ([GH-288](https://github.com/NVIDIA/warp/issues/288)). - Fix errors when launching a CUDA graph after a module is reloaded. Modules that were used during graph capture will no longer be unloaded before the graph is released. - Fix a bug in `wp.sim.collide.triangle_closest_point_barycentric()` where the returned barycentric coordinates may be @@ -205,7 +216,7 @@ - Fix for handling of `bool` types in generic kernels - Publish CUDA 12.5 binaries for Hopper support, see https://github.com/nvidia/warp?tab=readme-ov-file#installing for details -## [1.1.1] - 2024-05-24 +## 1.1.1 - 2024-05-24 - `wp.init()` is no longer required to be called explicitly and will be performed on first call to the API - Speed up `omni.warp.core`'s startup time @@ -240,7 +251,7 @@ - Support gradient propagation for indexing sliced multi-dimensional arrays, i.e. `a[i][j]` vs. `a[i, j]` - Provide an informative message if setting DLL C-types failed, instructing to try rebuilding the library -## [1.0.3] - 2024-04-17 +## 1.0.3 - 2024-04-17 - Add a `support_level` entry to the configuration file of the extensions @@ -318,7 +329,7 @@ - Added `wp.ones()` to efficiently create one-initialized arrays - Rename `wp.config.graph_capture_module_load_default` to `wp.config.enable_graph_capture_module_load_by_default` -## [0.14.0] - 2024-02-19 +## 0.14.0 - 2024-02-19 - Add support for CUDA pooled (stream-ordered) allocators - Support memory allocation during graph capture @@ -355,7 +366,7 @@ - Fixed a small CPU memory leak related to DLPack interop - Improved performance of creating arrays -## [0.13.1] - 2024-02-22 +## 0.13.1 - 2024-02-22 - Ensure that the results from the `Noise Deform` are deterministic across different Kit sessions @@ -368,7 +379,7 @@ - Add missing `.py` extension to `warp/tests/walkthrough_debug` - Allow `wp.bool` usage in vector and matrix types -## [0.12.0] - 2024-02-05 +## 0.12.0 - 2024-02-05 - Add a warning when the `enable_backward` setting is set to `False` upon calling `wp.Tape.backward()` - Fix kernels not being recompiled as expected when defined using a closure @@ -384,7 +395,7 @@ - Point releases (if any) go on the same minor release branch and only contain bug fixes, not new features. - The `public` branch, previously used to merge releases into and corresponding with the GitHub `main` branch, is retired. -## [1.0.0-beta.7] - 2024-01-23 +## 1.0.0-beta.7 - 2024-01-23 - Ensure captures are always enclosed in `try`/`finally` - Only include .py files from the warp subdirectory into wheel packages @@ -446,7 +457,7 @@ - Documentation update for `wp.BVH` - Documentation and simplified API for runtime kernel specialization `wp.Kernel` -## [1.0.0-beta.4] - 2023-11-01 +## 1.0.0-beta.4 - 2023-11-01 - Add `wp.cbrt()` for cube root calculation - Add `wp.mesh_furthest_point_no_sign()` to compute furthest point on a surface from a query point @@ -458,7 +469,7 @@ - Fix for `wp.utils.array_sum()` output initialization when used with vector types - Coverage and documentation updates -## [1.0.0-beta.3] - 2023-10-19 +## 1.0.0-beta.3 - 2023-10-19 - Add support for code coverage scans (test_coverage.py), coverage at 85% in `omni.warp.core` - Add support for named component access for vector types, e.g.: `a = v.x` @@ -480,13 +491,13 @@ - To support grid-stride kernels, `wp.tid()` can no longer be called inside `wp.func` functions. -## [1.0.0-beta.2] - 2023-09-01 +## 1.0.0-beta.2 - 2023-09-01 - Fix for passing bool into `wp.func` functions - Fix for deprecation warnings appearing on `stderr`, now redirected to `stdout` - Fix for using `for i in wp.hash_grid_query(..)` syntax -## [1.0.0-beta.1] - 2023-08-29 +## 1.0.0-beta.1 - 2023-08-29 - Fix for `wp.float16` being passed as kernel arguments - Fix for compile errors with kernels using structs in backward pass @@ -525,7 +536,7 @@ - Update margin used by for mesh queries when using `wp.sim.create_soft_body_contacts()` - Improvements to gradient handling with `wp.from_torch()`, `wp.to_torch()` plus documentation -## [0.10.0] - 2023-07-05 +## 0.10.0 - 2023-07-05 - Add support for macOS universal binaries (x86 + aarch64) for M1+ support - Add additional methods for SDF generation please see the following new methods: @@ -601,7 +612,7 @@ - Deprecate `wp.Model.soft_contact_distance` which is now replaced by `wp.Model.particle_radius` - Deprecate single scalar particle radius (should be a per-particle array) -## [0.8.2] - 2023-04-21 +## 0.8.2 - 2023-04-21 - Add `ModelBuilder.soft_contact_max` to control the maximum number of soft contacts that can be registered. Use `Model.allocate_soft_contacts(new_count)` to change count on existing `Model` objects. - Add support for `bool` parameters @@ -612,12 +623,12 @@ - Add sign determination using winding number of `wp.mesh_query_point()` as `wp.mesh_query_sign_winding_number()` - Add query point without sign determination `wp.mesh_query_no_sign()` -## [0.8.1] - 2023-04-13 +## 0.8.1 - 2023-04-13 - Fix for regression when passing flattened numeric lists as matrix arguments to kernels - Fix for regressions when passing `wp.struct` types with uninitialized (`None`) member attributes -## [0.8.0] - 2023-04-05 +## 0.8.0 - 2023-04-05 - Add `Texture Write` node for updating dynamic RTX textures from Warp kernels / nodes - Add multi-dimensional kernel support to Warp Kernel Node @@ -661,14 +672,14 @@ - `wp.sim.model.ground_plane` is now a `wp.array` to support gradient, users should call `builder.set_ground_plane()` to create the ground - `wp.sim` capsule, cones, and cylinders are now aligned with the default USD up-axis -## [0.7.2] - 2023-02-15 +## 0.7.2 - 2023-02-15 - Reduce test time for vec/math types - Clean-up CUDA disabled build pipeline - Remove extension.gen.toml to make Kit packages Python version independent - Handle additional cases for array indexing inside Python -## [0.7.1] - 2023-02-14 +## 0.7.1 - 2023-02-14 - Disabling some slow tests for Kit - Make unit tests run on first GPU only by default @@ -685,13 +696,13 @@ - Add security pop-up for Kernel Node - Improve error handling for kernel return values -## [0.6.3] - 2023-01-31 +## 0.6.3 - 2023-01-31 - Add DLPack utilities, see `wp.from_dlpack()`, `wp.to_dlpack()` - Add Jax utilities, see `wp.from_jax()`, `wp.to_jax()`, `wp.device_from_jax()`, `wp.device_to_jax()` - Fix for Linux Kit extensions OM-80132, OM-80133 -## [0.6.2] - 2023-01-19 +## 0.6.2 - 2023-01-19 - Updated `wp.from_torch()` to support more data types - Updated `wp.from_torch()` to automatically determine the target Warp data type if not specified @@ -706,14 +717,14 @@ - Replace Python `imp` package with `importlib` - Fix for quaternion slerp gradients (`wp.quat_slerp()`) -## [0.6.1] - 2022-12-05 +## 0.6.1 - 2022-12-05 - Fix for non-CUDA builds - Fix strides computation in array_t constructor, fixes a bug with accessing mesh indices through mesh.indices[] - Disable backward pass code generation for kernel node (4-6x faster compilation) - Switch to linbuild for universal Linux binaries (affects TeamCity builds only) -## [0.6.0] - 2022-11-28 +## 0.6.0 - 2022-11-28 - Add support for CUDA streams, see `wp.Stream`, `wp.get_stream()`, `wp.set_stream()`, `wp.synchronize_stream()`, `wp.ScopedStream` - Add support for CUDA events, see `wp.Event`, `wp.record_event()`, `wp.wait_event()`, `wp.wait_stream()`, `wp.Stream.record_event()`, `wp.Stream.wait_event()`, `wp.Stream.wait_stream()` @@ -738,7 +749,7 @@ - Fix various deployment issues by statically linking with all CUDA libs - Update warp.so/warp.dll to CUDA Toolkit 11.5 -## [0.5.1] - 2022-11-01 +## 0.5.1 - 2022-11-01 - Fix for unit tests in Kit @@ -775,14 +786,14 @@ - Fix for arrays > 2GB in length - Add support for per-vertex USD mesh colors with `wp.render` class -## [0.4.2] - 2022-09-07 +## 0.4.2 - 2022-09-07 - Register Warp samples to the sample browser in Kit - Add NDEBUG flag to release mode kernel builds - Fix for particle solver node when using a large number of particles - Fix for broken cameras in Warp sample scenes -## [0.4.1] - 2022-08-30 +## 0.4.1 - 2022-08-30 - Add geometry sampling methods, see `wp.sample_unit_cube()`, `wp.sample_unit_disk()`, etc - Add `wp.lower_bound()` for searching sorted arrays @@ -792,7 +803,7 @@ - Fix for debug flags not being set correctly on CUDA when `wp.config.mode == "debug"`, this enables bounds checking on CUDA kernels in debug mode - Fix for code gen of functions that do not return a value -## [0.4.0] - 2022-08-09 +## 0.4.0 - 2022-08-09 - Fix for FP16 conversions on GPUs without hardware support - Fix for `runtime = None` errors when reloading the Warp module @@ -809,7 +820,7 @@ - Removed `wp.runtime` reference from the top-level module, as it should be considered private -## [0.3.2] - 2022-07-19 +## 0.3.2 - 2022-07-19 - Remove Torch import from `__init__.py`, defer import to `wp.from_torch()`, `wp.to_torch()` @@ -831,7 +842,7 @@ - `wp.synchronize()` now synchronizes all devices; for finer-grained control, use `wp.synchronize_device()` - Device alias `"cuda"` now refers to the current CUDA context, rather than a specific device like `"cuda:0"` or `"cuda:1"` -## [0.3.0] - 2022-07-08 +## 0.3.0 - 2022-07-08 - Add support for FP16 storage type, see `wp.float16` - Add support for per-dimension byte strides, see `wp.array.strides` @@ -868,7 +879,7 @@ - Tape `capture` option has been removed, users can now capture tapes inside existing CUDA graphs (e.g.: inside Torch) - Scalar loss arrays should now explicitly set `requires_grad=True` at creation time -## [0.2.2] - 2022-05-30 +## 0.2.2 - 2022-05-30 - Fix for `from import *` inside Warp initialization - Fix for body space velocity when using deforming Mesh objects with scale @@ -892,7 +903,7 @@ - Local `@wp.func` functions should not be namespaced when called, e.g.: previously `wp.myfunc()` would work even if `myfunc()` was not a builtin - Removed `wp.rpy2quat()`, please use `wp.quat_rpy()` instead -## [0.2.1] - 2022-05-11 +## 0.2.1 - 2022-05-11 - Fix for unit tests in Kit @@ -941,7 +952,7 @@ - `wp.array.length` member has been removed, please use `wp.array.shape` to access array dimensions, or use `wp.array.size` to get total element count - Marking `dense_gemm()`, `dense_chol()`, etc methods as experimental until we revisit them -## [0.1.25] - 2022-03-20 +## 0.1.25 - 2022-03-20 - Add support for class methods to be Warp kernels - Add HashGrid reserve() so it can be used with CUDA graphs @@ -951,7 +962,7 @@ - Add support for floored division on integer types - Move tests into core library so they can be run in Kit environment -## [0.1.24] - 2022-03-03 +## 0.1.24 - 2022-03-03 ### Warp Core @@ -967,7 +978,7 @@ - Fix for ranged for loops with negative step sizes - Fix for 3d and 4d spherical gradient distributions -## [0.1.23] - 2022-02-17 +## 0.1.23 - 2022-02-17 ### Warp Core @@ -977,7 +988,7 @@ - Add procedural noise primitives, see `wp.noise()`, `wp.pnoise()`, `wp.curlnoise()` - Move simulation helpers our of test into `wp.sim` module -## [0.1.22] - 2022-02-14 +## 0.1.22 - 2022-02-14 ### Warp Core @@ -991,7 +1002,7 @@ - Add support for universal and compound joint types -## [0.1.21] - 2022-01-19 +## 0.1.21 - 2022-01-19 ### Warp Core @@ -1011,19 +1022,19 @@ - New OgnParticleVolume node for sampling shapes -> particles - New OgnParticleSolver node for DEM style granular materials -## [0.1.20] - 2021-11-02 +## 0.1.20 - 2021-11-02 - Updates to the ripple solver for GTC (support for multiple colliders, buoyancy, etc) -## [0.1.19] - 2021-10-15 +## 0.1.19 - 2021-10-15 - Publish from 2021.3 to avoid omni.graph database incompatibilities -## [0.1.18] - 2021-10-08 +## 0.1.18 - 2021-10-08 - Enable Linux support (tested on 20.04) -## [0.1.17] - 2021-09-30 +## 0.1.17 - 2021-09-30 - Fix for 3x3 SVD adjoint - Fix for A6000 GPU (bump compute model to sm_52 minimum) @@ -1032,12 +1043,12 @@ - Rename spatial_transform -> transform - Documentation update -## [0.1.16] - 2021-09-06 +## 0.1.16 - 2021-09-06 - Fix for case where simple assignments (a = b) incorrectly generated reference rather than value copy - Handle passing zero-length (empty) arrays to kernels -## [0.1.15] - 2021-09-03 +## 0.1.15 - 2021-09-03 - Add additional math library functions (asin, etc) - Add builtin 3x3 SVD support @@ -1050,62 +1061,62 @@ - Removes the need to transfer array to CPU before numpy conversion (will be done implicitly) - Update the example OgnRipple wave equation solver to use bundles -## [0.1.14] - 2021-08-09 +## 0.1.14 - 2021-08-09 - Fix for out-of-bounds memory access in CUDA BVH - Better error checking after kernel launches (use `wp.config.verify_cuda=True`) - Fix for vec3 normalize adjoint code -## [0.1.13] - 2021-07-29 +## 0.1.13 - 2021-07-29 - Remove OgnShrinkWrap.py test node -## [0.1.12] - 2021-07-29 +## 0.1.12 - 2021-07-29 - Switch to Woop et al.'s watertight ray-tri intersection test - Disable --fast-math in CUDA compilation step for improved precision -## [0.1.11] - 2021-07-28 +## 0.1.11 - 2021-07-28 - Fix for `wp.mesh_query_ray()` returning incorrect t-value -## [0.1.10] - 2021-07-28 +## 0.1.10 - 2021-07-28 - Fix for OV extension fwatcher filters to avoid hot-reload loop due to OGN regeneration -## [0.1.9] - 2021-07-21 +## 0.1.9 - 2021-07-21 - Fix for loading sibling DLL paths - Better type checking for built-in function arguments - Added runtime docs, can now list all builtins using `wp.print_builtins()` -## [0.1.8] - 2021-07-14 +## 0.1.8 - 2021-07-14 - Fix for hot-reload of CUDA kernels - Add Tape object for replaying differentiable kernels - Add helpers for Torch interop (convert `torch.Tensor` to `wp.Array`) -## [0.1.7] - 2021-07-05 +## 0.1.7 - 2021-07-05 - Switch to NVRTC for CUDA runtime - Allow running without host compiler - Disable asserts in kernel release mode (small perf. improvement) -## [0.1.6] - 2021-06-14 +## 0.1.6 - 2021-06-14 - Look for CUDA toolchain in target-deps -## [0.1.5] - 2021-06-14 +## 0.1.5 - 2021-06-14 - Rename OgLang -> Warp - Improve CUDA environment error checking - Clean-up some logging, add verbose mode (`wp.config.verbose`) -## [0.1.4] - 2021-06-10 +## 0.1.4 - 2021-06-10 - Add support for mesh raycast -## [0.1.3] - 2021-06-09 +## 0.1.3 - 2021-06-09 - Add support for unary negation operator - Add support for mutating variables during dynamic loops (non-differentiable) @@ -1113,7 +1124,7 @@ - Improve kernel cache start up times (avoids adjointing before cache check) - Update README.md with requirements / examples -## [0.1.2] - 2021-06-03 +## 0.1.2 - 2021-06-03 - Add support for querying mesh velocities - Add CUDA graph support, see `wp.capture_begin()`, `wp.capture_end()`, `wp.capture_launch()` @@ -1123,10 +1134,10 @@ - Fix for Linux/macOS support -## [0.1.1] - 2021-05-18 +## 0.1.1 - 2021-05-18 - Fix bug with conflicting CUDA contexts -## [0.1.0] - 2021-05-17 +## 0.1.0 - 2021-05-17 - Initial publish for alpha testing diff --git a/exts/omni.warp/config/extension.toml b/exts/omni.warp/config/extension.toml index cfebd3b6..46985a75 100644 --- a/exts/omni.warp/config/extension.toml +++ b/exts/omni.warp/config/extension.toml @@ -1,6 +1,6 @@ [package] # Semantic Versioning is used: https://semver.org/ -version = "1.4.0" +version = "1.4.1" authors = ["NVIDIA"] title = "Warp" description="Warp OmniGraph Nodes and Sample Scenes" @@ -35,7 +35,7 @@ exclude = ["Ogn*Database.py", "*/ogn*"] "omni.timeline" = {} "omni.ui" = {optional = true} "omni.usd" = {} -"omni.warp.core" = {version = "1.4.0", exact = true} +"omni.warp.core" = {version = "1.4.1", exact = true} [[python.module]] name = "omni.warp._extension" diff --git a/exts/omni.warp/docs/CHANGELOG.md b/exts/omni.warp/docs/CHANGELOG.md index 82fb2e73..94d42d6e 100644 --- a/exts/omni.warp/docs/CHANGELOG.md +++ b/exts/omni.warp/docs/CHANGELOG.md @@ -1,5 +1,17 @@ # CHANGELOG +## [1.4.1] - 2024-10-15 + +### Fixed + +- Fix `iter_reverse()` not working as expected for ranges with steps other than 1 ([GH-311](https://github.com/NVIDIA/warp/issues/311)). +- Fix potential out-of-bounds memory access when a `wp.sparse.BsrMatrix` object is reused for storing matrices of different shapes. +- Fix robustness to very low desired tolerance in `wp.fem.utils.symmetric_eigenvalues_qr`. +- Fix invalid code generation error messages when nesting dynamic and static for-loops. +- Fix caching of kernels with static expressions. +- Fix `ModelBuilder.add_builder(builder)` to correctly update `articulation_start` and thereby `articulation_count` when `builder` contains more than one articulation. +- Re-introduced the `wp.rand*()`, `wp.sample*()`, and `wp.poisson()` onto the Python scope to revert a breaking change. + ## [1.4.0] - 2024-10-01 ### Added @@ -72,15 +84,14 @@ - Bug fixes - Fix an aliasing issue with zero-copy array initialization from NumPy introduced in Warp 1.3.0. - - Fix `wp.Volume.load_from_numpy()` behavior when `bg_value` is a sequence of values. + - Fix `wp.Volume.load_from_numpy()` behavior when `bg_value` is a sequence of values ([GH-312](https://github.com/NVIDIA/warp/pull/312)). ## [1.3.2] - 2024-08-30 - Bug fixes - Fix accuracy of 3x3 SVD ``wp.svd3`` with fp64 numbers ([GH-281](https://github.com/NVIDIA/warp/issues/281)). - Fix module hashing when a kernel argument contained a struct array ([GH-287](https://github.com/NVIDIA/warp/issues/287)). - - Fix a bug in `wp.bvh_query_ray()` where the direction instead of the reciprocal direction was used - ([GH-288](https://github.com/NVIDIA/warp/issues/288)). + - Fix a bug in `wp.bvh_query_ray()` where the direction instead of the reciprocal direction was used ([GH-288](https://github.com/NVIDIA/warp/issues/288)). - Fix errors when launching a CUDA graph after a module is reloaded. Modules that were used during graph capture will no longer be unloaded before the graph is released. - Fix a bug in `wp.sim.collide.triangle_closest_point_barycentric()` where the returned barycentric coordinates may be @@ -205,7 +216,7 @@ - Fix for handling of `bool` types in generic kernels - Publish CUDA 12.5 binaries for Hopper support, see https://github.com/nvidia/warp?tab=readme-ov-file#installing for details -## [1.1.1] - 2024-05-24 +## 1.1.1 - 2024-05-24 - `wp.init()` is no longer required to be called explicitly and will be performed on first call to the API - Speed up `omni.warp.core`'s startup time @@ -240,7 +251,7 @@ - Support gradient propagation for indexing sliced multi-dimensional arrays, i.e. `a[i][j]` vs. `a[i, j]` - Provide an informative message if setting DLL C-types failed, instructing to try rebuilding the library -## [1.0.3] - 2024-04-17 +## 1.0.3 - 2024-04-17 - Add a `support_level` entry to the configuration file of the extensions @@ -318,7 +329,7 @@ - Added `wp.ones()` to efficiently create one-initialized arrays - Rename `wp.config.graph_capture_module_load_default` to `wp.config.enable_graph_capture_module_load_by_default` -## [0.14.0] - 2024-02-19 +## 0.14.0 - 2024-02-19 - Add support for CUDA pooled (stream-ordered) allocators - Support memory allocation during graph capture @@ -355,7 +366,7 @@ - Fixed a small CPU memory leak related to DLPack interop - Improved performance of creating arrays -## [0.13.1] - 2024-02-22 +## 0.13.1 - 2024-02-22 - Ensure that the results from the `Noise Deform` are deterministic across different Kit sessions @@ -368,7 +379,7 @@ - Add missing `.py` extension to `warp/tests/walkthrough_debug` - Allow `wp.bool` usage in vector and matrix types -## [0.12.0] - 2024-02-05 +## 0.12.0 - 2024-02-05 - Add a warning when the `enable_backward` setting is set to `False` upon calling `wp.Tape.backward()` - Fix kernels not being recompiled as expected when defined using a closure @@ -384,7 +395,7 @@ - Point releases (if any) go on the same minor release branch and only contain bug fixes, not new features. - The `public` branch, previously used to merge releases into and corresponding with the GitHub `main` branch, is retired. -## [1.0.0-beta.7] - 2024-01-23 +## 1.0.0-beta.7 - 2024-01-23 - Ensure captures are always enclosed in `try`/`finally` - Only include .py files from the warp subdirectory into wheel packages @@ -446,7 +457,7 @@ - Documentation update for `wp.BVH` - Documentation and simplified API for runtime kernel specialization `wp.Kernel` -## [1.0.0-beta.4] - 2023-11-01 +## 1.0.0-beta.4 - 2023-11-01 - Add `wp.cbrt()` for cube root calculation - Add `wp.mesh_furthest_point_no_sign()` to compute furthest point on a surface from a query point @@ -458,7 +469,7 @@ - Fix for `wp.utils.array_sum()` output initialization when used with vector types - Coverage and documentation updates -## [1.0.0-beta.3] - 2023-10-19 +## 1.0.0-beta.3 - 2023-10-19 - Add support for code coverage scans (test_coverage.py), coverage at 85% in `omni.warp.core` - Add support for named component access for vector types, e.g.: `a = v.x` @@ -480,13 +491,13 @@ - To support grid-stride kernels, `wp.tid()` can no longer be called inside `wp.func` functions. -## [1.0.0-beta.2] - 2023-09-01 +## 1.0.0-beta.2 - 2023-09-01 - Fix for passing bool into `wp.func` functions - Fix for deprecation warnings appearing on `stderr`, now redirected to `stdout` - Fix for using `for i in wp.hash_grid_query(..)` syntax -## [1.0.0-beta.1] - 2023-08-29 +## 1.0.0-beta.1 - 2023-08-29 - Fix for `wp.float16` being passed as kernel arguments - Fix for compile errors with kernels using structs in backward pass @@ -525,7 +536,7 @@ - Update margin used by for mesh queries when using `wp.sim.create_soft_body_contacts()` - Improvements to gradient handling with `wp.from_torch()`, `wp.to_torch()` plus documentation -## [0.10.0] - 2023-07-05 +## 0.10.0 - 2023-07-05 - Add support for macOS universal binaries (x86 + aarch64) for M1+ support - Add additional methods for SDF generation please see the following new methods: @@ -601,7 +612,7 @@ - Deprecate `wp.Model.soft_contact_distance` which is now replaced by `wp.Model.particle_radius` - Deprecate single scalar particle radius (should be a per-particle array) -## [0.8.2] - 2023-04-21 +## 0.8.2 - 2023-04-21 - Add `ModelBuilder.soft_contact_max` to control the maximum number of soft contacts that can be registered. Use `Model.allocate_soft_contacts(new_count)` to change count on existing `Model` objects. - Add support for `bool` parameters @@ -612,12 +623,12 @@ - Add sign determination using winding number of `wp.mesh_query_point()` as `wp.mesh_query_sign_winding_number()` - Add query point without sign determination `wp.mesh_query_no_sign()` -## [0.8.1] - 2023-04-13 +## 0.8.1 - 2023-04-13 - Fix for regression when passing flattened numeric lists as matrix arguments to kernels - Fix for regressions when passing `wp.struct` types with uninitialized (`None`) member attributes -## [0.8.0] - 2023-04-05 +## 0.8.0 - 2023-04-05 - Add `Texture Write` node for updating dynamic RTX textures from Warp kernels / nodes - Add multi-dimensional kernel support to Warp Kernel Node @@ -661,14 +672,14 @@ - `wp.sim.model.ground_plane` is now a `wp.array` to support gradient, users should call `builder.set_ground_plane()` to create the ground - `wp.sim` capsule, cones, and cylinders are now aligned with the default USD up-axis -## [0.7.2] - 2023-02-15 +## 0.7.2 - 2023-02-15 - Reduce test time for vec/math types - Clean-up CUDA disabled build pipeline - Remove extension.gen.toml to make Kit packages Python version independent - Handle additional cases for array indexing inside Python -## [0.7.1] - 2023-02-14 +## 0.7.1 - 2023-02-14 - Disabling some slow tests for Kit - Make unit tests run on first GPU only by default @@ -685,13 +696,13 @@ - Add security pop-up for Kernel Node - Improve error handling for kernel return values -## [0.6.3] - 2023-01-31 +## 0.6.3 - 2023-01-31 - Add DLPack utilities, see `wp.from_dlpack()`, `wp.to_dlpack()` - Add Jax utilities, see `wp.from_jax()`, `wp.to_jax()`, `wp.device_from_jax()`, `wp.device_to_jax()` - Fix for Linux Kit extensions OM-80132, OM-80133 -## [0.6.2] - 2023-01-19 +## 0.6.2 - 2023-01-19 - Updated `wp.from_torch()` to support more data types - Updated `wp.from_torch()` to automatically determine the target Warp data type if not specified @@ -706,14 +717,14 @@ - Replace Python `imp` package with `importlib` - Fix for quaternion slerp gradients (`wp.quat_slerp()`) -## [0.6.1] - 2022-12-05 +## 0.6.1 - 2022-12-05 - Fix for non-CUDA builds - Fix strides computation in array_t constructor, fixes a bug with accessing mesh indices through mesh.indices[] - Disable backward pass code generation for kernel node (4-6x faster compilation) - Switch to linbuild for universal Linux binaries (affects TeamCity builds only) -## [0.6.0] - 2022-11-28 +## 0.6.0 - 2022-11-28 - Add support for CUDA streams, see `wp.Stream`, `wp.get_stream()`, `wp.set_stream()`, `wp.synchronize_stream()`, `wp.ScopedStream` - Add support for CUDA events, see `wp.Event`, `wp.record_event()`, `wp.wait_event()`, `wp.wait_stream()`, `wp.Stream.record_event()`, `wp.Stream.wait_event()`, `wp.Stream.wait_stream()` @@ -738,7 +749,7 @@ - Fix various deployment issues by statically linking with all CUDA libs - Update warp.so/warp.dll to CUDA Toolkit 11.5 -## [0.5.1] - 2022-11-01 +## 0.5.1 - 2022-11-01 - Fix for unit tests in Kit @@ -775,14 +786,14 @@ - Fix for arrays > 2GB in length - Add support for per-vertex USD mesh colors with `wp.render` class -## [0.4.2] - 2022-09-07 +## 0.4.2 - 2022-09-07 - Register Warp samples to the sample browser in Kit - Add NDEBUG flag to release mode kernel builds - Fix for particle solver node when using a large number of particles - Fix for broken cameras in Warp sample scenes -## [0.4.1] - 2022-08-30 +## 0.4.1 - 2022-08-30 - Add geometry sampling methods, see `wp.sample_unit_cube()`, `wp.sample_unit_disk()`, etc - Add `wp.lower_bound()` for searching sorted arrays @@ -792,7 +803,7 @@ - Fix for debug flags not being set correctly on CUDA when `wp.config.mode == "debug"`, this enables bounds checking on CUDA kernels in debug mode - Fix for code gen of functions that do not return a value -## [0.4.0] - 2022-08-09 +## 0.4.0 - 2022-08-09 - Fix for FP16 conversions on GPUs without hardware support - Fix for `runtime = None` errors when reloading the Warp module @@ -809,7 +820,7 @@ - Removed `wp.runtime` reference from the top-level module, as it should be considered private -## [0.3.2] - 2022-07-19 +## 0.3.2 - 2022-07-19 - Remove Torch import from `__init__.py`, defer import to `wp.from_torch()`, `wp.to_torch()` @@ -831,7 +842,7 @@ - `wp.synchronize()` now synchronizes all devices; for finer-grained control, use `wp.synchronize_device()` - Device alias `"cuda"` now refers to the current CUDA context, rather than a specific device like `"cuda:0"` or `"cuda:1"` -## [0.3.0] - 2022-07-08 +## 0.3.0 - 2022-07-08 - Add support for FP16 storage type, see `wp.float16` - Add support for per-dimension byte strides, see `wp.array.strides` @@ -868,7 +879,7 @@ - Tape `capture` option has been removed, users can now capture tapes inside existing CUDA graphs (e.g.: inside Torch) - Scalar loss arrays should now explicitly set `requires_grad=True` at creation time -## [0.2.2] - 2022-05-30 +## 0.2.2 - 2022-05-30 - Fix for `from import *` inside Warp initialization - Fix for body space velocity when using deforming Mesh objects with scale @@ -892,7 +903,7 @@ - Local `@wp.func` functions should not be namespaced when called, e.g.: previously `wp.myfunc()` would work even if `myfunc()` was not a builtin - Removed `wp.rpy2quat()`, please use `wp.quat_rpy()` instead -## [0.2.1] - 2022-05-11 +## 0.2.1 - 2022-05-11 - Fix for unit tests in Kit @@ -941,7 +952,7 @@ - `wp.array.length` member has been removed, please use `wp.array.shape` to access array dimensions, or use `wp.array.size` to get total element count - Marking `dense_gemm()`, `dense_chol()`, etc methods as experimental until we revisit them -## [0.1.25] - 2022-03-20 +## 0.1.25 - 2022-03-20 - Add support for class methods to be Warp kernels - Add HashGrid reserve() so it can be used with CUDA graphs @@ -951,7 +962,7 @@ - Add support for floored division on integer types - Move tests into core library so they can be run in Kit environment -## [0.1.24] - 2022-03-03 +## 0.1.24 - 2022-03-03 ### Warp Core @@ -967,7 +978,7 @@ - Fix for ranged for loops with negative step sizes - Fix for 3d and 4d spherical gradient distributions -## [0.1.23] - 2022-02-17 +## 0.1.23 - 2022-02-17 ### Warp Core @@ -977,7 +988,7 @@ - Add procedural noise primitives, see `wp.noise()`, `wp.pnoise()`, `wp.curlnoise()` - Move simulation helpers our of test into `wp.sim` module -## [0.1.22] - 2022-02-14 +## 0.1.22 - 2022-02-14 ### Warp Core @@ -991,7 +1002,7 @@ - Add support for universal and compound joint types -## [0.1.21] - 2022-01-19 +## 0.1.21 - 2022-01-19 ### Warp Core @@ -1011,19 +1022,19 @@ - New OgnParticleVolume node for sampling shapes -> particles - New OgnParticleSolver node for DEM style granular materials -## [0.1.20] - 2021-11-02 +## 0.1.20 - 2021-11-02 - Updates to the ripple solver for GTC (support for multiple colliders, buoyancy, etc) -## [0.1.19] - 2021-10-15 +## 0.1.19 - 2021-10-15 - Publish from 2021.3 to avoid omni.graph database incompatibilities -## [0.1.18] - 2021-10-08 +## 0.1.18 - 2021-10-08 - Enable Linux support (tested on 20.04) -## [0.1.17] - 2021-09-30 +## 0.1.17 - 2021-09-30 - Fix for 3x3 SVD adjoint - Fix for A6000 GPU (bump compute model to sm_52 minimum) @@ -1032,12 +1043,12 @@ - Rename spatial_transform -> transform - Documentation update -## [0.1.16] - 2021-09-06 +## 0.1.16 - 2021-09-06 - Fix for case where simple assignments (a = b) incorrectly generated reference rather than value copy - Handle passing zero-length (empty) arrays to kernels -## [0.1.15] - 2021-09-03 +## 0.1.15 - 2021-09-03 - Add additional math library functions (asin, etc) - Add builtin 3x3 SVD support @@ -1050,62 +1061,62 @@ - Removes the need to transfer array to CPU before numpy conversion (will be done implicitly) - Update the example OgnRipple wave equation solver to use bundles -## [0.1.14] - 2021-08-09 +## 0.1.14 - 2021-08-09 - Fix for out-of-bounds memory access in CUDA BVH - Better error checking after kernel launches (use `wp.config.verify_cuda=True`) - Fix for vec3 normalize adjoint code -## [0.1.13] - 2021-07-29 +## 0.1.13 - 2021-07-29 - Remove OgnShrinkWrap.py test node -## [0.1.12] - 2021-07-29 +## 0.1.12 - 2021-07-29 - Switch to Woop et al.'s watertight ray-tri intersection test - Disable --fast-math in CUDA compilation step for improved precision -## [0.1.11] - 2021-07-28 +## 0.1.11 - 2021-07-28 - Fix for `wp.mesh_query_ray()` returning incorrect t-value -## [0.1.10] - 2021-07-28 +## 0.1.10 - 2021-07-28 - Fix for OV extension fwatcher filters to avoid hot-reload loop due to OGN regeneration -## [0.1.9] - 2021-07-21 +## 0.1.9 - 2021-07-21 - Fix for loading sibling DLL paths - Better type checking for built-in function arguments - Added runtime docs, can now list all builtins using `wp.print_builtins()` -## [0.1.8] - 2021-07-14 +## 0.1.8 - 2021-07-14 - Fix for hot-reload of CUDA kernels - Add Tape object for replaying differentiable kernels - Add helpers for Torch interop (convert `torch.Tensor` to `wp.Array`) -## [0.1.7] - 2021-07-05 +## 0.1.7 - 2021-07-05 - Switch to NVRTC for CUDA runtime - Allow running without host compiler - Disable asserts in kernel release mode (small perf. improvement) -## [0.1.6] - 2021-06-14 +## 0.1.6 - 2021-06-14 - Look for CUDA toolchain in target-deps -## [0.1.5] - 2021-06-14 +## 0.1.5 - 2021-06-14 - Rename OgLang -> Warp - Improve CUDA environment error checking - Clean-up some logging, add verbose mode (`wp.config.verbose`) -## [0.1.4] - 2021-06-10 +## 0.1.4 - 2021-06-10 - Add support for mesh raycast -## [0.1.3] - 2021-06-09 +## 0.1.3 - 2021-06-09 - Add support for unary negation operator - Add support for mutating variables during dynamic loops (non-differentiable) @@ -1113,7 +1124,7 @@ - Improve kernel cache start up times (avoids adjointing before cache check) - Update README.md with requirements / examples -## [0.1.2] - 2021-06-03 +## 0.1.2 - 2021-06-03 - Add support for querying mesh velocities - Add CUDA graph support, see `wp.capture_begin()`, `wp.capture_end()`, `wp.capture_launch()` @@ -1123,10 +1134,10 @@ - Fix for Linux/macOS support -## [0.1.1] - 2021-05-18 +## 0.1.1 - 2021-05-18 - Fix bug with conflicting CUDA contexts -## [0.1.0] - 2021-05-17 +## 0.1.0 - 2021-05-17 - Initial publish for alpha testing diff --git a/warp/__init__.py b/warp/__init__.py index b051f837..243d7ae1 100644 --- a/warp/__init__.py +++ b/warp/__init__.py @@ -26,6 +26,9 @@ from warp.types import spatial_vector, spatial_vectorh, spatial_vectorf, spatial_vectord from warp.types import spatial_matrix, spatial_matrixh, spatial_matrixf, spatial_matrixd +# annotation types +from warp.types import Int, Float, Scalar + # geometry types from warp.types import Bvh, Mesh, HashGrid, Volume, MarchingCubes from warp.types import BvhQuery, HashGridQuery, MeshQueryAABB, MeshQueryPoint, MeshQueryRay diff --git a/warp/builtins.py b/warp/builtins.py index 0d2d51b2..21b62429 100644 --- a/warp/builtins.py +++ b/warp/builtins.py @@ -1499,7 +1499,8 @@ def transform_identity_dispatch_func(input_types: Mapping[str, type], return_typ doc="""Apply the transform to a point ``point`` treating the homogeneous coordinate as w=1. The transformation is applied treating ``point`` as a column vector, e.g.: ``y = mat*point``. - Note this is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = point^T*mat^T``. + + This is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = point^T*mat^T``. If the transform is coming from a library that uses row-vectors, then users should transpose the transformation matrix before calling this method.""", ) @@ -1517,8 +1518,9 @@ def transform_identity_dispatch_func(input_types: Mapping[str, type], return_typ group="Vector Math", doc="""Apply the transform to a vector ``vec`` treating the homogeneous coordinate as w=0. - The transformation is applied treating ``vec`` as a column vector, e.g.: ``y = mat*vec`` - note this is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = vec^T*mat^T``. + The transformation is applied treating ``vec`` as a column vector, e.g.: ``y = mat*vec``. + + This is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = vec^T*mat^T``. If the transform is coming from a library that uses row-vectors, then users should transpose the transformation matrix before calling this method.""", ) @@ -3551,6 +3553,16 @@ def compute(): "iter_next", input_types={"query": mesh_query_aabb_t}, value_type=int, group="Utility", export=False, hidden=True ) +add_builtin( + "reversed", + input_types={"range": range_t}, + value_type=range_t, + native_func="iter_reverse", + group="Utility", + doc="""Returns the range in reversed order.""", + export=False, +) + # --------------------------------- # Volumes @@ -3922,7 +3934,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "rand_init", input_types={"seed": int}, value_type=uint32, - export=False, group="Random", doc="Initialize a new random number generator given a user-defined seed. Returns a 32-bit integer representing the RNG state.", ) @@ -3931,7 +3942,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "rand_init", input_types={"seed": int, "offset": int}, value_type=uint32, - export=False, group="Random", doc="""Initialize a new random number generator given a user-defined seed and an offset. @@ -3943,7 +3953,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "randi", input_types={"state": uint32}, value_type=int, - export=False, group="Random", doc="Return a random integer in the range [0, 2^32).", ) @@ -3951,7 +3960,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "randi", input_types={"state": uint32, "low": int, "high": int}, value_type=int, - export=False, group="Random", doc="Return a random integer between [low, high).", ) @@ -3959,7 +3967,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "randf", input_types={"state": uint32}, value_type=float, - export=False, group="Random", doc="Return a random float between [0.0, 1.0).", ) @@ -3967,24 +3974,17 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "randf", input_types={"state": uint32, "low": float, "high": float}, value_type=float, - export=False, group="Random", doc="Return a random float between [low, high).", ) add_builtin( - "randn", - input_types={"state": uint32}, - value_type=float, - export=False, - group="Random", - doc="Sample a normal distribution.", + "randn", input_types={"state": uint32}, value_type=float, group="Random", doc="Sample a normal distribution." ) add_builtin( "sample_cdf", input_types={"state": uint32, "cdf": array(dtype=float)}, value_type=int, - export=False, group="Random", doc="Inverse-transform sample a cumulative distribution function.", ) @@ -3992,7 +3992,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_triangle", input_types={"state": uint32}, value_type=vec2, - export=False, group="Random", doc="Uniformly sample a triangle. Returns sample barycentric coordinates.", ) @@ -4000,7 +3999,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_ring", input_types={"state": uint32}, value_type=vec2, - export=False, group="Random", doc="Uniformly sample a ring in the xy plane.", ) @@ -4008,7 +4006,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_disk", input_types={"state": uint32}, value_type=vec2, - export=False, group="Random", doc="Uniformly sample a disk in the xy plane.", ) @@ -4016,7 +4013,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_sphere_surface", input_types={"state": uint32}, value_type=vec3, - export=False, group="Random", doc="Uniformly sample a unit sphere surface.", ) @@ -4024,7 +4020,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_sphere", input_types={"state": uint32}, value_type=vec3, - export=False, group="Random", doc="Uniformly sample a unit sphere.", ) @@ -4032,7 +4027,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_hemisphere_surface", input_types={"state": uint32}, value_type=vec3, - export=False, group="Random", doc="Uniformly sample a unit hemisphere surface.", ) @@ -4040,7 +4034,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_hemisphere", input_types={"state": uint32}, value_type=vec3, - export=False, group="Random", doc="Uniformly sample a unit hemisphere.", ) @@ -4048,7 +4041,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_square", input_types={"state": uint32}, value_type=vec2, - export=False, group="Random", doc="Uniformly sample a unit square.", ) @@ -4056,7 +4048,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "sample_unit_cube", input_types={"state": uint32}, value_type=vec3, - export=False, group="Random", doc="Uniformly sample a unit cube.", ) @@ -4065,7 +4056,6 @@ def volume_sample_grad_index_value_func(arg_types: Mapping[str, type], arg_value "poisson", input_types={"state": uint32, "lam": float}, value_type=uint32, - export=False, group="Random", doc="""Generate a random sample from a Poisson distribution. @@ -4353,7 +4343,8 @@ def address_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, A for array_type in array_types: add_builtin( "address", - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "l": Int}, + constraint=sametypes, defaults={"j": None, "k": None, "l": None}, hidden=True, value_func=address_value_func, @@ -4397,8 +4388,9 @@ def view_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any] for array_type in array_types: add_builtin( "view", - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int}, defaults={"j": None, "k": None}, + constraint=sametypes, hidden=True, value_func=view_value_func, group="Utility", @@ -4440,7 +4432,8 @@ def array_store_value_func(arg_types: Mapping[str, type], arg_values: Mapping[st for array_type in array_types: add_builtin( "array_store", - input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "value": Any}, + constraint=sametypes, hidden=True, value_func=array_store_value_func, skip_replay=True, @@ -4448,7 +4441,8 @@ def array_store_value_func(arg_types: Mapping[str, type], arg_values: Mapping[st ) add_builtin( "array_store", - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "value": Any}, + constraint=sametypes, hidden=True, value_func=array_store_value_func, skip_replay=True, @@ -4456,7 +4450,8 @@ def array_store_value_func(arg_types: Mapping[str, type], arg_values: Mapping[st ) add_builtin( "array_store", - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "value": Any}, + constraint=sametypes, hidden=True, value_func=array_store_value_func, skip_replay=True, @@ -4464,7 +4459,8 @@ def array_store_value_func(arg_types: Mapping[str, type], arg_values: Mapping[st ) add_builtin( "array_store", - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "l": Int, "value": Any}, + constraint=sametypes, hidden=True, value_func=array_store_value_func, skip_replay=True, @@ -4516,6 +4512,11 @@ def load_dispatch_func(input_types: Mapping[str, type], return_type: Any, args: ) +def atomic_op_constraint(arg_types: Mapping[str, Any]): + idx_types = tuple(arg_types[x] for x in "ijkl" if arg_types.get(x, None) is not None) + return all(types_equal(idx_types[0], t) for t in idx_types[1:]) and arg_types["arr"].ndim == len(idx_types) + + def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, Any]): if arg_types is None: return Any @@ -4560,7 +4561,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_add", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically add ``value`` onto ``arr[i]`` and return the old value.", group="Utility", @@ -4569,7 +4571,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_add", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically add ``value`` onto ``arr[i,j]`` and return the old value.", group="Utility", @@ -4578,7 +4581,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_add", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically add ``value`` onto ``arr[i,j,k]`` and return the old value.", group="Utility", @@ -4587,7 +4591,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_add", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "l": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically add ``value`` onto ``arr[i,j,k,l]`` and return the old value.", group="Utility", @@ -4597,7 +4602,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_sub", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically subtract ``value`` onto ``arr[i]`` and return the old value.", group="Utility", @@ -4606,7 +4612,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_sub", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically subtract ``value`` onto ``arr[i,j]`` and return the old value.", group="Utility", @@ -4615,7 +4622,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_sub", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically subtract ``value`` onto ``arr[i,j,k]`` and return the old value.", group="Utility", @@ -4624,7 +4632,8 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_sub", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "l": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="Atomically subtract ``value`` onto ``arr[i,j,k,l]`` and return the old value.", group="Utility", @@ -4634,44 +4643,48 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_min", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) add_builtin( "atomic_min", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) add_builtin( "atomic_min", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) add_builtin( "atomic_min", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "l": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -4679,44 +4692,48 @@ def atomic_op_value_func(arg_types: Mapping[str, type], arg_values: Mapping[str, add_builtin( "atomic_max", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) add_builtin( "atomic_max", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) add_builtin( "atomic_max", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) add_builtin( "atomic_max", hidden=hidden, - input_types={"arr": array_type(dtype=Any), "i": int, "j": int, "k": int, "l": int, "value": Any}, + input_types={"arr": array_type(dtype=Any), "i": Int, "j": Int, "k": Int, "l": Int, "value": Any}, + constraint=atomic_op_constraint, value_func=atomic_op_value_func, doc="""Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices.""", + The operation is only atomic on a per-component basis for vectors and matrices.""", group="Utility", skip_replay=True, ) @@ -5958,13 +5975,12 @@ def tile_fft_generic_lto_dispatch_func( value_type=Any, doc="""Evaluates a static Python expression and replaces it with its result. - See the `codegen.html#static-expressions
`_ for more details. + See the :ref:`code generation guide ` for more details. - Note: - The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, - which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. - The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions - (excluding Warp arrays since they cannot be created in a Warp kernel at the moment).""", + The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, + which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. + The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions + (excluding Warp arrays since they cannot be created in a Warp kernel at the moment).""", group="Code Generation", ) diff --git a/warp/codegen.py b/warp/codegen.py index 53519521..000ea4d5 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -951,7 +951,9 @@ def build(adj, builder, default_builder_options=None): adj.return_var = None # return type for function or kernel adj.loop_symbols = [] # symbols at the start of each loop - adj.loop_const_iter_symbols = set() # iteration variables (constant) for static loops + adj.loop_const_iter_symbols = ( + set() + ) # constant iteration variables for static loops (mutating them does not raise an error) # blocks adj.blocks = [Block()] @@ -1007,7 +1009,6 @@ def format_args(adj, prefix, args): arg_strs.append(f"{a.namespace}{a.native_func}") else: arg_strs.append(f"{a.namespace}{prefix}_{a.native_func}") - elif is_reference(a.type): arg_strs.append(f"{prefix}_{a}") elif isinstance(a, Var): @@ -1339,6 +1340,10 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): if return_type is None: # handles expression (zero output) functions, e.g.: void do_something(); + + output = None + output_list = [] + forward_call = ( f"{func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" ) @@ -1348,14 +1353,23 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): elif not isinstance(return_type, Sequence) or len(return_type) == 1: # handle simple function (one output) - forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" + if isinstance(return_type, Sequence): + return_type = return_type[0] + output = adj.add_var(return_type) + output_list = [output] + + forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" replay_call = forward_call if func.custom_replay_func is not None: replay_call = f"var_{output} = {func.namespace}replay_{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" else: # handle multiple value functions + + output = [adj.add_var(v) for v in return_type] + output_list = output + forward_call = ( f"{func.namespace}{func_name}({adj.format_forward_call_args(fwd_args + output, use_initializer_list)});" ) @@ -1366,7 +1380,7 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): else: adj.add_forward(forward_call, replay=replay_call) - if not func.missing_grad and len(args): + if not func.missing_grad and len(func_args): adj_args = tuple(strip_reference(x) for x in func_args) reverse_has_output_args = ( func.require_original_output_arg or len(output_list) > 1 @@ -1871,7 +1885,7 @@ def materialize_redefinitions(adj, symbols): # detect symbols with conflicting definitions (assigned inside the for loop) for items in symbols.items(): sym = items[0] - if adj.loop_const_iter_symbols is not None and sym in adj.loop_const_iter_symbols: + if adj.is_constant_iter_symbol(sym): # ignore constant overwriting in for-loops if it is a loop iterator # (it is no problem to unroll static loops multiple times in sequence) continue @@ -2022,12 +2036,11 @@ def get_unroll_range(adj, loop): ) return range_call - def begin_record_constant_iter_symbols(adj): - if adj.loop_const_iter_symbols is None: - adj.loop_const_iter_symbols = set() + def record_constant_iter_symbol(adj, sym): + adj.loop_const_iter_symbols.add(sym) - def end_record_constant_iter_symbols(adj): - adj.loop_const_iter_symbols = None + def is_constant_iter_symbol(adj, sym): + return sym in adj.loop_const_iter_symbols def emit_For(adj, node): # try and unroll simple range() statements that use constant args @@ -2035,9 +2048,8 @@ def emit_For(adj, node): if isinstance(unroll_range, range): const_iter_sym = node.target.id - if adj.loop_const_iter_symbols is not None: - # prevent constant conflicts in `materialize_redefinitions()` - adj.loop_const_iter_symbols.add(const_iter_sym) + # prevent constant conflicts in `materialize_redefinitions()` + adj.record_constant_iter_symbol(const_iter_sym) # unroll static for-loop for i in unroll_range: @@ -2058,7 +2070,6 @@ def emit_For(adj, node): iter = adj.eval(node.iter) adj.symbols[node.target.id] = adj.begin_for(iter) - adj.begin_record_constant_iter_symbols() # for loops should be side-effect free, here we store a copy adj.loop_symbols.append(adj.symbols.copy()) @@ -2069,7 +2080,6 @@ def emit_For(adj, node): adj.materialize_redefinitions(adj.loop_symbols[-1]) adj.loop_symbols.pop() - adj.end_record_constant_iter_symbols() adj.end_for(iter) @@ -2288,8 +2298,8 @@ def emit_Subscript(adj, node): return var target, indices = adj.eval_subscript(node) - target_type = strip_reference(target.type) + target_type = strip_reference(target.type) if is_array(target_type): if len(indices) == target_type.ndim: # handles array loads (where each dimension has an index specified) @@ -3108,7 +3118,6 @@ def get_references(adj) -> Tuple[Dict[str, Any], Dict[Any, Any], Dict[warp.conte """ - cpu_kernel_template = """ void {name}_cpu_kernel_forward( diff --git a/warp/config.py b/warp/config.py index 49df51ea..a703df21 100644 --- a/warp/config.py +++ b/warp/config.py @@ -7,7 +7,7 @@ from typing import Optional -version: str = "1.4.0" +version: str = "1.4.1" """Warp version string""" verify_fp: bool = False diff --git a/warp/context.py b/warp/context.py index 65ddeebe..d3f6adbc 100644 --- a/warp/context.py +++ b/warp/context.py @@ -6,7 +6,6 @@ # license agreement from NVIDIA CORPORATION is strictly prohibited. import ast -import builtins import ctypes import functools import hashlib @@ -22,7 +21,6 @@ import weakref from copy import copy as shallowcopy from pathlib import Path -from struct import pack as struct_pack from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, Union import numpy as np @@ -1257,6 +1255,7 @@ def initializer_list_func(args, return_type): key, input_types=arg_types, value_type=return_type, + value_func=value_func if return_type is Any else None, export_func=export_func, dispatch_func=dispatch_func, lto_dispatch_func=lto_dispatch_func, @@ -1495,30 +1494,16 @@ def hash_adjoint(self, adj): # hash referenced constants for name, value in constants.items(): ch.update(bytes(name, "utf-8")) - # hash the referenced object - if isinstance(value, builtins.bool): - # This needs to come before the check for `int` since all boolean - # values are also instances of `int`. - ch.update(struct_pack("?", value)) - elif isinstance(value, int): - ch.update(struct_pack(" -inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, const array_t& adj_buf, int& adj_i, const T& adj_output) +inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, const array_t& adj_buf, int adj_i, const T& adj_output) { if (adj_buf.data) adj_atomic_add(&index(adj_buf, i), adj_output); @@ -826,7 +826,7 @@ inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, const array_ adj_atomic_add(&index_grad(buf, i), adj_output); } template -inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, const array_t& adj_buf, int& adj_i, int& adj_j, const T& adj_output) +inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, const array_t& adj_buf, int adj_i, int adj_j, const T& adj_output) { if (adj_buf.data) adj_atomic_add(&index(adj_buf, i, j), adj_output); @@ -834,7 +834,7 @@ inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, const adj_atomic_add(&index_grad(buf, i, j), adj_output); } template -inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, int k, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, const T& adj_output) +inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, int k, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, const T& adj_output) { if (adj_buf.data) adj_atomic_add(&index(adj_buf, i, j, k), adj_output); @@ -842,7 +842,7 @@ inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, int k adj_atomic_add(&index_grad(buf, i, j, k), adj_output); } template -inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, int k, int l, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, const T& adj_output) +inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, int k, int l, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, const T& adj_output) { if (adj_buf.data) adj_atomic_add(&index(adj_buf, i, j, k, l), adj_output); @@ -851,7 +851,7 @@ inline CUDA_CALLABLE void adj_address(const array_t& buf, int i, int j, int k } template -inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, T value, const array_t& adj_buf, int& adj_i, T& adj_value) +inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, T value, const array_t& adj_buf, int adj_i, T& adj_value) { if (adj_buf.data) adj_value += index(adj_buf, i); @@ -861,7 +861,7 @@ inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, T value, FP_VERIFY_ADJ_1(value, adj_value) } template -inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, T value, const array_t& adj_buf, int& adj_i, int& adj_j, T& adj_value) +inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, T value, const array_t& adj_buf, int adj_i, int adj_j, T& adj_value) { if (adj_buf.data) adj_value += index(adj_buf, i, j); @@ -871,7 +871,7 @@ inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, T FP_VERIFY_ADJ_2(value, adj_value) } template -inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, int k, T value, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value) +inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, int k, T value, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value) { if (adj_buf.data) adj_value += index(adj_buf, i, j, k); @@ -881,7 +881,7 @@ inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, i FP_VERIFY_ADJ_3(value, adj_value) } template -inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, int k, int l, T value, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value) +inline CUDA_CALLABLE void adj_array_store(const array_t& buf, int i, int j, int k, int l, T value, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value) { if (adj_buf.data) adj_value += index(adj_buf, i, j, k, l); @@ -905,7 +905,7 @@ inline CUDA_CALLABLE void adj_load(const T* address, const T& adj_address, T& ad } template -inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, T value, const array_t& adj_buf, int& adj_i, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, T value, const array_t& adj_buf, int adj_i, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value += index(adj_buf, i); @@ -915,7 +915,7 @@ inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, T value, FP_VERIFY_ADJ_1(value, adj_value) } template -inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, T value, const array_t& adj_buf, int& adj_i, int& adj_j, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, T value, const array_t& adj_buf, int adj_i, int adj_j, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value += index(adj_buf, i, j); @@ -925,7 +925,7 @@ inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, T FP_VERIFY_ADJ_2(value, adj_value) } template -inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, int k, T value, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, int k, T value, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value += index(adj_buf, i, j, k); @@ -935,7 +935,7 @@ inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, in FP_VERIFY_ADJ_3(value, adj_value) } template -inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, int k, int l, T value, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, int k, int l, T value, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value += index(adj_buf, i, j, k, l); @@ -946,7 +946,7 @@ inline CUDA_CALLABLE void adj_atomic_add(const array_t& buf, int i, int j, in } template -inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, T value, const array_t& adj_buf, int& adj_i, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, T value, const array_t& adj_buf, int adj_i, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value -= index(adj_buf, i); @@ -956,7 +956,7 @@ inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, T value, FP_VERIFY_ADJ_1(value, adj_value) } template -inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, T value, const array_t& adj_buf, int& adj_i, int& adj_j, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, T value, const array_t& adj_buf, int adj_i, int adj_j, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value -= index(adj_buf, i, j); @@ -966,7 +966,7 @@ inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, T FP_VERIFY_ADJ_2(value, adj_value) } template -inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, int k, T value, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, int k, T value, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value -= index(adj_buf, i, j, k); @@ -976,7 +976,7 @@ inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, in FP_VERIFY_ADJ_3(value, adj_value) } template -inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, int k, int l, T value, const array_t& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value, const T& adj_ret) +inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, int k, int l, T value, const array_t& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_value -= index(adj_buf, i, j, k, l); @@ -988,44 +988,44 @@ inline CUDA_CALLABLE void adj_atomic_sub(const array_t& buf, int i, int j, in // generic array types that do not support gradient computation (indexedarray, etc.) template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_address(const A1& buf, int i, const A2& adj_buf, int& adj_i, const T& adj_output) {} +inline CUDA_CALLABLE void adj_address(const A1& buf, int i, const A2& adj_buf, int adj_i, const T& adj_output) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_address(const A1& buf, int i, int j, const A2& adj_buf, int& adj_i, int& adj_j, const T& adj_output) {} +inline CUDA_CALLABLE void adj_address(const A1& buf, int i, int j, const A2& adj_buf, int adj_i, int adj_j, const T& adj_output) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_address(const A1& buf, int i, int j, int k, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, const T& adj_output) {} +inline CUDA_CALLABLE void adj_address(const A1& buf, int i, int j, int k, const A2& adj_buf, int adj_i, int adj_j, int adj_k, const T& adj_output) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_address(const A1& buf, int i, int j, int k, int l, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, const T& adj_output) {} +inline CUDA_CALLABLE void adj_address(const A1& buf, int i, int j, int k, int l, const A2& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, const T& adj_output) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, T value, const A2& adj_buf, int& adj_i, T& adj_value) {} +inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, T value, const A2& adj_buf, int adj_i, T& adj_value) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, int j, T value, const A2& adj_buf, int& adj_i, int& adj_j, T& adj_value) {} +inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, int j, T value, const A2& adj_buf, int adj_i, int adj_j, T& adj_value) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value) {} +inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value) {} +inline CUDA_CALLABLE void adj_array_store(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, T value, const A2& adj_buf, int& adj_i, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, T value, const A2& adj_buf, int adj_i, T& adj_value, const T& adj_ret) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, int j, T value, const A2& adj_buf, int& adj_i, int& adj_j, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, int j, T value, const A2& adj_buf, int adj_i, int adj_j, T& adj_value, const T& adj_ret) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value, const T& adj_ret) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_add(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value, const T& adj_ret) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, T value, const A2& adj_buf, int& adj_i, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, T value, const A2& adj_buf, int adj_i, T& adj_value, const T& adj_ret) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, int j, T value, const A2& adj_buf, int& adj_i, int& adj_j, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, int j, T value, const A2& adj_buf, int adj_i, int adj_j, T& adj_value, const T& adj_ret) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value, const T& adj_ret) {} template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value, const T& adj_ret) {} +inline CUDA_CALLABLE void adj_atomic_sub(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value, const T& adj_ret) {} // generic handler for scalar values template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, T value, const A2& adj_buf, int& adj_i, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, T value, const A2& adj_buf, int adj_i, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i), &index(adj_buf, i), value, adj_value); else if (buf.grad) @@ -1034,7 +1034,7 @@ inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, T value, const FP_VERIFY_ADJ_1(value, adj_value) } template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, T value, const A2& adj_buf, int& adj_i, int& adj_j, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, T value, const A2& adj_buf, int adj_i, int adj_j, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i, j), &index(adj_buf, i, j), value, adj_value); else if (buf.grad) @@ -1043,7 +1043,7 @@ inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, T value FP_VERIFY_ADJ_2(value, adj_value) } template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i, j, k), &index(adj_buf, i, j, k), value, adj_value); else if (buf.grad) @@ -1052,7 +1052,7 @@ inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, int k, FP_VERIFY_ADJ_3(value, adj_value) } template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i, j, k, l), &index(adj_buf, i, j, k, l), value, adj_value); else if (buf.grad) @@ -1062,7 +1062,7 @@ inline CUDA_CALLABLE void adj_atomic_min(const A1& buf, int i, int j, int k, } template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, T value, const A2& adj_buf, int& adj_i, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, T value, const A2& adj_buf, int adj_i, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i), &index(adj_buf, i), value, adj_value); else if (buf.grad) @@ -1071,7 +1071,7 @@ inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, T value, const FP_VERIFY_ADJ_1(value, adj_value) } template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, T value, const A2& adj_buf, int& adj_i, int& adj_j, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, T value, const A2& adj_buf, int adj_i, int adj_j, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i, j), &index(adj_buf, i, j), value, adj_value); else if (buf.grad) @@ -1080,7 +1080,7 @@ inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, T value FP_VERIFY_ADJ_2(value, adj_value) } template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, int k, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i, j, k), &index(adj_buf, i, j, k), value, adj_value); else if (buf.grad) @@ -1089,7 +1089,7 @@ inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, int k, FP_VERIFY_ADJ_3(value, adj_value) } template class A1, template class A2, typename T> -inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int& adj_i, int& adj_j, int& adj_k, int& adj_l, T& adj_value, const T& adj_ret) { +inline CUDA_CALLABLE void adj_atomic_max(const A1& buf, int i, int j, int k, int l, T value, const A2& adj_buf, int adj_i, int adj_j, int adj_k, int adj_l, T& adj_value, const T& adj_ret) { if (adj_buf.data) adj_atomic_minmax(&index(buf, i, j, k, l), &index(adj_buf, i, j, k, l), value, adj_value); else if (buf.grad) diff --git a/warp/native/builtin.h b/warp/native/builtin.h index bf12b765..6c8fb637 100644 --- a/warp/native/builtin.h +++ b/warp/native/builtin.h @@ -1608,35 +1608,73 @@ inline CUDA_CALLABLE void print(transform_t t) printf("(%g %g %g) (%g %g %g %g)\n", float(t.p[0]), float(t.p[1]), float(t.p[2]), float(t.q.x), float(t.q.y), float(t.q.z), float(t.q.w)); } -inline CUDA_CALLABLE void adj_print(bool i, bool adj_i) { printf("%d adj: %d\n", i, adj_i); } -inline CUDA_CALLABLE void adj_print(int8 i, int8 adj_i) { printf("%hhd adj: %hhd\n", i, adj_i); } -inline CUDA_CALLABLE void adj_print(int i, int adj_i) { printf("%d adj: %d\n", i, adj_i); } -inline CUDA_CALLABLE void adj_print(float f, float adj_f) { printf("%g adj: %g\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(short f, short adj_f) { printf("%hd adj: %hd\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(long f, long adj_f) { printf("%ld adj: %ld\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(long long f, long long adj_f) { printf("%lld adj: %lld\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(uint8 i, uint8 adj_i) { printf("%hhu adj: %hhu\n", i, adj_i); } -inline CUDA_CALLABLE void adj_print(unsigned f, unsigned adj_f) { printf("%u adj: %u\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(unsigned short f, unsigned short adj_f) { printf("%hu adj: %hu\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(unsigned long f, unsigned long adj_f) { printf("%lu adj: %lu\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(unsigned long long f, unsigned long long adj_f) { printf("%llu adj: %llu\n", f, adj_f); } -inline CUDA_CALLABLE void adj_print(half h, half adj_h) { printf("%g adj: %g\n", half_to_float(h), half_to_float(adj_h)); } -inline CUDA_CALLABLE void adj_print(double f, double adj_f) { printf("%g adj: %g\n", f, adj_f); } +template +inline CUDA_CALLABLE void adj_print(const T& x, const T& adj_x) +{ + printf("adj: \n"); +} + +// note: adj_print() only prints the adjoint value, since the value itself gets printed in replay print() +inline CUDA_CALLABLE void adj_print(half x, half adj_x) { printf("adj: %g\n", half_to_float(adj_x)); } +inline CUDA_CALLABLE void adj_print(float x, float adj_x) { printf("adj: %g\n", adj_x); } +inline CUDA_CALLABLE void adj_print(double x, double adj_x) { printf("adj: %g\n", adj_x); } + +inline CUDA_CALLABLE void adj_print(signed char x, signed char adj_x) { printf("adj: %d\n", adj_x); } +inline CUDA_CALLABLE void adj_print(short x, short adj_x) { printf("adj: %d\n", adj_x); } +inline CUDA_CALLABLE void adj_print(int x, int adj_x) { printf("adj: %d\n", adj_x); } +inline CUDA_CALLABLE void adj_print(long x, long adj_x) { printf("adj: %ld\n", adj_x); } +inline CUDA_CALLABLE void adj_print(long long x, long long adj_x) { printf("adj: %lld\n", adj_x); } + +inline CUDA_CALLABLE void adj_print(unsigned char x, unsigned char adj_x) { printf("adj: %u\n", adj_x); } +inline CUDA_CALLABLE void adj_print(unsigned short x, unsigned short adj_x) { printf("adj: %u\n", adj_x); } +inline CUDA_CALLABLE void adj_print(unsigned x, unsigned adj_x) { printf("adj: %u\n", adj_x); } +inline CUDA_CALLABLE void adj_print(unsigned long x, unsigned long adj_x) { printf("adj: %lu\n", adj_x); } +inline CUDA_CALLABLE void adj_print(unsigned long long x, unsigned long long adj_x) { printf("adj: %llu\n", adj_x); } + +inline CUDA_CALLABLE void adj_print(bool x, bool adj_x) { printf("adj: %s\n", (adj_x ? "True" : "False")); } template -inline CUDA_CALLABLE void adj_print(vec_t v, vec_t& adj_v) { printf("%g %g adj: %g %g \n", v[0], v[1], adj_v[0], adj_v[1]); } +inline CUDA_CALLABLE void adj_print(const vec_t& v, const vec_t& adj_v) +{ + printf("adj:"); + for (unsigned i = 0; i < Length; i++) + printf(" %g", float(adj_v[i])); + printf("\n"); +} template -inline CUDA_CALLABLE void adj_print(mat_t m, mat_t& adj_m) { } +inline CUDA_CALLABLE void adj_print(const mat_t& m, const mat_t& adj_m) +{ + for (unsigned i = 0; i < Rows; i++) + { + if (i == 0) + printf("adj:"); + else + printf(" "); + for (unsigned j = 0; j < Cols; j++) + printf(" %g", float(adj_m.data[i][j])); + printf("\n"); + } +} template -inline CUDA_CALLABLE void adj_print(quat_t q, quat_t& adj_q) { printf("%g %g %g %g adj: %g %g %g %g\n", q.x, q.y, q.z, q.w, adj_q.x, adj_q.y, adj_q.z, adj_q.w); } +inline CUDA_CALLABLE void adj_print(const quat_t& q, const quat_t& adj_q) +{ + printf("adj: %g %g %g %g\n", float(adj_q.x), float(adj_q.y), float(adj_q.z), float(adj_q.w)); +} template -inline CUDA_CALLABLE void adj_print(transform_t t, transform_t& adj_t) {} - -inline CUDA_CALLABLE void adj_print(str t, str& adj_t) {} +inline CUDA_CALLABLE void adj_print(const transform_t& t, const transform_t& adj_t) +{ + printf("adj: (%g %g %g) (%g %g %g %g)\n", + float(adj_t.p[0]), float(adj_t.p[1]), float(adj_t.p[2]), + float(adj_t.q.x), float(adj_t.q.y), float(adj_t.q.z), float(adj_t.q.w)); +} +inline CUDA_CALLABLE void adj_print(str t, str& adj_t) +{ + printf("adj: %s\n", t); +} template inline CUDA_CALLABLE void expect_eq(const T& actual, const T& expected) diff --git a/warp/native/bvh.cu b/warp/native/bvh.cu index b8bc69f6..6a67287b 100644 --- a/warp/native/bvh.cu +++ b/warp/native/bvh.cu @@ -65,7 +65,7 @@ __global__ void bvh_refit_kernel(int n, const int* __restrict__ parents, int* __ int finished = atomicAdd(&child_count[parent], 1); // if we have are the last thread (such that the parent node is now complete) - // then update its bounds and move onto the the next parent in the hierarchy + // then update its bounds and move onto the next parent in the hierarchy if (finished == 1) { const int left_child = node_lowers[parent].i; @@ -273,7 +273,7 @@ __global__ void build_hierarchy(int n, int* root, const int* __restrict__ deltas } // if we have are the last thread (such that the parent node is now complete) - // then update its bounds and move onto the the next parent in the hierarchy + // then update its bounds and move onto the next parent in the hierarchy if (childCount == 1) { const int left_child = lowers[parent].i; diff --git a/warp/native/bvh.h b/warp/native/bvh.h index eed1ffd8..e2dca507 100644 --- a/warp/native/bvh.h +++ b/warp/native/bvh.h @@ -404,6 +404,10 @@ CUDA_CALLABLE inline bvh_query_t iter_reverse(const bvh_query_t& query) return query; } +CUDA_CALLABLE inline void adj_iter_reverse(const bvh_query_t& query, bvh_query_t& adj_query, bvh_query_t& adj_ret) +{ +} + // stub CUDA_CALLABLE inline void adj_bvh_query_next(bvh_query_t& query, int& index, bvh_query_t&, int&, bool&) diff --git a/warp/native/exports.h b/warp/native/exports.h index 17778056..f8fd82af 100644 --- a/warp/native/exports.h +++ b/warp/native/exports.h @@ -1013,6 +1013,23 @@ WP_API void builtin_volume_index_to_world_uint64_vec3f(uint64 id, vec3f& uvw, ve WP_API void builtin_volume_world_to_index_uint64_vec3f(uint64 id, vec3f& xyz, vec3f* ret) { *ret = wp::volume_world_to_index(id, xyz); } WP_API void builtin_volume_index_to_world_dir_uint64_vec3f(uint64 id, vec3f& uvw, vec3f* ret) { *ret = wp::volume_index_to_world_dir(id, uvw); } WP_API void builtin_volume_world_to_index_dir_uint64_vec3f(uint64 id, vec3f& xyz, vec3f* ret) { *ret = wp::volume_world_to_index_dir(id, xyz); } +WP_API void builtin_rand_init_int32(int32 seed, uint32* ret) { *ret = wp::rand_init(seed); } +WP_API void builtin_rand_init_int32_int32(int32 seed, int32 offset, uint32* ret) { *ret = wp::rand_init(seed, offset); } +WP_API void builtin_randi_uint32(uint32 state, int* ret) { *ret = wp::randi(state); } +WP_API void builtin_randi_uint32_int32_int32(uint32 state, int32 low, int32 high, int* ret) { *ret = wp::randi(state, low, high); } +WP_API void builtin_randf_uint32(uint32 state, float* ret) { *ret = wp::randf(state); } +WP_API void builtin_randf_uint32_float32_float32(uint32 state, float32 low, float32 high, float* ret) { *ret = wp::randf(state, low, high); } +WP_API void builtin_randn_uint32(uint32 state, float* ret) { *ret = wp::randn(state); } +WP_API void builtin_sample_triangle_uint32(uint32 state, vec2f* ret) { *ret = wp::sample_triangle(state); } +WP_API void builtin_sample_unit_ring_uint32(uint32 state, vec2f* ret) { *ret = wp::sample_unit_ring(state); } +WP_API void builtin_sample_unit_disk_uint32(uint32 state, vec2f* ret) { *ret = wp::sample_unit_disk(state); } +WP_API void builtin_sample_unit_sphere_surface_uint32(uint32 state, vec3f* ret) { *ret = wp::sample_unit_sphere_surface(state); } +WP_API void builtin_sample_unit_sphere_uint32(uint32 state, vec3f* ret) { *ret = wp::sample_unit_sphere(state); } +WP_API void builtin_sample_unit_hemisphere_surface_uint32(uint32 state, vec3f* ret) { *ret = wp::sample_unit_hemisphere_surface(state); } +WP_API void builtin_sample_unit_hemisphere_uint32(uint32 state, vec3f* ret) { *ret = wp::sample_unit_hemisphere(state); } +WP_API void builtin_sample_unit_square_uint32(uint32 state, vec2f* ret) { *ret = wp::sample_unit_square(state); } +WP_API void builtin_sample_unit_cube_uint32(uint32 state, vec3f* ret) { *ret = wp::sample_unit_cube(state); } +WP_API void builtin_poisson_uint32_float32(uint32 state, float32 lam, uint32* ret) { *ret = wp::poisson(state, lam); } WP_API void builtin_noise_uint32_float32(uint32 state, float32 x, float* ret) { *ret = wp::noise(state, x); } WP_API void builtin_noise_uint32_vec2f(uint32 state, vec2f& xy, float* ret) { *ret = wp::noise(state, xy); } WP_API void builtin_noise_uint32_vec3f(uint32 state, vec3f& xyz, float* ret) { *ret = wp::noise(state, xyz); } diff --git a/warp/native/hashgrid.h b/warp/native/hashgrid.h index 148f4ded..d5ed485b 100644 --- a/warp/native/hashgrid.h +++ b/warp/native/hashgrid.h @@ -209,6 +209,10 @@ CUDA_CALLABLE inline hash_grid_query_t iter_reverse(const hash_grid_query_t& que return query; } +CUDA_CALLABLE inline void adj_iter_reverse(const hash_grid_query_t& query, hash_grid_query_t& adj_query, hash_grid_query_t& adj_ret) +{ +} + CUDA_CALLABLE inline int hash_grid_point_id(uint64_t id, int& index) diff --git a/warp/native/mesh.cu b/warp/native/mesh.cu index 4ebdf3f3..3bfac181 100644 --- a/warp/native/mesh.cu +++ b/warp/native/mesh.cu @@ -101,7 +101,7 @@ __global__ void bvh_refit_with_solid_angle_kernel(int n, const int* __restrict__ int finished = atomicAdd(&child_count[parent], 1); // if we have are the last thread (such that the parent node is now complete) - // then update its bounds and move onto the the next parent in the hierarchy + // then update its bounds and move onto the next parent in the hierarchy if (finished == 1) { //printf("Compute non-leaf at %d\n", index); @@ -340,4 +340,4 @@ void mesh_set_velocities_device(uint64_t id, wp::array_t velocities) fprintf(stderr, "The mesh id provided to mesh_set_velocities_device is not valid!\n"); return; } -} \ No newline at end of file +} diff --git a/warp/native/mesh.h b/warp/native/mesh.h index 68680479..2f6ad0cb 100644 --- a/warp/native/mesh.h +++ b/warp/native/mesh.h @@ -1693,6 +1693,10 @@ CUDA_CALLABLE inline mesh_query_aabb_t iter_reverse(const mesh_query_aabb_t& que return query; } +CUDA_CALLABLE inline void adj_iter_reverse(const mesh_query_aabb_t& query, mesh_query_aabb_t& adj_query, mesh_query_aabb_t& adj_ret) +{ +} + // stub CUDA_CALLABLE inline void adj_mesh_query_aabb_next(mesh_query_aabb_t& query, int& index, mesh_query_aabb_t&, int&, bool&) diff --git a/warp/native/range.h b/warp/native/range.h index 408ad067..24458bdc 100644 --- a/warp/native/range.h +++ b/warp/native/range.h @@ -97,8 +97,17 @@ CUDA_CALLABLE inline range_t iter_reverse(const range_t& r) { // generates a reverse range, equivalent to reversed(range()) range_t rev; - rev.start = r.end-1; - rev.end = r.start-1; + + if (r.step > 0) + { + rev.start = r.start + int((r.end - r.start - 1) / r.step) * r.step; + } + else + { + rev.start = r.start + int((r.end - r.start + 1) / r.step) * r.step; + } + + rev.end = r.start - r.step; rev.step = -r.step; rev.i = rev.start; @@ -106,4 +115,8 @@ CUDA_CALLABLE inline range_t iter_reverse(const range_t& r) return rev; } +CUDA_CALLABLE inline void adj_iter_reverse(const range_t& r, range_t& adj_r, range_t& adj_ret) +{ +} + } // namespace wp \ No newline at end of file diff --git a/warp/sim/integrator_xpbd.py b/warp/sim/integrator_xpbd.py index ef585c29..d8d1d854 100644 --- a/warp/sim/integrator_xpbd.py +++ b/warp/sim/integrator_xpbd.py @@ -2808,12 +2808,8 @@ def simulate(self, model: Model, state_in: State, state_out: State, dt: float, c with wp.ScopedTimer("simulate", False): if model.particle_count: - if requires_grad: - particle_q = state_out.particle_q - particle_qd = state_out.particle_qd - else: - particle_q = state_out.particle_q - particle_qd = state_out.particle_qd + particle_q = state_out.particle_q + particle_qd = state_out.particle_qd self.particle_q_init = wp.clone(state_in.particle_q) if self.enable_restitution: diff --git a/warp/sim/model.py b/warp/sim/model.py index 4d9df0fb..98a055dd 100644 --- a/warp/sim/model.py +++ b/warp/sim/model.py @@ -641,7 +641,7 @@ class Model: joint_dof_count (int): Total number of velocity degrees of freedom of all joints in the system joint_coord_count (int): Total number of position degrees of freedom of all joints in the system - particle_coloring (list of array): The coloring of all the particles, used for VBD's Gauss-Seidel interation. + particle_coloring (list of array): The coloring of all the particles, used for VBD's Gauss-Seidel iteration. device (wp.Device): Device on which the Model was allocated @@ -1404,9 +1404,8 @@ def add_builder(self, builder, xform=None, update_num_env_count=True, separate_c self.joint_X_p.extend(joint_X_p) self.joint_q.extend(joint_q) - self.add_articulation() - # offset the indices + self.articulation_start.extend([a + self.joint_count for a in builder.articulation_start]) self.joint_parent.extend([p + self.joint_count if p != -1 else -1 for p in builder.joint_parent]) self.joint_child.extend([c + self.joint_count for c in builder.joint_child]) diff --git a/warp/sparse.py b/warp/sparse.py index 37259e70..0b86bd17 100644 --- a/warp/sparse.py +++ b/warp/sparse.py @@ -106,7 +106,7 @@ def _setup_nnz_transfer(self): return BsrMatrix.__setattr__( - self, "_nnz_buf", wp.zeros(dtype=int, shape=(1,), device="cpu", pinned=self.device.is_cuda) + self, "_nnz_buf", wp.empty(dtype=int, shape=(1,), device="cpu", pinned=self.device.is_cuda) ) if self.device.is_cuda: BsrMatrix.__setattr__(self, "_nnz_event", wp.Event(self.device)) @@ -524,7 +524,7 @@ def _bsr_assign_split_blocks( if dest_block >= dest_offsets[dest_row_count]: return - dest_row = wp.lower_bound(dest_offsets, dest_block + 1) - 1 + dest_row = wp.lower_bound(dest_offsets, 0, dest_row_count + 1, dest_block + 1) - 1 src_row = dest_row // row_factor dest_col_in_row = dest_block - dest_offsets[dest_row] @@ -566,7 +566,7 @@ def _bsr_assign_merge_row_col( dest_rows[block] = -1 # invalid dest_cols[block] = -1 else: - row = wp.lower_bound(src_offsets, block + 1) - 1 + row = wp.lower_bound(src_offsets, 0, src_row_count + 1, block + 1) - 1 dest_rows[block] = row // row_factor dest_cols[block] = src_columns[block] // col_factor @@ -589,7 +589,7 @@ def _bsr_assign_merge_blocks( if src_block >= src_offsets[src_row_count]: return - src_row = wp.lower_bound(src_offsets, src_block + 1) - 1 + src_row = wp.lower_bound(src_offsets, 0, src_row_count + 1, src_block + 1) - 1 src_col = src_columns[src_block] dest_row = src_row // row_factor @@ -828,7 +828,7 @@ def bsr_copy( block_type=block_type, device=A.device, ) - bsr_assign(dest=copy, src=A) + bsr_assign(dest=copy, src=A, structure_only=structure_only) return copy @@ -1190,7 +1190,7 @@ def _bsr_get_block_row(dest_offset: int, row_count: int, bsr_offsets: wp.array(d if i >= bsr_offsets[row_count]: rows[dest_offset + i] = -1 # invalid else: - row = wp.lower_bound(bsr_offsets, i + 1) - 1 + row = wp.lower_bound(bsr_offsets, 0, row_count + 1, i + 1) - 1 rows[dest_offset + i] = row @@ -1461,13 +1461,14 @@ def _bsr_mm_compute_values( y_offsets: wp.array(dtype=int), y_columns: wp.array(dtype=int), y_values: wp.array(dtype=Any), + mm_row_count: int, mm_offsets: wp.array(dtype=int), mm_cols: wp.array(dtype=int), mm_values: wp.array(dtype=Any), ): mm_block = wp.tid() - row = wp.lower_bound(mm_offsets, mm_block + 1) - 1 + row = wp.lower_bound(mm_offsets, 0, mm_row_count + 1, mm_block + 1) - 1 col = mm_cols[mm_block] mm_val = mm_values.dtype(type(alpha)(0.0)) @@ -1759,6 +1760,7 @@ def bsr_mm( work_arrays._old_z_offsets if y == z else y.offsets, work_arrays._old_z_columns if y == z else y.columns, work_arrays._old_z_values if y == z else y.values, + z.nrow, z.offsets, z.columns, mm_values, diff --git a/warp/stubs.py b/warp/stubs.py index 77e1c548..eb9cdb6f 100644 --- a/warp/stubs.py +++ b/warp/stubs.py @@ -11,9 +11,6 @@ Rows = TypeVar("Rows", bound=int) Cols = TypeVar("Cols", bound=int) DType = TypeVar("DType") -Int = TypeVar("Int") -Float = TypeVar("Float") -Scalar = TypeVar("Scalar") Vector = Generic[Length, Scalar] Matrix = Generic[Rows, Cols, Scalar] Quaternion = Generic[Float] @@ -39,6 +36,8 @@ from warp.types import spatial_vector, spatial_vectorh, spatial_vectorf, spatial_vectord from warp.types import spatial_matrix, spatial_matrixh, spatial_matrixf, spatial_matrixd +from warp.types import Int, Float, Scalar + from warp.types import Bvh, Mesh, HashGrid, Volume, MarchingCubes from warp.types import BvhQuery, HashGridQuery, MeshQueryAABB, MeshQueryPoint, MeshQueryRay @@ -786,7 +785,8 @@ def transform_point(mat: Matrix[4, 4, Float], point: Vector[3, Float]) -> Vector """Apply the transform to a point ``point`` treating the homogeneous coordinate as w=1. The transformation is applied treating ``point`` as a column vector, e.g.: ``y = mat*point``. - Note this is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = point^T*mat^T``. + + This is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = point^T*mat^T``. If the transform is coming from a library that uses row-vectors, then users should transpose the transformation matrix before calling this method. """ @@ -803,8 +803,9 @@ def transform_vector(xform: Transformation[Float], vec: Vector[3, Float]) -> Vec def transform_vector(mat: Matrix[4, 4, Float], vec: Vector[3, Float]) -> Vector[3, Float]: """Apply the transform to a vector ``vec`` treating the homogeneous coordinate as w=0. - The transformation is applied treating ``vec`` as a column vector, e.g.: ``y = mat*vec`` - note this is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = vec^T*mat^T``. + The transformation is applied treating ``vec`` as a column vector, e.g.: ``y = mat*vec``. + + This is in contrast to some libraries, notably USD, which applies transforms to row vectors, ``y^T = vec^T*mat^T``. If the transform is coming from a library that uses row-vectors, then users should transpose the transformation matrix before calling this method. """ @@ -1604,6 +1605,12 @@ def closest_point_edge_edge(p1: vec3f, q1: vec3f, p2: vec3f, q2: vec3f, epsilon: ... +@over +def reversed(range: range_t) -> range_t: + """Returns the range in reversed order.""" + ... + + @over def volume_sample(id: uint64, uvw: vec3f, sampling_mode: int32, dtype: Any) -> Any: """Sample the volume of type `dtype` given by ``id`` at the volume local-space point ``uvw``. @@ -2082,361 +2089,361 @@ def select(arr: Array[Any], value_if_false: Any, value_if_true: Any) -> Any: @over -def atomic_add(arr: Array[Any], i: int32, value: Any) -> Any: +def atomic_add(arr: Array[Any], i: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i]`` and return the old value.""" ... @over -def atomic_add(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_add(arr: Array[Any], i: Int, j: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j]`` and return the old value.""" ... @over -def atomic_add(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_add(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j,k]`` and return the old value.""" ... @over -def atomic_add(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_add(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j,k,l]`` and return the old value.""" ... @over -def atomic_add(arr: FabricArray[Any], i: int32, value: Any) -> Any: +def atomic_add(arr: FabricArray[Any], i: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i]`` and return the old value.""" ... @over -def atomic_add(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_add(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j]`` and return the old value.""" ... @over -def atomic_add(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_add(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j,k]`` and return the old value.""" ... @over -def atomic_add(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_add(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j,k,l]`` and return the old value.""" ... @over -def atomic_add(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: +def atomic_add(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i]`` and return the old value.""" ... @over -def atomic_add(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_add(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j]`` and return the old value.""" ... @over -def atomic_add(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_add(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j,k]`` and return the old value.""" ... @over -def atomic_add(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_add(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Atomically add ``value`` onto ``arr[i,j,k,l]`` and return the old value.""" ... @over -def atomic_sub(arr: Array[Any], i: int32, value: Any) -> Any: +def atomic_sub(arr: Array[Any], i: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i]`` and return the old value.""" ... @over -def atomic_sub(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_sub(arr: Array[Any], i: Int, j: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j]`` and return the old value.""" ... @over -def atomic_sub(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_sub(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j,k]`` and return the old value.""" ... @over -def atomic_sub(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_sub(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j,k,l]`` and return the old value.""" ... @over -def atomic_sub(arr: FabricArray[Any], i: int32, value: Any) -> Any: +def atomic_sub(arr: FabricArray[Any], i: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i]`` and return the old value.""" ... @over -def atomic_sub(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_sub(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j]`` and return the old value.""" ... @over -def atomic_sub(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_sub(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j,k]`` and return the old value.""" ... @over -def atomic_sub(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_sub(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j,k,l]`` and return the old value.""" ... @over -def atomic_sub(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: +def atomic_sub(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i]`` and return the old value.""" ... @over -def atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_sub(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j]`` and return the old value.""" ... @over -def atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_sub(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j,k]`` and return the old value.""" ... @over -def atomic_sub(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_sub(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Atomically subtract ``value`` onto ``arr[i,j,k,l]`` and return the old value.""" ... @over -def atomic_min(arr: Array[Any], i: int32, value: Any) -> Any: +def atomic_min(arr: Array[Any], i: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_min(arr: Array[Any], i: Int, j: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_min(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_min(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: FabricArray[Any], i: int32, value: Any) -> Any: +def atomic_min(arr: FabricArray[Any], i: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_min(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_min(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_min(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: +def atomic_min(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_min(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_min(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_min(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_min(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Compute the minimum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: Array[Any], i: int32, value: Any) -> Any: +def atomic_max(arr: Array[Any], i: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: Array[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_max(arr: Array[Any], i: Int, j: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_max(arr: Array[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: Array[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_max(arr: Array[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: FabricArray[Any], i: int32, value: Any) -> Any: +def atomic_max(arr: FabricArray[Any], i: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: FabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_max(arr: FabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_max(arr: FabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: FabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_max(arr: FabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: IndexedFabricArray[Any], i: int32, value: Any) -> Any: +def atomic_max(arr: IndexedFabricArray[Any], i: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, value: Any) -> Any: +def atomic_max(arr: IndexedFabricArray[Any], i: Int, j: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, value: Any) -> Any: +def atomic_max(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j,k]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @over -def atomic_max(arr: IndexedFabricArray[Any], i: int32, j: int32, k: int32, l: int32, value: Any) -> Any: +def atomic_max(arr: IndexedFabricArray[Any], i: Int, j: Int, k: Int, l: Int, value: Any) -> Any: """Compute the maximum of ``value`` and ``arr[i,j,k,l]``, atomically update the array, and return the old value. - .. note:: The operation is only atomic on a per-component basis for vectors and matrices. + The operation is only atomic on a per-component basis for vectors and matrices. """ ... @@ -2945,12 +2952,11 @@ def tile_ifft(inout: Tile) -> Tile: def static(expr: Any) -> Any: """Evaluates a static Python expression and replaces it with its result. - See the `codegen.html#static-expressions
`_ for more details. + See the :ref:`code generation guide ` for more details. - Note: - The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, - which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. - The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions - (excluding Warp arrays since they cannot be created in a Warp kernel at the moment). + The inner expression must only reference variables that are available from the current scope where the Warp kernel or function containing the expression is defined, + which includes constant variables and variables captured in the current closure in which the function or kernel is implemented. + The return type of the expression must be either a Warp function, a string, or a type that is supported inside Warp kernels and functions + (excluding Warp arrays since they cannot be created in a Warp kernel at the moment). """ ... diff --git a/warp/tests/test_array.py b/warp/tests/test_array.py index 77721ca5..3ffddb71 100644 --- a/warp/tests/test_array.py +++ b/warp/tests/test_array.py @@ -2609,6 +2609,87 @@ def test_numpy_array_interface(test, device): assert a1.strides == a2.strides +@wp.kernel +def kernel_indexing_types( + arr_1d: wp.array(dtype=wp.int32, ndim=1), + arr_2d: wp.array(dtype=wp.int32, ndim=2), + arr_3d: wp.array(dtype=wp.int32, ndim=3), + arr_4d: wp.array(dtype=wp.int32, ndim=4), +): + x = arr_1d[wp.uint8(0)] + y = arr_1d[wp.int16(1)] + z = arr_1d[wp.uint32(2)] + w = arr_1d[wp.int64(3)] + + x = arr_2d[wp.uint8(0), wp.uint8(0)] + y = arr_2d[wp.int16(1), wp.int16(1)] + z = arr_2d[wp.uint32(2), wp.uint32(2)] + w = arr_2d[wp.int64(3), wp.int64(3)] + + x = arr_3d[wp.uint8(0), wp.uint8(0), wp.uint8(0)] + y = arr_3d[wp.int16(1), wp.int16(1), wp.int16(1)] + z = arr_3d[wp.uint32(2), wp.uint32(2), wp.uint32(2)] + w = arr_3d[wp.int64(3), wp.int64(3), wp.int64(3)] + + x = arr_4d[wp.uint8(0), wp.uint8(0), wp.uint8(0), wp.uint8(0)] + y = arr_4d[wp.int16(1), wp.int16(1), wp.int16(1), wp.int16(1)] + z = arr_4d[wp.uint32(2), wp.uint32(2), wp.uint32(2), wp.uint32(2)] + w = arr_4d[wp.int64(3), wp.int64(3), wp.int64(3), wp.int64(3)] + + arr_1d[wp.uint8(0)] = 123 + arr_1d[wp.int16(1)] = 123 + arr_1d[wp.uint32(2)] = 123 + arr_1d[wp.int64(3)] = 123 + + arr_2d[wp.uint8(0), wp.uint8(0)] = 123 + arr_2d[wp.int16(1), wp.int16(1)] = 123 + arr_2d[wp.uint32(2), wp.uint32(2)] = 123 + arr_2d[wp.int64(3), wp.int64(3)] = 123 + + arr_3d[wp.uint8(0), wp.uint8(0), wp.uint8(0)] = 123 + arr_3d[wp.int16(1), wp.int16(1), wp.int16(1)] = 123 + arr_3d[wp.uint32(2), wp.uint32(2), wp.uint32(2)] = 123 + arr_3d[wp.int64(3), wp.int64(3), wp.int64(3)] = 123 + + arr_4d[wp.uint8(0), wp.uint8(0), wp.uint8(0), wp.uint8(0)] = 123 + arr_4d[wp.int16(1), wp.int16(1), wp.int16(1), wp.int16(1)] = 123 + arr_4d[wp.uint32(2), wp.uint32(2), wp.uint32(2), wp.uint32(2)] = 123 + arr_4d[wp.int64(3), wp.int64(3), wp.int64(3), wp.int64(3)] = 123 + + wp.atomic_add(arr_1d, wp.uint8(0), 123) + wp.atomic_sub(arr_1d, wp.int16(1), 123) + wp.atomic_min(arr_1d, wp.uint32(2), 123) + wp.atomic_max(arr_1d, wp.int64(3), 123) + + wp.atomic_add(arr_2d, wp.uint8(0), wp.uint8(0), 123) + wp.atomic_sub(arr_2d, wp.int16(1), wp.int16(1), 123) + wp.atomic_min(arr_2d, wp.uint32(2), wp.uint32(2), 123) + wp.atomic_max(arr_2d, wp.int64(3), wp.int64(3), 123) + + wp.atomic_add(arr_3d, wp.uint8(0), wp.uint8(0), wp.uint8(0), 123) + wp.atomic_sub(arr_3d, wp.int16(1), wp.int16(1), wp.int16(1), 123) + wp.atomic_min(arr_3d, wp.uint32(2), wp.uint32(2), wp.uint32(2), 123) + wp.atomic_max(arr_3d, wp.int64(3), wp.int64(3), wp.int64(3), 123) + + wp.atomic_add(arr_4d, wp.uint8(0), wp.uint8(0), wp.uint8(0), wp.uint8(0), 123) + wp.atomic_sub(arr_4d, wp.int16(1), wp.int16(1), wp.int16(1), wp.int16(1), 123) + wp.atomic_min(arr_4d, wp.uint32(2), wp.uint32(2), wp.uint32(2), wp.uint32(2), 123) + wp.atomic_max(arr_4d, wp.int64(3), wp.int64(3), wp.int64(3), wp.int64(3), 123) + + +def test_indexing_types(test, device): + arr_1d = wp.zeros(shape=(4,), dtype=wp.int32, device=device) + arr_2d = wp.zeros(shape=(4, 4), dtype=wp.int32, device=device) + arr_3d = wp.zeros(shape=(4, 4, 4), dtype=wp.int32, device=device) + arr_4d = wp.zeros(shape=(4, 4, 4, 4), dtype=wp.int32, device=device) + wp.launch( + kernel=kernel_indexing_types, + dim=1, + inputs=(arr_1d, arr_2d, arr_3d, arr_4d), + device=device, + ) + + devices = get_test_devices() @@ -2675,6 +2756,7 @@ def test_array_new_del(self): add_function_test(TestArray, "test_array_from_int32_domain", test_array_from_int32_domain, devices=devices) add_function_test(TestArray, "test_array_from_int64_domain", test_array_from_int64_domain, devices=devices) +add_function_test(TestArray, "test_indexing_types", test_indexing_types, devices=devices) try: import torch diff --git a/warp/tests/test_codegen.py b/warp/tests/test_codegen.py index e3552ad2..db0bdee7 100644 --- a/warp/tests/test_codegen.py +++ b/warp/tests/test_codegen.py @@ -503,6 +503,76 @@ def dynamic_loop_kernel(n: int, input: wp.array(dtype=float)): ): wp.launch(dynamic_loop_kernel, dim=1, inputs=[3, inputs], device=device) + # the following nested loop must not raise an error + const_a = 7 + const_b = 5 + + @wp.kernel + def mixed_dyn_static_loop_kernel(dyn_a: int, dyn_b: int, dyn_c: int, output: wp.array(dtype=float, ndim=2)): + tid = wp.tid() + for i in range(const_a + 1): + for j in range(dyn_a + 1): + for k in range(dyn_b + 1): + for l in range(const_b + 1): + for m in range(dyn_c + 1): + coeff = i + j + k + l + m + output[tid, coeff] = 1.0 + + dyn_a, dyn_b, dyn_c = 3, 4, 5 + num_threads = 10 + output = wp.empty([num_threads, const_a + const_b + dyn_a + dyn_b + dyn_c + 1], dtype=float, device=device) + wp.launch( + mixed_dyn_static_loop_kernel, + num_threads, + inputs=[ + dyn_a, + dyn_b, + dyn_c, + ], + outputs=[output], + device=device, + ) + assert_np_equal(output.numpy(), np.ones([num_threads, const_a + const_b + dyn_a + dyn_b + dyn_c + 1])) + + @wp.kernel + def static_then_dynamic_loop_kernel(mats: wp.array(dtype=wp.mat33d)): + tid = wp.tid() + mat = wp.mat33d() + for i in range(3): + for j in range(3): + mat[i, j] = wp.float64(0.0) + + dim = 2 + for i in range(dim + 1): + for j in range(dim + 1): + mat[i, j] = wp.float64(1.0) + + mats[tid] = mat + + mats = wp.empty(1, dtype=wp.mat33d, device=device) + wp.launch(static_then_dynamic_loop_kernel, dim=1, inputs=[mats], device=device) + assert_np_equal(mats.numpy(), np.ones((1, 3, 3))) + + @wp.kernel + def dynamic_then_static_loop_kernel(mats: wp.array(dtype=wp.mat33d)): + tid = wp.tid() + mat = wp.mat33d() + + dim = 2 + for i in range(dim + 1): + for j in range(dim + 1): + mat[i, j] = wp.float64(1.0) + + for i in range(3): + for j in range(3): + mat[i, j] = wp.float64(0.0) + + mats[tid] = mat + + mats = wp.empty(1, dtype=wp.mat33d, device=device) + wp.launch(dynamic_then_static_loop_kernel, dim=1, inputs=[mats], device=device) + assert_np_equal(mats.numpy(), np.zeros((1, 3, 3))) + @wp.kernel def test_call_syntax(): diff --git a/warp/tests/test_fabricarray.py b/warp/tests/test_fabricarray.py index 0bf0f41f..6ffe04a2 100644 --- a/warp/tests/test_fabricarray.py +++ b/warp/tests/test_fabricarray.py @@ -821,6 +821,38 @@ def test_fabricarray_fill_matrix(test, device): assert_np_equal(ifb.numpy(), np.zeros((*ifb.shape, *mat_shape), dtype=nptype)) +@wp.kernel +def fa_kernel_indexing_types( + a: wp.fabricarray(dtype=wp.int32), +): + x = a[wp.uint8(0)] + y = a[wp.int16(1)] + z = a[wp.uint32(2)] + w = a[wp.int64(3)] + + a[wp.uint8(0)] = 123 + a[wp.int16(1)] = 123 + a[wp.uint32(2)] = 123 + a[wp.int64(3)] = 123 + + wp.atomic_add(a, wp.uint8(0), 123) + wp.atomic_sub(a, wp.int16(1), 123) + # wp.atomic_min(a, wp.uint32(2), 123) + # wp.atomic_max(a, wp.int64(3), 123) + + +def test_fabricarray_indexing_types(test, device): + data = wp.zeros(shape=(4,), dtype=wp.int32, device=device) + iface = _create_fabric_array_interface(data, "foo", copy=True) + fa = wp.fabricarray(data=iface, attrib="foo") + wp.launch( + kernel=fa_kernel_indexing_types, + dim=1, + inputs=(fa,), + device=device, + ) + + @wp.kernel def fa_generic_sums_kernel(a: wp.fabricarrayarray(dtype=Any), sums: wp.array(dtype=Any)): i = wp.tid() @@ -945,6 +977,7 @@ def test_fabricarray_new_del(self): add_function_test(TestFabricArray, "test_fabricarray_fill_scalar", test_fabricarray_fill_scalar, devices=devices) add_function_test(TestFabricArray, "test_fabricarray_fill_vector", test_fabricarray_fill_vector, devices=devices) add_function_test(TestFabricArray, "test_fabricarray_fill_matrix", test_fabricarray_fill_matrix, devices=devices) +add_function_test(TestFabricArray, "test_fabricarray_indexing_types", test_fabricarray_indexing_types, devices=devices) # fabric arrays of arrays add_function_test(TestFabricArray, "test_fabricarrayarray", test_fabricarrayarray, devices=devices) diff --git a/warp/tests/test_fem.py b/warp/tests/test_fem.py index 63e3cde9..e8e96ece 100644 --- a/warp/tests/test_fem.py +++ b/warp/tests/test_fem.py @@ -28,6 +28,9 @@ ) from warp.tests.unittest_utils import * +vec6f = wp.vec(length=6, dtype=float) +mat66f = wp.mat(shape=(6, 6), dtype=float) + @integrand def linear_form(s: Sample, u: Field): @@ -1507,7 +1510,7 @@ def test_implicit_fields(test, device): @wp.kernel def test_qr_eigenvalues(): - tol = 1.0e-6 + tol = 1.0e-8 # zero Zero = wp.mat33(0.0) @@ -1546,6 +1549,19 @@ def test_qr_eigenvalues(): Err4 = wp.transpose(P4) * wp.diag(D4) * P4 - Rank4 wp.expect_near(wp.ddot(Err4, Err4), 0.0, tol) + # test robustness to low requested tolerance + Rank6 = mat66f( + vec6f(0.00171076, 0.0, 0.0, 0.0, 0.0, 0.0), + vec6f(0.0, 0.00169935, 6.14367e-06, -3.52589e-05, 3.02397e-05, -1.53458e-11), + vec6f(0.0, 6.14368e-06, 0.00172217, 2.03568e-05, 1.74589e-05, -2.92627e-05), + vec6f(0.0, -3.52589e-05, 2.03568e-05, 0.00172178, 2.53422e-05, 3.02397e-05), + vec6f(0.0, 3.02397e-05, 1.74589e-05, 2.53422e-05, 0.00171114, 3.52589e-05), + vec6f(0.0, 6.42993e-12, -2.92627e-05, 3.02397e-05, 3.52589e-05, 0.00169935), + ) + D6, P6 = symmetric_eigenvalues_qr(Rank6, 0.0) + Err6 = wp.transpose(P6) * wp.diag(D6) * P6 - Rank6 + wp.expect_near(wp.ddot(Err6, Err6), 0.0, 1.0e-13) + @wp.kernel def test_qr_inverse(): diff --git a/warp/tests/test_func.py b/warp/tests/test_func.py index 495e0a9c..631fe769 100644 --- a/warp/tests/test_func.py +++ b/warp/tests/test_func.py @@ -7,7 +7,7 @@ import math import unittest -from typing import Tuple +from typing import Any, Tuple import numpy as np @@ -191,6 +191,37 @@ def test_user_func_return_multiple_values(): wp.expect_eq(b, 54756.0) +@wp.func +def user_func_overload( + b: wp.array(dtype=Any), + i: int, +): + return b[i] * 2.0 + + +@wp.kernel +def user_func_overload_resolution_kernel( + a: wp.array(dtype=Any), + b: wp.array(dtype=Any), +): + i = wp.tid() + a[i] = user_func_overload(b, i) + + +def test_user_func_overload_resolution(test, device): + a0 = wp.array((1, 2, 3), dtype=wp.vec3) + b0 = wp.array((2, 3, 4), dtype=wp.vec3) + + a1 = wp.array((5,), dtype=float) + b1 = wp.array((6,), dtype=float) + + wp.launch(user_func_overload_resolution_kernel, a0.shape, (a0, b0)) + wp.launch(user_func_overload_resolution_kernel, a1.shape, (a1, b1)) + + assert_np_equal(a0.numpy()[0], (4, 6, 8)) + assert a1.numpy()[0] == 12 + + devices = get_test_devices() @@ -375,6 +406,9 @@ def test_native_function_error_resolution(self): dim=1, devices=devices, ) +add_function_test( + TestFunc, func=test_user_func_overload_resolution, name="test_user_func_overload_resolution", devices=devices +) if __name__ == "__main__": diff --git a/warp/tests/test_generics.py b/warp/tests/test_generics.py index ed769338..1b5ab9ac 100644 --- a/warp/tests/test_generics.py +++ b/warp/tests/test_generics.py @@ -522,6 +522,57 @@ def kernel(): ) +@wp.func +def vec_int_annotation_func(v: wp.vec(3, wp.Int)) -> wp.Int: + return v[0] + v[1] + v[2] + + +@wp.func +def vec_float_annotation_func(v: wp.vec(3, wp.Float)) -> wp.Float: + return v[0] + v[1] + v[2] + + +@wp.func +def vec_scalar_annotation_func(v: wp.vec(3, wp.Scalar)) -> wp.Scalar: + return v[0] + v[1] + v[2] + + +@wp.func +def mat_int_annotation_func(m: wp.mat((2, 2), wp.Int)) -> wp.Int: + return m[0, 0] + m[0, 1] + m[1, 0] + m[1, 1] + + +@wp.func +def mat_float_annotation_func(m: wp.mat((2, 2), wp.Float)) -> wp.Float: + return m[0, 0] + m[0, 1] + m[1, 0] + m[1, 1] + + +@wp.func +def mat_scalar_annotation_func(m: wp.mat((2, 2), wp.Scalar)) -> wp.Scalar: + return m[0, 0] + m[0, 1] + m[1, 0] + m[1, 1] + + +mat22s = wp.mat((2, 2), wp.int16) +mat22d = wp.mat((2, 2), wp.float64) + + +@wp.kernel +def test_annotations_kernel(): + vi16 = wp.vec3s(wp.int16(1), wp.int16(2), wp.int16(3)) + vf64 = wp.vec3d(wp.float64(1), wp.float64(2), wp.float64(3)) + wp.expect_eq(vec_int_annotation_func(vi16), wp.int16(6)) + wp.expect_eq(vec_float_annotation_func(vf64), wp.float64(6)) + wp.expect_eq(vec_scalar_annotation_func(vi16), wp.int16(6)) + wp.expect_eq(vec_scalar_annotation_func(vf64), wp.float64(6)) + + mi16 = mat22s(wp.int16(1), wp.int16(2), wp.int16(3), wp.int16(4)) + mf64 = mat22d(wp.float64(1), wp.float64(2), wp.float64(3), wp.float64(4)) + wp.expect_eq(mat_int_annotation_func(mi16), wp.int16(10)) + wp.expect_eq(mat_float_annotation_func(mf64), wp.float64(10)) + wp.expect_eq(mat_scalar_annotation_func(mi16), wp.int16(10)) + wp.expect_eq(mat_scalar_annotation_func(mf64), wp.float64(10)) + + class TestGenerics(unittest.TestCase): pass @@ -590,6 +641,7 @@ class TestGenerics(unittest.TestCase): ) add_function_test(TestGenerics, "test_type_operator_misspell", test_type_operator_misspell, devices=devices) add_function_test(TestGenerics, "test_type_attribute_error", test_type_attribute_error, devices=devices) +add_kernel_test(TestGenerics, name="test_annotations_kernel", kernel=test_annotations_kernel, dim=1, devices=devices) if __name__ == "__main__": wp.clear_kernel_cache() diff --git a/warp/tests/test_iter.py b/warp/tests/test_iter.py new file mode 100644 index 00000000..32a066b4 --- /dev/null +++ b/warp/tests/test_iter.py @@ -0,0 +1,68 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +import unittest + +import warp as wp +from warp.tests.unittest_utils import * + + +@wp.kernel +def reversed_kernel( + start: wp.int32, + end: wp.int32, + step: wp.int32, + out_count: wp.array(dtype=wp.int32), + out_values: wp.array(dtype=wp.int32), +): + count = wp.int32(0) + for i in reversed(range(start, end, step)): + out_values[count] = i + count += 1 + + out_count[0] = count + + +def test_reversed(test, device): + count = wp.empty(1, dtype=wp.int32) + values = wp.empty(32, dtype=wp.int32) + + start, end, step = (-2, 8, 3) + wp.launch( + reversed_kernel, + dim=1, + inputs=(start, end, step), + outputs=(count, values), + ) + expected = tuple(reversed(range(start, end, step))) + assert count.numpy()[0] == len(expected) + assert_np_equal(values.numpy()[: len(expected)], expected) + + start, end, step = (9, -3, -2) + wp.launch( + reversed_kernel, + dim=1, + inputs=(start, end, step), + outputs=(count, values), + ) + expected = tuple(reversed(range(start, end, step))) + assert count.numpy()[0] == len(expected) + assert_np_equal(values.numpy()[: len(expected)], expected) + + +devices = get_test_devices() + + +class TestIter(unittest.TestCase): + pass + + +add_function_test(TestIter, "test_reversed", test_reversed, devices=devices) + +if __name__ == "__main__": + wp.clear_kernel_cache() + unittest.main(verbosity=2) diff --git a/warp/tests/test_model.py b/warp/tests/test_model.py index dde81889..da872a6c 100644 --- a/warp/tests/test_model.py +++ b/warp/tests/test_model.py @@ -157,6 +157,19 @@ def add_three_cubes(builder: ModelBuilder, parent_body=-1): assert builder.body_mass == [1.0, 4.0] assert builder.body_inv_mass == [1.0, 0.25] + # create another builder, test add_builder function + builder2 = ModelBuilder() + builder2.add_builder(builder) + assert builder2.articulation_count == builder.articulation_count + assert builder2.joint_count == builder.joint_count + assert builder2.body_count == builder.body_count + assert builder2.shape_count == builder.shape_count + assert builder2.articulation_start == builder.articulation_start + # add the same builder again + builder2.add_builder(builder) + assert builder2.articulation_count == 2 * builder.articulation_count + assert builder2.articulation_start == [0, 1, 2, 3] + if __name__ == "__main__": wp.clear_kernel_cache() diff --git a/warp/tests/test_print.py b/warp/tests/test_print.py index 542db95b..e5431684 100644 --- a/warp/tests/test_print.py +++ b/warp/tests/test_print.py @@ -7,6 +7,7 @@ import sys import unittest +from typing import Any import warp as wp from warp.tests.unittest_utils import * @@ -126,6 +127,139 @@ def test_print_boolean(test, device): test.assertRegex(s, rf"True{os.linesep}False{os.linesep}") +@wp.kernel +def generic_print_kernel(x: Any): + print(x) + + +@wp.struct +class SimpleStruct: + x: float + y: float + + +generic_print_types = [*wp.types.scalar_types] +for scalar_type in wp.types.scalar_types: + generic_print_types.append(wp.types.vector(2, scalar_type)) + generic_print_types.append(wp.types.vector(3, scalar_type)) + generic_print_types.append(wp.types.vector(4, scalar_type)) + generic_print_types.append(wp.types.matrix((2, 2), scalar_type)) + generic_print_types.append(wp.types.matrix((3, 3), scalar_type)) + generic_print_types.append(wp.types.matrix((4, 4), scalar_type)) +generic_print_types.append(wp.bool) +generic_print_types.append(SimpleStruct) +generic_print_types.append(wp.array(dtype=float)) + +for T in generic_print_types: + wp.overload(generic_print_kernel, [T]) + + +def test_print_adjoint(test, device): + for scalar_type in wp.types.scalar_types: + # scalar + capture = StdOutCapture() + capture.begin() + wp.launch( + generic_print_kernel, + dim=1, + inputs=[scalar_type(17)], + adj_inputs=[scalar_type(42)], + adjoint=True, + device=device, + ) + wp.synchronize_device(device) + s = capture.end() + + # We skip the win32 comparison for now since the capture sometimes is an empty string + if sys.platform != "win32": + test.assertRegex(s, rf"17{os.linesep}adj: 42{os.linesep}") + + for dim in (2, 3, 4): + # vector + vec_type = wp.types.vector(dim, scalar_type) + vec_data = np.arange(vec_type._length_, dtype=wp.dtype_to_numpy(scalar_type)) + v = vec_type(vec_data) + adj_v = vec_type(vec_data[::-1]) + + capture = StdOutCapture() + capture.begin() + wp.launch(generic_print_kernel, dim=1, inputs=[v], adj_inputs=[adj_v], adjoint=True, device=device) + wp.synchronize_device(device) + s = capture.end() + + # We skip the win32 comparison for now since the capture sometimes is an empty string + if sys.platform != "win32": + expected_forward = " ".join(str(int(x)) for x in v) + " " + expected_adjoint = " ".join(str(int(x)) for x in adj_v) + test.assertRegex(s, rf"{expected_forward}{os.linesep}adj: {expected_adjoint}{os.linesep}") + + # matrix + mat_type = wp.types.matrix((dim, dim), scalar_type) + mat_data = np.arange(mat_type._length_, dtype=wp.dtype_to_numpy(scalar_type)) + m = mat_type(mat_data) + adj_m = mat_type(mat_data[::-1]) + + capture = StdOutCapture() + capture.begin() + wp.launch(generic_print_kernel, dim=1, inputs=[m], adj_inputs=[adj_m], adjoint=True, device=device) + wp.synchronize_device(device) + s = capture.end() + + # We skip the win32 comparison for now since the capture sometimes is an empty string + if sys.platform != "win32": + expected_forward = "" + expected_adjoint = "" + for row in range(dim): + if row == 0: + adj_prefix = "adj: " + else: + adj_prefix = " " + expected_forward += " ".join(str(int(x)) for x in m[row]) + f" {os.linesep}" + expected_adjoint += adj_prefix + " ".join(str(int(x)) for x in adj_m[row]) + f"{os.linesep}" + test.assertRegex(s, rf"{expected_forward}{expected_adjoint}") + + # Booleans + capture = StdOutCapture() + capture.begin() + wp.launch(generic_print_kernel, dim=1, inputs=[True], adj_inputs=[False], adjoint=True, device=device) + wp.synchronize_device(device) + s = capture.end() + + # We skip the win32 comparison for now since the capture sometimes is an empty string + if sys.platform != "win32": + test.assertRegex(s, rf"True{os.linesep}adj: False{os.linesep}") + + # structs, not printable yet + capture = StdOutCapture() + capture.begin() + wp.launch( + generic_print_kernel, dim=1, inputs=[SimpleStruct()], adj_inputs=[SimpleStruct()], adjoint=True, device=device + ) + wp.synchronize_device(device) + s = capture.end() + + # We skip the win32 comparison for now since the capture sometimes is an empty string + if sys.platform != "win32": + test.assertRegex( + s, rf"{os.linesep}adj: {os.linesep}" + ) + + # arrays, not printable + capture = StdOutCapture() + capture.begin() + a = wp.ones(10, dtype=float, device=device) + adj_a = wp.zeros(10, dtype=float, device=device) + wp.launch(generic_print_kernel, dim=1, inputs=[a], adj_inputs=[adj_a], adjoint=True, device=device) + wp.synchronize_device(device) + s = capture.end() + + # We skip the win32 comparison for now since the capture sometimes is an empty string + if sys.platform != "win32": + test.assertRegex( + s, rf"{os.linesep}adj: {os.linesep}" + ) + + class TestPrint(unittest.TestCase): pass @@ -134,6 +268,7 @@ class TestPrint(unittest.TestCase): add_function_test(TestPrint, "test_print", test_print, devices=devices, check_output=False) add_function_test(TestPrint, "test_print_numeric", test_print_numeric, devices=devices, check_output=False) add_function_test(TestPrint, "test_print_boolean", test_print_boolean, devices=devices, check_output=False) +add_function_test(TestPrint, "test_print_adjoint", test_print_adjoint, devices=devices, check_output=False) if __name__ == "__main__": diff --git a/warp/tests/test_static.py b/warp/tests/test_static.py index d816af4f..9e3f7393 100644 --- a/warp/tests/test_static.py +++ b/warp/tests/test_static.py @@ -5,6 +5,8 @@ # distribution of this software and related documentation without an express # license agreement from NVIDIA CORPORATION is strictly prohibited. +import importlib +import tempfile import unittest from typing import Dict, List @@ -17,6 +19,23 @@ global_variable = 3 +def load_code_as_module(code, name): + file, file_path = tempfile.mkstemp(suffix=".py") + + try: + with os.fdopen(file, "w") as f: + f.write(code) + + spec = importlib.util.spec_from_file_location(name, file_path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + finally: + os.remove(file_path) + + # return Warp module + return wp.get_module(module.__name__) + + @wp.func def static_global_variable_func(): static_var = warp.static(global_variable + 2) @@ -234,7 +253,7 @@ def function_variable_kernel(results: wp.array(dtype=int)): results[0] = wp.static(func)(3, 2) # noqa: B023 results = wp.zeros(1, dtype=int, device=device) - # note that the kernel has to be recompiled everytime the value of func changes + # note that the kernel has to be recompiled every time the value of func changes wp.launch(function_variable_kernel, 1, [results], device=device) assert_np_equal(results.numpy(), np.array([func(3, 2)], dtype=int)) @@ -383,6 +402,140 @@ def static_condition3(results: wp.array(dtype=int)): assert_np_equal(counts["else"], 0) +static_builtin_constant_template = """ +import warp as wp + +# Python builtin literal like 17, 42.0, or True +C = {value} + +@wp.kernel +def k(): + print(wp.static(C)) +""" + +static_warp_constant_template = """ +import warp as wp + +# Warp scalar value like wp.uint8(17) +C = wp.{dtype}({value}) + +@wp.kernel +def k(): + print(wp.static(C)) +""" + +static_struct_constant_template = """ +import warp as wp + +@wp.struct +class SimpleStruct: + x: float + +C = SimpleStruct() +C.x = {value} + +@wp.kernel +def k(): + print(wp.static(C)) +""" + +static_func_template = """ +import warp as wp + +@wp.func +def f(): + # modify the function to verify hashing + return {value} + +@wp.kernel +def k(): + print(wp.static(f)()) +""" + + +def test_static_constant_hash(test, _): + # Python literals + # (type, value1, value2) + literals = [ + (int, 17, 42), + (float, 17.5, 42.5), + (bool, True, False), + ] + + for builtin_type, value1, value2 in literals: + type_name = builtin_type.__name__ + with test.subTest(msg=f"{type_name}"): + source1 = static_builtin_constant_template.format(value=value1) + source2 = static_builtin_constant_template.format(value=value2) + source3 = static_builtin_constant_template.format(value=value1) + + module1 = load_code_as_module(source1, f"aux_static_constant_builtin_{type_name}_1") + module2 = load_code_as_module(source2, f"aux_static_constant_builtin_{type_name}_2") + module3 = load_code_as_module(source3, f"aux_static_constant_builtin_{type_name}_3") + + hash1 = module1.hash_module() + hash2 = module2.hash_module() + hash3 = module3.hash_module() + + test.assertNotEqual(hash1, hash2) + test.assertEqual(hash1, hash3) + + # Warp types (scalars, vectors, matrices) + for warp_type in [*wp.types.scalar_types, *wp.types.vector_types]: + type_name = warp_type.__name__ + with test.subTest(msg=f"wp.{type_name}"): + value1 = ", ".join([str(17)] * warp_type._length_) + value2 = ", ".join([str(42)] * warp_type._length_) + source1 = static_warp_constant_template.format(dtype=type_name, value=value1) + source2 = static_warp_constant_template.format(dtype=type_name, value=value2) + source3 = static_warp_constant_template.format(dtype=type_name, value=value1) + + module1 = load_code_as_module(source1, f"aux_static_constant_wp_{type_name}_1") + module2 = load_code_as_module(source2, f"aux_static_constant_wp_{type_name}_2") + module3 = load_code_as_module(source3, f"aux_static_constant_wp_{type_name}_3") + + hash1 = module1.hash_module() + hash2 = module2.hash_module() + hash3 = module3.hash_module() + + test.assertNotEqual(hash1, hash2) + test.assertEqual(hash1, hash3) + + # structs + with test.subTest(msg="struct"): + source1 = static_struct_constant_template.format(value=17) + source2 = static_struct_constant_template.format(value=42) + source3 = static_struct_constant_template.format(value=17) + + module1 = load_code_as_module(source1, "aux_static_constant_struct_1") + module2 = load_code_as_module(source2, "aux_static_constant_struct_2") + module3 = load_code_as_module(source3, "aux_static_constant_struct_3") + + hash1 = module1.hash_module() + hash2 = module2.hash_module() + hash3 = module3.hash_module() + + test.assertNotEqual(hash1, hash2) + test.assertEqual(hash1, hash3) + + +def test_static_function_hash(test, _): + source1 = static_func_template.format(value=17) + source2 = static_func_template.format(value=42) + source3 = static_func_template.format(value=17) + + module1 = load_code_as_module(source1, "aux_static_func1") + module2 = load_code_as_module(source2, "aux_static_func2") + module3 = load_code_as_module(source3, "aux_static_func3") + + hash1 = module1.hash_module() + hash2 = module2.hash_module() + hash3 = module3.hash_module() + + test.assertNotEqual(hash1, hash2) + test.assertEqual(hash1, hash3) + + devices = get_test_devices() @@ -406,6 +559,9 @@ def test_static_python_call(self): add_function_test(TestStatic, "test_static_for_loop", test_static_for_loop, devices=devices) add_function_test(TestStatic, "test_static_if_else_elif", test_static_if_else_elif, devices=devices) +add_function_test(TestStatic, "test_static_constant_hash", test_static_constant_hash, devices=None) +add_function_test(TestStatic, "test_static_function_hash", test_static_function_hash, devices=None) + if __name__ == "__main__": wp.clear_kernel_cache() diff --git a/warp/tests/unittest_suites.py b/warp/tests/unittest_suites.py index 26ccf9a4..2d76557f 100644 --- a/warp/tests/unittest_suites.py +++ b/warp/tests/unittest_suites.py @@ -170,6 +170,7 @@ def default_suite(test_loader: unittest.TestLoader = unittest.defaultTestLoader) from warp.tests.test_sparse import TestSparse from warp.tests.test_spatial import TestSpatial from warp.tests.test_special_values import TestSpecialValues + from warp.tests.test_static import TestStatic from warp.tests.test_streams import TestStreams from warp.tests.test_struct import TestStruct from warp.tests.test_tape import TestTape @@ -269,6 +270,7 @@ def default_suite(test_loader: unittest.TestLoader = unittest.defaultTestLoader) TestSparse, TestSpatial, TestSpecialValues, + TestStatic, TestStreams, TestStruct, TestTape, @@ -329,6 +331,7 @@ def kit_suite(test_loader: unittest.TestLoader = unittest.defaultTestLoader): from warp.tests.test_rounding import TestRounding from warp.tests.test_runlength_encode import TestRunlengthEncode from warp.tests.test_sparse import TestSparse + from warp.tests.test_static import TestStatic from warp.tests.test_streams import TestStreams from warp.tests.test_tape import TestTape from warp.tests.test_transient_module import TestTransientModule @@ -374,6 +377,7 @@ def kit_suite(test_loader: unittest.TestLoader = unittest.defaultTestLoader): TestRounding, TestRunlengthEncode, TestSparse, + TestStatic, TestStreams, TestTape, TestTransientModule, diff --git a/warp/types.py b/warp/types.py index ea9604e4..e9722b1f 100644 --- a/warp/types.py +++ b/warp/types.py @@ -100,8 +100,10 @@ class vec_t(ctypes.Array): if dtype is bool: _type_ = ctypes.c_bool - elif dtype in [Scalar, Float]: + elif dtype in (Scalar, Float): _type_ = ctypes.c_float + elif dtype is Int: + _type_ = ctypes.c_int else: _type_ = dtype._type_ @@ -289,8 +291,10 @@ class mat_t(ctypes.Array): if dtype is bool: _type_ = ctypes.c_bool - elif dtype in [Scalar, Float]: + elif dtype in (Scalar, Float): _type_ = ctypes.c_float + elif dtype is Int: + _type_ = ctypes.c_int else: _type_ = dtype._type_ @@ -1490,7 +1494,11 @@ def types_equal(a, b, match_generic=False): return True - if is_array(a) and type(a) is type(b): + if is_array(a) and type(a) is type(b) and types_equal(a.dtype, b.dtype, match_generic=match_generic): + return True + + # match NewStructInstance and Struct dtype + if getattr(a, "cls", "a") is getattr(b, "cls", "b"): return True # match NewStructInstance and Struct dtype @@ -2266,13 +2274,22 @@ def grad(self, grad): self._requires_grad = False else: # make sure the given gradient array is compatible - if ( - grad.dtype != self.dtype - or grad.shape != self.shape - or grad.strides != self.strides - or grad.device != self.device - ): - raise ValueError("The given gradient array is incompatible") + if grad.dtype != self.dtype: + raise ValueError( + f"The given gradient array is incompatible: expected dtype {self.dtype}, got {grad.dtype}" + ) + if grad.shape != self.shape: + raise ValueError( + f"The given gradient array is incompatible: expected shape {self.shape}, got {grad.shape}" + ) + if grad.device != self.device: + raise ValueError( + f"The given gradient array is incompatible: expected device {self.device}, got {grad.device}" + ) + if grad.strides != self.strides: + raise ValueError( + f"The given gradient array is incompatible: expected strides {self.strides}, got {grad.strides}" + ) self._grad = grad self._requires_grad = True From 96b0d0adc5e764beefe1ddc974121415b6dd1233 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 29 Oct 2024 14:28:45 -0700 Subject: [PATCH 088/102] Fix issues with tile print --- warp/native/tile.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index 6d164d7f..dad774ec 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -768,18 +768,29 @@ void tile_register_t::print() WP_TILE_SYNC(); } -template -inline CUDA_CALLABLE void print(Tile& t) +template +inline CUDA_CALLABLE void print(const tile_register_t& t) { t.print(); } -template -inline CUDA_CALLABLE void adj_print(Tile& t, AdjTile& a) +template +inline CUDA_CALLABLE void adj_print(const tile_register_t& t, const tile_register_t& a) { a.print(); } +template +inline CUDA_CALLABLE void print(const tile_shared_t& t) +{ + t.print(); +} + +template +inline CUDA_CALLABLE void adj_print(const tile_shared_t& t, const tile_shared_t& a) +{ + a.print(); +} // helpers to allocate shared tiles template From 246c9e9fb7567282470fa13c8322e5763d2470a2 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 29 Oct 2024 14:54:35 -0700 Subject: [PATCH 089/102] Experiment with mathdx support pipeline --- .gitlab-ci.yml | 54 ++++----- .gitlab/ci/cuda-11-build-and-test.yml | 2 +- .gitlab/ci/debug-build-and-test.yml | 2 +- .gitlab/ci/mathdx-support.yml | 167 -------------------------- 4 files changed, 26 insertions(+), 199 deletions(-) delete mode 100644 .gitlab/ci/mathdx-support.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 566a12bc..2135f7c2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -62,15 +62,23 @@ stages: linux-aarch64 build: stage: build - image: ubuntu:22.04 + image: ubuntu:20.04 extends: - .save_warp_bin_artifact before_script: - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - apt-get update && apt-get install build-essential curl --no-install-recommends -y + - > + curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/96/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz + -o libmathdx.tar.gz + - mkdir -p _build/target-deps + - tar -xzf libmathdx.tar.gz -C _build/target-deps + - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" + - gcc --version - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - - ./tools/ci/building/build-linux-aarch64/build.sh --no-docker # We are already using the builder image + - ./tools/ci/building/build-linux-aarch64/build.sh --no-docker - mkdir -p warp/bin/linux-aarch64 - mv warp/bin/warp.so warp/bin/linux-aarch64 - mv warp/bin/warp-clang.so warp/bin/linux-aarch64 @@ -79,12 +87,24 @@ linux-aarch64 build: linux-x86_64 build: stage: build - image: urm.nvidia.com/ct-omniverse-docker/centos7-gcc10-builder:3.2.0 + image: ubuntu:20.04 extends: - .save_warp_bin_artifact - .runner-build-linux-x86_64 + before_script: + - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" + - apt-get update && apt-get install build-essential curl --no-install-recommends -y + - > + curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/96/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz + -o libmathdx.tar.gz + - mkdir -p _build/target-deps + - tar -xzf libmathdx.tar.gz -C _build/target-deps + - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" + - gcc --version + - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" script: - - ./tools/ci/building/build-linux-x86_64/build.sh --no-docker # We are already using the builder image + - ./tools/ci/building/build-linux-x86_64/build.sh --no-docker - mkdir -p warp/bin/linux-x86_64 - mv warp/bin/warp.so warp/bin/linux-x86_64 - mv warp/bin/warp-clang.so warp/bin/linux-x86_64 @@ -502,32 +522,6 @@ debug build and test: extends: - .trigger_common -trigger mathdx support pipeline: - stage: test - image: busybox - extends: - - .runner-utility-linux-x86_64 - needs: [] - rules: - - if: $CI_PIPELINE_SOURCE == "schedule" - - if: $CI_COMMIT_TAG - - if: $CI_COMMIT_BRANCH =~ /^release-.*/ - - when: manual # Can be triggered in all other scenarios - allow_failure: true - variables: - GIT_STRATEGY: none - script: - - echo "Run this job to test Warp compiled with mathdx support." - -# Uses the same Python version as the main pipeline. -mathdx support: - stage: child pipelines - needs: [trigger mathdx support pipeline] - trigger: - include: /.gitlab/ci/mathdx-support.yml - extends: - - .trigger_common - # Trigger CUDA 11 pipelines # Workaround from https://gitlab.com/gitlab-org/gitlab/-/issues/284086 trigger cuda 11 pipeline: diff --git a/.gitlab/ci/cuda-11-build-and-test.yml b/.gitlab/ci/cuda-11-build-and-test.yml index 3f5cd25d..2276aafd 100644 --- a/.gitlab/ci/cuda-11-build-and-test.yml +++ b/.gitlab/ci/cuda-11-build-and-test.yml @@ -45,7 +45,7 @@ stages: linux-aarch64 build: stage: build - image: ubuntu:22.04 + image: ubuntu:20.04 extends: - .save_warp_bin_artifact before_script: diff --git a/.gitlab/ci/debug-build-and-test.yml b/.gitlab/ci/debug-build-and-test.yml index d028af2e..ca389d9d 100644 --- a/.gitlab/ci/debug-build-and-test.yml +++ b/.gitlab/ci/debug-build-and-test.yml @@ -35,7 +35,7 @@ stages: # Hide this job for now until debug aarch64 builds work .linux-aarch64 build: stage: build - image: ubuntu:22.04 + image: ubuntu:20.04 extends: - .save_warp_bin_artifact before_script: diff --git a/.gitlab/ci/mathdx-support.yml b/.gitlab/ci/mathdx-support.yml deleted file mode 100644 index bc711297..00000000 --- a/.gitlab/ci/mathdx-support.yml +++ /dev/null @@ -1,167 +0,0 @@ -# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. -# NVIDIA CORPORATION and its licensors retain all intellectual property -# and proprietary rights in and to this software, related documentation -# and any modifications thereto. Any use, reproduction, disclosure or -# distribution of this software and related documentation without an express -# license agreement from NVIDIA CORPORATION is strictly prohibited. - -# ============================================================================== -# CI/CD Pipeline Configuration -# ============================================================================== - -include: /.gitlab/ci/common.yml - -workflow: - rules: - - if: $CI_PIPELINE_SOURCE == "parent_pipeline" - -stages: - - build - - test - - package - - deploy - -# ============================================================================== -# Build Jobs (Release) -# ============================================================================== - -linux-x86_64 build: - stage: build - image: ubuntu:20.04 - extends: - - .save_warp_bin_artifact - - .runner-build-linux-x86_64 - before_script: - - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - - apt-get update && apt-get install build-essential curl --no-install-recommends -y - - > - curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/93/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz - -o libmathdx.tar.gz - - mkdir -p _build/target-deps - - tar -xzf libmathdx.tar.gz -C _build/target-deps - - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" - - gcc --version - - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" - script: - - ./tools/ci/building/build-linux-x86_64/build.sh --no-docker # We are already using the builder image - - mkdir -p warp/bin/linux-x86_64 - - mv warp/bin/warp.so warp/bin/linux-x86_64 - - mv warp/bin/warp-clang.so warp/bin/linux-x86_64 - -linux-aarch64 build: - stage: build - image: ubuntu:20.04 - extends: - - .save_warp_bin_artifact - before_script: - - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - - apt-get update && apt-get install build-essential curl --no-install-recommends -y - - > - curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/93/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz - -o libmathdx.tar.gz - - mkdir -p _build/target-deps - - tar -xzf libmathdx.tar.gz -C _build/target-deps - - export LIBMATHDX_HOME="$CI_PROJECT_DIR/_build/target-deps/libmathdx-0.0.1-Linux" - - gcc --version - - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" - script: - - ./tools/ci/building/build-linux-x86_64/build.sh --no-docker # We are already using the builder image - - mkdir -p warp/bin/linux-aarch64 - - mv warp/bin/warp.so warp/bin/linux-aarch64 - - mv warp/bin/warp-clang.so warp/bin/linux-aarch64 - tags: - - arch/arm - -# ============================================================================== -# Unit Testing Jobs (MathDx Support) -# -# Unlike the main testing jobs defined in /.gitlab-ci.yml, the jobs don't -# generate code coverage reports. -# ============================================================================== - -linux-x86_64 test: - stage: test - needs: [linux-x86_64 build] - extends: - - .omni_nvks_gpu_2x - - .save_test_report_artifact - before_script: - - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - - df -h - # Move compiled binaries out of platform-specific directory - - mv warp/bin/linux-x86_64/warp.so warp/bin/ - - mv warp/bin/linux-x86_64/warp-clang.so warp/bin/ - - tools/packman/packman install -l _build/target-deps/python python ${DEFAULT_PYTHON}-linux-x86_64 - - export PATH="$CUDA_BIN:$PATH" - - $PYTHON -m venv _venv - - source _venv/bin/activate - - python -m pip install --upgrade pip - - python -m pip install --upgrade usd-core - - python -m pip install --upgrade torch --extra-index-url https://download.pytorch.org/whl/cu121 - - python -m pip install -U "jax[cuda12]" - - python -m pip install -e . - - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" - # HACK: disable P2P tests due to misbehaving agents - - export WARP_DISABLE_P2P_TESTS=1 - script: - - python -m warp.tests --junit-report-xml rspec.xml -s autodetect --failfast - -linux-aarch64 test jetson: - image: ubuntu:22.04 - needs: [linux-aarch64 build] - extends: - - .save_test_report_artifact - before_script: - - echo -e "\\e[0Ksection_start:`date +%s`:install_dependencies[collapsed=true]\\r\\e[0KInstalling dependencies" - - !reference [.snippets, install-python+warp-aarch64] - - python -m pip install -U "jax[cuda12]" - - echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" - script: - - python -m warp.tests --junit-report-xml rspec.xml -s autodetect --failfast - tags: - - gpu/orin - -# ============================================================================== -# Packaging Jobs -# ============================================================================== - -# Creates wheel files for PyPI -create pypi wheels: - stage: package - needs: - - linux-aarch64 build - - linux-x86_64 build - extends: - - .runner-utility-linux-x86_64 - before_script: - - python3 -m pip install --upgrade pip - - python3 -m pip install build - script: - - sed -i "s/^\(.*\)$/\1+tile/" VERSION.md # Modify VERSION.md with +tile - - python3 -m build --wheel -C--build-option=-Plinux-x86_64 - - python3 -m build --wheel -C--build-option=-Plinux-aarch64 - - find . -type f -exec chmod 664 {} + - - find . -type d -exec chmod 775 {} + - artifacts: - name: $CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA - expose_as: "Python Wheels MathDx" - paths: - - "dist/" - when: always - -publish wheels to gitlab pypi registry: - stage: deploy - image: python:3.11-slim - needs: ["create pypi wheels"] - extends: - - .runner-utility-linux-x86_64 - rules: - - when: manual - allow_failure: true - before_script: - - python3 -m pip install --upgrade pip - - python3 -m pip install --upgrade build twine - script: - - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python3 -m twine upload --verbose --skip-existing --non-interactive --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/* From 548cb9f1356b687a59afeeef8143bdcae65f224c Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Tue, 29 Oct 2024 21:07:25 -0700 Subject: [PATCH 090/102] Fix merge issue that broke test_tile_mlp --- warp/codegen.py | 14 -------------- warp/tests/test_tile_mlp.py | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/warp/codegen.py b/warp/codegen.py index 000ea4d5..51c98c72 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -1340,10 +1340,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): if return_type is None: # handles expression (zero output) functions, e.g.: void do_something(); - - output = None - output_list = [] - forward_call = ( f"{func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" ) @@ -1353,12 +1349,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): elif not isinstance(return_type, Sequence) or len(return_type) == 1: # handle simple function (one output) - - if isinstance(return_type, Sequence): - return_type = return_type[0] - output = adj.add_var(return_type) - output_list = [output] - forward_call = f"var_{output} = {func.namespace}{func_name}({adj.format_forward_call_args(fwd_args, use_initializer_list)});" replay_call = forward_call if func.custom_replay_func is not None: @@ -1366,10 +1356,6 @@ def add_call(adj, func, args, kwargs, type_args, min_outputs=None): else: # handle multiple value functions - - output = [adj.add_var(v) for v in return_type] - output_list = output - forward_call = ( f"{func.namespace}{func_name}({adj.format_forward_call_args(fwd_args + output, use_initializer_list)});" ) diff --git a/warp/tests/test_tile_mlp.py b/warp/tests/test_tile_mlp.py index 89fcf052..9ae760f4 100644 --- a/warp/tests/test_tile_mlp.py +++ b/warp/tests/test_tile_mlp.py @@ -391,5 +391,5 @@ class TestTileMLP(unittest.TestCase): if __name__ == "__main__": - # wp.clear_kernel_cache() + wp.clear_kernel_cache() unittest.main(verbosity=2, failfast=True) From 4d6444bd51cbab7f09c656f710f8be50a86ba646 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 12:26:01 -0700 Subject: [PATCH 091/102] Relocate tile examples --- {examples => warp/examples/tile}/tile_fft.py | 0 {examples => warp/examples/tile}/tile_matmul.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {examples => warp/examples/tile}/tile_fft.py (100%) rename {examples => warp/examples/tile}/tile_matmul.py (100%) diff --git a/examples/tile_fft.py b/warp/examples/tile/tile_fft.py similarity index 100% rename from examples/tile_fft.py rename to warp/examples/tile/tile_fft.py diff --git a/examples/tile_matmul.py b/warp/examples/tile/tile_matmul.py similarity index 100% rename from examples/tile_matmul.py rename to warp/examples/tile/tile_matmul.py From b7962f824b6433f71bdba89670725e0ceba9b13d Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 12:53:52 -0700 Subject: [PATCH 092/102] Add license for Pixel the Cat image --- licenses/assets/pixel-LICENSE.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 licenses/assets/pixel-LICENSE.txt diff --git a/licenses/assets/pixel-LICENSE.txt b/licenses/assets/pixel-LICENSE.txt new file mode 100644 index 00000000..b01f22c5 --- /dev/null +++ b/licenses/assets/pixel-LICENSE.txt @@ -0,0 +1,3 @@ +Pixel the Cat (pixel.jpg) (c) 2020 by Alison Wawrzyniak is licensed under CC BY 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/ + +Resized from original. From 24960d7b873e602233db7bd4f42236cd1b843d0b Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 12:55:33 -0700 Subject: [PATCH 093/102] Fix CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fae03b44..24987e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [1.4.0] - 2024-10-01 +## [Unreleased] - 2024-?? ### Added From 91f634ab1960ca5e842ee30037e7f529d0602f2d Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 13:01:09 -0700 Subject: [PATCH 094/102] Don't install mathdx in build.sh --- tools/ci/building/build-linux-x86_64/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ci/building/build-linux-x86_64/build.sh b/tools/ci/building/build-linux-x86_64/build.sh index e9af605d..51940183 100755 --- a/tools/ci/building/build-linux-x86_64/build.sh +++ b/tools/ci/building/build-linux-x86_64/build.sh @@ -74,7 +74,7 @@ CUDA="$SCRIPT_DIR/../../../../_build/target-deps/cuda" # pip deps $PYTHON -m pip install --upgrade pip -$PYTHON -m pip install --upgrade numpy gitpython cmake ninja nvidia-mathdx==24.4.0 +$PYTHON -m pip install --upgrade numpy gitpython cmake ninja if [ "$GITLAB_CI" = "true" ]; then echo -e "\\e[0Ksection_end:`date +%s`:install_dependencies\\r\\e[0K" From 4ee746f1761608d06de59c40e38b1f537ce9da41 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 13:15:44 -0700 Subject: [PATCH 095/102] Merge tile_matmul examples --- warp/examples/tile/example_tile_matmul.py | 8 +++--- warp/examples/tile/tile_matmul.py | 34 ----------------------- 2 files changed, 4 insertions(+), 38 deletions(-) delete mode 100644 warp/examples/tile/tile_matmul.py diff --git a/warp/examples/tile/example_tile_matmul.py b/warp/examples/tile/example_tile_matmul.py index b8ee510c..b795b35a 100644 --- a/warp/examples/tile/example_tile_matmul.py +++ b/warp/examples/tile/example_tile_matmul.py @@ -26,11 +26,11 @@ @wp.kernel -def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.array2d(dtype=float)): +def tile_gemm(A: wp.array2d(dtype=wp.float32), B: wp.array2d(dtype=wp.float16), C: wp.array2d(dtype=wp.float64)): # output tile index i, j = wp.tid() - sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float32) + sum = wp.tile_zeros(m=TILE_M, n=TILE_N, dtype=wp.float64) _M = A.shape[0] _N = B.shape[1] @@ -58,8 +58,8 @@ def tile_gemm(A: wp.array2d(dtype=float), B: wp.array2d(dtype=float), C: wp.arra rng = np.random.default_rng(42) A = rng.random((M, K), dtype=np.float32) - B = rng.random((K, N), dtype=np.float32) - C = np.zeros((M, N), dtype=np.float32) + B = rng.random((K, N), dtype=np.float32).astype(np.float16) + C = np.zeros((M, N), dtype=np.float64) A_wp = wp.array(A, requires_grad=True) B_wp = wp.array(B, requires_grad=True) diff --git a/warp/examples/tile/tile_matmul.py b/warp/examples/tile/tile_matmul.py deleted file mode 100644 index 57b94bbc..00000000 --- a/warp/examples/tile/tile_matmul.py +++ /dev/null @@ -1,34 +0,0 @@ -import numpy as np - -import warp as wp - -wp.init() -wp.build.clear_kernel_cache() - -BLOCK_DIM = 32 -M, N, K = 4, 8, 16 - - -@wp.kernel -def matmul_tiled(ga: wp.array2d(dtype=wp.float32), gb: wp.array2d(dtype=wp.float16), gc: wp.array2d(dtype=wp.float64)): - i, j, _ = wp.tid() - a = wp.tile_load(ga, i, j, m=M, n=K) - b = wp.tile_load(gb, i, j, m=K, n=N) - c = wp.tile_zeros(m=M, n=N, dtype=wp.float64) - wp.tile_matmul(a, b, c) - wp.tile_store(gc, i, j, c) - - -A = np.ones((M, K), dtype=np.float32) -B = 3 * np.ones((K, N), dtype=np.float16) -C = np.zeros((M, N), dtype=np.float64) - -A_wp = wp.array2d(A, dtype=wp.float32) -B_wp = wp.array2d(B, dtype=wp.float16) -C_wp = wp.array2d(C, dtype=wp.float64) - -wp.launch(matmul_tiled, dim=[1, 1, BLOCK_DIM], inputs=[A_wp, B_wp, C_wp], block_dim=BLOCK_DIM) -wp.synchronize() - -print("inputs:\n", A, "\n", B) -print("output (should be = 48 * np.ones(4, 8)):\n", C_wp) From 5304a66d13105a02b1a9f9db389fd2296e8aaf6a Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 15:19:30 -0700 Subject: [PATCH 096/102] Rename example, add headers --- warp/examples/benchmarks/benchmark_tile.py | 7 +++++++ warp/examples/tile/{tile_fft.py => example_tile_fft.py} | 0 warp/examples/tile/example_tile_matmul.py | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) rename warp/examples/tile/{tile_fft.py => example_tile_fft.py} (100%) diff --git a/warp/examples/benchmarks/benchmark_tile.py b/warp/examples/benchmarks/benchmark_tile.py index 54fec3f9..051aaf1c 100644 --- a/warp/examples/benchmarks/benchmark_tile.py +++ b/warp/examples/benchmarks/benchmark_tile.py @@ -1,3 +1,10 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + import numpy as np import torch diff --git a/warp/examples/tile/tile_fft.py b/warp/examples/tile/example_tile_fft.py similarity index 100% rename from warp/examples/tile/tile_fft.py rename to warp/examples/tile/example_tile_fft.py diff --git a/warp/examples/tile/example_tile_matmul.py b/warp/examples/tile/example_tile_matmul.py index b795b35a..a275c820 100644 --- a/warp/examples/tile/example_tile_matmul.py +++ b/warp/examples/tile/example_tile_matmul.py @@ -67,7 +67,11 @@ def tile_gemm(A: wp.array2d(dtype=wp.float32), B: wp.array2d(dtype=wp.float16), with wp.Tape() as tape: wp.launch_tiled( - tile_gemm, dim=(int(M / TILE_M), int(N / TILE_N)), inputs=[A_wp, B_wp, C_wp], block_dim=TILE_THREADS + tile_gemm, + dim=(int(M / TILE_M), int(N / TILE_N)), + inputs=[A_wp, B_wp], + outputs=[C_wp], + block_dim=TILE_THREADS, ) assert np.allclose(C_wp.numpy(), A @ B) From 8fe55f551ac211a0b9ef30ec2f80ad03b7d0d14a Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 15:26:17 -0700 Subject: [PATCH 097/102] Rename example, add copyright headers --- warp/examples/tile/example_tile_fft.py | 36 ++++++++++++++++++-------- warp/native/tile.h | 8 ++++++ warp/native/tile_gemm.h | 8 ++++++ warp/native/tile_reduce.h | 10 ++++++- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/warp/examples/tile/example_tile_fft.py b/warp/examples/tile/example_tile_fft.py index f47e0b4a..2ad87fc0 100644 --- a/warp/examples/tile/example_tile_fft.py +++ b/warp/examples/tile/example_tile_fft.py @@ -1,11 +1,22 @@ +# Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +########################################################################### +# Example Tile FFT +# +# Shows how to write a simple FFT kernel using Warp tile primitives. +# +########################################################################### + import numpy as np import warp as wp -wp.init() wp.set_module_options({"enable_backward": False}) -wp.set_device("cuda:0") -wp.build.clear_kernel_cache() BLOCK_DIM = 8 TILE_M = 1 @@ -21,13 +32,16 @@ def fft_tiled(x: wp.array2d(dtype=wp.vec2d), y: wp.array2d(dtype=wp.vec2d)): wp.tile_store(y, i, j, a) -x_h = np.ones((TILE_M, TILE_N, 2), dtype=np.float64) -x_h[:, :, 1] = 0 -y_h = 3 * np.ones((TILE_M, TILE_N, 2), dtype=np.float64) -x_wp = wp.array2d(x_h, dtype=wp.vec2d) -y_wp = wp.array2d(y_h, dtype=wp.vec2d) +if __name__ == "__main__": + wp.set_device("cuda:0") + + x_h = np.ones((TILE_M, TILE_N, 2), dtype=np.float64) + x_h[:, :, 1] = 0 + y_h = 3 * np.ones((TILE_M, TILE_N, 2), dtype=np.float64) + x_wp = wp.array2d(x_h, dtype=wp.vec2d) + y_wp = wp.array2d(y_h, dtype=wp.vec2d) -wp.launch(fft_tiled, dim=[1, 1, BLOCK_DIM], inputs=[x_wp, y_wp], block_dim=BLOCK_DIM) + wp.launch_tiled(fft_tiled, dim=[1, 1], inputs=[x_wp], outputs=[y_wp], block_dim=BLOCK_DIM) -print("inputs:\n", x_wp) # [1+0i, 1+0i, 1+0i, ...] -print("output:\n", y_wp) # [32+0i, 0, 0, ...] + print("Inputs:\n", x_wp) # [1+0i, 1+0i, 1+0i, ...] + print("Output:\n", y_wp) # [32+0i, 0, 0, ...] diff --git a/warp/native/tile.h b/warp/native/tile.h index dad774ec..e5b48a9d 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -1,3 +1,11 @@ +/** Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + */ + #pragma once #include "builtin.h" diff --git a/warp/native/tile_gemm.h b/warp/native/tile_gemm.h index c033330a..2ab0fe40 100644 --- a/warp/native/tile_gemm.h +++ b/warp/native/tile_gemm.h @@ -1,3 +1,11 @@ +/** Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + */ + #pragma once #include "builtin.h" diff --git a/warp/native/tile_reduce.h b/warp/native/tile_reduce.h index 3b5da6d9..67d0e5c9 100644 --- a/warp/native/tile_reduce.h +++ b/warp/native/tile_reduce.h @@ -1,3 +1,11 @@ +/** Copyright (c) 2024 NVIDIA CORPORATION. All rights reserved. + * NVIDIA CORPORATION and its licensors retain all intellectual property + * and proprietary rights in and to this software, related documentation + * and any modifications thereto. Any use, reproduction, disclosure or + * distribution of this software and related documentation without an express + * license agreement from NVIDIA CORPORATION is strictly prohibited. + */ + #pragma once #include "tile.h" @@ -202,4 +210,4 @@ void adj_tile_min(Tile& t, Tile& adj_t, AdjTile& adj_ret) -} // namespace wp \ No newline at end of file +} // namespace wp From 59ed9f2b19ab5675eaf73f77f5b46b20b3b75fb4 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 15:55:19 -0700 Subject: [PATCH 098/102] Update CI scripts --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e6a26363..17833f4c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -70,7 +70,7 @@ linux-aarch64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/96/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/99/libmathdx_build_aarch64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps @@ -96,7 +96,7 @@ linux-x86_64 build: - apt-get update && apt-get install build-essential curl --no-install-recommends -y - > curl -k -H "Authorization: Bearer $ARTIFACTORY_ACCESS_TOKEN" - $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/96/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz + $ARTIFACTORY_BASE_URL/sw-cuda-math-mathdx-generic-local/cicd/libmathdx/main/PostMerge/99/libmathdx_build_x86_64_rockylinux8_cuda12.0.0_release.tar.gz -o libmathdx.tar.gz - mkdir -p _build/target-deps - tar -xzf libmathdx.tar.gz -C _build/target-deps From a13a44edaf864513493992d99583adc67b44ffb7 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Wed, 30 Oct 2024 16:39:23 -0700 Subject: [PATCH 099/102] More FFT tile tests --- warp/tests/test_tile_mathdx.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py index 2c8d7180..c441a9f3 100644 --- a/warp/tests/test_tile_mathdx.py +++ b/warp/tests/test_tile_mathdx.py @@ -8,6 +8,7 @@ import unittest import numpy as np +import functools import warp as wp from warp.tests.unittest_utils import * @@ -18,8 +19,6 @@ TILE_N = wp.constant(4) TILE_K = wp.constant(8) -N_FFT = wp.constant(128) - # num threads per-tile TILE_DIM = 64 @@ -67,33 +66,36 @@ def test_tile_math_matmul(test, device): assert_np_equal(B_wp.grad.numpy(), A.T @ adj_C, tol=1e-2) -@wp.kernel() -def tile_math_fft_kernel(gx: wp.array2d(dtype=wp.vec2f), gy: wp.array2d(dtype=wp.vec2f)): - i, j = wp.tid() - xy = wp.tile_load(gx, i, j, m=N_FFT, n=N_FFT) - wp.tile_fft(xy) - wp.tile_store(gy, i, j, xy) +def test_tile_math_fft(test, device, wp_dtype, fft_size): + np_real_dtype = {wp.vec2f: np.float32, wp.vec2d: np.float64}[wp_dtype] + np_cplx_dtype = {wp.vec2f: np.complex64, wp.vec2d: np.complex128}[wp_dtype] -def test_tile_math_fft(test, device): + @wp.kernel() + def tile_math_fft_kernel(gx: wp.array2d(dtype=wp_dtype), gy: wp.array2d(dtype=wp_dtype)): + i, j = wp.tid() + xy = wp.tile_load(gx, i, j, m=fft_size, n=fft_size) + wp.tile_fft(xy) + wp.tile_store(gy, i, j, xy) + rng = np.random.default_rng(42) # Warp doesn't really have a complex64 type, # so we use 2 float32 to represent a single complex64 number and then convert it to vec2f - X = rng.random((N_FFT, 2 * N_FFT), dtype=np.float32) + X = rng.random((fft_size, 2 * fft_size), dtype=np_real_dtype) Y = np.zeros_like(X) - X_wp = wp.array2d(X, requires_grad=True, dtype=wp.vec2f, device=device) - Y_wp = wp.array2d(Y, requires_grad=True, dtype=wp.vec2f, device=device) + X_wp = wp.array2d(X, requires_grad=True, dtype=wp_dtype, device=device) + Y_wp = wp.array2d(Y, requires_grad=True, dtype=wp_dtype, device=device) - X_c64 = X.view(np.complex64).reshape(N_FFT, N_FFT) + X_c64 = X.view(np_cplx_dtype).reshape(fft_size, fft_size) Y_c64 = np.fft.fft(X_c64, axis=-1) with wp.Tape() as tape: wp.launch_tiled(tile_math_fft_kernel, dim=[1, 1], inputs=[X_wp, Y_wp], block_dim=TILE_DIM, device=device) - Y_wp_c64 = Y_wp.numpy().view(np.complex64).reshape(N_FFT, N_FFT) + Y_wp_c64 = Y_wp.numpy().view(np_cplx_dtype).reshape(fft_size, fft_size) assert_np_equal(Y_wp_c64, Y_c64, tol=1.0e-4) @@ -109,7 +111,8 @@ class TestTileMathDx(unittest.TestCase): add_function_test(TestTileMathDx, "test_tile_math_matmul", test_tile_math_matmul, devices=devices) -add_function_test(TestTileMathDx, "test_tile_math_fft", test_tile_math_fft, devices=devices) +add_function_test(TestTileMathDx, "test_tile_math_fft", functools.partial(test_tile_math_fft, wp_dtype=wp.vec2f, fft_size=wp.constant(128)), devices=devices) +add_function_test(TestTileMathDx, "test_tile_math_fft", functools.partial(test_tile_math_fft, wp_dtype=wp.vec2d, fft_size=wp.constant(256)), devices=devices) if __name__ == "__main__": wp.clear_kernel_cache() From c52e54f1dddaaf0ea64eea9414ef1e011b49fd00 Mon Sep 17 00:00:00 2001 From: Leopold Cambier Date: Wed, 30 Oct 2024 20:06:34 -0700 Subject: [PATCH 100/102] Fix FFT alignment --- warp/native/tile.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/warp/native/tile.h b/warp/native/tile.h index e5b48a9d..9c896d10 100644 --- a/warp/native/tile.h +++ b/warp/native/tile.h @@ -1390,13 +1390,18 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, WP_TILE_SYNC(); } - +// TODO(lcambier): use a properly overaligned complex type that matches cuFFTDx's expectation +// TODO(lcambier): use dynamic smem #define tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, Xinout) \ do { \ void function_name(dtype*, dtype*); \ WP_TILE_SHARED __align__(16) char buffer[shared_memory_size]; \ + __align__(16) dtype data[ept]; \ for(int b = 0; b < (int)batch_size; b++) { \ - function_name(Xinout.data + (int)b * (int)ept, (dtype*)buffer); \ + dtype* inout = Xinout.data + (int)b * (int)ept; \ + memcpy(data, inout, sizeof(dtype) * ept); \ + function_name(data, (dtype*)buffer); \ + memcpy(inout, data, sizeof(dtype) * ept); \ WP_TILE_SYNC(); \ } \ } while (0) From 389e8592331698fbb96a7d3ae49cad75a3439b8b Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 21:06:28 -0700 Subject: [PATCH 101/102] Fix Ruff issues --- warp/tests/test_tile_mathdx.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py index c441a9f3..b5e3bb2b 100644 --- a/warp/tests/test_tile_mathdx.py +++ b/warp/tests/test_tile_mathdx.py @@ -5,10 +5,10 @@ # distribution of this software and related documentation without an express # license agreement from NVIDIA CORPORATION is strictly prohibited. +import functools import unittest import numpy as np -import functools import warp as wp from warp.tests.unittest_utils import * @@ -67,7 +67,6 @@ def test_tile_math_matmul(test, device): def test_tile_math_fft(test, device, wp_dtype, fft_size): - np_real_dtype = {wp.vec2f: np.float32, wp.vec2d: np.float64}[wp_dtype] np_cplx_dtype = {wp.vec2f: np.complex64, wp.vec2d: np.complex128}[wp_dtype] @@ -77,7 +76,7 @@ def tile_math_fft_kernel(gx: wp.array2d(dtype=wp_dtype), gy: wp.array2d(dtype=wp xy = wp.tile_load(gx, i, j, m=fft_size, n=fft_size) wp.tile_fft(xy) wp.tile_store(gy, i, j, xy) - + rng = np.random.default_rng(42) # Warp doesn't really have a complex64 type, @@ -111,9 +110,19 @@ class TestTileMathDx(unittest.TestCase): add_function_test(TestTileMathDx, "test_tile_math_matmul", test_tile_math_matmul, devices=devices) -add_function_test(TestTileMathDx, "test_tile_math_fft", functools.partial(test_tile_math_fft, wp_dtype=wp.vec2f, fft_size=wp.constant(128)), devices=devices) -add_function_test(TestTileMathDx, "test_tile_math_fft", functools.partial(test_tile_math_fft, wp_dtype=wp.vec2d, fft_size=wp.constant(256)), devices=devices) +add_function_test( + TestTileMathDx, + "test_tile_math_fft", + functools.partial(test_tile_math_fft, wp_dtype=wp.vec2f, fft_size=wp.constant(128)), + devices=devices, +) +add_function_test( + TestTileMathDx, + "test_tile_math_fft", + functools.partial(test_tile_math_fft, wp_dtype=wp.vec2d, fft_size=wp.constant(256)), + devices=devices, +) if __name__ == "__main__": wp.clear_kernel_cache() - unittest.main(verbosity=2) + unittest.main(verbosity=2) \ No newline at end of file From e0fc988db319f7ad00986c951e70ac42ca94cac6 Mon Sep 17 00:00:00 2001 From: Eric Shi Date: Wed, 30 Oct 2024 21:44:09 -0700 Subject: [PATCH 102/102] Add a trailing newline to appease Ruff --- warp/tests/test_tile_mathdx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/warp/tests/test_tile_mathdx.py b/warp/tests/test_tile_mathdx.py index b5e3bb2b..31fc32a7 100644 --- a/warp/tests/test_tile_mathdx.py +++ b/warp/tests/test_tile_mathdx.py @@ -125,4 +125,4 @@ class TestTileMathDx(unittest.TestCase): if __name__ == "__main__": wp.clear_kernel_cache() - unittest.main(verbosity=2) \ No newline at end of file + unittest.main(verbosity=2)