-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6e1e06
commit 7a1366d
Showing
12 changed files
with
153 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
--- | ||
|
||
title: Cast To Tensor Backport | ||
|
||
|
||
keywords: fastai | ||
sidebar: home_sidebar | ||
|
||
summary: "A callback to cast model inputs to `Tensor` as a workaroud for a PyTorch performance bug" | ||
description: "A callback to cast model inputs to `Tensor` as a workaroud for a PyTorch performance bug" | ||
nb_path: "nbs/callback.casttotensor.ipynb" | ||
--- | ||
<!-- | ||
################################################# | ||
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ### | ||
################################################# | ||
# file to edit: nbs/callback.casttotensor.ipynb | ||
# command to build the docs after a change: nbdev_build_docs | ||
--> | ||
|
||
<div class="container" id="notebook-container"> | ||
|
||
{% raw %} | ||
|
||
<div class="cell border-box-sizing code_cell rendered"> | ||
|
||
</div> | ||
{% endraw %} | ||
|
||
{% raw %} | ||
|
||
<div class="cell border-box-sizing code_cell rendered"> | ||
|
||
</div> | ||
{% endraw %} | ||
|
||
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> | ||
<div class="text_cell_render border-box-sizing rendered_html"> | ||
<p>For use in fastai 2.6.x or older. Import globally:</p> | ||
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">fastxtend.vision.all</span> <span class="kn">import</span> <span class="o">*</span> | ||
</pre></div> | ||
<p>or individually:</p> | ||
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">fastxtend.callback</span> <span class="kn">import</span> <span class="n">casttotensor</span> | ||
</pre></div> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
{% raw %} | ||
|
||
<div class="cell border-box-sizing code_cell rendered"> | ||
|
||
<div class="output_wrapper"> | ||
<div class="output"> | ||
|
||
<div class="output_area"> | ||
|
||
|
||
<div class="output_markdown rendered_html output_subarea "> | ||
<h2 id="CastToTensorBackport" class="doc_header"><code>class</code> <code>CastToTensorBackport</code><a href="https://github.com/warner-benjamin/fastxtend/tree/main/fastxtend/callback/casttotensor.py#L22" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>CastToTensorBackport</code>(<strong><code>after_create</code></strong>=<em><code>None</code></em>, <strong><code>before_fit</code></strong>=<em><code>None</code></em>, <strong><code>before_epoch</code></strong>=<em><code>None</code></em>, <strong><code>before_train</code></strong>=<em><code>None</code></em>, <strong><code>before_batch</code></strong>=<em><code>None</code></em>, <strong><code>after_pred</code></strong>=<em><code>None</code></em>, <strong><code>after_loss</code></strong>=<em><code>None</code></em>, <strong><code>before_backward</code></strong>=<em><code>None</code></em>, <strong><code>before_step</code></strong>=<em><code>None</code></em>, <strong><code>after_cancel_step</code></strong>=<em><code>None</code></em>, <strong><code>after_step</code></strong>=<em><code>None</code></em>, <strong><code>after_cancel_batch</code></strong>=<em><code>None</code></em>, <strong><code>after_batch</code></strong>=<em><code>None</code></em>, <strong><code>after_cancel_train</code></strong>=<em><code>None</code></em>, <strong><code>after_train</code></strong>=<em><code>None</code></em>, <strong><code>before_validate</code></strong>=<em><code>None</code></em>, <strong><code>after_cancel_validate</code></strong>=<em><code>None</code></em>, <strong><code>after_validate</code></strong>=<em><code>None</code></em>, <strong><code>after_cancel_epoch</code></strong>=<em><code>None</code></em>, <strong><code>after_epoch</code></strong>=<em><code>None</code></em>, <strong><code>after_cancel_fit</code></strong>=<em><code>None</code></em>, <strong><code>after_fit</code></strong>=<em><code>None</code></em>) :: <code>Callback</code></p> | ||
</blockquote> | ||
<p>Cast Subclassed Tensors to <code>Tensor</code></p> | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
</div> | ||
</div> | ||
|
||
</div> | ||
{% endraw %} | ||
|
||
{% raw %} | ||
|
||
<div class="cell border-box-sizing code_cell rendered"> | ||
|
||
</div> | ||
{% endraw %} | ||
|
||
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> | ||
<div class="text_cell_render border-box-sizing rendered_html"> | ||
<p>Workaround for bug in PyTorch where subclassed tensors, such as <code>TensorBase</code>, train up to ~20% slower than <code>Tensor</code> when passed to a model. Added to <code>Learner</code> by default if using fastai 2.6.x or older.</p> | ||
<p>CastToTensorBackport is identical to the CastToTensor callback releasing with fastai 2.7.0.</p> | ||
<p>CastToTensorBackport's order is right before <code>MixedPrecision</code> so callbacks which make use of fastai's tensor subclasses still can use them.</p> | ||
<p>If inputs are not a subclassed tensor or tuple of tensors, you may need to cast inputs in <code>Learner.xb</code> and <code>Learner.yb</code> to <code>Tensor</code> via your own callback or in the dataloader before <code>Learner</code> performs the forward pass.</p> | ||
<p>If the CastToTensorBackport workaround interferes with custom code, it can be removed:</p> | ||
<div class="highlight"><pre><span></span><span class="n">learn</span> <span class="o">=</span> <span class="n">Learner</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> | ||
<span class="n">learn</span><span class="o">.</span><span class="n">remove_cb</span><span class="p">(</span><span class="n">CastToTensorBackport</span><span class="p">)</span> | ||
</pre></div> | ||
<p>You should verify your inputs are of type <code>Tensor</code> or implement a cast to <code>Tensor</code> via a custom callback or dataloader if CastToTensor is removed.</p> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
{% raw %} | ||
|
||
<div class="cell border-box-sizing code_cell rendered"> | ||
|
||
</div> | ||
{% endraw %} | ||
|
||
</div> | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from . import channelslast, lr_finder | ||
from .cutmixup import * | ||
from .tensorcast import * | ||
from .casttotensor import * | ||
from .tracker import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.