|
| 1 | +import logging |
| 2 | + |
| 3 | +import torch |
| 4 | +import triton |
| 5 | +import triton.language as tl |
| 6 | + |
| 7 | +from flag_gems.runtime import torch_device_fn |
| 8 | +from flag_gems.utils import pointwise_dynamic |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | +_FALLBACK_KEYSET = torch._C.DispatchKeySet( |
| 13 | + torch._C.DispatchKey.CompositeExplicitAutograd |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pointwise_dynamic(is_tensor=[True, False], promotion_methods=[(0, "DEFAULT")]) |
| 18 | +@triton.jit |
| 19 | +def leaky_relu_fallback(x, negative_slope): |
| 20 | + return tl.where(x > 0, x, x * negative_slope) |
| 21 | + |
| 22 | + |
| 23 | +@triton.autotune( |
| 24 | + configs=[ |
| 25 | + triton.Config({"BLOCK_SIZE": 1024}, num_warps=4), |
| 26 | + triton.Config({"BLOCK_SIZE": 1024}, num_warps=8), |
| 27 | + triton.Config({"BLOCK_SIZE": 2048}, num_warps=8), |
| 28 | + triton.Config({"BLOCK_SIZE": 4096}, num_warps=4), |
| 29 | + triton.Config({"BLOCK_SIZE": 4096}, num_warps=8), |
| 30 | + triton.Config({"BLOCK_SIZE": 8192}, num_warps=4), |
| 31 | + triton.Config({"BLOCK_SIZE": 8192}, num_warps=8), |
| 32 | + triton.Config({"BLOCK_SIZE": 16384}, num_warps=4), |
| 33 | + triton.Config({"BLOCK_SIZE": 16384}, num_warps=8), |
| 34 | + ], |
| 35 | + key=["n_elements"], |
| 36 | +) |
| 37 | +@triton.jit |
| 38 | +def leaky_relu_kernel_fp16( |
| 39 | + x_ptr, out_ptr, negative_slope, n_elements, BLOCK_SIZE: tl.constexpr |
| 40 | +): |
| 41 | + pid = tl.program_id(axis=0) |
| 42 | + block_start = pid * BLOCK_SIZE |
| 43 | + offsets = block_start + tl.arange(0, BLOCK_SIZE) |
| 44 | + mask = offsets < n_elements |
| 45 | + |
| 46 | + x = tl.load(x_ptr + offsets, mask=mask) |
| 47 | + y = tl.where(x > 0, x, x * negative_slope) |
| 48 | + tl.store(out_ptr + offsets, y, mask=mask) |
| 49 | + |
| 50 | + |
| 51 | +@triton.autotune( |
| 52 | + configs=[ |
| 53 | + triton.Config({"BLOCK_SIZE": 1024}, num_warps=4), |
| 54 | + triton.Config({"BLOCK_SIZE": 1024}, num_warps=8), |
| 55 | + triton.Config({"BLOCK_SIZE": 4096}, num_warps=8), |
| 56 | + triton.Config({"BLOCK_SIZE": 8192}, num_warps=8), |
| 57 | + triton.Config({"BLOCK_SIZE": 16384}, num_warps=4), |
| 58 | + triton.Config({"BLOCK_SIZE": 32768}, num_warps=8), |
| 59 | + ], |
| 60 | + key=["n_elements"], |
| 61 | +) |
| 62 | +@triton.jit |
| 63 | +def leaky_relu_kernel_fp32( |
| 64 | + x_ptr, out_ptr, negative_slope, n_elements, BLOCK_SIZE: tl.constexpr |
| 65 | +): |
| 66 | + pid = tl.program_id(axis=0) |
| 67 | + block_start = pid * BLOCK_SIZE |
| 68 | + offsets = block_start + tl.arange(0, BLOCK_SIZE) |
| 69 | + mask = offsets < n_elements |
| 70 | + |
| 71 | + x = tl.load(x_ptr + offsets, mask=mask) |
| 72 | + y = tl.where(x > 0, x, x * negative_slope) |
| 73 | + tl.store(out_ptr + offsets, y, mask=mask) |
| 74 | + |
| 75 | + |
| 76 | +def _get_fast_kernel(inp): |
| 77 | + if inp.dtype in (torch.float16, torch.bfloat16): |
| 78 | + return leaky_relu_kernel_fp16 |
| 79 | + if inp.dtype == torch.float32: |
| 80 | + return leaky_relu_kernel_fp32 |
| 81 | + return None |
| 82 | + |
| 83 | + |
| 84 | +def _leaky_relu_contiguous(inp, negative_slope, out): |
| 85 | + n_elements = inp.numel() |
| 86 | + if n_elements == 0: |
| 87 | + return out |
| 88 | + kernel = _get_fast_kernel(inp) |
| 89 | + if kernel is None: |
| 90 | + return leaky_relu_fallback(inp, negative_slope, out0=out) |
| 91 | + |
| 92 | + grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),) |
| 93 | + with torch_device_fn.device(inp.device): |
| 94 | + kernel[grid]( |
| 95 | + inp, |
| 96 | + out, |
| 97 | + negative_slope, |
| 98 | + n_elements, |
| 99 | + ) |
| 100 | + return out |
| 101 | + |
| 102 | + |
| 103 | +def _can_use_fast_path(inp): |
| 104 | + return ( |
| 105 | + inp.layout == torch.strided |
| 106 | + and inp.is_cuda |
| 107 | + and not inp.is_quantized |
| 108 | + and not inp.is_complex() |
| 109 | + and inp.is_contiguous() |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def leaky_relu(inp, negative_slope=0.01): |
| 114 | + logger.debug("GEMS LEAKY_RELU") |
| 115 | + if _can_use_fast_path(inp): |
| 116 | + return _leaky_relu_contiguous(inp, negative_slope, torch.empty_like(inp)) |
| 117 | + if not inp.is_cuda or inp.is_complex(): |
| 118 | + return torch.ops.aten.leaky_relu.default.redispatch( |
| 119 | + _FALLBACK_KEYSET, inp, negative_slope |
| 120 | + ) |
| 121 | + return leaky_relu_fallback(inp, negative_slope) |
| 122 | + |
| 123 | + |
| 124 | +def leaky_relu_(inp, negative_slope=0.01): |
| 125 | + logger.debug("GEMS LEAKY_RELU_") |
| 126 | + if _can_use_fast_path(inp): |
| 127 | + return _leaky_relu_contiguous(inp, negative_slope, inp) |
| 128 | + if not inp.is_cuda or inp.is_complex(): |
| 129 | + return torch.ops.aten.leaky_relu_.default.redispatch( |
| 130 | + _FALLBACK_KEYSET, inp, negative_slope |
| 131 | + ) |
| 132 | + return leaky_relu_fallback(inp, negative_slope, out0=inp) |
| 133 | + |
| 134 | + |
| 135 | +def leaky_relu_out(inp, negative_slope=0.01, *, out): |
| 136 | + logger.debug("GEMS LEAKY_RELU_OUT") |
| 137 | + if ( |
| 138 | + not _can_use_fast_path(inp) |
| 139 | + or out.layout != torch.strided |
| 140 | + or out.device != inp.device |
| 141 | + or out.dtype != inp.dtype |
| 142 | + ): |
| 143 | + return torch.ops.aten.leaky_relu.out.redispatch( |
| 144 | + _FALLBACK_KEYSET, inp, negative_slope, out=out |
| 145 | + ) |
| 146 | + |
| 147 | + if out.shape != inp.shape: |
| 148 | + out.resize_(inp.shape) |
| 149 | + |
| 150 | + if out.is_contiguous(): |
| 151 | + return _leaky_relu_contiguous(inp, negative_slope, out) |
| 152 | + leaky_relu_fallback(inp, negative_slope, out0=out) |
| 153 | + return out |
0 commit comments