Forwarding a list of tensors

simoloca: <p>Hi,<br>
I’m testing pytorch mobile capabilities and when I try to forward a single tensors there’s no problem.<br>
But trying to pass a list of tensors is completely different.<br>
I declared and defined an array of tensors in which I passed the images I want to forward to the module.<br>
Then I convert that to a list of tensors with this: “List i = Arrays.asList(mInputTensor);”.<br>
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();”.<br>
But this expression gives me errors, I can’t understand where is the problem, could anyone help me with this?<br>
Thanks</p>

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

dragen: <p><a class="mention" href="/u/simoloca">@simoloca</a> I face the same problems with you. I want to feed a network with List[Tensor] using c++.</p>

IvanKobzarev: <p>Hello <a class="mention" href="/u/simoloca">@simoloca</a>, <a class="mention" href="/u/dragen">@dragen</a><br>
Sorry for delay with reply.</p>
<p>List[Tensor] is a separate type than list of IValue that is Tensor inside.<br>
Make sure that overloaded  <code>IValue.listFrom</code>  goes to  <code>IValue.listFrom(Tensor... list)</code> , not to  <code>IValue.listFrom(IValue... array)</code> .</p>
<p>I created PR for example where I call scripted method that accepts  <code>List[Tensor]</code> :<br>
</p><aside class="onebox githubpullrequest">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/pull/40408/files" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Pull Request">
    <svg width="60" height="60" class="github-icon" viewBox="0 0 12 16" aria-hidden="true"><path d="M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/pull/40408" target="_blank" rel="nofollow noopener">[NOT_FOR_LAND] Test android api with torchscript methos arg List[Tensor]</a>
    </h4>

    <div class="branches">
      <code>pytorch:gh/IvanKobzarev/47/base</code> ← <code>pytorch:gh/IvanKobzarev/47/head</code>
    </div>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-06-23" data-time="00:08:21" data-timezone="UTC">12:08AM - 23 Jun 20 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/IvanKobzarev" target="_blank" rel="nofollow noopener">
          <img alt="IvanKobzarev" src="https://avatars3.githubusercontent.com/u/6638825?v=4" class="onebox-avatar-inline" width="20" height="20">
          IvanKobzarev
        </a>
      </div>

      <div class="lines" title="2 commits changed 6 files with 60 additions and 9 deletions">
        <a href="https://github.com/pytorch/pytorch/pull/40408/files" target="_blank" rel="nofollow noopener">
          <span class="added">+60</span>
          <span class="removed">-9</span>
        </a>
      </div>
    </div>

  </div>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<pre><code class="lang-auto">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]) -&gt; Tensor
        sum = torch.zeros_like(input[0])
        for x in input:
            sum += x
        return sum
scriptAndSave(Test(), "sum_tensors_list.pt")
</code></pre>
<pre><code class="lang-auto">  private Tensor makeTensor(final long[] shape, float value) {
    long numElements = 1;
    for (int i = 0; i &lt; shape.length; i++) {
      numElements *= shape[i];
    }
    FloatBuffer buffer = Tensor.allocateFloatBuffer((int) numElements);
    for(int i = 0; i &lt; 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();
</code></pre>

gohna: <p>I did, but I made the following mistakes：<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/9/697553387c911773f091cf69ec43885937f85314.png" data-download-href="https://discuss.pytorch.org/uploads/default/697553387c911773f091cf69ec43885937f85314" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/697553387c911773f091cf69ec43885937f85314_2_690x210.png" alt="image" data-base62-sha1="f2Vv8xQvp0nC4ScpuDjh8ki6ELy" width="690" height="210" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/9/697553387c911773f091cf69ec43885937f85314_2_690x210.png, https://discuss.pytorch.org/uploads/default/original/3X/6/9/697553387c911773f091cf69ec43885937f85314.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/9/697553387c911773f091cf69ec43885937f85314.png 2x" data-dominant-color="EEEFF7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">869×265 39.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto">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();
</code></pre>

Linbin: <p>see my comment in <a href="https://discuss.pytorch.org/t/how-to-use-module-foward-ivalue-totensor/135048/3" class="inline-onebox">How to use module.foward(Ivalue).toTensor() - #3 by gohna</a></p>