# ONNX import fails

**URL:** https://community.konduit.ai/t/onnx-import-fails/1626
**Category:** SameDiff
**Created:** [October 1, 2021, 9:19pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626 "2021-10-01T21:19:42Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![craig88](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@craig88](https://community.konduit.ai/u/craig88)
#### Post date: [October 1, 2021, 9:19pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626/1 "2021-10-01T21:19:42Z")

</div>

I’m following this [guide](https://deeplearning4j.konduit.ai/samediff/explanation/model-import-framework)  
problem 1)  
bad link under ‘For implementing your own custom overrides please see here’

problem 2)

I am trying to import a HuggingFace Bigbird ONNX model, when calling:  
SameDiff graph = onnxFrameworkImporter.runImport(file.getAbsolutePath(), Collections.emptyMap());

I get error:

```auto
Exception in thread "main" java.lang.IllegalArgumentException: No import process defined for Unsqueeze
	at org.nd4j.samediff.frameworkimport.registry.OpMappingRegistry.lookupOpMappingProcess(OpMappingRegistry.kt:129)

```

I have downloaded and put under src/main/resources [these files.](https://github.com/eclipse/deeplearning4j/tree/master/nd4j/samediff-import/samediff-import-onnx/src/main/resources)

I’m using JDK1.8, DL4J 1.0.0-M1.1

my save command was:

```auto
torch.onnx.export(model, # model being run
                  torch.tensor(train_input).to(device), # model input (or a tuple for multiple inputs)
                  dataset+"_hf_bigbird.onnx",# where to save the model (can be a file or file-like object)
                  export_params=True, # store the trained parameter weights inside the model file
                  opset_version=11, # the ONNX version to export the model to
                  do_constant_folding=True, # whether to execute constant folding for optimization
                  input_names = ['input'], # the model's input names
                  output_names = ['output'] # the model's output names
                 )

```

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [October 1, 2021, 11:49pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626/2 "2021-10-01T23:49:55Z")

</div>

@craig88 Unsqueeze is missing. I’ll actually be adding it.

For now you can see what’s implemented here:

> <https://github.com/eclipse/deeplearning4j/blob/master/nd4j/samediff-import/samediff-import-onnx/src/main/kotlin/org/nd4j/samediff/frameworkimport/onnx/definitions/OnnxOpDeclarations.kt>

I’ve updated the documentation page with an explanation on how to use it:

> **[Model Import Framework](https://deeplearning4j.konduit.ai/samediff/explanation/model-import-framework)**
>
> Model import framework overview and examples

Please let me know if you want help with mapping Unsqueeze.

Much of the libnd4j framework was implemented for interopping with tensorflow, so you generally can follow the onnx tensorflow backend like here: [onnx-tensorflow/unsqueeze.py at af8b6a9825c288a80b8dd1b6edd1ea0cecd1556e · onnx/onnx-tensorflow · GitHub](https://github.com/onnx/onnx-tensorflow/blob/af8b6a9825c288a80b8dd1b6edd1ea0cecd1556e/onnx_tf/handlers/backend/unsqueeze.py#L16)

Samediff also has the same operations available for you.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [October 4, 2021, 12:47am UTC](https://community.konduit.ai/t/onnx-import-fails/1626/3 "2021-10-04T00:47:04Z")

</div>

@craig88 I added Unsqueeze can be found here:

> <https://github.com/eclipse/deeplearning4j/blob/d5ee31d8c86136c657d562152fcb152727f39395/nd4j/samediff-import/samediff-import-onnx/src/main/kotlin/org/nd4j/samediff/frameworkimport/onnx/definitions/implementations/Unsqueeze.kt>

if you’d like you can pull my branch and test it if you’d like. I am adding quite a few ops in this pull request but unsqueeze should be there for you.

That or I can try to convert your model for you if you can DM me your model.

---

<div class="post-metadata">

### Author: ![craig88](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@craig88](https://community.konduit.ai/u/craig88)
#### Post date: [October 4, 2021, 6:59pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626/4 "2021-10-04T18:59:27Z")

</div>

> [@craig88](#):
>
> onnxFrameworkImporter

Is this via the pom? doesn’t seem to be working:

```auto
        <dependency>
            <groupId>org.nd4j</groupId>
            <artifactId>samediff-import-onnx</artifactId>
            <version>d5ee31d8c8</version>
        </dependency>

```

Here is the [onnx model](https://wildwebwidget.com/temp/shapen_hf_bigbird.zip), it’s just a smoketest.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [October 5, 2021, 12:01am UTC](https://community.konduit.ai/t/onnx-import-fails/1626/5 "2021-10-05T00:01:10Z")

</div>

@craig88 No…where did you get that so I can fix it? It should just be 1.0.0-SNAPSHOT or the latest version 1.0.0-M1.1

---

<div class="post-metadata">

### Author: ![craig88](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@craig88](https://community.konduit.ai/u/craig88)
#### Post date: [October 5, 2021, 2:54pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626/6 "2021-10-05T14:54:55Z")

</div>

Sorry new to Maven; had the naive notion I could use your github branches/tags in the POM dependency. Anyway I’ll wait till the updates are officially released; can use the onnxruntime until then.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [October 6, 2021, 2:03am UTC](https://community.konduit.ai/t/onnx-import-fails/1626/7 "2021-10-06T02:03:35Z")

</div>

@craig88 could you clarify? We have the nd4j-onnxruntime bindings that will allow an easy transition. I’m not sure if you saw those:

> **[deeplearning4j/nd4j/nd4j-onnxruntime at master · eclipse/deeplearning4j](https://github.com/eclipse/deeplearning4j/tree/master/nd4j/nd4j-onnxruntime)**
>
> master/nd4j/nd4j-onnxruntime

For your maven setup, I would seriously consider reading a quickstart to make sure you understand the basics. It won’t take long and will save you lots of headaches and unnecessary detours in the future.  
[https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html)

You can only pull maven dependencies from a maven repository (maven central by default) You can’t just pull random git commits. Those commits and versions have to be manually set and deployed by the library to maven central or a maven repository.

Beyond that, yes you can once this is merged to master.  
In order to embed the unsqueeze implementation I wrote, all you have to do is declare the annotations as listed in the docs there and put it in your project.

That won’t be necessary after this is merged to master since it will be available by default.

There are other ops (sometimes custom to pytorch or other frameworks or even one off ops implemented in research papers) where you need to do the same thing and declare the dependencies in your project in order for it to work. That would allow you to just add a shiv to the import without the need to change the dependencies.

That or again if you want, you can just DM me your model and I’ll convert it for you.  
After that you don’t even need the model import framework. It will be converted to our flatbuffers format which you can then load with Samediff.load(…)

If you want to use the new changes after it’s merged tp master then that’s called snapshots. You just specify 1.0.0-SNAPSHOT with the snapshots repository. You can find that here:

> **[Snapshots](https://deeplearning4j.konduit.ai/multi-project/explanation/snapshots)**
>
> Using daily builds for access to latest Eclipse Deeplearning4j features.

---

<div class="post-metadata">

### Author: ![craig88](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@craig88](https://community.konduit.ai/u/craig88)
#### Post date: [October 6, 2021, 5:10pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626/8 "2021-10-06T17:10:20Z")

</div>

I put in the [Unsqueeze](https://github.com/eclipse/deeplearning4j/blob/d5ee31d8c86136c657d562152fcb152727f39395/nd4j/samediff-import/samediff-import-onnx/src/main/kotlin/org/nd4j/samediff/frameworkimport/onnx/definitions/implementations/Unsqueeze.kt) Op and the [op defs](https://github.com/eclipse/deeplearning4j/blob/master/nd4j/samediff-import/samediff-import-onnx/src/main/kotlin/org/nd4j/samediff/frameworkimport/onnx/definitions/OnnxOpDeclarations.kt) but it is not liking the function signature:

```auto

 override fun preProcess(
        op: SameDiffOp,
        sd: SameDiff,
        attributes: Map<String, Any>,
        descriptor: OpNamespace.OpDescriptor,
        outputNames: List<String>,
        isFinalOutput: Boolean
    ): HookResult {

```

it likes this signature:

```auto
   override fun preProcess(
            op: SameDiffOp, 
            sd: SameDiff, 
            attributes: Map<String, Any>, 
            descriptor: OpNamespace.OpDescriptor): HookResult {

```

I’m using: 1.0-SNAPSHOT

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [October 6, 2021, 10:19pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626/9 "2021-10-06T22:19:38Z")

</div>

@craig88 We just merged some changes to master that updates the signature. That is the old one. For Unsqueeze it should work fine.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [October 13, 2021, 1:07pm UTC](https://community.konduit.ai/t/onnx-import-fails/1626/10 "2021-10-13T13:07:48Z")

</div>

@craig88 JFYI snapshots should have this now if you want to try it.
