Instructions to use lca0503/IRIS-Embed-gemma-4-E2B-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use lca0503/IRIS-Embed-gemma-4-E2B-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="lca0503/IRIS-Embed-gemma-4-E2B-it", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForCausalLM processor = AutoProcessor.from_pretrained("lca0503/IRIS-Embed-gemma-4-E2B-it", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("lca0503/IRIS-Embed-gemma-4-E2B-it", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
IRIS-Embed-gemma-4-E2B-it
IRIS-Embed is an instruction-aware speech embedding model. This model is based on Gemma-4-E2B-it.
Model
| Component | Details |
|---|---|
| Backbone | google/gemma-4-E2B-it |
| Text encoder | sentence-transformers/all-MiniLM-L6-v2 |
| Audio states | 13: pre_encoder and encoder_0 through encoder_11 |
| Audio projection | Linear (1024 β 1536), RMSNorm, then Linear (1536 β 1536) |
| Embedding | 1536 dimensions, from the final LLM layer's last non-padding token |
| Similarity | Cosine similarity between L2-normalized embeddings |
MiniLM predicts softmax weights for combining audio encoder states before projection. The states are not normalized before mixing. MiniLM weights and its tokenizer are included in this repository.
We train LoRA adapters in the LLM, the MiniLM routing head, and the audio projection. The routing head is a LayerNorm followed by a linear layer. The base LLM weights, MiniLM text encoder, and audio encoder are frozen during training.
Usage
Use Python 3.12 and PyTorch 2.11. Install the dependencies:
python -m pip install "transformers==5.8.1" "accelerate>=1.13,<2" \
"torchvision>=0.26,<0.27" "pillow>=10" \
"librosa==0.11.0" "soundfile>=0.13,<0.14"
Load the model with Transformers and encode query and document audio:
import librosa
import torch
import torch.nn.functional as F
from transformers import AutoModelForCausalLM, AutoProcessor
model_id = "lca0503/IRIS-Embed-gemma-4-E2B-it"
revision = "main"
model = AutoModelForCausalLM.from_pretrained(
model_id,
revision=revision,
trust_remote_code=True,
dtype=torch.bfloat16,
device_map="auto",
attn_implementation="sdpa",
).eval()
processor = AutoProcessor.from_pretrained(
model_id,
revision=model.config._commit_hash or revision,
trust_remote_code=True,
)
@torch.inference_mode()
def encode(audio_file, instruction=None):
audio, _ = librosa.load(
audio_file, sr=processor.feature_extractor.sampling_rate, mono=True
)
prefix = (
"Document: "
if instruction is None
else f"Instruction: {instruction}\nQuery: "
)
messages = [{
"role": "user",
"content": [
{"type": "text", "text": prefix},
{"type": "audio", "audio": audio},
],
}]
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
processor_kwargs={"audio_weight_text": instruction},
).to(model.device)
outputs = model(
**inputs, output_hidden_states=True, return_dict=True, use_cache=False
)
mask = inputs["attention_mask"]
last = mask.shape[1] - 1 - mask.flip(1).argmax(dim=1)
rows = torch.arange(mask.shape[0], device=mask.device)
embedding = outputs.hidden_states[-1][rows, last].float()
return F.normalize(embedding, dim=-1, eps=1e-8).cpu()
query = encode("query.wav", "Find speech from the same speaker as the query.")
documents = torch.cat([encode("document_1.wav"), encode("document_2.wav")])
scores = query @ documents.T
print(scores)
Prompt and router inputs
| Side | LLM user content | MiniLM input |
|---|---|---|
| Query | Instruction: <instruction>\nQuery: <audio> |
Instruction: <instruction>\nQuery: |
| Document | Document: <audio> |
Document: |
Pass the query instruction as audio_weight_text in processor_kwargs.
The processor adds the MiniLM prompt prefix.
For documents, pass None.
The embedding uses the last token of the assistant prefix added by the chat template.
License
The pretrained Gemma and MiniLM components are provided under Apache-2.0; see LICENSE and LICENSE-MiniLM. See NOTICE for provenance and modifications.
- Downloads last month
- 35