NCHW layout as default for a new accelerator device

iviarcio: <p>Hello everyone,<br>
I’m trying to build a backend for a new accelerator device in Glow.<br>
It happens that this device only works with the NCHW layout (Convolution, etc.). I used OpenCL Backend as base but I still see in HIR, after optimizations, transpose operations as well as convolution operation in NHWC layout. What am I not doing? The models I’m using (lenet-mnist, resnet50) come from ONNX, in NCHW layout.</p>

ptrblck: <p>I don’t know which 3rd party libraries you are using, but note that e.g. cudnn can internally transpose your NCHW data to NHWC if a faster kernel can be found and the overall performance with the permutation would still be better than the alternative NCHW kernel. Depending on your custom backend you might also use a similar 3rd party library.</p>

iviarcio: <p>Thanks for the reply <a class="mention" href="/u/ptrblck">@ptrblck</a>, but unfortunately there is no use of 3rd party libraries. In fact, it is a new neural processing unit that performs operations such as convolution in hardware and, in this case, constants and placeholders must be in NCHW layout also.</p>

jfix: <p>Hi <a class="mention" href="/u/iviarcio">@iviarcio</a>, the OCL backend does this transformation in the below location for convolutions, did you also copy this transforms file over for your backend? Perhaps it needs to be tweaked for your usecase. I’d suggest debugging why the below code isn’t being run on your backend.</p>
<aside class="onebox githubblob">
  <header class="source">

      <a href="https://github.com/pytorch/glow/blob/778d44eff16f8dfac25503e0f917531d71bc950b/lib/Backends/OpenCL/Transforms.cpp#L62" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/glow/blob/778d44eff16f8dfac25503e0f917531d71bc950b/lib/Backends/OpenCL/Transforms.cpp#L62" target="_blank" rel="noopener nofollow ugc">pytorch/glow/blob/778d44eff16f8dfac25503e0f917531d71bc950b/lib/Backends/OpenCL/Transforms.cpp#L62</a></h4>



  <pre class="onebox">    <code class="lang-cpp">
      <ol class="start lines" start="52" style="counter-reset: li-counter 51 ;">
          <li>  if (CN-&gt;getLayout() == NCHW) {</li>
          <li>    continue;</li>
          <li>  }</li>
          <li>
          </li>
<li>  // If there is no compiler controlled local memory on the device,</li>
          <li>  // try to avoid kernels that use (additional) copies to local memory.</li>
          <li>  if (devInfo != nullptr &amp;&amp; devInfo-&gt;availableLocalMemory == 0) {</li>
          <li>    continue;</li>
          <li>  }</li>
          <li>
          </li>
<li class="selected">  auto *NR = convertConvToNCHWConv(CN, F);</li>
          <li>  CN-&gt;getResult().replaceAllUsesOfWith(NR);</li>
          <li>  changed = true;</li>
          <li>  continue;</li>
          <li>}</li>
          <li>if (auto *PMN = dyn_cast&lt;MaxPoolNode&gt;(&amp;node)) {</li>
          <li>  if (cctx.compMode == CompilationMode::Train) {</li>
          <li>    continue;</li>
          <li>  }</li>
          <li>
          </li>
<li>  if (PMN-&gt;getLayout() == NCHW) {</li>
      </ol>
    </code>
  </pre>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


iviarcio: <p>Thank you very much <a class="mention" href="/u/jfix">@jfix</a>. I’ll check it out and get back to you, in case you don’t mind, with any other questions or concerns.</p>