[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python support for GPU ops [WIP] #759

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ cc_library(
# TODO: After merging this patch later into qsim mainstream, remove this and uncomment the above.
http_archive(
name = "qsim",
sha256 = "",
strip_prefix = "qsim-0.15.0-dev20230327_v3",
urls = ["https://github.com/jaeyoo/qsim/archive/refs/tags/v0.15.0+dev20230327_v3.tar.gz"],
sha256 = "75d62843020f8a70cf2aac85aca5e25fa6c1cea0945323afd06c3b7fcf3ee2b7",
strip_prefix = "qsim-0.15.0-dev-20230330_v2",
urls = ["https://github.com/jaeyoo/qsim/archive/refs/tags/v0.15.0-dev+20230330_v2.tar.gz"],
)

http_archive(
Expand Down Expand Up @@ -81,21 +81,6 @@ bind(
actual = "@six_archive//:six",
)

new_local_repository(
name = "cuquantum_libs",
path = "/usr/local/google/home/jaeyoo/workspace/cuquantum-linux-x86_64-22.11.0.13-archive",
build_file_content = """
cc_library(
name = "custatevec_headers",
srcs = ["include/custatevec.h"],
visibility = ["//visibility:public"],
)

cc_library(
name = "custatevec",
srcs = ["lib/libcustatevec.so"],
visibility = ["//visibility:public"],
)
""",
)
load("//third_party/cuquantum:cuquantum_configure.bzl", "cuquantum_configure")

cuquantum_configure(name = "local_config_cuquantum")
11 changes: 11 additions & 0 deletions configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ while [[ "$TF_CUDA_VERSION" == "" ]]; do
esac
done

# Check if we are building cuQuantum ops on top of CUDA.
if [[ "$TF_NEED_CUDA" == "1" ]]; then
echo "GPU is selected, default acceleration is CUDA for TFQuantum."
echo "Searching cuQuantum library from environment variable CUQUANTUM_ROOT..."
if [[ "$CUQUANTUM_ROOT" != "" ]]; then
echo " [*] cuQuantum library is detected here: CUQUANTUM_ROOT=$CUQUANTUM_ROOT."
write_action_env_to_bazelrc "CUQUANTUM_ROOT" ${CUQUANTUM_ROOT}
else
echo " [*] cuQuantum library is NOT detected. Using general CUDA ops..."
fi
fi

# Check if it's installed
if [[ $(pip show tensorflow) == *tensorflow* ]] || [[ $(pip show tf-nightly) == *tf-nightly* ]]; then
Expand Down
2 changes: 1 addition & 1 deletion release/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def finalize_options(self):


REQUIRED_PACKAGES = [
'cirq-core==0.13.1', 'cirq-google>=0.13.1', 'sympy == 1.8',
'cirq-core==0.13.1', 'cirq-google==0.13.1', 'sympy == 1.8',
'googleapis-common-protos==1.52.0', 'google-api-core==1.21.0',
'google-auth==1.18.0', 'protobuf==3.19.5'
]
Expand Down
45 changes: 38 additions & 7 deletions tensorflow_quantum/core/ops/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -668,15 +668,15 @@ py_library(
)

py_test(
name = "tfq_simulate_ops_gpu_test",
srcs = ["tfq_simulate_ops_gpu_test.py"],
name = "tfq_simulate_ops_cuda_test",
srcs = ["tfq_simulate_ops_cuda_test.py"],
deps = [
":tfq_simulate_ops_cuda_py",
":tfq_simulate_ops_cuquantum_py",
":tfq_simulate_ops_py",
"//tensorflow_quantum/python:util",
],
srcs_version = "PY3",
tags = ["cuda"],
)

cc_binary(
Expand Down Expand Up @@ -717,6 +717,7 @@ cc_binary(
"/wd4577",
"/DNOGDI",
"/UTF_COMPILE_LIBRARY",
"/D__CUDA__",
],
"//conditions:default": [
"-Iexternal/local_cuda/cuda/include",
Expand All @@ -733,7 +734,14 @@ cc_binary(
"-DNV_CUDNN_DISABLE_EXCEPTION",
# "-fpermissive",
],
}) + if_cuda_is_configured(["-DTENSORFLOW_USE_NVCC=1", "-DGOOGLE_CUDA=1", "-x cuda", "-nvcc_options=relaxed-constexpr", "-nvcc_options=ftz=true"]),
}) + if_cuda_is_configured([
"-DTENSORFLOW_USE_NVCC=1",
"-DGOOGLE_CUDA=1",
"-x cuda",
"-nvcc_options=relaxed-constexpr",
"-nvcc_options=ftz=true",
"-D__CUDA__",
]),
deps = [
# cirq cc proto
"//tensorflow_quantum/core/ops:parse_context",
Expand All @@ -755,10 +763,25 @@ cc_binary(
# alwayslink=1,
)

py_test(
name = "tfq_simulate_ops_cuquantum_test",
srcs = ["tfq_simulate_ops_cuquantum_test.py"],
deps = [
":tfq_simulate_ops_cuquantum_py",
":tfq_simulate_ops_py",
"//tensorflow_quantum/python:util",
],
srcs_version = "PY3",
tags = ["cuquantum"],
)

cc_binary(
name = "_tfq_simulate_ops_cuquantum.so",
srcs = [
"tfq_simulate_expectation_op_cuquantum.cu.cc",
"tfq_simulate_sampled_expectation_op_cuquantum.cu.cc",
"tfq_simulate_samples_op_cuquantum.cu.cc",
"tfq_simulate_state_op_cuquantum.cu.cc",
],
linkshared = 1,
features = select({
Expand Down Expand Up @@ -793,6 +816,7 @@ cc_binary(
"/wd4577",
"/DNOGDI",
"/UTF_COMPILE_LIBRARY",
"/D__CUSTATEVEC__",
],
"//conditions:default": [
"-Iexternal/local_cuda/cuda/include",
Expand All @@ -809,7 +833,14 @@ cc_binary(
"-DNV_CUDNN_DISABLE_EXCEPTION",
# "-fpermissive",
],
}) + if_cuda_is_configured(["-DTENSORFLOW_USE_NVCC=1", "-DGOOGLE_CUDA=1", "-x cuda", "-nvcc_options=relaxed-constexpr", "-nvcc_options=ftz=true"]),
}) + if_cuda_is_configured([
"-DTENSORFLOW_USE_NVCC=1",
"-DGOOGLE_CUDA=1",
"-x cuda",
"-nvcc_options=relaxed-constexpr",
"-nvcc_options=ftz=true",
"-D__CUSTATEVEC__",
]),
deps = [
# cirq cc proto
"//tensorflow_quantum/core/ops:parse_context",
Expand All @@ -825,9 +856,9 @@ cc_binary(
# tensorflow core protos
] + if_cuda_is_configured([
":cuda",
"@cuquantum_libs//:custatevec",
"@cuquantum_libs//:custatevec_headers",
"@local_config_cuda//cuda:cuda_headers",
"@local_config_cuquantum//:cuquantum_headers",
"@local_config_cuquantum//:libcuquantum",
"@qsim//lib:qsim_cuquantum_lib",
]),
# alwayslink=1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <chrono>
#include <memory>
#include <vector>

#include <chrono>

#include "../qsim/lib/circuit.h"
#include "../qsim/lib/gate_appl.h"
#include "../qsim/lib/gates_cirq.h"
#include "../qsim/lib/gates_qsim.h"
#include "../qsim/lib/seqfor.h"
#include "../qsim/lib/simulator_cuda.h"
#include "../qsim/lib/statespace_cuda.h"
#include "../qsim/lib/simmux.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/framework/tensor_shape.h"
Expand All @@ -43,10 +41,10 @@ using ::tfq::proto::Program;
typedef qsim::Cirq::GateCirq<float> QsimGate;
typedef qsim::Circuit<QsimGate> QsimCircuit;


class TfqSimulateExpectationOpCuda : public tensorflow::OpKernel {
public:
explicit TfqSimulateExpectationOpCuda(tensorflow::OpKernelConstruction* context)
explicit TfqSimulateExpectationOpCuda(
tensorflow::OpKernelConstruction* context)
: OpKernel(context) {}

void Compute(tensorflow::OpKernelContext* context) override {
Expand Down Expand Up @@ -169,7 +167,6 @@ class TfqSimulateExpectationOpCuda : public tensorflow::OpKernel {
}
}
}

};

REGISTER_KERNEL_BUILDER(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <memory>
#include <vector>
#include <custatevec.h>

#include <chrono>
#include <memory>
#include <vector>

#include "../cuquantum_libs/include/custatevec.h"
#include "../qsim/lib/circuit.h"
#include "../qsim/lib/gate_appl.h"
#include "../qsim/lib/gates_cirq.h"
#include "../qsim/lib/gates_qsim.h"
#include "../qsim/lib/seqfor.h"
#include "../qsim/lib/simulator_custatevec.h"
#include "../qsim/lib/statespace_custatevec.h"
#include "../qsim/lib/simmux.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/framework/tensor_shape.h"
Expand All @@ -44,14 +43,13 @@ using ::tfq::proto::Program;
typedef qsim::Cirq::GateCirq<float> QsimGate;
typedef qsim::Circuit<QsimGate> QsimCircuit;


class TfqSimulateExpectationOpCuQuantum : public tensorflow::OpKernel {
public:
explicit TfqSimulateExpectationOpCuQuantum(tensorflow::OpKernelConstruction* context)
explicit TfqSimulateExpectationOpCuQuantum(
tensorflow::OpKernelConstruction* context)
: OpKernel(context) {}

void Compute(tensorflow::OpKernelContext* context) override {

// TODO (mbbrough): add more dimension checks for other inputs here.
const int num_inputs = context->num_inputs();
OP_REQUIRES(context, num_inputs == 4,
Expand All @@ -67,15 +65,16 @@ class TfqSimulateExpectationOpCuQuantum : public tensorflow::OpKernel {

tensorflow::Tensor* output = nullptr;
tensorflow::AllocatorAttributes alloc_attr;
alloc_attr.set_on_host(true); // why??
alloc_attr.set_on_host(true); // why??
alloc_attr.set_gpu_compatible(true);
OP_REQUIRES_OK(context, context->allocate_output(0, output_shape, &output,
alloc_attr));
auto output_tensor = output->matrix<float>();
// Parse program protos.
std::vector<Program> programs;
std::vector<int> num_qubits;
std::vector<std::vector<PauliSum>> pauli_sums; // why is this a vector of vectors??
std::vector<std::vector<PauliSum>>
pauli_sums; // why is this a vector of vectors??
OP_REQUIRES_OK(context, GetProgramsAndNumQubits(context, &programs,
&num_qubits, &pauli_sums));

Expand Down Expand Up @@ -141,7 +140,6 @@ class TfqSimulateExpectationOpCuQuantum : public tensorflow::OpKernel {
using Simulator = qsim::SimulatorCuStateVec<float>;
using StateSpace = Simulator::StateSpace;


// Launch the cuda kernel.
// Begin simulation.
int largest_nq = 1;
Expand Down
1 change: 0 additions & 1 deletion tensorflow_quantum/core/ops/tfq_simulate_ops_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
# ==============================================================================
"""Module to register cuda simulation python op."""
import os
import tensorflow as tf
from tensorflow_quantum.core.ops.load_module import load_module

Expand Down
Loading