Skip to content

Commit

Permalink
deploy: fdb9e79
Browse files Browse the repository at this point in the history
  • Loading branch information
facebook-github-bot committed Nov 7, 2024
1 parent 36ae063 commit 13e49cc
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="c1"># pyre-strict</span>
<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Callable</span><span class="p">,</span> <span class="n">cast</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Literal</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">overload</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">,</span> <span class="n">Union</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="p">(</span>
<span class="n">Any</span><span class="p">,</span>
<span class="n">Callable</span><span class="p">,</span>
<span class="n">cast</span><span class="p">,</span>
<span class="n">Dict</span><span class="p">,</span>
<span class="n">List</span><span class="p">,</span>
<span class="n">Literal</span><span class="p">,</span>
<span class="n">Optional</span><span class="p">,</span>
<span class="n">overload</span><span class="p">,</span>
<span class="n">Tuple</span><span class="p">,</span>
<span class="n">Union</span><span class="p">,</span>
<span class="p">)</span>

<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">captum._utils.common</span> <span class="kn">import</span> <span class="p">(</span>
Expand Down Expand Up @@ -147,6 +158,7 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="bp">self</span><span class="p">,</span>
<span class="n">num_outputs_cumsum</span><span class="p">:</span> <span class="n">Tensor</span><span class="p">,</span>
<span class="n">attribute_to_layer_input</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
<span class="n">grad_kwargs</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]],</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">]]:</span>

<span class="k">def</span> <span class="nf">_gradient_func</span><span class="p">(</span>
Expand Down Expand Up @@ -254,7 +266,9 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="p">)</span>
<span class="c1"># torch.unbind(forward_out) is a list of scalar tensor tuples and</span>
<span class="c1"># contains batch_size * #steps elements</span>
<span class="n">grads</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">autograd</span><span class="o">.</span><span class="n">grad</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">unbind</span><span class="p">(</span><span class="n">output</span><span class="p">),</span> <span class="n">inputs</span><span class="p">)</span>
<span class="n">grads</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">autograd</span><span class="o">.</span><span class="n">grad</span><span class="p">(</span>
<span class="n">torch</span><span class="o">.</span><span class="n">unbind</span><span class="p">(</span><span class="n">output</span><span class="p">),</span> <span class="n">inputs</span><span class="p">,</span> <span class="o">**</span><span class="n">grad_kwargs</span> <span class="ow">or</span> <span class="p">{}</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">grads</span>

<span class="k">return</span> <span class="n">_gradient_func</span>
Expand All @@ -271,6 +285,7 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="n">internal_batch_size</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="nb">int</span><span class="p">],</span>
<span class="n">return_convergence_delta</span><span class="p">:</span> <span class="n">Literal</span><span class="p">[</span><span class="kc">False</span><span class="p">],</span>
<span class="n">attribute_to_layer_input</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
<span class="n">grad_kwargs</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]],</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">]]]]:</span> <span class="o">...</span>

<span class="nd">@overload</span>
Expand All @@ -285,6 +300,7 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="n">internal_batch_size</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="nb">int</span><span class="p">],</span>
<span class="n">return_convergence_delta</span><span class="p">:</span> <span class="n">Literal</span><span class="p">[</span><span class="kc">True</span><span class="p">],</span>
<span class="n">attribute_to_layer_input</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
<span class="n">grad_kwargs</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]],</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span>
<span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">]]]],</span>
<span class="n">Tensor</span><span class="p">,</span>
Expand All @@ -304,6 +320,7 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="n">internal_batch_size</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">return_convergence_delta</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
<span class="n">attribute_to_layer_input</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
<span class="n">grad_kwargs</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Union</span><span class="p">[</span>
<span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">]]]],</span>
<span class="n">Tuple</span><span class="p">[</span>
Expand All @@ -328,6 +345,7 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="n">internal_batch_size</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">return_convergence_delta</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
<span class="n">attribute_to_layer_input</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
<span class="n">grad_kwargs</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Union</span><span class="p">[</span>
<span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">Tensor</span><span class="p">,</span> <span class="o">...</span><span class="p">]]]],</span>
<span class="n">Tuple</span><span class="p">[</span>
Expand Down Expand Up @@ -463,6 +481,9 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="sd"> attribute to the input or output, is a single tensor.</span>
<span class="sd"> Support for multiple tensors will be added later.</span>
<span class="sd"> Default: False</span>
<span class="sd"> grad_kwargs (Dict[str, Any], optional): Additional keyword</span>
<span class="sd"> arguments for torch.autograd.grad.</span>
<span class="sd"> Default: None</span>

<span class="sd"> Returns:</span>
<span class="sd"> **attributions** or 2-element tuple of **attributions**, **delta**:</span>
Expand Down Expand Up @@ -559,7 +580,7 @@ <h1>Source code for captum.attr._core.layer.layer_integrated_gradients</h1><div
<span class="c1"># inputs -&gt; these inputs are scaled</span>

<span class="bp">self</span><span class="o">.</span><span class="n">ig</span><span class="o">.</span><span class="n">gradient_func</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_gradient_func</span><span class="p">(</span>
<span class="n">num_outputs_cumsum</span><span class="p">,</span> <span class="n">attribute_to_layer_input</span>
<span class="n">num_outputs_cumsum</span><span class="p">,</span> <span class="n">attribute_to_layer_input</span><span class="p">,</span> <span class="n">grad_kwargs</span>
<span class="p">)</span>
<span class="n">all_inputs</span> <span class="o">=</span> <span class="p">(</span>
<span class="p">(</span><span class="n">inps</span> <span class="o">+</span> <span class="n">additional_forward_args</span><span class="p">)</span>
Expand Down
Loading

0 comments on commit 13e49cc

Please sign in to comment.