Forwarding a list of tensors

Hi,
I’m testing pytorch mobile capabilities and when I try to forward a single tensors there’s no problem.
But trying to pass a list of tensors is completely different.
I declared and defined an array of tensors in which I passed the images I want to forward to the module.
Then I convert that to a list of tensors with this: “List i = Arrays.asList(mInputTensor);”.
Because I would like to pass the entire list to the forward method, then I do: “final Tensor[] o = module.forward(IValue.listFrom(i)).toTensorList();”.
But this expression gives me errors, I can’t understand where is the problem, could anyone help me with this?
Thanks

Could anyone help me?
I just want to know if I can put as input an array of tensors to the forward method, and if yes how.
Thanks

@simoloca I face the same problems with you. I want to feed a network with List[Tensor] using c++.

Hello @simoloca, @dragen
Sorry for delay with reply.

List[Tensor] is a separate type than list of IValue that is Tensor inside.
Make sure that overloaded IValue.listFrom goes to IValue.listFrom(Tensor... list) , not to IValue.listFrom(IValue... array) .

I created PR for example where I call scripted method that accepts List[Tensor] :

import torch

print(torch.version.__version__)

def scriptAndSave(module, fileName):
    print('-' * 80)
    script_module = torch.jit.script(module)
    print(script_module.graph)
    outputFileName = fileName
    script_module.save(outputFileName)
    print("Saved to " + outputFileName)
    print('=' * 80)

class Test(torch.jit.ScriptModule):
    def __init__(self):
        super(Test, self).__init__()

    @torch.jit.script_method
    def forward(self, input):
        return None

    @torch.jit.script_method
    def sumTensorsList(self, input):
        # type: (List[Tensor]) -> Tensor
        sum = torch.zeros_like(input[0])
        for x in input:
            sum += x
        return sum
scriptAndSave(Test(), "sum_tensors_list.pt")
  private Tensor makeTensor(final long[] shape, float value) {
    long numElements = 1;
    for (int i = 0; i < shape.length; i++) {
      numElements *= shape[i];
    }
    FloatBuffer buffer = Tensor.allocateFloatBuffer((int) numElements);
    for(int i = 0; i < numElements; i++) {
      buffer.put(i, value);
    }
    return Tensor.fromBlob(buffer, shape);
  }

  final long[] shape = new long[] {2, 2};

    IValue ivalue = IValue.listFrom(
      makeTensor(shape, 1),
      makeTensor(shape, 2),
      makeTensor(shape, 3));

    final Tensor outputTensor = mModule.runMethod("sumTensorsList", ivalue).toTensor();
1 Like