Skip to content

Commit

Permalink
Merge pull request #9 from moskomule/dev
Browse files Browse the repository at this point in the history
bug fix
  • Loading branch information
moskomule authored Feb 25, 2019
2 parents ded7ec6 + 07a4ae1 commit 532133d
Show file tree
Hide file tree
Showing 38 changed files with 347 additions and 149 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ Now `iteration` of trainer can be updated as follows,
```python
from homura.utils.containers import Map

def iteration(trainer: Trainer, inputs: Tuple[torch.Tensor]) -> Mapping[torch.Tensor]:
input, target = trainer.to_device(inputs)
def iteration(trainer: Trainer, data: Tuple[torch.Tensor]) -> Mapping[torch.Tensor]:
input, target = data
output = trainer.model(input)
loss = trainer.loss_f(output, target)
results = Map(loss=loss, output=output)
Expand Down Expand Up @@ -120,6 +120,23 @@ from homura.reproductivity import set_deterministic
set_deterministic(1)
```

## debugger

```python
>>> debug.module_debugger(nn.Sequential(nn.Linear(10, 5),
nn.Linear(5, 1)),
torch.randn(4, 10))
[homura.debug|2019-02-25 17:57:06|DEBUG] Start forward calculation
[homura.debug|2019-02-25 17:57:06|DEBUG] forward> name=Sequential(1)
[homura.debug|2019-02-25 17:57:06|DEBUG] forward> name=Linear(2)
[homura.debug|2019-02-25 17:57:06|DEBUG] forward> name=Linear(3)
[homura.debug|2019-02-25 17:57:06|DEBUG] Start backward calculation
[homura.debug|2019-02-25 17:57:06|DEBUG] backward> name=Linear(3)
[homura.debug|2019-02-25 17:57:06|DEBUG] backward> name=Sequential(1)
[homura.debug|2019-02-25 17:57:06|DEBUG] backward> name=Linear(2)
[homura.debug|2019-02-25 17:57:06|INFO] Finish debugging mode
```

# Examples

See [examples](examples).
Expand Down
77 changes: 47 additions & 30 deletions docs/build/html/_modules/homura/callbacks.html

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions docs/build/html/_modules/homura/debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ <h1>Source code for homura.debug</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">torch</span> <span class="k">import</span> <span class="n">nn</span>

<span class="kn">from</span> <span class="nn">homura.liblog</span> <span class="k">import</span> <span class="n">get_logger</span><span class="p">,</span> <span class="n">set_verb_level</span>
<span class="kn">from</span> <span class="nn">homura.liblog</span> <span class="k">import</span> <span class="n">get_logger</span><span class="p">,</span> <span class="n">set_verb_level</span><span class="p">,</span> <span class="n">get_verb_level</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;module_debugger&quot;</span><span class="p">]</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">set_verb_level</span><span class="p">(</span><span class="s2">&quot;debug&quot;</span><span class="p">)</span>
<span class="n">_counter</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">()</span>


Expand All @@ -182,19 +181,22 @@ <h1>Source code for homura.debug</h1><div class="highlight"><pre>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;</span><span class="si">{message}</span><span class="s2">&gt;{&#39; &#39; * m.debug_depth} name=</span><span class="si">{m.__class__.__name__}</span><span class="s2">(</span><span class="si">{m.debug_id}</span><span class="s2">)&quot;</span><span class="p">)</span>


<div class="viewcode-block" id="module_debugger"><a class="viewcode-back" href="../../homura.html#homura.debug.module_debugger">[docs]</a><span class="k">def</span> <span class="nf">module_debugger</span><span class="p">(</span><span class="n">model</span><span class="p">:</span> <span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">,</span> <span class="nb">input</span><span class="p">:</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">]</span> <span class="ow">or</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">,</span>
<span class="n">target</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">loss</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> log all modules connected with forward and backward calculation</span>
<div class="viewcode-block" id="module_debugger"><a class="viewcode-back" href="../../homura.html#homura.debug.module_debugger">[docs]</a><span class="k">def</span> <span class="nf">module_debugger</span><span class="p">(</span><span class="n">model</span><span class="p">:</span> <span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">,</span>
<span class="nb">input</span><span class="p">:</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">]</span> <span class="ow">or</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">,</span>
<span class="n">target</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">loss</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Callable</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot; log all modules connected with forward and backward calculation</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">original_verb</span> <span class="o">=</span> <span class="n">get_verb_level</span><span class="p">()</span>
<span class="n">set_verb_level</span><span class="p">(</span><span class="s2">&quot;debug&quot;</span><span class="p">)</span>
<span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="o">.</span><span class="n">extend_apply</span> <span class="o">=</span> <span class="n">_extend_apply</span>
<span class="k">if</span> <span class="n">target</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">loss</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;argument loss should be Callable but got None&quot;</span><span class="p">)</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;Argument loss should be Callable but got None&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DataParallel</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">parallel</span><span class="o">.</span><span class="n">DistributedDataParallel</span><span class="p">):</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;Debugger may not be able to work with {type(model)}&quot;</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">extend_apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">m</span><span class="p">:</span> <span class="n">m</span><span class="o">.</span><span class="n">register_forward_pre_hook</span><span class="p">(</span><span class="n">partial</span><span class="p">(</span><span class="n">_log</span><span class="p">,</span> <span class="s2">&quot;forward&quot;</span><span class="p">)))</span>
<span class="n">model</span><span class="o">.</span><span class="n">extend_apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">m</span><span class="p">:</span> <span class="n">m</span><span class="o">.</span><span class="n">register_backward_hook</span><span class="p">(</span><span class="n">backward_log</span><span class="o">=</span><span class="n">partial</span><span class="p">(</span><span class="n">_log</span><span class="p">,</span> <span class="s2">&quot;backward&quot;</span><span class="p">)))</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Start forward calculation&quot;</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">extend_apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">m</span><span class="p">:</span> <span class="n">m</span><span class="o">.</span><span class="n">register_backward_hook</span><span class="p">(</span><span class="n">partial</span><span class="p">(</span><span class="n">_log</span><span class="p">,</span> <span class="s2">&quot;backward&quot;</span><span class="p">)))</span>
<span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Start forward calculation&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">torch</span><span class="o">.</span><span class="n">is_tensor</span><span class="p">(</span><span class="nb">input</span><span class="p">):</span>
<span class="nb">input</span> <span class="o">=</span> <span class="p">(</span><span class="nb">input</span><span class="p">,)</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">model</span><span class="p">(</span><span class="o">*</span><span class="nb">input</span><span class="p">)</span>
Expand All @@ -203,7 +205,9 @@ <h1>Source code for homura.debug</h1><div class="highlight"><pre>
<span class="k">else</span><span class="p">:</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">output</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Start backward calculation&quot;</span><span class="p">)</span>
<span class="n">output</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span></div>
<span class="n">output</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
<span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Finish debugging mode&quot;</span><span class="p">)</span>
<span class="n">set_verb_level</span><span class="p">(</span><span class="n">original_verb</span><span class="p">)</span></div>
</pre></div>

