<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sindhuja T on Medium]]></title>
        <description><![CDATA[Stories by Sindhuja T on Medium]]></description>
        <link>https://medium.com/@sindhujat?source=rss-243c848fd13------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*41HRegH-ugyGkRJv</url>
            <title>Stories by Sindhuja T on Medium</title>
            <link>https://medium.com/@sindhujat?source=rss-243c848fd13------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:41:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sindhujat/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[‘Legacy autograd’ issue while converting torch.autograd.Function to ONNX]]></title>
            <link>https://sindhujat.medium.com/legacy-autograd-issue-while-converting-torch-autograd-function-to-onnx-fd80ec26077e?source=rss-243c848fd13------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd80ec26077e</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[issues]]></category>
            <category><![CDATA[autograd]]></category>
            <category><![CDATA[onnx]]></category>
            <dc:creator><![CDATA[Sindhuja T]]></dc:creator>
            <pubDate>Sun, 07 Feb 2021 12:16:45 GMT</pubDate>
            <atom:updated>2021-02-08T05:37:04.841Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yc-8Nocc0GsGfwS3rMXTSw.jpeg" /></figure><p>There is a legacy autograd issue when we convert a pytorch model with torch.autograd.Function to onnx. The error is as follows</p><blockquote>RuntimeError: Attempted to trace Detect, but tracing of legacy functions is not supported</blockquote><p>torch.autograd library computes sum of gradients of given tensors w.r.t. graph. By extending torch.autograd to torch.autograd.Function, it records the history of operations and we can define the formulae for differentiating ops.</p><pre>&gt;&gt;&gt; <strong>class</strong> <strong>Exp</strong>(Function):<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;     <strong>@staticmethod</strong><br>&gt;&gt;&gt;     <strong>def</strong> <strong>forward</strong>(ctx, i):<br>&gt;&gt;&gt;         result <strong>=</strong> i<strong>.</strong>exp()<br>&gt;&gt;&gt;         ctx<strong>.</strong>save_for_backward(result)<br>&gt;&gt;&gt;         <strong>return</strong> result<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;     <strong>@staticmethod</strong><br>&gt;&gt;&gt;     <strong>def</strong> <strong>backward</strong>(ctx, grad_output):<br>&gt;&gt;&gt;         result, <strong>=</strong> ctx<strong>.</strong>saved_tensors<br>&gt;&gt;&gt;         <strong>return</strong> grad_output <strong>*</strong> result<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; <em>#Use it by calling the apply method:</em><br>&gt;&gt;&gt; output <strong>=</strong> Exp<strong>.</strong>apply(input)</pre><p>Any model that uses a class definition similar to the one given in the above example faces the runtime error while converting it to ONNX. This is primarily because ONNX does not support legacy functions. Modifying the code as follows would work:</p><pre><strong>def</strong> <strong>Exp_forward</strong>(ctx, i):<br>    result <strong>=</strong> i<strong>.</strong>exp()<br>    ctx<strong>.</strong>save_for_backward(result)<br>    <strong>return</strong> result</pre><pre><strong>def</strong> <strong>Exp_backward</strong>(ctx, grad_output):<br>    result, <strong>=</strong> ctx<strong>.</strong>saved_tensors<br>    <strong>return</strong> grad_output <strong>*</strong> result</pre><pre><strong>main():</strong><br>    Exp_forward(ctx,i)<br>    Exp_backward(ctx,i)</pre><p>Modifying the function call accordingly will help build the model network and there’ll be no issue while converting it to ONNX.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd80ec26077e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>