internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: Lightning-AI/pytorch-lightning

Issue:
Fix callback signature on trainer.html
## 📚 Documentation

According to [callbacks.html][1], `on_train_start` and `on_train_end` receive three arguments, but the callback example on [trainer.html][2] has only one:

```
from pytorch_lightning.callbacks import Callback

class PrintCallback(Callback):
    def on_train_start(self):
        print("Training is started!")
    def on_train_end(self):
        print(f"Training is done. The logs are: {self.trainer.logs}")
```
Also, it seems that `logs` doesn't exists anymore.  Could you please confirm me?


[1]: https://pytorch-lightning.readthedocs.io/en/stable/callbacks.html
[2]: https://pytorch-lightning.readthedocs.io/en/stable/trainer.html


Discussion:

Resolving patch:
diff --git a/pytorch_lightning/trainer/__init__.py b/pytorch_lightning/trainer/__init__.py
--- a/pytorch_lightning/trainer/__init__.py
+++ b/pytorch_lightning/trainer/__init__.py
@@ -238,10 +238,10 @@ def forward(self, x):
     from pytorch_lightning.callbacks import Callback
 
     class PrintCallback(Callback):
-        def on_train_start(self):
+        def on_train_start(self, trainer, pl_module):
             print("Training is started!")
-        def on_train_end(self):
-            print(f"Training is done. The logs are: {self.trainer.logs}")
+        def on_train_end(self, trainer, pl_module):
+            print("Training is done.")
 
 check_val_every_n_epoch
 ^^^^^^^^^^^^^^^^^^^^^^^


Test patch:
Repository: Lightning-AI/pytorch-lightning

Issue:
Wrong order of arguments to loss function in template example for finetuning
Hi,
I observed that the order for the loss function in the example for finetuning is switched. Instead of
`self.loss(y_true, y_logits)` it should be `self.loss(y_logits, y_true)`

Below is the exact location of the error:
https://github.com/PyTorchLightning/pytorch-lightning/blob/bd49b07fbba09b1e7d8851ee5a1ffce3d5925e9e/pl_examples/domain_templates/computer_vision_fine_tuning.py#L246


Discussion:
Hi! thanks for your contribution!, great first issue!
It's correct I think.
https://github.com/PyTorchLightning/pytorch-lightning/blob/bd49b07fbba09b1e7d8851ee5a1ffce3d5925e9e/pl_examples/domain_templates/computer_vision_fine_tuning.py#L206-L207

https://github.com/PyTorchLightning/pytorch-lightning/blob/bd49b07fbba09b1e7d8851ee5a1ffce3d5925e9e/pl_examples/domain_templates/computer_vision_fine_tuning.py#L192
Yes. I was used to seeing it in the (logits, target) order. Maybe we can change the loss to take in (self, logits, labels) in order to be consistent PyTorch conventions.
@ajinkya-deogade mind send a PR?

Resolving patch:
diff --git a/pl_examples/domain_templates/computer_vision_fine_tuning.py b/pl_examples/domain_templates/computer_vision_fine_tuning.py
--- a/pl_examples/domain_templates/computer_vision_fine_tuning.py
+++ b/pl_examples/domain_templates/computer_vision_fine_tuning.py
@@ -203,7 +203,7 @@ def forward(self, x):
 
         return x
 
-    def loss(self, labels, logits):
+    def loss(self, logits, labels):
         return self.loss_func(input=logits, target=labels)
 
     def train(self, mode=True):
@@ -243,7 +243,7 @@ def training_step(self, batch, batch_idx):
         y_bin = torch.ge(y_logits, 0)
 
         # 2. Compute loss & accuracy:
-        train_loss = self.loss(y_true, y_logits)
+        train_loss = self.loss(y_logits, y_true)
         num_correct = torch.eq(y_bin.view(-1), y_true.view(-1)).sum()
 
         # 3. Outputs:
@@ -276,7 +276,7 @@ def validation_step(self, batch, batch_idx):
         y_bin = torch.ge(y_logits, 0)
 
         # 2. Compute loss & accuracy:
