From e8dddb63f929ad9b1f31772a9e17f09f9943bb86 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Fri, 29 Nov 2024 15:07:24 +0100 Subject: [PATCH 1/2] Refactoring before new release --- dicee/evaluator.py | 10 -------- dicee/static_funcs_training.py | 13 ++++++---- dicee/trainer/model_parallelism.py | 2 +- tests/test_regression_model_paralelisim.py | 28 ++++++++++------------ 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/dicee/evaluator.py b/dicee/evaluator.py index 4aed4897..3721e1b7 100644 --- a/dicee/evaluator.py +++ b/dicee/evaluator.py @@ -57,16 +57,6 @@ def vocab_preparation(self, dataset) -> None: else: self.ee_vocab = dataset.ee_vocab.result() - """ - if isinstance(dataset.constraints, tuple): - self.domain_constraints_per_rel, self.range_constraints_per_rel = dataset.constraints - else: - try: - self.domain_constraints_per_rel, self.range_constraints_per_rel = dataset.constraints.result() - except RuntimeError: - print('Domain constraint exception occurred') - """ - self.num_entities = dataset.num_entities self.num_relations = dataset.num_relations self.func_triple_to_bpe_representation = dataset.func_triple_to_bpe_representation diff --git a/dicee/static_funcs_training.py b/dicee/static_funcs_training.py index 6e08797c..f0117131 100644 --- a/dicee/static_funcs_training.py +++ b/dicee/static_funcs_training.py @@ -10,8 +10,10 @@ def make_iterable_verbose(iterable_object, verbose, desc="Default", position=Non return iterable_object -def evaluate_lp(model, triple_idx, num_entities, er_vocab: Dict[Tuple, List], re_vocab: Dict[Tuple, List], - info='Eval Starts', batch_size=128, chunk_size=1000): +def evaluate_lp(model, triple_idx, num_entities:int, + er_vocab: Dict[Tuple, List], + re_vocab: Dict[Tuple, List], + info='Eval Starts', batch_size=1024, chunk_size=1024): """ Evaluate model in a standard link prediction task @@ -20,6 +22,9 @@ def evaluate_lp(model, triple_idx, num_entities, er_vocab: Dict[Tuple, List], re the filtered missing tail entity rank :param model: :param triple_idx: + :param num_entities: + :param er_vocab: + :param re_vocab: :param info: :param batch_size: :param chunk_size: @@ -127,7 +132,7 @@ def evaluate_lp(model, triple_idx, num_entities, er_vocab: Dict[Tuple, List], re ), dim=1) # Predict scores for missing tails - preds_tails = model.forward_triples(x_tails) + preds_tails = model(x_tails) preds_tails = preds_tails.view(batch_size_current, chunk_size_current) predictions_tails[:, chunk_start:chunk_end] = preds_tails del x_tails @@ -140,7 +145,7 @@ def evaluate_lp(model, triple_idx, num_entities, er_vocab: Dict[Tuple, List], re ), dim=1) # Predict scores for missing heads - preds_heads = model.forward_triples(x_heads) + preds_heads = model(x_heads) preds_heads = preds_heads.view(batch_size_current, chunk_size_current) predictions_heads[:, chunk_start:chunk_end] = preds_heads del x_heads diff --git a/dicee/trainer/model_parallelism.py b/dicee/trainer/model_parallelism.py index 8f2eba9b..7c222952 100644 --- a/dicee/trainer/model_parallelism.py +++ b/dicee/trainer/model_parallelism.py @@ -169,7 +169,7 @@ def fit(self, *args, **kwargs): persistent_workers=False) if batch_rt is not None: expected_training_time=batch_rt * len(train_dataloader) * self.attributes.num_epochs - print(f"Exp.Training Runtime: {expected_training_time/60 :.3f} in mins\t|\tBatch Size:{batch_size}\t|\tBatch RT:{batch_rt:.3f}\t|\t # of batches:{len(train_dataloader)}\t|\t# of epochs:{self.attributes.num_epochs}") + # print(f"Exp.Training Runtime: {expected_training_time/60 :.3f} in mins\t|\tBatch Size:{batch_size}\t|\tBatch RT:{batch_rt:.3f}\t|\t # of batches:{len(train_dataloader)}\t|\t# of epochs:{self.attributes.num_epochs}") # () Number of batches to reach a single epoch. num_of_batches = len(train_dataloader) diff --git a/tests/test_regression_model_paralelisim.py b/tests/test_regression_model_paralelisim.py index af41268b..2d7e3788 100644 --- a/tests/test_regression_model_paralelisim.py +++ b/tests/test_regression_model_paralelisim.py @@ -4,28 +4,26 @@ from dicee.config import Namespace import os import torch -class TestRegressionModelParallel: +class TestRegressionTensorParallel: @pytest.mark.filterwarnings('ignore::UserWarning') def test_k_vs_all(self): - # @TODO: - """ if torch.cuda.is_available(): args = Namespace() args.model = 'Keci' - args.trainer = "MP" + args.trainer = "TP" args.scoring_technique = "KvsAll" # 1vsAll, or AllvsAll, or NegSample args.dataset_dir = "KGs/UMLS" - args.path_to_store_single_run = "Keci_UMLS" - args.num_epochs = 100 + # CD: TP currently doesn't work with path_to_store_single_run and eval. + #args.path_to_store_single_run = "Keci_UMLS" + args.optim="Adopt" + args.num_epochs = 10 args.embedding_dim = 32 - args.batch_size = 1024 + args.batch_size = 32 + args.lr=0.1 reports = Execute(args).start() - assert reports["Train"]["MRR"] >= 0.990 - assert reports["Test"]["MRR"] >= 0.810 - write_csv_from_model_parallel(path="Keci_UMLS") - assert os.path.exists("Keci_UMLS/entity_embeddings.csv") - assert os.path.exists("Keci_UMLS/relation_embeddings.csv") + assert reports["Train"]["MRR"] >= 0.60 + assert reports["Test"]["MRR"] >= 0.58 + #assert os.path.exists("Keci_UMLS/entity_embeddings.csv") + #assert os.path.exists("Keci_UMLS/relation_embeddings.csv") - os.system(f'rm -rf Keci_UMLS') - - """ + #os.system(f'rm -rf Keci_UMLS') From 1dc3000949cf7d02391c0df7bb7d6e29907259fe Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Sat, 30 Nov 2024 21:00:33 +0100 Subject: [PATCH 2/2] F841 lint error fixed --- dicee/trainer/model_parallelism.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dicee/trainer/model_parallelism.py b/dicee/trainer/model_parallelism.py index 7c222952..cb78b24d 100644 --- a/dicee/trainer/model_parallelism.py +++ b/dicee/trainer/model_parallelism.py @@ -167,9 +167,9 @@ def fit(self, *args, **kwargs): timeout=0, worker_init_fn=None, persistent_workers=False) - if batch_rt is not None: - expected_training_time=batch_rt * len(train_dataloader) * self.attributes.num_epochs - # print(f"Exp.Training Runtime: {expected_training_time/60 :.3f} in mins\t|\tBatch Size:{batch_size}\t|\tBatch RT:{batch_rt:.3f}\t|\t # of batches:{len(train_dataloader)}\t|\t# of epochs:{self.attributes.num_epochs}") + #if batch_rt is not None: + # expected_training_time=batch_rt * len(train_dataloader) * self.attributes.num_epochs + # print(f"Exp.Training Runtime: {expected_training_time/60 :.3f} in mins\t|\tBatch Size:{batch_size}\t|\tBatch RT:{batch_rt:.3f}\t|\t # of batches:{len(train_dataloader)}\t|\t# of epochs:{self.attributes.num_epochs}") # () Number of batches to reach a single epoch. num_of_batches = len(train_dataloader)