Beatmup
Beatmup::NNets::DeserializedModel Class Reference

Model reconstructed from a serialized representation. More...

#include <deserialized_model.h>

Inheritance diagram for Beatmup::NNets::DeserializedModel:
Beatmup::NNets::Model Beatmup::GL::ProgramBank Beatmup::Object

Public Member Functions

 DeserializedModel (Context &context, const Listing &listing)
 
 DeserializedModel (Context &context, const std::string &str)
 Constructs a model from its serialized representation. More...
 
 ~DeserializedModel ()
 
- Public Member Functions inherited from Beatmup::NNets::Model
 Model (Context &context, std::initializer_list< AbstractOperation * > ops)
 Instantiates a model from a list of operations interconnecting them in a feedforward fashion. More...
 
 Model (Context &context)
 Instantiates an empty model. More...
 
 ~Model ()
 
void append (AbstractOperation *newOp, bool connect=false)
 Adds a new operation to the model. More...
 
void append (std::initializer_list< AbstractOperation * > newOps, bool connect=false)
 Adds new operations to the model. More...
 
void addOperation (const std::string &opName, AbstractOperation *newOp)
 Adds a new operation to the model before another operation in the execution order. More...
 
void addOperation (const AbstractOperation &operation, AbstractOperation *newOp)
 
void addConnection (const std::string &sourceOpName, const std::string &destOpName, int output=0, int input=0, int shuffle=0)
 Adds a connection between two given ops. More...
 
void addOutput (const std::string &operation, int output=0)
 Enables reading output data from the model memory through getOutputData(). More...
 
void addOutput (const AbstractOperation &operation, int output=0)
 
const float * getOutputData (size_t &numSamples, const std::string &operation, int output=0) const
 Reads data from the model memory. More...
 
const float * getOutputData (size_t &numSamples, const AbstractOperation &operation, int output=0) const
 
virtual void prepare (GraphicPipeline &gpu, ChunkCollection &data)
 Prepares all operations: reads the model data from chunks and builds GPU programs. More...
 
bool isReady () const
 
void execute (TaskThread &thread, GraphicPipeline *gpu)
 Runs the inference. More...
 
bool isOperationInModel (const AbstractOperation &operation) const
 Checks if a specific operation makes part of the model. More...
 
AbstractOperationgetFirstOperation ()
 
AbstractOperationgetLastOperation ()
 
const AbstractOperationgetFirstOperation () const
 
const AbstractOperationgetLastOperation () const
 
size_t getNumberOfOperations () const
 
template<class OperationClass = AbstractOperation>
OperationClass & getOperation (const std::string &operationName)
 Retrieves an operation by its name. More...
 
const ProgressTrackinggetPreparingProgress () const
 Returns model preparation progress tracking. More...
 
const ProgressTrackinggetInferenceProgress () const
 Returns inference progress tracking. More...
 
unsigned long countMultiplyAdds () const
 Provides an estimation of the number of multiply-adds characterizing the model complexity. More...
 
unsigned long countTexelFetches () const
 Provides an estimation of the total number of texels fetched by all the operations in the model per image. More...
 
size_t getMemorySize () const
 Returns the amount of texture memory in bytes currently allocated by the model to run the inference. More...
 
Listing serialize () const
 Returns serialized representation of the model as a Listing. More...
 
std::string serializeToString () const
 Returns serialized representation of the model as a string. More...
 
void setProfiler (Profiler *profiler)
 Attaches a profiler instance to meter the execution time per operation during the inference. More...
 
- Public Member Functions inherited from Beatmup::GL::ProgramBank
 ProgramBank (Context &context)
 
 ~ProgramBank ()
 
GL::RenderingProgramoperator() (GraphicPipeline &gpu, const std::string &code, bool enableExternalTextures=false)
 Provides a program given a fragment shader source code. More...
 
void release (GraphicPipeline &gpu, GL::RenderingProgram *program)
 Marks a program as unused any more. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Private Member Functions

void deserialize (const Listing &listing)
 

Private Attributes

std::vector< AbstractOperation * > ownedOps
 initially deserialized operations to be destroyed with the model More...
 

Additional Inherited Members

- Protected Member Functions inherited from Beatmup::NNets::Model
void freeMemory ()
 Frees all allocated storages. More...
 
StorageallocateStorage (GraphicPipeline &gpu, const Size size, bool forGpu=true, bool forCpu=false, const int pad=0, const int reservedChannels=0)
 Allocates a new storage. More...
 
StorageallocateFlatStorage (GraphicPipeline &gpu, const int size)
 Allocates a new flat storage. More...
 
GL::VectorallocateVector (GraphicPipeline &gpu, const int size)
 Allocates a vector that can be used as operation input or output. More...
 
InternalBitmapallocateTexture (GraphicPipeline &gpu, const Size size)
 Allocates a texture that can be used as operation input or output. More...
 
