[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"tool-yalue--onnxruntime_go":3,"similar-yalue--onnxruntime_go":95},{"id":4,"github_repo":5,"name":6,"description_en":7,"description_zh":8,"ai_summary_zh":9,"readme_en":10,"readme_zh":11,"quickstart_zh":12,"use_case_zh":13,"hero_image_url":14,"owner_login":15,"owner_name":16,"owner_avatar_url":17,"owner_bio":18,"owner_company":18,"owner_location":18,"owner_email":19,"owner_twitter":18,"owner_website":20,"owner_url":21,"languages":22,"stars":35,"forks":36,"last_commit_at":37,"license":38,"difficulty_score":39,"env_os":40,"env_gpu":41,"env_ram":42,"env_deps":43,"category_tags":49,"github_topics":51,"view_count":60,"oss_zip_url":18,"oss_zip_packed_at":18,"status":61,"created_at":62,"updated_at":63,"faqs":64,"releases":94},6484,"yalue\u002Fonnxruntime_go","onnxruntime_go","A Go (golang) library wrapping microsoft\u002Fonnxruntime.","onnxruntime_go 是一个专为 Go 语言开发者设计的跨平台库，旨在简化在 Go 程序中加载和运行 ONNX 格式神经网络模型的过程。它封装了微软官方的 onnxruntime 核心功能，让 Go 开发者能轻松集成高性能的 AI 推理能力。\n\n该工具主要解决了现有 Go 包装器在 Windows 系统上的兼容性难题。由于官方库依赖 MSVC 编译器而 Go 的 CGo 在 Windows 上通常使用 Mingw，导致以往方案难以在 Windows 运行。onnxruntime_go 通过手动动态加载共享库的方式巧妙规避了这一冲突，无需修改源码即可在 Windows、Linux 和 macOS 上无缝工作。此外，它还利用 Go 的泛型特性优雅地支持多种张量数据类型，并经过验证可兼容 TensorRT、CUDA 和 CoreML 等加速后端。\n\n非常适合需要将预训练 AI 模型部署到生产环境的 Go 后端工程师、算法研究人员以及希望在本地应用中嵌入推理功能的开发者。只要具备基础的 Go 开发环境并配置好对应版本的 onnxruntime 动态库，即可快速上手，让 AI 模型在 G","onnxruntime_go 是一个专为 Go 语言开发者设计的跨平台库，旨在简化在 Go 程序中加载和运行 ONNX 格式神经网络模型的过程。它封装了微软官方的 onnxruntime 核心功能，让 Go 开发者能轻松集成高性能的 AI 推理能力。\n\n该工具主要解决了现有 Go 包装器在 Windows 系统上的兼容性难题。由于官方库依赖 MSVC 编译器而 Go 的 CGo 在 Windows 上通常使用 Mingw，导致以往方案难以在 Windows 运行。onnxruntime_go 通过手动动态加载共享库的方式巧妙规避了这一冲突，无需修改源码即可在 Windows、Linux 和 macOS 上无缝工作。此外，它还利用 Go 的泛型特性优雅地支持多种张量数据类型，并经过验证可兼容 TensorRT、CUDA 和 CoreML 等加速后端。\n\n非常适合需要将预训练 AI 模型部署到生产环境的 Go 后端工程师、算法研究人员以及希望在本地应用中嵌入推理功能的开发者。只要具备基础的 Go 开发环境并配置好对应版本的 onnxruntime 动态库，即可快速上手，让 AI 模型在 Go 生态中高效运转。","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:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go_examples).\n\nThe [onnxruntime](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fonnxruntime) 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\nSeveral accelerated execution providers (including TensorRT, CUDA and CoreML)\nare tested and confirmed to work with `onnxruntime_go`.  The \"Requirements\"\nportion of this README (below) has a few more details.\n\n\nNote on onnxruntime Library Versions\n------------------------------------\n\nAt the time of writing, this library uses version 1.24.1 of the onnxruntime\nC API headers.  So, it will probably only work with version 1.24.1 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` and `onnxruntime_ep_c_api.h` files with\n    the versions corresponding to the onnxruntime version you wish to use.\n\n 2. Replace the `test_data\u002Fonnxruntime.dll` (or `test_data\u002Fonnxruntime*.so`,\n    `test_data\u002Fonnxruntime*.dylib`) file with the version corresponding to the\n    onnxruntime version you wish to 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 headers and the shared library files are available to\ndownload from the releases page in the\n[official repo](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fonnxruntime). Download the archive\nfor the release you want to use, and extract it. The header files are located\nin the \"include\" subdirectory, and the shared library will be located in the\n\"lib\" subdirectory. (On Linux systems, you'll need the version of the .so with\nthe appended version numbers, e.g., `libonnxruntime.so.1.24.1`, 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\nthe two `_c_api.h` header files.  (The exception is if you're wanting to enable\nGPU support, where you may need other shared-library files, such as\n`execution_providers_cuda.dll` and `execution_providers_shared.dll` (or their\nequivalents for Linux or OSX).\n\n\nRequirements\n------------\n\nTo use this library, you'll need a version of Go with cgo support.  You'll also\nneed a copy of the correct version of the onnxruntime shared library or DLL for\nyour operating system and architecture.  Prior to initializing\n`onnxruntime_go`, you need to provide a path to this shared library.  See the\nfirst couple lines (i.e., `ort.SetSharedLibraryPath(...)`) of the following\nexample.\n\nIf you want to use CUDA, you'll need to be using a version of the onnxruntime\nshared library with CUDA support, as well as be using a CUDA version supported\nby the underlying version of your onnxruntime library.  For example, version\n1.23.2 of the onnxruntime library only supports CUDA versions 12.x.  See\n[the onnxruntime CUDA support documentation](https:\u002F\u002Fonnxruntime.ai\u002Fdocs\u002Fexecution-providers\u002FCUDA-ExecutionProvider.html)\nfor more specifics.\n\nSimilarly to CUDA, other execution providers have their own separate\nrequirements.  All of these requirements are too numerous to document in this\nREADME.  Please ensure that you are successfully able to use your execution\nprovider of choice in a python script before raising issues about it here.\n\n\nExample Usage\n-------------\n\nThe full documentation can be found at [pkg.go.dev](https:\u002F\u002Fpkg.go.dev\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_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:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_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\u002Fyalue\u002Fonnxruntime_go\"\n    \"os\"\n)\n\nfunc main() {\n    \u002F\u002F This line _may_ be optional; by default the library will try to load\n    \u002F\u002F \"onnxruntime.dll\" on Windows, and \"onnxruntime.so\" on any other system.\n    \u002F\u002F For stability, programs should always set this explicitly.\n    ort.SetSharedLibraryPath(\"path\u002Fto\u002Fonnxruntime.so\")\n\n    err := ort.InitializeEnvironment()\n    if err != nil {\n        panic(err)\n    }\n    defer ort.DestroyEnvironment()\n\n    \u002F\u002F For a slight performance boost and convenience when re-using existing\n    \u002F\u002F tensors, this library expects the user to create all input and output\n    \u002F\u002F tensors prior to creating the session. If this isn't ideal for your use\n    \u002F\u002F case, see the DynamicAdvancedSession type in the documnentation, which\n    \u002F\u002F allows input and output tensors to be specified when calling Run()\n    \u002F\u002F 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    \u002F\u002F This hypothetical network maps a 2x5 input -> 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\u002Fto\u002Fnetwork.onnx\",\n        []string{\"Input 1 Name\"}, []string{\"Output 1 Name\"},\n        []ort.Value{inputTensor}, []ort.Value{outputTensor}, nil)\n    defer session.Destroy()\n\n    \u002F\u002F Calling Run() will run the network, reading the current contents of the\n    \u002F\u002F input tensors and modifying the contents of the output tensors.\n    err = session.Run()\n\n    \u002F\u002F Get a slice view of the output tensor's data.\n    outputData := outputTensor.GetData()\n\n    \u002F\u002F If you want to run the network on a different input, all you need to do\n    \u002F\u002F is modify the input tensor data (available via inputTensor.GetData())\n    \u002F\u002F and call Run() again.\n\n    \u002F\u002F ...\n}\n```\n\n\nDeprecated APIs\n---------------\n\n**Typed `Session[t]`:** Older versions of this library used a typed\n`Session[T]` struct to keep track of sessions. In retrospect, associating type\nparameters with Sessions was unnecessary, and the `AdvancedSession` type, along\nwith its associated APIs, was added to rectify this mistake.  For backwards\ncompatibility, the old typed `Session[T]` and `DynamicSession[T]` types are\nstill included and unlikely to be removed.  However, they now delegate their\nfunctionality to `AdvancedSession` internally.  New code should always favor\nusing `AdvancedSession` directly.\n\n**Onnxruntime's training API:** The training API has been deprecated as of\nonnxruntime version 1.20.  Rather than continuing to maintain wrappers for a\ndeprecated API, `onnxruntime_go` has replaced the wrapper functions for the\ntraining API with stubs that return an error.  Users who need to continue to\nuse the training API will need to use an older version.  For example the\nfollowing versions should be compatible with training:\n\n - Version `v1.12.1` of `onnxruntime_go`, and\n - Version 1.19.2 of `onnxruntime`.\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\u002F` 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\u002F`. 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","跨平台 Go 语言 ONNX Runtime 封装库\n===========================================\n\n简介\n-----\n\n本库旨在为 Go 语言代码提供加载和执行神经网络的接口，同时尽可能保持简单易用。\n\n使用本库的一些示例应用可以在 [`onnxruntime_go_examples` 仓库](https:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go_examples) 中找到。\n\n`onnxruntime` 库提供了加载和执行 ONNX 格式神经网络的方法，但该库主要支持 C 和 C++ API。目前已有多个针对 `onnxruntime` 库编写的 Go 语言封装库，然而据我所知，这些现有封装库均不支持 Windows 平台。其原因在于 Microsoft 的 `onnxruntime` 库假设用户将在 Windows 系统上使用 MSVC 编译器，而 Windows 上的 CGo 则要求使用 Mingw。\n\n本封装库通过手动加载 `onnxruntime` 共享库来绕过这些问题，从而不再依赖于 `onnxruntime` 源代码，仅需头文件即可。当然，这种方法同样适用于非 Windows 系统。\n\n此外，本库利用 Go 语言最近引入的泛型特性，支持多种张量数据类型；可参阅 `NewTensor` 或 `NewEmptyTensor` 函数。\n\n经过测试并确认，包括 TensorRT、CUDA 和 CoreML 在内的多种加速执行提供者均可与 `onnxruntime_go` 配合使用。本 README 的“要求”部分（见下文）提供了更多详细信息。\n\n\n关于 onnxruntime 库版本的说明\n------------------------------\n\n截至撰写本文时，本库使用的是 onnxruntime C API 头文件的 1.24.1 版本。因此，它很可能也只能与 onnxruntime 共享库的 1.24.1 版本配合使用。如果您需要使用其他版本，或者当我未能及时更新此仓库时，更新或更改 onnxruntime 版本应该相对容易：\n\n1. 将 `onnxruntime_c_api.h` 和 `onnxruntime_ep_c_api.h` 文件替换为您希望使用的 onnxruntime 版本对应的文件。\n2. 将 `test_data\u002Fonnxruntime.dll`（或 `test_data\u002Fonnxruntime*.so`、`test_data\u002Fonnxruntime*.dylib`）文件替换为您希望使用的 onnxruntime 版本对应的文件。\n3. （如果您关心 DirectML 支持）请验证 `onnxruntime_wrapper.c` 中 `DummyOrtDMLAPI` 结构体中的条目顺序是否与官方仓库中 `dml_provider_factory.h` 头文件中 `OrtDmlApi` 结构体中的顺序一致。更多信息请参阅 `onnxruntime_wrapper.c` 中对该结构体的注释。\n\n请注意，C API 头文件和共享库文件均可从 [官方仓库](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fonnxruntime) 的发布页面下载。下载您希望使用的版本的压缩包并解压即可。头文件位于 “include” 子目录中，共享库则位于 “lib” 子目录中。（在 Linux 系统上，您需要带有版本号后缀的 .so 文件，例如 `libonnxruntime.so.1.24.1`，而不是仅仅作为符号链接的 `libonnxruntime.so`。）压缩包中还包含其他 C++ 头文件、调试符号等文件，但您通常只需要单个 onnxruntime 共享库以及两个 `_c_api.h` 头文件即可。（例外情况是如果您希望启用 GPU 支持，可能还需要其他共享库文件，如 `execution_providers_cuda.dll` 和 `execution_providers_shared.dll`（或其在 Linux 或 OSX 上的对应文件）。）\n\n\n要求\n----\n\n要使用本库，您需要一个支持 cgo 的 Go 版本。此外，您还需要根据您的操作系统和架构获取正确版本的 onnxruntime 共享库或 DLL。在初始化 `onnxruntime_go` 之前，您需要提供该共享库的路径。请参阅以下示例的前几行代码（即 `ort.SetSharedLibraryPath(...)`）。\n\n如果您希望使用 CUDA，您不仅需要使用支持 CUDA 的 onnxruntime 共享库版本，还需确保所使用的 CUDA 版本与您的 onnxruntime 库底层版本兼容。例如，onnxruntime 1.23.2 版本仅支持 CUDA 12.x 版本。有关更具体的信息，请参阅 [onnxruntime CUDA 支持文档](https:\u002F\u002Fonnxruntime.ai\u002Fdocs\u002Fexecution-providers\u002FCUDA-ExecutionProvider.html)。\n\n与 CUDA 类似，其他执行提供者也有各自独立的使用要求。这些要求过于繁多，无法在此 README 中一一列出。请务必先在 Python 脚本中成功使用您选择的执行提供者，再在此处提出相关问题。\n\n\n使用示例\n--------\n\n完整的文档可在 [pkg.go.dev](https:\u002F\u002Fpkg.go.dev\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go) 上查阅。\n\n此外，几个包含必要网络和数据的命令行示例应用程序也可在 [`onnxruntime_go_examples` 仓库](https:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go_examples) 中找到。\n\n以下示例展示了如何使用本库加载并运行一个接受单个输入张量、输出单个张量的 ONNX 网络，这两个张量均包含 32 位浮点数值。请注意，此处省略了错误处理；每个函数都会返回一个 err 值，若发生失败则该值将非空。\n\n```go\nimport (\n    \"fmt\"\n    ort \"github.com\u002Fyalue\u002Fonnxruntime_go\"\n    \"os\"\n)\n\nfunc main() {\n    \u002F\u002F 此行 _可能_ 是可选的；默认情况下，库会在 Windows 上尝试加载 \"onnxruntime.dll\"，而在其他系统上尝试加载 \"onnxruntime.so\"。为保证稳定性，建议程序始终显式设置此路径。\n    ort.SetSharedLibraryPath(\"path\u002Fto\u002Fonnxruntime.so\")\n\n    err := ort.InitializeEnvironment()\n    if err != nil {\n        panic(err)\n    }\n    defer ort.DestroyEnvironment()\n\n    \u002F\u002F 为了略微提升性能并方便重复使用现有张量，本库要求用户在创建会话之前先创建所有输入和输出张量。如果这对您的应用场景不太合适，可以参考文档中的 DynamicAdvancedSession 类型，它允许在调用 Run() 时而非初始化会话时指定输入和输出张量。\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    \u002F\u002F 该假设网络将 2x5 的输入映射为 2x3x4 的输出。\n    outputShape := ort.NewShape(2, 3, 4)\n    outputTensor, err := ort.NewEmptyTensor[float32](outputShape)\n    defer outputTensor.Destroy()\n\nsession, err := ort.NewAdvancedSession(\"path\u002Fto\u002Fnetwork.onnx\",\n        []string{\"Input 1 Name\"}, []string{\"Output 1 Name\"},\n        []ort.Value{inputTensor}, []ort.Value{outputTensor}, nil)\n    defer session.Destroy()\n\n    \u002F\u002F 调用 Run() 将运行网络，读取输入张量的当前内容并修改输出张量的内容。\n    err = session.Run()\n\n    \u002F\u002F 获取输出张量数据的切片视图。\n    outputData := outputTensor.GetData()\n\n    \u002F\u002F 如果你想使用不同的输入运行网络，只需修改输入张量的数据（可通过 inputTensor.GetData() 获取）\n    \u002F\u002F 然后再次调用 Run() 即可。\n\n    \u002F\u002F ...\n}\n```\n\n\n已弃用的 API\n-------------\n\n**类型化的 `Session[t]`:** 该库的旧版本使用了一个带类型的 `Session[T]` 结构体来管理会话。回过头来看，将类型参数与会话关联起来是不必要的，因此引入了 `AdvancedSession` 类型及其相关 API 来纠正这一问题。出于向后兼容性的考虑，旧的类型化 `Session[T]` 和 `DynamicSession[T]` 类型仍然保留，并且短期内不太可能被移除。不过，它们现在会在内部将功能委托给 `AdvancedSession`。建议新代码始终直接使用 `AdvancedSession`。\n\n**ONNX Runtime 的训练 API:** 训练 API 自 ONNX Runtime 1.20 版本起已被弃用。为了不再维护针对已弃用 API 的封装，`onnxruntime_go` 已将训练 API 的封装函数替换为返回错误的存根函数。如果用户仍需使用训练 API，则需要使用较旧的版本。例如，以下版本应与训练功能兼容：\n\n- `onnxruntime_go` 的 `v1.12.1` 版本，\n- ONNX Runtime 的 `1.19.2` 版本。\n\n\n运行测试及测试的系统兼容性\n---------------------------\n\n请导航到此目录并运行 `go test -v`，或者可选地运行 `go test -v -bench=.`。所有测试都应通过；与 CUDA 或其他加速器支持相关的测试将在不支持这些功能的系统或 ONNX Runtime 构建中被跳过。\n\n目前，该仓库包含适用于几种系统的 ONNX Runtime 共享库副本，包括 AMD64 Windows、ARM64 Linux 和 ARM64 Darwin。这些库应该能够让这些系统上的测试顺利通过，而无需用户在克隆仓库之外再复制额外的库文件。然而，未来随着对更多或更少系统的支持增加或减少，这种情况可能会发生变化。\n\n您可能出于以下原因希望使用不同版本的 ONNX Runtime 共享库：\n\n1. 仓库中包含的共享库副本并不支持 CUDA 或其他加速执行提供者，因此如果您使用的是默认库，与 CUDA 相关的测试将始终被跳过。\n   \n2. 许多系统，包括 AMD64 和 i386 Linux 以及 x86 macOS，目前根本未在 `test_data\u002F` 中包含共享库。（我希望通过尽量减少共享库的数量来保持该目录及整个仓库的体积较小。）\n\n如果上述情况或其他原因适用于您，测试代码会在尝试从 `test_data\u002F` 加载库之前检查 `ONNXRUNTIME_SHARED_LIBRARY_PATH` 环境变量。因此，如果您正在使用这些系统之一，或者希望运行与加速器相关的测试，您应当将该环境变量设置为 ONNX Runtime 共享库的路径。之后，运行 `go test -v` 应当能够成功通过所有测试。","# onnxruntime_go 快速上手指南\n\n## 环境准备\n\n### 系统要求\n- **Go 版本**：需要支持 `cgo` 的 Go 版本（建议使用 Go 1.18+ 以利用泛型特性）。\n- **操作系统**：支持 Windows、Linux 和 macOS。\n  - Windows 需注意：本库通过手动加载共享库规避了 MSVC 与 Mingw 的编译器冲突问题，因此可在标准 Go 环境下运行。\n- **架构支持**：AMD64、ARM64 等主流架构。\n\n### 前置依赖\n1. **ONNX Runtime 共享库**：\n   - 需下载与代码中头文件版本匹配（当前为 **1.24.1**）的共享库文件。\n   - **Windows**: `onnxruntime.dll`\n   - **Linux**: `libonnxruntime.so.1.24.1` (注意需带版本号的文件，而非符号链接)\n   - **macOS**: `libonnxruntime.1.24.1.dylib`\n   - 下载地址：[Microsoft ONNX Runtime Releases](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fonnxruntime\u002Freleases)。下载对应版本的压缩包，从 `lib` 目录获取共享库文件。\n   \n2. **GPU 加速支持（可选）**：\n   - 若需使用 CUDA、TensorRT 或 CoreML，请下载包含相应 Execution Provider 支持的 ONNX Runtime 版本，并确保系统已安装对应的驱动（如 CUDA Toolkit）。\n\n## 安装步骤\n\n1. **初始化 Go 模块并安装库**：\n   ```bash\n   go get github.com\u002Fyalue\u002Fonnxruntime_go\n   ```\n\n2. **配置共享库路径**：\n   在运行程序前，需确保程序能找到 ONNX Runtime 共享库。有两种方式：\n   \n   - **方式一：代码中指定（推荐）**\n     在代码初始化环境中显式设置路径（见下方“基本使用”示例）。\n   \n   - **方式二：环境变量指定（用于测试）**\n     如果运行测试或希望全局指定，可设置环境变量：\n     ```bash\n     # Linux\u002FmacOS\n     export ONNXRUNTIME_SHARED_LIBRARY_PATH=\u002Fpath\u002Fto\u002Flibonnxruntime.so.1.24.1\n     \n     # Windows (PowerShell)\n     $env:ONNXRUNTIME_SHARED_LIBRARY_PATH=\"C:\\path\\to\\onnxruntime.dll\"\n     ```\n\n## 基本使用\n\n以下示例展示如何加载一个 ONNX 模型并执行推理。假设模型接受一个 `2x5` 的浮点输入，输出一个 `2x3x4` 的浮点结果。\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    ort \"github.com\u002Fyalue\u002Fonnxruntime_go\"\n    \"os\"\n)\n\nfunc main() {\n    \u002F\u002F 1. 设置共享库路径 (为了稳定性，建议始终显式设置)\n    \u002F\u002F Windows 下通常为 \"onnxruntime.dll\"，其他系统为 \"onnxruntime.so\"\n    ort.SetSharedLibraryPath(\"path\u002Fto\u002Fonnxruntime.so\")\n\n    \u002F\u002F 2. 初始化环境\n    err := ort.InitializeEnvironment()\n    if err != nil {\n        panic(err)\n    }\n    defer ort.DestroyEnvironment()\n\n    \u002F\u002F 3. 准备输入数据 (2x5 的浮点数组)\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    if err != nil {\n        panic(err)\n    }\n    defer inputTensor.Destroy()\n\n    \u002F\u002F 4. 创建空输出张量 (2x3x4)\n    outputShape := ort.NewShape(2, 3, 4)\n    outputTensor, err := ort.NewEmptyTensor[float32](outputShape)\n    if err != nil {\n        panic(err)\n    }\n    defer outputTensor.Destroy()\n\n    \u002F\u002F 5. 创建会话 (需替换为实际的模型路径及输入\u002F输出节点名称)\n    session, err := ort.NewAdvancedSession(\"path\u002Fto\u002Fnetwork.onnx\",\n        []string{\"Input 1 Name\"}, []string{\"Output 1 Name\"},\n        []ort.Value{inputTensor}, []ort.Value{outputTensor}, nil)\n    if err != nil {\n        panic(err)\n    }\n    defer session.Destroy()\n\n    \u002F\u002F 6. 执行推理\n    err = session.Run()\n    if err != nil {\n        panic(err)\n    }\n\n    \u002F\u002F 7. 获取输出结果\n    outputData := outputTensor.GetData()\n    fmt.Println(\"Output data:\", outputData)\n}\n```\n\n### 关键点说明\n- **张量管理**：输入和输出张量需在创建 Session 前准备好。使用 `defer tensor.Destroy()` 及时释放内存。\n- **泛型支持**：使用 `NewEmptyTensor[T]` 可灵活指定数据类型（如 `float32`, `int64` 等）。\n- **重复推理**：若需对同一模型进行多次推理，只需修改 `inputTensor.GetData()` 中的数据并再次调用 `session.Run()` 即可，无需重建 Session。","某电商团队需要在 Go 语言构建的高并发推荐后端中，实时运行基于 PyTorch 训练的商品点击率预测模型。\n\n### 没有 onnxruntime_go 时\n- **跨平台部署受阻**：由于现有 Go 封装库在 Windows 上依赖 Mingw 编译器，而 ONNX Runtime 官方要求 MSVC，导致开发团队无法在统一的 Windows 开发环境中构建和测试服务，只能被迫迁移到 Linux 或配置复杂的混合编译环境。\n- **架构耦合度高**：为了调用模型，团队不得不将核心推荐逻辑拆分为\"Go 业务层 + Python\u002FC++ 微服务”，通过 HTTP 或 gRPC 进行进程间通信，显著增加了系统复杂度和运维成本。\n- **推理延迟不可控**：额外的网络跳转和数据序列化开销使得单次推理耗时增加 20-50 毫秒，在流量高峰期难以满足低延迟的实时响应需求。\n- **硬件加速集成困难**：想要利用 CUDA 或 TensorRT 加速时，需要手动处理不同语言间的内存拷贝和设备上下文切换，极易出错且性能损耗大。\n\n### 使用 onnxruntime_go 后\n- **原生跨平台支持**：onnxruntime_go 通过手动加载动态库绕过编译器限制，让团队可以直接在 Windows、Linux 和 macOS 上使用纯 Go 代码编译并运行模型，实现了开发环境与生产环境的一致性。\n- **进程内高效推理**：模型被直接嵌入 Go 应用程序进程中，消除了网络通信开销，数据无需序列化即可在内存中直接传递，大幅简化了系统架构。\n- **极致低延迟表现**：去除了中间层后，端到端推理延迟降低至毫秒级，轻松支撑每秒数万次的实时请求，显著提升了用户浏览体验。\n- **无缝硬件加速**：只需简单配置路径，onnxruntime_go 即可直接调用底层的 CUDA、TensorRT 或 CoreML 提供者，让 Go 服务能直接利用 GPU 算力而不必关心底层 C\u002FC++ 细节。\n\nonnxruntime_go 打破了 Go 语言在高性能 AI 推理领域的壁垒，让开发者能用单一语言栈构建跨平台、低延迟且支持硬件加速的智能应用。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fyalue_onnxruntime_go_0eb84893.png","yalue","Nathan O","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fyalue_6ad267d3.png",null,"otternes@cs.unc.edu","nathanotterness.com","https:\u002F\u002Fgithub.com\u002Fyalue",[23,27,31],{"name":24,"color":25,"percentage":26},"Go","#00ADD8",80.2,{"name":28,"color":29,"percentage":30},"C","#555555",14.7,{"name":32,"color":33,"percentage":34},"Python","#3572A5",5.2,601,77,"2026-04-10T00:17:08","MIT",4,"Linux, macOS, Windows","非必需。若需使用 CUDA 加速，需 NVIDIA GPU 及对应的 CUDA 版本（例如 onnxruntime 1.23.2 需 CUDA 12.x）；支持 TensorRT 和 CoreML 等其他加速提供者。","未说明",{"notes":44,"python":45,"dependencies":46},"1. 必须安装支持 CGo 的 Go 版本。2. 需手动下载并指定 onnxruntime 共享库路径（Windows 为 DLL，Linux\u002FmacOS 为 SO\u002FDylib）。3. 若需 GPU 支持，需额外下载包含对应执行提供者（如 CUDA）的 onnxruntime 构建版本及相关依赖库。4. 训练 API 已在 onnxruntime 1.20+ 废弃，如需使用训练功能需降级到旧版本库。","不需要 Python (这是一个 Go 语言库)",[47,48],"Go (支持 cgo)","onnxruntime 共享库 (版本需匹配，默认为 1.24.1)",[50],"开发框架",[52,53,54,55,56,57,58,59],"arm64","go","golang","linux","neural-networks","onnx","onnxruntime","windows",2,"ready","2026-03-27T02:49:30.150509","2026-04-11T10:03:12.451077",[65,70,75,80,85,90],{"id":66,"question_zh":67,"answer_zh":68,"source_url":69},29341,"如何在 Windows 上解决 'build constraints exclude all Go files' 编译错误？","该错误通常是因为 Windows 系统默认没有安装 C 编译器，导致 Go 自动禁用了 CGO（CGO_ENABLED=0），而该库的所有文件都依赖 CGO。解决方案是安装一个 C 编译器（如 MinGW-w64、TDM-gcc 或 Clang），并在构建时显式启用 CGO 并指定编译器。例如，如果安装了 Clang，可以使用以下命令构建：\nCGO_ENABLED=1 CC=clang go build .\n如果使用的是 TDM-gcc 或 MinGW，确保它们已添加到系统 PATH 中，Go 通常会自动识别。","https:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go\u002Fissues\u002F53",{"id":71,"question_zh":72,"answer_zh":73,"source_url":74},29342,"如何处理非张量类型的输出（如 Maps 和 Sequences）？","该库已支持接收 Map 和 Sequence 类型的输出。Map 支持已在 commit 4904ca2553aac67 中添加，Sequence 支持在 commit 80e480e2a820fb 中添加。具体的使用示例可以参考 `onnxruntime_go_examples` 仓库中的 `non_tensor_outputs` 示例代码，或者查看源码中的 `TestSklearnNetwork` 测试用例来学习如何读取这些类型的输出。","https:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go\u002Fissues\u002F57",{"id":76,"question_zh":77,"answer_zh":78,"source_url":79},29343,"重新创建 Session 时遇到内存泄漏怎么办？","如果您在销毁旧 Session 后仍观察到内存增长，这通常是由于 Go 的垃圾回收机制（GC）未及时释放内存，而非库本身的泄漏。建议采取以下措施：\n1. 确保在替换 Session 前显式调用了 `oldSession.Destroy()`。\n2. 在销毁 Session 后手动触发垃圾回收：`runtime.GC()`。\n3. 运行循环多次（如 100 次以上）观察内存是否持续无限增长。如果内存稳定在一定水平，则属于正常的 GC 行为。","https:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go\u002Fissues\u002F112",{"id":81,"question_zh":82,"answer_zh":83,"source_url":84},29344,"如何启用和使用 DirectML 执行提供者？","要使用 DirectML，您需要下载并链接启用了 DirectML 版本的 ONNX Runtime 共享库（onnxruntime.dll）。由于版本兼容性要求严格，建议通过设置环境变量 `ONNXRUNTIME_SHARED_LIBRARY_PATH` 指向您下载的特定 DLL 文件路径，而不是使用库中默认的测试文件。启用后，可以通过运行基准测试（`go test -v -bench=.`）来验证加速效果，通常能看到显著的性能提升。","https:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go\u002Fissues\u002F30",{"id":86,"question_zh":87,"answer_zh":88,"source_url":89},29345,"是否支持 CoreML 或 CUDA 硬件加速？","是的，该库支持 CoreML 和 CUDA。对于 CoreML，相关代码已合并到主分支。您可以拉取代码并运行 `go test -v -bench=.` 进行测试，如果看到 CoreML 相关的测试和基准测试通过（而不是被跳过），则说明支持已生效。注意，用户可能需要自行查找并设置相应的标志位（flags），因为为了保持仓库简洁，并未直接包含所有平台的头文件。CUDA 的支持方式类似，需要确保链接的 onnxruntime 库包含 CUDA 支持。","https:\u002F\u002Fgithub.com\u002Fyalue\u002Fonnxruntime_go\u002Fissues\u002F11",{"id":91,"question_zh":92,"answer_zh":93,"source_url":79},29346,"如何在项目中正确管理 Session 的生命周期以避免资源泄露？","在动态创建新 Session 替换旧 Session 时，必须遵循严格的资源管理顺序：\n1. 创建新 Session 时传入配置选项（Options）。\n2. 如果新 Session 创建成功，Options 的所有权会转移给 Session，无需手动销毁 Options；如果创建失败，则必须在 defer 中手动调用 `options.Destroy()`。\n3. 在将新 Session 赋值给变量前，先保存旧 Session 的引用。\n4. 显式调用旧 Session 的 `Destroy()` 方法释放资源。代码示例逻辑如下：\n   - 创建 newSession。\n   - 若 err != nil，销毁 options。\n   - 若成功，将 oldSession = currentSession，currentSession = newSession。\n   - 调用 oldSession.Destroy()。",[],[96,108,116,125,133,142],{"id":97,"name":98,"github_repo":99,"description_zh":100,"stars":101,"difficulty_score":102,"last_commit_at":103,"category_tags":104,"status":61},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[105,50,106,107],"Agent","图像","数据工具",{"id":109,"name":110,"github_repo":111,"description_zh":112,"stars":113,"difficulty_score":102,"last_commit_at":114,"category_tags":115,"status":61},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[50,106,105],{"id":117,"name":118,"github_repo":119,"description_zh":120,"stars":121,"difficulty_score":60,"last_commit_at":122,"category_tags":123,"status":61},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",150037,"2026-04-10T23:33:47",[50,105,124],"语言模型",{"id":126,"name":127,"github_repo":128,"description_zh":129,"stars":130,"difficulty_score":60,"last_commit_at":131,"category_tags":132,"status":61},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108322,"2026-04-10T11:39:34",[50,106,105],{"id":134,"name":135,"github_repo":136,"description_zh":137,"stars":138,"difficulty_score":60,"last_commit_at":139,"category_tags":140,"status":61},6121,"gemini-cli","google-gemini\u002Fgemini-cli","gemini-cli 是一款由谷歌推出的开源 AI 命令行工具，它将强大的 Gemini 大模型能力直接集成到用户的终端环境中。对于习惯在命令行工作的开发者而言，它提供了一条从输入提示词到获取模型响应的最短路径，无需切换窗口即可享受智能辅助。\n\n这款工具主要解决了开发过程中频繁上下文切换的痛点，让用户能在熟悉的终端界面内直接完成代码理解、生成、调试以及自动化运维任务。无论是查询大型代码库、根据草图生成应用，还是执行复杂的 Git 操作，gemini-cli 都能通过自然语言指令高效处理。\n\n它特别适合广大软件工程师、DevOps 人员及技术研究人员使用。其核心亮点包括支持高达 100 万 token 的超长上下文窗口，具备出色的逻辑推理能力；内置 Google 搜索、文件操作及 Shell 命令执行等实用工具；更独特的是，它支持 MCP（模型上下文协议），允许用户灵活扩展自定义集成，连接如图像生成等外部能力。此外，个人谷歌账号即可享受免费的额度支持，且项目基于 Apache 2.0 协议完全开源，是提升终端工作效率的理想助手。",100752,"2026-04-10T01:20:03",[141,105,106,50],"插件",{"id":143,"name":144,"github_repo":145,"description_zh":146,"stars":147,"difficulty_score":60,"last_commit_at":148,"category_tags":149,"status":61},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[141,50]]