-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NDArray] Extend set_index for N-D Arrays (#1353)
* Extend set_index for N-D Arrays - Add suppport for N-dimensional indices. - Add more information to assertion messages. * Update magma/primitives/set_index.py Co-authored-by: rsetaluri <[email protected]> * Add author to NOTE Co-authored-by: rsetaluri <[email protected]> * Update docstring for `idx` type Co-authored-by: rsetaluri <[email protected]> * Update docstring for `target` Co-authored-by: rsetaluri <[email protected]> * Change `idx` type from List to Sequenct Co-authored-by: rsetaluri <[email protected]> * Use Sequence instead of List for `idx` Co-authored-by: rsetaluri <[email protected]> * Add test for N-D set_index * Fix Sequence for 3.8 * Fix test bench logic * Fix style * Disable failing coreir test * Fix parameter type --------- Co-authored-by: Rakshith Ramesh <[email protected]> Co-authored-by: rsetaluri <[email protected]>
- Loading branch information
1 parent
818ad9b
commit dcd5b89
Showing
3 changed files
with
74 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,45 @@ | ||
from magma.array import Array | ||
from collections.abc import Sequence | ||
from typing import Union, Sequence as Sequnce_T | ||
|
||
from magma.bits import UInt | ||
from magma.bitutils import clog2 | ||
from magma.primitives import mux | ||
|
||
|
||
def set_index(target: Array, value, idx: UInt): | ||
def set_index(target: Array, value, idx: Union[UInt, Sequnce_T[UInt]]): | ||
""" | ||
Returns a new value where index `idx` of value `target` is set to `value` | ||
* `target` - a value of type `Array[N, T]` | ||
Returns a new value where index `idx` of `target` is set to `value` | ||
* `target` - a value of type `Array[N, T]` or `Array[(N, M, L, ...), T]` | ||
* `value` - a value of type `T` | ||
* `idx` - a value of type `UInt[clog2(N)]` | ||
* `idx` - a value of type `UInt[clog2(N)]` or `(UInt[clog2(L)], | ||
UInt[clog2(M), UInt[clog2(N)], ...)` | ||
NOTE(rkshthrmsh): Ordering of indices in `idx` is reverse of ordering in | ||
N-D Array definition. | ||
For more details see: https://github.com/phanrahan/magma/issues/1310. | ||
""" | ||
if not isinstance(target, Array): | ||
raise TypeError("Expected target to be an array") | ||
target_T = type(target) | ||
if not isinstance(value, target_T.T): | ||
raise TypeError( | ||
"Expected value to be the same type as `target`'s contents") | ||
if not isinstance(idx, UInt): | ||
raise TypeError("Expected `idx` to be a UInt") | ||
if len(idx) != clog2(len(target_T)): | ||
raise TypeError( | ||
"Expected number of `idx` bits to map to the length of `target`") | ||
|
||
return target_T([ | ||
mux([elem, value], idx == i) for i, elem in enumerate(target) | ||
]) | ||
if isinstance(idx, UInt): | ||
target_T = type(target) | ||
if not isinstance(value, target_T.T): | ||
raise TypeError( | ||
f"Expected `value` ({type(value)}) to be the same type as" | ||
f"`target`'s contents ({target_T.T})" | ||
) | ||
if len(idx) != clog2(len(target_T)): | ||
raise TypeError( | ||
f"Expected number of `idx` ({len(idx)}) bits to map to the " | ||
f"length of `target` ({clog2(len(target_T))})") | ||
return target_T([ | ||
mux([elem, value], idx == i) for i, elem in enumerate(target) | ||
]) | ||
if isinstance(idx, Sequence): | ||
if len(idx) == 1: | ||
return set_index(target, value, idx[0]) | ||
return set_index(target, set_index(target[idx[0]], value, idx[1:]), | ||
idx[0]) | ||
raise TypeError( | ||
f"Expected `idx` ({type(idx)}) to be UInt or List[UInt, ...] " | ||
) |
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