How do you use Pytorch model's function in Onnx to get output instead of model.forward() function
TL;DR: How can I use model.whatever_function(input) instead of model.forward(input) for the onnxruntime?
I use CLIP embedding to create embedding for my Image and texts as:
Code is from the official git merge
! pip install ftfy regex tqdm
! pip install git+https://github.com/openai/CLIP.git

import clip
import torch

device = 'cuda' if torch.cuda.is_available() else 'cpu'

model, preprocess = clip.load(&quot;RN50&quot;, device=device) # Load any model
model = model.eval() # Inference Only

img_size = model.visual.input_resolution
dummy_image = torch.randn(10, 3, img_size, img_size).to(device)
image_embedding = model.encode_image(dummy_image).to(device))

dummy_texts = clip.tokenize([&quot;quick brown fox&quot;, &quot;lorem ipsum&quot;]).to(device)
model.encode_text(dummy_texts)

and it works fine giving me [Batch, 1024] tensors for both for the loaded model.
Now I have quantized my model in Onnx as:
model.forward(dummy_image,dummy_texts) # Original CLIP result (1)

torch.onnx.export(model, (dummy_image, dummy_texts), &quot;model.onnx&quot;, export_params=True,
  input_names=[&quot;IMAGE&quot;, &quot;TEXT&quot;],
  output_names=[&quot;LOGITS_PER_IMAGE&quot;, &quot;LOGITS_PER_TEXT&quot;],
  opset_version=14,
  dynamic_axes={
      &quot;IMAGE&quot;: {
          0: &quot;image_batch_size&quot;,
      },
      &quot;TEXT&quot;: {
          0: &quot;text_batch_size&quot;,
      },
      &quot;LOGITS_PER_IMAGE&quot;: {
          0: &quot;image_batch_size&quot;,
          1: &quot;text_batch_size&quot;,
      },
      &quot;LOGITS_PER_TEXT&quot;: {
          0: &quot;text_batch_size&quot;,
          1: &quot;image_batch_size&quot;,
      },
  }
)

and the model is saved.
When I test the model as :
# Now run onnxruntime to verify
import onnxruntime as ort

ort_sess = ort.InferenceSession(&quot;model.onnx&quot;)
result=ort_sess.run([&quot;LOGITS_PER_IMAGE&quot;, &quot;LOGITS_PER_TEXT&quot;], 
  {&quot;IMAGE&quot;: dummy_image.numpy(), &quot;TEXT&quot;: dummy_texts.numpy()})

It gives me a list of length 2, one for each image and text and the result[0] has shape of [Batch,2].

If your encode_image on your module isn't calling forward then nothing is stopping you from overriding forward before exporting to Onnx:
&gt;&gt;&gt; model.forward = model.encode_image
&gt;&gt;&gt; torch.onnx.export(model, (dummy_image, dummy_texts), &quot;model.onnx&quot;, ...))

