Porting Numpy to DaphneLib in Python Scripts
A large part of the operations offered by Numpy can already be expressed in DaphneLib. By porting Numpy-based Python scripts to DaphneLib-based Python scripts, existing workflows can be executed in DAPHNE. In some cases, this translation is straightforward, especially when a Numpy operation can be replaced by a DaphneLib operation with the same name. However, in some cases this mapping may not be obvious. Some Numpy operations must be replaced by a single DaphneLib operation with a different name or by a more general/specialized operation. Some Numpy operations must be replaced by a complex expression of DaphneLib operations.
In the following, we provide a few non-exhaustive guidelines on porting Numpy-based Python scripts to DaphneLib-based Python scripts. These guidelines represent some cases we have stumbled upon so far. However, we hope that they convey some general insights into how to approach the translation. In general, the DaphneLib API is subject to future changes, so things that are difficult at the moment may become easier in the future.
Some Noteworthy Differences of Numpy and DaphneLib
Without claim of completeness.
-
Numpy supports n-dimensional data objects. DAPHNE natively only supports 2-dimensional data objects. 1-d data objects can be represented in DAPHNE as 2-d objects where one dimension is 1. General n-d objects can be represented in DAPHNE as 2-d objects where all but one dimension are linearized into the second dimension. Furthermore, native support for n-d tensors is work-in-progress in DAPHNE.
-
Both Numpy and DAPHNE support various types for the elements of a matrix. These are called dtypes in Numpy and value types in DAPHNE. Numpy's dtypes can be mapped to DAPHNE value types with the following table:
Numpy dtype DAPHNE vtype np.float64
"f64"
np.float32
"f32"
np.int64
"si64"
np.int32
"si32"
np.int8
"si8"
np.uint64
"ui64"
np.uint32
"ui32"
np.uint8
"ui8"
-
The Numpy API offers access to low-level aspects that DAPHNE does not, because DAPHNE decides (or will decide) these points automatically to optimize the program. Examples include: uninitialized data, the memory layout/order (e.g., C, F, ...), device placement, providing an output object, etc. DAPHNE supports (or will support) hints for expert users to optionally make such decision by hand. Such hints are already partly supported in DaphneDSL, but not supported in DaphneLib yet.
Concrete Example Operations
In the following, we assume the Numpy 2.0 API. Porting for other Numpy versions may be possible by following similar lines of thought.
-
numpy.
astype
(x, dtype, /, *, copy=True)
Parameters
x
: supporteddtype
: supported- Numpy dtypes are mapped to DAPHNE value types using the table above.
copy
: not supported
Example
-
Numpy
-
DaphneLib
-
numpy.
column_stack
(tup)
Parameters
tup
: supported
Example
-
Numpy
-
DaphneLib
-
numpy.
concatenate
((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
Parameters
(a1, a2, ...)
: supported- only two at a time
axis
: supported- via different operation names (
rbind
/cbind
)
- via different operation names (
out
: not supporteddtype
: supported- documentation coming later
Example 1
-
Numpy
-
DaphneLib
Example 2
-
Numpy
-
DaphneLib
-
numpy.
copy
(a, order='K', subok=False)
Explicit copying is generally not supported/necessary in DAPHNE. Editable references to a data object are not possible in DAPHNE, since references are copy-on-write.
-
numpy.
empty
(shape, dtype=float, order='C', *, device=None, like=None)
Explicitly creating an uninitialized matrix is not possible in DAPHNE. As a workaround, one could create an initialized matrix.
Parameters
shape
: supporteddtype
: supported- Numpy dtypes are mapped to DAPHNE value types by the table above.
order
: not supporteddevice
: not supportedlike
: not supported
Example
-
Numpy
-
DaphneLib
-
numpy.
full
(shape, fill_value, dtype=None, order='C', *, device=None, like=None)
Parameters
shape
: supported- only 2d shapes like
(m, n)
- currently bug in DaphneLib when at least one dimension is
0
- only 2d shapes like
fill_value
: supporteddtype
: supported- documentation coming later
order
: not supported- deliberately no direct control in DAPHNE
device
: not supported- deliberately no direct control in DAPHNE
like
: not supported
Example
-
Numpy
-
DaphneLib
-
numpy.
isnan
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isnan'>
Parameters
x
: supported- all other parameters: not supported
Example
-
Numpy
-
DaphneLib
-
numpy.
logical_not
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'logical_not'>
Will be directly supported in DaphneLib in the future.
Parameters
x
: supported- all other parameters: not supported
Example
-
We assume that
X
is of dtypenp.bool
. DaphneLib doesn't support the exchange of boolean arrays with Numpy yet. Thus, for the DaphneLib variant,X
must be changed to an integral dtype before the transfer to DaphneLib, e.g., byX = X.astype(np.int64)
; and the result must be changed back to boolean dtype, e.g., byY = Y.astype(np.bool)
. -
Numpy
-
DaphneLib
-
numpy.
random.choice
(a, size=None, replace=True, p=None)
Parameters
a
: supported- Note that all DAPHNE data objects are 2d.
size
: supported- Only 2d shapes are supported.
replace
: supportedp
: not supported- Could be achieved with workarounds. TODO
Example 1 (draw 1d array from arange without replacement)
-
Numpy
-
DaphneLib
-
Note that Numpy returns a 1d array, while DAPHNE returns a 5x1 column matrix.
Example 2 (draw 2d array from 1d array with replacement)
-
Numpy
-
DaphneLib
-
numpy.
random.permutation
(x)
Parameters
x
: supported- only 2d arrays/matrices are supported
Example
-
Numpy
-
DaphneLib
-
numpy.
repeat
(a, repeats, axis=None)
Parameters
a
: supportedrepeats
: supported- only scalars are supported
axis
: supported- only
None
,0
, and1
are supported
- only
Example 1 (repeat column matrix along row dimension)
-
Assuming
X
is a column (m x 1) matrix. -
Numpy
-
DaphneLib
Example 2 (repeat row matrix along row dimension)
-
Assuming
X
is a row (1 x n) matrix. -
Numpy
-
DaphneLib
-
numpy.
tile
(A, reps)
Parameters
A
: supported- Only 2d matrices are supported.
reps
: supported- Only scalars and 2x1 matrices are supported, i.e., repetition along row and/or column dimension.
Example
-
Numpy
-
DaphneLib