Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/microsoft/onnxruntime into …
Browse files Browse the repository at this point in the history
…wgpu
  • Loading branch information
xadupre committed Jul 5, 2023
2 parents 3db9066 + e252671 commit 53db62e
Show file tree
Hide file tree
Showing 43 changed files with 726 additions and 386 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cffconvert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out a copy of the repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Check whether the citation metadata from CITATION.cff is valid
uses: citation-file-format/cffconvert-github-action@2.0.0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-c-apidocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
name: Generate C/C++ API docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install doxygen and dependencies
run: |
sudo apt update
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-csharp-apidocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-java-apidocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
name: Generate Java docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-python-apidocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
name: Generate Python API docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install tools
run: |
sudo apt-get update
Expand Down
42 changes: 0 additions & 42 deletions csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/InferenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2098,46 +2098,4 @@ public SkipNonPackageTests()
}
}

// Copy of the class that is internal in the main package
internal class DisposableListTest<T> : List<T>, IDisposableReadOnlyCollection<T>
where T : IDisposable
{
public DisposableListTest() { }
public DisposableListTest(int count) : base(count) { }

#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// Dispose in the reverse order.
// Objects should typically be destroyed/disposed
// in the reverse order of its creation
// especially if the objects created later refer to the
// objects created earlier. For homogeneous collections of objects
// it would not matter.
for (int i = this.Count - 1; i >= 0; --i)
{
this[i]?.Dispose();
}
this.Clear();
}

disposedValue = true;
}
}

// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,49 @@

namespace Microsoft.ML.OnnxRuntime.Tests
{
// Copy of the class that is internal in the main package
public class DisposableListTest<T> : List<T>, IDisposableReadOnlyCollection<T>
where T : IDisposable
{
public DisposableListTest() { }
public DisposableListTest(int count) : base(count) { }

#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// Dispose in the reverse order.
// Objects should typically be destroyed/disposed
// in the reverse order of its creation
// especially if the objects created later refer to the
// objects created earlier. For homogeneous collections of objects
// it would not matter.
for (int i = this.Count - 1; i >= 0; --i)
{
this[i]?.Dispose();
}
this.Clear();
}

disposedValue = true;
}
}

// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}

