rtengo

package module
v0.0.0-...-f70a8e3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 15 Imported by: 0

README

rtengo

Lightweight Go bindings for the RTen WebAssembly runtime. It runs RTen WASM on top of wazero and provides high-level APIs for text embedding, reranking, pruning, and image preprocessing.

Features

  • Pure Go WASM runtime via wazero (no CGO)
  • RTen WASM embedded by default
  • Simple Runtime / Model / Tensor API
  • SentencePiece tokenizer support
  • High-level task APIs (Embedding / Rerank / Prune)

Requirements

  • Go 1.24+
  • RTen .rten model file
  • SentencePiece tokenizer.model

Install

go get github.com/masacento/rtengo

Quick Start

package main

import (
  "context"
  "log"
  "os"

  rten "github.com/masacento/rtengo"
)

func main() {
  ctx := context.Background()

  // Runtime
  rt, err := rten.NewRuntime(ctx)
  if err != nil {
    log.Fatal(err)
  }
  defer rt.Close(ctx)

  // Model
  modelBytes, err := os.ReadFile("model.rten")
  if err != nil {
    log.Fatal(err)
  }
  model, err := rt.LoadModel(ctx, modelBytes)
  if err != nil {
    log.Fatal(err)
  }
  defer model.Close(ctx)

  // Tokenizer
  tk, err := rten.NewSentencePiece("tokenizer.model")
  if err != nil {
    log.Fatal(err)
  }

  // Embedding
  embedder, err := rten.NewEmbedder(ctx, rt, model, tk)
  if err != nil {
    log.Fatal(err)
  }
  vec, err := embedder.Embed(ctx, "Hello")
  if err != nil {
    log.Fatal(err)
  }
  _ = vec
}

Core API

Runtime / Model / Tensor
rt, _ := rten.NewRuntime(ctx)
model, _ := rt.LoadModel(ctx, modelBytes)
outputs, _ := model.Run(ctx, inputTensor)

shape := outputs[0].Shape()
vec, _ := outputs[0].Float32Data(ctx)
Embedding
embedder, _ := rten.NewEmbedder(ctx, rt, model, tk,
  rten.WithEmbeddingNormalize(true),
  rten.WithEmbeddingPooling(rten.PoolingMean),
)
vec, _ := embedder.Embed(ctx, "text")
Rerank
reranker, _ := rten.NewReranker(ctx, rt, model, tk)
results, _ := reranker.Rerank(ctx, "question", passages)
Prune (OpenProvence, etc.)
pruner, _ := rten.NewPruner(ctx, rt, model, tk, rten.PrunerConfig{Threshold: 0.5})
result, _ := pruner.Prune(ctx, "query", "document")
Image Preprocessing
data, _ := rten.LoadAndPreprocessFile(ctx, "cat.jpg", 224, 224)
inputTensor, _ := rt.NewTensorFloat32([]uint64{1, 3, 224, 224}, data)

Examples

  • examples/embedding: text embedding
  • examples/rerank: passage reranking
  • examples/openprovence: document pruning
  • examples/mobilenet: image classification

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ImageNetMean = [3]float32{0.485, 0.456, 0.406}
	ImageNetStd  = [3]float32{0.229, 0.224, 0.225}
)

ImageNet normalization constants.

Functions

func LoadAndPreprocess

func LoadAndPreprocess(ctx context.Context, r io.Reader, width, height int, opts ...Option) ([]float32, error)

LoadAndPreprocess loads an image from a reader, resizes it, and converts to NCHW tensor format.

func LoadAndPreprocessFile

func LoadAndPreprocessFile(ctx context.Context, path string, width, height int, opts ...Option) ([]float32, error)

LoadAndPreprocessFile loads an image from a file path and preprocesses it.

func SplitIntoSentences

func SplitIntoSentences(context string) []string

SplitIntoSentences splits context into sentences for reranking.

Types

type DataType

type DataType int

