concat with different data formats
Hi,
I have designed a custom network which has one of the layers as concat layer. Question is : Concat is getting runtime inputs with data formats, nChw8c, nchw .Is there a way to use any and configure concat so that it handles reordering runtime.
If we can do it using any please provide me sample code If we can't use any then, can concat take the inputs of nchw and nchw8c and the output the data in nchw data format?
Thank you.
concat will not take any as arg.
In your followup question, are you asking if the following is possible?
concat (out, {in1, in2})
where: in1: nchw in2: nchw8c out: nchw
Yes that is exactly the same question im asking for Nathan
Hi @Subarukun,
I think concat actually can take any.
In fact, there are two different APIs for that:
concat::primitive_desc(concat_dim, inputs), where a user doesn't specify destination at all. In this case the concat would define the destination format on its own.concat::primitive_desc(dst_md, concat_dim, inputs), where a user specifies the destination, but destination format might beanyor some defined format. In the former, case the concat would define the destination format on its own. In the latter case, the concat would follow user's direction.
I made a very simple example that shows all three cases (gist).
Thank you @emfomenk for the detailed example, I got the answer for my query.
Let us take the same scenario for sum primitive too.. I have 2 inputs : n-2,c-16,h-3,w-3 and n-2,c-16,h-3,w-3 with different data formats some how (nchw and nChw8c).
I have changed your concat example slightly for sum, passing the input vector of scales to sum::primitive_desc and built and run it. I found different observations . enforced dst (false case) -> bit compliance is failing enforced dst (true case-any) -> I am seeing exception. enforced dst (true case-nchw) -> I am seeing exception.
So the question is can I use any for sum primitive ? and how to handle the output primitive if the input to sum have different data formats?
Hi @emfomenk, Let us hold the query related to sum primitive for a while. I have one more scenario in the concat. I have 2 inputs n-2,c-16,h-3,w-3 and n-2,c-14,h-3,w-3 with data formats (nChw8c and nChw8c).
We see the second input primitive of channels 14 has nChw8c, this is due to convolution which adds the zero padding to the 14 and transforms it to nChw8c.
In this scenario I am seeing crash , when I try to use any . So do the user need to handle this case explicitly instead of using any?
Thank you.
Hi @Subarukun,
Regarding the first question.
I have changed your concat example slightly for sum, passing the input vector of scales to sum::primitive_desc and built and run it. I found different observations . enforced dst (false case) -> bit compliance is failing enforced dst (true case-any) -> I am seeing exception. enforced dst (true case-nchw) -> I am seeing exception.
So the question is can I use any for sum primitive ? and how to handle the output primitive if the input to sum have different data formats?
The sum works for me in the same manner the concat does. And that is how it is intended to be. I post a sample code here. Could you please check it? If the sample fails for, could you please confirm you use the latest Intel MKL-DNN (we made related fixes sometime ago)?
Regarding the second question.
We see the second input primitive of channels 14 has nChw8c, this is due to convolution which adds the zero padding to the 14 and transforms it to nChw8c.
In this scenario I am seeing crash , when I try to use any . So do the user need to handle this case explicitly instead of using any?
You are absolutely right about zero padding in the case when C = 14. However any should still work just fine (again, assuming you use the fresh Intel MKL-DNN).
The first example I made for concat assumed that channels are always multiple of block size. Hence for memory allocation I used:
memory::desc src1_md({N, C1, H, W}, memory::f32, fmt1);
const int sz1 = N * C1 * H * W; // !!!!!
float *src1 = (float *)malloc(sizeof(float) * sz1);
memory src1_m({src1_md, eng}, src1);
This is fine for either plain formats or the cases when channels are multiple of block sizes. However, for C = 14 and format = nChw8c the size computed is underestimated. The correct way to compute size is:
memory::desc src1_md({N, C1, H, W}, memory::f32, fmt1);
memory::primitive_desc src1_mpd(src1_md, eng);
const int sz1 = src1_mpd.get_size(); // !!!!!
float *src1 = (float *)malloc(sizeof(float) * sz1); init_data(src1, sz1, 1);
memory src1_m({src1_md, eng}, src1);
The function memory::primitive_desc::get_size() guarantees to return the actual size of memory.
I extended the concat gist with this case. And it works fine for me.
It is also worth mentioning that using any is preferable over using some defined format, e.g. nChw8c. In the extended example I showed that if a user tries to force nChw8c as an output for this case the concat fails on creation:
dst is defined by user, use regular API (blocked)
error: could not create a concat primitive descriptor
Using any should not fail. At least in theory... :smile:
Thank you @emfomenk. Examplesyou have given answered my queries.