internal struct DisposableTestPair<TValue> : IDisposable
where TValue : IDisposable
{
Expand Down Expand Up @@ -184,7 +227,7 @@ internal static NamedOnnxValue LoadOnnxValueFromFilePb(string fullFilename, stri
}
case OnnxValueType.ONNX_TYPE_MAP:
{
throw new OnnxRuntimeException(ErrorCode.NotImplemented,
throw new NotImplementedException(
"Map test data format requires clarification: https://github.com/onnx/onnx/issues/5072");
}

Expand Down Expand Up @@ -220,7 +263,7 @@ internal static DisposableTestPair<OrtValue> LoadOrtValueFromFilePb(string fullF
}
case OnnxValueType.ONNX_TYPE_MAP:
{
throw new OnnxRuntimeException(ErrorCode.NotImplemented,
throw new NotImplementedException(
"Map test data format requires clarification: https://github.com/onnx/onnx/issues/5072");
}

Expand Down Expand Up @@ -411,7 +454,7 @@ internal static OrtValue CreateOrtValueFromSequence(Onnx.SequenceProto sequence,
case Onnx.SequenceProto.Types.DataType.Tensor:
{
SequenceCheckMatchOnnxType(nodeName, sequenceMeta, OnnxValueType.ONNX_TYPE_TENSOR);
using (var sequenceOfTensors = new DisposableList<OrtValue>(sequence.TensorValues.Count))
using (var sequenceOfTensors = new DisposableListTest<OrtValue>(sequence.TensorValues.Count))
{
foreach (var tensor in sequence.TensorValues)
{
Expand All @@ -424,7 +467,7 @@ internal static OrtValue CreateOrtValueFromSequence(Onnx.SequenceProto sequence,
case Onnx.SequenceProto.Types.DataType.Sequence: // Sequence of sequences
{
SequenceCheckMatchOnnxType(nodeName, sequenceMeta, OnnxValueType.ONNX_TYPE_SEQUENCE);
using (var seqOfSequences = new DisposableList<OrtValue>(sequence.TensorValues.Count))
using (var seqOfSequences = new DisposableListTest<OrtValue>(sequence.TensorValues.Count))
{
foreach (var s in sequence.SequenceValues)
{
Expand All @@ -437,13 +480,13 @@ internal static OrtValue CreateOrtValueFromSequence(Onnx.SequenceProto sequence,
}
case Onnx.SequenceProto.Types.DataType.Map:
{
throw new OnnxRuntimeException(ErrorCode.NotImplemented,
throw new NotImplementedException(
"Test data format for maps is under investigation");
}
case Onnx.SequenceProto.Types.DataType.Optional:
{
SequenceCheckMatchOnnxType(nodeName, sequenceMeta, OnnxValueType.ONNX_TYPE_OPTIONAL);
using (var seqOfSequences = new DisposableList<OrtValue>(sequence.TensorValues.Count))
using (var seqOfSequences = new DisposableListTest<OrtValue>(sequence.TensorValues.Count))
{
foreach (var opt in sequence.OptionalValues)
{
Expand Down Expand Up @@ -478,7 +521,7 @@ internal static OrtValue CreateOrtValueFromOptional(Onnx.OptionalProto optional,
}
case Onnx.OptionalProto.Types.DataType.Map:
{
throw new OnnxRuntimeException(ErrorCode.NotImplemented,
throw new NotImplementedException(
"Test data format for maps is under investigation");

}
Expand All @@ -499,7 +542,8 @@ internal static OrtValue CreateOrtValueFromRawData(OrtAllocator allocator, ReadO
var typeInfo = TensorBase.GetElementTypeInfo(elementType);
Assert.NotNull(typeInfo);

var shapeSize = ArrayUtilities.GetSizeForShape(shape);
// ArrayUtilities not accessible in all builds
var shapeSize = shape.Aggregate(1L, (a, v) => a * v);
var inferredSize = rawData.Length / typeInfo.TypeSize;
Assert.Equal(shapeSize, inferredSize);
Assert.Equal(0, rawData.Length % typeInfo.TypeSize);
Expand Down
16 changes: 16 additions & 0 deletions java/src/main/java/ai/onnxruntime/OrtSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ public int getID() {
}
}

static {
try {
OnnxRuntime.init();
} catch (IOException e) {
throw new RuntimeException("Failed to load onnx-runtime library", e);
}
}

private final long nativeHandle;

private final List<Long> customLibraryHandles;
Expand Down Expand Up @@ -1164,6 +1172,14 @@ private native void addExecutionProvider(
/** Used to control logging and termination of a call to {@link OrtSession#run}. */
public static class RunOptions implements AutoCloseable {

static {
try {
OnnxRuntime.init();
} catch (IOException e) {
throw new RuntimeException("Failed to load onnx-runtime library", e);
}
}

private final long nativeHandle;

private boolean closed = false;
Expand Down
8 changes: 6 additions & 2 deletions js/web/lib/wasm/jsep/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ class TensorViewImpl implements TensorView {
if (this.dataType !== DataType.float) {
throw new Error('Invalid data type');
}
return new Float32Array(this.module.HEAP8.buffer, this.data, ShapeUtil.size(this.dims));
const elementCount = ShapeUtil.size(this.dims);
return elementCount === 0 ? new Float32Array() :
new Float32Array(this.module.HEAP8.buffer, this.data, elementCount);
}

getBigInt64Array(): BigInt64Array {
if (this.dataType !== DataType.int64) {
throw new Error('Invalid data type');
}
return new BigInt64Array(this.module.HEAP8.buffer, this.data, ShapeUtil.size(this.dims));
const elementCount = ShapeUtil.size(this.dims);
return elementCount === 0 ? new BigInt64Array() :
new BigInt64Array(this.module.HEAP8.buffer, this.data, elementCount);
}

reshape(newDims: readonly number[]): TensorView {
Expand Down
6 changes: 3 additions & 3 deletions js/web/lib/wasm/jsep/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ export class ShapeUtil {
let size = 1;
for (let i = start; i < end; i++) {
// safety check as this method is called by multiple other methods requiring size.
// size cannot be 0 or negative.
if (dims[i] <= 0) {
// size cannot be negative.
if (dims[i] < 0) {
throw new Error(
// eslint-disable-next-line max-len
'cannot get valid size from specified dimension range. Most likely the range contains 0 or negative values in them.');
'cannot get valid size from specified dimension range. Most likely the range contains negative values in them.');
}
size *= dims[i];
}
Expand Down
46 changes: 30 additions & 16 deletions js/web/lib/wasm/jsep/webgpu/ops/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ const validateInputs = (inputs: readonly TensorView[]): void => {
throw new Error('Reduce op requires 1 or 2 inputs.');
}

if (inputs.length === 2 && inputs[1].dims.length !== 1) {
throw new Error('Invalid axes input dims.');
}

if (inputs[0].dataType !== DataType.float) {
throw new Error('Invalid input type.');
}
};

export interface ReduceAttributes extends AttributeWithCacheKey {
axes: number[];
keepDims: boolean;
noopWithEmptyAxes: boolean;
axes: number[];
}

type ReduceOp = (inputs: readonly TensorView[], axes: number[]) => string[];

const noOp: ReduceOp = (): string[] => ['', '', 'value = _A[inputIdx];', ''];
const createReduceProgramInfo =
(metadata: ProgramMetadata, inputs: readonly TensorView[], attributes: ReduceAttributes,
reduceOp: ReduceOp): ProgramInfo => {
Expand All @@ -36,17 +40,18 @@ const createReduceProgramInfo =
const idxCopy: string[] = []; // copy output indexes to input indexes

const axes = ShapeUtil.normalizeAxes(attributes.axes, inputs[0].dims.length);
const outputDimsLength = inputs[0].dims.length - (attributes.keepDims ? 0 : axes.length);
const ops = reduceOp(inputs, axes);
const inputIndicesHelper = createIndicesHelper('input', inputShape);
const initInputIdx = (ops[1] === '') ? '' : `let inputIdx = ${inputIndicesHelper.i2oExpression('inputIndices')};`;
let reduceOps = `
let inputIdx = ${inputIndicesHelper.i2oExpression('inputIndices')};
${ops[2]};`;

const reduceOnAllAxes = !attributes.noopWithEmptyAxes && attributes.axes.length === 0;
for (let k = 0; k < inputs[0].dims.length; k++) {
// if this axis is reduced
if (axes.indexOf(k) >= 0 || axes.length === 0) {
if (attributes.keepDims === true) {
if (reduceOnAllAxes || axes.indexOf(k) >= 0) {
if (attributes.keepDims) {
outputShape.push(1);
} // else { remove the axis from outputShape; }

Expand All @@ -56,7 +61,11 @@ const createReduceProgramInfo =
${reduceOps}
}`;
} else {
idxCopy.push(`inputIndices[${k}] = outputIndices[${outputShape.length}];`);
if (outputDimsLength > 1) {
idxCopy.push(`inputIndices[${k}] = outputIndices[${outputShape.length}];`);
} else {
idxCopy.push(`inputIndices[${k}] = outputIndices;`);
}
outputShape.push(inputs[0].dims[k]);
}
}
Expand Down Expand Up @@ -97,23 +106,28 @@ const createReduceProgramInfo =
};
};

const createReduceAttributesFromInput = (input: TensorView, attributes: ReduceAttributes): ReduceAttributes => {
const axes: number[] = [];
input.getBigInt64Array().forEach(v => axes.push(Number(v)));
const keepDims = attributes.keepDims;
const noopWithEmptyAxes = attributes.noopWithEmptyAxes;
return createAttributeWithCacheKey({axes, keepDims, noopWithEmptyAxes});
};
const createReduceAttributesFromInputs =
(inputs: readonly TensorView[], attributes: ReduceAttributes): ReduceAttributes => {
const axes: number[] = [];
if (inputs[1].dims[0] > 0) {
inputs[1].getBigInt64Array().forEach(v => axes.push(Number(v)));
}
return createAttributeWithCacheKey(
{axes, keepDims: attributes.keepDims, noopWithEmptyAxes: attributes.noopWithEmptyAxes});
};

const createReduceProgramInfoLoader =
(inputs: readonly TensorView[], name: string, attributes: ReduceAttributes, reduceOp: ReduceOp):
ProgramInfoLoader => {
const metadata: ProgramMetadata = {name, inputTypes: [GpuDataType.default]};
const updatedAttributes: ReduceAttributes =
inputs.length === 1 ? attributes : createReduceAttributesFromInputs(inputs, attributes);
const metadata:
ProgramMetadata = {name, inputTypes: [GpuDataType.default], cacheHint: updatedAttributes.cacheKey};
return {
...metadata,
get: () => createReduceProgramInfo(
metadata, [inputs[0]],
(inputs.length === 1) ? attributes : createReduceAttributesFromInput(inputs[1], attributes), reduceOp)
metadata, [inputs[0]], updatedAttributes,
updatedAttributes.noopWithEmptyAxes && updatedAttributes.axes.length === 0 ? noOp : reduceOp)
};
};

Expand Down
Loading

0 comments on commit 53db62e

Please sign in to comment.