Forwarding a list of tensors

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?

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.

@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


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

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

    def forward(self, input):
        return None

    def sumTensorsList(self, input):
        # type: (List[Tensor]) -> Tensor
        sum = torch.zeros_like(input[0])
        for x in input:
            sum += x
        return sum
scriptAndSave(Test(), "")
  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

I did, but I made the following mistakes:

final Tensor img1 = TensorImageUtils.bitmapToFloat32Tensor(bitmap1,
                            TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);
final Tensor img2 = TensorImageUtils.bitmapToFloat32Tensor(bitmap2,
                    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);
IValue img = IValue.listFrom(img1, img2);
final Tensor outputTensor = module.forward(img).toTensor();

see my comment in How to use module.foward(Ivalue).toTensor() - #3 by gohna