MCPcopy Create free account
hub / github.com/dmlc/dgl / test_local_scope

Function test_local_scope

tests/python/common/function/test_basics.py:594–691  ·  view source on GitHub ↗
(idtype)

Source from the content-addressed store, hash-verified

592
593@parametrize_idtype
594def test_local_scope(idtype):
595 g = dgl.graph(([0, 1, 2, 3], [1, 2, 3, 4]), idtype=idtype, device=F.ctx())
596 g.ndata["h"] = F.zeros((g.num_nodes(), 3))
597 g.edata["w"] = F.zeros((g.num_edges(), 4))
598
599 # test override
600 def foo(g):
601 with g.local_scope():
602 g.ndata["h"] = F.ones((g.num_nodes(), 3))
603 g.edata["w"] = F.ones((g.num_edges(), 4))
604
605 foo(g)
606 assert F.allclose(g.ndata["h"], F.zeros((g.num_nodes(), 3)))
607 assert F.allclose(g.edata["w"], F.zeros((g.num_edges(), 4)))
608
609 # test out-place update
610 def foo(g):
611 with g.local_scope():
612 g.nodes[[2, 3]].data["h"] = F.ones((2, 3))
613 g.edges[[2, 3]].data["w"] = F.ones((2, 4))
614
615 foo(g)
616 assert F.allclose(g.ndata["h"], F.zeros((g.num_nodes(), 3)))
617 assert F.allclose(g.edata["w"], F.zeros((g.num_edges(), 4)))
618
619 # test out-place update 2
620 def foo(g):
621 with g.local_scope():
622 g.apply_nodes(lambda nodes: {"h": nodes.data["h"] + 10}, [2, 3])
623 g.apply_edges(lambda edges: {"w": edges.data["w"] + 10}, [2, 3])
624
625 foo(g)
626 assert F.allclose(g.ndata["h"], F.zeros((g.num_nodes(), 3)))
627 assert F.allclose(g.edata["w"], F.zeros((g.num_edges(), 4)))
628
629 # test auto-pop
630 def foo(g):
631 with g.local_scope():
632 g.ndata["hh"] = F.ones((g.num_nodes(), 3))
633 g.edata["ww"] = F.ones((g.num_edges(), 4))
634
635 foo(g)
636 assert "hh" not in g.ndata
637 assert "ww" not in g.edata
638
639 # test nested scope
640 def foo(g):
641 with g.local_scope():
642 g.ndata["hh"] = F.ones((g.num_nodes(), 3))
643 g.edata["ww"] = F.ones((g.num_edges(), 4))
644 with g.local_scope():
645 g.ndata["hhh"] = F.ones((g.num_nodes(), 3))
646 g.edata["www"] = F.ones((g.num_edges(), 4))
647 assert "hhh" not in g.ndata
648 assert "www" not in g.edata
649
650 foo(g)
651 assert "hh" not in g.ndata

Callers

nothing calls this directly

Calls 7

set_n_initializerMethod · 0.80
set_e_initializerMethod · 0.80
fooFunction · 0.70
graphMethod · 0.45
ctxMethod · 0.45
num_nodesMethod · 0.45
num_edgesMethod · 0.45

Tested by

no test coverage detected