Instructions to use handshake-ai-research/cue with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use handshake-ai-research/cue with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="handshake-ai-research/cue", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("handshake-ai-research/cue", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 907 Bytes
d9d4242 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | """Layer-wise attention pooling over transformer hidden states."""
from __future__ import annotations
import torch
import torch.nn as nn
class LayerwiseAttention(nn.Module):
def __init__(self, num_layers: int) -> None:
super().__init__()
self.num_layers = num_layers
self.weights = nn.Parameter(torch.zeros(num_layers))
self.gamma = nn.Parameter(torch.ones(1))
def forward(
self, hidden_states: tuple[torch.Tensor, ...] | list[torch.Tensor]
) -> torch.Tensor:
if len(hidden_states) != self.num_layers:
raise ValueError(
f"expected {self.num_layers} layers, got {len(hidden_states)}"
)
w = torch.softmax(self.weights, dim=0)
stacked = torch.stack(list(hidden_states), dim=0) # [L,B,T,H]
combined = (w.view(-1, 1, 1, 1) * stacked).sum(dim=0)
return self.gamma * combined
|