rotary_embedding.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 PygmalionAI team.
  6. # Copyright 2023 The PygmalionAI team.
  7. # Copyright 2023 The vLLM team.
  8. # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
  9. #
  10. # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
  11. # and OPT implementations in this library. It has been modified from its
  12. # original forms to accommodate minor architectural differences compared
  13. # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
  14. #
  15. # Licensed under the Apache License, Version 2.0 (the "License");
  16. # you may not use this file except in compliance with the License.
  17. # You may obtain a copy of the License at
  18. #
  19. # http://www.apache.org/licenses/LICENSE-2.0
  20. #
  21. # Unless required by applicable law or agreed to in writing, software
  22. # distributed under the License is distributed on an "AS IS" BASIS,
  23. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. # See the License for the specific language governing permissions and
  25. # limitations under the License.
  26. """Rotary Positional Embeddings."""
  27. import math
  28. from typing import Any, Dict, List, Optional, Tuple, Union
  29. import torch
  30. import torch.nn as nn
  31. from aphrodite._C import ops
  32. def _rotate_neox(x: torch.Tensor) -> torch.Tensor:
  33. x1 = x[..., :x.shape[-1] // 2]
  34. x2 = x[..., x.shape[-1] // 2:]
  35. return torch.cat((-x2, x1), dim=-1)
  36. def _rotate_gptj(x: torch.Tensor) -> torch.Tensor:
  37. x1 = x[..., ::2]
  38. x2 = x[..., 1::2]
  39. x = torch.stack((-x2, x1), dim=-1)
  40. return x.flatten(-2)
  41. class RotaryEmbedding(nn.Module):
  42. """Original rotary positional embedding."""
  43. def __init__(
  44. self,
  45. head_size: int,
  46. rotary_dim: int,
  47. max_position_embeddings: int,
  48. base: int,
  49. is_neox_style: bool,
  50. ) -> None:
  51. super().__init__()
  52. self.head_size = head_size
  53. self.rotary_dim = rotary_dim
  54. self.max_position_embeddings = max_position_embeddings
  55. self.base = base
  56. self.is_neox_style = is_neox_style
  57. cache = self._compute_cos_sin_cache()
  58. cache = cache.to(torch.get_default_dtype())
  59. self.register_buffer("cos_sin_cache", cache, persistent=False)
  60. def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
  61. """Compute the inverse frequency."""
  62. # NOTE: The HF implementation uses `torch.arange(...).float()`.
  63. # However, we use `torch.arange(..., dtype=torch.float)` instead to
  64. # avoid numerical issues with large base values (e.g., 10000000).
  65. # This may cause a slight numerical difference between the HF
  66. # implementation and ours.
  67. # NOTE: To exactly match the HF implementation, we need to
  68. # use CPU to compute the cache and then move it to GPU. However, we
  69. # create the cache on GPU for faster initialization. This may cause
  70. # a slight numerical difference between the HF implementation and ours.
  71. inv_freq = 1.0 / (base**(torch.arange(
  72. 0, self.rotary_dim, 2, dtype=torch.float) / self.rotary_dim))
  73. return inv_freq
  74. def _compute_cos_sin_cache(self) -> torch.Tensor:
  75. """Compute the cos and sin cache."""
  76. inv_freq = self._compute_inv_freq(self.base)
  77. t = torch.arange(self.max_position_embeddings, dtype=torch.float)
  78. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  79. cos = freqs.cos()
  80. sin = freqs.sin()
  81. cache = torch.cat((cos, sin), dim=-1)
  82. return cache
  83. def _forward(
  84. self,
  85. positions: torch.Tensor,
  86. query: torch.Tensor,
  87. key: torch.Tensor,
  88. offsets: Optional[torch.Tensor] = None,
  89. ) -> Tuple[torch.Tensor, torch.Tensor]:
  90. """PyTorch-native implementation equivalent to forward()."""
  91. query = query.view(*query.shape[:-1], -1, self.head_size)
  92. key = key.view(*key.shape[:-1], -1, self.head_size)
  93. query_rot = query[..., :self.rotary_dim]
  94. key_rot = key[..., :self.rotary_dim]
  95. if self.rotary_dim < self.head_size:
  96. query_pass = query[..., self.rotary_dim:]
  97. key_pass = key[..., self.rotary_dim:]
  98. self.cos_sin_cache: torch.Tensor = self.cos_sin_cache.to(
  99. positions.device)
  100. cos_sin = self.cos_sin_cache[torch.add(positions, offsets)
  101. if offsets is not None else positions]
  102. cos, sin = cos_sin.chunk(2, dim=-1)
  103. if self.is_neox_style:
  104. # NOTE: Here we assume that the positions tensor has the
  105. # shape [batch_size, seq_len].
  106. cos = cos.repeat(1, 1, 2).unsqueeze(-2)
  107. sin = sin.repeat(1, 1, 2).unsqueeze(-2)
  108. else:
  109. cos = cos.repeat_interleave(2, dim=-1).unsqueeze(-2)
  110. sin = sin.repeat_interleave(2, dim=-1).unsqueeze(-2)
  111. rotate_fn = _rotate_neox if self.is_neox_style else _rotate_gptj
  112. query_rot = query_rot * cos + rotate_fn(query_rot) * sin
  113. key_rot = key_rot * cos + rotate_fn(key_rot) * sin
  114. if self.rotary_dim < self.head_size:
  115. query = torch.cat((query_rot, query_pass), dim=-1)
  116. key = torch.cat((key_rot, key_pass), dim=-1)
  117. else:
  118. query = query_rot
  119. key = key_rot
  120. query = query.flatten(-2)
  121. key = key.flatten(-2)
  122. return query, key
  123. def forward(
  124. self,
  125. positions: torch.Tensor,
  126. query: torch.Tensor,
  127. key: torch.Tensor,
  128. offsets: Optional[torch.Tensor] = None,
  129. ) -> Tuple[torch.Tensor, torch.Tensor]:
  130. self.cos_sin_cache = self.cos_sin_cache.to(positions.device)
  131. # ops.rotary_embedding()/batched_rotary_embedding()
  132. # are in-place operations that update the query and key tensors.
  133. if offsets is not None:
  134. ops.batched_rotary_embedding(positions, query, key, self.head_size,
  135. self.cos_sin_cache,
  136. self.is_neox_style, self.rotary_dim,
  137. offsets)
  138. else:
  139. ops.rotary_embedding(positions, query, key, self.head_size,
  140. self.cos_sin_cache, self.is_neox_style)
  141. return query, key
  142. class LinearScalingRotaryEmbedding(RotaryEmbedding):
  143. """RotaryEmbedding extended with linear scaling.
  144. Credits to the Reddit user /u/kaiokendev
  145. """
  146. def __init__(
  147. self,
  148. head_size: int,
  149. rotary_dim: int,
  150. max_position_embeddings: int,
  151. base: int,
  152. is_neox_style: bool,
  153. scaling_factors: Union[List[float], float],
  154. ) -> None:
  155. if isinstance(scaling_factors, float):
  156. scaling_factors = [scaling_factors]
  157. self.scaling_factors = scaling_factors
  158. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  159. is_neox_style)
  160. def _compute_cos_sin_cache(self) -> torch.Tensor:
  161. inv_freq = self._compute_inv_freq(self.base)
  162. cache_list = []
  163. for scaling_factor in self.scaling_factors:
  164. # NOTE: self.max_position_embeddings is the original
  165. # maximum length before applying the rope scaling.
  166. # Thus, the maximum length after applying the rope scaling is
  167. # self.max_position_embeddings * self.scaling_factor.
  168. max_len = self.max_position_embeddings * scaling_factor
  169. t = torch.arange(max_len, dtype=torch.float)
  170. t = t / scaling_factor
  171. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  172. cos = freqs.cos()
  173. sin = freqs.sin()
  174. cache = torch.cat((cos, sin), dim=-1)
  175. cache_list.append(cache)
  176. return torch.cat(cache_list, dim=0)
  177. class DynamicNTKScalingRotaryEmbedding(RotaryEmbedding):
  178. """RotaryEmbedding extended with Dynamic NTK scaling.
  179. Credits to the Reddit users /u/bloc97 and /u/emozilla
  180. """
  181. def __init__(
  182. self,
  183. head_size: int,
  184. rotary_dim: int,
  185. max_position_embeddings: int,
  186. base: int,
  187. is_neox_style: bool,
  188. scaling_factor: float,
  189. ) -> None:
  190. self.scaling_factor = scaling_factor
  191. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  192. is_neox_style)
  193. def _compute_cos_sin_cache(self) -> torch.Tensor:
  194. # NOTE: self.max_position_embeddings is the original
  195. # maximum length before applying the rope scaling.
  196. # Thus, the maximum length after applying the rope scaling is
  197. # self.max_position_embeddings * self.scaling_factor.
  198. max_len = self.max_position_embeddings * self.scaling_factor
  199. base = self.base * (
  200. (self.scaling_factor * max_len / self.max_position_embeddings) -
  201. (self.scaling_factor - 1))**(self.rotary_dim /
  202. (self.rotary_dim - 2))
  203. inv_freq = self._compute_inv_freq(base)
  204. t = torch.arange(max_len, dtype=torch.float)
  205. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  206. cos = freqs.cos()
  207. sin = freqs.sin()
  208. cache = torch.cat((cos, sin), dim=-1)
  209. return cache
  210. # Inverse dim formula to find dim based on number of rotations
  211. def _yarn_find_correction_dim(num_rotations: int,
  212. dim: int,
  213. base: float = 10000,
  214. max_position_embeddings: int = 2048) -> float:
  215. return (dim * math.log(max_position_embeddings /
  216. (num_rotations * 2 * math.pi))) / (2 *
  217. math.log(base))
  218. # Find dim range bounds based on rotations
  219. def _yarn_find_correction_range(
  220. low_rot: int,
  221. high_rot: int,
  222. dim: int,
  223. base: float = 10000,
  224. max_position_embeddings: int = 2048) -> Tuple[int, int]:
  225. low = math.floor(
  226. _yarn_find_correction_dim(low_rot, dim, base, max_position_embeddings))
  227. high = math.ceil(
  228. _yarn_find_correction_dim(high_rot, dim, base,
  229. max_position_embeddings))
  230. return max(low, 0), min(high, dim - 1) # Clamp values just in case
  231. def _yarn_linear_ramp_mask(low: float, high: float, dim: int,
  232. dtype: torch.dtype) -> torch.Tensor:
  233. if low == high:
  234. high += 0.001 # Prevent singularity
  235. linear_func = (torch.arange(dim, dtype=dtype) - low) / (high - low)
  236. ramp_func = torch.clamp(linear_func, 0, 1)
  237. return ramp_func
  238. def _yarn_get_mscale(scale: float = 1) -> float:
  239. if scale <= 1:
  240. return 1.0
  241. return 0.1 * math.log(scale) + 1.0
  242. class YaRNScalingRotaryEmbedding(RotaryEmbedding):
  243. """RotaryEmbedding extended with YaRN method.
  244. Credits to Peng et al. github.com/jquesnelle/yarn
  245. """
  246. def __init__(
  247. self,
  248. head_size: int,
  249. rotary_dim: int,
  250. max_position_embeddings: int,
  251. base: int,
  252. is_neox_style: bool,
  253. scaling_factor: float,
  254. *,
  255. extrapolation_factor: float = 1,
  256. attn_factor: float = 1,
  257. beta_fast: int = 32,
  258. beta_slow: int = 1,
  259. ) -> None:
  260. self.scaling_factor = scaling_factor
  261. self.extrapolation_factor = extrapolation_factor
  262. self.attn_factor = attn_factor
  263. self.beta_fast = beta_fast
  264. self.beta_slow = beta_slow
  265. # Get n-d magnitude scaling corrected for interpolation
  266. self.mscale = float(
  267. _yarn_get_mscale(self.scaling_factor) * attn_factor)
  268. super().__init__(head_size, rotary_dim, max_position_embeddings, base,
  269. is_neox_style)
  270. def _compute_inv_freq(self, scaling_factor: float) -> torch.Tensor:
  271. pos_freqs = self.base**(
  272. torch.arange(0, self.rotary_dim, 2, dtype=torch.float) /
  273. self.rotary_dim)
  274. inv_freq_extrapolation = 1.0 / pos_freqs
  275. inv_freq_interpolation = 1.0 / (scaling_factor * pos_freqs)
  276. low, high = _yarn_find_correction_range(self.beta_fast, self.beta_slow,
  277. self.rotary_dim, self.base,
  278. self.max_position_embeddings)
  279. # Get n-d rotational scaling corrected for extrapolation
  280. inv_freq_mask = (1 - _yarn_linear_ramp_mask(
  281. low, high, self.rotary_dim // 2,
  282. dtype=torch.float)) * self.extrapolation_factor
  283. inv_freq = inv_freq_interpolation * (
  284. 1 - inv_freq_mask) + inv_freq_extrapolation * inv_freq_mask
  285. return inv_freq
  286. def _compute_cos_sin_cache(self) -> torch.Tensor:
  287. inv_freq = self._compute_inv_freq(self.scaling_factor)
  288. t = torch.arange(self.max_position_embeddings * self.scaling_factor,
  289. dtype=torch.float32)
  290. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  291. cos = (freqs.cos() * self.mscale)
  292. sin = (freqs.sin() * self.mscale)
  293. cache = torch.cat((cos, sin), dim=-1)
  294. return cache
  295. class Phi3LongRoPERotaryEmbedding(nn.Module):
  296. def __init__(
  297. self,
  298. head_size: int,
  299. rotary_dim: int,
  300. max_position_embeddings: int,
  301. original_max_position_embeddings: int,
  302. base: int,
  303. is_neox_style: bool,
  304. short_factor: List[float],
  305. long_factor: List[float],
  306. short_mscale: float = 1.1,
  307. long_mscale: float = 1.225,
  308. ):
  309. super().__init__()
  310. if rotary_dim != head_size:
  311. raise ValueError(
  312. f"Rotary dim must be equal to head size, got {rotary_dim} "
  313. f"and {head_size}")
  314. if is_neox_style is False:
  315. raise ValueError(
  316. "Phi3SuScaledRotaryEmbedding only supports Neox style")
  317. self.head_size = head_size
  318. self.max_position_embeddings = max_position_embeddings
  319. self.original_max_position_embeddings = original_max_position_embeddings
  320. self.base = base
  321. self.short_factor = short_factor
  322. self.long_factor = long_factor
  323. self.short_mscale = short_mscale
  324. self.long_mscale = long_mscale
  325. short_cache = self._compute_cos_sin_cache(
  326. original_max_position_embeddings, short_factor, short_mscale)
  327. short_cache = short_cache.to(torch.get_default_dtype())
  328. self.register_buffer("short_cos_sin_cache",
  329. short_cache,
  330. persistent=False)
  331. long_cache = self._compute_cos_sin_cache(
  332. original_max_position_embeddings, long_factor, long_mscale)
  333. long_cache = long_cache.to(torch.get_default_dtype())
  334. self.register_buffer("long_cos_sin_cache",
  335. long_cache,
  336. persistent=False)
  337. long_short_cache = torch.cat(
  338. [self.short_cos_sin_cache, self.long_cos_sin_cache], dim=0)
  339. self.register_buffer("long_short_cos_sin_cache",
  340. long_short_cache,
  341. persistent=False)
  342. def _compute_inv_freq(self, rescale_factors: List[float]) -> torch.Tensor:
  343. rescale_factors = torch.tensor(rescale_factors, dtype=torch.float32)
  344. inv_freq = 1.0 / (rescale_factors * (self.base**(torch.arange(
  345. 0, self.head_size, 2, dtype=torch.float) / self.head_size)))
  346. return inv_freq
  347. def _compute_cos_sin_cache(self, max_position_embeddings: int,
  348. rescale_factors: List[float],
  349. mscale: float) -> torch.Tensor:
  350. inv_freq = self._compute_inv_freq(rescale_factors)
  351. t = torch.arange(max_position_embeddings, dtype=torch.float)
  352. freqs = torch.einsum("i,j -> ij", t, inv_freq)
  353. cos = freqs.cos() * mscale
  354. sin = freqs.sin() * mscale
  355. cache = torch.cat((cos, sin), dim=-1)
  356. return cache
  357. def forward(
  358. self,
  359. positions: torch.Tensor,
  360. query: torch.Tensor,
  361. key: torch.Tensor,
  362. offsets: Optional[torch.Tensor] = None,
  363. ) -> Tuple[torch.Tensor, torch.Tensor]:
  364. query = query.view(*query.shape[:-1], -1, self.head_size)
  365. key = key.view(*key.shape[:-1], -1, self.head_size)
  366. k = self.original_max_position_embeddings
  367. long_prompt_offset = (torch.any(positions > k).float() *
  368. torch.full_like(positions, k)).long()
  369. idx = (torch.add(positions, long_prompt_offset)
  370. if long_prompt_offset is not None else positions)
  371. self.long_short_cos_sin_cache = self.long_short_cos_sin_cache.to(
  372. idx.device)
  373. idx = torch.add(idx, offsets) if offsets is not None else idx
  374. cos_sin = torch.index_select(self.long_short_cos_sin_cache, 0, idx)
  375. cos, sin = cos_sin.chunk(2, dim=-1)
  376. cos = cos.repeat(1, 2).unsqueeze(-2)
  377. sin = sin.repeat(1, 2).unsqueeze(-2)
  378. query = query * cos + _rotate_neox(query) * sin
  379. key = key * cos + _rotate_neox(key) * sin
  380. return query.flatten(-2), key.flatten(-2)
  381. class ExtendedRotaryEmbedding(RotaryEmbedding):
  382. def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
  383. inv_freqs = super()._compute_inv_freq(base)
  384. return self.apply_scaling(inv_freqs)
  385. def apply_scaling(self, freqs: torch.Tensor):
  386. scale_factor = 8
  387. low_freq_factor = 1
  388. high_freq_factor = 4
  389. old_context_len = 8192
  390. low_freq_wavelen = old_context_len / low_freq_factor
  391. high_freq_wavelen = old_context_len / high_freq_factor
  392. new_freqs = []
  393. for freq in freqs:
  394. wavelen = 2 * math.pi / freq
  395. if wavelen < high_freq_wavelen:
  396. new_freqs.append(freq)
  397. elif wavelen > low_freq_wavelen:
  398. new_freqs.append(freq / scale_factor)
  399. else:
  400. assert low_freq_wavelen != high_freq_wavelen
  401. smooth = (old_context_len / wavelen - low_freq_factor) / (
  402. high_freq_factor - low_freq_factor)
  403. new_freqs.append((1 - smooth) * freq / scale_factor +
  404. smooth * freq)
  405. return torch.tensor(new_freqs, dtype=freqs.dtype, device=freqs.device)
  406. _ROPE_DICT: Dict[Tuple, RotaryEmbedding] = {}
  407. def get_rope(
  408. head_size: int,
  409. rotary_dim: int,
  410. max_position: int,
  411. base: int,
  412. is_neox_style: bool = True,
  413. rope_scaling: Optional[Dict[str, Any]] = None,
  414. ) -> RotaryEmbedding:
  415. if rope_scaling is not None:
  416. rope_scaling_tuple = {
  417. k: tuple(v) if isinstance(v, list) else v
  418. for k, v in rope_scaling.items()
  419. }
  420. rope_scaling_args = tuple(rope_scaling_tuple.items())
  421. else:
  422. rope_scaling_args = None
  423. key = (head_size, rotary_dim, max_position, base, is_neox_style,
  424. rope_scaling_args)
  425. if key in _ROPE_DICT:
  426. return _ROPE_DICT[key]
  427. if rope_scaling is None:
  428. rotary_emb = RotaryEmbedding(head_size, rotary_dim, max_position, base,
  429. is_neox_style)
  430. else:
  431. scaling_type = rope_scaling["type"]
  432. if scaling_type not in {"su", "longrope", "extended"}:
  433. scaling_factor = rope_scaling["factor"]
  434. if scaling_type == "extended":
  435. rotary_emb = ExtendedRotaryEmbedding(head_size, rotary_dim,
  436. max_position, base,
  437. is_neox_style)
  438. elif scaling_type == "linear":
  439. rotary_emb = LinearScalingRotaryEmbedding(head_size, rotary_dim,
  440. max_position, base,
  441. is_neox_style,
  442. scaling_factor)
  443. elif scaling_type == "dynamic":
  444. rotary_emb = DynamicNTKScalingRotaryEmbedding(
  445. head_size, rotary_dim, max_position, base, is_neox_style,
  446. scaling_factor)
  447. elif scaling_type == "yarn":
  448. original_max_position = rope_scaling[
  449. "original_max_position_embeddings"]
  450. extra_kwargs = {
  451. k: v
  452. for k, v in rope_scaling.items()
  453. if k in ("extrapolation_factor", "attn_factor", "beta_fast",
  454. "beta_slow")
  455. }
  456. rotary_emb = YaRNScalingRotaryEmbedding(head_size, rotary_dim,
  457. original_max_position,
  458. base, is_neox_style,
  459. scaling_factor,
  460. **extra_kwargs)
  461. elif scaling_type == "su" or scaling_type == "longrope":
  462. short_factor = rope_scaling["short_factor"]
  463. long_factor = rope_scaling["long_factor"]
  464. original_max_position = rope_scaling[
  465. "original_max_position_embeddings"]
  466. extra_kwargs = {
  467. k: v
  468. for k, v in rope_scaling.items()
  469. if k in ("short_mscale", "long_mscale")
  470. }
  471. rotary_emb = Phi3LongRoPERotaryEmbedding(
  472. head_size, rotary_dim, max_position, original_max_position,
  473. base, is_neox_style, short_factor, long_factor, **extra_kwargs)
  474. else:
  475. raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
  476. _ROPE_DICT[key] = rotary_emb
  477. return rotary_emb