Skip to content

Commit

Permalink
Merge branch 'feature/add-RAM-usage-property' into dev-0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
c-trejos committed Feb 28, 2020
2 parents 703c9f9 + ec6677c commit 2cc637c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 3 deletions.
16 changes: 16 additions & 0 deletions r2i/iparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class IParameters {
*/
virtual RuntimeError Get (const std::string &in_parameter, int &value) = 0;

/**
* \brief Queries a double parameter.
* \param in_parameter Name of the parameter to get a value
* \param value [out] Return value of the parameter to query
* \return RuntimeError with a description of an error.
*/
virtual RuntimeError Get (const std::string &in_parameter, double &value) = 0;

/**
* \brief Queries a string parameter.
* \param in_parameter Name of the parameter to get a value
Expand All @@ -89,6 +97,14 @@ class IParameters {
*/
virtual RuntimeError Set (const std::string &in_parameter, int in_value) = 0;

/**
* \brief Sets an double parameter.
* \param in_parameter Name of the parameter to set a value
* \param in_value New value to set for in_parameter
* \return RuntimeError with a description of an error.
*/
virtual RuntimeError Set (const std::string &in_parameter, double in_value) = 0;

/**
* \brief Lists the available parameters for this framework
* \param metas [out] A vector of ParameterMeta with the description of
Expand Down
7 changes: 6 additions & 1 deletion r2i/parametermeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ struct ParameterMeta {
/**
* A standard string
*/
STRING
STRING,

/**
* System dependent double
*/
DOUBLE
};

/**
Expand Down
16 changes: 14 additions & 2 deletions r2i/tensorflow/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
*/

#include "r2i/tensorflow/engine.h"

#include <tensorflow/c/c_api.h>

#include "r2i/tensorflow/prediction.h"
#include "r2i/tensorflow/frame.h"

Expand Down Expand Up @@ -55,6 +53,18 @@ RuntimeError Engine::SetModel (std::shared_ptr<r2i::IModel> in_model) {
return error;
}

RuntimeError Engine::SetMemoryUsage (double memory_usage) {
RuntimeError error;

if (memory_usage > 1.0 || memory_usage < 0.1) {
error.Set (RuntimeError::Code::WRONG_API_USAGE, "Invalid memory usage value");
return error;
}

this->session_memory_usage_index = (static_cast<int>(memory_usage * 10) - 1);
return error;
}

