Skip to content

Commit f063417

Browse files
committed
TFQ simulate expectation CUDA op
1 parent 1317edf commit f063417

File tree

6 files changed

+613
-11
lines changed

6 files changed

+613
-11
lines changed

WORKSPACE

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ cc_library(
2424
],
2525
)
2626

27+
# http_archive(
28+
# name = "qsim",
29+
# sha256 = "b9c1eba09a885a938b5e73dfc2e02f5231cf3b01d899415caa24769346a731d5",
30+
# strip_prefix = "qsim-0.13.3",
31+
# urls = ["https://github.com/quantumlib/qsim/archive/refs/tags/v0.13.3.zip"],
32+
# )
33+
34+
# TODO: After merging this patch later into qsim mainstream, remove this and uncomment the above.
2735
http_archive(
2836
name = "qsim",
29-
sha256 = "b9c1eba09a885a938b5e73dfc2e02f5231cf3b01d899415caa24769346a731d5",
30-
strip_prefix = "qsim-0.13.3",
31-
urls = ["https://github.com/quantumlib/qsim/archive/refs/tags/v0.13.3.zip"],
37+
sha256 = "97b26e1a8fe13cfa465611f2618ade00f539480c6a7849f020fbee3582686bbf",
38+
strip_prefix = "qsim-0.15.0-dev20230317",
39+
urls = ["https://github.com/jaeyoo/qsim/archive/refs/tags/v0.15.0+dev20230317.tar.gz"],
3240
)
3341

3442
http_archive(

configure.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ while [[ "$TF_NEED_CUDA" == "" ]]; do
6262
done
6363

6464
while [[ "$TF_CUDA_VERSION" == "" ]]; do
65-
read -p "Are you building against TensorFlow 2.1(including RCs) or newer?[Y/n] " INPUT
65+
read -p "Are you building against TensorFlow 2.11(including RCs) or newer?[Y/n] " INPUT
6666
case $INPUT in
67-
[Yy]* ) echo "Build against TensorFlow 2.1 or newer."; TF_CUDA_VERSION=10.1;;
68-
[Nn]* ) echo "Build against TensorFlow <2.1."; TF_CUDA_VERSION=10.0;;
69-
"" ) echo "Build against TensorFlow 2.1 or newer."; TF_CUDA_VERSION=10.1;;
67+
[Yy]* ) echo "Build against TensorFlow 2.11 or newer."; TF_CUDA_VERSION=11;;
68+
[Nn]* ) echo "Build against TensorFlow <2.11."; TF_CUDA_VERSION=10.0;;
69+
"" ) echo "Build against TensorFlow 2.11 or newer."; TF_CUDA_VERSION=11;;
7070
* ) echo "Invalid selection: " $INPUT;;
7171
esac
7272
done
@@ -94,10 +94,11 @@ fi
9494
TF_CFLAGS=( $(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_compile_flags()))') )
9595
TF_LFLAGS="$(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_link_flags()))')"
9696

97+
# This config refers to building CUDA op kernels with nvcc.
9798
write_to_bazelrc "build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true"
98-
if [[ "$PIP_MANYLINUX2010" == "0" ]]; then
99-
write_to_bazelrc "build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain"
100-
fi
99+
write_to_bazelrc "build:cuda --@local_config_cuda//:enable_cuda"
100+
write_to_bazelrc "build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain"
101+
101102

102103
write_to_bazelrc "build --experimental_repo_remote_exec"
103104
write_to_bazelrc "build --spawn_strategy=standalone"
@@ -143,7 +144,7 @@ fi
143144
# TODO(yifeif): do not hardcode path
144145
if [[ "$TF_NEED_CUDA" == "1" ]]; then
145146
write_action_env_to_bazelrc "TF_CUDA_VERSION" ${TF_CUDA_VERSION}
146-
write_action_env_to_bazelrc "TF_CUDNN_VERSION" "7"
147+
write_action_env_to_bazelrc "TF_CUDNN_VERSION" "8"
147148
if is_windows; then
148149
write_action_env_to_bazelrc "CUDNN_INSTALL_PATH" "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v${TF_CUDA_VERSION}"
149150
write_action_env_to_bazelrc "CUDA_TOOLKIT_PATH" "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v${TF_CUDA_VERSION}"

tensorflow_quantum/core/ops/BUILD

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# load op_wrapper
2+
load("@org_tensorflow//tensorflow:tensorflow.bzl", "tf_gpu_kernel_library", "tf_gen_op_wrapper_py")
3+
load("@local_config_cuda//cuda:build_defs.bzl", "if_cuda_is_configured", "if_cuda")
24

35
package(default_visibility = ["//visibility:public"])
46

@@ -12,6 +14,23 @@ config_setting(
1214
constraint_values = ["@bazel_tools//platforms:windows"],
1315
)
1416

