oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

Weights memory tag for a GEMM-base convolution

Open alenik01 opened this issue 6 years ago • 6 comments

Good afternoon.

Let's consider GEMM-based 2D convolution with NHWC layout. In this case memory tag for the weights tensor will be chosen as hwio == cdba according to gemm_convolution_utils.cpp and dnnl_types.h. However, nhwc layout corresponds to acdb, which is equal to ohwi and not hwio. May you please comment, why different layouts are chosen in this case for src/dst tensors and wei?

Best regards, Aleksandr Nikolaev

alenik01 avatar Oct 30 '20 21:10 alenik01

Hi Aleksandr,

Yeah, this situation around abcd / nchw / hwio is a little convoluted. Let me try to clarify.

  1. abcd...z notation is domain agnostic. It doesn't bring any meaning to the axes. It just defines the permutation of the physical dimensions with respect to the axes order of what we call "logical tensors".

    • Each primitive defines its own meaning for the logical tensors (documented in the user's guide). Let's focus on a convolution. The link to the guide, check "Data Representation" section. The documentation says, that the logical tensor of source is NCHW and logical tensor of weights is OIHW. That means that src_md.dims = {N, C, H, W} and wei_md.dims = {O, I, H, W} no matter what the physical format is. The native order of the dimensions always represented as abcd....
    • If physical order of dimensions for the source is NCHW, there is no permutation, so that tag will abcd.
    • If physical order of dimensions for the source is NHWC, there is a permutation (NCHW->NHWC, i.e. 1234->1342), and this permutation is acdb.
    • If physical order of dimensions for the weights is HWIO, there is a permutation (OIHW->HWIO, i.e. 1234->3421), and this permutation is cdba.
  2. For simplicity of use, and due to some historical reasons, we also keep the domain-specific tags, like nchw / nhwc / oihw / hwio. These tags are just aliases to the corresponding domain-agnostic tags like abcd/acdb/..., and used just to make the reading of the code a little easier. For instance:

    • { dims={N, C, H, W}, type=f32, tag=nhwc } -- easy to grasp that the physical order is NHWC
    • { dims={N, C, H, W}, type=f32, tag=acdb } -- same as above, but one need to "decode" that for the logical dimensions NCHW and permutation abcd->acdb, the physical order of dimensions will be NHWC.
  3. Notice that nchw / nhwc are tags that correspond to the data, the logical order of which is NCHW. Hence:

    • nchw corresponds to abcd (permutation: 1234->1234)
    • nhwc corresponds to acdb (permutation: 1234->1342)
  4. The hwio / oihw are tags that correspond to the weights, for which the logical order is OIHW. Hence:

    • oihw corresponds to abcd (permutation: 1234->1234)
    • hwio corresponds to cdba (permutation: 1234->3421)

ohwi is a tag for weights, so it corresponds to 1234->1342 permutation, which is acdb. For the source data, that corresponds to nhwc. But there is no much sense in this "equality" from the user perspective. This equality makes sense only for reorder primitive, because it doesn't really care whether you do nchw -> nhwc or oihw -> ohwi transformations -- both are identical from the loop order and stride computation perspective...

Hope this helps.


P.S. I also want to note, that it is not really a "rule" that for src=nhwc and the weights format any the chosen format for weights should be hwio. It was what we just chose back when implemented gemm-based convolution (in fact, I guess partially the rationale was that TensorFlow uses nhwc and hwio combination, so we chose the format that is friendly for TF). But the implementations are free to choose whatever works best for them. So, if for Arm it would be beneficial to use some other weights format than hwio it is perfectly fine to use it.

emfomenk avatar Oct 31 '20 00:10 emfomenk

Hello, Evarist.

Many thanks for the detailed explanation, this indeed helps. I also have a question about the grouped convolutions with ngroups = 1: if there is {G, O, I, KH, KW} weights with abcde and the group dimension is "degenerate" to 1, will then such a tensor have the same physical layout as {O, I, KH, KW} with abcd? I have seen several times in CI tests ./benchdnn --conv problem descriptors which start from "g1". May you please enlighten me here: if the number of groups equals to 1 and the group dimension is physically outermost, will it make any difference compared to rank-4 tensor?

I guess partially the rationale was that TensorFlow uses nhwc and hwio combination, so we chose the format that is friendly for TF

This was my main question, on the reasoning for hwio. Arm Compute Library at the moment supports only the same physical layout for all of the three tensors (with the possible subsequent transformation of weights), thus setting the correct memory tag for NHWC will allow to extend its functionality support within oneDNN.

Best wishes, Aleksandr

alenik01 avatar Nov 01 '20 17:11 alenik01

Yeah, in oneDNN groups is a dedicated dimension in the weights (unlike some other libraries/frameworks where groups are just embedded into the output channels, i.e. oc, ic --> oc/g*g, ic/g).

A convolution w/o groups is a special case of a convolution with the number groups equals 1. OneDNN can handle both cases:

  1. Non-grouped convolution:
    • src{n, ic, ih, iw} (*) weights{oc, ic, kh, kw} --> dst{n, oc, oh, ow}
  2. Grouped convlution with g=1:
    • src{n, ic, ih, iw} (*) weights{g, oc / g, ic / g, kh, kw} --> dst{n, oc, oh, ow}

For most layouts (especially the plain ones like goihw, hwigo, ihwgo) the physical data layout of grouped (with g=1) weights and non-grouped weights will be the same. Indeed:

// Row-major
[g][o][i][h][w] ~ [o][i][h][w] // when g is 1
[i][h][w][g][o] ~ [i][h][w][o] // when g is 1

However, in hypothetical case this is not always true. Example:

LOGICAL DIMS:   g   o    i        h      w 
dims:         { 1,  o,   i,       h,     w}
padded_dims:  { 8,  o,   i,       h,     w}
strides:      { o,  1, 8*o, w*i*8*o, i*8*o}

This example is similar to hwigo format, but groups (for whatever reason) are padded to be multiple of 8. In this case, of course the physical layout of such weights won't be the same as just hwio weights. But as I said, this is very hypothetical example.

Re question about benchdnn. Since, formally convolution w/o groups is different from the convolution w/ groups = 1 (since the op desc is different, and the number of dimensions in weights is different, and so on), we must test both approaches. In benchdnn this is implemented in the following way:

  • If a users passes g1 in the problem, that means user wants to create a grouped convolution with g=1.
  • Otherwise, if problem doesn't have g1, that means the convolution will be non-grouped.

Example:

# w/o groups
$ DNNL_VERBOSE=1 ./benchdnn --conv ic32oc64_ih13kh3ph1
...
dnnl_verbose,exec,cpu,convolution,jit:avx2,forward_training,src_f32::blocked:aBcd8b:f0 wei_f32::blocked:ABcd8b8a:f0 bia_f32::blocked:a:f0 dst_f32::blocked:aBcd8b:f0,,alg:convolution_direct,mb2_ic32oc64_ih13oh13kh3sh1dh0ph1_iw13ow13kw3sw1dw0pw1,0.0629883
...

# w/ groups (g=1) 
$ DNNL_VERBOSE=1 ./benchdnn --conv g1_ic32oc64_ih13kh3ph1
...
dnnl_verbose,exec,cpu,convolution,jit:avx2,forward_training,src_f32::blocked:aBcd8b:f0 wei_f32::blocked:aBCde8c8b:f0 bia_f32::blocked:a:f0 dst_f32::blocked:aBcd8b:f0,,alg:convolution_direct,mb2_g1ic32oc64_ih13oh13kh3sh1dh0ph1_iw13ow13kw3sw1dw0pw1,0.0800781
...

Notice the difference in the weights format:

Case Weights format Number of dims
w/o groups blocked:ABcd8b8a 4 dims (a ~ oc, b ~ ic, c ~ kh, d ~ kw)
w/ groups (g=1) blocked:aBCde8c8b:f0 5 dims (a ~ 1, b ~ oc, c ~ ic, d ~ kh, e ~ kw)

Now, given the whole theory, let me go back to your questions.

May you please enlighten me here: if the number of groups equals to 1 and the group dimension is physically outermost, will it make any difference compared to rank-4 tensor?

Assuming the "group" dimension is not padded, there would be no difference with rank-4 tensor as long as the groups go right after the output-channels dimension. Examples:

  • goihw ~ oihw,
  • hwigo ~ hwio,
  • ghwio !~ hwio, because g and o are not together.

This was my main question, on the reasoning for hwio. Arm Compute Library at the moment supports only the same physical layout for all of the three tensors (with the possible subsequent transformation of weights), thus setting the correct memory tag for NHWC will allow to extend its functionality support within oneDNN.

If a user specifies any for the weights, it is perfectly fine to return other than hwio format in case of ArmCL implementation. There is no requirement that all implementations should behave similarly. There might be some obstacles, such as:

  • Current framework integration gaps (e.g. it could be hardcoded that if src format is nhwc than the weights format must be hwio even if passed as any)
    • This is highly unlikely.
  • Framework doesn't "cache" the reordered weights because it expects that the weights format will be compatible with the framework ones
    • This is highly unlikely as well, since oneDNN likes to use some custom weights format such as OIhw16i16o.
  • Creating extra reorders between operations. But this is relevant for the data, not the weights. In general, OneDNN tries to stick to a single data format to avoid extra reorder between the convolutions. Just FYI.

emfomenk avatar Nov 03 '20 05:11 emfomenk

@emfomenk , thanks a lot, now everything is clear. I have tested these g1 convolutions -- ACL supports it with the tags abcde and abdec.

Current framework integration gaps

And may you please concretise, by "framework" here do you mean TensorFlow, PyTorch or similar things? I have not yet seen such a hardcoding of tags in oneDNN codebase.

Creating extra reorders between operations

I will check this in the case of ACL and acdb.

Best wishes, Aleksandr

alenik01 avatar Nov 04 '20 22:11 alenik01

Hi @alenik01 ,

And may you please concretise, by "framework" here do you mean TensorFlow, PyTorch or similar things? I have not yet seen such a hardcoding of tags in oneDNN codebase.

Yeah, I meant something like TF, PyTorch, or, in fact, any application/library that uses oneDNN. But as you mentioned TF and PyTorch probably don't have such hard-coding anywhere.

Creating extra reorders between operations

I will check this in the case of ACL and acdb.

What I meant is that when one adds a new convolution implementation to oneDNN (here, new implementation means not only a new kernel code, but also changing the behavior, like the default formats), what's important is to not only maximize the performance of the individual convolution, but also make sure that the chosen behavior won't affect negatively the overall topology performance. Example:

1. conv_impl1
    - default src_fmt: nhwc
    - default dst_fmt: nhwc
2. conv_impl2
    - default src_fmt: nhwc
    - default dst_fmt: nhwc
3. conv_impl3
    - default src_fmt: nchw
    - default dst_fmt: nchw
4. conv_impl4
    - default src_fmt: nchw
    - default dst_fmt: nhwc
  • Implementations conv_impl1 and conv_impl2 could easily work together in one topology. No extra reorders will be required if conv_impl1 happens after conv_impl2 or vice versa.
  • However, conv_impl3 has a different default src and dst formats. So, if for whatever reason the topology will make oneDNN to use the mix of conv_impl1 and conv_impl3, there would be a bunch of reorders between those, which could negatively affect the overall performance.
  • Finally, the conv_impl4 is the worst case scenario. It is not even consistent with itself. Indeed, the sequence of conv_impl4 --> conv_impl4 will require the intermediate reorder as well.

Of course, it doesn't mean that it is not allowed to use any other formats than the current ones. It is just about being cautious about choosing the default formats.

// And as I said this whole paragraph is not really related to the weights, but to the data 😄

emfomenk avatar Nov 04 '20 22:11 emfomenk

Hello, @emfomenk .

Ah, now I get the point, thanks for clarifying. Actually for CNNs Compute Library implementations are called at every layer, acdb leads to a noticeable speedup compared to abcd at least for ResNet50 and VGG19 cases.

Kind regards, Aleksandr

alenik01 avatar Nov 05 '20 18:11 alenik01