prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  valid_batch_size
  In the train_single.py file , lines 120 to 124
  ```
  def train_iter_fct(): return build_dataset_iter(
          lazily_load_dataset("train", opt), fields, opt)
  
      def valid_iter_fct(): return build_dataset_iter(
          lazily_load_dataset("valid", opt), fields)
  ```
  should be changed
  ```
  def train_iter_fct(): return build_dataset_iter(
          lazily_load_dataset("train", opt), fields, opt)
  
      def valid_iter_fct(): return build_dataset_iter(
          lazily_load_dataset("valid", opt), fields, opt, is_train=False)
  ```
  If it doesn't, it will not use `valid_batch_size`.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `onmt/train_single.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 """
  3     Training on a single process
  4 """
  5 from __future__ import division
  6 
  7 import argparse
  8 import os
  9 import random
  10 import torch
  11 
  12 import onmt.opts as opts
  13 
  14 from onmt.inputters.inputter import build_dataset_iter, lazily_load_dataset, \
  15     _load_fields, _collect_report_features
  16 from onmt.model_builder import build_model
  17 from onmt.utils.optimizers import build_optim
  18 from onmt.trainer import build_trainer
  19 from onmt.models import build_model_saver
  20 from onmt.utils.logging import init_logger, logger
  21 
  22 
  23 def _check_save_model_path(opt):
  24     save_model_path = os.path.abspath(opt.save_model)
  25     model_dirname = os.path.dirname(save_model_path)
  26     if not os.path.exists(model_dirname):
  27         os.makedirs(model_dirname)
  28 
  29 
  30 def _tally_parameters(model):
  31     n_params = sum([p.nelement() for p in model.parameters()])
  32     enc = 0
  33     dec = 0
  34     for name, param in model.named_parameters():
  35         if 'encoder' in name:
  36             enc += param.nelement()
  37         elif 'decoder' or 'generator' in name:
  38             dec += param.nelement()
  39     return n_params, enc, dec
  40 
  41 
  42 def training_opt_postprocessing(opt):
  43     if opt.word_vec_size != -1:
  44         opt.src_word_vec_size = opt.word_vec_size
  45         opt.tgt_word_vec_size = opt.word_vec_size
  46 
  47     if opt.layers != -1:
  48         opt.enc_layers = opt.layers
  49         opt.dec_layers = opt.layers
  50 
  51     opt.brnn = (opt.encoder_type == "brnn")
  52 
  53     if opt.rnn_type == "SRU" and not opt.gpuid:
  54         raise AssertionError("Using SRU requires -gpuid set.")
  55 
  56     if torch.cuda.is_available() and not opt.gpuid:
  57         logger.info("WARNING: You have a CUDA device, should run with -gpuid")
  58 
  59     if opt.gpuid:
  60         torch.cuda.set_device(opt.device_id)
  61         if opt.seed > 0:
  62             # this one is needed for torchtext random call (shuffled iterator)
  63             # in multi gpu it ensures datasets are read in the same order
  64             random.seed(opt.seed)
  65             # These ensure same initialization in multi gpu mode
  66             torch.manual_seed(opt.seed)
  67             torch.cuda.manual_seed(opt.seed)
  68 
  69     return opt
  70 
  71 
  72 def main(opt):
  73     opt = training_opt_postprocessing(opt)
  74     init_logger(opt.log_file)
  75     # Load checkpoint if we resume from a previous training.
  76     if opt.train_from:
  77         logger.info('Loading checkpoint from %s' % opt.train_from)
  78         checkpoint = torch.load(opt.train_from,
  79                                 map_location=lambda storage, loc: storage)
  80         model_opt = checkpoint['opt']
  81     else:
  82         checkpoint = None
  83         model_opt = opt
  84 
  85     # Peek the first dataset to determine the data_type.
  86     # (All datasets have the same data_type).
  87     first_dataset = next(lazily_load_dataset("train", opt))
  88     data_type = first_dataset.data_type
  89 
  90     # Load fields generated from preprocess phase.
  91     fields = _load_fields(first_dataset, data_type, opt, checkpoint)
  92 
  93     # Report src/tgt features.
  94 
  95     src_features, tgt_features = _collect_report_features(fields)
  96     for j, feat in enumerate(src_features):
  97         logger.info(' * src feature %d size = %d'
  98                     % (j, len(fields[feat].vocab)))
  99     for j, feat in enumerate(tgt_features):
  100         logger.info(' * tgt feature %d size = %d'
  101                     % (j, len(fields[feat].vocab)))
  102 
  103     # Build model.
  104     model = build_model(model_opt, opt, fields, checkpoint)
  105     n_params, enc, dec = _tally_parameters(model)
  106     logger.info('encoder: %d' % enc)
  107     logger.info('decoder: %d' % dec)
  108     logger.info('* number of parameters: %d' % n_params)
  109     _check_save_model_path(opt)
  110 
  111     # Build optimizer.
  112     optim = build_optim(model, opt, checkpoint)
  113 
  114     # Build model saver
  115     model_saver = build_model_saver(model_opt, opt, model, fields, optim)
  116 
  117     trainer = build_trainer(
  118         opt, model, fields, optim, data_type, model_saver=model_saver)
  119 
  120     def train_iter_fct(): return build_dataset_iter(
  121         lazily_load_dataset("train", opt), fields, opt)
  122 
  123     def valid_iter_fct(): return build_dataset_iter(
  124         lazily_load_dataset("valid", opt), fields, opt)
  125 
  126     # Do training.
  127     trainer.train(train_iter_fct, valid_iter_fct, opt.train_steps,
  128                   opt.valid_steps)
  129 
  130     if opt.tensorboard:
  131         trainer.report_manager.tensorboard_writer.close()
  132 
  133 
  134 if __name__ == "__main__":
  135     parser = argparse.ArgumentParser(
  136         description='train.py',
  137         formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  138 
  139     opts.add_md_help_argument(parser)
  140     opts.model_opts(parser)
  141     opts.train_opts(parser)
  142 
  143     opt = parser.parse_args()
  144     main(opt)
  145
  ```
  --- 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/onmt/train_single.py b/onmt/train_single.py
  --- a/onmt/train_single.py
  +++ b/onmt/train_single.py
  @@ -121,7 +121,7 @@
           lazily_load_dataset("train", opt), fields, opt)
   
       def valid_iter_fct(): return build_dataset_iter(
  -        lazily_load_dataset("valid", opt), fields, opt)
  +        lazily_load_dataset("valid", opt), fields, opt, is_train=False)
   
       # Do training.
       trainer.train(train_iter_fct, valid_iter_fct, opt.train_steps,
  
verification_info: {"golden_diff": "diff --git a/onmt/train_single.py b/onmt/train_single.py\n--- a/onmt/train_single.py\n+++ b/onmt/train_single.py\n@@ -121,7 +121,7 @@\n         lazily_load_dataset(\"train\", opt), fields, opt)\n \n     def valid_iter_fct(): return build_dataset_iter(\n-        lazily_load_dataset(\"valid\", opt), fields, opt)\n+        lazily_load_dataset(\"valid\", opt), fields, opt, is_train=False)\n \n     # Do training.\n     trainer.train(train_iter_fct, valid_iter_fct, opt.train_steps,\n", "issue": "valid_batch_size\nIn the train_single.py file , lines 120 to 124\r\n```\r\ndef train_iter_fct(): return build_dataset_iter(\r\n        lazily_load_dataset(\"train\", opt), fields, opt)\r\n\r\n    def valid_iter_fct(): return build_dataset_iter(\r\n        lazily_load_dataset(\"valid\", opt), fields)\r\n```\r\nshould be changed\r\n```\r\ndef train_iter_fct(): return build_dataset_iter(\r\n        lazily_load_dataset(\"train\", opt), fields, opt)\r\n\r\n    def valid_iter_fct(): return build_dataset_iter(\r\n        lazily_load_dataset(\"valid\", opt), fields, opt, is_train=False)\r\n```\r\nIf it doesn't, it will not use `valid_batch_size`.\r\n\n", "before_files": [{"content": "#!/usr/bin/env python\n\"\"\"\n    Training on a single process\n\"\"\"\nfrom __future__ import division\n\nimport argparse\nimport os\nimport random\nimport torch\n\nimport onmt.opts as opts\n\nfrom onmt.inputters.inputter import build_dataset_iter, lazily_load_dataset, \\\n    _load_fields, _collect_report_features\nfrom onmt.model_builder import build_model\nfrom onmt.utils.optimizers import build_optim\nfrom onmt.trainer import build_trainer\nfrom onmt.models import build_model_saver\nfrom onmt.utils.logging import init_logger, logger\n\n\ndef _check_save_model_path(opt):\n    save_model_path = os.path.abspath(opt.save_model)\n    model_dirname = os.path.dirname(save_model_path)\n    if not os.path.exists(model_dirname):\n        os.makedirs(model_dirname)\n\n\ndef _tally_parameters(model):\n    n_params = sum([p.nelement() for p in model.parameters()])\n    enc = 0\n    dec = 0\n    for name, param in model.named_parameters():\n        if 'encoder' in name:\n            enc += param.nelement()\n        elif 'decoder' or 'generator' in name:\n            dec += param.nelement()\n    return n_params, enc, dec\n\n\ndef training_opt_postprocessing(opt):\n    if opt.word_vec_size != -1:\n        opt.src_word_vec_size = opt.word_vec_size\n        opt.tgt_word_vec_size = opt.word_vec_size\n\n    if opt.layers != -1:\n        opt.enc_layers = opt.layers\n        opt.dec_layers = opt.layers\n\n    opt.brnn = (opt.encoder_type == \"brnn\")\n\n    if opt.rnn_type == \"SRU\" and not opt.gpuid:\n        raise AssertionError(\"Using SRU requires -gpuid set.\")\n\n    if torch.cuda.is_available() and not opt.gpuid:\n        logger.info(\"WARNING: You have a CUDA device, should run with -gpuid\")\n\n    if opt.gpuid:\n        torch.cuda.set_device(opt.device_id)\n        if opt.seed > 0:\n            # this one is needed for torchtext random call (shuffled iterator)\n            # in multi gpu it ensures datasets are read in the same order\n            random.seed(opt.seed)\n            # These ensure same initialization in multi gpu mode\n            torch.manual_seed(opt.seed)\n            torch.cuda.manual_seed(opt.seed)\n\n    return opt\n\n\ndef main(opt):\n    opt = training_opt_postprocessing(opt)\n    init_logger(opt.log_file)\n    # Load checkpoint if we resume from a previous training.\n    if opt.train_from:\n        logger.info('Loading checkpoint from %s' % opt.train_from)\n        checkpoint = torch.load(opt.train_from,\n                                map_location=lambda storage, loc: storage)\n        model_opt = checkpoint['opt']\n    else:\n        checkpoint = None\n        model_opt = opt\n\n    # Peek the first dataset to determine the data_type.\n    # (All datasets have the same data_type).\n    first_dataset = next(lazily_load_dataset(\"train\", opt))\n    data_type = first_dataset.data_type\n\n    # Load fields generated from preprocess phase.\n    fields = _load_fields(first_dataset, data_type, opt, checkpoint)\n\n    # Report src/tgt features.\n\n    src_features, tgt_features = _collect_report_features(fields)\n    for j, feat in enumerate(src_features):\n        logger.info(' * src feature %d size = %d'\n                    % (j, len(fields[feat].vocab)))\n    for j, feat in enumerate(tgt_features):\n        logger.info(' * tgt feature %d size = %d'\n                    % (j, len(fields[feat].vocab)))\n\n    # Build model.\n    model = build_model(model_opt, opt, fields, checkpoint)\n    n_params, enc, dec = _tally_parameters(model)\n    logger.info('encoder: %d' % enc)\n    logger.info('decoder: %d' % dec)\n    logger.info('* number of parameters: %d' % n_params)\n    _check_save_model_path(opt)\n\n    # Build optimizer.\n    optim = build_optim(model, opt, checkpoint)\n\n    # Build model saver\n    model_saver = build_model_saver(model_opt, opt, model, fields, optim)\n\n    trainer = build_trainer(\n        opt, model, fields, optim, data_type, model_saver=model_saver)\n\n    def train_iter_fct(): return build_dataset_iter(\n        lazily_load_dataset(\"train\", opt), fields, opt)\n\n    def valid_iter_fct(): return build_dataset_iter(\n        lazily_load_dataset(\"valid\", opt), fields, opt)\n\n    # Do training.\n    trainer.train(train_iter_fct, valid_iter_fct, opt.train_steps,\n                  opt.valid_steps)\n\n    if opt.tensorboard:\n        trainer.report_manager.tensorboard_writer.close()\n\n\nif __name__ == \"__main__\":\n    parser = argparse.ArgumentParser(\n        description='train.py',\n        formatter_class=argparse.ArgumentDefaultsHelpFormatter)\n\n    opts.add_md_help_argument(parser)\n    opts.model_opts(parser)\n    opts.train_opts(parser)\n\n    opt = parser.parse_args()\n    main(opt)\n", "path": "onmt/train_single.py"}], "after_files": [{"content": "#!/usr/bin/env python\n\"\"\"\n    Training on a single process\n\"\"\"\nfrom __future__ import division\n\nimport argparse\nimport os\nimport random\nimport torch\n\nimport onmt.opts as opts\n\nfrom onmt.inputters.inputter import build_dataset_iter, lazily_load_dataset, \\\n    _load_fields, _collect_report_features\nfrom onmt.model_builder import build_model\nfrom onmt.utils.optimizers import build_optim\nfrom onmt.trainer import build_trainer\nfrom onmt.models import build_model_saver\nfrom onmt.utils.logging import init_logger, logger\n\n\ndef _check_save_model_path(opt):\n    save_model_path = os.path.abspath(opt.save_model)\n    model_dirname = os.path.dirname(save_model_path)\n    if not os.path.exists(model_dirname):\n        os.makedirs(model_dirname)\n\n\ndef _tally_parameters(model):\n    n_params = sum([p.nelement() for p in model.parameters()])\n    enc = 0\n    dec = 0\n    for name, param in model.named_parameters():\n        if 'encoder' in name:\n            enc += param.nelement()\n        elif 'decoder' or 'generator' in name:\n            dec += param.nelement()\n    return n_params, enc, dec\n\n\ndef training_opt_postprocessing(opt):\n    if opt.word_vec_size != -1:\n        opt.src_word_vec_size = opt.word_vec_size\n        opt.tgt_word_vec_size = opt.word_vec_size\n\n    if opt.layers != -1:\n        opt.enc_layers = opt.layers\n        opt.dec_layers = opt.layers\n\n    opt.brnn = (opt.encoder_type == \"brnn\")\n\n    if opt.rnn_type == \"SRU\" and not opt.gpuid:\n        raise AssertionError(\"Using SRU requires -gpuid set.\")\n\n    if torch.cuda.is_available() and not opt.gpuid:\n        logger.info(\"WARNING: You have a CUDA device, should run with -gpuid\")\n\n    if opt.gpuid:\n        torch.cuda.set_device(opt.device_id)\n        if opt.seed > 0:\n            # this one is needed for torchtext random call (shuffled iterator)\n            # in multi gpu it ensures datasets are read in the same order\n            random.seed(opt.seed)\n            # These ensure same initialization in multi gpu mode\n            torch.manual_seed(opt.seed)\n            torch.cuda.manual_seed(opt.seed)\n\n    return opt\n\n\ndef main(opt):\n    opt = training_opt_postprocessing(opt)\n    init_logger(opt.log_file)\n    # Load checkpoint if we resume from a previous training.\n    if opt.train_from:\n        logger.info('Loading checkpoint from %s' % opt.train_from)\n        checkpoint = torch.load(opt.train_from,\n                                map_location=lambda storage, loc: storage)\n        model_opt = checkpoint['opt']\n    else:\n        checkpoint = None\n        model_opt = opt\n\n    # Peek the first dataset to determine the data_type.\n    # (All datasets have the same data_type).\n    first_dataset = next(lazily_load_dataset(\"train\", opt))\n    data_type = first_dataset.data_type\n\n    # Load fields generated from preprocess phase.\n    fields = _load_fields(first_dataset, data_type, opt, checkpoint)\n\n    # Report src/tgt features.\n\n    src_features, tgt_features = _collect_report_features(fields)\n    for j, feat in enumerate(src_features):\n        logger.info(' * src feature %d size = %d'\n                    % (j, len(fields[feat].vocab)))\n    for j, feat in enumerate(tgt_features):\n        logger.info(' * tgt feature %d size = %d'\n                    % (j, len(fields[feat].vocab)))\n\n    # Build model.\n    model = build_model(model_opt, opt, fields, checkpoint)\n    n_params, enc, dec = _tally_parameters(model)\n    logger.info('encoder: %d' % enc)\n    logger.info('decoder: %d' % dec)\n    logger.info('* number of parameters: %d' % n_params)\n    _check_save_model_path(opt)\n\n    # Build optimizer.\n    optim = build_optim(model, opt, checkpoint)\n\n    # Build model saver\n    model_saver = build_model_saver(model_opt, opt, model, fields, optim)\n\n    trainer = build_trainer(\n        opt, model, fields, optim, data_type, model_saver=model_saver)\n\n    def train_iter_fct(): return build_dataset_iter(\n        lazily_load_dataset(\"train\", opt), fields, opt)\n\n    def valid_iter_fct(): return build_dataset_iter(\n        lazily_load_dataset(\"valid\", opt), fields, opt, is_train=False)\n\n    # Do training.\n    trainer.train(train_iter_fct, valid_iter_fct, opt.train_steps,\n                  opt.valid_steps)\n\n    if opt.tensorboard:\n        trainer.report_manager.tensorboard_writer.close()\n\n\nif __name__ == \"__main__\":\n    parser = argparse.ArgumentParser(\n        description='train.py',\n        formatter_class=argparse.ArgumentDefaultsHelpFormatter)\n\n    opts.add_md_help_argument(parser)\n    opts.model_opts(parser)\n    opts.train_opts(parser)\n\n    opt = parser.parse_args()\n    main(opt)\n", "path": "onmt/train_single.py"}]}