{"id":14970654,"url":"https://github.com/lwch/gotorch","last_synced_at":"2025-04-10T05:07:23.963Z","repository":{"id":173617954,"uuid":"650926827","full_name":"lwch/gotorch","owner":"lwch","description":"golang libtorch binding","archived":false,"fork":false,"pushed_at":"2025-02-08T08:08:13.000Z","size":196,"stargazers_count":66,"open_issues_count":2,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T05:07:00.293Z","etag":null,"topics":["deep-learning","go","gpu","libtorch","mlp","pytorch"],"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/lwch.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,"publiccode":null,"codemeta":null}},"created_at":"2023-06-08T05:43:25.000Z","updated_at":"2025-04-09T16:36:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"4715d482-58f0-46dd-bebf-4f6ccba29a34","html_url":"https://github.com/lwch/gotorch","commit_stats":{"total_commits":190,"total_committers":1,"mean_commits":190.0,"dds":0.0,"last_synced_commit":"b117637a14f60e32989037ff300cb17909c6ed5d"},"previous_names":["lwch/gotorch"],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lwch%2Fgotorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lwch%2Fgotorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lwch%2Fgotorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lwch%2Fgotorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lwch","download_url":"https://codeload.github.com/lwch/gotorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161273,"owners_count":21057555,"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","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":["deep-learning","go","gpu","libtorch","mlp","pytorch"],"created_at":"2024-09-24T13:43:56.700Z","updated_at":"2025-04-10T05:07:23.903Z","avatar_url":"https://github.com/lwch.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gotorch\n\n[![gotorch](https://github.com/lwch/gotorch/actions/workflows/cpu.yml/badge.svg)](https://github.com/lwch/gotorch/actions/workflows/cpu.yml)\n[![gotorch](https://github.com/lwch/gotorch/actions/workflows/gpu.yml/badge.svg)](https://github.com/lwch/gotorch/actions/workflows/gpu.yml)\n[![Go Reference](https://pkg.go.dev/badge/github.com/lwch/gotorch.svg)](https://pkg.go.dev/github.com/lwch/gotorch)\n\n这是一个GO版本的libtorch封装库，通过该库可快速搭建torch的模型，目前已支持最新版本的libtorch(2.0.1)，支持的操作系统如下\n\n- windows\n- linux\n- macos\n\n已支持*CPU*和*GPU*运算\n\n## 安装\n\n1. 下载[libtorch](https://pytorch.org/get-started/locally/)，windows下解压到D盘，linux和mac下解压到/usr/local/lib目录下\n2. 下载[libgotorch](https://github.com/lwch/gotorch/releases/latest)并放置在libtorch的lib目录下\n  - windows操作系统请更名为gotorch.dll\n  - linux操作系统请根据glibc版本下载对应so文件并更名为libgotorch.so\n  - macos最新版本仅支持arm64架构，下载后请更名为libgotorch.dylib\n\n注：由于官方提供的windows版本libtorch使用msvc进行编译，通过mingw无法正常链接，因此增加libgotorch库来进行转换，有关libgotorch库的编译请看[libgotorch编译](docs/libgotorch.md)，另外也可参考[release.yml](.github/workflows/release.yml)中的命令。\n\n### linux\n\n在.bashrc中添加以下内容\n\n```\nexport LIBRARY_PATH=\"$LIBRARY_PATH:/usr/local/lib/libtorch/lib\"\nexport LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH:/usr/local/lib/libtorch/lib\"\n```\n\n### macos\n\n在.bashrc中添加以下内容\n\n```\nexport LIBRARY_PATH=\"$LIBRARY_PATH:/usr/local/lib/libtorch/lib\"\nexport DYLD_FALLBACK_LIBRARY_PATH=\"$DYLD_FALLBACK_LIBRARY_PATH:/usr/local/lib/libtorch/lib\"\n```\n\n### windows\n\nwindows系统下使用cgo需要依赖mingw，推荐使用[llvm-mingw](https://github.com/mstorsjo/llvm-mingw)，并添加以下环境变量\n\n```\nLIBRARY_PATH=\"D:\\libtorch\\lib\"\nPath=\"D:\\libtorch\\lib;\u003cmingw所在路径\u003e\\bin\"\n```\n\n## 使用\n\n可查看[mlp](example/mlp)中的示例\n\n```go\na := tensor.ARange(nil, 6, consts.KFloat,\n    tensor.WithShape(2, 3),\n    tensor.WithDevice(consts.KCUDA))\nb := tensor.ARange(nil, 6, consts.KFloat,\n    tensor.WithShape(3, 2),\n    tensor.WithDevice(consts.KCUDA))\nc := a.MatMul(b)\nfmt.Println(c.ToDevice(consts.KCPU).Float32Value()) // 注意：显存中的数据无法直接读取，需将其转换到CPU后才可读取\n```\n\n**注意: 由于大部分tensor对象在C栈中创建，在go中无法正确捕获内存用量，因此建议在长期运行的服务中（如模型训练）使用debug.SetGCPercent将go的GC关闭并在每个迭代中手动调用runtime.GC进行内存释放**\n\n## 模型的checkpoint加载\n\n```go\nm, _ := model.Load(\"yolo_tiny.pt\", nil)\nfor name, t := m.Params() {\n    fmt.Println(name, t.Shapes())\n}\n```\n\n## 版本维护\n\n| gotorch版本 | libtorch版本 |\n| --- | --- |\n| v1.0.0~v1.5.7 | v2.0.1 |\n| v1.6.0~v1.7.2 | v2.1~v2.2.1 |\n| v1.7.3 | v2.2.2 |\n| v1.7.4~v1.8.0 | v2.3.1 |\n| v1.9.0~v1.9.2 | v2.4.x |\n| v1.9.3 | v2.5.x |\n| v1.9.4 | v2.6.x |","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flwch%2Fgotorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flwch%2Fgotorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flwch%2Fgotorch/lists"}