Skip to content

Memory leak  #5744

Description

@davipeag

System information

  • Windows 10
  • .net 5.0
  • ONNX Runtime installed from nugget
  • ONNX Runtime version: 1.5.2
  • Microsoft.ML version: 1.5.2
  • CPU only

Issue

Even after disposing the Microsoft.ML.PredictionEngine and Microsoft.ML.Transforms.Onnx.OnnxTransformer objects, memory is not freed

Source code

using System;
using System.Collections.Generic;
using Microsoft.ML.Data;
using Microsoft.ML;

namespace Test
{
    class LstmCellInput
    {
        [ColumnName("attributes")]
        [VectorType(1,1,8)]
        public float[] Attributes { get; set; }

        [ColumnName("h_in")]
        [VectorType(1, 1, 16)]
        public float[] HiddenState { get; set; }

        [ColumnName("c_in")]
        [VectorType(1, 1, 16)]
        public float[] CellState { get; set; }
    }

    class LSTMCellOutput
    {
        [ColumnName("prediction")]
        [VectorType(1, 1, 1)]
        public float[] Prediction { get; set; }

        [ColumnName("h_out")]
        [VectorType(1, 1, 16)]
        public float[] HiddenState { get; set; }

        [ColumnName("c_out")]
        [VectorType(1, 1, 16)]
        public float[] CellState { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("start");

            var x = new LstmCellInput {
                HiddenState = new float[16],
                CellState = new float[16],
                Attributes = new float[8]
            };

            for (int i = 0; i < 5000; i++)
            {
                var context = new MLContext();
                var transforms = context.Transforms.ApplyOnnxModel(
                    modelFile: "celllstm.onnx",
                    inputColumnNames: new String[] { "attributes", "h_in", "c_in" },
                    outputColumnNames: new String[] { "prediction", "h_out", "c_out" }
                );
                
                var model = transforms.Fit(context.Data.LoadFromEnumerable(new List<LstmCellInput>()));
                var engine = context.Model.CreatePredictionEngine<LstmCellInput, LSTMCellOutput>(model);
                var y = engine.Predict(x);
                model.Dispose();
                engine.Dispose();
            }

            Console.WriteLine("finish");
        }
    }
}

Additional Info

The onnx model was converted from pytorch
This sample program along with the onnx model is attached
I originally posted this question to ONNX RUNTIME (microsoft/onnxruntime#7303)

Test (1).zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Priority of the issue for triage purpose: Needs to be fixed soon.bugSomething isn't workingonnxExporting ONNX models or loading ONNX models

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions