Skip to content

Commit eed6456

Browse files
Onnx Export change to allow for multiple rows (#4783)
* changed onnx export from always exporting 1 row to exporting -1 which allows for any number of rows * added additional comments per pr comments
1 parent 5f449a4 commit eed6456

20 files changed

+144
-139
lines changed

src/Microsoft.ML.OnnxConverter/OnnxUtils.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,13 @@ public static ModelArgs GetModelArgs(DataViewType type, string colName,
375375
dimsLocal.Add(vec.Dimensions[i]);
376376
}
377377
}
378-
//batch size.
379-
dimsLocal?.Insert(0, 1);
378+
// Set batch size to -1. The ONNX docs, https://github.com/onnx/onnx/blob/master/docs/IR.md#static-tensor-shapes, state that if
379+
// dim_param is used instead of dim_value, that the size of the dimension "is not statically constrained to a particular number"
380+
// "This is useful for declaring the interfaces that care about the number of dimensions, but not the exact size of each dimension"
381+
// This file, https://github.com/onnx/onnx/blob/master/onnx/tools/update_model_dims.py, explains that if the dim value is negative
382+
// than it treats that as a dim_param instead of a dim_value. This allows ML.NET to run 1 row at a time in a streaming fassion,
383+
// but allows the ONNX model the flexiblity to be run in batch mode if that is desired.
384+
dimsLocal?.Insert(0, -1);
380385

