ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw::cker::train Namespace Reference

Namespaces

namespace  functor
 

Data Structures

struct  LaunchConv2DBackpropFilterOp
 
struct  LaunchConv2DBackpropInputOp
 
struct  LaunchConv2DBackpropInputOpImpl
 

Enumerations

enum class  ArithmeticType { kAdd , kSub , kMul , kDiv }
 
enum class  LossReductionType { SUM_OVER_BATCH_SIZE , SUM }
 

Functions

void AveragePool2DGrad (const PoolParams &params, const Shape &incoming_shape, const float *incoming_data, const Shape &grad_shape, float *grad_data)
 
template<typename T >
void BinaryArithmeticGrad (const Shape &lhs_shape, const T *lhs_data, const Shape &rhs_shape, const T *rhs_data, const Shape &incoming_shape, const T *incoming_data, const Shape &lhs_grad_shape, T *lhs_grad_data, const Shape &rhs_grad_shape, T *rhs_grad_data, ArithmeticType arithmetic_type)
 
void ConvInputGrad (const ConvParams &params, const Shape &incoming_shape, const float *incoming_data, const Shape &filter_shape, const float *filter_data, const int padding_bottom, const int padding_right, const Shape &grad_shape, float *grad_data)
 
void ConvFilterGrad (const ConvParams &params, const Shape &incoming_shape, const float *incoming_data, const Shape &input_shape, const float *input_data, const int padding_bottom, const int padding_right, const Shape &filter_backprop_shape, float *filter_backprop_data)
 
template<typename T >
void backpropInput (const DepthwiseConvParams &params, const Shape &incoming_shape, const T *incoming_data, const Shape &filter_shape, const T *filter_data, T *padded_filter_data, const Shape &grad_shape, T *grad_data, bool pad_filter, T *filter_buffers_data, T *filter_dim_buffers_data)
 
template<typename T >
void backpropFilter (const DepthwiseConvParams &params, const Shape &incoming_shape, const T *incoming_data, const Shape &input_shape, const T *input_data, const Shape &filter_grad_shape, T *filter_grad_data, T *padded_filter_data, T *filter_buffers_data)
 
template<typename T >
void FullyConnectedBiasGrad (const Shape &incomming_shape, const T *incomming_data, const Shape &grad_shape, T *grad_data)
 
template<typename T >
square (T value)
 
template<typename T >
log_threshold ()
 
template<typename T >
void MSE (const Shape &y_pred_shape, const T *y_pred_data, const Shape &y_true_shape, const T *y_true_data, const Shape &output_shape, T *output_data)
 
template<typename T >
void MSEGrad (const Shape &y_pred_shape, const T *y_pred_data, const Shape &y_true_shape, const T *y_true_data, const Shape &grad_shape, T *grad_data, LossReductionType reduction_type)
 
template<typename T >
void CategoricalCrossEntropy (const Shape &y_pred_shape, const T *y_pred_data, const Shape &y_true_shape, const T *y_true_data, const Shape &output_shape, T *output_data)
 
template<typename T >
void CategoricalCrossEntropyGrad (const Shape &y_pred_shape, const T *y_pred_data, const Shape &y_true_shape, const T *y_true_data, const Shape &grad_shape, T *grad_data, LossReductionType reduction_type)
 
template<typename T >
void CategoricalCrossEntropyWithLogits (const Shape &logits_shape, const T *logits_data, const Shape &y_true_shape, const T *y_true_data, const Shape &loss_out_shape, T *loss_out_data, const Shape &grad_shape, T *grad_data, LossReductionType reduction_type)
 
void MaxPool2D (const PoolParams &params, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data, int *arg_max_index)
 
void MaxPool2DGrad (const Shape &incoming_shape, const float *incoming_data, const int *arg_max_index, const Shape &grad_shape, float *grad_data)
 
template<typename T >
void Depad (const int32_t *padding_data, int32_t pad_rank, const Shape &input_shape, const T *input_data, const Shape &output_shape, T *output_data)
 
template<typename T >
void MeanGrad (const Shape &incoming_shape, const T *incoming_data, const Shape &grad_shape, T *grad_data)
 
void ReLUGrad (const Shape &output_shape, const float *output_data, const Shape &incoming_shape, const float *incoming_data, const Shape &grad_shape, float *grad_data)
 
void ReLU6Grad (const Shape &output_shape, const float *output_data, const Shape &incoming_shape, const float *incoming_data, const Shape &grad_shape, float *grad_data)
 
void SoftMaxGrad (const Shape &output_shape, const float *output_data, const Shape &incoming_shape, const float *incoming_data, const Shape &grad_shape, float *grad_data)
 
void Adam (const Shape &trainable_shape, float *trainable_data, const Shape &grad_shape, const float *grad_data, const Shape &m_shape, float *m_data, const Shape &v_shape, float *v_data, float beta1_power, float beta2_power, float learning_rate, float beta1, float beta2, float epsilon, bool use_nesterov)
 
void GradientDescent (const Shape &output_shape, float *output_data, const Shape &grad_shape, const float *grad_data, float learning_rate)
 

Enumeration Type Documentation

◆ ArithmeticType

◆ LossReductionType

Enumerator
SUM_OVER_BATCH_SIZE 
SUM 

Definition at line 27 of file Types.h.

Function Documentation

◆ Adam()

void nnfw::cker::train::Adam ( const Shape trainable_shape,
float *  trainable_data,
const Shape grad_shape,
const float *  grad_data,
const Shape m_shape,
float *  m_data,
const Shape v_shape,
float *  v_data,
float  beta1_power,
float  beta2_power,
float  learning_rate,
float  beta1,
float  beta2,
float  epsilon,
bool  use_nesterov 
)
inline

Definition at line 33 of file Adam.h.

