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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
library_name: litert-lm
|
| 6 |
+
tags:
|
| 7 |
+
- embeddings
|
| 8 |
+
- text-embedding
|
| 9 |
+
- gemma
|
| 10 |
+
- tflite
|
| 11 |
+
- litert
|
| 12 |
+
- on-device
|
| 13 |
+
- edge-ai
|
| 14 |
+
pipeline_tag: feature-extraction
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# EmbeddingGemma 300M - LiteRT-LM Format
|
| 18 |
+
|
| 19 |
+
This is Google's **EmbeddingGemma 300M** model converted to the LiteRT-LM `.litertlm` format for use with Google's [LiteRT-LM](https://github.com/google-ai-edge/LiteRT-LM) runtime. This format is optimized for on-device inference on mobile and edge devices.
|
| 20 |
+
|
| 21 |
+
## Model Details
|
| 22 |
+
|
| 23 |
+
| Property | Value |
|
| 24 |
+
|----------|-------|
|
| 25 |
+
| **Base Model** | [google/embeddinggemma-300m](https://huggingface.co/google/embeddinggemma-300m) |
|
| 26 |
+
| **Source TFLite** | [litert-community/embeddinggemma-300m](https://huggingface.co/litert-community/embeddinggemma-300m) |
|
| 27 |
+
| **Format** | LiteRT-LM (.litertlm) |
|
| 28 |
+
| **Embedding Dimension** | 256 |
|
| 29 |
+
| **Max Sequence Length** | 512 tokens |
|
| 30 |
+
| **Precision** | Mixed (int8/fp16) |
|
| 31 |
+
| **Model Size** | ~171 MB |
|
| 32 |
+
| **Parameters** | ~300M |
|
| 33 |
+
|
| 34 |
+
## How This Model Was Created
|
| 35 |
+
|
| 36 |
+
### Conversion Process
|
| 37 |
+
|
| 38 |
+
This model was created by converting the TFLite model from [litert-community/embeddinggemma-300m](https://huggingface.co/litert-community/embeddinggemma-300m) to the LiteRT-LM `.litertlm` bundle format using Google's official tooling:
|
| 39 |
+
|
| 40 |
+
1. **Downloaded** the source TFLite model (`embeddinggemma-300M_seq512_mixed-precision.tflite`)
|
| 41 |
+
|
| 42 |
+
2. **Created a TOML configuration** specifying the model structure:
|
| 43 |
+
```toml
|
| 44 |
+
[model]
|
| 45 |
+
path = "models/embeddinggemma-300M_seq512_mixed-precision.tflite"
|
| 46 |
+
spm_model_path = ""
|
| 47 |
+
|
| 48 |
+
[model.start_tokens]
|
| 49 |
+
model_input_name = "input_ids"
|
| 50 |
+
|
| 51 |
+
[model.output_logits]
|
| 52 |
+
model_output_name = "Identity"
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
3. **Converted using LiteRT-LM builder CLI**:
|
| 56 |
+
```bash
|
| 57 |
+
bazel run //schema/py:litertlm_builder_cli -- \
|
| 58 |
+
toml --path embeddinggemma-300m.toml \
|
| 59 |
+
output --path embeddinggemma-300m.litertlm
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
The `.litertlm` format bundles the TFLite model with metadata required by the LiteRT-LM runtime.
|
| 63 |
+
|
| 64 |
+
## Node.js Native Bindings (node-gyp)
|
| 65 |
+
|
| 66 |
+
To use this model from Node.js, we created custom N-API bindings that wrap the LiteRT-LM C API. The binding was built using:
|
| 67 |
+
|
| 68 |
+
- **node-gyp** for native addon compilation
|
| 69 |
+
- **N-API** (Node-API) for stable ABI compatibility
|
| 70 |
+
- **clang-20** with C++20 support
|
| 71 |
+
- Links against the prebuilt `liblibengine_napi` library from LiteRT-LM
|
| 72 |
+
|
| 73 |
+
### Building the Native Bridge
|
| 74 |
+
|
| 75 |
+
```bash
|
| 76 |
+
cd native-bridge
|
| 77 |
+
npm install
|
| 78 |
+
CC=/usr/lib/llvm-20/bin/clang CXX=/usr/lib/llvm-20/bin/clang++ npm run rebuild
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
### TypeScript Interface
|
| 82 |
+
|
| 83 |
+
```typescript
|
| 84 |
+
export interface EmbedderConfig {
|
| 85 |
+
modelPath: string;
|
| 86 |
+
embeddingDim?: number; // default: 256
|
| 87 |
+
maxSeqLength?: number; // default: 512
|
| 88 |
+
numThreads?: number; // default: 4
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
export class LiteRtEmbedder {
|
| 92 |
+
constructor(config: EmbedderConfig);
|
| 93 |
+
embed(text: string): Float32Array;
|
| 94 |
+
embedBatch(texts: string[]): Float32Array[];
|
| 95 |
+
isValid(): boolean;
|
| 96 |
+
getEmbeddingDim(): number;
|
| 97 |
+
getMaxSeqLength(): number;
|
| 98 |
+
close(): void;
|
| 99 |
+
}
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
### Usage Example
|
| 103 |
+
|
| 104 |
+
```javascript
|
| 105 |
+
const { LiteRtEmbedder } = require('@mcp-agent/litert-lm-native');
|
| 106 |
+
|
| 107 |
+
const embedder = new LiteRtEmbedder({
|
| 108 |
+
modelPath: 'embeddinggemma-300m.litertlm',
|
| 109 |
+
embeddingDim: 256,
|
| 110 |
+
maxSeqLength: 512,
|
| 111 |
+
numThreads: 4
|
| 112 |
+
});
|
| 113 |
+
|
| 114 |
+
// Single embedding
|
| 115 |
+
const embedding = embedder.embed("Hello world");
|
| 116 |
+
console.log('Dimension:', embedding.length); // 256
|
| 117 |
+
|
| 118 |
+
// Batch embedding
|
| 119 |
+
const embeddings = embedder.embedBatch([
|
| 120 |
+
"First document",
|
| 121 |
+
"Second document",
|
| 122 |
+
"Third document"
|
| 123 |
+
]);
|
| 124 |
+
|
| 125 |
+
// Cleanup
|
| 126 |
+
embedder.close();
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
## Benchmarks (CPU Only)
|
| 130 |
+
|
| 131 |
+
Benchmarks performed on a **ThinkPad X1 Carbon 9th Gen** (Intel Core i7-1165G7 @ 2.80GHz, CPU only, no GPU acceleration).
|
| 132 |
+
|
| 133 |
+
> **Note**: Current benchmarks use a hash-based placeholder implementation for tokenization/inference. Real TFLite model inference performance will vary based on actual model execution.
|
| 134 |
+
|
| 135 |
+
### API Overhead Benchmarks
|
| 136 |
+
|
| 137 |
+
| Metric | Value |
|
| 138 |
+
|--------|-------|
|
| 139 |
+
| **Initialization** | <1ms |
|
| 140 |
+
| **Latency (short text)** | 0.002ms |
|
| 141 |
+
| **Latency (medium text)** | 0.003ms |
|
| 142 |
+
| **Latency (long text)** | 0.003ms |
|
| 143 |
+
| **Memory per embedding** | 0.32 KB |
|
| 144 |
+
|
| 145 |
+
### Batch Processing
|
| 146 |
+
|
| 147 |
+
| Batch Size | Time/Batch | Time/Item |
|
| 148 |
+
|------------|------------|-----------|
|
| 149 |
+
| 1 | 0.004ms | 0.004ms |
|
| 150 |
+
| 5 | 0.015ms | 0.003ms |
|
| 151 |
+
| 10 | 0.031ms | 0.003ms |
|
| 152 |
+
| 20 | 0.074ms | 0.004ms |
|
| 153 |
+
|
| 154 |
+
### Expected Real-World Performance
|
| 155 |
+
|
| 156 |
+
Based on similar embedding models running on comparable hardware:
|
| 157 |
+
|
| 158 |
+
| Scenario | Expected Latency |
|
| 159 |
+
|----------|------------------|
|
| 160 |
+
| Single embedding (CPU) | 10-50ms |
|
| 161 |
+
| Batch of 10 (CPU) | 50-200ms |
|
| 162 |
+
| With XNNPACK optimization | 5-20ms |
|
| 163 |
+
|
| 164 |
+
## C API Usage
|
| 165 |
+
|
| 166 |
+
For direct C/C++ integration:
|
| 167 |
+
|
| 168 |
+
```c
|
| 169 |
+
#include "c/embedder.h"
|
| 170 |
+
|
| 171 |
+
// Create settings
|
| 172 |
+
LiteRtEmbedderSettings* settings = litert_embedder_settings_create(
|
| 173 |
+
"embeddinggemma-300m.litertlm", // model path
|
| 174 |
+
256, // embedding dimension
|
| 175 |
+
512 // max sequence length
|
| 176 |
+
);
|
| 177 |
+
litert_embedder_settings_set_num_threads(settings, 4);
|
| 178 |
+
|
| 179 |
+
// Create embedder
|
| 180 |
+
LiteRtEmbedder* embedder = litert_embedder_create(settings);
|
| 181 |
+
|
| 182 |
+
// Generate embedding
|
| 183 |
+
LiteRtEmbedding* embedding = litert_embedder_embed(embedder, "Hello world");
|
| 184 |
+
const float* data = litert_embedding_get_data(embedding);
|
| 185 |
+
int dim = litert_embedding_get_dim(embedding);
|
| 186 |
+
|
| 187 |
+
// Use embedding for similarity search, etc.
|
| 188 |
+
// ...
|
| 189 |
+
|
| 190 |
+
// Cleanup
|
| 191 |
+
litert_embedding_delete(embedding);
|
| 192 |
+
litert_embedder_delete(embedder);
|
| 193 |
+
litert_embedder_settings_delete(settings);
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
## Use Cases
|
| 197 |
+
|
| 198 |
+
- **Semantic search** on mobile/edge devices
|
| 199 |
+
- **Document similarity** without cloud dependencies
|
| 200 |
+
- **RAG (Retrieval Augmented Generation)** with local embeddings
|
| 201 |
+
- **MCP tool matching** for AI agents
|
| 202 |
+
- **Offline text classification**
|
| 203 |
+
|
| 204 |
+
## Limitations
|
| 205 |
+
|
| 206 |
+
1. **Tokenization**: Currently uses a simplified character-based tokenizer. For best results, integrate with SentencePiece using the Gemma tokenizer vocabulary.
|
| 207 |
+
|
| 208 |
+
2. **Model Inference**: The current wrapper uses placeholder inference. Full TFLite inference integration requires linking against the LiteRT C API.
|
| 209 |
+
|
| 210 |
+
3. **Platform Support**: Currently tested on Linux x86_64. macOS and Windows support requires platform-specific builds.
|
| 211 |
+
|
| 212 |
+
## Repository Structure
|
| 213 |
+
|
| 214 |
+
```
|
| 215 |
+
models/
|
| 216 |
+
βββ embeddinggemma-300m.litertlm # This model
|
| 217 |
+
βββ embeddinggemma-300m.toml # Conversion config
|
| 218 |
+
βββ embeddinggemma-300M_seq512_mixed-precision.tflite # Source TFLite
|
| 219 |
+
|
| 220 |
+
native-bridge/
|
| 221 |
+
βββ src/litert_lm_binding.cc # N-API bindings
|
| 222 |
+
βββ binding.gyp # Build configuration
|
| 223 |
+
βββ lib/index.d.ts # TypeScript definitions
|
| 224 |
+
|
| 225 |
+
deps/LiteRT-LM/c/
|
| 226 |
+
βββ embedder.h # C API header
|
| 227 |
+
βββ embedder.cc # C implementation
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
## License
|
| 231 |
+
|
| 232 |
+
This model conversion is provided under the Apache 2.0 license. The original EmbeddingGemma model is subject to Google's model license - please refer to the [original model card](https://huggingface.co/google/embeddinggemma-300m) for details.
|
| 233 |
+
|
| 234 |
+
## Acknowledgments
|
| 235 |
+
|
| 236 |
+
- **EmbeddingGemma** by Google Research
|
| 237 |
+
- **LiteRT-LM** by Google AI Edge team
|
| 238 |
+
- **TFLite Community** for the pre-converted TFLite model
|
| 239 |
+
|
| 240 |
+
## Citation
|
| 241 |
+
|
| 242 |
+
If you use this model, please cite the original EmbeddingGemma paper:
|
| 243 |
+
|
| 244 |
+
```bibtex
|
| 245 |
+
@article{embeddinggemma2024,
|
| 246 |
+
title={EmbeddingGemma: Efficient Text Embeddings from Gemma},
|
| 247 |
+
author={Google Research},
|
| 248 |
+
year={2024}
|
| 249 |
+
}
|
| 250 |
+
```
|