Skip to content

Commit 96a18ef

Browse files
j341nonowarp-agent
andcommitted
feat: flexible transformers config
- allow passing arbitrary kwargs to transformers backend - merge model_kwargs with kwargs for backward compatibility - document transformers configuration examples (flash attn, custom bnb) Co-Authored-By: Warp <agent@warp.dev>
1 parent 46a6458 commit 96a18ef

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ enc = llmembed.Encoder(
7171
embeddings = enc.encode("Hello world", pooling="pcoteol")
7272
```
7373

74+
## Transformers Backend Configuration
75+
76+
When using the `transformers` backend, you can pass standard Hugging Face `AutoModel` arguments directly to the `Encoder`.
77+
78+
**Example 1: Using Flash Attention 2**
79+
80+
```python
81+
import torch
82+
83+
encoder = Encoder(
84+
model_name="meta-llama/Llama-3.1-8B",
85+
backend="transformers",
86+
attn_implementation="flash_attention_2",
87+
torch_dtype=torch.bfloat16
88+
)
89+
```
90+
91+
**Example 2: Custom Quantization Config**
92+
93+
```python
94+
from transformers import BitsAndBytesConfig
95+
96+
bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16)
97+
98+
encoder = Encoder(
99+
model_name="meta-llama/Llama-3.1-8B",
100+
backend="transformers",
101+
quantization_config=bnb_config
102+
)
103+
```
104+
74105
## vLLM Backend Configuration
75106

76107
When using the `vllm` backend, you can pass native vLLM configuration arguments directly to the `Encoder`. This allows full control over memory usage, parallelism, and model length.

docs/index.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ enc = llmembed.Encoder(
7171
embeddings = enc.encode("Hello world", pooling="pcoteol")
7272
```
7373

74+
## Transformers Backend Configuration
75+
76+
When using the `transformers` backend, you can pass standard Hugging Face `AutoModel` arguments directly to the `Encoder`.
77+
78+
**Example 1: Using Flash Attention 2**
79+
80+
```python
81+
import torch
82+
83+
encoder = Encoder(
84+
model_name="meta-llama/Llama-3.1-8B",
85+
backend="transformers",
86+
attn_implementation="flash_attention_2",
87+
torch_dtype=torch.bfloat16
88+
)
89+
```
90+
91+
**Example 2: Custom Quantization Config**
92+
93+
```python
94+
from transformers import BitsAndBytesConfig
95+
96+
bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16)
97+
98+
encoder = Encoder(
99+
model_name="meta-llama/Llama-3.1-8B",
100+
backend="transformers",
101+
quantization_config=bnb_config
102+
)
103+
```
104+
74105
## vLLM Backend Configuration
75106

76107
When using the `vllm` backend, you can pass native vLLM configuration arguments directly to the `Encoder`. This allows full control over memory usage, parallelism, and model length.

src/llmembed/backends/transformers_backend.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ def __init__(
4040
self.model = None
4141
self.tokenizer = None
4242

43-
# Extract model_kwargs if present
44-
model_kwargs = kwargs.pop("model_kwargs", {})
45-
self._load_model(model_kwargs)
43+
# Merge model_kwargs into kwargs if present for backward compatibility or explicit usage
44+
if "model_kwargs" in kwargs:
45+
kwargs.update(kwargs.pop("model_kwargs"))
46+
47+
self._load_model(kwargs)
4648

47-
def _load_model(self, model_kwargs: "Dict[str, Any]") -> None:
49+
def _load_model(self, load_kws: "Dict[str, Any]") -> None:
4850
quantization_config = None
49-
load_kws = model_kwargs.copy()
51+
# Make a copy to avoid mutating the original kwargs if used elsewhere
52+
load_kws = load_kws.copy()
5053

5154
if self.quantization:
5255
if self.quantization == "4bit":

0 commit comments

Comments
 (0)