bitorch_engine.utils.quant_operators.get_binary_row

bitorch_engine.utils.quant_operators.get_binary_row(nd_row, binary_row, nd_size, bits_per_binary_word)[source]

Binarizes an input NDArray (nd_row) into a binary representation (binary_row) based on the specified number of bits per binary word (bits_per_binary_word).

This function iteratively processes each segment of the input array with the length of ‘bits_per_binary_word’, converting each segment into a binary word. Each bit in the binary word represents the sign (positive or negative) of the corresponding element in the input array segment.

Specifically, for each segment:
  • A binary word (‘rvalue’) is initialized to 0.

  • For each element in the segment, if the element is non-negative, the corresponding bit in ‘rvalue’ is set to 1; otherwise, it remains 0.

  • The binary word is then stored in ‘binary_row’ at the position corresponding to the segment index.

Parameters:
  • nd_row (array-like) – The input array to be binarized.

  • binary_row (array-like) – The output array where each element is a binary word representing a segment of ‘nd_row’.

  • nd_size (int) – The size of the ‘nd_row’ array.

  • bits_per_binary_word (int) – The number of bits in each binary word, determining the segment size for binarization.

Returns:

The binarized representation of ‘nd_row’ stored in ‘binary_row’.

Return type:

array-like

Example of equivalent C++ logic:

for (int i = 0; i < size; i+=BITS_PER_BINARY_WORD) {
  BINARY_WORD rvalue=0;
  BINARY_WORD sign;
  for (int j = 0;j < BITS_PER_BINARY_WORD; ++j) {
    sign = (row[i+j]>=0);
    BIT_SET(rvalue, j, sign);
  }
  b_row[i/BITS_PER_BINARY_WORD] = rvalue;
}