-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from NillionNetwork/feature/distance
Adds dot product distance function
- Loading branch information
Showing
9 changed files
with
122 additions
and
9 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
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,23 @@ | ||
"""Distance implementation""" | ||
|
||
import nada_algebra as na | ||
from nada_ai.nn.module import Module | ||
|
||
__all__ = ["DotProductSimilarity"] | ||
|
||
|
||
class DotProductSimilarity(Module): | ||
"""Dot product similarity module""" | ||
|
||
def forward(self, x_1: na.NadaArray, x_2: na.NadaArray) -> na.NadaArray: | ||
""" | ||
Forward pass logic. | ||
Args: | ||
x_1 (na.NadaArray): First input array. | ||
x_2 (na.NadaArray): Second input array. | ||
Returns: | ||
na.NadaArray: Dot product between input arrays. | ||
""" | ||
return x_1 @ x_2.T |
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
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,19 @@ | ||
from nada_dsl import * | ||
from nada_ai.nn import DotProductSimilarity | ||
import nada_algebra as na | ||
|
||
|
||
def nada_main(): | ||
party = Party("party") | ||
|
||
# 2 queries, each of size 3 | ||
queries = na.array((2, 3), party, "input_x", SecretInteger) | ||
# 5 values, each also of size 3 | ||
values = na.array((5, 3), party, "input_y", SecretInteger) | ||
|
||
dps = DotProductSimilarity() | ||
|
||
similarities = dps(queries, values) | ||
assert similarities.shape == (2, 5) # for each query, the similarity to each value | ||
|
||
return similarities.output(party, "my_output") |
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,68 @@ | ||
--- | ||
program: distance | ||
inputs: | ||
secrets: | ||
input_x_0_0: | ||
SecretInteger: "1" | ||
input_x_0_1: | ||
SecretInteger: "2" | ||
input_x_0_2: | ||
SecretInteger: "3" | ||
input_x_1_0: | ||
SecretInteger: "4" | ||
input_x_1_1: | ||
SecretInteger: "5" | ||
input_x_1_2: | ||
SecretInteger: "6" | ||
input_y_0_0: | ||
SecretInteger: "1" | ||
input_y_0_1: | ||
SecretInteger: "2" | ||
input_y_0_2: | ||
SecretInteger: "3" | ||
input_y_1_0: | ||
SecretInteger: "4" | ||
input_y_1_1: | ||
SecretInteger: "5" | ||
input_y_1_2: | ||
SecretInteger: "6" | ||
input_y_2_0: | ||
SecretInteger: "7" | ||
input_y_2_1: | ||
SecretInteger: "8" | ||
input_y_2_2: | ||
SecretInteger: "9" | ||
input_y_3_0: | ||
SecretInteger: "10" | ||
input_y_3_1: | ||
SecretInteger: "11" | ||
input_y_3_2: | ||
SecretInteger: "12" | ||
input_y_4_0: | ||
SecretInteger: "13" | ||
input_y_4_1: | ||
SecretInteger: "14" | ||
input_y_4_2: | ||
SecretInteger: "15" | ||
public_variables: {} | ||
expected_outputs: | ||
my_output_0_0: | ||
SecretInteger: "14" | ||
my_output_0_1: | ||
SecretInteger: "32" | ||
my_output_0_2: | ||
SecretInteger: "50" | ||
my_output_0_3: | ||
SecretInteger: "68" | ||
my_output_0_4: | ||
SecretInteger: "86" | ||
my_output_1_0: | ||
SecretInteger: "32" | ||
my_output_1_1: | ||
SecretInteger: "77" | ||
my_output_1_2: | ||
SecretInteger: "122" | ||
my_output_1_3: | ||
SecretInteger: "167" | ||
my_output_1_4: | ||
SecretInteger: "212" |
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
"pool", | ||
"linear_regression", | ||
"end-to-end", | ||
"distance", | ||
"prophet", | ||
] | ||
|
||
|