Issue with Softmaxlayer
Hi, We are trying to use the softmax layer with input sizes 8,10,16. We are unable to get the desired output. Below is the code for input size 10.Please point us if we are missing out anything:
main() { // The src tensor which will contain the input image Tensor src; Tensor dup_src;
// The weights and biases tensors should be initialized with the values inferred with the training
Tensor dst;
NESoftmaxLayer SoftmaxLayer;
float input_buffer[10] = {-5.174491
,1.911436
,-9.226623
,0.341730
,-6.581529
,16.006193
,4.922042
,-5.961459
,2.368166
,1.662867};
/* [Initialize tensors] */
src.allocator()->init(TensorInfo(TensorShape(64), 1, DataType::F32));
dup_src.allocator()->init(TensorInfo(TensorShape(10), 1, DataType::F32));
dst.allocator()->init(TensorInfo(TensorShape(10), 1, DataType::F32));
printf("SoftmaxLayer :: %d %d\n",dup_src.info()->total_size(),dst.info()->total_size());
SoftmaxLayer.configure(&dup_src, &dst);
printf("SoftmaxLayer :: %d %d\n",dup_src.info()->total_size(),dst.info()->total_size());
src.allocator()->allocate();
dup_src.allocator()->allocate();
dst.allocator()->allocate();
memcpy(src.buffer(),input_buffer,40);
if(src.info()->total_size() == dup_src.info()->total_size())
{
std::copy_n((unsigned char*)src.buffer(), 10*sizeof(float), (unsigned char*)dup_src.buffer());
}
else if(src.info()->total_size() > dup_src.info()->total_size())
{
printf("Entered the elseif %d %d %d\n",src.info()->offset_element_in_bytes(Coordinates(0, 0)),dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)),10*sizeof(float));
std::copy_n((unsigned char*)src.buffer(), 10*sizeof(float), (unsigned char*)dup_src.buffer()+dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)));
}
else
{
printf("WARNING ::: Padding added during the Configure reconsider the copying !!!\n");
printf("%d %d %d %d \n",src.info()->total_size(),dup_src.info()->total_size(),src.info()->offset_element_in_bytes(Coordinates(0, 0)),dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)));
std::copy_n((unsigned char*)src.buffer() + src.info()->offset_element_in_bytes(Coordinates(0, 0)), 10*sizeof(float), (unsigned char*)dup_src.buffer() + dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)));
}
SoftmaxLayer.run();
float * out = (float*)src.buffer();
for(int i=0;i<10;i++)
{
printf("%d :%f\n",i,dst.buffer()+dst.info()->offset_element_in_bytes(Coordinates(0, 0)));
}
} Thanks, G.Praveen.
Hello @uu-praveeng,
If it does work for 8 and 16 then this is the same issue as #27.
Its not working even for 8,16
So for 8 elements (just removed the last two from the matrix above) our gdb outputs:
(gdb) x /8f dst.buffer()
0x1a560: 6.32904895e-10 7.56340739e-07 1.10032391e-11 1.57399327e-07
0x1a570: 1.54977739e-10 0.999983191 1.53534766e-05 5.89882006e-07
which seems the elements are there and with correct values
The issue is with you last print. You print a pointer (to the first element always) as a float value. You should do instead:
for(int i = 0; i < 8; ++i)
{
// Get the pointer to the element, then cast to float* and then dereference to get the value
printf("%d :%f\n",i,*(reinterpret_cast<float*>(dst.ptr_to_element(Coordinates(i,0)))));
}
Hi George,
I have tried your suggestion for 8 inputs,but still no luck .
Target platform is Juno Board Rev 0. Tool chain used is aarch64-oe-linux-g++ (Linaro GCC 4.9-2015.03) 4.9.3 20150311
Unable to attach the files - Please find the code and build commands used for compiling ComputeLibrary-master and the below softmax test code.
`#include <stdio.h>
#include <string.h>
#include <math.h>
#include
#include "arm_compute/runtime/NEON/NEFunctions.h" #include "arm_compute/core/Types.h" #include <sys/time.h>
using namespace arm_compute;
main() { // The src tensor which will contain the input image Tensor src; Tensor dup_src;
// The weights and biases tensors should be initialized with the values inferred with the training
Tensor dst;
NESoftmaxLayer SoftmaxLayer;
float input_buffer[8] = {-5.174491
,1.911436
,-9.226623
,0.341730
,-6.581529
,16.006193
,4.922042
,-5.961459
};
/* [Initialize tensors] */
src.allocator()->init(TensorInfo(TensorShape(64), 1, DataType::F32));
dup_src.allocator()->init(TensorInfo(TensorShape(8), 1, DataType::F32));
dst.allocator()->init(TensorInfo(TensorShape(8), 1, DataType::F32));
printf("SoftmaxLayer :: %d %d\n",dup_src.info()->total_size(),dst.info()->total_size());
SoftmaxLayer.configure(&dup_src, &dst);
printf("SoftmaxLayer :: %d %d\n",dup_src.info()->total_size(),dst.info()->total_size());
src.allocator()->allocate();
dup_src.allocator()->allocate();
dst.allocator()->allocate();
memcpy(src.buffer(),input_buffer,32);
if(src.info()->total_size() == dup_src.info()->total_size())
{
std::copy_n((unsigned char*)src.buffer(), 8*sizeof(float), (unsigned char*)dup_src.buffer());
}
else if(src.info()->total_size() > dup_src.info()->total_size())
{
printf("Entered the elseif %d %d %d\n",src.info()->offset_element_in_bytes(Coordinates(0, 0)),dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)),8*sizeof(float));
std::copy_n((unsigned char*)src.buffer(), 8*sizeof(float), (unsigned char*)dup_src.buffer()+dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)));
}
else
{
printf("WARNING ::: Padding added during the Configure reconsider the copying !!!\n");
printf("%d %d %d %d \n",src.info()->total_size(),dup_src.info()->total_size(),src.info()->offset_element_in_bytes(Coordinates(0, 0)),dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)));
std::copy_n((unsigned char*)src.buffer() + src.info()->offset_element_in_bytes(Coordinates(0, 0)), 8*sizeof(float), (unsigned char*)dup_src.buffer() + dup_src.info()->offset_element_in_bytes(Coordinates(0, 0)));
}
SoftmaxLayer.run();
float * out = (float*)(dst.buffer()+dst.info()->offset_element_in_bytes(Coordinates(0, 0)));
for(int i=0;i<8;i++)
{
printf("%d :%f\n",i,*(reinterpret_cast<float*>(dst.ptr_to_element(Coordinates(i,0)))));
}
}`
Make file commands are
CPP = aarch64-oe-linux-g++
#Creates the libarm.so using the Compute Library code
arm_lib:
$(CPP) -c -fpic -D_GLIBCXX_USE_NANOSLEEP -Wno-deprecated-declarations -Wall -DARCH_ARM -Wextra -Wno-unused-parameter -pedantic -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-include-dirs -Wstrict-overflow=2 -Wswitch-default -fpermissive -std=c++11 -Wno-vla -Woverloaded-virtual -Wctor-dtor-privacy -Wsign-promo -Weffc++ -Wno-format-nonliteral -Wno-overlength-strings -Wno-strict-overflow -Wlogical-op -Wnoexcept -Wstrict-null-sentinel -march=armv8-a -Werror -O3 -gdwarf-2 -Ibuild -I. -Iinclude src/core/NEON/kernels/*.cpp src/runtime/NEON/*.cpp src/runtime/NEON/functions/*.cpp src/core/*.cpp src/core/CPP/*.cpp src/runtime/*.cpp src/core/CPP/kernels/*.cpp src/runtime/CPP/CPPScheduler.cpp
$(CPP) -shared -o libarm.so *.o -lpthread -lrt
rm *.o
sfmax:
$(CPP) -march=armv8-a -O0 -g sfmax_layer.cpp -I. -Iinclude -std=c++11 -L. -larm -o sfmax_layer -Wl,-rpath,.
clean:
rm *.o libarm.so
Thanks in advance.
@uu-praveeng thanks for the details, I managed to reproduce the issue. It's not that you are doing something wrong. It seems that there is an issue with the padding and when 1D input is passed to the SoftmaxLayer. When we fill the border around it overwrites some of the input values if the input is 1D. Our tests were running in higher dimensionality tensors and we didn't spot this. We have fixed this internally and will be committed with the next maintenance patch soon.
Thanks a lot for your help.
Fixed in v17.04 (Re-open if you still see the issue)