bool isPreceding (const AbstractOperation &first, const AbstractOperation &second) const
 Checks whether an operation goes before another operation in the model according the ops execution order. More...
 
AbstractOperationoperator[] (const std::string &operationName)
 
const AbstractOperationoperator[] (const std::string &operationName) const
 
void addConnection (AbstractOperation &source, AbstractOperation &dest, int output=0, int input=0, int shuffle=0)
 
- Protected Attributes inherited from Beatmup::NNets::Model
std::vector< AbstractOperation * > ops
 model operations More...
 
ProgressTracking preparingProgress
 model preparation progress More...
 
ProgressTracking inferenceProgress
 inference progress More...
 
bool ready
 if true, ops are connected to each other and storages are allocated More...
 
- Protected Attributes inherited from Beatmup::GL::ProgramBank
Contextcontext
 

Detailed Description

Model reconstructed from a serialized representation.

The representation format is the one rendered with Model::serialize(): a YAML-like listing containing "ops" and "connections" sections describing the model operations in execution order and connections between them respectively (see NNets model serialization format).

Definition at line 89 of file deserialized_model.h.

Constructor & Destructor Documentation

◆ DeserializedModel() [1/2]

DeserializedModel::DeserializedModel ( Context context,
const Listing listing 
)

Definition at line 60 of file deserialized_model.cpp.

60  :
61  Model(context)
62 {
63  deserialize(listing);
64 }
void deserialize(const Listing &listing)
Model(Context &context, std::initializer_list< AbstractOperation * > ops)
Instantiates a model from a list of operations interconnecting them in a feedforward fashion.
Definition: model.cpp:27

◆ DeserializedModel() [2/2]

DeserializedModel::DeserializedModel ( Context context,
const std::string &  str 
)

Constructs a model from its serialized representation.

The expected representation format is the one rendered with Model::serialize().

Parameters
[in]contextA context instance the model resources are bound to
[in]strA string containing the model representation

Definition at line 67 of file deserialized_model.cpp.

67  :
68  Model(context)
69 {
70  std::istringstream strstr(str);
71  Listing listing(strstr);
72  deserialize(listing);
73 }
Parser of simple YAML-like listings.
Definition: listing.h:40
JNIEnv jobject jstring str

◆ ~DeserializedModel()

DeserializedModel::~DeserializedModel ( )

Definition at line 76 of file deserialized_model.cpp.

76  {
77  for (auto op : ownedOps) {
78  ops.erase(std::remove(ops.begin(), ops.end(), op), ops.end());
79  delete op;
80  }
81 }
std::vector< AbstractOperation * > ownedOps
initially deserialized operations to be destroyed with the model
std::vector< AbstractOperation * > ops
model operations
Definition: model.h:122
seq remove(start, end)
jlong jint op

Member Function Documentation

◆ deserialize()

void DeserializedModel::deserialize ( const Listing listing)
private

Definition at line 26 of file deserialized_model.cpp.

26  {
27  InvalidArgument::check(listing.has("ops"), "The listing has no 'ops'");
28  InvalidArgument::check(listing.has("connections"), "The listing has no 'connections'");
29 
31 
32  // process ops
33  const auto& ops = listing["ops"];
34  for (const auto& op : ops) {
35  // find a deserializer
36  const std::string type = op["_type"];
37  auto john = theMap.find(type);
38  if (john == theMap.end())
39  throw InvalidArgument("Cannot find deserializer of type " + type + " declared in operation starting at line " + std::to_string(op.getLineNumber()));
40 
41  // create op
42  auto newOp = john->second->deserialize(context, op);
43  ownedOps.push_back(newOp);
44  append(newOp, false);
45  }
46 
47  // process connections
48  const auto conns = listing["connections"];
49  for (const auto& conn : conns)
51  conn["from"],
52  conn["to"],
53  conn.get<int>("from_output", 0),
54  conn.get<int>("to_input", 0),
55  conn.get<int>("shuffle", 0)
56  );
57 }
static void check(const bool condition, const std::string &message)
Definition: exception.h:75
bool has(const std::string &key) const
Returns true if a specific chapter is present in the listing.
Definition: listing.h:132
static std::map< std::string, Deserializer * > & getDeserializersMap()
Definition: operation.cpp:83
void addConnection(AbstractOperation &source, AbstractOperation &dest, int output=0, int input=0, int shuffle=0)
Definition: model.cpp:91
void append(AbstractOperation *newOp, bool connect=false)
Adds a new operation to the model.
Definition: model.cpp:47
std::string to_string(Beatmup::NNets::ActivationFunction function)

Member Data Documentation

◆ ownedOps

std::vector<AbstractOperation*> Beatmup::NNets::DeserializedModel::ownedOps
private

initially deserialized operations to be destroyed with the model

Definition at line 93 of file deserialized_model.h.


The documentation for this class was generated from the following files: