prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  is corpus object reusable across ModelTrainer instances ?
  I have three checkpoint files generated from a training run that uses PooledFlair embedding. Say chk10.pt, chk20.pt, chk30.pt.
  
  I finalize using the following code in a for loop to get the F1 predictions out:
  
  trainer: ModelTrainer = ModelTrainer.load_checkpoint(chkfile, corpus)
  trainer.train('.',  checkpoint = False, train_with_dev=True, max_epochs=epochs)
  
  I set the epochs to the value at which this checkpoint got generated. So 10, 20, 30 etc. So typically it goes straight to creating the final model and emitting the predictions.
  
  This works perfectly fine for the first time in the loop, after which the predictions are quite wrong. Now instead of doing it in the loop, if i simply do just once by restarting the process i get the values i expect. This behavior happens only with PooledFlairEmbedding. Same program runs just fine with ElmoEmbedding, BertEmbedding.
  
  So my question is why is this the case ? Is it because i create the corpus object outside the for loop and keep reusing it across different ModelTrainer instances ? 
  
  It happens quite regularly for me. If needed i can make a small program and share.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `flair/nn.py`
  Content:
  ```
  1 import warnings
  2 from pathlib import Path
  3 
  4 import torch.nn
  5 
  6 from abc import abstractmethod
  7 
  8 from typing import Union, List
  9 
  10 from torch.utils.data.dataset import Dataset
  11 
  12 import flair
  13 from flair import file_utils
  14 from flair.data import DataPoint, Sentence
  15 from flair.datasets import DataLoader
  16 from flair.training_utils import Result
  17 
  18 
  19 class Model(torch.nn.Module):
  20     """Abstract base class for all downstream task models in Flair, such as SequenceTagger and TextClassifier.
  21     Every new type of model must implement these methods."""
  22 
  23     @abstractmethod
  24     def forward_loss(
  25         self, data_points: Union[List[DataPoint], DataPoint]
  26     ) -> torch.tensor:
  27         """Performs a forward pass and returns a loss tensor for backpropagation. Implement this to enable training."""
  28         pass
  29 
  30     @abstractmethod
  31     def evaluate(
  32         self,
  33         sentences: Union[List[DataPoint], Dataset],
  34         out_path: Path = None,
  35         embedding_storage_mode: str = "none",
  36     ) -> (Result, float):
  37         """Evaluates the model. Returns a Result object containing evaluation
  38         results and a loss value. Implement this to enable evaluation.
  39         :param data_loader: DataLoader that iterates over dataset to be evaluated
  40         :param out_path: Optional output path to store predictions
  41         :param embedding_storage_mode: One of 'none', 'cpu' or 'gpu'. 'none' means all embeddings are deleted and
  42         freshly recomputed, 'cpu' means all embeddings are stored on CPU, or 'gpu' means all embeddings are stored on GPU
  43         :return: Returns a Tuple consisting of a Result object and a loss float value
  44         """
  45         pass
  46 
  47     @abstractmethod
  48     def _get_state_dict(self):
  49         """Returns the state dictionary for this model. Implementing this enables the save() and save_checkpoint()
  50         functionality."""
  51         pass
  52 
  53     @staticmethod
  54     @abstractmethod
  55     def _init_model_with_state_dict(state):
  56         """Initialize the model from a state dictionary. Implementing this enables the load() and load_checkpoint()
  57         functionality."""
  58         pass
  59 
  60     @staticmethod
  61     @abstractmethod
  62     def _fetch_model(model_name) -> str:
  63         return model_name
  64 
  65     def save(self, model_file: Union[str, Path]):
  66         """
  67         Saves the current model to the provided file.
  68         :param model_file: the model file
  69         """
  70         model_state = self._get_state_dict()
  71 
  72         torch.save(model_state, str(model_file), pickle_protocol=4)
  73 
  74     @classmethod
  75     def load(cls, model: Union[str, Path]):
  76         """
  77         Loads the model from the given file.
  78         :param model: the model file
  79         :return: the loaded text classifier model
  80         """
  81         model_file = cls._fetch_model(str(model))
  82 
  83         with warnings.catch_warnings():
  84             warnings.filterwarnings("ignore")
  85             # load_big_file is a workaround by https://github.com/highway11git to load models on some Mac/Windows setups
  86             # see https://github.com/zalandoresearch/flair/issues/351
  87             f = file_utils.load_big_file(str(model_file))
  88             state = torch.load(f, map_location=flair.device)
  89 
  90         model = cls._init_model_with_state_dict(state)
  91 
  92         model.eval()
  93         model.to(flair.device)
  94 
  95         return model
  96 
  97 
  98 class LockedDropout(torch.nn.Module):
  99     """
  100     Implementation of locked (or variational) dropout. Randomly drops out entire parameters in embedding space.
  101     """
  102 
  103     def __init__(self, dropout_rate=0.5, batch_first=True, inplace=False):
  104         super(LockedDropout, self).__init__()
  105         self.dropout_rate = dropout_rate
  106         self.batch_first = batch_first
  107         self.inplace = inplace
  108 
  109     def forward(self, x):
  110         if not self.training or not self.dropout_rate:
  111             return x
  112 
  113         if not self.batch_first:
  114             m = x.data.new(1, x.size(1), x.size(2)).bernoulli_(1 - self.dropout_rate)
  115         else:
  116             m = x.data.new(x.size(0), 1, x.size(2)).bernoulli_(1 - self.dropout_rate)
  117 
  118         mask = torch.autograd.Variable(m, requires_grad=False) / (1 - self.dropout_rate)
  119         mask = mask.expand_as(x)
  120         return mask * x
  121 
  122     def extra_repr(self):
  123         inplace_str = ", inplace" if self.inplace else ""
  124         return "p={}{}".format(self.dropout_rate, inplace_str)
  125 
  126 
  127 class WordDropout(torch.nn.Module):
  128     """
  129     Implementation of word dropout. Randomly drops out entire words (or characters) in embedding space.
  130     """
  131 
  132     def __init__(self, dropout_rate=0.05, inplace=False):
  133         super(WordDropout, self).__init__()
  134         self.dropout_rate = dropout_rate
  135         self.inplace = inplace
  136 
  137     def forward(self, x):
  138         if not self.training or not self.dropout_rate:
  139             return x
  140 
  141         m = x.data.new(x.size(0), x.size(1), 1).bernoulli_(1 - self.dropout_rate)
  142 
  143         mask = torch.autograd.Variable(m, requires_grad=False)
  144         return mask * x
  145 
  146     def extra_repr(self):
  147         inplace_str = ", inplace" if self.inplace else ""
  148         return "p={}{}".format(self.dropout_rate, inplace_str)
  149
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/flair/nn.py b/flair/nn.py
  --- a/flair/nn.py
  +++ b/flair/nn.py
  @@ -85,7 +85,7 @@
               # load_big_file is a workaround by https://github.com/highway11git to load models on some Mac/Windows setups
               # see https://github.com/zalandoresearch/flair/issues/351
               f = file_utils.load_big_file(str(model_file))
  -            state = torch.load(f, map_location=flair.device)
  +            state = torch.load(f, map_location='cpu')
   
           model = cls._init_model_with_state_dict(state)
  
verification_info: {"golden_diff": "diff --git a/flair/nn.py b/flair/nn.py\n--- a/flair/nn.py\n+++ b/flair/nn.py\n@@ -85,7 +85,7 @@\n             # load_big_file is a workaround by https://github.com/highway11git to load models on some Mac/Windows setups\n             # see https://github.com/zalandoresearch/flair/issues/351\n             f = file_utils.load_big_file(str(model_file))\n-            state = torch.load(f, map_location=flair.device)\n+            state = torch.load(f, map_location='cpu')\n \n         model = cls._init_model_with_state_dict(state)\n", "issue": "is corpus object reusable across ModelTrainer instances ?\nI have three checkpoint files generated from a training run that uses PooledFlair embedding. Say chk10.pt, chk20.pt, chk30.pt.\r\n\r\nI finalize using the following code in a for loop to get the F1 predictions out:\r\n\r\ntrainer: ModelTrainer = ModelTrainer.load_checkpoint(chkfile, corpus)\r\ntrainer.train('.',  checkpoint = False, train_with_dev=True, max_epochs=epochs)\r\n\r\nI set the epochs to the value at which this checkpoint got generated. So 10, 20, 30 etc. So typically it goes straight to creating the final model and emitting the predictions.\r\n\r\nThis works perfectly fine for the first time in the loop, after which the predictions are quite wrong. Now instead of doing it in the loop, if i simply do just once by restarting the process i get the values i expect. This behavior happens only with PooledFlairEmbedding. Same program runs just fine with ElmoEmbedding, BertEmbedding.\r\n\r\nSo my question is why is this the case ? Is it because i create the corpus object outside the for loop and keep reusing it across different ModelTrainer instances ? \r\n\r\nIt happens quite regularly for me. If needed i can make a small program and share.\r\n\n", "before_files": [{"content": "import warnings\nfrom pathlib import Path\n\nimport torch.nn\n\nfrom abc import abstractmethod\n\nfrom typing import Union, List\n\nfrom torch.utils.data.dataset import Dataset\n\nimport flair\nfrom flair import file_utils\nfrom flair.data import DataPoint, Sentence\nfrom flair.datasets import DataLoader\nfrom flair.training_utils import Result\n\n\nclass Model(torch.nn.Module):\n    \"\"\"Abstract base class for all downstream task models in Flair, such as SequenceTagger and TextClassifier.\n    Every new type of model must implement these methods.\"\"\"\n\n    @abstractmethod\n    def forward_loss(\n        self, data_points: Union[List[DataPoint], DataPoint]\n    ) -> torch.tensor:\n        \"\"\"Performs a forward pass and returns a loss tensor for backpropagation. Implement this to enable training.\"\"\"\n        pass\n\n    @abstractmethod\n    def evaluate(\n        self,\n        sentences: Union[List[DataPoint], Dataset],\n        out_path: Path = None,\n        embedding_storage_mode: str = \"none\",\n    ) -> (Result, float):\n        \"\"\"Evaluates the model. Returns a Result object containing evaluation\n        results and a loss value. Implement this to enable evaluation.\n        :param data_loader: DataLoader that iterates over dataset to be evaluated\n        :param out_path: Optional output path to store predictions\n        :param embedding_storage_mode: One of 'none', 'cpu' or 'gpu'. 'none' means all embeddings are deleted and\n        freshly recomputed, 'cpu' means all embeddings are stored on CPU, or 'gpu' means all embeddings are stored on GPU\n        :return: Returns a Tuple consisting of a Result object and a loss float value\n        \"\"\"\n        pass\n\n    @abstractmethod\n    def _get_state_dict(self):\n        \"\"\"Returns the state dictionary for this model. Implementing this enables the save() and save_checkpoint()\n        functionality.\"\"\"\n        pass\n\n    @staticmethod\n    @abstractmethod\n    def _init_model_with_state_dict(state):\n        \"\"\"Initialize the model from a state dictionary. Implementing this enables the load() and load_checkpoint()\n        functionality.\"\"\"\n        pass\n\n    @staticmethod\n    @abstractmethod\n    def _fetch_model(model_name) -> str:\n        return model_name\n\n    def save(self, model_file: Union[str, Path]):\n        \"\"\"\n        Saves the current model to the provided file.\n        :param model_file: the model file\n        \"\"\"\n        model_state = self._get_state_dict()\n\n        torch.save(model_state, str(model_file), pickle_protocol=4)\n\n    @classmethod\n    def load(cls, model: Union[str, Path]):\n        \"\"\"\n        Loads the model from the given file.\n        :param model: the model file\n        :return: the loaded text classifier model\n        \"\"\"\n        model_file = cls._fetch_model(str(model))\n\n        with warnings.catch_warnings():\n            warnings.filterwarnings(\"ignore\")\n            # load_big_file is a workaround by https://github.com/highway11git to load models on some Mac/Windows setups\n            # see https://github.com/zalandoresearch/flair/issues/351\n            f = file_utils.load_big_file(str(model_file))\n            state = torch.load(f, map_location=flair.device)\n\n        model = cls._init_model_with_state_dict(state)\n\n        model.eval()\n        model.to(flair.device)\n\n        return model\n\n\nclass LockedDropout(torch.nn.Module):\n    \"\"\"\n    Implementation of locked (or variational) dropout. Randomly drops out entire parameters in embedding space.\n    \"\"\"\n\n    def __init__(self, dropout_rate=0.5, batch_first=True, inplace=False):\n        super(LockedDropout, self).__init__()\n        self.dropout_rate = dropout_rate\n        self.batch_first = batch_first\n        self.inplace = inplace\n\n    def forward(self, x):\n        if not self.training or not self.dropout_rate:\n            return x\n\n        if not self.batch_first:\n            m = x.data.new(1, x.size(1), x.size(2)).bernoulli_(1 - self.dropout_rate)\n        else:\n            m = x.data.new(x.size(0), 1, x.size(2)).bernoulli_(1 - self.dropout_rate)\n\n        mask = torch.autograd.Variable(m, requires_grad=False) / (1 - self.dropout_rate)\n        mask = mask.expand_as(x)\n        return mask * x\n\n    def extra_repr(self):\n        inplace_str = \", inplace\" if self.inplace else \"\"\n        return \"p={}{}\".format(self.dropout_rate, inplace_str)\n\n\nclass WordDropout(torch.nn.Module):\n    \"\"\"\n    Implementation of word dropout. Randomly drops out entire words (or characters) in embedding space.\n    \"\"\"\n\n    def __init__(self, dropout_rate=0.05, inplace=False):\n        super(WordDropout, self).__init__()\n        self.dropout_rate = dropout_rate\n        self.inplace = inplace\n\n    def forward(self, x):\n        if not self.training or not self.dropout_rate:\n            return x\n\n        m = x.data.new(x.size(0), x.size(1), 1).bernoulli_(1 - self.dropout_rate)\n\n        mask = torch.autograd.Variable(m, requires_grad=False)\n        return mask * x\n\n    def extra_repr(self):\n        inplace_str = \", inplace\" if self.inplace else \"\"\n        return \"p={}{}\".format(self.dropout_rate, inplace_str)\n", "path": "flair/nn.py"}], "after_files": [{"content": "import warnings\nfrom pathlib import Path\n\nimport torch.nn\n\nfrom abc import abstractmethod\n\nfrom typing import Union, List\n\nfrom torch.utils.data.dataset import Dataset\n\nimport flair\nfrom flair import file_utils\nfrom flair.data import DataPoint, Sentence\nfrom flair.datasets import DataLoader\nfrom flair.training_utils import Result\n\n\nclass Model(torch.nn.Module):\n    \"\"\"Abstract base class for all downstream task models in Flair, such as SequenceTagger and TextClassifier.\n    Every new type of model must implement these methods.\"\"\"\n\n    @abstractmethod\n    def forward_loss(\n        self, data_points: Union[List[DataPoint], DataPoint]\n    ) -> torch.tensor:\n        \"\"\"Performs a forward pass and returns a loss tensor for backpropagation. Implement this to enable training.\"\"\"\n        pass\n\n    @abstractmethod\n    def evaluate(\n        self,\n        sentences: Union[List[DataPoint], Dataset],\n        out_path: Path = None,\n        embedding_storage_mode: str = \"none\",\n    ) -> (Result, float):\n        \"\"\"Evaluates the model. Returns a Result object containing evaluation\n        results and a loss value. Implement this to enable evaluation.\n        :param data_loader: DataLoader that iterates over dataset to be evaluated\n        :param out_path: Optional output path to store predictions\n        :param embedding_storage_mode: One of 'none', 'cpu' or 'gpu'. 'none' means all embeddings are deleted and\n        freshly recomputed, 'cpu' means all embeddings are stored on CPU, or 'gpu' means all embeddings are stored on GPU\n        :return: Returns a Tuple consisting of a Result object and a loss float value\n        \"\"\"\n        pass\n\n    @abstractmethod\n    def _get_state_dict(self):\n        \"\"\"Returns the state dictionary for this model. Implementing this enables the save() and save_checkpoint()\n        functionality.\"\"\"\n        pass\n\n    @staticmethod\n    @abstractmethod\n    def _init_model_with_state_dict(state):\n        \"\"\"Initialize the model from a state dictionary. Implementing this enables the load() and load_checkpoint()\n        functionality.\"\"\"\n        pass\n\n    @staticmethod\n    @abstractmethod\n    def _fetch_model(model_name) -> str:\n        return model_name\n\n    def save(self, model_file: Union[str, Path]):\n        \"\"\"\n        Saves the current model to the provided file.\n        :param model_file: the model file\n        \"\"\"\n        model_state = self._get_state_dict()\n\n        torch.save(model_state, str(model_file), pickle_protocol=4)\n\n    @classmethod\n    def load(cls, model: Union[str, Path]):\n        \"\"\"\n        Loads the model from the given file.\n        :param model: the model file\n        :return: the loaded text classifier model\n        \"\"\"\n        model_file = cls._fetch_model(str(model))\n\n        with warnings.catch_warnings():\n            warnings.filterwarnings(\"ignore\")\n            # load_big_file is a workaround by https://github.com/highway11git to load models on some Mac/Windows setups\n            # see https://github.com/zalandoresearch/flair/issues/351\n            f = file_utils.load_big_file(str(model_file))\n            state = torch.load(f, map_location='cpu')\n\n        model = cls._init_model_with_state_dict(state)\n\n        model.eval()\n        model.to(flair.device)\n\n        return model\n\n\nclass LockedDropout(torch.nn.Module):\n    \"\"\"\n    Implementation of locked (or variational) dropout. Randomly drops out entire parameters in embedding space.\n    \"\"\"\n\n    def __init__(self, dropout_rate=0.5, batch_first=True, inplace=False):\n        super(LockedDropout, self).__init__()\n        self.dropout_rate = dropout_rate\n        self.batch_first = batch_first\n        self.inplace = inplace\n\n    def forward(self, x):\n        if not self.training or not self.dropout_rate:\n            return x\n\n        if not self.batch_first:\n            m = x.data.new(1, x.size(1), x.size(2)).bernoulli_(1 - self.dropout_rate)\n        else:\n            m = x.data.new(x.size(0), 1, x.size(2)).bernoulli_(1 - self.dropout_rate)\n\n        mask = torch.autograd.Variable(m, requires_grad=False) / (1 - self.dropout_rate)\n        mask = mask.expand_as(x)\n        return mask * x\n\n    def extra_repr(self):\n        inplace_str = \", inplace\" if self.inplace else \"\"\n        return \"p={}{}\".format(self.dropout_rate, inplace_str)\n\n\nclass WordDropout(torch.nn.Module):\n    \"\"\"\n    Implementation of word dropout. Randomly drops out entire words (or characters) in embedding space.\n    \"\"\"\n\n    def __init__(self, dropout_rate=0.05, inplace=False):\n        super(WordDropout, self).__init__()\n        self.dropout_rate = dropout_rate\n        self.inplace = inplace\n\n    def forward(self, x):\n        if not self.training or not self.dropout_rate:\n            return x\n\n        m = x.data.new(x.size(0), x.size(1), 1).bernoulli_(1 - self.dropout_rate)\n\n        mask = torch.autograd.Variable(m, requires_grad=False)\n        return mask * x\n\n    def extra_repr(self):\n        inplace_str = \", inplace\" if self.inplace else \"\"\n        return \"p={}{}\".format(self.dropout_rate, inplace_str)\n", "path": "flair/nn.py"}]}