Beatmup
operation.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 #include "../context.h"
21 #include "storage.h"
22 #include "../gpu/linear_mapping.h"
23 #include "../gpu/program.h"
24 #include "../gpu/program_bank.h"
25 #include "../utils/chunkfile.h"
26 #include "../utils/progress_tracking.h"
27 #include "../utils/string_builder.h"
28 #include "../utils/listing.h"
29 #include <string>
30 
31 namespace Beatmup {
32  namespace NNets {
33  class Model;
34 
35  /**
36  Abstract neural net operation (layer).
37  Has a name used to refer the operation in a Model. The operation data (such as convolution weights) is provided through a ChunkCollection
38  in single precision floating point format, where the chunks are searched by operation name.
39  Operations have several inputs and outputs numbered starting from zero. Different operations accept different kinds of input and output
40  data.
41  - Inputs may be Storage, GL::TextureHandler or GL::Vector.
42  - Outputs may be Storage or GL::Vector.
43  The operations are built in a deferred fashion during the first inference run, which makes the first run much slower than the subsequent
44  runs.
45  */
47  friend class Model;
48  AbstractOperation(const AbstractOperation&) = delete; //!< disabling copying constructor
49  private:
50  std::string name;
51 
52  protected:
53  AbstractOperation(const std::string& name): name(name) {}
54 
55  /**
56  Compiles GLSL shaders.
57  \param[in,out] gpu A graphic pipeline instance
58  \param[in,out] data Chunkfile containing operation data (e.g. weights and biases)
59  \param[in,out] bank A program bank with existing GLSL programs to be reused when possible.
60  If a new program is built, it is added to the bank.
61  */
62  virtual void prepare(GraphicPipeline& gpu, ChunkCollection& data, GL::ProgramBank& bank) = 0;
63 
64  /**
65  Executes the operation.
66  The operation should be prepared.
67  \param[in,out] thread Calling CPU thread descriptor
68  \param[in,out] gpu A graphic pipeline instance
69  */
70  virtual void execute(TaskThread& thread, GraphicPipeline& gpu) = 0;
71 
72  /**
73  Executes the operation within a specific CPU thread.
74  \param[in,out] thread Calling CPU thread descriptor
75  */
76  virtual void execute(TaskThread& thread) {}
77 
78  /**
79  Retrieves minimum required size of zero padding for a given input.
80  Operations that sample a neighborhood of a pixel may need the input to be padded with zeros, if some of the neighboring samples fall
81  out of the are containing data. In %Beatmup the zero padding is handled by allocating a bigger input and putting zeros around the area
82  that is actually filled with data.
83  \return number of zero columns and rows to be added to the input area.
84  */
85  virtual int getInputPadding(int index = 0) const { return 0; }
86 
87  /**
88  Retrieves range of input features channels sampled at the same time for a specific input.
89  The operation would typically take the entire storage and sample it at once, if needed. If the number of textures in a storage
90  exceeds the number of texture samplers that the GPU may use simultaneously, an exception occurs. This function provides the necessary
91  information to limit the number of textures in the storage when allocating it. When the limit is reached, multiple channels are
92  packed into a single texture in the storage.
93  \param[in] index The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive.
94  \param[out] min The minimum number of channels that can be sampled at once
95  \param[out] max The maximum number of channels that can be sampled at once
96  */
97  virtual void getSampledChannels(int index, int& min, int& max) const = 0;
98 
99  public:
101  public:
102  InconsistentModelData(const AbstractOperation* op, const char* shortDescription) :
103  Exception("Error when configuring '%s' operation: %s.", op->getName().c_str(), shortDescription)
104  {}
105 
106  InconsistentModelData(const AbstractOperation* op, const char* shortDescription, const size_t entriesRequested, const size_t sizeBytesGot) :
107  Exception("Error when configuring '%s' operation: %s. Cannot convert %u bytes of the given weights to %u requested entries.",
108  op->getName().c_str(), shortDescription, (unsigned int)sizeBytesGot, (unsigned int)entriesRequested)
109  {}
110  };
111 
112  class NotReady : public Exception {
113  public:
114  NotReady(const AbstractOperation* op): Exception("Operation not ready: '%s'", op->getName().c_str()) {}
115  };
116 
117  virtual ~AbstractOperation() {}
118 
119  /**
120  Returns `true` if the operation is run on GPU.
121  Otherwise, it is run on CPU.
122  */
123  virtual bool usesGpu() const { return true; }
124 
125  /**
126  Returns number of operation inputs.
127  Inputs are then indexed from zero to the returned value minus one inclusive.
128  */
129  virtual int getInputCount() const { return 0; }
130 
131  /**
132  Returns number of operation outputs.
133  Outputs are then indexed from zero to the returned value minus one inclusive.
134  */
135  virtual int getOutputCount() const { return 0; }
136 
137  /**
138  Returns `true` if the operation can take a Storage::View at a specific input.
139  Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector
140  and textures. This function is used to check whether a given operation accepts a storage view on input.
141  \param[in] index The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive.
142  */
143  virtual bool acceptsStorageInput(int index = 0) const { return false; }
144 
145  /**
146  Returns `true` if the operation can take a GL::Vector at a specific input.
147  Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector
148  and textures. This function is used to check whether a given operation accepts a vector on input.
149  \param[in] index The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive.
150  */
151  virtual bool acceptsVectorInput(int index = 0) const { return false; }
152 
153  /**
154  Returns `true` if the operation can take a GL::TextureHandler at a specific input.
155  Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector
156  and textures. This function is used to check whether a given operation accepts a texture on input.
157  \param[in] index The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive.
158  */
159  virtual bool acceptsTextureInput(int index = 0) const { return false; }
160 
161  /**
162  Returns `true` if the operation can take a Storage::View at a specific output.
163  Neural network operations may accept different kinds of data containers on outputs and outputs, namely Storage::View, GL::Vector
164  and textures. This function is used to check whether a given operation accepts a storage view on output.
165  \param[in] index The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive.
166  */
167  virtual bool acceptsStorageOutput(int index = 0) const { return false; }
168 
169  /**
170  Returns `true` if the operation can take a GL::Vector at a specific output.
171  Neural network operations may accept different kinds of data containers on outputs and outputs, namely Storage::View, GL::Vector
172  and textures. This function is used to check whether a given operation accepts a vector on output.
173  \param[in] index The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive.
174  */
175  virtual bool acceptsVectorOutput(int index = 0) const { return false; }
176 
177  /**
178  Returns `true` if the operation can take a GL::TextureHandler at a specific output.
179  Neural network operations may accept different kinds of data containers on outputs and outputs, namely Storage::View, GL::Vector
180  and textures. This function is used to check whether a given operation accepts a texture on output.
181  \param[in] index The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive.
182  */
183  virtual bool acceptsTextureOutput(int index = 0) const { return false; }
184 
185  /**
186  Returns full size of a specific operation output.
187  */
188  virtual Size getOutputSize(int outputIndex = 0) const;
189 
190  /**
191  Returns a storage view bound to a specific operation output.
192  If no view is bound, returns empty view.
193  \param[in] index The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive.
194  */
195  virtual Storage::View getOutput(int index = 0);
196 
197  /**
198  Returns a GL::Vector bound to a specific operation output.
199  \param[out] vector Pointer to the GL::Vector. If no vector is bound, becomes null.
200  \param[in] index The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive.
201  */
202  virtual void getOutput(GL::Vector*& vector, int index = 0);
203 
204  /**
205  Returns a GL::TextureHandler bound to a specific operation output.
206  \param[out] vector Pointer to the GL::TextureHandler. If no texture is bound, becomes null.
207  \param[in] index The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive.
208  */
209  virtual void getOutput(GL::TextureHandler*& vector, int index = 0);
210 
211  virtual void setInput(Storage::View&& storage, int index = 0);
212  virtual void setOutput(Storage::View&& storage, int index = 0);
213  virtual void setInput(GL::Vector& vector, int index = 0);
214  virtual void setOutput(GL::Vector& vector, int index = 0);
215  virtual void setInput(GL::TextureHandler& image, int index = 0);
216  virtual void setOutput(GL::TextureHandler& image, int index = 0);
217 
218  /**
219  Returns a serialized representation of th operation;
220  */
221  virtual std::map<std::string, std::string> serialize() const = 0;
222 
223  /**
224  Assigns empty inputs and outputs
225  */
226  virtual void disconnect() = 0;
227 
228  /**
229  Counts (approximate) number of multiply-adds used by this operation.
230  A single multiply-add is one multiplication and one addition.
231  */
232  virtual inline unsigned long countMultiplyAdds() const { return 0; }
233 
234  /**
235  Counts (approximate) number of texels fetches.
236  */
237  virtual inline unsigned long countTexelFetches() const { return 0; }
238 
239  /**
240  \return operation name
241  */
242  std::string getName() const { return name; }
243 
244  /**
245  Enables construction of an operation from its serialized representation.
246  Every new instance of this class is registered in a static map and used to build operations during the model deserialization.
247  */
248  class Deserializer {
249  public:
250  static std::map<std::string, Deserializer*>& getDeserializersMap();
251 
252  /**
253  Registers a new deserializer capable of building an operation from a listing block.
254  \param[in] opType Operation type
255  */
256  Deserializer(const char* opType);
257 
258  /**
259  Constructs an operation from a serialized representation (a listing block).
260  Raises exceptions if the representation contains errors or has missing parameters.
261  \param[in] context A context instance passed to the operation constructor if needed
262  \param[in] block The block containing the operation description
263  */
264  virtual AbstractOperation* deserialize(Context& context, const Listing::Block& block) = 0;
265  };
266  };
267 
268 
269  /**
270  Generates GLSL fragment shader code sampling a local neighborhood around the current texture coordinates for further filtering.
271  */
273  private:
274  const int nbSizeX, nbSizeY;
275  Point shift; //!< current static shift of the sampling position
276  float* deltas; //!< array storing pixel position differences for neighborhood sampling
277  bool useUniformShift; //!< if `true`, the sampling position can be shifted dynamically at every run
278 
279  /**
280  \return number of 2D delta-vectors stored in a uniform variable in the fragment shader
281  */
282  int getDeltasSize() const { return std::max(1, std::max(nbSizeX, nbSizeY) / 2); }
283 
284  protected:
285  static const char* SAMPLE_ID_PREFIX; //!< prefix of variables declaring a neighbor sample
286 
287  /**
288  Initializes spatial filtering mixin
289  \param[in] nbSizeX Neighborhood width in samples
290  \param[in] nbSizeY Neighborhood height in samples
291  */
292  SpatialFilteringMixin(const int nbSizeX, const int nbSizeY);
293 
295 
296  /**
297  Writes out the very GLSL fragment shader header required for spatial neighborhood sampling
298  \param[in,out] code The GLSL code storage
299  \param[in] useUniformShift If `true`, the sampling position can be shifted dynamically at every run
300  */
301  void writeHeader(StringBuilder& code, bool useUniformShift);
302 
303  /**
304  Declares GLSL fragment shader main(..) code part required for spatial neighborhood sampling
305  \param[in,out] code The GLSL code storage
306  \param[in] datatype The neighborhood samples datatype
307  \param[in] inlineSampling If `true`, sampled values are not stored in intermediate variables
308  (sampleInline() can only be used, not sample())
309  */
310  void declare(StringBuilder& code, const char* datatype, bool inlineSampling = false);
311 
312  /**
313  Samples a neighborhood of a given texture.
314  \param[in,out] code The GLSL code storage
315  \param[in] inputName The texture sampler array variable name
316  \param[in] inputIndex Index of the texture in the array
317  \param[in] shift Sampling position shift
318  \param[in] isFirstSample If `true`, this is a very first sampling operation. A required initial setup will be run.
319  \param[in] suffix Sampling operation suffix, e.g. ".rgb" to sample 3 values only
320  */
321  void sample(StringBuilder& code, const char* inputName, const int inputIndex, const Point& shift, const bool isFirstSample = true, const char* suffix = "");
322 
323  void sampleInline(StringBuilder& code, const char* inputName, const int inputIndex, const IntPoint& position, const Point& shift, const char* suffix = "");
324 
325  /**
326  Prepares the spatial filtering operation execution.
327  \param[in] width Input texture width in pixels
328  \param[in] height Input texture height in pixels
329  */
330  void setup(const int width, const int height);
331 
332  /**
333  Applies an offset to the sampling position at runtime. Only used if the uniform shift is enabled when writing out the header code.
334  \param[in,out] program The program
335  \param[in] shift The shift in pixels
336  \param[in] inputSize Size of the sampled input texture in pixels
337  */
338  void setUniformShift(GL::Program& program, const IntPoint& shift, const IntPoint& inputSize);
339 
340  /**
341  Prepares a given program for spatial filtering.
342  \param[in,out] program The program
343  */
344  void setupProgram(GL::Program& program);
345 
346  /**
347  Implements common padding policies by computing a rectangular area of positions the sampling kernel takes in order to get the result
348  with the required padding.
349  \param[in] size The input size in pixels
350  \param[in] stride The stride
351  \param[in] padding The padding policy
352  \return kernel center point positions in pixels.
353  */
354  IntRectangle getSamplingArea(const IntPoint& size, const IntPoint& stride, const Size::Padding padding) const;
355 
356  /**
357  Computes area in pixels to sample a given storage according to specific stride and padding.
358  \param[in] storage The input storage to sample
359  \param[in] channel Number of the channel to sample
360  \param[in] stride The stride
361  \param[in] padding The padding policy
362  \return kernel center point positions in pixels.
363  */
365  const Storage::View& storage,
366  const int channel,
367  const IntPoint& stride,
368  const Size::Padding padding
369  ) const;
370 
371  /**
372  Computes texture coordinates sampling a specific storage channel for given stride, padding and output size.
373  \param[in] storage The storage to sample
374  \param[in] channel The channel number in the storage to sample
375  \param[in] stride The stride
376  \param[in] padding The padding
377  \param[in] outputSize Size of output texture in pixels
378  \return rectangle containing texture coordinates.
379  */
381  const Storage::View& storage,
382  const int channel,
383  const IntPoint& stride,
384  const Size::Padding padding,
385  const IntPoint& outputSize
386  ) const;
387 
388  /**
389  Retrieves input sampling point position for the current fragment. Defined after declare() is called.
390  \return the GLSL expression of the sampling position.
391  */
392  std::string getInputSamplingPos() const;
393 
394  inline bool isUniformShiftUsed() const { return useUniformShift; }
395  };
396 
397 
398  /**
399  Activation function specification
400  */
401  enum class ActivationFunction {
402  DEFAULT, //!< default activation: 0..1 bounded ReLU (identity clipped to 0..1 range)
403  BRELU6, //!< 0.167 times identity clipped to 0..1 range
404  SIGMOID_LIKE //!< piecewise-linear sigmoid approximation
405  };
406 
407 
408  /**
409  A mixin implementing activation functions in GLSL.
410  The result of any operation must fit into 0..1 range to be stored as a color texture. The activation
411  function introduces non-linear behavior in the model and determines how the computed values are mapped to
412  the valid output range.
413  */
415  private:
416  protected:
418 
420 
421  /**
422  Renders a GLSL code applying activation function to a specific variable and writing the result to
423  gl_FragColor shader output variable.
424  \param[in,out] code A GLSL source code to be appended by the activation function code
425  \param[in] inputVariable Name of the variable to apply the activation function to
426  */
427  void apply(StringBuilder& code, const char* inputVariable);
428  };
429 
430 
431  /**
432  Operation computed on CPU
433  */
435  protected:
436  CpuOperation(const std::string& name) : AbstractOperation(name) {}
437 
438  inline void prepare(GraphicPipeline& gpu, ChunkCollection& data, GL::ProgramBank& bank) {}
439 
440  inline void getSampledChannels(int index, int& min, int& max) const { min = max = 0; }
441 
442  /**
443  Returns amount of work in arbitrary units to be splitted among threads.
444  */
445  virtual int getAmountOfWork() const = 0;
446 
447  /**
448  Called right before the operation is executed
449  */
450  virtual void beforeExecute(GraphicPipeline& gpu, const int threadCount) {}
451 
452  /**
453  Called right after the operation is executed
454  */
455  virtual void afterExecute(const int threadCount) {}
456 
457  void execute(TaskThread& thread, GraphicPipeline& gpu);
458  void execute(TaskThread& thread);
459 
460  /**
461  Executes the operation body within a specific CPU thread.
462  The threads can process different slices according to a given amount of work (see getAmountOfWork()).
463  \param[in] sliceStart Current slice starting point (included in the slice)
464  \param[in] sliceStop Current slice end point (excluded from the slice)
465  \param[in] threadIdx Zero-based calling thread number
466  \param[in] threadCount Total number of threads executing the operation
467  */
468  virtual void execute(const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount) = 0;
469  public:
470  bool usesGpu() const { return false; }
471  };
472 
473 
474  /**
475  Returns a zero padding value from a string.
476  The conversion is case-insensitive. Raises an exception if cannot interpret the string.
477  \param[in] str The input string
478  */
480  }
481 }
482 
483 namespace std {
485 }
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
Base class for all exceptions.
Definition: exception.h:37
Stores linked GLSL programs and their associated fragment shader codes.
Definition: program_bank.h:31
Regular OpenGL program.
Definition: program.h:229
Real-valued vector usable by GPU.
Internal low-level GPU control API.
Definition: pipeline.h:33
Set of key-value pairs.
Definition: listing.h:46
Enables construction of an operation from its serialized representation.
Definition: operation.h:248
Deserializer(const char *opType)
Registers a new deserializer capable of building an operation from a listing block.
Definition: operation.cpp:89
static std::map< std::string, Deserializer * > & getDeserializersMap()
Definition: operation.cpp:83
virtual AbstractOperation * deserialize(Context &context, const Listing::Block &block)=0
Constructs an operation from a serialized representation (a listing block).
InconsistentModelData(const AbstractOperation *op, const char *shortDescription)
Definition: operation.h:102
InconsistentModelData(const AbstractOperation *op, const char *shortDescription, const size_t entriesRequested, const size_t sizeBytesGot)
Definition: operation.h:106
NotReady(const AbstractOperation *op)
Definition: operation.h:114
Abstract neural net operation (layer).
Definition: operation.h:46
virtual int getOutputCount() const
Returns number of operation outputs.
Definition: operation.h:135
virtual bool acceptsTextureInput(int index=0) const
Returns true if the operation can take a GL::TextureHandler at a specific input.
Definition: operation.h:159
virtual bool acceptsStorageOutput(int index=0) const
Returns true if the operation can take a Storage::View at a specific output.
Definition: operation.h:167
AbstractOperation(const AbstractOperation &)=delete
disabling copying constructor
virtual Size getOutputSize(int outputIndex=0) const
Returns full size of a specific operation output.
Definition: operation.cpp:32
virtual bool acceptsVectorInput(int index=0) const
Returns true if the operation can take a GL::Vector at a specific input.
Definition: operation.h:151
virtual void setInput(Storage::View &&storage, int index=0)
Definition: operation.cpp:52
virtual std::map< std::string, std::string > serialize() const =0
Returns a serialized representation of th operation;.
virtual int getInputCount() const
Returns number of operation inputs.
Definition: operation.h:129
virtual int getInputPadding(int index=0) const
Retrieves minimum required size of zero padding for a given input.
Definition: operation.h:85
AbstractOperation(const std::string &name)
Definition: operation.h:53
virtual void execute(TaskThread &thread, GraphicPipeline &gpu)=0
Executes the operation.
virtual void prepare(GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank)=0
Compiles GLSL shaders.
virtual bool acceptsStorageInput(int index=0) const
Returns true if the operation can take a Storage::View at a specific input.
Definition: operation.h:143
virtual Storage::View getOutput(int index=0)
Returns a storage view bound to a specific operation output.
Definition: operation.cpp:37
virtual unsigned long countMultiplyAdds() const
Counts (approximate) number of multiply-adds used by this operation.
Definition: operation.h:232
virtual void setOutput(Storage::View &&storage, int index=0)
Definition: operation.cpp:57
virtual bool acceptsVectorOutput(int index=0) const
Returns true if the operation can take a GL::Vector at a specific output.
Definition: operation.h:175
virtual unsigned long countTexelFetches() const
Counts (approximate) number of texels fetches.
Definition: operation.h:237
virtual bool usesGpu() const
Returns true if the operation is run on GPU.
Definition: operation.h:123
virtual bool acceptsTextureOutput(int index=0) const
Returns true if the operation can take a GL::TextureHandler at a specific output.
Definition: operation.h:183
std::string getName() const
Definition: operation.h:242
virtual void execute(TaskThread &thread)
Executes the operation within a specific CPU thread.
Definition: operation.h:76
virtual void disconnect()=0
Assigns empty inputs and outputs.
virtual void getSampledChannels(int index, int &min, int &max) const =0
Retrieves range of input features channels sampled at the same time for a specific input.
A mixin implementing activation functions in GLSL.
Definition: operation.h:414
ActivationFunctionMixin(const ActivationFunction activationFunc)
Definition: operation.h:419
const ActivationFunction activationFunc
Definition: operation.h:417
void apply(StringBuilder &code, const char *inputVariable)
Renders a GLSL code applying activation function to a specific variable and writing the result to gl_...
Definition: operation.cpp:282
Operation computed on CPU.
Definition: operation.h:434
void execute(TaskThread &thread, GraphicPipeline &gpu)
Executes the operation.
Definition: operation.cpp:298
void prepare(GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank)
Compiles GLSL shaders.
Definition: operation.h:438
bool usesGpu() const
Returns true if the operation is run on GPU.
Definition: operation.h:470
virtual void execute(const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount)=0
Executes the operation body within a specific CPU thread.
virtual int getAmountOfWork() const =0
Returns amount of work in arbitrary units to be splitted among threads.
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: operation.h:440
virtual void beforeExecute(GraphicPipeline &gpu, const int threadCount)
Called right before the operation is executed.
Definition: operation.h:450
CpuOperation(const std::string &name)
Definition: operation.h:436
virtual void afterExecute(const int threadCount)
Called right after the operation is executed.
Definition: operation.h:455
Neural net model.
Definition: model.h:92
Operation 3D input/output size.
Definition: storage.h:37
Padding
Zero padding specification.
Definition: storage.h:45
Generates GLSL fragment shader code sampling a local neighborhood around the current texture coordina...
Definition: operation.h:272
Rectangle getTextureCoordinates(const Storage::View &storage, const int channel, const IntPoint &stride, const Size::Padding padding, const IntPoint &outputSize) const
Computes texture coordinates sampling a specific storage channel for given stride,...
Definition: operation.cpp:261
void sample(StringBuilder &code, const char *inputName, const int inputIndex, const Point &shift, const bool isFirstSample=true, const char *suffix="")
Samples a neighborhood of a given texture.
Definition: operation.cpp:150
void sampleInline(StringBuilder &code, const char *inputName, const int inputIndex, const IntPoint &position, const Point &shift, const char *suffix="")
Definition: operation.cpp:174
void setup(const int width, const int height)
Prepares the spatial filtering operation execution.
Definition: operation.cpp:197
static const char * SAMPLE_ID_PREFIX
prefix of variables declaring a neighbor sample
Definition: operation.h:285
Point shift
current static shift of the sampling position
Definition: operation.h:275
void writeHeader(StringBuilder &code, bool useUniformShift)
Writes out the very GLSL fragment shader header required for spatial neighborhood sampling.
Definition: operation.cpp:110
std::string getInputSamplingPos() const
Retrieves input sampling point position for the current fragment.
Definition: operation.cpp:276
void declare(StringBuilder &code, const char *datatype, bool inlineSampling=false)
Declares GLSL fragment shader main(..) code part required for spatial neighborhood sampling.
Definition: operation.cpp:119
void setupProgram(GL::Program &program)
Prepares a given program for spatial filtering.
Definition: operation.cpp:217
IntRectangle getSamplingArea(const IntPoint &size, const IntPoint &stride, const Size::Padding padding) const
Implements common padding policies by computing a rectangular area of positions the sampling kernel t...
Definition: operation.cpp:223
bool useUniformShift
if true, the sampling position can be shifted dynamically at every run
Definition: operation.h:277
SpatialFilteringMixin(const int nbSizeX, const int nbSizeY)
Initializes spatial filtering mixin.
Definition: operation.cpp:98
void setUniformShift(GL::Program &program, const IntPoint &shift, const IntPoint &inputSize)
Applies an offset to the sampling position at runtime.
Definition: operation.cpp:209
float * deltas
array storing pixel position differences for neighborhood sampling
Definition: operation.h:276
Maps a 3D tensor onto a storage.
Definition: storage.h:308
Toolset to build a string content.
Thread executing tasks.
Definition: parallelism.h:154
ActivationFunction activationFunctionFromString(const std::string &str)
Returns a zero padding value from a string.
Definition: operation.cpp:329
ActivationFunction
Activation function specification.
Definition: operation.h:401
@ SIGMOID_LIKE
piecewise-linear sigmoid approximation
@ DEFAULT
default activation: 0..1 bounded ReLU (identity clipped to 0..1 range)
@ BRELU6
0.167 times identity clipped to 0..1 range
Definition: geometry.h:721
std::string to_string(Beatmup::NNets::ActivationFunction function)
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
jlong jint width
jlong jint jint height
jlong jobject size
jlong jint op
JNIEnv jobject jstring str