def test_tokens_dictfeat_contextual(self): # TODO (T65593688): this should be removed after # https://github.com/pytorch/pytorch/pull/33645 is merged. with torch.no_grad(): model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), inputs=Seq2SeqModel.Config.ModelInput( dict_feat=GazetteerTensorizer.Config( text_column="source_sequence" ), contextual_token_embedding=ByteTokenTensorizer.Config(), ), encoder_decoder=RNNModel.Config( encoder=LSTMSequenceEncoder.Config(embed_dim=619) ), dict_embedding=DictEmbedding.Config(), contextual_token_embedding=ContextualTokenEmbedding.Config( embed_dim=7 ), ), get_tensorizers(add_dict_feat=True, add_contextual_feat=True), ) model.eval() ts_model = model.torchscriptify() res = ts_model( ["call", "mom"], (["call", "mom"], [0.42, 0.17], [4, 3]), [0.42] * (7 * 2), ) assert res is not None
def test_reset_incremental_states(self): """ This test might seem trivial. However, interacting with the scripted sequence generator crosses the Torchscript boundary, which can lead to weird behavior. If the incremental states don't get properly reset, the model will produce garbage _after_ the first call, which is a pain to debug when you only catch it after training. """ tensorizers = get_tensorizers() # Avoid numeric issues with quantization by setting a known seed. torch.manual_seed(42) model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), ), tensorizers, ) # Get sample inputs using a data source. schema = { "source_sequence": str, "dict_feat": Gazetteer, "target_sequence": str, } data = Data.from_config( Data.Config(source=TSVDataSource.Config( train_filename=TEST_FILE_NAME, field_names=[ "source_sequence", "dict_feat", "target_sequence" ], )), schema, tensorizers, ) data.batcher = Batcher(1, 1, 1) raw_batch, batch = next( iter(data.batches(Stage.TRAIN, load_early=True))) inputs = model.arrange_model_inputs(batch) model.eval() outputs = model(*inputs) pred, scores = model.get_pred(outputs, {"stage": Stage.TEST}) # Verify that the incremental states reset correctly. decoder = model.sequence_generator.beam_search.decoder_ens decoder.reset_incremental_states() self.assertDictEqual(decoder.incremental_states, {"0": {}}) # Verify that the model returns the same predictions. new_pred, new_scores = model.get_pred(outputs, {"stage": Stage.TEST}) self.assertEqual(new_scores, scores)
def test_tokens(self): model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), ), get_tensorizers(), ) model.eval() ts_model = model.torchscriptify() res = ts_model(["call", "mom"]) assert res is not None
def test_tokens(self): # TODO: this should be removed after # https://github.com/pytorch/pytorch/pull/33645 is merged. with torch.no_grad(): model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), ), get_tensorizers(), ) model.eval() ts_model = model.torchscriptify() res = ts_model(["call", "mom"]) assert res is not None
def test_tokens_contextual(self): model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), inputs=Seq2SeqModel.Config.ModelInput( contextual_token_embedding=ByteTokenTensorizer.Config()), contextual_token_embedding=ContextualTokenEmbedding.Config( embed_dim=7), encoder_decoder=RNNModel.Config( encoder=LSTMSequenceEncoder.Config(embed_dim=519)), ), get_tensorizers(add_contextual_feat=True), ) model.eval() ts_model = model.torchscriptify() res = ts_model(["call", "mom"], contextual_token_embedding=[0.42] * (7 * 2)) assert res is not None
def test_tokens_dictfeat(self): model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), inputs=Seq2SeqModel.Config.ModelInput( dict_feat=GazetteerTensorizer.Config( text_column="source_sequence")), encoder_decoder=RNNModel.Config( encoder=LSTMSequenceEncoder.Config(embed_dim=612)), dict_embedding=DictEmbedding.Config(), ), get_tensorizers(add_dict_feat=True), ) model.eval() ts_model = model.torchscriptify() res = ts_model(["call", "mom"], (["call", "mom"], [0.42, 0.17], [4, 3])) assert res is not None
def test_force_predictions_on_eval(self): tensorizers = get_tensorizers() model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), ), tensorizers, ) # Get sample inputs using a data source. schema = { "source_sequence": str, "dict_feat": Gazetteer, "target_sequence": str, } data = Data.from_config( Data.Config(source=TSVDataSource.Config( train_filename=TEST_FILE_NAME, field_names=[ "source_sequence", "dict_feat", "target_sequence" ], )), schema, tensorizers, ) data.batcher = Batcher(1, 1, 1) raw_batch, batch = next( iter(data.batches(Stage.TRAIN, load_early=True))) inputs = model.arrange_model_inputs(batch) # Verify that model does not run sequence generation on prediction. outputs = model(*inputs) pred = model.get_pred(outputs, {"stage": Stage.EVAL}) self.assertEqual(pred, (None, None)) # Verify that attempting to set force_eval_predictions is correctly # accounted for. model.force_eval_predictions = True with self.assertRaises(AssertionError): _ = model.get_pred(outputs, {"stage": Stage.EVAL})
def test_generator(self): model = Seq2SeqModel.from_config( Seq2SeqModel.Config( source_embedding=WordEmbedding.Config(embed_dim=512), target_embedding=WordEmbedding.Config(embed_dim=512), ), self._get_tensorizers(), ) sample, _ = get_example_and_check() repacked_inputs = { "src_tokens": sample[0].t(), "src_lengths": sample[1] } scripted_generator = ScriptedSequenceGenerator( [model.model], model.trg_eos_index, ScriptedSequenceGenerator.Config()) scripted_preds = scripted_generator.generate_hypo(repacked_inputs) self.assertIsNotNone(scripted_preds)