Q.ANT Native Computing Toolkit 2.4.0
All you need for photonic computing
Loading...
Searching...
No Matches
qant_native_computing_toolkit_native.h File Reference

Native math functionality. More...

#include <stdint.h>
#include <dlpack/dlpack.h>
#include "qant_native_computing_toolkit_generic.h"
Include dependency graph for qant_native_computing_toolkit_native.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

DLManagedTensorVersioned * mul_npu (const uint32_t npu_id, DLManagedTensorVersioned const *us, DLManagedTensorVersioned const *vs)
 Multiplies two 1D Tensors element-wise.
DLManagedTensorVersioned * calc_scaled_periodic_nl_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *weights)
 Calculates a scaled periodic nonlinearity pairwise for all elements of features $u$ and weights $v$.
DLManagedTensorVersioned * linear_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *weights)
 Performs a linear forward propagation between input features and a weight matrix.
DLManagedTensorVersioned * linear_fprop_preloaded_weights (DLManagedTensorVersioned const *features, QantMemoryHandle const *weights)
 Same as linear_fprop(), but uses a weight matrix previously uploaded.
int linear_fprop_preloaded (QantMemoryHandle const *features, QantMemoryHandle const *weights, QantMemoryHandle const *result)
 Same as linear_fprop(), but features, weights and result all live in device memory.

Detailed Description

Native math functionality.

Functionality that is available directly on the NPU with no data processing on the host.

Function Documentation

◆ calc_scaled_periodic_nl_fprop()

DLManagedTensorVersioned * calc_scaled_periodic_nl_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * weights )

Calculates a scaled periodic nonlinearity pairwise for all elements of features $u$ and weights $v$.

The output for each input pair is $w(u,v) = \mathrm{tcos}(u) \cdot v$, where $\mathrm{tcos}()$ is a $2\pi$ - periodic function with values between $-1$ and $1$, similar to a cosine.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 1D tensor with data type bfloat16.
weightsA 1D tensor with data type bfloat16, same length as features.
Returns
A 1D tensor containing the result, same shape as input features.

◆ linear_fprop()

DLManagedTensorVersioned * linear_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * weights )

Performs a linear forward propagation between input features and a weight matrix.

The operation computes output = features @ weights^T, i.e.:

\‍[  \mathrm{output}[i,\,j] = \sum_k \mathrm{features}[i,k]\cdot\mathrm{weights}[j,k]
\‍]

Note that this is not a plain features @ weights — the weight matrix is implicitly transposed, matching the convention where weights has shape (n_channels_out, n_channels_in).

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 2D tensor with data type bfloat16 and shape (n_batches, n_channels_in).
weightsA 2D tensor with data type bfloat16 and shape (n_channels_out, n_channels_in).
Returns
A 2D tensor containing the result of the forward propagation with shape (n_batches, n_channels_out).

◆ linear_fprop_preloaded()

int linear_fprop_preloaded ( QantMemoryHandle const * features,
QantMemoryHandle const * weights,
QantMemoryHandle const * result )

Same as linear_fprop(), but features, weights and result all live in device memory.

The features and weights must have been uploaded with npu_upload() or npu_upload_into() (data roles Features and Weights respectively). The result handle is not allocated by this function: it must be allocated beforehand, e.g. with npu_allocate(), on the same NPU and with data role Features. The operation is performed on the NPU the weights were uploaded to.

Parameters
featuresHandle to the features with shape (n_batches, n_channels_in).
weightsHandle to the weights with shape (n_channels_out, n_channels_in).
resultPre-allocated handle to receive the result with shape (n_batches, n_channels_out) and data role Features. Its previous contents are overwritten.
Returns
0 if successful, or a negative error code.

◆ linear_fprop_preloaded_weights()

DLManagedTensorVersioned * linear_fprop_preloaded_weights ( DLManagedTensorVersioned const * features,
QantMemoryHandle const * weights )

Same as linear_fprop(), but uses a weight matrix previously uploaded.

Use in combination with npu_upload() or npu_upload_into() (data role Weights). The operation is performed on the NPU the weights were uploaded to.

Parameters
featuresA 2D tensor with data type bfloat16 and shape (n_batches, n_channels_in).
weightsHandle to the weights with shape (n_channels_out, n_channels_in), as returned by npu_upload().
Returns
A 2D tensor containing the result of the forward propagation with shape (n_batches, n_channels_out).

◆ mul_npu()

DLManagedTensorVersioned * mul_npu ( const uint32_t npu_id,
DLManagedTensorVersioned const * us,
DLManagedTensorVersioned const * vs )

Multiplies two 1D Tensors element-wise.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
usThe first input tensor. Must be data type bfloat16, same length as vs.
vsThe second input tensor. Must be data type bfloat16, same length as us.
Returns
A 1D tensor containing the elementwise product, same length as input.