Repository: Lightning-AI/pytorch-lightning
Pull request: Use high progress_bar_refresh_rate on Google Colab
## What does this PR do?

Automatically override `progress_bar_refresh_rate` when on Google Colab.
For this, I added a `IS_COLAB` constant under `utilities`.

The changes in import were by `isort`.

Fixes #3786

## Before submitting
- [x] Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
- [x] Did you read the [contributor guideline](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/.github/CONTRIBUTING.md), Pull Request section?
- [x] Did you make sure your PR does only one thing, instead of bundling different changes together? Otherwise, we ask you to create a separate PR for every change.
- [ ] Did you make sure to update the documentation with your changes?
- [x] Did you write any new necessary tests? 
- [x] Did you verify new and existing tests pass locally with your changes?
- [x] If you made a notable change (that affects users), did you update the [CHANGELOG](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/CHANGELOG.md)?

## Did you have fun?
Yes 😄 


Problem statement:
automatically override progress_bar_refresh_rate in google colab
## 🚀 Feature
<!-- A clear and concise description of the feature proposal -->

If running on google colab, automatically assign `progress_bar_refresh_rate=20` if the user didn't override the default of 1.

### Motivation

Google colab is notorious for crashing because of the tqdm progress bar while training. We usually solve that by assigning a larger number to `progress_bar_refresh_rate` flag of Trainer. However, this is super annoying since you have to do it all the time.

Also, when writing up examples for beginners, it would be nice to just say `pl.Trainer()` instead of having to pass the refresh rate flag, as that may confuse newcomers. 

<!-- Please outline the motivation for the proposal. Is your feature request related to a problem? e.g., I'm always frustrated when [...]. If this is related to another GitHub issue, please link here too -->

### Pitch

<!-- A clear and concise description of what you want to happen. -->

Put this logic in `CallbackConnector`, or `ProgressBarBase`. I'm not sure which makes more sense.

```python
import sys


def check_if_on_google_colab():
    return 'google.colab' in sys.modules

```

### Alternatives

<!-- A clear and concise description of any alternative solutions or features you've considered, if any. -->

### Additional context

<!-- Add any other context or screenshots about the feature request here. -->

Hints from issue discussion:
Yes please, this is nice. It crashes my whole Chrome if I forget it, it's annoying.
I would put it into the callback connector for the default case because in the progressbarBase it could override the settings if a user has a custom bar and that's probably undesired?
This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, Pytorch Lightning Team!

Commit message: Change warning to suggestion and move it

Moved warning to configure_progress_bar instead of on_trainer_init

Patch to review:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 236784afec84e..47205edb0ea3c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
     [#4495](https://github.com/PyTorchLightning/pytorch-lightning/pull/4495),
     [#4439](https://github.com/PyTorchLightning/pytorch-lightning/pull/4439))
 
+- Added warning when progress bar refresh rate is less than 20 on Google Colab to prevent crashing ([#4654](https://github.com/PyTorchLightning/pytorch-lightning/pull/4654))
+
 
 ### Changed
 
diff --git a/pytorch_lightning/trainer/connectors/callback_connector.py b/pytorch_lightning/trainer/connectors/callback_connector.py
index c9ef4ae32be77..16e7d65d0bbb7 100644
--- a/pytorch_lightning/trainer/connectors/callback_connector.py
+++ b/pytorch_lightning/trainer/connectors/callback_connector.py
@@ -12,11 +12,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import os
+from typing import Optional, Union
 
-from typing import Union, Optional
-
-from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping, ProgressBarBase, ProgressBar
-from pytorch_lightning.utilities import rank_zero_warn
+from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint, ProgressBar, ProgressBarBase
+from pytorch_lightning.utilities import IS_COLAB, rank_zero_warn
 from pytorch_lightning.utilities.exceptions import MisconfigurationException
 
 
@@ -74,6 +73,14 @@ def configure_checkpoint_callbacks(self, checkpoint_callback: Union[ModelCheckpo
             self.trainer.callbacks.append(ModelCheckpoint(dirpath=None, filename=None))
 
     def configure_progress_bar(self, refresh_rate=1, process_position=0):
+        # smaller refresh rate on colab causes crashes, warn user about this
+        if IS_COLAB and refresh_rate < 20:
+            rank_zero_warn(
+                "You have set progress_bar_refresh_rate < 20 on Google Colab. This"
+                " may crash. Consider using progress_bar_refresh_rate>=20 in Trainer.",
+                UserWarning
+            )
+
         progress_bars = [c for c in self.trainer.callbacks if isinstance(c, ProgressBarBase)]
         if len(progress_bars) > 1:
             raise MisconfigurationException(
diff --git a/pytorch_lightning/utilities/__init__.py b/pytorch_lightning/utilities/__init__.py
index 7075c790c60d9..d5da9063fdc8e 100644
--- a/pytorch_lightning/utilities/__init__.py
+++ b/pytorch_lightning/utilities/__init__.py
@@ -12,13 +12,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 """General utilities"""
+import sys
 from enum import Enum
 
 import numpy
 import torch
 
 from pytorch_lightning.utilities.apply_func import move_data_to_device
-from pytorch_lightning.utilities.distributed import rank_zero_only, rank_zero_warn, rank_zero_info
+from pytorch_lightning.utilities.distributed import rank_zero_info, rank_zero_only, rank_zero_warn
 from pytorch_lightning.utilities.parsing import AttributeDict, flatten_dict, is_picklable
 
 try:
@@ -38,3 +39,6 @@
 class AMPType(Enum):
     APEX = 'apex'
     NATIVE = 'native'
+
+
+IS_COLAB = 'google.colab' in sys.modules


Code review comments:
diff_hunk: @@ -38,3 +39,6 @@
 class AMPType(Enum):
     APEX = 'apex'
     NATIVE = 'native'
+
+
+IS_COLAB = 'google.colab' in sys.modules
path: pytorch_lightning/utilities/__init__.py
text: @user1:
```suggestion
```
and remove this.

diff_hunk: @@ -12,11 +12,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import os
+from typing import Optional, Union
 
-from typing import Union, Optional
-
-from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping, ProgressBarBase, ProgressBar
-from pytorch_lightning.utilities import rank_zero_warn
+from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint, ProgressBar, ProgressBarBase
+from pytorch_lightning.utilities import IS_COLAB, rank_zero_warn
path: pytorch_lightning/trainer/connectors/callback_connector.py
text: @user1:
```suggestion
from pytorch_lightning.utilities import rank_zero_warn
```

diff_hunk: @@ -74,6 +73,14 @@ def configure_checkpoint_callbacks(self, checkpoint_callback: Union[ModelCheckpo
             self.trainer.callbacks.append(ModelCheckpoint(dirpath=None, filename=None))
 
     def configure_progress_bar(self, refresh_rate=1, process_position=0):
+        # smaller refresh rate on colab causes crashes, warn user about this
+        if IS_COLAB and refresh_rate < 20:
path: pytorch_lightning/trainer/connectors/callback_connector.py
text: @user1:
```suggestion
        if os.getenv('COLAB_GPU') and refresh_rate < 20:
```
I suggest using this since this is what we use at other places.

@author:
Does this work for Colab CPU and TPU runtimes too?

@user1:
Yes. Its actually a string. On other runtimes its value is '0'.