Where is the implementation of torchvision roi_align?

bughunter: <p>I can’t find anywhere the implementation of this code:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/68f511eb3510ab6fffd258c8aaeffdc1a574212c/torchvision/ops/roi_align.py#L61">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/68f511eb3510ab6fffd258c8aaeffdc1a574212c/torchvision/ops/roi_align.py#L61" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/68f511eb3510ab6fffd258c8aaeffdc1a574212c/torchvision/ops/roi_align.py#L61" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/68f511eb3510ab6fffd258c8aaeffdc1a574212c/torchvision/ops/roi_align.py#L61</a></h4>



  <pre class="onebox">    <code class="lang-py">
      <ol class="start lines" start="51" style="counter-reset: li-counter 50 ;">
          <li>        Tensor[K, C, output_size[0], output_size[1]]: The pooled RoIs.</li>
          <li>    """</li>
          <li>    if not torch.jit.is_scripting() and not torch.jit.is_tracing():</li>
          <li>        _log_api_usage_once(roi_align)</li>
          <li>    _assert_has_ops()</li>
          <li>    check_roi_boxes_shape(boxes)</li>
          <li>    rois = boxes</li>
          <li>    output_size = _pair(output_size)</li>
          <li>    if not isinstance(rois, torch.Tensor):</li>
          <li>        rois = convert_boxes_to_roi_format(rois)</li>
          <li class="selected">    return torch.ops.torchvision.roi_align(</li>
          <li>        input, rois, spatial_scale, output_size[0], output_size[1], sampling_ratio, aligned</li>
          <li>    )</li>
          <li>
          </li>
<li>
          </li>
<li>class RoIAlign(nn.Module):</li>
          <li>    """</li>
          <li>    See :func:`roi_align`.</li>
          <li>    """</li>
          <li>
          </li>
<li>    def __init__(</li>
      </ol>
    </code>
  </pre>


  </article>

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

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


talhaanwarch: <p>may be this is what you are looking</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/eac3dc7bab436725b0ba65e556d3a6ffd43c24e1/torchvision/ops/roi_align.py#L13">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/eac3dc7bab436725b0ba65e556d3a6ffd43c24e1/torchvision/ops/roi_align.py#L13" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/eac3dc7bab436725b0ba65e556d3a6ffd43c24e1/torchvision/ops/roi_align.py#L13" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/eac3dc7bab436725b0ba65e556d3a6ffd43c24e1/torchvision/ops/roi_align.py#L13</a></h4>



  <pre class="onebox">    <code class="lang-py">
      <ol class="start lines" start="3" style="counter-reset: li-counter 2 ;">
          <li>import torch</li>
          <li>from torch import nn, Tensor</li>
          <li>from torch.jit.annotations import BroadcastingList2</li>
          <li>from torch.nn.modules.utils import _pair</li>
          <li>from torchvision.extension import _assert_has_ops</li>
          <li>
          </li>
<li>from ..utils import _log_api_usage_once</li>
          <li>from ._utils import convert_boxes_to_roi_format, check_roi_boxes_shape</li>
          <li>
          </li>
<li>
          </li>
<li class="selected">def roi_align(</li>
          <li>    input: Tensor,</li>
          <li>    boxes: Union[Tensor, List[Tensor]],</li>
          <li>    output_size: BroadcastingList2[int],</li>
          <li>    spatial_scale: float = 1.0,</li>
          <li>    sampling_ratio: int = -1,</li>
          <li>    aligned: bool = False,</li>
          <li>) -&gt; Tensor:</li>
          <li>    """</li>
          <li>    Performs Region of Interest (RoI) Align operator with average pooling, as described in Mask R-CNN.</li>
          <li>
      </li>
</ol>
    </code>
  </pre>


  </article>

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

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


bughunter: <p><a class="mention" href="/u/talhaanwarch">@talhaanwarch</a> so the method is basically calling itself? Line 61 is calling line 13?</p>

ptrblck: <p>The CPU kernel can be found <a href="https://github.com/pytorch/vision/blob/7947fc8fb38b1d3a2aca03f22a2e6a3caa63f2a0/torchvision/csrc/ops/cpu/roi_align_kernel.cpp">here</a> the CUDA kernel <a href="https://github.com/pytorch/vision/blob/7947fc8fb38b1d3a2aca03f22a2e6a3caa63f2a0/torchvision/csrc/ops/cuda/roi_align_kernel.cu">here</a>.</p>

bughunter: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> ! That responds my question since I was interested in learning about how the ROI Align algorith was implemented since I was not able to understand just reading the Mask R CNN Paper.<br>
If you don’t mind I’d like to ask another question now. Is there some documentation explaining how this cpp code is loaded into the python side? When I call <code>torch.ops.torchvision.roi_align</code>, how does that load the cpp kernel that you sent above?<br>
Thank you!</p>

Ritvick_Gupta: <p>Are there plans to support Max mode in ROIALIGN <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

ptrblck: <p>I’m not aware of any plans but also see that you’ve already created a <a href="https://github.com/pytorch/vision/issues/7542">feature request</a>.</p>