Download code/multi-hop/export_two_graph_dataset.py from fhyfhy/Implicit-Reasoning-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 6.83 kB
-
https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/export_two_graph_dataset.py
- Command line
-
hf download hf://fhyfhy/Implicit-Reasoning-checkpoints/code/multi-hop/export_two_graph_dataset.py
-
curl -L -o export_two_graph_dataset.py https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/export_two_graph_dataset.py
6.83 kB
| #!/usr/bin/env python3 | |
| """ | |
| Build a static two-graph dataset: | |
| - train: all atom data from graph 1 + graph 2, plus a train subset of multi-hop QA from graph 1 | |
| - test_id: disjoint multi-hop QA subset from graph 1 | |
| - test_ood: multi-hop QA subset from graph 2 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import random | |
| from pathlib import Path | |
| from typing import Dict, List | |
| from generate_data import DataGenerator | |
| from utils.data.name_space import single_names | |
| def graph_to_records(generator: DataGenerator) -> List[Dict]: | |
| return [ | |
| {"source": source, "relation": relation, "target": target} | |
| for (source, relation), target in generator.knowledge_graph.items() | |
| ] | |
| def add_metadata(records: List[Dict], split: str, graph_name: str) -> List[Dict]: | |
| out: List[Dict] = [] | |
| for record in records: | |
| item = dict(record) | |
| item["split"] = split | |
| item["graph"] = graph_name | |
| out.append(item) | |
| return out | |
| def build_generators(args: argparse.Namespace) -> tuple[DataGenerator, DataGenerator]: | |
| total_entities = 2 * args.num_entities | |
| if total_entities > len(single_names): | |
| raise ValueError( | |
| f"Need {total_entities} unique entities for two graphs, but namespace only has {len(single_names)} names" | |
| ) | |
| rng = random.Random(args.seed) | |
| sampled_entities = rng.sample(single_names, total_entities) | |
| graph1_entities = sampled_entities[: args.num_entities] | |
| graph2_entities = sampled_entities[args.num_entities :] | |
| shared_relations = DataGenerator(num_entities=1, num_relations=args.num_relations, seed=args.seed).relations | |
| graph1 = DataGenerator( | |
| num_entities=args.num_entities, | |
| num_relations=args.num_relations, | |
| seed=args.seed, | |
| sampling_alpha=args.sampling_alpha, | |
| entities=graph1_entities, | |
| relations=shared_relations, | |
| ) | |
| graph1.generate_graph(edges_per_entity=args.edges_per_entity) | |
| graph2 = DataGenerator( | |
| num_entities=args.num_entities, | |
| num_relations=args.num_relations, | |
| seed=args.seed + 1, | |
| sampling_alpha=args.sampling_alpha, | |
| entities=graph2_entities, | |
| relations=shared_relations, | |
| ) | |
| graph2.generate_graph(edges_per_entity=args.edges_per_entity) | |
| return graph1, graph2 | |
| def write_json(path: Path, payload) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps(payload, indent=2)) | |
| print(f"Saved {path}") | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser(description="Export a static two-graph multi-hop dataset.") | |
| parser.add_argument("--output-dir", type=Path, required=True) | |
| parser.add_argument("--num-entities", type=int, default=50) | |
| parser.add_argument("--num-relations", type=int, default=50) | |
| parser.add_argument("--edges-per-entity", type=int, default=10) | |
| parser.add_argument("--num-hops", type=int, default=2) | |
| parser.add_argument("--num-train-multihop", type=int, default=5000) | |
| parser.add_argument("--num-test-id", type=int, default=1000) | |
| parser.add_argument("--num-test-ood", type=int, default=1000) | |
| parser.add_argument("--seed", type=int, default=42) | |
| parser.add_argument("--sampling-alpha", type=float, default=None) | |
| parser.add_argument("--atom-sequence-mode", choices=["grouped", "single"], default="grouped") | |
| parser.add_argument("--multi-hop-format", choices=["reverse", "direct"], default="reverse") | |
| parser.add_argument("--save-graphs", action="store_true") | |
| parser.add_argument("--no-leading-space", action="store_true", help="Disable the leading whitespace prefix.") | |
| return parser.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| leading_space = not args.no_leading_space | |
| split_atom_sequences = args.atom_sequence_mode == "single" | |
| graph1, graph2 = build_generators(args) | |
| random.seed(args.seed + 101) | |
| train_atom = add_metadata( | |
| graph1.generate_all_atom_examples( | |
| split_atom_sequences=split_atom_sequences, | |
| leading_space=leading_space, | |
| ), | |
| split="train", | |
| graph_name="graph1", | |
| ) | |
| random.seed(args.seed + 202) | |
| train_atom += add_metadata( | |
| graph2.generate_all_atom_examples( | |
| split_atom_sequences=split_atom_sequences, | |
| leading_space=leading_space, | |
| ), | |
| split="train", | |
| graph_name="graph2", | |
| ) | |
| seen_graph1 = set() | |
| random.seed(args.seed + 303) | |
| train_multihop = add_metadata( | |
| graph1.sample_unique_multihop_examples( | |
| num_examples=args.num_train_multihop, | |
| num_hops=args.num_hops, | |
| multi_hop_format=args.multi_hop_format, | |
| leading_space=leading_space, | |
| seen=seen_graph1, | |
| ), | |
| split="train", | |
| graph_name="graph1", | |
| ) | |
| random.seed(args.seed + 404) | |
| test_id = add_metadata( | |
| graph1.sample_unique_multihop_examples( | |
| num_examples=args.num_test_id, | |
| num_hops=args.num_hops, | |
| multi_hop_format=args.multi_hop_format, | |
| leading_space=leading_space, | |
| seen=seen_graph1, | |
| ), | |
| split="test_id", | |
| graph_name="graph1", | |
| ) | |
| random.seed(args.seed + 505) | |
| test_ood = add_metadata( | |
| graph2.sample_unique_multihop_examples( | |
| num_examples=args.num_test_ood, | |
| num_hops=args.num_hops, | |
| multi_hop_format=args.multi_hop_format, | |
| leading_space=leading_space, | |
| ), | |
| split="test_ood", | |
| graph_name="graph2", | |
| ) | |
| train_records = train_atom + train_multihop | |
| manifest = { | |
| "num_entities_per_graph": args.num_entities, | |
| "num_relations": args.num_relations, | |
| "edges_per_entity": args.edges_per_entity, | |
| "num_hops": args.num_hops, | |
| "multi_hop_format": args.multi_hop_format, | |
| "atom_sequence_mode": args.atom_sequence_mode, | |
| "leading_space": leading_space, | |
| "counts": { | |
| "train_atom": len(train_atom), | |
| "train_multihop": len(train_multihop), | |
| "train_total": len(train_records), | |
| "test_id": len(test_id), | |
| "test_ood": len(test_ood), | |
| }, | |
| "graph1_entities": graph1.entities, | |
| "graph2_entities": graph2.entities, | |
| "relations": graph1.relations, | |
| } | |
| write_json(args.output_dir / "train.json", train_records) | |
| write_json(args.output_dir / "test_id.json", test_id) | |
| write_json(args.output_dir / "test_ood.json", test_ood) | |
| write_json(args.output_dir / "manifest.json", manifest) | |
| if args.save_graphs: | |
| write_json(args.output_dir / "graph1_edges.json", graph_to_records(graph1)) | |
| write_json(args.output_dir / "graph2_edges.json", graph_to_records(graph2)) | |
| if __name__ == "__main__": | |
| main() | |