|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import time |
| 4 | +from collections.abc import Sequence |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from docs.doc_modules.experiments import AutoDocExperimentInfo, run_capture_and_save |
| 8 | +from eir.setup.config_setup_modules.config_setup_utils import load_yaml_config |
| 9 | +from eir.utils.logging import get_logger |
| 10 | + |
| 11 | +logger = get_logger(name=__name__) |
| 12 | + |
| 13 | +CONTENT_ROOT = "user_guides" |
| 14 | +TUTORIAL_NAME = "01_streaming_data" |
| 15 | + |
| 16 | + |
| 17 | +def run_with_server(command: list[str]) -> Path: |
| 18 | + server_path = f"docs.doc_modules.{CONTENT_ROOT}.simulation_streamer" |
| 19 | + |
| 20 | + globals_file = next(i for i in command if "globals" in i) |
| 21 | + globals_dict = load_yaml_config(config_path=globals_file) |
| 22 | + run_folder = Path(globals_dict["basic_experiment"]["output_folder"]) |
| 23 | + |
| 24 | + output_folder_injected = tuple(i for i in command if ".output_folder=" in i) |
| 25 | + if output_folder_injected: |
| 26 | + assert len(output_folder_injected) == 1 |
| 27 | + output_folder_inject_string = output_folder_injected[0] |
| 28 | + run_folder = Path(output_folder_inject_string.split(".output_folder=")[-1]) |
| 29 | + |
| 30 | + if run_folder.exists(): |
| 31 | + return run_folder |
| 32 | + |
| 33 | + cur_env = os.environ.copy() |
| 34 | + cur_env["SEQUENCE_LENGTH"] = "64" |
| 35 | + |
| 36 | + server_process = subprocess.Popen( |
| 37 | + ["python", "-m", server_path], |
| 38 | + stdout=subprocess.PIPE, |
| 39 | + stderr=subprocess.PIPE, |
| 40 | + env=cur_env, |
| 41 | + ) |
| 42 | + logger.info("Server process starting...") |
| 43 | + |
| 44 | + try: |
| 45 | + time.sleep(10) |
| 46 | + subprocess.run(args=command, check=True) |
| 47 | + finally: |
| 48 | + server_process.terminate() |
| 49 | + server_process.wait(timeout=5) |
| 50 | + |
| 51 | + return run_folder |
| 52 | + |
| 53 | + |
| 54 | +def get_streaming_generation_experiment() -> AutoDocExperimentInfo: |
| 55 | + base_path = f"docs/tutorials/tutorial_files/{CONTENT_ROOT}/{TUTORIAL_NAME}" |
| 56 | + conf_output_path = f"eir_tutorials/{CONTENT_ROOT}/{TUTORIAL_NAME}" |
| 57 | + |
| 58 | + command = [ |
| 59 | + "eirtrain", |
| 60 | + "--global_configs", |
| 61 | + f"{conf_output_path}/globals.yaml", |
| 62 | + "--input_configs", |
| 63 | + f"{conf_output_path}/input.yaml", |
| 64 | + "--fusion_configs", |
| 65 | + f"{conf_output_path}/fusion.yaml", |
| 66 | + "--output_configs", |
| 67 | + f"{conf_output_path}/output.yaml", |
| 68 | + ] |
| 69 | + |
| 70 | + mapping = [ |
| 71 | + ( |
| 72 | + "training_curve_LOSS", |
| 73 | + "figures/training_curve_LOSS.pdf", |
| 74 | + ), |
| 75 | + ( |
| 76 | + "samples/500/auto/0_generated.txt", |
| 77 | + "figures/auto_generated_iter_500.txt", |
| 78 | + ), |
| 79 | + ( |
| 80 | + "samples/2500/auto/0_generated.txt", |
| 81 | + "figures/auto_generated_iter_2500.txt", |
| 82 | + ), |
| 83 | + ] |
| 84 | + |
| 85 | + get_tutorial_folder = ( |
| 86 | + run_capture_and_save, |
| 87 | + { |
| 88 | + "command": [ |
| 89 | + "tree", |
| 90 | + f"eir_tutorials/{CONTENT_ROOT}/{TUTORIAL_NAME}", |
| 91 | + "-L", |
| 92 | + "3", |
| 93 | + "--noreport", |
| 94 | + ], |
| 95 | + "output_path": Path(base_path) / "commands/tutorial_folder.txt", |
| 96 | + }, |
| 97 | + ) |
| 98 | + |
| 99 | + ade = AutoDocExperimentInfo( |
| 100 | + name="STREAMING_SEQUENCE_GENERATION", |
| 101 | + data_url=None, |
| 102 | + data_output_path=None, |
| 103 | + base_path=Path(base_path), |
| 104 | + conf_output_path=Path(conf_output_path), |
| 105 | + command=command, |
| 106 | + files_to_copy_mapping=mapping, |
| 107 | + post_run_functions=(get_tutorial_folder,), |
| 108 | + run_command_wrapper=run_with_server, |
| 109 | + ) |
| 110 | + |
| 111 | + return ade |
| 112 | + |
| 113 | + |
| 114 | +def get_experiments() -> Sequence[AutoDocExperimentInfo]: |
| 115 | + exp_1 = get_streaming_generation_experiment() |
| 116 | + return [exp_1] |
0 commit comments