66 * LICENSE file in the root directory of this source tree.
77 */
88
9+ #include < algorithm>
910#include < array>
11+ #include < cstddef>
1012#include < cstdint>
13+ #include < initializer_list>
14+ #include < limits>
15+ #include < string>
1116
1217#include < esp_nn.h>
1318#include < pybind11/pybind11.h>
1419#include < pybind11/stl.h>
1520
1621namespace {
1722
23+ template <std::size_t N>
24+ void check_bounds (
25+ const std::array<int32_t , N>& values,
26+ const char * name,
27+ int32_t minimum = 1 ,
28+ int32_t maximum = std::numeric_limits<uint16_t >::max()) {
29+ if (std::any_of (values.begin (), values.end (), [=](int32_t value) {
30+ return value < minimum || value > maximum;
31+ })) {
32+ throw pybind11::value_error (
33+ std::string (name) + " must be in [" + std::to_string (minimum) + " , " +
34+ std::to_string (maximum) + " ]" );
35+ }
36+ }
37+
38+ int64_t checked_product (std::initializer_list<int64_t > factors) {
39+ int64_t product = 1 ;
40+ for (const auto factor : factors) {
41+ if (factor > std::numeric_limits<int32_t >::max () / product) {
42+ throw pybind11::value_error (
43+ " scratch query intermediates exceed the int32 range" );
44+ }
45+ product *= factor;
46+ }
47+ return product;
48+ }
49+
50+ void check_conv_arithmetic (
51+ const std::array<int32_t , 3 >& input,
52+ const std::array<int32_t , 3 >& filter,
53+ const std::array<int32_t , 3 >& output,
54+ const std::array<int32_t , 2 >& stride,
55+ const std::array<int32_t , 2 >& padding,
56+ int32_t channel_multiplier = 1 ) {
57+ const int64_t aligned_channels = (int64_t {input[2 ]} + 15 ) & ~int64_t {15 };
58+ const int64_t output_channels =
59+ std::max (int64_t {output[2 ]}, int64_t {input[2 ]} * channel_multiplier);
60+ std::array<int64_t , 2 > extent;
61+ for (std::size_t axis = 0 ; axis < extent.size (); ++axis) {
62+ extent[axis] =
63+ std::max (int64_t {input[axis]}, int64_t {output[axis]} * stride[axis]) +
64+ 2 * int64_t {padding[axis]} + filter[axis];
65+ }
66+ // Bound padded copies, aligned filters and coordinates before vendor int32
67+ // arithmetic. S3 stages 16 windows plus 24 windows of panel work and a 16 KiB
68+ // panel. Four int32 channel arrays and 256 bytes cover parameters and
69+ // alignment.
70+ const auto input_bound =
71+ checked_product ({extent[0 ], extent[1 ], aligned_channels});
72+ const auto filter_bound = checked_product (
73+ {int64_t {filter[0 ]} * aligned_channels + 15 , filter[1 ], output_channels});
74+ const auto output_bound = checked_product (
75+ {output[0 ], output[1 ], std::max (aligned_channels, output_channels)});
76+ const auto window_bound =
77+ checked_product ({filter[0 ], filter[1 ], aligned_channels});
78+ const auto bound = 2 * input_bound + 2 * filter_bound + output_bound +
79+ 40 * window_bound + 16 * std::max (aligned_channels, output_channels) +
80+ 16 * 1024 + 256 ;
81+ if (bound > std::numeric_limits<int32_t >::max ()) {
82+ throw pybind11::value_error (
83+ " scratch query intermediates exceed the int32 range" );
84+ }
85+ }
86+
1887int conv2d_scratch_size (
1988 std::array<int32_t , 3 > input_dims,
2089 std::array<int32_t , 3 > filter_dims,
2190 std::array<int32_t , 3 > output_dims,
2291 std::array<int32_t , 2 > stride,
2392 std::array<int32_t , 2 > padding) {
93+ check_bounds (input_dims, " input_dims" );
94+ check_bounds (filter_dims, " filter_dims" );
95+ check_bounds (output_dims, " output_dims" );
96+ check_bounds (stride, " stride" );
97+ check_bounds (padding, " padding" , 0 );
98+ check_conv_arithmetic (input_dims, filter_dims, output_dims, stride, padding);
2499 const data_dims_t input{input_dims[0 ], input_dims[1 ], input_dims[2 ], 0 };
25100 const data_dims_t filter{filter_dims[0 ], filter_dims[1 ], filter_dims[2 ], 0 };
26101 const data_dims_t output{output_dims[0 ], output_dims[1 ], output_dims[2 ], 0 };
@@ -38,6 +113,19 @@ int depthwise_conv2d_scratch_size(
38113 std::array<int32_t , 2 > stride,
39114 std::array<int32_t , 2 > padding,
40115 int32_t channel_multiplier) {
116+ check_bounds (input_dims, " input_dims" );
117+ check_bounds (filter_dims, " filter_dims" );
118+ check_bounds (output_dims, " output_dims" );
119+ check_bounds (stride, " stride" );
120+ check_bounds (padding, " padding" , 0 );
121+ check_bounds (std::array{channel_multiplier}, " channel_multiplier" );
122+ check_conv_arithmetic (
123+ input_dims,
124+ filter_dims,
125+ output_dims,
126+ stride,
127+ padding,
128+ channel_multiplier);
41129 const data_dims_t input{input_dims[0 ], input_dims[1 ], input_dims[2 ], 0 };
42130 const data_dims_t filter{filter_dims[0 ], filter_dims[1 ], filter_dims[2 ], 0 };
43131 const data_dims_t output{output_dims[0 ], output_dims[1 ], output_dims[2 ], 0 };
@@ -50,6 +138,42 @@ int depthwise_conv2d_scratch_size(
50138 &input, &filter, &output, ¶ms);
51139}
52140
141+ int32_t softmax_scratch_size (int32_t width, int32_t height) {
142+ check_bounds (
143+ std::array{width, height},
144+ " softmax dimensions" ,
145+ 1 ,
146+ std::numeric_limits<int32_t >::max ());
147+ checked_product ({width, sizeof (int32_t )});
148+ return esp_nn_get_softmax_scratch_size (width, height);
149+ }
150+
151+ int32_t mean_scratch_size (int32_t height, int32_t width, int32_t channels) {
152+ check_bounds (
153+ std::array{height, width, channels},
154+ " mean dimensions" ,
155+ 1 ,
156+ std::numeric_limits<int32_t >::max ());
157+ return esp_nn_get_mean_scratch_size (height, width, channels);
158+ }
159+
160+ int fc_batch_scratch_size (int32_t batches, int32_t row_len, int32_t out_ch) {
161+ check_bounds (
162+ std::array{batches}, " batches" , 1 , std::numeric_limits<int32_t >::max ());
163+ check_bounds (std::array{row_len}, " row_len" );
164+ check_bounds (std::array{out_ch}, " out_ch" );
165+ if (batches <= std::numeric_limits<uint16_t >::max ()) {
166+ check_conv_arithmetic (
167+ {1 , batches, row_len},
168+ {1 , 1 , row_len},
169+ {1 , batches, out_ch},
170+ {1 , 1 },
171+ {0 , 0 });
172+ }
173+ return esp_nn_get_fc_batch_scratch_size (
174+ batches, static_cast <uint16_t >(row_len), static_cast <uint16_t >(out_ch));
175+ }
176+
53177} // namespace
54178
55179namespace py = pybind11;
@@ -77,12 +201,12 @@ PYBIND11_MODULE(ESP_NN_SCRATCH_MODULE, m) {
77201 py::arg (" channel_multiplier" ));
78202 m.def (
79203 " softmax_scratch_size" ,
80- &esp_nn_get_softmax_scratch_size ,
204+ &softmax_scratch_size ,
81205 py::arg (" width" ),
82206 py::arg (" height" ));
83207 m.def (
84208 " mean_scratch_size" ,
85- &esp_nn_get_mean_scratch_size ,
209+ &mean_scratch_size ,
86210 py::arg (" height" ),
87211 py::arg (" width" ),
88212 py::arg (" channels" ));
@@ -91,7 +215,7 @@ PYBIND11_MODULE(ESP_NN_SCRATCH_MODULE, m) {
91215 });
92216 m.def (
93217 " fc_batch_scratch_size" ,
94- &esp_nn_get_fc_batch_scratch_size ,
218+ &fc_batch_scratch_size ,
95219 py::arg (" batches" ),
96220 py::arg (" row_len" ),
97221 py::arg (" out_ch" ));
0 commit comments