Skip to content

Commit 2939301

Browse files
Merge pull request #137 from arnor-sigurdsson/more-streaming-docs
More streaming docs
2 parents d5ef7d4 + 0c48e1f commit 2939301

30 files changed

Lines changed: 1714 additions & 254 deletions

docs/automake_docs.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
AutoDocServingInfo,
4545
make_serving_tutorial_data,
4646
)
47+
from docs.doc_modules.user_guides import a_streaming_hands_on
4748
from docs.doc_scripts.doc_performance_report import (
4849
collect_performance_data,
4950
generate_report,
@@ -159,6 +160,14 @@ def get_i_scaling_experiments() -> Iterable[AutoDocExperimentInfo]:
159160
)
160161

161162

163+
def get_user_guide_experiments() -> Iterable[AutoDocExperimentInfo]:
164+
a_experiments = a_streaming_hands_on.get_experiments()
165+
166+
return chain(
167+
a_experiments,
168+
)
169+
170+
162171
def generate_performance_report(root_dir, output_dir):
163172
if not os.path.exists(root_dir):
164173
logger.error(f"Error: Root directory '{root_dir}' does not exist.")
@@ -223,7 +232,9 @@ def parse_args():
223232
args = parse_args()
224233

225234
if args.experiments is None:
226-
run_a = run_b = run_c = run_d = run_e = run_f = run_g = run_h = run_i = True
235+
run_a = run_b = run_c = run_d = run_e = run_f = run_g = run_h = run_i = (
236+
run_ug
237+
) = True
227238
else:
228239
experiment_ids = args.experiments.split(",")
229240
run_a = "a" in experiment_ids
@@ -235,6 +246,7 @@ def parse_args():
235246
run_g = "g" in experiment_ids
236247
run_h = "h" in experiment_ids
237248
run_i = "i" in experiment_ids
249+
run_ug = "ug" in experiment_ids
238250

239251
experiments = []
240252

@@ -274,6 +286,10 @@ def parse_args():
274286
i_scaling_experiments = get_i_scaling_experiments()
275287
experiments.append(i_scaling_experiments)
276288

289+
if run_ug:
290+
user_guide_experiments = get_user_guide_experiments()
291+
experiments.append(user_guide_experiments)
292+
277293
experiment_iter = chain.from_iterable(experiments)
278294
for experiment in experiment_iter:
279295
match experiment:

docs/doc_modules/user_guides/__init__.py

Whitespace-only changes.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import os
2+
from typing import Any
3+
4+
from docs.doc_modules.user_guides.single_sample_simulation import simulate_health_sample
5+
from eir.utils.logging import get_logger
6+
7+
logger = get_logger(__name__)
8+
9+
10+
class DataSimulator:
11+
def __init__(
12+
self,
13+
sequence_length: int = 64,
14+
max_iterations: int | None = None,
15+
):
16+
self.sequence_length = sequence_length
17+
self.max_iterations = max_iterations
18+
self.position = 0
19+
20+
logger.info(f"Using sequence_length={self.sequence_length}")
21+
if self.max_iterations is not None:
22+
logger.info(f"Will terminate after {self.max_iterations} iterations")
23+
24+
def reset(self):
25+
self.position = 0
26+
if self.max_iterations is not None:
27+
logger.info(f"Reset: Will terminate after {self.max_iterations} iterations")
28+
29+
def get_batch(self, batch_size: int) -> list[dict[str, Any]]:
30+
if self.max_iterations is not None and self.position >= self.max_iterations:
31+
logger.info(f"Reached max iterations ({self.max_iterations}), terminating")
32+
return []
33+
34+
batch = []
35+
for _ in range(batch_size):
36+
if self.max_iterations is not None and self.position >= self.max_iterations:
37+
break
38+
39+
text_input, text_sequence = simulate_health_sample(sequence_length=64)
40+
sample_id = f"sample_{self.position}"
41+
42+
batch.append(
43+
{
44+
"inputs": {
45+
"text_output": text_sequence,
46+
"text_input": text_input,
47+
},
48+
"target_labels": {"text_output": {"text_output": text_sequence}},
49+
"sample_id": sample_id,
50+
}
51+
)
52+
53+
self.position += 1
54+
55+
return batch
56+
57+
def get_status(self) -> dict[str, Any]:
58+
status_data = {
59+
"current_position": self.position,
60+
}
61+
62+
if self.max_iterations is not None:
63+
status_data["max_iterations"] = self.max_iterations
64+
status_data["remaining_iterations"] = max(
65+
0, self.max_iterations - self.position
66+
)
67+
68+
return status_data
69+
70+
71+
def create_simulator() -> DataSimulator:
72+
sequence_length = int(os.getenv("SEQUENCE_LENGTH", "512"))
73+
74+
max_iterations_str = os.getenv("MAX_ITERATIONS")
75+
max_iterations = int(max_iterations_str) if max_iterations_str else None
76+
77+
logger.info(
78+
f"Creating DataSimulator with sequence_length={sequence_length}, "
79+
f"max_iterations={max_iterations}"
80+
)
81+
82+
return DataSimulator(
83+
sequence_length=sequence_length,
84+
max_iterations=max_iterations,
85+
)

0 commit comments

Comments
 (0)