-
Notifications
You must be signed in to change notification settings - Fork 126
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
Ring attention #181
base: main
Are you sure you want to change the base?
Ring attention #181
Conversation
|
||
## Copy from transformers. Non interleaved version of RoPE. Will be refactored later | ||
def rotate_half(x): | ||
"""Rotates half the hidden dims of the input.""" | ||
x1 = x[..., : x.shape[-1] // 2] | ||
x2 = x[..., x.shape[-1] // 2 :] | ||
return torch.cat((-x2, x1), dim=-1) | ||
|
||
|
||
class LlamaRotaryEmbedding(nn.Module): | ||
def __init__(self, dim: int, end: int, theta: float = 500000.0): | ||
super().__init__() | ||
self.dim = dim | ||
self.end = end | ||
self.theta = theta | ||
self.init_rotary_embeddings() | ||
|
||
def init_rotary_embeddings(self): | ||
inv_freq = 1.0 / (self.theta ** (torch.arange(0, self.dim, 2, dtype=torch.float, device="cuda") / self.dim)) | ||
self.register_buffer("inv_freq", inv_freq, persistent=False) | ||
|
||
@torch.no_grad() | ||
def forward( | ||
self, | ||
x: torch.Tensor, # [batch_size, seq_length, num_heads, d_qk] | ||
position_ids: Optional[torch.LongTensor], # [batch_size, seq_length] | ||
): | ||
# x: [bs, num_attention_heads, seq_len, head_size] | ||
# print("rotary") | ||
inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) | ||
position_ids_expanded = position_ids[:, None, :].float() | ||
# Force float32 since bfloat16 loses precision on long contexts | ||
# See https://github.com/huggingface/transformers/pull/29285 | ||
device_type = x.device.type | ||
device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu" | ||
with torch.autocast(device_type=device_type, enabled=False): | ||
freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) | ||
emb = torch.cat((freqs, freqs), dim=-1) | ||
cos = emb.cos() | ||
sin = emb.sin() | ||
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype) | ||
|
||
|
||
def apply_rotary_pos_emb(q, k, cos, sin, unsqueeze_dim=2): | ||
"""Applies Rotary Position Embedding to the query and key tensors. | ||
|
||
Args: | ||
q (`torch.Tensor`): The query tensor. | ||
k (`torch.Tensor`): The key tensor. | ||
cos (`torch.Tensor`): The cosine part of the rotary embedding. | ||
sin (`torch.Tensor`): The sine part of the rotary embedding. | ||
unsqueeze_dim (`int`, *optional*, defaults to 1): | ||
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and | ||
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note | ||
that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and | ||
k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes | ||
cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have | ||
the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. | ||
Returns: | ||
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. | ||
""" | ||
cos = cos.unsqueeze(unsqueeze_dim) | ||
sin = sin.unsqueeze(unsqueeze_dim) | ||
q_embed = (q * cos) + (rotate_half(q) * sin) | ||
k_embed = (k * cos) + (rotate_half(k) * sin) | ||
return q_embed, k_embed | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we open a separate PR first to replace current RotaryEmbedding
init_distributed(tp=tp, dp=dp, pp=pp)(_test_save_zero_optimizer_and_load_optimizer)(test_context=test_context) | ||
# Currently SP doesn't support zero. | ||
if sp != 1: | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print with message
# We use DP=2 as we're interested in testing that one | ||
init_distributed(tp=tp, dp=dp, pp=pp)(_test_save_zero_optimizer_and_load_data_parallel_optimizer)( | ||
if sp != 1: | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print with message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve merge conflicts!
Ring attention for training on long sequences. Similar to Megatron context parallel. Idea from https://github.com/zhuzilin/ring-flash-attention