-
Notifications
You must be signed in to change notification settings - Fork 290
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
Feat: Add jasper #1591
Merged
Merged
Feat: Add jasper #1591
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ebe87d5
init jasper
Samoed 0a70486
init jasper
Samoed 337ceca
add to overview
Samoed 016ee5b
add to overview
Samoed f54c281
remove some params
Samoed 045b2d6
fix max length
Samoed 22a958e
return sdpa
Samoed ad94219
add dtype
Samoed 26a2943
add dtype
Samoed 9ba4553
fix convert_to_tensor
Samoed bba7024
change to encode
Samoed 7cabbeb
return whitespace processing
Samoed 5db50a5
explicitly add instructions
Samoed a52cf40
move seq length
Samoed 1d7b250
try float
Samoed d54ba1b
fix max_seq_length
Samoed a2575e6
add prompt validation to format instruction
Samoed 011b791
don't use instructions only to s2p
Samoed 8dee219
Merge branch 'refs/heads/main' into add_jasper
Samoed f8ccd76
Merge branch 'main' into add_jasper
Samoed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from collections.abc import Sequence | ||
from functools import partial | ||
from typing import Any, Callable | ||
|
||
import numpy as np | ||
import torch | ||
from sentence_transformers import SentenceTransformer | ||
|
||
import mteb | ||
from mteb.encoder_interface import PromptType | ||
from mteb.model_meta import ModelMeta | ||
|
||
from .wrapper import Wrapper | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class JasperWrapper(Wrapper): | ||
def __init__( | ||
self, | ||
model_name: str, | ||
revision: str, | ||
instruction_template: str | Callable[[str], str] | None = None, | ||
max_seq_length: int = 2048, | ||
**kwargs: Any, | ||
): | ||
self.model_name = model_name | ||
self.model = SentenceTransformer(model_name, revision=revision, **kwargs) | ||
self.instruction_template = instruction_template | ||
self.model.max_seq_length = max_seq_length | ||
|
||
def encode( | ||
self, | ||
sentences: Sequence[str], | ||
*, | ||
task_name: str, | ||
prompt_type: PromptType | None = None, | ||
**kwargs: Any, | ||
) -> np.ndarray: | ||
task = mteb.get_task(task_name=task_name) | ||
instruction = self.get_task_instruction(task_name, prompt_type) | ||
|
||
# to passage prompts won't be applied to passages | ||
if prompt_type == PromptType.passage and task.metadata.type == "s2p": | ||
instruction = None | ||
|
||
embeddings = self.model.encode( | ||
sentences, | ||
normalize_embeddings=True, | ||
prompt=instruction, | ||
**kwargs, | ||
) | ||
|
||
if isinstance(embeddings, torch.Tensor): | ||
# sometimes in kwargs can be return_tensors=True | ||
embeddings = embeddings.cpu().detach().float().numpy() | ||
return embeddings | ||
|
||
|
||
jasper_en_v1 = ModelMeta( | ||
loader=partial( # type: ignore | ||
JasperWrapper, | ||
model_name="infgrad/jasper_en_vision_language_v1", | ||
revision="d6330ce98f8a0d741e781df845904c9484f00efa", | ||
config_kwargs={"is_text_encoder": True, "vector_dim": 12288}, | ||
model_kwargs={ | ||
"attn_implementation": "sdpa", | ||
"torch_dtype": torch.float16, | ||
}, | ||
trust_remote_code=True, | ||
max_seq_length=2048, | ||
instruction_template="Instruct: {instruction}\nQuery: ", | ||
), | ||
name="infgrad/jasper_en_vision_language_v1", | ||
languages=["eng-Latn"], | ||
open_weights=True, | ||
revision="d6330ce98f8a0d741e781df845904c9484f00efa", | ||
release_date="2024-12-11", # first commit | ||
n_parameters=1_999_000_000, | ||
memory_usage=None, | ||
max_tokens=131072, | ||
embed_dim=8960, | ||
license="apache-2.0", | ||
reference="https://huggingface.co/infgrad/jasper_en_vision_language_v1/tree/main", | ||
similarity_fn_name="cosine", | ||
framework=["Sentence Transformers", "PyTorch"], | ||
use_instructions=True, | ||
adapted_from=None, | ||
superseded_by=None, | ||
training_datasets={ | ||
"non_mteb": ["BAAI/Infinity-MM", "HuggingFaceFW/fineweb-edu"], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated it to apply the passage prompt only if the task type is
s2s
orp2p
.