Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GraphBolt] Add check to NegativeSampler. #6976

Merged
merged 3 commits into from
Jan 22, 2024
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
15 changes: 9 additions & 6 deletions python/dgl/graphbolt/impl/uniform_negative_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ def __init__(
super().__init__(datapipe, negative_ratio)
self.graph = graph

def _sample_with_etype(self, node_pairs, etype=None):
return self.graph.sample_negative_edges_uniform(
etype,
node_pairs,
self.negative_ratio,
)
def _sample_with_etype(self, node_pairs, etype=None, use_seeds=False):
if not use_seeds:
return self.graph.sample_negative_edges_uniform(
etype,
node_pairs,
self.negative_ratio,
)
else:
raise NotImplementedError("Not implemented yet.")
48 changes: 28 additions & 20 deletions python/dgl/graphbolt/negative_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,24 @@ def _sample(self, minibatch):
An instance of 'MiniBatch' encompasses both positive and negative
samples.
"""
node_pairs = minibatch.node_pairs
assert node_pairs is not None
if isinstance(node_pairs, Mapping):
minibatch.negative_srcs, minibatch.negative_dsts = {}, {}
for etype, pos_pairs in node_pairs.items():
self._collate(
minibatch, self._sample_with_etype(pos_pairs, etype), etype
)
if minibatch.seeds is None:
node_pairs = minibatch.node_pairs
assert node_pairs is not None
if isinstance(node_pairs, Mapping):
minibatch.negative_srcs, minibatch.negative_dsts = {}, {}
for etype, pos_pairs in node_pairs.items():
self._collate(
minibatch,
self._sample_with_etype(pos_pairs, etype),
etype,
)
else:
self._collate(minibatch, self._sample_with_etype(node_pairs))
else:
self._collate(minibatch, self._sample_with_etype(node_pairs))
raise NotImplementedError("Not implemented yet.")
return minibatch

def _sample_with_etype(self, node_pairs, etype=None):
def _sample_with_etype(self, node_pairs, etype=None, use_seeds=False):
"""Generate negative pairs for a given etype form positive pairs
for a given etype.

Expand Down Expand Up @@ -102,14 +107,17 @@ def _collate(self, minibatch, neg_pairs, etype=None):
etype : str
Canonical edge type.
"""
neg_src, neg_dst = neg_pairs
if neg_src is not None:
neg_src = neg_src.view(-1, self.negative_ratio)
if neg_dst is not None:
neg_dst = neg_dst.view(-1, self.negative_ratio)
if etype is not None:
minibatch.negative_srcs[etype] = neg_src
minibatch.negative_dsts[etype] = neg_dst
if minibatch.seeds is None:
neg_src, neg_dst = neg_pairs
if neg_src is not None:
neg_src = neg_src.view(-1, self.negative_ratio)
if neg_dst is not None:
neg_dst = neg_dst.view(-1, self.negative_ratio)
if etype is not None:
minibatch.negative_srcs[etype] = neg_src
minibatch.negative_dsts[etype] = neg_dst
else:
minibatch.negative_srcs = neg_src
minibatch.negative_dsts = neg_dst
else:
minibatch.negative_srcs = neg_src
minibatch.negative_dsts = neg_dst
raise NotImplementedError("Not implemented yet.")
27 changes: 27 additions & 0 deletions tests/python/pytorch/graphbolt/impl/test_negative_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ def test_NegativeSampler_invoke():
next(iter(negative_sampler))


def test_UniformNegativeSampler_seeds_invoke():
# Instantiate graph and required datapipes.
graph = gb_test_utils.rand_csc_graph(100, 0.05, bidirection_edge=True)
num_seeds = 30
item_set = gb.ItemSet(
torch.arange(0, 2 * num_seeds).reshape(-1, 2), names="seeds"
)
batch_size = 10
item_sampler = gb.ItemSampler(item_set, batch_size=batch_size)
negative_ratio = 2
# Invoke UniformNegativeSampler via class constructor.
negative_sampler = gb.UniformNegativeSampler(
item_sampler,
graph,
negative_ratio,
)
with pytest.raises(NotImplementedError):
next(iter(negative_sampler))
# Invoke UniformNegativeSampler via functional form.
negative_sampler = item_sampler.sample_uniform_negative(
graph,
negative_ratio,
)
with pytest.raises(NotImplementedError):
next(iter(negative_sampler))


def test_UniformNegativeSampler_invoke():
# Instantiate graph and required datapipes.
graph = gb_test_utils.rand_csc_graph(100, 0.05, bidirection_edge=True)
Expand Down