[New Feature]: Add support for transposing a tensor along a specified axes#443
Open
silencrown wants to merge 2 commits intoOpenMined:mainfrom
Open
[New Feature]: Add support for transposing a tensor along a specified axes#443silencrown wants to merge 2 commits intoOpenMined:mainfrom
silencrown wants to merge 2 commits intoOpenMined:mainfrom
Conversation
Author
|
Here's a simple example that might better describe what this PR does. In the following example, the three operators shape = [2, 3, 4]
expected_shape = [2, 4, 3]
data = np.arange(24).reshape(shape)
# numpy
np_trans = np.transpose(data, [0, 2, 1])
# torch
pt_trans = np.array(torch.tensor(data).transpose(-2, -1).tolist())
# CKKSTensor
enc_tensor = cxt_man.encrypt(data)
enc_trans = enc_tensor.transpose([0, 2, 1])
dec_trans = np.array(enc_trans.decrypt().tolist())
assert list(dec_trans.shape) == expected_shape
assert np.allclose(pt_trans, np_trans, rtol=0, atol=0.01)
assert np.allclose(dec_trans, np_trans, rtol=0, atol=0.01) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Hello, while using TenSEAL, I noticed that the current version does not support transposing a tensor along a specified dimension. This may make it more complicated for users to implement functions like multi-head attention. Based on xtensor, I have modified the existing code to add support for the aforementioned operation in TenSEAL. In this code submission, users can utilize the Python method transpose(0, 2, 1). This operation is consistent with NumPy and can be adapted with minimal changes to support operations similar to PyTorch's transpose(-2, -1).
Affected Dependencies
No
How has this been tested?
The test code is included in this pull request.
Checklist