Files
gpu/gpu_onnx.go
zeekay b9eb573818 refactor: consolidate GPU types and add session support
- Remove backend.go (duplicate Backend, Device types)
- Keep types in types.go with CGO-compatible fields
- Add Float64 to Dtype enum in ops.go
- Add newSession for CGO and non-CGO builds
- Fix gpu_cpu.go build tag (!cgo instead of cgo)
- Add cgoSessionHandle and cpuSessionHandle
- Remove deprecated metal/, mlx files
2026-01-09 14:50:48 -08:00

98 lines
2.3 KiB
Go

//go:build cgo && onnx
package gpu
/*
#cgo windows CXXFLAGS: -I${SRCDIR}/onnxruntime/include
#cgo windows LDFLAGS: -L${SRCDIR}/onnxruntime/lib -lonnxruntime
#cgo linux CXXFLAGS: -I${SRCDIR}/onnxruntime/include
#cgo linux LDFLAGS: -L${SRCDIR}/onnxruntime/lib -lonnxruntime -ldl
#cgo darwin CXXFLAGS: -I${SRCDIR}/onnxruntime/include
#cgo darwin LDFLAGS: -L${SRCDIR}/onnxruntime/lib -lonnxruntime
#include <stdlib.h>
// Forward declarations for ONNX Runtime C API
typedef struct OrtApi OrtApi;
typedef struct OrtEnv OrtEnv;
typedef struct OrtSession OrtSession;
typedef struct OrtSessionOptions OrtSessionOptions;
typedef struct OrtValue OrtValue;
typedef struct OrtMemoryInfo OrtMemoryInfo;
typedef struct OrtAllocator OrtAllocator;
// Wrapper functions to check if ONNX Runtime is available
int onnx_runtime_available() {
#ifdef _WIN32
return 1; // Always try ONNX on Windows
#else
return 0; // Use Metal on other platforms by default
#endif
}
const char* onnx_runtime_version() {
return "1.17.0"; // ONNX Runtime version
}
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
// ONNX backend value is defined in gpu.go
// ONNXContext manages ONNX Runtime resources
type ONNXContext struct {
env unsafe.Pointer
session unsafe.Pointer
options unsafe.Pointer
}
// hasONNXSupport checks if ONNX Runtime is available
func hasONNXSupport() bool {
return int(C.onnx_runtime_available()) != 0
}
// getONNXVersion returns the ONNX Runtime version
func getONNXVersion() string {
return C.GoString(C.onnx_runtime_version())
}
// initONNXRuntime initializes ONNX Runtime environment
func initONNXRuntime() (*ONNXContext, error) {
if !hasONNXSupport() {
return nil, fmt.Errorf("ONNX Runtime not available on this platform")
}
ctx := &ONNXContext{}
// TODO: Initialize ONNX Runtime API
// This would call OrtApi functions to create environment and session
return ctx, nil
}
// Close releases ONNX Runtime resources
func (ctx *ONNXContext) Close() error {
if ctx.session != nil {
// TODO: Release session
ctx.session = nil
}
if ctx.env != nil {
// TODO: Release environment
ctx.env = nil
}
return nil
}
// detectONNXBackend checks if ONNX Runtime should be used as fallback
func detectONNXBackend() bool {
// Use ONNX Runtime on Windows when Metal isn't available
if runtime.GOOS == "windows" {
return hasONNXSupport()
}
return false
}