compiler_gym/service/runtime

This directory contains the CompilerGym runtime that takes a compiler_gym::CompilationSession subclass and provides an RPC service that can be used by the Python frontend.

Runtime.h

#include "compiler_gym/service/runtime/Runtime.h"

namespace compiler_gym
namespace runtime

Functions

template<typename CompilationSessionType>
int createAndRunCompilerGymService(int argc, char **argv, const char *usage)

Create and run an RPC service for the given compilation session.

This should be called on its own in a self contained script to implement a compilation service. Example:

#include "compiler_gym/service/runtime/Runtime.h"
#include "my_compiler_service/MyCompilationSession.h"

int main(int argc, char** argv) {
  return createAndRunCompilerGymService<MyCompilationSession>(
      argc, argc, "My compiler service"
  );
}

Template Parameters

CompilationSessionType – A sublass of CompilationSession that provides implementations of the abstract methods.

Returns

An integer return code.

CompilerGymService.h

#include "compiler_gym/service/runtime/CompilerGymService.h"

namespace compiler_gym
namespace runtime
template<typename CompilationSessionType>
class CompilerGymService : public compiler_gym::CompilerGymService::Service
#include <CompilerGymService.h>

A default implementation of the CompilerGymService.

When parametrized by a CompilationSession subclass, this provides the RPC handling logic to run a gym service. User should call createAndRunCompilerGymService() rather than interacting with this class directly.

Public Functions

CompilerGymService(const boost::filesystem::path &workingDirectory, std::unique_ptr<BenchmarkCache> benchmarks = nullptr)
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
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
grpc::Status SendSessionParameter(grpc::ServerContext *context, const SendSessionParameterRequest *request, SendSessionParameterReply *reply) final override
inline BenchmarkCache &benchmarks()
inline int sessionCount() const

Protected Functions

grpc::Status session(uint64_t id, CompilationSession **environment)
grpc::Status session(uint64_t id, const CompilationSession **environment) const
grpc::Status action_space(const CompilationSession *session, int index, const ActionSpace **actionSpace) const
grpc::Status observation_space(const CompilationSession *session, int index, const ObservationSpace **observationSpace) const
inline const boost::filesystem::path &workingDirectory() const
uint64_t addSession(std::unique_ptr<CompilationSession> session)
grpc::Status handleBuiltinSessionParameter(const std::string &key, const std::string &value, std::optional<std::string> &reply)

Private Members

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_
std::mutex sessionsMutex_
uint64_t nextSessionId_

BenchmarkCache.h

#include "compiler_gym/service/runtime/BenchmarkCache.h"

namespace compiler_gym
namespace runtime

Variables

constexpr size_t kEvictionSizeInBytes = 256 * 1024 * 1024
class BenchmarkCache
#include <BenchmarkCache.h>

A cache of Benchmark protocol messages.

This object caches Benchmark messages by URI. Once the cache reaches a predetermined size, benchmarks are evicted randomly until the capacity is reduced to 50%.

Public Functions

BenchmarkCache(size_t maxSizeInBytes = kEvictionSizeInBytes, std::optional<std::mt19937_64> rand = std::nullopt)

Constructor.

Parameters
  • maxSizeInBytes – The maximum size of the benchmark buffer before an automated eviction is run.

  • rand – A random start used for selecting benchmarks for random eviction.

const Benchmark *get(const std::string &uri) const

Lookup a benchmark.

The pointer set by this method is valid only until the next call to add().

Parameters

uri – The URI of the benchmark.

Returns

A Benchmark pointer.

void add(const Benchmark &&benchmark)

Move-insert the given benchmark to the cache.

Parameters

benchmark – A benchmark to insert.

inline size_t size() const

Get the number of elements in the cache.

Returns

A nonnegative integer.

inline size_t sizeInBytes() const

Get the size of the cache in bytes.

Returns

A nonnegative integer.

inline size_t maxSizeInBytes() const

The maximum size of the cache before an eviction.

Returns

A nonnegative integer.

void setMaxSizeInBytes(size_t maxSizeInBytes)

Set a new maximum size of the cache.

Parameters

maxSizeInBytes – A number of bytes.

void evictToCapacity(std::optional<size_t> targetSizeInBytes = std::nullopt)

Evict benchmarks randomly to reduce the capacity to the given size.

If targetSizeInBytes is not provided, benchmarks are evicted to 50% of maxSizeInBytes.

Parameters

targetSizeInBytes – A target maximum size in bytes.

Private Members

std::unordered_map<std::string, const Benchmark> benchmarks_
std::mt19937_64 rand_
size_t maxSizeInBytes_
size_t sizeInBytes_