|
| 1 | +""" |
| 2 | +Low mem dropout Example |
| 3 | +================ |
| 4 | +
|
| 5 | +This example demonstrates how to implement a Low mem dropout using Helion. |
| 6 | +""" |
| 7 | + |
| 8 | +# %% |
| 9 | +# Imports |
| 10 | +# ------- |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from typing import Callable |
| 14 | + |
| 15 | +import torch |
| 16 | + |
| 17 | +import helion |
| 18 | +import helion.language as hl |
| 19 | + |
| 20 | + |
| 21 | +# %% |
| 22 | +# Low mem dropout forward implementations |
| 23 | +# ------------------- |
| 24 | +@helion.kernel() |
| 25 | +def low_mem_dropout(p: float, x: torch.Tensor) -> torch.Tensor: |
| 26 | + """ |
| 27 | + Applies dropout on x using p |
| 28 | + Args: |
| 29 | + p (float): dropout probability |
| 30 | + x (torch.Tensor): input tensor |
| 31 | + Returns: |
| 32 | + Output tensor, mask tensor |
| 33 | + """ |
| 34 | + scale = 1.0 / (1.0 - p) |
| 35 | + # flatten to 1D so we can use tile |
| 36 | + n = x.numel() |
| 37 | + x_flat = x.view(-1) |
| 38 | + out_flat = torch.empty_like(x_flat) |
| 39 | + mask_flat = torch.empty_like(x_flat, dtype=torch.bool) |
| 40 | + |
| 41 | + for tidx in hl.tile(n): |
| 42 | + xi = x_flat[tidx].to(torch.float32) |
| 43 | + r = torch.rand_like(xi, dtype=torch.float32) |
| 44 | + keep = r > p |
| 45 | + yscaled = xi * scale |
| 46 | + yi = torch.where(keep, yscaled, 0.0) |
| 47 | + out_flat[tidx] = yi.to(x.dtype) |
| 48 | + mask_flat[tidx] = keep |
| 49 | + return out_flat.view_as(x), mask_flat.view_as(x) |
| 50 | + |
| 51 | + |
| 52 | +# %% |
| 53 | +# Low mem dropout backward implementation |
| 54 | +# ------------------- |
| 55 | +@helion.kernel() |
| 56 | +def low_mem_dropout_bwd(p: float, grad_y: torch.Tensor) -> torch.Tensor: |
| 57 | + """ |
| 58 | + For low mem dropout we are applying randomness inside both fwd and bwd |
| 59 | + technically dropout bwd is same as fwd |
| 60 | + Args: |
| 61 | + p (float): Dropout probability |
| 62 | + grad_y (torch.Tensor): Gradient tensor |
| 63 | + Returns: |
| 64 | + Output tensor, mask tensor |
| 65 | + """ |
| 66 | + scale = 1.0 / (1.0 - p) |
| 67 | + n = grad_y.numel() |
| 68 | + grad_y_flat = grad_y.view(-1) |
| 69 | + out_flat = torch.empty_like(grad_y_flat) |
| 70 | + mask_flat = torch.empty_like(grad_y_flat, dtype=torch.bool) |
| 71 | + for tidx in hl.tile(n): |
| 72 | + gi = grad_y_flat[tidx].to(torch.float32) |
| 73 | + r = torch.rand_like(gi, dtype=torch.float32) |
| 74 | + keep = r > p |
| 75 | + g_scaled = gi * scale |
| 76 | + gxi = torch.where(keep, g_scaled, 0.0) |
| 77 | + out_flat[tidx] = gxi.to(grad_y.dtype) |
| 78 | + mask_flat[tidx] = keep |
| 79 | + return out_flat.view_as(grad_y), mask_flat.view_as(grad_y) |
| 80 | + |
| 81 | + |
| 82 | +# %% |
| 83 | +# TritonBench Wrapper |
| 84 | +# ------------------- |
| 85 | +def low_mem_dropout_tritonbench(tb_op: object, p: float, x: torch.Tensor) -> Callable: |
| 86 | + """ |
| 87 | + Wrapper for TritonBench compatibility. |
| 88 | +
|
| 89 | + Args: |
| 90 | + tb_op: TritonBench operator instance |
| 91 | + p (float): dropout probability |
| 92 | + x (torch.Tensor): Input tensor |
| 93 | +
|
| 94 | + Returns: |
| 95 | + Callable: A function that performs the low_mem_dropout. |
| 96 | + """ |
| 97 | + |
| 98 | + def _inner() -> torch.Tensor: |
| 99 | + out, _ = low_mem_dropout(p, x) |
| 100 | + return out |
| 101 | + |
| 102 | + return _inner |
| 103 | + |
| 104 | + |
| 105 | +# %% |
| 106 | +# Verification Function |
| 107 | +# ------------------- |
| 108 | +def check(p: float, size: int) -> None: |
| 109 | + """ |
| 110 | + Verify the low mem dropout kernel implementation against PyTorch's native dropout implementation. |
| 111 | +
|
| 112 | + Args: |
| 113 | + p (float): dropout probability |
| 114 | + size (int): input tensor size |
| 115 | + """ |
| 116 | + |
| 117 | + x = torch.randn(size=(size,)).cuda() |
| 118 | + |
| 119 | + torch.manual_seed(123) |
| 120 | + y, fwd_mask = low_mem_dropout(p, x) |
| 121 | + |
| 122 | + # need to set seed again else we can't reproduce |
| 123 | + torch.manual_seed(123) |
| 124 | + grad_y = torch.ones_like(x) |
| 125 | + grad_x, bwd_mask = low_mem_dropout_bwd(p, grad_y) |
| 126 | + assert torch.equal(fwd_mask, bwd_mask) |
| 127 | + |
| 128 | + |
| 129 | +# %% |
| 130 | +# Main Function |
| 131 | +# ----------- |
| 132 | +def main() -> None: |
| 133 | + """ |
| 134 | + Main entry point that runs the low mem dropout kernel verification with different tensor sizes. |
| 135 | + Tests with two configurations: |
| 136 | + - p=0.25, s=8192 |
| 137 | + - p=0.25, s=32768 |
| 138 | + """ |
| 139 | + check(0.25, 8192) |
| 140 | + check(0.25, 32768) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
0 commit comments