vllm.model_executor.layers.fla.ops.chunk ¶
ChunkGatedDeltaRuleFunction ¶
Bases: Function
Source code in vllm/model_executor/layers/fla/ops/chunk.py
forward staticmethod
¶
forward(
ctx,
q: Tensor,
k: Tensor,
v: Tensor,
g: Tensor,
beta: Tensor,
scale: float,
initial_state: Tensor,
output_final_state: bool,
cu_seqlens: Optional[LongTensor] = None,
use_qk_l2norm_in_kernel: bool = False,
)
Source code in vllm/model_executor/layers/fla/ops/chunk.py
chunk_gated_delta_rule ¶
chunk_gated_delta_rule(
q: Tensor,
k: Tensor,
v: Tensor,
g: Tensor,
beta: Tensor,
scale: float = None,
initial_state: Tensor = None,
output_final_state: bool = False,
cu_seqlens: Optional[LongTensor] = None,
head_first: bool = False,
use_qk_l2norm_in_kernel: bool = False,
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q | Tensor | queries of shape | required |
k | Tensor | keys of shape | required |
v | Tensor | values of shape | required |
g | Tensor | (forget) gating tensor (in log space!) of shape | required |
beta | Tensor | betas of shape | required |
scale | Optional[int] | Scale factor for the RetNet attention scores. If not provided, it will default to | None |
initial_state | Optional[Tensor] | Initial state of shape | None |
output_final_state | Optional[bool] | Whether to output the final state of shape | False |
cu_seqlens | LongTensor | Cumulative sequence lengths of shape | None |
head_first | Optional[bool] | Whether the inputs are in the head-first format, which is not supported for variable-length inputs. Default: | False |
Returns:
Name | Type | Description |
---|---|---|
o | Tensor | Outputs of shape |
final_state | Tensor | Final state of shape |
Examples:: >>> import torch >>> import torch.nn.functional as F >>> from einops import rearrange >>> from fla.ops.gated_delta_rule import chunk_gated_delta_rule # inputs with equal lengths >>> B, T, H, K, V = 4, 2048, 4, 512, 512 >>> q = torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda') >>> k = F.normalize(torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda'), p=2, dim=-1) >>> v = torch.randn(B, T, H, V, dtype=torch.bfloat16, device='cuda') >>> beta = torch.rand(B, T, H, dtype=torch.bfloat16, device='cuda').sigmoid() >>> g = F.logsigmoid(torch.rand(B, T, H, dtype=torch.bfloat16, device='cuda')) >>> h0 = torch.randn(B, H, K, V, dtype=torch.bfloat16, device='cuda') >>> o, ht = chunk_gated_delta_rule( q, k, v, g, beta, initial_state=h0, output_final_state=True ) # for variable-length inputs, the batch size B
is expected to be 1 and cu_seqlens
is required >>> q, k, v, beta, g = map(lambda x: rearrange(x, 'b t ... -> 1 (b t) ...'), (q, k, v, beta, g)) # for a batch with 4 sequences, cu_seqlens
with 5 start/end positions are expected >>> cu_seqlens = q.new_tensor([0, 2048, 4096, 6144, 8192], dtype=torch.long) >>> o_var, ht_var = chunk_gated_delta_rule( q, k, v, g, beta, initial_state=h0, output_final_state=True, cu_seqlens=cu_seqlens )
Source code in vllm/model_executor/layers/fla/ops/chunk.py
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 218 219 220 221 222 223 224 225 |
|
chunk_gated_delta_rule_fwd ¶
chunk_gated_delta_rule_fwd(
q: Tensor,
k: Tensor,
v: Tensor,
g: Tensor,
beta: Tensor,
scale: float,
initial_state: Tensor,
output_final_state: bool,
cu_seqlens: Optional[LongTensor] = None,
)