Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin-yateto/master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
davschneller committed May 2, 2024
2 parents 0b0cb17 + 35001bd commit d33c1fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
5 changes: 3 additions & 2 deletions yateto/codegen/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def temporary(self, bufname, size, iniZero=False, memory=list()):

if self._target == 'cpu':
if self._arch.onHeap(size):
if memory:
raise NotImplementedError('Direct temporary initialization is not supported for heap-allocated memory.')
if len(self._freeList) == 0:
self._cpp('int {};'.format(self.ERROR_NAME))
self._cpp('{}* {};'.format(self._arch.typename, bufname))
Expand All @@ -43,6 +41,9 @@ def temporary(self, bufname, size, iniZero=False, memory=list()):
self._arch.typename))
if iniZero:
self._cpp.memset(bufname, size, self._arch.typename)
if memory:
for i, data in enumerate(memory):
self._cpp(f'{bufname}[{i}] = {data};')
self._freeList.append(bufname)
else:
ini = ''
Expand Down
36 changes: 22 additions & 14 deletions yateto/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@
def __transposeMatrix(matrix):
matrixT = dict()
for entry,value in matrix.items():
matrixT[(entry[1], entry[0])] = value
matrixT[tuple(entry[::-1])] = value
return matrixT

def __processMatrix(name, rows, columns, entries, clones, transpose, alignStride, namespace=None):
def __processMatrix(name, shape, entries, clones, transpose, alignStride, namespace=None):
matrix = dict()

dim = len(shape)

# traverse a list of matrix entries and generate a matrix description
# as a hash table
for entry in entries:
# adjust row and column numbers
row = int(entry[0])-1
col = int(entry[1])-1
index = tuple(int(entry[i]) - 1 for i in range(dim))

# allocate a matrix element inside of a table
matrix[(row, col)] = entry[2]
matrix[index] = entry[-1]

# allocate an empty hash table to hold tensors (matrices) which are going to be generated
# using the matrix description
Expand All @@ -42,16 +43,16 @@ def __processMatrix(name, rows, columns, entries, clones, transpose, alignStride

# generate tensors using description of a give matrix
for name in names:
# compute a shape of a tensor
shape = (columns, rows) if transpose(name) else (rows, columns)
if shape[1] == 1:
# compute a shape of a tensor (for now, assume transpose == invert dimensions)
shape = shape[::-1] if transpose(name) else shape
if shape[1] == 1 and len(shape) == 2: # TODO: remove once all files are converted
shape = (shape[0],)

# transpose matrix if it is needed
mtx = __transposeMatrix(matrix) if transpose(name) else matrix

# adjust layout description in case if a given matrix is a vector
if len(shape) == 1:
if len(shape) == 1: # TODO: remove once all files are converted
mtx = {(i[0],): val for i,val in mtx.items()}

# Create an tensor(matrix) using the matrix description and append the hash table
Expand All @@ -60,7 +61,7 @@ def __processMatrix(name, rows, columns, entries, clones, transpose, alignStride
'Please, check your input file and rename')

matrices[name] = Tensor(name=name,
shape=shape,
shape=tuple(shape),
spp=mtx,
alignStride=alignStride(name),
namespace=namespace)
Expand Down Expand Up @@ -91,7 +92,7 @@ def parseXMLMatrixFile(xmlFile, clones=dict(), transpose=lambda name: False, ali
else:
__complain(child)

matrices.update( __processMatrix(name, rows, columns, entries, clones, transpose, alignStride, namespace) )
matrices.update( __processMatrix(name, (rows, columns), entries, clones, transpose, alignStride, namespace) )
else:
__complain(node)

Expand All @@ -104,9 +105,16 @@ def parseJSONMatrixFile(jsonFile, clones=dict(), transpose=lambda name: False, a
content = json.load(j)
for m in content:
entries = m['entries']
if len(next(iter(entries))) == 2:
entries = [(entry[0], entry[1], True) for entry in entries]
matrices.update( __processMatrix(m['name'], m['rows'], m['columns'], entries, clones, transpose, alignStride, namespace) )
if 'rows' in m:
shape = [m['rows']]
if 'columns' in m:
shape += [m['columns']]
else:
shape = m['shape']
dim = len(shape)
if len(next(iter(entries))) == dim:
entries = [(*entry, True) for entry in entries]
matrices.update( __processMatrix(m['name'], shape, entries, clones, transpose, alignStride, namespace) )

return create_collection(matrices)

Expand Down

0 comments on commit d33c1fe

Please sign in to comment.