37{
38 Tensor trainable_tensor;
39 Tensor grad_tensor;
40 Tensor m_tensor;
41 Tensor v_tensor;
42 Tensor beta1_power_tensor;
43 Tensor beta2_power_tensor;
44 Tensor lr_tensor;
45 Tensor beta1_tensor;
46 Tensor beta2_tensor;
47 Tensor epsilon_tensor;
48
49 trainable_tensor.shape.ReplaceWith(trainable_shape.DimensionsCount(), trainable_shape.DimsData());
50 trainable_tensor.buffer = trainable_data;
51
52 grad_tensor.shape.ReplaceWith(grad_shape.DimensionsCount(), grad_shape.DimsData());
53 grad_tensor.buffer = const_cast<float *>(grad_data);
54
55 m_tensor.shape.ReplaceWith(m_shape.DimensionsCount(), m_shape.DimsData());
56 m_tensor.buffer = m_data;
57
58 v_tensor.shape.ReplaceWith(v_shape.DimensionsCount(), v_shape.DimsData());
59 v_tensor.buffer = v_data;
60
61 std::vector<float> beta1_power_vec{beta1_power};
62 beta1_power_tensor.buffer = beta1_power_vec.data();
63
64 std::vector<float> beta2_power_vec{beta2_power};
65 beta2_power_tensor.buffer = beta2_power_vec.data();
66
67 std::vector<float> lr_vec{learning_rate};
68 lr_tensor.buffer = lr_vec.data();
69
70 std::vector<float> beta1_vec{beta1};
71 beta1_tensor.buffer = beta1_vec.data();
72
73 std::vector<float> beta2_vec{beta2};
74 beta2_tensor.buffer = beta2_vec.data();
75
76 std::vector<float> epsilon_vec{epsilon};
77 epsilon_tensor.buffer = epsilon_vec.data();
78
79 if (trainable_shape != m_shape)
80 throw std::runtime_error("cker::Adam: output and m do not have the same shape");
81
82 if (trainable_shape != v_shape)
83 throw std::runtime_error("cker::Adam: output and v do not have the same shape");
84
85 if (trainable_shape != grad_shape)
86 throw std::runtime_error("cker::Adam: output and gradient do not have the same shape");
87
88 const training_ops::CPUDevice &device = *eigen_support::GetThreadPoolDevice();
89 training_ops::functor::ApplyAdam<training_ops::CPUDevice, float>()(
90 device, trainable_tensor.flat<float>(), m_tensor.flat<float>(), v_tensor.flat<float>(),
91 beta1_power_tensor.scalar<float>(), beta2_power_tensor.scalar<float>(),
92 lr_tensor.scalar<float>(), beta1_tensor.scalar<float>(), beta2_tensor.scalar<float>(),
93 epsilon_tensor.scalar<float>(), static_cast<const Tensor &>(grad_tensor).flat<float>(),
94 use_nesterov);
95}
int32_t DimensionsCount() const
Definition Shape.h:91
void ReplaceWith(int dimensions_count, const int32_t *dims_data)
Definition Shape.h:130
int32_t * DimsData()
Definition Shape.h:112
TTypes< T >::ConstScalar scalar() const
Definition Tensor.h:156
TTypes< T >::Flat flat()
Definition Tensor.h:127

References nnfw::cker::Tensor::buffer, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::DimsData(), nnfw::cker::Tensor::flat(), nnfw::cker::eigen_support::GetThreadPoolDevice(), nnfw::cker::Shape::ReplaceWith(), nnfw::cker::Tensor::scalar(), and nnfw::cker::Tensor::shape.

Referenced by onert::backend::train::optimizer::Adam::applyGradient().

◆ AveragePool2DGrad()

void nnfw::cker::train::AveragePool2DGrad ( const PoolParams params,
const Shape incoming_shape,
const float *  incoming_data,
const Shape grad_shape,
float *  grad_data 
)
inline

Definition at line 33 of file AveragePool.h.

35{
36 assert(grad_shape.DimensionsCount() == 4);
37 assert(incoming_shape.DimensionsCount() == 4);
38
39 const int batches = MatchingDim(incoming_shape, 0, grad_shape, 0);
40 const int grad_height = grad_shape.Dims(1);
41 const int grad_width = grad_shape.Dims(2);
42 const int incoming_height = incoming_shape.Dims(1);
43 const int incoming_width = incoming_shape.Dims(2);
44 const int stride_height = params.stride_height;
45 const int stride_width = params.stride_width;
46
47 // initialize grad_data
48 std::fill(grad_data, grad_data + grad_shape.FlatSize(), 0.0);
49
50 const auto incoming_mat = MapAsMatrixWithLastDimAsRows(incoming_data, incoming_shape);
51 auto grad_mat = MapAsMatrixWithLastDimAsRows(grad_data, grad_shape);
52
53 for (int b = 0; b < batches; ++b)
54 {
55 for (int h = 0; h < incoming_height; ++h)
56 {
57 for (int w = 0; w < incoming_width; ++w)
58 {
59 // (h_start, h_end) * (w_start, w_end) is input range
60 // that output is projected from.
61 int h_start = h * stride_height - params.padding_values.height;
62 int h_end = std::min(h_start + params.filter_height, grad_height);
63 h_start = h_start < 0 ? 0 : h_start;
64
65 int w_start = w * stride_width - params.padding_values.width;
66 int w_end = std::min(w_start + params.filter_width, grad_width);
67 w_start = w_start < 0 ? 0 : w_start;
68
69 int count = (h_end - h_start) * (w_end - w_start);
70
71 if (h_end <= 0 || w_end <= 0 || count <= 0 || h_start >= grad_height ||
72 w_start >= grad_width)
73 continue;
74
75 int incoming_offset = NodeOffset(b, h, w, incoming_height, incoming_width);
76 for (int ph = h_start; ph < h_end; ++ph)
77 {
78 for (int pw = w_start; pw < w_end; ++pw)
79 {
80 int grad_offset = NodeOffset(b, ph, pw, grad_height, grad_width);
81 grad_mat.col(grad_offset) += incoming_mat.col(incoming_offset) / count;
82 }
83 }
84 }
85 }
86 }
87}
int NodeOffset(int b, int h, int w, int height, int width)
Definition FeatureMap.h:21
int32_t Dims(int i) const
Definition Shape.h:92
int FlatSize() const
Definition Shape.h:181
int MatchingDim(const Shape &shape1, int index1, const Shape &shape2, int index2)
Definition Shape.h:220
MatrixMap< Scalar > MapAsMatrixWithLastDimAsRows(Scalar *data, const Shape &shape)
Definition Utils.h:60
PaddingValues padding_values
Definition Types.h:89

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::PoolParams::filter_height, nnfw::cker::PoolParams::filter_width, nnfw::cker::Shape::FlatSize(), nnfw::cker::PaddingValues::height, nnfw::cker::MapAsMatrixWithLastDimAsRows(), nnfw::cker::MatchingDim(), nnfw::cker::NodeOffset(), nnfw::cker::PoolParams::padding_values, nnfw::cker::PoolParams::stride_height, nnfw::cker::PoolParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ backpropFilter()

template<typename T >
void nnfw::cker::train::backpropFilter ( const DepthwiseConvParams params,
const Shape incoming_shape,
const T *  incoming_data,
const Shape input_shape,
const T *  input_data,
const Shape filter_grad_shape,
T *  filter_grad_data,
T *  padded_filter_data,
T *  filter_buffers_data 
)

Definition at line 65 of file DepthwiseConv.h.

69{
70 if (params.stride_height != params.stride_width)
71 throw std::runtime_error("Not support different length strides");
72
73 if (params.dilation_height_factor != 1 || params.dilation_width_factor != 1)
74 throw std::runtime_error{"Not support dilation other than 1."};
75
76 const int batch = MatchingDim(incoming_shape, 0, input_shape, 0);
77 const int input_depth = input_shape.Dims(3);
78 const int output_depth = incoming_shape.Dims(3);
79 const int incoming_height = incoming_shape.Dims(1);
80 const int incoming_width = incoming_shape.Dims(2);
81 const int input_height = input_shape.Dims(1);
82 const int input_width = input_shape.Dims(2);
83 const int stride = params.stride_height;
84 const int depth_multiplier = params.depth_multiplier;
85 const int filter_height = filter_grad_shape.Dims(1);
86 const int filter_width = filter_grad_shape.Dims(2);
87 const int pad_height = params.padding_values.height;
88 const int pad_width = params.padding_values.width;
89
91 batch, input_height, input_width, input_depth, filter_height, filter_width, depth_multiplier,
92 stride, pad_height, pad_width, incoming_height, incoming_width, output_depth, incoming_data,
93 input_data, filter_grad_data, padded_filter_data, filter_buffers_data);
94}
PaddingValues padding_values
Definition Types.h:234

