-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the issue with cpp codegen, where it currently cannot handle inputs
like: ```c++ cpp.reshape_strides(Range([(0, 4, 1), (0, 5, 1)]), None, None, [2, 3, 5]) ``` and crashes with an index error.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import unittest | ||
from functools import reduce | ||
from operator import mul | ||
|
||
from dace.codegen.targets import cpp | ||
from dace.subsets import Range | ||
|
||
|
||
class ReshapeStrides(unittest.TestCase): | ||
def test_multidim_array_all_dims_unit(self): | ||
r = Range([(0, 0, 1), (0, 0, 1)]) | ||
|
||
# To smaller-sized shape | ||
target_dims = [1] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, [1]) | ||
|
||
# To equal-sized shape | ||
target_dims = [1, 1] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, [1, 1]) | ||
|
||
# To larger-sized shape | ||
target_dims = [1, 1, 1] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, [1, 1, 1]) | ||
|
||
def test_multidim_array_some_dims_unit(self): | ||
r = Range([(0, 1, 1), (0, 0, 1)]) | ||
|
||
# To smaller-sized shape | ||
target_dims = [2] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, target_dims) | ||
|
||
# To equal-sized shape | ||
target_dims = [2, 1] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, target_dims) | ||
|
||
# To equal-sized shape | ||
target_dims = [2, 1, 1] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, target_dims) | ||
|
||
def test_multidim_array_different_shape(self): | ||
r = Range([(0, 4, 1), (0, 5, 1)]) | ||
|
||
# To smaller-sized shape | ||
target_dims = [30] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, target_dims) | ||
|
||
# To equal-sized shape | ||
target_dims = [15, 2] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, target_dims) | ||
|
||
# To equal-sized shape | ||
target_dims = [3, 5, 2] | ||
self.assertEqual(r.num_elements_exact(), reduce(mul, target_dims)) | ||
reshaped, strides = cpp.reshape_strides(r, None, None, target_dims) | ||
self.assertEqual(reshaped, target_dims) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |