Repository navigation
Misc inference fixes #4397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Misc inference fixes #4397
Changes from 6 commits
1376e4b
658454b
d5c5001
65caf92
b199aa1
61c62b8
c1b5c26
2b1e854
cd10037
c744556
908d772
823aa26
9a98c55
131286f
6df6a9c
93244f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ | |
| # Copyright (c) 2024, Tri Dao, Albert Gu. | ||
|
|
||
| # Some of this code was adopted from https://github.com/state-spaces/mamba/ | ||
| # and https://github.com/vllm-project/vllm. | ||
| # This source code is licensed under the Apache license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
|
|
||
| import torch | ||
| from packaging import version | ||
|
|
||
|
|
@@ -41,6 +43,12 @@ def softplus(dt): | |
| return tl.math.log1p(tl.exp(dt)) | ||
|
|
||
|
|
||
| @triton.jit | ||
| def fast_exp(x): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have seen this trick before but does this change numerics at all, as in is accuracy of models training without it giving the same accuracy? also assuming this is used for both training and inference so we dont get larger train/inference diff
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if these kernels are used in training. I do not see a backwards pass here, so I assume they are not. Concerns about numerics are valid. This trick is borrowed from vLLM, so atleast it has some precedent.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we verify that our functional tests still pass at least? |
||
| LOG2E = tl.constexpr(1.4426950408889634) | ||
| return tl.math.exp2(LOG2E * x) | ||
|
|
||
|
|
||
| @triton.heuristics({"HAS_DT_BIAS": lambda args: args["dt_bias_ptr"] is not None}) | ||
| @triton.heuristics({"HAS_D": lambda args: args["D_ptr"] is not None}) | ||
| @triton.heuristics({"HAS_Z": lambda args: args["z_ptr"] is not None}) | ||
|
|
@@ -49,7 +57,7 @@ def softplus(dt): | |
| ) | ||
| @triton.heuristics({"HAS_INT_STATE": lambda args: args["int_state_ptr"] is not None}) | ||
| @triton.heuristics({"BLOCK_SIZE_DSTATE": lambda args: triton.next_power_of_2(args["dstate"])}) | ||
| @triton.jit | ||
| @triton.jit(do_not_specialize=["batch"]) | ||
| def _selective_scan_update_kernel( | ||
| # Pointers to matrices | ||
| state_ptr, | ||
|
|
@@ -223,14 +231,14 @@ def _selective_scan_update_kernel( | |
| dt += tl.load(dt_bias_ptrs, mask=offs_m < dim, other=0.0).to(tl.float32) | ||
| if DT_SOFTPLUS: | ||
| dt = tl.where(dt <= 20.0, softplus(dt), dt) | ||
| dA = tl.exp(A * dt[:, None]) | ||
| dA = fast_exp(A * dt[:, None]) | ||
| else: | ||
| dt = tl.load(dt_ptr + s * stride_dt_seq).to(tl.float32) | ||
| if HAS_DT_BIAS: | ||
| dt += tl.load(dt_bias_ptr).to(tl.float32) | ||
| if DT_SOFTPLUS: | ||
| dt = tl.where(dt <= 20.0, softplus(dt), dt) | ||
| dA = tl.exp(A * dt) | ||
| dA = fast_exp(A * dt) | ||
|
|
||
| # Load B and C | ||
| B = tl.load(B_s_ptrs, mask=offs_n < dstate, other=0.0).to(tl.float32) | ||
|
|
@@ -368,15 +376,24 @@ def selective_state_update( | |
| (z.stride(0), z.stride(1), z.stride(2), z.stride(3)) if z is not None else (0, 0, 0, 0) | ||
| ) | ||
|
|
||
| BLOCK_SIZE_M, num_warps = ( | ||
| (32, 4) | ||
| if dstate <= 16 | ||
| else ( | ||
| (16, 4) | ||
| if dstate <= 32 | ||
| else ((8, 4) if dstate <= 64 else ((4, 4) if dstate <= 128 else ((4, 8)))) | ||
| ) | ||
| ) | ||
| is_blackwell = torch.cuda.get_device_capability(x.device)[0] >= 10 | ||
|
|
||
|
|
||
| # Default | ||
| BLOCK_SIZE_M, num_warps = 4, 8 | ||
| if dstate <= 16: | ||
| BLOCK_SIZE_M, num_warps = 32, 4 | ||
| elif dstate <= 32: | ||
| BLOCK_SIZE_M, num_warps = 16, 4 | ||
| elif dstate <= 64: | ||
| BLOCK_SIZE_M, num_warps = 8, 4 | ||
| else: | ||
| # dstate > 64 | ||
| if is_blackwell: | ||
| # Optimized for B200 with dstate>64 | ||
| BLOCK_SIZE_M, num_warps = 32, 8 | ||
| elif dstate <= 128: | ||
| BLOCK_SIZE_M, num_warps = 4, 4 | ||
|
|
||
| tie_hdim = ( | ||
| A.stride(-1) == 0 | ||
|
|
@@ -451,4 +468,4 @@ def selective_state_update( | |
| if is_seq_unsq: | ||
| out = out.squeeze(1) | ||
|
|
||
| return out | ||
| return out | ||
Uh oh!
There was an error while loading. Please reload this page.