Skip to content
Snippets Groups Projects
Commit c3431220 authored by auphelia's avatar auphelia
Browse files

[Core Utils] Added unpack innermost dim from hex string function

parent 9f45ec4b
No related branches found
No related tags found
No related merge requests found
import os
import random
import string
import subprocess
import os
import numpy as np
import onnx
......@@ -111,6 +111,37 @@ def pack_innermost_dim_as_hex_string(ndarray, dtype, pad_to_nbits):
return np.apply_along_axis(fun, ndarray.ndim - 1, ndarray)
def unpack_innermost_dim_from_hex_string(data, shape, packedBits, targetBits):
# function expects flattens array and returns an array in the desired shape
outer_dim_elems = 1
for dim in range(len(shape) - 1):
outer_dim_elems = outer_dim_elems * shape[dim]
inner_dim_elems = shape[-1]
array = []
for outer_elem in range(outer_dim_elems):
ar_list = []
ar_elem = data[0]
data.pop(0)
ar_elem = ar_elem.split("x")
ar_elem_bin = bin(int(ar_elem[1], 16))[2:].zfill(packedBits)
ar_elem_bin = [int(x) for x in ar_elem_bin]
ar_elem_bin.reverse()
for i in range(inner_dim_elems):
upper_limit = (i + 1) * targetBits
lower_limit = i * targetBits
elem = ar_elem_bin[lower_limit:upper_limit]
elem.reverse()
elem_str = "".join(map(str, elem))
ar_list.append(int(elem_str, 2))
ar_list.reverse()
array.append(ar_list)
array = np.asarray(array, dtype=np.float32).reshape(shape)
return array
def interleave_matrix_outer_dim_from_partitions(matrix, n_partitions):
if type(matrix) != np.ndarray or matrix.dtype != np.float32:
# try to convert to a float numpy array (container dtype is float)
......@@ -202,7 +233,7 @@ def calculate_signed_dot_prod_range(dt_a, dt_b, len):
types dt_a and dt_b of len elements can take."""
assert dt_a.signed() and dt_b.signed()
min_prod = 2 ** 30
max_prod = -2 ** 30
max_prod = -(2 ** 30)
for a_val in [dt_a.min(), dt_a.max()]:
for b_val in [dt_b.min(), dt_b.max()]:
prod = a_val * b_val * len
......@@ -250,12 +281,13 @@ class CppBuilder:
process_compile = subprocess.Popen(bash_command, stdout=subprocess.PIPE)
process_compile.communicate()
class IPGenBuilder:
def __init__(self):
self.tcl_script = ""
self.ipgen_path = ""
self.code_gen_dir = ""
self.ipgen_script=""
self.ipgen_script = ""
def append_tcl(self, tcl_script):
self.tcl_script = tcl_script
......@@ -276,4 +308,3 @@ class IPGenBuilder:
bash_command = ["bash", self.ipgen_script]
process_compile = subprocess.Popen(bash_command, stdout=subprocess.PIPE)
process_compile.communicate()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment