def from_config(cls, task_config, metadata=None, model_state=None): """ Create the task from config, and optionally load metadata/model_state This function will create components including :class:`~DataHandler`, :class:`~Trainer`, :class:`~MetricReporter`, :class:`~Exporter`, and wire them up. Args: task_config (Task.Config): the config of the current task metadata: saved global context of this task, e.g: vocabulary, will be generated by :class:`~DataHandler` if it's None model_state: saved model parameters, will be loaded into model when given """ if hasattr(task_config.labels, "target_prob"): assert task_config.labels.target_prob == isinstance( task_config.model.output_layer.loss, ( KLDivergenceBCELoss.Config, KLDivergenceCELoss.Config, SoftHardBCELoss.Config, ), ), "target_prob must be set to True for KD losses" featurizer = create_featurizer(task_config.featurizer, task_config.features) # load data data_handler = create_data_handler( task_config.data_handler, task_config.features, task_config.labels, featurizer=featurizer, ) print("\nLoading data...") if metadata: data_handler.load_metadata(metadata) else: data_handler.init_metadata() metadata = data_handler.metadata model = create_model(task_config.model, task_config.features, metadata) if model_state: model.load_state_dict(model_state) if cuda.CUDA_ENABLED: model = model.cuda() metric_reporter = create_metric_reporter(task_config.metric_reporter, metadata) exporter = (create_exporter( task_config.exporter, task_config.features, task_config.labels, data_handler.metadata, task_config.model, ) if task_config.exporter else None) return cls( trainer=create_trainer(task_config.trainer, model), data_handler=data_handler, model=model, metric_reporter=metric_reporter, exporter=exporter, )
def _create_dummy_data_handler(self): feat = WordFeatConfig( vocab_size=4, vocab_from_all_data=True, vocab_from_train_data=True, vocab_from_pretrained_embeddings=False, pretrained_embeddings_path=None, ) featurizer = create_featurizer(SimpleFeaturizer.Config(), FeatureConfig(word_feat=feat)) data_handler = DocClassificationDataHandler.from_config( DocClassificationDataHandler.Config(), ModelInputConfig(word_feat=feat), TargetConfig(), featurizer=featurizer, ) train_data = data_handler.gen_dataset([{ "text": "<pad>" }], include_label_fields=False) eval_data = data_handler.gen_dataset([{ "text": "<pad>" }], include_label_fields=False) test_data = data_handler.gen_dataset([{ "text": "<pad>" }], include_label_fields=False) data_handler.init_feature_metadata(train_data, eval_data, test_data) return data_handler
def from_config(cls, task_config, metadata=None, model_state=None): """ Create the task from config, and optionally load metadata/model_state This function will create components including :class:`~DataHandler`, :class:`~Trainer`, :class:`~Optimizer`, :class:`~Scheduler`, :class:`~MetricReporter`, :class:`~Exporter`, and wire them up. Args: task_config (Task.Config): the config of the current task metadata: saved global context of this task, e.g: vocabulary, will be generated by :class:`~DataHandler` if it's None model_state: saved model parameters, will be loaded into model when given """ print("Task parameters:\n") pprint(config_to_json(type(task_config), task_config)) featurizer = create_featurizer(task_config.featurizer, task_config.features) # load data data_handler = create_data_handler( task_config.data_handler, task_config.features, task_config.labels, featurizer=featurizer, ) print("\nLoading data...") if metadata: data_handler.load_metadata(metadata) else: data_handler.init_metadata() metadata = data_handler.metadata model = create_model(task_config.model, task_config.features, metadata) if model_state: model.load_state_dict(model_state) if cuda_utils.CUDA_ENABLED: model = model.cuda() metric_reporter = create_metric_reporter(task_config.metric_reporter, metadata) optimizer = create_optimizer(task_config.optimizer, model) exporter = ( create_exporter( task_config.exporter, task_config.features, task_config.labels, data_handler.metadata, task_config.model, ) if task_config.exporter else None ) return cls( trainer=create_trainer(task_config.trainer), data_handler=data_handler, model=model, metric_reporter=metric_reporter, optimizer=optimizer, lr_scheduler=Scheduler( optimizer, task_config.scheduler, metric_reporter.lower_is_better ), exporter=exporter, )
def test_read_partially_from_csv(self): file_name = tests_module.test_file("train_data_tiny.tsv") columns = {DFColumn.DOC_LABEL: 0, DFColumn.UTTERANCE: 2} feat = WordFeatConfig( vocab_from_all_data=True, vocab_from_train_data=False, vocab_from_pretrained_embeddings=False, ) featurizer = create_featurizer( SimpleFeaturizer.Config(), FeatureConfig(word_feat=feat) ) data_handler = DocClassificationDataHandler.from_config( DocClassificationDataHandler.Config(), ModelInputConfig(word_feat=feat), TargetConfig(), featurizer=featurizer, ) data = list(data_handler.read_from_file(file_name, columns)) for col in columns: self.assertTrue(col in data[0], "{} must in the data".format(col)) self.assertEqual("alarm/modify_alarm", data[0][DFColumn.DOC_LABEL]) self.assertEqual( "change my alarm tomorrow to wake me up 30 minutes earlier", data[0][DFColumn.UTTERANCE], )
def from_config(cls, task_config, metadata=None, model_state=None): print("Task parameters:\n") pprint(config_to_json(type(task_config), task_config)) data_handlers = OrderedDict() exporters = OrderedDict() for name, task in task_config.tasks.items(): featurizer = create_featurizer(task.featurizer, task.features) data_handlers[name] = create_data_handler(task.data_handler, task.features, task.labels, featurizer=featurizer) data_handler = DisjointMultitaskDataHandler(task_config.data_handler, data_handlers) print("\nLoading data...") if metadata: data_handler.load_metadata(metadata) else: data_handler.init_metadata() metadata = data_handler.metadata exporters = { name: (create_exporter( task.exporter, task.features, task.labels, data_handler.data_handlers[name].metadata, task.model, ) if task.exporter else None) for name, task in task_config.tasks.items() } metric_reporter = DisjointMultitaskMetricReporter( OrderedDict( (name, create_metric_reporter(task.metric_reporter, metadata[name])) for name, task in task_config.tasks.items()), target_task_name=task_config.metric_reporter.target_task_name, ) model = DisjointMultitaskModel( OrderedDict( (name, create_model(task.model, task.features, metadata[name])) for name, task in task_config.tasks.items())) if model_state: model.load_state_dict(model_state) if cuda_utils.CUDA_ENABLED: model = model.cuda() optimizers = create_optimizer(model, task_config.optimizer) return cls( exporters=exporters, trainer=create_trainer(task_config.trainer), data_handler=data_handler, model=model, metric_reporter=metric_reporter, optimizers=optimizers, lr_scheduler=Scheduler(optimizers, task_config.scheduler, metric_reporter.lower_is_better), )
def _init_data_handler(self): data_handler = LanguageModelDataHandler.from_config( LanguageModelDataHandler.Config(), FeatureConfig(), WordLabelConfig(), featurizer=create_featurizer(SimpleFeaturizer.Config(), FeatureConfig()), shuffle=False, ) data_handler.init_metadata_from_path(FILE_NAME, FILE_NAME, FILE_NAME) return data_handler
def from_config(cls, task_config, metadata=None, model_state=None): print("(mldc/task/gpt_task.py def from_config) Task parameters:\n") pprint(config_to_json(type(task_config), task_config)) featurizer = create_featurizer( task_config.featurizer, task_config.features, text_embedder_config=task_config.text_embedder ) # featurizer :: text embedder GPT2Embed # load data data_handler = create_data_handler( task_config.data_handler, task_config.features, task_config.labels, text_embedder_config=task_config.text_embedder, featurizer=featurizer, ) print( "\n(mldc/task/retrieval.py GptTask def from_config) Loading data..." ) if metadata: data_handler.load_metadata(metadata) else: data_handler.init_metadata() metadata = data_handler.metadata task_config.features.seq_word_feat.embed_dim = data_handler.text_embedder.embed_dim print("create model!") model = create_model(task_config.model, task_config.features, metadata) if model_state: model.load_state_dict(model_state) if cuda_utils.CUDA_ENABLED: model = model.cuda() metric_reporter = create_metric_reporter( task_config.metric_reporter, metadata, text_embedder=task_config.text_embedder) return cls( trainer=create_trainer(task_config.trainer), data_handler=data_handler, model=model, metric_reporter=metric_reporter, model_needs_meta_training=task_config.model_needs_meta_training, )
def create_language_model_data_handler(cls) -> LanguageModelDataHandler: # TODO: Refactor this after Shicong refactors PyText config and removes # Thrift. After that directly use Data Handler's from config method # with synthetic configs columns = [DFColumn.UTTERANCE] features: Dict[str, Field] = { DatasetFieldName.TEXT_FIELD: TextFeatureField( eos_token=VocabMeta.EOS_TOKEN, init_token=VocabMeta.INIT_TOKEN ) } return LanguageModelDataHandler( raw_columns=columns, features=features, labels={}, featurizer=create_featurizer(SimpleFeaturizer.Config(), FeatureConfig()), )
def from_config( cls, task_config: Config, metadata=None, model_state=None, tensorizers=None, rank=0, world_size=1, ): print("Task parameters:\n") pprint(config_to_json(type(task_config), task_config)) data_handlers = OrderedDict() exporters = OrderedDict() for name, task in task_config.tasks.items(): featurizer = create_featurizer(task.featurizer, task.features) data_handlers[name] = create_data_handler( task.data_handler, task.features, task.labels, featurizer=featurizer ) data_handler = DisjointMultitaskDataHandler( task_config.data_handler, data_handlers, target_task_name=task_config.target_task_name, ) print("\nLoading data...") if metadata: data_handler.load_metadata(metadata) else: data_handler.init_metadata() metadata = data_handler.metadata exporters = { name: ( create_exporter( task.exporter, task.features, task.labels, data_handler.data_handlers[name].metadata, task.model, ) if task.exporter else None ) for name, task in task_config.tasks.items() } task_weights = { task_name: task_config.task_weights.get(task_name, 1) for task_name in task_config.tasks.keys() } metric_reporter = DisjointMultitaskMetricReporter( OrderedDict( (name, create_metric_reporter(task.metric_reporter, metadata[name])) for name, task in task_config.tasks.items() ), loss_weights=task_weights, target_task_name=task_config.target_task_name, use_subtask_select_metric=( task_config.metric_reporter.use_subtask_select_metric ), ) model = DisjointMultitaskModel( OrderedDict( (name, create_model(task.model, task.features, metadata[name])) for name, task in task_config.tasks.items() ), loss_weights=task_weights, ) if model_state: model.load_state_dict(model_state) if cuda.CUDA_ENABLED: model = model.cuda() return cls( target_task_name=task_config.target_task_name, exporters=exporters, trainer=create_trainer(task_config.trainer, model), data_handler=data_handler, model=model, metric_reporter=metric_reporter, )