Dumps intermediate results of a torch model

Looking for discrepancies is quickly annoying. Discrepancies come from two results obtained with the same models implemented in two different ways, pytorch and onnx. Models are big so where do they come from? That’s the unavoidable question. Unless there is an obvious reason, the only way is to compare intermediate outputs alon the computation. The first step into that direction is to dump the intermediate results coming from pytorch. We use onnx_diagnostic.helpers.torch_helper.steal_forward() for that.

A simple LLM Model

See onnx_diagnostic.helpers.torch_helper.dummy_llm() for its definition. It is mostly used for unit test or example.

import numpy as np
import pandas
import onnx
import torch
import onnxruntime
from onnx_array_api.plotting.graphviz_helper import plot_dot
from onnx_diagnostic import doc
from onnx_diagnostic.helpers import max_diff, string_diff, string_type
from onnx_diagnostic.helpers.torch_helper import dummy_llm, steal_forward
from onnx_diagnostic.helpers.mini_onnx_builder import create_input_tensors_from_onnx_model
from onnx_diagnostic.reference import OnnxruntimeEvaluator, ReportResultComparison


model, inputs, ds = dummy_llm(dynamic_shapes=True)

We use float16.

Let’s check.

print(f"type(model)={type(model)}")
print(f"inputs={string_type(inputs, with_shape=True)}")
print(f"ds={string_type(ds, with_shape=True)}")
type(model)=<class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.LLM'>
inputs=(T7s2x30,)
ds=dict(input_ids:{0:Dim(batch),1:Dim(length)})

It contains the following submodules.

for name, mod in model.named_modules():
    print(f"- {name}: {type(mod)}")
- : <class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.LLM'>
- embedding: <class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.Embedding'>
- embedding.embedding: <class 'torch.nn.modules.sparse.Embedding'>
- embedding.pe: <class 'torch.nn.modules.sparse.Embedding'>
- decoder: <class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.DecoderLayer'>
- decoder.attention: <class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.MultiAttentionBlock'>
- decoder.attention.attention: <class 'torch.nn.modules.container.ModuleList'>
- decoder.attention.attention.0: <class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.AttentionBlock'>
- decoder.attention.attention.0.query: <class 'torch.nn.modules.linear.Linear'>
- decoder.attention.attention.0.key: <class 'torch.nn.modules.linear.Linear'>
- decoder.attention.attention.0.value: <class 'torch.nn.modules.linear.Linear'>
- decoder.attention.attention.1: <class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.AttentionBlock'>
- decoder.attention.attention.1.query: <class 'torch.nn.modules.linear.Linear'>
- decoder.attention.attention.1.key: <class 'torch.nn.modules.linear.Linear'>
- decoder.attention.attention.1.value: <class 'torch.nn.modules.linear.Linear'>
- decoder.attention.linear: <class 'torch.nn.modules.linear.Linear'>
- decoder.feed_forward: <class 'onnx_diagnostic.helpers.torch_helper.dummy_llm.<locals>.FeedForward'>
- decoder.feed_forward.linear_1: <class 'torch.nn.modules.linear.Linear'>
- decoder.feed_forward.relu: <class 'torch.nn.modules.activation.ReLU'>
- decoder.feed_forward.linear_2: <class 'torch.nn.modules.linear.Linear'>
- decoder.norm_1: <class 'torch.nn.modules.normalization.LayerNorm'>
- decoder.norm_2: <class 'torch.nn.modules.normalization.LayerNorm'>

Steal and dump the output of submodules

The following context spies on the intermediate results for the following module and submodules. It stores in one onnx file all the input/output for those.

with steal_forward(
    [
        ("model", model),
        ("model.decoder", model.decoder),
        ("model.decoder.attention", model.decoder.attention),
        ("model.decoder.feed_forward", model.decoder.feed_forward),
        ("model.decoder.norm_1", model.decoder.norm_1),
        ("model.decoder.norm_2", model.decoder.norm_2),
    ],
    dump_file="plot_dump_intermediate_results.inputs.onnx",
    verbose=1,
    storage_limit=2**28,
):
    expected = model(*inputs)
+model -- stolen forward for class LLM -- iteration 0
  <- args=(T7s2x30,) --- kwargs={}
+model.decoder -- stolen forward for class DecoderLayer -- iteration 0
  <- args=(T10s2x30x16,) --- kwargs={}
+model.decoder.norm_1 -- stolen forward for class LayerNorm -- iteration 0
  <- args=(T10s2x30x16,) --- kwargs={}
  -> T10s2x30x16
