From 4722a3a3728d004eb2b41371c76c549280b1577f Mon Sep 17 00:00:00 2001 From: MatthewDaggitt Date: Fri, 24 Jan 2025 16:12:32 +0800 Subject: [PATCH 1/2] Fixed bug in node in command-line ONNX parser --- CHANGELOG.md | 1 + src/input_parsers/TensorUtils.cpp | 2 +- src/input_parsers/TensorUtils.h | 16 +++++++++------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb8bd09ef..8ecc8e54f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Next Release - Adding incremental infrastructure which allows pushing and popping constraints to/from the InputQuery. - Dropped support for parsing Tensorflow network format. Newest Marabou version that supports Tensorflow is at commit 190555573e4702. + - Fixed bug in the parsing of `transpose` nodes in command line C++ parser. ## Version 2.0.0 diff --git a/src/input_parsers/TensorUtils.cpp b/src/input_parsers/TensorUtils.cpp index eab6aae7f..865a1419c 100644 --- a/src/input_parsers/TensorUtils.cpp +++ b/src/input_parsers/TensorUtils.cpp @@ -165,7 +165,7 @@ calculatePaddingNeeded( int inputSize, int filterSize, int stride, bool padFront Permutation reversePermutation( unsigned int size ) { Permutation result; - for ( unsigned int i = size - 1; i-- > 0; ) + for ( unsigned int i = size; i-- > 0; ) { result.append( i ); } diff --git a/src/input_parsers/TensorUtils.h b/src/input_parsers/TensorUtils.h index 295f8aac6..e9ca275e1 100644 --- a/src/input_parsers/TensorUtils.h +++ b/src/input_parsers/TensorUtils.h @@ -55,6 +55,8 @@ TensorIndices unpackIndex( TensorShape shape, PackedTensorIndices packedIndex ); PackedTensorIndices packIndex( TensorShape shape, TensorIndices indices ); +unsigned int tensorSize( TensorShape shape ); + template T tensorLookup( Vector tensor, TensorShape shape, TensorIndices indices ) { return tensor[packIndex( shape, indices )]; @@ -77,20 +79,20 @@ Vector transposeTensor( Vector tensor, TensorShape shape, Permutation perm // NOTE this implementation is *very* inefficient. Eventually we might want to // switch to a similar implementation as NumPy arrays with internal strides etc. ASSERT( shape.size() == permutation.size() ); + ASSERT( tensorSize(shape) == tensor.size() ); + TensorShape transposedShape = transposeVector( shape, permutation ); Vector result( tensor.size() ); - for ( PackedTensorIndices rawOutputIndex = 0; rawOutputIndex < tensor.size(); rawOutputIndex++ ) + for ( PackedTensorIndices rawInputIndex = 0; rawInputIndex < tensor.size(); rawInputIndex++ ) { - TensorIndices outputIndex = unpackIndex( transposedShape, rawOutputIndex ); - TensorIndices inputIndex = transposeVector( outputIndex, permutation ); - T value = tensorLookup( tensor, shape, inputIndex ); - result[rawOutputIndex] = value; + TensorIndices inputIndex = unpackIndex( shape, rawInputIndex ); + TensorIndices outputIndex = transposeVector( inputIndex, permutation ); + int rawOutputIndex = packIndex( transposedShape, outputIndex ); + result[rawOutputIndex] = tensor[rawInputIndex]; } return result; } -unsigned int tensorSize( TensorShape shape ); - // See https://github.com/onnx/onnx/blob/main/docs/Broadcasting.md#multidirectional-broadcasting TensorShape getMultidirectionalBroadcastShape( TensorShape shape1, TensorShape shape2 ); From 8e6db4b6153acd7ced4548b13f1a2c4f501b9581 Mon Sep 17 00:00:00 2001 From: MatthewDaggitt Date: Fri, 24 Jan 2025 16:29:28 +0800 Subject: [PATCH 2/2] Fix formatting issues --- src/input_parsers/TensorUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input_parsers/TensorUtils.h b/src/input_parsers/TensorUtils.h index e9ca275e1..3d4607485 100644 --- a/src/input_parsers/TensorUtils.h +++ b/src/input_parsers/TensorUtils.h @@ -79,7 +79,7 @@ Vector transposeTensor( Vector tensor, TensorShape shape, Permutation perm // NOTE this implementation is *very* inefficient. Eventually we might want to // switch to a similar implementation as NumPy arrays with internal strides etc. ASSERT( shape.size() == permutation.size() ); - ASSERT( tensorSize(shape) == tensor.size() ); + ASSERT( tensorSize( shape ) == tensor.size() ); TensorShape transposedShape = transposeVector( shape, permutation ); Vector result( tensor.size() );