From 1b5b210f2cc7f54de11dd17d36ef34549b08b3ef Mon Sep 17 00:00:00 2001 From: Linbin Yu Date: Thu, 9 Sep 2021 16:56:50 -0700 Subject: [PATCH] [Android] print type name for IValues (#64602) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/64602 print type name in error message for easier debugging. Test Plan: Example: java.lang.IllegalStateException: Expected IValue type Tensor, actual type TensorList Reviewed By: beback4u Differential Revision: D30782318 fbshipit-source-id: 60d88a659e7b4bb2b574b12c7652a28f0d5ad0d2 --- .../src/main/java/org/pytorch/IValue.java | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/android/pytorch_android/src/main/java/org/pytorch/IValue.java b/android/pytorch_android/src/main/java/org/pytorch/IValue.java index 50ce61c..28ebf7a 100644 --- a/android/pytorch_android/src/main/java/org/pytorch/IValue.java +++ b/android/pytorch_android/src/main/java/org/pytorch/IValue.java @@ -40,6 +40,24 @@ public class IValue { private static final int TYPE_CODE_DICT_STRING_KEY = 13; private static final int TYPE_CODE_DICT_LONG_KEY = 14; + private String[] TYPE_NAMES = { + "Unknown", + "Null", + "Tensor", + "Bool", + "Long", + "Double", + "String", + "Tuple", + "BoolList", + "LongList", + "DoubleList", + "TensorList", + "GenericList", + "DictStringKey", + "DictLongKey", + }; + @DoNotStrip private final int mTypeCode; @DoNotStrip private Object mData; @@ -312,7 +330,14 @@ public class IValue { if (typeCode != typeCodeExpected) { throw new IllegalStateException( String.format( - Locale.US, "Expected IValue type %d, actual type %d", typeCodeExpected, typeCode)); + Locale.US, + "Expected IValue type %s, actual type %s", + getTypeName(typeCodeExpected), + getTypeName(typeCode))); } } + + private String getTypeName(int typeCode) { + return typeCode >= 0 && typeCode < TYPE_NAMES.length ? TYPE_NAMES[typeCode] : "Unknown"; + } } -- 2.7.4