2626
2727import dataclasses_json
2828from dgf .src .data import schema as schema_lib
29- from dgf .src .io import schema as io_schema
3029from dgf .src .sampling import config as config_lib
31- from dgf .src .util import filesystem
30+ from dgf .src .sampling . offline_distributed import common
3231from dgf .src .util import log
32+ from dgf .src .util import util
3333from dgf .src .util .weak_dep .weak_dep_aiplatform import aiplatform
3434import tqdm
3535
4040_WORKER_MACHINE_TYPE = "n1-highmem-4"
4141_DEFAULT_POLL_INTERVAL_SEC = 5.0
4242
43- # Containers the input seeds can be read from. SSTable is excluded: it is a DAX
44- # sink that the sampler cannot read seeds from.
45- _SUPPORTED_INPUT_SEED_CONTAINERS = frozenset ({"TFRECORD" , "RECORDIO" })
46-
4743_CONSOLE_JOB_URL_TEMPLATE = "https://console.cloud.google.com/vertex-ai/locations/{region}/training/{job_id}?project={project}"
4844_CONSOLE_JOBS_LIST_URL_TEMPLATE = "https://console.cloud.google.com/vertex-ai/training/custom-jobs?project={project}"
4945_LOGGING_JOB_URL_TEMPLATE = "https://console.cloud.google.com/logs/viewer?project={project}&resource=ml_job%2Fjob_id%2F{job_id}"
@@ -69,14 +65,6 @@ def _get_terminal_failure_states() -> set[Any]:
6965]
7066
7167
72- def _format_duration (seconds : float ) -> str :
73- """Formats a duration in seconds into human-readable format."""
74- sec = int (seconds )
75- if sec >= 60 :
76- return f"{ sec // 60 } m { sec % 60 :02d} s"
77- return f"{ sec } s"
78-
79-
8068def _get_default_gcp_project () -> str | None :
8169 """Gets the active GCP project from environment or gcloud config."""
8270 for env_var in (
@@ -121,52 +109,6 @@ def _validate_paths(input_path: str, output_path: str) -> tuple[str, str]:
121109 )
122110
123111
124- def _validate_seed_args (
125- num_seeds : int | None ,
126- num_samples_per_seed : int ,
127- input_seed_container : str ,
128- filter_seed_node : str | None = None ,
129- input_seeds : str | None = None ,
130- ) -> None :
131- """Validates the arguments controlling the selection of the seeds.
132-
133- Args:
134- num_seeds: Number of seeds to select among the available ones, or None.
135- num_samples_per_seed: Number of samples to generate for each seed.
136- input_seed_container: Container of the input seeds.
137- filter_seed_node: Optional filter on the seed nodes.
138- input_seeds: Optional path to input seeds.
139-
140- Raises:
141- ValueError: If the arguments are inconsistent.
142- """
143- if num_seeds is not None and num_seeds < 0 :
144- raise ValueError (f"num_seeds cannot be negative, got { num_seeds } ." )
145- if num_samples_per_seed < 1 :
146- raise ValueError (
147- "num_samples_per_seed cannot be less than one, got"
148- f" { num_samples_per_seed } ."
149- )
150- if num_seeds and num_samples_per_seed > 1 :
151- raise ValueError (
152- f"num_seeds ({ num_seeds } ) and num_samples_per_seed"
153- f" ({ num_samples_per_seed } ) are exclusive: use num_seeds to generate"
154- " fewer samples than there are seed nodes, and num_samples_per_seed to"
155- " generate more."
156- )
157- if input_seed_container not in _SUPPORTED_INPUT_SEED_CONTAINERS :
158- raise ValueError (
159- f"Unsupported input_seed_container { input_seed_container !r} . Supported"
160- f" containers are { sorted (_SUPPORTED_INPUT_SEED_CONTAINERS )} ."
161- )
162- if filter_seed_node is not None and input_seeds is not None :
163- raise ValueError (
164- "filter_seed_node and input_seeds are exclusive: filter_seed_node only"
165- " applies to the seed nodes taken from the root nodeset of the sampling"
166- " plan."
167- )
168-
169-
170112def _get_job_urls (
171113 project : str , region : str , resource_name : str
172114) -> tuple [str , str ]:
@@ -201,38 +143,6 @@ def _get_job_stage_and_name(state: Any) -> tuple[int, str]:
201143 return 1 , state_name
202144
203145
204- def _write_sampling_plan (
205- input_path : str ,
206- output_path : str ,
207- plan : config_lib .SimpleSamplingConfig | config_lib .SamplingPlan ,
208- schema : schema_lib .GraphSchema | None ,
209- ) -> str :
210- """Converts sampling config to plan if needed and writes it to GCS."""
211- sampling_config_path = f"{ output_path } /sampling_config.json"
212-
213- if isinstance (plan , config_lib .SimpleSamplingConfig ):
214- if schema is None :
215- schema_path = f"{ input_path } /schema.json"
216- log .info ("Reading graph schema from %s" , schema_path )
217- schema = io_schema .read_schema (schema_path )
218- plan = config_lib .simple_sampling_config_to_sampling_plan (plan , schema )
219- # Early check. The Go sampler validates the plan again.
220- config_lib .check_supported_options (
221- plan ,
222- schema ,
223- "offline distributed sampler" ,
224- support_non_with_replacement = True ,
225- support_multi_visit = True ,
226- support_temporal_sampling = True ,
227- )
228-
229- log .info ("Writing sampling plan to %s" , sampling_config_path )
230- with filesystem .open_write (sampling_config_path ) as f :
231- f .write (plan .to_json (indent = 2 )) # pyrefly: ignore[missing-attribute]
232-
233- return sampling_config_path
234-
235-
236146@dataclasses_json .dataclass_json
237147@dataclasses .dataclass
238148class OfflineDistributedSamplingConfig :
@@ -373,7 +283,7 @@ def _monitor_job(
373283 while True :
374284 state = job .state
375285 stage_idx , state_name = _get_job_stage_and_name (state )
376- elapsed_str = _format_duration (time .time () - start_time )
286+ elapsed_str = util . format_duration (time .time () - start_time )
377287
378288 if state_name != last_state_name :
379289 log .info (
@@ -537,7 +447,7 @@ def offline_distributed_sampler_gcp(
537447 be resolved.
538448 """
539449 input_path , output_path = _validate_paths (input_path , output_path )
540- _validate_seed_args (
450+ common . validate_seed_args (
541451 num_seeds = num_seeds ,
542452 num_samples_per_seed = num_samples_per_seed ,
543453 input_seed_container = input_seed_container ,
@@ -566,7 +476,7 @@ def offline_distributed_sampler_gcp(
566476 display_name = f"dgf-distributed-sampler-glassbox-{ timestamp } "
567477
568478 start_time = time .time ()
569- sampling_config_path = _write_sampling_plan (
479+ sampling_config_path = common . write_sampling_plan (
570480 input_path = input_path ,
571481 output_path = output_path ,
572482 plan = plan ,
@@ -617,7 +527,7 @@ def offline_distributed_sampler_gcp(
617527 region = region ,
618528 poll_interval = poll_interval ,
619529 )
620- total_duration = _format_duration (time .time () - start_time )
530+ total_duration = util . format_duration (time .time () - start_time )
621531 log .info ("Total duration: %s" , total_duration )
622532
623533 return job
0 commit comments