Beatmup
Beatmup::NNets::Softmax Class Reference

Softmax layer. More...

#include <softmax.h>

Inheritance diagram for Beatmup::NNets::Softmax:
Beatmup::NNets::CpuOperation Beatmup::NNets::AbstractOperation

Public Member Functions

 Softmax (const std::string &name="Softmax")
 Creates a softmax layer. More...
 
int getInputCount () const
 Returns number of operation inputs. More...
 
int getOutputCount () const
 Returns number of operation outputs. More...
 
bool acceptsStorageInput (int index=0) const
 Returns true if the operation can take a Storage::View at a specific input. More...
 
bool acceptsVectorInput (int index=0) const
 Returns true if the operation can take a GL::Vector at a specific input. More...
 
void setInput (Storage::View &&view, int index=0)
 
void setInput (GL::Vector &vector, int index=0)
 
std::map< std::string, std::string > serialize () const
 Returns a serialized representation of th operation;. More...
 
void disconnect ()
 Assigns empty inputs and outputs. More...
 
const std::vector< float > & getProbabilities () const
 
- Public Member Functions inherited from Beatmup::NNets::CpuOperation
bool usesGpu () const
 Returns true if the operation is run on GPU. More...
 
- Public Member Functions inherited from Beatmup::NNets::AbstractOperation
virtual ~AbstractOperation ()
 
virtual bool acceptsTextureInput (int index=0) const
 Returns true if the operation can take a GL::TextureHandler at a specific input. More...
 
virtual bool acceptsStorageOutput (int index=0) const
 Returns true if the operation can take a Storage::View at a specific output. More...
 
virtual bool acceptsVectorOutput (int index=0) const
 Returns true if the operation can take a GL::Vector at a specific output. More...
 
virtual bool acceptsTextureOutput (int index=0) const
 Returns true if the operation can take a GL::TextureHandler at a specific output. More...
 
virtual Size getOutputSize (int outputIndex=0) const
 Returns full size of a specific operation output. More...
 
virtual Storage::View getOutput (int index=0)
 Returns a storage view bound to a specific operation output. More...
 
virtual void getOutput (GL::Vector *&vector, int index=0)
 Returns a GL::Vector bound to a specific operation output. More...
 
virtual void getOutput (GL::TextureHandler *&vector, int index=0)
 Returns a GL::TextureHandler bound to a specific operation output. More...
 
virtual void setOutput (Storage::View &&storage, int index=0)
 
virtual void setOutput (GL::Vector &vector, int index=0)
 
virtual void setInput (GL::TextureHandler &image, int index=0)
 
virtual void setOutput (GL::TextureHandler &image, int index=0)
 
virtual unsigned long countMultiplyAdds () const
 Counts (approximate) number of multiply-adds used by this operation. More...
 
virtual unsigned long countTexelFetches () const
 Counts (approximate) number of texels fetches. More...
 
std::string getName () const
 

Static Public Member Functions

static bool initDeserializer ()
 Sets up deserialization of the operation. More...
 

Private Member Functions

int getAmountOfWork () const
 Returns amount of work in arbitrary units to be splitted among threads. More...
 
void beforeExecute (GraphicPipeline &gpu, const int threadCount)
 Called right before the operation is executed. More...
 
void execute (const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount)
 Executes the operation body within a specific CPU thread. More...
 
void afterExecute (const int threadCount)
 Called right after the operation is executed. More...
 

Private Attributes

std::vector< float > output
 
std::vector< float > partialSums
 
Storage::View inputView
 
GL::VectorinputVector
 

Additional Inherited Members

- Protected Member Functions inherited from Beatmup::NNets::CpuOperation
 CpuOperation (const std::string &name)
 
void prepare (GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank)
 Compiles GLSL shaders. More...
 
void getSampledChannels (int index, int &min, int &max) const
 Retrieves range of input features channels sampled at the same time for a specific input. More...
 
void execute (TaskThread &thread, GraphicPipeline &gpu)
 Executes the operation. More...
 
