How to decode the constant value from the result of "tvm.ir.save_json"

How to decode the constant value from the result of “tvm.ir.save_json”

Hi, I would like to ask about encoding-decoding between relay and json in TVM.

I converted a PyTorch model into relay and saved the parsed result as a json file. After that, I found out that constant value is encoded as something like "64base-representation’ like

[‘P6G0lvBAXt0AAAAAAAAAAAEAAAAAAAAABAAAAAIgAQABAAAAAAAAAAEAAAAAAAAAAgAAAAAAAAACAAAAAAAAABAAAAAAAAAAAACAPwAAAEAAAEBAAACAQA==’]

Can I decode it to restore the original value?

Here is an example code I used:

import torch
import torch.nn as nn
import json
import tvm
from tvm import relay
from tvm.relay.frontend import from_pytorch
import numpy as np

class ConvTest(nn.Module):
    def __init__(self, inputShape, dtype):
        super().__init__()
        self.inputShape = inputShape
        self.dtype = dtype
        b, c, h, w = self.inputShape
        self.conv = nn.Conv2d(c, 3, 1, 1)
        self.cons = torch.tensor([[[[1, 2], [3, 4]]]], dtype=self.dtype)

    def forward(self, x):
        return self.conv(x) + self.cons

inputShape = (1, 1, 2, 2)
dtype = torch.float32
model = ConvTest(inputShape, dtype)
inputData = torch.randn(inputShape)
scriptedModel = torch.jit.trace(model, inputData).eval()
shape_list = [('input', inputShape)]

mod, params = relay.frontend.from_pytorch(scriptedModel, shape_list) # torch script to relay IR
mod_json = json.loads(tvm.ir.save_json(mod))

import base64

print(mod_json['b64ndarrays'])
s = mod_json['b64ndarrays']
r = base64.decodebytes(s[0].encode('utf-8'))
q = np.frombuffer(r, dtype=np.float32)
print(q)

the result is given as

['P6G0lvBAXt0AAAAAAAAAAAEAAAAAAAAABAAAAAIgAQABAAAAAAAAAAEAAAAAAAAAAgAAAAAAAAACAAAAAAAAABAAAAAAAAAAAACAPwAAAEAAAEBAAACAQA==']
[-2.9182329e-25 -1.0009415e+18  0.0000000e+00  0.0000000e+00
  1.4012985e-45  0.0000000e+00  5.6051939e-45  1.0331774e-40
  1.4012985e-45  0.0000000e+00  1.4012985e-45  0.0000000e+00
  2.8025969e-45  0.0000000e+00  2.8025969e-45  0.0000000e+00
  2.2420775e-44  0.0000000e+00  1.0000000e+00  2.0000000e+00
  3.0000000e+00  4.0000000e+00]

1 Like