tensorflow_ops.bzl 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. # -*- Python -*-
  2. # Given a source file, generate a test name.
  3. # i.e. "common_runtime/direct_session_test.cc" becomes
  4. # "common_runtime_direct_session_test"
  5. def src_to_test_name(src):
  6. return src.replace("/", "_").split(".")[0]
  7. # Return the options to use for a C++ library or binary build.
  8. # Uses the ":optmode" config_setting to pick the options.
  9. load(
  10. "@org_tensorflow//tensorflow/core:platform/default/build_config_root.bzl",
  11. "tf_cuda_tests_tags",
  12. "tf_sycl_tests_tags",
  13. )
  14. load(
  15. "@local_config_cuda//cuda:build_defs.bzl",
  16. "if_cuda",
  17. "cuda_default_copts"
  18. )
  19. # List of proto files for android builds
  20. def tf_android_core_proto_sources(core_proto_sources_relative):
  21. return ["@org_tensorflow//tensorflow/core:" + p
  22. for p in core_proto_sources_relative]
  23. # Returns the list of pb.h and proto.h headers that are generated for
  24. # tf_android_core_proto_sources().
  25. def tf_android_core_proto_headers(core_proto_sources_relative):
  26. return (["@org_tensorflow//tensorflow/core/" + p.replace(".proto", ".pb.h")
  27. for p in core_proto_sources_relative] +
  28. ["@org_tensorflow//tensorflow/core/" + p.replace(".proto", ".proto.h")
  29. for p in core_proto_sources_relative])
  30. def if_android_arm(a):
  31. return select({
  32. "@org_tensorflow//tensorflow:android_arm": a,
  33. "//conditions:default": [],
  34. })
  35. def if_android_arm64(a):
  36. return select({
  37. "@org_tensorflow//tensorflow:android_arm64": a,
  38. "//conditions:default": [],
  39. })
  40. def if_not_android(a):
  41. return select({
  42. "@org_tensorflow//tensorflow:android": [],
  43. "//conditions:default": a,
  44. })
  45. def if_android(a):
  46. return select({
  47. "@org_tensorflow//tensorflow:android": a,
  48. "//conditions:default": [],
  49. })
  50. def if_ios(a):
  51. return select({
  52. "@org_tensorflow//tensorflow:ios": a,
  53. "//conditions:default": [],
  54. })
  55. def if_mobile(a):
  56. return select({
  57. "@org_tensorflow//tensorflow:android": a,
  58. "@org_tensorflow//tensorflow:ios": a,
  59. "//conditions:default": [],
  60. })
  61. def if_not_mobile(a):
  62. return select({
  63. "@org_tensorflow//tensorflow:android": [],
  64. "@org_tensorflow//tensorflow:ios": [],
  65. "//conditions:default": a,
  66. })
  67. def if_not_windows(a):
  68. return select({
  69. "@org_tensorflow//tensorflow:windows": [],
  70. "//conditions:default": a,
  71. })
  72. def if_x86(a):
  73. return select({
  74. "@org_tensorflow//tensorflow:linux_x86_64": a,
  75. "@org_tensorflow//tensorflow:windows": a,
  76. "//conditions:default": [],
  77. })
  78. def tf_copts():
  79. return (["-DEIGEN_AVOID_STL_ARRAY",
  80. "-Iexternal/gemmlowp",
  81. "-Wno-sign-compare",
  82. "-fno-exceptions",] +
  83. if_cuda(["-DGOOGLE_CUDA=1"]) +
  84. if_android_arm(["-mfpu=neon"]) +
  85. select({
  86. "@org_tensorflow//tensorflow:android": [
  87. "-std=c++11",
  88. "-DTF_LEAN_BINARY",
  89. "-O2",
  90. ],
  91. "@org_tensorflow//tensorflow:darwin": [],
  92. "@org_tensorflow//tensorflow:windows": [
  93. "/DLANG_CXX11",
  94. "/D__VERSION__=\\\"MSVC\\\"",
  95. "/DPLATFORM_WINDOWS",
  96. "/DEIGEN_HAS_C99_MATH",
  97. "/DTENSORFLOW_USE_EIGEN_THREADPOOL",
  98. ],
  99. "@org_tensorflow//tensorflow:ios": ["-std=c++11"],
  100. "//conditions:default": ["-pthread"]}))
  101. def tf_opts_nortti_if_android():
  102. return if_android([
  103. "-fno-rtti",
  104. "-DGOOGLE_PROTOBUF_NO_RTTI",
  105. "-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER",
  106. ])
  107. # Given a list of "op_lib_names" (a list of files in the ops directory
  108. # without their .cc extensions), generate a library for that file.
  109. def tf_gen_op_libs(op_lib_names, deps=None):
  110. # Make library out of each op so it can also be used to generate wrappers
  111. # for various languages.
  112. if not deps:
  113. deps = []
  114. for n in op_lib_names:
  115. native.cc_library(name=n + "_op_lib",
  116. copts=tf_copts(),
  117. srcs=["ops/" + n + ".cc"],
  118. deps=deps + ["@org_tensorflow//tensorflow/core:framework"],
  119. visibility=["//visibility:public"],
  120. alwayslink=1,
  121. linkstatic=1,)
  122. def tf_gen_op_wrapper_cc(name, out_ops_file, pkg="",
  123. op_gen="@org_tensorflow//tensorflow/cc:cc_op_gen_main",
  124. deps=None,
  125. override_file=None,
  126. include_internal_ops=0):
  127. # Construct an op generator binary for these ops.
  128. tool = out_ops_file + "_gen_cc"
  129. if deps == None:
  130. deps = [pkg + ":" + name + "_op_lib"]
  131. native.cc_binary(
  132. name = tool,
  133. copts = tf_copts(),
  134. linkopts = ["-lm"],
  135. linkstatic = 1, # Faster to link this one-time-use binary dynamically
  136. deps = [op_gen] + deps
  137. )
  138. if override_file == None:
  139. srcs = []
  140. override_arg = ","
  141. else:
  142. srcs = [override_file]
  143. override_arg = "$(location " + override_file + ")"
  144. native.genrule(
  145. name=name + "_genrule",
  146. outs=[out_ops_file + ".h", out_ops_file + ".cc",
  147. out_ops_file + "_internal.h", out_ops_file + "_internal.cc"],
  148. srcs=srcs,
  149. tools=[":" + tool],
  150. cmd=("$(location :" + tool + ") $(location :" + out_ops_file + ".h) " +
  151. "$(location :" + out_ops_file + ".cc) " + override_arg + " " +
  152. str(include_internal_ops)))
  153. # Given a list of "op_lib_names" (a list of files in the ops directory
  154. # without their .cc extensions), generate individual C++ .cc and .h
  155. # files for each of the ops files mentioned, and then generate a
  156. # single cc_library called "name" that combines all the
  157. # generated C++ code.
  158. #
  159. # For example, for:
  160. # tf_gen_op_wrappers_cc("tf_ops_lib", [ "array_ops", "math_ops" ])
  161. #
  162. #
  163. # This will ultimately generate ops/* files and a library like:
  164. #
  165. # cc_library(name = "tf_ops_lib",
  166. # srcs = [ "ops/array_ops.cc",
  167. # "ops/math_ops.cc" ],
  168. # hdrs = [ "ops/array_ops.h",
  169. # "ops/math_ops.h" ],
  170. # deps = [ ... ])
  171. #
  172. # Plus a private library for the "hidden" ops.
  173. # cc_library(name = "tf_ops_lib_internal",
  174. # srcs = [ "ops/array_ops_internal.cc",
  175. # "ops/math_ops_internal.cc" ],
  176. # hdrs = [ "ops/array_ops_internal.h",
  177. # "ops/math_ops_internal.h" ],
  178. # deps = [ ... ])
  179. # TODO(googleuser): Cleaner approach for hidden ops.
  180. def tf_gen_op_wrappers_cc(name,
  181. op_lib_names=[],
  182. other_srcs=[],
  183. other_hdrs=[],
  184. pkg="",
  185. deps=[
  186. "@org_tensorflow//tensorflow/cc:ops",
  187. "@org_tensorflow//tensorflow/cc:scope",
  188. "@org_tensorflow//tensorflow/cc:const_op",
  189. ],
  190. op_gen="@org_tensorflow//tensorflow/cc:cc_op_gen_main",
  191. override_file=None,
  192. include_internal_ops=0,
  193. visibility=None):
  194. subsrcs = other_srcs
  195. subhdrs = other_hdrs
  196. internalsrcs = []
  197. internalhdrs = []
  198. for n in op_lib_names:
  199. tf_gen_op_wrapper_cc(
  200. n, "ops/" + n, pkg=pkg, op_gen=op_gen, override_file=override_file,
  201. include_internal_ops=include_internal_ops)
  202. subsrcs += ["ops/" + n + ".cc"]
  203. subhdrs += ["ops/" + n + ".h"]
  204. internalsrcs += ["ops/" + n + "_internal.cc"]
  205. internalhdrs += ["ops/" + n + "_internal.h"]
  206. native.cc_library(name=name,
  207. srcs=subsrcs,
  208. hdrs=subhdrs,
  209. deps=deps + if_not_android([
  210. "@org_tensorflow//tensorflow/core:core_cpu",
  211. "@org_tensorflow//tensorflow/core:framework",
  212. "@org_tensorflow//tensorflow/core:lib",
  213. "@org_tensorflow//tensorflow/core:protos_all_cc",
  214. ]) + if_android([
  215. "@org_tensorflow//tensorflow/core:android_tensorflow_lib",
  216. ]),
  217. copts=tf_copts(),
  218. alwayslink=1,
  219. visibility=visibility)
  220. native.cc_library(name=name + "_internal",
  221. srcs=internalsrcs,
  222. hdrs=internalhdrs,
  223. deps=deps + if_not_android([
  224. "@org_tensorflow//tensorflow/core:core_cpu",
  225. "@org_tensorflow//tensorflow/core:framework",
  226. "@org_tensorflow//tensorflow/core:lib",
  227. "@org_tensorflow//tensorflow/core:protos_all_cc",
  228. ]) + if_android([
  229. "@org_tensorflow//tensorflow/core:android_tensorflow_lib",
  230. ]),
  231. copts=tf_copts(),
  232. alwayslink=1,
  233. visibility=["@org_tensorflow//tensorflow:internal"])
  234. # Invoke this rule in .../tensorflow/python to build the wrapper library.
  235. def tf_gen_op_wrapper_py(name, out=None, hidden=None, visibility=None, deps=[],
  236. require_shape_functions=False, hidden_file=None,
  237. generated_target_name=None):
  238. # Construct a cc_binary containing the specified ops.
  239. tool_name = "gen_" + name + "_py_wrappers_cc"
  240. if not deps:
  241. deps = ["@org_tensorflow//tensorflow/core:" + name + "_op_lib"]
  242. native.cc_binary(
  243. name = tool_name,
  244. linkopts = ["-lm"],
  245. copts = tf_copts(),
  246. linkstatic = 1, # Faster to link this one-time-use binary dynamically
  247. deps = (["@org_tensorflow//tensorflow/core:framework",
  248. "@org_tensorflow//tensorflow/python:python_op_gen_main"] + deps),
  249. visibility = ["@org_tensorflow//tensorflow:internal"],
  250. )
  251. # Invoke the previous cc_binary to generate a python file.
  252. if not out:
  253. out = "ops/gen_" + name + ".py"
  254. if hidden:
  255. # `hidden` is a list of op names to be hidden in the generated module.
  256. native.genrule(
  257. name=name + "_pygenrule",
  258. outs=[out],
  259. tools=[tool_name],
  260. cmd=("$(location " + tool_name + ") " + ",".join(hidden)
  261. + " " + ("1" if require_shape_functions else "0") + " > $@"))
  262. elif hidden_file:
  263. # `hidden_file` is file containing a list of op names to be hidden in the
  264. # generated module.
  265. native.genrule(
  266. name=name + "_pygenrule",
  267. outs=[out],
  268. srcs=[hidden_file],
  269. tools=[tool_name],
  270. cmd=("$(location " + tool_name + ") @$(location "
  271. + hidden_file + ") " + ("1" if require_shape_functions else "0")
  272. + " > $@"))
  273. else:
  274. # No ops should be hidden in the generated module.
  275. native.genrule(
  276. name=name + "_pygenrule",
  277. outs=[out],
  278. tools=[tool_name],
  279. cmd=("$(location " + tool_name + ") "
  280. + ("1" if require_shape_functions else "0") + " > $@"))
  281. # Make a py_library out of the generated python file.
  282. if not generated_target_name:
  283. generated_target_name = name
  284. native.py_library(name=generated_target_name,
  285. srcs=[out],
  286. srcs_version="PY2AND3",
  287. visibility=visibility,
  288. deps=[
  289. "@org_tensorflow//tensorflow/python:framework_for_generated_wrappers",
  290. ],)
  291. # Define a bazel macro that creates cc_test for tensorflow.
  292. # TODO(googleuser): we need to enable this to work around the hidden symbol
  293. # __cudaRegisterFatBinary error. Need more investigations.
  294. def tf_cc_test(name, srcs, deps, linkstatic=0, tags=[], data=[], size="medium",
  295. suffix="", args=None, linkopts=[]):
  296. native.cc_test(name="%s%s" % (name, suffix),
  297. srcs=srcs,
  298. size=size,
  299. args=args,
  300. copts=tf_copts(),
  301. data=data,
  302. deps=deps,
  303. linkopts=["-lpthread", "-lm"] + linkopts,
  304. linkstatic=linkstatic,
  305. tags=tags)
  306. # Part of the testing workflow requires a distinguishable name for the build
  307. # rules that involve a GPU, even if otherwise identical to the base rule.
  308. def tf_cc_test_gpu(name, srcs, deps, linkstatic=0, tags=[], data=[],
  309. size="medium", suffix="", args=None):
  310. tf_cc_test(name, srcs, deps, linkstatic=linkstatic, tags=tags, data=data,
  311. size=size, suffix=suffix, args=args)
  312. def tf_cuda_cc_test(name, srcs=[], deps=[], tags=[], data=[], size="medium",
  313. linkstatic=0, args=[], linkopts=[]):
  314. tf_cc_test(name=name,
  315. srcs=srcs,
  316. deps=deps,
  317. tags=tags + ["manual"],
  318. data=data,
  319. size=size,
  320. linkstatic=linkstatic,
  321. linkopts=linkopts,
  322. args=args)
  323. tf_cc_test(name=name,
  324. srcs=srcs,
  325. suffix="_gpu",
  326. deps=deps + if_cuda(["@org_tensorflow//tensorflow/core:gpu_runtime"]),
  327. linkstatic=if_cuda(1, 0),
  328. tags=tags + tf_cuda_tests_tags(),
  329. data=data,
  330. size=size,
  331. linkopts=linkopts,
  332. args=args)
  333. # Create a cc_test for each of the tensorflow tests listed in "tests"
  334. def tf_cc_tests(srcs, deps, name='', linkstatic=0, tags=[], size="medium",
  335. args=None, linkopts=[]):
  336. for src in srcs:
  337. tf_cc_test(
  338. name=src_to_test_name(src),
  339. srcs=[src],
  340. deps=deps,
  341. linkstatic=linkstatic,
  342. tags=tags,
  343. size=size,
  344. args=args,
  345. linkopts=linkopts)
  346. def tf_cc_tests_gpu(srcs, deps, name='', linkstatic=0, tags=[], size="medium",
  347. args=None):
  348. tf_cc_tests(srcs, deps, linkstatic, tags=tags, size=size, args=args)
  349. def tf_cuda_cc_tests(srcs, deps, name='', tags=[], size="medium", linkstatic=0,
  350. args=None, linkopts=[]):
  351. for src in srcs:
  352. tf_cuda_cc_test(
  353. name=src_to_test_name(src),
  354. srcs=[src],
  355. deps=deps,
  356. tags=tags,
  357. size=size,
  358. linkstatic=linkstatic,
  359. args=args,
  360. linkopts=linkopts)
  361. def _cuda_copts():
  362. """Gets the appropriate set of copts for (maybe) CUDA compilation.
  363. If we're doing CUDA compilation, returns copts for our particular CUDA
  364. compiler. If we're not doing CUDA compilation, returns an empty list.
  365. """
  366. return cuda_default_copts() + select({
  367. "//conditions:default": [],
  368. "@local_config_cuda//cuda:using_nvcc": (
  369. [
  370. "-nvcc_options=relaxed-constexpr",
  371. "-nvcc_options=ftz=true",
  372. ]
  373. ),
  374. "@local_config_cuda//cuda:using_clang": (
  375. [
  376. "-fcuda-flush-denormals-to-zero",
  377. ]
  378. ),
  379. })
  380. # Build defs for TensorFlow kernels
  381. # When this target is built using --config=cuda, a cc_library is built
  382. # that passes -DGOOGLE_CUDA=1 and '-x cuda', linking in additional
  383. # libraries needed by GPU kernels.
  384. def tf_gpu_kernel_library(srcs, copts=[], cuda_copts=[], deps=[], hdrs=[],
  385. **kwargs):
  386. copts = copts + _cuda_copts() + if_cuda(cuda_copts) + tf_copts()
  387. native.cc_library(
  388. srcs = srcs,
  389. hdrs = hdrs,
  390. copts = copts,
  391. deps = deps + if_cuda([
  392. "@org_tensorflow//tensorflow/core:cuda",
  393. "@org_tensorflow//tensorflow/core:gpu_lib",
  394. ]),
  395. alwayslink=1,
  396. **kwargs)
  397. def tf_cuda_library(deps=None, cuda_deps=None, copts=None, **kwargs):
  398. """Generate a cc_library with a conditional set of CUDA dependencies.
  399. When the library is built with --config=cuda:
  400. - both deps and cuda_deps are used as dependencies
  401. - the cuda runtime is added as a dependency (if necessary)
  402. - The library additionally passes -DGOOGLE_CUDA=1 to the list of copts
  403. Args:
  404. - cuda_deps: BUILD dependencies which will be linked if and only if:
  405. '--config=cuda' is passed to the bazel command line.
  406. - deps: dependencies which will always be linked.
  407. - copts: copts always passed to the cc_library.
  408. - kwargs: Any other argument to cc_library.
  409. """
  410. if not deps:
  411. deps = []
  412. if not cuda_deps:
  413. cuda_deps = []
  414. if not copts:
  415. copts = []
  416. native.cc_library(
  417. deps = deps + if_cuda(cuda_deps + [
  418. "@org_tensorflow//tensorflow/core:cuda",
  419. "@local_config_cuda//cuda:cuda_headers"
  420. ]),
  421. copts = copts + if_cuda(["-DGOOGLE_CUDA=1"]),
  422. **kwargs)
  423. def tf_kernel_library(name, prefix=None, srcs=None, gpu_srcs=None, hdrs=None,
  424. deps=None, alwayslink=1, copts=tf_copts(), **kwargs):
  425. """A rule to build a TensorFlow OpKernel.
  426. May either specify srcs/hdrs or prefix. Similar to tf_cuda_library,
  427. but with alwayslink=1 by default. If prefix is specified:
  428. * prefix*.cc (except *.cu.cc) is added to srcs
  429. * prefix*.h (except *.cu.h) is added to hdrs
  430. * prefix*.cu.cc and prefix*.h (including *.cu.h) are added to gpu_srcs.
  431. With the exception that test files are excluded.
  432. For example, with prefix = "cast_op",
  433. * srcs = ["cast_op.cc"]
  434. * hdrs = ["cast_op.h"]
  435. * gpu_srcs = ["cast_op_gpu.cu.cc", "cast_op.h"]
  436. * "cast_op_test.cc" is excluded
  437. With prefix = "cwise_op"
  438. * srcs = ["cwise_op_abs.cc", ..., "cwise_op_tanh.cc"],
  439. * hdrs = ["cwise_ops.h", "cwise_ops_common.h"],
  440. * gpu_srcs = ["cwise_op_gpu_abs.cu.cc", ..., "cwise_op_gpu_tanh.cu.cc",
  441. "cwise_ops.h", "cwise_ops_common.h",
  442. "cwise_ops_gpu_common.cu.h"]
  443. * "cwise_ops_test.cc" is excluded
  444. """
  445. if not srcs:
  446. srcs = []
  447. if not hdrs:
  448. hdrs = []
  449. if not deps:
  450. deps = []
  451. if prefix:
  452. if native.glob([prefix + "*.cu.cc"], exclude = ["*test*"]):
  453. if not gpu_srcs:
  454. gpu_srcs = []
  455. gpu_srcs = gpu_srcs + native.glob([prefix + "*.cu.cc", prefix + "*.h"],
  456. exclude = ["*test*"])
  457. srcs = srcs + native.glob([prefix + "*.cc"],
  458. exclude = ["*test*", "*.cu.cc"])
  459. hdrs = hdrs + native.glob([prefix + "*.h"], exclude = ["*test*", "*.cu.h"])
  460. cuda_deps = ["@org_tensorflow//tensorflow/core:gpu_lib"]
  461. if gpu_srcs:
  462. for gpu_src in gpu_srcs:
  463. if gpu_src.endswith(".cc") and not gpu_src.endswith(".cu.cc"):
  464. fail("{} not allowed in gpu_srcs. .cc sources must end with .cu.cc".format(gpu_src))
  465. tf_gpu_kernel_library(
  466. name = name + "_gpu",
  467. srcs = gpu_srcs,
  468. deps = deps,
  469. **kwargs)
  470. cuda_deps.extend([":" + name + "_gpu"])
  471. tf_cuda_library(
  472. name = name,
  473. srcs = srcs,
  474. hdrs = hdrs,
  475. copts = copts,
  476. cuda_deps = cuda_deps,
  477. linkstatic = 1, # Needed since alwayslink is broken in bazel b/27630669
  478. alwayslink = alwayslink,
  479. deps = deps,
  480. **kwargs)
  481. # Bazel rules for building swig files.
  482. def _py_wrap_cc_impl(ctx):
  483. srcs = ctx.files.srcs
  484. if len(srcs) != 1:
  485. fail("Exactly one SWIG source file label must be specified.", "srcs")
  486. module_name = ctx.attr.module_name
  487. src = ctx.files.srcs[0]
  488. inputs = set([src])
  489. inputs += ctx.files.swig_includes
  490. for dep in ctx.attr.deps:
  491. inputs += dep.cc.transitive_headers
  492. inputs += ctx.files._swiglib
  493. inputs += ctx.files.toolchain_deps
  494. swig_include_dirs = set(_get_repository_roots(ctx, inputs))
  495. swig_include_dirs += sorted([f.dirname for f in ctx.files._swiglib])
  496. args = ["-c++",
  497. "-python",
  498. "-module", module_name,
  499. "-o", ctx.outputs.cc_out.path,
  500. "-outdir", ctx.outputs.py_out.dirname]
  501. args += ["-l" + f.path for f in ctx.files.swig_includes]
  502. args += ["-I" + i for i in swig_include_dirs]
  503. args += [src.path]
  504. outputs = [ctx.outputs.cc_out,
  505. ctx.outputs.py_out]
  506. ctx.action(executable=ctx.executable._swig,
  507. arguments=args,
  508. inputs=list(inputs),
  509. outputs=outputs,
  510. mnemonic="PythonSwig",
  511. progress_message="SWIGing " + src.path)
  512. return struct(files=set(outputs))
  513. _py_wrap_cc = rule(
  514. attrs = {
  515. "srcs": attr.label_list(
  516. mandatory = True,
  517. allow_files = True,
  518. ),
  519. "swig_includes": attr.label_list(
  520. cfg = "data",
  521. allow_files = True,
  522. ),
  523. "deps": attr.label_list(
  524. allow_files = True,
  525. providers = ["cc"],
  526. ),
  527. "toolchain_deps": attr.label_list(
  528. allow_files = True,
  529. ),
  530. "module_name": attr.string(mandatory = True),
  531. "py_module_name": attr.string(mandatory = True),
  532. "_swig": attr.label(
  533. default = Label("@swig//:swig"),
  534. executable = True,
  535. cfg = "host",
  536. ),
  537. "_swiglib": attr.label(
  538. default = Label("@swig//:templates"),
  539. allow_files = True,
  540. ),
  541. },
  542. outputs = {
  543. "cc_out": "%{module_name}.cc",
  544. "py_out": "%{py_module_name}.py",
  545. },
  546. implementation = _py_wrap_cc_impl,
  547. )
  548. def _get_repository_roots(ctx, files):
  549. """Returns abnormal root directories under which files reside.
  550. When running a ctx.action, source files within the main repository are all
  551. relative to the current directory; however, files that are generated or exist
  552. in remote repositories will have their root directory be a subdirectory,
  553. e.g. bazel-out/local-fastbuild/genfiles/external/jpeg_archive. This function
  554. returns the set of these devious directories, ranked and sorted by popularity
  555. in order to hopefully minimize the number of I/O system calls within the
  556. compiler, because includes have quadratic complexity.
  557. """
  558. result = {}
  559. for f in files:
  560. root = f.root.path
  561. if root:
  562. if root not in result:
  563. result[root] = 0
  564. result[root] -= 1
  565. work = f.owner.workspace_root
  566. if work:
  567. if root:
  568. root += "/"
  569. root += work
  570. if root:
  571. if root not in result:
  572. result[root] = 0
  573. result[root] -= 1
  574. return [k for v, k in sorted([(v, k) for k, v in result.items()])]
  575. # Bazel rule for collecting the header files that a target depends on.
  576. def _transitive_hdrs_impl(ctx):
  577. outputs = set()
  578. for dep in ctx.attr.deps:
  579. outputs += dep.cc.transitive_headers
  580. return struct(files=outputs)
  581. _transitive_hdrs = rule(
  582. attrs = {
  583. "deps": attr.label_list(
  584. allow_files = True,
  585. providers = ["cc"],
  586. ),
  587. },
  588. implementation = _transitive_hdrs_impl,
  589. )
  590. def transitive_hdrs(name, deps=[], **kwargs):
  591. _transitive_hdrs(name=name + "_gather",
  592. deps=deps)
  593. native.filegroup(name=name,
  594. srcs=[":" + name + "_gather"])
  595. # Create a header only library that includes all the headers exported by
  596. # the libraries in deps.
  597. def cc_header_only_library(name, deps=[], **kwargs):
  598. _transitive_hdrs(name=name + "_gather",
  599. deps=deps)
  600. native.cc_library(name=name,
  601. hdrs=[":" + name + "_gather"],
  602. **kwargs)
  603. def tf_custom_op_library_additional_deps():
  604. return [
  605. "@protobuf//:protobuf",
  606. "//third_party/eigen3",
  607. "@org_tensorflow//tensorflow/core:framework_headers_lib",
  608. ]
  609. # Traverse the dependency graph along the "deps" attribute of the
  610. # target and return a struct with one field called 'tf_collected_deps'.
  611. # tf_collected_deps will be the union of the deps of the current target
  612. # and the tf_collected_deps of the dependencies of this target.
  613. def _collect_deps_aspect_impl(target, ctx):
  614. alldeps = set()
  615. if hasattr(ctx.rule.attr, "deps"):
  616. for dep in ctx.rule.attr.deps:
  617. alldeps = alldeps | set([dep.label])
  618. if hasattr(dep, "tf_collected_deps"):
  619. alldeps = alldeps | dep.tf_collected_deps
  620. return struct(tf_collected_deps=alldeps)
  621. collect_deps_aspect = aspect(
  622. implementation=_collect_deps_aspect_impl,
  623. attr_aspects=["deps"])
  624. def _dep_label(dep):
  625. label = dep.label
  626. return label.package + ":" + label.name
  627. # This rule checks that the transitive dependencies of targets listed
  628. # in the 'deps' attribute don't depend on the targets listed in
  629. # the 'disallowed_deps' attribute.
  630. def _check_deps_impl(ctx):
  631. disallowed_deps = ctx.attr.disallowed_deps
  632. for input_dep in ctx.attr.deps:
  633. if not hasattr(input_dep, "tf_collected_deps"):
  634. continue
  635. for dep in input_dep.tf_collected_deps:
  636. for disallowed_dep in disallowed_deps:
  637. if dep == disallowed_dep.label:
  638. fail(_dep_label(input_dep) + " cannot depend on " +
  639. _dep_label(disallowed_dep))
  640. return struct()
  641. check_deps = rule(
  642. _check_deps_impl,
  643. attrs = {
  644. "deps": attr.label_list(
  645. aspects=[collect_deps_aspect],
  646. mandatory = True,
  647. allow_files = True
  648. ),
  649. "disallowed_deps": attr.label_list(
  650. mandatory = True,
  651. allow_files = True
  652. )},
  653. )
  654. # Helper to build a dynamic library (.so) from the sources containing
  655. # implementations of custom ops and kernels.
  656. def tf_custom_op_library(name, srcs=[], gpu_srcs=[], deps=[]):
  657. cuda_deps = [
  658. "@org_tensorflow//tensorflow/core:stream_executor_headers_lib",
  659. "@local_config_cuda//cuda:cudart_static",
  660. ]
  661. deps = deps + tf_custom_op_library_additional_deps()
  662. if gpu_srcs:
  663. basename = name.split(".")[0]
  664. native.cc_library(
  665. name = basename + "_gpu",
  666. srcs = gpu_srcs,
  667. copts = _cuda_copts(),
  668. deps = deps + if_cuda(cuda_deps))
  669. cuda_deps.extend([":" + basename + "_gpu"])
  670. check_deps(name=name+"_check_deps",
  671. deps=deps + if_cuda(cuda_deps),
  672. disallowed_deps=["@org_tensorflow//tensorflow/core:framework",
  673. "@org_tensorflow//tensorflow/core:lib"])
  674. native.cc_binary(name=name,
  675. srcs=srcs,
  676. deps=deps + if_cuda(cuda_deps),
  677. data=[name + "_check_deps"],
  678. copts=tf_copts(),
  679. linkshared=1,
  680. linkopts = select({
  681. "//conditions:default": [
  682. "-lm",
  683. ],
  684. "@org_tensorflow//tensorflow:darwin": [],
  685. }),
  686. )
  687. def tf_extension_linkopts():
  688. return [] # No extension link opts
  689. def tf_extension_copts():
  690. return [] # No extension c opts
  691. def tf_py_wrap_cc(name, srcs, swig_includes=[], deps=[], copts=[], **kwargs):
  692. module_name = name.split("/")[-1]
  693. # Convert a rule name such as foo/bar/baz to foo/bar/_baz.so
  694. # and use that as the name for the rule producing the .so file.
  695. cc_library_name = "/".join(name.split("/")[:-1] + ["_" + module_name + ".so"])
  696. cc_library_pyd_name = "/".join(name.split("/")[:-1] + ["_" + module_name + ".pyd"])
  697. extra_deps = []
  698. _py_wrap_cc(name=name + "_py_wrap",
  699. srcs=srcs,
  700. swig_includes=swig_includes,
  701. deps=deps + extra_deps,
  702. toolchain_deps=["//tools/defaults:crosstool"],
  703. module_name=module_name,
  704. py_module_name=name)
  705. extra_linkopts = select({
  706. "@local_config_cuda//cuda:darwin": [
  707. "-Wl,-exported_symbols_list",
  708. "@org_tensorflow//tensorflow:tf_exported_symbols.lds"
  709. ],
  710. "@org_tensorflow//tensorflow:windows": [
  711. ],
  712. "//conditions:default": [
  713. "-Wl,--version-script",
  714. "@org_tensorflow//tensorflow:tf_version_script.lds"
  715. ]})
  716. extra_deps += select({
  717. "@local_config_cuda//cuda:darwin": [
  718. "@org_tensorflow//tensorflow:tf_exported_symbols.lds"
  719. ],
  720. "@org_tensorflow//tensorflow:windows": [
  721. ],
  722. "//conditions:default": [
  723. "@org_tensorflow//tensorflow:tf_version_script.lds"
  724. ]
  725. })
  726. native.cc_binary(
  727. name=cc_library_name,
  728. srcs=[module_name + ".cc"],
  729. copts=(copts + ["-Wno-self-assign",
  730. "-Wno-sign-compare",
  731. "-Wno-write-strings"]
  732. + tf_extension_copts()),
  733. linkopts=tf_extension_linkopts() + extra_linkopts,
  734. linkstatic=1,
  735. linkshared=1,
  736. deps=deps + extra_deps)
  737. native.genrule(
  738. name = "gen_" + cc_library_pyd_name,
  739. srcs = [":" + cc_library_name],
  740. outs = [cc_library_pyd_name],
  741. cmd = "cp $< $@",
  742. )
  743. native.py_library(name=name,
  744. srcs=[":" + name + ".py"],
  745. srcs_version="PY2AND3",
  746. data=select({
  747. "@org_tensorflow//tensorflow:windows": [":" + cc_library_pyd_name],
  748. "//conditions:default": [":" + cc_library_name],
  749. }))
  750. def py_test(deps=[], **kwargs):
  751. native.py_test(
  752. deps=select({
  753. "//conditions:default" : deps,
  754. "@org_tensorflow//tensorflow:no_tensorflow_py_deps" : []
  755. }),
  756. **kwargs)
  757. def tf_py_test(name, srcs, size="medium", data=[], main=None, args=[],
  758. tags=[], shard_count=1, additional_deps=[], flaky=0):
  759. native.py_test(
  760. name=name,
  761. size=size,
  762. srcs=srcs,
  763. main=main,
  764. args=args,
  765. tags=tags,
  766. visibility=["@org_tensorflow//tensorflow:internal"],
  767. shard_count=shard_count,
  768. data=data,
  769. deps=select({
  770. "//conditions:default" : [
  771. "@org_tensorflow//tensorflow/python:extra_py_tests_deps",
  772. "@org_tensorflow//tensorflow/python:gradient_checker",
  773. ] + additional_deps,
  774. "@org_tensorflow//tensorflow:no_tensorflow_py_deps" : []
  775. }),
  776. flaky=flaky,
  777. srcs_version="PY2AND3")
  778. def cuda_py_test(name, srcs, size="medium", data=[], main=None, args=[],
  779. shard_count=1, additional_deps=[], tags=[], flaky=0):
  780. test_tags = tags + tf_cuda_tests_tags()
  781. tf_py_test(name=name,
  782. size=size,
  783. srcs=srcs,
  784. data=data,
  785. main=main,
  786. args=args,
  787. tags=test_tags,
  788. shard_count=shard_count,
  789. additional_deps=additional_deps,
  790. flaky=flaky)
  791. def sycl_py_test(name, srcs, size="medium", data=[], main=None, args=[],
  792. shard_count=1, additional_deps=[], tags=[], flaky=0):
  793. test_tags = tags + tf_sycl_tests_tags()
  794. tf_py_test(name=name,
  795. size=size,
  796. srcs=srcs,
  797. data=data,
  798. main=main,
  799. args=args,
  800. tags=test_tags,
  801. shard_count=shard_count,
  802. additional_deps=additional_deps,
  803. flaky=flaky)
  804. def py_tests(name,
  805. srcs,
  806. size="medium",
  807. additional_deps=[],
  808. data=[],
  809. tags=[],
  810. shard_count=1,
  811. prefix=""):
  812. for src in srcs:
  813. test_name = src.split("/")[-1].split(".")[0]
  814. if prefix:
  815. test_name = "%s_%s" % (prefix, test_name)
  816. tf_py_test(name=test_name,
  817. size=size,
  818. srcs=[src],
  819. main=src,
  820. tags=tags,
  821. shard_count=shard_count,
  822. data=data,
  823. additional_deps=additional_deps)
  824. def cuda_py_tests(name, srcs, size="medium", additional_deps=[], data=[],
  825. shard_count=1, tags=[], prefix=""):
  826. test_tags = tags + tf_cuda_tests_tags()
  827. py_tests(name=name, size=size, srcs=srcs, additional_deps=additional_deps,
  828. data=data, tags=test_tags, shard_count=shard_count,prefix=prefix)
  829. # Creates a genrule named <name> for running tools/proto_text's generator to
  830. # make the proto_text functions, for the protos passed in <srcs>.
  831. #
  832. # Return a struct with fields (hdrs, srcs) containing the names of the
  833. # generated files.
  834. def tf_generate_proto_text_sources(name, srcs_relative_dir, srcs):
  835. out_hdrs = ([p.replace(".proto", ".pb_text.h") for p in srcs] +
  836. [p.replace(".proto", ".pb_text-impl.h") for p in srcs])
  837. out_srcs = [p.replace(".proto", ".pb_text.cc") for p in srcs]
  838. native.genrule(
  839. name = name,
  840. srcs = srcs + ["@org_tensorflow//tensorflow/tools/proto_text:placeholder.txt"],
  841. outs = out_hdrs + out_srcs,
  842. cmd = "$(location //tensorflow/tools/proto_text:gen_proto_text_functions) " +
  843. "$(@D) " + srcs_relative_dir + " $(SRCS)",
  844. tools = ["@org_tensorflow//tensorflow/tools/proto_text:gen_proto_text_functions"],
  845. )
  846. return struct(hdrs=out_hdrs, srcs=out_srcs)
  847. def tf_genrule_cmd_append_to_srcs(to_append):
  848. return ("cat $(SRCS) > $(@) && " +
  849. "echo >> $(@) && " +
  850. "echo " + to_append + " >> $(@)")
  851. def tf_version_info_genrule():
  852. native.genrule(
  853. name = "version_info_gen",
  854. srcs = [
  855. "@org_tensorflow//tensorflow/tools/git:gen/spec.json",
  856. "@org_tensorflow//tensorflow/tools/git:gen/head",
  857. "@org_tensorflow//tensorflow/tools/git:gen/branch_ref",
  858. ],
  859. outs = ["util/version_info.cc"],
  860. cmd = "$(location //tensorflow/tools/git:gen_git_source.py) --generate $(SRCS) \"$@\"",
  861. local = 1,
  862. tools = ["@org_tensorflow//tensorflow/tools/git:gen_git_source.py"],
  863. )
  864. def cc_library_with_android_deps(deps, android_deps=[],
  865. common_deps=[], **kwargs):
  866. deps = if_not_android(deps) + if_android(android_deps) + common_deps
  867. native.cc_library(deps=deps, **kwargs)