DataType represents the data type of a tensor.

const (
	DTypeUnknown DataType = 0
	DTypeFloat32 DataType = 1
	DTypeInt32   DataType = 2
	DTypeInt8    DataType = 3
	DTypeUint8   DataType = 4
)

type Embedder

type Embedder struct {
	// contains filtered or unexported fields
}

Embedder produces embeddings from text.

func NewEmbedder

func NewEmbedder(ctx context.Context, rt *Runtime, model *Model, tk Tokenizer, opts ...EmbedderOption) (*Embedder, error)

NewEmbedder creates a new Embedder.

func (*Embedder) Embed

func (e *Embedder) Embed(ctx context.Context, text string) ([]float32, error)

Embed generates an embedding for a single text.

func (*Embedder) EmbedBatch

func (e *Embedder) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)

EmbedBatch generates embeddings for a batch of texts.

type EmbedderConfig

type EmbedderConfig struct {
	Normalize bool
	Pooling   PoolingType
}

EmbedderConfig configures embedding behavior.

type EmbedderOption

type EmbedderOption func(*EmbedderConfig)

EmbedderOption configures an Embedder.

func WithEmbeddingNormalize

func WithEmbeddingNormalize(normalize bool) EmbedderOption

WithEmbeddingNormalize enables or disables L2 normalization.

func WithEmbeddingPooling

func WithEmbeddingPooling(pooling PoolingType) EmbedderOption

WithEmbeddingPooling sets the pooling strategy.

type Encoding

type Encoding struct {
	Tokens        []string
	IDs           []int
	AttentionMask []int
}

Encoding represents the result of tokenization.

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model represents a loaded machine learning model.

func (*Model) Close

func (m *Model) Close(ctx context.Context) error

Close releases resources associated with the model.

func (*Model) GetInputCount

func (m *Model) GetInputCount(ctx context.Context) (uint32, error)

GetInputCount returns the number of model inputs.

func (*Model) GetInputDims

func (m *Model) GetInputDims(ctx context.Context, inputIndex uint32) ([]int32, error)

GetInputDims returns the dimensions of the specified input.

func (*Model) GetOutputCount

func (m *Model) GetOutputCount(ctx context.Context) (uint32, error)

GetOutputCount returns the number of model outputs.

func (*Model) Run

func (m *Model) Run(ctx context.Context, inputs ...*Tensor) ([]*Tensor, error)

Run executes the model with the given input tensors.

type Option

type Option func(*imageConfig)

Option configures image preprocessing.

func WithNormalization

func WithNormalization(mean, std [3]float32) Option

WithNormalization overrides the mean and std used for normalization.

type PoolingType

type PoolingType int

PoolingType defines how to pool token embeddings.

const (
	PoolingMean PoolingType = iota + 1
)

type PruneResult

type PruneResult struct {
	PrunedDocument string
	KeptRatio      float32
	KeptTokens     int
	TotalTokens    int
	RankingScore   float32
	Probability    float32
}

PruneResult represents the result of document pruning.

type Pruner

type Pruner struct {
	// contains filtered or unexported fields
}

Pruner prunes documents based on a query.

func NewPruner

func NewPruner(ctx context.Context, rt *Runtime, model *Model, tk Tokenizer, cfg PrunerConfig) (*Pruner, error)

NewPruner creates a new Pruner.

func (*Pruner) Prune

func (p *Pruner) Prune(ctx context.Context, query, document string) (*PruneResult, error)

Prune processes a query and document through OpenProvence model for pruning and ranking.

func (*Pruner) PruneBatch

func (p *Pruner) PruneBatch(ctx context.Context, query string, documents []string) ([]*PruneResult, error)

PruneBatch processes multiple documents with the same query.

type PrunerConfig

type PrunerConfig struct {
	Threshold float32
}

PrunerConfig configures pruning behavior.

type RerankResult

