Driver API#
Warning
The Driver API is for extremely advanced use-cases only! All users should use the Accelerator API instead.
Please refer to this page only if explicitly told to do so by MemryX customer engineering teams.
Danger
Seriously, please don’t try to use this API unless you know what you’re doing.
Terminology
The terms ‘model context’ and ‘MXA device group’ are introduced in the driver.
/**
* +------------+ PCIE +------+ +------+
* | |--------------->| MX3 |-----| MX3 |
* | | +------+ +------+
* | | (MXA device group 0)
* | |
* | Host |
* | | PCIE +------+ +------+
* | |--------------->| MX3 |-----| MX3 |
* | | +------+ +------+
* | | (MXA device group 1)
* +------------+
*/
- MXA device group (group_id):
Hardware device (e.g., MX3) connected to host PC. No matter how many devices are cascaded (1x, 2x, 4x, 8x) in a chain, there is always one device node per chain on the host PC. We called the cascaded/chained devices an MXA device group, and we used hardware pins to assign different group IDs to each MXA device group. The group ID is later used for the driver to access the target MXA device group. When multi-threading, locks apply at the MXA device group level.
- Model context (model_id):
Model context is a software resource (model’s parameters and buffers) stored within the driver on the host PC. A specific MXA device group (hardware resource) is required to be given to run inference to create a new model context. However, the model context is not limited to the first bound MXA device group and can be re-configured to different MXA device groups later.
Constants
-
MEMX_DEVICE_CASCADE_PLUS#
MemryX device MX3:Cascade+. Constant value which should be referenced only and not be modified manually.
Options
Model download options
-
MEMX_DOWNLOAD_TYPE_WTMEM#
Option of
memx_download_model()to download weight memory only to device. Can be used together withMEMX_DOWNLOAD_TYPE_MODEL.
-
MEMX_DOWNLOAD_TYPE_MODEL#
Option of
memx_download_model()to download model only to device. Can be used together withMEMX_DOWNLOAD_TYPE_WTMEM.
-
MEMX_DOWNLOAD_TYPE_WTMEM_AND_MODEL#
Option of
memx_download_model()to download both weight memory and model to device. The same effect as usingMEMX_DOWNLOAD_TYPE_WTMEMandMEMX_DOWNLOAD_TYPE_MODELtogether.
Macros
-
memx_status_no_error(_status_)#
Helper macro to check if there is no error occurs.
-
memx_status_error(_status_)#
Helper macro to check if there is any error occurs.
Functions
-
memx_status memx_lock(uint8_t dev_id)#
Acquires mutex lock from driver. To be noticed, this function will not actually lock any operation but only acquire the unique mutex within driver. Lock is only required in multi-threading or multi-processing case to avoid driver access conflict when multiple processes trying to re-configure the same hardware device. Also, this function will block until mutex is acquired successfully. See
memx_trylock()for non-blocking mutex lock.// lock MPU device 0 memx_lock(0); printf("lock success.\n"); // always remember to release lock finally memx_unlock(0);
See also
Advanced Driver Usage: Example 3: Runtime Model Swap
- Parameters:
dev_id – device ID
- Returns:
0 on success, otherwise error code
-
memx_status memx_trylock(uint8_t dev_id)#
Acquires mutex lock from driver. Apart from
memx_lock(), this function returns immediately if fail to acquire lock.// do something only if lock MPU device 0 successfully if (memx_trylock(0) == 0) { printf("lock success.\n"); // always remember to release lock finally memx_unlock(0); } else { printf("lock failure.\n"); }
- Parameters:
dev_id – device ID
- Returns:
0 on success, otherwise error code
-
memx_status memx_unlock(uint8_t dev_id)#
Releases the mutex lock acquired from driver. Be careful and not to unexpectedly release mutex which is not locked by current process. Uses
memx_lock()andmemx_trylock()to acquire lock first.- Parameters:
dev_id – device ID
- Returns:
0 on success, otherwise error code
-
memx_status memx_open(uint8_t dev_id, float chip_gen)#
Open new device context with specified MPU device driver. Internal reference count increases if the same device ID is re-used and opened multiple times. All operations later using this device ID will use the binded MPU device driver. Currently, at most 128 device contexts (device ID = 0 ~ 127) can be stored within driver.
// open device 0 as MX3 memx_status status = memx_open(0, MEMX_DEVICE_CASCADE_PLUS); if (status == 0) { printf("open success.\n"); } else { printf("open failure.\n"); } // always remember to close device finally memx_close(0);
See also
Classification (Driver APIs): Basic Inference
- Parameters:
dev_id – device ID
chip_gen – Deprecated, user don’t care. device chip_gen was detected by driver
- Returns:
0 on success, otherwise error code
-
memx_status memx_close(uint8_t dev_id)#
Close current connected MPU device and destroy device context stored in driver. If device context is opened multiple times, calling close will decrease internal reference count one each time. Only after reference count goes down to zero will device context finally be released. So make sure to use equivalent times of open and close to clean-up driver resources. Otherwise, it is safe to call close device ID which is not opened or is closed already. See
memx_open()for more information.- Parameters:
dev_id – device ID
- Returns:
0 on success, otherwise error code
-
memx_status memx_set_mpu_count(uint8_t dev_id, memx_mpu_count_t mpu_count)#
Set MPU count on the device, support MEMX_*_MPUS values.
- Parameters:
dev_id – device ID
mpu_count – MPU count config
- Returns:
0 on success, otherwise error code
-
memx_status memx_get_chip_gen(uint8_t dev_id, uint8_t *chip_gen)#
Read back the chip gen for the currently active model.
- Parameters:
dev_id – device ID
chip_gen – chip gen of device (MEMX_MPU_CHIP_GEN_CASCADE_PLUS)
- Returns:
0 on success, otherwise error code
-
memx_status memx_download_model(uint8_t dev_id, const char *file_path, uint8_t model_idx, int32_t type)#
Download weight memory and model to device based on given download type selection. If both weight memory and model download are required, weight memory download will be performed before model download. After model download is completed, input and output feature map shape will be configured to driver to allocate resources automatically. After model download and before data can be transferred to device,
memx_set_stream_enable()must be called to change driver internal state from idle to data-transfer.// bind MPU device 0 as MX3 to model 0 memx_status status = memx_open(0, MEMX_DEVICE_CASCADE_PLUS); if (status == 0) { // download weight memory and 1st model within DFP file to device status = memx_download_model(0, "model.dfp", 0, MEMX_DOWNLOAD_TYPE_WTMEM_AND_MODEL); } // always remember to close device finally memx_close(0);
// download weight memory and 1st model within DFP file to device separately // same effect as example using type MEMX_DOWNLOAD_TYPE_WTMEM_AND_MODEL // bind MPU device 0 as MX3 to model 0 memx_status status = memx_open(0, MEMX_DEVICE_CASCADE_PLUS); if (status == 0) { // download weight memory to device, model index within DFP will be ignored status = memx_download_model(0, "model.dfp", 0, MEMX_DOWNLOAD_TYPE_WTMEM); } if (status == 0) { // download 1st model within DFP file to device status = memx_download_model(0, "model.dfp", 0, MEMX_DOWNLOAD_TYPE_MODEL); } // always remember to close device finally memx_close(0);
- Parameters:
dev_id – device ID
file_path – DFP file path
model_idx – model index within DFP file, give ‘0’ if there is only one model compiled in DFP file. model index will be ignored if download type selection is weight memory only
type – 0: ignored, 1: weight memory only, 2: model only, 3: both weight memory and model
- Returns:
0 on success, otherwise error code
-
memx_status memx_set_stream_enable(uint8_t dev_id, int32_t wait)#
Enable all input and output data to be transferred to device of specified model. When device context is first initialized, all data transfers are blocked (but model download is allowed) to avoid data being transferred to device before model download is finished.
// bind MPU device 0 as MX3 to model 0 memx_status status = memx_open(0, MEMX_DEVICE_CASCADE_PLUS); if (status == 0) { // download weight memory and 1st model within DFP file to device status = memx_download_model(0, "model.dfp", 0, MEMX_DOWNLOAD_TYPE_WTMEM_AND_MODEL); } if (status == 0) { // enable all data being transferred to device status = memx_set_stream_enable(0, 0); } // always remember to close device finally memx_close(0);
- Parameters:
dev_id – device ID
wait – wait until driver state change from idle to data-transfer complete
- Returns:
0 on success, otherwise error code
-
memx_status memx_set_stream_disable(uint8_t dev_id, int32_t wait)#
Disable all input and output data to be transferred to device of specified model. This function is used to guarantee no data from specified model will be transferred to device in case multiple models are sharing the the same hardware resource and want to transfer data in the same time. Use
memx_set_stream_enable()to resume data transfer of specified model.- Parameters:
dev_id – device ID
wait – wait until driver state change from data-transfer to idle complete
- Returns:
0 on success, otherwise error code
-
memx_status memx_get_ifmap_size(uint8_t dev_id, uint8_t flow_id, int32_t *height, int32_t *width, int32_t *z, int32_t *channel_number, int32_t *format)#
Read back input feature map shape configured of specific flow (port). Returns failure with variables set to all zeros in case given flow is not configured.
// bind MPU device 0 as MX3 to model 0 memx_status status = memx_open(0, MEMX_DEVICE_CASCADE_PLUS); if (status == 0) { // download weight memory and 1st model within DFP file to device status = memx_download_model(0, "model.dfp", 0, MEMX_DOWNLOAD_TYPE_WTMEM_AND_MODEL); } if (status == 0) { // read back flow 0 (port 0) input feature map shape for debug information int32_t height, width, z, channel_number, format; memx_get_ifmap_size(0, 0, &height, &width, &z, &channel_number, &format); // for example: shape = (224, 224, 1, 3), format = 1 printf("shape = (%d, %d, %d, %d), format = %d\n", height, width, z, channel_number, format); } // always remember to close device finally memx_close(0);
- Parameters:
dev_id – device ID
flow_id – input flow (port) ID
height – input feature map height
width – input feature map width
z – input feature map z
channel_number – input feature map channel number
format – input feature map format
- Returns:
0 on success, otherwise error code
-
memx_status memx_get_ofmap_size(uint8_t dev_id, uint8_t flow_id, int32_t *height, int32_t *width, int32_t *z, int32_t *channel_number, int32_t *format)#
Read back output feature map shape configured of specific flow (port). Returns failure with variables set to all zeros in case given flow is not configured.
// bind MPU device 0 as MX3 to model 0 memx_status status = memx_open(0, MEMX_DEVICE_CASCADE_PLUS); if (status == 0) { // download weight memory and 1st model within DFP file to device status = memx_download_model(0, "model.dfp", 0, MEMX_DOWNLOAD_TYPE_WTMEM_AND_MODEL); } if (status == 0) { // read back flow 0 (port 0) input feature map shape for debug information int32_t height, width, z, channel_number, format; memx_get_ofmap_size(0, 0, &height, &width, &z, &channel_number, &format); // for example: shape = (224, 224, 1, 3), format = 1 printf("shape = (%d, %d, %d, %d), format = %d\n", height, width, z, channel_number, format); } // always remember to close device finally memx_close(0);
- Parameters:
dev_id – device ID
flow_id – output flow (port) ID
height – output feature map height
width – output feature map width
z – output feature map z
channel_number – output feature map channel number
format – output feature map format
- Returns:
0 on success, otherwise error code
-
memx_status memx_stream_ifmap(uint8_t dev_id, uint8_t flow_id, void *ifmap, int32_t timeout)#
Data channel write input feature map of specified flow (port) to device. This function must be called after
memx_download_model().memx_status status = MEMX_STATUS_OK; if (status == 0) { // write data to model 0 flow 0 (port 0) to run inference status = memx_stream_ifmap(0, 0, ifmap, 200); }
- Parameters:
dev_id – device ID
flow_id – input flow (port) ID
ifmap – input feature map (frame)
timeout – milliseconds timeout, ‘0’ indicates infinite
- Returns:
0 on success, otherwise error code
-
memx_status memx_stream_ofmap(uint8_t dev_id, uint8_t flow_id, void *ofmap, int32_t timeout)#
Data channel read output feature map of specified flow (port) from device. This function must be called after
memx_download_model().memx_status status = MEMX_STATUS_OK; if (status == 0) { // read data from model 0 flow 0 (port 0) as inference result status = memx_stream_ofmap(0, 0, ofmap, 200); }
- Parameters:
dev_id – device ID
flow_id – output flow (port) ID
ofmap – output feature map (frame)
timeout – milliseconds timeout, ‘0’ indicates infinite
- Returns:
0 on success, otherwise error code
Functions
- class mxa#
MemryX driver.
The interface to the MemryX driver. This class wraps MemryX driver C API to Python.
- lock(dev_id: int)#
Acquires mutex lock from driver. To be noticed, this function will not actually lock any operation but only acquire the unique mutex within driver. In multi-threads programming, user can use this lock to prevent driver access conflict. Also, this function will block until mutex is acquired.
- Parameters:
- dev_idint
Device identifier (device index)
- unlock(dev_id: int)#
Releases the mutex lock acquired from driver. Be careful and not to unexpectedly release mutex which is not locked by current process (thread).
- Parameters:
- dev_idint
Device identifier (device index)
- open(dev_id: int, chip_gen: int)#
Open device with interface auto-detected. Currently only one device connected to host is supported within this version implementation. Even in case chips are cascaded to compose data flow, there is only one device can be seen within host.
- Parameters:
- dev_idint
Device identifier (device index)
- chip_genint
Target system Casade = 3
- close(dev_id: int)#
Close current connected device and interface in use. This function returns always no error even if device is not opened.
- Parameters:
- dev_idint
Device identifier (device index)
- download(dev_id: int, file_path: str, dev_idx: int, type: int)#
Download weight memory and model to device based on given download type selection. If both weight memory and model download are required, weight memory download will be performed before model download. Also after model is downloaded, input and output feature map shape will be configured to driver automatically.
- Parameters:
- dev_idint
Device identifier (device index)
- file_pathstring
DFP file path
- dev_idxint
Which rgcfg to use (used in model swapping). Set to 0 if only 1 model.
- typeint
Target model type (any of the following)
mxa.download_type_wtmem/1mxa.download_type_model/2mxa.download_type_wtmem_and_model/3
- set_stream_enable(dev_id: int, wait: int)#
Enable all input and output data flows of this MPU context to interface driver.
- Parameters:
- dev_idint
Device identifier (device index)
- waitbool
Wait until state changed or not
- set_stream_disable(dev_id: int, wait: int)#
Disable all input and output data flows of this MPU context to interface driver.
- Parameters:
- dev_idint
Device identifier (device index)
- waitbool
Wait until state changed or not
- stream_ifmap(dev_id: int, flow_id: int, ifmap: ndarray, timeout: int = 0)#
Data channel write frame to device.
- Parameters:
- dev_idint
Device identifier (device index)
- flow_idint
Input flow (port) ID
- ifmapnp.ndarray
Input feature map (frame)
- timeoutint
Milliseconds timeout, ‘0’ indicates infinite
- stream_ofmap(dev_id: int, flow_id: int, ofmap: ndarray, timeout: int = 0)#
Stream data out from device.
- Parameters:
- dev_idint
Device identifier (device index)
- flow_idint
Output flow (port) ID
- ofmapnp.ndarray
Output feature map (frame).
- timeoutint
Milliseconds timeout, ‘0’ indicates infinite
