Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions genc/cc/examples/executors/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ pybind_extension(
tags = ["generated_py_module=genc.examples.executors"],
deps = [
":executor_stacks",
"//genc/cc/runtime:control_flow_executor",
"//genc/cc/runtime:executor",
"//genc/cc/runtime:executor_bindings",
"//genc/proto/v0:computation_cc_proto",
"@com_google_absl//absl/status:statusor",
"@pybind11_abseil//pybind11_abseil:absl_casters",
"@pybind11_abseil//pybind11_abseil:status_casters",
"@pybind11_protobuf//pybind11_protobuf:native_proto_caster",
Expand All @@ -33,6 +36,7 @@ cc_library(
"//genc/cc/modules/retrieval:local_cache",
"//genc/cc/modules/tools:wolfram_alpha",
"//genc/cc/runtime:concurrency",
"//genc/cc/runtime:control_flow_executor",
"//genc/cc/runtime:executor",
"//genc/cc/runtime:executor_stacks",
"//genc/cc/runtime:status_macros",
Expand Down
65 changes: 64 additions & 1 deletion genc/cc/examples/executors/executor_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ limitations under the License
// - Python methods defined here (e.g. `.def_*()`) should not contain
// "business logic". That should be implemented on the underlying C++ class.

#include <memory>

#include "absl/status/statusor.h"
#include "genc/cc/examples/executors/executor_stacks.h"
#include "genc/cc/runtime/control_flow_executor.h"
#include "genc/cc/runtime/executor.h"
#include "genc/proto/v0/computation.pb.h"
#include "include/pybind11/cast.h"
Expand Down Expand Up @@ -48,7 +52,66 @@ namespace {
PYBIND11_MODULE(executor_bindings, m) {
py::google::ImportStatusModule();

m.def("create_default_executor", &CreateDefaultExecutor,
// Provide an `OwnedValueId` class to handle return values from the
// `Executor` interface.
//
// Note: no `init<>()` method defined, this object is only constructor from
// Executor instances.
py::class_<OwnedValueId>(m, "OwnedValueId")
.def_property_readonly("ref", &OwnedValueId::ref)
.def("__str__",
[](const OwnedValueId& self) { return absl::StrCat(self.ref()); })
.def("__repr__", [](const OwnedValueId& self) {
return absl::StrCat("<OwnedValueId: ", self.ref(), ">");
});

// Provide the `Executor` interface.
//
// A `dispose` method is purposely not exposed. Though `Executor::Dispose`
// exists in C++, Python should call `Dispose` via the `OwnedValueId`
// destructor during garbage collection.
//
// Note: no `init<>()` method defined, must be constructed useing the create_*
// methods defined below.
py::class_<Executor, std::shared_ptr<Executor>>(m, "Executor")
.def("create_value", &Executor::CreateValue, py::arg("value_pb"),
py::return_value_policy::move,
py::call_guard<py::gil_scoped_release>())
.def("create_struct", &Executor::CreateStruct,
py::return_value_policy::move,
py::call_guard<py::gil_scoped_release>())
.def("create_selection", &Executor::CreateSelection,
py::return_value_policy::move,
py::call_guard<py::gil_scoped_release>())
.def("create_call", &Executor::CreateCall, py::arg("function"),
// Allow `argument` to be `None`.
py::arg("argument").none(true), py::return_value_policy::move,
py::call_guard<py::gil_scoped_release>())
.def(
"materialize",
[](Executor& e,
const ValueId& value_id) -> absl::StatusOr<v0::Value> {
// Construct a new `v0::Value` to write to and return it to Python.
v0::Value value_pb;
absl::Status result = e.Materialize(value_id, &value_pb);
if (!result.ok()) {
return result;
}
return value_pb;
},
py::call_guard<py::gil_scoped_release>());

m.def("create_default_executor",
[]() -> absl::StatusOr<std::shared_ptr<
::genc::Executor>> {
absl::StatusOr<std::shared_ptr<::genc::Executor>> ex =
::genc::CreateDefaultExecutor();
if (!ex.ok()) {
return ex.status();
}
return ex.value();
},
py::call_guard<py::gil_scoped_release>(),
"Creates a default executor with predefined components used in "
"GenC demos.");
}
Expand Down
7 changes: 5 additions & 2 deletions genc/cc/examples/executors/executor_stacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License
#include "genc/cc/modules/retrieval/local_cache.h"
#include "genc/cc/modules/tools/wolfram_alpha.h"
#include "genc/cc/runtime/concurrency.h"
#include "genc/cc/runtime/control_flow_executor.h"
#include "genc/cc/runtime/executor.h"
#include "genc/cc/runtime/executor_stacks.h"
#include "genc/cc/runtime/status_macros.h"
Expand Down Expand Up @@ -86,8 +87,10 @@ void SetLlamaCppModelInferenceHandler(intrinsics::HandlerSetConfig* config,
} // namespace

absl::StatusOr<std::shared_ptr<Executor>> CreateDefaultExecutor() {
return CreateDefaultExecutorWithConcurrencyInterface(
CreateThreadBasedConcurrencyManager());
absl::StatusOr<std::shared_ptr<Executor>> result =
CreateDefaultExecutorWithConcurrencyInterface(
CreateThreadBasedConcurrencyManager());
return result;
}

absl::StatusOr<std::shared_ptr<Executor>>
Expand Down
1 change: 1 addition & 0 deletions genc/cc/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pybind_extension(
srcs = ["executor_bindings.cc"],
tags = ["generated_py_module=genc.runtime"],
deps = [
":control_flow_executor",
":executor",
":executor_stacks",
"//genc/proto/v0:computation_cc_proto",
Expand Down
5 changes: 3 additions & 2 deletions genc/cc/runtime/control_flow_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,9 @@ absl::StatusOr<std::shared_ptr<Executor>> CreateControlFlowExecutor(
std::shared_ptr<IntrinsicHandlerSet> handler_set,
std::shared_ptr<Executor> child_executor,
std::shared_ptr<ConcurrencyInterface> concurrency_interface) {
return std::make_shared<ControlFlowExecutor>(handler_set, child_executor,
concurrency_interface);
return std::shared_ptr<Executor>(
new ControlFlowExecutor(
handler_set, child_executor, concurrency_interface));
}

} // namespace genc
1 change: 1 addition & 0 deletions genc/cc/runtime/executor_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ limitations under the License
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "genc/cc/runtime/control_flow_executor.h"
#include "genc/cc/runtime/executor.h"
#include "genc/cc/runtime/executor_stacks.h"
#include "genc/proto/v0/computation.pb.h"
Expand Down
5 changes: 5 additions & 0 deletions genc/python/examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ py_test(
name = "authoring_demo",
srcs = ["authoring_demo.py"],
deps = [
":executor",
"//genc",
"//genc/cc/examples/executors:executor_bindings",
"//genc/cc/runtime:executor_bindings",
"//genc/python/runtime",
],
)

Expand All @@ -35,6 +38,8 @@ py_binary(
srcs = ["langchain_demo.py"],
deps = [
":executor",
"//genc/cc/examples/executors:executor_bindings",
"//genc/cc/runtime:executor_bindings",
"//genc/proto/v0:computation_py_pb2",
"//genc/python/interop",
"//genc/python/runtime",
Expand Down
6 changes: 3 additions & 3 deletions genc/python/examples/authoring_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from absl.testing import absltest
import genc as genc
from genc.cc.runtime import executor_bindings
from genc.python.examples import executor

# pylint:disable=missing-class-docstring
# pylint:disable=missing-function-docstring]
Expand All @@ -24,8 +24,8 @@
class AuthoringTest(absltest.TestCase):

def test_something(self):
genc.runtime.set_default_executor(
executor_bindings.create_default_local_executor())
my_executor = executor.create_default_executor()
genc.runtime.set_default_executor(my_executor)

@genc.authoring.traced_computation
def comp(x):
Expand Down
3 changes: 2 additions & 1 deletion genc/python/examples/langchain_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def main(argv: Sequence[str]) -> None:
)

comp_pb = interop.langchain.create_computation(my_chain)
comp = runtime.Runner(comp_pb, executor.create_default_executor())
my_executor = executor.create_default_executor()
comp = runtime.Runner(comp_pb, my_executor)
result = comp("a grocery store")
print(result)

Expand Down