ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

loading .npy weights into neon_cnn.cpp example

Open GalMoore opened this issue 6 years ago • 10 comments

Hey,

I would like to run the neon_cnn.cpp example with my own input and weights/biases. Rather than a ppm image as input and random weights/ biases I would like to load input from a .npy file and weights/biases from .npy files.

I've managed to use the NPYLoader to load a tensor from .npy file as demonstrated in neon_sgemm.cpp and print it to console (which required compiling with debug=1) to ensure loading correctness.

Now I would like to load my .npy weights/biases into the model in the first two convolutional layers and the fully connected layer.

  1. Should I be using the same NPYLoader for loading weights/biases? or should I try to use the get_weights_accessor method which is used in all the graph examples?

  2. Is there a full example of loading weights and biases from .npy into a neon model? Thanks!

GalMoore avatar Jul 08 '20 10:07 GalMoore

Hi @GalMoore

In my opinion, it'd be better to follow neon_sgemm.cpp's practice (i.e. NPYLoader) in this case as get_weights_accessor approach is more for graph examples whereas both neon_sgemm and neon_cnn are more of runtime examples.

Also, as you said, neon_sgemm.cpp is a good example of loading data from npy files to Tensor (used by NEON).

https://github.com/ARM-software/ComputeLibrary/blob/6a7771e460abeac7d401d6d38a0fcf0a0d2c3cbe/examples/neon_sgemm.cpp#L66

Could you be more specific which steps of loading data you'd like to see an example?

Thanks.

sanpar02 avatar Jul 08 '20 12:07 sanpar02

Hey @sanpar02,

Thanks for your advice.

I have now amended neon_cnn.cpp to accept input and weights/biases from .npy files. I built an identical architecture in pytorch and have checked that with the same input and weights/biases I get the same output at every layer.

This works up until the last layer. After the softmax, when I out_softmax.print(std::cout); in the do_run() function after softmax->run();, I get a vector of 128 elements which are all '1'. In the equivilant pytorch implementation I get actual numbers (0.0081, 0.0077 ...).

Any idea what could explain this? Thanks!

GalMoore avatar Jul 09 '20 14:07 GalMoore

Hi @GalMoore

Good to hear that you've got the results you want (at least before softmax!).

Regarding softmax, it is a bit difficult for me to pinpoint problems without further investigation, especially because you have the expected results from the layer right before the softmax layer.

If could you provide either

  • the input and expected output for the softmax layer so I can try to reproduce the issue in an isolated manner?
  • or if you prefer, you can share inputs and expected output for neon_cnn and the modifications you did to neon_cnn so I can reproduce the whole process. I'll try to reproduce myself.

Thanks.

sanpar02 avatar Jul 10 '20 08:07 sanpar02

Hey @sanpar02 ,

Thanks for your response. You can find the code here.

I could upload the weights and biases (and the input) there too apart from the fully connected weights which are not uploading for some reason. Might be a github limit. Might be a corporate proxy issue.

As all the weights and biases were randomly generated from numpy maybe you could do the same and check the result of the softmax layer? Whatever I try I only get a tensor of 128 '1's.

I look forward to hearing from you. Thanks!

GalMoore avatar Jul 12 '20 15:07 GalMoore

Hi @GalMoore

I have tried to reproduce the issue by downloading files but facing an issue blocking me. Here are the steps.

  • As you mentioned I've generated random numpy files with {shape = 8x8x16, dtype = float32}.
  • I modified all the file path to point correct file location

During runtime, at the following line,

npy0.fill_tensor(src);

I've met the following error message which looks related to the tensor's shape.

!!!!!!!!!!!!!!!!!!!!!!!!!!!

ERROR in fill_tensor ./utils/Utils.h:457: Tensor ranks mismatch 
!!!!!!!!!!!!!!!!!!!!!!!!!!!

FYI, apart from newly generated npy file and file paths, all files are the same as they are downloaded.

Thanks.

sanpar02 avatar Jul 14 '20 12:07 sanpar02

Hey @sanpar02 I think you need to add / remove dimensions from your input tensor and weights/biases so they match the expected dimensions of Arm Compute Library.

I am now facing a separate issue with dilation. I will post as a separate thread. Thank you

GalMoore avatar Jul 19 '20 14:07 GalMoore

Hi @GalMoore

Sorry for the late response.

So, I finally tested myself with

  • Reshaped input in python to the shape of (1x32x32)
  • Randomly generated weights_fc0 as the shape of (128, 1024)

and it could go all through the operators in your test case.

