Data layout when transitioning between conv to FC layer
When I do a convolution layer followed by an FC layer, the output has four dimensions, but FC expects 2 dimensions for the input. Simply trying to add a reorder between the conv output and the fc input doesn't work because of the different number of dimensions. Is it expected that we manually flatten the dimensions from 4 to 2? That sounds like it would be very error prone, and in any case, I thought the idea was to let the inner product primitive pick its desired input format.
FC might have input as 2 dimensional tensor, as well as 4 dimensional tensor. So if FC follows a convolution, create FC with {{mb, ic, ih, iw}, any} for src, {{oc, ic, kh=1, kw=1}, any} for weights, and {{mb, oc}, any} for dst.
That doesn't seem to match up with the consistency check in inner_product.cpp:
/* FIXME: fill-in! */
bool consistency = true
&& one_of(src_desc->ndims, 2, 4)
&& dst_desc->ndims == 2
&& weights_desc->ndims == src_desc->ndims
&& (with_bias ? bias_desc->ndims == 1 : true)
&& (with_bias ? bias_desc->dims[0] == dst_desc->dims[1] : true)
&& src_desc->dims[0] == dst_desc->dims[0]
&& array_cmp(&src_desc->dims[1], &weights_desc->dims[1],
src_desc->ndims - 1)
&& dst_desc->dims[1] == weights_desc->dims[0];
if (!consistency) return invalid_arguments;
It appears that the last N-1 dims of src_desc and weights_desc all have to be equal for this check to pass. I was able to get the correct result by supplying it that way, instead of {oc, ic, kh=1, kw=1}.
Indeed, you are right. Since it is fully connected layer, kh should be equal to ih, and kw should be equal to iw.
Thanks. I have a similar problem with FC -> batch norm -> FC.
If the previous layer was a full-connected layer, with output dims {mb, 1, rows, cols}, then the input batch norm dims would need to be {mb, rows * cols, 1, 1}, and this would be the output dimensions of BN as well. But when I create the logical descriptor for the last FC layer, knowing that the previous layer's output had 4 dimensions, I would write the expected input dimensions as {{mb, 1, rows, cols}, any}. This creates issues with the reordering primitive because the input memory has dimensions {mb, rows * cols, 1, 1}, and the output memory is expected to have dimensions {mb, 1, rows, cols}, and these dimensions are not considered "consistent".
I can't just reuse the dimensions from the batch norm since there's no way to get the dimensions from the primitive object. The framework I'm trying to integrate MKL-DNN into as a backend tracks all the dimensions of each layer as the logical dimensions, and I don't want to mix up the logical dimensions there with MKL-DNN's dimensionality idiosyncrasies. Any suggestions?
FC layer by definition has output dims {mb, oc, 1, 1}. Could you please give an example of FC computation which yields {mb, 1, rows, cols} ?
It's been awhile, so some of the details have since left my brain, but let me try to dig up an example to show. Thanks!
At first glance this looks like a convolution with src {mb, ic, h, w}, weights dims {ic, oc=1, kh=1, kw=1} and output dims {mb, 1, h, w}.
I believe part of my problem is that the output of FC is not {mb, oc, 1, 1} but rather {mb, oc}. The output memory can only have 2 dimensions. This is enforced by the consistency check in inner_product.cpp:
bool consistency = true
&& one_of(src_desc->ndims, 2, 4)
&& dst_desc->ndims == 2
&& weights_desc->ndims == src_desc->ndims
&& (with_bias ? bias_desc->ndims == 1 : true)
&& (with_bias ? bias_desc->dims[0] == dst_desc->dims[1] : true)
&& src_desc->dims[0] == dst_desc->dims[0]
&& array_cmp(&src_desc->dims[1], &weights_desc->dims[1],
src_desc->ndims - 1)
&& dst_desc->dims[1] == weights_desc->dims[0];
And this output requirement makes constructing the memory primitive for the batch norm more of a pain. Batch norm requires 4 dimensions, but FC gives it 2, so I actually have to extract the raw pointer from the FC output memory and construct a new memory primitive with it before giving it to the batch norm.
You are right. I guess the state of the art topologies we optimize, didn't have this sequence FC -> batchnom. I don't think we have plans to improve this in near future. We would welcome a PR though.
Thanks for confirming. I'll be able to get by with my current workaround. Are there any plans to expose the memory format/dims of a memory primitive in the API? Currently I can get access to them with memory.get_primitive_desc().get_desc().data.dims, but this looks more like an internal API that I can't rely on.
I captured a feature request to implement batch normalization for nc format based on this discussion.
Thanks a lot!