Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: deprecated results obj, added support for simpler comms (1/n) #3681

Merged
merged 14 commits into from
Sep 28, 2020

Conversation

williamFalcon
Copy link
Contributor

@williamFalcon williamFalcon commented Sep 27, 2020

Here we move to deprecate results objects in favor of simpler syntax. Results obj support will remain until 0.10.0 but not 1.0.0.
(ie: 0.10.0 is 1.0.0 but backwards compatible for anyone who really needs it).

The new pattern is to decouple logging from hooks. That means that step, step_end, epoch_end are independent from logging.

example

def training_step(...):
    loss = ...
    return loss

# equivalent
def training_step(...):
    return {'loss': loss}

# to log
def training_step(...):
    self.log('anything', x, on_step=True, on_epoch=True)

Passing around step results.

In the case the user still needs/wants to do something with the output of each batch, the other hooks are still there.

def training_step(...):
    return {'loss': loss, 'random_thing': [1, 'a', Tensor(), ...]}

def training_epoch_end(self, training_step_outputs):
    for d in training_step_outputs:
        random_thing = d['random_thing']
    

Hooks and .log are decoupled...

user can log from anywhere

def training_epoch_end(...):
    some_new_val = ...
    self.log('my_new_val', some_new_val)

cc @ananthsub @awaelchli @justusschock

@pep8speaks
Copy link

pep8speaks commented Sep 27, 2020

Hello @williamFalcon! Thanks for updating this PR.

Line 72:1: W293 blank line contains whitespace

Comment last updated at 2020-09-28 02:48:30 UTC

@mergify mergify bot requested a review from a team September 27, 2020 18:19
@mergify mergify bot requested a review from a team September 27, 2020 18:34
@mergify mergify bot requested a review from a team September 27, 2020 18:35
@williamFalcon williamFalcon changed the title ref: deprecated results obj, added support for simpler comms (1/n) [WIP] ref: deprecated results obj, added support for simpler comms (1/n) Sep 27, 2020
@williamFalcon williamFalcon changed the title [WIP] ref: deprecated results obj, added support for simpler comms (1/n) ref: deprecated results obj, added support for simpler comms (1/n) Sep 27, 2020
@mergify
Copy link
Contributor

mergify bot commented Sep 27, 2020

This pull request is now in conflict... :(

Comment on lines 68 to 75
m = f"""
The {{{k}:dict keyword}} was deprecated in 0.9.1 and will be removed in 1.0.0
Please use self.log(...) inside the lightningModule instead.

# log on a step or aggregate epoch metric to the logger and/or progress bar
# (inside LightningModule)
self.log('train_loss', loss, on_step=True, on_epoch=True, prog_bar=True)
"""
Copy link
Contributor

@carmocca carmocca Sep 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not use triple quote strings for warnings and logs since they include all the extra whitespace to the left:

>>> import warnings
>>> m1 = (
...     "this is "
...     "a "
...     "test"
... )
>>> warnings.warn(m1)
<stdin>:1: UserWarning: this is a test
>>> m2 = """
...   this
...   is a
...   test
... """
>>> warnings.warn(m2)
<stdin>:1: UserWarning: 
  this
  is a
  test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i disagree with this...

these long strings are more readable to the user when the get this kind of warning.

Copy link
Contributor

@carmocca carmocca Sep 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is not that it is multiline, the problem is is that the whitespace at the beginning depends on the indentation level.

For that case in particular:

<stdin>:1: UserWarning: 
                    The {{{k}:dict keyword}} was deprecated in 0.9.1 and will be removed in 1.0.0
                    Please use self.log(...) inside the lightningModule instead.
 
                    # log on a step or aggregate epoch metric to the logger and/or progress bar
                    # (inside LightningModule)
                    self.log('train_loss', loss, on_step=True, on_epoch=True, prog_bar=True)

You would have to either remove the indentation:

                    m = f"""
The {{{k}:dict keyword}} was deprecated in 0.9.1 and will be removed in 1.0.0
Please use self.log(...) inside the lightningModule instead.

# log on a step or aggregate epoch metric to the logger and/or progress bar
# (inside LightningModule)
self.log('train_loss', loss, on_step=True, on_epoch=True, prog_bar=True)
                    """

(pretty ugly if you ask me)

or split it and print it with more than one warnings/logs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @carmocca, if you need an extra line, just use \n

@mergify
Copy link
Contributor

mergify bot commented Sep 28, 2020

This pull request is now in conflict... :(

@williamFalcon williamFalcon changed the title ref: deprecated results obj, added support for simpler comms (1/n) [WIP] ref: deprecated results obj, added support for simpler comms (1/n) Sep 28, 2020
@williamFalcon williamFalcon marked this pull request as draft September 28, 2020 00:46
@williamFalcon williamFalcon marked this pull request as ready for review September 28, 2020 03:19
@williamFalcon williamFalcon merged commit ddd1107 into master Sep 28, 2020
@williamFalcon williamFalcon changed the title [WIP] ref: deprecated results obj, added support for simpler comms (1/n) ref: deprecated results obj, added support for simpler comms (1/n) Sep 28, 2020
@Borda Borda deleted the lg/wr branch September 28, 2020 06:50
@Borda Borda added the refactor label Sep 28, 2020
@Borda Borda added this to the 0.9.x milestone Sep 28, 2020
@nazim1021
Copy link

nazim1021 commented Oct 30, 2020

cc @ananthsub @awaelchli @justusschock
Hi, after updating to version 1.0.4, i think below approach seems to be not working as desired

def training_step(...):
    return {'loss': loss, 'random_thing': [1, 'a', Tensor(), ...]}

def training_epoch_end(self, training_step_outputs):
    for d in training_step_outputs:
        random_thing = d['random_thing']

training_step_outputs is always empty. On further debugging i find that training_step is never getting called, instead this function training_step_and_backward is called within run_training_batch

Any help appreciated and also apologies if i am making any mistake

@williamFalcon
Copy link
Contributor Author

ok, will fix asap. thanks for reporting!

mind replicating it on a boring model?

@edenafek (p0)

@edenlightning
Copy link
Contributor

@nazim1021! Thank you for reporting! I created an issue to track this, would be great if you can try to repro using boring model and paste a colab link in the new issue](#4452)

@nazim1021
Copy link

@williamFalcon @edenlightning sure, of course. Thanks for prompt reply
i ll add the colab link to new issue

@edenlightning edenlightning modified the milestones: 0.10.0, 1.0.x Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants