Skip metric computation if output is None
🚀 Feature
Considering the context of https://github.com/pytorch/ignite/pull/1024
and another point about using tensor.item() on each iteration with TPU which slows down the computation, we need to have a way to skip using state.output if set to None.
In training on TPU
def training_step(e, b):
# ....
# instead of returning each iteration loss value
# return loss.item()
# we would like to do it
return loss.item() if (e.state.iteration - 1) % 20 == 0 else None
however, if we have a running metric attached to the output it will fail if encounter None.
So, idea is to check engine.state.output here and return before doing any metric's update:
https://github.com/pytorch/ignite/blob/e3fc04e147db4c97a2ceb70597366d562e15c96f/ignite/metrics/metric.py#L123-L124
@sdesrozis @erip any thoughts ?
This isssue is related to https://github.com/pytorch/ignite/issues/996 but the difference is that we still want to execute all other handlers on iteration even if output is None.
Why do not return the tensor loss in function training_step and do the fix internally ? Like that, if one day it is resolved, we will not have to change user code.
@sdesrozis for XLA (see the link above):
Calling item() forces extra graph executions, which does indeed slow down performance. This is why we suggest (unless one does not care of execution time) to do that once every ~20 steps: https://github.com/pytorch/xla/blob/3b4b5790ba9e9141f19b0e33b3a7a7cd1bad8ead/test/test_train_mp_mnist.py#L129
if step % FLAGS.log_steps == 0:
Why do not return the tensor loss in function training_step and do the fix internally ? Like that, if one day it is resolved, we will not have to change user code.
IMO, such internal behaviour will be opaque for user who does not intend to skip loss value by returning a tensor. Another point is which rate we should use internally ?
Ok I agree what you said. But I’m really not fan, I hope it will be fixed by xla (if possible...)
An update on that. I tried to implement skipping and there can a potential roadblock with MetricUsage. For example, RunningAverage metric when setup to average output coming from engine.state.output needs to skip its update() AND compute().
While skip update() is easy with Metric class inside iteration_completed method by checking if engine.state.output is None. There is no simple way to skip compute() if update() was skipped. We can also imagine the situation with other types of metric usages...
compute() should be skipped if no update() has been triggered until last reset(), right ?
Hey @vfdev-5 I noticed #1310 was closed using BatchFiltered for a similar case, so I am not sure if the intention is to keep None handling explicit that way, or if a built-in guard in iteration_completed would still be welcome.
"Skip metric computation if output is None" is a "nice to have" feature, its implementation can be tricky. With the current code, we can achieve skipping metric updates using MetricUsage.
Hi @vfdev-5 I went through metric.py, running_average.py and accuracy.py A None guard in iteration_completed handles the partial-skip case fine, but the all-None epoch case is messier Accuracy would raise NotComputableError since _num_examples stays 0, and RunningAverage would silently write None into engine.state.metrics. Also when RunningAverage uses a src metric, attach() registers src.iteration_completed directly, so the base class guard wouldn't even cover that path.
Is the idea to scope this to just the partial-skip case and leave the all-None scenario to the user via MetricUsage
Is the idea to scope this to just the partial-skip case and leave the all-None scenario to the user via MetricUsage
@rogueslasher I'm open for ideas respecting BC compatibility, usefulness and simplicity.
@vfdev-5
For BC compatibility, I think a skip_none=False parameter on attach() rather than iteration_completed attach() So the behavior would be:
skip_none=False(default): current behavior, no changesskip_none=True: if output isNone,update()is skipped for that iteration
The all None epoch case stays out of scope as users can handle that via MetricUsage