Commit
·
a20b2e3
0
Parent(s):
Add activation
Browse files- .gitattributes +36 -0
- README.md +5 -0
- activation/activation_kernels.cu +204 -0
- activation/cuda_compat.h +49 -0
- activation/dispatch_utils.h +35 -0
- build.toml +20 -0
- ext-torch/activation/__init__.py +47 -0
- ext-torch/registration.h +27 -0
- ext-torch/torch_binding.cpp +37 -0
- ext-torch/torch_binding.h +18 -0
- flake.nix +14 -0
- tests/__init__.py +0 -0
- tests/kernels/__init__.py +0 -0
- tests/kernels/allclose_default.py +14 -0
- tests/kernels/test_activation.py +139 -0
- tests/kernels/utils.py +73 -0
.gitattributes
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.so filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Activation
|
2 |
+
|
3 |
+
Activation kernels from [vLLM](https://github.com/vllm-project/vllm/blob/main/csrc/activation_kernels.cu).
|
4 |
+
|
5 |
+
This repository is for testing only.
|
activation/activation_kernels.cu
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <ATen/cuda/CUDAContext.h>
|
2 |
+
#include <torch/all.h>
|
3 |
+
#include <c10/cuda/CUDAGuard.h>
|
4 |
+
|
5 |
+
#include <cmath>
|
6 |
+
|
7 |
+
#include "cuda_compat.h"
|
8 |
+
#include "dispatch_utils.h"
|
9 |
+
|
10 |
+
namespace vllm {
|
11 |
+
|
12 |
+
// Activation and gating kernel template.
|
13 |
+
template <typename scalar_t, scalar_t (*ACT_FN)(const scalar_t&)>
|
14 |
+
__global__ void act_and_mul_kernel(
|
15 |
+
scalar_t* __restrict__ out, // [..., d]
|
16 |
+
const scalar_t* __restrict__ input, // [..., 2, d]
|
17 |
+
const int d) {
|
18 |
+
const int64_t token_idx = blockIdx.x;
|
19 |
+
for (int64_t idx = threadIdx.x; idx < d; idx += blockDim.x) {
|
20 |
+
const scalar_t x = VLLM_LDG(&input[token_idx * 2 * d + idx]);
|
21 |
+
const scalar_t y = VLLM_LDG(&input[token_idx * 2 * d + d + idx]);
|
22 |
+
out[token_idx * d + idx] = ACT_FN(x) * y;
|
23 |
+
}
|
24 |
+
}
|
25 |
+
|
26 |
+
template <typename T>
|
27 |
+
__device__ __forceinline__ T silu_kernel(const T& x) {
|
28 |
+
// x * sigmoid(x)
|
29 |
+
return (T)(((float)x) / (1.0f + expf((float)-x)));
|
30 |
+
}
|
31 |
+
|
32 |
+
template <typename T>
|
33 |
+
__device__ __forceinline__ T gelu_kernel(const T& x) {
|
34 |
+
// Equivalent to PyTorch GELU with 'none' approximation.
|
35 |
+
// Refer to:
|
36 |
+
// https://github.com/pytorch/pytorch/blob/8ac9b20d4b090c213799e81acf48a55ea8d437d6/aten/src/ATen/native/cuda/ActivationGeluKernel.cu#L36-L38
|
37 |
+
const float f = (float)x;
|
38 |
+
constexpr float ALPHA = M_SQRT1_2;
|
39 |
+
return (T)(f * 0.5f * (1.0f + ::erf(f * ALPHA)));
|
40 |
+
}
|
41 |
+
|
42 |
+
template <typename T>
|
43 |
+
__device__ __forceinline__ T gelu_tanh_kernel(const T& x) {
|
44 |
+
// Equivalent to PyTorch GELU with 'tanh' approximation.
|
45 |
+
// Refer to:
|
46 |
+
// https://github.com/pytorch/pytorch/blob/8ac9b20d4b090c213799e81acf48a55ea8d437d6/aten/src/ATen/native/cuda/ActivationGeluKernel.cu#L25-L30
|
47 |
+
const float f = (float)x;
|
48 |
+
constexpr float BETA = M_SQRT2 * M_2_SQRTPI * 0.5f;
|
49 |
+
constexpr float KAPPA = 0.044715;
|
50 |
+
float x_cube = f * f * f;
|
51 |
+
float inner = BETA * (f + KAPPA * x_cube);
|
52 |
+
return (T)(0.5f * f * (1.0f + ::tanhf(inner)));
|
53 |
+
}
|
54 |
+
|
55 |
+
} // namespace vllm
|
56 |
+
|
57 |
+
// Launch activation and gating kernel.
|
58 |
+
#define LAUNCH_ACTIVATION_GATE_KERNEL(KERNEL) \
|
59 |
+
int d = input.size(-1) / 2; \
|
60 |
+
int64_t num_tokens = input.numel() / input.size(-1); \
|
61 |
+
dim3 grid(num_tokens); \
|
62 |
+
dim3 block(std::min(d, 1024)); \
|
63 |
+
const at::cuda::OptionalCUDAGuard device_guard(device_of(input)); \
|
64 |
+
const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); \
|
65 |
+
VLLM_DISPATCH_FLOATING_TYPES( \
|
66 |
+
input.scalar_type(), "act_and_mul_kernel", [&] { \
|
67 |
+
vllm::act_and_mul_kernel<scalar_t, KERNEL<scalar_t>> \
|
68 |
+
<<<grid, block, 0, stream>>>(out.data_ptr<scalar_t>(), \
|
69 |
+
input.data_ptr<scalar_t>(), d); \
|
70 |
+
});
|
71 |
+
|
72 |
+
void silu_and_mul(torch::Tensor& out, // [..., d]
|
73 |
+
torch::Tensor& input) // [..., 2 * d]
|
74 |
+
{
|
75 |
+
LAUNCH_ACTIVATION_GATE_KERNEL(vllm::silu_kernel);
|
76 |
+
}
|
77 |
+
|
78 |
+
void gelu_and_mul(torch::Tensor& out, // [..., d]
|
79 |
+
torch::Tensor& input) // [..., 2 * d]
|
80 |
+
{
|
81 |
+
LAUNCH_ACTIVATION_GATE_KERNEL(vllm::gelu_kernel);
|
82 |
+
}
|
83 |
+
|
84 |
+
void gelu_tanh_and_mul(torch::Tensor& out, // [..., d]
|
85 |
+
torch::Tensor& input) // [..., 2 * d]
|
86 |
+
{
|
87 |
+
LAUNCH_ACTIVATION_GATE_KERNEL(vllm::gelu_tanh_kernel);
|
88 |
+
}
|
89 |
+
|
90 |
+
namespace vllm {
|
91 |
+
|
92 |
+
template <typename T>
|
93 |
+
__device__ __forceinline__ T fatrelu_kernel(const T& x, const float threshold) {
|
94 |
+
const float f = (float)x;
|
95 |
+
return (T)(f > threshold ? f : 0.0f);
|
96 |
+
}
|
97 |
+
|
98 |
+
template <typename scalar_t, scalar_t (*ACT_FN)(const scalar_t&, const float)>
|
99 |
+
__global__ void act_and_mul_kernel_with_param(
|
100 |
+
scalar_t* __restrict__ out, const scalar_t* __restrict__ input, const int d,
|
101 |
+
const float param) {
|
102 |
+
const int64_t token_idx = blockIdx.x;
|
103 |
+
for (int64_t idx = threadIdx.x; idx < d; idx += blockDim.x) {
|
104 |
+
const scalar_t x = VLLM_LDG(&input[token_idx * 2 * d + idx]);
|
105 |
+
const scalar_t y = VLLM_LDG(&input[token_idx * 2 * d + d + idx]);
|
106 |
+
out[token_idx * d + idx] = ACT_FN(x, param) * y;
|
107 |
+
}
|
108 |
+
}
|
109 |
+
|
110 |
+
} // namespace vllm
|
111 |
+
|
112 |
+
#define LAUNCH_ACTIVATION_GATE_KERNEL_WITH_PARAM(KERNEL, PARAM) \
|
113 |
+
int d = input.size(-1) / 2; \
|
114 |
+
int64_t num_tokens = input.numel() / input.size(-1); \
|
115 |
+
dim3 grid(num_tokens); \
|
116 |
+
dim3 block(std::min(d, 1024)); \
|
117 |
+
const at::cuda::OptionalCUDAGuard device_guard(device_of(input)); \
|
118 |
+
const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); \
|
119 |
+
VLLM_DISPATCH_FLOATING_TYPES( \
|
120 |
+
input.scalar_type(), "act_and_mul_kernel_with_param", [&] { \
|
121 |
+
vllm::act_and_mul_kernel_with_param<scalar_t, KERNEL<scalar_t>> \
|
122 |
+
<<<grid, block, 0, stream>>>(out.data_ptr<scalar_t>(), \
|
123 |
+
input.data_ptr<scalar_t>(), d, \
|
124 |
+
PARAM); \
|
125 |
+
});
|
126 |
+
|
127 |
+
void fatrelu_and_mul(torch::Tensor& out, // [..., d],
|
128 |
+
torch::Tensor& input, // [..., 2 * d]
|
129 |
+
double threshold) {
|
130 |
+
LAUNCH_ACTIVATION_GATE_KERNEL_WITH_PARAM(vllm::fatrelu_kernel, threshold);
|
131 |
+
}
|
132 |
+
namespace vllm {
|
133 |
+
|
134 |
+
// Element-wise activation kernel template.
|
135 |
+
template <typename scalar_t, scalar_t (*ACT_FN)(const scalar_t&)>
|
136 |
+
__global__ void activation_kernel(
|
137 |
+
scalar_t* __restrict__ out, // [..., d]
|
138 |
+
const scalar_t* __restrict__ input, // [..., d]
|
139 |
+
const int d) {
|
140 |
+
const int64_t token_idx = blockIdx.x;
|
141 |
+
for (int64_t idx = threadIdx.x; idx < d; idx += blockDim.x) {
|
142 |
+
const scalar_t x = VLLM_LDG(&input[token_idx * d + idx]);
|
143 |
+
out[token_idx * d + idx] = ACT_FN(x);
|
144 |
+
}
|
145 |
+
}
|
146 |
+
|
147 |
+
} // namespace vllm
|
148 |
+
|
149 |
+
// Launch element-wise activation kernel.
|
150 |
+
#define LAUNCH_ACTIVATION_KERNEL(KERNEL) \
|
151 |
+
int d = input.size(-1); \
|
152 |
+
int64_t num_tokens = input.numel() / d; \
|
153 |
+
dim3 grid(num_tokens); \
|
154 |
+
dim3 block(std::min(d, 1024)); \
|
155 |
+
const at::cuda::OptionalCUDAGuard device_guard(device_of(input)); \
|
156 |
+
const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); \
|
157 |
+
VLLM_DISPATCH_FLOATING_TYPES(input.scalar_type(), "activation_kernel", [&] { \
|
158 |
+
vllm::activation_kernel<scalar_t, KERNEL<scalar_t>> \
|
159 |
+
<<<grid, block, 0, stream>>>(out.data_ptr<scalar_t>(), \
|
160 |
+
input.data_ptr<scalar_t>(), d); \
|
161 |
+
});
|
162 |
+
|
163 |
+
namespace vllm {
|
164 |
+
|
165 |
+
template <typename T>
|
166 |
+
__device__ __forceinline__ T gelu_new_kernel(const T& x) {
|
167 |
+
const float x3 = (float)(x * x * x);
|
168 |
+
const T t = (T)tanhf((T)(0.79788456f * (float)(x + (T)(0.044715f * x3))));
|
169 |
+
return ((T)0.5) * x * (((T)1.0) + t);
|
170 |
+
}
|
171 |
+
|
172 |
+
template <typename T>
|
173 |
+
__device__ __forceinline__ T gelu_fast_kernel(const T& x) {
|
174 |
+
const float f = (float)x;
|
175 |
+
const T t =
|
176 |
+
(T)tanhf(((T)(f * 0.79788456f)) * (((T)1.0) + (T)(0.044715f * f) * x));
|
177 |
+
return ((T)0.5) * x * (((T)1.0) + t);
|
178 |
+
}
|
179 |
+
|
180 |
+
template <typename T>
|
181 |
+
__device__ __forceinline__ T gelu_quick_kernel(const T& x) {
|
182 |
+
// x * sigmoid(1.702 * x)
|
183 |
+
return (T)(((float)x) / (1.0f + expf(-1.702f * (float)x)));
|
184 |
+
}
|
185 |
+
|
186 |
+
} // namespace vllm
|
187 |
+
|
188 |
+
void gelu_new(torch::Tensor& out, // [..., d]
|
189 |
+
torch::Tensor& input) // [..., d]
|
190 |
+
{
|
191 |
+
LAUNCH_ACTIVATION_KERNEL(vllm::gelu_new_kernel);
|
192 |
+
}
|
193 |
+
|
194 |
+
void gelu_fast(torch::Tensor& out, // [..., d]
|
195 |
+
torch::Tensor& input) // [..., d]
|
196 |
+
{
|
197 |
+
LAUNCH_ACTIVATION_KERNEL(vllm::gelu_fast_kernel);
|
198 |
+
}
|
199 |
+
|
200 |
+
void gelu_quick(torch::Tensor& out, // [..., d]
|
201 |
+
torch::Tensor& input) // [..., d]
|
202 |
+
{
|
203 |
+
LAUNCH_ACTIVATION_KERNEL(vllm::gelu_quick_kernel);
|
204 |
+
}
|
activation/cuda_compat.h
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#ifdef USE_ROCM
|
4 |
+
#include <hip/hip_runtime.h>
|
5 |
+
#endif
|
6 |
+
|
7 |
+
#ifndef USE_ROCM
|
8 |
+
#define WARP_SIZE 32
|
9 |
+
#else
|
10 |
+
#define WARP_SIZE warpSize
|
11 |
+
#endif
|
12 |
+
|
13 |
+
#ifndef USE_ROCM
|
14 |
+
#define VLLM_LDG(arg) __ldg(arg)
|
15 |
+
#else
|
16 |
+
#define VLLM_LDG(arg) *(arg)
|
17 |
+
#endif
|
18 |
+
|
19 |
+
#ifndef USE_ROCM
|
20 |
+
#define VLLM_SHFL_XOR_SYNC(var, lane_mask) \
|
21 |
+
__shfl_xor_sync(uint32_t(-1), var, lane_mask)
|
22 |
+
#define VLLM_SHFL_XOR_SYNC_WIDTH(var, lane_mask, width) \
|
23 |
+
__shfl_xor_sync(uint32_t(-1), var, lane_mask, width)
|
24 |
+
#else
|
25 |
+
#define VLLM_SHFL_XOR_SYNC(var, lane_mask) __shfl_xor(var, lane_mask)
|
26 |
+
#define VLLM_SHFL_XOR_SYNC_WIDTH(var, lane_mask, width) \
|
27 |
+
__shfl_xor(var, lane_mask, width)
|
28 |
+
#endif
|
29 |
+
|
30 |
+
#ifndef USE_ROCM
|
31 |
+
#define VLLM_SHFL_SYNC(var, src_lane) __shfl_sync(uint32_t(-1), var, src_lane)
|
32 |
+
#else
|
33 |
+
#define VLLM_SHFL_SYNC(var, src_lane) __shfl(var, src_lane)
|
34 |
+
#endif
|
35 |
+
|
36 |
+
#ifndef USE_ROCM
|
37 |
+
#define VLLM_SHFL_DOWN_SYNC(var, lane_delta) \
|
38 |
+
__shfl_down_sync(uint32_t(-1), var, lane_delta)
|
39 |
+
#else
|
40 |
+
#define VLLM_SHFL_DOWN_SYNC(var, lane_delta) __shfl_down(var, lane_delta)
|
41 |
+
#endif
|
42 |
+
|
43 |
+
#ifndef USE_ROCM
|
44 |
+
#define VLLM_DevFuncAttribute_SET_MaxDynamicSharedMemorySize(FUNC, VAL) \
|
45 |
+
cudaFuncSetAttribute(FUNC, cudaFuncAttributeMaxDynamicSharedMemorySize, VAL)
|
46 |
+
#else
|
47 |
+
#define VLLM_DevFuncAttribute_SET_MaxDynamicSharedMemorySize(FUNC, VAL) \
|
48 |
+
hipFuncSetAttribute(FUNC, hipFuncAttributeMaxDynamicSharedMemorySize, VAL)
|
49 |
+
#endif
|
activation/dispatch_utils.h
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
* Adapted from
|
3 |
+
* https://github.com/pytorch/pytorch/blob/v2.0.1/aten/src/ATen/Dispatch.h
|
4 |
+
*/
|
5 |
+
#pragma once
|
6 |
+
|
7 |
+
#include <torch/all.h>
|
8 |
+
|
9 |
+
#define VLLM_DISPATCH_CASE_FLOATING_TYPES(...) \
|
10 |
+
AT_DISPATCH_CASE(at::ScalarType::Float, __VA_ARGS__) \
|
11 |
+
AT_DISPATCH_CASE(at::ScalarType::Half, __VA_ARGS__) \
|
12 |
+
AT_DISPATCH_CASE(at::ScalarType::BFloat16, __VA_ARGS__)
|
13 |
+
|
14 |
+
#define VLLM_DISPATCH_FLOATING_TYPES(TYPE, NAME, ...) \
|
15 |
+
AT_DISPATCH_SWITCH(TYPE, NAME, VLLM_DISPATCH_CASE_FLOATING_TYPES(__VA_ARGS__))
|
16 |
+
|
17 |
+
#define VLLM_DISPATCH_CASE_FLOATING_AND_BYTE_TYPES(...) \
|
18 |
+
AT_DISPATCH_CASE(at::ScalarType::Float, __VA_ARGS__) \
|
19 |
+
AT_DISPATCH_CASE(at::ScalarType::Half, __VA_ARGS__) \
|
20 |
+
AT_DISPATCH_CASE(at::ScalarType::BFloat16, __VA_ARGS__) \
|
21 |
+
AT_DISPATCH_CASE(at::ScalarType::Byte, __VA_ARGS__)
|
22 |
+
|
23 |
+
#define VLLM_DISPATCH_FLOATING_AND_BYTE_TYPES(TYPE, NAME, ...) \
|
24 |
+
AT_DISPATCH_SWITCH(TYPE, NAME, \
|
25 |
+
VLLM_DISPATCH_CASE_FLOATING_AND_BYTE_TYPES(__VA_ARGS__))
|
26 |
+
|
27 |
+
#define VLLM_DISPATCH_CASE_INTEGRAL_TYPES(...) \
|
28 |
+
AT_DISPATCH_CASE(at::ScalarType::Byte, __VA_ARGS__) \
|
29 |
+
AT_DISPATCH_CASE(at::ScalarType::Char, __VA_ARGS__) \
|
30 |
+
AT_DISPATCH_CASE(at::ScalarType::Short, __VA_ARGS__) \
|
31 |
+
AT_DISPATCH_CASE(at::ScalarType::Int, __VA_ARGS__) \
|
32 |
+
AT_DISPATCH_CASE(at::ScalarType::Long, __VA_ARGS__)
|
33 |
+
|
34 |
+
#define VLLM_DISPATCH_INTEGRAL_TYPES(TYPE, NAME, ...) \
|
35 |
+
AT_DISPATCH_SWITCH(TYPE, NAME, VLLM_DISPATCH_CASE_INTEGRAL_TYPES(__VA_ARGS__))
|
build.toml
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[general]
|
2 |
+
version = "0.0.1"
|
3 |
+
|
4 |
+
[torch]
|
5 |
+
name = "activation"
|
6 |
+
src = [
|
7 |
+
"ext-torch/registration.h",
|
8 |
+
"ext-torch/torch_binding.cpp",
|
9 |
+
"ext-torch/torch_binding.h"
|
10 |
+
]
|
11 |
+
pyroot = "ext-torch"
|
12 |
+
|
13 |
+
[kernel.activation]
|
14 |
+
capabilities = [ "7.0", "7.2", "7.5", "8.0", "8.6", "8.7", "8.9", "9.0" ]
|
15 |
+
src = [
|
16 |
+
"activation/activation_kernels.cu",
|
17 |
+
"activation/cuda_compat.h",
|
18 |
+
"activation/dispatch_utils.h",
|
19 |
+
]
|
20 |
+
depends = [ "torch" ]
|
ext-torch/activation/__init__.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
try:
|
4 |
+
from ._ops import ops
|
5 |
+
except ImportError as e:
|
6 |
+
# Fallback for local development.
|
7 |
+
try:
|
8 |
+
import _activation
|
9 |
+
|
10 |
+
ops = torch.ops._activition
|
11 |
+
except ImportError:
|
12 |
+
raise e
|
13 |
+
|
14 |
+
|
15 |
+
def silu_and_mul(out: torch.Tensor, x: torch.Tensor) -> None:
|
16 |
+
ops.silu_and_mul(out, x)
|
17 |
+
return out
|
18 |
+
|
19 |
+
|
20 |
+
def gelu_and_mul(out: torch.Tensor, x: torch.Tensor) -> None:
|
21 |
+
ops.gelu_and_mul(out, x)
|
22 |
+
return out
|
23 |
+
|
24 |
+
|
25 |
+
def gelu_tanh_and_mul(out: torch.Tensor, x: torch.Tensor) -> None:
|
26 |
+
ops.gelu_tanh_and_mul(out, x)
|
27 |
+
return out
|
28 |
+
|
29 |
+
|
30 |
+
def fatrelu_and_mul(out: torch.Tensor, x: torch.Tensor, threshold: float = 0.0) -> None:
|
31 |
+
ops.fatrelu_and_mul(out, x, threshold)
|
32 |
+
return out
|
33 |
+
|
34 |
+
|
35 |
+
def gelu_fast(out: torch.Tensor, x: torch.Tensor) -> None:
|
36 |
+
ops.gelu_fast(out, x)
|
37 |
+
return out
|
38 |
+
|
39 |
+
|
40 |
+
def gelu_new(out: torch.Tensor, x: torch.Tensor) -> None:
|
41 |
+
ops.gelu_new(out, x)
|
42 |
+
return out
|
43 |
+
|
44 |
+
|
45 |
+
def gelu_quick(out: torch.Tensor, x: torch.Tensor) -> None:
|
46 |
+
ops.gelu_quick(out, x)
|
47 |
+
return out
|
ext-torch/registration.h
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#include <Python.h>
|
4 |
+
|
5 |
+
#define _CONCAT(A, B) A##B
|
6 |
+
#define CONCAT(A, B) _CONCAT(A, B)
|
7 |
+
|
8 |
+
#define _STRINGIFY(A) #A
|
9 |
+
#define STRINGIFY(A) _STRINGIFY(A)
|
10 |
+
|
11 |
+
// A version of the TORCH_LIBRARY macro that expands the NAME, i.e. so NAME
|
12 |
+
// could be a macro instead of a literal token.
|
13 |
+
#define TORCH_LIBRARY_EXPAND(NAME, MODULE) TORCH_LIBRARY(NAME, MODULE)
|
14 |
+
|
15 |
+
// A version of the TORCH_LIBRARY_IMPL macro that expands the NAME, i.e. so NAME
|
16 |
+
// could be a macro instead of a literal token.
|
17 |
+
#define TORCH_LIBRARY_IMPL_EXPAND(NAME, DEVICE, MODULE) \
|
18 |
+
TORCH_LIBRARY_IMPL(NAME, DEVICE, MODULE)
|
19 |
+
|
20 |
+
// REGISTER_EXTENSION allows the shared library to be loaded and initialized
|
21 |
+
// via python's import statement.
|
22 |
+
#define REGISTER_EXTENSION(NAME) \
|
23 |
+
PyMODINIT_FUNC CONCAT(PyInit_, NAME)() { \
|
24 |
+
static struct PyModuleDef module = {PyModuleDef_HEAD_INIT, \
|
25 |
+
STRINGIFY(NAME), nullptr, 0, nullptr}; \
|
26 |
+
return PyModule_Create(&module); \
|
27 |
+
}
|
ext-torch/torch_binding.cpp
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <torch/library.h>
|
2 |
+
|
3 |
+
#include "registration.h"
|
4 |
+
#include "torch_binding.h"
|
5 |
+
|
6 |
+
TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
7 |
+
// Activation ops
|
8 |
+
// Activation function used in SwiGLU.
|
9 |
+
ops.def("silu_and_mul(Tensor! out, Tensor input) -> ()");
|
10 |
+
ops.impl("silu_and_mul", torch::kCUDA, &silu_and_mul);
|
11 |
+
|
12 |
+
// Activation function used in GeGLU with `none` approximation.
|
13 |
+
ops.def("gelu_and_mul(Tensor! out, Tensor input) -> ()");
|
14 |
+
ops.impl("gelu_and_mul", torch::kCUDA, &gelu_and_mul);
|
15 |
+
|
16 |
+
// Activation function used in GeGLU with `tanh` approximation.
|
17 |
+
ops.def("gelu_tanh_and_mul(Tensor! out, Tensor input) -> ()");
|
18 |
+
ops.impl("gelu_tanh_and_mul", torch::kCUDA, &gelu_tanh_and_mul);
|
19 |
+
|
20 |
+
// FATReLU implementation.
|
21 |
+
ops.def("fatrelu_and_mul(Tensor! out, Tensor input, float threshold) -> ()");
|
22 |
+
ops.impl("fatrelu_and_mul", torch::kCUDA, &fatrelu_and_mul);
|
23 |
+
|
24 |
+
// GELU implementation used in GPT-2.
|
25 |
+
ops.def("gelu_new(Tensor! out, Tensor input) -> ()");
|
26 |
+
ops.impl("gelu_new", torch::kCUDA, &gelu_new);
|
27 |
+
|
28 |
+
// Approximate GELU implementation.
|
29 |
+
ops.def("gelu_fast(Tensor! out, Tensor input) -> ()");
|
30 |
+
ops.impl("gelu_fast", torch::kCUDA, &gelu_fast);
|
31 |
+
|
32 |
+
// Quick GELU implementation.
|
33 |
+
ops.def("gelu_quick(Tensor! out, Tensor input) -> ()");
|
34 |
+
ops.impl("gelu_quick", torch::kCUDA, &gelu_quick);
|
35 |
+
}
|
36 |
+
|
37 |
+
REGISTER_EXTENSION(TORCH_EXTENSION_NAME)
|
ext-torch/torch_binding.h
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#pragma once
|
2 |
+
|
3 |
+
#include <torch/torch.h>
|
4 |
+
|
5 |
+
void silu_and_mul(torch::Tensor &out, torch::Tensor &input);
|
6 |
+
|
7 |
+
void gelu_and_mul(torch::Tensor &out, torch::Tensor &input);
|
8 |
+
|
9 |
+
void gelu_tanh_and_mul(torch::Tensor &out, torch::Tensor &input);
|
10 |
+
|
11 |
+
void fatrelu_and_mul(torch::Tensor &out, torch::Tensor &input,
|
12 |
+
double threshold);
|
13 |
+
|
14 |
+
void gelu_new(torch::Tensor &out, torch::Tensor &input);
|
15 |
+
|
16 |
+
void gelu_fast(torch::Tensor &out, torch::Tensor &input);
|
17 |
+
|
18 |
+
void gelu_quick(torch::Tensor &out, torch::Tensor &input);
|
flake.nix
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
description = "Flake for activation kernels";
|
3 |
+
|
4 |
+
inputs = {
|
5 |
+
kernel-builder.url = "git+ssh://git@github.com/huggingface/kernel-builder";
|
6 |
+
};
|
7 |
+
|
8 |
+
outputs =
|
9 |
+
{
|
10 |
+
self,
|
11 |
+
kernel-builder,
|
12 |
+
}:
|
13 |
+
kernel-builder.lib.genFlakeOutputs ./.;
|
14 |
+
}
|
tests/__init__.py
ADDED
File without changes
|
tests/kernels/__init__.py
ADDED
File without changes
|
tests/kernels/allclose_default.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
# Reference default values of atol and rtol are from
|
4 |
+
# https://github.com/pytorch/pytorch/blob/6d96beb6bec24d73ee3f080bac54d2104068f675/test/test_transformers.py#L67
|
5 |
+
default_atol = {torch.float16: 1e-3, torch.bfloat16: 1e-3, torch.float: 1e-5}
|
6 |
+
default_rtol = {torch.float16: 1e-3, torch.bfloat16: 1.6e-2, torch.float: 1.3e-6}
|
7 |
+
|
8 |
+
|
9 |
+
def get_default_atol(output) -> float:
|
10 |
+
return default_atol[output.dtype]
|
11 |
+
|
12 |
+
|
13 |
+
def get_default_rtol(output) -> float:
|
14 |
+
return default_rtol[output.dtype]
|
tests/kernels/test_activation.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import random
|
3 |
+
from typing import Type
|
4 |
+
|
5 |
+
import activation
|
6 |
+
import pytest
|
7 |
+
import torch
|
8 |
+
import torch.nn.functional as F
|
9 |
+
|
10 |
+
from .utils import opcheck
|
11 |
+
from .allclose_default import get_default_atol, get_default_rtol
|
12 |
+
|
13 |
+
DTYPES = [torch.half, torch.bfloat16, torch.float]
|
14 |
+
NUM_TOKENS = [7, 83, 2048] # Arbitrary values for testing
|
15 |
+
D = [512, 13824] # Arbitrary values for testing
|
16 |
+
SEEDS = [0]
|
17 |
+
CUDA_DEVICES = [f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)]
|
18 |
+
|
19 |
+
|
20 |
+
def gelu_fast(x: torch.Tensor) -> torch.Tensor:
|
21 |
+
return 0.5 * x * (1.0 + torch.tanh(x * 0.7978845608 * (1.0 + 0.044715 * x * x)))
|
22 |
+
|
23 |
+
|
24 |
+
def gelu_new(x: torch.Tensor) -> torch.Tensor:
|
25 |
+
c = math.sqrt(2.0 / math.pi)
|
26 |
+
return 0.5 * x * (1.0 + torch.tanh(c * (x + 0.044715 * torch.pow(x, 3.0))))
|
27 |
+
|
28 |
+
|
29 |
+
def gelu_quick(x: torch.Tensor) -> torch.Tensor:
|
30 |
+
return x * torch.sigmoid(1.702 * x)
|
31 |
+
|
32 |
+
|
33 |
+
def fatrelu_and_mul(x: torch.Tensor, threshold: float) -> torch.Tensor:
|
34 |
+
d = x.shape[-1] // 2
|
35 |
+
x1 = x[..., :d]
|
36 |
+
x2 = x[..., d:]
|
37 |
+
x1 = F.threshold(x1, threshold, 0.0)
|
38 |
+
return x1 * x2
|
39 |
+
|
40 |
+
|
41 |
+
def silu_and_mul(x: torch.Tensor) -> torch.Tensor:
|
42 |
+
d = x.shape[-1] // 2
|
43 |
+
return F.silu(x[..., :d]) * x[..., d:]
|
44 |
+
|
45 |
+
|
46 |
+
def gelu_and_mul(x: torch.Tensor, approximate: str) -> torch.Tensor:
|
47 |
+
d = x.shape[-1] // 2
|
48 |
+
return F.gelu(x[..., :d], approximate=approximate) * x[..., d:]
|
49 |
+
|
50 |
+
|
51 |
+
@pytest.mark.parametrize("activation_name", ["silu", "gelu", "gelu_tanh", "fatrelu"])
|
52 |
+
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
53 |
+
@pytest.mark.parametrize("d", D)
|
54 |
+
@pytest.mark.parametrize("dtype", DTYPES)
|
55 |
+
@pytest.mark.parametrize("seed", SEEDS)
|
56 |
+
@pytest.mark.parametrize("device", CUDA_DEVICES)
|
57 |
+
@torch.inference_mode()
|
58 |
+
def test_act_and_mul(
|
59 |
+
activation_name: str,
|
60 |
+
num_tokens: int,
|
61 |
+
d: int,
|
62 |
+
dtype: torch.dtype,
|
63 |
+
seed: int,
|
64 |
+
device: str,
|
65 |
+
) -> None:
|
66 |
+
random.seed(seed)
|
67 |
+
torch.manual_seed(seed)
|
68 |
+
torch.set_default_device(device)
|
69 |
+
x = torch.randn(num_tokens, 2 * d, dtype=dtype)
|
70 |
+
if activation_name == "silu":
|
71 |
+
torch_fn = silu_and_mul
|
72 |
+
fn = activation.silu_and_mul
|
73 |
+
op = activation.ops.silu_and_mul
|
74 |
+
elif activation_name == "gelu":
|
75 |
+
torch_fn = lambda x: gelu_and_mul(x, "none")
|
76 |
+
fn = activation.gelu_and_mul
|
77 |
+
op = activation.ops.gelu_and_mul
|
78 |
+
elif activation_name == "gelu_tanh":
|
79 |
+
torch_fn = lambda x: gelu_and_mul(x, "tanh")
|
80 |
+
fn = activation.gelu_tanh_and_mul
|
81 |
+
op = activation.ops.gelu_tanh_and_mul
|
82 |
+
elif activation_name == "fatrelu":
|
83 |
+
threshold = random.uniform(0, 1)
|
84 |
+
torch_fn = lambda x: fatrelu_and_mul(x, threshold)
|
85 |
+
fn = lambda out, x: activation.fatrelu_and_mul(out, x, threshold)
|
86 |
+
op = activation.ops.fatrelu_and_mul
|
87 |
+
|
88 |
+
out_shape = x.shape[:-1] + (x.shape[-1] // 2,)
|
89 |
+
out = torch.empty(out_shape, dtype=x.dtype, device=x.device)
|
90 |
+
out = fn(out, x)
|
91 |
+
ref_out = torch_fn(x)
|
92 |
+
|
93 |
+
# The SiLU, GELU and FatReLU implementations are equivalent to the native
|
94 |
+
# PyTorch implementations, so we can do exact comparison.
|
95 |
+
torch.testing.assert_close(out, ref_out, atol=0.0, rtol=0.0)
|
96 |
+
|
97 |
+
d = x.shape[-1] // 2
|
98 |
+
output_shape = x.shape[:-1] + (d,)
|
99 |
+
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
|
100 |
+
if activation_name == "fatrelu":
|
101 |
+
opcheck(op, (out, x, threshold))
|
102 |
+
else:
|
103 |
+
opcheck(op, (out, x))
|
104 |
+
|
105 |
+
|
106 |
+
@pytest.mark.parametrize(
|
107 |
+
"activation_fns",
|
108 |
+
[
|
109 |
+
(gelu_fast, activation.gelu_fast, activation.ops.gelu_fast),
|
110 |
+
(gelu_new, activation.gelu_new, activation.ops.gelu_new),
|
111 |
+
(gelu_quick, activation.gelu_quick, activation.ops.gelu_quick),
|
112 |
+
],
|
113 |
+
)
|
114 |
+
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
115 |
+
@pytest.mark.parametrize("d", D)
|
116 |
+
@pytest.mark.parametrize("dtype", DTYPES)
|
117 |
+
@pytest.mark.parametrize("seed", SEEDS)
|
118 |
+
@pytest.mark.parametrize("device", CUDA_DEVICES)
|
119 |
+
@torch.inference_mode()
|
120 |
+
def test_activation(
|
121 |
+
activation_fns,
|
122 |
+
num_tokens: int,
|
123 |
+
d: int,
|
124 |
+
dtype: torch.dtype,
|
125 |
+
seed: int,
|
126 |
+
device: str,
|
127 |
+
) -> None:
|
128 |
+
torch.manual_seed(seed)
|
129 |
+
torch.set_default_device(device)
|
130 |
+
x = torch.randn(num_tokens, d, dtype=dtype)
|
131 |
+
torch_fn, fn, op = activation_fns
|
132 |
+
out = fn(torch.empty_like(x), x)
|
133 |
+
ref_out = torch_fn(x)
|
134 |
+
torch.testing.assert_close(
|
135 |
+
out, ref_out, atol=get_default_atol(out), rtol=get_default_rtol(out)
|
136 |
+
)
|
137 |
+
|
138 |
+
out = torch.empty_like(x)
|
139 |
+
opcheck(op, (out, x))
|
tests/kernels/utils.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Kernel test utils"""
|
2 |
+
|
3 |
+
import itertools
|
4 |
+
import random
|
5 |
+
import unittest
|
6 |
+
from numbers import Number
|
7 |
+
from typing import Any, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union
|
8 |
+
|
9 |
+
import pytest
|
10 |
+
import torch
|
11 |
+
from torch._prims_common import TensorLikeType
|
12 |
+
|
13 |
+
# For now, disable "test_aot_dispatch_dynamic" since there are some
|
14 |
+
# bugs related to this test in PyTorch 2.4.
|
15 |
+
DEFAULT_OPCHECK_TEST_UTILS: Tuple[str, ...] = (
|
16 |
+
"test_schema",
|
17 |
+
"test_autograd_registration",
|
18 |
+
"test_faketensor",
|
19 |
+
)
|
20 |
+
|
21 |
+
ALL_OPCHECK_TEST_UTILS: Tuple[str, ...] = (
|
22 |
+
"test_schema",
|
23 |
+
"test_autograd_registration",
|
24 |
+
"test_faketensor",
|
25 |
+
"test_aot_dispatch_dynamic",
|
26 |
+
)
|
27 |
+
|
28 |
+
|
29 |
+
# Copied/modified from torch._refs.__init__.py
|
30 |
+
def fp8_allclose(
|
31 |
+
a: TensorLikeType,
|
32 |
+
b: TensorLikeType,
|
33 |
+
rtol: float = 1e-05,
|
34 |
+
atol: float = 1e-08,
|
35 |
+
equal_nan: bool = False,
|
36 |
+
) -> bool:
|
37 |
+
"""
|
38 |
+
Reference implementation of torch.allclose
|
39 |
+
"""
|
40 |
+
torch._refs._check_close_args(name="torch.allclose", a=a, b=b, rtol=rtol, atol=atol)
|
41 |
+
|
42 |
+
return bool(
|
43 |
+
torch.all(
|
44 |
+
torch.isclose(
|
45 |
+
a.double(), b.double(), rtol=rtol, atol=atol, equal_nan=equal_nan
|
46 |
+
)
|
47 |
+
).item()
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
# A special version of op check that has a restricted default set of test_utils
|
52 |
+
# and a patched version of allclose that supports fp8 types.
|
53 |
+
def opcheck(
|
54 |
+
op: Union[
|
55 |
+
torch._ops.OpOverload,
|
56 |
+
torch._ops.OpOverloadPacket,
|
57 |
+
torch._library.custom_ops.CustomOpDef,
|
58 |
+
],
|
59 |
+
args: Tuple[Any, ...],
|
60 |
+
kwargs: Optional[Dict[str, Any]] = None,
|
61 |
+
*,
|
62 |
+
test_utils: Union[str, Sequence[str]] = ALL_OPCHECK_TEST_UTILS,
|
63 |
+
raise_exception: bool = True,
|
64 |
+
cond: bool = True
|
65 |
+
) -> Dict[str, str]:
|
66 |
+
with unittest.mock.patch("torch.allclose", new=fp8_allclose):
|
67 |
+
return (
|
68 |
+
torch.library.opcheck(
|
69 |
+
op, args, kwargs, test_utils=test_utils, raise_exception=raise_exception
|
70 |
+
)
|
71 |
+
if cond
|
72 |
+
else {}
|
73 |
+
)
|