-model.decoder.norm_1.
-- stores key=('model.decoder.norm_1', 0), size 1Kb -- T10s2x30x16
+model.decoder.attention -- stolen forward for class MultiAttentionBlock -- iteration 0
  <- args=(T10s2x30x16,) --- kwargs={}
  -> T10s2x30x16
-model.decoder.attention.
-- stores key=('model.decoder.attention', 0), size 1Kb -- T10s2x30x16
+model.decoder.norm_2 -- stolen forward for class LayerNorm -- iteration 0
  <- args=(T10s2x30x16,) --- kwargs={}
  -> T10s2x30x16
-model.decoder.norm_2.
-- stores key=('model.decoder.norm_2', 0), size 1Kb -- T10s2x30x16
+model.decoder.feed_forward -- stolen forward for class FeedForward -- iteration 0
  <- args=(T10s2x30x16,) --- kwargs={}
  -> T10s2x30x16
-model.decoder.feed_forward.
-- stores key=('model.decoder.feed_forward', 0), size 1Kb -- T10s2x30x16
  -> T10s2x30x16
-model.decoder.
-- stores key=('model.decoder', 0), size 1Kb -- T10s2x30x16
  -> T10s2x30x16
-model.
-- stores key=('model', 0), size 1Kb -- T10s2x30x16
-- gather stored 12 objects, size=0 Mb
-- dumps stored objects
-- done dump stored objects

Restores saved inputs/outputs

All the intermediate tensors were saved in one unique onnx model, every tensor is stored in a constant node. The model can be run with any runtime to restore the inputs and function create_input_tensors_from_onnx_model can restore their names.

saved_tensors = create_input_tensors_from_onnx_model(
    "plot_dump_intermediate_results.inputs.onnx"
)
for k, v in saved_tensors.items():
    print(f"{k} -- {string_type(v, with_shape=True)}")
('model', 0, 'I') -- ((T7s2x30,),{})
('model.decoder', 0, 'I') -- ((T10s2x30x16,),{})
('model.decoder.norm_1', 0, 'I') -- ((T10s2x30x16,),{})
('model.decoder.norm_1', 0, 'O') -- T10s2x30x16
('model.decoder.attention', 0, 'I') -- ((T10s2x30x16,),{})
('model.decoder.attention', 0, 'O') -- T10s2x30x16
('model.decoder.norm_2', 0, 'I') -- ((T10s2x30x16,),{})
('model.decoder.norm_2', 0, 'O') -- T10s2x30x16
('model.decoder.feed_forward', 0, 'I') -- ((T10s2x30x16,),{})
('model.decoder.feed_forward', 0, 'O') -- T10s2x30x16
('model.decoder', 0, 'O') -- T10s2x30x16
('model', 0, 'O') -- T10s2x30x16

Let’s explained the naming convention.

('model.decoder.norm_2', 0, 'I') -- ((T1s2x30x16,),{})
            |            |   |
            |            |   +--> input, the format is args, kwargs
            |            |
            |            +--> iteration, 0 means the first time the execution
            |                 went through that module
            |                 it is possible to call multiple times,
            |                 the model to store more
            |
            +--> the name given to function steal_forward

The same goes for output except 'I' is replaced by 'O'.

('model.decoder.norm_2', 0, 'O') -- T1s2x30x16

This trick can be used to compare intermediate results coming from pytorch to any other implementation of the same model as long as it is possible to map the stored inputs/outputs.

Conversion to ONNX

The difficult point is to be able to map the saved intermediate results to intermediate results in ONNX. Let’s create the ONNX model.

ep = torch.export.export(model, inputs, dynamic_shapes=ds)
epo = torch.onnx.export(ep)
epo.optimize()
epo.save("plot_dump_intermediate_results.onnx")
[torch.onnx] Run decomposition...
[torch.onnx] Run decomposition... ✅
[torch.onnx] Translate the graph into ONNX...
[torch.onnx] Translate the graph into ONNX... ✅
Applied 4 of general pattern rewrite rules.

Discrepancies

We have a torch model, intermediate results and an ONNX graph equivalent to the torch model. Let’s see how we can check the discrepancies. First the discrepancies of the whole model.

sess = onnxruntime.InferenceSession(
    "plot_dump_intermediate_results.onnx", providers=["CPUExecutionProvider"]
)
feeds = dict(
    zip([i.name for i in sess.get_inputs()], [t.detach().cpu().numpy() for t in inputs])
)
got = sess.run(None, feeds)
diff = max_diff(expected, got)
print(f"discrepancies torch/ORT: {string_diff(diff)}")
discrepancies torch/ORT: abs=0.001953125, rel=0.021564639439228577, n=960.0,amax=0,0,12

What about intermediate results? Let’s use a runtime still based on onnxruntime running an eager evaluation.

