Beatmup
softmax.cpp
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 #include "softmax.h"
20 #include "deserialized_model.h"
21 
22 using namespace Beatmup;
23 using namespace NNets;
24 
25 
26 Softmax::Softmax(const std::string& name): CpuOperation(name) {}
27 
28 
30  return (int)output.size();
31 }
32 
33 
34 void Softmax::beforeExecute(GraphicPipeline& gpu, const int threadCount) {
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 }
46 
47 
48 void Softmax::execute(const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount) {
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 }
55 
56 
57 void Softmax::afterExecute(const int threadCount) {
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 }
68 
69 
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 }
76 
77 
78 void Softmax::setInput(GL::Vector& vector, int index) {
79  OutOfRange::check(index, 0, 1, "Softmax operation input out of range: %d");
80  this->inputVector = &vector;
81  this->inputView = Storage::View();
82 }
83 
84 
86  static class SoftmaxDeserializer : public AbstractOperation::Deserializer {
87  public:
88  SoftmaxDeserializer() : Deserializer("softmax") {}
89 
90  AbstractOperation* deserialize(Context& context, const Listing::Block& block) {
91  /** \page NNetsOpsSerialization
92  \section Softmax
93  Softmax operation has no parameters (except its name and the type).
94  \code{yaml}
95  - _name: arbitrary operation name
96  _type: softmax # fixed string
97  \endcode
98  */
99  return new Softmax(block["_name"]);
100  }
101  } john;
102  return true;
103 }
104 
105 
106 std::map<std::string, std::string> Softmax::serialize() const {
107  return {
108  { "_name", getName() },
109  { "_type", "softmax" }
110  };
111 }
112 
113 
115  this->inputVector = nullptr;
116  this->inputView = Storage::View();
117 }
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
Real-valued vector usable by GPU.
void fetch(GraphicPipeline &gpu, std::vector< float > &output) const
Grabs vector values back from GPU to user memory.
Internal low-level GPU control API.
Definition: pipeline.h:33
static void check(const bool condition, const std::string &message)
Definition: exception.h:75
Set of key-value pairs.
Definition: listing.h:46
Enables construction of an operation from its serialized representation.
Definition: operation.h:248
Abstract neural net operation (layer).
Definition: operation.h:46
std::string getName() const
Definition: operation.h:242
Operation computed on CPU.
Definition: operation.h:434
int volume() const
Definition: storage.h:79
static bool initDeserializer()
Sets up deserialization of the operation.
void beforeExecute(GraphicPipeline &gpu, const int threadCount)
Called right before the operation is executed.
Definition: softmax.cpp:34
std::vector< float > output
Definition: softmax.h:34
std::vector< float > partialSums
Definition: softmax.h:35
Softmax(const std::string &name="Softmax")
Creates a softmax layer.
Definition: softmax.cpp:26
Storage::View inputView
Definition: softmax.h:36
void afterExecute(const int threadCount)
Called right after the operation is executed.
Definition: softmax.cpp:57
void execute(const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount)
Executes the operation body within a specific CPU thread.
Definition: softmax.cpp:48
GL::Vector * inputVector
Definition: softmax.h:37
void disconnect()
Assigns empty inputs and outputs.
Definition: softmax.cpp:114
int getAmountOfWork() const
Returns amount of work in arbitrary units to be splitted among threads.
Definition: softmax.cpp:29
void setInput(Storage::View &&view, int index=0)
Definition: softmax.cpp:70
std::map< std::string, std::string > serialize() const
Returns a serialized representation of th operation;.
Definition: softmax.cpp:106
Scans a storageview in RAM for further computations on CPU.
Definition: storage.h:466
void move(int x, int y)
Sets the pointer to a specific spatial position.
Definition: storage.cpp:663
void fill(T begin, T limit)
Extracts the content of feature maps at the current position.
Definition: storage.h:508
Maps a 3D tensor onto a storage.
Definition: storage.h:308
friend class View
Definition: storage.h:135
void pull(GraphicPipeline &gpu)
Pulls storage data from GPU memory to RAM.
Definition: storage.cpp:233
static void check(const datatype value, const datatype min, const datatype max, const char *message)
Definition: exception.h:86
return(jlong) new Beatmup jlong jstring name
jlong jint index
jobject jlong jint jint y