Q.ANT Native Computing Toolkit 2.4.0
All you need for photonic computing
Loading...
Searching...
No Matches
qant_native_computing_toolkit_ai.h File Reference
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <dlpack/dlpack.h>
Include dependency graph for qant_native_computing_toolkit_ai.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct ConvWeightsHandle ConvWeightsHandle
 Opaque handle to convolution weights that have been uploaded to device memory.
typedef struct ConvTransposeWeightsHandle ConvTransposeWeightsHandle
 Opaque handle to transposed convolution weights that have been uploaded to device memory.

Functions

DLManagedTensorVersioned * add_bias_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *bias)
 Element-wise addition. Supports batched input.
DLManagedTensorVersioned * add_bias_for_conv2d_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *bias)
 Element-wise addition. Specifically used for conv2d.
DLManagedTensorVersioned * relu_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features)
 Performs a forward pass through a ReLU layer.
DLManagedTensorVersioned * sigmoid_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features)
 Performs a forward pass through a Sigmoid layer.
DLManagedTensorVersioned * softmax_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features)
 Performs a forward pass through a Softmax layer.
DLManagedTensorVersioned * conv_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *weights, size_t const padding, size_t const stride, size_t const dilation)
 Performs a forward pass through a convolution layer.
DLManagedTensorVersioned * conv_transpose_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *weights, size_t const padding, size_t const stride, size_t const dilation, size_t const output_padding)
 Performs a forward pass through a transposed convolution layer.
ConvWeightsHandle * upload_conv_weights (const uint32_t npu_id, DLManagedTensorVersioned const *weights)
 Uploads convolution weights to device memory so they can be reused across multiple calls to conv_fprop_preloaded_weights().
int conv_weights_free (ConvWeightsHandle *handle)
 Frees convolution weights previously uploaded with upload_conv_weights().
DLManagedTensorVersioned * conv_fprop_preloaded_weights (DLManagedTensorVersioned const *features, ConvWeightsHandle const *weights, size_t const padding, size_t const stride, size_t const dilation)
 Performs a forward pass through a convolution layer using weights already uploaded to device memory. The computation runs on the NPU that holds the weights.
ConvTransposeWeightsHandle * upload_conv_transpose_weights (const uint32_t npu_id, DLManagedTensorVersioned const *weights)
 Uploads transposed convolution weights to device memory so they can be reused across multiple calls to conv_transpose_fprop_preloaded_weights().
int conv_transpose_weights_free (ConvTransposeWeightsHandle *handle)
 Frees transposed convolution weights previously uploaded with upload_conv_transpose_weights().
DLManagedTensorVersioned * conv_transpose_fprop_preloaded_weights (DLManagedTensorVersioned const *features, ConvTransposeWeightsHandle const *weights, size_t const padding, size_t const stride, size_t const dilation, size_t const output_padding)
 Performs a forward pass through a transposed convolution layer using weights already uploaded to device memory. The computation runs on the NPU that holds the weights.
DLManagedTensorVersioned * batchnorm2d_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *means, DLManagedTensorVersioned const *variances, DLManagedTensorVersioned const *weights, DLManagedTensorVersioned const *bias, float const eps)
 Performs a forward pass through a batchnorm2d layer.
DLManagedTensorVersioned * maxpool2d_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, size_t const kernel_height, size_t const kernel_width, size_t const padding, size_t const stride)
 Performs a forward pass through a maxpooling2d layer.
DLManagedTensorVersioned * avgpool2d_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, size_t const kernel_height, size_t const kernel_width, size_t const padding, size_t const stride, bool const count_include_pad)
 Performs a forward pass through a avgpooling2d layer.
DLManagedTensorVersioned * adaptive_maxpool2d_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, size_t const output_height, size_t const output_width)
 Performs a forward pass through an adaptive maxpooling2d layer.
DLManagedTensorVersioned * adaptive_avgpool2d_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, size_t const output_height, size_t const output_width)
 Performs a forward pass through an adaptive avgpooling2d layer.
