gurudesh/pytracebugs huggingface dataset:
# Buggy code
def training_step(self, batch, batch_idx):
    images, target = batch
    output = self.model(images)
    loss_val = F.cross_entropy(output, target)
    acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))

    # in DP mode (default) make sure if result is scalar, there's another dim in the beginning
    if self.trainer.use_dp or self.trainer.use_ddp2:
        loss_val = loss_val.unsqueeze(0)
        acc1 = acc1.unsqueeze(0)
        acc5 = acc5.unsqueeze(0)

    tqdm_dict = {"train_loss": loss_val}
    output = OrderedDict(
        {
            "loss": loss_val,
            "acc1": acc1,
            "acc5": acc5,
            "progress_bar": tqdm_dict,
            "log": tqdm_dict,
        }
    )

    return output


# Traceback (TypeError)
Traceback (most recent call last):
File "imagenet_example.py", line 248, in <module>
main(get_args())
File "imagenet_example.py", line 229, in main
model = ImageNetLightningModel(hparams)
TypeError: Can't instantiate abstract class ImageNetLightningModel with abstract methods forward

# Fixed code
def training_step(self, batch, batch_idx):
    images, target = batch
    output = self.forward(images)
    loss_val = F.cross_entropy(output, target)
    acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))

    # in DP mode (default) make sure if result is scalar, there's another dim in the beginning
    if self.trainer.use_dp or self.trainer.use_ddp2:
        loss_val = loss_val.unsqueeze(0)
        acc1 = acc1.unsqueeze(0)
        acc5 = acc5.unsqueeze(0)

    tqdm_dict = {"train_loss": loss_val}
    output = OrderedDict(
        {
            "loss": loss_val,
            "acc1": acc1,
            "acc5": acc5,
            "progress_bar": tqdm_dict,
            "log": tqdm_dict,
        }
    )

    return output
# Buggy code
def validation_step(self, batch, batch_idx):
    images, target = batch
    output = self.model(images)
    loss_val = F.cross_entropy(output, target)
    acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))

    # in DP mode (default) make sure if result is scalar, there's another dim in the beginning
    if self.trainer.use_dp or self.trainer.use_ddp2:
        loss_val = loss_val.unsqueeze(0)
        acc1 = acc1.unsqueeze(0)
        acc5 = acc5.unsqueeze(0)

    output = OrderedDict(
        {
            "val_loss": loss_val,
            "val_acc1": acc1,
            "val_acc5": acc5,
        }
    )

    return output


# Traceback (TypeError)
Traceback (most recent call last):
File "imagenet_example.py", line 248, in <module>
main(get_args())
File "imagenet_example.py", line 229, in main
model = ImageNetLightningModel(hparams)
TypeError: Can't instantiate abstract class ImageNetLightningModel with abstract methods forward

# Fixed code
def validation_step(self, batch, batch_idx):
    images, target = batch
    output = self.forward(images)
    loss_val = F.cross_entropy(output, target)
    acc1, acc5 = self.__accuracy(output, target, topk=(1, 5))

    # in DP mode (default) make sure if result is scalar, there's another dim in the beginning
    if self.trainer.use_dp or self.trainer.use_ddp2:
        loss_val = loss_val.unsqueeze(0)
        acc1 = acc1.unsqueeze(0)
        acc5 = acc5.unsqueeze(0)

    output = OrderedDict(
        {
            "val_loss": loss_val,
            "val_acc1": acc1,
            "val_acc5": acc5,
        }
    )

    return output
# Buggy code
def train_dataloader(self):
    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225],
    )

    train_dir = os.path.join(self.hparams.data, "train")
    train_dataset = datasets.ImageFolder(
        train_dir,
        transforms.Compose(
            [
                transforms.RandomResizedCrop(224),
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                normalize,
            ]
        ),
    )

    if self.use_ddp:
        train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
    else:
        train_sampler = None

    train_loader = torch.utils.data.DataLoader(
        dataset=train_dataset,
        batch_size=self.hparams.batch_size,
        shuffle=(train_sampler is None),
        num_workers=0,
        sampler=train_sampler,
    )
    return train_loader


# Traceback (TypeError)
Traceback (most recent call last):
File "imagenet_example.py", line 248, in <module>
main(get_args())
File "imagenet_example.py", line 229, in main
model = ImageNetLightningModel(hparams)
TypeError: Can't instantiate abstract class ImageNetLightningModel with abstract methods forward

# Fixed code
def train_dataloader(self):
    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225],
    )

    train_dir = os.path.join(self.hparams.data_path, "train")
    train_dataset = datasets.ImageFolder(
        train_dir,
        transforms.Compose(
            [
                transforms.RandomResizedCrop(224),
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                normalize,
            ]
        ),
    )

    if self.use_ddp:
        train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
    else:
        train_sampler = None

    train_loader = torch.utils.data.DataLoader(
        dataset=train_dataset,
        batch_size=self.hparams.batch_size,
        shuffle=(train_sampler is None),
        num_workers=0,
        sampler=train_sampler,
    )
    return train_loader