sess_eager = OnnxruntimeEvaluator(
    "plot_dump_intermediate_results.onnx",
    providers=["CPUExecutionProvider"],
    torch_or_numpy=True,
)
feeds_tensor = dict(zip([i.name for i in sess.get_inputs()], inputs))
got = sess_eager.run(None, feeds_tensor)
diff = max_diff(expected, got)
print(f"discrepancies torch/eager ORT: {string_diff(diff)}")
discrepancies torch/eager ORT: abs=0.00390625, rel=0.03979624323463865, n=960.0,amax=1,5,5

They are almost the same. That’s good. Let’s now dig into the intermediate results. They are compared to the outputs stored in saved_tensors during the execution of the model.

baseline = {}
for k, v in saved_tensors.items():
    if k[-1] == "I":  # inputs are excluded
        continue
    if isinstance(v, torch.Tensor):
        baseline[f"{k[0]}.{k[1]}".replace("model.decoder", "decoder")] = v

report_cmp = ReportResultComparison(baseline)
sess_eager.run(None, feeds_tensor, report_cmp=report_cmp)
[tensor([[[ 5.8887e-01, -6.9043e-01,  1.2373e+00,  8.9648e-01, -1.1289e+00,
           1.8730e+00,  2.8662e-01,  9.6313e-02, -2.2852e-01,  6.3135e-01,
           1.5039e+00,  4.1260e-01,  3.1719e+00,  8.2178e-01, -1.2559e+00,
           1.3271e+00],
         [ 1.5308e-01, -7.5293e-01, -1.5527e+00,  1.1133e-01, -2.7949e+00,
           1.6367e+00,  8.5742e-01,  4.5361e-01, -1.5469e+00, -2.7168e+00,
          -3.9697e-01,  2.3398e+00, -1.4990e-01,  3.0884e-01,  3.6621e-01,
           1.6768e+00],
         [ 3.6646e-01, -1.1084e+00,  1.0361e+00,  5.3320e-01,  2.2734e+00,
           4.6655e-01, -1.6816e+00, -1.8447e+00, -2.5000e-01, -1.1162e+00,
           1.0352e+00,  1.4180e+00, -1.2412e+00, -1.5488e+00,  1.6719e+00,
           1.9238e+00],
         [-1.4658e+00,  1.8408e+00, -1.2266e+00, -1.3398e+00,  2.3340e-01,
          -3.3862e-01, -1.6533e+00,  1.7090e+00,  8.4229e-01, -3.3154e-01,
           6.8750e-01,  1.2637e+00, -9.6582e-01, -1.8379e+00,  1.2354e-01,
           5.2197e-01],
         [ 5.7031e-01, -1.2695e+00, -4.4287e-01, -7.4512e-01,  1.2334e+00,
           8.6670e-02,  1.1777e+00,  2.5527e+00,  2.8516e+00, -2.3926e+00,
          -8.5693e-02,  1.0928e+00, -1.3525e+00, -2.1758e+00,  9.0576e-01,
          -1.5149e-01],
         [ 5.3418e-01,  2.0886e-01, -1.7920e-01,  1.3564e+00, -2.6836e+00,
          -6.4941e-01, -1.5271e-01,  6.0986e-01,  1.9707e+00,  5.3467e-01,
           1.1699e+00,  2.8242e+00, -2.8496e+00, -1.8115e-01,  1.4229e+00,
           2.0691e-01],
         [ 1.2285e+00, -2.6836e+00, -7.4951e-02, -6.1816e-01, -2.3047e+00,
          -1.5391e+00,  2.2617e+00,  2.9805e+00, -3.0859e-01, -8.3350e-01,
          -1.1289e+00, -5.2393e-01,  3.6328e-01, -2.1309e+00, -7.8027e-01,
          -3.8721e-01],
         [ 4.7461e-01,  2.1484e+00,  8.1250e-01, -1.2115e-02, -4.1406e-01,
           1.6592e+00, -2.3462e-01,  3.6401e-01, -7.5977e-01,  3.4619e-01,
          -6.5918e-01, -4.9609e-01,  1.2461e+00,  5.4248e-01, -2.1558e-01,
          -4.5605e-01],
         [-7.5195e-01, -1.3789e+00,  1.8066e+00,  3.2285e+00, -1.9727e+00,
          -3.5273e+00, -5.3223e-01,  8.4473e-02,  1.5225e+00,  7.7734e-01,
           9.8438e-01,  2.3945e+00,  2.4746e+00, -1.3750e+00,  1.2490e+00,
          -7.4023e-01],
         [-7.9736e-01, -3.9429e-01, -1.0020e+00,  1.4951e+00,  1.6016e+00,
          -9.1797e-01,  9.3066e-01,  5.6201e-01,  3.2656e+00,  8.4473e-01,
          -1.1797e+00, -3.9429e-01,  1.1816e+00, -1.5469e+00,  1.5498e+00,
           1.4404e+00],
         [ 3.7061e-01,  5.8984e-01,  1.3721e+00,  2.0156e+00, -7.7441e-01,
           1.4873e+00,  2.4434e+00, -2.6797e+00, -4.8560e-01, -5.7520e-01,
          -1.2314e+00,  2.0957e+00,  4.7217e-01,  5.0000e-01,  1.2051e+00,
           1.7102e-01],
         [ 1.8750e+00,  1.3418e+00,  1.9404e+00,  1.6855e+00,  1.0029e+00,
           1.2266e+00,  6.9824e-01,  1.2578e+00,  1.5566e+00,  9.9023e-01,
           2.0203e-01,  2.5332e+00, -1.5479e-01, -6.8750e-01, -1.4902e+00,
           1.1816e+00],
         [-1.3232e+00, -4.0674e-01,  1.6533e+00,  2.2109e+00,  2.1738e+00,
           1.3643e+00, -1.2754e+00,  7.1094e-01,  2.1797e+00,  3.6406e+00,
           7.9150e-01,  5.4199e-01,  3.7744e-01,  3.7695e-01, -1.2256e+00,
           3.4414e+00],
         [-1.5234e+00, -1.0146e+00,  9.6191e-01,  6.6357e-01, -4.3042e-01,
          -1.2812e+00,  8.5303e-01,  9.1699e-01,  4.1357e-01, -8.1909e-02,
          -2.0020e+00,  3.9282e-01,  1.5195e+00, -1.9873e+00,  4.3262e-01,
           1.5015e-02],
         [-7.6123e-01,  1.4961e+00,  2.1082e-01, -2.8076e-01,  5.0488e-01,
          -2.3555e+00,  7.0166e-01, -1.5601e-01,  3.7201e-02,  1.1553e+00,
          -4.3311e-01, -1.2559e+00,  1.3965e+00,  2.1602e+00,  3.9795e-01,
           1.7354e+00],
         [-4.3066e-01,  1.5195e+00,  1.1445e+00,  1.0840e+00, -1.3682e+00,
           2.6709e-01, -1.0625e+00, -2.0527e+00, -6.3965e-01, -1.7700e-01,
           6.9531e-01,  1.3350e+00, -6.2158e-01,  5.4297e-01,  9.4177e-02,
          -1.5352e+00],
         [ 9.2871e-01, -5.1660e-01,  1.5137e+00,  2.4207e-01,  1.3857e+00,
           1.3574e+00, -2.7148e+00, -8.9502e-01,  1.1992e+00,  4.0527e-01,
          -4.8584e-01,  4.3848e-01, -3.0625e+00, -1.1992e+00,  2.1230e+00,
          -4.1992e-01],
         [-4.1309e-01, -6.9482e-01, -2.2461e+00, -3.2349e-01, -6.1230e-01,
          -6.6650e-01, -1.0811e+00,  4.9561e-01,  1.7451e+00, -1.0508e+00,
          -1.7529e+00,  6.9629e-01,  3.1982e-01, -2.0547e+00, -1.5161e-01,
          -3.8613e+00],
         [-6.8054e-02,  2.3848e+00, -1.3594e+00,  4.8706e-01, -4.7852e-02,
          -1.8726e-01,  9.1553e-02, -7.8125e-03,  1.7480e+00, -4.9561e-01,
           3.0508e+00, -1.9062e+00, -1.4268e+00, -9.3311e-01,  1.8201e-01,
           1.2383e+00],
         [-1.3955e+00,  9.5215e-02,  1.7114e-01,  1.9072e+00,  1.7227e+00,
          -1.1279e+00,  9.2285e-01,  3.8574e-01,  1.0293e+00, -7.4023e-01,
          -6.2109e-01, -2.0078e+00, -1.2002e+00,  8.5632e-02,  7.4036e-02,
           6.4893e-01],
         [-9.5166e-01, -1.6279e+00,  3.2969e+00,  2.9517e-01, -3.7939e-01,
           6.0791e-02,  1.9104e-01,  1.7246e+00, -1.8701e+00,  2.4121e+00,
           1.5088e+00,  1.4023e+00,  1.0260e-01, -1.8857e+00, -1.0352e+00,
          -2.0625e+00],
         [ 4.0845e-01,  1.0674e+00, -2.6929e-01, -1.0625e+00,  2.0488e+00,
          -9.5801e-01, -7.0752e-01, -9.0430e-01, -2.9844e+00,  2.2266e+00,
           1.0566e+00,  4.1626e-01, -2.6641e+00, -1.6602e+00,  2.3613e+00,
          -2.9980e-01],
         [-3.3545e-01,  4.9512e-01,  6.3379e-01,  2.0752e-02, -2.1021e-01,
          -3.1909e-01, -6.9873e-01,  6.2158e-01,  1.1592e+00, -2.0996e+00,
          -1.0586e+00,  3.5278e-01, -1.8713e-01,  1.2568e+00,  1.2305e+00,
           1.0168e-01],
         [-8.0957e-01, -6.0938e-01,  1.4004e+00,  7.3145e-01,  2.5977e+00,
           1.3145e+00, -2.4980e+00,  7.4707e-01,  3.0000e+00, -3.9038e-01,
          -1.1035e-01, -1.5391e+00,  6.9824e-01, -2.4824e+00, -1.0381e+00,
          -3.1299e-01],
         [-5.6104e-01, -3.6963e-01, -9.1406e-01,  5.0732e-01,  1.6719e+00,
          -2.6738e+00, -2.1802e-01,  2.5537e-01,  2.2344e+00, -1.7207e+00,
           4.5068e-01,  1.8086e+00,  1.9102e+00, -9.3945e-01, -7.4365e-01,
          -6.0352e-01],
         [-1.4248e+00,  6.1963e-01, -6.7480e-01,  2.6074e-01,  1.2207e+00,
           2.7905e-01,  2.2383e+00,  7.2949e-01,  5.3174e-01, -3.2544e-01,
          -5.1270e-01,  1.3242e+00, -5.7373e-03, -1.7969e+00,  2.0352e+00,
           8.9062e-01],
         [-1.0635e+00,  3.1699e+00, -1.1592e+00, -6.8848e-02,  1.4980e+00,
           3.7695e-01,  1.2324e+00,  2.0923e-01,  1.5527e+00, -2.1055e+00,
          -7.7100e-01, -8.0566e-01, -7.6758e-01, -4.7070e-01,  2.5938e+00,
          -8.7109e-01],
         [ 1.2139e+00,  3.2983e-01,  1.1826e+00, -9.8389e-01, -3.4180e-03,
           1.2070e+00,  1.0858e-01, -4.3604e-01,  3.1680e+00,  2.4048e-01,
          -7.5244e-01,  5.3320e-01, -1.8418e+00,  6.5332e-01, -1.8298e-01,
          -2.5854e-01],
         [-1.0938e+00,  2.5146e-01,  4.9805e-01, -9.3359e-01,  1.0166e+00,
           2.4551e+00, -9.6313e-02,  4.8193e-01,  9.0234e-01, -1.2490e+00,
          -2.6416e-01,  4.7632e-01, -1.0859e+00, -3.2207e+00,  4.3945e-02,
          -1.1748e+00],
         [ 2.1953e+00, -7.4268e-01,  2.4170e-02,  9.0967e-01,  2.6543e+00,
           1.1211e+00,  9.5117e-01, -6.4551e-01,  3.4863e+00, -1.3691e+00,
          -8.5645e-01, -6.4355e-01,  9.1260e-01,  3.7988e-01,  1.2109e+00,
           1.1992e+00]],

        [[-7.5195e-01,  1.7461e+00,  1.8408e-01, -1.0791e-01,  8.5352e-01,
          -2.3984e+00,  5.6006e-01,  2.8857e-01, -7.8809e-01,  1.5410e+00,
          -7.7051e-01, -1.3652e+00,  1.3555e+00,  2.2109e+00,  5.5371e-01,
           1.7715e+00],
         [-1.6211e+00,  1.2676e+00, -1.5586e+00,  7.6660e-02, -2.7480e+00,
          -1.6973e+00, -1.9053e+00,  2.3169e-01,  9.5312e-01, -3.5156e-01,
           2.3652e+00,  1.5977e+00, -2.1924e-01, -1.1504e+00,  1.1973e+00,
          -1.0876e-01],
         [-2.9736e-01,  1.0156e+00, -1.3877e+00, -1.4443e+00,  2.3584e-01,
           1.7588e+00, -2.4570e+00, -1.4404e+00, -1.9473e+00, -7.7441e-01,
          -3.3643e-01, -3.8770e-01,  3.2305e+00, -2.1016e+00, -7.7295e-01,
           2.3008e+00],
         [ 1.4727e+00,  1.0391e+00, -1.6719e+00,  1.6484e+00,  5.4297e-01,
           1.9258e+00,  1.5957e+00,  1.6895e+00,  2.1436e-01,  1.0322e+00,
          -1.5654e+00,  1.8623e+00, -1.9053e+00, -3.3667e-01,  7.4609e-01,
          -2.1465e+00],
         [-1.6172e+00, -4.5239e-01,  1.0381e+00,  1.3594e+00,  1.2637e+00,
          -2.3477e+00,  4.0649e-02,  1.0957e+00, -9.3213e-01,  7.9980e-01,
           5.8350e-01, -2.6123e-02,  6.5186e-01, -9.9170e-01,  1.4053e+00,
          -3.4961e-01],
         [-9.7266e-01,  2.4902e+00, -5.7471e-01,  4.7217e-01,  2.7207e+00,
          -2.2031e+00, -8.9453e-01,  1.9570e+00,  2.6367e-01, -1.4258e+00,
          -4.8779e-01, -2.3291e-01,  6.0059e-02, -3.8320e+00,  6.4648e-01,
          -1.5820e+00],
         [-8.2422e-01,  2.0801e+00,  3.0996e+00,  2.1914e+00, -5.1660e-01,
          -3.4180e-01,  1.2822e+00,  2.0195e+00,  7.1533e-02, -3.2959e-02,
          -1.0332e+00, -2.5098e-01,  3.8940e-01, -1.4180e+00,  3.9062e-01,
          -1.1328e+00],
         [-7.7759e-02,  4.7913e-02,  2.1523e+00, -1.1465e+00, -6.4990e-01,
          -8.6523e-01, -1.8445e-01,  4.3408e-01, -2.5234e+00,  1.4287e+00,
           1.4648e-01,  6.9873e-01, -2.1387e-01, -1.3110e-01,  3.6450e-01,
          -2.9248e-01],
         [ 1.1682e-01, -1.8823e-01, -1.1104e+00, -1.2393e+00, -5.5859e-01,
          -1.3965e-01, -4.9634e-01,  3.7744e-01,  8.1689e-01, -8.3350e-01,
           2.0586e+00,  2.0947e-01, -1.6484e+00, -1.3086e+00,  2.1484e+00,
          -1.7158e+00],
         [ 9.3555e-01, -5.5420e-02,  1.6963e+00,  7.7734e-01,  6.2012e-01,
           4.0547e+00, -9.7559e-01,  4.0869e-01,  5.7812e-01, -4.2529e-01,
           8.9990e-01, -7.7539e-01, -2.3047e+00, -2.0469e+00, -1.1855e+00,
           7.6367e-01],
         [-3.7573e-01, -4.6191e-01,  1.5654e+00, -9.3262e-02, -4.6484e-01,
           1.3242e+00,  2.9336e+00, -4.6191e-01,  1.9600e+00,  1.4482e+00,
           1.8652e+00,  5.0684e-01, -1.0400e+00, -1.9033e+00, -1.0693e+00,
           2.7969e+00],
         [ 4.3799e-01, -1.0693e-01,  9.1992e-01,  5.1465e-01,  1.0605e+00,
          -3.6699e+00,  1.6152e+00, -2.3066e+00, -2.2305e+00,  1.5244e+00,
           1.3809e+00, -5.5176e-01,  8.3008e-01,  5.8838e-01,  1.0742e+00,
           1.2881e+00],
         [-2.5000e-01, -7.5098e-01,  6.9385e-01,  5.9521e-01, -4.7949e-01,
           1.6758e+00,  7.7197e-01, -1.1533e+00, -4.7754e-01,  1.2598e-01,
          -2.8857e-01,  2.8564e-02,  2.8125e+00, -1.3171e-01,  1.7900e+00,
          -4.7192e-01],
         [ 3.7090e+00, -1.9209e+00, -2.6660e+00,  8.7158e-01, -3.2559e+00,
          -1.8516e+00,  2.5938e+00,  1.2764e+00,  1.1074e+00, -5.4749e-02,
          -9.8535e-01,  2.5220e-01, -1.5381e-01,  2.2344e+00, -2.0288e-01,
          -1.0950e-01],
         [ 4.8975e-01,  2.5220e-01,  1.6602e+00,  2.0664e+00, -8.2031e-01,
           1.4385e+00,  7.1484e-01,  3.7402e-01,  3.0625e+00, -4.9805e-01,
           2.9736e-01,  9.9121e-01, -2.5708e-01, -1.5791e+00,  6.7578e-01,
          -1.9785e+00],
         [-1.1982e+00,  1.6221e+00,  1.7468e-01,  1.0449e+00, -1.2920e+00,
           7.9785e-01, -1.2773e+00,  5.5664e-02, -2.7266e+00, -1.8418e+00,
          -2.6978e-02,  2.5391e+00, -6.0645e-01,  2.0264e-01, -1.0703e+00,
          -6.4453e-01],
         [-8.1934e-01, -3.9087e-01,  1.7100e+00,  1.3799e+00,  1.1414e-02,
           1.0801e+00,  1.3440e-01,  1.2334e+00, -8.8562e-02,  6.0889e-01,
           2.4062e+00,  1.7224e-01, -1.3789e+00, -7.4524e-02, -4.5776e-02,
          -5.2344e-01],
         [ 2.7393e-01, -1.4697e-01,  6.5137e-01, -4.2285e-01,  2.5098e-01,
           9.8145e-01,  1.9824e+00, -1.1560e-01,  1.9592e-01,  1.0410e+00,
           9.0479e-01, -2.2090e+00, -1.9062e+00,  9.7949e-01,  1.9492e+00,
           3.9722e-01],
         [ 1.6230e+00, -1.1045e+00, -1.3408e+00, -1.9531e+00, -2.4316e+00,
           1.6953e+00,  2.9648e+00,  8.1445e-01, -1.2324e+00,  7.3096e-01,
           5.7520e-01,  1.6035e+00, -7.5488e-01, -6.2012e-01, -5.6445e-01,
          -1.6895e-01],
         [-1.1328e+00, -8.2324e-01,  3.3535e+00,  1.3828e+00,  7.4951e-02,
           1.3174e+00,  3.0518e-03,  1.7881e+00,  1.2520e+00, -1.0635e+00,
           8.5742e-01, -1.3594e+00, -4.4385e-01, -3.4741e-01,  1.8604e+00,
          -2.2461e+00],
         [-1.7627e+00, -2.5801e+00,  1.3203e+00,  2.0234e+00, -8.2471e-01,
           1.4727e+00, -1.2227e+00, -1.9043e+00, -2.5352e+00,  1.4648e-01,
           1.5127e+00,  7.2937e-02, -1.4150e+00, -6.1670e-01, -8.9355e-01,
           1.8945e+00],
         [ 1.7126e-01, -2.6504e+00, -1.0469e+00,  4.5947e-01,  3.3086e+00,
           8.4180e-01, -1.2246e+00,  4.0869e-01,  3.7158e-01, -1.5173e-01,
          -5.2930e-01,  1.1201e+00, -1.6201e+00,  1.1543e+00,  2.2578e+00,
           1.3926e+00],
         [ 2.7656e+00, -3.0298e-01,  5.2490e-02,  1.3174e+00,  2.6406e+00,
           7.3535e-01, -6.6833e-02,  4.3506e-01,  1.2041e+00,  2.9023e+00,
          -1.1066e-01, -1.3501e-01,  1.2070e+00,  3.3813e-01, -1.4502e-01,
           9.6985e-02],
         [ 6.6797e-01, -3.9160e-01, -1.5020e+00, -1.6650e+00,  1.1143e+00,
           4.6692e-02,  2.4297e+00,  1.7273e-01,  1.9458e-01,  1.8848e+00,
           6.3574e-01,  2.7246e+00,  1.3489e-01, -1.8721e+00,  3.0225e-01,
           6.7432e-01],
         [-9.6240e-01, -4.3384e-01, -1.2969e+00, -8.2422e-01, -1.1113e+00,
          -6.4404e-01, -1.1992e+00, -4.4165e-01, -2.2900e-01,  5.4840e-02,
          -1.0879e+00, -7.2314e-01,  1.7930e+00,  1.7969e+00,  1.0332e+00,
           8.2520e-01],
         [ 1.9165e-01,  3.2910e-01, -3.8452e-01,  7.6123e-01,  9.9609e-01,
           1.9226e-01,  1.8164e+00,  2.2305e+00,  3.4277e-01,  1.3691e+00,
           6.8604e-02, -5.2100e-01,  1.1543e+00, -2.1934e+00,  1.4482e+00,
          -1.3501e-01],
         [-1.5430e+00,  1.6602e+00, -4.3701e-01,  1.3730e+00,  1.7578e-01,
           1.3193e+00, -3.8306e-01, -4.6094e-01, -5.8691e-01, -1.1543e+00,
           5.5957e-01,  2.9346e-01,  2.3418e+00,  1.7676e+00,  1.3369e+00,
           7.1436e-01],
         [ 6.9287e-01, -1.1982e+00,  2.0078e+00, -1.2275e+00,  1.9941e+00,
          -1.0068e+00, -1.0273e+00,  1.9043e+00, -9.2090e-01,  2.4817e-01,
           6.6943e-01, -6.9043e-01,  1.3242e+00,  1.8135e+00,  2.9766e+00,
          -1.0889e+00],
         [ 1.2168e+00,  3.5815e-01, -7.6660e-01,  1.2119e+00,  3.1230e+00,
          -7.5293e-01, -7.5488e-01,  2.0093e-01, -3.0234e+00, -1.5308e-01,
           2.8369e-01, -1.1562e+00,  7.3828e-01, -9.1162e-01,  1.5645e+00,
           8.0859e-01],
         [-2.0820e+00,  9.9707e-01, -2.2148e+00,  2.2485e-01, -9.1455e-01,
           8.8184e-01,  2.4473e+00, -1.0312e+00, -6.2842e-01, -1.8975e+00,
          -1.2236e+00, -8.0566e-02,  1.9941e+00, -2.0469e+00,  2.8770e+00,
           2.6514e-01]]], dtype=torch.float16)]