References nnfw::cker::DepthwiseConvParams::depth_multiplier, nnfw::cker::DepthwiseConvParams::dilation_height_factor, nnfw::cker::DepthwiseConvParams::dilation_width_factor, nnfw::cker::Shape::Dims(), nnfw::cker::PaddingValues::height, nnfw::cker::MatchingDim(), nnfw::cker::DepthwiseConvParams::padding_values, nnfw::cker::DepthwiseConvParams::stride_height, nnfw::cker::DepthwiseConvParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ backpropInput()

template<typename T >
void nnfw::cker::train::backpropInput ( const DepthwiseConvParams params,
const Shape incoming_shape,
const T *  incoming_data,
const Shape filter_shape,
const T *  filter_data,
T *  padded_filter_data,
const Shape grad_shape,
T *  grad_data,
bool  pad_filter,
T *  filter_buffers_data,
T *  filter_dim_buffers_data 
)

Definition at line 32 of file DepthwiseConv.h.

36{
37 if (params.stride_height != params.stride_width)
38 throw std::runtime_error("Not support different length strides");
39
40 if (params.dilation_height_factor != 1 || params.dilation_width_factor != 1)
41 throw std::runtime_error{"Not support dilation other than 1."};
42
43 const int batch = MatchingDim(incoming_shape, 0, grad_shape, 0);
44 const int input_depth = grad_shape.Dims(3);
45 const int output_depth = incoming_shape.Dims(3);
46 const int incoming_height = incoming_shape.Dims(1);
47 const int incoming_width = incoming_shape.Dims(2);
48 const int grad_height = grad_shape.Dims(1);
49 const int grad_width = grad_shape.Dims(2);
50 const int stride = params.stride_height;
51 const int depth_multiplier = params.depth_multiplier;
52 const int filter_height = filter_shape.Dims(1);
53 const int filter_width = filter_shape.Dims(2);
54 const int pad_height = params.padding_values.height;
55 const int pad_width = params.padding_values.width;
56
58 batch, grad_height, grad_width, input_depth, filter_height, filter_width, depth_multiplier,
59 stride, pad_height, pad_width, incoming_height, incoming_width, output_depth, incoming_data,
60 filter_data, padded_filter_data, grad_data, pad_filter, filter_buffers_data,
61 filter_dim_buffers_data);
62}

References nnfw::cker::DepthwiseConvParams::depth_multiplier, nnfw::cker::DepthwiseConvParams::dilation_height_factor, nnfw::cker::DepthwiseConvParams::dilation_width_factor, nnfw::cker::Shape::Dims(), nnfw::cker::PaddingValues::height, nnfw::cker::MatchingDim(), nnfw::cker::DepthwiseConvParams::padding_values, nnfw::cker::DepthwiseConvParams::stride_height, nnfw::cker::DepthwiseConvParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ BinaryArithmeticGrad()

template<typename T >
void nnfw::cker::train::BinaryArithmeticGrad ( const Shape lhs_shape,
const T *  lhs_data,
const Shape rhs_shape,
const T *  rhs_data,
const Shape incoming_shape,
const T *  incoming_data,
const Shape lhs_grad_shape,
T *  lhs_grad_data,
const Shape rhs_grad_shape,
T *  rhs_grad_data,
ArithmeticType  arithmetic_type 
)

Definition at line 39 of file BinaryArithmetic.h.

44{
45 if (!(lhs_shape == rhs_shape && rhs_shape == incoming_shape && incoming_shape == lhs_grad_shape &&
46 lhs_grad_shape == rhs_grad_shape))
47 throw std::runtime_error{"Shape of lhs, rhs, incoming, lhs_grad, and rhs_grad must match"};
48
49 switch (arithmetic_type)
50 {
51 case ArithmeticType::kAdd:
52 {
53 BroadcastTo(incoming_shape, const_cast<T *>(incoming_data), lhs_grad_shape, lhs_grad_data);
54 BroadcastTo(incoming_shape, const_cast<T *>(incoming_data), rhs_grad_shape, rhs_grad_data);
55 }
56 break;
57
58 case ArithmeticType::kSub:
59 {
60 BroadcastTo(incoming_shape, const_cast<T *>(incoming_data), lhs_grad_shape, lhs_grad_data);
61
62 auto const in_map = MapAsVector(incoming_data, incoming_shape);
63 auto rhs_grad_map = MapAsVector(rhs_grad_data, rhs_grad_shape);
64 rhs_grad_map = -in_map;
65 }
66 break;
67
68 case ArithmeticType::kMul:
69 {
70 auto const in_map = MapAsVector(incoming_data, incoming_shape);
71 auto const lhs_map = MapAsVector(lhs_data, lhs_shape);
72 auto const rhs_map = MapAsVector(rhs_data, rhs_shape);
73 auto lhs_grad_map = MapAsVector(lhs_grad_data, lhs_grad_shape);
74 auto rhs_grad_map = MapAsVector(rhs_grad_data, rhs_grad_shape);
75
76 lhs_grad_map = in_map.array() * rhs_map.array();
77 rhs_grad_map = in_map.array() * lhs_map.array();
78 }
79 break;
80
81 case ArithmeticType::kDiv:
82 default:
83 throw std::runtime_error{"Unsupported Binary Arithmetic Operation"};
84 }
85}
VectorMap< Scalar > MapAsVector(Scalar *data, const Dims< N > &dims)
Definition Matrix.h:40
void BroadcastTo(const Shape &input_shape, T *input_data, const Shape &output_shape, T *output_data)

References nnfw::cker::BroadcastTo(), kAdd, kDiv, kMul, kSub, and nnfw::cker::MapAsVector().

Referenced by onert::backend::train::ops::BinaryArithmeticLayer::backward().

◆ CategoricalCrossEntropy()

template<typename T >
void nnfw::cker::train::CategoricalCrossEntropy ( const Shape y_pred_shape,
const T *  y_pred_data,
const Shape y_true_shape,
const T *  y_true_data,
const Shape output_shape,
T *  output_data 
)
inline

Definition at line 103 of file Loss.h.

