Set ElementType to String type of node metadata, instead of byte[] (#2348)

* Set ElementType to String type of node metadata, instead of byte[]

* Fix spacing
This commit is contained in:
jignparm 2019-11-08 14:52:56 -08:00 committed by GitHub
parent 7fcd752393
commit fa30b1e758
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 8 deletions

View file

@ -205,7 +205,7 @@ namespace Microsoft.ML.OnnxRuntime
{
if (typeof(T) == typeof(string))
{
var nativeTensorWrapper = new NativeOnnxTensorMemory<byte>(nativeOnnxValue, true);
var nativeTensorWrapper = new NativeOnnxTensorMemory<string>(nativeOnnxValue);
var dt = new DenseTensor<string>(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<string, V>();
var nativeTensorWrapper = new NativeOnnxTensorMemory<byte>(nativeOnnxValueKeys, true);
var nativeTensorWrapper = new NativeOnnxTensorMemory<string>(nativeOnnxValueKeys);
var denseTensorKeys = new DenseTensor<string>(nativeTensorWrapper.GetBytesAsStringMemory(), nativeTensorWrapper.Dimensions);
for (var i = 0; i < denseTensorKeys.Length; i++)
{

View file

@ -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:

View file

@ -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>) + " 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<T>));
if (typeof(T) != typeof(byte))
if (typeof(T) != typeof(string))
throw new NotSupportedException(nameof(NativeOnnxTensorMemory<T>.GetBytesAsStringMemory) + ": T must be byte");
return (_dataBufferAsString == null) ? new Memory<string>() : new Memory<string>(_dataBufferAsString);