Skip to content

Commit

Permalink
Update base for Update on "[Better Engineering] Bump ruff to 0.0.278 …
Browse files Browse the repository at this point in the history
…and fix new lint errors"


Based on #16788

Bump ruff to 0.0.278 and fix new lint errors. I added noqa to all existing RUF012 errors which requires mutable class variables to be annotated with `ClassVar`.

Signed-off-by: Justin Chu <justinchumicrosoft.com>

[ghstack-poisoned]
  • Loading branch information
justinchuby committed Jul 21, 2023
2 parents 12f4c5a + d3295f4 commit 6fe32b8
Show file tree
Hide file tree
Showing 25 changed files with 1,460 additions and 460 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface IDisposableReadOnlyCollection<T> : IReadOnlyCollection<T>, IRea
internal class DisposableList<T> : List<T>, IDisposableReadOnlyCollection<T>
where T : IDisposable
{
private bool _disposed;
public DisposableList() { }
public DisposableList(int count) : base(count) { }

Expand All @@ -30,6 +31,11 @@ public DisposableList(IEnumerable<T> collection) : base(collection) { }

protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

if (disposing)
{
// Dispose in the reverse order.
Expand All @@ -43,6 +49,7 @@ protected virtual void Dispose(bool disposing)
this[i]?.Dispose();
}
this.Clear();
_disposed = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static FixedBufferOnnxValue CreateFromTensor<T>(Tensor<T> value)
/// \endcode
/// </example>
public static FixedBufferOnnxValue CreateFromMemory<T>(OrtMemoryInfo memoryInfo, Memory<T> memory,
TensorElementType elementType, long[] shape, long bytesSize)
TensorElementType elementType, long[] shape, long bytesSize) where T : unmanaged
{
if(elementType == TensorElementType.String)
{
Expand Down
56 changes: 32 additions & 24 deletions csharp/src/Microsoft.ML.OnnxRuntime/ManagedProjections.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Microsoft.ML.OnnxRuntime
{
Expand All @@ -24,8 +25,7 @@ internal class ManagedTypeProjection
/// </summary>
/// <param name="namedOnnxValue"></param>
/// <param name="metadata"></param>
/// <param name="disposables"></param>
/// <returns></returns>
/// <returns>OrtValye created accoding to the metadata</returns>
internal static OrtValue CreateProjection(NamedOnnxValue namedOnnxValue, NodeMetadata metadata)
{
OrtValue result;
Expand Down Expand Up @@ -67,8 +67,7 @@ internal static OrtValue CreateProjection(NamedOnnxValue namedOnnxValue, NodeMet
/// </summary>
/// <param name="namedOnnxValue">NamedOnnxValue containing a IEnumerable<NameOnnValue></param>
/// <param name="metadata">sequence metadata</param>
/// <param name="disposables">cleanup list</param>
/// <returns></returns>
/// <returns>OrtValue that represents a sequence</returns>
/// <exception cref="OnnxRuntimeException"></exception>
private static OrtValue CreateSequenceProjection(NamedOnnxValue namedOnnxValue, NodeMetadata metadata)
{
Expand All @@ -84,8 +83,8 @@ private static OrtValue CreateSequenceProjection(NamedOnnxValue namedOnnxValue,
capacity = collection.Count;
}

// Record all the ortValues belonging to the sequence locally
using (var sequenceOrtValues = new DisposableList<OrtValue>(capacity))
DisposableList<OrtValue> sequenceOrtValues = new(capacity);
try
{
foreach (var element in seqContainer)
{
Expand All @@ -97,7 +96,12 @@ private static OrtValue CreateSequenceProjection(NamedOnnxValue namedOnnxValue,

sequenceOrtValues.Add(CreateProjection(element, elementMeta));
}
return OrtValue.CreateSequence(sequenceOrtValues);
return OrtValue.CreateSequence(ref sequenceOrtValues);
}
catch(Exception)
{
sequenceOrtValues?.Dispose();
throw;
}
}

Expand All @@ -107,7 +111,6 @@ private static OrtValue CreateSequenceProjection(NamedOnnxValue namedOnnxValue,
/// </summary>
/// <param name="node"></param>
/// <param name="elementMeta"></param>
/// <param name="disposables"></param>
/// <returns>OrtValue</returns>
/// <exception cref="OnnxRuntimeException"></exception>
private static OrtValue CreateMapProjection(NamedOnnxValue node, NodeMetadata elementMeta)
Expand All @@ -123,57 +126,62 @@ private static OrtValue CreateMapProjection(NamedOnnxValue node, NodeMetadata el
$"Node: {node.Name} onnxruntime only supports maps with primitive types values");
}

TensorBase keys = node.GetDictionaryKeys();
using (OrtValue ortValueKeys = OrtValue.CreateFromTensorObject(keys, out TensorElementType elementTypeKeys))
Span<OrtValue> ortValues = new OrtValue[2];
var disposableGuard = new DisposableArray<OrtValue>(ortValues);
try
{
TensorBase keys = node.GetDictionaryKeys();
ortValues[0] = OrtValue.CreateFromTensorObject(keys, out TensorElementType elementTypeKeys);

if (elementTypeKeys != mapMeta.KeyDataType)
{
throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
$"Map key data type supplied: {elementTypeKeys} metadata expected: {mapMeta.KeyDataType}");
}

TensorBase values = node.GetDictionaryValues();
using (OrtValue ortValueValues = OrtValue.CreateFromTensorObject(values, out TensorElementType elementTypeValues))
ortValues[1] = OrtValue.CreateFromTensorObject(values, out TensorElementType elementTypeValues);
if (elementTypeValues != mapValuesMeta.ElementDataType)
{
if (elementTypeValues != mapValuesMeta.ElementDataType)
{
throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
$"Map value data type supplied: {elementTypeValues} metadata expected: {mapValuesMeta.ElementDataType}");
}

// Create Map OrtValue
return OrtValue.CreateMap(ortValueKeys, ortValueValues);
throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
$"Map value data type supplied: {elementTypeValues} metadata expected: {mapValuesMeta.ElementDataType}");
}

// Create Map OrtValue
return OrtValue.CreateMap(ref ortValues[0], ref ortValues[1]);
}
catch (Exception)
{
disposableGuard.Dispose();
throw;
}
}


/// <summary>
/// This pins memory that is contained within DenseTensor.
/// </summary>
/// <param name="node">NodeOnnxValue containing DenseTensor</param>
/// <param name="elementMeta"></param>
/// <param name="disposables">cleanup list</param>
/// <returns></returns>
/// <exception cref="OnnxRuntimeException"></exception>
private static OrtValue CreateTensorProjection(NamedOnnxValue node, NodeMetadata elementMeta)
{
if (!(node.Value is TensorBase))
if (node.Value is not TensorBase)
{
throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
$"NamedOnnxValue contains: {node.Value.GetType()}, expecting a Tensor<T>");
}

OrtValue ortValue = OrtValue.CreateFromTensorObject(node.Value as TensorBase, out TensorElementType elementType);
try
try
{
if (elementType != elementMeta.ElementDataType)
{
throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
$"Tensor element data type discovered: {elementType} metadata expected: {elementMeta.ElementDataType}");
}
}
catch(Exception)
catch (Exception)
{
ortValue.Dispose();
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

<PropertyGroup>
<Platforms>AnyCPU;x86</Platforms>
<LangVersion>7.3</LangVersion>
<LangVersion>default</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\OnnxRuntime.snk</AssemblyOriginatorKeyFile>
Expand Down
Loading

0 comments on commit 6fe32b8

Please sign in to comment.