# Buggy code
def val_dataloader(self):
    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225],
    )
    val_dir = os.path.join(self.hparams.data, "val")
    val_loader = torch.utils.data.DataLoader(
        datasets.ImageFolder(
            val_dir,
            transforms.Compose(
                [
                    transforms.Resize(256),
                    transforms.CenterCrop(224),
                    transforms.ToTensor(),
                    normalize,
                ]
            ),
        ),
        batch_size=self.hparams.batch_size,
        shuffle=False,
        num_workers=0,
    )
    return val_loader


# Traceback (TypeError)
Traceback (most recent call last):
File "imagenet_example.py", line 248, in <module>
main(get_args())
File "imagenet_example.py", line 229, in main
model = ImageNetLightningModel(hparams)
TypeError: Can't instantiate abstract class ImageNetLightningModel with abstract methods forward

# Fixed code
def val_dataloader(self):
    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225],
    )
    val_dir = os.path.join(self.hparams.data_path, "val")
    val_loader = torch.utils.data.DataLoader(
        datasets.ImageFolder(
            val_dir,
            transforms.Compose(
                [
                    transforms.Resize(256),
                    transforms.CenterCrop(224),
                    transforms.ToTensor(),
                    normalize,
                ]
            ),
        ),
        batch_size=self.hparams.batch_size,
        shuffle=False,
        num_workers=0,
    )
    return val_loader
# Buggy code
def get_args():
    parent_parser = argparse.ArgumentParser(add_help=False)
    parent_parser.add_argument(
        "--data-path", metavar="DIR", type=str, help="path to dataset"
    )
    parent_parser.add_argument(
        "--save-path", metavar="DIR", default=".", type=str, help="path to save output"
    )
    parent_parser.add_argument("--gpus", type=int, default=1, help="how many gpus")
    parent_parser.add_argument(
        "--distributed-backend",
        type=str,
        default="dp",
        choices=("dp", "ddp", "ddp2"),
        help="supports three options dp, ddp, ddp2",
    )
    parent_parser.add_argument(
        "--use-16bit",
        dest="use-16bit",
        action="store_true",
        help="if true uses 16 bit precision",
    )
    parent_parser.add_argument(
        "-e",
        "--evaluate",
        dest="evaluate",
        action="store_true",
        help="evaluate model on validation set",
    )

    parser = ImageNetLightningModel.add_model_specific_args(parent_parser)
    return parser.parse_args()


# Traceback (TypeError)
Traceback (most recent call last):
File "imagenet_example.py", line 248, in <module>
main(get_args())
File "imagenet_example.py", line 229, in main
model = ImageNetLightningModel(hparams)
TypeError: Can't instantiate abstract class ImageNetLightningModel with abstract methods forward

# Fixed code
def get_args():
    parent_parser = argparse.ArgumentParser(add_help=False)
    parent_parser.add_argument(
        "--data-path", metavar="DIR", type=str, help="path to dataset"
    )
    parent_parser.add_argument(
        "--save-path", metavar="DIR", default=".", type=str, help="path to save output"
    )
    parent_parser.add_argument("--gpus", type=int, default=1, help="how many gpus")
    parent_parser.add_argument(
        "--distributed-backend",
        type=str,
        default="dp",
        choices=("dp", "ddp", "ddp2"),
        help="supports three options dp, ddp, ddp2",
    )
    parent_parser.add_argument(
        "--use-16bit",
        dest="use_16bit",
        action="store_true",
        help="if true uses 16 bit precision",
    )
    parent_parser.add_argument(
        "-e",
        "--evaluate",
        dest="evaluate",
        action="store_true",
        help="evaluate model on validation set",
    )

    parser = ImageNetLightningModel.add_model_specific_args(parent_parser)
    return parser.parse_args()
# Buggy code
def get_memory_profile(mode):
    """
    'all' means return memory for all gpus
    'min_max' means return memory for max and min
    :param mode:
    :return:
    """
    memory_map = get_gpu_memory_map()

    if mode == "min_max":
        min_mem = 1000000
        min_k = None
        max_mem = 0
        max_k = None
        for k, v in memory_map:
            if v > max_mem:
                max_mem = v
                max_k = k
            if v < min_mem:
                min_mem = v
                min_k = k

        memory_map = {min_k: min_mem, max_k: max_mem}

    return memory_map


# Traceback (ValueError)
Traceback (most recent call last):
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 347, in fit
self.single_gpu_train(model)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/dp_mixin.py", line 79, in single_gpu_train
self.run_pretrain_routine(model)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 467, in run_pretrain_routine
self.train()
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/train_loop_mixin.py", line 60, in train
self.run_training_epoch()
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/train_loop_mixin.py", line 126, in run_training_epoch
self.log_metrics(batch_step_metrics, grad_norm_dic)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/logging_mixin.py", line 20, in log_metrics
mem_map = memory.get_memory_profile(self.log_gpu_memory)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/root_module/memory.py", line 205, in get_memory_profile
for k, v in memory_map:
ValueError: too many values to unpack (expected 2)

