ML functions
 
Loading...
Searching...
No Matches
NNBuilder.h
Go to the documentation of this file.
1
15
16#pragma once
17#include "velox/ml_functions/functions.h"
18
19using namespace facebook::velox;
20using namespace facebook::velox::test;
21using namespace facebook::velox::exec::test;
22
31
36class NNBuilder {
37 public:
42 function_count = 0;
43 compute_string = "{}";
44 }
45
51 NNBuilder(std::string weightsFile, std::string biasFile) : NNBuilder() {
52 weightsFile_ = weightsFile;
53 biasFile_ = biasFile;
54 }
55
60
65 std::string build() {
66 return compute_string;
67 }
68
79 int units,
80 int input_size,
81 float* weights,
82 float* bias,
83 Activation ac) {
84 std::string mat_mul_name =
85 MatrixMultiply::getName() + std::to_string(function_count++);
86 std::string mat_add_name =
87 MatrixVectorAddition::getName() + std::to_string(function_count++);
88 std::string act_name = "";
89
90 exec::registerVectorFunction(
91 mat_mul_name,
93 std::make_unique<MatrixMultiply>(weights, input_size, units));
94
95 exec::registerVectorFunction(
96 mat_add_name,
97 MatrixVectorAddition::signatures(),
98 std::make_unique<MatrixVectorAddition>(bias, units));
99
100 if (ac == Activation::RELU) {
101 act_name = Relu::getName() + std::to_string(function_count++);
102 exec::registerVectorFunction(
103 act_name, Relu::signatures(), std::make_unique<Relu>());
104 } else {
105 act_name = Softmax::getName() + std::to_string(function_count++);
106 exec::registerVectorFunction(
107 act_name, Softmax::signatures(), std::make_unique<Softmax>());
108 }
109
110 compute_string = fmt::format(
111 "{}({}({}({})))", act_name, mat_add_name, mat_mul_name, compute_string);
112 return *this;
113 }
114
122 NNBuilder& denseLayer(int units, int input_size, Activation ac) {
123 std::string mat_mul_name =
124 MatrixMultiply::getName() + std::to_string(function_count++);
125 std::string mat_add_name =
126 MatrixAddition::getName() + std::to_string(function_count++);
127 std::string act_name = "";
128
129 exec::registerVectorFunction(
130 mat_mul_name,
132 std::make_unique<MatrixMultiply>(weightsFile_, input_size, units));
133
134 exec::registerVectorFunction(
135 mat_add_name,
137 std::make_unique<MatrixAddition>(biasFile_, units));
138
139 if (ac == Activation::RELU) {
140 act_name = Relu::getName() + std::to_string(function_count++);
141 exec::registerVectorFunction(
142 act_name, Relu::signatures(), std::make_unique<Relu>());
143 } else {
144 act_name = Softmax::getName() + std::to_string(function_count++);
145 exec::registerVectorFunction(
146 act_name, Softmax::signatures(), std::make_unique<Softmax>());
147 }
148
149 compute_string = fmt::format(
150 "{}({}({}({})))", act_name, mat_add_name, mat_mul_name, compute_string);
151 return *this;
152 }
153
164 int num_filters,
165 int* dims,
166 float* weights,
167 float* bias,
168 Activation ac) {
169 std::string conv_name =
170 Convolute::getName() + std::to_string(function_count++);
171 std::string scal_add_name =
172 VectorScalarAddition::getName() + std::to_string(function_count++);
173 std::string act_name = "";
174
175 exec::registerVectorFunction(
176 conv_name,
178 std::make_unique<Convolute>(weights, dims));
179
180 exec::registerVectorFunction(
181 scal_add_name,
183 std::make_unique<VectorScalarAddition>(bias, num_filters));
184
185 if (ac == Activation::RELU) {
186 act_name = Relu::getName() + std::to_string(function_count++);
187 exec::registerVectorFunction(
188 act_name, Relu::signatures(), std::make_unique<Relu>());
189 } else if (ac == Activation::SOFTMAX) {
190 act_name = Softmax::getName() + std::to_string(function_count++);
191 exec::registerVectorFunction(
192 act_name, Softmax::signatures(), std::make_unique<Softmax>());
193 }
194 if (act_name == "")
195 compute_string =
196 fmt::format("{}({}({}))", scal_add_name, conv_name, compute_string);
197 else
198 compute_string = fmt::format(
199 "{}({}({}({})))", act_name, scal_add_name, conv_name, compute_string);
200
201 return *this;
202 }
203
211 NNBuilder& maxPoolLayer(int side, int height, int width) {
212 std::string max_pool_name =
213 MaxPool::getName() + std::to_string(function_count++);
214 exec::registerVectorFunction(
215 max_pool_name,
217 std::make_unique<MaxPool>(side, height, width));
218 compute_string = fmt::format("{}({})", max_pool_name, compute_string);
219 return *this;
220 }
221
222 private:
223 int function_count;
224 std::string compute_string;
225 std::string weightsFile_;
226 std::string biasFile_;
227};
Activation
Enumeration of activation functions supported by the neural network builder.
Definition NNBuilder.h:27
@ RELU
Rectified Linear Unit (ReLU) activation.
Definition NNBuilder.h:27
@ SOFTMAX
Softmax activation.
Definition NNBuilder.h:28
@ NONE
No activation.
Definition NNBuilder.h:29
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures for the convolution operation.
Definition functions.h:3290
static std::string getName()
Returns the name of the function.
Definition functions.h:3317
static std::string getName()
Get the name of the function.
Definition functions.h:866
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Get the function signatures for matrix addition.
Definition functions.h:839
static std::string getName()
Get the name of the function.
Definition functions.h:291
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Get the function signatures for matrix multiplication.
Definition functions.h:257
static std::string getName()
Static method to return the name of the function.
Definition functions.h:1080
static std::string getName()
Static method to return the name of the function.
Definition functions.h:3866
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures supported by this class.
Definition functions.h:3836
std::string build()
Builds and returns the computation string representing the neural network.
Definition NNBuilder.h:65
NNBuilder & convLayer(int num_filters, int *dims, float *weights, float *bias, Activation ac)
Adds a convolutional layer to the neural network.
Definition NNBuilder.h:163
NNBuilder()
Default constructor for NNBuilder.
Definition NNBuilder.h:41
NNBuilder(std::string weightsFile, std::string biasFile)
Constructor for NNBuilder with weights and bias files.
Definition NNBuilder.h:51
NNBuilder & denseLayer(int units, int input_size, float *weights, float *bias, Activation ac)
Adds a dense layer to the neural network.
Definition NNBuilder.h:78
NNBuilder & maxPoolLayer(int side, int height, int width)
Adds a max pooling layer to the neural network.
Definition NNBuilder.h:211
~NNBuilder()
Destructor for NNBuilder.
Definition NNBuilder.h:59
NNBuilder & denseLayer(int units, int input_size, Activation ac)
Adds a dense layer to the neural network using weights and biases from files.
Definition NNBuilder.h:122
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures supported by this class.
Definition functions.h:1305
static std::string getName()
Static method to return the name of the function.
Definition functions.h:1335
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures supported by this class.
Definition functions.h:1471
static std::string getName()
Static method to return the name of the function.
Definition functions.h:1501
static std::string getName()
Static method to return the name of the function.
Definition functions.h:3731
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures supported by this class.
Definition functions.h:3701