Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions dgf/src/bin/create_graph_sampling_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@
"If true and temporal sampling is enabled, the edgesets without a creation"
" time get one derived from their connected nodes.",
)
_WITH_REPLACEMENT = flags.DEFINE_bool(
"with_replacement",
False,
"If true, the sampled graphs are trees where the nodes visited several"
" times are duplicated.",
)
_MULTI_VISIT = flags.DEFINE_bool(
"multi_visit",
True,
"If true, a node reached several times at the same step of the plan is"
" expanded once per visit.",
)
_MAX_TIMESERIES_LEN = flags.DEFINE_integer(
"max_timeseries_len",
32,
"Maximum number of steps retained for each timeseries feature.",
)


def main(argv):
Expand All @@ -96,6 +113,9 @@ def main(argv):
reverse=_REVERSE.value,
temporal_sampling=_TEMPORAL_SAMPLING.value,
propagate_timestamp_to_edges=_PROPAGATE_TIMESTAMP_TO_EDGES.value,
with_replacement=_WITH_REPLACEMENT.value,
multi_visit=_MULTI_VISIT.value,
max_timeseries_len=_MAX_TIMESERIES_LEN.value,
)

# Convert the config to a full sampling plan
Expand Down
17 changes: 15 additions & 2 deletions dgf/src/sampling/offline_distributed/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ package(
# Libraries
# =========

py_library(
name = "common",
srcs = ["common.py"],
deps = [
"//dgf/src/data:schema",
"//dgf/src/io:schema",
"//dgf/src/sampling:config",
"//dgf/src/util:filesystem",
"//dgf/src/util:log",
],
)

py_library(
name = "offline_distributed_gcp",
srcs = ["offline_distributed_gcp.py"],
deps = [
":common",
# dataclasses_json dep,
"//dgf/src/data:schema",
"//dgf/src/io:schema",
"//dgf/src/sampling:config",
"//dgf/src/util:filesystem",
"//dgf/src/util:log",
"//dgf/src/util:util_py",
"//dgf/src/util/weak_dep:weak_dep_aiplatform",
# tqdm dep,
],
Expand All @@ -41,6 +53,7 @@ py_test(
name = "offline_distributed_gcp_test",
srcs = ["offline_distributed_gcp_test.py"],
deps = [
":common",
":offline_distributed_gcp",
# absl/testing:absltest dep,
"//dgf/src/sampling:config",
Expand Down
107 changes: 107 additions & 0 deletions dgf/src/sampling/offline_distributed/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright 2022 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities shared by the offline distributed samplers (GCP and Borg)."""

from dgf.src.data import schema as schema_lib
from dgf.src.io import schema as io_schema
from dgf.src.sampling import config as config_lib
from dgf.src.util import filesystem
from dgf.src.util import log

# Containers the input seeds can be read from. SSTable is excluded: it is a DAX
# sink that the sampler cannot read seeds from.
SUPPORTED_INPUT_SEED_CONTAINERS = frozenset({"TFRECORD", "RECORDIO"})

SAMPLING_CONFIG_FILENAME = "sampling_config.json"


def validate_seed_args(
num_seeds: int | None,
num_samples_per_seed: int,
input_seed_container: str,
filter_seed_node: str | None = None,
input_seeds: str | None = None,
) -> None:
"""Validates the arguments controlling the selection of the seeds.

Args:
num_seeds: Number of seeds to select among the available ones, or None.
num_samples_per_seed: Number of samples to generate for each seed.
input_seed_container: Container of the input seeds.
filter_seed_node: Optional filter on the seed nodes.
input_seeds: Optional path to input seeds.

Raises:
ValueError: If the arguments are inconsistent.
"""
if num_seeds is not None and num_seeds < 0:
raise ValueError(f"num_seeds cannot be negative, got {num_seeds}.")
if num_samples_per_seed < 1:
raise ValueError(
"num_samples_per_seed cannot be less than one, got"
f" {num_samples_per_seed}."
)
if num_seeds and num_samples_per_seed > 1:
raise ValueError(
f"num_seeds ({num_seeds}) and num_samples_per_seed"
f" ({num_samples_per_seed}) are exclusive: use num_seeds to generate"
" fewer samples than there are seed nodes, and num_samples_per_seed to"
" generate more."
)
if input_seed_container not in SUPPORTED_INPUT_SEED_CONTAINERS:
raise ValueError(
f"Unsupported input_seed_container {input_seed_container!r}. Supported"
f" containers are {sorted(SUPPORTED_INPUT_SEED_CONTAINERS)}."
)
if filter_seed_node is not None and input_seeds is not None:
raise ValueError(
"filter_seed_node and input_seeds are exclusive: filter_seed_node only"
" applies to the seed nodes taken from the root nodeset of the sampling"
" plan."
)


def write_sampling_plan(
input_path: str,
output_path: str,
plan: config_lib.SimpleSamplingConfig | config_lib.SamplingPlan,
schema: schema_lib.GraphSchema | None,
) -> str:
"""Converts the sampling config to a plan if needed and writes it."""
sampling_config_path = f"{output_path}/{SAMPLING_CONFIG_FILENAME}"

if isinstance(plan, config_lib.SimpleSamplingConfig):
if schema is None:
schema_path = f"{input_path}/schema.json"
log.info("Reading graph schema from %s", schema_path)
schema = io_schema.read_schema(schema_path)
plan = config_lib.simple_sampling_config_to_sampling_plan(plan, schema)
# Early check. The Go sampler validates the plan again.
config_lib.check_supported_options(
plan,
schema,
"offline distributed sampler",
support_non_with_replacement=True,
support_multi_visit=True,
support_temporal_sampling=True,
)

if not filesystem.is_gcs_path(output_path):
filesystem.makedirs(output_path)
log.info("Writing sampling plan to %s", sampling_config_path)
with filesystem.open_write(sampling_config_path) as f:
f.write(plan.to_json(indent=2)) # pyrefly: ignore[missing-attribute]

return sampling_config_path
102 changes: 6 additions & 96 deletions dgf/src/sampling/offline_distributed/offline_distributed_gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

import dataclasses_json
from dgf.src.data import schema as schema_lib
from dgf.src.io import schema as io_schema
from dgf.src.sampling import config as config_lib
from dgf.src.util import filesystem
from dgf.src.sampling.offline_distributed import common
from dgf.src.util import log
from dgf.src.util import util
from dgf.src.util.weak_dep.weak_dep_aiplatform import aiplatform
import tqdm

Expand All @@ -40,10 +40,6 @@
_WORKER_MACHINE_TYPE = "n1-highmem-4"
_DEFAULT_POLL_INTERVAL_SEC = 5.0

# Containers the input seeds can be read from. SSTable is excluded: it is a DAX
# sink that the sampler cannot read seeds from.
_SUPPORTED_INPUT_SEED_CONTAINERS = frozenset({"TFRECORD", "RECORDIO"})

_CONSOLE_JOB_URL_TEMPLATE = "https://console.cloud.google.com/vertex-ai/locations/{region}/training/{job_id}?project={project}"
_CONSOLE_JOBS_LIST_URL_TEMPLATE = "https://console.cloud.google.com/vertex-ai/training/custom-jobs?project={project}"
_LOGGING_JOB_URL_TEMPLATE = "https://console.cloud.google.com/logs/viewer?project={project}&resource=ml_job%2Fjob_id%2F{job_id}"
Expand All @@ -69,14 +65,6 @@ def _get_terminal_failure_states() -> set[Any]:
]


def _format_duration(seconds: float) -> str:
"""Formats a duration in seconds into human-readable format."""
sec = int(seconds)
if sec >= 60:
return f"{sec // 60}m {sec % 60:02d}s"
return f"{sec}s"


def _get_default_gcp_project() -> str | None:
"""Gets the active GCP project from environment or gcloud config."""
for env_var in (
Expand Down Expand Up @@ -121,52 +109,6 @@ def _validate_paths(input_path: str, output_path: str) -> tuple[str, str]:
)


def _validate_seed_args(
num_seeds: int | None,
num_samples_per_seed: int,
input_seed_container: str,
filter_seed_node: str | None = None,
input_seeds: str | None = None,
) -> None:
"""Validates the arguments controlling the selection of the seeds.

Args:
num_seeds: Number of seeds to select among the available ones, or None.
num_samples_per_seed: Number of samples to generate for each seed.
input_seed_container: Container of the input seeds.
filter_seed_node: Optional filter on the seed nodes.
input_seeds: Optional path to input seeds.

Raises:
ValueError: If the arguments are inconsistent.
"""
if num_seeds is not None and num_seeds < 0:
raise ValueError(f"num_seeds cannot be negative, got {num_seeds}.")
if num_samples_per_seed < 1:
raise ValueError(
"num_samples_per_seed cannot be less than one, got"
f" {num_samples_per_seed}."
)
if num_seeds and num_samples_per_seed > 1:
raise ValueError(
f"num_seeds ({num_seeds}) and num_samples_per_seed"
f" ({num_samples_per_seed}) are exclusive: use num_seeds to generate"
" fewer samples than there are seed nodes, and num_samples_per_seed to"
" generate more."
)
if input_seed_container not in _SUPPORTED_INPUT_SEED_CONTAINERS:
raise ValueError(
f"Unsupported input_seed_container {input_seed_container!r}. Supported"
f" containers are {sorted(_SUPPORTED_INPUT_SEED_CONTAINERS)}."
)
if filter_seed_node is not None and input_seeds is not None:
raise ValueError(
"filter_seed_node and input_seeds are exclusive: filter_seed_node only"
" applies to the seed nodes taken from the root nodeset of the sampling"
" plan."
)


def _get_job_urls(
project: str, region: str, resource_name: str
) -> tuple[str, str]:
Expand Down Expand Up @@ -201,38 +143,6 @@ def _get_job_stage_and_name(state: Any) -> tuple[int, str]:
return 1, state_name


def _write_sampling_plan(
input_path: str,
output_path: str,
plan: config_lib.SimpleSamplingConfig | config_lib.SamplingPlan,
schema: schema_lib.GraphSchema | None,
) -> str:
"""Converts sampling config to plan if needed and writes it to GCS."""
sampling_config_path = f"{output_path}/sampling_config.json"

if isinstance(plan, config_lib.SimpleSamplingConfig):
if schema is None:
schema_path = f"{input_path}/schema.json"
log.info("Reading graph schema from %s", schema_path)
schema = io_schema.read_schema(schema_path)
plan = config_lib.simple_sampling_config_to_sampling_plan(plan, schema)
# Early check. The Go sampler validates the plan again.
config_lib.check_supported_options(
plan,
schema,
"offline distributed sampler",
support_non_with_replacement=True,
support_multi_visit=True,
support_temporal_sampling=True,
)

log.info("Writing sampling plan to %s", sampling_config_path)
with filesystem.open_write(sampling_config_path) as f:
f.write(plan.to_json(indent=2)) # pyrefly: ignore[missing-attribute]

return sampling_config_path


@dataclasses_json.dataclass_json
@dataclasses.dataclass
class OfflineDistributedSamplingConfig:
Expand Down Expand Up @@ -373,7 +283,7 @@ def _monitor_job(
while True:
state = job.state
stage_idx, state_name = _get_job_stage_and_name(state)
elapsed_str = _format_duration(time.time() - start_time)
elapsed_str = util.format_duration(time.time() - start_time)

if state_name != last_state_name:
log.info(
Expand Down Expand Up @@ -537,7 +447,7 @@ def offline_distributed_sampler_gcp(
be resolved.
"""
input_path, output_path = _validate_paths(input_path, output_path)
_validate_seed_args(
common.validate_seed_args(
num_seeds=num_seeds,
num_samples_per_seed=num_samples_per_seed,
input_seed_container=input_seed_container,
Expand Down Expand Up @@ -566,7 +476,7 @@ def offline_distributed_sampler_gcp(
display_name = f"dgf-distributed-sampler-glassbox-{timestamp}"

start_time = time.time()
sampling_config_path = _write_sampling_plan(
sampling_config_path = common.write_sampling_plan(
input_path=input_path,
output_path=output_path,
plan=plan,
Expand Down Expand Up @@ -617,7 +527,7 @@ def offline_distributed_sampler_gcp(
region=region,
poll_interval=poll_interval,
)
total_duration = _format_duration(time.time() - start_time)
total_duration = util.format_duration(time.time() - start_time)
log.info("Total duration: %s", total_duration)

return job
Loading
Loading