DLManagedTensorVersioned * calc_kan_layer_fprop (const uint32_t npu_id, DLManagedTensorVersioned const *features, DLManagedTensorVersioned const *phis, DLManagedTensorVersioned const *ampls, DLManagedTensorVersioned const *ks)
 Calculates a Q.ANT version of a KAN layer (https://arxiv.org/abs/2404.19756) based on scaled_periodic_nl.

Typedef Documentation

◆ ConvTransposeWeightsHandle

Opaque handle to transposed convolution weights that have been uploaded to device memory.

Instances are created by upload_conv_transpose_weights() and must be released exactly once with conv_transpose_weights_free(). Using a handle after it has been freed is undefined behavior.

◆ ConvWeightsHandle

Opaque handle to convolution weights that have been uploaded to device memory.

Instances are created by upload_conv_weights() and must be released exactly once with conv_weights_free(). Using a handle after it has been freed is undefined behavior.

Function Documentation

◆ adaptive_avgpool2d_fprop()

DLManagedTensorVersioned * adaptive_avgpool2d_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
size_t const output_height,
size_t const output_width )

Performs a forward pass through an adaptive avgpooling2d layer.

Only works for symmetric input and output sizes. Input size has to be integer multiple of output size.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, n_channels, height, width)
output_heightHeight of the output.
output_widthWidth of the output.
Returns
A 4D tensor with shape (n_batches, n_channels, output_height, output_width)

◆ adaptive_maxpool2d_fprop()

DLManagedTensorVersioned * adaptive_maxpool2d_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
size_t const output_height,
size_t const output_width )

Performs a forward pass through an adaptive maxpooling2d layer.

Only works for symmetric input and output sizes. Input size has to be integer multiple of output size.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, n_channels, height, width)
output_heightHeight of the output.
output_widthWidth of the output.
Returns
A 4D tensor with shape (n_batches, n_channels, output_height, output_width)

◆ add_bias_for_conv2d_fprop()

DLManagedTensorVersioned * add_bias_for_conv2d_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * bias )

Element-wise addition. Specifically used for conv2d.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, n_channels, height, width)
biasA 1D tensor with data type bfloat16 and shape (n_channels)
Returns
A 4D tensor containing the result of the addition, same shape as the input features.

◆ add_bias_fprop()

DLManagedTensorVersioned * add_bias_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * bias )

Element-wise addition. Supports batched input.

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).
biasA 1D tensor with data type bfloat16 and shape (n_channels)
Returns
A 2D tensor, same shape as inputs.

◆ avgpool2d_fprop()

DLManagedTensorVersioned * avgpool2d_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
size_t const kernel_height,
size_t const kernel_width,
size_t const padding,
size_t const stride,
bool const count_include_pad )

Performs a forward pass through a avgpooling2d layer.

For a mathematical definition, see https://en.wikipedia.org/wiki/Pooling_layer

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, n_channels, height, width)
kernel_heightHeight of the pooling kernel.
kernel_widthWidth of the pooling kernel.
paddingAmount of padding added to the input features.
strideStep size for moving the kernel over the input features.
count_include_padWhen True, will include the zero-padding in the averaging calculation.
Returns
A 4D tensor with shape (n_batches, n_channels, height, width)

◆ batchnorm2d_fprop()

DLManagedTensorVersioned * batchnorm2d_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * means,
DLManagedTensorVersioned const * variances,
DLManagedTensorVersioned const * weights,
DLManagedTensorVersioned const * bias,
float const eps )

Performs a forward pass through a batchnorm2d layer.

For a mathematical definition, see https://en.wikipedia.org/wiki/Batch_normalization

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, n_channels, height, width)
meansA 1D tensor containing the mean values for each feature, with data type bfloat16 and length n_channels
variancesA 1D tensor containing the variance values for each feature, with data type bfloat16 and length n_channels
weightsA 1D tensor containing the parameter for each feature, with data type bfloat16 and length n_channels
biasA 1D tensor containing the bias value for each feature, with data type bfloat16 and length n_channels
epsA small value added to the variance for numerical stability.
Returns
A 4D tensor containing the result of the batchnorm operation with shape (n_batches, n_channels, height, width)

