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

How to export a fixed shape ONNX model? #78

Open
wangjl1993 opened this issue Dec 20, 2024 · 0 comments
Open

How to export a fixed shape ONNX model? #78

wangjl1993 opened this issue Dec 20, 2024 · 0 comments

Comments

@wangjl1993
Copy link

wangjl1993 commented Dec 20, 2024

Great work!
I would like to export an ONNX model with a fixed shape (10 seconds input). However, I noticed that the input and output dimensions of the cache_decoder increase with each decoding step. Is there any way to solve this issue? My code like this:

```python
model_name = "moonshine/tiny"
model = moonshine.load_model(model_name)
output_dir = "onnx_models"
Path(output_dir).mkdir(parents=True, exist_ok=True)
convert_and_store(
    model.preprocessor.preprocess,
    input_signature=[tf.TensorSpec([1, 16_0000], dtype=tf.float32)],  # batch=1, seconds=10s
    output_file=f"{output_dir}/preprocess.onnx",
)

seq_len_spec = tf.TensorSpec([1], dtype=tf.int32)

convert_and_store(
    model.encoder.encoder,
    input_signature=[
        tf.TensorSpec([1, 415, model.dim], dtype=tf.float32),
        seq_len_spec,
    ],
    output_file=f"{output_dir}/encode.onnx",
)

input_spec = tf.TensorSpec([1, 1], dtype=tf.int32)
context_spec = tf.TensorSpec([1, 415, model.dim], dtype=tf.float32)
cache_spec = model.dec_n_layers * [
    tf.TensorSpec([1, 1, model.n_head, model.inner_dim // model.n_head], dtype=tf.float32,),
    tf.TensorSpec([1, 1, model.n_head, model.inner_dim // model.n_head], dtype=tf.float32,),
    tf.TensorSpec([1, 415, model.n_head, model.inner_dim // model.n_head],dtype=tf.float32),
    tf.TensorSpec([1, 415, model.n_head, model.inner_dim // model.n_head],dtype=tf.float32)
] 
print(model.dim, model.n_head, model.dec_n_layers, model.inner_dim, model.dec_n_layers)
convert_and_store(
    model.decoder.uncached_call,
    input_signature=[input_spec, context_spec, seq_len_spec],
    output_file=f"{output_dir}/uncached_decode.onnx",
)

convert_and_store(
    model.decoder.cached_call,
    input_signature=[input_spec, context_spec, seq_len_spec] + cache_spec,
    output_file=f"{output_dir}/cached_decode.onnx",
)

```

Thanks!

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

1 participant