Documentation ¶
Overview ¶
This library wraps the C "onnxruntime" library maintained at https://github.com/microsoft/onnxruntime. It seeks to provide as simple an interface as possible to load and run ONNX-format neural networks from Go code.
Index ¶
- Variables
- func DestroyEnvironment() error
- func DisableTelemetry() error
- func EnableTelemetry() error
- func GetTensorElementDataType[T TensorData]() C.ONNXTensorElementDataType
- func InitializeEnvironment() error
- func IsInitialized() bool
- func SetSharedLibraryPath(path string)
- type BadShapeDimensionError
- type DynamicSession
- type FloatData
- type IntData
- type Session
- type Shape
- type Tensor
- type TensorData
Constants ¶
This section is empty.
Variables ¶
var NotInitializedError error = fmt.Errorf("InitializeRuntime() has either " +
"not yet been called, or did not return successfully")
var ShapeOverflowError error = fmt.Errorf("The shape's flattened size " +
"overflows an int64")
var ZeroShapeLengthError error = fmt.Errorf("The shape has no dimensions")
Functions ¶
func DestroyEnvironment ¶
func DestroyEnvironment() error
Call this function to cleanup the internal onnxruntime environment when it is no longer needed.
func DisableTelemetry ¶
func DisableTelemetry() error
Disables telemetry events for the onnxruntime environment. Must be called after initializing the environment using InitializeEnvironment(). It is unclear from the onnxruntime docs whether this will cause an error or silently return if telemetry is already disabled.
func EnableTelemetry ¶
func EnableTelemetry() error
Enables telemetry events for the onnxruntime environment. Must be called after initializing the environment using InitializeEnvironment(). It is unclear from the onnxruntime docs whether this will cause an error or silently return if telemetry is already enabled.
func GetTensorElementDataType ¶
func GetTensorElementDataType[T TensorData]() C.ONNXTensorElementDataType
Returns the ONNX enum value used to indicate TensorData type T.
func InitializeEnvironment ¶
func InitializeEnvironment() error
Call this function to initialize the internal onnxruntime environment. If this doesn't return an error, the caller will be responsible for calling DestroyEnvironment to free the onnxruntime state when no longer needed.
func IsInitialized ¶
func IsInitialized() bool
Returns false if the onnxruntime package is not initialized. Called internally by several functions, to avoid segfaulting if InitializeEnvironment hasn't been called yet.
func SetSharedLibraryPath ¶
func SetSharedLibraryPath(path string)
Use this function to set the path to the "onnxruntime.so" or "onnxruntime.dll" function. By default, it will be set to "onnxruntime.so" on non-Windows systems, and "onnxruntime.dll" on Windows. Users wishing to specify a particular location of this library must call this function prior to calling onnxruntime.InitializeEnvironment().
Types ¶
type BadShapeDimensionError ¶
This type of error is returned when we attempt to validate a tensor that has a negative or 0 dimension.
func (*BadShapeDimensionError) Error ¶
func (e *BadShapeDimensionError) Error() string
type DynamicSession ¶
type DynamicSession[In TensorData, Out TensorData] struct { // contains filtered or unexported fields }
Similar to Session, but does not require the specification of the input and output shapes at session creation time, and allows for input and output tensors to have different types. This allows for fully dynamic input to the onnx model.
func NewDynamicSession ¶
func NewDynamicSession[in TensorData, out TensorData](onnxFilePath string, inputNames, outputNames []string) (*DynamicSession[in, out], error)
Same as NewSession, but for dynamic sessions.
func NewDynamicSessionWithONNXData ¶
func NewDynamicSessionWithONNXData[in TensorData, out TensorData](onnxData []byte, inputNames, outputNames []string) (*DynamicSession[in, out], error)
Similar to NewSessionWithOnnxData, but for dynamic sessions.
func (*DynamicSession[_, _]) Destroy ¶
func (s *DynamicSession[_, _]) Destroy() error
func (*DynamicSession[in, out]) Run ¶
func (s *DynamicSession[in, out]) Run(inputs []*Tensor[in], outputs []*Tensor[out]) error
Runs the dynamic session. Differently from the Session object, this method requires the caller to provide the slice of input and output tensor pointer of the right type. The resulting output is stored in the output tensors, and it is the responsibility of the caller to destroy the tensors to free memory.
type Session ¶
type Session[T TensorData] struct { // contains filtered or unexported fields }
A wrapper around the OrtSession C struct. Requires the user to maintain all input and output tensors, and to use the same data type for input and output tensors.
func NewSession ¶
func NewSession[T TensorData](onnxFilePath string, inputNames, outputNames []string, inputs, outputs []*Tensor[T]) (*Session[T], error)
Loads the ONNX network at the given path, and initializes a Session instance. If this returns successfully, the caller must call Destroy() on the returned session when it is no longer needed. We require the user to provide the input and output tensors and names at this point, in order to not need to re-allocate them every time Run() is called. The user instead can just update or access the input/output tensor data after calling Run(). The input and output tensors MUST outlive this session, and calling session.Destroy() will not destroy the input or output tensors.
func NewSessionWithONNXData ¶
func NewSessionWithONNXData[T TensorData](onnxData []byte, inputNames, outputNames []string, inputs, outputs []*Tensor[T]) (*Session[T], error)
The same as NewSession, but takes a slice of bytes containing the .onnx network rather than a file path.
type Shape ¶
type Shape []int64
The Shape type holds the shape of the tensors used by the network input and outputs.
func (Shape) FlattenedSize ¶
Returns the total number of elements in a tensor with the given shape. Note that this may be an invalid value due to overflow or negative dimensions. If a shape comes from an untrusted source, it may be a good practice to call Validate() prior to trusting the FlattenedSize.
type Tensor ¶
type Tensor[T TensorData] struct { // contains filtered or unexported fields }
func NewEmptyTensor ¶
func NewEmptyTensor[T TensorData](s Shape) (*Tensor[T], error)
Creates a new empty tensor with the given shape. The shape provided to this function is copied, and is no longer needed after this function returns.
func NewTensor ¶
func NewTensor[T TensorData](s Shape, data []T) (*Tensor[T], error)
Creates a new tensor backed by an existing data slice. The shape provided to this function is copied, and is no longer needed after this function returns. If the data slice is longer than s.FlattenedSize(), then only the first portion of the data will be used.
func (*Tensor[T]) Clone ¶
Makes a deep copy of the tensor, including its ONNXRuntime value. The Tensor returned by this function must be destroyed when no longer needed. The returned tensor will also no longer refer to the same underlying data; use GetData() to obtain the new underlying slice.
func (*Tensor[T]) GetData ¶
func (t *Tensor[T]) GetData() []T
Returns the slice containing the tensor's underlying data. The contents of the slice can be read or written to get or set the tensor's contents.
type TensorData ¶
This is used as a type constraint for the generic Tensor type.