106{
107 if (output_shape.DimensionsCount() != 1)
108 throw std::runtime_error("cker::CategoricalCrossEntropy: output dimension count should be 1");
109 if (y_pred_shape != y_true_shape)
110 throw std::runtime_error(
111 "cker::CategoricalCrossEntropy: y_pred and y_true do not have the same shape");
112 if (output_shape.Dims(0) != y_pred_shape.Dims(0))
113 throw std::runtime_error(
114 "cker::CategoricalCrossEntropy: output and y_pred do not have the same batch");
115
116 const auto y_pred = MapAsMatrixWithLastDimAsRows(y_pred_data, y_pred_shape);
117 const auto y_true = MapAsMatrixWithLastDimAsRows(y_true_data, y_true_shape);
118 auto output = MapAsVector(output_data, output_shape);
119
120 output = -(y_true.array() * y_pred.array().cwiseMax(log_threshold<T>()).log()).colwise().sum();
121}
const luci_interpreter::RuntimeShape output_shape

References nnfw::cker::Shape::Dims(), nnfw::cker::MapAsMatrixWithLastDimAsRows(), nnfw::cker::MapAsVector(), and output_shape.

Referenced by onert::backend::train::ops::LossCategoricalCrossentropyLayer::forward().

◆ CategoricalCrossEntropyGrad()

template<typename T >
void nnfw::cker::train::CategoricalCrossEntropyGrad ( const Shape y_pred_shape,
const T *  y_pred_data,
const Shape y_true_shape,
const T *  y_true_data,
const Shape grad_shape,
T *  grad_data,
LossReductionType  reduction_type 
)
inline

Definition at line 124 of file Loss.h.

128{
129 if (y_pred_shape != y_true_shape)
130 throw std::runtime_error(
131 "cker::CategoricalCrossEntropyGrad: y_pred and y_true do not have the same shape");
132 if (y_pred_shape != grad_shape)
133 throw std::runtime_error(
134 "cker::CategoricalCrossEntropyGrad: y_pred and grad do not have the same shape");
135
136 const auto y_pred = MapAsMatrixWithLastDimAsRows(y_pred_data, y_pred_shape);
137 const auto y_true = MapAsMatrixWithLastDimAsRows(y_true_data, y_true_shape);
138 auto grad = MapAsMatrixWithLastDimAsRows(grad_data, grad_shape);
139
140 const int32_t batch_size = grad_shape.Dims(0);
141 int32_t reduction_size = 1;
142 switch (reduction_type)
143 {
144 case LossReductionType::SUM_OVER_BATCH_SIZE:
145 reduction_size = batch_size;
146 break;
147 case LossReductionType::SUM:
148 reduction_size = 1;
149 break;
150 default:
151 throw std::runtime_error("Unsupported reduction type");
152 }
153 assert(reduction_size > 0);
154
155 grad = -(y_true.array() / y_pred.array().cwiseMax(log_threshold<T>())) /
156 static_cast<T>(reduction_size);
157}

References nnfw::cker::Shape::Dims(), nnfw::cker::MapAsMatrixWithLastDimAsRows(), SUM, and SUM_OVER_BATCH_SIZE.

Referenced by onert::backend::train::ops::LossCategoricalCrossentropyLayer::backward().

◆ CategoricalCrossEntropyWithLogits()

template<typename T >
void nnfw::cker::train::CategoricalCrossEntropyWithLogits ( const Shape logits_shape,
const T *  logits_data,
const Shape y_true_shape,
const T *  y_true_data,
const Shape loss_out_shape,
T *  loss_out_data,
const Shape grad_shape,
T *  grad_data,
LossReductionType  reduction_type 
)

Definition at line 160 of file Loss.h.

165{
166 // TODO Enable broadcast shapes
167 if (loss_out_shape.DimensionsCount() != 1)
168 throw std::runtime_error(
169 "cker::CategoricalCrossEntropyWithLogits: loss output dimension count should be 1");
170 if (logits_shape != y_true_shape)
171 throw std::runtime_error(
172 "cker::CategoricalCrossEntropyWithLogits: logits and y_true do not have the same shape");
173 if (loss_out_shape.Dims(0) != logits_shape.Dims(0))
174 throw std::runtime_error(
175 "cker::CategoricalCrossEntropyWithLogits: loss_out and logits do not have the same batch");
176 if (logits_shape != grad_shape)
177 throw std::runtime_error(
178 "cker::CategoricalCrossEntropyWithLogits: logits and grad do not have the same shape");
179
180 auto shape_in = logits_shape;
181
182 BCast bcast(BCast::FromShape(shape_in), BCast::FromShape(y_true_shape),
183 /*fewer_dims_optimization=*/false);
184
185 // loss is 1-D (one per example), and size is batch_size.
186
187 Tensor logits_in;
188 Tensor labels_in;
189 Tensor scratch;
190 Tensor loss_out;
191 Tensor back_out;
192
193 logits_in.shape.ReplaceWith(shape_in.DimensionsCount(), shape_in.DimsData());
194 logits_in.buffer = const_cast<T *>(logits_data);
195
196 labels_in.shape.ReplaceWith(y_true_shape.DimensionsCount(), y_true_shape.DimsData());
197 labels_in.buffer = const_cast<T *>(y_true_data);
198
199 scratch.shape.ReplaceWith(shape_in.DimensionsCount(), shape_in.DimsData());
200 std::vector<T> scratch_vec(shape_in.Dims(0) * shape_in.Dims(1), static_cast<T>(0));
201 scratch.buffer = scratch_vec.data();
202
203 Shape shape_loss_out{shape_in.Dims(0)};
204 loss_out.shape.ReplaceWith(shape_loss_out.DimensionsCount(), shape_loss_out.DimsData());
205 loss_out.buffer = loss_out_data;
206
207 back_out.shape.ReplaceWith(shape_in.DimensionsCount(), shape_in.DimsData());
208 back_out.buffer = grad_data;
209
210 if (shape_in.Dims(0) > 0)
211 {
212 const int32_t batch_size = grad_shape.Dims(0);
213 int32_t reduction_size = 1;
214 switch (reduction_type)
215 {
216 case LossReductionType::SUM_OVER_BATCH_SIZE:
217 reduction_size = batch_size;
218 break;
219 case LossReductionType::SUM:
220 reduction_size = 1;
221 break;
222 default:
223 throw std::runtime_error("Unsupported reduction type");
224 }
225 assert(reduction_size > 0);
226
227 const xent_ops::CPUDevice &device = *eigen_support::GetThreadPoolDevice();
228 xent_ops::functor::XentFunctor<xent_ops::CPUDevice, T> functor;
229 const Eigen::DSizes<Eigen::DenseIndex, 2> shape{shape_in.Dims(0), shape_in.Dims(1)};
230
231 functor(device, shape, BCast::ToIndexArray<2>(bcast.x_bcast()),
232 BCast::ToIndexArray<2>(bcast.y_bcast()),
233 logits_in.template shaped<const T, 2>(bcast.x_reshape()),
234 labels_in.template shaped<const T, 2>(bcast.y_reshape()), scratch.matrix<T>(),
235 loss_out.vec<T>(), back_out.matrix<T>(), static_cast<T>(reduction_size));
236 }
237}
TTypes< T >::Vec vec()
Definition Tensor.h:161
TTypes< T >::Matrix matrix()
Definition Tensor.h:163