-        val_loss = self.loss(y_true, y_logits)
+        val_loss = self.loss(y_logits, y_true)
         num_correct = torch.eq(y_bin.view(-1), y_true.view(-1)).sum()
 
         return {'val_loss': val_loss,


Test patch:
Repository: Lightning-AI/pytorch-lightning

Issue:
Global Gradient calculation is turned off during validation step.
If an error occurs during the validation step, the tradition calculation is turned off for the runtime, you have to either specifically enable it or restart runtime!



Discussion:
@Nilanshrajput is it a question?
@Borda, no it's a bug!
could you please describe more what is happening or give an example?
I used the  MNIST example for demonstrating the error. https://colab.research.google.com/drive/1LWlUurpIu_pD4fP_cUgwhF5f0nS6fvb4?usp=sharing 
Here in validation step I added `assert 1==2` which will cause the error during execution, to remove the error you can comment that line, but this error will pop-up `element 0 of tensors does not require grad and does not have a grad_fn` . This error is generated because gradient calculation was turned off during the validation step globally, and due to error while executing and never reverted.
`# disable gradients to save memory
        torch.set_grad_enabled(False)
`
This line in trainer.evaluation_loop.py sets global grad calculation False, this is reverted at the end of evaluation loop but that's is never executed if there is an error raised in between, and the model remain in eval mode with gradient calculation disabled. 
@Borda This could be resolved in two ways i think:
1. use local context for disabling grad ` with torch.no_grad():`. but here also if error occurs the model will remain in eval mode, also for a long piece of code it might not look good.
2. At the start of training_loop add following lines `model.train()
        torch.set_grad_enabled(True)`. This is better method I think.
I can add a PR if you agree.
> 2. At the start of training_loop add following lines `model.train() torch.set_grad_enabled(True)`. This is better method I think.
>    I can add a PR if you agree.

Yeah, sending a PR would be great...
cc: @awaelchli 

Resolving patch:
diff --git a/pytorch_lightning/trainer/training_loop.py b/pytorch_lightning/trainer/training_loop.py
--- a/pytorch_lightning/trainer/training_loop.py
+++ b/pytorch_lightning/trainer/training_loop.py
@@ -319,6 +319,12 @@ def train(self):
         # get model
         model = self.get_model()
 
+        # enable train mode
+        model.train()
+
+        # enable gradients
+        torch.set_grad_enabled(True)
+
         # load data
         # if reload_dataloaders_every_epoch, this is moved to the epoch loop
         if not self.reload_dataloaders_every_epoch:


Test patch:
Repository: Lightning-AI/pytorch-lightning

Issue:
Model.load_from_checkpoint tries to open file path as URL and fail
## 🐛 Bug
 load_from_checkpoint tries to classify if the input is a file path or an URL and detects the hard drive letter as a scheme and then classify wrongly the input because of this.
urllib.error.URLError: \<urlopen error unknown url type: d\>

My input:
D:\Prog\Projects\AceriNet\research_seed\checkpoints\acerinet\bnacerinet0_target=OVA_OK_penalized=None_loss_fn=ce_normalized=True_balanced=FalseFalse_seed=42_val_loss=0.374_val_auroc=0.9041_v0.ckpt

### To Reproduce

Steps to reproduce the behavior:
Use any path with a hard drive letter at the start (windows formatting) for pl.LightningModule().load_from_checkpoint(path)



Discussion:
Hi! thanks for your contribution!, great first issue!

Resolving patch:
diff --git a/pytorch_lightning/utilities/cloud_io.py b/pytorch_lightning/utilities/cloud_io.py
--- a/pytorch_lightning/utilities/cloud_io.py
+++ b/pytorch_lightning/utilities/cloud_io.py
@@ -1,11 +1,12 @@
 import torch
 
+from pathlib import Path
 from urllib.parse import urlparse
 
 
 def load(path_or_url: str, map_location=None):
     parsed = urlparse(path_or_url)
-    if parsed.scheme == '':
-        # local file
+    if parsed.scheme == '' or Path(path_or_url).is_file():
+        # no scheme or local file
         return torch.load(path_or_url, map_location=map_location)
     return torch.hub.load_state_dict_from_url(path_or_url, map_location=map_location)


Test patch:
diff --git a/tests/trainer/test_trainer.py b/tests/trainer/test_trainer.py
--- a/tests/trainer/test_trainer.py
+++ b/tests/trainer/test_trainer.py
@@ -582,7 +582,7 @@ def load_from_checkpoint(cls, checkpoint_path, *args, **kwargs):
             with pytest.raises(FileNotFoundError):
                 trainer.test(ckpt_path='random.ckpt')
         else:
-            ckpt_path = str(list((Path(tmpdir) / 'lightning_logs/version_0/checkpoints').iterdir())[0])
+            ckpt_path = str(list((Path(tmpdir) / 'lightning_logs/version_0/checkpoints').iterdir())[0].absolute())
             trainer.test(ckpt_path=ckpt_path)
             assert loaded_checkpoint_path == ckpt_path
Repository: Lightning-AI/pytorch-lightning

Issue:
model.setup and model.on_fit_start not working
self.model not yet set so `self.is_function_implemented('on_fit_start')` and `self.is_function_implemented('setup')` in `trainer.fit` will both always return false.

Resolve by checking if model instead of self has the relevant function, e.g.
`if callable(getattr(model, f_name, None)): model.f_name()`

relevant lines:
https://github.com/PyTorchLightning/pytorch-lightning/blob/f8c10fbff03240bf76896bc78def2118ad4fe141/pytorch_lightning/trainer/trainer.py#L851
https://github.com/PyTorchLightning/pytorch-lightning/blob/f8c10fbff03240bf76896bc78def2118ad4fe141/pytorch_lightning/trainer/trainer.py#L855


Discussion:

Resolving patch:
diff --git a/pytorch_lightning/trainer/model_hooks.py b/pytorch_lightning/trainer/model_hooks.py
--- a/pytorch_lightning/trainer/model_hooks.py
+++ b/pytorch_lightning/trainer/model_hooks.py
@@ -6,8 +6,9 @@
 
 class TrainerModelHooksMixin(ABC):
 
-    def is_function_implemented(self, f_name):
-        model = self.get_model()
+    def is_function_implemented(self, f_name, model=None):
+        if model is None:
+            model = self.get_model()
         f_op = getattr(model, f_name, None)
         return callable(f_op)
 
diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py
--- a/pytorch_lightning/trainer/trainer.py
+++ b/pytorch_lightning/trainer/trainer.py
@@ -848,7 +848,7 @@ def fit(
 
         # callbacks
         self.on_fit_start()
-        if self.is_function_implemented('on_fit_start'):
+        if self.is_function_implemented('on_fit_start', model):
             model.on_fit_start()
 
         # on multi-gpu jobs we only want to manipulate (download, etc) on node_rank=0, local_rank=0
@@ -860,7 +860,7 @@ def fit(
         self.barrier('fit_prepare_data')
 
         self.setup('fit')
-        if self.is_function_implemented('setup'):
+        if self.is_function_implemented('setup', model):
             model.setup('fit')
 
         # Run auto batch size scaling
@@ -1149,8 +1149,8 @@ def test(
             trainer.test(model, test_dataloaders=test)
         """
         self.setup('test')
-        if self.is_function_implemented('setup'):
-            model_ref = self.model if model is None else model
+        model_ref = self.model if model is None else model
+        if self.is_function_implemented('setup', model_ref):
             model_ref.setup('test')
 
         self.barrier('test_setup')


Test patch:
Repository: Lightning-AI/pytorch-lightning

Issue:
Checkpointing with SLURM
#### What is your question?
I have a pytorch-lightning code with checkpointing that runs well on my desktop. But when I run it on our cluster with SLURM, the checkpoints do not get saved.

#### Code
```python
    model = Predictor(args)
    check = ModelCheckpoint(save_top_k=1, verbose=True, monitor='val_acc', mode='max',
            filepath='checks/{epoch})
    trainer = pl.Trainer(checkpoint_callback=check, max_epochs=100, gpus=1)
    trainer.fit(model)
```
#### What have you tried?
I run it in the cluster with the following code:
```bash
salloc -G 1 srun python main.py
```
#### What's your environment?

 - OS: Linux
 - Packaging conda



Discussion:
Hi! thanks for your contribution!, great first issue!
is this with 0.8.1?
It was with 0.8.0. I upgraded to 0.8.1 and the problem persists.

Also, my manual checkpointing with `torch.save` does work.
Could be related to issue with https://github.com/PyTorchLightning/pytorch-lightning/pull/2231 experienced the same problems in my setup.
My hypothesis is that something is going wrong with determining the rank of the process when running on slurm, thus the logger calls are never executed and some outputs for example
```
GPU available: True, used: True
TPU available: False, using: 0 TPU cores
CUDA_VISIBLE_DEVICES: [0]
```
Are never shown, otherwise training executes as regulalry.

This could be due to an incorrect (non-zero) value in `rank_zero_only.rank` such that the wrapped commands are never executed.
You might want to try after this #1504 is merged. 
Can you check to see if the weights are being saved under `trainer.weights_save_path`?
https://github.com/PyTorchLightning/pytorch-lightning/blob/f278ac42c81fc344fe1cb673877b8a9dfca9c9b5/pytorch_lightning/trainer/training_io.py#L224

My guess is that they're being saved but for some reason `hpc_save` doesn't save to the normal checkpoint directory.

Resolving patch:
diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py
--- a/pytorch_lightning/trainer/trainer.py
+++ b/pytorch_lightning/trainer/trainer.py
@@ -327,8 +327,8 @@ def __init__(
         # this way we only show it on rank 0
         if 'LOCAL_RANK' in os.environ:
             rank_zero_only.rank = os.environ['LOCAL_RANK']
-        if 'SLURM_JOB_ID' in os.environ:
-            rank_zero_only.rank = os.environ['SLURM_JOB_ID']
+        if 'SLURM_LOCALID' in os.environ:
+            rank_zero_only.rank = os.environ['SLURM_LOCALID']
 
         # Init callbacks
         self.prepare_data_per_node = prepare_data_per_node


Test patch:
Repository: Lightning-AI/pytorch-lightning

Issue:
AttributeError: 'LightningDataParallel' object has no attribute 'teardown'
<!-- 
### Common bugs:
1. Tensorboard not showing in Jupyter-notebook see [issue 79](https://github.com/PyTorchLightning/pytorch-lightning/issues/79).    
2. PyTorch 1.1.0 vs 1.2.0 support [see FAQ](https://github.com/PyTorchLightning/pytorch-lightning#faq)    
-->

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

### To Reproduce

Steps to reproduce the behavior:

    trainer = pytorch_lightning.Trainer(
        gpus=2,
        distributed_backend='dp'
    )
    model = BaseModel.load_from_checkpoint(...)
    trainer.test(model)


Traceback (most recent call last):
  File "run_kitti.py", line 351, in <module>
    trainer.test(model)
  File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 1198, in test
    self.model.teardown('test')
  File "/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py", line 594, in __getattr__
    type(self).__name__, name))
AttributeError: 'LightningDataParallel' object has no attribute 'teardown'
<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->


#### Code sample
<!-- Ideally attach a minimal code sample to reproduce the decried issue. 
Minimal means having the shortest code but still preserving the bug. -->

### Expected behavior

<!-- A clear and concise description of what you expected to happen. -->

### Environment


* CUDA:
	- GPU:
		- GeForce GTX 1080 Ti
		- GeForce GTX 1080 Ti
	- available:         True
	- version:           10.1
* Packages:
	- numpy:             1.18.1
	- pyTorch_debug:     False
	- pyTorch_version:   1.5.1
	- pytorch-lightning: 0.8.1
	- tensorboard:       2.2.2
	- tqdm:              4.46.0
* System:
	- OS:                Linux
	- architecture:
		- 64bit
		- 
	- processor:         x86_64
	- python:            3.7.7
	- version:           #53~18.04.1-Ubuntu SMP Thu Jun 4 14:58:26 UTC 2020


### Additional context

<!-- Add any other context about the problem here. -->

If I'm not missing something, this AttributeError is a bug on your side.


Discussion:
Hi! thanks for your contribution!, great first issue!
+1 on this issue.
Also confirm this issue.

Resolving patch:
diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py
--- a/pytorch_lightning/trainer/trainer.py
+++ b/pytorch_lightning/trainer/trainer.py
@@ -1193,7 +1193,8 @@ def test(
 
         self.teardown('test')
         if self.is_function_implemented('teardown'):
-            self.model.teardown('test')
+            model_ref = self.get_model()
+            model_ref.teardown('test')
 
     def check_model_configuration(self, model: LightningModule):
         r"""


Test patch:
Repository: Lightning-AI/pytorch-lightning

Issue:
name 'IProgress' is not defined
name 'IProgress' is not defined when running from Jupyter notebook.


Discussion:
Did you try to install ipywidgets?

https://ipywidgets.readthedocs.io/en/latest/user_install.html
should we try catch this? and change usage + warn user when this happens?
Isn't it only tqdm.auto which uses ipywidgets to display a progress bar? That we only use here:
https://github.com/PyTorchLightning/pytorch-lightning/search?q=tqdm.auto&unscoped_q=tqdm.auto

Maybe it should be changed in tqdm.auto to not take IProgess if ipywidgets is not installed. There are already some checks:
https://github.com/tqdm/tqdm/blob/master/tqdm/autonotebook.py

But I had a similar problem when using jupyter lab and I had to install nodejs for it (which I wanted to avoid) but found no way of disable tqdm.auto taking IProgress. In my case it didn't crashed but no progress bar were displayed. If someone knows a way of overwriting the tqdm.auto selection let me know. Otherwise I think the checks should be better or there should be a way of overwriting the tqdm.auto selection. (In my opinion)
I am doing:
`from tqdm import tqdm`
and it seems to work.
In addition I had to install `ipywidgets`.

Perhaps pytorch-lightning should depend on ipywidgets? It seems like jupyter is a pretty common use case for people.
I do not think we want to add `ipywidgets` as dependency bu we can do similar check on our side too
@olineumann mind send PR?

Resolving patch:
diff --git a/pytorch_lightning/callbacks/progress.py b/pytorch_lightning/callbacks/progress.py
--- a/pytorch_lightning/callbacks/progress.py
+++ b/pytorch_lightning/callbacks/progress.py
@@ -5,9 +5,16 @@
 Use or override one of the progress bar callbacks.
 
 """
+import importlib
 import sys
 
-from tqdm.auto import tqdm
+
+# check if ipywidgets is installed before importing tqdm.auto
+# to ensure it won't fail and a progress bar is displayed
+if importlib.util.find_spec('ipywidgets') is not None:
+    from tqdm.auto import tqdm
+else:
+    from tqdm import tqdm
 
 from pytorch_lightning.callbacks import Callback
 
diff --git a/pytorch_lightning/trainer/lr_finder.py b/pytorch_lightning/trainer/lr_finder.py
--- a/pytorch_lightning/trainer/lr_finder.py
+++ b/pytorch_lightning/trainer/lr_finder.py
@@ -1,6 +1,8 @@
 """
 Trainer Learning Rate Finder
 """
+import os
+import importlib
 from abc import ABC, abstractmethod
 from typing import Optional, Sequence, Tuple, List, Union
 
@@ -8,8 +10,14 @@
 import torch
 from torch.optim.lr_scheduler import _LRScheduler
 from torch.utils.data import DataLoader
-from tqdm.auto import tqdm
-import os
+
+# check if ipywidgets is installed before importing tqdm.auto
+# to ensure it won't fail and a progress bar is displayed
+if importlib.util.find_spec('ipywidgets') is not None:
+    from tqdm.auto import tqdm
+else:
+    from tqdm import tqdm
+
 
 from pytorch_lightning.core.lightning import LightningModule
 from pytorch_lightning.callbacks import Callback


Test patch:
Repository: Lightning-AI/pytorch-lightning

Issue:
Wandb Flatten Dict



Wandb logger should flatten the dictionary of parameters before logging. Every other logger has the bellow pattern of code:
```
 params = self._convert_params(params)
 params = self._flatten_dict(params)
```

## 🐛 Bug

Wandb logger does not flatten parameters resulting in dictionaries being logged to Wandb, which are not searchable causing for some loss of features in wandb. 
### To Reproduce

Run the cpu_template with wandb logger, and log a nested dictionary. 


### Expected behavior

Solution, just call  ` params = self._flatten_dict(params)` this in the wandb logger. 
### Environment
* CUDA:
	- GPU:
	- available:         False
	- version:           None
* Packages:
	- numpy:             1.18.5
	- pyTorch_debug:     False
	- pyTorch_version:   1.5.0
	- pytorch-lightning: 0.8.4
	- tensorboard:       2.2.2
	- tqdm:              4.46.1
* System:
	- OS:                Darwin
	- architecture:
		- 64bit
		-
	- processor:         i386
	- python:            3.7.7
	- version:           Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64



Discussion:

Resolving patch:
diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py
--- a/pytorch_lightning/loggers/wandb.py
+++ b/pytorch_lightning/loggers/wandb.py
@@ -125,6 +125,7 @@ def watch(self, model: nn.Module, log: str = 'gradients', log_freq: int = 100):
     @rank_zero_only
     def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
         params = self._convert_params(params)
+        params = self._flatten_dict(params)
         self.experiment.config.update(params, allow_val_change=True)
 
     @rank_zero_only


Test patch:
diff --git a/tests/loggers/test_wandb.py b/tests/loggers/test_wandb.py
--- a/tests/loggers/test_wandb.py
+++ b/tests/loggers/test_wandb.py
@@ -19,9 +19,12 @@ def test_wandb_logger(wandb):
     logger.log_metrics({'acc': 1.0}, step=3)
     wandb.init().log.assert_called_once_with({'global_step': 3, 'acc': 1.0})
 
-    logger.log_hyperparams({'test': None})
-    wandb.init().config.update.assert_called_once_with({'test': None}, allow_val_change=True)
-
+    logger.log_hyperparams({'test': None, 'nested': {'a': 1}, 'b': [2, 3, 4]})
+    wandb.init().config.update.assert_called_once_with(
+        {'test': 'None', 'nested/a': 1, 'b': [2, 3, 4]},
+        allow_val_change=True,
+    )
+    
     logger.watch('model', 'log', 10)
     wandb.init().watch.assert_called_once_with('model', log='log', log_freq=10)