Fill-Mask
Transformers
PyTorch
English
bert
splade
query-expansion
document-expansion
bag-of-words
passage-retrieval
knowledge-distillation
document encoder
Instructions to use marmalade/efficient-splade-VI-BT-large-query with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use marmalade/efficient-splade-VI-BT-large-query with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="marmalade/efficient-splade-VI-BT-large-query")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("marmalade/efficient-splade-VI-BT-large-query") model = AutoModelForMaskedLM.from_pretrained("marmalade/efficient-splade-VI-BT-large-query", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from marmalade/efficient-splade-VI-BT-large-query: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/marmalade/efficient-splade-VI-BT-large-query/resolve/main/handler.py
- Command line
-
hf download hf://marmalade/efficient-splade-VI-BT-large-query/handler.py
-
curl -L -o handler.py https://huggingface.co/marmalade/efficient-splade-VI-BT-large-query/resolve/main/handler.py
1.58 kB
| from typing import Any, List, Dict | |
| from pathlib import Path | |
| import torch | |
| from transformers import AutoModelForMaskedLM, AutoTokenizer | |
| class EndpointHandler(): | |
| def __init__(self, path="."): | |
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| self.tokenizer = AutoTokenizer.from_pretrained(path) | |
| self.model = AutoModelForMaskedLM.from_pretrained(path).to(self.device) | |
| def __call__(self, data: Any) -> List[List[Dict[str, float]]]: | |
| """ | |
| Args: | |
| data (:obj:): | |
| includes the input data and the parameters for the inference. | |
| Return: | |
| A :obj:`list`:. The list contains the embeddings of the inference inputs | |
| """ | |
| inputs = data.get("inputs", data) | |
| with torch.no_grad(): | |
| tokens = self.tokenizer( | |
| inputs, padding=True, truncation=True, return_tensors='pt' | |
| ).to(self.device) | |
| outputs = self.model(**tokens) | |
| vecs = torch.max( | |
| torch.log( | |
| 1 + torch.relu(outputs.logits) | |
| ) * tokens.attention_mask.unsqueeze(-1), | |
| dim=1 | |
| )[0] | |
| embeds = [] | |
| for vec in vecs: | |
| # extract non-zero positions | |
| cols = vec.nonzero().squeeze().cpu().tolist() | |
| # extract the non-zero values | |
| weights = vec[cols].cpu().tolist() | |
| sparse = { | |
| "indices": cols, | |
| "values": weights, | |
| } | |
| embeds.append(sparse) | |
| return embeds |