ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

How could I implement an efficient ConcatLayer in ACL?

Open yangyanzhan opened this issue 9 years ago • 11 comments

We have ConcatLayer in Caffe which concatenates several blobs into one blob. I'm wondering how to make an ACL clone for this layer. After inspecting the copy_from method for tensor, I've come up with this code:

const size_t line_size = src_info->num_channels() * src_info->element_size() * src_info->dimension(0);
execute_window_loop(win_src, [&](const Coordinates & id) {
    memcpy(dst_it.ptr(), src_it.ptr(), line_size);
}, src_it, dst_it);

What I did is to repeat the above procedure for all the input tensors and for each input tensor adjust its corresponding memcpy offset accordingly.

execute_window_loop(win_src, [&](const Coordinates & id) {
    memcpy(dst_it.ptr() + offset, src_it.ptr(), line_size);
}, src_it, dst_it);

However, the time consumption of this implementation is too high compared to Caffe. Could anyone give me a hand?

yangyanzhan avatar Apr 20 '17 11:04 yangyanzhan

I'm not sure I understand what you are trying to do: are you trying to batch images ? (i.e put several images in a single tensor ?)

AnthonyBarbier avatar Apr 20 '17 13:04 AnthonyBarbier

As @AnthonyARM mentions we use the Caffe ConcatLayer to concatenate blobs in the batch or channel axis mostly. Is your intention to create an inception like module where you concatenate (merge) the feature maps of the individual branches or is your intention to merge batches (multiple images) ?

Well the problem is more or less the same for both. Due to the padding that we add to optimize the execution of each algorithm we have to copy row by row to the output tensor. We cannot use a single memcpy as caffe does using the caffe_copy call (Caffe doesn't add padding I assume).

I cannot think of a really easy work-around. The only solution I can think of at the moment is to not copy at all. We could just create an output tensor and then create sub-tensors from this tensor where the memory is shared, meaning that changes in the sub-tensors will be visible in the memory of the parent tensor. We could then assign each sub-tensor to the last layers of each path before the ConcatLayer. In this way each path will write to the pre-specified part of the output tensor. This approach needs careful design if you are planning to split each inception branch to a different device (CPU, GPU).

Am I correct assuming that this is the use case or do you want to concatenate on another axis? What is the performance difference you notice? Will you use only NEON, CL or both? Is this something that you urgently need to port your models?

GeorgeARM avatar Apr 20 '17 14:04 GeorgeARM

Hi, thank you Anthony and George. There's only one image in my model. My intention is to concatenate feature maps. Or in Caffe's jargons, I want to concatenate channels. I'm using ACL's NEON backend. It costs caffe less than 1ms to concat four blobs, but costs ACL more than 10ms to do it in a row by row copy fashion.

yangyanzhan avatar Apr 21 '17 07:04 yangyanzhan

I don't understand: why do you have 4 blobs in the first place ? Why don't you load your features directly in the batched features object ?

AnthonyBarbier avatar Apr 21 '17 07:04 AnthonyBarbier

Hi, I'm creating an inception module which extracts 4 kinds of features of the input at the same time and combine them into one output. That's why I have 4 blobs.

yangyanzhan avatar Apr 21 '17 07:04 yangyanzhan

Yes, but where do the features come from ? Do you load them from file ? Why can't you load them directly in the structure instead of creating 4 blobs then combining them ?

AnthonyBarbier avatar Apr 21 '17 07:04 AnthonyBarbier

Do you mean you've got 4 networks generating the 4 features in parallel then you combine them and want to run them on a single image ?

AnthonyBarbier avatar Apr 21 '17 07:04 AnthonyBarbier

Hi, Anthony. These four features are calculated based on the same input data. They are calculated on the fly not in a file so I can't preload them. Yes, you are right, I have 4 sub networks to generate these features.

yangyanzhan avatar Apr 21 '17 07:04 yangyanzhan

I see, it makes more sense now, thanks for explaining. We're working on adding support for sub-tensors to the library where you create a big tensor, then create 1 or more sub-tensors from it which point at the same memory. With that each sub network would generate their feature in a sub tensor then you would the parent tensor to detect all the features at the same time. This is a fair amount of work so it's still going to take some time, but it's coming !

AnthonyBarbier avatar Apr 21 '17 08:04 AnthonyBarbier

Thanks, Anthony. The inception module is critical to our model. I'm really looking forward to see a sub-tensor feature. 👍

yangyanzhan avatar Apr 21 '17 08:04 yangyanzhan

DepthConcatenate and SubTensor support are available now. Example of SubTensor use can be found in the AlexNet network here. Closing! Reopen if needed.

GeorgeARM avatar Sep 29 '17 12:09 GeorgeARM