Loading...
Searching...
No Matches
tensor_operations.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
9
10namespace opennn
11{
12
13inline constexpr float INV_SQRT_2 = 0.70710678118654752440f;
14inline constexpr float INV_SQRT_2_PI = 0.39894228040143267794f;
15inline constexpr float SQRT_2_OVER_PI = 0.7978845608028654f;
16inline constexpr float GELU_TANH_CUBIC = 0.044715f;
17
18inline float gelu_value(float x)
19{
20 return 0.5f * x * (1.0f + erff(x * INV_SQRT_2));
21}
22
23inline float gelu_tanh_value(float x)
24{
25 return 0.5f * x * (1.0f + tanhf(SQRT_2_OVER_PI * (x + GELU_TANH_CUBIC * x * x * x)));
26}
27
28inline float silu_value(float x)
29{
30 return x / (1.0f + exp(-x));
31}
32
33inline float gelu_derivative(float x)
34{
35 const float cdf = 0.5f * (1.0f + erff(x * INV_SQRT_2));
36 const float pdf = INV_SQRT_2_PI * expf(-0.5f * x * x);
37 return cdf + x * pdf;
38}
39
40inline float gelu_tanh_derivative(float x)
41{
42 const float x2 = x * x;
43 const float u = SQRT_2_OVER_PI * (x + GELU_TANH_CUBIC * x * x2);
44 const float t = tanhf(u);
45 const float du = SQRT_2_OVER_PI * (1.0f + 3.0f * GELU_TANH_CUBIC * x2);
46 return 0.5f * (1.0f + t) + 0.5f * x * (1.0f - t * t) * du;
47}
48
49inline float silu_derivative(float x)
50{
51 const float s = 1.0f / (1.0f + expf(-x));
52 return s * (1.0f + x * (1.0f - s));
53}
54
58
60
61// Whether this build has the MKL CPU kernels at all. `Configuration::set_blas`
62// accepts `Blas::Mkl` either way; this is how a caller finds out whether that
63// request has anywhere to go.
65
66inline float activation_forward_value(ActivationFunction function, float x)
67{
68 using enum ActivationFunction;
69 switch (function)
70 {
71 case Identity: return x;
72 case Sigmoid: return 1.0f / (1.0f + exp(-x));
73 case Tanh: return tanh(x);
74 case ReLU: return max(0.0f, x);
75 case LeakyReLU: return x >= 0.0f ? x : x * LEAKY_RELU_SLOPE;
76 case GELU: return gelu_value(x);
77 case GELUTanh: return gelu_tanh_value(x);
78 case SiLU: return silu_value(x);
79 case Softmax: break;
80 }
81
82 throw runtime_error("activation_forward_value: Softmax must be handled separately.");
83}
84
86{
87 using enum ActivationFunction;
88 switch (function)
89 {
90 case Identity: return 1.0f;
91 case Sigmoid: return y * (1.0f - y);
92 case Tanh: return 1.0f - y * y;
93 case ReLU: return y > 0.0f ? 1.0f : 0.0f;
94 case LeakyReLU: return y >= 0.0f ? 1.0f : LEAKY_RELU_SLOPE;
95 case Softmax: break;
96 case GELU:
97 case GELUTanh:
98 case SiLU: break;
99 }
100
101 throw runtime_error("activation_derivative_from_output_value: Softmax/GELU/GELUTanh/SiLU must be handled separately.");
102}
103
104template<typename TensorType>
105typename TensorType::PlainObject activation_forward_values(ActivationFunction function, const TensorType& values)
106{
107 return values.unaryExpr([function](float value) { return activation_forward_value(function, value); });
108}
109
110template<typename TensorType>
111typename TensorType::PlainObject activation_derivative_from_output_values(ActivationFunction function, const TensorType& values)
112{
113 return values.unaryExpr([function](float value) { return activation_derivative_from_output_value(function, value); });
114}
115
117
118void add(const TensorView&, const TensorView&, TensorView&);
119
120enum class Transpose { No, Yes };
121
123 float alpha = 1.0f, float beta = 0.0f);
124
126void softmax_backward(const TensorView&, TensorView&, float alpha = 1.0f);
127
130
131void linear_forward(const TensorView&, const TensorView&, const TensorView&,
133 TensorView* pre_activation = nullptr,
134 const TensorView& weight_scale = {},
137{
138 const TensorView* drelu_mask = nullptr;
139
140 const TensorView* addend = nullptr;
141
142 bool* fused_input_relu = nullptr;
143};
144
146 const TensorView&, const TensorView&,
147 TensorView&, bool accumulate_input_delta = false,
148 const LinearBackwardOptions& options = {});
149
150void linear_forward_transposed(const TensorView& input, const TensorView& embed_weight, TensorView& output,
151 const TensorView& weight_scale = {});
152
155pair<MatrixR, MatrixR> append_rows(const pair<MatrixR, MatrixR>&, const pair<MatrixR, MatrixR>&);
156MatrixR append_columns(const pair<MatrixR, MatrixR>&);
157
158VectorR slice_rows(const VectorR&, const vector<Index>&);
159MatrixR slice_rows(const MatrixR&, const vector<Index>&);
160pair<MatrixR, MatrixR> slice_rows(const pair<MatrixR, MatrixR>&, const vector<Index>&);
161
163VectorI get_nearest_points(const MatrixR&, const VectorR&, Index = 1);
164
165bool row_dominates(const MatrixR&, Index, Index);
166
167}
Definition enum_map.h:15
Definition configuration.h:10
VectorI get_nearest_points(const MatrixR &, const VectorR &, Index=1)
float activation_forward_value(ActivationFunction function, float x)
Definition tensor_operations.h:66
void linear_backward(const TensorView &, const TensorView &, const TensorView &, const TensorView &, const TensorView &, TensorView &, bool accumulate_input_delta=false, const LinearBackwardOptions &options={})
void linear_forward_transposed(const TensorView &input, const TensorView &embed_weight, TensorView &output, const TensorView &weight_scale={})
constexpr float LEAKY_RELU_SLOPE
Definition configuration.h:53
float gelu_value(float x)
Definition tensor_operations.h:18
MatrixR append_columns(const MatrixR &, const MatrixR &)
ActivationFunction activation_function_from_string(const string &)
void copy(const TensorView &, TensorView &)
const EnumMap< ActivationFunction > & activation_function_map()
VectorR slice_rows(const VectorR &, const vector< Index > &)
float silu_value(float x)
Definition tensor_operations.h:28
constexpr float INV_SQRT_2_PI
Definition tensor_operations.h:14
void linear_forward(const TensorView &, const TensorView &, const TensorView &, TensorView &, LinearEpilogue epilogue=LinearEpilogue::Bias, TensorView *pre_activation=nullptr, const TensorView &weight_scale={}, ActivationFunction fused_activation=ActivationFunction::Identity)
Transpose
Definition tensor_operations.h:120
@ Yes
Definition tensor_operations.h:120
@ No
Definition tensor_operations.h:120
LinearEpilogue
Definition configuration.h:42
@ Bias
Definition configuration.h:45
void softmax(TensorView &)
TensorType::PlainObject activation_forward_values(ActivationFunction function, const TensorType &values)
Definition tensor_operations.h:105
TensorType::PlainObject activation_derivative_from_output_values(ActivationFunction function, const TensorType &values)
Definition tensor_operations.h:111
float silu_derivative(float x)
Definition tensor_operations.h:49
void activation_forward(TensorView &, ActivationFunction)
bool activation_needs_input(ActivationFunction function)
void activation_backward(const TensorView &, TensorView &, ActivationFunction)
bool blas_mkl_available()
void softmax_backward(const TensorView &, TensorView &, float alpha=1.0f)
const string & activation_function_to_string(ActivationFunction)
float gelu_tanh_value(float x)
Definition tensor_operations.h:23
float activation_derivative_from_output_value(ActivationFunction function, float y)
Definition tensor_operations.h:85
float gelu_tanh_derivative(float x)
Definition tensor_operations.h:40
MatrixR append_rows(const MatrixR &, const MatrixR &)
void add(const TensorView &, const TensorView &, TensorView &)
MatrixR calculate_distances(const MatrixR &)
constexpr float SQRT_2_OVER_PI
Definition tensor_operations.h:15
bool row_dominates(const MatrixR &, Index, Index)
void multiply(const TensorView &, Transpose, const TensorView &, Transpose, TensorView &, float alpha=1.0f, float beta=0.0f)
ActivationFunction
Definition configuration.h:39
@ Sigmoid
Definition configuration.h:39
@ Softmax
Definition configuration.h:39
@ GELU
Definition configuration.h:39
@ GELUTanh
Definition configuration.h:39
@ LeakyReLU
Definition configuration.h:39
@ Identity
Definition configuration.h:39
@ Tanh
Definition configuration.h:39
@ ReLU
Definition configuration.h:39
@ SiLU
Definition configuration.h:39
float gelu_derivative(float x)
Definition tensor_operations.h:33
constexpr float GELU_TANH_CUBIC
Definition tensor_operations.h:16
constexpr float INV_SQRT_2
Definition tensor_operations.h:13
Eigen::Matrix< float, Eigen::Dynamic, 1 > VectorR
Definition opennn_types.h:335
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Layout > MatrixR
Definition opennn_types.h:331
Eigen::Matrix< Index, Eigen::Dynamic, 1 > VectorI
Definition opennn_types.h:336
Definition tensor_operations.h:137
const TensorView * addend
Definition tensor_operations.h:140
bool * fused_input_relu
Definition tensor_operations.h:142
const TensorView * drelu_mask
Definition tensor_operations.h:138
Definition tensor_types.h:540