def test_to_device(index_dtype): g1 = dgl.heterograph({('user', 'plays', 'game'): [(0, 0), (1, 1)]}, index_dtype=index_dtype) g1.nodes['user'].data['h1'] = F.copy_to(F.tensor([[0.], [1.]]), F.cpu()) g1.nodes['user'].data['h2'] = F.copy_to(F.tensor([[3.], [4.]]), F.cpu()) g1.edges['plays'].data['h1'] = F.copy_to(F.tensor([[2.], [3.]]), F.cpu()) g2 = dgl.heterograph({('user', 'plays', 'game'): [(0, 0), (1, 0)]}, index_dtype=index_dtype) g2.nodes['user'].data['h1'] = F.copy_to(F.tensor([[1.], [2.]]), F.cpu()) g2.nodes['user'].data['h2'] = F.copy_to(F.tensor([[4.], [5.]]), F.cpu()) g2.edges['plays'].data['h1'] = F.copy_to(F.tensor([[0.], [1.]]), F.cpu()) bg = dgl.batch_hetero([g1, g2]) if F.is_cuda_available(): bg1 = bg.to(F.cuda()) assert bg1 is not None assert bg.batch_size == bg1.batch_size assert bg.batch_num_nodes('user') == bg1.batch_num_nodes('user') assert bg.batch_num_edges('plays') == bg1.batch_num_edges('plays') # set feature g1 = dgl.heterograph({('user', 'plays', 'game'): [(0, 0), (1, 1)]}, index_dtype=index_dtype) g2 = dgl.heterograph({('user', 'plays', 'game'): [(0, 0), (1, 0)]}, index_dtype=index_dtype) bg = dgl.batch_hetero([g1, g2]) if F.is_cuda_available(): bg1 = bg.to(F.cuda()) bg1.nodes['user'].data['test'] = F.copy_to(F.tensor([0, 1, 2, 3]), F.cuda()) bg1.edata['test'] = F.copy_to(F.tensor([0, 1, 2, 3]), F.cuda())
def test_copy_from_gpu(): hg = create_test_graph(idtype=F.int32) hg_gpu = hg.to(F.cuda()) hg_share = hg_gpu.shared_memory("hg_gpu") p = mp.Process(target=sub_proc, args=(hg, "hg_gpu")) p.start() p.join()
def test_subframes(parent_idx_device, child_device): parent_device, idx_device = parent_idx_device g = dgl.graph((F.tensor([1, 2, 3], dtype=F.int64), F.tensor([2, 3, 4], dtype=F.int64))) print(g.device) g.ndata['x'] = F.randn((5, 4)) g.edata['a'] = F.randn((3, 6)) idx = F.tensor([1, 2], dtype=F.int64) if parent_device == 'cuda': g = g.to(F.cuda()) elif parent_device == 'uva': g = g.to(F.cpu()) g.create_formats_() g.pin_memory_() elif parent_device == 'cpu': g = g.to(F.cpu()) idx = F.copy_to(idx, idx_device) sg = g.sample_neighbors(idx, 2).to(child_device) assert sg.device == sg.ndata['x'].device assert sg.device == sg.edata['a'].device assert sg.device == child_device if parent_device != 'uva': sg = g.to(child_device).sample_neighbors(F.copy_to(idx, child_device), 2) assert sg.device == sg.ndata['x'].device assert sg.device == sg.edata['a'].device assert sg.device == child_device if parent_device == 'uva': g.unpin_memory_()
def test_to_device(): g = dgl.DGLGraph() g.add_nodes(5, {'h': F.ones((5, 2))}) g.add_edges([0, 1], [1, 2], {'m': F.ones((2, 2))}) if F.is_cuda_available(): g = g.to(F.cuda()) assert g is not None
def test_subframes(parent_idx_device, child_device): parent_device, idx_device = parent_idx_device g = dgl.graph((F.tensor([1,2,3], dtype=F.int64), F.tensor([2,3,4], dtype=F.int64))) print(g.device) g.ndata['x'] = F.randn((5, 4)) g.edata['a'] = F.randn((3, 6)) idx = F.tensor([1, 2], dtype=F.int64) if parent_device == 'cuda': g = g.to(F.cuda()) elif parent_device == 'uva': if F.backend_name != 'pytorch': pytest.skip("UVA only supported for PyTorch") g = g.to(F.cpu()) g.create_formats_() g.pin_memory_() elif parent_device == 'cpu': g = g.to(F.cpu()) idx = F.copy_to(idx, idx_device) sg = g.sample_neighbors(idx, 2).to(child_device) assert sg.device == F.context(sg.ndata['x']) assert sg.device == F.context(sg.edata['a']) assert sg.device == child_device if parent_device != 'uva': sg = g.to(child_device).sample_neighbors(F.copy_to(idx, child_device), 2) assert sg.device == F.context(sg.ndata['x']) assert sg.device == F.context(sg.edata['a']) assert sg.device == child_device if parent_device == 'uva': g.unpin_memory_()
def test_node_dataloader(idtype, sampler_name, pin_graph): g1 = dgl.graph(([0, 0, 0, 1, 1], [1, 2, 3, 3, 4])).astype(idtype) g1.ndata['feat'] = F.copy_to(F.randn((5, 8)), F.cpu()) g1.ndata['label'] = F.copy_to(F.randn((g1.num_nodes(),)), F.cpu()) indices = F.arange(0, g1.num_nodes(), idtype) if F.ctx() != F.cpu(): if pin_graph: g1.create_formats_() g1.pin_memory_() if pin_graph == 'cpu_indices': indices = F.arange(0, g1.num_nodes(), idtype, F.cpu()) elif pin_graph == 'cuda_indices': if F._default_context_str == 'gpu': indices = F.arange(0, g1.num_nodes(), idtype, F.cuda()) else: return # skip else: g1 = g1.to('cuda') use_uva = pin_graph is not None and F.ctx() != F.cpu() for num_workers in [0, 1, 2]: sampler = { 'full': dgl.dataloading.MultiLayerFullNeighborSampler(2), 'neighbor': dgl.dataloading.MultiLayerNeighborSampler([3, 3]), 'neighbor2': dgl.dataloading.MultiLayerNeighborSampler([3, 3])}[sampler_name] dataloader = dgl.dataloading.NodeDataLoader( g1, indices, sampler, device=F.ctx(), batch_size=g1.num_nodes(), num_workers=(num_workers if (pin_graph and F.ctx() == F.cpu()) else 0), use_uva=use_uva) for input_nodes, output_nodes, blocks in dataloader: _check_device(input_nodes) _check_device(output_nodes) _check_device(blocks) _check_dtype(input_nodes, idtype, 'dtype') _check_dtype(output_nodes, idtype, 'dtype') _check_dtype(blocks, idtype, 'idtype') if g1.is_pinned(): g1.unpin_memory_() g2 = dgl.heterograph({ ('user', 'follow', 'user'): ([0, 0, 0, 1, 1, 1, 2], [1, 2, 3, 0, 2, 3, 0]), ('user', 'followed-by', 'user'): ([1, 2, 3, 0, 2, 3, 0], [0, 0, 0, 1, 1, 1, 2]), ('user', 'play', 'game'): ([0, 1, 1, 3, 5], [0, 1, 2, 0, 2]), ('game', 'played-by', 'user'): ([0, 1, 2, 0, 2], [0, 1, 1, 3, 5]) }).astype(idtype) for ntype in g2.ntypes: g2.nodes[ntype].data['feat'] = F.copy_to(F.randn((g2.num_nodes(ntype), 8)), F.cpu()) indices = {nty: F.arange(0, g2.num_nodes(nty)) for nty in g2.ntypes} if F.ctx() != F.cpu(): if pin_graph: g2.create_formats_() g2.pin_memory_() if pin_graph == 'cpu_indices': indices = {nty: F.arange(0, g2.num_nodes(nty), idtype, F.cpu()) for nty in g2.ntypes} elif pin_graph == 'cuda_indices': if F._default_context_str == 'gpu': indices = {nty: F.arange(0, g2.num_nodes(), idtype, F.cuda()) for nty in g2.ntypes} else: return # skip else: g2 = g2.to('cuda') batch_size = max(g2.num_nodes(nty) for nty in g2.ntypes) sampler = { 'full': dgl.dataloading.MultiLayerFullNeighborSampler(2), 'neighbor': dgl.dataloading.MultiLayerNeighborSampler([{etype: 3 for etype in g2.etypes}] * 2), 'neighbor2': dgl.dataloading.MultiLayerNeighborSampler([3, 3])}[sampler_name] dataloader = dgl.dataloading.NodeDataLoader( g2, {nty: g2.nodes(nty) for nty in g2.ntypes}, sampler, device=F.ctx(), batch_size=batch_size, num_workers=(num_workers if (pin_graph and F.ctx() == F.cpu()) else 0), use_uva=use_uva) assert isinstance(iter(dataloader), Iterator) for input_nodes, output_nodes, blocks in dataloader: _check_device(input_nodes) _check_device(output_nodes) _check_device(blocks) _check_dtype(input_nodes, idtype, 'dtype') _check_dtype(output_nodes, idtype, 'dtype') _check_dtype(blocks, idtype, 'idtype') if g2.is_pinned(): g2.unpin_memory_()
'game': 0 }, k=1) assert sg.num_edges('follows') == 0 u, v = sg['plays'].edges() edge_set = set(zip(list(F.asnumpy(u)), list(F.asnumpy(v)))) assert edge_set == {(0, 1)} assert F.array_equal(F.astype(inv['user'], idtype), F.tensor([0], idtype)) assert F.array_equal(F.astype(inv['game'], idtype), F.tensor([0], idtype)) @unittest.skipIf(not F.gpu_ctx(), 'only necessary with GPU') @unittest.skipIf(dgl.backend.backend_name != "pytorch", reason="UVA only supported for PyTorch") @pytest.mark.parametrize('parent_idx_device', [('cpu', F.cpu()), ('cuda', F.cuda()), ('uva', F.cpu()), ('uva', F.cuda())]) @pytest.mark.parametrize('child_device', [F.cpu(), F.cuda()]) def test_subframes(parent_idx_device, child_device): parent_device, idx_device = parent_idx_device g = dgl.graph((F.tensor([1, 2, 3], dtype=F.int64), F.tensor([2, 3, 4], dtype=F.int64))) print(g.device) g.ndata['x'] = F.randn((5, 4)) g.edata['a'] = F.randn((3, 6)) idx = F.tensor([1, 2], dtype=F.int64) if parent_device == 'cuda': g = g.to(F.cuda()) elif parent_device == 'uva':
def test_knn_cuda(algorithm, dist): if not th.cuda.is_available(): return x = th.randn(8, 3).to(F.cuda()) kg = dgl.nn.KNNGraph(3) if dist == 'euclidean': d = th.cdist(x, x).to(F.cpu()) else: x = x + th.randn(1).item() tmp_x = x / (1e-5 + F.sqrt(F.sum(x * x, dim=1, keepdims=True))) d = 1 - F.matmul(tmp_x, tmp_x.T).to(F.cpu()) def check_knn(g, x, start, end, k): assert g.device == x.device g = g.to(F.cpu()) for v in range(start, end): src, _ = g.in_edges(v) src = set(src.numpy()) i = v - start src_ans = set( th.topk(d[start:end, start:end][i], k, largest=False)[1].numpy() + start) assert src == src_ans # check knn with 2d input g = kg(x, algorithm, dist) check_knn(g, x, 0, 8, 3) # check knn with 3d input g = kg(x.view(2, 4, 3), algorithm, dist) check_knn(g, x, 0, 4, 3) check_knn(g, x, 4, 8, 3) # check segmented knn kg = dgl.nn.SegmentedKNNGraph(3) g = kg(x, [3, 5], algorithm, dist) check_knn(g, x, 0, 3, 3) check_knn(g, x, 3, 8, 3) # check k > num_points kg = dgl.nn.KNNGraph(10) with pytest.warns(DGLWarning): g = kg(x, algorithm, dist) check_knn(g, x, 0, 8, 8) with pytest.warns(DGLWarning): g = kg(x.view(2, 4, 3), algorithm, dist) check_knn(g, x, 0, 4, 4) check_knn(g, x, 4, 8, 4) kg = dgl.nn.SegmentedKNNGraph(5) with pytest.warns(DGLWarning): g = kg(x, [3, 5], algorithm, dist) check_knn(g, x, 0, 3, 3) check_knn(g, x, 3, 8, 3) # check k == 0 kg = dgl.nn.KNNGraph(0) with pytest.raises(DGLError): g = kg(x, algorithm, dist) kg = dgl.nn.SegmentedKNNGraph(0) with pytest.raises(DGLError): g = kg(x, [3, 5], algorithm, dist) # check empty x_empty = th.tensor([]) kg = dgl.nn.KNNGraph(3) with pytest.raises(DGLError): g = kg(x_empty, algorithm, dist) kg = dgl.nn.SegmentedKNNGraph(3) with pytest.raises(DGLError): g = kg(x_empty, [3, 5], algorithm, dist)
def test_to_device(): hg = create_test_heterograph() if F.is_cuda_available(): hg = hg.to(F.cuda()) assert hg is not None