-
Notifications
You must be signed in to change notification settings - Fork 22
dynamic scoring for LM #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9c51d9d
47dbd8c
769b9b8
b9fa354
45e563e
4b76ffb
25afba6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,9 @@ def tokenize_string(self, string, side="src", is_train=False): | |
| kwargs = {"max_length": self.max_length, "truncation": True} | ||
| else: | ||
| kwargs = {} | ||
| string = string.replace(DefaultTokens.SEP, "\n").replace( | ||
| DefaultTokens.MASK_BEFORE, self.tokenizers[side].pad_token | ||
| ) | ||
|
Contributor
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. shouldn't we handle it the same way for other tokenizers ?
Contributor
Author
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. I have only tested for that one (with eurollm) at this point; an error will be raised with the others. |
||
| tokens = self.tokenizers[side].encode(string, **kwargs) | ||
| return tokens | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import codecs | ||
| import os | ||
| from eole.predict import GNMTGlobalScorer, Translator | ||
| from eole.predict import GNMTGlobalScorer, Translator, GeneratorLM | ||
| from eole.config.run import ( | ||
| PredictConfig, | ||
| ) # probably should be done differently, but might work for now | ||
|
|
@@ -42,58 +42,94 @@ def translate(self, model, gpu_rank, step): | |
| preds (list): Detokenized predictions | ||
| texts_ref (list): Detokenized target sentences | ||
| """ | ||
| # ########## # | ||
| # Translator # | ||
| # ########## # | ||
| # ######### # | ||
| # Predictor # | ||
| # ######### # | ||
|
|
||
| # Build translator from options | ||
| # Build predictor from options | ||
| model_config = self.config.model | ||
| model_config._validate_model_config() | ||
|
|
||
| # This is somewhat broken and we shall remove or improve | ||
| # (take 'inference' field of config if exists?) | ||
| # Set "default" translation options on empty cfgfile | ||
| self.config.training.num_workers = 0 | ||
| is_seq2seq = model.encoder is not None and model.decoder is not None | ||
| if not is_seq2seq: | ||
| if "insert_mask_before_placeholder" in self.config.transforms: | ||
| self.response_patterns = self.config.transforms_configs.insert_mask_before_placeholder.response_patterns | ||
| else: | ||
| self.response_patterns = None | ||
|
|
||
| predict_config = PredictConfig( | ||
| model_path=["dummy"], | ||
| src=self.config.data["valid"].path_src, | ||
| src="dummy", | ||
vince62s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| compute_dtype=self.config.training.compute_dtype, | ||
| beam_size=1, | ||
| transforms=self.config.transforms, | ||
| transforms_configs=self.config.transforms_configs, | ||
| model=model_config, | ||
| tgt_file_prefix=self.config.transforms_configs.prefix.tgt_prefix != "", | ||
| gpu_ranks=[gpu_rank], | ||
| batch_type=self.config.training.batch_type, | ||
| batch_size=self.config.training.batch_size, | ||
| ) | ||
|
|
||
| scorer = GNMTGlobalScorer.from_config(predict_config) | ||
| translator = Translator.from_config( # we need to review opt/config stuff in translator | ||
| model, | ||
| self.vocabs, | ||
| predict_config, | ||
| model_config, | ||
| device_id=gpu_rank, | ||
| global_scorer=scorer, | ||
| report_align=predict_config.report_align, | ||
| report_score=False, | ||
| logger=None, | ||
| ) | ||
|
|
||
| if is_seq2seq: | ||
| predictor = Translator.from_config( # we need to review opt/config stuff in translator | ||
| model, | ||
| self.vocabs, | ||
| predict_config, | ||
| model_config, | ||
| device_id=gpu_rank, | ||
| global_scorer=scorer, | ||
| report_align=predict_config.report_align, | ||
| report_score=False, | ||
| logger=None, | ||
| ) | ||
| else: | ||
| predictor = GeneratorLM.from_config( | ||
| model, | ||
| self.vocabs, | ||
| predict_config, | ||
| model_config, | ||
| device_id=gpu_rank, | ||
| global_scorer=scorer, | ||
| report_align=predict_config.report_align, | ||
| report_score=False, | ||
| logger=None, | ||
| ) | ||
|
Comment on lines
+80
to
+103
Member
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. Maybe cleaner to just define a |
||
|
|
||
| # ################### # | ||
| # Validation iterator # | ||
| # ################### # | ||
|
|
||
| # Reinstantiate the validation iterator | ||
| # Retrieve raw references and sources | ||
| with codecs.open(self.config.data["valid"].path_tgt, "r", encoding="utf-8") as f: | ||
| raw_refs = [line.strip("\n") for line in f if line.strip("\n")] | ||
| with codecs.open(self.config.data["valid"].path_src, "r", encoding="utf-8") as f: | ||
| raw_srcs = [line.strip("\n") for line in f if line.strip("\n")] | ||
|
|
||
| if not is_seq2seq and self.response_patterns is not None: | ||
| prompts, answers = [], [] | ||
| for i, _raw_src in enumerate(raw_srcs): | ||
| for _pattern in self.response_patterns: | ||
| if len(_raw_src.split(_pattern)) == 2: | ||
| prompt, answer = _raw_src.split(_pattern) | ||
| prompts.append(prompt + _pattern) | ||
| answers.append(answer) | ||
| raw_srcs = prompts | ||
| raw_refs = answers | ||
| else: | ||
| with codecs.open(self.config.data["valid"].path_tgt, "r", encoding="utf-8") as f: | ||
| raw_refs = [line.strip("\n") for line in f if line.strip("\n")] | ||
|
|
||
| infer_iter = build_dynamic_dataset_iter( | ||
| predict_config, | ||
| self.transforms, | ||
| translator.vocabs, | ||
| predictor.vocabs, | ||
| src=raw_srcs, | ||
| task=CorpusTask.INFER, | ||
| tgt="", # This force to clear the target side (needed when using tgt_file_prefix) | ||
| device_id=gpu_rank, | ||
|
|
@@ -102,7 +138,7 @@ def translate(self, model, gpu_rank, step): | |
| # ########### # | ||
| # Predictions # | ||
| # ########### # | ||
| _, _, preds = translator._predict( | ||
| _, _, preds = predictor._predict( | ||
| infer_iter, | ||
| transform=infer_iter.transforms, | ||
| attn_debug=predict_config.attn_debug, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.