Documentation
¶
Index ¶
- func AllInputTokens(pipeline *BasePipeline) error
- func CreateImageTensors(batch *PipelineBatch, model *Model, preprocessed [][][][]float32) error
- func CreateInputTensors(batch *PipelineBatch, model *Model) error
- func CreateInputTensorsTraining(batch *PipelineBatch, model *Model) error
- func CreateMessages(batch *PipelineBatch, p *BasePipeline, inputs any, systemPrompt string) error
- func CreateMessagesORT(_ *PipelineBatch, _ any, _ string) error
- func CreateModelBackend(ctx context.Context, model *Model, s *options.Options) error
- func CreateTabularTensors(batch *PipelineBatch, model *Model, features [][]float32) error
- func Decode(tokens []uint32, tokenizer *Tokenizer) (string, error)
- func DetectImageTensorFormat(model *Model) (string, error)
- func GetNames(info []InputOutputInfo) []string
- func GetOnnxModelPath(ctx context.Context, model *Model) error
- func LoadTokenizer(ctx context.Context, model *Model, s *options.Options) error
- func PreprocessImages(format string, images []image.Image, preprocess []imageutil.PreprocessStep, ...) ([][][][]float32, error)
- func ReshapeOutput[T float32 | int64 | int32](input []T, meta InputOutputInfo, batchSize int, paddingMask [][]bool, ...) any
- func RunGenerativeSessionOnBatch(ctx context.Context, batch *PipelineBatch, p *BasePipeline, maxLength int, ...) (chan SequenceDelta, chan error, error)
- func RunPipeline[T any](ctx context.Context, size int, preprocess func(*PipelineBatch) error, ...) (result *T, err error)
- func RunSessionOnBatch(ctx context.Context, batch *PipelineBatch, p *BasePipeline) error
- func TokenizeInputPairs(batch *PipelineBatch, tk *Tokenizer, inputs [][2]string, sepToken string)
- func TokenizeInputs(batch *PipelineBatch, tk *Tokenizer, inputs []string)
- type Backend
- type BasePipeline
- type GenerativeModel
- type GenerativeOptions
- type GoMLXModel
- type GoTokenizer
- type Guidance
- type GuidanceType
- type InputOutputInfo
- type Message
- type Model
- type ModelMetadata
- type ModelResources
- type ORTModel
- type OutputInfo
- type Pipeline
- type PipelineBatch
- type PipelineBatchOutput
- type PipelineConfig
- type PipelineMetadata
- type PipelineOption
- type PipelineStatistics
- type RustTokenizer
- type SequenceDelta
- type Shape
- type TokenizedInput
- type Tokenizer
- type TokenizerRuntime
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllInputTokens ¶
func AllInputTokens(pipeline *BasePipeline) error
func CreateImageTensors ¶
func CreateImageTensors(batch *PipelineBatch, model *Model, preprocessed [][][][]float32) error
func CreateInputTensors ¶
func CreateInputTensors(batch *PipelineBatch, model *Model) error
func CreateInputTensorsTraining ¶
func CreateInputTensorsTraining(batch *PipelineBatch, model *Model) error
CreateInputTensorsTraining creates input tensors for training. Same as CreateInputTensors but we never pad the batch size as we expect regular batch sizes from the dataset.
func CreateMessages ¶ added in v0.6.0
func CreateMessages(batch *PipelineBatch, p *BasePipeline, inputs any, systemPrompt string) error
func CreateMessagesORT ¶ added in v0.6.0
func CreateMessagesORT(_ *PipelineBatch, _ any, _ string) error
func CreateModelBackend ¶
func CreateTabularTensors ¶ added in v0.6.2
func CreateTabularTensors(batch *PipelineBatch, model *Model, features [][]float32) error
CreateTabularTensors builds input tensors for classic ML/tabular models.
func DetectImageTensorFormat ¶ added in v0.6.0
DetectImageTensorFormat inspects the first image-like input and infers NHWC or NCHW.
func GetNames ¶
func GetNames(info []InputOutputInfo) []string
func GetOnnxModelPath ¶ added in v0.6.2
func LoadTokenizer ¶
func PreprocessImages ¶ added in v0.6.0
func PreprocessImages(format string, images []image.Image, preprocess []imageutil.PreprocessStep, normalize []imageutil.NormalizationStep) ([][][][]float32, error)
PreprocessImages preprocesses images into a 4D tensor slice according to format and steps.
func ReshapeOutput ¶
func RunGenerativeSessionOnBatch ¶
func RunGenerativeSessionOnBatch(ctx context.Context, batch *PipelineBatch, p *BasePipeline, maxLength int, stopSequences []string, temperature *float64, topP *float64, seed *int, tools []string, guidance *Guidance) (chan SequenceDelta, chan error, error)
func RunPipeline ¶ added in v0.7.8
func RunPipeline[T any](ctx context.Context, size int, preprocess func(*PipelineBatch) error, forward func(context.Context, *PipelineBatch) error, postprocess func(*PipelineBatch) (*T, error)) (result *T, err error)
RunPipeline executes the common synchronous pipeline lifecycle. The typed postprocessor keeps the concrete output type visible to pipeline callers.
func RunSessionOnBatch ¶
func RunSessionOnBatch(ctx context.Context, batch *PipelineBatch, p *BasePipeline) error
func TokenizeInputPairs ¶ added in v0.7.0
func TokenizeInputPairs(batch *PipelineBatch, tk *Tokenizer, inputs [][2]string, sepToken string)
func TokenizeInputs ¶
func TokenizeInputs(batch *PipelineBatch, tk *Tokenizer, inputs []string)
Types ¶
type Backend ¶ added in v0.7.8
type Backend interface {
RunSessionOnBatch(context.Context, *PipelineBatch, *BasePipeline) error
RunGenerativeSessionOnBatch(context.Context, *PipelineBatch, *BasePipeline, int, []string, *float64, *float64, *int, []string, *Guidance) (chan SequenceDelta, chan error, error)
CreateMessages(*PipelineBatch, any, string) error
CreateInputTensors(*PipelineBatch, *Model, bool) error
CreateImageTensors(*PipelineBatch, *Model, [][][][]float32) error
CreateTabularTensors(*PipelineBatch, *Model, [][]float32) error
}
Backend owns all operations that depend on the selected inference runtime. Pipelines use this contract instead of switching on a runtime name.
type BasePipeline ¶
type BasePipeline struct {
SessionContext context.Context
Model *Model
ONNXTimings *timings
TokenizerTimings *timings
PipelineName string
Backend Backend
}
BasePipeline can be embedded by a pipeline.
func NewBasePipeline ¶
func NewBasePipeline[T Pipeline](sessionContext context.Context, config PipelineConfig[T], model *Model) *BasePipeline
type GenerativeModel ¶ added in v0.7.1
type GenerativeModel interface {
Generate(ctx context.Context, inputs [][]Message, tools []string, options *GenerativeOptions) (chan SequenceDelta, chan error, error)
GetStatistics() PipelineStatistics
Destroy() error
}
GenerativeModel abstracts either a generative session or engine.
type GenerativeOptions ¶ added in v0.7.1
type GenerativeOptions struct {
Temperature *float64
TopP *float64
Seed *int
Guidance *Guidance
MaxLength int
}
GenerativeOptions contains settings for text generation.
type GoMLXModel ¶
type GoMLXModel struct {
Backend compute.Backend
OnnxModel onnx.Model
Scope *mlmodel.Scope
Store *mlmodel.Store
Exec *mlmodel.Exec
Call func(scope *mlmodel.Scope, inputs []*graph.Node) []*graph.Node
BatchBuckets []int // BatchBuckets defines bucket sizes for batch dimension padding.
SequenceBuckets []int // SequenceBuckets defines bucket sizes for sequence length padding.
MaxCache int // MaxCache sets the maximum number of unique input shapes to cache.
}
func (*GoMLXModel) Close ¶ added in v0.7.8
func (m *GoMLXModel) Close()
type GoTokenizer ¶
type GoTokenizer struct {
Tokenizer *hftokenizer.Tokenizer
TypeIDs bool
AttentionMask bool
}
type Guidance ¶ added in v0.7.0
type Guidance struct {
Type GuidanceType
Data string
EnableFFTokens bool
}
Guidance configures constrained (guided) generation. EnableFFTokens speeds up generation by force-forwarding tokens that satisfy the grammar without calling the model. Requires BatchSize=1.
type GuidanceType ¶ added in v0.7.0
type GuidanceType string
GuidanceType specifies the constrained-generation strategy.
const ( GuidanceTypeJSONSchema GuidanceType = "json_schema" GuidanceTypeRegex GuidanceType = "regex" GuidanceTypeLarkGrammar GuidanceType = "lark_grammar" )
type InputOutputInfo ¶
type Message ¶ added in v0.6.0
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
ImageURLs []string `json:"image_urls,omitempty"` // File paths or data URIs for multimodal support
}
Message represents a single message in a conversation. Images can be included via ImageURLs for multimodal models.
type Model ¶
type Model struct {
ModelMetadata
ModelResources
// contains filtered or unexported fields
}
type ModelMetadata ¶ added in v0.7.8
type ModelMetadata struct {
ID string
Path string
OnnxFilename string
OnnxPath string
UnknownToken string
InputsMeta []InputOutputInfo
OutputsMeta []InputOutputInfo
IDLabelMap map[int]string
SeparatorToken string
MaxPositionEmbeddings int
IsGenerative bool
}
ModelMetadata contains model identity and inference metadata.
type ModelResources ¶ added in v0.7.8
type ModelResources struct {
Backend Backend
ORTModel *ORTModel
GoMLXModel *GoMLXModel
Tokenizer *Tokenizer
OnnxReader io.ReadCloser
Pipelines map[string]Pipeline
}
ModelResources owns the backend and tokenizer resources for a model.
type ORTModel ¶
type ORTModel struct {
GenerativeSession *disabledGenerativeSession // placeholder when ORT disabled
GenerativeEngine *disabledGenerativeEngine // placeholder when ORT disabled
}
type OutputInfo ¶
type Pipeline ¶
type Pipeline interface {
GetStatistics() PipelineStatistics // Get the pipeline running statistics
Validate() error // Validate the pipeline for correctness
GetMetadata() PipelineMetadata // Return metadata information for the pipeline
GetModel() *Model // Return the model used by the pipeline
IsGenerative() bool // Return whether the pipeline is generative
Run(context.Context, []string) (PipelineBatchOutput, error) // Run the pipeline on an input
}
Pipeline is the interface that any pipeline must implement.
type PipelineBatch ¶
type PipelineBatch struct {
InputValues any
// Multimodal support
Images any // Will hold *ortgenai.Images for generative models
DestroyInputs func() error
DestroyMultimodal func() error
Input []TokenizedInput
PaddingMask [][]bool
OutputValues []any
Size int
MaxSequenceLength int
MaxNewTokens int
// PaddedBatchSize is the bucketed batch size used when XLA pads the batch dimension.
// Zero means no batch padding was applied (ORT / GO backend).
PaddedBatchSize int
}
PipelineBatch represents a batch of inputs that runs through the pipeline.
func NewBatch ¶
func NewBatch(size int) *PipelineBatch
NewBatch initializes a new batch for inference.
func (*PipelineBatch) Destroy ¶
func (b *PipelineBatch) Destroy() error
type PipelineBatchOutput ¶
type PipelineBatchOutput interface {
GetOutput() []any
}
type PipelineConfig ¶
type PipelineConfig[T Pipeline] struct { ModelPath string Name string OnnxFilename string Options []PipelineOption[T] }
PipelineConfig is a configuration for a pipeline type that can be used to create that pipeline.
type PipelineMetadata ¶
type PipelineMetadata struct {
OutputsInfo []OutputInfo
}
type PipelineOption ¶
PipelineOption is an option for a pipeline type.
type PipelineStatistics ¶
type PipelineStatistics struct {
TokenizerTotalTime time.Duration
TokenizerExecutionCount uint64
TokenizerAvgQueryTime time.Duration
OnnxTotalTime time.Duration
OnnxExecutionCount uint64
OnnxAvgQueryTime time.Duration
TotalQueries uint64
TotalDocuments uint64
AverageLatency time.Duration
AverageBatchSize float64
FilteredResults uint64
AvgPrefillSeconds float64
TokensPerSecond float64
CumulativePrefillSum float64
CumulativePrefillCount int
CumulativeTokens int
CumulativeTokenDurationSeconds float64
}
func (*PipelineStatistics) ComputeOnnxStatistics ¶
func (p *PipelineStatistics) ComputeOnnxStatistics(timings *timings)
func (*PipelineStatistics) ComputeTokenizerStatistics ¶
func (p *PipelineStatistics) ComputeTokenizerStatistics(timings *timings)
func (*PipelineStatistics) Print ¶
func (p *PipelineStatistics) Print()
type RustTokenizer ¶
type RustTokenizer struct{}
type SequenceDelta ¶ added in v0.6.0
type Shape ¶
type Shape []int64
type TokenizedInput ¶
type TokenizedInput struct {
Raw string
Tokens []string
TokenIDs []uint32
TypeIDs []uint32
AttentionMask []uint32
SpecialTokensMask []uint32
Offsets [][2]uint
MaxAttentionIndex int
}
TokenizedInput holds the result of running tokenizer on an input.
type Tokenizer ¶
type Tokenizer struct {
RustTokenizer *RustTokenizer
GoTokenizer *GoTokenizer
Runtime TokenizerRuntime
MaxAllowedTokens int
// contains filtered or unexported fields
}
type TokenizerRuntime ¶ added in v0.7.8
type TokenizerRuntime string
TokenizerRuntime identifies the tokenizer implementation in use.
const ( TokenizerRuntimeRust TokenizerRuntime = "RUST" TokenizerRuntimeGo TokenizerRuntime = "GO" )