Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update
  • Loading branch information
yanboliang committed May 9, 2024
commit 1ba129af0b7a91cadc236772416b92123e0eda2e
33 changes: 20 additions & 13 deletions mixtral-moe/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def __init__(self, config: ModelArgs) -> None:

self.tok_embeddings = nn.Embedding(config.vocab_size, config.dim)
self.layers = nn.ModuleList(TransformerBlock(config) for _ in range(config.n_layer))
self.norm = RMSNorm(config.dim, eps=config.norm_eps)
if is_dbrx(config):
self.norm = nn.LayerNorm(config.dim, eps=config.norm_eps, bias=False)
else:
self.norm = RMSNorm(config.dim, eps=config.norm_eps)
self.output = nn.Linear(config.dim, config.vocab_size, bias=False)

self.freqs_cis: Optional[Tensor] = None
Expand Down Expand Up @@ -261,16 +264,20 @@ def precompute_freqs_cis(
return cache.to(dtype=torch.bfloat16)


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)


def apply_rotary_emb(x: Tensor, freqs_cis: Tensor) -> Tensor:
xshaped = x.float().reshape(*x.shape[:-1], -1, 2)
freqs_cis = freqs_cis.view(1, xshaped.size(1), 1, xshaped.size(3), 2)
x_out2 = torch.stack(
[
xshaped[..., 0] * freqs_cis[..., 0] - xshaped[..., 1] * freqs_cis[..., 1],
xshaped[..., 1] * freqs_cis[..., 0] + xshaped[..., 0] * freqs_cis[..., 1],
],
-1,
)

x_out2 = x_out2.flatten(3)
return x_out2.type_as(x)
fc_shape = freqs_cis.shape
freqs_cis = freqs_cis.view(1, fc_shape[0], 1, fc_shape[1], fc_shape[2])
cos, sin = freqs_cis.split([1, 1], dim=-1)
cos = cos.squeeze(-1)
sin = sin.squeeze(-1)
cos = torch.cat((cos, cos), dim=-1)
sin = torch.cat((sin, sin), dim=-1)
z = (x * cos) + (rotate_half(x)) * sin
return z
4 changes: 2 additions & 2 deletions mixtral-moe/scripts/convert_hf_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def _convert_dbrx(
model_name = checkpoint_dir.name

if "Mixtral-8x7B" in model_name:
return _convert_mixtral(checkpoint_dir, model_name)
_convert_mixtral(checkpoint_dir=checkpoint_dir, model_name=model_name)
else:
assert "dbrx" in model_name, f"Unknown model name {model_name}"
return _convert_dbrx(checkpoint_dir, model_name)
_convert_dbrx(checkpoint_dir=checkpoint_dir, model_name=model_name)