|
| 1 | +"""Repository rule for Ascend CANN autoconfiguration. |
| 2 | +
|
| 3 | +`ascend_configure` depends on the following environment variables: |
| 4 | +
|
| 5 | + * `TF_NEED_ASCEND`: Whether to enable building with Ascend CANN. |
| 6 | + * `ASCEND_TOOLKIT_PATH`: The path to the Ascend CANN toolkit. Default is |
| 7 | + `/usr/local/Ascend/ascend-toolkit/latest`. |
| 8 | + * `TF_ASCEND_VERSION`: The version of the CANN toolkit. |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +_ASCEND_TOOLKIT_PATH = "ASCEND_TOOLKIT_PATH" |
| 14 | +_TF_ASCEND_VERSION = "TF_ASCEND_VERSION" |
| 15 | +_TF_NEED_ASCEND = "TF_NEED_ASCEND" |
| 16 | +_DEFAULT_ASCEND_TOOLKIT_PATH = "/usr/local/Ascend/ascend-toolkit/latest" |
| 17 | + |
| 18 | +def _ascend_autoconf_impl(repository_ctx): |
| 19 | + """Implementation of the ascend_configure repository rule.""" |
| 20 | + |
| 21 | + # 1. Check TF_NEED_ASCEND environment variable |
| 22 | + tf_need_ascend = repository_ctx.os.environ.get(_TF_NEED_ASCEND, "0") |
| 23 | + if tf_need_ascend != "1": |
| 24 | + _create_dummy_repo(repository_ctx) |
| 25 | + return |
| 26 | + |
| 27 | + # 2. Determine CANN installation path |
| 28 | + ascend_toolkit_path = repository_ctx.os.environ.get( |
| 29 | + _ASCEND_TOOLKIT_PATH, _DEFAULT_ASCEND_TOOLKIT_PATH |
| 30 | + ) |
| 31 | + |
| 32 | + # 3. Probe CANN headers and libraries |
| 33 | + include_path = ascend_toolkit_path + "/include" |
| 34 | + lib_path = ascend_toolkit_path + "/lib64" |
| 35 | + |
| 36 | + _check_file_exists(repository_ctx, include_path + "/acl/acl.h", "CANN header") |
| 37 | + _check_file_exists(repository_ctx, lib_path + "/libascendcl.so", "CANN library") |
| 38 | + |
| 39 | + # 4. Get version number |
| 40 | + ascend_version = repository_ctx.os.environ.get(_TF_ASCEND_VERSION, "") |
| 41 | + |
| 42 | + # 5. Generate template files |
| 43 | + _ascend_create(repository_ctx, ascend_toolkit_path, include_path, |
| 44 | + lib_path, ascend_version) |
| 45 | + |
| 46 | + |
| 47 | +def _check_file_exists(repository_ctx, path, label): |
| 48 | + if not repository_ctx.path(path).exists: |
| 49 | + fail("Cannot find %s at %s" % (label, path)) |
| 50 | + |
| 51 | + |
| 52 | +def _create_dummy_repo(repository_ctx): |
| 53 | + """Create empty BUILD and build_defs.bzl when Ascend is not configured.""" |
| 54 | + repository_ctx.file("ascend/BUILD", """ |
| 55 | +package(default_visibility = ["//visibility:public"]) |
| 56 | +config_setting(name = "using_ascend", values = {"define": "using_ascend=true"}) |
| 57 | +""") |
| 58 | + repository_ctx.file("ascend/build_defs.bzl", """ |
| 59 | +def if_ascend(if_true, if_false = []): |
| 60 | + return select({ |
| 61 | + "@local_config_ascend//ascend:using_ascend": if_true, |
| 62 | + "//conditions:default": if_false, |
| 63 | + }) |
| 64 | +
|
| 65 | +def ascend_default_copts(): |
| 66 | + return if_ascend([]) |
| 67 | +
|
| 68 | +def ascend_is_configured(): |
| 69 | + return False |
| 70 | +""") |
| 71 | + |
| 72 | + |
| 73 | +def _ascend_create(repository_ctx, toolkit_path, include_path, lib_path, version): |
| 74 | + """Generate the @local_config_ascend repository from CANN installation.""" |
| 75 | + |
| 76 | + # Symlink headers so they exist at analysis time (glob evaluation time) |
| 77 | + repository_ctx.symlink(repository_ctx.path(include_path), "ascend/include") |
| 78 | + |
| 79 | + # Symlink library files |
| 80 | + repository_ctx.execute(["mkdir", "-p", "ascend/lib"]) |
| 81 | + lib_files = { |
| 82 | + "libascendcl.so": "ascend", |
| 83 | + "libhccl.so": "hccl", |
| 84 | + } |
| 85 | + for lib_name, target_name in lib_files.items(): |
| 86 | + src = repository_ctx.path(lib_path + "/" + lib_name) |
| 87 | + if src.exists: |
| 88 | + repository_ctx.symlink(src, "ascend/lib/" + lib_name) |
| 89 | + |
| 90 | + # --- Generate BUILD content --- |
| 91 | + build_content = """package(default_visibility = ["//visibility:public"]) |
| 92 | +
|
| 93 | +config_setting( |
| 94 | + name = "using_ascend", |
| 95 | + values = {"define": "using_ascend=true"}, |
| 96 | +) |
| 97 | +
|
| 98 | +cc_library( |
| 99 | + name = "ascend_headers", |
| 100 | + hdrs = glob(["include/**/*.h"]), |
| 101 | + includes = [".", "include"], |
| 102 | + visibility = ["//visibility:public"], |
| 103 | +) |
| 104 | +
|
| 105 | +cc_library( |
| 106 | + name = "ascend", |
| 107 | + srcs = ["lib/libascendcl.so"], |
| 108 | + data = ["lib/libascendcl.so"], |
| 109 | + includes = [".", "include"], |
| 110 | + linkstatic = 1, |
| 111 | + visibility = ["//visibility:public"], |
| 112 | +) |
| 113 | +
|
| 114 | +cc_library( |
| 115 | + name = "hccl", |
| 116 | + srcs = ["lib/libhccl.so"], |
| 117 | + data = ["lib/libhccl.so"], |
| 118 | + includes = [".", "include"], |
| 119 | + linkstatic = 1, |
| 120 | + visibility = ["//visibility:public"], |
| 121 | +) |
| 122 | +""" |
| 123 | + |
| 124 | + repository_ctx.file("ascend/BUILD", build_content) |
| 125 | + |
| 126 | + # --- Generate build_defs.bzl --- |
| 127 | + repository_ctx.file("ascend/build_defs.bzl", """def if_ascend(if_true, if_false = []): |
| 128 | + return select({ |
| 129 | + "@local_config_ascend//ascend:using_ascend": if_true, |
| 130 | + "//conditions:default": if_false, |
| 131 | + }) |
| 132 | +
|
| 133 | +def ascend_default_copts(): |
| 134 | + return if_ascend([]) |
| 135 | +
|
| 136 | +def ascend_is_configured(): |
| 137 | + return True |
| 138 | +""") |
| 139 | + |
| 140 | + # --- Generate ascend_config.h --- |
| 141 | + repository_ctx.file("ascend/ascend_config.h", """#ifndef ASCEND_ASCEND_CONFIG_H_ |
| 142 | +#define ASCEND_ASCEND_CONFIG_H_ |
| 143 | +#define TF_ASCEND_TOOLKIT_PATH "%s" |
| 144 | +#define TF_ASCEND_VERSION "%s" |
| 145 | +#endif |
| 146 | +""" % (toolkit_path, version)) |
| 147 | + |
| 148 | + |
| 149 | +ascend_configure = repository_rule( |
| 150 | + implementation = _ascend_autoconf_impl, |
| 151 | + environ = [ |
| 152 | + _TF_NEED_ASCEND, |
| 153 | + _ASCEND_TOOLKIT_PATH, |
| 154 | + _TF_ASCEND_VERSION, |
| 155 | + ], |
| 156 | +) |
0 commit comments