rotary_embedding.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. # coding=utf-8
  2. # Adapted from
  3. # https://github.com/huggingface/transformers/blob/v4.33.2/src/transformers/models/llama/modeling_llama.py
  4. # Copyright 2023 The PygmalionAI team.
  5. # Copyright 2023 The vLLM team.
  6. # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
  7. #
  8. # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
  9. # and OPT implementations in this library. It has been modified from its
  10. # original forms to accommodate minor architectural differences compared
  11. # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
  12. #
  13. # Licensed under the Apache License, Version 2.0 (the "License");
  14. # you may not use this file except in compliance with the License.
  15. # You may obtain a copy of the License at
  16. #
  17. # http://www.apache.org/licenses/LICENSE-2.0
  18. #
  19. # Unless required by applicable law or agreed to in writing, software
  20. # distributed under the License is distributed on an "AS IS" BASIS,
  21. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. # See the License for the specific language governing permissions and
  23. # limitations under the License.
  24. """Rotary Positional Embeddings."""
  25. import math
  26. from typing import Any, Dict, List, Optional, Tuple, Union
  27. import torch
  28. import torch.nn as nn
  29. from aphrodite.modeling._custom_op import CustomOp
  30. from aphrodite.platforms import current_platform
  31. def _rotate_neox(x: torch.Tensor) -> torch.Tensor:
  32. x1 = x[..., :x.shape[-1] // 2]
  33. x2 = x[..., x.shape[-1] // 2:]
  34. return torch.cat((-x2, x1), dim=-1)
  35. def _rotate_gptj(x: torch.Tensor) -> torch.Tensor:
  36. x1 = x[..., ::2]
  37. x2 = x[..., 1::2]
  38. x = torch.stack((-x2, x1), dim=-1)
  39. return x.flatten(-2)
  40. # for TPUs
  41. def _apply_rotary_emb(
  42. x: torch.Tensor,
  43. cos: torch.Tensor,
  44. sin: torch.Tensor,
  45. ) -> torch.Tensor:
  46. """
  47. Args:
  48. x: [num_tokens, num_heads, head_size]
  49. cos: [num_tokens, head_size // 2]
  50. sin: [num_tokens, head_size // 2]
  51. """
  52. orig_dtype = x.dtype
  53. x = x.float()
  54. x1, x2 = torch.chunk(x, 2, dim=-1)
  55. cos = cos.unsqueeze(-2)
  56. sin = sin.unsqueeze(-2)
  57. o1 = x1 * cos - x2 * sin
  58. o2 = x2 * cos + x1 * sin
  59. return torch.cat((o1, o2), dim=-1).to(orig_dtype)
  60. class RotaryEmbedding(CustomOp):
  61. """Original rotary positional embedding."""
  62. def __init__(
  63. self,
  64. head_size: int,
  65. rotary_dim: int,
  66. max_position_embeddings: int,
  67. base: int,
  68. is_neox_style: bool,
  69. dtype: torch.dtype,
  70. ) -> None:
  71. super().__init__()
  72. self.head_size = head_size
  73. self.rotary_dim = rotary_dim
  74. self.max_position_embeddings = max_position_embeddings
  75. self.base = base
  76. self.is_neox_style = is_neox_style
  77. self.dtype = dtype
  78. cache = self._compute_cos_sin_cache()
  79. cache = cache.to(dtype)
  80. self.register_buffer("cos_sin_cache", cache, persistent=False)
  81. self.use_native2 = current_platform.is_tpu() and is_neox_style
  82. def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
  83. """Compute the inverse frequency."""
  84. # NOTE: The HF implementation uses `torch.arange(...).float()`.
  85. # However, we use `torch.arange(..., dtype=torch.float)` instead to
  86. # avoid numerical issues with large base values (e.g., 10000000).
  87. # This may cause a slight numerical difference between the HF
  88. # implementation and ours.
  89. # NOTE: To exactly match the HF implementation, we need to
  90. # use CPU to compute the cache and then move it to GPU. However, we
  91. # create the cache on GPU for faster initialization. This may cause
  92. # a slight numerical difference between the HF implementation and ours.
  93. inv_freq = 1.0 / (base**(torch.arange(
  94. 0, self.rotary_dim, 2, dtype=torch.float) / self.rotary_dim))
  95. return inv_freq
  96. def _compute_cos_sin_cache(self) -> torch.Tensor:
  97. """Compute the cos and sin cache."""
  98. inv_freq = self._compute_inv_freq(self.base)
  99. t = torch.arange(self.max_position_embeddings, dtype=torch.float)
  100. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  101. cos = freqs.cos()
  102. sin = freqs.sin()
  103. cache = torch.cat((cos, sin), dim=-1)
  104. return cache
  105. def forward_native(
  106. self,
  107. positions: torch.Tensor,
  108. query: torch.Tensor,
  109. key: torch.Tensor,
  110. offsets: Optional[torch.Tensor] = None,
  111. ) -> Tuple[torch.Tensor, torch.Tensor]:
  112. """A PyTorch-native implementation equivalent to forward().
  113. This method mimics the implementation of the custom CUDA kernel
  114. used in `forward_cuda()`.
  115. """
  116. query = query.view(*query.shape[:-1], -1, self.head_size)
  117. key = key.view(*key.shape[:-1], -1, self.head_size)
  118. query_rot = query[..., :self.rotary_dim]
  119. key_rot = key[..., :self.rotary_dim]
  120. if self.rotary_dim < self.head_size:
  121. query_pass = query[..., self.rotary_dim:]
  122. key_pass = key[..., self.rotary_dim:]
  123. self.cos_sin_cache: torch.Tensor = self.cos_sin_cache.to(
  124. positions.device, dtype=query.dtype)
  125. cos_sin = self.cos_sin_cache[torch.add(positions, offsets)
  126. if offsets is not None else positions]
  127. cos, sin = cos_sin.chunk(2, dim=-1)
  128. if self.is_neox_style:
  129. # NOTE: Here we assume that the positions tensor has the
  130. # shape [batch_size, seq_len].
  131. cos = cos.repeat(1, 1, 2).unsqueeze(-2)
  132. sin = sin.repeat(1, 1, 2).unsqueeze(-2)
  133. else:
  134. cos = cos.repeat_interleave(2, dim=-1).unsqueeze(-2)
  135. sin = sin.repeat_interleave(2, dim=-1).unsqueeze(-2)
  136. rotate_fn = _rotate_neox if self.is_neox_style else _rotate_gptj
  137. query_rot = query_rot * cos + rotate_fn(query_rot) * sin
  138. key_rot = key_rot * cos + rotate_fn(key_rot) * sin
  139. if self.rotary_dim < self.head_size:
  140. query = torch.cat((query_rot, query_pass), dim=-1)
  141. key = torch.cat((key_rot, key_pass), dim=-1)
  142. else:
  143. query = query_rot
  144. key = key_rot
  145. query = query.flatten(-2)
  146. key = key.flatten(-2)
  147. return query, key
  148. def forward_native2(
  149. self,
  150. positions: torch.Tensor,
  151. query: torch.Tensor,
  152. key: torch.Tensor,
  153. offsets: Optional[torch.Tensor] = None,
  154. ) -> Tuple[torch.Tensor, torch.Tensor]:
  155. """Another PyTorch-native implementation of forward().
  156. This method might perform better than `forward_native()` when compiled.
  157. """
  158. if offsets is not None:
  159. positions = positions + offsets
  160. positions = positions.flatten()
  161. num_tokens = positions.shape[0]
  162. cos_sin = self.cos_sin_cache.index_select(0, positions)
  163. cos, sin = cos_sin.chunk(2, dim=-1)
  164. query_shape = query.shape
  165. query = query.view(num_tokens, -1, self.head_size)
  166. query_rot = query[..., :self.rotary_dim]
  167. query_pass = query[..., self.rotary_dim:]
  168. query_rot = _apply_rotary_emb(query_rot, cos, sin)
  169. query = torch.cat((query_rot, query_pass), dim=-1).reshape(query_shape)
  170. key_shape = key.shape
  171. key = key.view(num_tokens, -1, self.head_size)
  172. key_rot = key[..., :self.rotary_dim]
  173. key_pass = key[..., self.rotary_dim:]
  174. key_rot = _apply_rotary_emb(key_rot, cos, sin)
  175. key = torch.cat((key_rot, key_pass), dim=-1).reshape(key_shape)
  176. return query, key
  177. def forward_cuda(
  178. self,
  179. positions: torch.Tensor,
  180. query: torch.Tensor,
  181. key: torch.Tensor,
  182. offsets: Optional[torch.Tensor] = None,
  183. ) -> Tuple[torch.Tensor, torch.Tensor]:
  184. from aphrodite import _custom_ops as ops
  185. self.cos_sin_cache = self.cos_sin_cache.to(positions.device,
  186. dtype=query.dtype)
  187. # ops.rotary_embedding()/batched_rotary_embedding()
  188. # are in-place operations that update the query and key tensors.
  189. if offsets is not None:
  190. ops.batched_rotary_embedding(positions, query, key, self.head_size,
  191. self.cos_sin_cache,
  192. self.is_neox_style, self.rotary_dim,
  193. offsets)
  194. else:
  195. ops.rotary_embedding(positions, query, key, self.head_size,
  196. self.cos_sin_cache, self.is_neox_style)
  197. return query, key
  198. def forward_xpu(
  199. self,
  200. positions: torch.Tensor,
  201. query: torch.Tensor,
  202. key: torch.Tensor,
  203. offsets: Optional[torch.Tensor] = None,
  204. ) -> Tuple[torch.Tensor, torch.Tensor]:
  205. from aphrodite._ipex_ops import ipex_ops as ops
  206. self.cos_sin_cache = self.cos_sin_cache.to(positions.device,
  207. dtype=query.dtype)
  208. # ops.rotary_embedding()/batched_rotary_embedding()
  209. # are in-place operations that update the query and key tensors.
  210. if offsets is not None:
  211. ops.batched_rotary_embedding(positions, query, key, self.head_size,
  212. self.cos_sin_cache,
  213. self.is_neox_style, self.rotary_dim,
  214. offsets)
  215. else:
  216. ops.rotary_embedding(positions, query, key, self.head_size,
  217. self.cos_sin_cache, self.is_neox_style)
  218. return query, key
  219. def forward_tpu(
  220. self,
  221. positions: torch.Tensor,
  222. query: torch.Tensor,
  223. key: torch.Tensor,
  224. offsets: Optional[torch.Tensor] = None,
  225. ) -> Tuple[torch.Tensor, torch.Tensor]:
  226. forward_fn = (self.forward_native2
  227. if self.use_native2 else self.forward_native)
  228. return forward_fn(positions, query, key, offsets)
  229. def extra_repr(self) -> str:
  230. s = f"head_size={self.head_size}, rotary_dim={self.rotary_dim}"
  231. s += f", max_position_embeddings={self.max_position_embeddings}"
  232. s += f", base={self.base}, is_neox_style={self.is_neox_style}"
  233. return s
  234. class LinearScalingRotaryEmbedding(RotaryEmbedding):
  235. """RotaryEmbedding extended with linear scaling.
  236. It supports multiple scaling factors. Since multiple LoRA adapters may have
  237. different scaling factors, we need multiple cos/sin caches. In this way,
  238. instead of running rotary embedding kernel per lora, we can run multiple
  239. lora in a batched way.
  240. In addition to that, we also keep the cos/sin cache for the scaling factor
  241. of 1 (default) at all times.
  242. Exemplary for two scaling factors x=1, y and z with embeddings
  243. [[x11, x12, ... x1m], ..., [xn1, xn2, ..., xnm]] and
  244. [[y11, y12, ... y1o], ..., [yn1, yn2, ..., yno]], and
  245. [[z11, z12, ... z1p], ..., [zn1, zn2, ..., znp]],
  246. we construct the cos/sin cache as follows:
  247. [[x11, x12, ... x1m, y11, y12, ... y1o, z11, z12, ... z1p],
  248. ...
  249. [xn1, xn2, ... xnm, yn1, yn2, ... yno, zn1, zn2, ... znp]]
  250. We then use offsets to index into the cos/sin cache for
  251. the respective scaling factors.
  252. The offset to cache can be accessed via `scaling_factor_to_offset` API.
  253. Credits to the Reddit user /u/kaiokendev
  254. """
  255. def __init__(
  256. self,
  257. head_size: int,
  258. rotary_dim: int,
  259. max_position_embeddings: int,
  260. base: int,
  261. is_neox_style: bool,
  262. scaling_factors: Union[List[float], float],
  263. dtype: torch.dtype,
  264. ) -> None:
  265. if isinstance(scaling_factors, float):
  266. scaling_factors = [scaling_factors]
  267. self.scaling_factors: List[float] = scaling_factors # noqa
  268. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  269. is_neox_style, dtype)
  270. # Lazy initialized.
  271. self._scaling_factor_to_offset: Dict[float, int]
  272. def _compute_cos_sin_cache(self) -> torch.Tensor:
  273. inv_freq = self._compute_inv_freq(self.base)
  274. cache_list: List[torch.Tensor] = []
  275. # offsets to the next cache in a tensor.
  276. # Each offset corresponds to the same index in scaling_factors.
  277. offsets: List[int] = []
  278. for scaling_factor in self.scaling_factors:
  279. # NOTE: self.max_position_embeddings is the original
  280. # maximum length before applying the rope scaling.
  281. # Thus, the maximum length after applying the rope scaling is
  282. # self.max_position_embeddings * self.scaling_factor.
  283. max_len = self.max_position_embeddings * scaling_factor
  284. t = torch.arange(max_len, dtype=torch.float)
  285. t = t / scaling_factor
  286. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  287. cos = freqs.cos()
  288. sin = freqs.sin()
  289. cache = torch.cat((cos, sin), dim=-1)
  290. if not cache_list:
  291. offset = 0
  292. else:
  293. last_offset = offsets[-1]
  294. next_max_len = cache_list[-1].shape[0]
  295. offset = last_offset + next_max_len
  296. offsets.append(offset)
  297. cache_list.append(cache)
  298. self._scaling_factor_to_offset = {
  299. float(scaling_factor): offsets[i]
  300. for i, scaling_factor in enumerate(self.scaling_factors)
  301. }
  302. assert len(self.scaling_factors) == len(offsets)
  303. return torch.cat(cache_list, dim=0)
  304. @property
  305. def scaling_factor_to_offset(self) -> Dict[float, int]:
  306. return self._scaling_factor_to_offset
  307. class DynamicNTKScalingRotaryEmbedding(RotaryEmbedding):
  308. """RotaryEmbedding extended with Dynamic NTK scaling.
  309. Credits to the Reddit users /u/bloc97 and /u/emozilla
  310. """
  311. def __init__(
  312. self,
  313. head_size: int,
  314. rotary_dim: int,
  315. max_position_embeddings: int,
  316. base: int,
  317. is_neox_style: bool,
  318. scaling_factor: float,
  319. dtype: torch.dtype,
  320. ) -> None:
  321. self.scaling_factor = scaling_factor
  322. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  323. is_neox_style, dtype)
  324. def _compute_cos_sin_cache(self) -> torch.Tensor:
  325. # NOTE: self.max_position_embeddings is the original
  326. # maximum length before applying the rope scaling.
  327. # Thus, the maximum length after applying the rope scaling is
  328. # self.max_position_embeddings * self.scaling_factor.
  329. max_len = self.max_position_embeddings * self.scaling_factor
  330. base = self.base * (
  331. (self.scaling_factor * max_len / self.max_position_embeddings) -
  332. (self.scaling_factor - 1))**(self.rotary_dim /
  333. (self.rotary_dim - 2))
  334. inv_freq = self._compute_inv_freq(base)
  335. t = torch.arange(max_len, dtype=torch.float)
  336. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  337. cos = freqs.cos()
  338. sin = freqs.sin()
  339. cache = torch.cat((cos, sin), dim=-1)
  340. return cache
  341. # Inverse dim formula to find dim based on number of rotations
  342. def _yarn_find_correction_dim(num_rotations: int,
  343. dim: int,
  344. base: float = 10000,
  345. max_position_embeddings: int = 2048) -> float:
  346. return (dim * math.log(max_position_embeddings /
  347. (num_rotations * 2 * math.pi))) / (2 *
  348. math.log(base))
  349. # Find dim range bounds based on rotations
  350. def _yarn_find_correction_range(
  351. low_rot: int,
  352. high_rot: int,
  353. dim: int,
  354. base: float = 10000,
  355. max_position_embeddings: int = 2048) -> Tuple[int, int]:
  356. low = math.floor(
  357. _yarn_find_correction_dim(low_rot, dim, base, max_position_embeddings))
  358. high = math.ceil(
  359. _yarn_find_correction_dim(high_rot, dim, base,
  360. max_position_embeddings))
  361. return max(low, 0), min(high, dim - 1) # Clamp values just in case
  362. def _yarn_linear_ramp_mask(low: float, high: float, dim: int,
  363. dtype: torch.dtype) -> torch.Tensor:
  364. if low == high:
  365. high += 0.001 # Prevent singularity
  366. linear_func = (torch.arange(dim, dtype=dtype) - low) / (high - low)
  367. ramp_func = torch.clamp(linear_func, 0, 1)
  368. return ramp_func
  369. def _yarn_get_mscale(scale: float = 1) -> float:
  370. if scale <= 1:
  371. return 1.0
  372. return 0.1 * math.log(scale) + 1.0
  373. class YaRNScalingRotaryEmbedding(RotaryEmbedding):
  374. """RotaryEmbedding extended with YaRN method.
  375. Credits to Peng et al. github.com/jquesnelle/yarn
  376. """
  377. def __init__(
  378. self,
  379. head_size: int,
  380. rotary_dim: int,
  381. max_position_embeddings: int,
  382. base: int,
  383. is_neox_style: bool,
  384. scaling_factor: float,
  385. dtype: torch.dtype,
  386. *,
  387. extrapolation_factor: float = 1,
  388. attn_factor: float = 1,
  389. beta_fast: int = 32,
  390. beta_slow: int = 1,
  391. ) -> None:
  392. self.scaling_factor = scaling_factor
  393. self.extrapolation_factor = extrapolation_factor
  394. self.attn_factor = attn_factor
  395. self.beta_fast = beta_fast
  396. self.beta_slow = beta_slow
  397. # Get n-d magnitude scaling corrected for interpolation
  398. self.mscale = float(
  399. _yarn_get_mscale(self.scaling_factor) * attn_factor)
  400. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  401. is_neox_style, dtype)
  402. def _compute_inv_freq(self, scaling_factor: float) -> torch.Tensor:
  403. pos_freqs = self.base**(
  404. torch.arange(0, self.rotary_dim, 2, dtype=torch.float) /
  405. self.rotary_dim)
  406. inv_freq_extrapolation = 1.0 / pos_freqs
  407. inv_freq_interpolation = 1.0 / (scaling_factor * pos_freqs)
  408. low, high = _yarn_find_correction_range(self.beta_fast, self.beta_slow,
  409. self.rotary_dim, self.base,
  410. self.max_position_embeddings)
  411. # Get n-d rotational scaling corrected for extrapolation
  412. inv_freq_mask = (1 - _yarn_linear_ramp_mask(
  413. low, high, self.rotary_dim // 2,
  414. dtype=torch.float)) * self.extrapolation_factor
  415. inv_freq = inv_freq_interpolation * (
  416. 1 - inv_freq_mask) + inv_freq_extrapolation * inv_freq_mask
  417. return inv_freq
  418. def _compute_cos_sin_cache(self) -> torch.Tensor:
  419. inv_freq = self._compute_inv_freq(self.scaling_factor)
  420. t = torch.arange(self.max_position_embeddings * self.scaling_factor,
  421. dtype=torch.float32)
  422. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  423. cos = (freqs.cos() * self.mscale)
  424. sin = (freqs.sin() * self.mscale)
  425. cache = torch.cat((cos, sin), dim=-1)
  426. return cache
  427. class Phi3LongRoPEScaledRotaryEmbedding(nn.Module):
  428. """Phi3 family of models scaled rotary embedding.
  429. Based on the original RotaryEmbedding implementation.
  430. """
  431. def __init__(
  432. self,
  433. head_size: int,
  434. rotary_dim: int,
  435. max_position_embeddings: int,
  436. original_max_position_embeddings: int,
  437. base: int,
  438. is_neox_style: bool,
  439. dtype: torch.dtype,
  440. short_factor: List[float],
  441. long_factor: List[float],
  442. short_mscale: float = 1.0,
  443. long_mscale: float = 1.0,
  444. ):
  445. super().__init__()
  446. if rotary_dim != head_size:
  447. raise ValueError(
  448. f"`Phi3LongRoPEScaledRotaryEmbedding` does not support \
  449. rotary_dim != head_size ({rotary_dim}!={head_size}).")
  450. if is_neox_style is False:
  451. raise ValueError(
  452. "`Phi3LongRoPEScaledRotaryEmbedding` only supports neox_style."
  453. )
  454. self.head_size = head_size
  455. self.max_position_embeddings = max_position_embeddings
  456. self.original_max_position_embeddings = original_max_position_embeddings
  457. self.base = base
  458. self.short_factor = short_factor
  459. self.long_factor = long_factor
  460. self.short_mscale = short_mscale
  461. self.long_mscale = long_mscale
  462. scale = (self.max_position_embeddings /
  463. self.original_max_position_embeddings)
  464. if scale <= 1.0:
  465. self.scaling_factor = 1.0
  466. else:
  467. self.scaling_factor = math.sqrt(
  468. 1 + math.log(scale) /
  469. math.log(self.original_max_position_embeddings))
  470. short_cache = self._compute_cos_sin_cache(
  471. original_max_position_embeddings, short_factor, short_mscale)
  472. short_cache = short_cache.to(dtype)
  473. self.register_buffer("short_cos_sin_cache",
  474. short_cache,
  475. persistent=False)
  476. long_cache = self._compute_cos_sin_cache(max_position_embeddings,
  477. long_factor, long_mscale)
  478. long_cache = long_cache.to(dtype)
  479. self.register_buffer("long_cos_sin_cache",
  480. long_cache,
  481. persistent=False)
  482. long_short_cache = torch.cat(
  483. [self.short_cos_sin_cache, self.long_cos_sin_cache], dim=0)
  484. self.register_buffer("long_short_cos_sin_cache",
  485. long_short_cache,
  486. persistent=False)
  487. def _compute_inv_freq(self, rescale_factors: List[float]) -> torch.Tensor:
  488. rescale_factors = torch.tensor(rescale_factors, dtype=torch.float32)
  489. inv_freq = 1.0 / (rescale_factors * (self.base**(torch.arange(
  490. 0, self.head_size, 2, dtype=torch.float) / self.head_size)))
  491. return inv_freq
  492. def _compute_cos_sin_cache(
  493. self,
  494. max_position_embeddings: int,
  495. rescale_factors: List[float],
  496. mscale: float,
  497. ) -> torch.Tensor:
  498. inv_freq = self._compute_inv_freq(rescale_factors)
  499. t = torch.arange(max_position_embeddings, dtype=torch.float)
  500. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  501. cos = freqs.cos() * mscale * self.scaling_factor
  502. sin = freqs.sin() * mscale * self.scaling_factor
  503. cache = torch.cat((cos, sin), dim=-1)
  504. return cache
  505. def forward(
  506. self,
  507. positions: torch.Tensor,
  508. query: torch.Tensor,
  509. key: torch.Tensor,
  510. offsets: Optional[torch.Tensor] = None,
  511. ) -> Tuple[torch.Tensor, torch.Tensor]:
  512. query = query.view(*query.shape[:-1], -1, self.head_size)
  513. key = key.view(*key.shape[:-1], -1, self.head_size)
  514. k = self.original_max_position_embeddings
  515. long_prompt_offset = (torch.any(positions > k).float() *
  516. torch.full_like(positions, k)).long()
  517. idx = (torch.add(positions, long_prompt_offset)
  518. if long_prompt_offset is not None else positions)
  519. self.long_short_cos_sin_cache: torch.Tensor = (
  520. self.long_short_cos_sin_cache.to(idx.device))
  521. idx = torch.add(idx, offsets) if offsets is not None else idx
  522. cos_sin = torch.index_select(self.long_short_cos_sin_cache, 0, idx)
  523. cos, sin = cos_sin.chunk(2, dim=-1)
  524. cos = cos.repeat(1, 2).unsqueeze(-2)
  525. sin = sin.repeat(1, 2).unsqueeze(-2)
  526. query = query * cos + _rotate_neox(query) * sin
  527. key = key * cos + _rotate_neox(key) * sin
  528. return query.flatten(-2), key.flatten(-2)
  529. def yarn_get_mscale(scale: float = 1, mscale: float = 1) -> float:
  530. if scale <= 1:
  531. return 1.0
  532. return 0.1 * mscale * math.log(scale) + 1.0
  533. class DeepseekScalingRotaryEmbedding(RotaryEmbedding):
  534. """RotaryEmbedding extended with YaRN method.
  535. Credits to Peng et al. github.com/jquesnelle/yarn
  536. """
  537. def __init__(
  538. self,
  539. head_size: int,
  540. rotary_dim: int,
  541. max_position_embeddings: int,
  542. base: int,
  543. is_neox_style: bool,
  544. scaling_factor: float,
  545. dtype: torch.dtype,
  546. *,
  547. extrapolation_factor: float = 1,
  548. attn_factor: float = 1,
  549. beta_fast: int = 32,
  550. beta_slow: int = 1,
  551. mscale: float = 1,
  552. mscale_all_dim: float = 0,
  553. ) -> None:
  554. self.scaling_factor = scaling_factor
  555. self.extrapolation_factor = extrapolation_factor
  556. self.attn_factor = attn_factor
  557. self.beta_fast = beta_fast
  558. self.beta_slow = beta_slow
  559. # Get n-d magnitude scaling corrected for interpolation.
  560. self.mscale = float(
  561. yarn_get_mscale(self.scaling_factor, float(mscale)) /
  562. yarn_get_mscale(self.scaling_factor, float(mscale_all_dim)) *
  563. attn_factor)
  564. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  565. is_neox_style, dtype)
  566. def _compute_inv_freq(self, scaling_factor: float) -> torch.Tensor:
  567. pos_freqs = self.base**(torch.arange(
  568. 0, self.rotary_dim, 2, dtype=torch.float, device="cuda") /
  569. self.rotary_dim)
  570. inv_freq_extrapolation = 1.0 / pos_freqs
  571. inv_freq_interpolation = 1.0 / (scaling_factor * pos_freqs)
  572. low, high = _yarn_find_correction_range(self.beta_fast, self.beta_slow,
  573. self.rotary_dim, self.base,
  574. self.max_position_embeddings)
  575. # Get n-d rotational scaling corrected for extrapolation
  576. inv_freq_mask = (1 - _yarn_linear_ramp_mask(
  577. low, high, self.rotary_dim // 2,
  578. dtype=torch.float)) * self.extrapolation_factor
  579. inv_freq = inv_freq_interpolation * (
  580. 1 - inv_freq_mask) + inv_freq_extrapolation * inv_freq_mask
  581. return inv_freq
  582. def _compute_cos_sin_cache(self) -> torch.Tensor:
  583. inv_freq = self._compute_inv_freq(self.scaling_factor)
  584. t = torch.arange(self.max_position_embeddings * self.scaling_factor,
  585. device="cuda",
  586. dtype=torch.float32)
  587. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  588. cos = (freqs.cos() * self.mscale)
  589. sin = (freqs.sin() * self.mscale)
  590. cache = torch.cat((cos, sin), dim=-1)
  591. print("Cache shape", cache.shape)
  592. return cache
  593. def forward(
  594. self,
  595. positions: torch.Tensor,
  596. query: torch.Tensor,
  597. key: torch.Tensor,
  598. offsets: Optional[torch.Tensor] = None,
  599. ) -> Tuple[torch.Tensor, torch.Tensor]:
  600. """PyTorch-native implementation equivalent to forward()."""
  601. query_rot = query[..., :self.rotary_dim]
  602. key_rot = key[..., :self.rotary_dim]
  603. if self.rotary_dim < self.head_size:
  604. query_pass = query[..., self.rotary_dim:]
  605. key_pass = key[..., self.rotary_dim:]
  606. self.cos_sin_cache: torch.Tensor = self.cos_sin_cache.to(
  607. positions.device)
  608. cos_sin = self.cos_sin_cache[torch.add(positions, offsets)
  609. if offsets is not None else positions]
  610. cos, sin = cos_sin.chunk(2, dim=-1)
  611. if self.is_neox_style:
  612. # NOTE: Here we assume that the positions tensor has the
  613. # shape [batch_size, seq_len].
  614. cos = cos.repeat(1, 1, 2).unsqueeze(-2)
  615. sin = sin.repeat(1, 1, 2).unsqueeze(-2)
  616. else:
  617. cos = cos.repeat_interleave(2, dim=-1).unsqueeze(-2)
  618. sin = sin.repeat_interleave(2, dim=-1).unsqueeze(-2)
  619. rotate_fn = _rotate_neox if self.is_neox_style else _rotate_gptj
  620. query_rot = query_rot * cos + rotate_fn(query_rot) * sin
  621. key_rot = key_rot * cos + rotate_fn(key_rot) * sin
  622. if self.rotary_dim < self.head_size:
  623. query = torch.cat((query_rot, query_pass), dim=-1)
  624. key = torch.cat((key_rot, key_pass), dim=-1)
  625. else:
  626. query = query_rot
  627. key = key_rot
  628. return query, key
  629. class GemmaRotaryEmbedding(RotaryEmbedding):
  630. def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
  631. # https://github.com/huggingface/transformers/blob/v4.41.2/src/transformers/models/gemma/modeling_gemma.py#L107
  632. inv_freq = 1.0 / (base**(
  633. torch.arange(0, self.rotary_dim, 2, dtype=torch.int64).float() /
  634. self.rotary_dim))
  635. return inv_freq
  636. class Llama3RotaryEmbedding(RotaryEmbedding):
  637. def __init__(
  638. self,
  639. head_size: int,
  640. rotary_dim: int,
  641. max_position_embeddings: int,
  642. base: int,
  643. is_neox_style: bool,
  644. dtype: torch.dtype,
  645. scaling_factor: float,
  646. low_freq_factor: float,
  647. high_freq_factor: float,
  648. orig_max_position: int,
  649. ) -> None:
  650. self.scaling_factor = scaling_factor
  651. self.low_freq_factor = low_freq_factor
  652. self.high_freq_factor = high_freq_factor
  653. self.orig_max_position = orig_max_position
  654. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  655. is_neox_style, dtype)
  656. def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
  657. inv_freqs = super()._compute_inv_freq(base)
  658. low_freq_wavelen = self.orig_max_position / self.low_freq_factor
  659. high_freq_wavelen = self.orig_max_position / self.high_freq_factor
  660. wave_len = 2 * math.pi / inv_freqs
  661. if self.low_freq_factor != self.high_freq_factor:
  662. smooth = (self.orig_max_position / wave_len - self.low_freq_factor
  663. ) / (self.high_freq_factor - self.low_freq_factor)
  664. else:
  665. smooth = 0
  666. new_freqs = torch.where(
  667. wave_len < high_freq_wavelen,
  668. inv_freqs,
  669. torch.where(
  670. wave_len > low_freq_wavelen,
  671. inv_freqs / self.scaling_factor,
  672. (1 - smooth) * inv_freqs / self.scaling_factor +
  673. smooth * inv_freqs,
  674. ),
  675. )
  676. return new_freqs
  677. _ROPE_DICT: Dict[Tuple, RotaryEmbedding] = {}
  678. def get_rope(
  679. head_size: int,
  680. rotary_dim: int,
  681. max_position: int,
  682. base: int,
  683. is_neox_style: bool = True,
  684. rope_scaling: Optional[Dict[str, Any]] = None,
  685. dtype: Optional[torch.dtype] = None,
  686. rotary_percent: float = 1.0,
  687. ) -> RotaryEmbedding:
  688. if dtype is None:
  689. dtype = torch.get_default_dtype()
  690. if rope_scaling is not None:
  691. # Transforms every value that is a list into a tuple for caching calls
  692. rope_scaling_tuple = {
  693. k: tuple(v) if isinstance(v, list) else v
  694. for k, v in rope_scaling.items()
  695. }
  696. rope_scaling_args = tuple(rope_scaling_tuple.items())
  697. else:
  698. rope_scaling_args = None
  699. if rotary_percent < 1.0:
  700. rotary_dim = int(rotary_dim * rotary_percent)
  701. key = (head_size, rotary_dim, max_position, base, is_neox_style,
  702. rope_scaling_args, dtype)
  703. if key in _ROPE_DICT:
  704. return _ROPE_DICT[key]
  705. if rope_scaling is None:
  706. rotary_emb = RotaryEmbedding(head_size, rotary_dim, max_position, base,
  707. is_neox_style, dtype)
  708. else:
  709. scaling_type = rope_scaling[
  710. "type"] if "type" in rope_scaling else rope_scaling["rope_type"]
  711. # The correct one should be "longrope" but keep "su" here
  712. # for backward compatible
  713. if scaling_type not in {"su", "longrope"}:
  714. scaling_factor = rope_scaling["factor"]
  715. if scaling_type == "llama3":
  716. low_freq_factor = rope_scaling["low_freq_factor"]
  717. high_freq_factor = rope_scaling["high_freq_factor"]
  718. original_max_position = rope_scaling[
  719. "original_max_position_embeddings"]
  720. rotary_emb = Llama3RotaryEmbedding(head_size, rotary_dim,
  721. max_position, base,
  722. is_neox_style, dtype,
  723. scaling_factor, low_freq_factor,
  724. high_freq_factor,
  725. original_max_position)
  726. elif scaling_type == "linear":
  727. rotary_emb = LinearScalingRotaryEmbedding(head_size, rotary_dim,
  728. max_position, base,
  729. is_neox_style,
  730. scaling_factor, dtype)
  731. elif scaling_type == "dynamic":
  732. rotary_emb = DynamicNTKScalingRotaryEmbedding(
  733. head_size, rotary_dim, max_position, base, is_neox_style,
  734. scaling_factor, dtype)
  735. elif scaling_type == "yarn":
  736. original_max_position = rope_scaling[
  737. "original_max_position_embeddings"]
  738. extra_kwargs = {
  739. k: v
  740. for k, v in rope_scaling.items()
  741. if k in ("extrapolation_factor", "attn_factor", "beta_fast",
  742. "beta_slow")
  743. }
  744. rotary_emb = YaRNScalingRotaryEmbedding(head_size, rotary_dim,
  745. original_max_position,
  746. base, is_neox_style,
  747. scaling_factor, dtype,
  748. **extra_kwargs)
  749. elif scaling_type == "deepseek_yarn":
  750. original_max_position = rope_scaling[
  751. "original_max_position_embeddings"]
  752. # assert max_position == original_max_position * scaling_factor
  753. extra_kwargs = {
  754. k: v
  755. for k, v in rope_scaling.items()
  756. if k in ("extrapolation_factor", "attn_factor", "beta_fast",
  757. "beta_slow", "mscale", "mscale_all_dim")
  758. }
  759. rotary_emb = DeepseekScalingRotaryEmbedding(
  760. head_size, rotary_dim, original_max_position, base,
  761. is_neox_style, scaling_factor, dtype, **extra_kwargs)
  762. # The correct one should be "longrope" but keep "su" here
  763. # for backward compatible
  764. elif scaling_type == "su" or scaling_type == "longrope":
  765. short_factor = rope_scaling["short_factor"]
  766. long_factor = rope_scaling["long_factor"]
  767. original_max_position = rope_scaling[
  768. "original_max_position_embeddings"]
  769. extra_kwargs = {
  770. k: v
  771. for k, v in rope_scaling.items()
  772. if k in ("short_mscale", "long_mscale")
  773. }
  774. rotary_emb = Phi3LongRoPEScaledRotaryEmbedding(
  775. head_size, rotary_dim, max_position, original_max_position,
  776. base, is_neox_style, dtype, short_factor, long_factor,
  777. **extra_kwargs)
  778. else:
  779. raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
  780. _ROPE_DICT[key] = rotary_emb
  781. return rotary_emb