def get_original_task_module(opt: Opt, multi_possible: bool = False): """ Returns task module of "original" task. Original task in this case means the task we want to use with the control teacher. :param opt: opt dict :param multi_possible: specify True if multiple tasks are possible. :return task_module: return module associated with task. """ modules = [] tasks = opt['task'].split(',') if not multi_possible: assert len(tasks) == 1 for task in tasks: if len(task.split(':')) < 3: raise RuntimeError( '\n\n********************************************************\n' 'Must specify original task using the following format:\n' '`--task internal:flattened:task:<ORIGINAL TASK NAME>`' '\n********************************************************\n') original_task = ':'.join(task.split(':')[2:]) task_module = load_teacher_module(original_task) modules.append(task_module) if multi_possible: return modules return modules[0]
def create_task_agent_from_taskname(opt: Opt): """ Create task agent(s) assuming the input ``task_dir:teacher_class``. e.g. def_string is a shorthand path like ``babi:Task1k:1`` or ``#babi`` or a complete path like ``parlai.tasks.babi.agents:Task1kTeacher:1``, which essentially performs ``from parlai.tasks.babi import Task1kTeacher`` with the parameter ``1`` in ``opt['task']`` to be used by the class ``Task1kTeacher``. """ if not (opt.get('task') or opt.get('pytorch_teacher_task') or opt.get('pytorch_teacher_dataset')): raise RuntimeError('No task specified. Please select a task with ' + '--task {task_name}.') if not opt.get('task'): opt['task'] = 'pytorch_teacher' if ',' not in opt['task']: # Single task teacher_class = load_teacher_module(opt['task']) _add_task_flags_to_agent_opt(teacher_class, opt, opt['task']) task_agents = teacher_class(opt) if type(task_agents) != list: task_agents = [task_agents] return task_agents else: # Multitask teacher/agent task_agents = MultiTaskTeacher(opt) if type(task_agents) != list: task_agents = [task_agents] return task_agents
def add_task_args(self, task): """ Add arguments specific to the specified task. """ for t in ids_to_tasks(task).split(','): agent = load_teacher_module(t) try: if hasattr(agent, 'add_cmdline_args'): agent.add_cmdline_args(self) except argparse.ArgumentError: # already added pass
def add_task_args(self, task: str, partial: Opt): """ Add arguments specific to the specified task. """ for t in ids_to_tasks(task).split(','): agent = load_teacher_module(t) try: if hasattr(agent, 'add_cmdline_args'): agent.add_cmdline_args(self, partial) except TypeError as typ: raise TypeError( f"Task '{task}' appears to have signature " "add_cmdline_args(argparser) but we have updated the signature " "to add_cmdline_args(argparser, partial_opt). For details, see " "https://github.com/facebookresearch/ParlAI/pull/3328." ) from typ except argparse.ArgumentError: # already added pass
def test_load_teacher(self): teacher_module = load_teacher_module(OPTIONS['task']) self.assertEqual(teacher_module, c2agents.SelfRevisedTeacher)