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
Download encoder_layer_pool.py from handshake-ai-research/cue: direct link, hf CLI and curl.
- Browser
- Download file 907 Bytes
-
https://huggingface.co/handshake-ai-research/cue/resolve/main/encoder_layer_pool.py
- Command line
-
hf download hf://handshake-ai-research/cue/encoder_layer_pool.py
-
curl -L -o encoder_layer_pool.py https://huggingface.co/handshake-ai-research/cue/resolve/main/encoder_layer_pool.py
907 Bytes
| """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 | |