type RerankResult struct {
	OriginalIndex int
	Score         float32
	Probability   float32
	Passage       string
}

RerankResult represents a single rerank result with score and passage.

type Reranker

type Reranker struct {
	// contains filtered or unexported fields
}

Reranker reranks passages by relevance.

func NewReranker

func NewReranker(ctx context.Context, rt *Runtime, model *Model, tk Tokenizer) (*Reranker, error)

NewReranker creates a new Reranker.

func (*Reranker) Rerank

func (r *Reranker) Rerank(ctx context.Context, question string, passages []string) ([]RerankResult, error)

Rerank reranks passages by relevance to a question.

type Runtime

type Runtime struct {
	// contains filtered or unexported fields
}

Runtime holds the WebAssembly runtime and module.

func NewRuntime

func NewRuntime(ctx context.Context, opts ...RuntimeOption) (*Runtime, error)

NewRuntime creates a new RTen runtime from a WASM module.

func (*Runtime) Close

func (r *Runtime) Close(ctx context.Context) error

Close releases resources associated with the runtime.

func (*Runtime) LoadModel

func (r *Runtime) LoadModel(ctx context.Context, model []byte) (*Model, error)

LoadModel loads a model from binary data.

func (*Runtime) NewTensorFloat32

func (r *Runtime) NewTensorFloat32(shape []uint64, data []float32) (*Tensor, error)

NewTensorFloat32 creates a float32 tensor.

func (*Runtime) NewTensorInt32

func (r *Runtime) NewTensorInt32(shape []uint64, data []int32) (*Tensor, error)

NewTensorInt32 creates an int32 tensor.

type RuntimeConfig

type RuntimeConfig struct {
	Stdout io.Writer
	Stderr io.Writer
}

RuntimeConfig holds options for runtime creation.

type RuntimeOption

type RuntimeOption func(*RuntimeConfig)

RuntimeOption configures runtime creation.

func WithStderr

func WithStderr(w io.Writer) RuntimeOption

WithStderr sets the runtime stderr.

func WithStdout

func WithStdout(w io.Writer) RuntimeOption

WithStdout sets the runtime stdout.

type SentencePieceTokenizer

type SentencePieceTokenizer struct {
	// contains filtered or unexported fields
}

SentencePieceTokenizer implements Tokenizer using sentencepiece.

func NewSentencePiece

func NewSentencePiece(modelPath string) (*SentencePieceTokenizer, error)

NewSentencePiece creates a new SentencePiece tokenizer from a model file.

func (*SentencePieceTokenizer) Decode

func (t *SentencePieceTokenizer) Decode(ids []int) string

Decode converts token IDs back to text.

func (*SentencePieceTokenizer) Encode

func (t *SentencePieceTokenizer) Encode(text string, addSpecialTokens bool) (*Encoding, error)

Encode tokenizes the input text and returns an Encoding.

type Tensor

type Tensor struct {
	// contains filtered or unexported fields
}

Tensor represents a tensor in the WASM memory.

func (*Tensor) Close

func (t *Tensor) Close(ctx context.Context) error

Close releases resources associated with the tensor.

func (*Tensor) DataType

func (t *Tensor) DataType() DataType

DataType returns the data type of the tensor.

func (*Tensor) Float32Data

func (t *Tensor) Float32Data(ctx context.Context) ([]float32, error)

Float32Data returns the float32 data of the tensor.

func (*Tensor) Int32Data

func (t *Tensor) Int32Data(ctx context.Context) ([]int32, error)

Int32Data returns the int32 data of the tensor.

func (*Tensor) Shape

func (t *Tensor) Shape() []uint64

Shape returns the shape of the tensor.

type Tokenizer

type Tokenizer interface {
	Encode(text string, addSpecial bool) (*Encoding, error)
	Decode(ids []int) string
}

Tokenizer defines the interface for tokenizers.

Directories

Path Synopsis
examples
embedding command
mobilenet command
openprovence command
reranking command
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL