Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BUG Report]: LSTM/RNN model throws Null Pointer Exception #1082

Closed
@daz10000

Description

@daz10000

Description

RNN.cs line 42 gets null pointer referencing !cell.Built during model construction.

With the preface that I love this library, and it is likely a mistake on my part triggering this problem, a null pointer exception isn't a great user experience, so this can minimally provide better feedback (plus, would like to know what I'm doing wrong here - the docs for TensorNet.Keras aren't that extensive unless I missed something.

Reproduction Steps

This is a code fragment reproducing the issue. Save the code as example.fsx and run it from command line as dotnet fsi example.fsx

#r "nuget:TensorFlow.Net" 
#r "nuget:TensorFlow.Keras" 
#r "nuget:NumSharp"
#r "nuget:SciSharp.TensorFlow.Redist-Windows-GPU"

open type Tensorflow.KerasApi
open Tensorflow.Keras.Layers

//let test () =
let layers = LayersApi()
let vocab = 21
let inputs = layers.Input(vocab)
let embedding = layers.Embedding(input_dim = vocab, output_dim = 16).Apply(inputs)
// This next step throws NULLReferenceException
let lstm = layers.LSTM(units = 8).Apply(embedding)

When run, I see a stream of GPU errors and then the traceback at the bottom.


$ dotnet fsi t.fsx
2023-05-22 18:31:17.709978: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2023-05-22 18:31:17.710099: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2023-05-22 18:31:17.949514: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-05-22 18:31:17.991281: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2023-05-22 18:31:17.992939: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublas64_11.dll'; dlerror: cublas64_11.dll not found
2023-05-22 18:31:17.994341: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublasLt64_11.dll'; dlerror: cublasLt64_11.dll not found
2023-05-22 18:31:17.995936: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2023-05-22 18:31:17.997305: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2023-05-22 18:31:17.998769: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusolver64_11.dll'; dlerror: cusolver64_11.dll not found
2023-05-22 18:31:18.000300: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusparse64_11.dll'; dlerror: cusparse64_11.dll not found
2023-05-22 18:31:18.001689: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2023-05-22 18:31:18.001723: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1934] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
System.NullReferenceException: Object reference not set to an instance of an object.
   at Tensorflow.Keras.Layers.Rnn.RNN.build(KerasShapesWrapper input_shape)
   at Tensorflow.Keras.Engine.Layer.MaybeBuild(Tensors inputs)
   at Tensorflow.Keras.Engine.Layer.FunctionalConstructionCall(Tensors inputs)
   at Tensorflow.Keras.Engine.Layer.Apply(Tensors inputs, Tensor state, Boolean training)
   at <StartupCode$FSI_0002>.$FSI_0002.main@() in C:\f\BioLoomics\ML\simple1\t.fsx:line 15
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Stopped due to error

In order to capture the faulting line number, I built the libraries from the current main branch and ran locally against those to isolate the problem to line 42 of RNN.cs. Specifically cell appears to be null so something is going wrong during class setup.

 public override void build(KerasShapesWrapper input_shape)
        {
            if (!cell.Built) // Line 42,  cell is null
            {
                cell.build(input_shape);
            }
        }

Thoughts and help appreciated.
p.s. are there decent examples of using the Keras library with Tensor.Net - I am mostly going from python examples but would love to see a few fully worked cases.

Known Workarounds

None I can find so far.

Configuration and Other Information

I am testing on a Win11 machine, from a bash prompt with a Quadro GPU (I don't think that's relevant) and I may not have the CUDA drivers installed properly (also likely not relevant). Dotnet runtime is 7.0.203. The code breaks against both the most current nuget versions of the libraries shown below and also the current main branch of the github repo

  • NumSharp" Version="0.30.0"
  • SciSharp.TensorFlow.Redist-Windows-GPU" Version="2.10.1"
  • TensorFlow.Keras" Version="0.10.5"
  • TensorFlow.Net" Version="0.100.5"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmissing feature/sAn issue about missing features in the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions