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 option for QKV clipping #489

Merged
merged 2 commits into from
Mar 7, 2024
Merged
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
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added MMLU downstream evaluation tasks, with prompt variations.
- Added support for PyTorch v2.2.
- Added ability to show logs from all ranks


- Added option for QKV clipping.

### Changed

Expand Down
5 changes: 5 additions & 0 deletions olmo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ class ModelConfig(BaseConfig):
The number of self-attention heads.
"""

clip_qkv: Optional[float] = None
"""
Clip QKV to this value when set.
"""

n_layers: int = 12
"""
The number of layers/blocks.
Expand Down
21 changes: 17 additions & 4 deletions olmo/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,14 @@ def forward(
# - for multi-query attn q: (batch_size, seq_len, d_model)
# k, v: (batch_size, seq_len, d_model // n_heads)
if self._activation_checkpoint_fn is not None:
q, k, v = self.att_proj(self._activation_checkpoint_fn(self.attn_norm, x)).split(
self.fused_dims, dim=-1
)
qkv = self.att_proj(self._activation_checkpoint_fn(self.attn_norm, x))
else:
q, k, v = self.att_proj(self.attn_norm(x)).split(self.fused_dims, dim=-1)
qkv = self.att_proj(self.attn_norm(x))

if self.config.clip_qkv is not None:
qkv.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)

q, k, v = qkv.split(self.fused_dims, dim=-1)

# Get attention scores.
if self._activation_checkpoint_fn is not None:
Expand Down Expand Up @@ -780,6 +783,11 @@ def forward(
else:
q, k, v, ff = self.fused_attn_ff_proj(self.norm(x)).split(self.fused_dims, dim=-1)

if self.config.clip_qkv is not None:
q.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)
k.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)
v.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)

# Get attention scores.
# shape: (B, T, C)
if self._activation_checkpoint_fn is not None:
Expand Down Expand Up @@ -896,6 +904,11 @@ def forward(
k = self.k_proj(x_normed)
v = self.v_proj(x_normed)

if self.config.clip_qkv is not None:
q.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)
k.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)
v.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)

# Get attention scores.
att, cache = self.attention(q, k, v, attention_bias, layer_past=layer_past, use_cache=use_cache)

Expand Down
Loading