mirror of
https://github.com/ml-explore/mlx.git
synced 2025-10-19 08:38:09 +08:00
RoPE with frequencies as optional input (#1337)
* start rope with freq input * rope with frequencies * nits * fix bug * fix bug + test * cleanup * optional base
This commit is contained in:
@@ -7,13 +7,18 @@ import mlx.core as mx
|
||||
import mlx_tests
|
||||
|
||||
|
||||
def rope_orig(x, dims, traditional, base, scale, offset):
|
||||
def rope_orig(x, dims, traditional, base, scale, offset, freqs=None):
|
||||
N = x.shape[1] + offset
|
||||
dtype = x.dtype
|
||||
half_D = dims // 2
|
||||
positions = mx.arange(offset, N, dtype=dtype) * scale
|
||||
freqs = mx.exp(-mx.arange(0.0, half_D, dtype=dtype) * (math.log(base) / half_D))
|
||||
theta = mx.reshape(positions, (-1, 1)) * mx.reshape(freqs, (1, -1))
|
||||
if freqs is None:
|
||||
inv_freqs = mx.exp(
|
||||
-mx.arange(0.0, half_D, dtype=dtype) * (math.log(base) / half_D)
|
||||
)
|
||||
else:
|
||||
inv_freqs = 1 / freqs
|
||||
theta = mx.reshape(positions, (-1, 1)) * mx.reshape(inv_freqs, (1, -1))
|
||||
costheta, sintheta = mx.cos(theta), mx.sin(theta)
|
||||
if traditional:
|
||||
x1 = x[..., :dims:2]
|
||||
@@ -138,6 +143,84 @@ class TestFast(mlx_tests.MLXTestCase):
|
||||
)
|
||||
self.assertLess(mx.abs(rx - rx_fast).max(), tolerances[dtype])
|
||||
|
||||
def test_rope_with_freqs(self):
|
||||
# Check throws
|
||||
T = 4
|
||||
dims = 8
|
||||
x = mx.random.uniform(shape=(2, T, dims))
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
freqs = mx.random.uniform(shape=(dims - 1,))
|
||||
mx.fast.rope(
|
||||
x,
|
||||
dims,
|
||||
traditional=False,
|
||||
base=None,
|
||||
scale=1.0,
|
||||
offset=0,
|
||||
freqs=freqs,
|
||||
)
|
||||
with self.assertRaises(ValueError):
|
||||
freqs = mx.random.uniform(shape=(1, dims))
|
||||
mx.fast.rope(
|
||||
x,
|
||||
dims,
|
||||
traditional=False,
|
||||
base=None,
|
||||
scale=1.0,
|
||||
offset=0,
|
||||
freqs=freqs,
|
||||
)
|
||||
|
||||
freqs = mx.random.uniform(shape=(dims // 2,))
|
||||
|
||||
rx = rope_orig(x, dims, False, None, 1.0, 0, freqs)
|
||||
rx_fast = mx.fast.rope(
|
||||
x,
|
||||
dims,
|
||||
traditional=False,
|
||||
base=None,
|
||||
scale=1.0,
|
||||
offset=0,
|
||||
freqs=freqs,
|
||||
)
|
||||
self.assertLess(mx.abs(rx - rx_fast).max(), 1e-5)
|
||||
|
||||
# Test single vector
|
||||
x = mx.random.uniform(shape=(1, 1, dims))
|
||||
rx = rope_orig(x, dims, False, None, 1.0, 0, freqs)
|
||||
rx_fast = mx.fast.rope(
|
||||
x,
|
||||
dims,
|
||||
traditional=False,
|
||||
base=None,
|
||||
scale=1.0,
|
||||
offset=0,
|
||||
freqs=freqs,
|
||||
)
|
||||
self.assertLess(mx.abs(rx - rx_fast).max(), 1e-5)
|
||||
|
||||
# Test grad with freqs
|
||||
f1 = lambda x, y: (rope_orig(x, dims, False, None, 1.0, 0, freqs) * y).sum()
|
||||
f2 = lambda x, y: (
|
||||
mx.fast.rope(
|
||||
x,
|
||||
dims,
|
||||
traditional=False,
|
||||
base=None,
|
||||
scale=1.0,
|
||||
offset=0,
|
||||
freqs=freqs,
|
||||
)
|
||||
* y
|
||||
).sum()
|
||||
|
||||
x = mx.random.uniform(shape=(2, 4, dims))
|
||||
y = mx.random.uniform(shape=(2, 4, dims))
|
||||
g1 = mx.grad(f1)(x, y)
|
||||
g2 = mx.grad(f2)(x, y)
|
||||
self.assertLess(mx.abs(g1 - g2).max(), 1e-5)
|
||||
|
||||
def test_rope_grad(self):
|
||||
D = 32
|
||||
defaults = (D, 10000.0, 1.0, 0, False)
|
||||
|
Reference in New Issue
Block a user