How can I use C-Go bindings in a different module/directory than the one I built them in?

huangapple go评论92阅读模式
英文:

How can I use C-Go bindings in a different module/directory than the one I built them in?

问题

我正在使用这些绑定。

根据文档,我执行以下命令:

git clone --recurse-submodules https://github.com/go-skynet/go-llama.cpp
cd go-llama.cpp
make libbinding.a

然后,在go-llama.cpp的子目录中运行示例脚本或任何其他脚本,可以使用以下命令成功运行:

LIBRARY_PATH=$PWD C_INCLUDE_PATH=$PWD go run ./examples -m "/model/path/here" -t 14

然而,当我尝试从不同的目录使用这些绑定时遇到了问题。
假设我有以下项目布局:

ParentDir \
--- go-llama.cpp \
--- --- <go-llama.cpp的内容>
--- project \
--- --- <引用绑定的Go代码>

如果我尝试在project目录中运行代码,会出现编译错误,找不到common.h。
我尝试过更改编译器标志(我怀疑你必须这样做),例如将=$PWD更改为=$(pwd)../go-llama.cpp,或者使用绝对路径~/tes/go-llama.cpp等。

我认为我的编译器标志没问题,因为当我在go-llama.cpp的更深层次的目录中更改标志以指向主go-llama.cpp目录时,我能够成功编译。

英文:

I'm using these bindings.

As per the docs, I

git clone --recurse-submodules https://github.com/go-skynet/go-llama.cpp
cd go-llama.cpp
make libbinding.a

and then running the example script, or any other script in a sub direcotory of go-llama.cpp works just fine with a command structured like:

LIBRARY_PATH=$PWD C_INCLUDE_PATH=$PWD go run ./examples -m &quot;/model/path/here&quot; -t 14

However, I run into problems trying to use these bindings from a different dir.
Say I have the following project layout:

ParentDir \
--- go-llama.cpp \
--- --- &lt;go-llama.cpp contents&gt;
--- project \
--- --- &lt;Go code which references bindings&gt;

If I try to run code in the project dir, I get a compilation error, common.h can't be found.
I've messed around with changing the compiler flags (which i suspect you have to do) like =$PWD to =$(pwd)../go-llama.cpp, and absolute ~/tes/go-llama.cpp, etc.

I think my compiler flags are fine, because I'm able to compile when testing with an even-more-nested dir within go-llama.cpp if I change the flags to point to the main go-llama.cpp dir from the relative location deep within the dirs.

答案1

得分: 1

我认为这是因为你没有从克隆的路径引用github.com/go-skynet/go-llama.cpp - 当go工具下载该包到$GOPATH/pkg/mod时,并没有执行--recurse-submodules操作。

你可以通过在项目中运行以下命令来查看该模块的路径:

go list -m -f '{{.Dir}}' github.com/go-skynet/go-llama.cpp

这应该会显示类似于以下内容:

$GOPATH/pkg/mod/github.com/go-skynet/go-llama.cpp@v0.0.0-20230509080828-f4d26f43f1d3

如果你在该目录中查看,你会发现llama.cpp目录(以及其下的examples目录)不存在。

你可以通过在项目目录中运行以下命令,编辑你的项目go.mod文件,将其引用到你本地克隆的go-llama.cpp存储库:

go mod edit -replace=github.com/go-skynet/go-llama.cpp=../go-llama.cpp/

我还没有尝试运行,因为我还没有下载模型,但是(在运行了go mod edit命令之后),我可以成功地使用以下命令构建代码:

go build

希望对你有帮助。

英文:

I think this is because you're not referencing github.com/go-skynet/go-llama.cpp from the path you cloned – the go tool has downloaded the package into $GOPATH/pkg/mod but not done the --recurse-submodules thing when it did that.

You can see the path being used for that module by running the following command from your project:

go list -m -f &#39;{{.Dir}}&#39; github.com/go-skynet/go-llama.cpp

That should show something like

$GOPATH/pkg/mod/github.com/go-skynet/go-llama.cpp@v0.0.0-20230509080828-f4d26f43f1d3

If you look in that directory, you'll see the llama.cpp directory (and examples directory under it) are not there.

You can edit your project go.mod to reference your locally-cloned copy of the go-llama.cpp repo by running the following command from your project directory:

go mod edit -replace=github.com/go-skynet/go-llama.cpp=../go-llama.cpp/

I haven't tried running as I haven't downloaded a model, but (after running that go mod edit command) I'm able to successfully build the code using just:

go build

HTH.

huangapple
  • 本文由 发表于 2023年5月3日 05:21:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76158963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定