void execute (TaskThread &thread)
 Executes the operation within a specific CPU thread. More...
 
- Protected Member Functions inherited from Beatmup::NNets::AbstractOperation
 AbstractOperation (const std::string &name)
 
virtual int getInputPadding (int index=0) const
 Retrieves minimum required size of zero padding for a given input. More...
 

Detailed Description

Softmax layer.

It does not have output, but acts as a sink. The resulting probabilities are returned by getProbabilities(). This operation is executed on CPU.

Definition at line 32 of file softmax.h.

Constructor & Destructor Documentation

◆ Softmax()

Softmax::Softmax ( const std::string &  name = "Softmax")

Creates a softmax layer.

Parameters
[in]nameOperation name

Definition at line 26 of file softmax.cpp.

26 : CpuOperation(name) {}
CpuOperation(const std::string &name)
Definition: operation.h:436

Member Function Documentation

◆ getAmountOfWork()

int Softmax::getAmountOfWork ( ) const
privatevirtual

Returns amount of work in arbitrary units to be splitted among threads.

Implements Beatmup::NNets::CpuOperation.

Definition at line 29 of file softmax.cpp.

29  {
30  return (int)output.size();
31 }
std::vector< float > output
Definition: softmax.h:34

◆ beforeExecute()

void Softmax::beforeExecute ( GraphicPipeline gpu,
const int  threadCount 
)
privatevirtual

Called right before the operation is executed.

Reimplemented from Beatmup::NNets::CpuOperation.

Definition at line 34 of file softmax.cpp.

34  {
35  partialSums.resize(threadCount);
36  if (inputVector)
37  inputVector->fetch(gpu, output);
38  else {
39  inputView.getStorage().pull(gpu);
40  output.resize(inputView.getSize().volume());
41  Storage::Scanner scanner(inputView);
42  scanner.move(0, 0);
43  scanner.fill(output.begin(), output.end());
44  }
45 }
void fetch(GraphicPipeline &gpu, std::vector< float > &output) const
Grabs vector values back from GPU to user memory.
int volume() const
Definition: storage.h:79
std::vector< float > partialSums
Definition: softmax.h:35
Storage::View inputView
Definition: softmax.h:36
GL::Vector * inputVector
Definition: softmax.h:37
Scans a storageview in RAM for further computations on CPU.
Definition: storage.h:466
void pull(GraphicPipeline &gpu)
Pulls storage data from GPU memory to RAM.
Definition: storage.cpp:233

◆ execute()

void Softmax::execute ( const int  sliceStart,
const int  sliceStop,
const int  threadIdx,
const int  threadCount 
)
privatevirtual

Executes the operation body within a specific CPU thread.

The threads can process different slices according to a given amount of work (see getAmountOfWork()).

Parameters
[in]sliceStartCurrent slice starting point (included in the slice)
[in]sliceStopCurrent slice end point (excluded from the slice)
[in]threadIdxZero-based calling thread number
[in]threadCountTotal number of threads executing the operation

Implements Beatmup::NNets::CpuOperation.

Definition at line 48 of file softmax.cpp.

48  {
49  // compute the exponential mapping of the input vector and partial sums
50  float sum = 0;
51  for (int i = sliceStart; i < sliceStop; ++i)
52  sum += (output[i] = std::exp(output[i]));
53  partialSums[threadIdx] = sum;
54 }

◆ afterExecute()

void Softmax::afterExecute ( const int  threadCount)
privatevirtual

Called right after the operation is executed.

Reimplemented from Beatmup::NNets::CpuOperation.

Definition at line 57 of file softmax.cpp.

57  {
58  // sum up partial sums
59  float sum = partialSums[0];
60  for (size_t i = 1; i < partialSums.size(); ++i)
61  sum += partialSums[i];
62 
63  // normalize the output vector
64  const float normFactor = 1 / sum;
65  for (auto& y : output)
66  y *= normFactor;
67 }
jobject jlong jint jint y

