Feature Extraction
Transformers
ONNX
Safetensors
multilingual
bidirectional_pplx_qwen3
sentence-similarity
conteb
contextual-embeddings
custom_code
text-embeddings-inference
Instructions to use perplexity-ai/pplx-embed-context-v1-4b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use perplexity-ai/pplx-embed-context-v1-4b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="perplexity-ai/pplx-embed-context-v1-4b", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("perplexity-ai/pplx-embed-context-v1-4b", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
Fix transformers 5.9+ compatibility: create_causal_mask kwargs
#6
by maximilian-schall-ppx - opened
- modeling.py +21 -14
modeling.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from typing import Callable, Literal
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
|
@@ -11,6 +12,14 @@ from .configuration import PPLXQwen3Config
|
|
| 11 |
from transformers import AutoTokenizer
|
| 12 |
from .st_quantize import FlexibleQuantizer
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# From modeling_t5gemma.py
|
| 16 |
def bidirectional_mask_function(attention_mask: torch.Tensor | None) -> Callable:
|
|
@@ -57,21 +66,19 @@ class PPLXQwen3Model(Qwen3Model):
|
|
| 57 |
inputs_embeds = self.embed_tokens(input_ids)
|
| 58 |
input_ids = None
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"
|
| 66 |
-
|
| 67 |
-
input_embeds=inputs_embeds,
|
| 68 |
-
attention_mask=attention_mask,
|
| 69 |
-
cache_position=dummy_cache_position,
|
| 70 |
-
past_key_values=None,
|
| 71 |
-
position_ids=position_ids,
|
| 72 |
-
or_mask_function=bidirectional_mask_function(attention_mask),
|
| 73 |
-
)
|
| 74 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
outputs = super().forward(
|
| 77 |
input_ids=input_ids,
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
from typing import Callable, Literal
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
|
|
|
| 12 |
from transformers import AutoTokenizer
|
| 13 |
from .st_quantize import FlexibleQuantizer
|
| 14 |
|
| 15 |
+
# The transformers `create_causal_mask` signature has shifted over releases
|
| 16 |
+
# (the embeds kwarg was renamed `input_embeds` -> `inputs_embeds`, and
|
| 17 |
+
# `cache_position` was eventually dropped). Probe the actual signature at import
|
| 18 |
+
# time so this works on any installed release, including dev/main builds.
|
| 19 |
+
_CCM_PARAMS = inspect.signature(create_causal_mask).parameters
|
| 20 |
+
_CCM_EMBEDS_KEY = "inputs_embeds" if "inputs_embeds" in _CCM_PARAMS else "input_embeds"
|
| 21 |
+
_CCM_ACCEPTS_CACHE_POSITION = "cache_position" in _CCM_PARAMS
|
| 22 |
+
|
| 23 |
|
| 24 |
# From modeling_t5gemma.py
|
| 25 |
def bidirectional_mask_function(attention_mask: torch.Tensor | None) -> Callable:
|
|
|
|
| 66 |
inputs_embeds = self.embed_tokens(input_ids)
|
| 67 |
input_ids = None
|
| 68 |
|
| 69 |
+
mask_kwargs = {
|
| 70 |
+
"config": self.config,
|
| 71 |
+
_CCM_EMBEDS_KEY: inputs_embeds,
|
| 72 |
+
"attention_mask": attention_mask,
|
| 73 |
+
"past_key_values": None,
|
| 74 |
+
"position_ids": position_ids,
|
| 75 |
+
"or_mask_function": bidirectional_mask_function(attention_mask),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
+
if _CCM_ACCEPTS_CACHE_POSITION:
|
| 78 |
+
mask_kwargs["cache_position"] = torch.arange(
|
| 79 |
+
inputs_embeds.shape[1], device=inputs_embeds.device, dtype=torch.long
|
| 80 |
+
)
|
| 81 |
+
attention_mask = {"full_attention": create_causal_mask(**mask_kwargs)}
|
| 82 |
|
| 83 |
outputs = super().forward(
|
| 84 |
input_ids=input_ids,
|