# Save Flux model to ONNX?

**URL:** <https://discourse.julialang.org/t/save-flux-model-to-onnx/70335>\
**Category:** General Usage\
**Created:** [October 25, 2021, 1:13pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335 "2021-10-25T13:13:58Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 25, 2021, 1:13pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/1 "2021-10-25T13:13:58Z")

</div>

How to save a Flux model to ONNX? Is there currently a way to do it?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [October 25, 2021, 1:19pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/2 "2021-10-25T13:19:08Z")

</div>

Seems to be under rework, have never tried it but you could probably just grab the last working version and later when they have reworked update to that.  
[https://github.com/FluxML/ONNX.jl](https://github.com/FluxML/ONNX.jl)

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 25, 2021, 1:40pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/3 "2021-10-25T13:40:53Z")

</div>

Did ONNX.jl actually support saving models? From looking at the [repo](https://github.com/FluxML/ONNX.jl/tree/05bf9d178a9aad5c2e8550b391a356778e156eee) it seems it supported only loading?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [October 25, 2021, 3:44pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/4 "2021-10-25T15:44:47Z")

</div>

That might be very true, didn’t really look into it 😓

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [October 25, 2021, 9:24pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/5 "2021-10-25T21:24:34Z")

</div>

We plan to support both - saving and loading. The exact timeframe however is unclear at the moment.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 26, 2021, 6:49am UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/6 "2021-10-26T06:49:18Z")

</div>

Thanks! That would be awesome. Meanwhile I would still be interested in a hacky solution that “works” right now.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [October 27, 2021, 4:33pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/7 "2021-10-27T16:33:19Z")

</div>

Unfortunately there’s no quick and dirty solution for getting ONNX support off the ground, modulo not using julia ML libraries. It’s a massive API to support, so aside from instantiating your own protobufs with ONNX.jl and manually writing weight params into them, there’s not much else that can be done.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 7, 2022, 5:03am UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/8 "2022-07-07T05:03:25Z")

</div>

is there anyway to do it via PyCall? the model itself isn’t large

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [July 7, 2022, 8:01pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/9 "2022-07-07T20:01:26Z")

</div>

Loading weights from an ONNX file is relatively easy, the hard part is to map the operations to Flux’s layers. For instance, the order of data dimensions in ONNX and Julia is reversed, indexing starts from different points, function signatures are just different sometimes, etc. PyCall can help with very few of these issues.

Fortunately, we move forward with ONNX.jl - many core operators are already supported, and now we have a [tutorial](https://github.com/FluxML/ONNX.jl/blob/master/CONTRIBUTE.md) on adding the new ones. I guess the simplest way is to just try loading your graph and implement / post feature requests for the missing parts.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 7, 2022, 8:46pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/10 "2022-07-07T20:46:18Z")

</div>

maybe it would be nice to have a short tutorial / test for saving a Flux chain?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [July 7, 2022, 9:15pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/11 "2022-07-07T21:15:02Z")

</div>

We don’t have integration with Flux yet, you’ll need to manually construct a `Ghost.Tape` with Base and NNlib primitives - see [Ghost tutorial](https://dfdx.github.io/Ghost.jl/dev/tape/#Transformations) and the list of [supported ops](https://github.com/FluxML/ONNX.jl/blob/master/src/save.jl) for details. In the future, it will be automated, but at the moment we mostly focus on adding new operators.

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [July 7, 2022, 11:03pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/12 "2022-07-07T23:03:46Z")

</div>

@dfdx : Doesn’t ONNX.jl support export of arbitrary functions (as long as they hit the primitives), including Flux layers?

Fwiw, I’m still maintaining [GitHub - DrChainsaw/ONNXNaiveNASflux.jl: Import/export ONNX models](https://github.com/DrChainsaw/ONNXNaiveNASflux.jl) which has primitives for most Flux layers, both export and import.

Importing to `Flux.Chain` is however not supported and I believe it is a bit of the same difficulty that ONNX.jl will face with imports: Chain is just so different compared to the more generic graph used by ONNX and I don’t think it can even represent all models which are representable in ONNX.

Exporting a `Flux.Chain` is ofc no problem.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 7, 2022, 11:19pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/13 "2022-07-07T23:19:20Z")

</div>

so I can export a `Flux.Chain` to be used by, say, Keras?

I’m using Julia but I want the collaborators who use C++ / Python to be able to use our trained model. I can separately save the Flux model in addition to ONNX as BSON. So unable to import to Flux.Chain is not a big problem for me

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [July 7, 2022, 11:28pm UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/14 "2022-07-07T23:28:12Z")

</div>

> [@jling](#):
>
> so I can export a `Flux.Chain` to be used by, say, Keras?

Yes, as long as there is a way to import an ONNX model in Keras.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [November 3, 2023, 3:06am UTC](https://discourse.julialang.org/t/save-flux-model-to-onnx/70335/15 "2023-11-03T03:06:57Z")

</div>

Hi @jling, did you manage to export a Flux model or even better Lux model to ONNX using ONNXNaiveNASflux.jl or any other packages?
