Elffuss BraidHub

kikocis/mlx-vlm public

main · 3.3 KiBRawPermalink
1import math
2
3import mlx.core as mx
4
5
6class Phi3SuScaledRotaryEmbedding:
7 def __init__(
8 self,
9 dims: int,
10 traditional: bool = False,
11 base: float = 10000.0,
12 scale: float = 1.0,
13 max_position_embeddings: int = 131072,
14 original_max_position_embeddings: int = 4096,
15 short_factor: list[float] | float = 1.0,
16 long_factor: list[float] | float = 1.0,
17 ):
18 """
19 Phi3Su Scaled Rotary Embedding layer for Phi-3 models.
20
21 Args:
22 dims (int): The feature dimensions to be rotated.
23 traditional (bool, optional): Unused. Default: ``False``.
24 base (int, optional): Base for the exponential scaling.
25 scale (float, optional): The scale used to scale the positions. Default: 1.0.
26 max_position_embeddings (int, optional): The maximum sequence length that this model was trained with. This is used to determine the size of the original RoPE embeddings when using long scaling. Default: 131072.
27 original_max_position_embeddings (int, optional): The maximum sequence length that this model was trained with. This is used to determine the size of the original RoPE embeddings when using long scaling. Default: 4096.
28 short_factor (float or list of floats, optional): List of scaling factors for sequences of length lesser than original_max_position_embeddings. Default: 1.0.
29 long_factor (float or list of floats, optional): List of scaling factors for sequences of length greater than original_max_position_embeddings. Default: 1.0.
30 """
31 self.inv_freq_short = 1.0 / (
32 mx.array(short_factor, dtype=mx.float32)
33 * base ** (mx.arange(0, dims, 2, dtype=mx.float32) / dims)
34 )
35 self.inv_freq_long = 1.0 / (
36 scale
37 * mx.array(long_factor, dtype=mx.float32)
38 * base ** (mx.arange(0, dims, 2, dtype=mx.float32) / dims)
39 )
40 self.original_max_position_embeddings = original_max_position_embeddings
41 self.scaling_factor = math.sqrt(
42 1
43 + math.log(max_position_embeddings / original_max_position_embeddings)
44 / math.log(original_max_position_embeddings)
45 )
46
47 def _get_cos_sin(self, offset, L):
48 position_ids = mx.arange(offset, offset + L, dtype=mx.float32)[None]
49 inv_freq = (
50 self.inv_freq_long
51 if position_ids.max() + 1 > self.original_max_position_embeddings
52 else self.inv_freq_short
53 )
54 inv_freq_expanded = mx.repeat(
55 inv_freq[None, :, None], position_ids.shape[0], axis=0
56 )
57 position_ids_expanded = position_ids[:, None, :]
58 freqs = (inv_freq_expanded @ position_ids_expanded).transpose(0, 2, 1)
59 emb = mx.concatenate([freqs, freqs], axis=-1)
60 cos = mx.cos(emb) * self.scaling_factor
61 sin = mx.sin(emb) * self.scaling_factor
62 return mx.expand_dims(cos, axis=1), mx.expand_dims(sin, axis=1)
63
64 def __call__(self, x, offset: int = 0):
65 def _rotate_half(_x):
66 midpoint = _x.shape[-1] // 2
67 x1, x2 = _x[..., :midpoint], _x[..., midpoint:]
68 return mx.concatenate([-x2, x1], axis=-1)
69
70 cos, sin = self._get_cos_sin(offset, x.shape[2])
71 return (x * cos) + (_rotate_half(x) * sin)