References nnfw::cker::Tensor::buffer, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::Shape::DimsData(), nnfw::cker::BCast::FromShape(), nnfw::cker::eigen_support::GetThreadPoolDevice(), nnfw::cker::Tensor::matrix(), nnfw::cker::Shape::ReplaceWith(), nnfw::cker::Tensor::shape, SUM, SUM_OVER_BATCH_SIZE, nnfw::cker::Tensor::vec(), nnfw::cker::BCast::x_bcast(), nnfw::cker::BCast::x_reshape(), nnfw::cker::BCast::y_bcast(), and nnfw::cker::BCast::y_reshape().

Referenced by onert::backend::train::ops::LossCategoricalCrossentropyLayer::backward().

◆ ConvFilterGrad()

void nnfw::cker::train::ConvFilterGrad ( const ConvParams params,
const Shape incoming_shape,
const float *  incoming_data,
const Shape input_shape,
const float *  input_data,
const int  padding_bottom,
const int  padding_right,
const Shape filter_backprop_shape,
float *  filter_backprop_data 
)
inline

Definition at line 225 of file Conv.h.

230{
231 const int stride_rows = params.stride_height;
232 const int stride_cols = params.stride_width;
233 const PaddingType padding = params.padding_type;
234 const int padding_top = params.padding_values.height;
235 const int padding_left = params.padding_values.width;
236 assert(padding_top >= 0);
237 assert(padding_bottom >= 0);
238 assert(padding_left >= 0);
239 assert(padding_right >= 0);
240 const int dilation_rows = params.dilation_height_factor;
241 const int dilation_cols = params.dilation_width_factor;
242
243 const int batches = MatchingDim(input_shape, 0, incoming_shape, 0);
244 const int input_depth = MatchingDim(filter_backprop_shape, 2, input_shape, 3);
245 const int output_depth = MatchingDim(filter_backprop_shape, 3, incoming_shape, 3);
246 const int input_height = input_shape.Dims(1);
247 const int input_width = input_shape.Dims(2);
248 const int filter_backprop_height = filter_backprop_shape.Dims(0);
249 const int filter_backprop_width = filter_backprop_shape.Dims(1);
250 const int incoming_height = incoming_shape.Dims(1);
251 const int incoming_width = incoming_shape.Dims(2);
252
253 if (dilation_rows != 1 || dilation_cols != 1)
254 throw std::runtime_error("cker::ConvFilterGrad: not yet support dilation rates larger than 1.");
255
257 incoming_data, batches, incoming_height, incoming_width, output_depth, input_data, input_height,
258 input_width, input_depth, dilation_rows, dilation_cols, stride_rows, stride_cols, padding,
259 padding_top, padding_bottom, padding_left, padding_right, filter_backprop_data,
260 filter_backprop_height, filter_backprop_width);
261}
PaddingType
Definition Types.h:41
int16_t stride_height
Definition Types.h:146
PaddingValues padding_values
Definition Types.h:143
int16_t dilation_width_factor
Definition Types.h:147
PaddingType padding_type
Definition Types.h:142
int16_t dilation_height_factor
Definition Types.h:148

References nnfw::cker::ConvParams::dilation_height_factor, nnfw::cker::ConvParams::dilation_width_factor, nnfw::cker::Shape::Dims(), nnfw::cker::PaddingValues::height, nnfw::cker::MatchingDim(), nnfw::cker::ConvParams::padding_type, nnfw::cker::ConvParams::padding_values, nnfw::cker::ConvParams::stride_height, nnfw::cker::ConvParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ ConvInputGrad()

void nnfw::cker::train::ConvInputGrad ( const ConvParams params,
const Shape incoming_shape,
const float *  incoming_data,
const Shape filter_shape,
const float *  filter_data,
const int  padding_bottom,
const int  padding_right,
const Shape grad_shape,
float *  grad_data 
)
inline

Definition at line 188 of file Conv.h.

192{
193 const int stride_rows = params.stride_height;
194 const int stride_cols = params.stride_width;
195 const PaddingType padding = params.padding_type;
196 const int padding_top = params.padding_values.height;
197 const int padding_left = params.padding_values.width;
198 assert(padding_top >= 0);
199 assert(padding_bottom >= 0);
200 assert(padding_left >= 0);
201 assert(padding_right >= 0);
202 const int dilation_rows = params.dilation_height_factor;
203 const int dilation_cols = params.dilation_width_factor;
204
205 const int batches = MatchingDim(grad_shape, 0, incoming_shape, 0);
206 const int input_depth = MatchingDim(filter_shape, 2, grad_shape, 3);
207 const int output_depth = MatchingDim(filter_shape, 3, incoming_shape, 3);
208 const int grad_height = grad_shape.Dims(1);
209 const int grad_width = grad_shape.Dims(2);
210 const int filter_height = filter_shape.Dims(0);
211 const int filter_width = filter_shape.Dims(1);
212 const int incoming_height = incoming_shape.Dims(1);
213 const int incoming_width = incoming_shape.Dims(2);
214
215 if (dilation_rows != 1 || dilation_cols != 1)
216 throw std::runtime_error("cker::ConvFilterGrad: not yet support dilation rates larger than 1.");
217
219 incoming_data, batches, incoming_height, incoming_width, output_depth, filter_data,
220 filter_height, filter_width, dilation_rows, dilation_cols, stride_rows, stride_cols, padding,
221 padding_top, padding_bottom, padding_left, padding_right, grad_data, grad_height, grad_width,
222 input_depth);
223}

References nnfw::cker::ConvParams::dilation_height_factor, nnfw::cker::ConvParams::dilation_width_factor, nnfw::cker::Shape::Dims(), nnfw::cker::PaddingValues::height, nnfw::cker::MatchingDim(), nnfw::cker::ConvParams::padding_type, nnfw::cker::ConvParams::padding_values, nnfw::cker::ConvParams::stride_height, nnfw::cker::ConvParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ Depad()

template<typename T >
void nnfw::cker::train::Depad ( const int32_t *  padding_data,
int32_t  pad_rank,
const Shape input_shape,
const T *  input_data,
const Shape output_shape,
T *  output_data 
)
inline

Definition at line 50 of file Pad.h.

