diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs b/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs index bb159ea29e..397eacbbf8 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs @@ -205,7 +205,7 @@ namespace Microsoft.ML.OnnxRuntime { if (typeof(T) == typeof(string)) { - var nativeTensorWrapper = new NativeOnnxTensorMemory(nativeOnnxValue, true); + var nativeTensorWrapper = new NativeOnnxTensorMemory(nativeOnnxValue); var dt = new DenseTensor(nativeTensorWrapper.GetBytesAsStringMemory(), nativeTensorWrapper.Dimensions); return new DisposableNamedOnnxValue(name, dt, nativeTensorWrapper); } @@ -225,7 +225,7 @@ namespace Microsoft.ML.OnnxRuntime if (typeof(K) == typeof(string)) { var map = new Dictionary(); - var nativeTensorWrapper = new NativeOnnxTensorMemory(nativeOnnxValueKeys, true); + var nativeTensorWrapper = new NativeOnnxTensorMemory(nativeOnnxValueKeys); var denseTensorKeys = new DenseTensor(nativeTensorWrapper.GetBytesAsStringMemory(), nativeTensorWrapper.Dimensions); for (var i = 0; i < denseTensorKeys.Length; i++) { diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NamedOnnxValue.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NamedOnnxValue.cs index 5a90d870e4..8413a01862 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NamedOnnxValue.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NamedOnnxValue.cs @@ -471,7 +471,7 @@ namespace Microsoft.ML.OnnxRuntime width = sizeof(sbyte); break; case TensorElementType.String: - type = typeof(byte); + type = typeof(string); width = sizeof(byte); break; case TensorElementType.Bool: diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs index 9a28596475..34f4894032 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs @@ -23,7 +23,7 @@ namespace Microsoft.ML.OnnxRuntime private int _elementWidth; private int[] _dimensions; - public NativeOnnxTensorMemory(IntPtr onnxValueHandle, bool isStringTensor = false) + public NativeOnnxTensorMemory(IntPtr onnxValueHandle) { IntPtr typeAndShape = IntPtr.Zero; try @@ -69,14 +69,12 @@ namespace Microsoft.ML.OnnxRuntime _dimensions[i] = (int)shape[i]; } - if (!isStringTensor) + if (typeof(T) != typeof(string)) { NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorMutableData(_onnxValueHandle, out _dataBufferPointer)); } else { - if (typeof(T) != typeof(byte)) - throw new NotSupportedException(nameof(NativeOnnxTensorMemory) + " T = " + nameof(T) + ". Should = byte, when isStringTensor is true"); UIntPtr strLen; var offsets = new UIntPtr[_elementCount]; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetStringTensorDataLength(_onnxValueHandle, out strLen)); @@ -191,7 +189,7 @@ namespace Microsoft.ML.OnnxRuntime if (IsDisposed) throw new ObjectDisposedException(nameof(NativeOnnxTensorMemory)); - if (typeof(T) != typeof(byte)) + if (typeof(T) != typeof(string)) throw new NotSupportedException(nameof(NativeOnnxTensorMemory.GetBytesAsStringMemory) + ": T must be byte"); return (_dataBufferAsString == null) ? new Memory() : new Memory(_dataBufferAsString);