Skip to content

Commit

Permalink
Dynamic generate oneDNN version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lu Teng committed Nov 27, 2023
1 parent 339d1da commit 5b980e0
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 24 deletions.
9 changes: 9 additions & 0 deletions third_party/mkl_dnn/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ bzl_library(
name = "build_defs_bzl",
srcs = ["build_defs.bzl"],
)

py_binary(
name = "gen_onednn_version",
srcs = ["gen_onednn_version.py"],
visibility = [
"@mkl_dnn_acl_compatible//:__subpackages__",
"@mkl_dnn_v1//:__subpackages__",
],
)
12 changes: 12 additions & 0 deletions third_party/mkl_dnn/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ def if_mkldnn_aarch64_acl_openmp(if_true, if_false = []):
"@org_tensorflow//third_party/mkl_dnn:build_with_mkl_aarch64_openmp": if_true,
"//conditions:default": if_false,
})

def gen_onednn_version(name, header_in, header_out, **kwargs):
tool = "@org_tensorflow//third_party/mkl_dnn:gen_onednn_version"

native.genrule(
name = name,
srcs = [header_in],
outs = [header_out],
tools = [tool],
cmd = "$(location {}) ".format(tool) + "--in=$< " + "--out=$@",
**kwargs
)
77 changes: 77 additions & 0 deletions third_party/mkl_dnn/gen_onednn_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import re
import os
import sys


def parse_args(argv):
result = {}
for arg in argv:
k, v = arg.split("=")
result[k] = v

return result


def parse_version(cmake):
pattern = re.compile('set\\(PROJECT_VERSION "([0-9]+\\.[0-9]+\\.[0-9]+)"\\)')
with open(os.path.expanduser(cmake)) as f:
for line in f.readlines():
result = pattern.match(line)
if result is not None:
return result.group(1)

sys.exit("Can't get the right version from ", cmake)


def get_root(header_in):
"""
This is an assumption that the root workspace should be the same depth
with "include" folder. It will find start from right position, so should
handle the include/**/tensorflow/***/onednn/include/**.
"""
pos = header_in.rindex("include")
root = header_in[:pos]
return root


# TODO(intel-tf): Add oneDNN commit hash if need in future.
def git_hash(header_in):
return "N/A"


def get_cmake(header_in):
root = get_root(header_in)
cmake = os.path.join(root, "CMakeLists.txt")
return cmake


def generate_version(version, header_in, header_out):
hash_value = git_hash(header_in)

[major, minor, patch] = version.split(".")

with open(os.path.expanduser(header_in)) as inf:
content = inf.read()
content = content.replace("@DNNL_VERSION_MAJOR@", major)
content = content.replace("@DNNL_VERSION_MINOR@", minor)
content = content.replace("@DNNL_VERSION_PATCH@", patch)
content = content.replace("@DNNL_VERSION_HASH@", hash_value)

header_out = os.path.expanduser(header_out)
header_out_dir = os.path.dirname(header_out)
if not os.path.exists(header_out_dir):
os.makedirs(header_out_dir, exist_ok=True)

with open(header_out, "w") as outf:
outf.write(content)


def main():
args = parse_args(sys.argv[1:])
cmake = get_cmake(args["--in"])
version = parse_version(cmake)
generate_version(version, args["--in"], args["--out"])


if __name__ == "__main__":
main()
14 changes: 5 additions & 9 deletions third_party/mkl_dnn/mkldnn_acl.BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
exports_files(["LICENSE"])

load("@org_tensorflow//third_party/mkl_dnn:build_defs.bzl", "gen_onednn_version")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")

_DNNL_COPTS_THREADPOOL = [
Expand Down Expand Up @@ -122,16 +123,11 @@ expand_template(
template = "include/oneapi/dnnl/dnnl_config.h.in",
)

expand_template(
# Create the file dnnl_version.h with DNNL version numbers.
gen_onednn_version(
name = "dnnl_version_h",
out = "include/oneapi/dnnl/dnnl_version.h",
substitutions = {
"@DNNL_VERSION_MAJOR@": "3",
"@DNNL_VERSION_MINOR@": "2",
"@DNNL_VERSION_PATCH@": "1",
"@DNNL_VERSION_HASH@": "N/A",
},
template = "include/oneapi/dnnl/dnnl_version.h.in",
header_in = "include/oneapi/dnnl/dnnl_version.h.in",
header_out = "include/oneapi/dnnl/dnnl_version.h",
)

cc_library(
Expand Down
19 changes: 4 additions & 15 deletions third_party/mkl_dnn/mkldnn_v1.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@local_tsl//tsl:tsl.bzl", "tf_openmp_copts")
load("@org_tensorflow//third_party/mkl:build_defs.bzl", "if_mkl")
load("@org_tensorflow//third_party/mkl_dnn:build_defs.bzl", "if_mkldnn_openmp")
load("@org_tensorflow//third_party/mkl:build_defs.bzl", "if_mkl_ml")
load("@org_tensorflow//third_party/mkl_dnn:build_defs.bzl", "gen_onednn_version")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")

exports_files(["LICENSE"])
Expand Down Expand Up @@ -83,22 +84,10 @@ expand_template(
)

# Create the file dnnl_version.h with DNNL version numbers.
# Currently, the version numbers are hard coded here. If DNNL is upgraded then
# the version numbers have to be updated manually. The version numbers can be
# obtained from the PROJECT_VERSION settings in CMakeLists.txt. The variable is
# set to "version_major.version_minor.version_patch". The git hash version can
# be set to NA.
# TODO(agramesh1): Automatically get the version numbers from CMakeLists.txt.
expand_template(
gen_onednn_version(
name = "dnnl_version_h",
out = "include/oneapi/dnnl/dnnl_version.h",
substitutions = {
"@DNNL_VERSION_MAJOR@": "3",
"@DNNL_VERSION_MINOR@": "3",
"@DNNL_VERSION_PATCH@": "0",
"@DNNL_VERSION_HASH@": "N/A",
},
template = "include/oneapi/dnnl/dnnl_version.h.in",
header_in = "include/oneapi/dnnl/dnnl_version.h.in",
header_out = "include/oneapi/dnnl/dnnl_version.h",
)

_COPTS_LIST = select({
Expand Down

0 comments on commit 5b980e0

Please sign in to comment.