{"id":17650904,"url":"https://github.com/yalue/onnxruntime_go","last_synced_at":"2025-08-20T02:04:37.767Z","repository":{"id":65788516,"uuid":"594482271","full_name":"yalue/onnxruntime_go","owner":"yalue","description":"A Go (golang) library wrapping microsoft/onnxruntime.","archived":false,"fork":false,"pushed_at":"2025-07-08T00:21:00.000Z","size":165948,"stargazers_count":357,"open_issues_count":3,"forks_count":50,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-08-13T19:36:09.420Z","etag":null,"topics":["arm64","go","golang","linux","neural-networks","onnx","onnxruntime","windows"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yalue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-01-28T17:30:37.000Z","updated_at":"2025-08-13T11:18:44.000Z","dependencies_parsed_at":"2024-03-06T14:53:03.774Z","dependency_job_id":null,"html_url":"https://github.com/yalue/onnxruntime_go","commit_stats":{"total_commits":13,"total_committers":3,"mean_commits":4.333333333333333,"dds":0.5384615384615384,"last_synced_commit":"33ddad1553627f5ca079282b6e3acb453bf18f07"},"previous_names":["yalue/onnxruntime"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/yalue/onnxruntime_go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yalue%2Fonnxruntime_go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yalue%2Fonnxruntime_go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yalue%2Fonnxruntime_go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yalue%2Fonnxruntime_go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yalue","download_url":"https://codeload.github.com/yalue/onnxruntime_go/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yalue%2Fonnxruntime_go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271252993,"owners_count":24726918,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm64","go","golang","linux","neural-networks","onnx","onnxruntime","windows"],"created_at":"2024-10-23T11:39:46.920Z","updated_at":"2025-08-20T02:04:37.754Z","avatar_url":"https://github.com/yalue.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Cross-Platform `onnxruntime` Wrapper for Go\n===========================================\n\nAbout\n-----\n\nThis library seeks to provide an interface for loading and executing neural\nnetworks from Go(lang) code, while remaining as simple to use as possible.\n\nA few example applications using this library can be found in the\n[`onnxruntime_go_examples` repository](https://github.com/yalue/onnxruntime_go_examples).\n\nThe [onnxruntime](https://github.com/microsoft/onnxruntime) library provides a\nway to load and execute ONNX-format neural networks, though the library\nprimarily supports C and C++ APIs.  Several efforts exist to have written\nGo(lang) wrappers for the `onnxruntime` library, but as far as I can tell, none\nof these existing Go wrappers support Windows. This is due to the fact that\nMicrosoft's `onnxruntime` library assumes the user will be using the MSVC\ncompiler on Windows systems, while CGo on Windows requires using Mingw.\n\nThis wrapper works around the issues by manually loading the `onnxruntime`\nshared library, removing any dependency on the `onnxruntime` source code beyond\nthe header files.  Naturally, this approach works equally well on non-Windows\nsystems.\n\nAdditionally, this library uses Go's recent addition of generics to support\nmultiple Tensor data types; see the `NewTensor` or `NewEmptyTensor` functions.\n\n**IMPORTANT:** As of onnxruntime_go v1.12.0 or above, for CUDA acceleration we\nnow require the use of CUDA 12.x and CuDNN 9.x (as required by onnxruntime\nv1.19.0+). Those wishing to stay on CUDA 11.8 should remain on onnxruntime_go\nv1.11.0 or below.\n\nNote on onnxruntime Library Versions\n------------------------------------\n\nAt the time of writing, this library uses version 1.22.0 of the onnxruntime\nC API headers.  So, it will probably only work with version 1.22.0 of the\nonnxruntime shared libraries, as well.  If you need to use a different version,\nor if I get behind on updating this repository, updating or changing the\nonnxruntime version should be fairly easy:\n\n 1. Replace the `onnxruntime_c_api.h` file with the version corresponding to\n    the onnxruntime version you wish to use.\n\n 2. Replace the `test_data/onnxruntime.dll` (or `test_data/onnxruntime*.so`)\n    file with the version corresponding to the onnxruntime version you wish to\n    use.\n\n 3. (If you care about DirectML support) Verify that the entries in the\n    `DummyOrtDMLAPI` struct in `onnxruntime_wrapper.c` match the order in which\n    they appear in the `OrtDmlApi` struct from the `dml_provider_factory.h`\n    header in the official repo.  See the comment on this struct in\n    `onnxruntime_wrapper.c` for more information.\n\nNote that both the C API header and the shared library files are available to\ndownload from the releases page in the\n[official repo](https://github.com/microsoft/onnxruntime). Download the archive\nfor the release you want to use, and extract it. The header file is located in\nthe \"include\" subdirectory, and the shared library will be located in the \"lib\"\nsubdirectory. (On Linux systems, you'll need the version of the .so with the\nappended version numbers, e.g., `libonnxruntime.so.1.22.0`, and _not_ the\n`libonnxruntime.so`, which is just a symbolic link.)  The archive will contain\nseveral other files containing C++ headers, debug symbols, and so on, but you\nshouldn't need anything other than the single onnxruntime shared library and\n`onnxruntime_c_api.h`.  (The exception is if you're wanting to enable GPU\nsupport, where you may need other shared-library files, such as\n`execution_providers_cuda.dll` and `execution_providers_shared.dll` on Windows.)\n\n\nRequirements\n------------\n\nTo use this library, you'll need a version of Go with cgo support.  If you are\nnot using an amd64 version of Windows or Linux (or if you want to provide your\nown library for some other reason), you simply need to provide the correct path\nto the shared library when initializing the wrapper.  This is seen in the first\nfew lines of the following example.\n\nNote that if you want to use CUDA, you'll need to be using a version of the\nonnxruntime shared library with CUDA support, as well as be using a CUDA\nversion supported by the underlying version of your onnxruntime library. For\nexample, version 1.22.0 of the onnxruntime library only supports CUDA versions\n12.x. See\n[the onnxruntime CUDA support documentation](https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html)\nfor more specifics.\n\n\nExample Usage\n-------------\n\nThe full documentation can be found at [pkg.go.dev](https://pkg.go.dev/github.com/yalue/onnxruntime_go).\n\nAdditionally, several example command-line applications complete with necessary\nnetworks and data can be found in the\n[`onnxruntime_go_examples` repository](https://github.com/yalue/onnxruntime_go_examples).\n\nThe following example illustrates how this library can be used to load and run\nan ONNX network taking a single input tensor and producing a single output\ntensor, both of which contain 32-bit floating point values.  Note that error\nhandling is omitted; each of the functions returns an err value, which will be\nnon-nil in the case of failure.\n\n```go\nimport (\n    \"fmt\"\n    ort \"github.com/yalue/onnxruntime_go\"\n    \"os\"\n)\n\nfunc main() {\n    // This line _may_ be optional; by default the library will try to load\n    // \"onnxruntime.dll\" on Windows, and \"onnxruntime.so\" on any other system.\n    // For stability, it is probably a good idea to always set this explicitly.\n    ort.SetSharedLibraryPath(\"path/to/onnxruntime.so\")\n\n    err := ort.InitializeEnvironment()\n    if err != nil {\n        panic(err)\n    }\n    defer ort.DestroyEnvironment()\n\n    // For a slight performance boost and convenience when re-using existing\n    // tensors, this library expects the user to create all input and output\n    // tensors prior to creating the session. If this isn't ideal for your use\n    // case, see the DynamicAdvancedSession type in the documnentation, which\n    // allows input and output tensors to be specified when calling Run()\n    // rather than when initializing a session.\n    inputData := []float32{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}\n    inputShape := ort.NewShape(2, 5)\n    inputTensor, err := ort.NewTensor(inputShape, inputData)\n    defer inputTensor.Destroy()\n    // This hypothetical network maps a 2x5 input -\u003e 2x3x4 output.\n    outputShape := ort.NewShape(2, 3, 4)\n    outputTensor, err := ort.NewEmptyTensor[float32](outputShape)\n    defer outputTensor.Destroy()\n\n    session, err := ort.NewAdvancedSession(\"path/to/network.onnx\",\n        []string{\"Input 1 Name\"}, []string{\"Output 1 Name\"},\n        []ort.Value{inputTensor}, []ort.Value{outputTensor}, nil)\n    defer session.Destroy()\n\n    // Calling Run() will run the network, reading the current contents of the\n    // input tensors and modifying the contents of the output tensors.\n    err = session.Run()\n\n    // Get a slice view of the output tensor's data.\n    outputData := outputTensor.GetData()\n\n    // If you want to run the network on a different input, all you need to do\n    // is modify the input tensor data (available via inputTensor.GetData())\n    // and call Run() again.\n\n    // ...\n}\n```\n\n\nDeprecated APIs\n---------------\n\nOlder versions of this library used a typed `Session[T]` struct to keep track\nof sessions. In retrospect, associating type parameters with Sessions was\nunnecessary, and the `AdvancedSession` type, along with its associated APIs,\nwas added to rectify this mistake.  For backwards compatibility, the old typed\n`Session[T]` and `DynamicSession[T]` types are still included and unlikely to\nbe removed.  However, they now delegate their functionality to\n`AdvancedSession` internally.  New code should always favor using\n`AdvancedSession` directly.\n\n\nRunning Tests and System Compatibility for Testing\n--------------------------------------------------\n\nNavigate to this directory and run `go test -v`, or optionally\n`go test -v -bench=.`.  All tests should pass; tests relating to CUDA or other\naccelerator support will be skipped on systems or onnxruntime builds that don't\nsupport them.\n\nCurrently, this repository includes a copy of the onnxruntime shared libraries\nfor a few systems, including AMD64 windows, ARM64 Linux, and ARM64 darwin.\nThese should allow tests to pass on those systems without users needing to copy\nadditional libraries beyond cloning this repository. In the future, however,\nthis may change if support for more systems are added or removed.\n\nYou may want to use a different version of the `onnxruntime` shared library for\na couple reasons.  In particular:\n\n 1. The included shared library copies do not include support for CUDA or other\n    accelerated execution providers, so CUDA-related tests will always be\n    skipped if you use the default libraries in this repo.\n\n 2. Many systems, including AMD64 and i386 Linux, and x86 osx, do not currently\n    have shared libraries included in `test_data/` in the first place. (I would\n    like to keep this directory, and the overall repo, smaller by keeping the\n    number of shared libraries small.)\n\nIf these or other reasons apply to you, the test code will check the\n`ONNXRUNTIME_SHARED_LIBRARY_PATH` environment variable before attempting to\nload a library from `test_data/`. So, if you are using one of these systems or\nwant accelerator-related tests to run, you should set the environment variable\nto the path to the onnxruntime shared library.  Afterwards, `go test -v` should\nrun and pass.\n\n\nTraining API Support\n--------------------\n\nThe training API has been deprecated as of onnxruntime version 1.20.  Rather\nthan continuing to maintain wrappers for a deprecated API, `onnxruntime_go` has\nreplaced the wrapper functions for the training API with stubs that return an\nerror.  Users who need to continue to use the training API will need to use an\nolder version.  For example the following versions should be compatible with\ntraining:\n\n - Version `v1.12.1` of `onnxruntime_go`, and\n - Version 1.19.2 of `onnxruntime`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyalue%2Fonnxruntime_go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyalue%2Fonnxruntime_go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyalue%2Fonnxruntime_go/lists"}