2021-03-06 14:03:01 -05:00
|
|
|
"""
|
|
|
|
Vector Addition
|
|
|
|
=================
|
2021-03-06 22:04:00 -05:00
|
|
|
In this tutorial, you will write a simple vector addition using Triton and learn about:
|
2021-03-06 17:26:49 -05:00
|
|
|
|
2021-07-22 22:45:19 -07:00
|
|
|
- The basic programming model of Triton
|
|
|
|
- The `triton.jit` decorator, which is used to define Triton kernels.
|
|
|
|
- The best practices for validating and benchmarking your custom ops against native reference implementations
|
2021-03-06 14:03:01 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
# %%
|
2021-03-06 17:26:49 -05:00
|
|
|
# Compute Kernel
|
2021-03-06 14:03:01 -05:00
|
|
|
# --------------------------
|
|
|
|
|
2021-04-22 10:27:02 -04:00
|
|
|
import torch
|
2022-01-06 14:34:17 -08:00
|
|
|
|
2021-04-22 10:27:02 -04:00
|
|
|
import triton
|
2021-08-05 12:27:06 -04:00
|
|
|
import triton.language as tl
|
2021-04-22 10:27:02 -04:00
|
|
|
|
2021-03-06 14:03:01 -05:00
|
|
|
|
2021-04-20 22:29:40 -04:00
|
|
|
@triton.jit
|
2021-08-05 12:27:06 -04:00
|
|
|
def add_kernel(
|
|
|
|
x_ptr, # *Pointer* to first input vector
|
|
|
|
y_ptr, # *Pointer* to second input vector
|
|
|
|
output_ptr, # *Pointer* to output vector
|
|
|
|
n_elements, # Size of the vector
|
2021-10-30 00:32:58 -07:00
|
|
|
BLOCK_SIZE: tl.constexpr, # Number of elements each program should process
|
|
|
|
# NOTE: `constexpr` so it can be used as a shape value
|
2021-04-20 22:29:40 -04:00
|
|
|
):
|
2021-08-05 12:27:06 -04:00
|
|
|
# There are multiple 'program's processing different data. We identify which program
|
|
|
|
# we are here
|
|
|
|
pid = tl.program_id(axis=0) # We use a 1D launch grid so axis is 0
|
|
|
|
# This program will process inputs that are offset from the initial data.
|
|
|
|
# for instance, if you had a vector of length 256 and block_size of 64, the programs
|
|
|
|
# would each access the elements [0:64, 64:128, 128:192, 192:256].
|
|
|
|
# Note that offsets is a list of pointers
|
|
|
|
block_start = pid * BLOCK_SIZE
|
|
|
|
offsets = block_start + tl.arange(0, BLOCK_SIZE)
|
|
|
|
# Create a mask to guard memory operations against out-of-bounds accesses
|
|
|
|
mask = offsets < n_elements
|
2021-12-29 18:09:34 -05:00
|
|
|
# Load x and y from DRAM, masking out any extra elements in case the input is not a
|
2021-08-05 12:27:06 -04:00
|
|
|
# multiple of the block size
|
|
|
|
x = tl.load(x_ptr + offsets, mask=mask)
|
|
|
|
y = tl.load(y_ptr + offsets, mask=mask)
|
|
|
|
output = x + y
|
|
|
|
# Write x + y back to DRAM
|
2021-09-02 22:02:40 -07:00
|
|
|
tl.store(output_ptr + offsets, output, mask=mask)
|
2021-03-06 17:26:49 -05:00
|
|
|
|
2021-03-06 14:03:01 -05:00
|
|
|
|
2021-03-06 22:04:00 -05:00
|
|
|
# %%
|
2021-07-28 01:51:17 -07:00
|
|
|
# Let's also declare a helper function to (1) allocate the `z` tensor
|
|
|
|
# and (2) enqueue the above kernel with appropriate grid/block sizes.
|
2021-04-20 22:29:40 -04:00
|
|
|
|
|
|
|
|
2021-08-05 12:27:06 -04:00
|
|
|
def add(x: torch.Tensor, y: torch.Tensor):
|
|
|
|
# We need to preallocate the output
|
|
|
|
output = torch.empty_like(x)
|
|
|
|
assert x.is_cuda and y.is_cuda and output.is_cuda
|
2021-08-20 08:05:12 +05:30
|
|
|
n_elements = output.numel()
|
2021-07-22 22:45:19 -07:00
|
|
|
# The SPMD launch grid denotes the number of kernel instances that run in parallel.
|
2021-04-20 22:29:40 -04:00
|
|
|
# It is analogous to CUDA launch grids. It can be either Tuple[int], or Callable(metaparameters) -> Tuple[int]
|
2021-08-05 12:27:06 -04:00
|
|
|
# In this case, we use a 1D grid where the size is the number of blocks
|
|
|
|
grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)
|
2021-04-20 22:29:40 -04:00
|
|
|
# NOTE:
|
2021-07-22 22:45:19 -07:00
|
|
|
# - each torch.tensor object is implicitly converted into a pointer to its first element.
|
2022-11-14 10:15:53 +08:00
|
|
|
# - `triton.jit`'ed functions can be indexed with a launch grid to obtain a callable GPU kernel
|
2021-04-20 22:29:40 -04:00
|
|
|
# - don't forget to pass meta-parameters as keywords arguments
|
2022-01-07 15:28:36 -08:00
|
|
|
add_kernel[grid](x, y, output, n_elements, BLOCK_SIZE=1024)
|
2021-04-20 22:29:40 -04:00
|
|
|
# We return a handle to z but, since `torch.cuda.synchronize()` hasn't been called, the kernel is still
|
2021-07-22 22:45:19 -07:00
|
|
|
# running asynchronously at this point.
|
2021-08-05 12:27:06 -04:00
|
|
|
return output
|
2021-04-20 22:29:40 -04:00
|
|
|
|
2021-03-06 22:04:00 -05:00
|
|
|
|
2021-03-06 14:03:01 -05:00
|
|
|
# %%
|
2021-07-22 22:45:19 -07:00
|
|
|
# We can now use the above function to compute the element-wise sum of two `torch.tensor` objects and test its correctness:
|
2021-03-06 22:04:00 -05:00
|
|
|
|
2021-03-06 14:03:01 -05:00
|
|
|
torch.manual_seed(0)
|
2021-04-20 22:29:40 -04:00
|
|
|
size = 98432
|
|
|
|
x = torch.rand(size, device='cuda')
|
|
|
|
y = torch.rand(size, device='cuda')
|
2021-08-05 12:27:06 -04:00
|
|
|
output_torch = x + y
|
|
|
|
output_triton = add(x, y)
|
|
|
|
print(output_torch)
|
|
|
|
print(output_triton)
|
|
|
|
print(
|
|
|
|
f'The maximum difference between torch and triton is '
|
|
|
|
f'{torch.max(torch.abs(output_torch - output_triton))}'
|
|
|
|
)
|
2021-03-06 14:03:01 -05:00
|
|
|
|
2021-03-06 22:04:00 -05:00
|
|
|
# %%
|
|
|
|
# Seems like we're good to go!
|
|
|
|
|
2021-03-06 14:03:01 -05:00
|
|
|
# %%
|
2021-03-14 18:49:59 -04:00
|
|
|
# Benchmark
|
|
|
|
# -----------
|
2021-07-28 01:51:17 -07:00
|
|
|
# We can now benchmark our custom op on vectors of increasing sizes to get a sense of how it does relative to PyTorch.
|
2021-07-22 22:45:19 -07:00
|
|
|
# To make things easier, Triton has a set of built-in utilities that allow us to concisely plot the performance of your custom ops
|
2021-03-11 00:29:16 -05:00
|
|
|
# for different problem sizes.
|
|
|
|
|
|
|
|
|
|
|
|
@triton.testing.perf_report(
|
|
|
|
triton.testing.Benchmark(
|
|
|
|
x_names=['size'], # argument names to use as an x-axis for the plot
|
2021-08-05 12:27:06 -04:00
|
|
|
x_vals=[
|
|
|
|
2 ** i for i in range(12, 28, 1)
|
|
|
|
], # different possible values for `x_name`
|
2021-03-11 00:29:16 -05:00
|
|
|
x_log=True, # x axis is logarithmic
|
2021-04-23 17:18:14 -04:00
|
|
|
line_arg='provider', # argument name whose value corresponds to a different line in the plot
|
2021-07-22 22:45:19 -07:00
|
|
|
line_vals=['triton', 'torch'], # possible values for `line_arg`
|
2021-08-05 12:27:06 -04:00
|
|
|
line_names=['Triton', 'Torch'], # label name for the lines
|
2021-07-22 22:45:19 -07:00
|
|
|
styles=[('blue', '-'), ('green', '-')], # line styles
|
2021-08-05 12:27:06 -04:00
|
|
|
ylabel='GB/s', # label name for the y-axis
|
|
|
|
plot_name='vector-add-performance', # name for the plot. Used also as a file name for saving the plot.
|
|
|
|
args={}, # values for function arguments not in `x_names` and `y_name`
|
2021-03-11 00:29:16 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
def benchmark(size, provider):
|
|
|
|
x = torch.rand(size, device='cuda', dtype=torch.float32)
|
|
|
|
y = torch.rand(size, device='cuda', dtype=torch.float32)
|
|
|
|
if provider == 'torch':
|
|
|
|
ms, min_ms, max_ms = triton.testing.do_bench(lambda: x + y)
|
|
|
|
if provider == 'triton':
|
|
|
|
ms, min_ms, max_ms = triton.testing.do_bench(lambda: add(x, y))
|
|
|
|
gbps = lambda ms: 12 * size / ms * 1e-6
|
|
|
|
return gbps(ms), gbps(max_ms), gbps(min_ms)
|
2021-03-06 22:04:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
# %%
|
2021-07-28 01:51:17 -07:00
|
|
|
# We can now run the decorated function above. Pass `print_data=True` to see the performance number, `show_plots=True` to plot them, and/or
|
2021-03-11 00:29:16 -05:00
|
|
|
# `save_path='/path/to/results/' to save them to disk along with raw CSV data
|
2022-03-30 13:32:52 +08:00
|
|
|
# benchmark.run(print_data=True, show_plots=True)
|