mirror of
https://github.com/yalue/onnxruntime_go.git
synced 2025-11-02 19:44:00 +08:00
Rename CleanupEnvironment -> DestroyEnvironment
- Named the cleanup function something more consistent with the other cleanup functions. - Fixed a couple bugs where deferred stuff was executed after the environment was destroyed, causing a segfault.
This commit is contained in:
@@ -53,7 +53,7 @@ func main() {
|
|||||||
onnxruntime.SetSharedLibraryPath("path/to/onnxruntime.so")
|
onnxruntime.SetSharedLibraryPath("path/to/onnxruntime.so")
|
||||||
|
|
||||||
err := onnxruntime.InitializeEnvironment()
|
err := onnxruntime.InitializeEnvironment()
|
||||||
defer onnxruntime.CleanupEnvironment()
|
defer onnxruntime.DestroyEnvironment()
|
||||||
|
|
||||||
// We'll assume that network.onnx takes a single 2x3x4 input tensor and
|
// We'll assume that network.onnx takes a single 2x3x4 input tensor and
|
||||||
// produces a 1x2x3 output tensor.
|
// produces a 1x2x3 output tensor.
|
||||||
|
|||||||
@@ -52,6 +52,14 @@ func run() int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
fmt.Printf("The onnxruntime environment initialized OK.\n")
|
fmt.Printf("The onnxruntime environment initialized OK.\n")
|
||||||
|
defer func() {
|
||||||
|
e := onnxruntime.DestroyEnvironment()
|
||||||
|
if e != nil {
|
||||||
|
fmt.Printf("Error destroying onnx environment: %s\n", e)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Environment cleaned up OK.\n")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Load the JSON with the test input and output data.
|
// Load the JSON with the test input and output data.
|
||||||
testInputs, e := loadInputsJSON(
|
testInputs, e := loadInputsJSON(
|
||||||
@@ -98,15 +106,6 @@ func run() int {
|
|||||||
fmt.Printf("Output value %d: expected %f, got %f\n", i,
|
fmt.Printf("Output value %d: expected %f, got %f\n", i,
|
||||||
outputTensor.GetData()[i], testInputs.FlattenedOutput[i])
|
outputTensor.GetData()[i], testInputs.FlattenedOutput[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ordinarily, it is probably fine to call this using defer, but we do it
|
|
||||||
// here just so we can print a status message after the cleanup completes.
|
|
||||||
e = onnxruntime.CleanupEnvironment()
|
|
||||||
if e != nil {
|
|
||||||
fmt.Printf("Error cleaning up the environment: %s\n", e)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
fmt.Printf("The onnxruntime environment was cleaned up OK.\n")
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ func SetSharedLibraryPath(path string) {
|
|||||||
|
|
||||||
// Call this function to initialize the internal onnxruntime environment. If
|
// Call this function to initialize the internal onnxruntime environment. If
|
||||||
// this doesn't return an error, the caller will be responsible for calling
|
// this doesn't return an error, the caller will be responsible for calling
|
||||||
// CleanupEnvironment to free the onnxruntime state when no longer needed.
|
// DestroyEnvironment to free the onnxruntime state when no longer needed.
|
||||||
func InitializeEnvironment() error {
|
func InitializeEnvironment() error {
|
||||||
if ortEnv != nil {
|
if ortEnv != nil {
|
||||||
return fmt.Errorf("The onnxruntime has already been initialized")
|
return fmt.Errorf("The onnxruntime has already been initialized")
|
||||||
@@ -69,7 +69,7 @@ func InitializeEnvironment() error {
|
|||||||
|
|
||||||
status = C.CreateOrtMemoryInfo(&ortMemoryInfo)
|
status = C.CreateOrtMemoryInfo(&ortMemoryInfo)
|
||||||
if status != nil {
|
if status != nil {
|
||||||
CleanupEnvironment()
|
DestroyEnvironment()
|
||||||
return fmt.Errorf("Error creating ORT memory info: %w",
|
return fmt.Errorf("Error creating ORT memory info: %w",
|
||||||
statusToError(status))
|
statusToError(status))
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ func InitializeEnvironment() error {
|
|||||||
|
|
||||||
// Call this function to cleanup the internal onnxruntime environment when it
|
// Call this function to cleanup the internal onnxruntime environment when it
|
||||||
// is no longer needed.
|
// is no longer needed.
|
||||||
func CleanupEnvironment() error {
|
func DestroyEnvironment() error {
|
||||||
var e error
|
var e error
|
||||||
if ortMemoryInfo != nil {
|
if ortMemoryInfo != nil {
|
||||||
C.ReleaseOrtMemoryInfo(ortMemoryInfo)
|
C.ReleaseOrtMemoryInfo(ortMemoryInfo)
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func TestTensorTypes(t *testing.T) {
|
|||||||
|
|
||||||
func TestCreateTensor(t *testing.T) {
|
func TestCreateTensor(t *testing.T) {
|
||||||
InitializeRuntime(t)
|
InitializeRuntime(t)
|
||||||
defer CleanupEnvironment()
|
defer DestroyEnvironment()
|
||||||
s := NewShape(1, 2, 3)
|
s := NewShape(1, 2, 3)
|
||||||
tensor1, e := NewEmptyTensor[uint8](s)
|
tensor1, e := NewEmptyTensor[uint8](s)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
@@ -135,6 +135,13 @@ func TestCreateTensor(t *testing.T) {
|
|||||||
|
|
||||||
func TestExampleNetwork(t *testing.T) {
|
func TestExampleNetwork(t *testing.T) {
|
||||||
InitializeRuntime(t)
|
InitializeRuntime(t)
|
||||||
|
defer func() {
|
||||||
|
e := DestroyEnvironment()
|
||||||
|
if e != nil {
|
||||||
|
t.Logf("Error cleaning up environment: %s\n", e)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Create input and output tensors
|
// Create input and output tensors
|
||||||
inputs := parseInputsJSON("test_data/example_network_results.json", t)
|
inputs := parseInputsJSON("test_data/example_network_results.json", t)
|
||||||
@@ -169,10 +176,4 @@ func TestExampleNetwork(t *testing.T) {
|
|||||||
t.Logf("The neural network didn't produce the correct result: %s\n", e)
|
t.Logf("The neural network didn't produce the correct result: %s\n", e)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
e = CleanupEnvironment()
|
|
||||||
if e != nil {
|
|
||||||
t.Logf("Failed cleaning up the environment: %s\n", e)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user