static RuntimeError FreeSession (TF_Session *session) {
RuntimeError error;
std::shared_ptr<TF_Status> pstatus(TF_NewStatus (), TF_DeleteStatus);
Expand Down Expand Up @@ -97,6 +107,8 @@ RuntimeError Engine::Start () {
TF_Graph *graph = pgraph.get();
TF_Status *status = pstatus.get ();
TF_SessionOptions *opt = popt.get ();
TF_SetConfig(opt, this->config[this->session_memory_usage_index],
RAM_ARRAY_SIZE, status);

std::shared_ptr<TF_Session> session (TF_NewSession(graph, opt, status),
FreeSession);
Expand Down
19 changes: 19 additions & 0 deletions r2i/tensorflow/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <r2i/tensorflow/model.h>

#define RAM_ARRAY_SIZE 11

namespace r2i {
namespace tensorflow {

Expand All @@ -27,6 +29,8 @@ class Engine : public IEngine {

r2i::RuntimeError SetModel (std::shared_ptr<r2i::IModel> in_model) override;

r2i::RuntimeError SetMemoryUsage (double memory_usage);

r2i::RuntimeError Start () override;

r2i::RuntimeError Stop () override;
Expand All @@ -41,10 +45,25 @@ class Engine : public IEngine {
STARTED,
STOPPED
};

State state;
int session_memory_usage_index;

std::shared_ptr<TF_Session> session;
std::shared_ptr<Model> model;

const uint8_t config[10][RAM_ARRAY_SIZE] = {
{0x32, 0x9, 0x9, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xb9, 0x3f},
{0x32, 0x9, 0x9, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xc9, 0x3f},
{0x32, 0x9, 0x9, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f},
{0x32, 0x9, 0x9, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xd9, 0x3f},
{0x32, 0x9, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe0, 0x3f},
{0x32, 0x9, 0x9, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xe3, 0x3f},
{0x32, 0x9, 0x9, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xe6, 0x3f},
{0x32, 0x9, 0x9, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xe9, 0x3f},
{0x32, 0x9, 0x9, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xec, 0x3f},
{0x32, 0x9, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf0, 0x3f}
};
};

}
Expand Down
39 changes: 39 additions & 0 deletions r2i/tensorflow/parameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Parameters::Parameters () :
r2i::ParameterMeta::Flags::READ,
r2i::ParameterMeta::Type::STRING,
std::make_shared<VersionAccessor>(this)),
PARAM("gpu-memory-usage", "Per process GPU memory usage fraction",
r2i::ParameterMeta::Flags::READWRITE | r2i::ParameterMeta::Flags::WRITE_BEFORE_START,
r2i::ParameterMeta::Type::DOUBLE,
std::make_shared<MemoryUsageAccessor>(this)),

/* Model parameters */
PARAM("input-layer", "Name of the input layer in the graph",
Expand Down Expand Up @@ -147,6 +151,25 @@ RuntimeError Parameters::Get (const std::string &in_parameter, int &value) {
return error;
}

RuntimeError Parameters::Get (const std::string &in_parameter, double &value) {
RuntimeError error;

ParamDesc param = this->Validate (in_parameter,
r2i::ParameterMeta::Type::DOUBLE, "double", error);
if (error.IsError ()) {
return error;
}

auto accessor = std::dynamic_pointer_cast<DoubleAccessor>(param.accessor);

error = accessor->Get ();
if (error.IsError ()) {
return error;
}

value = accessor->value;
return error;
}

RuntimeError Parameters::Get (const std::string &in_parameter,
std::string &value) {
Expand Down Expand Up @@ -195,6 +218,22 @@ RuntimeError Parameters::Set (const std::string &in_parameter,
return accessor->Set ();
}

RuntimeError Parameters::Set (const std::string &in_parameter,
double in_value) {
RuntimeError error;

ParamDesc param = this->Validate (in_parameter,
r2i::ParameterMeta::Type::DOUBLE, "double", error);
if (error.IsError ()) {
return error;
}

auto accessor = std::dynamic_pointer_cast<DoubleAccessor>(param.accessor);

accessor->value = in_value;
return accessor->Set ();
}

RuntimeError Parameters::Set (const std::string &in_parameter, int in_value) {
RuntimeError error;

Expand Down
22 changes: 22 additions & 0 deletions r2i/tensorflow/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Parameters : public IParameters {

RuntimeError Get (const std::string &in_parameter, int &value) override;

RuntimeError Get (const std::string &in_parameter, double &value) override;

RuntimeError Get (const std::string &in_parameter,
std::string &value) override;

Expand All @@ -45,6 +47,8 @@ class Parameters : public IParameters {

RuntimeError Set (const std::string &in_parameter, int in_value) override;

RuntimeError Set (const std::string &in_parameter, double in_value) override;

RuntimeError List (std::vector<ParameterMeta> &metas) override;

private:
Expand Down Expand Up @@ -75,6 +79,12 @@ class Parameters : public IParameters {
int value;
};

class DoubleAccessor : public Accessor {
public:
DoubleAccessor (Parameters *target) : Accessor(target) {}
double value;
};

class VersionAccessor : public StringAccessor {
public:
VersionAccessor (Parameters *target) : StringAccessor(target) {}
Expand All @@ -89,6 +99,18 @@ class Parameters : public IParameters {
}
};

class MemoryUsageAccessor : public DoubleAccessor {
public:
MemoryUsageAccessor (Parameters *target) : DoubleAccessor(target) {}
RuntimeError Set () {
return target->engine->SetMemoryUsage(this->value);
}

RuntimeError Get () {
return RuntimeError ();
}
};

class InputLayerAccessor : public StringAccessor {
public:
InputLayerAccessor (Parameters *target) : StringAccessor(target) {}
Expand Down

0 comments on commit 2cc637c

Please sign in to comment.