How to get Tensor size from torch::jit::module?

Hi there,
How could I go through the module and print its tensor size?

For example, I want to do the following thing:

void print_op_input(torch::jit::module & mod){
  auto fwd_mtd = mod.get_method("forward");
  for (torch::jit::Node * nd : fwd_method.graph()->nodes()){
    if (nd->kind().is_aten()){
      auto inputs = nd->inputs();
      // Question : How to access the input?
      // The inputs here are type intArrayRef<Value *>

The question is , how could I access the input? It is Value*, not IValue. How could I access its true stored value?

I think what you need is toIValue(), or even IValue(toIValue()) that removes c10::optional<> from c10::optional<IValue>.
No idea why we even need this whole c10::optional<> construct when IValue can simply return .isNone() == true — which it actually does after the proposed conversion if the value was empty.

The solution proposed by @hidefromkgb should work, but keep in mind that toIValue(const Value* v) will return an IValue only for Value*s that point to constants.

1 Like