Does torch.jit.script support custom operators?

yzh119: <p>I tried to compile a module that contains a custom op defined by <code>torch.autograd.Function</code>:</p>
<pre><code class="lang-python">import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Function

class mul2(Function):
    @staticmethod
    def forward(ctx, x):
        return x * 2

    @staticmethod
    def backward(ctx, dx):
        return dx * 2

def f(a, b):
    c = a + b
    d = mul2.apply(c)
    e = torch.tanh(d * c)
    return d + (e + e)

print(torch.jit.script(f).code)
</code></pre>
<p>and I received</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "revisble.py", line 21, in &lt;module&gt;
    print(torch.jit.script(f).code)
  File "/Users/***/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1226, in script
    fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))
  File "/Users/***/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1075, in _compile_and_register_class
    ast = get_jit_class_def(obj, obj.__name__)
  File "/Users/***/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 148, in get_jit_class_def
    self_name=self_name) for method in methods]
  File "/Users/***/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 148, in &lt;listcomp&gt;
    self_name=self_name) for method in methods]
  File "/Users/***/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 169, in get_jit_def
    return build_def(ctx, py_ast.body[0], type_line, self_name)
  File "/Users/***/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 198, in build_def
    param_list = build_param_list(ctx, py_def.args, self_name)
  File "/Users/***/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 224, in build_param_list
    raise NotSupportedError(ctx_range, _vararg_kwarg_err)
torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults:
at /Users/***/anaconda3/lib/python3.7/site-packages/torch/autograd/function.py:26:25
    def mark_dirty(self, *args):
                         ~~~~~ &lt;--- HERE
        r"""Marks given tensors as modified in an in-place operation.

        **This should be called at most once, only from inside the**
        :func:`forward` **method, and all arguments should be inputs.**

        Every tensor that's been modified in-place in a call to :func:`forward`
        should be given to this function, to ensure correctness of our checks.
        It doesn't matter whether the function is called before or after
        modification.
'mul2' is being compiled since it was called from 'f'
at revisble.py:17:4
def f(a, b):
    c = a + b
    d = mul2.apply(c)
    ~~~~~~~~~~~~~~~~ &lt;--- HERE
    e = torch.tanh(d * c)
    return d + (e + e)
</code></pre>
<p><code>torch.jit.trace</code> does work either. May I know do script mode support custom ops? If so, what is the correct way to handle custom op?</p>
<p>Thanks!</p>

driazati: <p>We currently don’t support <code>autograd.Function</code> in Python. Right now the workaround is to define the function in C++ and bind it to TorchScript as a <a href="https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html">custom op</a>. This was done in <code>pytorch/vision</code> to support the Mask R-CNN model, you can see the specific implementation <a href="https://github.com/pytorch/vision/pull/1407/files#diff-f78bab9f6ceace5815eccc4f84465144R3-R37">here</a>.</p>

yzh119: <p><a class="mention" href="/u/driazati">@driazati</a> thank you for your timely answer.<br>
It seems I can only write forward function in current torchscript custom ops, right? What if users would like to customize backward functions like what we did in <code>torch.autograd.Function</code>?</p>

driazati: <p>If your C++ op calls a C++ autograd op (i.e. a class that extends <code>public torch::autograd::Function</code>), it will act the same as <code>torch.autograd.Function</code>. For the code below, if you bind <code>my_cool_op</code> and call it from TorchScript, it will use the backward you defined in <code>MyCoolOp</code></p>
<pre><code class="lang-cpp">#include &lt;torch/all.h&gt;
#include &lt;torch/python.h&gt;

class MyCoolOp : public torch::autograd::Function&lt;MyCoolOp&gt; {
 public:
  static torch::autograd::variable_list forward(
      torch::autograd::AutogradContext* ctx,
      torch::autograd::Variable input) {
    // forward calculation
  }

  static torch::autograd::variable_list backward(
      torch::autograd::AutogradContext* ctx,
      torch::autograd::variable_list grad_output) {
    // backward calculation
  }
};

torch::Tensor my_cool_op(const torch::Tensor&amp; input) {
  return MyCoolOp::apply(input);
}
</code></pre>

ifgovh: <p>Hi Driazati, I tried to build my custom operation and the compilation worked. When I load my ops, I got below error. The error even happened when I load the example in documentation. Could you please help me out? Thank you so much!</p>
<p>print(torch.ops.my_ops.SpikeFunction)<br>
Traceback (most recent call last):<br>
File “”, line 1, in <br>
File “/calc/guozhang/anaconda3/lib/python3.8/site-packages/torch/_ops.py”, line 61, in <strong>getattr</strong><br>
op = torch._C._jit_get_operation(qualified_op_name)<br>
RuntimeError: No such operator my_ops::SpikeFunction</p>
<p>The code I used to load the module:</p>
<p>torch.utils.cpp_extension.load(<br>
…     name=“SpikeFunction”,<br>
…     sources=[“spikefunction.cpp”],<br>
…     is_python_module=False,<br>
…     verbose=True<br>
… )<br>
Using /home/guozhang/.cache/torch_extensions as PyTorch extensions root…<br>
Emitting ninja build file /home/guozhang/.cache/torch_extensions/SpikeFunction/build.ninja…<br>
Building extension module SpikeFunction…<br>
Allowing ninja to set a default number of workers… (overridable by setting the environment variable MAX_JOBS=N)<br>
[1/2] c++ -MMD -MF spikefunction.o.d -DTORCH_EXTENSION_NAME=SpikeFunction -DTORCH_API_INCLUDE_EXTENSION_H -DPYBIND11_COMPILER_TYPE="_gcc" -DPYBIND11_STDLIB="_libstdcpp" -DPYBIND11_BUILD_ABI="_cxxabi1011" -isystem /calc/guozhang/anaconda3/lib/python3.8/site-packages/torch/include -isystem /calc/guozhang/anaconda3/lib/python3.8/site-packages/torch/include/torch/csrc/api/include -isystem /calc/guozhang/anaconda3/lib/python3.8/site-packages/torch/include/TH -isystem /calc/guozhang/anaconda3/lib/python3.8/site-packages/torch/include/THC -isystem /calc/guozhang/anaconda3/include/python3.8 -D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -std=c++14 -c /home/guozhang/multiplicative_rule/eprop/spikefunction.cpp -o spikefunction.o<br>
[2/2] c++ spikefunction.o -shared -L/calc/guozhang/anaconda3/lib/python3.8/site-packages/torch/lib -lc10 -ltorch_cpu -ltorch -ltorch_python -o SpikeFunction.so<br>
Loading extension module SpikeFunction…</p>
<p>The C++ code I wrote:<br>
<span class="hashtag">#include</span> &lt;torch/all.h&gt;<br>
<span class="hashtag">#include</span> &lt;torch/python.h&gt;</p>
<p>class SpikeFunction : public torch::autograd::Function {<br>
public:<br>
static torch::autograd::variable_list forward(<br>
torch::autograd::AutogradContext* ctx,<br>
torch::autograd::Variable v_scaled,<br>
torch::autograd::Variable dampening_factor) {<br>
// forward calculation<br>
ctx-&gt;save_for_backward({v_scaled,dampening_factor});<br>
return {torch::greater(v_scaled, 0.)};<br>
}</p>
<p>static torch::autograd::variable_list backward(<br>
torch::autograd::AutogradContext* ctx,<br>
torch::autograd::variable_list grad_output) {<br>
// backward calculation<br>
auto saved = ctx-&gt;get_saved_variables();<br>
auto v_scaled = saved[0];<br>
auto dampening_factor = saved[1];<br>
auto dE_dz = grad_output[0];<br>
auto dz_dv_scaled = torch::maximum(1 - torch::abs(v_scaled), torch::zeros_like(v_scaled)) * dampening_factor;<br>
auto dE_dv_scaled = dE_dz * dz_dv_scaled;<br>
return {dE_dv_scaled, torch::zeros_like(dampening_factor)};<br>
}<br>
};</p>
<p>torch::autograd::variable_list SpikeFunction(const torch::Tensor&amp; v_scaled, const torch::Tensor&amp; dampening_factor) {<br>
return SpikeFunction::apply(v_scaled,dampening_factor);<br>
}</p>

jinfagang: <p>Instead using csrc, I wanna wrap some python line code sinto a single op, then later on impement in a new op in another inference engine.</p>
<p>How to export that?</p>