◆ calc_kan_layer_fprop()

DLManagedTensorVersioned * calc_kan_layer_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * phis,
DLManagedTensorVersioned const * ampls,
DLManagedTensorVersioned const * ks )

Calculates a Q.ANT version of a KAN layer (https://arxiv.org/abs/2404.19756) based on scaled_periodic_nl.

The mathematical function is

\‍[    y_j = \sum_{i,l} \mathrm{tcos}\!\left(ks_l x_i + \phi_{jil}\right) ampls_{jil},
\‍]

where $x_i$ is the input (vector), $ks_l$ is the frequency components, $\phi_{jil}$ the phase offsets (tensor) and $ampls_{jil}$ the amplitude (tensor) and $y_j$ output (vector). $\mathrm{tcos}$ denotes the cosine-related shape of the periodic optical nonlinearity.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 2D input tensor with shape (n_batches, n_channels_in), with data type bfloat16.
phisA 3D tensor of phase offsets, shape (n_channels_out, n_channels_in, n_ks), with data type bfloat16.
amplsA 3D array of amplitudes, same shape as phis, with data type bfloat16.
ksA 1D tensor of frequency components, shape (n_ks), with data type bfloat16.
Returns
The result of the KAN layer with shape (n_batches, n_channels_out), with data type bfloat16.

◆ conv_fprop()

DLManagedTensorVersioned * conv_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * weights,
size_t const padding,
size_t const stride,
size_t const dilation )

Performs a forward pass through a convolution layer.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, in_channels, height, width)
weightsA 4D tensor with data type bfloat16 and shape (out_channels, in_channels, height, width)
paddingAmount of padding added to the input features.
strideStep size for moving the kernel over the input features.
dilationDilation ("zoom out") of the kernel.
Returns
A 4D tensor containing the result of the convolution with shape (n_batches, out_channels, height_out, width_out)

◆ conv_fprop_preloaded_weights()

DLManagedTensorVersioned * conv_fprop_preloaded_weights ( DLManagedTensorVersioned const * features,
ConvWeightsHandle const * weights,
size_t const padding,
size_t const stride,
size_t const dilation )

Performs a forward pass through a convolution layer using weights already uploaded to device memory. The computation runs on the NPU that holds the weights.

Parameters
featuresA 4D tensor with data type bfloat16 and shape (n_batches, in_channels, height, width)
weightsHandle to the weights, as returned by upload_conv_weights().
paddingAmount of padding added to the input features.
strideStep size for moving the kernel over the input features.
dilationDilation ("zoom out") of the kernel.
Returns
A 4D tensor containing the result of the convolution with shape (n_batches, out_channels, height_out, width_out)

◆ conv_transpose_fprop()

DLManagedTensorVersioned * conv_transpose_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
DLManagedTensorVersioned const * weights,
size_t const padding,
size_t const stride,
size_t const dilation,
size_t const output_padding )

Performs a forward pass through a transposed convolution layer.

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, in_channels, height, width)
weightsA 4D tensor with data type bfloat16 and shape (in_channels, out_channels, height, width)
paddingAmount of padding added to the input features.
strideStep size for moving the kernel over the input features.
dilationDilation ("zoom out") of the kernel.
output_paddingPadding for the returned features.
Returns
A 4D tensor containing the result of the transpose convolution with shape (n_batches, out_channels, height_out, width_out)

◆ conv_transpose_fprop_preloaded_weights()

DLManagedTensorVersioned * conv_transpose_fprop_preloaded_weights ( DLManagedTensorVersioned const * features,
ConvTransposeWeightsHandle const * weights,
size_t const padding,
size_t const stride,
size_t const dilation,
size_t const output_padding )

Performs a forward pass through a transposed convolution layer using weights already uploaded to device memory. The computation runs on the NPU that holds the weights.

