|
| 1 | +<p>This rule raises an issue when <code>super()</code> calls are used in methods that are decorated with TorchScript decorators like |
| 2 | +<code>@torch.jit.script_method</code> or within classes likely to be compiled with TorchScript.</p> |
| 3 | +<h2>Why is this an issue?</h2> |
| 4 | +<p>TorchScript has limited support for Python’s <code>super()</code> mechanism, which can lead to compilation errors when converting PyTorch models |
| 5 | +for deployment.</p> |
| 6 | +<p>TorchScript is PyTorch’s way to create serializable and optimizable models from PyTorch code. It allows you to run models independently from |
| 7 | +Python, which is essential for production deployment, mobile applications, and performance optimization.</p> |
| 8 | +<p>However, TorchScript operates with a subset of Python’s features. The <code>super()</code> function relies on Python’s method resolution order |
| 9 | +(MRO) and dynamic attribute lookup, which are not fully supported in TorchScript’s static compilation environment.</p> |
| 10 | +<p>When TorchScript encounters <code>super()</code> calls, it may fail to properly resolve the method calls during compilation, resulting in runtime |
| 11 | +errors or unexpected behavior. This is particularly problematic in <code>forward()</code> methods of neural network modules, where inheritance is |
| 12 | +commonly used.</p> |
| 13 | +<h3>What is the potential impact?</h3> |
| 14 | +<p>Using <code>super()</code> calls in TorchScript methods can cause:</p> |
| 15 | +<ul> |
| 16 | + <li> Compilation failures when converting models to TorchScript format </li> |
| 17 | + <li> Runtime errors in deployed models </li> |
| 18 | + <li> Inconsistent behavior between eager mode and TorchScript execution </li> |
| 19 | + <li> Deployment issues in production environments and mobile applications </li> |
| 20 | +</ul> |
| 21 | +<h3>How to fix in PyTorch?</h3> |
| 22 | +<p>Replace super() calls with direct method calls or refactor the inheritance structure to avoid super() usage in TorchScript methods.</p> |
| 23 | +<h4>Non-compliant code example</h4> |
| 24 | +<pre data-diff-id="1" data-diff-type="noncompliant"> |
| 25 | +import torch |
| 26 | +import torch.nn as nn |
| 27 | + |
| 28 | +class MyModel(nn.Module): |
| 29 | + @torch.jit.script_method |
| 30 | + def forward(self, x): |
| 31 | + return super().forward(x) # Noncompliant |
| 32 | +</pre> |
| 33 | +<h4>Compliant code example</h4> |
| 34 | +<pre data-diff-id="1" data-diff-type="compliant"> |
| 35 | +import torch |
| 36 | +import torch.nn as nn |
| 37 | + |
| 38 | +class MyModel(nn.Module): |
| 39 | + def forward(self, x): |
| 40 | + # Avoid super() in TorchScript methods |
| 41 | + return self.process(x) |
| 42 | + |
| 43 | + def process(self, x): |
| 44 | + return x |
| 45 | +</pre> |
| 46 | +<p>For complex inheritance scenarios, explicitly call parent class methods by name instead of using super().</p> |
| 47 | +<h4>Non-compliant code example</h4> |
| 48 | +<pre data-diff-id="2" data-diff-type="noncompliant"> |
| 49 | +import torch |
| 50 | +import torch.nn as nn |
| 51 | + |
| 52 | +class BaseModel(nn.Module): |
| 53 | + def forward(self, x): |
| 54 | + return x * 2 |
| 55 | + |
| 56 | +class DerivedModel(BaseModel): |
| 57 | + @torch.jit.script_method |
| 58 | + def forward(self, x): |
| 59 | + result = super().forward(x) # Noncompliant |
| 60 | + return result + 1 |
| 61 | +</pre> |
| 62 | +<h4>Compliant code example</h4> |
| 63 | +<pre data-diff-id="2" data-diff-type="compliant"> |
| 64 | +import torch |
| 65 | +import torch.nn as nn |
| 66 | + |
| 67 | +class BaseModel(nn.Module): |
| 68 | + def forward(self, x): |
| 69 | + return x * 2 |
| 70 | + |
| 71 | + def base_forward(self, x): |
| 72 | + return x * 2 |
| 73 | + |
| 74 | +class DerivedModel(BaseModel): |
| 75 | + @torch.jit.script_method |
| 76 | + def forward(self, x): |
| 77 | + result = self.base_forward(x) |
| 78 | + return result + 1 |
| 79 | +</pre> |
| 80 | +<h3>Documentation</h3> |
| 81 | +<ul> |
| 82 | + <li> TorchScript Language Reference - <a href="https://pytorch.org/docs/stable/jit_language_reference.html">Official documentation on TorchScript |
| 83 | + language features and limitations</a> </li> |
| 84 | + <li> TorchScript Unsupported Features - <a href="https://pytorch.org/docs/stable/jit_unsupported.html">List of Python features not supported in |
| 85 | + TorchScript</a> </li> |
| 86 | + <li> PyTorch JIT Tutorial - <a href="https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html">Introduction to TorchScript and best |
| 87 | + practices</a> </li> |
| 88 | +</ul> |
| 89 | + |
0 commit comments