</div>
Expand Down
23 changes: 23 additions & 0 deletions docs/build/html/_modules/homura/liblog.html
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ <h1>Source code for homura.liblog</h1><div class="highlight"><pre>
<span class="n">formatter</span> <span class="o">=</span> <span class="n">_create_default_formatter</span><span class="p">()</span>
<span class="n">fh</span><span class="o">.</span><span class="n">setFormatter</span><span class="p">(</span><span class="n">formatter</span><span class="p">)</span>
<span class="n">_get_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">fh</span><span class="p">)</span></div>


<div class="viewcode-block" id="set_tqdm_handler"><a class="viewcode-back" href="../../homura.html#homura.liblog.set_tqdm_handler">[docs]</a><span class="k">def</span> <span class="nf">set_tqdm_handler</span><span class="p">(</span><span class="n">level</span><span class="p">:</span> <span class="nb">str</span> <span class="ow">or</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">,</span>
<span class="n">formatter</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">logging</span><span class="o">.</span><span class="n">Formatter</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">tqdm</span> <span class="k">import</span> <span class="n">tqdm</span>

<span class="k">class</span> <span class="nc">TQDMHandler</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">StreamHandler</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">logging</span><span class="o">.</span><span class="n">StreamHandler</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">emit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">record</span><span class="p">):</span>
<span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">record</span><span class="p">)</span>
<span class="n">tqdm</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

<span class="n">_configure_root_logger</span><span class="p">()</span>
<span class="n">th</span> <span class="o">=</span> <span class="n">TQDMHandler</span><span class="p">()</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="n">level</span> <span class="o">=</span> <span class="n">_LOG_LEVEL</span><span class="p">[</span><span class="n">level</span><span class="p">]</span>
<span class="n">th</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">level</span><span class="p">)</span>
<span class="k">if</span> <span class="n">formatter</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">formatter</span> <span class="o">=</span> <span class="n">_create_default_formatter</span><span class="p">()</span>
<span class="n">th</span><span class="o">.</span><span class="n">setFormatter</span><span class="p">(</span><span class="n">formatter</span><span class="p">)</span>
<span class="n">_get_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">th</span><span class="p">)</span></div>
</pre></div>

</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/_modules/homura/reporters.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ <h1>Source code for homura.reporters</h1><div class="highlight"><pre>

<span class="kn">from</span> <span class="nn">.callbacks</span> <span class="k">import</span> <span class="n">Callback</span><span class="p">,</span> <span class="n">CallbackList</span>
<span class="kn">from</span> <span class="nn">.utils.reporter_backends</span> <span class="k">import</span> <span class="n">TQDMWrapper</span><span class="p">,</span> <span class="n">TensorBoardWrapper</span><span class="p">,</span> <span class="n">LoggerWrapper</span><span class="p">,</span> <span class="n">_num_elements</span><span class="p">,</span> <span class="n">_WrapperBase</span>
<span class="kn">from</span> <span class="nn">.utils.vocabulary</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">.utils._vocabulary</span> <span class="k">import</span> <span class="o">*</span>


<div class="viewcode-block" id="Reporter"><a class="viewcode-back" href="../../homura.html#homura.reporters.Reporter">[docs]</a><span class="k">class</span> <span class="nc">Reporter</span><span class="p">(</span><span class="n">Callback</span><span class="p">,</span> <span class="n">metaclass</span><span class="o">=</span><span class="n">ABCMeta</span><span class="p">):</span>
Expand Down
Loading

0 comments on commit 532133d

Please sign in to comment.