Skip to content

Commit

Permalink
rename to CastToTensorBackport
Browse files Browse the repository at this point in the history
  • Loading branch information
warner-benjamin committed Jun 14, 2022
1 parent e6e1e06 commit 7a1366d
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 124 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Like fastai, fastxtend provides safe wildcard imports using python’s `__all__`
from fastai.vision.all import *
from fastxtend.vision.all import *
```
In general, import fastxtend after all fastai imports, as fastxtend modifies or replaces fastai code.
In general, import fastxtend after all fastai imports, as fastxtend modifies fastai. Any method modified by fastxtend is backwards compatible with the original fastai code.

## Documentation
https://warner-benjamin.github.io/fastxtend/
https://fastxtend.benjaminwarner.dev
6 changes: 3 additions & 3 deletions docs/_data/sidebars/home_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ entries:
output: web
title: Audio
- folderitems:
- output: web,pdf
title: Cast to Tensor Backport
url: callback.casttotensor.html
- output: web,pdf
title: Channels Last
url: callback.channelslast.html
Expand All @@ -42,9 +45,6 @@ entries:
- output: web,pdf
title: Simple Profiler
url: callback.simpleprofiler.html
- output: web,pdf
title: Tensor Cast Backport
url: callback.tensorcast.html
- output: web,pdf
title: Tracker
url: callback.tracker.html
Expand Down
107 changes: 107 additions & 0 deletions docs/callback.casttotensor.html
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>


95 changes: 0 additions & 95 deletions docs/callback.tensorcast.html

This file was deleted.

4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ <h2 id="Usage">Usage<a class="anchor-link" href="#Usage"> </a></h2><p>Like fasta
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">fastai.vision.all</span> <span class="kn">import</span> <span class="o">*</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>In general, import fastxtend after all fastai imports, as fastxtend modifies or replaces fastai code.</p>
<p>In general, import fastxtend after all fastai imports, as fastxtend modifies fastai. Any method modified by fastxtend is backwards compatible with the original fastai code.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Documentation">Documentation<a class="anchor-link" href="#Documentation"> </a></h2><p><a href="https://warner-benjamin.github.io/fastxtend/">https://warner-benjamin.github.io/fastxtend/</a></p>
<h2 id="Documentation">Documentation<a class="anchor-link" href="#Documentation"> </a></h2><p><a href="https://fastxtend.benjaminwarner.dev">https://fastxtend.benjaminwarner.dev</a></p>

</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"Mixup": "audio.05_mixup.html"
},
"Callbacks": {
"Cast to Tensor Backport": "callback.casttotensor.html",
"Channels Last": "callback.channelslast.html",
"CutMixUp": "callback.cutmixup.html",
"LR Finder": "callback.lr_finder.html",
"Simple Profiler": "callback.simpleprofiler.html",
"Tensor Cast Backport": "callback.tensorcast.html",
"Tracker": "callback.tracker.html"
},
"Data": {
Expand Down
4 changes: 2 additions & 2 deletions fastxtend/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"AudioCutMix": "audio.05_mixup.ipynb",
"AudioCutMixUp": "audio.05_mixup.ipynb",
"AudioCutMixUpAugment": "audio.05_mixup.ipynb",
"CastToTensorBackport": "callback.casttotensor.ipynb",
"ChannelsLastTfm": "callback.channelslast.ipynb",
"ChannelsLastCallback": "callback.channelslast.ipynb",
"Learner.to_channelslast": "callback.channelslast.ipynb",
Expand All @@ -63,7 +64,6 @@
"SimpleProfilerPostCallback": "callback.simpleprofiler.ipynb",
"SimpleProfilerCallback": "callback.simpleprofiler.ipynb",
"Learner.profile": "callback.simpleprofiler.ipynb",
"TensorCastBackport": "callback.tensorcast.ipynb",
"TerminateOnTrainNaN": "callback.tracker.ipynb",
"SaveModelAtEnd": "callback.tracker.ipynb",
"KFoldColSplitter": "data.transforms.ipynb",
Expand Down Expand Up @@ -237,12 +237,12 @@
"audio/augment.py",
"audio/learner.py",
"audio/mixup.py",
"callback/casttotensor.py",
"callback/channelslast.py",
"callback/cutmixup.py",
"callback/ema.py",
"callback/lr_finder.py",
"callback/simpleprofiler.py",
"callback/tensorcast.py",
"callback/tracker.py",
"data/transforms.py",
"losses.py",
Expand Down
2 changes: 1 addition & 1 deletion fastxtend/audio/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .learner import *
from .mixup import *
from ..basics import *
from ..callback import channelslast, lr_finder, tensorcast, tracker
from ..callback import channelslast, lr_finder, casttotensor, tracker
from ..data.all import *
from ..losses import *
from ..metrics import *
Expand Down
2 changes: 1 addition & 1 deletion fastxtend/callback/all.py
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 *
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/callback.tensorcast.ipynb (unless otherwise specified).
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/callback.casttotensor.ipynb (unless otherwise specified).

__all__ = ['TensorCastBackport']
__all__ = ['CastToTensorBackport']

# Cell
from packaging.version import parse
Expand All @@ -19,13 +19,13 @@ def _cast_tensor(x):
else: return cast(x, Tensor) if isinstance(x,torch.Tensor) else x

# Cell
class TensorCastBackport(Callback):
class CastToTensorBackport(Callback):
"Cast Subclassed Tensors to `Tensor`"
order=9 # Right before MixedPrecision

def before_batch(self):
self.learn.xb,self.learn.yb = _cast_tensor(self.learn.xb),_cast_tensor(self.learn.yb)

# Cell
if parse(fastai.__version__) < parse('2.7.0') and TensorCastBackport not in defaults.callbacks:
defaults.callbacks.append(TensorCastBackport)
if parse(fastai.__version__) < parse('2.7.0') and CastToTensorBackport not in defaults.callbacks:
defaults.callbacks.append(CastToTensorBackport)
Loading

0 comments on commit 7a1366d

Please sign in to comment.