-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add RETURNN compute perplexity job #563
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
__all__ = ["ReturnnCalculatePerplexityJob"] | ||
|
||
import shutil | ||
import subprocess as sp | ||
from typing import Union | ||
|
||
from sisyphus import Job, Task, setup_path, tk | ||
|
||
import i6_core.util as util | ||
|
||
from .config import ReturnnConfig | ||
from .training import PtCheckpoint, Checkpoint | ||
|
||
Path = setup_path(__package__) | ||
|
||
|
||
class ReturnnCalculatePerplexityJob(Job): | ||
""" | ||
Calculates the perplexity of a language model trained in RETURNN | ||
on an evaluation data set | ||
""" | ||
|
||
def __init__( | ||
self, | ||
returnn_config: ReturnnConfig, | ||
returnn_model: Union[PtCheckpoint, Checkpoint], | ||
eval_dataset: tk.Path, | ||
*, | ||
log_verbosity: int = 3, | ||
returnn_root: tk.Path, | ||
returnn_python_exe: tk.Path, | ||
): | ||
returnn_config.config.pop("train") | ||
returnn_config.config.pop("dev") | ||
returnn_config.config["eval_datasets"] = {"eval": eval_dataset} | ||
|
||
# TODO verify paths | ||
if isinstance(returnn_model, PtCheckpoint): | ||
model_path = returnn_model.path | ||
self.add_input(returnn_model.path) | ||
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 think it should already be input to the Job as we pass the PT/Checkpoint object to the Job. 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. Are you sure?? 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.
never without actually testing ;) but very confident because:
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. ah yes. thanks for the explanation :) |
||
elif isinstance(returnn_model, Checkpoint): | ||
model_path = returnn_model.index_path | ||
self.add_input(returnn_model.index_path) | ||
else: | ||
raise NotImplementedError(f"returnn model has unknown type: {type(returnn_model)}") | ||
|
||
returnn_config.config["model"] = model_path | ||
|
||
returnn_config.post_config["log_verbosity"] = log_verbosity | ||
|
||
self.returnn_config = returnn_config | ||
|
||
self.returnn_python_exe = returnn_python_exe | ||
self.returnn_root = returnn_root | ||
|
||
self.out_returnn_config_file = self.output_path("returnn.config") | ||
self.out_returnn_log = self.output_path("returnn.log") | ||
self.out_perplexities = self.output_var("ppl_score") | ||
|
||
self.rqmt = {"gpu": 0, "cpu": 2, "mem": 4, "time": 4} | ||
|
||
def tasks(self): | ||
yield Task("create_files", mini_task=True) | ||
yield Task("run", resume="run", rqmt=self.rqmt) | ||
yield Task("gather", mini_task=True) | ||
|
||
def _get_run_cmd(self): | ||
run_cmd = [ | ||
self.returnn_python_exe.get_path(), | ||
self.returnn_root.join_right("rnn.py").get_path(), | ||
self.out_returnn_config_file.get_path(), | ||
"++task eval", | ||
] | ||
return run_cmd | ||
|
||
def create_files(self): | ||
self.returnn_config.write(self.out_returnn_config_file.get_path()) | ||
|
||
util.create_executable("rnn.sh", self._get_run_cmd()) | ||
|
||
def run(self): | ||
sp.check_call(self._get_run_cmd()) | ||
|
||
shutil.move("returnn_log", self.out_returnn_log.get_path()) | ||
|
||
def gather(self): | ||
for data_key in self.out_perplexities.keys(): | ||
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. Thanks I think that is from an earlier version... |
||
print(data_key) | ||
|
||
@classmethod | ||
def hash(cls, parsed_args): | ||
del parsed_args["log_verbosity"] | ||
return super().hash(parsed_args) |
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.
in principle any dataset is valid? Actually, does
eval_datasets = {"eval": "/path/to/file"}
constitute a correct dataset definition?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.
I have not tested if any dataset is valid. I am assuming that any dataset with text should be valid.. but this should be used with
LmDataset
.No that is not the correct dataset definition. It needs to be something like:
{"eval": "class": "LmDatset", ...}