forked from daphne-eu/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DAPHNE-daphne-eu#499] Data exchange with Pandas, PyTorch & TensorFlo…
…w via shared memory (daphne-eu#585) - Efficient data transfer via shared memory in DaphneLib. - Designed all functions in a zero-copy manner with strong focus on performance. - Added pandas shared memory support for frames. - Different pandas frame types (e.g., Series, Sparse, Categorical) are automatically transformed to standard frames. - With the argument "keepIndex=True" in the from_pandas function, the original df index is stored as the first column named "index". - With the argument "useIndexColumn=True" the Index column from a DAPHNE Frame is stored as the index of the pandas df and no longer as separate column. - Added PyTorch and TensorFlow shared memory support for 2d & nd tensors (nd tensors will be flattened to 2d). - Tensors are transformed to matrices, the original shape can be returned with the argument "return_shape=True" in the from_pytorch & from_tensorflow methods. - Matrices from DAPHNE can be returned as PyTorch & TensorFlow tensors, with the optional function arguments for the compute() function: "asTensorflow: bool", "asPytorch: bool", "shape" (original shape of the tensor). - Added additional frame operations in DaphneLib. - Intended for testing processing of data frames transferred from pandas. - Script-level test cases. - Examples and/or test cases for all the added functions. - Currently, the test cases related to DaphneLib are commented out as they require TensorFlow and PyTorch as dependencies. - Updated the DaphneLib documentation. - Closes daphne-eu#499. - These changes have been committed before in f359a77, but were reverted in 158772a, since the co-author note was forgotten in the commit message, when @pdamme "squash & merge"ed the pull request. - So they were re-commited in 4d4ec47, but there, the newly added files from f359a77 were forgotten, which are added again now. Co-authored-by: Niklas <[email protected]>
- Loading branch information
1 parent
73ff457
commit 3e66092
Showing
41 changed files
with
1,478 additions
and
47 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,63 @@ | ||
# Copyright 2023 The DAPHNE Consortium | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from daphne.context.daphne_context import DaphneContext | ||
import torch | ||
import numpy as np | ||
|
||
dc = DaphneContext() | ||
|
||
print("========== 2D TENSOR EXAMPLE ==========\n") | ||
|
||
# Create data in PyTorch/numpy. | ||
t2d = torch.tensor(np.random.random(size=(2, 4))) | ||
|
||
print("Original 2d tensor in PyTorch:") | ||
print(t2d) | ||
|
||
# Transfer data to DaphneLib (lazily evaluated). | ||
T2D = dc.from_pytorch(t2d) | ||
|
||
print("\nHow DAPHNE sees the 2d tensor from PyTorch:") | ||
T2D.print().compute() | ||
|
||
# Add 100 to each value in T2D. | ||
T2D = T2D + 100.0 | ||
|
||
# Compute in DAPHNE, transfer result back to Python. | ||
print("\nResult of adding 100, back in Python:") | ||
print(T2D.compute(asPyTorch=True)) | ||
|
||
print("\n========== 3D TENSOR EXAMPLE ==========\n") | ||
|
||
# Create data in PyTorch/numpy. | ||
t3d = torch.tensor(np.random.random(size=(2, 2, 2))) | ||
|
||
print("Original 3d tensor in PyTorch:") | ||
print(t3d) | ||
|
||
# Transfer data to DaphneLib (lazily evaluated). | ||
T3D, T3D_shape = dc.from_pytorch(t3d, return_shape=True) | ||
|
||
print("\nHow DAPHNE sees the 3d tensor from PyTorch:") | ||
T3D.print().compute() | ||
|
||
# Add 100 to each value in T3D. | ||
T3D = T3D + 100.0 | ||
|
||
# Compute in DAPHNE, transfer result back to Python. | ||
print("\nResult of adding 100, back in Python:") | ||
print(T3D.compute(asPyTorch=True)) | ||
print("\nResult of adding 100, back in Python (with original shape):") | ||
print(T3D.compute(asPyTorch=True, shape=T3D_shape)) |
Oops, something went wrong.