Let’s see the results.

data = report_cmp.data
df = pandas.DataFrame(data)
piv = df.pivot(index=("run_index", "run_name"), columns="ref_name", values="abs")
print(piv)
ref_name                decoder.0  decoder.attention.0  decoder.feed_forward.0  decoder.norm_1.0  decoder.norm_2.0   model.0
run_index run_name
1         embedding      3.160400             3.201172                3.392090          2.299286          2.337891  3.160400
2         embedding_1    3.035156             3.128082                3.023560          2.474609          2.409180  3.035156
3         add_8          0.920166             4.210815                4.098755          1.798828          1.964844  0.920166
4         layer_norm     1.951172             2.922241                3.067871          0.000000          0.725586  1.951172
5         linear         5.211914             1.956543                1.918945          3.928711          3.950195  5.211914
6         linear_1       4.316895             1.980957                2.213379          3.121338          3.253906  4.316895
7         linear_2       4.481689             1.783630                1.993530          3.558594          3.451172  4.481689
17        matmul_1       4.141602             1.783630                1.927551          3.243164          3.235352  4.141602
18        linear_3       4.667969             1.804321                1.844727          4.041016          3.921875  4.667969
19        linear_4       4.716797             1.970215                2.132812          3.389648          3.411133  4.716797
20        linear_5       4.763672             1.934326                2.229004          3.639160          3.656250  4.763672
30        matmul_3       4.093262             1.830811                2.214355          2.810059          2.831543  4.093262
32        val_48         4.116638             0.172852                0.981995          2.833435          2.854919  4.116638
33        linear_6       4.056793             0.000488                0.986389          2.922241          2.795074  4.056793
34        add_115        0.753174             4.046753                4.050293          1.731445          1.800781  0.753174
35        layer_norm_1   1.947266             2.795084                2.999512          0.725586          0.001953  1.947266
39        val_54         3.918091             0.968750                0.075745          3.009766          2.941406  3.918091
40        linear_8       3.881805             0.986206                0.000488          3.067871          2.999512  3.881805
41        add_136        0.003906             4.056803                3.883911          1.951172          1.947266  0.003906

