-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapyfixedarray.h
More file actions
402 lines (333 loc) · 16.8 KB
/
Copy pathapyfixedarray.h
File metadata and controls
402 lines (333 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Array/tensor type for arbitrary precision fixed-point formats.
*/
#ifndef _APYFIXEDARRAY_H
#define _APYFIXEDARRAY_H
#include "apyarray.h"
#include "apybuffer.h"
#include "apyfixed.h"
#include "apytypes_common.h"
#include "apytypes_util.h"
#include <nanobind/nanobind.h> // nanobind::object
#include <nanobind/ndarray.h> // nanobind::array_t
#include <nanobind/stl/variant.h> // std::variant (with nanobind support)
namespace nb = nanobind;
#include <cstddef> // std::size_t
#include <limits> // std::numeric_limits<>::is_iec559
#include <optional> // std::optional, std::nullopt
#include <string> // std::string
#include <string_view> // std::string_view
#include <vector> // std::vector
class APyFixedArray : public APyArray<apy_limb_t, APyFixedArray> {
/* ****************************************************************************** *
* * APyFixedArray C++ assumptions * *
* ****************************************************************************** */
static_assert(
(APY_LIMB_SIZE_BYTES == 8 || APY_LIMB_SIZE_BYTES == 4),
"The `apy_limb_t` data type is either 64-bit or 32-bit. Any other limb size "
"is unsupported. The size of limbs is specified during compilation with the C "
"Macro `COMPILER_LIMB_SIZE`."
);
static_assert(
(-1 >> 1 == -1),
"Right shift applied to signed integral types performs *arithmetic* right "
"shift. Arithmetic right shift of signed types is *the only* valid behaviour "
"since C++20, but before C++20 the right shift of signed integral types is "
"implementation defined. APyFixed relies heavily on arithmetic right shift."
);
static_assert(
(std::numeric_limits<double>::is_iec559),
"We assume IEEE-754 double-precision floating-point types."
);
/* ****************************************************************************** *
* * APyFixedArray data fields * *
* ****************************************************************************** */
private:
int _bits;
int _int_bits;
/* ****************************************************************************** *
* * CRTP methods * *
* ****************************************************************************** */
public:
//! Name of this array type (used when throwing errors)
static constexpr auto ARRAY_NAME = std::string_view("APyFixedArray");
APyFixed create_scalar() const { return APyFixed(_bits, _int_bits); }
APyFixedArray create_array(const std::vector<std::size_t>& shape) const
{
return APyFixedArray(shape, _bits, _int_bits);
}
static APyFixedArray
create_array_static(const std::vector<std::size_t>& shape, const APyFixed& fix)
{
return APyFixedArray(shape, fix._bits, fix._int_bits);
}
//! Test if two fixed-point vectors have the same bit specifiers
APY_INLINE bool is_same_spec(const APyFixedArray& other) const noexcept
{
return _bits == other._bits && _int_bits == other._int_bits;
}
//! Test if `*this` has the same bit specifiers as another `APyFixed`
APY_INLINE bool is_same_spec(const APyFixed& other) const noexcept
{
return _bits == other._bits && _int_bits == other._int_bits;
}
//! Retrieve the bit specification
APY_INLINE APyFixedSpec spec() const noexcept { return { _bits, _int_bits }; }
/* ****************************************************************************** *
* * Python constructors * *
* ****************************************************************************** */
public:
//! No default (empty) constructed `APyFixedArray` objects. At least the
//! bit-specifiers and shape has to be set during construction.
APyFixedArray() = delete;
explicit APyFixedArray(
const nb::typed<nb::iterable, nb::any>& bit_pattern_sequence,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
/* ****************************************************************************** *
* * Non-Python accessible constructors * *
* ****************************************************************************** */
public:
//! Constructor: specify only shape and word-length. Zero data on construction.
explicit APyFixedArray(
const std::vector<std::size_t>& shape, int bits, int int_bits
);
//! Constructor: specify shape and word-length and steal the data from vector
explicit APyFixedArray(
const std::vector<std::size_t>& shape, int bits, int int_bits, vector_type&& v
);
//! Constructor: specify only shape and word-length. Zero data on construction.
explicit APyFixedArray(
const std::vector<std::size_t>& shape,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
/* ****************************************************************************** *
* * Binary arithmetic operators * *
* ****************************************************************************** */
private:
//! Base addition/subtraction routine for `APyFixedArray`
template <class ripple_carry_op, class simd_op, class simd_shift_op>
inline APyFixedArray _apyfixedarray_base_add_sub(const APyFixedArray& rhs) const;
//! Base addition/subtraction routine for `APyFixedArray` with `APyFixed`
template <class ripple_carry_op, class simd_op_const, class simd_shift_op_const>
inline APyFixedArray _apyfixed_base_add_sub(const APyFixed& rhs) const;
public:
APyFixedArray operator+(const APyFixedArray& rhs) const;
APyFixedArray operator+(const APyFixed& rhs) const;
APyFixedArray operator-(const APyFixedArray& rhs) const;
APyFixedArray operator-(const APyFixed& rhs) const;
APyFixedArray operator*(const APyFixedArray& rhs) const;
APyFixedArray operator*(const APyFixed& rhs) const;
APyFixedArray operator/(const APyFixedArray& rhs) const;
APyFixedArray operator/(const APyFixed& rhs) const;
APyFixedArray operator<<(const int shift_val) const;
APyFixedArray operator>>(const int shift_val) const;
APyFixedArray& operator<<=(const int shift_val);
APyFixedArray& operator>>=(const int shift_val);
APyFixedArray rsub(const APyFixed& rhs) const;
APyFixedArray rdiv(const APyFixed& rhs) const;
template <typename T> ThirdPartyArray<bool> operator==(const T& rhs) const;
template <typename T> ThirdPartyArray<bool> operator!=(const T& rhs) const;
template <typename T> ThirdPartyArray<bool> operator<(const T& rhs) const;
template <typename T> ThirdPartyArray<bool> operator<=(const T& rhs) const;
template <typename T> ThirdPartyArray<bool> operator>(const T& rhs) const;
template <typename T> ThirdPartyArray<bool> operator>=(const T& rhs) const;
//! Elementwise unary negation
APyFixedArray operator-() const;
//! Elementwise unary positive
APY_INLINE APyFixedArray operator+() const { return *this; };
//! Elementwise logic not
APyFixedArray operator~() const;
APyFixedArray operator&(const APyFixedArray& rhs) const;
/*!
* Matrix multiplication. If both arguments ar 2-D tensors, this method performs the
* ordinary matrix multiplication. If input dimensions are greater than 2, this
* method performs stacked matrix multiplications, where the dimensions of last two
* dimensions are treated as matrices.
*/
std::variant<APyFixedArray, APyFixed> matmul(const APyFixedArray& rhs) const;
/* ****************************************************************************** *
* * Public member functions * *
* ****************************************************************************** */
//! Perform a linear convolution with `other` using `mode`
APyFixedArray convolve(const APyFixedArray& other, const std::string& mode) const;
//! Sum over one or more axes.
std::variant<APyFixedArray, APyFixed>
sum(const std::optional<PyShapeParam_t>& axis = std::nullopt) const;
//! Cumulative sum over one or more axes.
APyFixedArray cumsum(std::optional<nb::int_> axis = std::nullopt) const;
//! Multiplication over one or more axes.
std::variant<APyFixedArray, APyFixed>
prod(const std::optional<PyShapeParam_t>& axis = std::nullopt) const;
//! Cumulative multiplication over one or more axes.
APyFixedArray cumprod(std::optional<nb::int_> axis = std::nullopt) const;
//! Return the maximum of an array or the maximum along an axis.
std::variant<APyFixedArray, APyFixed>
max(const std::optional<PyShapeParam_t>& axis = std::nullopt) const;
//! Return the minimum of an array or the minimum along an axis.
std::variant<APyFixedArray, APyFixed>
min(const std::optional<PyShapeParam_t>& axis = std::nullopt) const;
//! Python `__repr__()` function
std::string repr() const;
//! Number of bits
APY_INLINE int bits() const noexcept { return _bits; }
//! Number of integer bits
APY_INLINE int int_bits() const noexcept { return _int_bits; }
//! Number of fractional bits
APY_INLINE int frac_bits() const noexcept { return _bits - _int_bits; }
//! Extract bit-pattern
std::variant<
nb::list,
nb::ndarray<nb::numpy, uint64_t>,
nb::ndarray<nb::numpy, uint32_t>,
nb::ndarray<nb::numpy, uint16_t>,
nb::ndarray<nb::numpy, uint8_t>>
to_bits(bool numpy = false) const;
//! Create an N-dimensional array containing bit-patterns.
template <typename NB_ARRAY_TYPE, typename INT_TYPE>
nb::ndarray<NB_ARRAY_TYPE, INT_TYPE> to_bits_ndarray() const;
//! Create a nested Python list containing bit-patterns as Python integers.
nb::list to_bits_python_recursive_descent(
std::size_t dim,
APyBuffer<apy_limb_t>::vector_type::const_iterator& it,
bool vec_is_signed = false
) const;
//! Extract bit-pattern as signed Python integers
nb::list to_signed_bits() const;
//! Convert to a NumPy array
nanobind::ndarray<nanobind::numpy, double> to_numpy(
std::optional<nb::object> dtype = std::nullopt,
std::optional<bool> copy = std::nullopt
) const;
//! Elementwise absolute value
APyFixedArray abs() const;
/*!
* Construct a new `APyFixedArray` tensor object with the same `shape` and
* fixed-point values as `*this`, but with a new word-length. The underlying
* bit-pattern of each tensor element are copied into place, meaning that lowering
* the number of fractional bits may result in quantization, and lowering the number
* of integer bits may result in overflowing. Supports quantization and overflow
* options on narrowing casts.
*/
APyFixedArray cast(
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<QuantizationMode> quantization = std::nullopt,
std::optional<OverflowMode> overflow = std::nullopt,
std::optional<int> bits = std::nullopt
) const;
/* ****************************************************************************** *
* * Static conversion from other types * *
* ****************************************************************************** */
//! Create an `APyFixedArray` tensor object initialized with values from a sequence
//! of Python objects
static APyFixedArray from_numbers(
const nb::typed<nb::iterable, nb::any>& number_seq,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
//! Create an `APyFixedArray` tensor object initialized with values from an ndarray
static APyFixedArray from_array(
const nb::ndarray<nb::c_contig>& double_seq,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
/* ****************************************************************************** *
* * Static methods for array initialization * *
* ****************************************************************************** */
//! Create an `APyFixedArray` initialized with zeros
static APyFixedArray zeros(
const PyShapeParam_t& shape,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
//! Create an `APyFixedArray` initialized with ones
static APyFixedArray ones(
const PyShapeParam_t& shape,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
//! Create an `APyFixedArray` with ones on the diagonal and zeros elsewhere
static APyFixedArray
eye(const nb::int_& N,
std::optional<nb::int_> M = std::nullopt,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt);
//! Create a square `APyFixedArray` with ones on the diagonal and zeros elsewhere
static APyFixedArray identity(
const nb::int_& N,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
//! Create an `APyFixedArray` with evenly spaced values within a given interval
static APyFixedArray arange(
const nb::object& start,
const nb::object& stop,
const nb::object& step,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
//! Create an `APyFixedArray` with all values within a given interval
static APyFixedArray fullrange(
const nb::object& start,
const nb::object& stop,
std::optional<int> int_bits = std::nullopt,
std::optional<int> frac_bits = std::nullopt,
std::optional<int> bits = std::nullopt
);
/* ****************************************************************************** *
* Conversion to other types *
* ****************************************************************************** */
public:
//! Retrieve a string of the stored values in this array.
std::string to_string(int base = 10) const;
std::string to_string_dec() const;
/* ****************************************************************************** *
* * Private member functions * *
* ****************************************************************************** */
private:
/*!
* Evaluate the 2D matrix product between `*this` and `rhs`, possibly using an
* accumulator mode `mode`. This method assumes that the shape of `*this` and `rhs`
* have been checked to match a 2D matrix-matrix or matrix-vector multiplication.
* Anything else is undefined behaviour. Return result in a new `APyFixedArray`.
*/
APyFixedArray _checked_2d_matmul(
const APyFixedArray& rhs, // rhs
std::optional<APyFixedAccumulatorOption> mode // optional accumulation mode
) const;
/*!
* Evaluate the inner product between `*this` and `rhs`, possibly using an
* accumulator mode `mode`. This method assumes that the shape of both `*this`
* and `rhs` are equally long. Anything else is undefined behaviour. Return result
* in a new `APyFixedArray`.
*/
APyFixed _checked_inner_product(
const APyFixedArray& rhs, // rhs
std::optional<APyFixedAccumulatorOption> mode // optional accumulation mode
) const;
/*!
* Set the underlying bit values of `*this` from a NDArray object of integers. This
* member function assumes that the shape of `*this` and `ndarray` are equal.
*/
void _set_bits_from_ndarray(const nb::ndarray<nb::c_contig>& ndarray);
/*!
* Set the values of `*this` from a NDArray object of floats/integers. This member
* function assumes that the shape of `*this` and `ndarray` are equal. The elements
* in `ndarray` are explicitly converted to `double` before being copied into
* `*this`.
*/
void _set_values_from_ndarray(const nb::ndarray<nb::c_contig>& ndarray);
};
#endif // _APYFIXEDARRAY_H