@@ -46,20 +46,12 @@ def _rotate_gptj(x: torch.Tensor) -> torch.Tensor:
46
46
return x .flatten (- 2 )
47
47
48
48
49
- def _apply_rotary_emb (
49
+ def _apply_rotary_emb_torch (
50
50
x : torch .Tensor ,
51
51
cos : torch .Tensor ,
52
52
sin : torch .Tensor ,
53
53
is_neox_style : bool ,
54
54
) -> torch .Tensor :
55
- """
56
- Args:
57
- x: [num_tokens, num_heads, head_size]
58
- cos: [num_tokens, head_size // 2]
59
- sin: [num_tokens, head_size // 2]
60
- is_neox_style: Whether to use the Neox-style or GPT-J-style rotary
61
- positional embeddings.
62
- """
63
55
cos = cos .unsqueeze (- 2 ).to (x .dtype )
64
56
sin = sin .unsqueeze (- 2 ).to (x .dtype )
65
57
if is_neox_style :
@@ -75,6 +67,24 @@ def _apply_rotary_emb(
75
67
return torch .stack ((o1 , o2 ), dim = - 1 ).flatten (- 2 )
76
68
77
69
70
+ def _apply_rotary_emb (x : torch .Tensor , cos : torch .Tensor , sin : torch .Tensor ,
71
+ is_neox_style : bool ) -> torch .Tensor :
72
+ """
73
+ Args:
74
+ x: [num_tokens, num_heads, head_size]
75
+ cos: [num_tokens, head_size // 2]
76
+ sin: [num_tokens, head_size // 2]
77
+ is_neox_style: Whether to use the Neox-style or GPT-J-style rotary
78
+ positional embeddings.
79
+ """
80
+ if current_platform .is_cuda_alike ():
81
+ from vllm .vllm_flash_attn .layers .rotary import apply_rotary_emb
82
+ return apply_rotary_emb (x .unsqueeze (0 ), cos , sin ,
83
+ not is_neox_style ).squeeze (0 )
84
+ else :
85
+ return _apply_rotary_emb_torch (x , cos , sin , is_neox_style )
86
+
87
+
78
88
@CustomOp .register ("rotary_embedding" )
79
89
class RotaryEmbedding (CustomOp ):
80
90
"""Original rotary positional embedding."""
@@ -141,14 +151,16 @@ def forward_native(
141
151
query = query .view (num_tokens , - 1 , self .head_size )
142
152
query_rot = query [..., :self .rotary_dim ]
143
153
query_pass = query [..., self .rotary_dim :]
144
- query_rot = _apply_rotary_emb (query_rot , cos , sin , self .is_neox_style )
154
+ query_rot = _apply_rotary_emb_torch (query_rot , cos , sin ,
155
+ self .is_neox_style )
145
156
query = torch .cat ((query_rot , query_pass ), dim = - 1 ).reshape (query_shape )
146
157
147
158
key_shape = key .shape
148
159
key = key .view (num_tokens , - 1 , self .head_size )
149
160
key_rot = key [..., :self .rotary_dim ]
150
161
key_pass = key [..., self .rotary_dim :]
151
- key_rot = _apply_rotary_emb (key_rot , cos , sin , self .is_neox_style )
162
+ key_rot = _apply_rotary_emb_torch (key_rot , cos , sin ,
163
+ self .is_neox_style )
152
164
key = torch .cat ((key_rot , key_pass ), dim = - 1 ).reshape (key_shape )
153
165
return query , key
154
166
0 commit comments