question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NuGet packages with native assets in a notebook

See original GitHub issue

Describe the bug

When using the Microsoft.ML NuGet package in a notebook, you get an exception when ML.NET calls into a native assembly.

Repro

Run the following notebook:

#r "nuget:Microsoft.ML"
    
using Microsoft.ML;
using Microsoft.ML.Data;

class IrisData
{
    public IrisData(float sepalLength, float sepalWidth, float petalLength, float petalWidth)
    {
        SepalLength = sepalLength;
        SepalWidth = sepalWidth;
        PetalLength = petalLength;
        PetalWidth = petalWidth;
    }
    public float SepalLength;
    public float SepalWidth;
    public float PetalLength;
    public float PetalWidth;
}

var data = new []
{
    new IrisData(1.4f, 1.3f, 2.5f, 4.5f), 
    new IrisData(2.4f, 0.3f, 9.5f, 3.4f), 
    new IrisData(3.4f, 4.3f, 1.6f, 7.5f), 
    new IrisData(3.9f, 5.3f, 1.5f, 6.5f), 
};

MLContext mlContext = new MLContext();
var pipeline = mlContext.Transforms
    .Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
    .Append(mlContext.Clustering.Trainers.KMeans("Features", numberOfClusters: 2));

try
{
    pipeline.Fit(mlContext.Data.LoadFromEnumerable(data));
}
catch (Exception e)
{
    Console.WriteLine(e);
}

This shouldn’t throw an exception, but it does:

System.DllNotFoundException: Unable to load shared library 'CpuMathNative' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libCpuMathNative, 1): image not found
   at Microsoft.ML.Internal.CpuMath.Thunk.SumSqU(Single* ps, Int32 c)
   at Microsoft.ML.Internal.CpuMath.CpuMathUtils.SumSq(ReadOnlySpan`1 src)
   at Microsoft.ML.Numeric.VectorUtils.NormSquared(VBuffer`1& a)
   at Microsoft.ML.Trainers.KMeansBarBarInitialization.Initialize(IHost host, Int32 numThreads, IChannel ch, Factory cursorFactory, Int32 k, Int32 dimensionality, VBuffer`1[] centroids, Int64 accelMemBudgetMb, Int64& missingFeatureCount, Int64& totalTrainingInstances)
   at Microsoft.ML.Trainers.KMeansTrainer.TrainCore(IChannel ch, RoleMappedData data, Int32 dimensionality)
   at Microsoft.ML.Trainers.KMeansTrainer.TrainModelCore(TrainContext context)
   at Microsoft.ML.Trainers.TrainerEstimatorBase`2.TrainTransformer(IDataView trainSet, IDataView validationSet, IPredictor initPredictor)
   at Microsoft.ML.Trainers.TrainerEstimatorBase`2.Fit(IDataView input)
   at Microsoft.ML.Data.EstimatorChain`1.Fit(IDataView input)
   at Submission#6.<<Initialize>>d__0.MoveNext()

Did this error occur while using dotnet try or online?

  • dotnet-try
  • online

What kind of error was it?

  • User Interface (UI): For example the output never displayed
  • Service Error: For example “The service is temporarily unavailable. We are working on it”
  • Other:

Screenshots

Screen Shot 2019-07-27 at 12 31 07 PM

Please complete the following:

  • OS
    • Windows 10
    • macOS
    • Linux (Please specify distro)
    • iOS
    • Android
  • Browser
    • Chrome
    • Edge
    • Safari

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
ericstjcommented, Jun 25, 2021

How we would create that .deps.json file during dotnet-interactive is not clear to me. My current thinking is that we could get by with inspecting the project.assets.json file that we get from a NuGet restore + the RID fallback graph I described above.

Does dotnet-interactive run a restore on the packages it loads? If it did, it shouldn’t be too much more to generate a deps file for that assembly. According to https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support#load-plugins

It resolves assemblies and native libraries to their relative paths based on the .deps.json file for the class library whose path was passed to the AssemblyDependencyResolver constructor. The custom AssemblyLoadContext enables plugins to have their own dependencies, and the AssemblyDependencyResolver makes it easy to correctly load the dependencies.

Here’s a quick sample I threw together that produces a deps file for a package.

<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="GenerateBuildDependencyFile">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <IncludeMainProjectInDepsFile>false</IncludeMainProjectInDepsFile>
    <NoBuild>true</NoBuild>
    <ProjectDepsFileName>Microsoft.ML.deps.json</ProjectDepsFileName>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ML" Version="1.5.5" />
  </ItemGroup>

</Project>
1reaction
eerhardtcommented, Apr 2, 2020

Yep, that’s the one! That’s how you can get the RID of the current machine. The user shouldn’t need to supply it at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to include native assets in Nuget on multiple platforms ...
I'm creating a NuGet package with the following idea: ... The idea isn't to embed the native resources and then extract them but...
Read more >
Support for Native Projects - The NuGet Blog
If you're interested in publishing your native libraries via NuGet, you can choose to create the NuGet packages manually. However, there's an ...
Read more >
Microsoft.Net.Native.Compiler 2.2.3
NET Native compiler contained in the package, as opposed to any system installed version. ... There are no supported framework assets in this...
Read more >
How to make a Nuget package for C++
If you are looking for more examples of nuget packages holding native projects, do a search on nuget.org, and find a project. Browse...
Read more >
NuGet packages in the Package Registry
Publish NuGet packages in your project's Package Registry. Then, install the packages whenever you need to use them as a dependency. The Package...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found