### Run cuTile Python Quickstart Example Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Executes the vector addition quickstart example from the command line. This command verifies the installation and basic functionality of cuTile Python. ```bash $ python3 samples/quickstart/VectorAdd_quickstart.py ``` -------------------------------- ### Install cuPy for cuTile Python Examples Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Installs the cuPy library, which is used by some cuTile Python samples. This is necessary for running examples that leverage cuPy functionalities. ```bash pip install cupy-cuda13x ``` -------------------------------- ### Run cuTile Python FFT Example Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Executes the FFT example from the cuTile Python samples directory. This demonstrates running other provided examples by invoking their Python scripts. ```bash $ python3 samples/FFT.py ``` -------------------------------- ### Install Pytest and NumPy for cuTile Python Examples Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Installs Pytest and NumPy, common dependencies for cuTile Python samples. These are used for testing and numerical operations within the examples. ```bash pip install pytest numpy ``` -------------------------------- ### Install cuTile Python Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Installs the cuTile Python library using pip. This command should be run after ensuring all prerequisites are met. ```bash pip install cuda-tile ``` -------------------------------- ### Run Pytest for cuTile Python Samples Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Execute all sample tests for the cuTile Python project using pytest. This command initiates the test suite and reports the results. ```bash pytest samples ``` -------------------------------- ### Profile cuTile Python Kernels with Nsight Compute Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Profile cuTile Python kernels using NVIDIA Nsight Compute. This command generates a profile file that can be analyzed in the Nsight Compute GUI to view kernel statistics. ```bash ncu -o VecAddProfile --set detailed python3 VectorAdd_quickstart.py ``` -------------------------------- ### Vector Addition Kernel using cuTile Python Source: https://docs.nvidia.com/cuda/cutile-python/quickstart Demonstrates a simple vector addition kernel using cuTile Python. It showcases loading tiles from GPU memory, performing elementwise addition, and storing the result back to GPU memory. ```python # SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # SPDX-License-Identifier: Apache-2.0 """ Example demonstrating simple vector addition. Shows how to perform elementwise operations on vectors. """ import cupy as cp import numpy as np import cuda.tile as ct @ct.kernel def vector_add(a, b, c, tile_size: ct.Constant[int]): # Get the 1D pid pid = ct.bid(0) # Load input tiles a_tile = ct.load(a, index=(pid,), shape=(tile_size,)) b_tile = ct.load(b, index=(pid,), shape=(tile_size,)) # Perform elementwise addition result = a_tile + b_tile # Store result ct.store(c, index=(pid, ), tile=result) def test(): # Create input data vector_size = 2**12 tile_size = 2**4 grid = (ct.cdiv(vector_size, tile_size), 1, 1) a = cp.random.uniform(-1, 1, vector_size) b = cp.random.uniform(-1, 1, vector_size) c = cp.zeros_like(a) # Launch kernel ct.launch(cp.cuda.get_current_stream(), grid, # 1D grid of processors vector_add, (a, b, c, tile_size)) # Copy to host only to compare a_np = cp.asnumpy(a) b_np = cp.asnumpy(b) c_np = cp.asnumpy(c) # Verify results expected = a_np + b_np np.testing.assert_array_almost_equal(c_np, expected) print("✓ vector_add_example passed!") if __name__ == "__main__": test() ``` -------------------------------- ### cuda.tile.num_tiles Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.num_tiles Gets the number of tiles in the tile space of the array along the specified axis. ```APIDOC ## cuda.tile.num_tiles ### Description Gets the number of tiles in the tile space of the array along the axis. ### Method GET (conceptual, as this is a function call) ### Endpoint N/A (Python function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **array** (cuda.tile.Array) – An array object on a CUDA device. * **axis** (int) – The axis of the tile partition space to get the dimension size. * **shape** (int, ...) – A sequence of const integers defining the shape of the tile. * **order** (str, tuple) – Order of axis mapping. Accepts 'C', 'F', or a tuple of const integers. See `load()` documentation for details. ### Request Example ```python # Assuming 'array' is a cuda.tile.Array object # and tile shape is (4, 8) # Example 1: Get number of tiles along axis 0 ct.num_tiles(array, 0, shape=(4, 8)) # Example 2: Get number of tiles along axis 1 ct.num_tiles(array, 1, shape=(4, 8)) ``` ### Response #### Success Response (int32) Returns the number of tiles (int32) along the specified axis. #### Response Example ```json { "example": 8 } ``` #### Response Example (Axis 1) ```json { "example": 2 } ``` ``` -------------------------------- ### CUDA CUTILE Python Data Types Source: https://docs.nvidia.com/cuda/cutile-python/data Details the data types (dtypes) available in CUDA CUTILE Python, their properties, and examples. ```APIDOC ## CUDA CUTILE Python Data Types ### Description Data types (dtypes) in CUDA CUTILE Python describe the type of objects in arrays, tiles, or operations. They dictate memory storage and operation semantics. ### `cuda.tile.DType` Class A base class for data types. * **`bitwidth`** (int) - The number of bits in an element of the data type. * **`name`** (str) - The name of the data type. ### `cuda.tile.bool_` A boolean data type representing `True` or `False` with a bitwidth of 8. * **Type**: Arithmetic dtype * **Description**: Represents boolean values. ``` -------------------------------- ### Define and Launch a Tile Kernel in Python Source: https://docs.nvidia.com/cuda/cutile-python/execution This snippet demonstrates how to define a tile kernel using the @ct.kernel decorator and then launch it across a grid using ct.launch. It highlights the basic structure for executing custom kernels on the GPU. ```python import cutile as ct # Define a tile kernel @ct.kernel def f(a, b, c): pass # Define the grid dimensions grid = (8, 8) # Launch the kernel (assuming A, B, C are defined tensors and stream is a CUDA stream) # ct.launch(stream, grid, f, (A, B, C)) ``` -------------------------------- ### Scalar Power Operation Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.pow Computes the power of two scalar values. This is a standard Python operation and not specific to cuTile, but shown for completeness in the context of exponentiation examples. ```python # scalar and scalar z = 7 ** 2 ``` -------------------------------- ### CUDA Tile Kernel Launch Source: https://docs.nvidia.com/cuda/cutile-python/execution Launches a defined tile kernel over a specified grid and stream. Kernel arguments are passed positionally. ```APIDOC ## cuda.tile.launch ### Description Launches a CUDA tile kernel on a specified stream and grid. This function queues the kernel for execution. ### Method Function Call ### Endpoint N/A (Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **stream** (_cuda.Stream_) – The CUDA stream to execute the kernel on. * **grid** (_tuple_) – Tuple of up to 3 grid dimensions (e.g., (8, 8)) to execute the kernel over. * **kernel** (_function_) – The tile kernel function to execute. * **kernel_args** (_tuple_) – Positional arguments to pass to the kernel. ### Request Example ```python import cutile as ct import cuda_stream # Assuming cuda_stream is available # Assume A, B, C are CUDA-compatible tensors # Assume my_kernel is a defined @ct.kernel function stream = cuda_stream.create_stream() grid = (8, 8) ct.launch(stream, grid, my_kernel, (A, B, C)) ``` ### Response #### Success Response (200) N/A (Launches asynchronously) #### Response Example N/A ``` -------------------------------- ### Perform Sum Reduction on Tile with cuda.tile.sum in Python Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.sum This snippet demonstrates how to use the cuda.tile.sum function to perform sum reduction on a tile. It shows examples of reducing along a specific axis and preserving dimensions. ```python import cutile as ct # Create a sample tile tx = ct.full((2, 4), 3, dtype=ct.float32) # Perform sum reduction along axis 1 ty = ct.sum(tx, 1) print(ty.shape) # Perform sum reduction along axis 1 and keep dimensions ty = ct.sum(tx, 1, keepdims=True) print(ty.shape) ``` -------------------------------- ### Launch CUDA Kernel from Host (Python) Source: https://docs.nvidia.com/cuda/cutile-python/index This Python function demonstrates how to launch a CUDA kernel from the host using the CUDA Tile library. It takes CuPy arrays as input and output, asserts shape compatibility, calculates the grid dimensions, and then calls `ct.launch` to execute the kernel. ```python import cupy import cutile as ct TILE_SIZE = 32 # Example tile size # Assume vector_add_kernel is defined elsewhere as a CUDA kernel def vector_add_kernel(a, b, result): # Kernel logic would go here pass def vector_add(a: cupy.ndarray, b: cupy.ndarray, result: cupy.ndarray): assert a.shape == b.shape == result.shape grid = (ct.cdiv(a.shape[0], TILE_SIZE), 1, 1) ct.launch(cupy.cuda.get_current_stream(), grid, vector_add_kernel, (a, b, result)) ``` -------------------------------- ### CUDA CUTILE Python - Data Handling and Initialization Source: https://docs.nvidia.com/cuda/cutile-python/genindex This section covers classes and functions related to data types, array properties, and tile initialization within the cuda.tile module. ```APIDOC ## Data Handling and Initialization in cuda.tile ### Description Details on data types, array properties, and functions for initializing tiles with specific values. ### Classes and Attributes - **DType.name**: Property to get the name of a data type. - **Array.ndim**: Property to get the number of dimensions of an array. - **Array.shape**: Property to get the shape of an array. - **PaddingMode**: Enum for specifying padding behavior. - **NAN**: Use NaN for padding. - **NEG_INF**: Use negative infinity for padding. - **NEG_ZERO**: Use negative zero for padding. - **POS_INF**: Use positive infinity for padding. - **ones()**: Creates a tile filled with ones. ### Methods - **permute(** *dims* **)**: Permutes the dimensions of a tile. - **reshape(** *shape* **)**: Reshapes a tile to a new shape. ### Usage Examples ```python import cuda.tile as tl # Example for ones tile_ones = tl.ones((4, 4), dtype=tl.float32) # Example for Array properties print(f"Number of dimensions: {tile_ones.ndim}") print(f"Shape: {tile_ones.shape}") # Example for permute permuted_tile = tile_ones.permute((1, 0)) # Example for reshape reshaped_tile = tile_ones.reshape(16) # Example for PaddingMode pad_config = { "mode": tl.PaddingMode.NAN } ``` ### Response - **Success Response (200)**: Returns the initialized tile, modified tile, or property values. ### Response Example ```json { "result": "[tile, shape, ndim, or property value]" } ``` ``` -------------------------------- ### Perform Argmin Reduction on Tile with cuTile Python Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.argmin This snippet demonstrates how to use the cuda.tile.argmin function to find the index of the minimum element within a cuTile. It shows examples of reducing along a specific axis and preserving dimensions. ```python >>> tx = ct.full((2, 4), 3, dtype=ct.float32) >>> ty = ct.argmin(tx, 1) >>> ty.shape (2,) >>> ty = ct.argmin(tx, 1, keepdims=True) >>> ty.shape (2, 1) ``` -------------------------------- ### Memory Model Overview Source: https://docs.nvidia.com/cuda/cutile-python/memory_model Explains the cuTile Python memory model, emphasizing the need for explicit synchronization due to potential operation reordering and introducing Memory Order and Memory Scope attributes for atomic operations. ```APIDOC ## Memory Model Overview ### Description cuTile employs a memory model that permits the compiler and hardware to reorder operations for performance. Without explicit synchronization, there is no guaranteed ordering of memory accesses across threads. To coordinate memory accesses among threads, cuTile provides two attributes for atomic operations: Memory Order and Memory Scope. Synchronization occurs at a per-element granularity, with each element in the array participating independently in the memory model. For a more detailed explanation, refer to the [Tile IR documentation](https://docs.nvidia.com/cuda/tile-ir/). ### Method N/A (Conceptual Overview) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Negate a Scalar using cuda.tile.negative Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.negative This example shows how to negate a single scalar value using the cuda.tile.negative function. It takes a scalar input 'x' and returns its negative value 'y'. This is equivalent to using the unary minus operator. ```python >>> x = 3 >>> y = -x ``` -------------------------------- ### Tile Kernel Options Source: https://docs.nvidia.com/cuda/cutile-python/execution Configuration options for tile kernels, including host/tile code execution, number of CTAs, occupancy, and optimization level. ```APIDOC ## Tile Kernel Options ### Description Parameters that can be passed to tile kernels or the `cuda.tile.kernel` decorator to control execution behavior and performance. ### Method Parameter Configuration ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **host** (_bool_, optional) – Whether the function can be called from host code. Defaults to False. * **tile** (_bool_, optional) – Whether the function can be called from tile code. Defaults to True. * **num_ctas** (_int_, optional) – Number of CTAs in a CGA. Must be a power of 2 between 1 and 16. Default: None (auto). * **occupancy** (_int_, optional) – Expected number of active CTAs per SM, [1, 32]. Default: None (auto). * **opt_level** (_int_, optional) – Optimization level [0, 3]. Default: 3. Target-specific values can be provided using a `cuda.tile.ByTarget` object. ### Request Example ```python import cutile as ct @ct.kernel(num_ctas=4, opt_level=2) def optimized_kernel(a, b): pass # Or when launching: # ct.launch(stream, grid, kernel, args, num_ctas=4, opt_level=2) ``` ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Execution Control Source: https://docs.nvidia.com/cuda/cutile-python/genindex Information on launching CUDA kernels and managing execution flow within the CUTILE-Python environment. ```APIDOC ## Execution Control ### Description Utilities for controlling the execution of CUDA code, including kernel launching. ### Functions - **launch()**: Launches a CUDA kernel. ### Classes - **kernel**: Represents a CUDA kernel that can be launched. ### Examples ```python # Example for launch() and kernel (conceptual) import cuda.tile # Assume 'my_kernel_function' is a defined CUDA kernel # my_kernel_function = cuda.tile.kernel(...) # Example of launching a kernel (syntax may vary based on actual implementation) # grid_dim = (16, 16) # block_dim = (32, 32) # args = (input_tile, output_tile) # cuda.tile.launch(my_kernel_function, grid_dim, block_dim, args) print("Kernel launch mechanism available.") ``` ``` -------------------------------- ### Negate a Tile using cuda.tile.negative Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.negative This example demonstrates how to negate all elements within a cuTile tile using the cuda.tile.negative function. It takes an input tile 'tx' and returns a new tile 'ty' with each element being the negative of the corresponding element in 'tx'. ```python >>> import cutile as ct >>> tx = ct.arange(8, dtype=ct.int32) >>> ty = ct.negative(tx) >>> ty = -tx ``` -------------------------------- ### Define and Launch cuTile Kernel in Python Source: https://docs.nvidia.com/cuda/cutile-python/index This snippet demonstrates how to define a cuTile kernel for vector addition using the `@ct.kernel` decorator and launch it on the GPU using `ct.launch()`. It requires the `cuda.tile` and `cupy` libraries. The kernel operates on tiles loaded from input arrays and stores the result back. ```python import cuda.tile as ct import cupy TILE_SIZE = 16 # cuTile kernel for adding two dense vectors. It runs in parallel on the GPU. @ct.kernel def vector_add_kernel(a, b, result): block_id = ct.bid(0) a_tile = ct.load(a, index=(block_id,), shape=(TILE_SIZE,)) b_tile = ct.load(b, index=(block_id,), shape=(TILE_SIZE,)) result_tile = a_tile + b_tile ct.store(result, index=(block_id,), tile=result_tile) # Example of how to launch the kernel (assuming a and b are cupy arrays) # N = 1024 # a = cupy.arange(N, dtype=cupy.float32) # b = cupy.arange(N, dtype=cupy.float32) # result = cupy.empty_like(a) # ct.launch(vector_add_kernel, (a, b, result), block_count=N // TILE_SIZE) ``` -------------------------------- ### Max Reduction on Tile with cuTile Python Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.max This snippet demonstrates how to perform a maximum reduction on a cuTile object along a specified axis. It shows examples of reducing a single axis and reducing all axes while optionally preserving the dimensions of the input tile. ```python import cutile as ct # Create a sample tile tx = ct.full((2, 4), 3, dtype=ct.float32) # Perform max reduction along axis 1 ty = ct.max(tx, 1) print(ty.shape) # Perform max reduction along axis 1 and keep dimensions ty = ct.max(tx, 1, keepdims=True) print(ty.shape) ``` -------------------------------- ### Tile Functions Source: https://docs.nvidia.com/cuda/cutile-python/execution Details on tile functions, their decorators, and how they interact with different execution spaces. ```APIDOC ## Tile Functions ### `cuda.tile.function` decorator _class_ `cuda.tile.function`(_func=None_, _/_, _*_, _host=False_, _tile=True_) _Tile functions_ are functions that are usable in [tile code](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#tile-code). This decorator indicates what [execution spaces](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#execution-spaces) a function can be called from. With no arguments, it denotes a tile-only function. When an unannotated function is called by a [tile function](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#tile-functions), tile shall be added to the unannotated function’s execution space. This process is recursive. No explicit annotation is required. The types usable as parameters to a [tile function](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#tile-functions) are described in the [data model](https://docs.nvidia.com/cuda/cutile-python/data.html.md#data-model). #### Parameters * `func` (function) - The function to decorate. * `host` (bool) - If True, the function is usable in host code. * `tile` (bool) - If True, the function is usable in tile code. ``` -------------------------------- ### Get Number of Tiles in cuTile Python Array Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.num_tiles The `cuda.tile.num_tiles` function calculates the number of tiles in an array's tile space along a specified axis. It requires an array object on a CUDA device, the axis to query, and the shape of the tile. The order parameter can also be specified. ```python import cutile as ct # Assuming 'array' is a pre-defined cuTile array object # Example: array = ct.Array(...) # Get the number of tiles along axis 0 with tile shape (4, 8) num_tiles_axis_0 = ct.num_tiles(array, 0, shape=(4, 8)) print(num_tiles_axis_0) # Expected output: 8 # Get the number of tiles along axis 1 with tile shape (4, 8) num_tiles_axis_1 = ct.num_tiles(array, 1, shape=(4, 8)) print(num_tiles_axis_1) # Expected output: 2 ``` -------------------------------- ### CUDA Tile Index Conversion and Special Methods (Python) Source: https://docs.nvidia.com/cuda/cutile-python/genindex Explains the `__index__` method for the cuda.tile.Tile class in Python, which allows the tile to be used in contexts requiring an integer index. It also covers other special methods like `__hash__` for hashability. ```python from cuda.tile import Tile # Example usage of __index__ and __hash__ tile = Tile(shape=(4, 4)) # Using __index__ (conceptual example, requires specific context) # For instance, if a function expects an integer index derived from the tile: # def process_index(idx): # print(f"Processing index: {idx}") # try: # # This assumes __index__ returns a meaningful integer representation # process_index(tile) # except TypeError as e: # print(f"__index__ not applicable in this context: {e}") # Using __hash__ try: tile_hash = hash(tile) print(f"Hash of the tile: {tile_hash}") except TypeError as e: print(f"Hashing not supported or failed: {e}") ``` -------------------------------- ### Get Number of Blocks Along Axis in cuTile Python Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.num_blocks The cuda.tile.num_blocks function retrieves the number of blocks along a specified axis in the block index space. It takes an integer representing the axis (0, 1, or 2) and returns an int32 value. This is useful for understanding the grid dimensions for CUDA kernel launches. ```python import cutile as ct num_blocks_x = ct.num_blocks(0) num_blocks_y = ct.num_blocks(1) num_blocks_z = ct.num_blocks(2) ``` -------------------------------- ### Perform Cumulative Product on Tile using cuTile Python Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.cumprod This snippet demonstrates how to use the cuda.tile.cumprod function to calculate the cumulative product of a tile. It shows examples of performing the operation along axis 1 and in reverse direction, illustrating the input parameters and the resulting shape of the output tile. The function operates on cuTile objects. ```python >>> import cutile as ct >>> tx = ct.full((2, 4), 3, dtype=ct.float32) >>> ty = ct.cumprod(tx, 1) >>> ty.shape (2, 4) >>> ty = ct.cumprod(tx, 1, reverse=True) >>> ty.shape (2, 4) ``` -------------------------------- ### Execution Model Overview Source: https://docs.nvidia.com/cuda/cutile-python/execution Provides an overview of the execution model for cuTile Python, including abstract machines, blocks, grids, and execution spaces. ```APIDOC ## Execution Model ### Abstract Machine A _tile kernel_ is executed by logical thread blocks that are organized in a 1D, 2D, or 3D _grid_. Each _block_ is executed by a subset of a GPU. Each [block](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#block) executes the body of the [kernel](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#tile-kernels). Scalar operations are executed serially by a single thread of the [block](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#block), and array operations are collectively executed in parallel by all threads of the [block](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#block). Tile programs explicitly describe [block](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#block)-level parallelism, but not thread-level parallelism. Threads cannot be explicitly identified or manipulated in tile programs. Explicit synchronization or communication within a [block](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#block) is not permitted, but it is allowed between different [blocks](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#block). It is important to not confuse [blocks](https://docs.nvidia.com/cuda/cutile-python/execution.html.md#block) (units of execution) with [tiles](https://docs.nvidia.com/cuda/cutile-python/data/tile.html.md#cuda-tile-tile) (units of data). A block may work with multiple different [tiles](https://docs.nvidia.com/cuda/cutile-python/data/tile.html.md#cuda-tile-tile) with differing shapes originating from differing [global arrays](https://docs.nvidia.com/cuda/cutile-python/data/array.html.md#cuda-tile-array). ### Execution Spaces cuTile code is executed on one or more _targets_, which are distinct execution environments that are distinguished by different hardware resources or programming models. A function is _usable_ if it can be called. A type or object is _usable_ if its attributes are accessible (can be read and written) and its methods are callable. Some functions, types, and objects are only usable on certain _targets_. The set of _targets_ that such a construct is usable on is called its _execution space_. * _Host code_ is the execution space that includes all CPU targets. * _SIMT code_ is the execution space that includes all CUDA SIMT targets. Note: This has historically been called device code, but we avoid this term to prevent ambiguity. * _Tile code_ is the execution space that includes all CUDA tile targets. Functions can have decorators that explicitly specify their execution space. These are called _annotated functions_. ``` -------------------------------- ### CUDA Tile Utility Functions (Python) Source: https://docs.nvidia.com/cuda/cutile-python/operations Includes utility functions for debugging and control flow within CUDA kernels. `printf` allows formatted output, and `assert_` performs runtime assertions. ```python from cutile import tile # Example usage (conceptual) # tile.printf("Value: %d\n", some_value) # tile.assert_(condition, "Assertion failed!") ``` -------------------------------- ### Get Block Index using cuda.tile.bid in Python Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.bid This snippet demonstrates how to use the cuda.tile.bid function to retrieve the block index along different axes (x, y, z). The function requires the cuTile library to be imported as 'ct'. It takes an integer representing the axis (0 for x, 1 for y, 2 for z) and returns the corresponding block index. ```python import cutile as ct # Get the block index for the x-axis bid_x = ct.bid(0) # Get the block index for the y-axis bid_y = ct.bid(1) # Get the block index for the z-axis bid_z = ct.bid(2) print(f"Block index for axis 0 (x): {bid_x}") print(f"Block index for axis 1 (y): {bid_y}") print(f"Block index for axis 2 (z): {bid_z}") ``` -------------------------------- ### Arithmetic Operations on CUDA Tiles (Python) Source: https://docs.nvidia.com/cuda/cutile-python/genindex Demonstrates the implementation of various arithmetic operations for the cuda.tile.Tile class in Python. These methods allow for addition, subtraction, multiplication, division, modulo, and exponentiation between tile objects or with other compatible types. They are essential for performing mathematical computations on tiled data structures within CUDA. ```python from cuda.tile import Tile # Example usage of arithmetic operations tile1 = Tile(shape=(4, 4)) tile2 = Tile(shape=(4, 4)) # Addition result_add = tile1 + tile2 print(f"Addition: {result_add}") # Subtraction result_sub = tile1 - tile2 print(f"Subtraction: {result_sub}") # Multiplication result_mul = tile1 * tile2 print(f"Multiplication: {result_mul}") # True Division result_truediv = tile1 / tile2 print(f"True Division: {result_truediv}") # Floor Division result_floordiv = tile1 // tile2 print(f"Floor Division: {result_floordiv}") # Modulo result_mod = tile1 % tile2 print(f"Modulo: {result_mod}") # Power result_pow = tile1 ** 2 print(f"Power: {result_pow}") # Right-side arithmetic operations (e.g., scalar on the left) result_radd = 5 + tile1 print(f"Right Add: {result_radd}") result_rmul = 3 * tile1 print(f"Right Multiply: {result_rmul}") result_rsub = 10 - tile1 print(f"Right Subtract: {result_rsub}") result_rtruediv = 20 / tile1 print(f"Right True Divide: {result_rtruediv}") result_rfloordiv = 15 // tile1 print(f"Right Floor Divide: {result_rfloordiv}") result_rmod = 7 % tile1 print(f"Right Modulo: {result_rmod}") result_rpow = 2 ** tile1 print(f"Right Power: {result_rpow}") ``` -------------------------------- ### Create Tile with Sequential Values using cuda.tile.arange (Python) Source: https://docs.nvidia.com/cuda/cutile-python/generated/cuda.tile.arange The cuda.tile.arange function creates a tile where elements are populated with sequential integers starting from 0 up to the specified size minus one. It requires the tile size and optionally accepts a data type for the elements. This function is part of the cuTile Python library for CUDA tensor operations. ```python import cutile as ct tile = ct.arange(16, dtype=ct.int32) ``` -------------------------------- ### CUDA Tile Indexing and Hashing (Python) Source: https://docs.nvidia.com/cuda/cutile-python/genindex Covers the `__getitem__` and `__hash__` functionalities for the cuda.tile.Tile class in Python. `__getitem__` allows accessing elements or slices of the tile, while `__hash__` enables the tile object to be used in hash-based collections like sets and dictionaries. ```python from cuda.tile import Tile # Example usage of indexing and hashing tile = Tile(shape=(4, 4)) # Get an item (e.g., a specific element or slice) # Note: Actual indexing behavior depends on the Tile implementation # This is a conceptual example. # item = tile[0, 0] # print(f"Item at [0, 0]: {item}") # Get a slice # slice_data = tile[1:3, 2:4] # print(f"Slice [1:3, 2:4]: {slice_data}") # Hashing try: tile_hash = hash(tile) print(f"Hash of the tile: {tile_hash}") # Example of using in a set tile_set = {tile} print(f"Tile in a set: {tile_set}") except TypeError as e: print(f"Hashing not supported or failed: {e}") ``` -------------------------------- ### CUDA CUTILE Python Concepts Source: https://docs.nvidia.com/cuda/cutile-python/data Explains fundamental concepts in CUDA CUTILE Python, including element space, tile space, and shape broadcasting. ```APIDOC ## CUDA CUTILE Python Concepts ### Description This section details the core concepts of the CUDA CUTILE Python library, focusing on how data is organized and manipulated. ### Element Space The element space refers to the multidimensional space of elements within an array, stored according to a specific memory layout (e.g., row-major, column-major). ### Tile Space The tile space is the multidimensional space of tiles within an array, defined by a specific tile shape. A tile index `(i, j, ...)` with shape `S` corresponds to the elements belonging to the `(i+1)`-th, `(j+1)`-th, ... tile. ### Shape Broadcasting Shape broadcasting allows operations between tiles of different shapes. The smaller tile is automatically extended to match the larger tile's shape based on trailing dimension alignment and compatibility rules. Broadcasting follows NumPy's semantics for conciseness and efficiency. * Tiles are aligned by their trailing dimensions. * Dimensions are compatible if they have the same size or if one of them is 1. * If a tile has fewer dimensions, its shape is padded with 1s on the left. ``` -------------------------------- ### Utility Functions Source: https://docs.nvidia.com/cuda/cutile-python/operations Provides utility functions for block and tile information, loading, and storing data. ```APIDOC ## Utility Functions ### Description Provides utility functions for block and tile information, loading, and storing data. ### Methods - `bid()`: Returns the current block ID. - `num_blocks()`: Returns the total number of blocks. - `num_tiles()`: Returns the number of tiles. - `load(source, ...)`: Loads data into a tile. - `store(destination, tile, ...)`: Stores data from a tile. ### Parameters - `source` (MemoryLocation): The source of data to load. - `destination` (MemoryLocation): The destination to store data. - `tile` (Tile): The tile containing data to store. ### Request Example ```python # Example for bid block_id = cuda.tile.bid() # Example for load loaded_tile = cuda.tile.load(source_memory) # Example for store cuda.tile.store(destination_memory, my_tile) ``` ### Response #### Success Response (200) - `bid()`: Returns the block ID (int). - `num_blocks()`: Returns the number of blocks (int). - `num_tiles()`: Returns the number of tiles (int). - `load()`: Returns the loaded tile. - `store()`: Returns None. ``` -------------------------------- ### Constant Embedding Type Hints with CUDA C++ Tile API Source: https://docs.nvidia.com/cuda/cutile-python/execution Demonstrates how to use `cuda.tile.Constant` and `cuda.tile.Constant[int]` for type hinting parameters that should be constant embedded in CUDA kernels. This ensures that the compiler treats these parameters as literal values, optimizing kernel generation. ```python import cuda.tile as ct def needs_constant(x: ct.Constant): pass def needs_constant_int(x: ct.Constant[int]): pass ``` -------------------------------- ### Comparison Operations on CUDA Tiles (Python) Source: https://docs.nvidia.com/cuda/cutile-python/genindex Illustrates the comparison operators available for the cuda.tile.Tile objects in Python. This includes equality (==), inequality (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These are crucial for conditional logic and data filtering within CUDA. ```python from cuda.tile import Tile # Example usage of comparison operations tile1 = Tile(shape=(4, 4)) tile2 = Tile(shape=(4, 4)) # Equality result_eq = tile1 == tile2 print(f"Equality: {result_eq}") # Inequality result_ne = tile1 != tile2 print(f"Inequality: {result_ne}") # Greater Than result_gt = tile1 > tile2 print(f"Greater Than: {result_gt}") # Less Than result_lt = tile1 < tile2 print(f"Less Than: {result_lt}") # Greater Than or Equal To result_ge = tile1 >= tile2 print(f"Greater Than or Equal To: {result_ge}") # Less Than or Equal To result_le = tile1 <= tile2 print(f"Less Than or Equal To: {result_le}") ``` -------------------------------- ### Memory Management in cuda.tile Source: https://docs.nvidia.com/cuda/cutile-python/genindex Documentation for memory-related classes and attributes, including MemoryOrder and MemoryScope. ```APIDOC ## Memory Management in cuda.tile ### Description Details classes and attributes related to memory ordering and scope within CUDA tiles. ### Classes - **MemoryOrder**: Defines memory ordering semantics. - **RELAXED**: Relaxed memory order. - **RELEASE**: Release memory order. - **MemoryScope**: Defines the scope of memory operations. ### Parameters These are typically used as arguments to other functions or as attributes within specific contexts. Refer to the relevant function documentation for usage. ### Request Example ```python import cuda.tile # Example usage (conceptual, actual usage depends on context) # For instance, if a function accepted memory_order argument: # result = some_cuda_function(..., memory_order=cuda.tile.MemoryOrder.RELEASE) ``` ### Response These are typically enums or constants used to configure memory behavior in other operations. ``` -------------------------------- ### Execution and Memory Model Source: https://docs.nvidia.com/cuda/cutile-python/genindex Information regarding memory scopes and execution constructs like kernels and functions within the cuda.tile module. ```APIDOC ## Execution and Memory Model ### Description Details on memory scopes, kernel definitions, and function execution within CUTILE-Python. ### Memory Scope - **DEVICE**: Represents the device memory scope. ### Execution Constructs - **function**: Class for defining CUDA functions. - **kernel**: Class for defining CUDA kernels. - **launch()**: Function to launch CUDA kernels. ### Usage These components are used for defining and executing computations on the GPU. ``` -------------------------------- ### Mathematical Operations in cuda.tile Source: https://docs.nvidia.com/cuda/cutile-python/genindex This section details mathematical functions available in the cuda.tile module, such as less, less_equal, load, log, log2, matmul, max, maximum, min, minimum, mma, mod, mul, negative, not_equal, ones, pow, printf, and prod. ```APIDOC ## Mathematical Operations in cuda.tile ### Description Provides access to various mathematical operations that can be performed on CUDA tiles. ### Methods - **less(lhs, rhs)**: Performs element-wise less than comparison. - **less_equal(lhs, rhs)**: Performs element-wise less than or equal to comparison. - **load(src, layout, cache_modifier, cache_level)**: Loads data from a source into a tile. - **log(x)**: Computes the natural logarithm of each element. - **log2(x)**: Computes the base-2 logarithm of each element. - **matmul(A, B, layout_A, layout_B)**: Performs matrix multiplication. - **max(x)**: Computes the maximum value in a tile. - **maximum(lhs, rhs)**: Computes the element-wise maximum of two tiles. - **min(x)**: Computes the minimum value in a tile. - **minimum(lhs, rhs)**: Computes the element-wise minimum of two tiles. - **mma(A, B, C, layout_A, layout_B, layout_C)**: Performs matrix multiply-accumulate operation. - **mod(lhs, rhs)**: Computes the element-wise modulo. - **mul(lhs, rhs)**: Performs element-wise multiplication. - **negative(x)**: Computes the element-wise negation. - **not_equal(lhs, rhs)**: Performs element-wise not equal comparison. - **ones()**: Creates a tile filled with ones. - **pow(base, exp)**: Computes element-wise power. - **printf(fmt, *args)**: Formats and prints output (primarily for debugging). - **prod(x)**: Computes the product of all elements in a tile. ### Parameters Parameters vary per function. Refer to the specific function documentation for details. ### Request Example ```python # Example for mul import cuda.tile tile_a = cuda.tile.make_array(shape=(4, 4)) tile_b = cuda.tile.make_array(shape=(4, 4)) result_tile = cuda.tile.mul(tile_a, tile_b) ``` ### Response Responses vary depending on the function. Mathematical operations typically return tiles or scalar values. ``` -------------------------------- ### Reduction and Aggregation Functions in CUTILE Python Source: https://docs.nvidia.com/cuda/cutile-python/operations Enables performing reduction operations like sum, max, min, and product across tensors. Also includes functions for cumulative operations and finding indices of maximum/minimum values. ```python from cutile import tile # Example usage for sum (conceptual) # tensor_sum = tile.sum(tensor, dim=None) # Example usage for max (conceptual) # tensor_max = tile.max(tensor, dim=None) # Example usage for min (conceptual) # tensor_min = tile.min(tensor, dim=None) # Example usage for prod (conceptual) # tensor_prod = tile.prod(tensor, dim=None) # Example usage for argmax (conceptual) # argmax_indices = tile.argmax(tensor, dim=None) # Example usage for argmin (conceptual) # argmin_indices = tile.argmin(tensor, dim=None) # Example usage for cumsum (conceptual) # cumsum_tensor = tile.cumsum(tensor, dim=0) # Example usage for cumprod (conceptual) # cumprod_tensor = tile.cumprod(tensor, dim=0) ``` -------------------------------- ### Element-wise Mathematical Operations in CUTILE Python Source: https://docs.nvidia.com/cuda/cutile-python/operations Offers a suite of element-wise mathematical functions including arithmetic operations, exponentiation, logarithms, and trigonometric functions for tensor manipulation. ```python from cutile import tile # Example usage for add (conceptual) # result_add = tile.add(a_tensor, b_tensor) # Example usage for sub (conceptual) # result_sub = tile.sub(a_tensor, b_tensor) # Example usage for mul (conceptual) # result_mul = tile.mul(a_tensor, b_tensor) # Example usage for truediv (conceptual) # result_truediv = tile.truediv(a_tensor, b_tensor) # Example usage for floordiv (conceptual) # result_floordiv = tile.floordiv(a_tensor, b_tensor) # Example usage for cdiv (conceptual) # result_cdiv = tile.cdiv(a_tensor, b_tensor) # Example usage for pow (conceptual) # result_pow = tile.pow(base_tensor, exponent_tensor) # Example usage for mod (conceptual) # result_mod = tile.mod(a_tensor, b_tensor) # Example usage for minimum (conceptual) # result_minimum = tile.minimum(a_tensor, b_tensor) # Example usage for maximum (conceptual) # result_maximum = tile.maximum(a_tensor, b_tensor) # Example usage for negative (conceptual) # result_negative = tile.negative(tensor) # Example usage for exp (conceptual) # result_exp = tile.exp(tensor) # Example usage for exp2 (conceptual) # result_exp2 = tile.exp2(tensor) # Example usage for log (conceptual) # result_log = tile.log(tensor) # Example usage for log2 (conceptual) # result_log2 = tile.log2(tensor) # Example usage for sqrt (conceptual) # result_sqrt = tile.sqrt(tensor) # Example usage for rsqrt (conceptual) # result_rsqrt = tile.rsqrt(tensor) # Example usage for sin (conceptual) # result_sin = tile.sin(tensor) # Example usage for cos (conceptual) # result_cos = tile.cos(tensor) # Example usage for tan (conceptual) # result_tan = tile.tan(tensor) # Example usage for sinh (conceptual) # result_sinh = tile.sinh(tensor) # Example usage for cosh (conceptual) # result_cosh = tile.cosh(tensor) ``` -------------------------------- ### Matrix Multiplication and MMA in CUTILE Python Source: https://docs.nvidia.com/cuda/cutile-python/operations Provides functions for performing matrix multiplication (matmul) and specialized matrix multiply-accumulate (mma) operations, optimized for GPU performance. ```python from cutile import tile # Example usage for mma (conceptual) # result_mma = tile.mma(accumulator_tensor, a_tensor, b_tensor, ...) # Example usage for matmul (conceptual) # result_matmul = tile.matmul(a_tensor, b_tensor) ``` -------------------------------- ### CUDA CUTILE Python - Tile Information Source: https://docs.nvidia.com/cuda/cutile-python/genindex This section covers functions for retrieving information about tiles, such as the number of blocks and tiles. ```APIDOC ## Tile Information in cuda.tile ### Description Functions to query the number of blocks and tiles associated with a given tile or operation. ### Functions - **num_blocks()**: Returns the number of blocks. - **num_tiles()**: Returns the number of tiles. ### Usage Examples ```python import cuda.tile as tl # Assuming 'my_tile' is a valid tile object # num_blocks = tl.num_blocks(my_tile) # num_tiles = tl.num_tiles(my_tile) ``` ### Response - **Success Response (200)**: Returns the number of blocks or tiles as an integer. ### Response Example ```json { "num_blocks": 10, "num_tiles": 100 } ``` ``` -------------------------------- ### Bitwise Operations Source: https://docs.nvidia.com/cuda/cutile-python/operations Documentation for bitwise operations applied elementwise to tiles. ```APIDOC ## GET /cuda.tile.bitwise_and ### Description Performs elementwise bitwise AND operation on two tiles. ### Method GET ### Endpoint /cuda.tile.bitwise_and ### Parameters #### Query Parameters - **tile1** (tile) - Required - The first tile. - **tile2** (tile) - Required - The second tile. ### Response #### Success Response (200) - **result** (tile) - The resulting tile after elementwise bitwise AND. #### Response Example ```json { "result": "" } ``` ``` -------------------------------- ### CUDA Tile Math Functions (Python) Source: https://docs.nvidia.com/cuda/cutile-python/operations Provides mathematical functions such as tanh, floor, and ceil for use within CUDA kernels via the Tile API. These functions operate on tile data types. ```python from cutile import tile # Example usage (conceptual, actual implementation depends on kernel context) # result = tile.tanh(input_tile) # result = tile.floor(input_tile) # result = tile.ceil(input_tile) ```