TensorFlow的protobuf版本兼容问题

当使用TensorFlow遇到protobuf版本冲突时,可以通过两种方法解决:1)重新安装与TensorFlow相同版本的protobuf;2)修改TensorFlow的workspace.bzl文件,指定所需protobuf版本并更新相关SHA256值。这里选择了第二种方法,将protobuf版本从3.8.0更新到3.9.0,同时处理protobuf的patch文件以适应新版本。确保在tensorflow镜像仓库中添加对应protobuf版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要解决版本不匹配报错:
This file was generated by an older version of protoc which is incompatible with your Protocol Buffer headers. Please regenerate this file with a newer version of protoc.

问题描述:

在使用tensorflow时,protoc版本不一致会报错,因为tensorflow-1.15.5使用的protobuf版本为3.8.0,但我们使用的其他依赖库,是基于3.9.0编译的,所以需要保持一致。

/home/svn/tensorflow/include/tensorflow/core/lib/core/error_codes.pb.h
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please
#error regenerate this file with a newer version of protoc.

TensorFlow 编译过程使用了自己的 Protocol Buffer版本,如果想将自己的 C++ 代码与 TensorFlow 生成的头文件混合使用,则需要使用完全相同的版本。
Please use the same version with tensorflow to recompile your proto files.

有两种解决方法:

1、重新安装protobuf,编译其他依赖时使用与TensorFlow完全相同的protobuf版本。
使用源码编译TensorFlow的话(build_all_linux.sh方式), Bazel会下载的对应的版本,在tensorflow/tensorflow/contrib/makefile/gen/protobuf/bin/protoc)。可以通过protoc –version查看版本号,然后把自己的protobuf版本也改成一样的(重新安装protobuf)
2、修改TensorFlow的原生依赖,编译tensorflow时使用自己的 protoc 版本生成TensorFlow的相关头文件。
TensorFlow 使用的库版本定义在 tensorflow/workspace.bzl,原则上只要它与 TensorFlow 和所有其他依赖项兼容,就应该可以生成 TensorFlow 的自定义构建。

具体解决步骤

这里采用第2种方法,具体操作如下:
TensorFlow使用的protobuf版本是从镜像仓库,或指定的官网地址下载的,tensorflow-1.15.5/tensorflow/workspace.bzl中定义了下载地址:https://storage.googleapis.com/mirror.tensorflow.org
但是这个地址不包含全部的protobuf版本,只包含了TensorFlow现有各版本对应的protobuf版本。

修改tensorflow/workspace.bzl 中对protobuf版本的指定,并计算相应的sha256,填入相应变量。
注意需要在tensorflow的镜像仓库里加上相应的protobuf版本,并保持在前。因为:

tf_http_archive(urls) must have redundant URLs. The mirror.tensorflow.org URL must be present and it must come first. Even if you don’t have permission to mirror the file, please put the correctly formatted mirror URL there anyway, because someone will come along shortly thereafter and mirror the file.

https://github.com/tensorflow/tensorflow/blob/v1.15.5/tensorflow/workspace.bzl

    # 310ba5ee72661c081129eb878c1bbcec936b20f0 is based on 3.8.0 with a fix for protobuf.bzl.
    PROTOBUF_URLS = [
        # "https://storage.googleapis.com/mirror.tensorflow.org/github.com/protocolbuffers/protobuf/archive/310ba5ee72661c081129eb878c1bbcec936b20f0.tar.gz",
        # "https://github.com/protocolbuffers/protobuf/archive/310ba5ee72661c081129eb878c1bbcec936b20f0.tar.gz",
        "https://github.com/protocolbuffers/protobuf/archive/v3.9.0.tar.gz",
    ]
    #PROTOBUF_SHA256 = "b9e92f9af8819bbbc514e2902aec860415b70209f31dfc8c4fa72515a5df9d59"
    PROTOBUF_SHA256 = "725730117ea1722de5e2f99fe0d9de1cd2c4e48ff2c298fc7a2d8cb4e48811ef"
    #PROTOBUF_STRIP_PREFIX = "protobuf-310ba5ee72661c081129eb878c1bbcec936b20f0"
    PROTOBUF_STRIP_PREFIX = "protobuf-3.9.0"

    # protobuf depends on @zlib, it has to be renamed to @zlib_archive because "zlib" is already
    # defined using bind for grpc.
    PROTOBUF_PATCH = "//third_party/protobuf:protobuf.patch"

    tf_http_archive(
        name = "com_google_protobuf",
        patch_file = clean_dep(PROTOBUF_PATCH),
        sha256 = PROTOBUF_SHA256,
        strip_prefix = PROTOBUF_STRIP_PREFIX,
        system_build_file = clean_dep("//third_party/systemlibs:protobuf.BUILD"),
        system_link_files = {
            "//third_party/systemlibs:protobuf.bzl": "protobuf.bzl",
        },
        urls = PROTOBUF_URLS,
    )

另外,tensorflow会根据使用情况对protobuf作出一些修改,这修改通过git的patch方式生效(/home/svn/tensorflow-1.15.5/third_party/protobuf/protobuf.patch)
下图是tensorflow对protobuf-3.8.0的两处修改。但我们要使用3.9.0,所以也要对此做相应修改,只保留第一处对zlib的重命名,因为3.9.0对第二处已作出更新。
修改protobuf的BUILD文件

参考:https://stackoverflow.com/questions/46235376/tensorflow-protobuf-version-mismatch

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值