Beatmup
dense.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 #include "../gpu/linear_mapping.h"
22 #include "operation.h"
23 #include "storage.h"
24 
25 namespace Beatmup {
26  namespace NNets {
27 
28  /**
29  Dense (linear) layer.
30  Computes `A*x + b` for input feature vector `x`, a matrix `A` and an optional bias vector `b`.
31  Accepts a GL::Vector or a flat Storage view on input, amd only a GL::Vector on output.
32 
33  Constraints:
34  - Number of input channels must be a multiple of 8.
35  - No activation function is applied on output (not yet implemented).
36 
37  The matrix and bias coefficients are searched in chunks. The chunk names consist of the operation name followed
38  by Dense::MATRIX_CHUNK_SUFFIX and Dense::BIAS_CHUNK_SUFFIX respectively.
39  The chunk contents is a single precision floating point array.
40  The matrix coefficients are taken in row-major order.
41  */
42  class Dense : public AbstractOperation, private GL::LinearMapping {
43  private:
44  const int numOutputDims; //!< output feature vector size / number of rows in `A`
45  const bool useBias; //!< if `true`, the bias vector `b` is searched in the model data to add to the output
46  GL::Vector* inputVector; //!< if not null, points the input vector `x`; otherwise the input is a storage view
47  GL::Vector* outputVector; //!< pointer to the output vector
48  Storage::View inputStorage; //!< if not empty, contains the input features; otherwise the input is a GL vector
49 
50  void prepare(GraphicPipeline& gpu, ChunkCollection& data, GL::ProgramBank& bank);
51  void execute(TaskThread& thread, GraphicPipeline& gpu);
52  void getSampledChannels(int index, int& min, int& max) const;
53 
54  public:
55  static const char* MATRIX_CHUNK_SUFFIX; //!< suffix added to the op name to get the matrix chunk id in the model data
56  static const char* BIAS_CHUNK_SUFFIX; //!< suffix added to the op name to get the bias chunk id in the model data
57 
58  /**
59  Creates a Dense operation.
60  \param context A context instance
61  \param name Operation name
62  \param numOutputDims Number of output dimensions
63  \param useBias If `true`, the bias vector addition is enabled.
64  */
65  Dense(Context& context, const std::string& name, int numOutputDims, bool useBias);
66 
67  inline Size getOutputSize(int outputIndex = 0) const {
68  return Size(1, numOutputDims, 1);
69  }
70 
71  inline int getInputCount() const { return 1; }
72  inline int getOutputCount() const { return 1; }
73 
74  inline bool acceptsStorageInput(int index = 0) const { return index == 0; }
75  inline bool acceptsVectorInput(int index = 0) const { return index == 0; }
76  inline bool acceptsVectorOutput(int index = 0) const { return index == 0; }
77 
78  void getOutput(GL::Vector*&, int index = 0);
79 
80  void setInput(Storage::View&& view, int index = 0);
81  void setInput(GL::Vector& vector, int index = 0);
82  void setOutput(GL::Vector& vector, int index = 0);
83 
84  /**
85  \return a supported data format for GL::Vector on output.
86  */
88 
89  std::map<std::string, std::string> serialize() const;
90  void disconnect();
91 
92  unsigned long countMultiplyAdds() const;
93  unsigned long countTexelFetches() const;
94 
95  /**
96  Sets up deserialization of the operation.
97  */
98  static bool initDeserializer();
99  };
100 
101  /**
102  \internal
103  Being declared here, this variable ensures Dense::initDeserializer() is called with inclusion of this header file.
104  */
106  }
107 }
A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
Definition: chunkfile.h:36
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
Evaluates expression A*x + b = y for a matrix A and vectors x and b using GPU.
Stores linked GLSL programs and their associated fragment shader codes.
Definition: program_bank.h:31
Real-valued vector usable by GPU.
Format
Vector data format.
Internal low-level GPU control API.
Definition: pipeline.h:33
Abstract neural net operation (layer).
Definition: operation.h:46
Dense (linear) layer.
Definition: dense.h:42
static const char * BIAS_CHUNK_SUFFIX
suffix added to the op name to get the bias chunk id in the model data
Definition: dense.h:56
GL::Vector * outputVector
pointer to the output vector
Definition: dense.h:47
GL::Vector * inputVector
if not null, points the input vector x; otherwise the input is a storage view
Definition: dense.h:46
int getInputCount() const
Returns number of operation inputs.
Definition: dense.h:71
void setInput(Storage::View &&view, int index=0)
Definition: dense.cpp:46
static const char * MATRIX_CHUNK_SUFFIX
suffix added to the op name to get the matrix chunk id in the model data
Definition: dense.h:55
void setOutput(GL::Vector &vector, int index=0)
Definition: dense.cpp:65
Size getOutputSize(int outputIndex=0) const
Returns full size of a specific operation output.
Definition: dense.h:67
unsigned long countMultiplyAdds() const
Counts (approximate) number of multiply-adds used by this operation.
Definition: dense.cpp:121
GL::Vector::Format getOutputVectorFormat() const
Definition: dense.cpp:70
unsigned long countTexelFetches() const
Counts (approximate) number of texels fetches.
Definition: dense.cpp:127
void getOutput(GL::Vector *&, int index=0)
Returns a GL::Vector bound to a specific operation output.
Definition: dense.cpp:40
Storage::View inputStorage
if not empty, contains the input features; otherwise the input is a GL vector
Definition: dense.h:48
bool acceptsVectorInput(int index=0) const
Returns true if the operation can take a GL::Vector at a specific input.
Definition: dense.h:75
void getSampledChannels(int index, int &min, int &max) const
Retrieves range of input features channels sampled at the same time for a specific input.
Definition: dense.cpp:186
std::map< std::string, std::string > serialize() const
Returns a serialized representation of th operation;.
Definition: dense.cpp:79
static bool initDeserializer()
Sets up deserialization of the operation.
void execute(TaskThread &thread, GraphicPipeline &gpu)
Executes the operation.
Definition: dense.cpp:175
Dense(Context &context, const std::string &name, int numOutputDims, bool useBias)
Creates a Dense operation.
Definition: dense.cpp:30
const bool useBias
if true, the bias vector b is searched in the model data to add to the output
Definition: dense.h:45
int getOutputCount() const
Returns number of operation outputs.
Definition: dense.h:72
const int numOutputDims
output feature vector size / number of rows in A
Definition: dense.h:44
bool acceptsStorageInput(int index=0) const
Returns true if the operation can take a Storage::View at a specific input.
Definition: dense.h:74
void prepare(GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank)
Compiles GLSL shaders.
Definition: dense.cpp:144
bool acceptsVectorOutput(int index=0) const
Returns true if the operation can take a GL::Vector at a specific output.
Definition: dense.h:76
void disconnect()
Assigns empty inputs and outputs.
Definition: dense.cpp:114
Operation 3D input/output size.
Definition: storage.h:37
Maps a 3D tensor onto a storage.
Definition: storage.h:308
Thread executing tasks.
Definition: parallelism.h:154
static const bool DENSE_OP_DESERIALIZABLE
Definition: dense.h:105
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
jlong jint index