How to convert jit::Value into Tensor?

Currently, I load the torch script model as jit module.
I want to print all those parameters. So I want to convert jit::Value data strcture to Tensor.

if(value.type()->expect<torch::jit::TensorType>()) {
      auto tensor = value.toTensor();

I try to use same function of IValue but it does not work. Any idea?

1 Like

IValue doesn’t have a .type() method since it’s a generic container for values (similar to Python’s PyObject). You should be able to just call .toTensor() to convert it to a tensor (make sure to guard it with a call to .isTensor() to make sure the IValue is what you expect).

You can find more details here. We are actively working on improving the JIT’s C++ API so issues like this will be more clear in the future.

@driazati I think it does have a type method ? Here

Thanks a lot, @driazati.
I am using torch::jit::Value, not IValue .
I know how to convert IValue to tensor by toTensor. How about for for torch::jit::Value or torch::jit::Node?

Sorry for the confusion, you can think of torch::jit::Value as a variable in the graph (i.e. it has no concrete value until the graph is run), so there’s really no way to extract its value since it doesn’t have one. You can read more about it here. If you are trying to extract intermediate values from the graph, there’s no way to do that currently but you can follow along at this issue.

1 Like

I got it. So actually jit::Value is just symbol variable.
Thanks a lot, @driazati