# PyTorch and Julia

**URL:** https://discourse.julialang.org/t/pytorch-and-julia/22211
**Category:** Machine Learning
**Created:** [March 22, 2019, 6:26pm UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211 "2019-03-22T18:26:40Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [March 22, 2019, 3:26pm UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/1 "2019-03-22T15:26:55Z")

</div>

I was thinking about something related, but not about numpy, it’s about pytorch. As many folks know that PyTorch is implementing something seems to be very similar to Julia beneath the Python interface to enable JIT compilation, which is called the [TorchScript](https://pytorch.org/docs/stable/jit.html).

So what I was thinking is why not use Julia directly, we have `pyjulia` and `Flux` already, the rest won’t be hard, and this will benefits both Julia and PyTorch community in the following ways:

What torch will gain:

- Julia has an abundant array ecosystem, which includes StaticArrays, OffsetArray, NamedArray, etc. By providing pytorch a Julia backend people in torch side will be able to use these features, especially, NamedArray has been lying in Julia for 4~5 years and people in torch community find this is quite usefully recently: [Tensor Considered Harmful](http://nlp.seas.harvard.edu/NamedTensor)

- Although, I know torch community has some people working on TPU, but that’s not done yet, right? With Julia backend, torch people can use TPU as well.

- It’ll be definitely easier to implement new operators directly from Julia which is more mature than torch script (or maybe because I know Julia, but you know, at least much easier than writing C++)

What Julia will gain:

- pytorch, as a another large open source project used by many people and companies, I think this will bring this community more people
- Since, in today’s machine learning research community, a lot new research is done in torch based on its previous work which is also done in torch. This will make those old mature algorithm implementations just work for Julia, we can use them from Julia side, although this might be a bit ugly, but this brings a lot new models to Julia side.
- finally, as a machine learning researcher, I have to say, because other people is using python, I sometimes have to write it. By providing a Julia backend for torch, this will make things smoother. At least for myself, I did have a painful time working on custom pytorch tensors in C++, which it might be just a few hundred lines in Julia.

What need to do from Julia side:

- I think one of this year’s GSoC Project is quite important, which will make calling Julia from Python much easier ([https://julialang.org/soc/projects/compiler.html](https://julialang.org/soc/projects/compiler.html))
- Conditional dependencies in Pkg, this is quite important to support different hardware, I think it wouldn’t make sense to have users load `CuArrays` etc. with `Require.jl` from Python side separately. Installing the package with `cuda=true` is more explicit and simpler. Well, I tried to push people pay attention and start discussing details about this many times, cuz this is a crucial feature not only for this project but for most deep learning project.

Goal:

- a torch compatible python interface to the Julia side, might need a custom row-wise array, but it’ll be just a wrapper of `Array`.

And maybe there’ll be some other corner cases to make it compatible with `torch` (note: it’s about compatible, not re-write another torch, the functionalities of tensors, AD are already in Julia, just to make it compatible with pytorch Python interface and ship it through `conda/pip/Pkg`), it should be a Python frontend of a Julia AD/machine learning package (say Zygote + Flux).

I guess we could come up with some proof of concept package first (well, I’m working on several Julia packages recently, so I’d say I’ll try this a bit later in the summer, maybe just during JuliaCon).

I don’t if people in this field feels this similar needs with me.

I’ll post updates once I have some work on this.

* * *

(edited: this was for another topic)

But again, yeah, I agree, numpy folks did a great job and in my practice if you are just using what numpy has, it is as fast as Julia with MKL. But for Julia, the thing is, we have not only `Array`, but many many custom arrays and custom algorithms with a unified interface. (Like NamedArray, you never find so many custom arrays in Python world, because it’s hard to do etc.)

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [March 22, 2019, 6:26pm UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/2 "2019-03-22T18:26:40Z")

</div>

@Roger-luo I think your idea is very interesting (Indeed PyTorch came from Torch, a Lua library).

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [March 22, 2019, 7:52pm UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/3 "2019-03-22T19:52:46Z")

</div>

I really like this idea. I think there’s a lot to gain for the machine learning community with this approach.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 23, 2019, 2:13am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/4 "2019-03-23T02:13:21Z")

</div>

So, your plan is to write (1) a TorchScript-to-Julia transpiler and (2) a runtime library required to execute transpiled TorchScript in Julia using Flux? Then, the Python users can invoke it via PyJulia and Julia users can directly load it?

> [@Roger-luo](#):
>
> Conditional dependencies in Pkg, this is quite important to support different hardware, I think it wouldn’t make sense to have users load `CuArrays` etc. with `Require.jl` from Python side separately. Installing the package with `cuda=true` is more explicit and simpler.

In case of this project, I think you can simply do this via (say) `jltorch.install(cuda=true)` like [`diffeqpy.install()`](https://github.com/JuliaDiffEq/diffeqpy#installation) which simply invokes `Pkg.add(["PyCall", "DifferentialEquations"])`.

> [@Roger-luo](#):
>
> What need to do from Julia side:

I’d say the largest obstacle to improve Julia-Python interaction is precompilation cache handling. Here are some pointers for the changes required in Julia:

> <https://github.com/JuliaLang/julia/issues/27418>
>
> Precompile files are currently stored only based on the UUID of the package.
> So… if you change your project it is likely that you will have to recompile everything. And then again when you swap back etc.
> This will be very annoying for people trying to use multiple packages and people will likely just use one mega project like before.
> https://github.com/JuliaLang/julia/pull/26165 also removed any possibility for users to change the precompile path so there is no way to workaround this right now.
> 
> We should be smarter how we save precompile file to reduce the amount of recompilation needed. A very simple system is to just use one precompile directory for each project but that might be a bit wasteful since it is theoretically possible to share compilation files between projects.

> <https://github.com/JuliaLang/julia/issues/28518>
>
> I'm moving the discussion from https://github.com/JuliaPy/pyjulia/issues/173 to …gather more attention. I also describe in the last paragraphs how it may help not only PyJulia users but also other Julia users.
> 
> (@stevengj, @Keno, please correct me or add more details if I'm missing something)
> 
> Prior to Julia 0.7, PyJulia users could use multiple Python interpreters without rebuilding PyCall.jl (PyJulia depends heavily on PyCall.jl) against each Python interpreters. Considering it's a common practice to have multiple Python virtual environments (with venv/virtualenv/conda), being able to use PyJulia without recompiling PyCall.jl every time is an important feature. However, supporting this feature in Julia ≥ 0.7 seems hard.
> 
> (Some details: Just to be clear, not all PyJulia users will notice this effect. Notably, Windows or macOS users only using Python 3 (or only using Python 2...) are unaffected. Linux users only using dynamically linked Python 3 (or 2) are also fine. However, Linux users using statically linked Python, such as the ones distributed by Ubuntu and Anaconda, will not be able to use PyJulia. I think those platforms are popular enough to pay a special attention.)
> 
> PyJulia relies on \`Base.LOAD\_CACHE\_PATH\` in Julia 0.6 to separate the cache of PyCall.jl. It inserts a private path to \`Base.LOAD\_CACHE\_PATH\[1\]\` if the Python interpreter in which PyJulia is loaded is incompatible with the Python interpreter with which PyCall.jl is configured:
> 
> \`\`\`python
> \# Add a private cache directory. PyCall needs a different
> \# configuration and so do any packages that depend on it.
> self.\_call(u"unshift!(Base.LOAD\_CACHE\_PATH, abspath(Pkg.Dir.\_pkgroot()," +
> "\\"lib\\", \\"pyjulia%s-v$(VERSION.major).$(VERSION.minor)\\"))" % sys.version\_info\[0\])
> \`\`\`
> 
> \--- https://github.com/JuliaPy/pyjulia/blob/18d98e5b1b616a4d663273cc36cdd835ab0b33da/julia/core.py#L357-L360
> 
> This "hack" doesn't work in Julia ≥ 0.7 as \`Base.LOAD\_CACHE\_PATH\` is gone now. Also, reading how \`cache\_file\_entry\` is used to locate the \`.ji\` files, I couldn't find a way to implement a new "hack" other than duplicating the whole \`DEPOT\_PATH\[1\]\`.
> 
> https://github.com/JuliaLang/julia/blob/d038f2f4997e4a4544c790d05a7f54d316200bd6/base/loading.jl#L594-L608
> 
> But creating a new \`DEPOT\_PATH\[1\]\` for each incompatible Python interpreter type is rather overkill and ignores Pkg3 working hard to de-duplicate resources. It would be really nice to have a direct support from Julia and Pkg3, if it is feasible.
> 
> \### An idea: "build option" support
> 
> I think one way to directly support it is to add "build option" support in Julia and Pkg3. What I mean by "build option" is something that altering it changes the precompilation cache. Currently Julia packages do this via environment variables. Concrete examples are \`PYTHON\` of PyCall.jl and \`CONDA\_JL\_HOME\` of Conda.jl. If those options can be directly configured by \`Project.toml\` file, and if compilation cache paths depend on such options, PyJulia can just create a new \`Project.toml\` with appropriate options and launch a new Julia interpreter. Importantly, the compilation cache path of a package has to depend also on the build options of all its (direct and indirect) dependencies.
> 
> I think the "build option" support can help other Julia users (who do not use PyJulia) too. For example, you can change \`CONDA\_JL\_HOME\` for a particular \`Project.toml\` to tweak versions of external libraries provided via \`Conda.jl\` without contaminating other projects. (Maybe similar benefits can be gained via BinaryProvider.jl too?) I think supporting 32-bit and 64-bit Julia https://github.com/JuliaLang/Pkg.jl/issues/93 can also be done by changing "build option" of the "root" \`julia\` package. I suppose then it makes sense for \`deps\` directories to depend on "build options" too (https://github.com/JuliaLang/Pkg.jl/issues/93#issuecomment-354577967).

> <https://github.com/JuliaLang/Juleps/issues/38>
>
> It would be nice to have uniform way to specify package configuration options. …Currently, I'm using environment variables for this (e.g. PYTHON in PyCall), but that is somewhat non-Julian. More importantly, it requires some manual effort for me to save the option in a file so that it is "remembered" when you do \`Pkg.update\` etcetera even if the environment variable is no longer set. Many packages will get this wrong, and even if they get it right it is a lot of duplication of effort.
> 
> My suggestion would be that the package TOML file should have something like:
> \`\`\`
> \[option.python\]
> value = "python3" # default
> \`\`\`
> 
> The user interface would be something like:
> 
> \* \`Pkg.option("PyCall", :python)\` — return the current choice (saved in a file somewhere)
> \* \`Pkg.option("PyCall", python="python2")\` — change the option, maybe rebuilding automatically unless this is called \*by\* the package \*during\* the build
> \* \`Pkg.add\` and \`Pkg.build\` will accept \`python="python2"\` keyword arguments to set options when installing/building.
> 
> The value of all options would be saved in an \`options.toml\` file somewhere.
> 
> A related mechanism would be used for package alternatives, see #37.

> <https://github.com/JuliaLang/julia/pull/30065>
>
> In PyJulia, we've been using @Keno's trick (\`fake-julia\`) to force Julia to use …a Python script as the Julia process launched for precompilation: https://github.com/JuliaPy/pyjulia/tree/v0.2.0/julia/fake-julia. This is required for supporting Python installed by Debian-family such as Ubuntu and also by Anaconda.
> 
> In Julia 0.6, \`fake-julia\` worked by setting the environment variable \`JULIA\_HOME\` to a directory in which a fake executable \`julia\` exists. Applying similar trick to modify \`Sys.BINDIR\` via \`JULIA\_BINDIR\` in Julia 1.0 is problematic because \`Sys.BINDIR\` is used not only in \`Base.julia\_cmd\` but also in other places such as \`Pkg.Types.stdlib\_dir\` and most importantly for initializing \`DEPOT\_PATH\`. So, I request to add an API to configure what \`Base.julia\_cmd\` returns. It could be useful outside PyJulia usage. For example, it would make it possible to control what command line flags are used in the subprocesses by default (e.g., Pkg.build, Distributed, PackageCompiler).
> 
> I tested the concept by \[monkey-patching Julia \`Base\`\](https://github.com/tkf/julia-venv/blob/4ccefb119a7c34db96fe2aee3bafe9f4c224ad24/src/julia\_venv/JuliaVenv.jl#L46-L57) and it worked in Julia 1.0 as expected. However, the monkey-patching approach requires to know exactly when the precompilation happens which is impossible.

I also want to see signal handler situation to be improved:

> <https://github.com/JuliaPy/pyjulia/issues/211>
>
> Currently PyJulia does not support \`KeyboardInterrupt\`. That is to say, in long…-running Python and Julia computation, there is no way to terminate a sub-computation by catching \`KeyboardInterrupt\` as done in normal Python programming.
> 
> Aside: Recommended way to cancel current input in REPL is to use IPython 7.0 or above. Ctrl-C would cancel the input without causing SIGINT.
> 
> This problem is previously mentioned in: https://github.com/JuliaPy/pyjulia/issues/189, https://github.com/JuliaPy/pyjulia/issues/185#issuecomment-418614274
> 
> What follows is a summary of my understanding:
> 
> When PyJulia is initialized, libjulia takes over all signal handling. \[I couldn't find a way to disable this behavior\](https://discourse.julialang.org/t/how-can-i-handle-sigint-when-using-libjulia-c-api-in-python-ctypes/14794). Julia documentation mentions that \["Julia requires a few signal to function property."\](https://docs.julialang.org/en/latest/devdocs/debuggingtips/#Dealing-with-signals-1) so it probably would not have something like \[\`Py\_InitializeEx\`\](https://docs.python.org/3/c-api/init.html#c.Py\_InitializeEx) to initialize libjulia without installing signal handlers anytime soon.
> 
> What would be more easily achievable is to let Julia translate SIGINT to \`InterruptException\` and then let PyCall to translate it to Python's \`KeyboardInterrupt\`. I have implemented it in https://github.com/JuliaPy/PyCall.jl/pull/574 but it introduced \[a bug\](https://github.com/JuliaPy/PyCall.jl/pull/574#issuecomment-425322221) which is hard to track. I reported it in Julia: https://github.com/JuliaLang/julia/issues/29498. Note that this strategy is not perfect because long-running pure-Python computation or I/O cannot respond to SIGINT. If Julia implements some kind of signal handling https://github.com/JuliaLang/julia/issues/14675 then maybe we can call \[\`PyErr\_SetInterrupt\`\](https://docs.python.org/3/c-api/exceptions.html#c.PyErr\_SetInterrupt) (which does not need GIL) from it.

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [March 23, 2019, 8:58am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/5 "2019-03-23T08:58:09Z")

</div>

Thanks for your suggestions, this is very helpful.

I mean directly map part of the torch function to Julia via pyjulia. There won’t be a torch script anymore. Like what diffeqpy do, amd yes, torch user could still use their old code, but the backend is changed (backend I mean ATen and Autograd in C++)

---

<div class="post-metadata">

### Author: ![Balinus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/balinus/32/243_2.png) [@Balinus](https://discourse.julialang.org/u/Balinus)
#### Post date: [March 23, 2019, 8:42pm UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/6 "2019-03-23T20:42:06Z")

</div>

I think that `pyjulia` is a really strategic package and core Julia should ease the pain of using it. I know some projects that consider the use of Julia for hot-loops and right now, the set-up of `pyjulia` is far from plug and play and some people is reluctant to chose this approach. Hopefully, this will be easier as development goes by! Anyway, right now it’s usable and proof-of-concepts can be done to highlights the possibilities.

---

<div class="post-metadata">

### Author: ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)
#### Post date: [March 23, 2019, 9:05pm UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/7 "2019-03-23T21:05:28Z")

</div>

Personally I’d be very interested to see some development of the named tensor concept. I think the current approach to this is AxisArrays. But as I I’ve looked over the most recent work in the package it seems there’s a bit of collective discontent with bits of the current API it has (maybe I’m just projecting a little 😉).

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 24, 2019, 3:01am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/8 "2019-03-24T03:01:20Z")

</div>

I see. Now I realized that’s what you said in your first post. My bad. Transpiling was the topic in the original thread so I misread your post.

I don’t use TorchScript so I don’t have the full picture. But is TorchScript only used by PyTorch experts? If TorchScript is something used also by ML researchers, and if your plan is to use Julia language instead of TorchScript, my concern is that they may not use this backend if they feel they don’t have time to learn a new programming language.

But I think using PyCall/PyJulia only for high-level Julia-Python interaction is a good approach in general. I just don’t know PyTorch enough to see how appealing it would be for PyTorch users.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 24, 2019, 3:39am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/9 "2019-03-24T03:39:08Z")

</div>

I think the shortest path for making PyJulia easier to use is [RFC: a "workaround" for the multi-project precompilation cache problem without long-term code debt](https://discourse.julialang.org/t/rfc-a-workaround-for-the-multi-project-precompilation-cache-problem-without-long-term-code-debt/22233) with [Make PyCall.jl AOT-compilable by tkf · Pull Request #651 · JuliaPy/PyCall.jl · GitHub](https://github.com/JuliaPy/PyCall.jl/pull/651). (The idea is to create a system image for PyJulia and let Julia automatically use precompilation cache dedicated to PyJulia.) Maybe you can help me in [that thread](https://discourse.julialang.org/t/22233) 🙂

I also added Julia option support recently (available only in master). You can use `Julia(compiled_modules=False)` to workaround the compilation cache problem: [Troubleshooting — PyJulia 0.5.8.dev documentation](https://pyjulia.readthedocs.io/en/latest/troubleshooting.html#turn-off-compilation-cache) It would make PyJulia setup plug-and-play _if_ you are OK with waiting for precompilation for each Python process (yeah, I know this is not super practical for all purposes).

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [March 24, 2019, 6:09am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/10 "2019-03-24T06:09:38Z")

</div>

Well, TorchScript is kinda a python like new language as well, and it is not as mature as Julia yet (not every command you write intuitively is working), which is because again it’s hard to accelerate something like Python. I think it’s used for people writing fast custom operations while not writing c++. So it depends on what you need, currently I don’t see much people using torch script, because people don’t want to learn another new language… (at least among my collaborators, they’d rather writing c++, but some of these people know Julia already)

But Julia comes in as a nice option when someone really need to extend the functionality, define a lot customized tensor, data type (e.g complex number tensor, dual number tensor etc.) I don’t think this is doable in torchscript yet, and not straight forward in C++ (pytorch has data type hooks now, but yet not mature enough). I think this will be the best option and make this kind of user willing to learn a new language :-).

Well it’s hard to make someone who is OK with Python to learn another language, but it’s always easy to persuade a multilingual guy to learn another language who knows Python.

So I guess it will depends how fast and how mature we could make it.

And I notice that diffeqpy can use numba with Julia, is this something still working today?

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 24, 2019, 7:03am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/11 "2019-03-24T07:03:07Z")

</div>

> [@Roger-luo](#):
>
> Well, TorchScript is kinda a python like new language as well, and it is not as mature as Julia yet

As TorchScript is a subset of Python, I thought you can use it also in good old define-by-run mode? If that’s the case, I suppose you can use all the mature Python tools like a variety of debuggers? (Even though the TorchScript compiler may not be matured yet.)

> [@Roger-luo](#):
>
> currently I don’t see much people using torch script, because people don’t want to learn another new language… (at least among my collaborators, they’d rather writing c++, but some of these people know Julia already)
> 
> But Julia comes in as a nice option when someone really need to extend the functionality, define a lot customized tensor, data type (e.g complex number tensor, dual number tensor etc.) I don’t think this is doable in torchscript yet, and not straight forward in C++

I see. It makes sense now. Thanks a lot for explaining this.

My another concern is the overhead of calling Julia function from Python. I thought PyTorch devs wanted to develop JIT because even calling C extension from Python had some overhead (but I’m not sure if this info is fresh and correct). But this probably can be improved once enough people are interested in this. There is already optimization you can do like `pyfunction`.

> [@Roger-luo](#):
>
> And I notice that diffeqpy can use numba with Julia, is this something still working today?

I don’t think there is much benefit in using numba with diffeqpy _unless_ the computing the derivatives is so computation-intensive such that overhead of calling Python function from Julia is neglegible _and_ such computation-intensive code can benefit from using numba (i.e., way faster than composing numpy functions). But @ChrisRackauckas may know other use-cases.

I think a better approach would be to expose a Pythonic API for [ModelingToolkit.jl](https://github.com/JuliaDiffEq/ModelingToolkit.jl) or create [SymPy-to-Julia transpiler specialized for diffeq](https://github.com/JuliaDiffEq/diffeqpy/issues/32). Having said that, it would be very interesting (at least in purely curiosity-based sense) if you can inline LLVM IR generated by Numba into Julia function.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 26, 2019, 5:03am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/12 "2019-03-26T05:03:47Z")

</div>

> [@tkf](#):
>
> I don’t think there is much benefit in using numba with diffeqpy _unless_ the computing the derivatives is so computation-intensive such that overhead of calling Python function from Julia is neglegible _and_ such computation-intensive code can benefit from using numba (i.e., way faster than composing numpy functions). But @ChrisRackauckas may know other use-cases.

Yeah, that is the use case, and it should still work. If it’s a PDE or stochastic PDE discretization then this makes a lot of sense. If it’s a 3 ODE system then this overhead matters a lot. So it’s just a use case kind of thing. The asymtopically large problems are more algorithm driven (since implementation-wise everything is dominated by the sparse linear solver cost) and so diffeqr and diffeqpy are very effective interfaces to share our algorithms (since in this case, being algorithmically efficient matters a lot more than low level efficiency). This makes the performance of the bridge use-case dependent, similar to NumPy though.

> [@tkf](#):
>
> I think a better approach would be to expose a Pythonic API for [ModelingToolkit.jl](https://github.com/JuliaDiffEq/ModelingToolkit.jl) or create [SymPy-to-Julia transpiler specialized for diffeq](https://github.com/JuliaDiffEq/diffeqpy/issues/32). Having said that, it would be very interesting (at least in purely curiosity-based sense) if you can inline LLVM IR generated by Numba into Julia function.

Yup, I think this is the direction to go. And once we are sufficiently advanced here, we could just have some kind of DSL file format where the differential equation can be written in a way that can be used from any of the bridges we make to other languages. The core of DiffEq will always be Julia, but this would then be a nice way to make all of the advances useful everywhere else.

I think this is a good place to clarify my stance on the “language wars”. DifferentialEquations.jl and its components are built in Julia because of the competitive advantage we get for being able to make full use of the compilation structures of the language. Not just speed but also a lot of features, like easily writing compilers like ModelingToolkit, full language AD systems like Zygote, tooling built with Cassette, etc. that just aren’t available elsewhere. However, our dedication is to the science and not necessarily the language. I plan to continue to make bridges to allow DiffEq to be used from other languages. Tooling like the ability to compile OrdinaryDiffEq.jl on Float64s to a binary for use from Python/R will go a long way to making this nicer. Julia’s development advantages also make it a great language for building libraries, and we should share that as much as possible. Power users will still come to Julia since there will always be more you can do when using it from Julia of course (some of the ML integration is an example, in cases not using adjoint sensitivity analysis). And honestly, these tools have been great Julia recruitment tools not just for power users.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 27, 2019, 2:46am UTC](https://discourse.julialang.org/t/pytorch-and-julia/22211/13 "2019-03-27T02:46:04Z")

</div>

> [@ChrisRackauckas](#):
>
> we could just have some kind of DSL file format where the differential equation can be written in a way that can be used from any of the bridges we make to other languages

This sounds like a great approach. But one thing I’ve been wondering is that in DSL you tend to lose benefits in the host language like debugger and how to deal with that. This mirrors different debuggability in define-by-run and static graph approach of NN frameworks. DSL is an obvious way forward for readability and writability for complex mathematical models. It would be great if debuggability (and other benefits in Julia like `@code_*`) can be recovered. Maybe that’s as easy as recording and attaching `LineNumberNode` when generating function. I think this would benefit Python/R/… users, too. I don’t think it’s too crazy to launch Julia debugger from Python debugger.

> [@ChrisRackauckas](#):
>
> However, our dedication is to the science and not necessarily the language.

👏 👏 👏
