1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| import os import unittest import time import datetime import numpy as np from collections import OrderedDict
import oneflow as flow import oneflow.nn as nn import oneflow.unittest from oneflow.test_utils.test_util import GenArgList
test_direction = "forward"
class Glu(nn.Module): def __init__(self): super().__init__()
def forward( self, x: flow.Tensor, w: flow.Tensor, b: flow.Tensor, v: flow.Tensor = None, c: flow.Tensor = None, split_mode: bool = False, activation: str = "none", ) -> flow.Tensor: matmul_wx = flow._C.matmul( input=x, other=w, transpose_a=False, transpose_b=True ) if split_mode: matmul_vx = flow._C.matmul( input=x, other=v, transpose_a=False, transpose_b=True )
matmul_wx_b = flow._C.add(input=matmul_wx, other=b) if split_mode: matmul_vx_c = flow._C.add(input=matmul_vx, other=c)
if split_mode: hidden_state = matmul_wx_b gate = matmul_vx_c else: hidden_state, gate = matmul_wx_b.chunk(2, dim=-1)
if activation == "none": return hidden_state * gate elif activation == "sigmoid": return hidden_state * flow.sigmoid(gate) elif activation == "relu": return hidden_state * flow.relu(gate) elif activation == "gelu": return hidden_state * flow.gelu(gate) elif activation == "fast_gelu": return hidden_state * flow._C.fast_gelu(gate) elif activation == "silu": return hidden_state * flow.silu(gate)
def tensor_builder(params: dict, dtype=flow.float32, is_split_mode=True): m = params["m"] n = params["n"] k = params["k"]
x = np.random.randn(2, m, k) / 100 y_nor = np.random.randn(2, m, n) if is_split_mode: w = np.random.randn(n, k) / 100 b = np.random.randn(n) / 100 v = np.random.randn(n, k) / 100 c = np.random.randn(n) / 100 else: w = np.random.randn(n * 2, k) / 100 b = np.random.randn(n * 2) / 100
tensor_x = flow.FloatTensor(x).to(dtype=dtype, device="cuda") tensor_y_nor = flow.FloatTensor(y_nor).to(dtype=dtype, device="cuda") tensor_w = flow.FloatTensor(w).to(dtype=dtype, device="cuda").requires_grad_(True) tensor_b = flow.FloatTensor(b).to(dtype=dtype, device="cuda").requires_grad_(True) if is_split_mode: tensor_v = ( flow.FloatTensor(v).to(dtype=dtype, device="cuda").requires_grad_(True) ) tensor_c = ( flow.FloatTensor(c).to(dtype=dtype, device="cuda").requires_grad_(True) )
if is_split_mode: return tensor_x, tensor_w, tensor_b, tensor_v, tensor_c, tensor_y_nor else: return tensor_x, tensor_w, tensor_b, tensor_y_nor
def profile_naive_glu(test_case, params: dict, dtype=flow.float32): print(f"========== Start Testing ==========") print(f"impt: naive") print(f"direction: {test_direction}") print(f"weight tensor: merged") print(f'tensor shape: m={params["m"]}, n={params["n"]}, k={params["k"]}') print(f'activation: {params["act"]}') print(f"dtype: {dtype}")
flow_module = Glu() x, w, b, y_nor = tensor_builder(params=params, dtype=dtype, is_split_mode=False)
if test_direction == "forward": y = flow_module.forward(x=x, w=w, b=b, split_mode=False, activation=params["act"])
if test_direction == "backward": y.sum().backward()
print(f"============== PASSED =============") print("\n")
def profile_fused_glu(test_case, params: dict, dtype=flow.float32): print(f"========== Start Testing ==========") print(f"impt: fused") print(f"direction: {test_direction}") print(f"weight tensor: merged") print(f'tensor shape: m={params["m"]}, n={params["n"]}, k={params["k"]}') print(f'activation: {params["act"]}') print(f"dtype: {dtype}")
x, w, b, y_nor = tensor_builder(params=params, dtype=dtype, is_split_mode=False)
if test_direction == "forward": fused_y = flow._C.fused_glu(x=x, w=w, b=b, v=None, c=None, activation=params["act"])
if test_direction == "backward": fused_y.sum().backward()
print(f"============== PASSED =============") print("\n")
@flow.unittest.skip_unless_1n1d() @unittest.skipIf(os.getenv("ONEFLOW_TEST_CPU_ONLY"), "only test gpu cases") class TestFusedGlu(flow.unittest.TestCase): def test_gather(test_case): arg_dict = OrderedDict() arg_dict["test_fun"] = [ profile_naive_glu, profile_fused_glu, ]
if not test_dualgemm_impt: os.environ["ONEFLOW_KERNEL_GLU_ENABLE_DUAL_GEMM_IMPL"] = "false" else: os.environ["ONEFLOW_KERNEL_GLU_ENABLE_DUAL_GEMM_IMPL"] = "true"
if not test_dualgemm_impt: arg_dict["params"] = [ {"m": 256, "k": 1280, "n": 5120, "act": "none"}, {"m": 256, "k": 1280, "n": 5120, "act": "sigmoid"}, {"m": 256, "k": 1280, "n": 5120, "act": "relu"}, {"m": 256, "k": 1280, "n": 5120, "act": "gelu"}, {"m": 256, "k": 1280, "n": 5120, "act": "fast_gelu"}, {"m": 256, "k": 1280, "n": 5120, "act": "silu"}, {"m": 1024, "k": 640, "n": 2560, "act": "none"}, {"m": 1024, "k": 640, "n": 2560, "act": "sigmoid"}, {"m": 1024, "k": 640, "n": 2560, "act": "relu"}, {"m": 1024, "k": 640, "n": 2560, "act": "gelu"}, {"m": 1024, "k": 640, "n": 2560, "act": "fast_gelu"}, {"m": 1024, "k": 640, "n": 2560, "act": "silu"}, {"m": 4096, "k": 320, "n": 1280, "act": "none"}, {"m": 4096, "k": 320, "n": 1280, "act": "sigmoid"}, {"m": 4096, "k": 320, "n": 1280, "act": "relu"}, {"m": 4096, "k": 320, "n": 1280, "act": "gelu"}, {"m": 4096, "k": 320, "n": 1280, "act": "fast_gelu"}, {"m": 4096, "k": 320, "n": 1280, "act": "silu"}, {"m": 2560, "k": 1280, "n": 5120, "act": "none"}, {"m": 2560, "k": 1280, "n": 5120, "act": "sigmoid"}, {"m": 2560, "k": 1280, "n": 5120, "act": "relu"}, {"m": 2560, "k": 1280, "n": 5120, "act": "gelu"}, {"m": 2560, "k": 1280, "n": 5120, "act": "fast_gelu"}, {"m": 2560, "k": 1280, "n": 5120, "act": "silu"}, ] else: arg_dict["params"] = [ {"m": 256, "k": 1280, "n": 5120, "act": "fast_gelu"}, {"m": 1024, "k": 640, "n": 2560, "act": "fast_gelu"}, {"m": 4096, "k": 320, "n": 1280, "act": "fast_gelu"}, {"m": 2560, "k": 1280, "n": 5120, "act": "fast_gelu"}, ]
if not test_dualgemm_impt: arg_dict["dtype"] = [flow.float16, flow.float32] else: arg_dict["dtype"] = [flow.float16]
for arg in GenArgList(arg_dict): arg[0](test_case, *arg[1:])
if __name__ == "__main__": unittest.main()
|