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

Question: What is the best practice for working with PyTorch? #533

Open
famura opened this issue Jun 26, 2023 · 2 comments
Open

Question: What is the best practice for working with PyTorch? #533

famura opened this issue Jun 26, 2023 · 2 comments

Comments

@famura
Copy link

famura commented Jun 26, 2023

Hi PyJulia team,

I'm just getting started with porting a Julia package to a Python module. On the Python side, all the interaction will be with PyTorch tensors.

I noticed that one can pass numpy arrays and lists to Julia, which will then be cast to the built-in Array type. AFAIK, this is not possible with PyTorch tensors. A simple workaround would be to convert the tensors from torch to numpy (and the reverse). However, I am curious if there is a more elegant way. How can I control this mapping?

Best regards,
Fabio

@mkitti
Copy link
Member

mkitti commented Jun 26, 2023

I believe any Python array that uses the buffer protocol should be wrappable by Julia without copying. From the Julia perspective, we just need the dimensions, (d)type, and pointer for the data structure. We could then use Base.unsafe_wrap or its equivalent.

https://docs.julialang.org/en/v1/base/c/#Base.unsafe_wrap-Union{Tuple{N},%20Tuple{T},%20Tuple{Union{Type{Array},%20Type{Array{T}},%20Type{Array{T,%20N}}},%20Ptr{T},%20Tuple{Vararg{Int64,%20N}}}}%20where%20{T,%20N}

It is unclear to me what the status is of that in pytorch. The following issue appears relevant.
pytorch/pytorch#19143

@famura
Copy link
Author

famura commented Jun 27, 2023

Thank you @mkitti for the hints and explanation. I'll have a look at them.

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

No branches or pull requests

2 participants