# Fixed code
def get_memory_profile(mode):
    """
    'all' means return memory for all gpus
    'min_max' means return memory for max and min
    :param mode:
    :return:
    """
    memory_map = get_gpu_memory_map()

    if mode == "min_max":
        min_index, min_memory = min(memory_map.items(), key=lambda item: item[1])
        max_index, max_memory = max(memory_map.items(), key=lambda item: item[1])

        memory_map = {min_index: min_memory, max_index: max_memory}

    return memory_map
# Buggy code
def get_gpu_memory_map():
    """Get the current gpu usage.

    Returns
    -------
    usage: dict
        Keys are device ids as integers.
        Values are memory usage as integers in MB.
    """
    result = subprocess.check_output(
        ["nvidia-smi", "--query-gpu=memory.used", "--format=csv,nounits,noheader"],
        encoding="utf-8",
    )
    # Convert lines into a dictionary
    gpu_memory = [int(x) for x in result.strip().split("\n")]
    gpu_memory_map = {}
    for k, v in zip(range(len(gpu_memory)), gpu_memory):
        k = f"gpu_{k}"
        gpu_memory_map[k] = v
    return gpu_memory_map


# Traceback (ValueError)
Traceback (most recent call last):
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 347, in fit
self.single_gpu_train(model)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/dp_mixin.py", line 79, in single_gpu_train
self.run_pretrain_routine(model)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 467, in run_pretrain_routine
self.train()
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/train_loop_mixin.py", line 60, in train
self.run_training_epoch()
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/train_loop_mixin.py", line 126, in run_training_epoch
self.log_metrics(batch_step_metrics, grad_norm_dic)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/logging_mixin.py", line 20, in log_metrics
mem_map = memory.get_memory_profile(self.log_gpu_memory)
File "/opt/conda/lib/python3.7/site-packages/pytorch_lightning/root_module/memory.py", line 205, in get_memory_profile
for k, v in memory_map:
ValueError: too many values to unpack (expected 2)

# Fixed code
def get_gpu_memory_map():
    """Get the current gpu usage.

    Returns
    -------
    usage: dict
        Keys are device ids as integers.
        Values are memory usage as integers in MB.
    """
    result = subprocess.run(
        [
            "nvidia-smi",
            "--query-gpu=memory.used",
            "--format=csv,nounits,noheader",
        ],
        encoding="utf-8",
        capture_output=True,
        check=True,
    )
    # Convert lines into a dictionary
    gpu_memory = [int(x) for x in result.stdout.strip().split(os.linesep)]
    gpu_memory_map = {f"gpu_{index}": memory for index, memory in enumerate(gpu_memory)}
    return gpu_memory_map
# Buggy code
def data_loader(fn):
    """
    Decorator to make any fx with this use the lazy property
    :param fn:
    :return:
    """

    attr_name = "_lazy_" + fn.__name__

    @property
    def _data_loader(self):
        if not hasattr(self, attr_name):
            setattr(self, attr_name, fn(self))
        return getattr(self, attr_name)

    return _data_loader


# Traceback (AttributeError)
Traceback (most recent call last):
File "C:/minimal.py", line 8, in <module>
Foo().bar
File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 591, in __getattr__
type(self).__name__, name))
AttributeError: 'Foo' object has no attribute 'bar'

# Fixed code
def data_loader(fn):
    """
    Decorator to make any fx with this use the lazy property
    :param fn:
    :return:
    """

    attr_name = "_lazy_" + fn.__name__

    @property
    def _data_loader(self):
        try:
            value = getattr(self, attr_name)
        except AttributeError:
            try:
                value = fn(self)  # Lazy evaluation, done only once.
            except AttributeError as e:
                # Guard against AttributeError suppression. (Issue #142)
                raise RuntimeError(
                    "An AttributeError was encountered: " + str(e)
                ) from e
            setattr(self, attr_name, value)  # Memoize evaluation.
        return value

    return _data_loader
# Buggy code
def _data_loader(self):
    if not hasattr(self, attr_name):
        setattr(self, attr_name, fn(self))
    return getattr(self, attr_name)


# Traceback (AttributeError)
Traceback (most recent call last):
File "C:/minimal.py", line 8, in <module>
Foo().bar
File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 591, in __getattr__
type(self).__name__, name))
AttributeError: 'Foo' object has no attribute 'bar'

# Fixed code
def _data_loader(self):
    try:
        value = getattr(self, attr_name)
    except AttributeError:
        try:
            value = fn(self)  # Lazy evaluation, done only once.
        except AttributeError as e:
            # Guard against AttributeError suppression. (Issue #142)
            raise RuntimeError("An AttributeError was encountered: " + str(e)) from e
        setattr(self, attr_name, value)  # Memoize evaluation.
    return value