ignite icon indicating copy to clipboard operation
ignite copied to clipboard

Skip metric computation if output is None

Open vfdev-5 opened this issue 6 years ago • 10 comments

🚀 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.

vfdev-5 avatar May 24 '20 00:05 vfdev-5

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 avatar May 24 '20 07:05 sdesrozis

@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 ?

vfdev-5 avatar May 24 '20 09:05 vfdev-5

Ok I agree what you said. But I’m really not fan, I hope it will be fixed by xla (if possible...)

sdesrozis avatar May 24 '20 10:05 sdesrozis

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...

vfdev-5 avatar Jun 06 '20 13:06 vfdev-5

compute() should be skipped if no update() has been triggered until last reset(), right ?

sdesrozis avatar Jun 06 '20 15:06 sdesrozis

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.

aniketpandey05 avatar Mar 08 '26 16:03 aniketpandey05

"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.

vfdev-5 avatar Mar 08 '26 22:03 vfdev-5

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

aniketpandey05 avatar Mar 09 '26 12:03 aniketpandey05

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 avatar Mar 09 '26 21:03 vfdev-5

@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 changes
  • skip_none=True: if output is None, update() is skipped for that iteration

The all None epoch case stays out of scope as users can handle that via MetricUsage

aniketpandey05 avatar Mar 11 '26 15:03 aniketpandey05