def test_detokenizing_with_no_tokenizer_fails(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) with self.assertRaises(ValueError): sm.detokenize("hello world")
def test_run(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) inp = [{ "src": "hello how are you today" }, { "src": "good morning to you ." }] results, scores, n_best, time, aligns = sm.run(inp) self.assertIsInstance(results, list) for sentence_string in results: self.assertIsInstance(sentence_string, string_types) self.assertIsInstance(scores, list) for elem in scores: self.assertIsInstance(elem, float) self.assertIsInstance(aligns, list) for align_list in aligns: for align_string in align_list: if align_string is not None: self.assertIsInstance(align_string, string_types) self.assertEqual(len(results), len(scores)) self.assertEqual(len(scores), len(inp) * n_best) self.assertEqual(len(time), 1) self.assertIsInstance(time, dict) self.assertIn("translation", time)
def test_load_model_on_init_and_unload(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) self.assertTrue(sm.loaded) self.assertIsInstance(sm.translator, Translator) sm.unload() self.assertFalse(sm.loaded)
def test_moving_to_gpu_and_back(self): torch.cuda.set_device(torch.device("cuda", 0)) model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cpu") sm.to_gpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cuda") self.assertEqual(p.device.index, 0) sm.to_cpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cpu")
def test_run(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) inp = [{"src": "hello how are you today"}, {"src": "good morning to you ."}] results, scores, n_best, time = sm.run(inp) self.assertIsInstance(results, list) for sentence_string in results: self.assertIsInstance(sentence_string, string_types) self.assertIsInstance(scores, list) for elem in scores: self.assertIsInstance(elem, float) self.assertEqual(len(results), len(scores)) self.assertEqual(len(scores), len(inp)) self.assertEqual(n_best, 1) self.assertEqual(len(time), 1) self.assertIsInstance(time, dict) self.assertIn("translation", time)
def test_nbest_init_fails(self): model_id = 0 opt = {"models": ["test_model.pt"], "n_best": 2} model_root = TEST_DIR with self.assertRaises(ValueError): ServerModel(opt, model_id, model_root=model_root, load=True)