torchmx.utils
get_logger
Returns a logger with the specified name and format.
Arguments:
logger_name
str, optional - Name of the logger. Defaults to "TORCHMX".format_string
str, optional - Format of the log message. Defaults to "%(asctime)s - %(name)s - %(levelname)s - %(message)s".console_output
bool, optional - Whether to output the logs to the console. Defaults to True.
Returns:
logging.Logger
- Logger with the specified name and format.
get_uniform_random_number
Generate random numbers from uniform distribution of range [min_val, max_val)
Arguments:
min_val
int - minimum value of the rangemax_val
int - maximum value of the rangeshape
Iterable[int] - shape of the tensordtype
torch.dtype - data type of the tensor
Returns:
torch.Tensor
- tensor of shapeshape
and dtypedtype
with random numbers
tensor_size_hp_to_fp4x2
Converts the size of a tensor from half precision to fp4x2 precision.
Arguments:
orig_size
torch.Size - The size of the original tensor.packing_dim
int - The dimension where for packing 2xuint4 per byte
Returns:
List[int]
- The size of the tensor in fp4x2 precision.
tensor_size_fp4x2_to_hp
Converts the size of a tensor from fp4x2 precision to half precision by unpacking the 4-bits into 8-bits.
Arguments:
orig_size
torch.Size - The size of the original tensor.unpacking_dim
int - The dimension where for unpacking the uint4 values to a single byte
Returns:
List[int]
- The size of the tensor in half precision.
unpack_uint4
Unpacks a tensor of uint8 values into two tensors of uint4 values.
Arguments:
uint8_data
torch.Tensor - A tensor containing packed uint8 values.packing_dim
int - The dimension along which the unpacking is performed.
Returns:
torch.Tensor
- A tensor containing the unpacked uint4 values.
pack_uint4
Packs uint4 data to unit8 format along the specified dimension.
Arguments:
uint4_data
torch.Tensor - The input tensor containing uint8 data.packing_dim
int - The dimension along which to pack the data.
Returns:
torch.Tensor
- A tensor with the packed uint4 data.
Raises:
AssertionError
- If the size of the specified dimension is not even.
Notes:
The function assumes that the input data is contiguous and reshapes it accordingly. The packing is done by combining pairs of uint8 values into a single uint8 value where each original uint8 value is treated as a uint4.
set_seed
Set the random seed for reproducibility.
Arguments:
seed
int - The seed value to set.
Returns:
None