forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[aot] Serialize built graph, deserialize and run.
related: taichi-dev#4786 This PR demonstrates a minimal example of serializing a built graph, deserializing and running it. ghstack-source-id: e369b326734b3cffaf91262ed84806044e121c3c Pull Request resolved: taichi-dev#5016
- Loading branch information
Ailing Zhang
authored and
Ailing Zhang
committed
May 22, 2022
1 parent
fba92cf
commit 5e713b7
Showing
15 changed files
with
333 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "taichi/aot/graph_data.h" | ||
#include "taichi/program/ndarray.h" | ||
#define TI_RUNTIME_HOST | ||
#include "taichi/program/context.h" | ||
#undef TI_RUNTIME_HOST | ||
|
||
namespace taichi { | ||
namespace lang { | ||
namespace aot { | ||
void CompiledGraph::run( | ||
const std::unordered_map<std::string, IValue> &args) const { | ||
RuntimeContext ctx; | ||
for (const auto &dispatch : dispatches) { | ||
memset(&ctx, 0, sizeof(RuntimeContext)); | ||
|
||
TI_ASSERT(dispatch.compiled_kernel); | ||
// Populate args metadata into RuntimeContext | ||
const auto &symbolic_args_ = dispatch.symbolic_args; | ||
for (int i = 0; i < symbolic_args_.size(); ++i) { | ||
auto &symbolic_arg = symbolic_args_[i]; | ||
auto found = args.find(symbolic_arg.name); | ||
TI_ERROR_IF(found == args.end(), "Missing runtime value for {}", | ||
symbolic_arg.name); | ||
const aot::IValue &ival = found->second; | ||
if (ival.tag == aot::ArgKind::NDARRAY) { | ||
Ndarray *arr = reinterpret_cast<Ndarray *>(ival.val); | ||
TI_ERROR_IF(ival.tag != aot::ArgKind::NDARRAY, | ||
"Required a ndarray for argument {}", symbolic_arg.name); | ||
auto ndarray_elem_shape = std::vector<int>( | ||
arr->shape.end() - symbolic_arg.element_shape.size(), | ||
arr->shape.end()); | ||
TI_ERROR_IF(ndarray_elem_shape != symbolic_arg.element_shape, | ||
"Mismatched shape information for argument {}", | ||
symbolic_arg.name); | ||
set_runtime_ctx_ndarray(&ctx, i, arr); | ||
} else { | ||
TI_ERROR_IF(ival.tag != aot::ArgKind::SCALAR, | ||
"Required a scalar for argument {}", symbolic_arg.name); | ||
ctx.set_arg(i, ival.val); | ||
} | ||
} | ||
|
||
dispatch.compiled_kernel->launch(&ctx); | ||
} | ||
} | ||
} // namespace aot | ||
} // namespace lang | ||
} // namespace taichi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
#include "taichi/runtime/vulkan/runtime.h" | ||
|
||
namespace taichi { | ||
namespace lang { | ||
namespace vulkan { | ||
class KernelImpl : public aot::Kernel { | ||
public: | ||
explicit KernelImpl(VkRuntime *runtime, VkRuntime::RegisterParams &¶ms) | ||
: runtime_(runtime), params_(std::move(params)) { | ||
handle_ = runtime_->register_taichi_kernel(params_); | ||
} | ||
|
||
void launch(RuntimeContext *ctx) override { | ||
runtime_->launch_kernel(handle_, ctx); | ||
} | ||
|
||
const VkRuntime::RegisterParams ¶ms() { | ||
return params_; | ||
} | ||
|
||
private: | ||
VkRuntime *const runtime_; | ||
VkRuntime::KernelHandle handle_; | ||
const VkRuntime::RegisterParams params_; | ||
}; | ||
} // namespace vulkan | ||
} // namespace lang | ||
} // namespace taichi |
Oops, something went wrong.