Python API

class thanna.Thanna(base_address=268435456)[source]

Thanna class provides an interface to interact with the Thanna device.

Parameters:

base_address (int) – The base address to used for calculating addresses in thannas memory region. This should be the same address as specified in thannaOS/project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi

hexdump()[source]

Print the given buffer similar to the hexdump command on unix systems.

Parameters:

data (bytes) – The buffer to dump

read_mem(addr, size)[source]

Read memory from the specified address.

Parameters:
  • addr (int) – The address to read from.

  • size (int) – The size of the memory to read.

Returns:

The data read from memory.

Return type:

bytes

read_reg(reg)[source]

Read the value from the specified register.

Parameters:

reg (int) – The register to read from.

Returns:

The value read from the register.

Return type:

int

thanna_command_get_engine_ready()[source]

Read the engine ready status from the device.

Returns:

The engine ready status.

Return type:

list(bool)

thanna_command_read_error()[source]

Read the error status from the device.

Returns:

The error status.

Return type:

int

thanna_command_reset()[source]

Reset the device.

thanna_command_write_compute_payload(line_buffer_limit, sl_win_buffer_end, use_stride_2, window_strides, skip_relu, rbu_lc_cb_bursts, db1_lc_cb_items, rbu_lc_nb_bursts, input_offset, write_offset)[source]

Write the compute payload register in the device.

Parameters:
  • line_buffer_limit (int) – Item limit of the line buffer (PL1 bit 2-14) [REG 3]

  • sl_win_buffer_end (int) – Number of steps until the window of the sliding window buffer is full (PL1 bit 15-27) [REG 3]

  • use_stride_2 (bool) – Use stride of 2 flag (PL1 bit 28) [REG 3]

  • window_strides (int) – Number of window strides (2 for Fully Connected) (PL2 bit 3-15) [REG 4]

  • skip_relu (bool) – Skip ReLU flag (keep negative values) (PL2 bit 16) [REG 4]

  • rbu_lc_cb_bursts (int) – Number of bursts needet to compute a neuron (PL2 bit 19-31) [REG 4]

  • db1_lc_cb_items (int) – Number of burst words (AXI-Bus-Width/ bit_width from thanna_converter) to compute a neuron (PL3 bit 1-13) [REG 5]

  • rbu_lc_nb_bursts (int) – Number of bursts needet to compute a layer (PL3 bit 14-26) [REG 5]

  • input_offset (int) – Offset of the input data in THANNA_RAM (PL4 bit 0-32) [REG 6]

  • write_offset (int) – Offset of the output data in THANNA_RAM (PL5 bit 0-32) [REG 7]

thanna_command_write_head(core_index, op_code, async_exec)[source]

Write the head register in the device.

Parameters:
  • core_index (int) – The core index.

  • op_code (int) – The operation code.

  • async_exec (bool) – The async execution flag.

thanna_command_write_load_payload(memory_offset)[source]

Write the load payload register in the device.

Parameters:

memory_offset (int) – The memory offset.

update_reg_bits(reg, mask, val)[source]

Update specific bits in the specified register.

Parameters:
  • reg (int) – The register to update.

  • mask (int) – The bitmask to apply.

  • val (int) – The value to update.

Returns:

The result of the update operation.

Return type:

int

write_mem(addr, buf)[source]

Write data to memory at the specified address.

Parameters:
  • addr (int) – The address to write to.

  • buf (bytes) – The data to write.

Returns:

The result of the write operation.

Return type:

int

write_reg(reg, val)[source]

Write a value to the specified register.

Parameters:
  • reg (int) – The register to write to.

  • val (int) – The value to write.

Returns:

The result of the write operation.

Return type:

int

class thanna.ThannaConverter(bit_mode: int, value_bit_width: int)[source]
bitwise_compress(num_list)[source]
clip(value)[source]
combine_numbers_bitwise(numbers)[source]
convert_conv_matrix(conv_matrix)[source]

Converts a convolution matrix into a compressed format.

Parameters:

conv_matrix (ndarray) – The convolution matrix to be converted.

Returns:

The compressed representation of the convolution matrix.

Return type:

ndarray

convert_conv_weights(conv_weights)[source]

Converts convolutional weights.

Parameters:

conv_weights (ndarray) – The convolutional weights to be converted.

Returns:

The converted convolutional weights.

Return type:

ndarray

convert_fc_weights(fc_weights, kernel_width, kernel_height)[source]

Converts fully connected (FC) weights to a compressed format.

Parameters:
  • fc_weights (ndarray) – The FC weights to be converted.

  • kernel_width (int) – The width of the kernel.

  • kernel_height (int) – The height of the kernel.

Returns:

The converted and compressed FC weights.

Return type:

ndarray

convert_unfolded_to_fixedpoint(unfolded_data)[source]
convert_vec(vec)[source]

Converts a vector by padding it and then applying bitwise compression.

Parameters:

vec (numpy.ndarray) – The input vector to be converted.

Returns:

The converted vector.

Return type:

numpy.ndarray

pad_conv_matrix(conv_matrix)[source]

Pad the convolution matrix with zeros to ensure its shape is divisible by self.p.

Parameters:
  • conv_matrix (ndarray) – The convolution matrix to be padded.

  • Shape – (channel, row, column)

Returns:

The padded convolution matrix.

Return type:

ndarray

pad_conv_weights(conv_weights)[source]

Pad the convolutional weights to ensure their dimensions are divisible by ‘p’. :param conv_weights: The convolutional weights to be padded. :type conv_weights: ndarray :param shape: (filter, channel, row, column)

Returns:

The padded convolutional weights.

Return type:

ndarray

pad_fc_weights(fc_weights, kernel_width, kernel_height)[source]

Pads the fully connected weights matrix with zeros to ensure its dimensions are divisible by self.p.

Parameters:
  • fc_weights (numpy.ndarray) – The fully connected weights matrix.

  • shape – (neuron, input)

  • kernel_width (int) – The width of the kernel.

  • kernel_height (int) – The height of the kernel.

Returns:

The padded fully connected weights matrix.

Return type:

numpy.ndarray

pad_vec(vec)[source]

Pad the input vector with zeros to make its length a multiple of self.p.

Parameters:

vec (numpy.ndarray) – The input vector to be padded.

Returns:

The padded vector.

Return type:

numpy.ndarray

unfold_padded_conv_matrix(conv_matrix_padded)[source]

Unfolds a padded convolution matrix by splitting it into smaller matrices, swapping axes, flattening, and concatenating them.

Parameters:

conv_matrix_padded (numpy.ndarray) – The padded convolution matrix.

Returns:

The unfolded convolution matrix.

Return type:

numpy.ndarray

unfold_padded_conv_weights(conv_weights_padded)[source]

Unfolds the padded convolutional weights.

Parameters:

conv_weights_padded (ndarray) – The padded convolutional weights.

Returns:

The unfolded convolutional weights.

Return type:

ndarray

unfold_padded_fc_weights(fc_weights_padded, kernel_width, kernel_height)[source]

Unfolds the padded fully connected (FC) weights into a concatenated array.

Parameters:
  • fc_weights_padded (ndarray) – The padded FC weights.

  • kernel_width (int) – The width of the kernel.

  • kernel_height (int) – The height of the kernel.

Returns:

The unfolded and concatenated array of FC weights.

Return type:

ndarray