bitorch_engine.utils.quant_operators.get_binary_col

bitorch_engine.utils.quant_operators.get_binary_col(nd_col, binary_col, dim_n, dim_k, bits_per_binary_word)[source]

Binarizes an array column-wise, transforming each element into a binary representation.

This function is a Python re-implementation of an equivalent C++ version. It operates on a columnar slice of an array, encoding each segment of BITS_PER_BINARY_WORD bits into a binary word, where each bit is determined by the sign (positive or non-negative vs. negative) of the corresponding element in the input array.

The binarization process proceeds by iterating over the array in blocks of BITS_PER_BINARY_WORD, setting each bit based on the sign of the corresponding element. The result is stored in a pre-allocated array for binary representations.

Parameters:
  • nd_col (array-like) – The input array containing numerical values to be binarized.

  • binary_col (array-like) – Pre-allocated array where the binary representations are stored.

  • dim_n (int) – The size of the dimension over which to iterate, typically the number of rows in the array.

  • dim_k (int) – The size of the second dimension, typically the number of columns.

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

Returns:

The modified binary_col array containing the binary representations of the input array, column-wise.

Return type:

array-like

Example of equivalent C++ logic:

for(int y=0; y<(n/BITS_PER_BINARY_WORD); y++){
    for(int x=0; x < k; ++x){
    BINARY_WORD rvalue=0;
    BINARY_WORD sign;
    for(int b=0; b<BITS_PER_BINARY_WORD; ++b){
        sign = (col[(y*BITS_PER_BINARY_WORD+b)*k + x]>=0);
        BIT_SET(rvalue, b, sign);
    }
    b_col[y*k + x] = rvalue;
    }
}