381386
return new ModelArgs(name, dataType, dimsLocal, dimsParamLocal);
382387
}

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/ExcludeVariablesInOnnxConversion.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@
503503
"shape": {
504504
"dim": [
505505
{
506-
"dimValue": "1"
506+
"dimValue": "-1"
507507
},
508508
{
509509
"dimValue": "1"
@@ -521,7 +521,7 @@
521521
"shape": {
522522
"dim": [
523523
{
524-
"dimValue": "1"
524+
"dimValue": "-1"
525525
},
526526
{
527527
"dimValue": "1"
@@ -541,7 +541,7 @@
541541
"shape": {
542542
"dim": [
543543
{
544-
"dimValue": "1"
544+
"dimValue": "-1"
545545
},
546546
{
547547
"dimValue": "1"
@@ -559,7 +559,7 @@
559559
"shape": {
560560
"dim": [
561561
{
562-
"dimValue": "1"
562+
"dimValue": "-1"
563563
},
564564
{
565565
"dimValue": "1"
@@ -577,7 +577,7 @@
577577
"shape": {
578578
"dim": [
579579
{
580-
"dimValue": "1"
580+
"dimValue": "-1"
581581
},
582582
{
583583
"dimValue": "1"
@@ -597,7 +597,7 @@
597597
"shape": {
598598
"dim": [
599599
{
600-
"dimValue": "1"
600+
"dimValue": "-1"
601601
},
602602
{
603603
"dimValue": "1"
@@ -615,7 +615,7 @@
615615
"shape": {
616616
"dim": [
617617
{
618-
"dimValue": "1"
618+
"dimValue": "-1"
619619
},
620620
{
621621
"dimValue": "10"
@@ -633,7 +633,7 @@
633633
"shape": {
634634
"dim": [
635635
{
636-
"dimValue": "1"
636+
"dimValue": "-1"
637637
},
638638
{
639639
"dimValue": "10"
@@ -651,7 +651,7 @@
651651
"shape": {
652652
"dim": [
653653
{
654-
"dimValue": "1"
654+
"dimValue": "-1"
655655
},
656656
{
657657
"dimValue": "11"
@@ -669,7 +669,7 @@
669669
"shape": {
670670
"dim": [
671671
{
672-
"dimValue": "1"
672+
"dimValue": "-1"
673673
},
674674
{
675675
"dimValue": "11"

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/LightGbmBinaryClassificationOnnxConversionTest.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@
396396
"shape": {
397397
"dim": [
398398
{
399-
"dimValue": "1"
399+
"dimValue": "-1"
400400
},
401401
{
402402
"dimValue": "11"
@@ -414,7 +414,7 @@
414414
"shape": {
415415
"dim": [
416416
{
417-
"dimValue": "1"
417+
"dimValue": "-1"
418418
},
419419
{
420420
"dimValue": "1"
@@ -434,7 +434,7 @@
434434
"shape": {
435435
"dim": [
436436
{
437-
"dimValue": "1"
437+
"dimValue": "-1"
438438
},
439439
{
440440
"dimValue": "11"
@@ -452,7 +452,7 @@
452452
"shape": {
453453
"dim": [
454454
{
455-
"dimValue": "1"
455+
"dimValue": "-1"
456456
},
457457
{
458458
"dimValue": "1"
@@ -470,7 +470,7 @@
470470
"shape": {
471471
"dim": [
472472
{
473-
"dimValue": "1"
473+
"dimValue": "-1"
474474
},
475475
{
476476
"dimValue": "1"
@@ -490,7 +490,7 @@
490490
"shape": {
491491
"dim": [
492492
{
493-
"dimValue": "1"
493+
"dimValue": "-1"
494494
},
495495
{
496496
"dimValue": "11"
@@ -508,7 +508,7 @@
508508
"shape": {
509509
"dim": [
510510
{
511-
"dimValue": "1"
511+
"dimValue": "-1"
512512
},
513513
{
514514
"dimValue": "1"

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/LogisticRegressionSaveModelToOnnxTest.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
"shape": {
141141
"dim": [
142142
{
143-
"dimValue": "1"
143+
"dimValue": "-1"
144144
},
145145
{
146146
"dimValue": "11"
@@ -158,7 +158,7 @@
158158
"shape": {
159159
"dim": [
160160
{
161-
"dimValue": "1"
161+
"dimValue": "-1"
162162
},
163163
{
164164
"dimValue": "1"
@@ -178,7 +178,7 @@
178178
"shape": {
179179
"dim": [
180180
{
181-
"dimValue": "1"
181+
"dimValue": "-1"
182182
},
183183
{
184184
"dimValue": "11"
@@ -196,7 +196,7 @@
196196
"shape": {
197197
"dim": [
198198
{
199-
"dimValue": "1"
199+
"dimValue": "-1"
200200
},
201201
{
202202
"dimValue": "1"
@@ -214,7 +214,7 @@
214214
"shape": {
215215
"dim": [
216216
{
217-
"dimValue": "1"
217+
"dimValue": "-1"
218218
},
219219
{
220220
"dimValue": "1"
@@ -234,7 +234,7 @@
234234
"shape": {
235235
"dim": [
236236
{
237-
"dimValue": "1"
237+
"dimValue": "-1"
238238
},
239239
{
240240
"dimValue": "11"
@@ -252,7 +252,7 @@
252252
"shape": {
253253
"dim": [
254254
{
255-
"dimValue": "1"
255+
"dimValue": "-1"
256256
},
257257
{
258258
"dimValue": "1"

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/ModelWithLessIO.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@
829829
"shape": {
830830
"dim": [
831831
{
832-
"dimValue": "1"
832+
"dimValue": "-1"
833833
},
834834
{
835835
"dimValue": "8"
@@ -847,7 +847,7 @@
847847
"shape": {
848848
"dim": [
849849
{
850-
"dimValue": "1"
850+
"dimValue": "-1"
851851
},
852852
{
853853
"dimValue": "1"
@@ -867,7 +867,7 @@
867867
"shape": {
868868
"dim": [
869869
{
870-
"dimValue": "1"
870+
"dimValue": "-1"
871871
},
872872
{
873873
"dimValue": "1"
@@ -885,7 +885,7 @@
885885
"shape": {
886886
"dim": [
887887
{
888-
"dimValue": "1"
888+
"dimValue": "-1"
889889
},
890890
{
891891
"dimValue": "1"
@@ -903,7 +903,7 @@
903903
"shape": {
904904
"dim": [
905905
{
906-
"dimValue": "1"
906+
"dimValue": "-1"
907907
},
908908
{
909909
"dimValue": "1"
@@ -923,7 +923,7 @@
923923
"shape": {
924924
"dim": [
925925
{
926-
"dimValue": "1"
926+
"dimValue": "-1"
927927
},
928928
{
929929
"dimValue": "1"
@@ -941,7 +941,7 @@
941941
"shape": {
942942
"dim": [
943943
{
944-
"dimValue": "1"
944+
"dimValue": "-1"
945945
},
946946
{
947947
"dimValue": "9"
@@ -959,7 +959,7 @@
959959
"shape": {
960960
"dim": [
961961
{
962-
"dimValue": "1"
962+
"dimValue": "-1"
963963
},
964964
{
965965
"dimValue": "17"

0 commit comments

Comments
 (0)