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

Bugfix/type safety #19

Merged
merged 9 commits into from
Jun 20, 2024
Merged

Bugfix/type safety #19

merged 9 commits into from
Jun 20, 2024

Conversation

mathias-nillion
Copy link
Contributor

Enforce strong type guardrails introduced in latest version of nada-algebra / nada-numpy

@@ -8,6 +8,7 @@ class MyConvModule(nn.Module):

def __init__(self) -> None:
"""Contains some ConvNet components"""
super().__init__()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this is not strictly mandatory for us but it is in torch. so just copied this here so that the syntax is exactly equal

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, but at the same time I think having a direct coupling makes it much easier.

Setting this as a reminder to update the Nillion Docs on this regard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdym by this?

@@ -162,7 +157,7 @@ async def main():
nillion.Secrets({}),
)
# Rescale the obtained result by the quantization scale
outputs = [na_client.float_from_rational(result["my_output_0"])]
outputs = [na_client.float_from_rational(result["my_output"])]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this occurs b/c we decided to mirror NumPy's behaviour of matrix ops not necessarily returning other matrices

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay. I think that the more we mimick Numpy the better.

@mathias-nillion
Copy link
Contributor Author

Note: ci/cd fails because it relies on pre-release nada-algebra changes
it has been tested though

@@ -22,4 +23,4 @@ def nada_main():
result = my_model.forward(my_input)

# Step 6: We can use result.output() to produce the output for Party1 and variable name "my_output"
return result.output(parties[1], "my_output")
return [Output(result.value, "my_output", parties[1])]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion here. We have the na.output function which prevents from having to put result.value if it is a rational number.

na.output(result, "my_output", parties[1])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try this but if it works then IMO the type hinting for na.output needs to change as currently type na.NadaArray is listed as the only accepted input dtype (which is why i didn't use it initially)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with changing the type hinting not to have the NadaArray only. I think is good to have a single point of output that handles everything no matter what you put inside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed this but flagging to address that type hint in a separate nada-numpy PR at some point

@@ -11,16 +11,16 @@ def nada_main():

class TestModule1(Module):
def __init__(self) -> None:
self.param1 = Parameter((3, 2))
self.param2 = Parameter(3)
self.param1 = Parameter(na.zeros((3, 2), na.Rational))
Copy link
Member

@jcabrero jcabrero Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this comment here, but it may apply to other places. Also, feel free to disagree. I have seen that most (if not all) of the times, we initialize Parameter(na.zeros(..., ensure_cleartext(...))). Wouldn't it make sense to do the na.zeros(, ensure_cleartext()) in the Parameter.__init__.

Again, please, feel free to disagree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could but pytorch also does it this way
(side note: realistically it's very uncommon for users to interface with the Parameter class directly)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright! Seems reasonable


def test_parameters_9(self):
class TestModule(Module):
def __init__(self) -> None:
self.param = Parameter((2, 3))
self.param = Parameter((2, 2))
self.param = Parameter(na.zeros((2, 3)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw this that contradicts my previous comment. So, no problem if you feel like its should be this way.

Copy link
Member

@jcabrero jcabrero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job. As I said, great attention to detail. Fixing the various typing errors should fix CI.

@mathias-nillion mathias-nillion merged commit 2b1500d into main Jun 20, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants