-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmlm_training.py
More file actions
1405 lines (1173 loc) · 56.9 KB
/
mlm_training.py
File metadata and controls
1405 lines (1173 loc) · 56.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Copyright (c) 2018, salesforce.com, inc.
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
Experiment Portal.
"""
import argparse
import json
import logging
import os
from typing import List, Tuple, Dict, Any, DefaultDict
import debugpy
import sys
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
from torch.utils.tensorboard import SummaryWriter
import wandb
from rich import traceback
from torch import nn
from tqdm import tqdm
from transformers import (
AutoModel,
AutoTokenizer,
BartConfig,
BertModel,
PreTrainedTokenizer,
)
import multihopkg.data_utils as data_utils
import multihopkg.utils_debug.distribution_tracker as dist_tracker
from multihopkg.environments import Observation
from multihopkg.exogenous.sun_models import KGEModel, get_embeddings_from_indices
from multihopkg.models_language.classical import HunchBart, collate_token_ids_batch
from multihopkg.logging import setup_logger
from multihopkg.rl.graph_search.cpg import ContinuousPolicyGradient
from multihopkg.rl.graph_search.pn import ITLGraphEnvironment
from multihopkg.run_configs import alpha
from multihopkg.run_configs.common import overload_parse_defaults_with_yaml
from multihopkg.utils.convenience import tensor_normalization
from multihopkg.utils.setup import set_seeds
from multihopkg.vector_search import ANN_IndexMan, ANN_IndexMan_pRotatE
from multihopkg.logs import torch_module_logging
from multihopkg.utils.wandb import histogram_all_modules
from multihopkg.utils_debug.dump_evals import dump_evaluation_metrics
# PCA
from sklearn.decomposition import PCA
import io
from PIL import Image
traceback.install()
wandb_run = None
def initialize_model_directory(args, random_seed=None):
# add model parameter info to model directory
# TODO: We might2ant our implementation of something like this later
raise NotImplementedError
def initial_setup() -> Tuple[argparse.Namespace, PreTrainedTokenizer, PreTrainedTokenizer, logging.Logger]:
global logger
args = alpha.get_args()
args = overload_parse_defaults_with_yaml(args.preferred_config, args)
set_seeds(args.seed)
logger = setup_logger("__MLM__")
# Get Tokenizer
question_tokenizer = AutoTokenizer.from_pretrained(args.question_tokenizer_name)
answer_tokenizer = AutoTokenizer.from_pretrained(args.answer_tokenizer_name)
assert isinstance(args, argparse.Namespace)
return args, question_tokenizer, answer_tokenizer, logger
def prep_questions(questions: List[torch.Tensor], model: BertModel):
embedded_questions = model(questions)
return embedded_questions
def batch_loop_dev(
env: ITLGraphEnvironment,
mini_batch: pd.DataFrame, # Perhaps change this ?
nav_agent: ContinuousPolicyGradient,
hunch_llm: nn.Module,
steps_in_episode: int,
pad_token_id: int,
) -> Tuple[torch.Tensor, Dict[str, Any]]:
"""
Executes a batch loop for the development set to compute additional evaluation metrics.
This function is similar to `batch_loop` but focuses on collecting metrics for debugging
and analysis during development.
During the batch loop:
- The navigation agent (`nav_agent`) interacts with the environment (`env`) to take actions inside `rollout`.
- Rewards are computed from both the language model (`hunch_llm`) and the knowledge graph environment (KGE).
- Evaluation metrics are collected for analysis.
This function is found within `evaluate_training` calls upon `rollout`.
Args:
env (ITLGraphEnvironment):
The knowledge graph environment that provides observations, rewards, and state transitions.
mini_batch (pd.DataFrame):
A batch of data containing questions, answers, relevant entities, and relations.
nav_agent (ContinuousPolicyGradient):
The policy network responsible for deciding actions based on the current state.
hunch_llm (nn.Module):
A language model used to compute rewards based on how well the agent's state aligns with the expected answers.
steps_in_episode (int):
The number of steps to execute in each episode.
pad_token_id (int):
The token ID used for padding sequences in the answer IDs.
Returns:
- `pg_loss` (torch.Tensor):
The policy gradient loss computed for the batch.
- `eval_extras` (Dict[str, Any]):
A dictionary containing additional evaluation metrics collected during the batch loop.
Notes:
- This function is specifically designed for development and debugging purposes.
- Rewards are normalized for stability before being used to compute the policy gradient loss.
"""
########################################
# Start the batch loop with zero grad
########################################
nav_agent.zero_grad()
device = next(nav_agent.parameters()).device
# Deconstruct the batch
questions = mini_batch["Question"].tolist()
answers = mini_batch["Answer"].tolist()
query_ent = mini_batch["Query-Entity"].tolist()
query_rel = mini_batch["Query-Relation"].tolist()
answer_id = mini_batch["Answer-Entity"].tolist()
# question_embeddings = env.get_llm_embeddings(questions, device)
if env.use_kge_question_embedding:
question_embeddings = env.get_kge_question_embedding(query_ent, query_rel, device) # Shape: (batch, 2*embedding_dim)
else:
question_embeddings = env.get_llm_embeddings(questions, device)
answer_ids_padded_tensor = collate_token_ids_batch(answers, pad_token_id).to(torch.int32).to(device)
pad_mask = answer_ids_padded_tensor.ne(pad_token_id)
logger.warning(f"About to go into rollout")
log_probs, entropies, llm_rewards, kg_rewards, eval_extras = rollout(
steps_in_episode,
nav_agent,
hunch_llm,
env,
question_embeddings,
answer_ids_padded_tensor,
query_ent = query_ent,
query_rel = query_rel,
answer_id = answer_id,
dev_mode=True,
)
########################################
# Calculate Reinforce Objective
########################################
'LLM Rewards'
llm_rewards_t = (
torch.stack(llm_rewards)
).permute(1,0,2)
assert not torch.isnan(llm_rewards_t).any(), "NaN detected in the llm rewards (batch_loop_dev). Aborting training."
# Get only masked, then mean
llm_rewards_t_unpacked = []
for i, reward_batch_element in enumerate(llm_rewards_t):
mask_for_element = pad_mask[i][1:].unsqueeze(0).repeat(steps_in_episode,1)
filtered_rewards = reward_batch_element[mask_for_element].reshape(steps_in_episode, -1)
mean_reward = torch.mean(filtered_rewards, dim=-1)
llm_rewards_t_unpacked.append(mean_reward)
llm_rewards_t = torch.stack(llm_rewards_t_unpacked)
log_probs_t = torch.stack(log_probs).T
entropies_t = torch.stack(entropies).T
num_steps = log_probs_t.shape[-1]
assert not torch.isnan(log_probs_t).any(), "NaN detected in the log probs (batch_loop_dev). Aborting training."
# TODO: Check if this is not bad.
llm_rewards_t = llm_rewards_t.expand_as(log_probs_t) # TOREM: This is a hack to make the shapes match
#-------------------------------------------------------------------------
'Knowledge Graph Environment Rewards'
kg_rewards_t = (
torch.stack(kg_rewards)
).permute(1,0,2) # Correcting to Shape: (batch_size, num_steps, reward_type)
kg_rewards_t = kg_rewards_t.squeeze(2) # Shape: (batch_size, num_steps)
assert not torch.isnan(kg_rewards_t).any(), "NaN detected in the kg rewards (batch_loop_dev). Aborting training."
#-------------------------------------------------------------------------
'Discount and Merging of Rewards'
# TODO: Check if a weight is needed for combining the rewards
gamma = nav_agent.gamma
discounted_rewards = torch.zeros_like(llm_rewards_t).to(device) # Shape: (batch_size, num_steps)
G = torch.zeros_like(llm_rewards_t[:, 0]).to(device) # Shape: (batch_size, num_steps)
for t in reversed(range(kg_rewards_t.size(1))):
G = (llm_rewards_t[:, t] + kg_rewards_t[:, t]) + gamma * G
discounted_rewards[:, t] = G
# discounted_rewards[:,-1] = llm_rewards_t[:,-1] + kg_rewards_t[:,-1]
# for t in reversed(range(num_steps - 1)):
# discounted_rewards[:,t] += gamma * (llm_rewards_t[:,t + 1] + kg_rewards_t[:,t + 1])
# Sample-wise normalization of the rewards for stability
# discounted_rewards = (discounted_rewards - discounted_rewards.mean(axis=-1)[:, torch.newaxis]) / (discounted_rewards.std(axis=-1)[:, torch.newaxis] + 1e-8)
#--------------------------------------------------------------------------
'Loss Calculation'
pg_loss = -(discounted_rewards * log_probs_t) - nav_agent.beta * entropies_t # Have to negate it into order to do gradient ascent
logger.warning(f"We just left dev rollout")
return pg_loss, eval_extras
def batch_loop(
env: ITLGraphEnvironment,
mini_batch: pd.DataFrame, # Perhaps change this ?
nav_agent: ContinuousPolicyGradient,
hunch_llm: nn.Module,
steps_in_episode: int,
bos_token_id: int,
eos_token_id: int,
pad_token_id: int,
) -> Tuple[torch.Tensor, Dict[str, Any]]:
"""
Executes a batch loop for training the navigation agent and language model.
This function performs reinforcement learning (RL) rollouts for a batch of data
and computes the policy gradient loss for training.
During the batch loop:
- The navigation agent (`nav_agent`) interacts with the environment (`env`) to take actions inside `rollout`.
- Rewards are computed from both the language model (`hunch_llm`) and the knowledge graph environment (KGE).
- The policy gradient loss is calculated based on the rewards and log probabilities of actions.
This function is found within `train_multihokg` and calls upon `rollout`.
Args:
env (ITLGraphEnvironment):
The knowledge graph environment that provides observations, rewards, and state transitions.
mini_batch (pd.DataFrame):
A batch of data containing questions, answers, relevant entities, and relations.
nav_agent (ContinuousPolicyGradient):
The policy network responsible for deciding actions based on the current state.
hunch_llm (nn.Module):
A language model used to compute rewards based on how well the agent's state aligns with the expected answers.
steps_in_episode (int):
The number of steps to execute in each episode.
bos_token_id (int):
The token ID representing the beginning of a sequence in the answer IDs.
eos_token_id (int):
The token ID representing the end of a sequence in the answer IDs.
pad_token_id (int):
The token ID used for padding sequences in the answer IDs.
Returns:
- `pg_loss` (torch.Tensor):
The policy gradient loss computed for the batch.
- `eval_extras` (Dict[str, Any]):
A dictionary containing additional evaluation metrics collected during the batch loop.
Notes:
- Rewards are normalized for stability before being used to compute the policy gradient loss.
- This function is designed for training and does not collect as many metrics as `batch_loop_dev`.
"""
########################################
# Start the batch loop with zero grad
########################################
nav_agent.zero_grad()
device = next(nav_agent.parameters()).device
# Deconstruct the batch
questions = mini_batch["Question"].tolist()
answers = mini_batch["Answer"].tolist()
query_ent = mini_batch["Query-Entity"].tolist()
query_rel = mini_batch["Query-Relation"].tolist()
answer_id = mini_batch["Answer-Entity"].tolist()
# question_embeddings = env.get_llm_embeddings(questions, device)
if env.use_kge_question_embedding:
question_embeddings = env.get_kge_question_embedding(query_ent, query_rel, device) # Shape: (batch, 2*embedding_dim)
else:
question_embeddings = env.get_llm_embeddings(questions, device)
answer_ids_padded_tensor = collate_token_ids_batch(answers, pad_token_id).to(torch.int32).to(device)
pad_mask = answer_ids_padded_tensor.ne(pad_token_id)
log_probs, entropies, llm_rewards, kg_rewards, eval_extras = rollout(
steps_in_episode,
nav_agent,
hunch_llm,
env,
question_embeddings,
answer_ids_padded_tensor,
query_ent = query_ent,
query_rel = query_rel,
answer_id = answer_id,
)
########################################
# Calculate Reinforce Objective
########################################
logger.debug("About to calculate rewards")
#-------------------------------------------------------------------------
'LLM Rewards'
llm_rewards_t = (
torch.stack(llm_rewards)
).permute(1,0,2)
# Get only masked, then mean
llm_rewards_t_unpacked = []
for i, reward_batch_element in enumerate(llm_rewards_t):
mask_for_element = pad_mask[i][1:].unsqueeze(0).repeat(steps_in_episode,1)
filtered_rewards = reward_batch_element[mask_for_element].reshape(steps_in_episode, -1)
mean_reward = torch.mean(filtered_rewards, dim=-1)
llm_rewards_t_unpacked.append(mean_reward)
llm_rewards_t = torch.stack(llm_rewards_t_unpacked)
log_probs_t = torch.stack(log_probs).T
entropies_t = torch.stack(entropies).T
num_steps = log_probs_t.shape[-1]
# TODO: Check if this is not bad.
llm_rewards_t = llm_rewards_t.expand_as(log_probs_t) # TOREM: This is a hack to make the shapes match
#-------------------------------------------------------------------------
'Knowledge Graph Environment Rewards'
kg_rewards_t = (
torch.stack(kg_rewards)
).permute(1,0,2) # Correcting to Shape: (batch_size, num_steps, reward_type)
kg_rewards_t = kg_rewards_t.squeeze(2) # Shape: (batch_size, num_steps)
#-------------------------------------------------------------------------
'Discount and Merging of Rewards'
# TODO: Check if a weight is needed for combining the rewards
gamma = nav_agent.gamma
discounted_rewards = torch.zeros_like(llm_rewards_t).to(device) # Shape: (batch_size, num_steps)
G = torch.zeros_like(llm_rewards_t[:, 0]).to(device) # Shape: (batch_size, num_steps)
for t in reversed(range(kg_rewards_t.size(1))):
G = (llm_rewards_t[:, t] + kg_rewards_t[:, t]) + gamma * G
discounted_rewards[:, t] = G
# discounted_rewards[:,-1] = llm_rewards_t[:,-1] + kg_rewards_t[:,-1]
# for t in reversed(range(num_steps - 1)):
# discounted_rewards[:,t] += gamma * (llm_rewards_t[:,t + 1] + kg_rewards_t[:,t + 1])
# Sample-wise normalization of the rewards for stability
# discounted_rewards = (discounted_rewards - discounted_rewards.mean(axis=-1)[:, torch.newaxis]) / (discounted_rewards.std(axis=-1)[:, torch.newaxis] + 1e-8)
#--------------------------------------------------------------------------
'Loss Calculation'
pg_loss = -(discounted_rewards * log_probs_t) - nav_agent.beta * entropies_t # Have to negate it into order to do gradient ascent
return pg_loss, eval_extras
def evaluate_training(
env: ITLGraphEnvironment,
dev_df: pd.DataFrame,
nav_agent: ContinuousPolicyGradient,
hunch_llm: nn.Module,
steps_in_episode: int,
batch_size_dev: int,
batch_count: int,
verbose: bool,
visualize: bool,
writer: SummaryWriter,
question_tokenizer: PreTrainedTokenizer,
answer_tokenizer: PreTrainedTokenizer,
wandb_on: bool,
iteration: int,
timestamp: str,
):
"""
Evaluates the performance of the navigation agent and language model on the development set.
This function computes evaluation metrics, logs results, and optionally visualizes the evaluation process.
This function is found within `train_multihopkg` and is called periodically during training.
This function calls upon `batch_loop_dev` and `dump_evaluation_metrics`.
Args:
env (ITLGraphEnvironment):
The knowledge graph environment that provides observations, rewards, and state transitions.
dev_df (pd.DataFrame):
The development dataset containing questions, answers, relevant entities, and relations.
nav_agent (ContinuousPolicyGradient):
The policy network responsible for deciding actions based on the current state.
hunch_llm (nn.Module):
A language model used to compute rewards based on how well the agent's state aligns with the expected answers.
steps_in_episode (int):
The number of steps to execute in each episode.
batch_size_dev (int):
The batch size for the development set.
batch_count (int):
The current batch count during training.
verbose (bool):
If `True`, additional information is logged for debugging purposes.
visualize (bool):
If `True`, visualizations of the evaluation process are generated.
writer (SummaryWriter):
A TensorBoard writer for logging metrics and visualizations.
question_tokenizer (PreTrainedTokenizer):
The tokenizer used for processing questions.
answer_tokenizer (PreTrainedTokenizer):
The tokenizer used for processing answers.
wandb_on (bool):
If `True`, logs metrics to Weights & Biases (wandb).
iteration (int):
The current iteration number, used for logging and tracking progress.
answer_id (List[int], optional):
A list of IDs corresponding to the correct answer entities. Defaults to `None`.
Returns:
None
Notes:
- This function evaluates only the last batch of the development set.
- Metrics are logged to TensorBoard and optionally to wandb.
- The function ensures that the environment and models are in evaluation mode during the process.
"""
num_batches = len(dev_df) // batch_size_dev
nav_agent.eval()
hunch_llm.eval()
env.eval()
# env.question_embedding_module.eval()
assert (
not env.question_embedding_module.training
), "The question embedding module must not be in training mode"
batch_cumulative_metrics = {
"dev/batch_count": [batch_count],
"dev/pg_loss": [],
} # For storing results from all batches
current_evaluations = (
{}
) # For storing results from last batch. Otherwise too much info
with torch.no_grad():
# We will only evaluate on the last batch
batch_id = num_batches - 1
mini_batch = dev_df[
batch_id * batch_size_dev : (batch_id + 1) * batch_size_dev
]
if not isinstance( # TODO: Remove this assertion once it is never ever met again
mini_batch, pd.DataFrame
): # For the lsp to give me a break
raise RuntimeError(
f"The mini batch is not a pd.DataFrame, but a {type(mini_batch)}. Please check the data loading code."
)
current_evaluations["reference_questions"] = mini_batch["Question"]
current_evaluations["true_answer"] = mini_batch["Answer"]
current_evaluations["query_entity"] = mini_batch["Query-Entity"]
current_evaluations["query_relation"] = mini_batch["Query-Relation"]
current_evaluations["true_answer_id"] = mini_batch["Answer-Entity"]
# Get the Metrics
bos_token_id = answer_tokenizer.bos_token_id
eos_token_id = answer_tokenizer.eos_token_id
pad_token_id = answer_tokenizer.pad_token_id
if bos_token_id is None or eos_token_id is None or pad_token_id is None:
raise ValueError("Assumptions Wrong. The answer_tokenizer must have a bos_token_id, eos_token_id and pad_token_id")
pg_loss, eval_extras = batch_loop_dev(
env,
mini_batch,
nav_agent,
hunch_llm,
steps_in_episode,
pad_token_id,
)
'Extract all the variables from eval_extras'
for k, v in eval_extras.items():
current_evaluations[k] = v
# Accumlate the metrics
current_evaluations["pg_loss"] = pg_loss.detach().cpu()
batch_cumulative_metrics["dev/pg_loss"].append(pg_loss.mean().item())
########################################
# Take `current_evaluations` as
# a sample of batches and dump its results
########################################
if verbose and logger:
graph_annotation = []
if env.entity2title:
for i0 in range(len(env.graph_annotation)):
if env.graph_annotation[i0] in env.entity2title.keys():
graph_annotation.append(env.entity2title[env.graph_annotation[i0]])
else:
graph_annotation.append("")
# eval_extras has variables that we need
just_dump_it_here = f"./logs/mlm_{env.knowledge_graph.model_name.lower()}_{timestamp}_evaluation_dumps.log"
answer_id = current_evaluations["true_answer_id"].tolist()
answer_kge_tensor = get_embeddings_from_indices(
env.knowledge_graph.entity_embedding,
torch.tensor(answer_id, dtype=torch.int),
).unsqueeze(1) # Shape: (batch, 1, embedding_dim)
logger.warning(f"About to go into dump_evaluation_metrics")
dump_evaluation_metrics(
path_to_log=just_dump_it_here,
evaluation_metrics_dictionary=current_evaluations,
vector_entity_searcher=env.ann_index_manager_ent,
vector_rel_searcher=env.ann_index_manager_rel,
question_tokenizer=question_tokenizer,
answer_tokenizer=answer_tokenizer,
answer_kge_tensor=answer_kge_tensor,
id2entity=env.id2entity,
id2relations=env.id2relation,
entity2title=env.entity2title,
relation2title=env.relation2title,
kg_model_name=env.knowledge_graph.model_name,
kg_ent_distance_func=env.knowledge_graph.absolute_difference,
kg_rel_denormalize_func=env.knowledge_graph.denormalize_relation,
kg_rel_wrap_func=env.knowledge_graph.wrap_relation,
iteration=iteration,
writer=writer,
wandb_on=wandb_on,
logger=logger,
llm_answered_enabled=True
)
logger.warning(f"We just left dump_evaluation_metrics")
logger.warning(f"Cleaning up the dev dictionaries")
current_evaluations.clear()
eval_extras.clear()
if not mini_batch._is_view: # if a copy was created, delete after usage
del mini_batch
########################################
# Average out all metrics across batches
# The dump to wandb
########################################
"""
for k, v in batch_cumulative_metrics.items():
metric_to_report = 0
if isinstance(v[0],torch.Tensor):
metric_to_report = torch.stack(v).mean()
elif isinstance(v[0], int) or isinstance(v[0], float):
metric_to_report = v[0]
else:
raise ValueError(f"The metric to report is not a tensor or int but rather {type(v[0])}")
if wandb_run is not None:
wandb.log({k: metric_to_report})
logger.debug(f"Metric '{k}' has value {metric_to_report}")
nav_agent.train()
hunch_llm.train()
env.train()
dev_mode = False
logger.info("Done with Evaluation")
"""
# TODO: Implement this
def train_multihopkg(
batch_size: int,
batch_size_dev: int,
epochs: int,
nav_agent: ContinuousPolicyGradient,
hunch_llm: nn.Module,
learning_rate: float,
steps_in_episode: int,
env: ITLGraphEnvironment,
start_epoch: int,
train_data: pd.DataFrame,
test_data: pd.DataFrame,
dev_df: pd.DataFrame,
mbatches_b4_eval: int,
verbose: bool,
visualize: bool,
question_tokenizer: PreTrainedTokenizer,
answer_tokenizer: PreTrainedTokenizer,
track_gradients: bool,
num_batches_till_eval: int,
wandb_on: bool,
):
"""
Trains the navigation agent and language model using reinforcement learning (RL) on a knowledge graph environment.
This function performs training over multiple epochs and evaluates the model periodically on a development set.
During training:
- The navigation agent (`nav_agent`) interacts with the environment (`env`) to take actions in `rollout`.
- Rewards are computed from both the language model (`hunch_llm`) and the knowledge graph environment (KGE) in `batch_loop`.
- The policy gradient loss is calculated and used to update the model parameters.
- Evaluation is performed periodically using the `evaluate_training` function.
This function is found within `main` and is called to initiate the training process.
This function calls upon `batch_loop` and `evaluate_training`.
Args:
batch_size (int):
The batch size for training.
batch_size_dev (int):
The batch size for the development set.
epochs (int):
The total number of epochs to train the model.
nav_agent (ContinuousPolicyGradient):
The policy network responsible for deciding actions based on the current state.
hunch_llm (nn.Module):
A language model used to compute rewards based on how well the agent's state aligns with the expected answers.
learning_rate (float):
The learning rate for the optimizer.
steps_in_episode (int):
The number of steps to execute in each episode.
env (ITLGraphEnvironment):
The knowledge graph environment that provides observations, rewards, and state transitions.
start_epoch (int):
The epoch to start training from (useful for resuming training).
train_data (pd.DataFrame):
The training dataset containing questions, answers, relevant entities, and relations.
dev_df (pd.DataFrame):
The development dataset for periodic evaluation.
mbatches_b4_eval (int):
The number of mini-batches to process before performing evaluation.
verbose (bool):
If `True`, additional information is logged for debugging purposes.
visualize (bool):
If `True`, visualizations of gradients and weights histograms are tracked along with navigation movements.
question_tokenizer (PreTrainedTokenizer):
The tokenizer used for processing questions.
answer_tokenizer (PreTrainedTokenizer):
The tokenizer used for processing answers. Note: Not the same as the question tokenizer.
track_gradients (bool):
If `True`, tracks and logs gradient information for debugging.
num_batches_till_eval (int):
The number of batches to process before inspecting vanishing gradients.
wandb_on (bool):
If `True`, logs metrics to Weights & Biases (wandb).
Returns:
None
Notes:
- The function uses reinforcement learning to train the navigation agent and language model.
- Periodic evaluation is performed using the `evaluate_training` function.
- Metrics and visualizations are logged to TensorBoard and optionally to wandb.
- The function ensures that the environment and models are in training mode during the process.
"""
# TODO: Get the rollout working
# Print Model Parameters + Perhaps some more information
if verbose:
print(
"--------------------------\n" "Model Parameters\n" "--------------------------"
)
for name, param in nav_agent.named_parameters():
print(name, param.numel(), "requires_grad={}".format(param.requires_grad))
for name, param in env.named_parameters():
if param.requires_grad: print(name, param.numel(), "requires_grad={}".format(param.requires_grad))
local_time = time.localtime()
timestamp = time.strftime("%m%d%Y_%H%M%S", local_time)
writer = SummaryWriter(log_dir=f'runs/mlm/{env.knowledge_graph.model_name.lower()}/{timestamp}/')
named_param_map = {param: name for name, param in (list(nav_agent.named_parameters()) + list(env.named_parameters()) + list(hunch_llm.named_parameters()))}
optimizer = torch.optim.Adam( # type: ignore
filter(
lambda p: p.requires_grad,
list(env.concat_projector.parameters()) + list(nav_agent.parameters()) + list(hunch_llm.embedding_translator.parameters())
),
lr=learning_rate
)
modules_to_log: List[nn.Module] = [nav_agent]
# Variable to pass for logging
batch_count = 0
bos_token_id = answer_tokenizer.bos_token_id
eos_token_id = answer_tokenizer.eos_token_id
pad_token_id = answer_tokenizer.pad_token_id
if bos_token_id is None or eos_token_id is None or pad_token_id is None:
raise ValueError("Assumptions Wrong. The answer_tokenize must have a bos_token_id, eos_token_id and pad_token_id")
# Replacement for the hooks
if track_gradients:
grad_logger = torch_module_logging.ModuleSupervisor({
"navigation_agent" : nav_agent,
"hunch_llm" : hunch_llm
})
########################################
# Epoch Loop
########################################
for epoch_id in tqdm(range(epochs), desc="Epoch"):
logger.info("Epoch {}".format(epoch_id))
# TODO: Perhaps evaluate the epochs?
# Set in training mode
nav_agent.train()
##############################
# Batch Loop
##############################
# TODO: update the parameters.
for sample_offset_idx in tqdm(range(0, len(train_data), batch_size), desc="Training Batches", leave=False):
mini_batch = train_data[sample_offset_idx : sample_offset_idx + batch_size]
assert isinstance(
mini_batch, pd.DataFrame
) # For the lsp to give me a break
########################################
# Evaluation
########################################
'For debugging purposes, comment back in if needed'
# if batch_count % mbatches_b4_eval == 0:
# evaluate_training(
# env,
# dev_df,
# nav_agent,
# hunch_llm,
# steps_in_episode,
# batch_size_dev,
# batch_count,
# verbose,
# visualize,
# writer,
# question_tokenizer,
# answer_tokenizer,
# wandb_on,
# iteration = epoch_id * (len(train_data) // batch_size // mbatches_b4_eval) + (batch_count // mbatches_b4_eval),
# timestamp = timestamp,
# )
########################################
# Training
########################################
'Forward pass'
optimizer.zero_grad()
pg_loss, _ = batch_loop(
env, mini_batch, nav_agent, hunch_llm, steps_in_episode, bos_token_id, eos_token_id, pad_token_id
)
if torch.isnan(pg_loss).any():
logger.error("NaN detected in the loss. Aborting training.")
# Logg the mean, std, min, max of the rewards
reinforce_terms_mean = pg_loss.mean()
reinforce_terms_mean_item = reinforce_terms_mean.item()
reinforce_terms_std_item = pg_loss.std().item()
reinforce_terms_min_item = pg_loss.min().item()
reinforce_terms_max_item = pg_loss.max().item()
logger.debug(f"Reinforce terms mean: {reinforce_terms_mean_item}, std: {reinforce_terms_std_item}, min: {reinforce_terms_min_item}, max: {reinforce_terms_max_item}")
# TODO: Uncomment and try: (but comment out the normalization in batch_loop and bacth_loop_dev)
# pg_loss = tensor_normalization(pg_loss)
#---------------------------------
'Backward pass'
logger.debug("Bout to go backwords")
reinforce_terms_mean.backward()
#---------------------------------
'Gradient Tracking'
if sample_offset_idx == 0:
# Ask for the DAG to be dumped
if track_gradients:
grad_logger.dump_visual_dag(destination_path=f"./figures/grads/dag_{epoch_id:02d}.png", figsize=(10, 100)) # type: ignore
if torch.all(nav_agent.mu_layer.weight.grad == 0):
logger.warning("Gradients are zero for mu_layer!")
# Inspecting vanishing gradient
if sample_offset_idx % num_batches_till_eval == 0 and verbose:
# Retrieve named parameters from the optimizer
named_params = [
(named_param_map[param], param)
for group in optimizer.param_groups
for param in group['params']
]
# Wandb hisotram of modules
histograms = histogram_all_modules(modules_to_log, num_buckets=20)
# Report the histograms to wandb
if wandb_on:
for name, histogram in histograms.items():
wandb.log({f"{name}/Histogram": wandb.Histogram(np_histogram=histogram)})
# Iterate and calculate gradients as needed
for name, param in named_params:
if param.requires_grad and ('bias' not in name) and (param.grad is not None):
if name == 'weight': name = 'concat_projector.weight'
grads = param.grad.detach().cpu()
weights = param.detach().cpu()
dist_tracker.write_dist_parameters(grads, name, "Gradient", writer, epoch_id)
dist_tracker.write_dist_parameters(weights, name, "Weights", writer, epoch_id)
if wandb_on:
wandb.log({f"{name}/Gradient": wandb.Histogram(grads.numpy().flatten())})
wandb.log({f"{name}/Weights": wandb.Histogram(weights.numpy().flatten())})
elif visualize:
dist_tracker.write_dist_histogram(
grads.numpy().flatten(),
name,
'g',
"Gradient Histogram",
"Grad Value",
"Frequency",
writer,
epoch_id
)
dist_tracker.write_dist_histogram(
weights.numpy().flatten(),
name,
'b',
"Weights Histogram",
"Weight Value",
"Frequency",
writer,
epoch_id
)
if wandb_on:
loss_item = pg_loss.mean().item()
logger.info(f"Submitting train/pg_loss: {loss_item} to wandb")
wandb.log({"train/pg_loss": loss_item})
#---------------------------------
'Optimizer step'
optimizer.step()
batch_count += 1
'Evaluate at the end of the epoch'
evaluate_training(
env,
dev_df,
nav_agent,
hunch_llm,
steps_in_episode,
batch_size_dev,
batch_count,
verbose,
visualize,
writer,
question_tokenizer,
answer_tokenizer,
wandb_on,
iteration = epoch_id * (len(train_data) // batch_size // mbatches_b4_eval) + (batch_count // mbatches_b4_eval),
timestamp = timestamp,
)
# !TODO: Add evaluation metrics for the model's performance at the end of epoch with dev set
# !TODO: Add evaluation metrics for the model's performance at the end of epoch with test set
# TODO: Remove if unused
def initialize_path(questions: torch.Tensor):
# Questions must be turned into queries
raise NotImplementedError
# TODO: Move function to a separate file
def calculate_llm_reward(
hunch_llm: nn.Module,
obtained_state: torch.Tensor,
answers_ids: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Will take the answers and give an idea of how close we were.
This will of course require us to have a language model that will start giving us the answer.
"""
batch_size = answers_ids.size(0)
seq_max_len = answers_ids.size(1)
hidden_dim = obtained_state.shape[-1]
# From the obtained_state we will try to find an answer
conditioning_labels = answers_ids[:, :-1].contiguous().to(dtype=torch.int64)
teacher_forcing_labels = answers_ids[:, 1:].contiguous().to(dtype=torch.int64)
answers_inf_softmax = hunch_llm(graph_embeddings=obtained_state, decoder_input_ids=conditioning_labels)
_, logits = answers_inf_softmax.loss, answers_inf_softmax.logits
loss_fn = torch.nn.CrossEntropyLoss(reduction="none")
loss = loss_fn(logits.view(-1, logits.shape[-1]), teacher_forcing_labels.view(-1))
# TODO: Perhaps Stabilize the loss. Normalize it or SMTH like that
reward = -loss # We expect this reward function to be concave rather than convex.
# Reshape the reward to the batch size
reward = reward.view(batch_size, -1)
# # Get indices of the max value of the final output
# answers_inf_ids = torch.argmax(logits, dim=-1)
return reward, logits
def rollout(
# TODO: self.mdl should point to (policy network)
steps_in_episode: int,
nav_agent: ContinuousPolicyGradient,
hunch_llm: nn.Module,
env: ITLGraphEnvironment,
questions_embeddings: torch.Tensor,
answers_ids: torch.Tensor,
query_ent: List[int],
query_rel: List[int],
answer_id: List[int],
dev_mode: bool = False,
) -> Tuple[List[torch.Tensor], List[torch.Tensor], Dict[str, Any]]:
"""
Executes reinforcement learning (RL) episode rollouts in parallel for a given number of steps.
This function is the core of the training process, used by both `batch_loop` and `batch_loop_dev`.
During the rollout:
- The navigation agent (`nav_agent`) interacts with the environment (`env`) to take actions.
- Rewards are computed from both the language model (`hunch_llm`) and the knowledge graph environment (KGE).
- Evaluation metrics are optionally collected in development mode (`dev_mode`).
args:
steps_in_episode (int):
The number of steps to execute in each episode.
nav_agent (ContinuousPolicyGradient):
The policy network responsible for deciding actions based on the current state.
hunch_llm (nn.Module):
A language model used to compute rewards based on how well the agent's state aligns with the expected answers.
env (ITLGraphEnvironment):
The knowledge graph environment that provides observations, rewards, and state transitions.
questions_embeddings (torch.Tensor):
Pre-embedded representations of the questions to be answered. Shape: (batch_size, embedding_dim).
answers_ids (torch.Tensor):
Tokenized IDs of the correct answers. Shape: (batch_size, sequence_length).
relevant_entities (List[List[int]]):
A list of relevant entities for each question, represented as lists of entity IDs.
relevant_rels (List[List[int]]):
A list of relevant relations for each question, represented as lists of relation IDs.
answer_id (List[int]):
A list of IDs corresponding to the correct answer entities.
dev_mode (bool, optional):
If `True`, additional evaluation metrics are collected for debugging or analysis. Defaults to `False`.
returns:
- log_action_probs (List[torch.Tensor]):
A list of log probabilities of the actions taken by the navigation agent at each step.
- llm_rewards (List[torch.Tensor]):
A list of rewards computed by the language model for each step.
- kg_rewards (List[torch.Tensor]):
A list of rewards computed by the knowledge graph environment for each step.
- eval_metrics (Dict[str, Any]):
A dictionary of evaluation metrics collected during the rollout (only populated if `dev_mode=True`).
"""
assert steps_in_episode > 0