# Granite Embedding

> IBM's open Granite text embedding models for retrieval and search

- Category: Retrieval & Knowledge (RAG) / Embedding Models & Inference
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/ibm-granite/granite-embedding-models
- Website: https://www.ibm.com/granite
- Canonical page: https://ai-tldr.dev/tools/granite-embedding/

## In short

Granite Embedding is a family of open text embedding models from IBM. The models use a bi-encoder (sentence-transformer) architecture to turn text such as queries, passages, and documents into fixed-length vectors, which you can compare with cosine similarity to find relevant matches. It sits in the Embedding Models & Inference category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python and licensed Apache-2.0.

## Overview

Granite Embedding is a family of open text embedding models from IBM. The models use a bi-encoder (sentence-transformer) architecture to turn text such as queries, passages, and documents into fixed-length vectors, which you can compare with cosine similarity to find relevant matches.

The lineup covers different sizes and languages. English models come in 47M and 149M parameter versions, while multilingual models (97M and 311M parameters) support 200+ languages. The smaller models output 384-dimensional vectors and the larger ones 768, so you can trade off speed, memory, and accuracy.

It is aimed at developers building retrieval, semantic search, and RAG systems who want models trained on permissibly licensed data and released under Apache 2.0 for both research and commercial use. As an embedding model family, it fits the retrieval step that sits in front of a vector database or reranker.

## What it does

- Bi-encoder sentence-transformer models that produce fixed-length vectors for queries, passages, and documents
- English models at 47M and 149M parameters, plus multilingual models at 97M and 311M parameters
- Multilingual models cover 200+ languages with enhanced retrieval for 52, and a 32,768-token context window on the 97M model
- Output embedding sizes of 384 (small models) or 768 (larger models), with Matryoshka dimension support on the 311m multilingual model
- Apache 2.0 licensed, trained on carefully curated, permissibly licensed public datasets
- Works directly with the Sentence Transformers and Hugging Face Transformers libraries, with optional Flash Attention 2 for faster inference

## Getting started

The models load from Hugging Face and work with the Sentence Transformers library. Install it, then encode queries and passages and compare them with cosine similarity.

1. Install Sentence Transformers

   Install the library used to load and run the model.

```bash
pip install sentence_transformers
```

2. Encode text and compare

   Load a Granite Embedding model, encode queries and passages, then compute cosine similarity. The model returns unnormalized vectors; pass normalize_embeddings=True to encode if you need normalized output.

```python
from sentence_transformers import SentenceTransformer, util

model_path = "ibm-granite/granite-embedding-english-r2"
# Load the Sentence Transformer model
model = SentenceTransformer(model_path)

input_queries = [
    ' Who made the song My achy breaky heart? ',
    'summit define'
    ]

input_passages = [
    "Achy Breaky Heart is a country song written by Don Von Tress. Originally titled Don't Tell My Heart and performed by The Marcy Brothers in 1991. ",
    "Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."
    ]

# encode queries and passages
query_embeddings = model.encode(input_queries)
passage_embeddings = model.encode(input_passages)

# calculate cosine similarity
print(util.cos_sim(query_embeddings, passage_embeddings))
```

3. Optional: faster inference with Flash Attention 2

   Flash Attention 2 is optional but can speed up inference.

```bash
pip install flash_attn
```

## When to use it

- Power the retrieval step of a RAG pipeline by embedding documents and queries for a vector search
- Build semantic search over a document or passage collection using cosine similarity
- Match user questions to relevant passages in enterprise knowledge bases
- Run multilingual search and retrieval across 200+ languages with the multilingual models
