21#include "Validation.h"
42Index reduce_index(
const Index &index, uint32_t axis)
47 for (uint32_t i = 0; i <
index.rank(); ++i)
54Shape reduce_shape(
const Shape &shape, uint32_t axis)
59 for (uint32_t i = 0; i < shape.
rank(); ++i)
60 r_shape.
dim(i) = shape.
dim(i);
61 r_shape.
dim(axis) = 1;
77 validate(input_data,
"Input not ready");
79 "Input domain of TensorSoftmax is not Tensor");
81 std::unique_ptr<NodeData> softmax_data =
nullptr;
85 case loco::DataType::FLOAT32:
90 auto input_bufptr =
input_data->as_f32_bufptr();
91 auto softmax_buf = make_buffer<float, LexicalLayout>(*
input_data->shape());
93 auto reduce_sum_shape = reduce_shape(*input_shape, axis);
94 auto reduce_sum_bufptr = make_buffer<float, LexicalLayout>(reduce_sum_shape);
98 const auto &
index = e.current();
99 const auto r_index = reduce_index(index, axis);
101 reduce_sum_bufptr.at(r_index) +=
exp(input_bufptr->at(index));
106 const auto &
index = e.current();
107 const auto r_index = reduce_index(index, axis);
109 softmax_buf.at(index) =
exp(input_bufptr->at(index)) / reduce_sum_bufptr.at(r_index);
116 throw std::runtime_error(
"NYI for this DataType");
119 assert(softmax_data !=
nullptr);
120 annot_data(softmax, std::move(softmax_data));
Computes softmax activations for Tensor domain.
Index & resize(uint32_t size)
uint32_t & at(uint32_t axis)
uint32_t & dim(uint32_t axis)
Shape & resize(uint32_t size)
uint32_t rank(void) const
bool validate(Code *code)
void annot_domain(loco::Node *node, const loco::Domain &domain)
Wrapper to annotate domain to node. Cannot annotate unknown domain.
std::unique_ptr< NodeData > make_data(const NodeData::Buffer< DT > &buffer)
Copy buffer to make NodeData.
loco::GraphInputIndex index(const TFPlaceholder *node)
Buffer< T > make_buffer(const Shape &shape)