Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support using booleans with LookupTable #365

Merged
merged 1 commit into from Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions frontends/concrete-python/concrete/fhe/extensions/table.py
Expand Up @@ -59,7 +59,7 @@ def __init__(self, table: Any):
def __repr__(self):
return str(list(self.table))

def __getitem__(self, key: Union[int, np.integer, np.ndarray, Tracer]):
def __getitem__(self, key: Union[int, np.integer, np.bool_, np.ndarray, Tracer]):
if not isinstance(key, Tracer):
return LookupTable.apply(key, self.table)

Expand Down Expand Up @@ -92,14 +92,14 @@ def __getitem__(self, key: Union[int, np.integer, np.ndarray, Tracer]):

@staticmethod
def apply(
key: Union[int, np.integer, np.ndarray],
key: Union[int, np.integer, np.bool_, np.ndarray],
table: np.ndarray,
) -> Union[int, np.integer, np.ndarray]:
"""
Apply lookup table.
Args:
key (Union[int, np.integer, np.ndarray]):
key (Union[int, np.integer, np.bool_, np.ndarray]):
lookup key
table (np.ndarray):
Expand All @@ -114,6 +114,9 @@ def apply(
if `table` cannot be looked up with `key`
"""

if isinstance(key, (np.bool_, np.ndarray)) and np.issubdtype(key.dtype, np.bool_):
key = key.astype(np.int64)

if not isinstance(key, (int, np.integer, np.ndarray)) or (
isinstance(key, np.ndarray) and not np.issubdtype(key.dtype, np.integer)
):
Expand Down
7 changes: 7 additions & 0 deletions frontends/concrete-python/tests/execution/test_others.py
Expand Up @@ -677,6 +677,13 @@ def per_element(element):
},
id="np.squeeze(x, axis=1) where x.shape == (1, 1, 1)",
),
pytest.param(
lambda x: fhe.LookupTable([10, 5])[x > 5],
{
"x": {"status": "encrypted", "range": [0, 10]},
},
id="fhe.LookupTable([10, 5])[x > 5]",
),
],
)
def test_others(function, parameters, helpers):
Expand Down