Parameters
featuresA 4D tensor with data type bfloat16 and shape (n_batches, in_channels, height, width)
weightsHandle to the weights, as returned by upload_conv_transpose_weights().
paddingAmount of padding added to the input features.
strideStep size for moving the kernel over the input features.
dilationDilation ("zoom out") of the kernel.
output_paddingPadding for the returned features.
Returns
A 4D tensor containing the result of the transpose convolution with shape (n_batches, out_channels, height_out, width_out)

◆ conv_transpose_weights_free()

int conv_transpose_weights_free ( ConvTransposeWeightsHandle * handle)

Frees transposed convolution weights previously uploaded with upload_conv_transpose_weights().

Parameters
handleThe handle to free. Must not be used afterwards.
Returns
0 if successful, or a negative error code.

◆ conv_weights_free()

int conv_weights_free ( ConvWeightsHandle * handle)

Frees convolution weights previously uploaded with upload_conv_weights().

Parameters
handleThe handle to free. Must not be used afterwards.
Returns
0 if successful, or a negative error code.

◆ maxpool2d_fprop()

DLManagedTensorVersioned * maxpool2d_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features,
size_t const kernel_height,
size_t const kernel_width,
size_t const padding,
size_t const stride )

Performs a forward pass through a maxpooling2d layer.

For a mathematical definition, see https://en.wikipedia.org/wiki/Pooling_layer

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 4D tensor with data type bfloat16 and shape (n_batches, n_channels, height, width)
kernel_heightHeight of the pooling kernel.
kernel_widthWidth of the pooling kernel.
paddingAmount of padding added to the input features.
strideStep size for moving the kernel over the input features.
Returns
A 4D tensor containing the result of the maxpool operation with shape ([batches], n_channels, output_height, output_width).

◆ relu_fprop()

DLManagedTensorVersioned * relu_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features )

Performs a forward pass through a ReLU layer.

For a mathematical definition, see https://en.wikipedia.org/wiki/Rectifier_(neural_networks)

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 1D tensor with data type bfloat16.
Returns
A 1D array containing the result of the ReLU operation.

◆ sigmoid_fprop()

DLManagedTensorVersioned * sigmoid_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features )

Performs a forward pass through a Sigmoid layer.

For a mathematical definition, see https://en.wikipedia.org/wiki/Sigmoid_function

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

◆ softmax_fprop()

DLManagedTensorVersioned * softmax_fprop ( const uint32_t npu_id,
DLManagedTensorVersioned const * features )

Performs a forward pass through a Softmax layer.

For a mathematical definition, see https://en.wikipedia.org/wiki/Softmax_function

Parameters
npu_idThe identifier of the NPU on which to perform the operation.
featuresA 2D tensor with data type bfloat16 and shape (1, N).
Returns
A 2D tensor, containing the result of the Softmax operation with the same shape as the input.

◆ upload_conv_transpose_weights()

ConvTransposeWeightsHandle * upload_conv_transpose_weights ( const uint32_t npu_id,
DLManagedTensorVersioned const * weights )

Uploads transposed convolution weights to device memory so they can be reused across multiple calls to conv_transpose_fprop_preloaded_weights().

Parameters
npu_idThe identifier of the NPU on which the weights are stored.
weightsA 4D tensor with data type bfloat16 and shape (in_channels, out_channels, height, width)
Returns
An owning pointer to the weights handle, or NULL on error. The caller must release it with conv_transpose_weights_free().

◆ upload_conv_weights()

ConvWeightsHandle * upload_conv_weights ( const uint32_t npu_id,
DLManagedTensorVersioned const * weights )

Uploads convolution weights to device memory so they can be reused across multiple calls to conv_fprop_preloaded_weights().

Parameters
npu_idThe identifier of the NPU on which the weights are stored.
weightsA 4D tensor with data type bfloat16 and shape (out_channels, in_channels, height, width)
Returns
An owning pointer to the weights handle, or NULL on error. The caller must release it with conv_weights_free().