Casting on Session.Runner fetch with new Tensor Type.

11 views
Skip to first unread message

Jim Clarke

unread,
Jan 8, 2021, 2:31:52 PM1/8/21
to SIG JVM
@karl

This is the example of having to cast the result of a fetch to a specific TType class. We used to use .expect() to do this.
I know that Runner.run() returns List<Tensor>, also if you run multiple fetches on the same run there is no way you would know which type before hand.

The question is do we want to leave it as is now where you have to cast it yourself to the specific TType?

Or would we want to add  .expect() on the Tensor itself so something like:


TInt64 t = session.runner().fetch(argMax).run().get(0).expect(TInt64.class);

========

Single Fetch:

Operand<TInt64> argMax = tf.math.argMax(predictions, tf.constant(-1));
if(session != null) {
  try(TInt64 t = (TInt64)session.runner().fetch(argMax).run().get(0)) {
    t.scalars().forEachIndexed((idx, d) -> System.out.printf("%s) %d\n", Arrays.toString(idx), d.getLong()));
  }
}

Multiple fetches:

Operand<TInt64> arg1;
Operand<TFloat64> arg2;
try(List<Tensor> tensors = session.runner().fetch(arg1).fetch(arg2).run()) {
    TInt64 arg1Tensor = (TInt64)tensors.get(0);
    TFloat64 arg2Tensor = (TFloat64)tensors.get(1);
  }

=========

Karl Lessard

unread,
Jan 10, 2021, 12:12:26 AM1/10/21
to Jim Clarke, SIG JVM
Thanks Jim,

I think like we've discussed, I think we should be safe to implicitly cast the tensor based on the type of the variable receiving the value from the list (which would become a custom list, something already planned by this PR), e.g.

public final class TensorList {
    private List<Tensor> tensors;
    ...
    public <T extends TType> T get(int index) {
        return (T) tensors.get(index);
    }
    ...
}

try (TensorList tensors = session.runner().fetch(arg1).fetch(arg2).run()) {
    TInt64 arg1Tensor = tensors.get(0);
    TFloat64 arg2Tensor = tensors.get(1);
}

Dean Thompson has already pointed out that in some cases that won't work, like this one here, but I think this should be very rare to do such thing and it won't block neither a user doing it (he could still cast it explicitly later and that would work).

- Karl

--
You received this message because you are subscribed to the Google Groups "SIG JVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jvm+uns...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/jvm/F7190187-4722-4021-B792-B35DF3DC6364%40gmail.com.
Reply all
Reply to author
Forward
0 new messages