-
Notifications
You must be signed in to change notification settings - Fork 49
Exposing vector_add to zok frontend #78
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
base: master
Are you sure you want to change the base?
Changes from 12 commits
66438fb
b1cceff
f9619f4
a3d1f2f
49576d2
47fbdb8
3a065c0
123e691
d976738
8887be7
7057117
e246ddc
04d9785
c8608b3
47c0a40
8c5d3f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from "EMBED" import vadd_u16 | ||
|
|
||
| def main() -> u16[5]: | ||
| u16[5] a = [1, 2, 3, 4, 5] | ||
| u16[5] b = [2, 3, 4, 5, 6] | ||
| assert(vadd_u16(a, b) == [3, 5, 7, 9, 11]) | ||
| return vadd_u16(a,b) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from "EMBED" import vadd_u32 | ||
|
|
||
| def main() -> u32[5]: | ||
| u32[5] a = [1, 2, 3, 4, 5] | ||
| u32[5] b = [2, 3, 4, 5, 6] | ||
| assert(vadd_u32(a, b) == [3, 5, 7, 9, 11]) | ||
| return vadd_u32(a,b) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from "EMBED" import vadd_u32 | ||
|
|
||
| def main() -> u32[5]: | ||
| u32[5] a = [1, 2, 3, 4, 5] | ||
| u32[5] b = [] | ||
| return vadd_u32(a,b) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from "EMBED" import vadd_u64 | ||
|
|
||
| def main() -> u64[5]: | ||
| u64[5] a = [1, 2, 3, 4, 5] | ||
| u64[5] b = [2, 3, 4, 5, 6] | ||
| assert(vadd_u64(a, b) == [3, 5, 7, 9, 11]) | ||
| return vadd_u64(a,b) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from "EMBED" import vadd_u8 | ||
|
|
||
| def main() -> u8[5]: | ||
| u8[5] a = [1, 2, 3, 4, 5] | ||
| u8[5] b = [2, 3, 4, 5, 6] | ||
| assert(vadd_u8(a, b) == [3, 5, 7, 9, 11]) | ||
| return vadd_u8(a,b) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,8 +29,10 @@ use hashconsing::{HConsed, WHConsed}; | |
| use lazy_static::lazy_static; | ||
| use log::debug; | ||
| use rug::Integer; | ||
| use std::cmp::Ordering; | ||
| use std::collections::BTreeMap; | ||
| use std::fmt::{self, Debug, Display, Formatter}; | ||
| use std::iter::Peekable; | ||
| use std::sync::{Arc, RwLock}; | ||
|
|
||
| pub mod bv; | ||
|
|
@@ -721,6 +723,104 @@ impl Array { | |
| self.check_idx(idx); | ||
| self.map.get(idx).unwrap_or(&*self.default).clone() | ||
| } | ||
|
|
||
| /// Iter | ||
| pub fn into_iter(&self) -> std::collections::btree_map::IntoIter<Value, Value> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're going to implement the Continuing my crusade against unnecessary cloning: you probably want impl<'a> IntoIterator for &'a Array {
type Item = (&'a Value, &'a Value);
type IntoIter = std::collection::btree_map::Iter<'a, Value, Value>;
fn into_iter(self) -> Self::IntoIter {
self.map.iter()
}
}
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A second, higher-level comment: it's extremely un-idiomatic for an In contrast, what is currently implemented in the PR takes for (k, v) in some_array.into_iter() {
}they will have potentially caused a very expensive clone that they didn't expect (because idiomatically, into_iter never clones---it either consumes a value or it takes a reference and returns references). Does this make sense? |
||
| self.map.clone().into_iter() | ||
| } | ||
| } | ||
|
|
||
| /// Merge two Array Iterators | ||
| pub struct ArrayMerge { | ||
|
edwjchen marked this conversation as resolved.
Outdated
|
||
| left: Peekable<Box<std::collections::btree_map::IntoIter<Value, Value>>>, | ||
| right: Peekable<Box<std::collections::btree_map::IntoIter<Value, Value>>>, | ||
| left_dfl: Value, | ||
| right_dfl: Value, | ||
| } | ||
|
|
||
| impl ArrayMerge { | ||
| /// Create a new [ArrayMerge] from two [Array]s | ||
| pub fn new(a: Array, b: Array) -> Self { | ||
| if a.size != b.size { | ||
| panic!("IR Arrays have different lengths: {}, {}", a.size, b.size); | ||
| } | ||
| if a.key_sort != b.key_sort { | ||
| panic!( | ||
| "IR Arrays have different key sorts: {}, {}", | ||
| a.key_sort, b.key_sort | ||
| ); | ||
| } | ||
| if a.default.sort() != b.default.sort() { | ||
| panic!( | ||
| "IR Arrays default values have different key sorts: {}, {}", | ||
| a.default.sort(), | ||
| b.default.sort() | ||
| ); | ||
| } | ||
|
edwjchen marked this conversation as resolved.
Outdated
|
||
|
|
||
| Self { | ||
| left: Box::new(a.into_iter()).peekable(), | ||
| right: Box::new(b.into_iter()).peekable(), | ||
|
edwjchen marked this conversation as resolved.
Outdated
|
||
| left_dfl: *a.default, | ||
| right_dfl: *b.default, | ||
| } | ||
| } | ||
|
|
||
| /// Iter | ||
|
edwjchen marked this conversation as resolved.
Outdated
|
||
| pub fn into_iter(&mut self) -> Box<dyn Iterator<Item = (Value, Value, Value)>> { | ||
| let mut acc: Vec<(Value, Value, Value)> = Vec::new(); | ||
| let mut next = self.next_(); | ||
| while let Some(n) = next { | ||
| acc.push(n); | ||
| next = self.next_(); | ||
| } | ||
| Box::new(acc.into_iter()) | ||
| } | ||
|
|
||
| /// Next | ||
| pub fn next_(&mut self) -> Option<(Value, Value, Value)> { | ||
| let l_peek = self.left.peek(); | ||
| let r_peek = self.right.peek(); | ||
|
|
||
| let mut left_next = false; | ||
| let mut right_next = false; | ||
|
edwjchen marked this conversation as resolved.
Outdated
|
||
|
|
||
| let res = match (l_peek, r_peek) { | ||
|
edwjchen marked this conversation as resolved.
Outdated
|
||
| (Some((l_ind, l_val)), Some((r_ind, r_val))) => match l_ind.cmp(r_ind) { | ||
| Ordering::Less => { | ||
| left_next = true; | ||
| Some((l_ind.clone(), l_val.clone(), self.right_dfl.clone())) | ||
|
edwjchen marked this conversation as resolved.
Outdated
|
||
| } | ||
| Ordering::Greater => { | ||
| right_next = true; | ||
| Some((r_ind.clone(), self.left_dfl.clone(), r_val.clone())) | ||
| } | ||
| Ordering::Equal => { | ||
| left_next = true; | ||
| right_next = true; | ||
| Some((l_ind.clone(), l_val.clone(), r_val.clone())) | ||
| } | ||
| }, | ||
| (Some((l_ind, l_val)), None) => { | ||
| left_next = true; | ||
| Some((l_ind.clone(), l_val.clone(), self.right_dfl.clone())) | ||
| } | ||
| (None, Some((r_ind, r_val))) => { | ||
| right_next = true; | ||
| Some((r_ind.clone(), self.left_dfl.clone(), r_val.clone())) | ||
| } | ||
| (None, None) => None, | ||
| }; | ||
|
|
||
| if left_next { | ||
| self.left.next(); | ||
| } | ||
| if right_next { | ||
| self.right.next(); | ||
| } | ||
|
|
||
| res | ||
| } | ||
| } | ||
|
|
||
| impl Display for Value { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,3 +75,16 @@ def u16_to_u32(u16 i) -> u32: | |
|
|
||
| def u8_to_u16(u8 i) -> u16: | ||
| return 0u16 | ||
|
|
||
| // vector functions | ||
| def vadd_u8<N>(u8[N] a, u8[N] b) -> u8[N]: | ||
| return [0; N] | ||
|
|
||
| def vadd_u16<N>(u16[N] a, u16[N] b) -> u16[N]: | ||
| return [0; N] | ||
|
|
||
| def vadd_u32<N>(u32[N] a, u32[N] b) -> u32[N]: | ||
| return [0; N] | ||
|
|
||
| def vadd_u64<N>(u64[N] a, u64[N] b) -> u64[N]: | ||
| return [0; N] | ||
|
Comment on lines
+79
to
+90
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you're only going to support one type here it should be
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think only BV are currently supported in the FHE backend, which is why I left it as u32.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still, it might make sense to support all the basic types, no? It wouldn't be much extra work:
Since this is so simple, I don't see a reason to leave it half-done. But I could be missing something!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, updated! |
||
Uh oh!
There was an error while loading. Please reload this page.