dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
8.93k stars 1.86k forks source link

Microsoft.ML.Data.ApiUtils.GeneratePeek is not .NET native compiler compatible #6868

Open Braysoft opened 8 months ago

Braysoft commented 8 months ago

Windows 10 ML.NET 2.0.1 but applies to all including latest prerelease. NET nuget 2.0.3 Visual Studio 2022 latest.

User code mlContext.Data.LoadFromInumerable() causes: Microsoft.ML.Data.ApiUtils.GeneratePeek uses JIT on release so a release build that uses this will never work with NET native compiler and can not be used on the Microsoft Store rendering any project (such as mine) useless without a workaround.

To Reproduce Steps to reproduce the behavior: 1.Create a blank UWP project 2.Add the following code where it does not already exist:

    public MainPage()
    {
        this.InitializeComponent();
        MLContext mlContext=new MLContext();

        IDataView emptyDv = mlContext.Data.LoadFromEnumerable(new OnnxInput[] { });

    }
}
public class OnnxInput
{
    [VectorType(1 * 24 * 1)]
    [ColumnName("lambda_input")]
    public float[] Open { get; set; }
}

3.build in release mode with .NET native compiler (or debug with .NET native compiler checked and F5) 4.run app(ctrl+F5 release mode) or F5

Expected behavior App will run and then exit/fail. Inserting Dynamic into the default.rd.xml has no effect due to the JIT nature of the GeneratePeek using il.emit? as mentioned below that is fundamentally incompatible with NET native compiler. I have had frequent communications with someone from NET native at microsoft who has confirmed this issue.

Screenshots, Code, Sample Projects Results of error: System.TypeInitializationException HResult=0x80131534 Message=A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property. Source= StackTrace:

Inner Exception 1: MissingRuntimeArtifactException: Cannot retrieve a MethodInfo for this delegate because the method it targeted (Microsoft.ML.ApiUtils.GeneratePeek[TOwn, TRow, TValue](FieldInfo, OpCode)) was not enabled for metadata using the Dynamic attribute. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=616868 Additionaly a reference to the offending code: [https://github.com/dotnet/machinelearning/blob/766569b86a2ce121321132af46065230851b6559/src/Microsoft.ML.Data/Utils/ApiUtils.cs#L89C1-L107C10] **Additional context** If you require any other information please contact me.