52{
53 using PaddingInfo = std::pair<int32_t, int32_t>;
54 using PaddingList = std::vector<PaddingInfo>;
55
56 assert(output_shape.DimensionsCount() == input_shape.DimensionsCount());
57 assert(output_shape.DimensionsCount() == pad_rank);
58
59 PaddingList padding_list(pad_rank);
60 for (int32_t n = 0; n < pad_rank; ++n)
61 {
62 const int32_t *from = padding_data + (n * 2);
63 assert(from[0] >= 0 && from[1] >= 0);
64 padding_list[n] = {from[0], from[1]};
65 }
66 for (int32_t i = 0; i < pad_rank; ++i)
67 {
68 assert(output_shape.Dims(i) ==
69 input_shape.Dims(i) - padding_list[i].first - padding_list[i].second);
70 }
71
72 // logical axis: row -> col -> plain -> cube
73 switch (pad_rank)
74 {
75 case 0:
76 case 1:
77 {
78 const int32_t out_row_len = output_shape.Dims(0);
79 const int32_t padding_left = padding_list[0].first;
80 std::memcpy(output_data, input_data + padding_left, out_row_len * sizeof(T));
81 break;
82 }
83 case 2: // HW
84 {
85 const int32_t out_col_len = output_shape.Dims(0);
86 const int32_t out_row_len = output_shape.Dims(1);
87 const int32_t in_row_len = input_shape.Dims(1);
88 const int32_t padding_top = padding_list[0].first;
89 const int32_t padding_left = padding_list[1].first;
90 for (auto i = 0; i < out_col_len; ++i)
91 {
92 const auto in_offset = (i + padding_top) * in_row_len + padding_left;
93 const auto out_offset = i * out_row_len;
94 // copy a row of input data to output data
95 std::memcpy(output_data + out_offset, input_data + in_offset, out_row_len * sizeof(T));
96 }
97 break;
98 }
99 case 3: // HWC
100 {
101 const int32_t out_plain_len = output_shape.Dims(0);
102 const int32_t out_col_len = output_shape.Dims(1);
103 const int32_t out_row_len = output_shape.Dims(2);
104 const int32_t out_plain_size = out_col_len * out_row_len;
105 const int32_t in_col_len = input_shape.Dims(1);
106 const int32_t in_row_len = input_shape.Dims(2);
107 const int32_t in_plain_size = in_col_len * in_row_len;
108 const int32_t padding_depth = padding_list[0].first;
109 const int32_t padding_top = padding_list[1].first;
110 const int32_t padding_left = padding_list[2].first;
111 for (auto d = 0; d < out_plain_len; ++d)
112 {
113 for (auto h = 0; h < out_col_len; ++h)
114 {
115 const auto in_offset =
116 (d + padding_depth) * in_plain_size + (h + padding_top) * in_row_len + (padding_left);
117 const auto out_offset = (d * out_plain_size) + (h * out_row_len);
118 // copy a row of input data to output data
119 std::memcpy(output_data + out_offset, input_data + in_offset, out_row_len * sizeof(T));
120 }
121 }
122 break;
123 }
124 case 4: // NHWC
125 {
126 const int32_t out_cube_len = output_shape.Dims(0);
127 const int32_t out_plain_len = output_shape.Dims(1);
128 const int32_t out_col_len = output_shape.Dims(2);
129 const int32_t out_row_len = output_shape.Dims(3);
130 const int32_t out_plain_size = out_col_len * out_row_len;
131 const int32_t out_cube_size = out_plain_len * out_plain_size;
132 const int32_t in_plain_len = input_shape.Dims(1);
133 const int32_t in_col_len = input_shape.Dims(2);
134 const int32_t in_row_len = input_shape.Dims(3);
135 const int32_t in_plain_size = in_col_len * in_row_len;
136 const int32_t in_cube_size = in_plain_len * in_plain_size;
137 const int32_t padding_cube = padding_list[0].first;
138 const int32_t padding_depth = padding_list[1].first;
139 const int32_t padding_top = padding_list[2].first;
140 const int32_t padding_left = padding_list[3].first;
141 for (auto c = 0; c < out_cube_len; ++c)
142 {
143 for (auto d = 0; d < out_plain_len; ++d)
144 {
145 for (auto h = 0; h < out_col_len; ++h)
146 {
147 const auto in_offset = (c + padding_cube) * in_cube_size +
148 (d + padding_depth) * in_plain_size +
149 (h + padding_top) * in_row_len + (padding_left);
150 const auto out_offset = (c * out_cube_size) + (d * out_plain_size) + (h * out_row_len);
151 // copy a row of input data to output data
152 std::memcpy(output_data + out_offset, input_data + in_offset, out_row_len * sizeof(T));
153 }
154 }
155 }
156 break;
157 }
158 default:
159 throw std::runtime_error("Padding for rank > 4 NYI");
160 break;
161 }
162}

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), and output_shape.

◆ FullyConnectedBiasGrad()

template<typename T >
void nnfw::cker::train::FullyConnectedBiasGrad ( const Shape incomming_shape,
const T *  incomming_data,
const Shape grad_shape,
T *  grad_data 
)
inline

Definition at line 31 of file FullyConnected.h.

33{
34 const auto bias_size = grad_shape.FlatSize();
35 if (bias_size != incomming_shape.Dims(incomming_shape.DimensionsCount() - 1) ||
36 bias_size != grad_shape.Dims(0))
37 throw std::runtime_error("cker::FullyConnectedBiasGrad: Unmatched shape");
38
39 const auto in_mat = MapAsMatrixWithLastDimAsRows(incomming_data, incomming_shape);
40 auto grad_mat = MapAsMatrixWithLastDimAsRows(grad_data, grad_shape);
41
42 grad_mat = in_mat.rowwise().sum();
43}

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::Shape::FlatSize(), and nnfw::cker::MapAsMatrixWithLastDimAsRows().

◆ GradientDescent()

void nnfw::cker::train::GradientDescent ( const Shape output_shape,
float *  output_data,
const Shape grad_shape,
const float *  grad_data,
float  learning_rate 
)
inline

Definition at line 33 of file SGD.h.

35{
36 Tensor output_tensor;
37 Tensor grad_tensor;
38 Tensor lr_tensor;
39
40 output_tensor.shape.ReplaceWith(output_shape.DimensionsCount(), output_shape.DimsData());
41 output_tensor.buffer = output_data;
42
43 grad_tensor.shape.ReplaceWith(grad_shape.DimensionsCount(), grad_shape.DimsData());
44 grad_tensor.buffer = const_cast<float *>(grad_data);
45
46 std::vector<float> lr_vec{learning_rate};
47 lr_tensor.buffer = lr_vec.data();
48
49 if (output_shape != grad_shape)
50 throw std::runtime_error(
51 "cker::GradientDescent: output and gradient do not have the same shape");
52
53 const training_ops::CPUDevice &device = *eigen_support::GetThreadPoolDevice();
55 device, output_tensor.flat<float>(), lr_tensor.scalar<float>(),
56 static_cast<const Tensor &>(grad_tensor).flat<float>());
57}
Eigen::ThreadPoolDevice CPUDevice

References nnfw::cker::Tensor::buffer, nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::DimsData(), nnfw::cker::eigen_support::GetThreadPoolDevice(), output_shape, nnfw::cker::Shape::ReplaceWith(), nnfw::cker::Tensor::scalar(), and nnfw::cker::Tensor::shape.

Referenced by onert::backend::train::optimizer::SGD::applyGradient().

◆ log_threshold()

template<typename T >
T nnfw::cker::train::log_threshold ( )
inline

Definition at line 38 of file Loss.h.

38{ return static_cast<T>(1e-20); }

◆ MaxPool2D()

void nnfw::cker::train::MaxPool2D ( const PoolParams params,
const Shape input_shape,
const float *  input_data,
const Shape output_shape,
float *  output_data,
int *  arg_max_index 
)
inline

Definition at line 36 of file MaxPool.h.

