Skip to content

Commit

Permalink
[service] Add a reusable CompilationService runtime.
Browse files Browse the repository at this point in the history
This adds a new CompilerGymService class in both C++ and Python that
takes a concrete CompilationSession subclass and provides all of the
runtime logic to start and manage and RPC server that responds to
requests and dispatches to CompilationSession instances.

Issue facebookresearch#254.
  • Loading branch information
ChrisCummins committed May 12, 2021
1 parent 9fe88dc commit 88e7c3b
Show file tree
Hide file tree
Showing 9 changed files with 802 additions and 1 deletion.
76 changes: 75 additions & 1 deletion compiler_gym/service/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ py_library(
srcs = ["__init__.py"],
visibility = ["//visibility:public"],
deps = [
":benchmark_cache",
":create_and_run_compiler_gym_service",
],
)

cc_library(
name = "cc_runtime",
hdrs = ["Runtime.h"],
visibility = ["//visibility:public"],
deps = [
":CreateAndRunCompilerGymServiceImpl",
],
)

Expand All @@ -38,3 +47,68 @@ cc_library(
"@glog",
],
)

py_library(
name = "compiler_gym_service",
srcs = ["compiler_gym_service.py"],
deps = [
":benchmark_cache",
"//compiler_gym/service:compilation_session",
"//compiler_gym/service/proto",
"//compiler_gym/util",
],
)

cc_library(
name = "CompilerGymService",
hdrs = [
"CompilerGymService.h",
"CompilerGymServiceImpl.h",
],
visibility = ["//tests/service/runtime:__subpackages__"],
deps = [
":BenchmarkCache",
":CompilerGymServiceImpl",
"//compiler_gym/service:CompilationSession",
"//compiler_gym/service/proto:compiler_gym_service_cc",
"//compiler_gym/service/proto:compiler_gym_service_cc_grpc",
"@boost//:filesystem",
"@com_github_grpc_grpc//:grpc++",
],
)

cc_library(
name = "CompilerGymServiceImpl",
hdrs = ["CompilerGymServiceImpl.h"],
deps = [
"//compiler_gym/util:GrpcStatusMacros",
"//compiler_gym/util:Version",
"@fmt",
"@glog",
],
)

py_library(
name = "create_and_run_compiler_gym_service",
srcs = ["create_and_run_compiler_gym_service.py"],
deps = [
":compiler_gym_service",
"//compiler_gym/service/proto",
"//compiler_gym/util",
],
)

cc_library(
name = "CreateAndRunCompilerGymServiceImpl",
srcs = ["CreateAndRunCompilerGymServiceImpl.cc"],
hdrs = ["CreateAndRunCompilerGymServiceImpl.h"],
deps = [
":CompilerGymService",
"//compiler_gym/util:GrpcStatusMacros",
"//compiler_gym/util:Unreachable",
"@boost//:filesystem",
"@com_github_grpc_grpc//:grpc++",
"@gflags",
"@glog",
],
)
88 changes: 88 additions & 0 deletions compiler_gym/service/runtime/CompilerGymService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#pragma once

#include <grpcpp/grpcpp.h>

#include <memory>
#include <mutex>

#include "boost/filesystem.hpp"
#include "compiler_gym/service/CompilationSession.h"
#include "compiler_gym/service/proto/compiler_gym_service.grpc.pb.h"
#include "compiler_gym/service/proto/compiler_gym_service.pb.h"
#include "compiler_gym/service/runtime/BenchmarkCache.h"

namespace compiler_gym::runtime {

// A default implementation of the CompilerGymService. When parametrized by a
// CompilationSession subclass, this provides the RPC handling logic to run a
// gym service.
template <typename CompilationSessionType>
class CompilerGymService final : public compiler_gym::CompilerGymService::Service {
public:
CompilerGymService(const boost::filesystem::path& workingDirectory,
std::unique_ptr<BenchmarkCache> benchmarks = nullptr);

// RPC endpoints.
grpc::Status GetVersion(grpc::ServerContext* context, const GetVersionRequest* request,
GetVersionReply* reply) final override;

grpc::Status GetSpaces(grpc::ServerContext* context, const GetSpacesRequest* request,
GetSpacesReply* reply) final override;

grpc::Status StartSession(grpc::ServerContext* context, const StartSessionRequest* request,
StartSessionReply* reply) final override;

grpc::Status ForkSession(grpc::ServerContext* context, const ForkSessionRequest* request,
ForkSessionReply* reply) final override;

grpc::Status EndSession(grpc::ServerContext* context, const EndSessionRequest* request,
EndSessionReply* reply) final override;

// NOTE: Step() is not thread safe. The underlying assumption is that each
// CompilationSessionType is managed by a single thread, so race conditions
// between operations that affect the same CompilationSessionType are not
// protected against.
grpc::Status Step(grpc::ServerContext* context, const StepRequest* request,
StepReply* reply) final override;

grpc::Status AddBenchmark(grpc::ServerContext* context, const AddBenchmarkRequest* request,
AddBenchmarkReply* reply) final override;

inline BenchmarkCache& benchmarks() { return *benchmarks_; }

protected:
[[nodiscard]] grpc::Status session(uint64_t id, CompilationSession** environment);

[[nodiscard]] grpc::Status session(uint64_t id, const CompilationSession** environment) const;

[[nodiscard]] grpc::Status action_space(const CompilationSession* session, int index,
const ActionSpace** actionSpace) const;

[[nodiscard]] grpc::Status observation_space(const CompilationSession* session, int index,
const ObservationSpace** observationSpace) const;

inline const boost::filesystem::path& workingDirectory() const { return workingDirectory_; }

// Add the given session and return its ID.
uint64_t addSession(std::unique_ptr<CompilationSession> session);

private:
const boost::filesystem::path workingDirectory_;
const std::vector<ActionSpace> actionSpaces_;
const std::vector<ObservationSpace> observationSpaces_;

std::unordered_map<uint64_t, std::unique_ptr<CompilationSession>> sessions_;
std::unique_ptr<BenchmarkCache> benchmarks_;

// Mutex used to ensure thread safety of creation and destruction of sessions.
std::mutex sessionsMutex_;
uint64_t nextSessionId_;
};

} // namespace compiler_gym::runtime

#include "compiler_gym/service/runtime/CompilerGymServiceImpl.h"
Loading

0 comments on commit 88e7c3b

Please sign in to comment.