Skip to content

Commit

Permalink
Add hybridLaunchKernel dispatch (NVIDIA#2111)
Browse files Browse the repository at this point in the history
* Add hybridLaunchKernel

Dispatch kernel launch based on whether JIT+synthesis would be required.

* Fix tests

* Modify error message
  • Loading branch information
1tnguyen authored Aug 21, 2024
1 parent e7afc56 commit 18c305b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/cudaq/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def GenerateKernelExecution : Pass<"kernel-execution", "mlir::ModuleOp"> {
/*default=*/"\"-\"", "Name of output file.">,
Option<"startingArgIdx", "starting-arg-idx", "std::size_t", /*default=*/"0",
"The starting argument index for the argsCreator.">,
Option<"codegenKind", "codegen", "std::size_t", /*default=*/"1",
Option<"codegenKind", "codegen", "std::size_t", /*default=*/"0",
"Set the kind of code to generate for the launches.">
];
}
Expand Down
5 changes: 0 additions & 5 deletions runtime/cudaq/platform/default/DefaultQuantumPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class DefaultQPU : public cudaq::QPU {
kernelFunc(args);
}

void launchKernel(const std::string &name,
const std::vector<void *> &) override {
throw std::runtime_error("Wrong kernel launch point.");
}

/// Overrides setExecutionContext to forward it to the ExecutionManager
void setExecutionContext(cudaq::ExecutionContext *context) override {
ScopedTraceWithContext("DefaultPlatform::setExecutionContext",
Expand Down
5 changes: 0 additions & 5 deletions runtime/cudaq/platform/mqpu/custatevec/GPUEmulatedQPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class GPUEmulatedQPU : public cudaq::QPU {
kernelFunc(args);
}

void launchKernel(const std::string &name,
const std::vector<void *> &rawArgs) override {
throw std::runtime_error("not implemented");
}

/// Overrides setExecutionContext to forward it to the ExecutionManager
void setExecutionContext(cudaq::ExecutionContext *context) override {
cudaSetDevice(qpu_id);
Expand Down
11 changes: 10 additions & 1 deletion runtime/cudaq/platform/qpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,17 @@ class QPU : public registry::RegisteredType<QPU> {
/// as a struct-packed void pointer and its corresponding size.
virtual void launchKernel(const std::string &name, void (*kernelFunc)(void *),
void *args, std::uint64_t, std::uint64_t) = 0;

/// Launch the kernel with given name and argument arrays.
// This is intended for remote QPUs whereby we need to JIT-compile the kernel
// with argument synthesis. Remote QPU implementation to override this.
virtual void launchKernel(const std::string &name,
const std::vector<void *> &rawArgs) = 0;
const std::vector<void *> &rawArgs) {
if (!isRemote())
throw std::runtime_error("Wrong kernel launch point: Attempt to launch "
"kernel in streamlined for JIT mode on local "
"simulated QPU. This is not supported.");
}

/// Launch serialized code for remote execution. Subtypes that support this
/// should override this function.
Expand Down
13 changes: 13 additions & 0 deletions runtime/cudaq/platform/quantum_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,16 @@ void cudaq::streamlinedLaunchKernel(const char *kernelName,
std::string kernName = kernelName;
platform.launchKernel(kernName, rawArgs);
}

void cudaq::hybridLaunchKernel(const char *kernelName, void (*kernel)(void *),
void *args, std::uint64_t argsSize,
std::uint64_t resultOffset,
const std::vector<void *> &rawArgs) {
ScopedTraceWithContext("hybridLaunchKernel", kernelName);
auto &platform = *cudaq::getQuantumPlatformInternal();
const std::string kernName = kernelName;
if (platform.is_remote(platform.get_current_qpu()))
platform.launchKernel(kernName, rawArgs);
else
platform.launchKernel(kernName, kernel, args, argsSize, resultOffset);
}
2 changes: 1 addition & 1 deletion test/Quake-QIR/argument.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --kernel-execution --canonicalize %s | \
// RUN: cudaq-opt --kernel-execution=codegen=1 --canonicalize %s | \
// RUN: cudaq-translate --convert-to=qir | FileCheck %s

// NB: the mangled name map is required for the kernel-execution pass.
Expand Down
2 changes: 1 addition & 1 deletion test/Quake-QIR/return_values.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --add-dealloc --kernel-execution --canonicalize %s | \
// RUN: cudaq-opt --add-dealloc --kernel-execution=codegen=1 --canonicalize %s | \
// RUN: cudaq-translate --convert-to=qir | FileCheck %s

// NB: the mangled name map is required for the kernel-execution pass.
Expand Down
4 changes: 2 additions & 2 deletions test/Quake/kernel_exec-1.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --kernel-execution %s | FileCheck %s
// RUN: cudaq-opt --kernel-execution=codegen=1 %s | FileCheck %s
// RUN: cudaq-opt --kernel-execution=codegen=2 %s | FileCheck --check-prefix=STREAM %s
// RUN: cudaq-opt --kernel-execution=codegen=0 %s | FileCheck --check-prefix=HYBRID %s
// RUN: cudaq-opt --kernel-execution %s | FileCheck --check-prefix=HYBRID %s

module attributes {quake.mangled_name_map = {
__nvqpp__mlirgen__ghz = "_ZN3ghzclEi"}} {
Expand Down
2 changes: 1 addition & 1 deletion test/Quake/kernel_exec-2.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --kernel-execution %s | FileCheck %s
// RUN: cudaq-opt --kernel-execution=codegen=1 %s | FileCheck %s

module attributes {quake.mangled_name_map = {
__nvqpp__mlirgen__function_hawaiian = "shirt",
Expand Down
2 changes: 1 addition & 1 deletion test/Quake/return_vector.qke
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// the terms of the Apache License 2.0 which accompanies this distribution. //
// ========================================================================== //

// RUN: cudaq-opt --add-dealloc --kernel-execution --canonicalize %s | \
// RUN: cudaq-opt --add-dealloc --kernel-execution=codegen=1 --canonicalize %s | \
// RUN: FileCheck %s

// NB: the mangled name map is required for the kernel-execution pass.
Expand Down

0 comments on commit 18c305b

Please sign in to comment.