17+
cc_library(
18+
name = "cuda",
19+
data = [
20+
"@local_config_cuda//cuda:cudart",
21+
],
22+
linkopts = select({
23+
":windows": [],
24+
"//conditions:default": [
25+
"-Wl,-rpath,../local_config_cuda/cuda/lib64",
26+
"-Wl,-rpath,../local_config_cuda/cuda/extras/CUPTI/lib64",
27+
],
28+
}),
29+
deps = [
30+
"@local_config_cuda//cuda:cudart",
31+
],
32+
)
33+
1534
py_library(
1635
name = "ops",
1736
srcs = ["__init__.py"],
@@ -24,6 +43,7 @@ py_library(
2443
":tfq_adj_grad_op_py",
2544
":tfq_ps_util_ops_py",
2645
":tfq_simulate_ops_py",
46+
":tfq_simulate_ops_cuda_py",
2747
":tfq_unitary_op_py",
2848
":tfq_utility_ops_py",
2949
# test addons
@@ -619,6 +639,103 @@ py_test(
619639
],
620640
)
621641

642+
py_library(
643+
name = "tfq_simulate_ops_cuda_py",
644+
srcs = ["tfq_simulate_ops_cuda.py"],
645+
data = [
646+
":_tfq_simulate_ops_cuda.so",
647+
],
648+
srcs_version = "PY3",
649+
deps = [
650+
# tensorflow framework for wrappers
651+
":load_module",
652+
],
653+
)
654+
655+
py_test(
656+
name = "tfq_simulate_ops_cuda_test",
657+
srcs = ["tfq_simulate_ops_cuda_test.py"],
658+
deps = [
659+
":tfq_simulate_ops_cuda_py",
660+
":tfq_simulate_ops_py",
661+
"//tensorflow_quantum/python:util",
662+
],
663+
srcs_version = "PY3",
664+
)
665+
666+
cc_binary(
667+
name = "_tfq_simulate_ops_cuda.so",
668+
srcs = [
669+
"tfq_simulate_expectation_op_cuda.cu.cc",
670+
],
671+
linkshared = 1,
672+
features = select({
673+
":windows": ["windows_export_all_symbols"],
674+
"//conditions:default": [],
675+
}),
676+
copts = select({
677+
":windows": [
678+
"/D__CLANG_SUPPORT_DYN_ANNOTATION__",
679+
"/D_USE_MATH_DEFINES",
680+
"/DEIGEN_MPL2_ONLY",
681+
"/DEIGEN_MAX_ALIGN_BYTES=64",
682+
"/DEIGEN_HAS_TYPE_TRAITS=0",
683+
"/DTF_USE_SNAPPY",
684+
"/showIncludes",
685+
"/MD",
686+
"/O2",
687+
"/DNDEBUG",
688+
"/w",
689+
"-DWIN32_LEAN_AND_MEAN",
690+
"-DNOGDI",
691+
"/d2ReducedOptimizeHugeFunctions",
692+
"/arch:AVX",
693+
"/std:c++17",
694+
"-DTENSORFLOW_MONOLITHIC_BUILD",
695+
"/DPLATFORM_WINDOWS",
696+
"/DEIGEN_HAS_C99_MATH",
697+
"/DTENSORFLOW_USE_EIGEN_THREADPOOL",
698+
"/DEIGEN_AVOID_STL_ARRAY",
699+
"/Iexternal/gemmlowp",
700+
"/wd4018",
701+
"/wd4577",
702+
"/DNOGDI",
703+
"/UTF_COMPILE_LIBRARY",
704+
],
705+
"//conditions:default": [
706+
"-Iexternal/local_cuda/cuda/include",
707+
# "--cuda-gpu-arch=sm_86",
708+
# "-L/usr/local/cuda/lib64",
709+
# "-lcudart_static",
710+
# "-ldl",
711+
# "-lrt",
712+
"-pthread",
713+
"-std=c++17",
714+
"-D_GLIBCXX_USE_CXX11_ABI=1",
715+
"-O3",
716+
"-Iexternal/cuda_headers",
717+
"-DNV_CUDNN_DISABLE_EXCEPTION",
718+
# "-fpermissive",
719+
],
720+
}) + if_cuda_is_configured(["-DTENSORFLOW_USE_NVCC=1", "-DGOOGLE_CUDA=1", "-x cuda", "-nvcc_options=relaxed-constexpr", "-nvcc_options=ftz=true"]),
721+
deps = [
722+
# cirq cc proto
723+
"//tensorflow_quantum/core/ops:parse_context",
724+
"//tensorflow_quantum/core/ops:tfq_simulate_utils",
725+
"//tensorflow_quantum/core/proto:pauli_sum_cc_proto",
726+
"//tensorflow_quantum/core/proto:program_cc_proto",
727+
"//tensorflow_quantum/core/src:circuit_parser_qsim",
728+
"//tensorflow_quantum/core/src:util_qsim",
729+
"@qsim//lib:qsim_cuda_lib",
730+
"@eigen//:eigen3",
731+
# "@local_cuda//:cuda_headers"
732+
# tensorflow core framework
733+
# tensorflow core lib
734+
# tensorflow core protos
735+
] + if_cuda_is_configured([":cuda", "@local_config_cuda//cuda:cuda_headers"]),
736+
# alwayslink=1,
737+
)
738+
622739
py_library(
623740
name = "load_module",
624741
srcs = ["load_module.py"],

0 commit comments

Comments
 (0)