如何使用CLION或NETBEAN正确导入TensorFlow源代码

How to import Tensorflow source codes correctly with Clion or Netbeans

本文关键字:导入 TensorFlow 源代码 NETBEAN 何使用 CLION      更新时间:2023-10-16

i打算读取Tensorflow(TF(源代码的核心模块

我的问题是我没有阅读诸如IDE中TF之类的C/C 源代码的经验。谁能给我一些有关如何在IDE中有效读取TF源代码(核心模块(的说明。我在MacBook上有 CLION和NETBEANS ,但我不知道如何正确导入TF(也要导入哪一部分?;如何构建它?(,以至于当我想知道声明的声明时一个C 类我可以直接跳入其签名/声明。

我将感谢有效阅读TF源代码的任何建议/推荐工具。顺便说一句,我假设使用IDE读取TF代码是有效的。如果不是真的,我可以停止使用它们,然后转向Vim。

原始Tensorflow存储库(GitHub(不包含任何特定IDE的项目信息文件,这意味着您不能仅导入整个项目Project文件夹(Atom,Visual Studio代码,崇高等(建议使用其中之一,如果您的目标只是在代码库中读取导航。

不幸的是,您将无法与任何这些编辑者一起构建代码。TensorFlow使用Bazel作为其构建工具,目前具有Eclipse和Xcode的支持。老实说,我不确定您是否可以在其中一个ID中导入代码库。

我解决了这样做的问题:

  1. 在TF回购中创建CMakelists.txt文件。LS -LA输出应该像这样:
tensorflow
third-party
tools
CMakeLists.txt
some
other
files
  1. cmakelists.txt应该看起来像:

注意!!!这不是一个完整的文件,因为完整的文件应长800多行。这只是您获得这个想法的一个例子。您可以在此处获取完整的文件:

https://github.com/oleg-ostanin/tf_related_staff/blob/master/cmakelists.txt

,但下周可能会过时,所以甚至不要打扰。

cmake_minimum_required(VERSION 3.14)
project(tensorflow)
set(CMAKE_CXX_STANDARD 14)
# git clone https://github.com/abseil/abseil-cpp.git
include_directories(/home/oostanin/tf_related_repos/abseil-cpp)
# git clone https://github.com/protocolbuffers/protobuf.git
include_directories(/home/oostanin/tf_related_repos/protobuf/src/)
include_directories(/home/oostanin/tensorflow)
# you can get this directory only by building TF from sources using Bazel
include_directories(/home/oostanin/tensorflow/bazel-genfiles)
file(GLOB tensorflow_contrib_tensorrt_shape_fn "${CMAKE_SOURCE_DIR}/tensorflow/contrib/tensorrt/shape_fn/*.cc")
file(GLOB tensorflow_compiler_xla_service_llvm_ir "${CMAKE_SOURCE_DIR}/tensorflow/compiler/xla/service/llvm_ir/*.cc")
file(GLOB tensorflow_lite_delegates_gpu_gl "${CMAKE_SOURCE_DIR}/tensorflow/lite/delegates/gpu/gl/*.cc")
# here should be much more lines
add_library(
        tensorflow
        SHARED
        ${tensorflow_contrib_tensorrt_shape_fn}
        ${tensorflow_compiler_xla_service_llvm_ir}
        ${tensorflow_lite_delegates_gpu_gl}
        # here should be much more lines too
                )
target_link_libraries(
        tensorflow
        )

一些解释:

tf源文件取决于许多其他项目,因此您需要克隆并将这些项目放在附近的某个地方,并告诉您的cmake在哪里找到它们。这就是这两行的作用:

# git clone https://github.com/abseil/abseil-cpp.git
include_directories(/home/oostanin/tf_related_repos/abseil-cpp)
# git clone https://github.com/protocolbuffers/protobuf.git
include_directories(/home/oostanin/tf_related_repos/protobuf/src/)

可能有2个以上的存储库,但这会让您入门。

tf源文件依赖于编译的Protobuf标头,这对于我编译它们的最简单方法是使用Bazel从源构建TF的最简单方法。这是另一个痛苦的故事,但我做到了,我相信您也可以做到。您应该告诉您的CMAKE在哪里可以找到那些编译的Protobuf标头。这就是该行的作用:

include_directories(/home/oostanin/tensorflow/bazel-genfiles)

如果您无法使用Bazel构建TF,只需跳过该部分,您将看到不满意的更多红色错误,但是您将能够阅读和导航大多数代码。

您应该告诉Cmake在哪里可以找到TF源文件,对我来说最简单的方法是生成300多行的长列表,其中包含.cc文件的所有目录:

file(GLOB tensorflow_contrib_tensorrt_shape_fn "${CMAKE_SOURCE_DIR}/tensorflow/contrib/tensorrt/shape_fn/*.cc")
file(GLOB tensorflow_compiler_xla_service_llvm_ir "${CMAKE_SOURCE_DIR}/tensorflow/compiler/xla/service/llvm_ir/*.cc")
file(GLOB tensorflow_lite_delegates_gpu_gl "${CMAKE_SOURCE_DIR}/tensorflow/lite/delegates/gpu/gl/*.cc")
# here should be much more lines

然后生成另一个300多行的长列表,将它们包含在这样的来源:

add_library(
        tensorflow
        SHARED
        ${tensorflow_contrib_tensorrt_shape_fn}
        ${tensorflow_compiler_xla_service_llvm_ir}
        ${tensorflow_lite_delegates_gpu_gl}
        # here should be much more lines too
                )

您无法使用cmakelists.txt构建TF,甚至不要考虑它。但是,这将使您可以将TF源代码导入CLION,并读取和导航和编辑比使用VIM要容易得多。