Skip to content

[Model] Add support for normalized Transformer (nGPT) from NVIDIA #18798

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/models/supported_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ Specified using `--task generate`.
| `MixtralForCausalLM` | Mixtral-8x7B, Mixtral-8x7B-Instruct | `mistralai/Mixtral-8x7B-v0.1`, `mistralai/Mixtral-8x7B-Instruct-v0.1`, `mistral-community/Mixtral-8x22B-v0.1`, etc. | ✅︎ | ✅︎ |
| `MPTForCausalLM` | MPT, MPT-Instruct, MPT-Chat, MPT-StoryWriter | `mosaicml/mpt-7b`, `mosaicml/mpt-7b-storywriter`, `mosaicml/mpt-30b`, etc. | ✅︎ | |
| `NemotronForCausalLM` | Nemotron-3, Nemotron-4, Minitron | `nvidia/Minitron-8B-Base`, `mgoin/Nemotron-4-340B-Base-hf-FP8`, etc. | ✅︎ | ✅︎ |
| `NGPTForCausalLM` | Normalized-Nemotron | `nvidia/Normalized-Nemotron-8B-Reasoning`, etc. | ✅︎ | ✅︎ |
| `OLMoForCausalLM` | OLMo | `allenai/OLMo-1B-hf`, `allenai/OLMo-7B-hf`, etc. | ✅︎ | |
| `OLMo2ForCausalLM` | OLMo2 | `allenai/OLMo-2-0425-1B`, etc. | ✅︎ | |
| `OLMoEForCausalLM` | OLMoE | `allenai/OLMoE-1B-7B-0924`, `allenai/OLMoE-1B-7B-0924-Instruct`, etc. | ✅︎ | ✅︎ |
Expand Down
1 change: 1 addition & 0 deletions tests/models/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
"MptForCausalLM": _HfExamplesInfo("mpt", is_available_online=False),
"MPTForCausalLM": _HfExamplesInfo("mosaicml/mpt-7b"),
"NemotronForCausalLM": _HfExamplesInfo("nvidia/Minitron-8B-Base"),
"NGPTForCausalLM": _HfExamplesInfo("nvidia/Normalized-Nemotron-8B-Reasoning"),

Check failure on line 221 in tests/models/registry.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (E501)

tests/models/registry.py:221:81: E501 Line too long (82 > 80)
"OlmoForCausalLM": _HfExamplesInfo("allenai/OLMo-1B-hf"),
"Olmo2ForCausalLM": _HfExamplesInfo("allenai/OLMo-2-0425-1B"),
"OlmoeForCausalLM": _HfExamplesInfo("allenai/OLMoE-1B-7B-0924-Instruct"),
Expand Down
54 changes: 53 additions & 1 deletion vllm/model_executor/layers/rotary_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# limitations under the License.
"""Rotary Positional Embeddings."""
import math
from typing import Any, Optional, Union
from typing import Any, List, Optional, Union

Check failure on line 26 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (UP035)

vllm/model_executor/layers/rotary_embedding.py:26:1: UP035 `typing.List` is deprecated, use `list` instead

import torch
import torch.nn as nn
Expand Down Expand Up @@ -1698,6 +1698,47 @@
return s


class CustomWavelengthsRotaryEmbedding(RotaryEmbedding):
"""RotaryEmbedding extended with custom rope wavelengths."""

def __init__(
self,
head_size: int,
rotary_dim: int,
max_position_embeddings: int,
base: int,
is_neox_style: bool,
dtype: torch.dtype,
wavelengths: List[Union[float, str]],

Check failure on line 1712 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (UP006)

vllm/model_executor/layers/rotary_embedding.py:1712:22: UP006 Use `list` instead of `List` for type annotation
) -> None:
self.wavelengths = torch.tensor([float(w) for w in wavelengths],
dtype=torch.float32)
super().__init__(
head_size,
rotary_dim,
max_position_embeddings,
base,
is_neox_style,
dtype,
)

def _compute_inv_freq(self) -> torch.Tensor:

Check failure on line 1725 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Signature of "_compute_inv_freq" incompatible with supertype "RotaryEmbedding" [override]

Check failure on line 1725 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Signature of "_compute_inv_freq" incompatible with supertype "RotaryEmbedding" [override]

Check failure on line 1725 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Signature of "_compute_inv_freq" incompatible with supertype "RotaryEmbedding" [override]

Check failure on line 1725 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Signature of "_compute_inv_freq" incompatible with supertype "RotaryEmbedding" [override]

Check failure on line 1725 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Signature of "_compute_inv_freq" incompatible with supertype "RotaryEmbedding" [override]

Check failure on line 1725 in vllm/model_executor/layers/rotary_embedding.py

View workflow job for this annotation

GitHub Actions / pre-commit

Signature of "_compute_inv_freq" incompatible with supertype "RotaryEmbedding" [override]
"""Compute the inverse frequency."""
inv_freq = 2 * torch.pi / self.wavelengths
return inv_freq

def _compute_cos_sin_cache(self) -> torch.Tensor:
"""Compute the cos and sin cache."""
inv_freq = self._compute_inv_freq()
t = torch.arange(self.max_position_embeddings, dtype=torch.float)

freqs = torch.einsum("i,j -> ij", t, inv_freq)
cos = freqs.cos()
sin = freqs.sin()
cache = torch.cat((cos, sin), dim=-1)
return cache


_ROPE_DICT: dict[tuple, RotaryEmbedding] = {}


Expand Down Expand Up @@ -1855,6 +1896,17 @@
head_size, rotary_dim, max_position, original_max_position,
base, is_neox_style, dtype, short_factor, long_factor,
**extra_kwargs)
elif scaling_type == "custom_wavelengths":
wavelengths = rope_scaling["wavelengths"]
rotary_emb = CustomWavelengthsRotaryEmbedding(
head_size,
rotary_dim,
max_position,
base,
is_neox_style,
dtype,
wavelengths,
)
else:
raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
_ROPE_DICT[key] = rotary_emb
Expand Down
Loading