@@ -229,16 +229,26 @@ private void CanRunInferenceOnAModel(GraphOptimizationLevel graphOptimizationLev
229229 {
230230 string modelPath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "squeezenet.onnx" ) ;
231231
232- // Set the graph optimization level for this session.
233- SessionOptions options = new SessionOptions ( ) ;
234- options . GraphOptimizationLevel = graphOptimizationLevel ;
235- if ( enableParallelExecution ) options . ExecutionMode = ExecutionMode . ORT_PARALLEL ;
236-
237- using ( var session = new InferenceSession ( modelPath , options ) )
232+ using ( var cleanUp = new DisposableList < IDisposable > ( ) )
238233 {
234+ // Set the graph optimization level for this session.
235+ SessionOptions options = new SessionOptions ( ) ;
236+ options . GraphOptimizationLevel = graphOptimizationLevel ;
237+ if ( enableParallelExecution ) options . ExecutionMode = ExecutionMode . ORT_PARALLEL ;
238+ cleanUp . Add ( options ) ;
239+
240+ var session = new InferenceSession ( modelPath , options ) ;
241+ cleanUp . Add ( session ) ;
242+
239243 var inputMeta = session . InputMetadata ;
244+ var outputMeta = session . OutputMetadata ;
240245 var container = new List < NamedOnnxValue > ( ) ;
241246
247+ float [ ] expectedOutput = LoadTensorFromFile ( @"bench.expected_out" ) ;
248+ int [ ] expectedDimensions = { 1 , 1000 , 1 , 1 } ; // hardcoded for now for the test data
249+ ReadOnlySpan < int > expectedOutputDimensions = expectedDimensions ;
250+ string [ ] expectedOutputNames = new string [ ] { "softmaxout_1" } ;
251+
242252 float [ ] inputData = LoadTensorFromFile ( @"bench.in" ) ; // this is the data for only one input tensor for this model
243253
244254 foreach ( var name in inputMeta . Keys )
@@ -249,8 +259,6 @@ private void CanRunInferenceOnAModel(GraphOptimizationLevel graphOptimizationLev
249259 container . Add ( NamedOnnxValue . CreateFromTensor < float > ( name , tensor ) ) ;
250260 }
251261
252- ReadOnlySpan < int > expectedOutputDimensions = new int [ ] { 1 , 1000 , 1 , 1 } ;
253- string [ ] expectedOutputNames = new string [ ] { "softmaxout_1" } ;
254262
255263 // Run inference with named inputs and outputs created with in Run()
256264 using ( var results = session . Run ( container ) ) // results is an IReadOnlyList<NamedOnnxValue> container
@@ -291,9 +299,40 @@ private void CanRunInferenceOnAModel(GraphOptimizationLevel graphOptimizationLev
291299 }
292300 }
293301
302+ // Run inference with outputs pinned from buffers
303+ using ( var pinnedInputs = new DisposableListTest < FixedBufferOnnxValue > ( ) )
304+ using ( var pinnedOutputs = new DisposableListTest < FixedBufferOnnxValue > ( ) )
305+ {
306+ var memInfo = OrtMemoryInfo . DefaultInstance ; // CPU
307+
308+ // Create inputs
309+ Assert . Single ( inputMeta . Keys ) ;
310+ var inputNames = inputMeta . Keys . ToArray ( ) ;
311+ var inputName = inputNames [ 0 ] ;
312+ Assert . Equal ( typeof ( float ) , inputMeta [ inputName ] . ElementType ) ;
313+ Assert . True ( inputMeta [ inputName ] . IsTensor ) ;
314+ var longShape = Array . ConvertAll < int , long > ( inputMeta [ inputName ] . Dimensions , d => d ) ;
315+ var byteSize = ArrayUtilities . GetSizeForShape ( longShape ) * sizeof ( float ) ;
316+ pinnedInputs . Add ( FixedBufferOnnxValue . CreateFromMemory < float > ( memInfo , inputData ,
317+ TensorElementType . Float , longShape , byteSize ) ) ;
318+
319+
320+ // Prepare output buffer
321+ Assert . Single ( outputMeta . Keys ) ;
322+ var outputNames = outputMeta . Keys . ToArray ( ) ;
323+ var outputName = outputNames [ 0 ] ;
324+ Assert . Equal ( typeof ( float ) , outputMeta [ outputName ] . ElementType ) ;
325+ Assert . True ( outputMeta [ outputName ] . IsTensor ) ;
326+ longShape = Array . ConvertAll < int , long > ( outputMeta [ outputName ] . Dimensions , d => d ) ;
327+ byteSize = ArrayUtilities . GetSizeForShape ( longShape ) * sizeof ( float ) ;
328+ float [ ] outputBuffer = new float [ expectedOutput . Length ] ;
329+ pinnedOutputs . Add ( FixedBufferOnnxValue . CreateFromMemory < float > ( memInfo , outputBuffer ,
330+ TensorElementType . Float , longShape , byteSize ) ) ;
331+
332+ session . Run ( inputNames , pinnedInputs , outputNames , pinnedOutputs ) ;
333+ Assert . Equal ( expectedOutput , outputBuffer , new floatComparer ( ) ) ;
334+ }
294335
295- float [ ] expectedOutput = LoadTensorFromFile ( @"bench.expected_out" ) ;
296- int [ ] expectedDimensions = { 1 , 1000 , 1 , 1 } ; // hardcoded for now for the test data
297336 // Run inference with named inputs and named outputs
298337 {
299338 // correct pre-allocated outputs
@@ -1954,6 +1993,10 @@ private void TestIOBinding()
19541993 var inputTensor = tuple . Item3 ;
19551994 var outputData = tuple . Item4 ;
19561995 dispList . Add ( session ) ;
1996+ var runOptions = new RunOptions ( ) ;
1997+ dispList . Add ( runOptions ) ;
1998+
1999+ var inputMeta = session . InputMetadata ;
19572000 var outputMeta = session . OutputMetadata ;
19582001 var outputTensor = new DenseTensor < float > ( outputData , outputMeta [ outputName ] . Dimensions ) ;
19592002
@@ -1967,8 +2010,8 @@ private void TestIOBinding()
19672010 {
19682011 var cyrName = "несуществующийВыход" ;
19692012 var longShape = Array . ConvertAll < int , long > ( outputMeta [ outputName ] . Dimensions , i => i ) ;
1970- ioBinding . BindOutput ( outputName , Tensors . TensorElementType . Float , longShape , ortAllocationOutput ) ;
1971- ioBinding . BindOutput ( cyrName , Tensors . TensorElementType . Float , longShape , ortAllocationOutput ) ;
2013+ ioBinding . BindOutput ( outputName , TensorElementType . Float , longShape , ortAllocationOutput ) ;
2014+ ioBinding . BindOutput ( cyrName , TensorElementType . Float , longShape , ortAllocationOutput ) ;
19722015 string [ ] outputs = ioBinding . GetOutputNames ( ) ;
19732016 Assert . Equal ( 2 , outputs . Length ) ;
19742017 Assert . Equal ( outputName , outputs [ 0 ] ) ;
@@ -1982,7 +2025,7 @@ private void TestIOBinding()
19822025 {
19832026 ioBinding . BindInput ( inputName , fixeInputBuffer ) ;
19842027 ioBinding . BindOutput ( outputName , fixedOutputBuffer ) ;
1985- using ( var outputs = session . RunWithBindingAndNames ( new RunOptions ( ) , ioBinding ) )
2028+ using ( var outputs = session . RunWithBindingAndNames ( runOptions , ioBinding ) )
19862029 {
19872030 Assert . Equal ( 1 , outputs . Count ) ;
19882031 var output = outputs . First ( ) ;
@@ -2000,7 +2043,7 @@ private void TestIOBinding()
20002043 ioBinding . BindInput ( inputName , fixedInputBuffer ) ;
20012044 ioBinding . BindOutputToDevice ( outputName , allocator . Info ) ;
20022045
2003- using ( var outputs = session . RunWithBindingAndNames ( new RunOptions ( ) , ioBinding ) )
2046+ using ( var outputs = session . RunWithBindingAndNames ( runOptions , ioBinding ) )
20042047 {
20052048 Assert . Equal ( 1 , outputs . Count ) ;
20062049 var output = outputs . First ( ) ;
@@ -2040,7 +2083,7 @@ private void TestWeightSharingBetweenSessions()
20402083 }
20412084 var dataBufferNumBytes = ( uint ) dataBuffer . Length * sizeof ( float ) ;
20422085 var sharedInitializer = OrtValue . CreateTensorValueWithData ( ortCpuMemInfo , Tensors . TensorElementType . Float ,
2043- dims , dataHandle . AddrOfPinnedObject ( ) , dataBufferNumBytes ) ;
2086+ dims , dataHandle . AddrOfPinnedObject ( ) , dataBufferNumBytes ) ;
20442087
20452088 SessionOptions options = new SessionOptions ( ) ;
20462089 options . AddInitializer ( "W" , sharedInitializer ) ;
0 commit comments