Weirdly, it looks like I'm getting sane softmax results (not all 1's) through my test as shown below. The first group of numbers are biases0 printed after fill_tensor at around line 222 to check if values are well loaded. The second group of numbers are out_softmax printed after softmax->run().

sanpar02 @ vulcan: ~/ComputeLibrary/build/linux-arm64-v8a-neon-cl-debug/examples $ ./neon_Compute_Library_Project 

./neon_Compute_Library_Project

-0.0878641  -0.122517  -0.162436 -0.0359098 -0.0320662  -0.198077   0.179151   0.140276 

0.00877397  0.0163289   0.011537  0.0022342  0.0135069 0.00422067  0.0224981 0.00852463 0.00414811 0.00670129 0.00275945  0.0150879 0.00326452  0.0045554 0.00657585 0.00248928 0.00580079  0.0118074 0.00302621   0.004615 0.00401073  0.0044606 0.00413524  0.0130245 0.00510201    0.01287 0.00334764  0.0123956 0.00489858 0.00276224  0.0142751 0.00648505 0.00554792 0.00327984 0.00550424  0.0194534 0.00960374  0.0124245  0.0146411 0.00406584 0.00764761  0.0098202 0.00617777 0.00637768 0.00683557 0.00512488  0.0102949  0.0279506 0.00491032 0.00275535 0.00677876  0.0110792 0.00465704  0.0056211  0.0108308 0.00245453 0.00813218  0.0087898  0.0114921  0.0011822 0.00165729 0.00567158 0.00948341 0.00349598 0.00623437 0.00357857 0.00206954  0.0195952 0.00913149  0.0126444  0.0110986 0.00872867 0.00762891 0.00652275 0.00405662 0.00900099 0.00398516  0.0126999 0.00772725   0.013971  0.0040943  0.0144299 0.00538602 0.00285468 0.00667658 0.00206991 0.00623896  0.0108511  0.0113189 0.00621927 0.00366012 0.00196866  0.0108393 0.00638643 0.00467094 0.00320461  0.0150137 0.00566449 0.00573741  0.0106032  0.0100499 0.00323717  0.0103122  0.0109199 0.00805205 0.00560107 0.00357056 0.00605776  0.0131741 0.00423901 0.00952436 0.00567936 0.00625201 0.00262448 0.00412272 0.00556838  0.0114079  0.0102441 0.00748336  0.0115176 0.00379018  0.0066866 0.00728984 0.00315444 0.00705397  0.0145866  0.0081226  0.0231515 


Test passed

Please let me know if you find anything I might be missing here.

Thanks.

sanpar02 avatar Jul 30 '20 08:07 sanpar02

Thanks @sanpar02, I saw that someone else is having the same issue with softmax producing all 1s so I hope the culprit will eventually be found. In my case I do not actually need softmax for the time being. I did however want to ask you about running a neon_cnn (like this one) but without having to use the Example abstract class. My results are currently running slower than with TVM - and I wonder if this is because of things happening in the background of Example. Is there an example somewhere of how to build a cnn for Neon in ACL without having to to use the Example?

Thanks, Gal

GalMoore avatar Jul 30 '20 10:07 GalMoore

btw, I compiled ACL with: scons -j8 arch=arm64-v8.2-a extra_cxx_flags="-fPIC" debug=0 build=native neon=1 cppthreads=0 openmp=0. This is because I am checking inference runtime on one single core. Please let me know if you have suggestions to make it run faster. Thanks!

GalMoore avatar Jul 30 '20 10:07 GalMoore

Hi @GalMoore

  • Softmax issue Thanks for pointing out this. Despite the fact that I can't currently reproduce the issue, we'll keep on eye on it.

  • Create an example without Example class I think if you want to remove Example class, I think you can just include the required headers from the library and start from scratch. However, Example class is providing a simple way to write an example and generate a corresponding benchmark example with the help of our build script. Since our benchmarking is based on this Example class, I don't think the performance measurement using Example class shouldn't be a problem.

However, since there should be a lot ongoing with the operating system you are using, you can try to execute the example multiple times and see how the performance varies. If you're building your example using our build script (e.g., neon_Your_Project.cpp in examples folder), the build script will automatically give you "benchmark_neon_Your_Project" binary as well in %BUILD_ROOT%/tests folder.

You can try to run that binary with --iteration=N option and see if how performance varies with multiple times of executions.

Build option looks fine to me and if you are checking the single-core performance, I think the default parameters of examples should be fine.

Lastly, if you still see the performance issue, I suggest opening a separate issue so my colleagues more familiar with performance can help you.

Hope it helps.

sanpar02 avatar Aug 03 '20 14:08 sanpar02