MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Layers.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @file Layers.hpp
7 * @brief Descriptions of the layers the tensor kernels run, and their output sizes.
8 */
9
11
12/**
13 * @enum Activation
14 * @brief Output activation of a layer.
15 */
16enum class Activation : uint8_t {
21};
22
23/**
24 * @struct Conv1d
25 * @brief One convolution layer over [batch, c_in, t_in], with weights [c_out, c_in, kernel].
26 *
27 * The input is zero padded by pad on both sides, tap k reads dilation * k
28 * samples past the window start, and windows advance by stride. relu_input
29 * applies ReLU to the input before the weights.
30 */
31struct Conv1d {
32 uint32_t c_in {};
33 uint32_t c_out {};
34 uint32_t kernel {};
35 uint32_t stride { 1 };
36 uint32_t pad { 0 };
37 uint32_t dilation { 1 };
38 bool relu_input { false };
40};
41
42/**
43 * @struct GroupedConv1d
44 * @brief One grouped convolution layer over [batch, c_in, t_in], with weights [c_out, c_in / groups, kernel].
45 *
46 * The channels are split into groups equal runs and each output channel reads
47 * only the input channels of its own run. The input is zero padded by pad on
48 * both sides and windows advance by stride. trim drops that many outputs from
49 * the end.
50 */
52 uint32_t c_in {};
53 uint32_t c_out {};
54 uint32_t kernel {};
55 uint32_t groups { 1 };
56 uint32_t stride { 1 };
57 uint32_t pad { 0 };
58 uint32_t trim { 0 };
60};
61
62/**
63 * @enum NormAffine
64 * @brief Which index selects the weight and bias applied after a normalisation.
65 */
66enum class NormAffine : uint8_t {
67 None, ///< No weight or bias
68 Along, ///< The position along the normalised axis, as layer normalisation does
69 Outer ///< The outer index, as a per channel normalisation over time does
70};
71
72namespace detail {
73
74 [[noreturn]] inline void refuse(std::string_view message)
75 {
77 std::source_location::current(), "{}", message);
78 }
79
80}
81
82/**
83 * @brief Output length of a convolution, 0 when the window does not fit the padded input.
84 * @param layer The layer. Kernel, stride and dilation must be at least 1.
85 * @param t_in Input length.
86 * @throws std::invalid_argument When kernel, stride or dilation is 0.
87 */
88[[nodiscard]] inline uint32_t conv1d_length(const Conv1d& layer, uint32_t t_in)
89{
90 if (layer.kernel == 0 || layer.stride == 0 || layer.dilation == 0) {
91 detail::refuse("Conv1d needs a kernel, stride and dilation of at least 1");
92 }
93 const uint64_t span = static_cast<uint64_t>(layer.dilation) * (layer.kernel - 1U) + 1U;
94 const uint64_t padded = static_cast<uint64_t>(t_in) + 2ULL * layer.pad;
95 return padded < span ? 0U : static_cast<uint32_t>((padded - span) / layer.stride + 1U);
96}
97
98/**
99 * @brief Output length of a grouped convolution after its trim, 0 when nothing is left.
100 * @param layer The layer. Kernel, stride and groups must be at least 1.
101 * @param t_in Input length.
102 * @throws std::invalid_argument When kernel, stride or groups is 0.
103 */
104[[nodiscard]] inline uint32_t grouped_conv1d_length(const GroupedConv1d& layer, uint32_t t_in)
105{
106 if (layer.kernel == 0 || layer.stride == 0 || layer.groups == 0) {
107 detail::refuse("Grouped Conv1d needs a kernel, stride and groups of at least 1");
108 }
109 const uint64_t padded = static_cast<uint64_t>(t_in) + 2ULL * layer.pad;
110 if (padded < layer.kernel) {
111 return 0U;
112 }
113 const uint64_t full = (padded - layer.kernel) / layer.stride + 1U;
114 return full > layer.trim ? static_cast<uint32_t>(full - layer.trim) : 0U;
115}
116
117/**
118 * @brief Length of one frame nn_frame_audio writes.
119 */
120[[nodiscard]] constexpr uint32_t frame_audio_length(uint32_t chunk_size, uint32_t context, uint32_t pad)
121{
122 return context + chunk_size + pad;
123}
124
125} // namespace MayaFlux::Yantra::TensorKernels
@ ComputeMatrix
Compute operations (Yantra - algorithms, matrices, DSP).
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
void refuse(std::string_view message)
Definition Layers.hpp:74
NormAffine
Which index selects the weight and bias applied after a normalisation.
Definition Layers.hpp:66
@ Along
The position along the normalised axis, as layer normalisation does.
Definition Layers.hpp:68
@ Outer
The outer index, as a per channel normalisation over time does.
Definition Layers.hpp:69
Activation
Output activation of a layer.
Definition Layers.hpp:16
constexpr uint32_t frame_audio_length(uint32_t chunk_size, uint32_t context, uint32_t pad)
Length of one frame nn_frame_audio writes.
Definition Layers.hpp:120
uint32_t grouped_conv1d_length(const GroupedConv1d &layer, uint32_t t_in)
Output length of a grouped convolution after its trim, 0 when nothing is left.
Definition Layers.hpp:104
uint32_t conv1d_length(const Conv1d &layer, uint32_t t_in)
Output length of a convolution, 0 when the window does not fit the padded input.
Definition Layers.hpp:88
One convolution layer over [batch, c_in, t_in], with weights [c_out, c_in, kernel].
Definition Layers.hpp:31
One grouped convolution layer over [batch, c_in, t_in], with weights [c_out, c_in / groups,...
Definition Layers.hpp:51