oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

Data layout when transitioning between conv to FC layer

Open xyzsam opened this issue 8 years ago • 12 comments

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.

xyzsam avatar Dec 22 '17 16:12 xyzsam

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.

emfomenk avatar Dec 23 '17 01:12 emfomenk

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}.

xyzsam avatar Dec 23 '17 03:12 xyzsam

Indeed, you are right. Since it is fully connected layer, kh should be equal to ih, and kw should be equal to iw.

emfomenk avatar Dec 23 '17 20:12 emfomenk

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?

xyzsam avatar Dec 24 '17 16:12 xyzsam

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} ?

shelleygoel avatar Jan 05 '18 20:01 shelleygoel

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!

xyzsam avatar Jan 05 '18 21:01 xyzsam

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}.

shelleygoel avatar Jan 05 '18 21:01 shelleygoel

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.

xyzsam avatar Jan 10 '18 18:01 xyzsam

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.

shelleygoel avatar Jan 12 '18 17:01 shelleygoel

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.

xyzsam avatar Jan 22 '18 18:01 xyzsam

I captured a feature request to implement batch normalization for nc format based on this discussion.

vpirogov avatar Mar 29 '18 23:03 vpirogov

Thanks a lot!

xyzsam avatar Mar 30 '18 02:03 xyzsam