Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.index_precision = index_precision
def definition_cpp(self):
cpp_fmt = ('typedef struct {name} {{ '
'{index} row_index; '
'{index} col_index; '
'{precision} weight; }} {name};\n')
return cpp_fmt.format(name=self.name, index=self.index_precision, precision=self.precision)
class Variable(object):
def __init__(self, var_name, type_name, precision, **kwargs):
self.name = var_name.format(**kwargs)
self.type = HLSType(type_name, precision, **kwargs)
self.cppname = re.sub(r'\W|^(?=\d)','_', self.name)
class ArrayVariable(Variable):
def __init__(self, shape, dim_names, var_name='layer{index}', type_name='layer{index}_t', precision=None, pragma='partition', **kwargs):
super(ArrayVariable, self).__init__(var_name, type_name, precision, **kwargs)
self.shape = shape
self.dim_names = dim_names
if pragma == 'partition':
self.partition()
elif pragma == 'reshape':
self.reshape()
elif pragma == 'stream':
self.stream()
else:
self.pragma = None
def partition(self, type='complete', factor=None, dim=0):
if factor:
return zip(self.dim_names, self.shape)
def definition_cpp(self):
array_shape = self.size_cpp()
return '{type} {name}[{shape}]'.format(type=self.type.name, name=self.cppname, shape=array_shape)
def size(self):
nelem = 1
for dim in self.shape:
nelem *= dim
return nelem
def size_cpp(self):
return '*'.join([str(k) for k in self.dim_names])
class WeightVariable(Variable):
def __init__(self, var_name, type_name, precision, data, **kwargs):
super(WeightVariable, self).__init__(var_name, type_name, precision, **kwargs)
self.data = data
self.nzeros = -1
self.shape = list(self.data.shape)
self.data_length = np.prod(self.data.shape)
self.nonzeros = np.count_nonzero(self.data)
self.nzeros = self.data_length - self.nonzeros
self.min = np.min(self.data)
self.max = np.max(self.data)
self._iterator = None
self.update_precision(precision)
def __iter__(self):
self._iterator = np.nditer(self.data, order='C')
return self