38{
39 assert(input_shape.DimensionsCount() == 4);
40 assert(output_shape.DimensionsCount() == 4);
41 assert(input_shape.Dims(0) == output_shape.Dims(0)); // MaxPool2D doesn't change batch
42 assert(input_shape.Dims(3) == output_shape.Dims(3)); // MaxPool2D doesn't change depth
43
44 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
45 const int input_height = input_shape.Dims(1);
46 const int input_width = input_shape.Dims(2);
47 const int output_height = output_shape.Dims(1);
48 const int output_width = output_shape.Dims(2);
49 const int filter_height = params.filter_height;
50 const int filter_width = params.filter_width;
51 const int pad_height = params.padding_values.height;
52 const int pad_width = params.padding_values.width;
53 const int stride_height = params.stride_height;
54 const int stride_width = params.stride_width;
55
56 const auto in_mat = MapAsMatrixWithLastDimAsRows(input_data, input_shape);
57 auto out_mat = MapAsMatrixWithLastDimAsRows(output_data, output_shape);
58 auto arg_max_index_mat = MapAsMatrixWithLastDimAsRows(arg_max_index, output_shape);
59
60 // initialize output area
61 std::fill(output_data, output_data + output_shape.FlatSize(), 0.0);
62 std::fill(arg_max_index, arg_max_index + output_shape.FlatSize(), -1);
63
64 // initialize projected area with lowest float
65 const int h_start =
66 (pad_height < filter_height) ? 0 : (pad_height - filter_height) / stride_height + 1;
67 const int h_end = std::min((input_height + pad_height - 1) / stride_height + 1, output_height);
68
69 const int w_start =
70 (pad_width < filter_width) ? 0 : (pad_width - filter_width) / stride_width + 1;
71 const int w_end = std::min((input_width + pad_width - 1) / stride_width + 1, output_width);
72
73 for (int b = 0; b < batches; ++b)
74 {
75 for (int h_idx = h_start; h_idx < h_end; h_idx++)
76 {
77 for (int w_idx = w_start; w_idx < w_end; w_idx++)
78 {
79 const int offset = NodeOffset(b, h_idx, w_idx, output_height, output_width);
80 out_mat.col(offset).setConstant(std::numeric_limits<float>::lowest());
81 }
82 }
83 }
84
85 for (int b = 0; b < batches; ++b)
86 {
87 for (int h = 0; h < input_height; ++h)
88 {
89 for (int w = 0; w < input_width; ++w)
90 {
91 // (h_start, h_end) * (w_start, w_end) is the range that the input
92 // vector projects to.
93 int hpad = h + pad_height;
94 int wpad = w + pad_width;
95
96 int h_start = (hpad < filter_height) ? 0 : (hpad - filter_height) / stride_height + 1;
97 int h_end = std::min(hpad / stride_height + 1, output_height);
98
99 int w_start = (wpad < filter_width) ? 0 : (wpad - filter_width) / stride_width + 1;
100 int w_end = std::min(wpad / stride_width + 1, output_width);
101
102 // compute elementwise sum
103 for (int ph = h_start; ph < h_end; ++ph)
104 {
105 for (int pw = w_start; pw < w_end; ++pw)
106 {
107 const int out_offset = NodeOffset(b, ph, pw, output_height, output_width);
108 const int in_offset = NodeOffset(b, h, w, input_height, input_width);
109
110 const auto out_vector = out_mat.col(out_offset);
111 const auto in_vector = in_mat.col(in_offset);
112
113 // update arg_max_index_mat
114 arg_max_index_mat.col(out_offset) =
115 (out_vector.array() < in_vector.array())
116 .select(in_offset, arg_max_index_mat.col(out_offset));
117
118 // update out_mat
119 out_mat.col(out_offset) = out_vector.cwiseMax(in_vector);
120 }
121 }
122 }
123 }
124 }
125
126 out_mat = out_mat.cwiseMin(params.float_activation_max).cwiseMax(params.float_activation_min);
127}
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
float float_activation_max
Definition Types.h:99
float float_activation_min
Definition Types.h:98

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::PoolParams::filter_height, nnfw::cker::PoolParams::filter_width, nnfw::cker::PoolParams::float_activation_max, nnfw::cker::PoolParams::float_activation_min, nnfw::cker::PaddingValues::height, nnfw::cker::MapAsMatrixWithLastDimAsRows(), nnfw::cker::MatchingDim(), nnfw::cker::NodeOffset(), offset(), output_shape, nnfw::cker::PoolParams::padding_values, nnfw::cker::PoolParams::stride_height, nnfw::cker::PoolParams::stride_width, and nnfw::cker::PaddingValues::width.

◆ MaxPool2DGrad()

void nnfw::cker::train::MaxPool2DGrad ( const Shape incoming_shape,
const float *  incoming_data,
const int *  arg_max_index,
const Shape grad_shape,
float *  grad_data 
)
inline

Definition at line 129 of file MaxPool.h.

131{
132 assert(grad_shape.DimensionsCount() == 4);
133 assert(incoming_shape.DimensionsCount() == 4);
134
135 // initialize grad_data
136 std::fill(grad_data, grad_data + grad_shape.FlatSize(), 0.0);
137
138 const int depth = MatchingDim(grad_shape, 3, incoming_shape, 3);
139 const auto incoming_mat = MapAsMatrixWithLastDimAsRows(incoming_data, incoming_shape);
140 auto arg_max_index_mat = MapAsMatrixWithLastDimAsRows(arg_max_index, incoming_shape);
141 auto grad_mat = MapAsMatrixWithLastDimAsRows(grad_data, grad_shape);
142
143 for (int col_index = 0; col_index < incoming_mat.cols(); col_index++)
144 {
145 auto arg_indices = arg_max_index_mat.col(col_index);
146 for (int d = 0; d < depth; d++)
147 {
148 // output value is from padding, so nothing to propagate
149 if (arg_indices(d) == -1)
150 continue;
151
152 grad_mat(d, arg_indices(d)) += incoming_mat(d, col_index);
153 }
154 }
155}

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::FlatSize(), nnfw::cker::MapAsMatrixWithLastDimAsRows(), and nnfw::cker::MatchingDim().

◆ MeanGrad()

template<typename T >
void nnfw::cker::train::MeanGrad ( const Shape incoming_shape,
const T *  incoming_data,
const Shape grad_shape,
T *  grad_data 
)

Definition at line 32 of file ReduceMean.h.

34{
35 BroadcastTo(incoming_shape, const_cast<T *>(incoming_data), grad_shape, grad_data);
36 const auto incoming = MapAsMatrixWithLastDimAsRows(incoming_data, incoming_shape);
37 auto grad = MapAsMatrixWithLastDimAsRows(grad_data, grad_shape);
38 grad /= (grad.size() / incoming.size());
39}

References nnfw::cker::BroadcastTo(), and nnfw::cker::MapAsMatrixWithLastDimAsRows().

Referenced by onert::backend::train::ops::MeanLayer::backward().

◆ MSE()