◆ getInputCount()

int Beatmup::NNets::Softmax::getInputCount ( ) const
inlinevirtual

Returns number of operation inputs.

Inputs are then indexed from zero to the returned value minus one inclusive.

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 51 of file softmax.h.

51 { return 1; }

◆ getOutputCount()

int Beatmup::NNets::Softmax::getOutputCount ( ) const
inlinevirtual

Returns number of operation outputs.

Outputs are then indexed from zero to the returned value minus one inclusive.

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 52 of file softmax.h.

52 { return 0; }

◆ acceptsStorageInput()

bool Beatmup::NNets::Softmax::acceptsStorageInput ( int  index = 0) const
inlinevirtual

Returns true if the operation can take a Storage::View at a specific input.

Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector and textures. This function is used to check whether a given operation accepts a storage view on input.

Parameters
[in]indexThe input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive.

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 54 of file softmax.h.

54 { return index == 0; }
jlong jint index

◆ acceptsVectorInput()

bool Beatmup::NNets::Softmax::acceptsVectorInput ( int  index = 0) const
inlinevirtual

Returns true if the operation can take a GL::Vector at a specific input.

Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector and textures. This function is used to check whether a given operation accepts a vector on input.

Parameters
[in]indexThe input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive.

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 55 of file softmax.h.

55 { return index == 0; }

◆ setInput() [1/2]

void Softmax::setInput ( Storage::View &&  view,
int  index = 0 
)
virtual

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 70 of file softmax.cpp.

70  {
71  OutOfRange::check(index, 0, 1, "Softmax operation input out of range: %d");
72  InvalidArgument::check(view.getWidth() == 1 && view.getHeight() == 1, "A column-like storage view is expected on input of Softmax operation.");
73  this->inputView = std::move(view);
74  this->inputVector = nullptr;
75 }
static void check(const bool condition, const std::string &message)
Definition: exception.h:75
static void check(const datatype value, const datatype min, const datatype max, const char *message)
Definition: exception.h:86

◆ setInput() [2/2]

void Softmax::setInput ( GL::Vector vector,
int  index = 0 
)
virtual

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 78 of file softmax.cpp.

78  {
79  OutOfRange::check(index, 0, 1, "Softmax operation input out of range: %d");
80  this->inputVector = &vector;
81  this->inputView = Storage::View();
82 }
friend class View
Definition: storage.h:135

◆ serialize()

std::map< std::string, std::string > Softmax::serialize ( ) const
virtual

Returns a serialized representation of th operation;.

Implements Beatmup::NNets::AbstractOperation.

Definition at line 106 of file softmax.cpp.

106  {
107  return {
108  { "_name", getName() },
109  { "_type", "softmax" }
110  };
111 }
std::string getName() const
Definition: operation.h:242

◆ disconnect()

void Softmax::disconnect ( )
virtual

Assigns empty inputs and outputs.

Implements Beatmup::NNets::AbstractOperation.

Definition at line 114 of file softmax.cpp.

114  {
115  this->inputVector = nullptr;
116  this->inputView = Storage::View();
117 }

◆ getProbabilities()

const std::vector<float>& Beatmup::NNets::Softmax::getProbabilities ( ) const
inline

Definition at line 64 of file softmax.h.

64 { return output; }

◆ initDeserializer()

static bool Beatmup::NNets::Softmax::initDeserializer ( )
static

Sets up deserialization of the operation.

Member Data Documentation

◆ output

std::vector<float> Beatmup::NNets::Softmax::output
private

Definition at line 34 of file softmax.h.

◆ partialSums

std::vector<float> Beatmup::NNets::Softmax::partialSums
private

Definition at line 35 of file softmax.h.

◆ inputView

Storage::View Beatmup::NNets::Softmax::inputView
private

Definition at line 36 of file softmax.h.

◆ inputVector

GL::Vector* Beatmup::NNets::Softmax::inputVector
private

Definition at line 37 of file softmax.h.


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