-
-
Notifications
You must be signed in to change notification settings - Fork 2k
NumpyTensor #2913
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
Merged
Merged
NumpyTensor #2913
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1719223
Init experimental notebook
iamtrask 87db653
Init NumpyTensor functionality
iamtrask 2c1680f
Add NumpyTensor to syft. shorthand
iamtrask f3b2537
Add numpy unit tests
iamtrask 7b02f86
Merge branch 'master' into numpy_tensor2
iamtrask 66c986a
Remove extraneous imports
iamtrask 7846ac8
Merge branch 'numpy_tensor2' of github.com:OpenMined/PySyft into nump…
iamtrask 14c6847
Run black
iamtrask 32d4994
Abstract away automatic hooking functionality
iamtrask 6817e52
Run black
iamtrask 8484dab
Update doc
iamtrask 4ce1ca9
Add support for lists to be passed into NumpyTensor
iamtrask ecb9854
Allow creation of NumpyTensor without having to wrap straight from sy.
iamtrask 027a277
Run black
iamtrask 42f3d13
Fix bug in tests
iamtrask 4b74769
Fix docstring
iamtrask d38a880
Fix docstring
iamtrask 993bbe5
Add numpy casting to native.py with test
iamtrask 782a6af
Tweak inline documentation
iamtrask 00b2d34
Add inline docs
iamtrask File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,99 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Setting up Sandbox...\n", | ||
| "Done!\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "import syft as sy\n", | ||
| "import torch as th\n", | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "sy.create_sandbox(globals(), False, False)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 2, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from syft.frameworks.torch.tensors.interpreters.numpy import NumpyTensor" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 46, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "x = NumpyTensor(numpy_tensor=np.array([[1,2,3,4]])).wrap()\n", | ||
| "y = x.dot(x.transpose())\n", | ||
| "assert (y.child.child == np.array([[30]])).all()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 38, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 42, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "data": { | ||
| "text/plain": [ | ||
| "True" | ||
| ] | ||
| }, | ||
| "execution_count": 42, | ||
| "metadata": {}, | ||
| "output_type": "execute_result" | ||
| } | ||
| ], | ||
| "source": [] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.7.6" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 4 | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| from syft.generic.frameworks.hook import hook_args | ||
| from syft.generic.tensor import AbstractTensor | ||
|
|
||
|
|
||
| class HookedTensor(AbstractTensor): | ||
| """HookedTensor is an abstraction which should not be used directly on its own. Its purpose | ||
| is only to allow other tensors to extend it so that they automatically have all of the Torch | ||
| method hooked without having to add it to the hook.py file. | ||
| """ | ||
|
|
||
| def __init__(self, owner=None, id=None, tags=None, description=None, verbose=False): | ||
| """Initializes a HookedTensor. | ||
|
|
||
| Args: | ||
| numpy_tensor (np.array): The numpy array which this tensor should wrap. | ||
|
Contributor
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. Hey I'm new here, but I dont see a
Member
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. whoops - looks like a mistake. PR welcome :) |
||
| owner (BaseWorker): An optional BaseWorker object to specify the worker on which | ||
| the tensor is located. | ||
| id (str or int): An optional string or integer id of the LargePrecisionTensor. | ||
| tags (list): list of tags for searching. | ||
| description (str): a description of this tensor. | ||
| """ | ||
| super().__init__(id=id, owner=owner, tags=tags, description=description) | ||
| self.verbose = verbose | ||
|
|
||
|
|
||
| ### Register the tensor with hook_args.py ### | ||
| hook_args.default_register_tensor(HookedTensor) | ||
This file contains hidden or 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 hidden or 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,52 @@ | ||
| import numpy as np | ||
|
|
||
| from syft.generic.frameworks.hook import hook_args | ||
| from syft.generic.frameworks.overload import overloaded | ||
| from syft.frameworks.torch.tensors.interpreters.hook import HookedTensor | ||
|
|
||
|
|
||
| class NumpyTensor(HookedTensor): | ||
| """NumpyTensor is a tensor which seeks to wrap the Numpy API with the PyTorch tensor API. | ||
| This is useful because Numpy can offer a wide range of existing functionality ranging from | ||
| large precision, custom scalar types, and polynomial arithmetic. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, numpy_tensor=None, owner=None, id=None, tags=None, description=None, verbose=False | ||
| ): | ||
| """Initializes a NumpyTensor. | ||
|
|
||
| Args: | ||
| numpy_tensor (np.array): The numpy array which this tensor should wrap. | ||
| owner (BaseWorker): An optional BaseWorker object to specify the worker on which | ||
| the tensor is located. | ||
| id (str or int): An optional string or integer id of the LargePrecisionTensor. | ||
| tags (list): list of tags for searching. | ||
| description (str): a description of this tensor. | ||
| """ | ||
| super().__init__(id=id, owner=owner, tags=tags, description=description) | ||
| self.verbose = verbose | ||
|
|
||
| if isinstance(numpy_tensor, list): | ||
| numpy_tensor = np.array(numpy_tensor) | ||
|
|
||
| self.child = numpy_tensor | ||
|
|
||
| @overloaded.method | ||
| def mm(self, _self, other): | ||
| return _self.dot(other) | ||
|
|
||
| @overloaded.method | ||
| def transpose(self, _self, *dims): | ||
| # TODO: the semantics of the .transpose() dimensions are a bit different | ||
| # for Numpy than they are for PyTorch. Fix this. | ||
| # Related: https://github.com/pytorch/pytorch/issues/7609 | ||
| return _self.transpose(*reversed(dims)) | ||
|
|
||
|
|
||
| def create_numpy_tensor(numpy_tensor): | ||
| return NumpyTensor(numpy_tensor).wrap() | ||
|
|
||
|
|
||
| ### Register the tensor with hook_args.py ### | ||
| hook_args.default_register_tensor(NumpyTensor) |
This file contains hidden or 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 hidden or 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 hidden or 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,90 @@ | ||
| import pytest | ||
| import torch as th | ||
| import numpy as np | ||
| import syft as sy | ||
|
|
||
|
|
||
| def test_numpy_add(): | ||
| """ | ||
| Test basic NumpyTensor addition | ||
| """ | ||
|
|
||
| x = sy.NumpyTensor(numpy_tensor=[[1, 2, 3, 4]]) | ||
| y = x + x | ||
| assert (y.child.child == np.array([2, 4, 6, 8])).all() | ||
|
|
||
|
|
||
| def test_numpy_subtract(): | ||
| """ | ||
| Test basic NumpyTensor subtraction | ||
| """ | ||
|
|
||
| x = sy.NumpyTensor(numpy_tensor=np.array([[1, 2, 3, 4]])) | ||
| y = x - x | ||
| assert (y.child.child == np.array([0, 0, 0, 0])).all() | ||
|
|
||
|
|
||
| def test_numpy_multiply(): | ||
| """ | ||
| Test basic NumpyTensor multiplication | ||
| """ | ||
|
|
||
| x = sy.NumpyTensor(numpy_tensor=np.array([[1, 2, 3, 4]])) | ||
| y = x * x | ||
| assert (y.child.child == np.array([1, 4, 9, 16])).all() | ||
|
|
||
|
|
||
| def test_numpy_divide(): | ||
| """ | ||
| Test basic NumpyTensor division | ||
| """ | ||
|
|
||
| x = sy.NumpyTensor(numpy_tensor=np.array([[1, 2, 3, 4]])) | ||
| y = x / x | ||
| assert (y.child.child == np.array([1, 1, 1, 1])).all() | ||
|
|
||
|
|
||
| def test_numpy_dot(): | ||
| """ | ||
| Test basic NumpyTensor dot product | ||
| """ | ||
| x = sy.NumpyTensor(numpy_tensor=np.array([[1, 2, 3, 4]])) | ||
| y = x.dot(x.transpose()) | ||
| assert (y.child.child == np.array([[30]])).all() | ||
|
|
||
|
|
||
| def test_numpy_mm(): | ||
| """ | ||
| Test basic NumpyTensor matrix multiply | ||
| """ | ||
| x = sy.NumpyTensor(numpy_tensor=np.array([[1, 2, 3, 4]])) | ||
| y = x.mm(x.transpose()) | ||
| assert (y.child.child == np.array([[30]])).all() | ||
|
|
||
|
|
||
| def test_numpy_mm2(): | ||
| """ | ||
| Test @ based NumpyTensor matrix multiply | ||
| """ | ||
| x = sy.NumpyTensor(numpy_tensor=np.array([[1, 2, 3, 4]])) | ||
| y = x @ (x.transpose()) | ||
| assert (y.child.child == np.array([[30]])).all() | ||
|
|
||
|
|
||
| def test_numpy_transpose(): | ||
| """ | ||
| Test basic NumpyTensor transpose | ||
| """ | ||
| x = sy.NumpyTensor(numpy_tensor=np.array([[1, 2, 3, 4]])) | ||
| y = x.transpose(0, 1) | ||
| assert (y.child.child == np.array([[1], [2], [3], [4]])).all() | ||
|
|
||
|
|
||
| def test_numpy_casting(): | ||
| """ | ||
| This tests the ability to cast a data tensor to a tensor chain | ||
| with an underlying Numpy representation. | ||
| """ | ||
|
|
||
| out = th.tensor([1, 2, 23, 4]).numpy_tensor() | ||
| assert (out.child.child == np.array([1, 2, 23, 4])).all() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.