在tensorflow里使用bazel调用外部的库

以调用机器上存在的openmpi的库为例。

  1. 首先在tensorflow 根目录下的WORKSPACE里定义好自己要引用的外部库:
    使用new_local_respository命令
new_local_repository(
name = "openmpi",
path = "/usr/local/openmpi",
build_file_content = """
cc_library(
name = "openmpi-lib",
srcs = ["lib/libmpi.so","lib/libmpi_mpifh.so"],
hdrs = glob(["include/*.h"]),
includes = ["include"],
visibility = ["//visibility:public"],
)
"""
)

然后在要使用的.cc对应的BUILD文件中的cc_library里添加好刚刚定义的依赖:

cc_library(
    name = "my_instruction_fusion",
    srcs = ["my_instruction_fusion.cc"],
    hdrs = ["my_instruction_fusion.h"],
    deps = [
        ":gpu_fusible",
        ":instruction_fusion",
        ":multi_output_fusion",
        "//tensorflow/compiler/xla/service:all_reduce_combiner",
        ":ir_emission_utils",
        "//tensorflow/compiler/xla:shape_util",
        "//tensorflow/compiler/xla:xla_data_proto_cc",
        "//tensorflow/compiler/xla/service:fusion_node_indexing_evaluation",
        "//tensorflow/compiler/xla/service:hlo",
        "//tensorflow/compiler/xla/service:hlo_query",
        "//tensorflow/compiler/xla/service:pattern_matcher",
        "//tensorflow/compiler/xla/service/llvm_ir:fused_ir_emitter",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "//tensorflow/compiler/xla:debug_options_flags",
        "//tensorflow/compiler/xla:statusor",
        "//tensorflow/compiler/xla/service:hlo_pass",
        "//tensorflow/compiler/xla/service:hlo_reachability",
        "//tensorflow/core:lib",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/strings",
        "@openmpi//:openmpi-lib",
        
    ],
)

你可能感兴趣的:(在tensorflow里使用bazel调用外部的库)