首頁  >  文章  >  後端開發  >  張量流 Protobuf 中的導入週期

張量流 Protobuf 中的導入週期

王林
王林轉載
2024-02-06 11:09:07533瀏覽

张量流 Protobuf 中的导入周期

問題內容

我正在嘗試編寫客戶端程式碼來與張量流伺服器通訊。我需要為 tensorflowtensorflow_serving 編譯的 golang protobufs。這些都來之不易,我是透過這個才做到的。基本上,使用 buf 來產生它們。這是 buf yaml:

version: v1
managed:
  enabled: true
  optimize_for: code_size

  # go
  go_package_prefix:
    default: "some/path"

plugins:
  - plugin: buf.build/protocolbuffers/go
    out: gen/proto/go

運行成功,但運行應用程式日誌:

 package command-line-arguments
     imports my-package/internal/infer
     imports my-package/internal/infer/tensorflow_serving/apis
     imports my-package/internal/infer/tensorflow/core/protobuf
     imports my-package/internal/infer/tensorflow/compiler/xla/stream_executor
     imports my-package/internal/infer/tensorflow/compiler/xla
     imports my-package/internal/infer/tensorflow/compiler/xla/service
     imports my-package/internal/infer/tensorflow/compiler/xla: import cycle not allowed

請注意,tensorflowtensorflow_serving 下的所有內容都是直接從原始儲存庫編譯的。

令我驚訝的是,像張量流這樣廣泛使用的東西應該​​有一個導入週期,但也許確實如此。我該如何解決這個問題?


正確答案


tl;dr

根本原因是儲存庫 https://www.php.cn/link/1a16abf2a3149fc7cd6083687cce01c2 確實沒有正確組織原始檔案(或至少沒有使其對 go 友好)。

以下兩個檔案導致go中的導入迴圈(xla->xla/service->xla):

  • tensorflow/compiler/xla/xla.proto

    • #import "tensorflow/compiler/xla/service/hlo.proto"
  • tensorflow/compiler/xla/service/hlo.proto

    • #import "tensorflow/compiler/xla/xla_data.proto"

#由於 xla_data.proto 不導入任何其他文件,我們可以將其移動到自己的包中以打破導入循環。我們可以利用 buf 的 覆蓋功能做這個。這是最終的 buf.gen.yaml 檔案:

version: v1
managed:
  enabled: true
  go_package_prefix:
    default: example.com/mymodule/internal
  override:
    go_package:
      # move the generated xla_data.pb.go file into package xla/data to break the import cycle.
      tensorflow/compiler/xla/xla_data.proto: 'example.com/mymodule/internal/tensorflow/compiler/xla/data'
plugins:
  - name: go
    out: internal
    opt:
      - module=example.com/mymodule/internal

  - name: go-grpc
    out: internal
    opt:
      - module=example.com/mymodule/internal

使用 buf 編譯 tensorflow proto 檔案的完整設定

這是最終的目錄結構:

├── buf.gen.yaml
├── buf.work.yaml
├── buf.yaml
├── go.mod
├── go.sum
├── internal
│   ├── tensorflow
│   └── tensorflow_serving
└── testdata
    ├── serving
    └── tensorflow

buf.gen.yaml:請參閱「tl;dr」部分。

buf.work.yaml

version: v1
directories:
  - testdata/serving
  - testdata/tensorflow

buf.yaml

version: v1
breaking:
  use:
    - file
lint:
  use:
    - default

這是我的環境:

$ go version
go version go1.20.3 linux/amd64
$ buf --version
1.17.0
$ protoc --version
libprotoc 3.12.4
$ protoc-gen-go --version
protoc-gen-go v1.30.0
$ protoc-gen-go-grpc --version
protoc-gen-go-grpc 1.3.0
$ git version
git version 2.37.2

現在在此目錄的根目錄中執行以下命令:

$ go mod init example.com/mymodule
$ go get google.golang.org/grpc
$ git clone https://www.php.cn/link/1a16abf2a3149fc7cd6083687cce01c2.git testdata/tensorflow
$ git clone https://github.com/tensorflow/serving.git testdata/serving
$ buf generate
$ go build ./...

註解

  1. tensorflow 儲存庫被複製到 testdata 目錄中,以便 go build 將忽略它們。
  2. 安裝程式會在 internal 目錄中產生檔案。您可以修改 buf.gen.yaml 檔案以將它們放置在您想要的任何位置。
  3. go build ./... 不會回報任何錯誤。但我不確定生成的文件是否有效。

以上是張量流 Protobuf 中的導入週期的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除