zDNN API Reference
April 17, 2025 ยท View on GitHub
Contacts
- Nicholas Marion (nmarion@us.ibm.com)
- Andreas Krebbel (krebbel@linux.ibm.com)
- Steven Jones (sbj@us.ibm.com)
Version
1.2.0
Table of Contents
Overview
Deep Learning Library - the deep learning library support (zDNN) is the SW enablement technology provided by IBM to meet the following requirements:
- Specialized-function-assist instructions are intended to provide performance improvements for specific operations used in software libraries, utilities, and operating system (OS) services. The facilities and instructions described as specialized-function-assist instructions may be replaced or removed in the future. As such, the IBM recommendation for these instructions is that a software library or operating system function be used instead of directly accessing the instructions. This is the function provided by zDNN.
- zAIU has very complex data layout requirements; these requirements arrange the tensor to enhance the performance characteristics of the operations. zDNN will format the tensor appropriately on behalf of the caller, and it will do so using an optimized approach.
- For deep learning operations, zAIU requires the use of internal data types:
- DLFLOAT16, a 2-byte data type supported in Telum I, which optimizes training and inference while minimizing the loss of accuracy at inference time (versus standard 4-byte formats),
- INT8, a 1-byte data type supported with Telum II, which allows tensor quantization features.
The zDNN library provides a set of APIs that an exploiter will utilize to drive the desired request. zDNN will be available on both z/OS and Linux on Z; the inclusion of Linux on Z provides particular benefit, as it will allow us to enable acceleration in frameworks for z/OS via z/OS Container Extensions (zCX).
Environment
z/OS:
- Problem state
- AMODE64
- XPLINK
Alignment requirements
zAIU Op Limits
This implies a zDNN limitation as well at this point.
-
For all ops:
- Number of elements in any dimension must not exceed the value returned by
zdnn_get_max_for_dim(uint8_t dimension) - Total number of bytes required for storing a transformed tensor must not
exceed the value returned by
zdnn_get_nnpa_max_tensor_size()
- Number of elements in any dimension must not exceed the value returned by
Application interfaces for zAIU Enterprise Neural Network Inference
zDNN General
The zDNN deep learning library provides the standard IBM Z software interface to the zAIU. This IBM-provided C library provides a set of functions that handle the data transformation requirements of the zAIU and provide wrapper functions for the NNPA instruction primitives.
The zDNN functions use the following criteria to determine if zAIU can be used to accelerate a deep learning primitive:
- Neural Network Processing Assist (NNPA) facility indicator in the system STFLE output.
- Output of the NNPA-QAF (Query Available Functions) request.
Using zDNN
To use the IBM-provided zDNN C library for the NNPA instruction, follow these steps:
- Link or re-link applications to use the IBM-provided zDNN. The IBM-provided zDNN is a library file in the z/OS UNIX System Services file system and can be statically or dynamically linked into your applications. The paths for the zDNN archive file and the zDNN header files are:
z/OS (LE required): Path for 64-bit dynamic library files:
/lib/libzdnn.so/lib/libzdnn.x
Path for the zDNN header files:
/usr/include/
The XL C/C++ compiler and the z/OS Language Environment provide various environment variables to control processing, in addition to the variables provided by the zDNN library itself.
-
Use the environment variable
_CEE_RUNOPTSto specify invocation Language Environment runtime options. For more information about using the environment variable_CEE_RUNOPTSand other C and LE variables, see z/OS XL C/C++ Programming Guide. -
For environment variables accepted by the zDNN library, see Runtime Environment Variables.
Linux on Z:
On Linux on Z we expect to ship source as well a package-installable library and header. The library installation will conform to the standards of the packaging method chosen.
Building and Installing zDNN
Clone the Repository and Submodules
git clone --recurse-submodules git@github.com:IBM/zDNN.git
Create configure script
To create configure script
autoreconf .
Configure Build
Prepare the build and install environment and check for necessary dependencies
using ./configure script.
./configure [OPTION]... [VAR=VALUE]...
Installation Options
--prefix=PREFIX- Install architecture-independent files in PREFIX. Default location is
/usr/local
- Install architecture-independent files in PREFIX. Default location is
--exec-prefix=EPREFIX- Install architecture-independent files in EPREFIX. Default location is
PREFIX
- Install architecture-independent files in EPREFIX. Default location is
To explore all available configuration options and features, use -h
Build Library
Compile zDNN library using:
make build
Run Tests
To run tests:
make test
Unity Requirement
Please note that the Unity test framework source code is required to run unit
tests. If you did not clone submodules along with initial zDNN clone, please
perform the following steps to setup Unity prior to issuing make tests:
- Clone the source code from the Throw The Switch - Unity repository.
- Set the
UNITY_ROOTenvironment variable to the folder containing the Unity source code.
Python Package Requirements
Please note that junit_xml and pyparsing are required python packages in
order to properly parse and format Unity test results. Follow standard python
package installation practices to meet requirements.
Install
Install zDNN library:
sudo make install
Reference Commands
Configure help:
./configure -h
Make help:
make help
Prerequisite Tools
Compilers:
GCC: GNU Compiler Collection (gcc)
or
IBM XL C/C++: (xlc)
Build Tools and Dependencies:
AutoconfMakeUnityPython PackagesFor formatting test results- junit_xml
- pyparsing
Common Types and Structs
Include Files: zdnn.h
Version Information
#define ZDNN_VERSION "1.2.0"
#define ZDNN_VERNUM 0x010200 // 0x[major][minor][patch]
#define ZDNN_VER_MAJOR 1
#define ZDNN_VER_MINOR 2
#define ZDNN_VER_PATCH 0
- zDNN major version (ZDNN_VER_MAJOR) will be incremented if any backwards incompatible changes are introduced to the API. It may also include minor and patch level changes. Patch and minor version will be reset to 0 when major version is incremented.
- zDNN minor version (ZDNN_VER_MINOR) will be incremented if new, backwards compatible functionalities are introduced to the API or if any API functionalities are marked as deprecated. It may also include patch level changes. Patch version will be reset to 0 when minor version is incremented.
- zDNN patch version (ZDNN_VER_PATCH) will be incremented if only backwards compatible bug fixes are introduced. A bug fix being defined as an internal change that fixes incorrect behavior.
Functions for checking version incompatibility with the zDNN load library are provided and described in the Support Functions section.
zDNN zTensor
typedef struct zdnn_ztensor {
zdnn_tensor_desc
*pre_transformed_desc; // tensor's shape information before transformation
zdnn_tensor_desc *transformed_desc; // transformed tensor's shape information
uint64_t buffer_size; // tensor size in bytes
void *buffer; // pointer to the tensor in memory
bool is_transformed; // indicator if data in buffer has been transformed
char reserved[3]; // not currently used, should contain zeros.
float rec_scale; // the scale factor for quantization, stored as reciprocal
float offset; // the offset for quantization
char reserved2[20]; // not currently used, should contain zeros.
} zdnn_ztensor;
General zTensor Requirements
bufferrequirements:- Calling zdnn_init_ztensor_with_malloc
automatically allocates and sets a valid
bufferfor a tensor. bufferfield must point to storage allocated of sufficient size to contain the transformed tensor data described by the itstransformed_descfield.- Calling zdnn_getsize_ztensor with the tensor's
transformed_descreturns the required size.
- Calling zdnn_getsize_ztensor with the tensor's
- Start of
bufferfield must be 4k aligned.
- Calling zdnn_init_ztensor_with_malloc
automatically allocates and sets a valid
reservedshould contain zeros, otherwise the program may not operate compatibly in the future.- Calling zdnn_init_ztensor or
zdnn_init_ztensor_with_malloc will set
reservedto zeros.
- Calling zdnn_init_ztensor or
zdnn_init_ztensor_with_malloc will set
Concatenated zTensor Requirements
- For use with weights/biases/hidden-weights/hidden-biases RNN-gates tensors.
- You must use
zdnn_generate_transformed_desc_concatenated
with the appropriate concatenation info
- Do not use
zdnn_generate_transformed_descwith concatenated tensors
- Do not use
- The pre-transformed shape dimensions should not include the concatenation.
- Thus, the pre-transformed shape should be that of a single gate, not the shape of the combined gates
- Afterward transform with zdnn_transform_ztensor as normal
- Must follow general tensor requirements
Quantized zTensor Requirements
- Supported
transform_descandpre_transformed_desctypes for zdnn_transform_quantized_ztensor and zdnn_generate_quantized_transformed_desc:ZDNN_FORMAT_4DFEATUREformat:- ZDNN_DLFLOAT16
- FP16, FP32, BFLOAT
- ZDNN_BINARY_INT8
- INT8, FP16, FP32, BFLOAT
- ZDNN_DLFLOAT16
ZDNN_FORMAT_4DWEIGHTSformat:- ZDNN_BINARY_INT8
- INT8
- ZDNN_BINARY_INT8
zDNN Tensor Descriptors
typedef struct zdnn_tensor_desc {
zdnn_data_layouts layout; // data layout
zdnn_data_formats format; // internal use only
zdnn_data_types type; // data type
uint32_t dim4; // number of elements in outermost dimension
uint32_t dim3; // ... outer dimension
uint32_t dim2; // ... inner dimension
uint32_t dim1; // number of elements in innermost dimension
} zdnn_tensor_desc;
Programming Notes
- Helper methods zdnn_init_pre_transformed_desc and zdnn_generate_transformed_desc or zdnn_generate_transformed_desc_concatenated will set the correct dims based on the layout and format.
- The layout of the tensor descriptor affects the expected
order of the dims. For example:
- For tensors with less than 4 dimensions, unspecified dims:
- In the pre_transformed_desc are ignored. For example a ZDNN_3D expects values in dim4, dim3, and dim2.
- In the transformed_desc "unused" dims must be 1.
- A ZDNN_NHWC expects dims such that dim4 = N, dim3 = H, dim2 = W, dim1 = C
- A ZDNN_NCHW expects dims such that dim4 = N, dim3 = C, dim2 = H, dim1 = W
- A ZDNN_HWCK expects dims such that dim4 = H, dim3 = W, dim2 = C, dim1 = K
- For tensors with less than 4 dimensions, unspecified dims:
- The format changes the expected dims order for
ZDNN_4D tensors layouts
- ZDNN_FORMAT_4DFEATURE expects dims such that dim4 = N, dim3 = H, dim2 = W, dim1 = C
- ZDNN_FORMAT_4DKERNEL expects dims such that dim4 = H, dim3 = W, dim2 = C, dim1 = K
zDNN Data Layouts
The following are layouts for zDNN ztensor descriptors. These indicate the number and order of dimensions to expect for the ztensor data.
typedef enum zdnn_data_layouts {
ZDNN_1D, // 1d tensor
ZDNN_2D, // 2d tensor
ZDNN_2DS, // represents special 2D tensors required by LSTM/GRU
ZDNN_3D, // 3d tensor
ZDNN_3DS, // represents special 3D tensors required by
// LSTM/GRU/Softmax/Matmul
ZDNN_ZRH, // represents (update, reset, hidden) used by GRU
ZDNN_4D, // 4d tensor
ZDNN_4DS, // represents special 4D tensors required by LSTM/GRU output
ZDNN_NHWC, // 4d feature tensor in NHWC
ZDNN_NCHW, // 4d feature tensor in NCHW
ZDNN_FICO, // represents (forget, input, cell, output) used by LSTM
ZDNN_HWCK, // 4d kernel CNN tensor
ZDNN_BIDIR_ZRH, // ZRH variant to work with bidirectional LSTM/GRU output
ZDNN_BIDIR_FICO // FICO variant to work with bidirectional LSTM/GRU output
} zdnn_data_layouts;
Some layouts also indicate special re-arrangement of the data during ztensor transformation.
ZDNN_2DS- The outermost dimension of the original shape is promoted to dim4 during transformation. For example, a shape of (a, b) becomes [a, 1, 1, b] (dim4, dim3, dim2, dim1) in thetransformed_descZDNN_3DS- The outermost dimension of the original shape is promoted to dim4 during transformation. For example, a shape of (a, b, c) becomes [a, 1, b, c] (dim4, dim3, dim2, dim1) in thetransformed_descZDNN_4DS- Arrangement for RNN output tensor
The followings are set automatically in transformed_desc based on info when
calling zdnn_generate_transformed_desc_concatenated():
ZDNN_ZRH/FICO- During transformation, the RNN input gates data are concatenated on the innermost dimension. Supported withpre_transformed_layoutofZDNN_2DSorZDNN_3DS.ZDNN_BIDIR_ZRH/FICO- Similar toZDNN_ZRH/FICO, used when:- transforming RNN input weight gate data, and
- the input tensor for the current RNN layer is a bidirectional RNN output from a previous RNN layer
zDNN Data Formats
typedef enum zdnn_data_formats {
ZDNN_FORMAT_4DFEATURE, // tensor in zAIU data layout format 0
ZDNN_FORMAT_4DKERNEL, // tensor in zAIU data layout format 1
ZDNN_FORMAT_4DWEIGHTS, // tensor in zAIU data layout format 2
ZDNN_FORMAT_4DGENERIC, // tensor in zAIU data layout format 31
} zdnn_data_formats;
zDNN Data Types
typedef enum zdnn_data_types {
ZDNN_DLFLOAT16, // 16-bit deep learning format
ZDNN_BINARY_FP32, // 32-bit binary-floating-point format
ZDNN_BINARY_INT8, // 8-bit signed or unsighed binary integer
ZDNN_BINARY_INT32, // 32-bit signed or unsigned binary integer
INT8, // 8-bit signed or unsigned binary integer format
INT32, // 32-bit signed or unsigned binary integer format
BFLOAT, // Brain floating point format
FP16, // 16-bit IEEE-754 floating point format
FP32, // 32-bit IEEE-754 floating point format
} zdnn_data_types;
zDNN Quantized Transform Types
typedef enum zdnn_quantized_transform_types {
QUANTIZED_DLFLOAT16 = 0, // quantized dlfloat16
QUANTIZED_INT8 = 1, // quantized int8
QUANTIZED_WEIGHTS_INT8 = 2 // quantized weights
} zdnn_quantized_transform_types;
zDNN Statuses
| Mnemonic Constant | Value | Meaning |
|---|---|---|
| ZDNN_OK | 0x00000000 | Success. |
Warning Statuses
| Mnemonic Constant | Value | Meaning |
|---|---|---|
| ZDNN_ELEMENT_RANGE_VIOLATION | 0x00020001 | zAIU operation resulted in data that was out of the normal range. |
Note: ZDNN_ELEMENT_RANGE_VIOLATION indicates a range violation occurred for the zAIU operation based on the data in the tensors. This usually indicates an overflow of an NNPA internal data type, but can also be associated with operation specific errors, such as "divide by zero". See the "z/Architecture Principles of Operation" for information about range violation on the operation that encountered the violation.
General Failing Statuses
| Mnemonic Constant | Value | Meaning |
|---|---|---|
| ZDNN_INVALID_SHAPE* | 0x00040001 | Invalid shape information in one (or more) of the input/output tensor(s). |
| ZDNN_INVALID_LAYOUT | 0x00040002 | Invalid layout information in one (or more) of the input/output tensor(s). |
| ZDNN_INVALID_TYPE* | 0x00040003 | Invalid type information in one (or more) of the input/output tensor(s). |
| ZDNN_INVALID_FORMAT* | 0x00040004 | Invalid format information in one (or more) of the input/output tensor(s). |
| ZDNN_INVALID_DIRECTION | 0x00040005 | Invalid RNN direction. |
| ZDNN_INVALID_CONCAT_INFO | 0x00040006 | Invalid concatenation info. |
| ZDNN_INVALID_STRIDE_PADDING* | 0x00040007 | Invalid padding type parameter for current strides. |
| ZDNN_INVALID_STRIDES* | 0x00040008 | Invalid stride height or width parameter. |
| ZDNN_MISALIGNED_PARMBLOCK* | 0x00040009 | NNPA parameter block is not on double word boundary. |
| ZDNN_INVALID_CLIPPING_VALUE | 0x0004000A | Invalid clipping for the specified operation. |
| ZDNN_INVALID_ADJUSTMENT_FACTOR | 0x0004000B | Invalid adjustment for the specified operation. |
| ZDNN_INVALID_EPSILON | 0x0004000C | Invalid epsilon for the specified operation. |
| ZDNN_INVALID_TRANSFORM_TYPE | 0x0004000D | Invalid transformation type. |
| ZDNN_INVALID_BETA | 0x0004000E | Invalid beta value for the specified operation. |
| ZDNN_INVALID_GAMMA | 0x0004000F | Invalid gamma value for the specified operation. |
| ZDNN_INVALID_BESSEL_CORRECTION | 0x00040010 | Invalid bessel correction value for the specified operation. |
| ZDNN_INVALID_SCALE | 0x00040011 | Invalid scale value for the specified operation. |
| ZDNN_INVALID_OFFSET | 0x00040012 | Invalid offset value for the specified operation. |
| ZDNN_ALLOCATION_FAILURE | 0x00100001 | Can not allocate storage. |
| ZDNN_INVALID_BUFFER | 0x00100002 | Buffer address is NULL or not on 4K-byte boundary or insufficient buffer size. |
| ZDNN_CONVERT_FAILURE | 0x00100003 | Floating point data conversion failure. |
| ZDNN_INVALID_STATE | 0x00100004 | Invalid zTensor state. |
| ZDNN_UNSUPPORTED_AIU_EXCEPTION | 0x00100005 | zAIU operation returned an unexpected exception. |
Note: *In certain scenarios, these statuses are returned only if ZDNN_ENABLE_PRECHECK is enabled. When not enabled, these scenarios will lead to abnormal program termination.
Hardware Statuses
The following statuses indicate issues returned from the hardware.
| Mnemonic Constant | Value | Meaning |
|---|---|---|
| ZDNN_UNSUPPORTED_PARMBLOCK | 0x000C0001 | NNPA parameter block format is not supported by the model. |
| ZDNN_UNAVAILABLE_FUNCTION | 0x000C0002 | Specified NNPA function is not defined or installed on the machine. |
| ZDNN_UNSUPPORTED_FORMAT | 0x000C0010 | Specified tensor data layout format is not supported. |
| ZDNN_UNSUPPORTED_TYPE | 0x000C0011 | Specified tensor data type is not supported. |
| ZDNN_EXCEEDS_MDIS | 0x000C0012 | Tensor dimension exceeds maximum dimension index size (MDIS). |
| ZDNN_EXCEEDS_MTS | 0x000C0013 | Total number of bytes in tensor exceeds maximum tensor size. (MTS). |
| ZDNN_MISALIGNED_TENSOR | 0x000C0014 | Tensor address is not on 4K-byte boundary. |
| ZDNN_MISALIGNED_SAVEAREA | 0x000C0015 | Function specific save area address is not on 4K-byte boundary. |
The meaning of the following hardware statuses vary based on operation. See the operation that returned the status for the specific meaning.
| Mnemonic Constant | Value | Meaning |
|---|---|---|
| ZDNN_FUNC_RC_F000 | 0x000CF000 | Function specific response code (F000). |
| ZDNN_FUNC_RC_F001 | 0x000CF001 | Function specific response code (F001). |
| ZDNN_FUNC_RC_F002 | 0x000CF002 | Function specific response code (F002). |
| ZDNN_FUNC_RC_F003 | 0x000CF003 | Function specific response code (F003). |
| ZDNN_FUNC_RC_F004 | 0x000CF004 | Function specific response code (F004). |
| ZDNN_FUNC_RC_F005 | 0x000CF005 | Function specific response code (F005). |
| ZDNN_FUNC_RC_F006 | 0x000CF006 | Function specific response code (F006). |
| ZDNN_FUNC_RC_F007 | 0x000CF007 | Function specific response code (F007). |
| ZDNN_FUNC_RC_F008 | 0x000CF008 | Function specific response code (F008). |
| ZDNN_FUNC_RC_F009 | 0x000CF009 | Function specific response code (F009). |
Runtime Environment Variables
ZDNN_ENABLE_PRECHECK: true/false- If set to
true, tensor integrity prechecks are run before issuing NNPA operations. - Enabling precheck may impact performance.
- Enable to debug issues which cause hardware exceptions that otherwise would result in abnormal program termination.
- If set to
ZDNN_STATUS_DIAG: nnnnnnnn (decimal) or 0xnnnnnnnn (hexadecimal)- Prints or produces diagnostic information whenever zDNN status code is equal to the specified value. Only one status value can be specified.
The following are only available when the zDNN library was built with
ZDNN_CONFIG_DEBUG enabled.
ZDNN_LOGLEVEL: off/fatal/error/warn/info/debug/trace- Sets logging facility's output level
ZDNN_LOGMODULE: module name(s)- Produces log output only when the issuer's module name is in the list. You may specify multiple module names by separating them with either commas or spaces.
Programming Notes
- Environment variables settings are checked during initial library load by zdnn_init.
- To change environment variable settings afterward, zdnn_init must be called again manually.
Validating the environment at runtime
Programming Notes
- Most API calls require a minimum zDNN library and hardware for the API to
function. There are three zDNN APIs for validation of the zDNN runtime
environment:
- Validating the zDNN Library version:
- This is the version of the libzdnn package installed on the host or embedded in the runtime application.
- The zDNN library version is independent of the hardware available on the current system.
- zDNN APIs introduced in newer versions of the zDNN library will not exist in older versions of the library. Attempting to call them will result in application crashes.
- The zDNN library version is returned by zdnn_get_library_version.
- Validating the zDNN API version:
- This is the version of zDNN APIs that are compatible on the current system and is separate of the zDNN library version.
- Calling zDNN APIs while running on a system which does not support that zDNN API version will return a hardware status instead of ZDNN_OK.
- The zDNN API version available is returned by zdnn_get_max_runnable_version and is reflected in the return value of zdnn_is_version_runnable.
- zDNN API 1.0.x indicates the API requires Telum I or greater.
- zDNN API 1.1.x indicates the API requires Telum II or greater.
- Validating NNPA availability:
- This indicates if the current system has zAIU hardware present and enabled.
- It is possible to be on a system with zAIU hardware but the feature is unavailable, such as z/VM when there is a mix of hardware levels.
- This is returned by zdnn_is_nnpa_installed
- Validating the zDNN Library version:
- Examples:
- Given a Telum I system with zDNN 1.1.0 installed:
- zdnn_get_library_version will return
0x00010100indicating zDNN library 1.1.0 is installed. - zdnn_is_nnpa_installed will return
true(unless the zAIU feature is disabled for the system). - zdnn_get_max_runnable_version will
return
0x000100FFindicating zDNN APIs 1.0.x and below are available for use on the system. - Checking zdnn_is_version_runnable(0x00010100)
(1.1.0) will return
falseas only zDNN APIs 1.0.x and below are available for use on the system. - Checking zdnn_is_version_runnable(0x00010100)
(1.0.0) will return
trueas zDNN APIs 1.0.x and below are available for use on the system.
- zdnn_get_library_version will return
- Given a Telum II system with zDNN 1.1.0 installed:
- zdnn_get_library_version will return
0x00010100indicating zDNN library 1.1.0 is installed. - zdnn_is_nnpa_installed will return
true(unless the zAIU feature is disabled for the system). - zdnn_get_max_runnable_version will
return
0x000101FFindicating zDNN APIs 1.1.x and below are available for use on the system. - Checking zdnn_is_version_runnable(0x00010100)
(1.1.0) will return
trueas zDNN APIs 1.1.x and below are available for use on the system. - Checking zdnn_is_version_runnable(0x00010100)
(1.0.0) will return
trueas zDNN APIs 1.1.x and below are available for use on the system.
- zdnn_get_library_version will return
- Given a Telum II system with zDNN 1.0.0 installed:
- zdnn_get_library_version will return
0x00010000indicating zDNN library 1.0.0 is installed. - zdnn_is_nnpa_installed will return
true(unless the zAIU feature is disabled for the system). - zdnn_get_max_runnable_version will
return
0x000100FFindicating zDNN APIs 1.0.x and below are available for use on the system. - Checking zdnn_is_version_runnable(0x00010100)
(1.1.0) will return
falseas only zDNN APIs 1.0.x and below are available for use on the system. - Checking zdnn_is_version_runnable(0x00010100)
(1.0.0) will return
trueas zDNN APIs 1.1.x and below are available for use on the system.
- zdnn_get_library_version will return
- Given a Telum I system with zDNN 1.1.0 installed:
API Reference
Support Functions
- Initialization
- Get smallest of the max index size value from across all dimensions
- Get max index for a given dimension
- Get Size
- Get Range
- Get maximum limit for a given data type
- Get minimum limit for a given data type
- Initialize pre-transformed tensor descriptor
- Generate transformed tensor descriptor
- Generate quantized transformed tensor descriptor
- Generate concatenated transformed tensor descriptor
- Initialize zTensor
- Initialize zTensor with memory allocate
- Initialize quantized zTensor
- Initialize quantized zTensor with memory allocate
- Reset zTensor
- Allocate memory for zTensor
- De-allocate memory for zTensor
- Retrieve status message of the status code
- Reshape zTensor
- Check if version is runnable
- Get maximum runnable version
zdnn_init
Description
Initialize the zDNN library. This sends an NNPA_QAF to query the NNPA and loads the current environment variable settings.
This needs to be invoked at least once if zDNN library is statically-linked. It is automatically invoked if zDNN library is dynamically loaded.
Format
void zdnn_init();
Parameters
None
Returns
None
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_get_nnpa_max_dim_idx_size
Description
Retrieve the smallest of the maximum dimension index size values across all dimensions currently supported by the zAIU from zDNN's internal memory.
Format
uint32_t zdnn_get_nnpa_max_dim_idx_size();
Parameters
None
Returns
Maximum dimension index size supported by the zAIU across all dimensions
Since
Introduced in zDNN 1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_get_max_for_dim
Description
Retrieve the maximum dimension index size value currently supported by the zAIU for a given dimension from zDNN's internal memory. These limits relate to ztensor's transformed descriptor values. Special care is required when using layouts with special re-arrangements of data. See zDNN Data Layouts for more details.
Format
uint32_t zdnn_get_max_for_dim(uint8_t dimension);
Parameters
-
int dimension- dimension to get maximum index size for
Returns
Maximum dimension index size supported by the zAIU for a given dimension
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_get_nnpa_max_tensor_size
Description
Retrieve the maximum tensor size value (number of bytes required for storing a transformed tensor) currently supported by the zAIU from zDNN's internal memory.
Format
uint64_t zdnn_get_nnpa_max_tensor_size();
Parameters
None
Returns
Maximum tensor size supported by the zAIU
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_is_nnpa_installed
Description
Interrogates the hardware to determine if the NNPA and associated instructions are installed.
Use this function during application initialization to determine whether the zAIU hardware is available.
Format
bool zdnn_is_nnpa_installed();
Parameters
- None.
Returns
true if NNPA and associated instructions are installed, false otherwise.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_is_nnpa_function_installed
Description
Query, from zDNN internal memory, if requested NNPA functions are available.
Format
bool zdnn_is_nnpa_function_installed(int count, ...);
Parameters
-
int count- number of NNPA functions to check
-
... (additional arguments)- Function names separated by commas, e.g., NNPA_MUL, NNPA_MIN
NNPA_QAF
NNPA_ADD
NNPA_SUB
NNPA_MUL
NNPA_DIV
NNPA_MIN
NNPA_MAX
NNPA_LOG
NNPA_EXP
NNPA_RELU
NNPA_TANH
NNPA_SIGMOID
NNPA_SOFTMAX
NNPA_BATCHNORMALIZATION
NNPA_MAXPOOL2D
NNPA_AVGPOOL2D
NNPA_LSTMACT
NNPA_GRUACT
NNPA_CONVOLUTION
NNPA_MATMUL_OP
NNPA_MATMUL_OP_BCAST23
NNPA_MATMUL_OP_BCAST1
NNPA_TRANSFORM
Returns
true if all queried formats are installed or if count is zero, false
otherwise.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_is_nnpa_parmblk_fmt_installed
Description
Query, from zDNN internal memory, if requested parameter block formats are installed.
Format
bool zdnn_is_nnpa_parmblk_fmt_installed(int count, ...);
Parameters
-
int count- number of NNPA parameter block formats to check
-
... (additional arguments)- NNPA parameter block formats separated by commas
NNPA_PARMBLKFORMAT_0
NNPA_PARMBLKFORMAT_1
Returns
true if all queried formats are installed or if count is zero, false
otherwise.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_is_nnpa_datatype_installed
Description
Query, from zDNN internal memory, if requested NNPA data type are installed.
Format
bool zdnn_is_nnpa_datatype_installed(uint16_t types_bitmask);
Parameters
-
uint16_t types_bitmask- OR'd type bitmasks as defined in zdnn_query_datatypes enum
QUERY_DATATYPE_INTERNAL1
QUERY_DATATYPE_BINARY_FP32
QUERY_DATATYPE_BINARY_INT8
QUERY_DATATYPE_BINARY_INT32
Returns
true if all queried data types are installed, false otherwise.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_is_nnpa_layout_fmt_installed
Description
Query, from zDNN internal memory, if requested NNPA data layout format are installed.
Format
bool zdnn_is_nnpa_layout_fmt_installed(uint32_t layout_bitmask);
Parameters
-
uint32_t layout_bitmask- OR'd layout bitmasks as defined in zdnn_query_layoutfmts enum
QUERY_LAYOUTFMT_4DFEATURE
QUERY_LAYOUTFMT_4DKERNEL
QUERY_LAYOUTFMT_4DWEIGHTS
QUERY_LAYOUTFMT_4DGENERIC
Returns
true if all queried data layouts are installed, false otherwise.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_is_nnpa_conversion_installed
Description
Query, from zDNN internal memory, if requested NNPA data-type to/from BFP format conversions are installed.
Format
bool zdnn_is_nnpa_conversion_installed(nnpa_data_type type,
uint16_t format_bitmask);
Parameters
-
nnpa_data_type type- NNPA data-type number as defined in nnpa_data_type enum
NNPA_DATATYPE_1
-
uint16_t format_bitmask- OR'd BFP format bitmasks as defined in zdnn_query_bfpfmts enum
QUERY_BFPFMT_TINY (FP16)
QUERY_BFPFMT_SHORT (FP32/BFLOAT)
Returns
true if all queried conversions are installed, false otherwise.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_get_library_version
Description
Retrieve library version number as a 32-bit hex value in the form
0x00[major][minor][patch] where each segment is 1 byte. For example zDNN 1.2.3
would return 0x00010203.
This is the version of the libzdnn package installed on the system or zDNN embeded in a runtime application. The zDNN library version is independant of the system that zDNN is running on.
The library version indicates what zDNN APIs exist in that version of the zDNN library. It does NOT indicate whether those APIs are available for use. To check API availablity at runtime, see Validating the environment at runtime.
Format
uint32_t zdnn_get_library_version();
Returns
Library version number in 0x00[major][minor][patch] format.
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_get_library_version_str
Description
Retrieve the library version number and build information as a string.
Format
char *zdnn_get_library_version_str();
Returns
Library version number and build information as a string.
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_refresh_nnpa_query_result
Description
Refresh zDNN in-memory query result from zAIU.
Format
zdnn_status zdnn_refresh_nnpa_query_result();
Parameters
None
Programming Notes
This is called automatically as a part of zdnn_init and should not need to be
called directly. Manually refreshing query results before making other
zdnn_query_* calls may noticeably impact performance.
Returns zdnn_status indications
ZDNN_OKZDNN_UNAVAILABLE_FUNCTION
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_getsize_ztensor
Description
Used to determine the buffer size required for the transformed tensor (including
concatenated) in zDNN transformed format. Requires tensor descriptor
(zdnn_tensor_desc) with transformed shape information.
Format
uint64_t zdnn_getsize_ztensor(const zdnn_tensor_desc *tfrmd_desc);
Parameters
-
zdnn_tensor_desc *tfrmd_desc- Contains transformed information about the shape, layout and data type.
Returns zdnn_status indications
- required buffer size in bytes
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_getrange_ztensor
Description
Used to determine the minimum negative value and maximum positive value of the passed zdnn_ztensor, storing the results in min and max.
Format
void zdnn_getrange_ztensor(const zdnn_ztensor *ztensor, float *min, float *max);
Parameters
-
const zdnn_ztensor *ztensor- The zdnn_ztensor to return the min and max value of.
-
float *min- Pointer to a float used to store minimum negative value.
- If all values are positive, -0.0 will be used instead.
- Pointer to a float used to store minimum negative value.
-
float *max- Pointer to a float used to store maximum positive value.
- If all values are negative, 0.0 will be used instead.
- Pointer to a float used to store maximum positive value.
Returns
- None
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_get_max_limit
Description
Returns the maximum representable value between a transformed and pre-transformed zdnn_data_type.
Format
zdnn_status zdnn_get_max_limit(zdnn_data_types transformed_type,
zdnn_data_types pre_transformed_type, void *limit);
Parameters
-
zdnn_data_types transformed_type- input zdnn transformed data type.
- Restricted to the following transformed data types:
- ZDNN_DLFLOAT16
- ZDNN_BINARY_INT8
- ZDNN_BINARY_INT32
-
zdnn_data_types pre_transformed_type- input zdnn pre-transformed data type.
- Restricted to the following transformed data types:
- INT32
- INT8
- FP32
- FP16
- BFLOAT
-
void *limit- pointer to max value between transformed_type and pre_transformed_type in data type of pre_transformed_type.
Returns
ZDNN_OKZDNN_INVALID_TYPE- invalid transformed or pre_transformedtypeused and conversion could not be completed.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_get_min_limit
Description
Return the minimum representable value between a transformed and pre-transformed zdnn_data_type.
Format
zdnn_status zdnn_get_min_limit(zdnn_data_types transformed_type,
zdnn_data_types pre_transformed_type, void *limit);
Parameters
-
zdnn_data_types transformed_type- input zdnn transformed data type.
- Restricted to the following transformed data types:
- ZDNN_DLFLOAT16
- ZDNN_BINARY_INT8
- ZDNN_BINARY_INT32
-
zdnn_data_types pre_transformed_type- input zdnn pre-transformed data type.
- Restricted to the following transformed data types:
- INT32
- INT8
- FP32
- FP16
- BFLOAT
-
void *limit- pointer to min value between transformed_type and pre_transformed_type in data type of pre_transformed_type.
Returns
ZDNN_OKZDNN_INVALID_TYPE- invalid transformed or pre_transformedtypeused and conversion could not be completed.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_init_pre_transformed_desc
Description
Initialize tensor descriptor (zdnn_tensor_desc) struct with pre-transformed
(original) shape information.
Format
void zdnn_init_pre_transformed_desc(zdnn_data_layouts layout,
zdnn_data_types type,
zdnn_tensor_desc *pre_tfrmd_desc, ...);
Parameters
-
zdnn_data_layouts layout- data layout
-
zdnn_data_types type- data type
-
zdnn_tensor_desc *pre_tfrmd_desc- output zdnn_tensor_desc struct
-
... (additional arguments)- Variadic: number of elements in each dimension in accordance to the layout, in outermost to innermost order
Returns
- None
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_generate_transformed_desc
Description
Generate transformed tensor descriptor information based on supplied pre-transformed tensor descriptor.
Format
zdnn_status zdnn_generate_transformed_desc(
const zdnn_tensor_desc *pre_tfrmd_desc, zdnn_tensor_desc *tfrmd_desc);
Parameters
-
zdnn_tensor_desc *pre_tfrmd_desc- input tensor descriptor with pre-transformed shape information
-
zdnn_tensor_desc *tfrmd_desc- output
zdnn_tensor_descstruct
- output
zdnn_status indications
ZDNN_OKZDNN_INVALID_TYPE- pre-transformedtypeis not recognized or is a type only used for quantized ztensors.ZDNN_INVALID_LAYOUT- pre-transformedlayoutis not recognized or is a layout only used for concatenated tensors.
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_generate_quantized_transformed_desc
Description
Generate quantized transformed tensor descriptor information based on supplied pre-transformed tensor descriptor and quantized transform type.
Format
zdnn_status zdnn_generate_quantized_transformed_desc(
const zdnn_tensor_desc *pre_tfrmd_desc,
zdnn_quantized_transform_types transform_type,
zdnn_tensor_desc *tfrmd_desc);
Parameters
-
zdnn_tensor_desc *pre_tfrmd_desc- input tensor descriptor with pre-transformed shape information
- Has the following additional restrictions:
- Only the following pre-transformed layouts are supported.
- ZDNN_1D
- ZDNN_2D
- ZDNN_2DS
- ZDNN_3D
- ZDNN_3DS
- ZDNN_4D
- ZDNN_NHWC
- Only the following pre-transformed layouts are supported.
-
zdnn_quantized_transform_types transform_type- Type of quantized transformation
- QUANTIZED_DLFLOAT16
- QUANTIZED_INT8
- QUANTIZED_WEIGHTS_INT8
- Type of quantized transformation
-
zdnn_tensor_desc *tfrmd_desc- output
zdnn_tensor_descstruct
- output
zdnn_status indications
ZDNN_OKZDNN_INVALID_TYPE- pre-transformedtypeis not recognized, not supported for quantized ztensors: Quantized zTensor RequirementsZDNN_INVALID_LAYOUT- pre-transformedlayoutis not recognized, not supported for quantized ztensors, or is a layout only used for concatenated tensors.ZDNN_INVALID_TRANSFORM_TYPE- Invalid transformation type: Quantized zTensor Requirements
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_generate_transformed_desc_concatenated
Description
Generate concatenated transformed tensor descriptor information for RNN input-gates tensors based on a supplied pre-transformed tensor descriptor.
Format
zdnn_status zdnn_generate_transformed_desc_concatenated(
const zdnn_tensor_desc *pre_tfrmd_desc,
zdnn_concat_info info, zdnn_tensor_desc *tfrmd_desc);
Parameters
-
zdnn_tensor_desc *pre_tfrmd_desc- input tensor descriptor with pre-transformed shape information
-
zdnn_concat_info info-
Information about how the tensors will be concatenated, consists of the RNN_TYPE, PREV_LAYER and USAGE flags OR'd together:
RNN_TYPE flags:
- RNN_TYPE_LSTM - For LSTM
- RNN_TYPE_GRU - For GRU
PREV_LAYER flags:
- PREV_LAYER_UNI - Previous RNN layer is uni-directional
- PREV_LAYER_NONE - Previous layer is not a RNN layer
- PREV_LAYER_BIDIR - Previous RNN layer is bi-directional
USAGE flags:
- USAGE_WEIGHTS - Concatenate as input weights
- USAGE_HIDDEN_WEIGHTS - Concatenate as input hidden-weights
- USAGE_BIASES - Concatenate as input biases
- USAGE_HIDDEN_BIASES - Concatenate as input hidden-biases
-
-
zdnn_tensor_desc *tfrmd_desc- output
zdnn_tensor_descstruct
- output
zdnn_status indications
ZDNN_OKZDNN_INVALID_TYPE- pre-transformedtypeis not recognized or is not supported for concatenated tensors.ZDNN_INVALID_LAYOUT- pre-transformedlayoutis not recognized or is not supported for concatenated tensors.ZDNN_INVALID_CONCAT_INFO- invalid concatenation information.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_init_ztensor
Description
Initialize a zdnn_ztensor struct using the pre-transformed and transformed
tensor shape information
Format
void zdnn_init_ztensor(zdnn_tensor_desc *pre_tfrmd_desc,
zdnn_tensor_desc *tfrmd_desc, zdnn_ztensor *output);
Parameters
-
zdnn_tensor_desc *pre_tfrmd_desc- input tensor descriptor with pre-transformed shape information
-
zdnn_tensor_desc *tfrmd_desc- input tensor descriptor with transformed shape information
-
zdnn_ztensor *output- The
zdnn_ztensorstruct being initialized.
- The
Returns
- None
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_init_ztensor_with_malloc
Description
Same functionality as zdnn_init_ztensor, and computes the size required for
the tensor in the zDNN transformed format and allocates the storage for it. Sets
buffer and buffer_size fields within output.
Format
zdnn_status zdnn_init_ztensor_with_malloc(zdnn_tensor_desc *pre_tfrmd_desc,
zdnn_tensor_desc *tfrmd_desc,
zdnn_ztensor *output);
Parameters
-
zdnn_tensor_desc *pre_tfrmd_desc- input tensor descriptor with pre-transformed shape information
-
zdnn_tensor_desc *tfrmd_desc- input tensor descriptor with transformed shape information
-
zdnn_ztensor *output- The
zdnn_ztensorstruct being initialized.
- The
Returns zdnn_status indications
ZDNN_OKZDNN_INVALID_FORMAT-tfrmd_desc->formatis not recognized.ZDNN_INVALID_TYPE-tfrmd_desc->typeis not recognized or is a pre_tfrmd_desc type.ZDNN_INVALID_SHAPE- (if any of the following are true)- One of
tfrmd_desc->dim*dimensions is 0. - One of
tfrmd_desc->dim*dimensions is greater than zdnn_get_max_for_dim. - The total number of tfrmd_desc elements is larger than
zdnn_get_nnpa_max_tensor_size.
- One of
ZDNN_ALLOCATION_FAILURE- Unable to allocate required memory on a 4K boundary.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_init_quantized_ztensor
Description
Initialize a zdnn_ztensor struct using the pre-transformed and quantized
transformed tensor shape information along with scale and offset.
Format
void zdnn_init_quantized_ztensor(zdnn_tensor_desc *pre_tfrmd_desc,
zdnn_tensor_desc *tfrmd_desc, float scale,
float offset, zdnn_ztensor *output);
Parameters
-
zdnn_tensor_desc *pre_tfrmd_desc- input tensor descriptor with pre-transformed shape information
-
zdnn_tensor_desc *tfrmd_desc- input tensor descriptor with quantized transformed shape information
-
float scale- scale for quantized ztensor, must not be 0.
-
float offset- offset for quantized ztensor
-
zdnn_ztensor *output- The
zdnn_ztensorstruct being initialized.
- The
Programming Notes
-
The reciprocal of the
scalevalue is stored asoutput->rec_scaleand is used within subsequent quantized calls with reduced precision. Due to this, largescalevalues will lead to aoutput->rec_scalethat underflows to 0.0 and will result in an error in subsequent quantized calls. -
The
offsetvalue is stored asoutput->offsetand is used within subsequent quantized calls with reduced precision. Due to this, largeoffsetvalues will overflow to infinity and will result in an error in subsequent quantized calls.
Returns
- None
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_init_quantized_ztensor_with_malloc
Description
Same functionality as zdnn_init_quantized_ztensor, and computes the size
required for the tensor in the zDNN transformed format and allocates the storage
for it. Sets buffer and buffer_size fields within output.
Format
zdnn_status zdnn_init_quantized_ztensor_with_malloc(
zdnn_tensor_desc *pre_tfrmd_desc, zdnn_tensor_desc *tfrmd_desc, float scale,
float offset, zdnn_ztensor *output);
Parameters
-
zdnn_tensor_desc *pre_tfrmd_desc- input tensor descriptor with pre-transformed shape information
-
zdnn_tensor_desc *tfrmd_desc- input tensor descriptor with quantized transformed shape information
-
float scale- scale for quantized ztensor, must not be 0.
-
float offset- offset for quantized ztensor
-
zdnn_ztensor *output- The
zdnn_ztensorstruct being initialized.
- The
Programming Notes
-
The reciprocal of the
scalevalue is stored asoutput->rec_scaleand is used within subsequent quantized calls with reduced precision. Due to this, largescalevalues will lead to aoutput->rec_scalethat underflows to 0.0 and will result in an error in subsequent quantized calls. -
The
offsetvalue is stored asoutput->offsetand is used within subsequent quantized calls with reduced precision. Due to this, largeoffsetvalues will overflow to infinity and will result in an error in subsequent quantized calls.
Returns zdnn_status indications
ZDNN_OKZDNN_INVALID_FORMAT-tfrmd_desc->formatis not recognized.ZDNN_INVALID_TYPE-tfrmd_desc->typeis not recognized or is a pre_tfrmd_desc type.ZDNN_INVALID_SHAPE- (if any of the following are true)- One of
tfrmd_desc->dim*dimensions is 0. - One of
tfrmd_desc->dim*dimensions is greater than zdnn_get_max_for_dim. - The total number of tfrmd_desc elements is larger than
zdnn_get_nnpa_max_tensor_size.
- One of
ZDNN_ALLOCATION_FAILURE- Unable to allocate required memory on a 4K boundary.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_is_quantized_ztensor
Description
Check if a given zdnn_ztensor represents a quantized ztensor or not
Format
bool zdnn_is_quantized_ztensor(zdnn_ztensor *ztensor);
Parameters
-
zdnn_ztensor *ztensor- The
zdnn_ztensorbeing checked.
- The
Returns
true if zdnn_ztensor represents a quantized ztensor, false if not.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_reset_ztensor
Description
Reset a zdnn_ztensor struct for reuse.
Note this operation does not set or reset the buffer and buffer_size fields
nor free the transformed area storage.
Format
void zdnn_reset_ztensor(zdnn_ztensor *ztensor);
Parameters
-
zdnn_ztensor *output- The
zdnn_ztensorstruct being reset.
- The
Returns
- None
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_allochelper_ztensor
Description
Calculate the size required for the tensor in the zDNN transformed format and
allocate the needed storage, satisfying alignment requirements. Sets buffer
and buffer_size fields within ztensor.
Note that the calling application assumes ownership of this storage and is responsible for freeing it.
Format
zdnn_status zdnn_allochelper_ztensor(zdnn_ztensor *ztensor);
Parameters
-
zdnn_ztensor *ztensor- A
zdnn_ztensorstruct that contains the transformed shape information in thetransformed_descfield.
- A
Returns zdnn_status indications
ZDNN_OKZDNN_INVALID_FORMAT-ztensor->transformed_desc->formatis not recognized.ZDNN_INVALID_TYPE-ztensor->transformed_desc->typeis not recognized or is a pre_transformed_desc type.ZDNN_INVALID_LAYOUT-zdnn_ztensor->transformed_desc->layoutis not recognized or is not a valid transformed_desc layout.ZDNN_INVALID_SHAPE- (if any of the following are true)- One of
ztensor->transformed_desc->dim*dimensions is 0. - One of
ztensor->transformed_desc->dim*dimensions is greater than zdnn_get_max_for_dim. - The total number of transformed_desc elements is larger than
zdnn_get_nnpa_max_tensor_size.
- One of
ZDNN_ALLOCATION_FAILURE- Unable to allocate required memory on a 4K boundary.
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_free_ztensor_buffer
Description
Given an input zdnn_ztensor, zdnn_free_ztensor_buffer will free the transformed area storage associated with it.
Note that the routine does not free the storage allocated for the zdnn_ztensor struct itself.
Format
zdnn_status zdnn_free_ztensor_buffer(const zdnn_ztensor *ztensor);
Parameters
-
zdnn_ztensor *tensor- A
zdnn_ztensorstruct with field buffer pointing to storage allocated.
- A
Returns zdnn_status indications
ZDNN_OKZDNN_INVALID_BUFFER-tensor->bufferisNULL
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_get_status_message
Description
Retrieve status message of the status code
Format
const char *zdnn_get_status_message(zdnn_status status);
Parameters
-
zdnn_status status- Status code
Returns
Pointer to the description string or "(Status string is not defined.)" if
status is not defined.
Since
1.0.0
Requirements
- Any System Z hardware level
See Validating the environment at runtime.
zdnn_reshape_ztensor
Description
Reshape and copy buffer content from source zTensor's buffer to destination zTensor's in accordance to destination zTensor's shape.
The following conditions must be satisfied:
- Both tensor's transformed_desc must be fully initialized
dest->buffermust be pre-allocatedsrcmust be transformeddestmust be not already transformed- Both
transformed_desc->layoutmust be the same and either NHWC or HWCK - Both zTensors must contain equal number of elements
Format
zdnn_status zdnn_reshape_ztensor(const zdnn_ztensor *src, zdnn_ztensor *dest);
Parameters
-
src- Source zTensor to copy from
-
dest- Destination zTensor to copy to
Programming Notes
-
If
srcanddesthave the sametransformed_desc->dim1dimension size, the transformed data is directly copied to the destination without untransformation. -
If
srcanddesthave differenttransformed_desc->dim1dimension sizes, reshaping will internally un-transform the source and then re-transform the values into the destination.
Returns
ZDNN_OKZDNN_INVALID_SHAPE- (if any of the following are true)src's anddest'stransformed_desc->dim*total to different numbers of elements.- One of
dest->transformed_desc->dim*dimensions is 0. - One of
dest->transformed_desc->dim*dimensions is greater than zdnn_get_max_for_dim. - The total number of
dest->transformed_desc-dim*elements is larger thanzdnn_get_nnpa_max_tensor_size.
ZDNN_INVALID_LAYOUT- (if any of the following are true)src's anddest'stransformed_desc->layoutare not the same.transformed_desc->layoutis notZDNN_NHWCnorZDNN_HWCK.src->pre_transformed_desc->layoutis not recognized or is not a valid pre_transformed_desc layout.dest->pre_transformed_desc->layoutis not recognized or is not a valid pre_transformed_desc layout.
ZDNN_INVALID_STATE- (if any of the following are true)srcis not already transformed.destis already transformed.
ZDNN_INVALID_FORMAT-src->transformed_desc->formatis notZDNN_FORMAT_4DFEATURE.ZDNN_INVALID_TYPE(if any of the following are true)src->pre_transformed_desc->typeis not recognized or is a transformed_desc type.dest->pre_transformed_desc->typeis not recognized or is a transformed_desc type.dest->transformed_desc->typeis not recognized or is a pre_transformed_desc type.
ZDNN_INVALID_BUFFER(if any of the following are true)src->bufferisNULL.src->bufferis not on a 4K boundary.dest->bufferisNULL.dest->bufferis not on a 4K boundary.dest->buffer_sizeis too small to hold transformed values.
ZDNN_CONVERT_FAILURE- Values failed to un-transform or transform.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_is_version_runnable
Description
Check if application built for zDNN version ver_num can be run on the current
zAIU hardware with the installed zDNN library
Format
bool zdnn_is_version_runnable(uint32_t ver_num);
Parameters
-
ver_num- Version number of the zDNN library application itself, in 0x00[major][minor][patch] form. Typically this is the ZDNN_VERNUM used to compile the application
Returns
- true/false
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_get_max_runnable_version
Description
Returns the maximum version number associated with the APIs supported by the hardware and zDNN software in the current environment. This can be compared with the version documented in the "REQUIRES" section of each programming interface to discern whether the interface is supported at run-time.
The returned value is a version number in the major.minor format. APIs
defined at that level and below will be supported in the current environment.
Format
uint32_t zdnn_get_max_runnable_version();
Parameters
- None
Returns
- A 32-bit zDNN version number in
0x00\[major\]\[minor\]FFform.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Data Transformation
- Transform to zTensor
- Transform to zTensor with saturation
- Transform to quantized zTensor
- Transform to Original
zAIU requires the tensor data to be arranged in a format that enhances the performance characteristics of the operations. In this documentation, it is referred to as "transformed format". In addition, data conversions are necessary from the common formats (FP32, FP16, BFLOAT) to formats (DLFLOAT16) supported by the zAIU (DLFLOAT16, INT8). The following functions are provided:
-
'
zdnn_transform_ztensorandzdnn_transform_ztensor_with_saturation-
These functions will transform the input tensor and convert the input data to the format required by the zAIU. The resulting transformed ztensor can be reused as many times as necessary.
-
See zdnn_transform_ztensor and zdnn_transform_ztensor_with_saturation for details and restrictions on transforming an input tensor to the internal format.
-
-
zdnn_transform_origtensor-
zdnn_transform_origtensor transforms a ztensor (usually output from an operation or network) to the format and data types that are usable by the application.
-
See zdnn_transform_origtensor for details on transforming an input tensor to the internal format.
-
zdnn_transform_ztensor
Description
Converts the input tensor to the supported transformed format for execution by
zdnn operations. If transformation is successful the is_transformed field
within ztensor will be set to true otherwise it is set to false.
Transformation will fail if is_transformed was already true.
Note that the tensor layout in memory, once in transformed format, is dependent
on the content of the input tensor's descriptors (zdnn_tensor_desc fields).
Once converted, a zdnn_ztensor should only be manipulated by zDNN API
functions.
Format
zdnn_status zdnn_transform_ztensor(zdnn_ztensor *ztensor, ...);
Parameters
-
zdnn_ztensor *tensor- The input
zdnn_ztensorstruct.pre_transformed_descandtransformed_descmust be set,is_transformedmust befalse. A 4k-aligned tensor storage must be pre-allocated by the caller (directly or by calling the zDNN allocation helper function) and fieldbuffermust point to the storage.
- The input
-
... (additional arguments)- Variadic: list of pointers for input data to be transformed:
- Non-concatenated: 1 data pointer
- LSTM concatenated: 4 data pointers, one for each input gate in Forget, Input, Cell, Output (FICO) order
- GRU concatenated: 3 data pointers, one for each input gate in (Z)update, Reset, Hidden, (ZRH) gate order
- Variadic: list of pointers for input data to be transformed:
Programming Notes
- This function clears the pre-thread floating-point exception flags at entry,
and may set
FE_UNDERFLOW/FE_INVALID/FE_INEXACT/FE_OVERFLOWwhen it encounters errors during data conversion.
Returns zdnn_status indications
ZDNN_OKZDNN_INVALID_FORMAT-zdnn_ztensor->transformed_desc->formatis not recognized.ZDNN_INVALID_LAYOUT- (if any of the following are true)zdnn_ztensor->pre_transformed_desc->layoutis not recognized or is not a valid pre_transformed_desc layout.zdnn_ztensor->transformed_desc->layoutis not recognized or is not a valid transformed_desc layout.
ZDNN_INVALID_TYPE- (if any of the following are true)zdnn_ztensor->pre_transformed_desc->typeis not recognized or is a transformed_desc type.zdnn_ztensor->transformed_desc->typeis not recognized or is a pre_transformed_desc type.
ZDNN_INVALID_BUFFER(if any of the following are true)bufferisNULL.bufferis not on a 4K boundary.buffer_sizeis too small to hold transformed values.
ZDNN_INVALID_SHAPE- (if any of the following are true)- One of
zdnn_ztensor->transformed_desc->dim*dimensions is 0. - One of
zdnn_ztensor->transformed_desc->dim*dimensions is greater than zdnn_get_max_for_dim. - The total number of transformed_desc elements is larger than
zdnn_get_nnpa_max_tensor_size.
- One of
ZDNN_INVALID_STATE- Tensor is already transformed.ZDNN_CONVERT_FAILURE- Values failed to transform.- hardware statuses
ZDNN_FUNC_RC_F000- unsupported transformation function.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
zdnn_transform_ztensor_with_saturation
Description
Converts the input tensor to the supported transformed format for execution by
zdnn operations. If during transformation, an element results in a value that
exceeds the smallest or largest value that can be represented by DLFLOAT16, the
resulting element will contain the smallest or largest value and no
range-violation status will be triggered. If transformation is successful the
is_transformed field within ztensor will be set to true otherwise it is
set to false. Transformation will fail if is_transformed was already true.
Note that the tensor layout in memory, once in transformed format, is dependent
on the content of the input tensor's descriptors (zdnn_tensor_desc fields).
Once converted, a zdnn_ztensor should only be manipulated by zDNN API
functions.
Format
zdnn_status zdnn_transform_ztensor_with_saturation(zdnn_ztensor *ztensor, ...);
Parameters
-
zdnn_ztensor *tensor- The input
zdnn_ztensorstruct.pre_transformed_descandtransformed_descmust be set,is_transformedmust befalse. A 4k-aligned tensor storage must be pre-allocated by the caller (directly or by calling the zDNN allocation helper function) and fieldbuffermust point to the storage. - Has the following additional restrictions:
- Only non-quantized ztensors are supported. Use
zdnn_transform_quantized_ztensorif required.
- Only non-quantized ztensors are supported. Use
- The input
-
... (additional arguments)- Variadic: list of pointers for input data to be transformed:
- 1 data pointer supported at this time.
- Variadic: list of pointers for input data to be transformed:
Returns zdnn_status indications
ZDNN_OKZDNN_ELEMENT_RANGE_VIOLATIONZDNN_INVALID_FORMAT-zdnn_ztensor->transformed_desc->formatis not ZDNN_FORMAT_4DFEATURE.ZDNN_INVALID_LAYOUT- (if any of the following are true)zdnn_ztensor->pre_transformed_desc->layoutis not recognized or is not a valid pre_transformed_desc layout.zdnn_ztensor->transformed_desc->layoutis not recognized or is not a valid transformed_desc layout.
ZDNN_INVALID_TYPE- (if any of the following are true)zdnn_ztensor->pre_transformed_desc->typeis not recognized or is not a valid pre_transformed_desc type.zdnn_ztensor->transformed_desc->typeis not recognized or is not a valid transformed_desc type.
ZDNN_INVALID_BUFFER(if any of the following are true)bufferisNULL.bufferis not on a 4K boundary.buffer_sizeis too small to hold transformed values.
ZDNN_INVALID_SHAPE- (if any of the following are true)- One of
zdnn_ztensor->transformed_desc->dim*dimensions is 0. - One of
zdnn_ztensor->transformed_desc->dim*dimensions is greater than zdnn_get_max_for_dim. - The total number of transformed_desc elements is larger than
zdnn_get_nnpa_max_tensor_size.
- One of
ZDNN_INVALID_STATE- Tensor is already transformed.- hardware statuses
ZDNN_FUNC_RC_F000- unsupported transformation function.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_transform_quantized_ztensor
Description
Converts the input tensor to the supported quantized transformed format for
execution by zdnn operations. If transformation is successful the
is_transformed field within ztensor will be set to true otherwise it is
set to false. Transformation will fail if is_transformed was already true.
Note that the tensor layout in memory, once in transformed format, is dependent
on the content of the input tensor's descriptors (zdnn_tensor_desc fields).
Once converted, a zdnn_ztensor should only be manipulated by zDNN API
functions.
Format
zdnn_status zdnn_transform_quantized_ztensor(zdnn_ztensor *ztensor,
bool saturation_control,
int8_t clip_min, int8_t clip_max,
const void *data);
Parameters
-
zdnn_ztensor *tensor- The input
zdnn_ztensorstruct.pre_transformed_descandtransformed_descmust be set,is_transformedmust befalse. A 4k-aligned tensor storage must be pre-allocated by the caller (directly or by calling the zDNN allocation helper function) and fieldbuffermust point to the storage. - Has the following additional restrictions:
- Only the following pre-transformed layouts are supported.
- ZDNN_1D
- ZDNN_2D
- ZDNN_2DS
- ZDNN_3D
- ZDNN_3DS
- ZDNN_4D
- ZDNN_NHWC
- Only NHWC transformed layout is supported.
- See Quantized zTensor Requirements for supported transform types.
- Only the following pre-transformed layouts are supported.
- The input
-
bool saturation_control-
When enabled and an element results in a value that exceeds the smallest or largest value that can be represented by DLFLOAT16, the resulting element will contain the smallest or largest value and no range-violation status will be triggered.
-
Only applicable when all the following are true:
zdnn_ztensor *tensoris of zdnn_quantized_transform_types QUANTIZED_DLFLOAT16.- The
pre_transformed_desctypeof thezdnn_ztensor *tensoris FP32.
-
-
int8_t clip_min- Minimum clipping value
- Only applicable when
zdnn_ztensor *tensoris of zdnn_quantized_transform_types QUANTIZED_INT8. - Must be less than
clip_max
-
int8_t clip_max- Maximum clipping value
- Only applicable when
zdnn_ztensor *tensoris of zdnn_quantized_transform_types QUANTIZED_INT8. - Must be greater than
clip_min
Programming Notes
- This function clears the pre-thread floating-point exception flags at entry,
and may set
FE_UNDERFLOW/FE_INVALID/FE_INEXACT/FE_OVERFLOWwhen it encounters errors during data conversion.
Returns zdnn_status indications
ZDNN_OKZDNN_INVALID_FORMAT-zdnn_ztensor->transformed_desc->formatis not recognized.ZDNN_INVALID_LAYOUT- (if any of the following are true)zdnn_ztensor->pre_transformed_desc->layoutis not recognized or is not a valid pre_transformed_desc layout.zdnn_ztensor->transformed_desc->layoutis not recognized or is not a valid transformed_desc layout.
ZDNN_INVALID_TYPE- (if any of the following are true)zdnn_ztensor->pre_transformed_desc->typeis not recognized or is a transformed_desc type: Quantized zTensor Requirementszdnn_ztensor->transformed_desc->typeis not recognized or is a pre_transformed_desc type: Quantized zTensor Requirements
ZDNN_INVALID_BUFFER(if any of the following are true)bufferisNULL.bufferis not on a 4K boundary.buffer_sizeis too small to hold transformed values.
ZDNN_INVALID_SHAPE- (if any of the following are true)- One of
zdnn_ztensor->transformed_desc->dim*dimensions is 0. - One of
zdnn_ztensor->transformed_desc->dim*dimensions is greater than zdnn_get_max_for_dim. - The total number of transformed_desc elements is larger than
zdnn_get_nnpa_max_tensor_size.
- One of
ZDNN_INVALID_STATE- Tensor is already transformed.ZDNN_INVALID_CLIPPING_VALUE- clip_min value is not less than clip_max value.- hardware statuses
ZDNN_FUNC_RC_F000- Unsupported transformation function.ZDNN_FUNC_RC_F001- Either scale or offset is non-numeric or scale value is zero.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
zdnn_transform_origtensor
Description
Converts the input tensor from the zDNN transformed format back to a standard
non-transformed layout. The is_transformed field within ztensor must be
true.
All stick format tensors are supported, except:
- Kernel tensors
- Concatenated RNN input-gates tensors
Format
zdnn_status zdnn_transform_origtensor(const zdnn_ztensor *ztensor, void *out_buf);
Parameters
-
zdnn_ztensor *ztensor- The input
zdnn_ztensorstruct.pre_transformed_desc,transformed_descandbuffermust be set,is_transformedmust betrue.
- The input
-
void *out_buf- The buffer for storing the standard non-transformed tensor data. Must be pre-allocated by the caller.
Programming Notes
- This function clears the pre-thread floating-point exception flags at entry,
and may set
FE_UNDERFLOW/FE_INVALID/FE_INEXACT/FE_OVERFLOWwhen it encounters errors during data conversion.
Returns zdnn_status indications
ZDNN_OKZDNN_INVALID_FORMAT-ztensor->transformed_desc->formatis notZDNN_FORMAT_4DFEATURE.ZDNN_INVALID_LAYOUT- (if any of the following are true)zdnn_ztensor->pre_transformed_desc->layoutis not recognized or is not a valid pre_transformed_desc layout.zdnn_ztensor->transformed_desc->layoutis not recognized or is not a valid transformed_desc layout required by this function.
ZDNN_INVALID_TYPEztensor->pre_transformed_desc->typeis not recognized or is a transformed_desc type.ztensor->transformed_desc->typeis not recognized or is a pre_transformed_desc type.
ZDNN_INVALID_BUFFER(if any of the following are true)ztensor->bufferisNULL.ztensor->bufferis not on a 4K boundary.
ZDNN_INVALID_STATE-ztensoris not transformed.ZDNN_CONVERT_FAILURE- Values failed to un-transform.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Operations
See Table of Contents for operations list
Element-wise Operations
- Addition
- Subtraction
- Multiplication
- Division
- Minimum
- Maximum
- Natural Logarithm
- Exponential
- Square Root
- Inverse Square Root
zdnn_add
Description
Given two input tensors in zDNN transformed format, performs element-wise addition and stores the result into the provided output zDNN tensor.
Note that for zDNN use, broadcasting of the input tensor(s) must be performed by the caller. As such, the input tensors must be of the same shape.
Format
zdnn_status zdnn_add(const zdnn_ztensor *input_a, const zdnn_ztensor *input_b,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Tensor with addends to add to
input_btensor - Must follow general tensor requirements
- Tensor with addends to add to
-
zdnn_ztensor *input_b- Tensor with addends to add to
input_atensor - Must follow general tensor requirements
- Tensor with addends to add to
-
zdnn_ztensor *output- Tensor to hold the result of the addition
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_sub
Description
Given two input tensors in zDNN transformed format, performs element-wise subtraction and stores the result into the provided output zDNN tensor.
Note that for zDNN use, broadcasting of the input tensor(s) must be performed by the caller. As such, the input tensors must be of the same shape.
Format
zdnn_status zdnn_sub(const zdnn_ztensor *input_a, const zdnn_ztensor *input_b,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Tensor with minuends that will be subtracted by
input_btensor. - Must follow general tensor requirements
- Tensor with minuends that will be subtracted by
-
zdnn_ztensor *input_b- Tensor with subtrahends to subtract from
input_atensor. - Must follow general tensor requirements
- Tensor with subtrahends to subtract from
-
zdnn_ztensor *output- Tensor to hold the result of the subtraction
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_mul
Description
Given two input tensors in zDNN transformed format, performs element-wise multiplication and stores the result into the provided output zDNN tensor.
Note that for zDNN use, broadcasting of the input tensor(s) must be performed by the caller. As such, the input tensors must be of the same shape.
Format
zdnn_status zdnn_mul(const zdnn_ztensor *input_a, const zdnn_ztensor *input_b,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Tensor with multiplicands that will be multiplied by
input_btensor. - Must follow general tensor requirements
- Tensor with multiplicands that will be multiplied by
-
zdnn_ztensor *input_b- Tensor with multipliers for
input_atensor. - Must follow general tensor requirements
- Tensor with multipliers for
-
zdnn_ztensor *output- Tensor to hold the result of the multiplication.
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_div
Description
Given two input tensors in zDNN transformed format, performs element-wise division and stores the result into the provided output zDNN tensor.
Note that for zDNN use, broadcasting of the input tensor(s) must be performed by the caller. As such, the input tensors must be of the same shape.
Format
zdnn_status zdnn_div(const zdnn_ztensor *input_a, const zdnn_ztensor *input_b,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Tensor with dividends that will be divided by
input_btensor. - Must follow general tensor requirements
- Tensor with dividends that will be divided by
-
zdnn_ztensor *input_b- Tensor with divisors for
input_atensor. - Must follow general tensor requirements
- Tensor with divisors for
-
zdnn_ztensor *output- Tensor to hold the result of the division.
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_min
Description
Given two input tensors in zDNN transformed format, computes the element-wise minimum and stores the result into the provided output zDNN tensor.
Note that for zDNN use, broadcasting of the input tensor(s) must be performed by the caller. As such, the input tensors must be of the same shape.
Format
zdnn_status zdnn_min(const zdnn_ztensor *input_a, const zdnn_ztensor *input_b,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Tensor with values that will be compared with
input_btensor. - Must follow general tensor requirements
- Tensor with values that will be compared with
-
zdnn_ztensor *input_b- Tensor with values that will be compared with
input_atensor. - Must follow general tensor requirements
- Tensor with values that will be compared with
-
zdnn_ztensor *output- Tensor that holds the smaller value from each comparison of the inputs.
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_max
Description
Given two input tensors in zDNN transformed format, computes the element-wise maximum and stores the result into the provided output zDNN tensor.
Note that for zDNN use, broadcasting of the input tensor(s) must be performed by the caller. As such, the input tensors must be of the same shape.
Format
zdnn_status zdnn_max(const zdnn_ztensor *input_a, const zdnn_ztensor *input_b,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Tensor with values that will be compared with
input_btensor. - Must follow general tensor requirements
- Tensor with values that will be compared with
-
zdnn_ztensor *input_b- Tensor with values that will be compared with
input_atensor. - Must follow general tensor requirements
- Tensor with values that will be compared with
-
zdnn_ztensor *output- Tensor that holds the larger value from each comparison of the inputs.
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)s
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_log
Description
Given an input tensor in zDNN transformed format, computes the natural logarithm element-wise and stores the result into the provided output zDNN tensor.
Format
zdnn_status zdnn_log(const zdnn_ztensor *input, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *output- Tensor that holds the calculated natural logarithm of each value from
input_a - Must follow general tensor requirements
- Tensor that holds the calculated natural logarithm of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_exp
Description
Given an input tensor in zDNN transformed format, computes the exponential element-wise and stores the result into the provided output zDNN tensor.
Format
zdnn_status zdnn_exp(const zdnn_ztensor *input, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *output- Tensor that holds the calculated exponential of each value from
input - Must follow general tensor requirements
- Tensor that holds the calculated exponential of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_sqrt
Description
Given an input tensor in zDNN transformed format, computes the square root element-wise and stores the result into the provided output zDNN tensor.
Format
zdnn_status zdnn_sqrt(const zdnn_ztensor *input, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *output- Tensor that holds the calculated square root of each value from
input - Must follow general tensor requirements
- Tensor that holds the calculated square root of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_invsqrt
Description
Given an input tensor in zDNN transformed format, computes the inverse square root element-wise and stores the result into the provided output zDNN tensor.
Format
zdnn_status zdnn_invsqrt(const zdnn_ztensor *input, float epsilon,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
float epsilon- A float value added to input prior to computation.
-
zdnn_ztensor *output- Tensor that holds the calculated inverse square root of each value from
input - Must follow general tensor requirements
- Tensor that holds the calculated inverse square root of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_EPSILON- hardware statuses
Programming Notes
- On some models, if either or both an element and epsilon are very large, the addition of the two may result in a nonnumeric value, the inverse square root of which will also be nonnumeric. This may occur even though the inverse square root of an unconstrained sum would easily fit in the data type of an output-tensor element.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
TensorFlow Reciprical Square Root
Activation Operations
- Rectified Linear
- Leaky Rectified Linear
- Hyperbolic Tangent
- Sigmoid
- Softmax
- Softmax with Mask
- Gaussian Error Linear Unit
zdnn_relu
Description
Given an input tensor in zDNN transformed format produce an output tensor where the rectified linear function, y = max(0, x) is applied to the input element-wise. If an optional clipping_value is provided, clipping is performed against the intermediate output where z = min(y, clipping_value).
Format
zdnn_status zdnn_relu(const zdnn_ztensor *input, const void *clipping_value,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
void *clipping_value- A pointer to an FP32 value, used to clip input tensor's elements.
- If set to NULL or 0, no clipping will occur.
- Must not be a negative value.
-
zdnn_ztensor *output- Tensor that holds the rectified linear function result of each value from
input - Must follow general tensor requirements
- Tensor that holds the rectified linear function result of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_CLIPPING_VALUE- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_leaky_relu
Description
Given an input tensor in zDNN transformed format produce an output tensor where the leaky rectified linear function is applied to the input element-wise. The calculation used depends on the input element. When negative, y = a * x, where a is the adjustment factor. When 0 or positive, y = x. If an optional clipping_value is provided, clipping is performed against the intermediate output where z = min(y, clipping_value).
Format
zdnn_status zdnn_leaky_relu(const zdnn_ztensor *input,
const void *clipping_value,
float adjustment_factor, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
void *clipping_value- A pointer to an FP32 value, used to clip input tensor's elements.
- If set to NULL or 0, no clipping will occur.
- Must not be a negative value.
-
float adjustment_factor- A float value multiplied with negative elements from input.
- Must not be a negative value.
- Must not be greater than 1.
-
zdnn_ztensor *output- Tensor that holds the rectified linear function result of each value from
input - Must follow general tensor requirements
- Tensor that holds the rectified linear function result of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_CLIPPING_VALUEZDNN_INVALID_ADJUSTMENT_FACTOR- hardware statuses
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
TensorFlow Leaky Rectified Linear
zdnn_tanh
Description
Given an input tensor in zDNN transformed format, produces an output tensor where the hyperbolic tangent is applied to the input element-wise.
Format
zdnn_status zdnn_tanh(const zdnn_ztensor *input, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *output- Tensor that holds the hyperbolic tangent result of each value from
input - Must follow general tensor requirements
- Tensor that holds the hyperbolic tangent result of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_sigmoid
Description
Given an input tensor in zDNN transformed format, produces an output tensor where the sigmoid function is applied to the input element-wise.
Format
zdnn_status zdnn_sigmoid(const zdnn_ztensor *input, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *output- Tensor that holds the sigmoid result of each value from
input - Must follow general tensor requirements
- Tensor that holds the sigmoid result of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_softmax
Description
Given an input tensor in zDNN transformed format, computes the softmax
(normalized exponential) for each vector formed in dimension-1, then if
act_func is not SOFTMAX_ACT_NONE, the activation function is applied to the
results. Finally stores the results into the provided output zDNN tensor.
Note: Other parameters, such as axis, are not supported.
Format
zdnn_status zdnn_softmax(const zdnn_ztensor *input, void *save_area,
zdnn_softmax_act act_func, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- ZDNN_3DS tensor with pre-transformed shape [batch size, batch size, vector dimension size] or output from another operation that is of the correct shape.
- Must follow general tensor requirements
-
void *save_area- A preallocated memory address to use for temporary storage during internal operation processing.
- The preallocate memory must be at least 8K bytes in size, aligned on a 4k boundary.
- If set to NULL, the operation will determine, allocate and free storage automatically.
-
zdnn_softmax_act act_func- Activation function to apply to the results.
SOFTMAX_ACT_NONEorSOFTMAX_ACT_LOG
-
zdnn_ztensor *output- ZDNN_3DS tensor with the same shape as
input_athat holds the softmax result of each value frominput_a. - Must follow general tensor requirements
- ZDNN_3DS tensor with the same shape as
Programming Notes
-
If all elements of a dimension 1 vector are the largest magnitude negative number possible for the transformed data type, accuracy may be reduced.
-
A
ZDNN_3DStensor is expected, where thetransformed_descdim1 describes the vector, and dim2 and dim4 are used to batch multiple vector requests together. Dim3 must always be 1. Thezdnn_softmaxoperation is performed against the vector in dim1 repeating for each dim1 vector in the dim4 and dim2 dimensions. -
Tensors that cannot be processed as vectors in dim1 or as batches of dim1 vectors must be coerced or reshaped by the caller.
- When the entire tensor is to be processed by softmax, it can be coerced by
simply creating an alternate descriptor prior to zDNN transformation. For
example:
- A 4D tensor with
pre_transformed_descdimensions 2x2x2x2 and a data array of 16 FP32 entries could have an alternateZDNN_3DSlayoutpre_transformed_descusing dimensions 8x1x2 and use the same original data array prior tozdnn_transform_ztensor. After transformation, such a tensor would be valid forzdnn_softmax. - In another example, the 4D 2x2x2x2 tensor could be processed as 8 batches
of 2 vectors using a
ZDNN_3DSlayoutpre_transformed_descwith dimensions 1x8x2. - The inner-most dimension must remain the same during this coercion.
- A 4D tensor with
- When the entire tensor is to be processed by softmax, it can be coerced by
simply creating an alternate descriptor prior to zDNN transformation. For
example:
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_ALLOCATION_FAILURE- A preallocatedsave_areawas not specified and internal allocation for the required memory failed.- hardware statuses
ZDNN_FUNC_RC_F000- input tensorinput->transformed_desc->dim3was not 1.ZDNN_FUNC_RC_F001- Invalidact_func
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_softmax_mask
Description
Given an input tensor in zDNN transformed format, computes the softmax
(normalized exponential) for each vector formed in dimension-1 (from element
zero to mask - 1), then if act_func is not SOFTMAX_ACT_NONE, the activation
function is applied to the results. Finally stores the results into the provided
output zDNN tensor.
Note: Other parameters, such as axis, are not supported.
Format
zdnn_status zdnn_softmax_mask(const zdnn_ztensor *input, void *save_area,
zdnn_softmax_act act_func, uint32_t softmax_mask,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- ZDNN_3DS tensor with pre-transformed shape [batch size, batch size, vector dimension size] or output from another operation that is of the correct shape.
- Must follow general tensor requirements
-
void *save_area- A preallocated memory address to use for temporary storage during internal operation processing.
- The preallocate memory must be at least 8K bytes in size, aligned on a 4k boundary.
- If set to NULL, the operation will determine, allocate and free storage automatically.
-
zdnn_softmax_act act_func- Activation function to apply to the results.
SOFTMAX_ACT_NONEorSOFTMAX_ACT_LOG
-
uint32_t softmax_mask- 32-bit unsigned binary integer that specifies a count of dimensions 1 elements to be processed.
- If 0, behavior matches
zdnn_softmax - Must not exceed dimension 1 of input tensor.
-
zdnn_ztensor *output- ZDNN_3DS tensor with the same shape as
input_athat holds the softmax result of each value frominput_a. - Must follow general tensor requirements
- ZDNN_3DS tensor with the same shape as
Programming Notes
-
If all elements of a dimension 1 vector are the largest magnitude negative number possible for the transformed data type, accuracy may be reduced.
-
A
ZDNN_3DStensor is expected, where thetransformed_descdim1 describes the vector, and dim2 and dim4 are used to batch multiple vector requests together. Dim3 must always be 1. Thezdnn_softmax_maskoperation is performed against the vector in dim1 repeating for each dim1 vector in the dim4 and dim2 dimensions. -
Tensors that cannot be processed as vectors in dim1 or as batches of dim1 vectors must be coerced or reshaped by the caller.
- When the entire tensor is to be processed by softmax, it can be coerced by
simply creating an alternate descriptor prior to zDNN transformation. For
example:
- A 4D tensor with
pre_transformed_descdimensions 2x2x2x2 and a data array of 16 FP32 entries could have an alternateZDNN_3DSlayoutpre_transformed_descusing dimensions 8x1x2 and use the same original data array prior tozdnn_transform_ztensor. After transformation, such a tensor would be valid forzdnn_softmax_mask. - In another example, the 4D 2x2x2x2 tensor could be processed as 8 batches
of 2 vectors using a
ZDNN_3DSlayoutpre_transformed_descwith dimensions 1x8x2.
- A 4D tensor with
- When the entire tensor is to be processed by softmax, it can be coerced by
simply creating an alternate descriptor prior to zDNN transformation. For
example:
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_ALLOCATION_FAILURE- A preallocatedsave_areawas not specified and internal allocation for the required memory failed.- hardware statuses
ZDNN_FUNC_RC_F000- input tensorinput->transformed_desc->dim3was not 1.ZDNN_FUNC_RC_F001- Invalidact_funcZDNN_FUNC_RC_F002-softmax_maskexceeds dimension 1 of input tensor.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_gelu
Description
Given an input tensor in zDNN transformed format produce an output tensor where the Gaussian Error Linear Unit activation function, y = 0.5 * x * (1 + tanh(x * 0.7978845608 * (1 + 0.044715 * x * x))), is applied to the input element-wise.
Format
zdnn_status zdnn_gelu(const zdnn_ztensor *input, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *output- Tensor that holds the Gaussian Error Linear Unit results of each value from
input - Must follow general tensor requirements
- Tensor that holds the Gaussian Error Linear Unit results of each value from
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Programming Notes
- The range of certain input-element values may result in an error of greater than 1% in the output element, however the accuracy of properly conditioned models is not significantly degraded.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
TensorFlow Gaussian Error Linear Unit
ONNX Gaussian Error Linear Unit
Normalization Operations
zdnn_meanreduce2d
Description
Given an input tensor in zDNN transformed format, produces a downsampled tensor reducing the middle dimensions to a size of 1 based on the mean of the original values and stores the result to the provided output zDNN tensor.
Format
zdnn_status zdnn_meanreduce2d(const zdnn_ztensor *input, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Must be a ZDNN_NHWC tensor with pre_transformed shape [batch_Num, Height, Width, Channel].
- Height and Width dimension must be less than or equal to 1024.
- Must follow general tensor requirements
-
zdnn_ztensor *output- The result tensor which will hold the result of the pooling operation in its buffer.
- Shape:
outputdimensions batch_Num and Channel must be the same as the respective input dimensions.outputdimensions Height and Width must be 1.
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPE- Shape of input or output tensor is invalid based on given kernel and stride parametersZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
ZDNN_FUNC_RC_F001-inputtensor has a Height or Width dimension greater than allowed forzdnn_meanreduce2d.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
TensorFlow Reduce Mean with axis set for the Height and Width axes and
keepdims set to True.
zdnn_batchnorm
Description
Given three input zDNN tensors input_a, input_b, and input_c, computes the
batch-normalized result for each vector formed in dimension-1 as follows:
output = input_b * input_a + input_c
where input_b is a precomputed elementwise divide of scale and variance
tensors, and input_c is a precomputed elementwise multiply of (-1) * mean and
'input_b' + input bias tensors.
Format
zdnn_status zdnn_batchnorm(const zdnn_ztensor *input_a,
const zdnn_ztensor *input_b,
const zdnn_ztensor *input_c, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Must be a 4D ZDNN_NHWC tensor
- Must follow general tensor requirements
-
zdnn_ztensor *input_b- Must be a 1D ZDNN_1D tensor
- Must follow general tensor requirements
-
zdnn_ztensor *input_c- Must be a 1D ZDNN_1D tensor
- Must follow general tensor requirements
-
zdnn_ztensor *output- A zdnn_ztensor of the same size as
input_arepresenting the computed value of the above formula - Must follow general tensor requirements
- A zdnn_ztensor of the same size as
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_norm
Description
Given input_a and input_b tensors in zDNN transformed format, produces the norm of the difference of vectors. Calculation is performed as follows:
- Each element in dimension 1 of input_b is subtracted by the corresponding element of input_a.
- The difference is squared.
- The sum of the squared differences for dimension 1 is computed.
- The square root of the sum is placed in the first element of dimension 1 of output tensor.
Format
zdnn_status zdnn_norm(const zdnn_ztensor *input_a, zdnn_ztensor *input_b, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *input_b- Tensor with values to evaluate.
- Must follow general tensor requirements
-
zdnn_ztensor *output- Tensor with the result of the normalization operation.
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
[ONNX Normalization]
N / A
zdnn_moments
Description
Given an input tensor in zDNN transformed format and a bessel correction type, this produces the mean and variance for respective input tensor.
Format
zdnn_status zdnn_moments(const zdnn_ztensor *input,
zdnn_moments_bessel bessel_correction_type,
zdnn_ztensor *output_a, zdnn_ztensor *output_b);
Parameters
-
zdnn_ztensor *input_a- Must be a 4D ZDNN_NHWC tensor
- Must follow general tensor requirements
-
zdnn_moments_bessel bessel_correction_type- Bessel correction type to perform moments.
MOMENTS_BESSEL_POPULATIONMOMENTS_BESSEL_SAMPLE
- Bessel correction type to perform moments.
-
zdnn_ztensor *output_a- The output tensor that will hold the mean.
- Must follow general tensor requirements
-
zdnn_ztensor *output_b- The output tensor that will hold the variance.
- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_BESSEL_CORRECTION- hardware statuses
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Programming Notes
-
The
zdnn_momentsoperation may be used in combination of thezdnn_layernormoperation. Please see zdnn_layernorm for more guidance. -
When
MOMENTS_BESSEL_SAMPLEis provided for the bessel correction type, all provided input dimensions of the input tensor must not be equal to 1.
Framework Examples
[ONNX Moments]
N/A
zdnn_layernorm
Description
Given input_a, input_b, and input_c tensors in zDNN transformed format, produces the layernorm of the given tensors. Calculation is performed as follows:
- Each element in dimension 1 of input_b is subtracted by the corresponding element of input_a.
- A corresponding element of input_c is added to epsilon.
- The square root of the sume from step 2 is computed.
- The difference from step 1 is divided by the result of step 3.
- The quotient from step 4 is multiplied by gamma.
- The product from step 5 is added to beta.
- Result is stored in the corresponding element of output.
The above calculation could be depicted as follows:
Format
zdnn_status zdnn_layernorm(const
zdnn_ztensor *input_a,
const zdnn_ztensor *input_b,
const zdnn_ztensor *input_c,
float beta, float gamma, float epsilon,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input_a- Must be a 4D ZDNN_NHWC tensor
- Must follow general tensor requirements
-
zdnn_ztensor *input_b- Must be a 4D ZDNN_NHWC tensor
- Must follow general tensor requirements
- Contains arithmetic means (Moments output_a)
-
zdnn_ztensor *input_c- Must be a 4D ZDNN_NHWC tensor
- Must follow general tensor requirements
- Contains arithmetic variances (Moments output_b)
-
float beta- Final result adjustment addend.
-
float gamma- Final result adjustment multiplier.
-
float epsilon- Intermediate variance adjustment.
-
zdnn_ztensor *output- Must follow general tensor requirements
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_BETAZDNN_INVALID_GAMMAZDNN_INVALID_EPSILON- hardware statuses
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Programming Notes
-
zdnn_layernormis intended to be used in combination with thezdnn_momentsnormalization operation. Thezdnn_momentsoperation produces two output tensors containing the means and variances, respectively, of the dimension- 4-index elements of the input tensor. The original input tensor tozdnn_momentsis intended to be used as the input-tensor 1 tozdnn_layernorm. The output-tensors 1 and 2 ofzdnn_momentsare intended to be used as input as input-tensor 2 and input-tensor 3 of thezdnn_layernormoperation. -
The beta and gamma values in the 4th and 5th parameters of
zdnn_layernorm, (also reffered to as bias and gain), provide a learned scale and offset. The epsilon value in parameter 6 ofzdnn_layernormis intended to be a small value (for example, 0.001) to provide numerical stability.
Framework Examples
zdnn_reduce
Description
Given an input tensor in zDNN transformed format, produces an output tensor where the given reduction operation is performed.
Format
zdnn_status zdnn_reduce(const zdnn_ztensor *input, void *save_area,
zdnn_reduce_ops op_type, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with values to evaluate.
- Must follow general tensor requirements
-
void *save_area- A preallocated memory address to use for temporary storage during internal operation processing.
- The preallocate memory must be at least 8K bytes in size, aligned on a 4k boundary.
- If set to NULL, the operation will determine, allocate and free storage automatically.
-
zdnn_reduction_ops op_type- Reduction Operation to perform on input tensor.
REDUCE_OP_MINIMUMREDUCE_OP_MINIMUM_IDXREDUCE_OP_MAXIMUMREDUCE_OP_MINIMUM_IDX
- Reduction Operation to perform on input tensor.
-
zdnn_ztensor *output- Tensor that holds the reduction operation result of each value from
input- Output dimension 1 must 1
- Must follow general tensor requirements
- Data Type must be as follows:
- (FP32, FP16, BFLOAT) when
op_typeisREDUCE_OP_MINIMUMorREDUCE_OP_MAXIMUM. - INT32 when
op_typeisREDUCE_OP_MINIMUM_IDXorREDUCE_OP_MAXIMUM_IDX
- (FP32, FP16, BFLOAT) when
- Tensor that holds the reduction operation result of each value from
The output when op_type is REDUCE_OP_MINIMUM or REDUCE_OP_MAXIMUM can be
initialized using:
zdnn_data_layouts input_layout = ZDNN_3DS;
zdnn_data_types input_type = FP32;
uint32_t dim4 = 4;
uint32_t dim2 = 5;
uint32_t dim1 = 6;
zdnn_tensor_desc input_pre_transformed_desc;
zdnn_init_pre_transformed_desc(input_layout, input_type,
&input_pre_transformed_desc, dim4, dim2, dim1);
zdnn_tensor_desc output_pre_transformed_desc;
zdnn_init_pre_transformed_desc(input_layout, input_type,
&output_pre_transformed_desc, dim4, dim2, 1);
The output when op_type is REDUCE_OP_MINIMUM_IDX or REDUCE_OP_MAXIMUM_IDX
can be initialized using:
zdnn_data_layouts input_layout = ZDNN_3DS;
zdnn_data_types input_type = FP32;
uint32_t dim4 = 4;
uint32_t dim2 = 5;
uint32_t dim1 = 6;
zdnn_tensor_desc input_pre_transformed_desc;
zdnn_init_pre_transformed_desc(input_layout, input_type,
&input_pre_transformed_desc, dim4, dim2, dim1);
zdnn_data_types output_type = INT32;
zdnn_tensor_desc output_pre_transformed_desc;
zdnn_init_pre_transformed_desc(input_layout, output_type,
&output_pre_transformed_desc, dim4, dim2, 1);
Programming Notes
- If a nonnumeric element is encountered in a dimension-1 vecotr of input-tenzor 1, then (a) the resulting element in dimension 1 of output-tensor 1 is unpredictable, and the range-violation status will be returned.
- When the reduction operation is
REDUCE_OP_MINIMUM_IDXthe index of the first min value, from left-to-right, is returned when there are mulitple elements with the same min value. - When the reduction operation is
REDUCE_OP_MAXIMUM_IDXthe index of the first max value, from left-to-right, is returned when there are mulitple elements with the same max value.
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_UNAVAILABLE_FUNCTIONZDNN_ALLOCATION_FAILURE- A preallocatedsave_areawas not specified and internal allocation for the required memory failed.- hardware statuses
ZDNN_FUNC_RC_F000- Invalidop_type.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
[TensorFlow Reduce Min and Max]
tensorflow reduce minimum tensorflow reduce maximum
[ONNX Reduce Min and Max]
onnx reduce minimum onnx reduce maximum
zdnn_matmul_op
Description
Given three input zDNN tensors input_a, input_b, and input_c, determine
the matrix multiplication of input_a * input_b then perform one of the
following operations, using input_c against the dot product, storing the
result into the specified output zDNN tensor:
- Addition
- Compare - If dot product is greater than element.
- Compare - If dot product is greater or equal to element.
- Compare - If dot product is equal to element.
- Compare - If dot product is not equal to element.
- Compare - If dot product is less than or equal to element.
- Compare - If dot product is less than element.
For an operation type of addition, input_c is added to the intermediate dot
product. For operation types of comparison, the intermediate dot product is
compared to input_c and if the comparison is true, the result is set to a
value of 1; otherwise it is set to a value of 0.
The outermost dimension can optionally indicate that the inputs are stacks of matrices. The results for each matrix stack is independent of other stacks but all stacks are calculated in a single call.
Format
zdnn_status zdnn_matmul_op(const zdnn_ztensor *input_a,
const zdnn_ztensor *input_b,
const zdnn_ztensor *input_c,
zdnn_matmul_ops op_type, zdnn_ztensor *output);
Input / Output matmul tensor requirements
- See table in this section for
pre_transformed_descand shape requirements for each tensor. - All tensors must either be stacked or unstacked.
- Must follow general tensor requirements
| type | input_a | input_b | input_c | result |
|---|---|---|---|---|
| unstacked | ZDNN_2D (m, n) | ZDNN_2D (n, p) | ZDNN_1D (p) | ZDNN_2D (m, p) |
| stacked | ZDNN_3DS (s, m, n) | ZDNN_3DS (s, n, p) | ZDNN_2DS (s, p) | ZDNN_3DS (s, m, p) |
Parameters
-
zdnn_ztensor *input_a- Input tensor with the first matrix for multiplication
- pre_transformed shape and layout must match matmul tensor requirements
-
zdnn_ztensor *input_b- Input tensor with the second matrix for multiplication
- pre_transformed shape and layout must match matmul tensor requirements
-
zdnn_ztensor *input_c- Input tensor that will have the requested operation performed against the
intermediate dot product of
input_aandinput_b. - pre_transformed shape and layout must match matmul tensor requirements
- Input tensor that will have the requested operation performed against the
intermediate dot product of
-
zdnn_matmul_ops op_type- Operation to perform on dot product.
MATMUL_OP_ADDITIONMATMUL_OP_GREATERMATMUL_OP_GREATER_EQUALMATMUL_OP_EQUALMATMUL_OP_NOT_EQUALMATMUL_OP_LESSER_EQUALMATMUL_OP_LESSER
- Operation to perform on dot product.
-
zdnn_ztensor *output- The output tensor which will hold the result of the operation in its buffer.
- pre_transformed shape and layout must match matmul tensor requirements
Programming Notes
- Care must be exercised when comparing values for equality or inequality since the order of operations and rounding may produce, what appear to be, slightly different values when they are essentially the same value.
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMAT- hardware statuses
ZDNN_FUNC_RC_F000- Invalidop_type.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_matmul_bcast_op
Description
Given three input zDNN tensors input_a, input_b, and input_c, determine
the matrix multiplication of input_a * input_b, then perform one of the
following operations, using input_c against the dot product, storing the
result into the specified output zDNN tensor:
- Addition
- Compare - If dot product is greater than element.
- Compare - If dot product is greater or equal to element.
- Compare - If dot product is equal to element.
- Compare - If dot product is not equal to element.
- Compare - If dot product is less than or equal to element.
- Compare - If dot product is less than element.
When an input is ZDNN_3DS, the outermost dimension for that input can
optionally indicate that the input is a stack of matrices. Likewise, when an
input is ZDNN_2DS, the outermost dimension for that input can optionally
indicate that the input is a stack of vectors
For exmaple, if input_a were ZDNN_3DS, each stack of input_a is multiplied
by the same input_b matrix and input_c vector which are broadcast over each
stack of input_a. Results for each stack are returned in the corresponding
stack index of output.
Likewise, if input_b were ZDNN_3DS and input_c were ZDNN_2DS, each stack
of input_b is multiplied by the same input_a matrix which is broadcast over
each stack of input_b and input_c. Results for each stack are returned in
the corresponding stack index of output.
Format
zdnn_status zdnn_matmul_bcast_op(const zdnn_ztensor *input_a,
const zdnn_ztensor *input_b,
const zdnn_ztensor *input_c,
zdnn_matmul_bcast_ops op_type,
zdnn_ztensor *output);
Input / Output matmul broadcast tensor requirements
- See table in this section for
pre_transformed_descand shape requirements for each tensor. - Must follow general tensor requirements
| type | input_a | input_b | input_c | result |
|---|---|---|---|---|
| unstacked | ZDNN_2D (m, n) | ZDNN_2D (n, p) | ZDNN_1D (p) | ZDNN_2D (m, p) |
| stacked | ZDNN_3DS (s, m, n) | ZDNN_3DS (s, n, p) | ZDNN_2DS (s, p) | ZDNN_3DS (s, m, p) |
| bcast1 | ZDNN_2D (m, n) | ZDNN_3DS (s, n, p) | ZDNN_2DS (s, p) | ZDNN_3DS (s, m, p) |
| bcast23 | ZDNN_3DS (s, m, n) | ZDNN_2D (n, p) | ZDNN_1D (p) | ZDNN_3DS (s, m, p) |
Parameters
-
zdnn_ztensor *input_a- Input tensor with the first matrix for multiplication.
- pre_transformed shape and layout must match matmul broadcast tensor requirements
-
zdnn_ztensor *input_b- Input tensor with the second matrix for multiplication.
- pre_transformed shape and layout must match matmul broadcast tensor requirements
-
zdnn_ztensor *input_c- Input tensor that will have the requested operation performed against the
intermediate dot product for each "m" dimension in
output. - pre_transformed shape and layout must match matmul broadcast tensor requirements
- Input tensor that will have the requested operation performed against the
intermediate dot product for each "m" dimension in
-
zdnn_matmul_bcast_ops op_type- Operation to perform on dot product.
MATMUL_BCAST_OP_ADDITIONMATMUL_BCAST_OP_GREATERMATMUL_BCAST_OP_GREATER_EQUALMATMUL_BCAST_OP_EQUALMATMUL_BCAST_OP_NOT_EQUALMATMUL_BCAST_OP_LESSER_EQUALMATMUL_BCAST_OP_LESSER
- Operation to perform on dot product.
-
zdnn_ztensor *output- The output tensor which will hold the result of the operation in its buffer.
- pre_transformed shape and layout must match matmul broadcast tensor requirements
Programming Notes
- When
NNPA_PARMBLKFORMAT_1is not installed,zdnn_matmul_bcast_opsonly supports theMATMUL_BCAST_OP_ADDITIONop_type. If any other op_type is provided,ZDNN_UNAVAILABLE_FUNCTIONis returned. BCAST1is not supported whenNNPA_PARMBLKFORMAT_1is not installed and will returnZDNN_UNAVAILABLE_FUNCTION.- Care must be exercised when comparing values for equality or inequality since the order of operations and rounding may produce what appear to be slightly different values when they are essentially the same value.
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_UNAVAILABLE_FUNCTION- hardware statuses
ZDNN_FUNC_RC_F000- Invalidop_type.ZDNN_FUNC_RC_F001- Invalid input/output type or format combination.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime for the
following:
zdnn_data_layoutsspecification- bcast1
zdnn_matmul_bcast_opsspecifications:MATMUL_BCAST_OP_GREATERMATMUL_BCAST_OP_GREATER_EQUALMATMUL_BCAST_OP_EQUALMATMUL_BCAST_OP_NOT_EQUALMATMUL_BCAST_OP_LESSER_EQUALMATMUL_BCAST_OP_LESSER
- the underlying hardware supports zDNN APIs 1.0.x or later at runtime.
See Validating the environment at runtime.
Framework Examples
zdnn_matmul_transpose_op
Description
Given three input zDNN tensors input_a, input_b, and input_c, determine
the matrix multiplication of input_a * input_b then perform one of the
following operations, using input_c against the dot product, storing the
result into the specified output zDNN tensor:
- Addition
- Compare - If dot product is greater than element.
- Compare - If dot product is greater or equal to element.
- Compare - If dot product is equal to element.
- Compare - If dot product is not equal to element.
- Compare - If dot product is less than or equal to element.
- Compare - If dot product is less than element.
For an operation type of addition, input_c is added to the intermediate dot
product. For operation types of comparison, the intermediate dot product is
compared to input_c and if the comparison is true, the result is set to a
value of 1; otherwise it is set to a value of 0.
The outermost dimension can optionally indicate that the inputs are stacks of matrices. The results for each matrix stack is independent of other stacks but all stacks are calculated in a single call.
Format
zdnn_status zdnn_matmul_transpose_op(const zdnn_ztensor *input_a,
const zdnn_ztensor *input_b,
const zdnn_ztensor *input_c,
bool transpose_a, bool transpose_b,
zdnn_matmul_ops op_type,
zdnn_ztensor *output);
Input / Output matmul transpose tensor requirements
- See table in this section for
pre_transformed_descand shape requirements for each tensor. - All tensors must either be stacked or unstacked.
- Must follow general tensor requirements
| type | input_a | input_b | input_c | result |
|---|---|---|---|---|
| unstacked | ZDNN_2D (m, n) | ZDNN_2D (n, p) | ZDNN_1D (p) | ZDNN_2D (m, p) |
| stacked | ZDNN_3DS (s, m, n) | ZDNN_3DS (s, n, p) | ZDNN_2DS (s, p) | ZDNN_3DS (s, m, p) |
| bcast1 | ZDNN_2D (m, n) | ZDNN_3DS (s, n, p) | ZDNN_2DS (s, p) | ZDNN_3DS (s, m, p) |
| bcast23 | ZDNN_3DS (s, m, n) | ZDNN_2D (n, p) | ZDNN_1D (p) | ZDNN_3DS (s, m, p) |
Parameters
-
zdnn_ztensor *input_a- Input tensor with the first matrix for multiplication
- pre_transformed shape and layout must match matmul transpose tensor requirements
-
zdnn_ztensor *input_b- Input tensor with the second matrix for multiplication
- pre_transformed shape and layout must match matmul transpose tensor requirements
-
zdnn_ztensor *input_c- Input tensor that will have the requested operation performed against the
intermediate dot product of
input_aandinput_b. - pre_transformed shape and layout must match matmul transpose tensor requirements
- Input tensor that will have the requested operation performed against the
intermediate dot product of
-
bool transpose_a- Whether to transpose
input_aprior to dot product. - If
true,input_ashould have the unstacked dimensions (n, m) or stacked dimensions (s, n, m)
- Whether to transpose
-
bool transpose_b- Whether to transpose
input_bprior to dot product. - If
true,input_bshould have the unstacked dimensions (p, n) or stacked dimensions (s, p, n)
- Whether to transpose
-
zdnn_matmul_ops op_type- Operation to perform on dot product.
MATMUL_OP_ADDITIONMATMUL_OP_GREATERMATMUL_OP_GREATER_EQUALMATMUL_OP_EQUALMATMUL_OP_NOT_EQUALMATMUL_OP_LESSER_EQUALMATMUL_OP_LESSER
- Operation to perform on dot product.
-
zdnn_ztensor *output- The output tensor which will hold the result of the operation in its buffer.
- pre_transformed shape and layout must match matmul transpose tensor requirements
Programming Notes
zdnn_matmul_transpose_opis not supported whenNNPA_PARMBLKFORMAT_1is not installed and will returnZDNN_UNAVAILABLE_FUNCTION.- Care must be exercised when comparing values for equality or inequality since the order of operations and rounding may produce, what appear to be, slightly different values when they are essentially the same value.
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPEZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_UNAVAILABLE_FUNCTION- hardware statuses
ZDNN_FUNC_RC_F000- Invalidop_type.ZDNN_FUNC_RC_F001- Invalid input/output type or format combination.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.0.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_quantized_matmul_op
Description
Given three input zDNN tensors input_a, input_b, and input_c, determine
the matrix multiplication of input_a * input_b then perform one of the
following operations, using input_c against the dot product, storing the
result into the specified output zDNN tensor:
- Addition
- Compare - If dot product is greater than element.
- Compare - If dot product is greater or equal to element.
- Compare - If dot product is equal to element.
- Compare - If dot product is not equal to element.
- Compare - If dot product is less than or equal to element.
- Compare - If dot product is less than element.
For an operation type of addition, input_c is added to the intermediate dot
product. For operation types of comparison, the intermediate dot product is
compared to input_c and if the comparison is true, the result is set to a
value of 1; otherwise it is set to a value of 0.
The outermost dimension can optionally indicate that the inputs are stacks of matrices. The results for each matrix stack is independent of other stacks but all stacks are calculated in a single call.
When dequantize is true the output will be dequantized after computation.
When pre_computed is true. The pre-computed value of input_c for Addition
can be achieved using:
Za = input_a->offset;
Sa = 1 / input_a->rec_scale;
Zb = input_b->offset;
Sb = 1 / input_b->rec_scale;
Zc = input_c->offset;
Sc = 1 / input_c->rec_scale;
Zy = output->offset;
Sy = 1 / output->rec_scale;
N = input_b->pre_transformed_desc->dim2;
pre_computed = Zy - (Sc/Sy) * Zc - (Sc/Sy) * input_c + ((Sa * Sb) / Sy) * NZaZb;
The pre-computed value of input_c for Compare can be achieved using:
Za = input_a->offset;
Sa = 1 / input_a->rec_scale;
Zb = input_b->offset;
Sb = 1 / input_b->rec_scale;
Zc = input_c->offset;
Sc = 1 / input_c->rec_scale;
pre_computed = Sc / (Sa * Sb) * (input_c - Zc) + Za * sum(input_b, axis=-2)
Format
zdnn_status zdnn_quantized_matmul_op(const zdnn_ztensor *input_a,
const zdnn_ztensor *input_b,
const zdnn_ztensor *input_c,
zdnn_matmul_ops op_type,
const int8_t clip_min,
const int8_t clip_max,
const bool disable_clipping,
const bool dequantize,
const bool pre_computed,
void *work_area,
zdnn_ztensor *output);
Input / Output quantized matmul tensor requirements
- See table in this section for
pre_transformed_descand shape requirements for each tensor. - All tensors must either be stacked or unstacked.
- Must follow general tensor requirements
- All tensors should use
zdnn_generate_quantized_transformed_descwhen generating transformed descriptors, passing the appropriatezdnn_quantized_transform_types. - All quantized tensors should use
zdnn_init_quantized_ztensororzdnn_init_quantized_ztensor_with_mallocwhen initializing, passing thescaleandoffsetquantization parameters.scalemust be in range (-DLFLT_MAX <= scale <= DLFLT_MAX) and scale != 0.offsetmust be in range (-DLFLT_MAX <= offset <= DLFLT_MAX).
- All quantized input tensors should use
zdnn_transform_quantized_ztensorwhen transforming, passing theclip_minandclip_maxquantization parameters.
zdnn_data_layouts
| type | input_a | input_b | input_c | result |
|---|---|---|---|---|
| unstacked | ZDNN_2D (m, n) | ZDNN_2D (n, p) | ZDNN_1D (p) | ZDNN_2D (m, p) |
| stacked | ZDNN_3DS (s, m, n) | ZDNN_3DS (s, n, p) | ZDNN_2DS (s, p) | ZDNN_3DS (s, m, p) |
| bcast1 | ZDNN_2D (m, n) | ZDNN_3DS (s, n, p) | ZDNN_2DS (s, p) | ZDNN_3DS (s, m, p) |
| bcast23 | ZDNN_3DS (s, m, n) | ZDNN_2D (n, p) | ZDNN_1D (p) | ZDNN_3DS (s, m, p) |
zdnn_quantized_transform_types
| type | input_a | input_b | input_c | result |
|---|---|---|---|---|
| normal | QUANTIZED_INT8 | QUANTIZED_WEIGHTS_INT8 | QUANTIZED_INT8 | QUANTIZED_DLFLOAT16 |
| on-the-fly | QUANTIZED_DLFLOAT16 | QUANTIZED_WEIGHTS_INT8 | QUANTIZED_INT8 | QUANTIZED_DLFLOAT16 |
Parameters
-
zdnn_ztensor *input_a- Input tensor with the first matrix for multiplication
- pre_transformed shape and layout must match quantized matmul tensor requirements
-
zdnn_ztensor *input_b- Input tensor with the second matrix for multiplication
- pre_transformed shape and layout must match quantized matmul tensor requirements
-
zdnn_ztensor *input_c- Input tensor that will have the requested operation performed against the
intermediate dot product of
input_aandinput_b. - pre_transformed shape and layout must match quantized matmul tensor requirements
- Input tensor that will have the requested operation performed against the
intermediate dot product of
-
int8_t clip_min- Minimum quantized value for
input_aprior to dot product. - Only applicable when performing
on-the-flyquantization. - Must be less than
clip_max.
- Minimum quantized value for
-
int8_t clip_max- Maximum quantized value for
input_aprior to dot product. - Only applicable when performing
on-the-flyquantization. - Must be greater than
clip_min.
- Maximum quantized value for
-
bool disable_clipping- When
truedisables clipping and rounding.
- When
-
bool dequantize- Whether to dequantize returned ztensor.
-
bool pre_computed- Whether bias is already pre-computed.
-
void *work_area- A preallocated memory address to use for temporary storage during internal operation processing.
- If set to NULL, the operation will determine, allocate and free storage automatically.
- Amount of required storage is the same as
input_c->buffer_size. - The start of the buffer must be 4k aligned.
-
zdnn_matmul_ops op_type- Operation to perform on dot product.
MATMUL_OP_ADDITIONMATMUL_OP_GREATERMATMUL_OP_GREATER_EQUALMATMUL_OP_EQUALMATMUL_OP_NOT_EQUALMATMUL_OP_LESSER_EQUALMATMUL_OP_LESSER
- Operation to perform on dot product.
-
zdnn_ztensor *output- The output tensor which will hold the result of the operation in its buffer.
- pre_transformed shape and layout must match quantized matmul tensor requirements
Programming Notes
zdnn_quantized_matmul_opis not supported whenNNPA_PARMBLKFORMAT_1is not installed and will returnZDNN_UNAVAILABLE_FUNCTION.- Care must be exercised when comparing values for equality or inequality since the order of operations and rounding may produce, what appear to be, slightly different values when they are essentially the same value.
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPEZDNN_INVALID_TYPE: Quantized zTensor RequirementsZDNN_INVALID_FORMATZDNN_INVALID_SCALEZDNN_INVALID_OFFSETZDNN_INVALID_CLIPPING_VALUEZDNN_UNAVAILABLE_FUNCTION- hardware statuses
ZDNN_FUNC_RC_F000- Invalidop_type.ZDNN_FUNC_RC_F001- Invalid input/output type or format combination.ZDNN_FUNC_RC_F002- Invalid input/output scale.
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_lstm
Description
Implements Long-Short Term Memory layer (LSTM - Hochreiter 1997).
The following formula is computed for the input tensor input(t) for all time steps:
(Default: f=Sigmoid, g=Tanh, h=Tanh):
- it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Wbi + Rbi)
- ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Wbf + Rbf)
- ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)
- Ct = ft (.) Ct-1 + it (.) ct
- ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Wbo + Rbo)
- Ht = ot (.) h(Ct)
Format
zdnn_status zdnn_lstm(const zdnn_ztensor *input, const zdnn_ztensor *h0,
const zdnn_ztensor *c0, const zdnn_ztensor *weights,
const zdnn_ztensor *biases,
const zdnn_ztensor *hidden_weights,
const zdnn_ztensor *hidden_biases,
lstm_gru_direction direction, void *work_area,
zdnn_ztensor *hn_output, zdnn_ztensor *cf_output);
Also see an example in the usage example section.
LSTM Input / Output requirements
num_hiddendimensions:- Any num_hidden dimension must be less than or equal to
zdnn_get_max_for_dim(2) / 4elements.
- Any num_hidden dimension must be less than or equal to
Parameters
-
zdnn_ztensor *input- Input must be a tensor with the shape (num_timesteps, num_batches,
num_features) prior to transformation with the
zdnn_transform_ztensorAPI. - Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Must follow general tensor requirements
- Input must be a tensor with the shape (num_timesteps, num_batches,
num_features) prior to transformation with the
-
zdnn_ztensor *h0- Tensor containing the initial hidden state with shape (num_dirs,
num_batches, num_hidden) prior to transformation with the
zdnn_transform_ztensorAPI. - Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Must follow general tensor requirements
- Must follow num_hidden requirements
- Tensor containing the initial hidden state with shape (num_dirs,
num_batches, num_hidden) prior to transformation with the
-
zdnn_ztensor *c0- Tensor containing the initial cell state with shape (num_dirs, num_batches,
num_hidden) prior to transformation with the
zdnn_transform_ztensorAPI. - Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Must follow general tensor requirements
- Must follow num_hidden requirements
- Tensor containing the initial cell state with shape (num_dirs, num_batches,
num_hidden) prior to transformation with the
-
zdnn_ztensor *weights- Tensor containing the concatenated input connection weights in Forget, Input, Cell, Output (FICO) order.
- Prior to transformation, each gate needs to be transposed to shape (num_dirs, num_features, num_hidden) by the caller.
- Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_LSTMUSAGE_WEIGHTS- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
zdnn_ztensor *biases- Tensor containing the concatenated input connection bias in Forget, Input, Cell, Output (FICO) order.
- Prior to transformation, expects each gate needs to be shape (num_dirs, num_hidden).
- Expects
pre_transformed_desc->layoutto beZDNN_2DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_LSTMUSAGE_BIASES- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
zdnn_ztensor *hidden_weights- Tensor containing the concatenated hidden connection weights in Forget, Input, Cell, Output (FICO) order.
- Prior to transformation, each gate needs to be transposed to shape (num_dirs, num_hidden, num_hidden) by the caller.
- Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_LSTMUSAGE_HIDDEN_WEIGHTS- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
zdnn_ztensor *hidden_biases- Tensor containing the concatenated hidden connection bias in Forget, Input, Cell, Output (FICO) order.
- Prior to transformation, expects each gate needs to be shape (num_dirs, num_hidden).
- Expects
pre_transformed_desc->layoutto beZDNN_2DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_LSTMUSAGE_HIDDEN_BIASES- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
lstm_gru_direction direction- Direction indicator of
lstm_gru_direction directiontype. Valid values:FWD(forward)BWD(backward)BIDIR(bi-directional).
- For input and output shapes, the num_dirs dimension should be:
1for unidirectional calls such as FWD or BWD2for bidirectional calls such that:- dimension 0 contains FWD values.
- dimension 1 contains BWD values.
- Direction indicator of
-
void *work_area-
A preallocated memory address to use for temporary storage during internal operation processing.
-
If set to NULL, the operation will determine, allocate and free storage automatically.
-
Amount of required storage can be determined given the LSTM timestep, batch, and num_hidden values.
-
The sample code below creates a ztensor descriptor that is an equivalent size of the required
work_area. To use this sample code yourself, replace thenum_timesteps,num_batches, andnum_hiddenvariables with your own values.zdnn_tensor_desc desc; desc.dim4 = (4 * num_timesteps) + 6; desc.dim3 = 1; desc.dim2 = num_batches; desc.dim1 = num_hidden; uint64_t work_area_size = zdnn_getsize_ztensor(&desc);
-
-
For bidirectional, twice the amount of contiguous storage is required.
-
The start of the buffer must be 4k aligned.
-
-
zdnn_ztensor *hn_output-
Output results of the hidden states
-
Expects pre_transformed_desc->layout to be
ZDNN_4DS. -
Must follow general tensor requirements
-
Must follow num_hidden requirements
-
Output pre-transformed shapes:
- all timesteps: (num_timesteps, num_dirs, num_batches, num_hidden)
- final timestep only: (1, num_dirs, num_batches, num_hidden)
-
For bidirectional (
BIDIR) output:- Forward and backward results are concatenated on the innermost dimension.
- Can be used directly as input for subsequent RNN layers without needing
untransformation.
- Can not be used directly as input for other non-RNN zDNN ops.
- Untransformation is supported.
-
Note that for
BWDand the backward component ofBIDIRdirections, the output order matches the order of the input, not the processing order. For example, the first input timestep is the last to be processed and its result is the first timestep of the output.
-
-
zdnn_ztensor *cf_output-
Output results of the cell state for the last processed timestep
-
Expects pre_transformed_desc->layout to be
ZDNN_4DS. -
Must follow general tensor requirements
-
Must follow num_hidden requirements
-
Output pre-transformed shapes:
- (1, num_dirs, num_batches, num_hidden)
-
For bidirectional (
BIDIR):- Forward and backward results are concatenated on the innermost dimension.
- Can not be used directly as input for other non-RNN zDNN ops.
- Untransformation is supported.
-
Summary
| pre-transformed layout | pre-transformed shape | |
|---|---|---|
| input | ZDNN_3DS | (num_timesteps, num_batches, num_features) |
| h0 | ZDNN_3DS | (num_dirs, num_batches, num_hidden) |
| c0 | ZDNN_3DS | (num_dirs, num_batches, num_hidden) |
| weights | ZDNN_3DS | (num_dirs, num_features, num_hidden) |
| bias | ZDNN_2DS | (num_dirs, num_hidden) |
| hidden_weights | ZDNN_3DS | (num_dirs, num_hidden, num_hidden) |
| hidden_biases | ZDNN_2DS | (num_dirs, num_hidden) |
| hn_output | ZDNN_4DS | (num_timesteps, num_dirs, num_batches, num_hidden) (last timestep only when num_timesteps = 1) |
| cf_output | ZDNN_4DS | (1, num_dirs, num_batches, num_hidden) |
| create transformed descriptor via | |
|---|---|
| input | zdnn_generate_transformed_desc |
| h0 | zdnn_generate_transformed_desc |
| c0 | zdnn_generate_transformed_desc |
| weights | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_WEIGHTS + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| bias | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_BIASES + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| hidden_weights | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_HIDDEN_WEIGHTS + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| hidden_biases | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_HIDDEN_BIASES + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| hn_output | zdnn_generate_transformed_desc |
| cf_output | zdnn_generate_transformed_desc |
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_SHAPE- (if any of the following are not true)hn_outputtimesteps dimension must be 1 or the same size asinputtimestep dimension.- All tensors with a direction dimension have the same direction dimension size.
inputtimestep dimension must be greater than or equal to 1.- Other general shape violations (exceeds MDIS, etc.)
ZDNN_INVALID_DIRECTION-directionparameter was not a recognizedlstm_gru_direction.ZDNN_ALLOCATION_FAILURE- A preallocatedwork_areawas not specified and internal allocation for the required memory failed.- hardware statuses
Since
1.1.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_gru
Description
Implements Gated Recurrent Unit (Kyunghyun Cho 2014). Supports only reset after linear.
The following formula is computed for the input tensor input(t) for all time steps:
(Default: f=Sigmoid, g=Tanh):
- zt = f(Xt*(Wz^T) + Ht-1*(Rz^T) + Wbz + Rbz)
- rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr)
- ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh)
- Ht = (1 - zt) (.) ht + zt (.) Ht-1
Format
zdnn_status zdnn_gru(const zdnn_ztensor *input, const zdnn_ztensor *h0,
const zdnn_ztensor *weights, const zdnn_ztensor *biases,
const zdnn_ztensor *hidden_weights,
const zdnn_ztensor *hidden_biases,
lstm_gru_direction direction, void *work_area,
zdnn_ztensor *hn_output);
Also see an example in the usage example section.
GRU Input / Output requirements
num_hiddendimensions:- Any num_hidden dimension must be less than or equal to
zdnn_get_max_for_dim(2) / 3elements.
- Any num_hidden dimension must be less than or equal to
Parameters
-
zdnn_ztensor *input- Input must be a tensor with the shape (num_timesteps, num_batches,
num_features) prior to transformation with the
zdnn_transform_ztensorAPI. - Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Must follow general tensor requirements
- Input must be a tensor with the shape (num_timesteps, num_batches,
num_features) prior to transformation with the
-
zdnn_ztensor *h0- Tensor containing the initial hidden state with shape (num_dirs,
num_batches, num_hidden) prior to transformation with the
zdnn_transform_ztensorAPI. - Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Must follow general tensor requirements
- Must follow num_hidden requirements
- Tensor containing the initial hidden state with shape (num_dirs,
num_batches, num_hidden) prior to transformation with the
-
zdnn_ztensor *weights- Tensor containing the concatenated input connection weights in (Z)update, Reset, Hidden, (ZRH) order.
- Prior to transformation, each gate needs to be transposed to shape (num_dirs, num_features, num_hidden) by the caller.
- Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_GRUUSAGE_WEIGHTS- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
zdnn_ztensor *biases- Tensor containing the concatenated input connection bias in (Z)update, Reset, Hidden, (ZRH) order.
- Prior to transformation, expects each gate needs to be shape (num_dirs, num_hidden).
- Expects
pre_transformed_desc->layoutto beZDNN_2DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_GRUUSAGE_BIASES- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
zdnn_ztensor *hidden_weights- Tensor containing the concatenated hidden connection weights in (Z)update, Reset, Hidden, (ZRH) order.
- Prior to transformation, each gate needs to be transposed to shape (num_dirs, num_hidden, num_hidden) by the caller.
- Expects
pre_transformed_desc->layoutto beZDNN_3DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_GRUUSAGE_HIDDEN_WEIGHTS- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
zdnn_ztensor *hidden_biases- Tensor containing the concatenated hidden connection bias in (Z)update, Reset, Hidden, (ZRH) order.
- Prior to transformation, expects each gate needs to be shape (num_dirs, num_hidden).
- Expects
pre_transformed_desc->layoutto beZDNN_2DS. - Expects
zdnn_concat_infohaving the following flags turned on:RNN_TYPE_GRUUSAGE_HIDDEN_BIASES- Appropriate
PREV_LAYERflag:PREV_LAYER_NONEifinputtensor is not from a previous RNN layerPREV_LAYER_UNIifinputtensor is uni-directional output from a previous RNN layerPREV_LAYER_BIDIRifinputtensor is bi-directional output from a previous RNN layer
- Must follow concatenated tensor requirements
- Must follow num_hidden requirements
-
lstm_gru_direction direction- Direction indicator of
lstm_gru_direction directiontype. Valid values:FWD(forward)BWD(backward)BIDIR(bi-directional).
- For input shapes, the num_dirs dimension should be:
1for unidirectional calls such as FWD or BWD2for bidirectional calls such that:- dimension 0 contains FWD values.
- dimension 1 contains BWD values.
- Direction indicator of
-
void *work_area-
A preallocated memory address to use for temporary storage during internal operation processing.
-
If set to NULL, the operation will determine, allocate and free storage automatically.
-
Amount of required storage can be determined given the GRU timestep, batch, and num_hidden values.
-
The sample code below creates a ztensor descriptor that is an equivalent size of the required
work_area. To use this sample code yourself, replace thenum_timesteps,num_batches, andnum_hiddenvariables with your own values.zdnn_tensor_desc desc; desc.dim4 = (3 * num_timesteps) + 5; desc.dim3 = 1; desc.dim2 = num_batches; desc.dim1 = num_hidden; uint64_t work_area_size = zdnn_getsize_ztensor(&desc);
-
-
For bidirectional, twice the amount of contiguous storage is required.
-
The start of the buffer must be 4k aligned.
-
-
zdnn_ztensor *hn_output-
Output results of the hidden states
-
Expects pre_transformed_desc->layout to be
ZDNN_4DS. -
Must follow general tensor requirements
-
Must follow num_hidden requirements
-
Output pre-transformed shapes:
- all timesteps: (num_timesteps, num_dirs, num_batches, num_hidden)
- final timestep only: (1, num_dirs, num_batches, num_hidden)
-
For bidirectional (
BIDIR) output:- Forward and backward results are concatenated on the innermost dimension.
- Can be used directly as input for subsequent RNN layers without needing
untransformation.
- Can not be used directly as input for other non-RNN zDNN ops.
- Untransformation is supported.
-
Note that for
BWDand the backward component ofBIDIRdirections, the output order matches the order of the input, not the processing order. For example, the first input timestep is the last to be processed and its result is the first timestep of the output.
-
Summary
| pre-transformed layout | pre-transformed shape | |
|---|---|---|
| input | ZDNN_3DS | (num_timesteps, num_batches, num_features) |
| h0 | ZDNN_3DS | (num_dirs, num_batches, num_hidden) |
| weights | ZDNN_3DS | (num_dirs, num_features, num_hidden) |
| bias | ZDNN_2DS | (num_dirs, num_hidden) |
| hidden_weights | ZDNN_3DS | (num_dirs, num_hidden, num_hidden) |
| hidden_biases | ZDNN_2DS | (num_dirs, num_hidden) |
| hn_output | ZDNN_4DS | (num_timesteps, num_dirs, num_batches, num_hidden) (last timestep only when num_timesteps = 1) |
| create transformed descriptor via | |
|---|---|
| input | zdnn_generate_transformed_desc |
| h0 | zdnn_generate_transformed_desc |
| weights | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_WEIGHTS + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| bias | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_BIASES + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| hidden_weights | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_HIDDEN_WEIGHTS + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| hidden_biases | zdnn_generate_transformed_desc_concatenated - RNN_TYPE_LSTM + USAGE_HIDDEN_BIASES + one of the following:PREV_LAYER_NONE/PREV_LAYER_UNI/PREV_LAYER_BIDIR |
| hn_output | zdnn_generate_transformed_desc |
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_SHAPE- (if any of the following are not true)hn_outputtimesteps dimension must be 1 or the same size asinputtimestep dimension.- All tensors with a direction dimension have the same direction dimension size.
inputtimestep dimension must be greater than or equal to 1.- Other general shape violations (exceeds MDIS, etc.)
ZDNN_INVALID_DIRECTION-directionparameter was not a recognizedlstm_gru_direction.ZDNN_ALLOCATION_FAILURE- A preallocatedwork_areawas not specified and internal allocation for the required memory failed.- hardware statuses
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_avgpool2d
Description
Given an input tensor in zDNN transformed format, padding type, kernel size and kernel stride, produces a downsampled tensor reducing the middle dimensions based on the mean values within the kernel window at each step and stores the results into the provided output zDNN tensor.
Format
zdnn_status zdnn_avgpool2d(const zdnn_ztensor *input,
zdnn_pool_padding padding_type,
uint32_t kernel_height, uint32_t kernel_width,
uint32_t stride_height, uint32_t stride_width,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with original values to be downsampled in the output tensor.
- Must be a ZDNN_NHWC tensor with pre_transformed shape [batch_Num, Height, Width, Channel].
- See Parameter Restrictions below for information on the expected shape of the input tensor.
- Must follow general tensor requirements
-
padding_type- The type of padding to use for the pooling operations.
- Valid values: are
SAME_PADDINGorVALID_PADDING. - See Parameter Restrictions below for information on the expected value of padding_type.
- For information on "same" vs "valid" padding see: https://www.pico.net/kb/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-tensorflow.
-
kernel_height- Size of the kernel window that passes over the input's height dimension.
- See Parameter Restrictions below for information on the expected value of kerneL_height.
-
kernel_width- Size of the kernel window that passes over the input's width dimension.
- See Parameter Restrictions below for information on the expected value of kerneL_width.
-
stride_height- Number of positions the kernel moves over input's height dimension at each step.
- If
stride_heightis 0 thenstride_widthmust also be 0. - If strides are greater than 0 then
stride_heightmust be less than or equal to 30.
-
stride_width- Number of positions the kernel moves over the input's width dimension at each step.
- If
stride_heightis 0 thenstride_widthmust also be 0. - If strides are greater than 0 then
stride_widthmust be less than or equal to 30.
-
zdnn_ztensor *output- The result tensor which will hold the result of the pooling operation its buffer.
- Must be a ZDNN_NHWC tensor with pre_transformed shape [batch_Num, Height, Width, Channel].
- See Parameter Restrictions below for information on the expected shape of the output tensor.
- Must follow general tensor requirements
AvgPool2D Parameter Restrictions
Parameter restrictions may vary based on provided strides and padding_type.
-
Input tensor batch_Num and Channel dimensions must always match the output tensor's respective dimensions.
-
If strides are 0:
- Both input tensor's Height dimension and the kernel_height must match and be less than or equal to 1024.
- Both input tensor's Width dimension and the kernel_width must match and be less than or equal to 1024.
- Output tensor's height and width dimensions must be 1.
- padding_type must be
VALID_PADDING.
-
If strides are greater than zero:
- kernel_width and kernel_height must be less than or equal to 64.
- input tensor's height or weight dimension must not be greater than 1024.
- If padding_type is
SAME_PADDING:- Output tensor's height dimension must equal
ceil((float)input's height / stride_height). - Output tensor's width dimension must equal
ceil((float)input's width / stride_width).
- Output tensor's height dimension must equal
- If padding_type is
VALID_PADDING:- Output tensor's height dimension must equal
ceil((float)(input's height - kernel_height + 1) / stride_height). - Output tensor's width dimension must equal
ceil((float)(input's width - kernel_width + 1) / stride_width).
- Output tensor's height dimension must equal
Programming Notes
- If the magnitude of difference between elements of
inputis large (greater than 10), accuracy may be reduced.
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPE- Shape of input or output tensor is invalid based on given kernel and stride parameters
- Other general shape violations (exceeds MDIS, etc.)
ZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_STRIDE_PADDINGZDNN_INVALID_STRIDES- One stride was non-zero, but not the other.- hardware statuses
ZDNN_EXCEEDS_MDISwill also occur if any of the following conditions occur:- stride_height is larger than
zdnn_get_max_for_dim(3). - stride_width is larger than
zdnn_get_max_for_dim(2). - kernel_height is 0 or is larger than
zdnn_get_max_for_dim(3). - kernel_width is 0 or is larger than
zdnn_get_max_for_dim(2).
- stride_height is larger than
ZDNN_FUNC_RC_F000- Invalidpadding_typeZDNN_FUNC_RC_F001-stride_height= 0 andstride_width= 0, but a kernel parameter is greater than allowed (seekernel_heightorkernel_widthabove)ZDNN_FUNC_RC_F002-stride_height> 0 andstride_width> 0, but a kernel parameter is greater than allowed (seekernel_heightorkernel_widthabove)ZDNN_FUNC_RC_F003-stride_height> 0 andstride_width> 0, but a stride parameter is greater than allowed (seestride_heightorstride_widthabove)ZDNN_FUNC_RC_F004-stride_height> 0 andstride_width> 0, but either input tensor's height or weight dimension is greater than 1024.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_maxpool2d
Description
Given an input tensor in zDNN transformed format, padding type, kernel size and kernel stride, produces a downsampled tensor reducing the middle dimensions based on the maximum values within the kernel window at each step and stores the results into the provided output zDNN tensor.
Format
zdnn_status zdnn_maxpool2d(const zdnn_ztensor *input,
zdnn_pool_padding padding_type,
uint32_t kernel_height, uint32_t kernel_width,
uint32_t stride_height, uint32_t stride_width,
zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with original values to be downsampled in the output tensor.
- Must be a ZDNN_NHWC tensor with pre_transformed shape [batch_Num, Height, Width, Channel].
- See Parameter Restrictions below for information on the expected shape of the input tensor.
- Must follow general tensor requirements
-
padding_type- The type of padding to use for the pooling operations.
- Valid values: are
SAME_PADDINGorVALID_PADDING. - See Parameter Restrictions below for information on the expected value of padding_type.
- For information on "same" vs "valid" padding see: https://www.pico.net/kb/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-tensorflow.
-
kernel_height- Size of the kernel window that passes over the input's height dimension.
- See Parameter Restrictions below for information on the expected value of kerneL_height.
-
kernel_width- Size of the kernel window that passes over the input's width dimension.
- See Parameter Restrictions below for information on the expected value of kerneL_width.
-
stride_height- Number of positions the kernel moves over input's height dimension at each step.
- If
stride_heightis 0 thenstride_widthmust also be 0. - If strides are greater than 0 then
stride_heightmust be less than or equal to 30.
-
stride_width- Number of positions the kernel moves over the input's width dimension at each step.
- If
stride_heightis 0 thenstride_widthmust also be 0. - If strides are greater than 0 then
stride_widthmust be less than or equal to 30.
-
zdnn_ztensor *output- The result tensor which will hold the result of the pooling operation its buffer.
- Must be a ZDNN_NHWC tensor with pre_transformed shape [batch_Num, Height, Width, Channel].
- See Parameter Restrictions below for information on the expected shape of the output tensor.
- Must follow general tensor requirements
MaxPool2D Parameter Restrictions
Parameter restrictions may vary based on provided strides and padding_type.
-
Input tensor batch_Num and Channel dimensions must always match the output tensor's respective dimensions.
-
If strides are 0:
- Both input tensor's Height dimension and the kernel_height must match and be less than or equal to 1024.
- Both input tensor's Width dimension and the kernel_width must match and be less than or equal to 1024.
- Output tensor's height and width dimensions must be 1.
- padding_type must be
VALID_PADDING.
-
If strides are greater than zero:
- kernel_width and kernel_height must be less than or equal to 64.
- input tensor's height or weight dimension must not be greater than 1024.
- If padding_type is
SAME_PADDING:- Output tensor's height dimension must equal
ceil((float)input's height / stride_height). - Output tensor's width dimension must equal
ceil((float)input's width / stride_width).
- Output tensor's height dimension must equal
- If padding_type is
VALID_PADDING:- Output tensor's height dimension must equal
ceil((float)(input's height - kernel_height + 1) / stride_height). - Output tensor's width dimension must equal
ceil((float)(input's width - kernel_width + 1) / stride_width).
- Output tensor's height dimension must equal
Programming Notes
- If the magnitude of difference between elements of
inputis large (greater than 10), accuracy may be reduced.
Returns (see zDNN Statuses for descriptions)
ZDNN_OKZDNN_INVALID_SHAPE- Shape of input or output tensor is invalid based on given kernel and stride parameters
- Other general shape violations (exceeds MDIS, etc.)
ZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_STRIDE_PADDINGZDNN_INVALID_STRIDES- One stride was non-zero, but not the other.- hardware statuses
ZDNN_EXCEEDS_MDISwill also occur if any of the following conditions occur:- stride_height is larger than
zdnn_get_max_for_dim(3). - stride_width is larger than
zdnn_get_max_for_dim(2). - kernel_height is 0 or is larger than
zdnn_get_max_for_dim(3). - kernel_width is 0 or is larger than
zdnn_get_max_for_dim(2).
- stride_height is larger than
ZDNN_FUNC_RC_F000- Invalidpadding_typeZDNN_FUNC_RC_F001-stride_height= 0 andstride_width= 0, but a kernel parameter is greater than allowed (seekernel_heightorkernel_widthabove)ZDNN_FUNC_RC_F002-stride_height> 0 andstride_width> 0, but a kernel parameter is greater than allowed (seekernel_heightorkernel_widthabove)ZDNN_FUNC_RC_F003-stride_height> 0 andstride_width> 0, but a stride parameter is greater than allowed (seestride_heightorstride_widthabove)ZDNN_FUNC_RC_F004-stride_height> 0 andstride_width> 0, but either input tensor's height or weight dimension is greater than 1024.
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
zdnn_conv2d
Description
Perform 2D convolution over an input tensor in zDNN transformed format.
First the input tensor is convolved with the kernel tensor. Then the bias
tensor is added to the results. Then if act_func is not CONV2D_ACT_NONE, the
activation function is applied to the results. Then if act_func is set to
CONV2D_ACT_RELU, and clipping_value is not NULL or 0, clipping is
performed against the intermediate result where z = min(intermediate_result,
clipping_value). Finally the results are stored into the provided output zDNN
tensor.
Format
zdnn_status zdnn_conv2d(const zdnn_ztensor *input,
const zdnn_ztensor *kernel,
const zdnn_ztensor *bias,
zdnn_pool_padding padding_type,
uint32_t stride_height, uint32_t stride_width,
zdnn_conv2d_act act_func,
const void *clipping_value, zdnn_ztensor *output);
Parameters
-
zdnn_ztensor *input- Tensor with original values to be downsampled in the output tensor.
- Must be a ZDNN_NHWC tensor with pre_transformed shape [num_batches, height_in, width_in, channels_in].
- See Convolution 2D Requirements for requirements.
- Must follow general tensor requirements
-
zdnn_ztensor *kernel- The kernel tensor to convolute with the input tensor.
- Must be a ZDNN_HWCK tensor with pre_transformed shape [kernel_height, kernel_width, channels_in, channels_out].
- See Convolution 2D Requirements for requirements.
- Must follow general tensor requirements
-
zdnn_ztensor *bias- The bias tensor to add to the convoluted results.
- Must be a ZDNN_1D tensor with pre_transformed shape [channels_out].
- See Convolution 2D Requirements for requirements.
- Must follow general tensor requirements
-
zdnn_pool_padding padding_type- The type of padding to use for the pooling operations.
- Valid values: are
SAME_PADDINGorVALID_PADDING. - For information on "same" vs "valid" padding see: https://www.pico.net/kb/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-tensorflow.
-
uint32_t stride_height- Number of positions the kernel moves over the input's
dim3dimension at each step. - See Convolution 2D Requirements for requirements.
- Number of positions the kernel moves over the input's
-
uint32_t stride_width- Number of positions the kernel moves over the input's
dim2dimension at each step. - See Convolution 2D Requirements for requirements.
- Number of positions the kernel moves over the input's
-
zdnn_conv2d_act act_func- Activation function to apply to the results.
CONV2D_ACT_NONEorCONV2D_ACT_RELU
-
void *clipping_value- A pointer to an FP32 value, used to clip input tensor's elements.
- If set to NULL or 0, no clipping will occur.
- Must not be a negative value.
- Value is ignored if
act_funcis not set toCONV2D_ACT_RELU.
-
zdnn_ztensor *output- The result tensor which will hold the results.
- Must be a ZDNN_NHWC tensor with pre_transformed shape [num_batches, height_out, width_out, channels_out].
- See Convolution 2D Requirements for requirements.
- Must follow general tensor requirements
Convolution 2D Requirements
| strides and padding | input (num_batches, height_in, width_in, channels_in) | kernel (kernel_height, kernel_width, channels_in, channels_out) | bias (channels_out) | output (num_batches, height_out, width_out, channels_out) |
|---|---|---|---|---|
| both strides > 0 and =< 13, SAME padding | both kernel_height and kernel_width must be =< 64 | height_out = ceil(height_in/stride_height) width_out = ceil(width_in/stride_width) | ||
| both strides > 0 and =< 13, VALID padding | height_in must be >= kernel_height width_in must be >= kernel_width | both kernel_height and kernel_width must be =< 64 | height_out = ceil((height_in - kernel_height + 1)/stride_height) width_out = ceil((width_in - kernel_width + 1)/stride_width) | |
| both strides = 0, VALID padding | height_in must be = kernel_height width_in must be = kernel_width | both kernel_height and kernel_width must be =< 448 | both height_out and width_out must be 1 |
Returns (see zDNN Statuses for descriptions)
ZDNN_OK- warning statuses
ZDNN_INVALID_SHAPE- Shape of input or output tensor is invalid based on given kernel and stride parameters
- Other general shape violations (exceeds MDIS, etc.)
ZDNN_INVALID_TYPEZDNN_INVALID_FORMATZDNN_INVALID_STRIDE_PADDINGZDNN_INVALID_STRIDESZDNN_INVALID_CLIPPING_VALUE- hardware statuses
ZDNN_FUNC_RC_F000- Invalidpadding_typeZDNN_FUNC_RC_F001- Invalidact_funcZDNN_FUNC_RC_F002-stride_height= 0 andstride_width= 0, but eitherkernel_heightorkernel_width> 448ZDNN_FUNC_RC_F003-stride_height> 0 andstride_width> 0, but eitherkernel_heightorkernel_width> 64ZDNN_FUNC_RC_F004- Eitherstride_heightorstride_width> 13
Since
1.0.0
Requirements
This feature requires that:
zdnn_is_nnpa_installed()returns true- the underlying hardware supports zDNN APIs 1.1.x or later at runtime
See Validating the environment at runtime.
Framework Examples
Convenience Functions
- None
Usage Examples
Example flow of an application calling the zDNN APIs
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// ***************************************************************************
// Sample:
//
// Create 2 zTensors a and b, and add them together via zdnn_add()
// ***************************************************************************
int main(int argc, char *argv[]) {
zdnn_tensor_desc pre_tfrmd_desc, tfrmd_desc;
zdnn_ztensor ztensor_a;
zdnn_ztensor ztensor_b;
zdnn_ztensor ztensor_out;
zdnn_status status;
uint32_t dim_n = 1, dim_h = 32, dim_w = 32, dim_c = 3;
zdnn_data_types type = FP32;
short element_size = 4; // size of each element in bytes
uint64_t num_elements = dim_n * dim_h * dim_w * dim_c;
// allocate tensor data storage
void *data1 = malloc(num_elements * element_size);
void *data2 = malloc(num_elements * element_size);
void *data_out = malloc(num_elements * element_size);
// read input_data
// check status for zAIU availability, supported ops, etc. here
// status = zdnn_query();
// set input tensor data to 0 to 127 sequentially and repeat
for (uint64_t i = 0; i < num_elements; i++) {
((float *)data1)[i] = (float)(i & 0x7f);
((float *)data2)[i] = (float)(i & 0x7f);
}
zdnn_init_pre_transformed_desc(ZDNN_NHWC, type, &pre_tfrmd_desc, dim_n, dim_h,
dim_w, dim_c);
// generate transformed shape information
status = zdnn_generate_transformed_desc(&pre_tfrmd_desc, &tfrmd_desc);
assert(status == ZDNN_OK);
// initialize zTensors and allocate 4k-aligned storage via helper function
status =
zdnn_init_ztensor_with_malloc(&pre_tfrmd_desc, &tfrmd_desc, &ztensor_a);
assert(status == ZDNN_OK);
status =
zdnn_init_ztensor_with_malloc(&pre_tfrmd_desc, &tfrmd_desc, &ztensor_b);
assert(status == ZDNN_OK);
status =
zdnn_init_ztensor_with_malloc(&pre_tfrmd_desc, &tfrmd_desc, &ztensor_out);
assert(status == ZDNN_OK);
// transform the feature tensor
status = zdnn_transform_ztensor(&ztensor_a, data1);
assert(status == ZDNN_OK);
status = zdnn_transform_ztensor(&ztensor_b, data2);
assert(status == ZDNN_OK);
// perform element-wise add between the two input tensors
status = zdnn_add(&ztensor_a, &ztensor_b, &ztensor_out);
assert(status == ZDNN_OK);
// transform resultant zTensor back to original data format
status = zdnn_transform_origtensor(&ztensor_out, data_out);
assert(status == ZDNN_OK);
for (uint64_t i = 0; i < num_elements; i++) {
printf("out element %" PRIu64 " %f\n", i, ((float *)data_out)[i]);
}
// Free zTensors
status = zdnn_free_ztensor_buffer(&ztensor_a);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&ztensor_b);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&ztensor_out);
assert(status == ZDNN_OK);
free(data1);
free(data2);
free(data_out);
}
Example of calling the zdnn_quantized_matmul_op API (normal)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021, 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// Sample: Quantized Matmul
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
/***********************************************************************
*
* Quantized Matmul:
*
* INPUTS --------------------------------------------------------------
* input | ZDNN_3DS | (s, m, n)
* weights | ZDNN_3DS | (s, n, p)
* input_biases | ZDNN_2DS | (s, p)
*
* OUTPUTS -------------------------------------------------------------
* output | ZDNN_3DS | (s, m, p)
***********************************************************************/
uint32_t s = 2;
uint32_t m = 3;
uint32_t n = 4;
uint32_t p = 5;
short int8_size = 1; // size of each int8 element in bytes
short float_size = 4; // size of each float element in bytes
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &input_pre_tfrmd_desc,
s, m, n);
status = zdnn_generate_quantized_transformed_desc(
&input_pre_tfrmd_desc, QUANTIZED_INT8, &input_tfrmd_desc);
assert(status == ZDNN_OK);
float input_scale = 1.f;
float input_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc,
input_scale, input_offset,
&input);
assert(status == ZDNN_OK);
uint64_t input_data_size = s * m * n * float_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_quantized_ztensor(&input, false, INT8_MIN, INT8_MAX,
input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create weights zTensor
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, INT8, &weights_pre_tfrmd_desc,
s, n, p);
status = zdnn_generate_quantized_transformed_desc(
&weights_pre_tfrmd_desc, QUANTIZED_WEIGHTS_INT8, &weights_tfrmd_desc);
assert(status == ZDNN_OK);
float weights_scale = 1.f;
float weights_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc,
weights_scale,
weights_offset, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = s * n * p * int8_size;
void *weights_data = malloc(weights_data_size);
status = zdnn_transform_quantized_ztensor(&weights, false, INT8_MIN, INT8_MAX,
weights_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create biases zTensor
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, FP32, &biases_pre_tfrmd_desc,
s, p);
status = zdnn_generate_quantized_transformed_desc(
&biases_pre_tfrmd_desc, QUANTIZED_INT8, &biases_tfrmd_desc);
assert(status == ZDNN_OK);
float biases_scale = 1.f;
float biases_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc,
biases_scale, biases_offset,
&biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = s * p * float_size;
void *biases_data = malloc(biases_data_size);
status = zdnn_transform_quantized_ztensor(&biases, false, INT8_MIN, INT8_MAX,
biases_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create output zTensor
***********************************************************************/
zdnn_tensor_desc output_pre_tfrmd_desc, output_tfrmd_desc;
zdnn_ztensor output;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &output_pre_tfrmd_desc,
s, m, p);
status = zdnn_generate_quantized_transformed_desc(
&output_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &output_tfrmd_desc);
assert(status == ZDNN_OK);
float output_scale = 1.f;
float output_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&output_pre_tfrmd_desc,
&output_tfrmd_desc,
output_scale, output_offset,
&output);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
status = zdnn_quantized_matmul_op(&input, &weights, &biases,
MATMUL_OP_ADDITION, INT8_MIN, INT8_MAX,
NULL, &output);
assert(status == ZDNN_OK);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
uint64_t output_data_size = s * m * p * float_size;
void *output_data = malloc(output_data_size);
status = zdnn_transform_origtensor(&output, output_data);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&output);
assert(status == ZDNN_OK);
free(input_data);
free(weights_data);
free(biases_data);
free(output_data);
}
Example of calling the zdnn_quantized_matmul_op API (on-the-fly)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021, 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// Sample: Quantized Matmul on-the-fly
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
/***********************************************************************
*
* Quantized Matmul on-the-fly:
*
* INPUTS --------------------------------------------------------------
* input | ZDNN_3DS | (s, m, n)
* weights | ZDNN_3DS | (s, n, p)
* input_biases | ZDNN_2DS | (s, p)
*
* OUTPUTS -------------------------------------------------------------
* output | ZDNN_3DS | (s, m, p)
***********************************************************************/
uint32_t s = 2;
uint32_t m = 3;
uint32_t n = 4;
uint32_t p = 5;
short int8_size = 1; // size of each int8 element in bytes
short float_size = 4; // size of each float element in bytes
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &input_pre_tfrmd_desc,
s, m, n);
status = zdnn_generate_quantized_transformed_desc(
&input_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &input_tfrmd_desc);
assert(status == ZDNN_OK);
float input_scale = 1.f;
float input_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc,
input_scale, input_offset,
&input);
assert(status == ZDNN_OK);
uint64_t input_data_size = s * m * n * float_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_ztensor(&input, input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create weights zTensor
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, INT8, &weights_pre_tfrmd_desc,
s, n, p);
status = zdnn_generate_quantized_transformed_desc(
&weights_pre_tfrmd_desc, QUANTIZED_WEIGHTS_INT8, &weights_tfrmd_desc);
assert(status == ZDNN_OK);
float weights_scale = 1.f;
float weights_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc,
weights_scale,
weights_offset, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = s * n * p * int8_size;
void *weights_data = malloc(weights_data_size);
status = zdnn_transform_quantized_ztensor(&weights, false, INT8_MIN, INT8_MAX,
weights_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create biases zTensor
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, FP32, &biases_pre_tfrmd_desc,
s, p);
status = zdnn_generate_quantized_transformed_desc(
&biases_pre_tfrmd_desc, QUANTIZED_INT8, &biases_tfrmd_desc);
assert(status == ZDNN_OK);
float biases_scale = 1.f;
float biases_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc,
biases_scale, biases_offset,
&biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = s * p * float_size;
void *biases_data = malloc(biases_data_size);
status = zdnn_transform_quantized_ztensor(&biases, false, INT8_MIN, INT8_MAX,
biases_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create output zTensor
***********************************************************************/
zdnn_tensor_desc output_pre_tfrmd_desc, output_tfrmd_desc;
zdnn_ztensor output;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &output_pre_tfrmd_desc,
s, m, p);
status = zdnn_generate_quantized_transformed_desc(
&output_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &output_tfrmd_desc);
assert(status == ZDNN_OK);
float output_scale = 1.f;
float output_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&output_pre_tfrmd_desc,
&output_tfrmd_desc,
output_scale, output_offset,
&output);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
status = zdnn_quantized_matmul_op(&input, &weights, &biases,
MATMUL_OP_ADDITION, INT8_MIN, INT8_MAX,
NULL, &output);
assert(status == ZDNN_OK);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
uint64_t output_data_size = s * m * p * float_size;
void *output_data = malloc(output_data_size);
status = zdnn_transform_origtensor(&output, output_data);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&output);
assert(status == ZDNN_OK);
free(input_data);
free(weights_data);
free(biases_data);
free(output_data);
}
Example of calling the zdnn_quantized_matmul with pre_computed=true API (normal)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021, 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// Sample: Quantized Matmul Pre-Computed
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
/***********************************************************************
*
* Quantized Matmul Pre-Computed:
*
* INPUTS --------------------------------------------------------------
* input | ZDNN_3DS | (s, m, n)
* weights | ZDNN_3DS | (s, n, p)
* input_biases | ZDNN_2DS | (s, p)
*
* OUTPUTS -------------------------------------------------------------
* output | ZDNN_3DS | (s, m, p)
***********************************************************************/
uint32_t s = 2;
uint32_t m = 3;
uint32_t n = 4;
uint32_t p = 5;
short int8_size = 1; // size of each int8 element in bytes
short float_size = 4; // size of each float element in bytes
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &input_pre_tfrmd_desc,
s, m, n);
status = zdnn_generate_quantized_transformed_desc(
&input_pre_tfrmd_desc, QUANTIZED_INT8, &input_tfrmd_desc);
assert(status == ZDNN_OK);
float input_scale = 1.f;
float input_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc,
input_scale, input_offset,
&input);
assert(status == ZDNN_OK);
uint64_t input_data_size = s * m * n * float_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_quantized_ztensor(&input, false, INT8_MIN, INT8_MAX,
input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create weights zTensor
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, INT8, &weights_pre_tfrmd_desc,
s, n, p);
status = zdnn_generate_quantized_transformed_desc(
&weights_pre_tfrmd_desc, QUANTIZED_WEIGHTS_INT8, &weights_tfrmd_desc);
assert(status == ZDNN_OK);
float weights_scale = 1.f;
float weights_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc,
weights_scale,
weights_offset, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = s * n * p * int8_size;
void *weights_data = malloc(weights_data_size);
status = zdnn_transform_quantized_ztensor(&weights, false, INT8_MIN, INT8_MAX,
weights_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create pre-computed biases zTensor
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, FP32, &biases_pre_tfrmd_desc,
s, p);
status = zdnn_generate_quantized_transformed_desc(
&biases_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &biases_tfrmd_desc);
assert(status == ZDNN_OK);
float biases_scale = 1.f;
float biases_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc,
biases_scale, biases_offset,
&biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = s * p * float_size;
void *biases_data = malloc(biases_data_size);
status = zdnn_transform_ztensor(&biases, biases_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create output zTensor
***********************************************************************/
zdnn_tensor_desc output_pre_tfrmd_desc, output_tfrmd_desc;
zdnn_ztensor output;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &output_pre_tfrmd_desc,
s, m, p);
status = zdnn_generate_quantized_transformed_desc(
&output_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &output_tfrmd_desc);
assert(status == ZDNN_OK);
float output_scale = 1.f;
float output_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&output_pre_tfrmd_desc,
&output_tfrmd_desc,
output_scale, output_offset,
&output);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
status = zdnn_quantized_matmul_op(&input, &weights, &biases,
MATMUL_OP_ADDITION, INT8_MIN,
INT8_MAX, false, true, NULL, &output);
assert(status == ZDNN_OK);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
uint64_t output_data_size = s * m * p * float_size;
void *output_data = malloc(output_data_size);
status = zdnn_transform_origtensor(&output, output_data);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&output);
assert(status == ZDNN_OK);
free(input_data);
free(weights_data);
free(biases_data);
free(output_data);
}
Example of calling the zdnn_quantized_matmul_op with pre_computed=true API (on-the-fly)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021, 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// Sample: Quantized Matmul Pre-Computed on-the-fly
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
/***********************************************************************
*
* Quantized Matmul Pre-Computed on-the-fly:
*
* INPUTS --------------------------------------------------------------
* input | ZDNN_3DS | (s, m, n)
* weights | ZDNN_3DS | (s, n, p)
* input_biases | ZDNN_2DS | (s, p)
*
* OUTPUTS -------------------------------------------------------------
* output | ZDNN_3DS | (s, m, p)
***********************************************************************/
uint32_t s = 2;
uint32_t m = 3;
uint32_t n = 4;
uint32_t p = 5;
short int8_size = 1; // size of each int8 element in bytes
short float_size = 4; // size of each float element in bytes
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &input_pre_tfrmd_desc,
s, m, n);
status = zdnn_generate_quantized_transformed_desc(
&input_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &input_tfrmd_desc);
assert(status == ZDNN_OK);
float input_scale = 1.f;
float input_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc,
input_scale, input_offset,
&input);
assert(status == ZDNN_OK);
uint64_t input_data_size = s * m * n * float_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_ztensor(&input, input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create weights zTensor
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, INT8, &weights_pre_tfrmd_desc,
s, n, p);
status = zdnn_generate_quantized_transformed_desc(
&weights_pre_tfrmd_desc, QUANTIZED_WEIGHTS_INT8, &weights_tfrmd_desc);
assert(status == ZDNN_OK);
float weights_scale = 1.f;
float weights_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc,
weights_scale,
weights_offset, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = s * n * p * int8_size;
void *weights_data = malloc(weights_data_size);
status = zdnn_transform_quantized_ztensor(&weights, false, INT8_MIN, INT8_MAX,
weights_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create pre-computed biases zTensor
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, FP32, &biases_pre_tfrmd_desc,
s, p);
status = zdnn_generate_quantized_transformed_desc(
&biases_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &biases_tfrmd_desc);
assert(status == ZDNN_OK);
float biases_scale = 1.f;
float biases_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc,
biases_scale, biases_offset,
&biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = s * p * float_size;
void *biases_data = malloc(biases_data_size);
status = zdnn_transform_ztensor(&biases, biases_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create output zTensor
***********************************************************************/
zdnn_tensor_desc output_pre_tfrmd_desc, output_tfrmd_desc;
zdnn_ztensor output;
zdnn_init_pre_transformed_desc(ZDNN_3DS, FP32, &output_pre_tfrmd_desc,
s, m, p);
status = zdnn_generate_quantized_transformed_desc(
&output_pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &output_tfrmd_desc);
assert(status == ZDNN_OK);
float output_scale = 1.f;
float output_offset = 0.f;
status = zdnn_init_quantized_ztensor_with_malloc(&output_pre_tfrmd_desc,
&output_tfrmd_desc,
output_scale, output_offset,
&output);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
status = zdnn_quantized_matmul_op(&input, &weights, &biases,
MATMUL_OP_ADDITION, INT8_MIN,
INT8_MAX, false, true, NULL, &output);
assert(status == ZDNN_OK);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
uint64_t output_data_size = s * m * p * float_size;
void *output_data = malloc(output_data_size);
status = zdnn_transform_origtensor(&output, output_data);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&output);
assert(status == ZDNN_OK);
free(input_data);
free(weights_data);
free(biases_data);
free(output_data);
}
Example of an application calling the zdnn_lstm API (forward)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// Sample: LSTM
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
/***********************************************************************
*
* LSTM (FWD/BWD):
*
* INPUTS --------------------------------------------------------------
* input | ZDNN_3DS | (num_timesteps, num_batches, num_features)
* h0 | ZDNN_3DS | (1, num_batches, num_hidden)
* c0 | ZDNN_3DS | (1, num_batches, num_hidden)
* weights | ZDNN_3DS | (1, num_features, num_hidden)
* biases | ZDNN_2DS | (1, num_hidden)
* hidden_weights | ZDNN_3DS | (1, num_hidden, num_hidden)
* hidden_biases | ZDNN_2DS | (1, num_hidden)
*
* OUTPUTS -------------------------------------------------------------
* hn_output | ZDNN_4DS | (num_timesteps, 1, num_batches, num_hidden)
* | | or (1, 1, num_batches, num_hidden)
* cf_output | ZDNN_4DS | (1, 1, num_batches, num_hidden)
***********************************************************************/
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
uint32_t num_timesteps = 5;
uint32_t num_batches = 3;
uint32_t num_features = 32;
uint32_t num_hidden = 5;
zdnn_data_types type = FP32;
short element_size = 4; // size of each element in bytes
lstm_gru_direction dir = FWD;
uint8_t num_dirs = 1;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &input_pre_tfrmd_desc,
num_timesteps, num_batches, num_features);
status =
zdnn_generate_transformed_desc(&input_pre_tfrmd_desc, &input_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc, &input);
assert(status == ZDNN_OK);
uint64_t input_data_size =
num_timesteps * num_batches * num_features * element_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_ztensor(&input, input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create initial hidden and cell state zTensors
***********************************************************************/
zdnn_tensor_desc h0c0_pre_tfrmd_desc, h0c0_tfrmd_desc;
zdnn_ztensor h0, c0;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &h0c0_pre_tfrmd_desc, num_dirs,
num_batches, num_hidden);
status =
zdnn_generate_transformed_desc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc,
&h0);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc,
&c0);
assert(status == ZDNN_OK);
uint64_t h0c0_data_size = num_batches * num_hidden * element_size;
void *hidden_state_data = malloc(h0c0_data_size);
void *cell_state_data = malloc(h0c0_data_size);
status = zdnn_transform_ztensor(&h0, hidden_state_data);
assert(status == ZDNN_OK);
status = zdnn_transform_ztensor(&c0, cell_state_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create input weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &weights_pre_tfrmd_desc,
num_dirs, num_features, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&weights_pre_tfrmd_desc, RNN_TYPE_LSTM | USAGE_WEIGHTS | PREV_LAYER_NONE,
&weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = num_features * num_hidden * element_size;
void *weights_data_f = malloc(weights_data_size);
void *weights_data_i = malloc(weights_data_size);
void *weights_data_c = malloc(weights_data_size);
void *weights_data_o = malloc(weights_data_size);
status = zdnn_transform_ztensor(&weights, weights_data_f, weights_data_i,
weights_data_c, weights_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&biases_pre_tfrmd_desc, RNN_TYPE_LSTM | USAGE_BIASES | PREV_LAYER_NONE,
&biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc, &biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = num_hidden * element_size;
void *biases_data_f = malloc(biases_data_size);
void *biases_data_i = malloc(biases_data_size);
void *biases_data_c = malloc(biases_data_size);
void *biases_data_o = malloc(biases_data_size);
status = zdnn_transform_ztensor(&biases, biases_data_f, biases_data_i,
biases_data_c, biases_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc hidden_weights_pre_tfrmd_desc, hidden_weights_tfrmd_desc;
zdnn_ztensor hidden_weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &hidden_weights_pre_tfrmd_desc,
num_dirs, num_hidden, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_weights_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_HIDDEN_WEIGHTS | PREV_LAYER_NONE,
&hidden_weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hidden_weights_pre_tfrmd_desc,
&hidden_weights_tfrmd_desc,
&hidden_weights);
assert(status == ZDNN_OK);
uint64_t hidden_weights_data_size = num_hidden * num_hidden * element_size;
void *hidden_weights_data_f = malloc(hidden_weights_data_size);
void *hidden_weights_data_i = malloc(hidden_weights_data_size);
void *hidden_weights_data_c = malloc(hidden_weights_data_size);
void *hidden_weights_data_o = malloc(hidden_weights_data_size);
status = zdnn_transform_ztensor(&hidden_weights, hidden_weights_data_f,
hidden_weights_data_i, hidden_weights_data_c,
hidden_weights_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc hidden_biases_pre_tfrmd_desc, hidden_biases_tfrmd_desc;
zdnn_ztensor hidden_biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &hidden_biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_biases_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_HIDDEN_BIASES | PREV_LAYER_NONE,
&hidden_biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(
&hidden_biases_pre_tfrmd_desc, &hidden_biases_tfrmd_desc, &hidden_biases);
assert(status == ZDNN_OK);
uint64_t hidden_biases_data_size = num_hidden * element_size;
void *hidden_biases_data_f = malloc(hidden_biases_data_size);
void *hidden_biases_data_i = malloc(hidden_biases_data_size);
void *hidden_biases_data_c = malloc(hidden_biases_data_size);
void *hidden_biases_data_o = malloc(hidden_biases_data_size);
status = zdnn_transform_ztensor(&hidden_biases, hidden_biases_data_f,
hidden_biases_data_i, hidden_biases_data_c,
hidden_biases_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create output zTensor
***********************************************************************/
// get only the last timestep, thus hn and cf can share descriptor
zdnn_tensor_desc hncf_pre_tfrmd_desc, hncf_tfrmd_desc;
zdnn_ztensor hn_output_ztensor, cf_output_ztensor;
zdnn_init_pre_transformed_desc(ZDNN_4DS, type, &hncf_pre_tfrmd_desc, 1, 1,
num_batches, num_hidden);
status =
zdnn_generate_transformed_desc(&hncf_pre_tfrmd_desc, &hncf_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hncf_pre_tfrmd_desc, &hncf_tfrmd_desc,
&hn_output_ztensor);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hncf_pre_tfrmd_desc, &hncf_tfrmd_desc,
&cf_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
void *work_area = NULL;
status = zdnn_lstm(&input, &h0, &c0, &weights, &biases, &hidden_weights,
&hidden_biases, dir, work_area, &hn_output_ztensor,
&cf_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
uint64_t hncf_data_size = num_batches * num_hidden * element_size;
void *hn_output_data = malloc(hncf_data_size);
void *cf_output_data = malloc(hncf_data_size);
status = zdnn_transform_origtensor(&hn_output_ztensor, hn_output_data);
assert(status == ZDNN_OK);
status = zdnn_transform_origtensor(&cf_output_ztensor, cf_output_data);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&h0);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&c0);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hn_output_ztensor);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&cf_output_ztensor);
assert(status == ZDNN_OK);
free(input_data);
free(hidden_state_data);
free(cell_state_data);
free(weights_data_f);
free(weights_data_i);
free(weights_data_c);
free(weights_data_o);
free(hidden_weights_data_f);
free(hidden_weights_data_i);
free(hidden_weights_data_c);
free(hidden_weights_data_o);
free(biases_data_f);
free(biases_data_i);
free(biases_data_c);
free(biases_data_o);
free(hidden_biases_data_f);
free(hidden_biases_data_i);
free(hidden_biases_data_c);
free(hidden_biases_data_o);
free(hn_output_data);
free(cf_output_data);
}
Example of an application calling the zdnn_lstm API (bi-directional)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// Sample: LSTM BI-DIR
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
/***********************************************************************
*
* LSTM (BI-DIR):
*
* INPUTS --------------------------------------------------------------
* input | ZDNN_3DS | (num_timesteps, num_batches, num_features)
* h0 | ZDNN_3DS | (2, num_batches, num_hidden)
* c0 | ZDNN_3DS | (2, num_batches, num_hidden)
* weights | ZDNN_3DS | (2, num_features, num_hidden)
* biases | ZDNN_2DS | (2, num_hidden)
* hidden_weights | ZDNN_3DS | (2, num_hidden, num_hidden)
* hidden_biases | ZDNN_2DS | (2, num_hidden)
*
* OUTPUTS -------------------------------------------------------------
* hn_output | ZDNN_4DS | (num_timesteps, 2, num_batches, num_hidden)
* | | or (1, 2, num_batches, num_hidden)
* cf_output | ZDNN_4DS | (1, 2, num_batches, num_hidden)
***********************************************************************/
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
uint32_t num_timesteps = 5;
uint32_t num_batches = 3;
uint32_t num_features = 32;
uint32_t num_hidden = 5;
zdnn_data_types type = FP32;
short element_size = 4; // size of each element in bytes
lstm_gru_direction dir = BIDIR;
uint8_t num_dirs = 2;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &input_pre_tfrmd_desc,
num_timesteps, num_batches, num_features);
status =
zdnn_generate_transformed_desc(&input_pre_tfrmd_desc, &input_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc, &input);
assert(status == ZDNN_OK);
uint64_t input_data_size =
num_timesteps * num_batches * num_features * element_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_ztensor(&input, input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create initial hidden and cell state zTensors
***********************************************************************/
zdnn_tensor_desc h0c0_pre_tfrmd_desc, h0c0_tfrmd_desc;
zdnn_ztensor h0, c0;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &h0c0_pre_tfrmd_desc, num_dirs,
num_batches, num_hidden);
status =
zdnn_generate_transformed_desc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc,
&h0);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc,
&c0);
assert(status == ZDNN_OK);
uint64_t h0c0_data_size = num_batches * num_hidden * element_size;
void *hidden_state_data = malloc(h0c0_data_size);
void *cell_state_data = malloc(h0c0_data_size);
status = zdnn_transform_ztensor(&h0, hidden_state_data);
assert(status == ZDNN_OK);
status = zdnn_transform_ztensor(&c0, cell_state_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create input weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &weights_pre_tfrmd_desc,
num_dirs, num_features, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&weights_pre_tfrmd_desc, RNN_TYPE_LSTM | USAGE_WEIGHTS | PREV_LAYER_NONE,
&weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = num_features * num_hidden * element_size;
void *weights_data_f = malloc(weights_data_size);
void *weights_data_i = malloc(weights_data_size);
void *weights_data_c = malloc(weights_data_size);
void *weights_data_o = malloc(weights_data_size);
status = zdnn_transform_ztensor(&weights, weights_data_f, weights_data_i,
weights_data_c, weights_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&biases_pre_tfrmd_desc, RNN_TYPE_LSTM | USAGE_BIASES | PREV_LAYER_NONE,
&biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc, &biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = num_hidden * element_size;
void *biases_data_f = malloc(biases_data_size);
void *biases_data_i = malloc(biases_data_size);
void *biases_data_c = malloc(biases_data_size);
void *biases_data_o = malloc(biases_data_size);
status = zdnn_transform_ztensor(&biases, biases_data_f, biases_data_i,
biases_data_c, biases_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc hidden_weights_pre_tfrmd_desc, hidden_weights_tfrmd_desc;
zdnn_ztensor hidden_weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &hidden_weights_pre_tfrmd_desc,
num_dirs, num_hidden, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_weights_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_HIDDEN_WEIGHTS | PREV_LAYER_NONE,
&hidden_weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hidden_weights_pre_tfrmd_desc,
&hidden_weights_tfrmd_desc,
&hidden_weights);
assert(status == ZDNN_OK);
uint64_t hidden_weights_data_size = num_hidden * num_hidden * element_size;
void *hidden_weights_data_f = malloc(hidden_weights_data_size);
void *hidden_weights_data_i = malloc(hidden_weights_data_size);
void *hidden_weights_data_c = malloc(hidden_weights_data_size);
void *hidden_weights_data_o = malloc(hidden_weights_data_size);
status = zdnn_transform_ztensor(&hidden_weights, hidden_weights_data_f,
hidden_weights_data_i, hidden_weights_data_c,
hidden_weights_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc hidden_biases_pre_tfrmd_desc, hidden_biases_tfrmd_desc;
zdnn_ztensor hidden_biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &hidden_biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_biases_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_HIDDEN_BIASES | PREV_LAYER_NONE,
&hidden_biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(
&hidden_biases_pre_tfrmd_desc, &hidden_biases_tfrmd_desc, &hidden_biases);
assert(status == ZDNN_OK);
uint64_t hidden_biases_data_size = num_hidden * element_size;
void *hidden_biases_data_f = malloc(hidden_biases_data_size);
void *hidden_biases_data_i = malloc(hidden_biases_data_size);
void *hidden_biases_data_c = malloc(hidden_biases_data_size);
void *hidden_biases_data_o = malloc(hidden_biases_data_size);
status = zdnn_transform_ztensor(&hidden_biases, hidden_biases_data_f,
hidden_biases_data_i, hidden_biases_data_c,
hidden_biases_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create output zTensor
***********************************************************************/
zdnn_tensor_desc hn_pre_tfrmd_desc, hn_tfrmd_desc, cf_pre_tfrmd_desc,
cf_tfrmd_desc;
zdnn_ztensor hn_output_ztensor, cf_output_ztensor;
zdnn_init_pre_transformed_desc(ZDNN_4DS, type, &hn_pre_tfrmd_desc,
num_timesteps, 2, num_batches, num_hidden);
status = zdnn_generate_transformed_desc(&hn_pre_tfrmd_desc, &hn_tfrmd_desc);
assert(status == ZDNN_OK);
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &cf_pre_tfrmd_desc, 1, 2,
num_batches, num_hidden);
status = zdnn_generate_transformed_desc(&cf_pre_tfrmd_desc, &cf_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hn_pre_tfrmd_desc, &hn_tfrmd_desc,
&hn_output_ztensor);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&cf_pre_tfrmd_desc, &cf_tfrmd_desc,
&cf_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
void *work_area = NULL;
status = zdnn_lstm(&input, &h0, &c0, &weights, &biases, &hidden_weights,
&hidden_biases, dir, work_area, &hn_output_ztensor,
&cf_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
uint64_t hn_data_size =
num_timesteps * 2 * num_batches * num_hidden * element_size;
uint64_t cf_data_size = 2 * num_batches * num_hidden * element_size;
void *hn_output_data = malloc(hn_data_size);
void *cf_output_data = malloc(cf_data_size);
status = zdnn_transform_origtensor(&hn_output_ztensor, hn_output_data);
assert(status == ZDNN_OK);
status = zdnn_transform_origtensor(&cf_output_ztensor, cf_output_data);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&h0);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&c0);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hn_output_ztensor);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&cf_output_ztensor);
assert(status == ZDNN_OK);
free(input_data);
free(hidden_state_data);
free(cell_state_data);
free(weights_data_f);
free(weights_data_i);
free(weights_data_c);
free(weights_data_o);
free(hidden_weights_data_f);
free(hidden_weights_data_i);
free(hidden_weights_data_c);
free(hidden_weights_data_o);
free(biases_data_f);
free(biases_data_i);
free(biases_data_c);
free(biases_data_o);
free(hidden_biases_data_f);
free(hidden_biases_data_i);
free(hidden_biases_data_c);
free(hidden_biases_data_o);
free(hn_output_data);
free(cf_output_data);
}
Example of an application calling the zdnn_lstm API
Example of an application calling the zdnn_lstm API (multi-layer bi-directional)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
void do_bidir_layer(zdnn_ztensor *input, uint32_t num_hidden,
zdnn_ztensor *hn_output, bool is_prev_layer_bidir) {
zdnn_status status;
uint32_t num_batches = input->pre_transformed_desc->dim2;
// if input is bidir output from previous layer then number of features for
// this layer is 2x of hidden-state size (dim1) of the previous layer
uint32_t num_features =
input->pre_transformed_desc->dim1 * (is_prev_layer_bidir ? 2 : 1);
zdnn_data_types type = FP32;
short element_size = 4; // size of each element in bytes
lstm_gru_direction dir = BIDIR;
uint8_t num_dirs = 2;
/***********************************************************************
* Create initial hidden and cell state zTensors
***********************************************************************/
zdnn_tensor_desc h0c0_pre_tfrmd_desc, h0c0_tfrmd_desc;
zdnn_ztensor h0, c0;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &h0c0_pre_tfrmd_desc, num_dirs,
num_batches, num_hidden);
status =
zdnn_generate_transformed_desc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc,
&h0);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&h0c0_pre_tfrmd_desc, &h0c0_tfrmd_desc,
&c0);
assert(status == ZDNN_OK);
uint64_t h0c0_data_size = num_batches * num_hidden * element_size;
void *hidden_state_data = malloc(h0c0_data_size);
void *cell_state_data = malloc(h0c0_data_size);
status = zdnn_transform_ztensor(&h0, hidden_state_data);
assert(status == ZDNN_OK);
status = zdnn_transform_ztensor(&c0, cell_state_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create input weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
// if using previous layer bidir output as input then number of features of
// this layer is
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &weights_pre_tfrmd_desc,
num_dirs, num_features, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&weights_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_WEIGHTS |
(is_prev_layer_bidir ? PREV_LAYER_BIDIR : PREV_LAYER_UNI),
&weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = num_features * num_hidden * element_size;
void *weights_data_f = malloc(weights_data_size);
void *weights_data_i = malloc(weights_data_size);
void *weights_data_c = malloc(weights_data_size);
void *weights_data_o = malloc(weights_data_size);
status = zdnn_transform_ztensor(&weights, weights_data_f, weights_data_i,
weights_data_c, weights_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&biases_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_BIASES |
(is_prev_layer_bidir ? PREV_LAYER_BIDIR : PREV_LAYER_UNI),
&biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc, &biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = num_hidden * element_size;
void *biases_data_f = malloc(biases_data_size);
void *biases_data_i = malloc(biases_data_size);
void *biases_data_c = malloc(biases_data_size);
void *biases_data_o = malloc(biases_data_size);
status = zdnn_transform_ztensor(&biases, biases_data_f, biases_data_i,
biases_data_c, biases_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc hidden_weights_pre_tfrmd_desc, hidden_weights_tfrmd_desc;
zdnn_ztensor hidden_weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &hidden_weights_pre_tfrmd_desc,
num_dirs, num_hidden, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_weights_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_HIDDEN_WEIGHTS |
(is_prev_layer_bidir ? PREV_LAYER_BIDIR : PREV_LAYER_UNI),
&hidden_weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hidden_weights_pre_tfrmd_desc,
&hidden_weights_tfrmd_desc,
&hidden_weights);
assert(status == ZDNN_OK);
uint64_t hidden_weights_data_size = num_hidden * num_hidden * element_size;
void *hidden_weights_data_f = malloc(hidden_weights_data_size);
void *hidden_weights_data_i = malloc(hidden_weights_data_size);
void *hidden_weights_data_c = malloc(hidden_weights_data_size);
void *hidden_weights_data_o = malloc(hidden_weights_data_size);
status = zdnn_transform_ztensor(&hidden_weights, hidden_weights_data_f,
hidden_weights_data_i, hidden_weights_data_c,
hidden_weights_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc hidden_biases_pre_tfrmd_desc, hidden_biases_tfrmd_desc;
zdnn_ztensor hidden_biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &hidden_biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_biases_pre_tfrmd_desc,
RNN_TYPE_LSTM | USAGE_HIDDEN_BIASES |
(is_prev_layer_bidir ? PREV_LAYER_BIDIR : PREV_LAYER_UNI),
&hidden_biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(
&hidden_biases_pre_tfrmd_desc, &hidden_biases_tfrmd_desc, &hidden_biases);
assert(status == ZDNN_OK);
uint64_t hidden_biases_data_size = num_hidden * element_size;
void *hidden_biases_data_f = malloc(hidden_biases_data_size);
void *hidden_biases_data_i = malloc(hidden_biases_data_size);
void *hidden_biases_data_c = malloc(hidden_biases_data_size);
void *hidden_biases_data_o = malloc(hidden_biases_data_size);
status = zdnn_transform_ztensor(&hidden_biases, hidden_biases_data_f,
hidden_biases_data_i, hidden_biases_data_c,
hidden_biases_data_o);
assert(status == ZDNN_OK);
/***********************************************************************
* Create cf output zTensor
***********************************************************************/
zdnn_tensor_desc cf_pre_tfrmd_desc, cf_tfrmd_desc;
zdnn_ztensor cf_output_ztensor;
zdnn_init_pre_transformed_desc(ZDNN_4DS, type, &cf_pre_tfrmd_desc, 1, 2,
num_batches, num_hidden);
status = zdnn_generate_transformed_desc(&cf_pre_tfrmd_desc, &cf_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&cf_pre_tfrmd_desc, &cf_tfrmd_desc,
&cf_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
void *work_area = NULL;
status =
zdnn_lstm(input, &h0, &c0, &weights, &biases, &hidden_weights,
&hidden_biases, dir, work_area, hn_output, &cf_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Cleanup and Return
***********************************************************************/
status = zdnn_free_ztensor_buffer(&h0);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&c0);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&cf_output_ztensor);
assert(status == ZDNN_OK);
free(hidden_state_data);
free(cell_state_data);
free(weights_data_f);
free(weights_data_i);
free(weights_data_c);
free(weights_data_o);
free(hidden_weights_data_f);
free(hidden_weights_data_i);
free(hidden_weights_data_c);
free(hidden_weights_data_o);
free(biases_data_f);
free(biases_data_i);
free(biases_data_c);
free(biases_data_o);
free(hidden_biases_data_f);
free(hidden_biases_data_i);
free(hidden_biases_data_c);
free(hidden_biases_data_o);
}
// Sample: LSTM multi-layer BIDIR
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
uint32_t num_hidden[2] = {5, 4};
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
uint32_t num_timesteps = 5;
uint32_t num_batches = 3;
uint32_t num_features = 32;
zdnn_data_types type = FP32;
short element_size = 4; // size of each element in bytes
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &input_pre_tfrmd_desc,
num_timesteps, num_batches, num_features);
status =
zdnn_generate_transformed_desc(&input_pre_tfrmd_desc, &input_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc, &input);
assert(status == ZDNN_OK);
uint64_t input_data_size =
num_timesteps * num_batches * num_features * element_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_ztensor(&input, input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create 2 hn output zTensors
***********************************************************************/
zdnn_tensor_desc hn_pre_tfrmd_desc[2], hn_tfrmd_desc[2];
zdnn_ztensor hn_output[2];
for (int i = 0; i < 2; i++) {
zdnn_init_pre_transformed_desc(ZDNN_4DS, type, &hn_pre_tfrmd_desc[i],
num_timesteps, 2, num_batches,
num_hidden[i]);
status = zdnn_generate_transformed_desc(&hn_pre_tfrmd_desc[i],
&hn_tfrmd_desc[i]);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hn_pre_tfrmd_desc[i],
&hn_tfrmd_desc[i], &hn_output[i]);
assert(status == ZDNN_OK);
}
/***********************************************************************
* Do the layers
***********************************************************************/
// call the first layer with input, previous layer bidir = false, output goes
// to hn_output[0]
do_bidir_layer(&input, num_hidden[0], &hn_output[0], false);
// call the second layer with hn_output[0] from layer 1, previous layer bidir
// = true, output goes to hn_output[1]
do_bidir_layer(&hn_output[0], num_hidden[1], &hn_output[1], true);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
void *hn_output_data[2];
for (int i = 0; i < 2; i++) {
uint64_t hn_output_data_size = (uint64_t)num_timesteps * num_batches *
num_hidden[i] * 2 * element_size;
hn_output_data[i] = malloc(hn_output_data_size);
status = zdnn_transform_origtensor(&hn_output[i], hn_output_data[i]);
assert(status == ZDNN_OK);
}
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hn_output[0]);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hn_output[1]);
assert(status == ZDNN_OK);
free(input_data);
free(hn_output_data[0]);
free(hn_output_data[1]);
}
Example of an application calling the zdnn_gru API
Example of an application calling the zdnn_gru API (forward)
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// Sample: GRU
int main(int argc, char *argv[]) {
zdnn_status status;
#ifdef STATIC_LIB
zdnn_init();
#endif
/***********************************************************************
*
* GRU (FWD/BWD):
*
* INPUTS --------------------------------------------------------------
* input | ZDNN_3DS | (num_timesteps, num_batches, num_features)
* h0 | ZDNN_3DS | (1, num_batches, num_hidden)
* weights | ZDNN_3DS | (1, num_features, num_hidden)
* input_biases | ZDNN_2DS | (1, num_hidden)
* hidden_weights | ZDNN_3DS | (1, num_hidden, num_hidden)
* hidden_biases | ZDNN_2DS | (1, num_hidden)
*
* OUTPUTS -------------------------------------------------------------
* hn_output | ZDNN_4DS | (num_timesteps, 1, num_batches, num_hidden)
* | | or (1, 1, num_batches, num_hidden)
***********************************************************************/
/***********************************************************************
* Create input zTensor
***********************************************************************/
zdnn_tensor_desc input_pre_tfrmd_desc, input_tfrmd_desc;
zdnn_ztensor input;
uint32_t num_timesteps = 5;
uint32_t num_batches = 3;
uint32_t num_features = 32;
uint32_t num_hidden = 5;
zdnn_data_types type = FP32;
short element_size = 4; // size of each element in bytes
lstm_gru_direction dir = FWD;
uint8_t num_dirs = 1;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &input_pre_tfrmd_desc,
num_timesteps, num_batches, num_features);
status =
zdnn_generate_transformed_desc(&input_pre_tfrmd_desc, &input_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&input_pre_tfrmd_desc,
&input_tfrmd_desc, &input);
assert(status == ZDNN_OK);
uint64_t input_data_size =
num_timesteps * num_batches * num_features * element_size;
void *input_data = malloc(input_data_size);
status = zdnn_transform_ztensor(&input, input_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create initial hidden zTensor
***********************************************************************/
zdnn_tensor_desc h0_pre_tfrmd_desc, h0_tfrmd_desc;
zdnn_ztensor h0;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &h0_pre_tfrmd_desc, num_dirs,
num_batches, num_hidden);
status = zdnn_generate_transformed_desc(&h0_pre_tfrmd_desc, &h0_tfrmd_desc);
assert(status == ZDNN_OK);
status =
zdnn_init_ztensor_with_malloc(&h0_pre_tfrmd_desc, &h0_tfrmd_desc, &h0);
assert(status == ZDNN_OK);
uint64_t h0_data_size = num_batches * num_hidden * element_size;
void *hidden_state_data = malloc(h0_data_size);
status = zdnn_transform_ztensor(&h0, hidden_state_data);
assert(status == ZDNN_OK);
/***********************************************************************
* Create input weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc weights_pre_tfrmd_desc, weights_tfrmd_desc;
zdnn_ztensor weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &weights_pre_tfrmd_desc,
num_dirs, num_features, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&weights_pre_tfrmd_desc, RNN_TYPE_GRU | USAGE_WEIGHTS | PREV_LAYER_NONE,
&weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&weights_pre_tfrmd_desc,
&weights_tfrmd_desc, &weights);
assert(status == ZDNN_OK);
uint64_t weights_data_size = num_features * num_hidden * element_size;
void *weights_data_z = malloc(weights_data_size);
void *weights_data_r = malloc(weights_data_size);
void *weights_data_h = malloc(weights_data_size);
status = zdnn_transform_ztensor(&weights, weights_data_z, weights_data_r,
weights_data_h);
assert(status == ZDNN_OK);
/***********************************************************************
* Create biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc biases_pre_tfrmd_desc, biases_tfrmd_desc;
zdnn_ztensor biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&biases_pre_tfrmd_desc, RNN_TYPE_GRU | USAGE_BIASES | PREV_LAYER_NONE,
&biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&biases_pre_tfrmd_desc,
&biases_tfrmd_desc, &biases);
assert(status == ZDNN_OK);
uint64_t biases_data_size = num_hidden * element_size;
void *biases_data_z = malloc(biases_data_size);
void *biases_data_r = malloc(biases_data_size);
void *biases_data_h = malloc(biases_data_size);
status = zdnn_transform_ztensor(&biases, biases_data_z, biases_data_r,
biases_data_h);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden weights zTensor
* Resultant zTensor is concatenated
***********************************************************************/
zdnn_tensor_desc hidden_weights_pre_tfrmd_desc, hidden_weights_tfrmd_desc;
zdnn_ztensor hidden_weights;
zdnn_init_pre_transformed_desc(ZDNN_3DS, type, &hidden_weights_pre_tfrmd_desc,
num_dirs, num_hidden, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_weights_pre_tfrmd_desc,
RNN_TYPE_GRU | USAGE_HIDDEN_WEIGHTS | PREV_LAYER_NONE,
&hidden_weights_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hidden_weights_pre_tfrmd_desc,
&hidden_weights_tfrmd_desc,
&hidden_weights);
assert(status == ZDNN_OK);
uint64_t hidden_weights_data_size = num_hidden * num_hidden * element_size;
void *hidden_weights_data_z = malloc(hidden_weights_data_size);
void *hidden_weights_data_r = malloc(hidden_weights_data_size);
void *hidden_weights_data_h = malloc(hidden_weights_data_size);
status = zdnn_transform_ztensor(&hidden_weights, hidden_weights_data_z,
hidden_weights_data_r, hidden_weights_data_h);
assert(status == ZDNN_OK);
/***********************************************************************
* Create hidden biases zTensors
* Resultant zTensors are concatenated
***********************************************************************/
zdnn_tensor_desc hidden_biases_pre_tfrmd_desc, hidden_biases_tfrmd_desc;
zdnn_ztensor hidden_biases;
zdnn_init_pre_transformed_desc(ZDNN_2DS, type, &hidden_biases_pre_tfrmd_desc,
num_dirs, num_hidden);
status = zdnn_generate_transformed_desc_concatenated(
&hidden_biases_pre_tfrmd_desc,
RNN_TYPE_GRU | USAGE_HIDDEN_BIASES | PREV_LAYER_NONE,
&hidden_biases_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(
&hidden_biases_pre_tfrmd_desc, &hidden_biases_tfrmd_desc, &hidden_biases);
assert(status == ZDNN_OK);
uint64_t hidden_biases_data_size = num_hidden * element_size;
void *hidden_biases_data_z = malloc(hidden_biases_data_size);
void *hidden_biases_data_r = malloc(hidden_biases_data_size);
void *hidden_biases_data_h = malloc(hidden_biases_data_size);
status = zdnn_transform_ztensor(&hidden_biases, hidden_biases_data_z,
hidden_biases_data_r, hidden_biases_data_h);
assert(status == ZDNN_OK);
/***********************************************************************
* Create output zTensor
***********************************************************************/
// get only the last timestep
zdnn_tensor_desc hn_pre_tfrmd_desc, hn_tfrmd_desc;
zdnn_ztensor hn_output_ztensor;
zdnn_init_pre_transformed_desc(ZDNN_4DS, type, &hn_pre_tfrmd_desc, 1, 1,
num_batches, num_hidden);
status = zdnn_generate_transformed_desc(&hn_pre_tfrmd_desc, &hn_tfrmd_desc);
assert(status == ZDNN_OK);
status = zdnn_init_ztensor_with_malloc(&hn_pre_tfrmd_desc, &hn_tfrmd_desc,
&hn_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Call the zAIU
***********************************************************************/
void *work_area = NULL;
status = zdnn_gru(&input, &h0, &weights, &biases, &hidden_weights,
&hidden_biases, dir, work_area, &hn_output_ztensor);
assert(status == ZDNN_OK);
/***********************************************************************
* Output and Cleanup
***********************************************************************/
uint64_t hn_data_size = num_batches * num_hidden * element_size;
void *hn_output_data = malloc(hn_data_size);
status = zdnn_transform_origtensor(&hn_output_ztensor, hn_output_data);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&input);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&h0);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_weights);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hidden_biases);
assert(status == ZDNN_OK);
status = zdnn_free_ztensor_buffer(&hn_output_ztensor);
assert(status == ZDNN_OK);
free(input_data);
free(hidden_state_data);
free(weights_data_z);
free(weights_data_r);
free(weights_data_h);
free(hidden_weights_data_z);
free(hidden_weights_data_r);
free(hidden_weights_data_h);
free(biases_data_z);
free(biases_data_r);
free(biases_data_h);
free(hidden_biases_data_z);
free(hidden_biases_data_r);
free(hidden_biases_data_h);
free(hn_output_data);
}
Example of an application creating a quantized ztensor
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zdnn.h"
// ***************************************************************************
// Sample:
//
// Create a quantized zTensors
// ***************************************************************************
int main(int argc, char *argv[]) {
zdnn_tensor_desc pre_tfrmd_desc, tfrmd_desc;
zdnn_ztensor ztensor;
zdnn_status status;
uint32_t dim_n = 1, dim_h = 32, dim_w = 32, dim_c = 3;
zdnn_data_types type = FP32;
short element_size = 4; // size of each element in bytes
uint64_t num_elements = dim_n * dim_h * dim_w * dim_c;
// allocate tensor data storage
void *data1 = malloc(num_elements * element_size);
// read input_data
// check status for zAIU availability, supported ops, etc. here
// status = zdnn_query();
// set input tensor data to 0 to 127 sequentially and repeat
for (uint64_t i = 0; i < num_elements; i++) {
((float *)data1)[i] = (float)(i & 0x7f);
}
zdnn_init_pre_transformed_desc(ZDNN_NHWC, type, &pre_tfrmd_desc, dim_n, dim_h,
dim_w, dim_c);
float scale = 3;
float offset = 2;
// generate transformed shape information
status = zdnn_generate_quantized_transformed_desc(
&pre_tfrmd_desc, QUANTIZED_DLFLOAT16, &tfrmd_desc);
assert(status == ZDNN_OK);
// initialize zTensors and allocate 4k-aligned storage via helper function
status = zdnn_init_quantized_ztensor_with_malloc(&pre_tfrmd_desc, &tfrmd_desc,
scale, offset, &ztensor);
assert(status == ZDNN_OK);
// transform the feature tensor
status = zdnn_transform_ztensor(&ztensor, data1);
assert(status == ZDNN_OK);
// Free zTensors
status = zdnn_free_ztensor_buffer(&ztensor);
assert(status == ZDNN_OK);
free(data1);
}