template<typename T >
void nnfw::cker::train::MSE ( const Shape y_pred_shape,
const T *  y_pred_data,
const Shape y_true_shape,
const T *  y_true_data,
const Shape output_shape,
T *  output_data 
)
inline

Definition at line 41 of file Loss.h.

43{
44 if (output_shape.DimensionsCount() != 1)
45 throw std::runtime_error("cker::MSE: output dimension count should be 1");
46 if (output_shape.Dims(0) != y_pred_shape.Dims(0))
47 throw std::runtime_error("cker::MSE: output and y_pred do not have the same batch");
48 if (y_pred_shape != y_true_shape)
49 throw std::runtime_error("cker::MSE: y_pred_shape != y_true_shape");
50
51 const auto batch = y_pred_shape.Dims(0);
52 const auto size = FlatSizeSkipDim(y_pred_shape, 0);
53
54 for (int b = 0; b < batch; ++b)
55 {
56 float sum = 0.f;
57 for (int i = 0; i < size; ++i)
58 {
59 sum += square(y_pred_data[b * size + i] - y_true_data[b * size + i]);
60 }
61 output_data[b] = static_cast<T>(sum / size);
62 }
63}
T square(T value)
Definition Loss.h:37
int FlatSizeSkipDim(const Shape &shape, int skip_dim)
Definition Shape.h:253
int32_t size[5]
Definition Slice.cpp:35

References nnfw::cker::Shape::Dims(), nnfw::cker::FlatSizeSkipDim(), output_shape, size, and square().

Referenced by onert::backend::train::ops::LossMeanSquaredErrorLayer::forward().

◆ MSEGrad()

template<typename T >
void nnfw::cker::train::MSEGrad ( const Shape y_pred_shape,
const T *  y_pred_data,
const Shape y_true_shape,
const T *  y_true_data,
const Shape grad_shape,
T *  grad_data,
LossReductionType  reduction_type 
)
inline

Definition at line 66 of file Loss.h.

69{
70 if (y_pred_shape != y_true_shape)
71 throw std::runtime_error("cker::MSEGrad: y_pred_shape != y_true_shape");
72 if (y_pred_shape != grad_shape)
73 throw std::runtime_error("cker::MSEGrad: y_pred_shape != grad_shape");
74
75 const int batch_size = grad_shape.Dims(0);
76 const auto flat_size = FlatSizeSkipDim(grad_shape, 0);
77 auto reduction_size = 1;
78 switch (reduction_type)
79 {
80 case LossReductionType::SUM_OVER_BATCH_SIZE:
81 reduction_size = batch_size * flat_size;
82 break;
83 case LossReductionType::SUM:
84 reduction_size = flat_size;
85 break;
86 default:
87 throw std::runtime_error("Unsupported reduction type");
88 }
89
90 for (int b = 0; b < batch_size; ++b)
91 {
92 for (int i = 0; i < flat_size; ++i)
93 {
94 const int offset = b * flat_size + i;
95 assert(offset >= 0);
96 grad_data[offset] =
97 static_cast<T>(-2 * (y_true_data[offset] - y_pred_data[offset]) / reduction_size);
98 }
99 }
100}

References nnfw::cker::Shape::Dims(), nnfw::cker::FlatSizeSkipDim(), offset(), SUM, and SUM_OVER_BATCH_SIZE.

Referenced by onert::backend::train::ops::LossMeanSquaredErrorLayer::backward().

◆ ReLU6Grad()

void nnfw::cker::train::ReLU6Grad ( const Shape output_shape,
const float *  output_data,
const Shape incoming_shape,
const float *  incoming_data,
const Shape grad_shape,
float *  grad_data 
)
inline

Definition at line 31 of file ReLU6.h.

34{
35 const auto output_map = MapAsVector(output_data, output_shape);
36 const auto incoming_map = MapAsVector(incoming_data, incoming_shape);
37 auto grad_map = MapAsVector(grad_data, grad_shape);
38
39 if (output_shape != incoming_shape || output_shape != grad_shape)
40 throw std::runtime_error{"cker::ReLU6Grad: Unsupported shape"};
41
42 grad_map.array() =
43 incoming_map.array() *
44 (0.0f < output_map.array() && output_map.array() < 6.0f).template cast<float>();
45}

References nnfw::cker::MapAsVector(), and output_shape.

Referenced by onert::backend::train::ops::backpropActivation(), and onert::backend::train::ops::ElementwiseActivationLayer::configureBackward().

◆ ReLUGrad()

void nnfw::cker::train::ReLUGrad ( const Shape output_shape,
const float *  output_data,
const Shape incoming_shape,
const float *  incoming_data,
const Shape grad_shape,
float *  grad_data 
)
inline

Definition at line 32 of file ReLU.h.

35{
36 const auto output_map = MapAsVector(output_data, output_shape);
37 const auto incoming_map = MapAsVector(incoming_data, incoming_shape);
38 auto grad_map = MapAsVector(grad_data, grad_shape);
39
40 if (output_shape == incoming_shape && output_shape == grad_shape)
41 grad_map.array() = incoming_map.array() * (output_map.array() > 0.0f).template cast<float>();
42 else
43 throw std::runtime_error("cker::ReLUGrad: Unsupported shape");
44}

References nnfw::cker::MapAsVector(), and output_shape.

Referenced by onert::backend::train::ops::backpropActivation(), and onert::backend::train::ops::ElementwiseActivationLayer::configureBackward().

◆ SoftMaxGrad()

void nnfw::cker::train::SoftMaxGrad ( const Shape output_shape,
const float *  output_data,
const Shape incoming_shape,
const float *  incoming_data,
const Shape grad_shape,
float *  grad_data 
)
inline

Definition at line 30 of file SoftMax.h.

33{
34 // TODO Support 4dim softmax gradient
35 assert(incoming_shape.DimensionsCount() == 2);
36 MatchingFlatSize(output_shape, incoming_shape, grad_shape);
37
38 const int batches = incoming_shape.Dims(0);
39 const int width = incoming_shape.Dims(1);
40
41 for (int b = 0; b < batches; ++b)
42 {
43 int b_offset = b * width;
44 for (int w1 = 0; w1 < width; ++w1)
45 {
46 float sum = 0.0f;
47 for (int w2 = 0; w2 < width; ++w2)
48 {
49 float val;
50 if (w1 == w2)
51 {
52 val = output_data[b_offset + w2] * (1.f - output_data[b_offset + w2]);
53 }
54 else
55 {
56 val = -output_data[b_offset + w2] * output_data[b_offset + w1];
57 }
58 val *= incoming_data[b_offset + w2];
59 sum += val;
60 }
61 grad_data[b_offset + w1] = sum;
62 }
63 }
64}
int MatchingFlatSize(const Dims< N > &dims, const Dims< N > &check_dims_0)
Definition Dims.h:108

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), nnfw::cker::MatchingFlatSize(), and output_shape.

Referenced by onert::backend::train::ops::SoftMaxLayer::backward().

◆ square()

template<typename T >
T nnfw::cker::train::square ( value)
inline

Definition at line 37 of file Loss.h.

37{ return value * value; }

Referenced by MSE().