Hello,@ masahi As the title says, I took the model from zenodo and used the following code to export torchscript
model = BertForQuestionAnswering(config)
model.to(dev)
model.eval()
model_file = "/model/bert/model.pytorch"
model.load_state_dict(torch.load(model_file), strict=False)
inputs = [
torch.ones((64, 384), dtype=torch.int64),
torch.ones((64, 384), dtype=torch.int64),
torch.ones((64, 384), dtype=torch.int64),
]
scripted_model = torch.jit.trace(model, inputs).eval()
torch.jit.save(scripted_model, "bert.jit")
When I use the jit.load
model and use from_pytorch
to import TVM, the final inference result is inconsistent every time, and it is different from the direct inference result of torch. What is the reason for this, thank you!
jit_model = torch.jit.load(model_path, map_location="cpu")
with torch.no_grad():
start_pos, end_pos = jit_model(tokens_tensor, segments_tensors, mask_tensors)
input_shapes = [
("input_ids", ([64, 384], "int64")),
("attention_mask", ([64, 384], "int64")),
("token_type_ids", ([64, 384], "int64")),
]
mod, params = relay.frontend.from_pytorch(jit_model, input_shapes)
with tvm.transform.PassContext(opt_level=3):
lib = relay.build(mod, target=target, params=params)
dev = tvm.device(str(target), 0)
module = graph_executor.GraphModule(lib["default"](dev))
input_ids = tvm.nd.array(np.array(input_ids_list).astype(dtype))
segment_ids = tvm.nd.array(np.array(segment_ids_list).astype(dtype))
input_mask = tvm.nd.array(np.array(input_mask_list).astype(dtype))
module.set_input(input_ids=input_ids, input_mask=input_mask, segment_ids=segment_ids)
module.run()
tvm_start_pos = module.get_output(0).numpy()
tvm_end_pos = module.get_output(1).numpy()