This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[BlenderBot2] A hybrid mode (skip search when needed) #4221
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a4125c2
hybrid
ffeddb1
lint
2dd81be
the fid search agent
mojtaba-komeili 7f14a17
the added teacher with skip search
mojtaba-komeili 463cbe2
reformat
mojtaba-komeili f1140aa
renamed the skip search param + nits
mojtaba-komeili 158a121
fix task test failures
f3166e4
ci config
f0a5075
del 1 test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,14 @@ | |
The Memory Decoder examines the context and generates memories to write to | ||
the long-term memory module. | ||
""" | ||
import copy | ||
import torch | ||
import torch.nn | ||
import torch.nn.functional as F | ||
from typing import Union, Dict, List, Tuple, Optional, Any | ||
|
||
from parlai.agents.fid.fid import FidAgent, WizIntGoldDocRetrieverFiDAgent | ||
from parlai.agents.rag.args import DPR_ZOO_MODEL, QUERY_MODEL_TYPES | ||
from parlai.agents.rag.args import DPR_ZOO_MODEL, QUERY_MODEL_TYPES, RetrieverType | ||
from parlai.agents.rag.rag import RagAgent | ||
from parlai.agents.rag.model_types import ( | ||
RagTurn, | ||
|
@@ -37,8 +38,10 @@ | |
SELECTED_DOCS_TITLES, | ||
SELECTED_SENTENCES, | ||
NO_SELECTED_DOCS_TOKEN, | ||
SKIP_SEARCH, | ||
) | ||
from parlai.utils.torch import padded_3d | ||
from parlai.utils.typing import TShared | ||
|
||
from .modules import ( | ||
BlenderBot2RagModel, | ||
|
@@ -99,6 +102,7 @@ def augment_batch_for_generation( | |
batch.num_gold_docs, | ||
batch.memory_decoder_vec, | ||
batch.num_memory_decoder_vecs, | ||
batch.skip_search, | ||
) | ||
doc_log_probs = F.log_softmax(doc_scores, dim=1) | ||
batch.src_text_vec = batch.text_vec | ||
|
@@ -222,6 +226,12 @@ def add_cmdline_args( | |
default=SELECTED_DOCS_TITLES, | ||
help='Field for selected docs titles.', | ||
) | ||
bb2_group.add_argument( | ||
'--skip-search-key', | ||
type=str, | ||
default=SKIP_SEARCH, | ||
help='Field for whether to skip search or not.', | ||
) | ||
bb2_group.add_argument( | ||
'--insert-gold-docs', | ||
type='bool', | ||
|
@@ -700,6 +710,7 @@ def batchify(self, obs_batch: List[Message], sort: bool = False) -> Batch: | |
batch.num_gold_docs = None | ||
batch.memory_decoder_vec = None | ||
batch.num_memory_decoder_vecs = None | ||
batch.skip_search = None | ||
if any(ex.get('memory_vec') is not None for ex in valid_exs): | ||
batch = self._set_batch_memory_vec(valid_exs, batch) | ||
if any(ex.get('query_generator_vec') is not None for ex in valid_exs): | ||
|
@@ -708,6 +719,8 @@ def batchify(self, obs_batch: List[Message], sort: bool = False) -> Batch: | |
batch = self._set_batch_gold_doc_vec(valid_exs, batch) | ||
if any(ex.get('memory_decoder_vec') is not None for ex in valid_exs): | ||
batch = self._set_batch_memory_decoder_vec(valid_exs, batch) | ||
if any(ex.get(self.opt['skip_search_key']) is not None for ex in valid_exs): | ||
batch = self._set_batch_skip_search(valid_exs, batch) | ||
return batch | ||
|
||
def _set_batch_memory_vec(self, valid_exs: List[Message], batch: Batch) -> Batch: | ||
|
@@ -780,6 +793,11 @@ def _set_batch_memory_decoder_vec( | |
batch.num_memory_decoder_vecs = torch.LongTensor(num_memory_dec_toks) | ||
return batch | ||
|
||
def _set_batch_skip_search(self, valid_exs: List[Message], batch: Batch) -> Batch: | ||
skip_search = [ex.get(self.opt['skip_search_key'], False) for ex in valid_exs] | ||
batch.skip_search = torch.BoolTensor(skip_search) | ||
return batch | ||
|
||
def eval_step(self, batch): | ||
output = super().eval_step(batch) | ||
if output is None or not hasattr(self.model, 'retriever'): | ||
|
@@ -807,6 +825,7 @@ def _model_input( | |
torch.LongTensor, | ||
torch.LongTensor, | ||
torch.LongTensor, | ||
torch.BoolTensor, | ||
]: | ||
""" | ||
Override RagAgent._model_input to include several more input vectors. | ||
|
@@ -826,6 +845,7 @@ def _model_input( | |
batch.num_gold_docs, | ||
batch.memory_decoder_vec, | ||
batch.num_memory_decoder_vecs, | ||
batch.skip_search, | ||
) | ||
|
||
def compute_loss( | ||
|
@@ -894,6 +914,13 @@ def build_model(self) -> Union[BlenderBot2FidModel, T5BlenderBot2FidModel]: | |
return model | ||
|
||
|
||
class BlenderBot2SearchQueryFiDAgent(BlenderBot2FidAgent): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
def __init__(self, opt: Opt, shared: TShared = None): | ||
opt = copy.deepcopy(opt) | ||
opt['rag_retriever_type'] = RetrieverType.SEARCH_ENGINE.value | ||
super().__init__(opt, shared=shared) | ||
|
||
|
||
class BlenderBot2WizIntGoldDocRetrieverFiDAgent( | ||
WizIntGoldDocRetrieverFiDAgent, BlenderBot2FidAgent | ||
): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,7 +181,7 @@ def __init__(self, opt: Opt): | |
) | ||
assert isinstance(base_agent, TorchAgent) | ||
self.agents = [base_agent] | ||
bsz = max(opt.get('batchsize', 1), opt.get('eval_batchsize', 1)) | ||
bsz = max(opt.get('batchsize') or 1, opt.get('eval_batchsize') or 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm interesting, I thought anything return from opt.get(XXX, 1) would never be None There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it can be |
||
rag_turn_n_turns = opt.get('rag_turn_n_turns', 1) | ||
if bsz > 1 or rag_turn_n_turns > 1: | ||
self.agents += [ | ||
|
@@ -196,6 +196,7 @@ def classify_retrieval( | |
input: torch.LongTensor, | ||
num_memories: torch.LongTensor, | ||
generated_memories: Optional[List[List[str]]], | ||
skip_search: Optional[torch.BoolTensor], | ||
) -> Tuple[torch.LongTensor, List[str]]: | ||
""" | ||
Classify input and get retrieval type. | ||
|
@@ -242,6 +243,8 @@ def classify_retrieval( | |
self.retrieval_type[i] = RetrievalType.MEMORY.value | ||
elif strip_punc(s) in NONE_STRINGS + MEMORY_STRINGS: | ||
self.retrieval_type[i] = RetrievalType.NONE.value | ||
elif skip_search is not None and skip_search[i]: | ||
self.retrieval_type[i] = RetrievalType.NONE.value | ||
else: | ||
self.retrieval_type[i] = RetrievalType.SEARCH.value | ||
searches.append(s) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah thanks for adding this, otherwise the
train_model
always flags this red warning ;)