Running a CNTK model under a VS unit test

1

I have trained a CNTK model using C#, and now I want to run test cases against a wrapper class so that we can detect problems if somebody replaces the model with one that doesn't perform as well, and additionally have a repeating comparison of the neural network against our old model. However, when I try to run the test I get:

Result StackTrace:  
at CNTK.CNTKLibPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_CNTKLib(ExceptionDelegate applicationDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate divideByZeroDelegate, ExceptionDelegate indexOutOfRangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate invalidOperationDelegate, ExceptionDelegate ioDelegate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate outOfMemoryDelegate, ExceptionDelegate overflowDelegate, ExceptionDelegate systemExceptionDelegate)
   at CNTK.CNTKLibPINVOKE.SWIGExceptionHelper..cctor()
 --- End of inner exception stack trace ---
...
System.TypeInitializationException: The type initializer for 'CNTK.CNTKLibPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'Cntk.Core.CSBinding-2.3.1.dll': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

I have gotten this same error from a stand-alone console app and fixed it by ensuring the console app and class library both build as x64. I made the same changes to the test project, and additionally set the test architecture to x64 (Test > Test Settings > Default Processor Architecture > x64), but no dice.

I am using the GPU version of CNTK (2.3.1), though for the unit test context it should be using CPU. I have verified that Cntk.Core.CSBinding-2.3.1.dll is present in the bin directory.

Any ideas out there? Has anybody tried this?

c#
visual-studio
unit-testing
cntk
asked on Stack Overflow Feb 23, 2018 by C S

2 Answers

1

If you're using NUnit, I've seen this a lot. There comes a bunch of native DLLs with the CNTK package, which the test runner doesn't find, because the working directory is set to some temp directory, not to your output folder. My solution is to copy all necessary DLLs to the working directory every time the test start. I do this in the [SetUp] method of the [TestFixture] in question.

And if you're using a different test framework, maybe it has similar problems.

answered on Stack Overflow Mar 24, 2018 by Yngve Moe
1

I had the same problems, when I used NUnit. Adding the following code to the Test Setup, solved my issue.

[SetUp]
public void Setup()
{
    Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
}
answered on Stack Overflow Mar 28, 2018 by MartinAy

User contributions licensed under CC BY-SA 3.0