Let’s clean a little bit.

piv[piv >= 1] = np.nan
print(piv.dropna(axis=0, how="all"))
ref_name                decoder.0  decoder.attention.0  decoder.feed_forward.0  decoder.norm_1.0  decoder.norm_2.0   model.0
run_index run_name
3         add_8          0.920166                  NaN                     NaN               NaN               NaN  0.920166
4         layer_norm          NaN                  NaN                     NaN          0.000000          0.725586       NaN
32        val_48              NaN             0.172852                0.981995               NaN               NaN       NaN
33        linear_6            NaN             0.000488                0.986389               NaN               NaN       NaN
34        add_115        0.753174                  NaN                     NaN               NaN               NaN  0.753174
35        layer_norm_1        NaN                  NaN                     NaN          0.725586          0.001953       NaN
39        val_54              NaN             0.968750                0.075745               NaN               NaN       NaN
40        linear_8            NaN             0.986206                0.000488               NaN               NaN       NaN
41        add_136        0.003906                  NaN                     NaN               NaN               NaN  0.003906

We can identity which results is mapped to which expected tensor.

Picture of the model

onx = onnx.load("plot_dump_intermediate_results.onnx")
plot_dot(onx)
plot dump intermediate results
doc.plot_legend("steal and dump\nintermediate\nresults", "steal_forward", "blue")
plot dump intermediate results

Total running time of the script: (0 minutes 7.963 seconds)

Related examples

Export microsoft/phi-2

Export microsoft/phi-2

Intermediate results with onnxruntime

Intermediate results with onnxruntime

Export with dynamic dimensions in {0,1} into ONNX (custom)

Export with dynamic dimensions in {0,1} into ONNX (custom)

Gallery generated by Sphinx-Gallery