ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

How to use NESoftmaxLayer

Open SCUTE-ZZ opened this issue 9 years ago • 6 comments

as this code

Tensor in,out;
in.allocator()->init(TensorInfo(10,1,Format::F32));
out.allocator()->init(*in.info());
printf("before softmax.configure\n");
printf("in.info()->total_size()=%d\n",in.info()->total_size());
printf("out.info()->total_size()=%d\n",out.info()->total_size());

NESoftmaxLayer softmax;
softmax.configure(&in,&out);

in.allocator()->allocate();
out.allocator()->allocate();

printf("after softmax.configure\n");
printf("in.info()->total_size()=%d\n",in.info()->total_size());
printf("out.info()->total_size()=%d\n",out.info()->total_size());

float dst[10];
for(int i=0;i<10;i++) dst[i]=i;
if(!in.info()->has_padding()){
        std::copy_n((char*)dst, in.info()->total_size(), in.buffer());
}

softmax.run();

in my environment the output are

before softmax.configure
in.info()->total_size()=40
out.info()->total_size()=40

after softmax.configure
in.info()->total_size()=1800
out.info()->total_size()=1800

is that normal?

SCUTE-ZZ avatar Apr 06 '17 03:04 SCUTE-ZZ

NESoftMaxLayer is in the list of functions which haven't been ported to use the new accurate padding yet, therefore it falls back onto the overly conservative auto_padding which is why the size ends up being so big. We're in the process of finishing porting the functions to use the new accurate padding method. We'll release an update as soon as it's ready.

AnthonyBarbier avatar Apr 06 '17 08:04 AnthonyBarbier

Thanks I have read the auto_padding. now I change my code as

Tensor in,out;
in.allocator()->init(TensorInfo(10,1,Format::F32));
out.allocator()->init(*in.info());

NESoftmaxLayer softmax;
softmax.configure(&in,&out);


in.allocator()->allocate();
out.allocator()->allocate();

float in_data[10];
for(int i=0;i<10;i++) in_data[i]=i;

std::copy_n(in_data, 10, 
in.buffer()+in.info()->offset_element_in_bytes(Coordinates(0, 0)));

softmax.run();

float out_data[10];      
std::copy_n(out.buffer()+out.info()->offset_element_in_bytes(Coordinates(0, 0)), 
4*10, out_data);
for(int i=0;i<10;i++) printf("%f ",out_data[i]);
puts("");

but the output was 0.000000 0.000000 0.000000 128.000000 0.000000 0.000000 0.000000 128.000000 0.000000 0.000000

what's wrong for me?

SCUTE-ZZ avatar Apr 06 '17 08:04 SCUTE-ZZ

Hello @SCUTE-ZZ,

Can you cast your in pointer to float * when you copy the data to the input tensor (std::copy works differently than mempy)?

std::copy_n(in_data, 10, reinterpret_cast<float*>(in.buffer() + in.info()->offset_element_in_bytes(arm_compute::Coordinates(0, 0))));

GeorgeARM avatar Apr 06 '17 09:04 GeorgeARM

Hello @GeorgeARM, now I have use mempy instead of std::copy

Tensor in,out;
in.allocator()->init(TensorInfo(10,1,Format::F32));
out.allocator()->init(*in.info());

NESoftmaxLayer softmax;
softmax.configure(&in,&out);

in.allocator()->allocate();
out.allocator()->allocate();

float in_data[10];
for(int i=0;i<10;i++) in_data[i]=i;
memcpy(
in.buffer()+in.info()->offset_element_in_bytes(Coordinates(0, 0)),
in_data,
10*sizeof(float));

softmax.run();

float out_data[10];
memcpy(
out_data,
out.buffer()+out.info()->offset_element_in_bytes(Coordinates(0, 0)),
10*sizeof(float));

for(int i=0;i<10;i++)
       printf("%f ",out_data[i]);
puts("");

but the output was -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000

SCUTE-ZZ avatar Apr 06 '17 11:04 SCUTE-ZZ

@SCUTE-ZZ so it seems they were two problems: a) you were copying wrong with copy_n which you addressed in the patch above b) it seems that softmax might fail if your output is not a multiple of 4, we did notice this recently (and is fixed internally) as we added more boards in our testing farm. It seems that an exponent is raised to a really high negative value causing the sequence of calculating this exponent to return -inf breaking the sum part of the softmax equation [exp(x - max(x)) / sum(exp(x - max(x)))], that's why you get -0. We didn't notice this as the boards we were initially testing on didn't have such behavior.

In other words, this will be fixed in the next maintenance release in the following days.

Thanks

GeorgeARM avatar Apr 06 '17 12:04 GeorgeARM

@GeorgeARM I change size 10 to 16 the output was true. Thanks

SCUTE-ZZ avatar Apr 06 '17 13:04 SCUTE-ZZ