Download src/pooling.py from zaaabik/paper_extraction: direct link, hf CLI and curl.
- Browser
- Download file 893 Bytes
-
https://huggingface.co/datasets/zaaabik/paper_extraction/resolve/main/src/pooling.py
- Command line
-
hf download hf://datasets/zaaabik/paper_extraction/src/pooling.py
-
curl -L -o pooling.py https://huggingface.co/datasets/zaaabik/paper_extraction/resolve/main/src/pooling.py
893 Bytes
| import torch | |
| class LastTokenPooling: | |
| def __init__(self, layer_number: int = -1): | |
| self.layer_number = layer_number | |
| def __call__(self, hidden_states, input_ids, model): | |
| hs = hidden_states[self.layer_number] | |
| bs = input_ids.shape[0] | |
| non_pad_mask = (input_ids != model.config.pad_token_id).to(device=hs.device, dtype=torch.int32) | |
| token_indices = torch.arange(input_ids.shape[-1], device=hs.device, dtype=torch.int32) | |
| last_non_pad_token = (token_indices * non_pad_mask).argmax(-1) | |
| pooled_logits = hs[torch.arange(bs, device=hs.device), last_non_pad_token] | |
| return pooled_logits | |
| class CLSTokenPooling: | |
| def __init__(self, layer_number: int = -1): | |
| self.layer_number = layer_number | |
| def __call__(self, hidden_states, input_ids, model): | |
| hs = hidden_states[self.layer_number] | |
| return hs[:, 0, :] | |