Skip to main content

The EmbeddingRWKV Model

Project description

EmbeddingRWKV

A high-efficiency text embedding and reranking model based on RWKV architecture.

🚀 Quick Start (End-to-End)

Get text embeddings in just a few lines. The tokenizer and model are designed to work seamlessly together.

Note: Always set add_eos=True during tokenization. The model relies on the EOS token (65535) to mark the end of a sentence for correct embedding generation.

import os
# Set environment for JIT compilation (Optional, set to '1' for CUDA acceleration)
os.environ["RWKV_CUDA_ON"] = '1'

from rwkv_emb.tokenizer import RWKVTokenizer
from rwkv_emb.model import EmbeddingRWKV

# 1. Initialize Tokenizer & Model
model = EmbeddingRWKV(model_path='/path/to/model.pth')
tokenizer = RWKVTokenizer()

# 2. Tokenize (Text -> Tokens)
text = "Hello world! This is RWKV embedding."
# Important: Enable add_eos=True to append the required EOS token (65535)
tokens = tokenizer.encode(text, add_eos=True)
print(f"Tokens: {tokens}") 

# 3. Inference (Tokens -> Embedding)
embedding, state = model.forward(tokens, None)

print(f"Embedding shape: {embedding.shape}")
print(embedding.shape)

⚡ Batch Inference & Performance Guide

For production use cases, running inference in batches is significantly faster.

⚠️ Critical Performance Tip: Pad to Same Length

While the model supports batches with variable sequence lengths, we strongly recommend padding all sequences to the same length for maximum GPU throughput.

  • Pad Token: 0
  • Performance: Fixed-length batches allow the CUDA kernel to parallelize computation efficiently. Variable-length batches will trigger a slower execution path.

Batch Example (Recommended)

# Example: Batching two sentences of different lengths
sentences = [
    "Short sentence.",
    "This is a slightly longer sentence for demonstration."
]

# 1. Tokenize all
batch_tokens = [tokenizer.encode(s, add_eos=True) for s in sentences]

# 2. Left pad to the longest sequence in the batch using 0
max_len = max(len(t) for t in batch_tokens)

for i in range(len(batch_tokens)):
    pad_len = max_len - len(batch_tokens[i])
    # insert 0s to the beginning
    batch_tokens[i] = [0] * pad_len + batch_tokens[i]

# batch_tokens is now a rectangular matrix (List of Lists with same length)
print(f"Padded Batch: {batch_tokens}")

# 3. Fast Inference
embeddings, states = model.forward(batch_tokens, None)

# embeddings shape: [Batch_Size, Embedding_Dim]
print("Batch Embeddings:", embeddings.shape)
print("Batch States:", states[0].shape, states[1].shape)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rwkv_emb-0.0.7.tar.gz (397.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rwkv_emb-0.0.7-py3-none-any.whl (396.0 kB view details)

Uploaded Python 3

File details

Details for the file rwkv_emb-0.0.7.tar.gz.

File metadata

  • Download URL: rwkv_emb-0.0.7.tar.gz
  • Upload date:
  • Size: 397.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rwkv_emb-0.0.7.tar.gz
Algorithm Hash digest
SHA256 ff00770c083635ba75320646832fc02b1f352e60ac5fab464a3058e980b8acfb
MD5 f8e030f5a3bc5349f106607ae3df079b
BLAKE2b-256 93d8e1ebbbc37be68554e2f5001fe94e35e2911e583cd529cad536aa8625a79f

See more details on using hashes here.

File details

Details for the file rwkv_emb-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: rwkv_emb-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 396.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rwkv_emb-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b0a6e6e656a232ab56ddca32e060b8ddd31097c2429a56c33a3a51508a2e84e6
MD5 8f5c53436895afe4aa5260d139d35cf7
BLAKE2b-256 3edd12680810f1f357a8973f6e2c38aab660c80148246e2eb31be75f2f24179d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page