Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on-paper form of RoPE kernel #465

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Comet0322
Copy link
Contributor

Summary

Implement the on-paper form of the RoPE kernel from RoFormer
This implementation does not support optional value input, unlike the HuggingFace RoFormer RoPE implementation.

Details

The code is adapted from Liger Kernel's RoPE implementation.
In the current Liger Kernel's RoPE implementation, the head is divided into left and right parts for computation:

$y = [x1, x2] * [cos, cos] + [-x2, x1] * [sin, sin]$
$dy = [dx1, dx2] * [cos, cos] + [-dx2, dx1] * [-sin, -sin]$

Corresponds to the vector-vector multiplication-addition form:

$$\begin{pmatrix} q_0\\\ q_1\\\ q_2\\\ \vdots\\\ q_{d/2-1}\\\ q_{d/2}\\\ q_{d/2+1}\\\ q_{d/2+2}\\\ \vdots\\\ q_{d-1} \end{pmatrix} \otimes \begin{pmatrix} cos\ m\theta_0 \\\ cos\ m\theta_1 \\\ cos\ m\theta_2 \\\ \vdots\\\ cos\ m\theta_{d/2-1} \\\ cos\ m\theta_0 \\\ cos\ m\theta_1 \\\ cos\ m\theta_2 \\\ \vdots\\\ cos\ m\theta_{d/2-1} \end{pmatrix} + \begin{pmatrix} -q_{d/2}\\\ -q_{d/2+1}\\\ -q_{d/2+2}\\\ \vdots\\\ -q_{d-1}\\\ q_0\\\ q_1\\\ q_2\\\ \vdots\\\ q_{d/2-1} \end{pmatrix} \otimes \begin{pmatrix} sin\ m\theta_0 \\\ sin\ m\theta_1 \\\ sin\ m\theta_2 \\\ \vdots\\\ sin\ m\theta_{d/2-1} \\\ sin\ m\theta_0 \\\ sin\ m\theta_1 \\\ sin\ m\theta_2 \\\ \vdots\\\ sin\ m\theta_{d/2-1} \end{pmatrix}$$

To obtain the on-paper form of RoPE, the head is instead divided into even-indexed and odd-indexed parts for computation:
$y_{even} = x_{even} * cos - x_{odd} * sin$
$y_{odd} = x_{odd} * cos + x_{even} * sin$

$dy_{even} = dx_{even} * cos + dx_{odd} * sin$
$dy_{odd} = dx_{odd} * cos - dx_{even} * sin$

Corresponds to the vector-vector multiplication-addition form:

$$\begin{pmatrix} q_0\\\ q_1\\\ q_2\\\ q_3\\\ \vdots\\\ q_{d-2}\\\ q_{d-1} \end{pmatrix} \otimes \begin{pmatrix} cos\ m\theta_0 \\\ cos\ m\theta_0 \\\ cos\ m\theta_1 \\\ cos\ m\theta_1 \\\ \vdots\\\ cos\ m\theta_{d/2-1} \\\ cos\ m\theta_{d/2-1} \end{pmatrix} + \begin{pmatrix} -q_1\\\ q_0\\\ -q_3\\\ q_2\\\ \vdots\\\ -q_{d-1}\\\ q_{d-2} \end{pmatrix} \otimes \begin{pmatrix} sin\ m\theta_0 \\\ sin\ m\theta_0 \\\ sin\ m\theta_1 \\\ sin\ m\theta_1 \\\ \vdots\\\ sin\ m\theta_{d/2-1} \\\ sin\ m\theta_{d/2-1} \end{pmatrix}$$

Testing Done

rope_paper_memory
rope_paper_speed

  • Hardware Type: A100-80G-PCIe
  • run make test to ensure correctness
  • run make checkstyle to ensure code style
  • run make test-convergence to ensure convergence

@Comet0322
Copy link
Contributor Author

I've added a paper-form option to the current Liger Kernel RoPE implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants