dotnet / infer

Infer.NET is a framework for running Bayesian inference in graphical models
https://dotnet.github.io/infer/
MIT License
1.56k stars 230 forks source link

NullReferenceException when using Power operator #427

Closed fvisintini1 closed 1 year ago

fvisintini1 commented 1 year ago

When raising a Variable to a power, using the overloaded XOR operator (^) I get a NullReferenceException.

This is minimal working example to reproduce:

using System;
using Microsoft.ML.Probabilistic.Models;

internal static class Program
{
    private static void Main()
    {
        var a = Variable.Observed(3.14);
        var b = Variable.Observed(2.0);

        var c = a ^ b;

        var engine = new InferenceEngine();
        var res = engine.Infer(c);

        Console.WriteLine(res.ToString());
    }
}

And this is the stack trace:

Compiling model...
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.ML.Probabilistic.Compiler.Transforms.StocAnalysisTransform.InferMarginalPrototype(IMethodInvokeExpression imie, IExpression target, Object targetDecl)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.StocAnalysisTransform.InferMarginalPrototype(IAssignExpression iae, Boolean targetHasLiteralIndices)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.StocAnalysisTransform.ConvertAssign(IAssignExpression iae)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.ConvertExpression(IExpression expr)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.ConvertExpressionStatement(IExpressionStatement ies)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.ConvertStatement(IStatement ist)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.ConvertStatements(IList`1 outputs, IEnumerable`1 inputs)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.DoConvertMethod(IMethodDeclaration md, IMethodDeclaration imd)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.ConvertMethod(IMethodDeclaration imd)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.ConvertMethods(ITypeDeclaration td, ITypeDeclaration itd)
   at Microsoft.ML.Probabilistic.Compiler.Transforms.ShallowCopyTransform.ConvertType(ITypeDeclaration itd)
   at Microsoft.ML.Probabilistic.Compiler.CodeTransformer.TransformToDeclaration(ITypeDeclaration typeDecl)
   at Microsoft.ML.Probabilistic.Compiler.TransformerChain.TransformToDeclaration(ITypeDeclaration itd, AttributeRegistry`2 inputAttributes, Boolean trackTransform, Boolean showProgress, List`1& warnings, Boolean catchExceptions, Boolean treatWarningsAsErrors)
   at Microsoft.ML.Probabilistic.Compiler.ModelCompiler.GetTransformedDeclaration(ITypeDeclaration itd, MethodBase method, AttributeRegistry`2 inputAttributes)
   at Microsoft.ML.Probabilistic.Compiler.ModelCompiler.CompileWithoutParams(ITypeDeclaration itd, MethodBase method, AttributeRegistry`2 inputAttributes)
   at Microsoft.ML.Probabilistic.Models.InferenceEngine.Compile()
   at Microsoft.ML.Probabilistic.Models.InferenceEngine.GetCompiledInferenceAlgorithm(Boolean inferOnlySpecifiedVars, IVariable var)
   at Microsoft.ML.Probabilistic.Models.InferenceEngine.Infer(IVariable var)
   at Program.Main()
tminka commented 1 year ago

Thanks, that is a bug. You can work around it, but I've made a PR to fix.

fvisintini1 commented 1 year ago

Thanks! Do you suggest to build code from source or will you release a new version of the NuGet packages?

tminka commented 1 year ago

The new packages are up.

fvisintini1 commented 1 year ago

Hi @tminka, just want to let you know that I'm experiencing issues with the new nugets.

In many cases, the compilation take quite a long time, where the previous version of the nuget worked just fine.

I was able to narrow the source of these problems to the ContrainBetween factor, and solve by replacing with 2 ContrainTrue factors. This apparently solved some of the issues I had, but other applications are still unstable.

I suspect one or more commits between the last two published versions introduced these instabilities.

tminka commented 1 year ago

Can you create a new issue for this problem, and include repro steps?