Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Allow missing args when using --init-opt (#3112)
Browse files Browse the repository at this point in the history
* Allow missing init opt opts

* Add part of unit test

* Work on unit test

* Test fixes

* Fix second test

* Fix test

* Check obsolete arg does not exist
  • Loading branch information
EricMichaelSmith authored Oct 2, 2020
1 parent 23beb37 commit 6b52b48
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
21 changes: 18 additions & 3 deletions parlai/core/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,15 @@ def add_parlai_args(self, args=None):
help='Path to json file of options. '
'Note: Further Command-line arguments override file-based options.',
)
parlai.add_argument(
'--allow-missing-init-opts',
type='bool',
default=False,
help=(
'Warn instead of raising if an argument passed in with --init-opt is '
'not in the target opt.'
),
)
parlai.add_argument(
'-t', '--task', help='ParlAI task(s), e.g. "babi:Task1" or "babi,cbt"'
)
Expand Down Expand Up @@ -927,9 +936,15 @@ def _load_opts(self, opt):
for key, value in new_opt.items():
# existing command line parameters take priority.
if key not in opt:
raise RuntimeError(
'Trying to set opt from file that does not exist: ' + str(key)
)
if opt.get('allow_missing_init_opts', False):
logging.warn(
f'The "{key}" key in {optfile} will not be loaded, because it '
f'does not exist in the target opt.'
)
else:
raise RuntimeError(
'Trying to set opt from file that does not exist: ' + str(key)
)
if key not in opt['override']:
opt[key] = value
opt['override'][key] = value
Expand Down
51 changes: 51 additions & 0 deletions tests/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import parlai.utils.testing as testing_utils
from parlai.core.opt import Opt
from parlai.core.params import ParlaiParser
from parlai.scripts.compare_opts import compare_opts

"""
Expand Down Expand Up @@ -126,3 +127,53 @@ def test_compare_opts_load_raw(self):
\t\tIn opt 1: <MISSING>
\t\tIn opt 2: no"""
self.assertEqual(output, desired_output)


class TestInitOpt(unittest.TestCase):
"""
Test functionality related to --init-opt.
"""

def test_init_opt(self):
"""
Test --init-opt.
"""

with testing_utils.tempdir() as temp_dir:

init_opt_path = os.path.join(temp_dir, 'init_opt.opt')
test_model_file = '/test_model_path/model'

# Save a test opt file
init_opt = Opt({'model_file': test_model_file})
init_opt.save(init_opt_path)

# Load the opt back in with --init-opt and make sure it's been set
# correctly
opt = ParlaiParser(True, True).parse_kwargs(init_opt=init_opt_path)
self.assertEqual(opt['model_file'], test_model_file)

def test_allow_missing_init_opts(self):
"""
Test --allow-missing-init-opts.
"""

with testing_utils.tempdir() as temp_dir:

init_opt_path = os.path.join(temp_dir, 'init_opt.opt')

# Save a test opt file with an argument that doesn't exist
init_opt = Opt({'made_up_arg': 'foo'})
init_opt.save(init_opt_path)

# Assert that the opt file normally can't be loaded in
with self.assertRaises(RuntimeError):
_ = ParlaiParser(True, True).parse_kwargs(init_opt=init_opt_path)

# Assert that the opt file *can* be loaded in if we set
# --allow-missing-init-opts, and assert that the made-up arg does not exist
# in the opt
opt = ParlaiParser(True, True).parse_kwargs(
init_opt=init_opt_path, allow_missing_init_opts=True
)
self.assertFalse(hasattr(opt, 'made_up_arg'))

0 comments on commit 6b52b48

Please sign in to comment.