Skip to content

Commit

Permalink
[Misc] Rename number_of_edges and number_of_nodes to num_edges and nu…
Browse files Browse the repository at this point in the history
…m_nodes. (dmlc#5490)

* Other

* revert

---------

Co-authored-by: Ubuntu <[email protected]>
  • Loading branch information
frozenbugs and Ubuntu authored Mar 29, 2023
1 parent 8f9f2e2 commit 3c8ac09
Show file tree
Hide file tree
Showing 34 changed files with 103 additions and 107 deletions.
6 changes: 3 additions & 3 deletions benchmarks/benchmarks/model_acc/bench_rgcn_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def track_acc(data):
g.ndata["type_id"] = g.ndata[dgl.NID]
g.ndata["ntype"] = g.ndata[dgl.NTYPE]

node_ids = th.arange(g.number_of_nodes())
node_ids = th.arange(g.num_nodes())
# find out the target node ids
node_tids = g.ndata[dgl.NTYPE]
loc = node_tids == category_id
Expand Down Expand Up @@ -330,7 +330,7 @@ def track_acc(data):
# None for one-hot feature, if not none, it should be the feature tensor.
embed_layer = RelGraphEmbedLayer(
device,
g.number_of_nodes(),
g.num_nodes(),
node_tids,
num_of_ntype,
node_feats,
Expand All @@ -342,7 +342,7 @@ def track_acc(data):
# all model params are in device.
model = EntityClassify(
device,
g.number_of_nodes(),
g.num_nodes(),
n_hidden,
num_classes,
num_rels,
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchmarks/model_acc/bench_sage_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def inference(self, g, x, batch_size, device):
# TODO: can we standardize this?
for l, layer in enumerate(self.layers):
y = th.zeros(
g.number_of_nodes(),
g.num_nodes(),
self.n_hidden if l != len(self.layers) - 1 else self.n_classes,
)

sampler = dgl.dataloading.MultiLayerFullNeighborSampler(1)
dataloader = dgl.dataloading.DataLoader(
g,
th.arange(g.number_of_nodes()),
th.arange(g.num_nodes()),
sampler,
batch_size=batch_size,
shuffle=True,
Expand Down
14 changes: 7 additions & 7 deletions benchmarks/benchmarks/model_speed/bench_pinsage.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __init__(self, hidden_dims, n_layers):

def forward(self, blocks, h):
for layer, block in zip(self.convs, blocks):
h_dst = h[: block.number_of_nodes("DST/" + block.ntypes[0])]
h_dst = h[: block.num_nodes("DST/" + block.ntypes[0])]
h = layer(block, (h, h_dst), block.edata["weights"])
return h

Expand Down Expand Up @@ -191,7 +191,7 @@ class ItemToItemScorer(nn.Module):
def __init__(self, full_graph, ntype):
super().__init__()

n_nodes = full_graph.number_of_nodes(ntype)
n_nodes = full_graph.num_nodes(ntype)
self.bias = nn.Parameter(torch.zeros(n_nodes))

def _add_bias(self, edges):
Expand Down Expand Up @@ -253,15 +253,15 @@ def __init__(self, g, user_type, item_type, batch_size):
def __iter__(self):
while True:
heads = torch.randint(
0, self.g.number_of_nodes(self.item_type), (self.batch_size,)
0, self.g.num_nodes(self.item_type), (self.batch_size,)
)
tails = dgl.sampling.random_walk(
self.g,
heads,
metapath=[self.item_to_user_etype, self.user_to_item_etype],
)[0][:, 2]
neg_tails = torch.randint(
0, self.g.number_of_nodes(self.item_type), (self.batch_size,)
0, self.g.num_nodes(self.item_type), (self.batch_size,)
)

mask = tails != -1
Expand Down Expand Up @@ -324,10 +324,10 @@ def sample_from_item_pairs(self, heads, tails, neg_tails):
# Create a graph with positive connections only and another graph with negative
# connections only.
pos_graph = dgl.graph(
(heads, tails), num_nodes=self.g.number_of_nodes(self.item_type)
(heads, tails), num_nodes=self.g.num_nodes(self.item_type)
)
neg_graph = dgl.graph(
(heads, neg_tails), num_nodes=self.g.number_of_nodes(self.item_type)
(heads, neg_tails), num_nodes=self.g.num_nodes(self.item_type)
)
pos_graph, neg_graph = dgl.compact_graphs([pos_graph, neg_graph])
seeds = pos_graph.ndata[dgl.NID]
Expand Down Expand Up @@ -459,7 +459,7 @@ def track_time(data):
num_workers=num_workers,
)
dataloader_test = DataLoader(
torch.arange(g.number_of_nodes(item_ntype)),
torch.arange(g.num_nodes(item_ntype)),
batch_size=batch_size,
collate_fn=collator.collate_test,
num_workers=num_workers,
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/model_speed/bench_rgcn_hetero_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def track_time(data):
if "feat" in hg.nodes[ntype].data
else None
)
num_nodes[ntype] = hg.number_of_nodes(ntype)
num_nodes[ntype] = hg.num_nodes(ntype)

embed_layer = RelGraphEmbed(hg, device, n_hidden, num_nodes, node_feats)
model = EntityClassify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def track_time(data):
g.ndata["type_id"] = g.ndata[dgl.NID]
g.ndata["ntype"] = g.ndata[dgl.NTYPE]

node_ids = th.arange(g.number_of_nodes())
node_ids = th.arange(g.num_nodes())
# find out the target node ids
node_tids = g.ndata[dgl.NTYPE]
loc = node_tids == category_id
Expand All @@ -297,7 +297,7 @@ def track_time(data):
#
embed_layer = RelGraphEmbedLayer(
device,
g.number_of_nodes(),
g.num_nodes(),
node_tids,
num_of_ntype,
node_feats,
Expand All @@ -309,7 +309,7 @@ def track_time(data):
# all model params are in device.
model = EntityClassify(
device,
g.number_of_nodes(),
g.num_nodes(),
n_hidden,
num_classes,
num_rels,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def track_time(data, num_negs, batch_size):
iter_start = 3
iter_count = 10

n_edges = g.number_of_edges()
n_edges = g.num_edges()
train_seeds = np.arange(n_edges)

# Create PyTorch DataLoader for constructing blocks
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/benchmarks/multigpu/bench_multigpu_rgcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def run(proc_id, n_gpus, n_cpus, args, devices, dataset, split, queue=None):
#
embed_layer = RelGraphEmbedLayer(
dev_id,
g.number_of_nodes(),
g.num_nodes(),
node_tids,
num_of_ntype,
node_feats,
Expand All @@ -223,7 +223,7 @@ def run(proc_id, n_gpus, n_cpus, args, devices, dataset, split, queue=None):
# all model params are in device.
model = EntityClassify(
dev_id,
g.number_of_nodes(),
g.num_nodes(),
args.n_hidden,
num_classes,
num_rels,
Expand Down Expand Up @@ -438,7 +438,7 @@ def track_time(data, dgl_sparse):
node_feats = []
for ntype in hg.ntypes:
if len(hg.nodes[ntype].data) == 0 or args.node_feats is False:
node_feats.append(hg.number_of_nodes(ntype))
node_feats.append(hg.num_nodes(ntype))
else:
assert len(hg.nodes[ntype].data) == 1
feat = hg.nodes[ntype].data.pop("feat")
Expand All @@ -458,7 +458,7 @@ def track_time(data, dgl_sparse):
g.edata["etype"].share_memory_()
g.ndata["type_id"] = g.ndata[dgl.NID]
g.ndata["type_id"].share_memory_()
node_ids = th.arange(g.number_of_nodes())
node_ids = th.arange(g.num_nodes())

# find out the target node ids
node_tids = g.ndata[dgl.NTYPE]
Expand Down
18 changes: 9 additions & 9 deletions benchmarks/benchmarks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ def load_ogb_product():
splitted_idx["valid"],
splitted_idx["test"],
)
train_mask = torch.zeros((graph.number_of_nodes(),), dtype=torch.bool)
train_mask = torch.zeros((graph.num_nodes(),), dtype=torch.bool)
train_mask[train_nid] = True
val_mask = torch.zeros((graph.number_of_nodes(),), dtype=torch.bool)
val_mask = torch.zeros((graph.num_nodes(),), dtype=torch.bool)
val_mask[val_nid] = True
test_mask = torch.zeros((graph.number_of_nodes(),), dtype=torch.bool)
test_mask = torch.zeros((graph.num_nodes(),), dtype=torch.bool)
test_mask[test_nid] = True
graph.ndata["train_mask"] = train_mask
graph.ndata["val_mask"] = val_mask
Expand Down Expand Up @@ -241,11 +241,11 @@ def load_ogb_mag():
hg = dgl.heterograph(subgs)
hg.nodes["paper"].data["feat"] = hg_orig.nodes["paper"].data["feat"]
hg.nodes["paper"].data["labels"] = labels["paper"].squeeze()
train_mask = torch.zeros((hg.number_of_nodes("paper"),), dtype=torch.bool)
train_mask = torch.zeros((hg.num_nodes("paper"),), dtype=torch.bool)
train_mask[train_idx] = True
val_mask = torch.zeros((hg.number_of_nodes("paper"),), dtype=torch.bool)
val_mask = torch.zeros((hg.num_nodes("paper"),), dtype=torch.bool)
val_mask[val_idx] = True
test_mask = torch.zeros((hg.number_of_nodes("paper"),), dtype=torch.bool)
test_mask = torch.zeros((hg.num_nodes("paper"),), dtype=torch.bool)
test_mask[test_idx] = True
hg.nodes["paper"].data["train_mask"] = train_mask
hg.nodes["paper"].data["val_mask"] = val_mask
Expand Down Expand Up @@ -294,13 +294,13 @@ def load_nowplaying_rs():

# Assign user and movie IDs and use them as features (to learn an individual trainable
# embedding for each entity)
g.nodes[user_ntype].data["id"] = torch.arange(g.number_of_nodes(user_ntype))
g.nodes[item_ntype].data["id"] = torch.arange(g.number_of_nodes(item_ntype))
g.nodes[user_ntype].data["id"] = torch.arange(g.num_nodes(user_ntype))
g.nodes[item_ntype].data["id"] = torch.arange(g.num_nodes(item_ntype))

# Prepare torchtext dataset and vocabulary
fields = {}
examples = []
for i in range(g.number_of_nodes(item_ntype)):
for i in range(g.num_nodes(item_ntype)):
example = torchtext.data.Example.fromlist([], [])
examples.append(example)
textset = torchtext.data.Dataset(examples, fields)
Expand Down
4 changes: 2 additions & 2 deletions docs/source/gen_dataset_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
num_edges = []
for i in range(len(ds)):
g = extract_graph(ds[i])
num_nodes.append(g.number_of_nodes())
num_edges.append(g.number_of_edges())
num_nodes.append(g.num_nodes())
num_edges.append(g.num_edges())

gg = extract_graph(ds[0])
dd = {
Expand Down
6 changes: 3 additions & 3 deletions docs/source/guide/distributed-apis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ sampling APIs to support mini-batch training (see `Distributed sampling`_).

.. code:: python
print(g.number_of_nodes())
print(g.num_nodes())
Access node/edge data
^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -132,7 +132,7 @@ in the cluster even if the :class:`~dgl.distributed.DistTensor` object disappear

.. code:: python
tensor = dgl.distributed.DistTensor((g.number_of_nodes(), 10), th.float32, name='test')
tensor = dgl.distributed.DistTensor((g.num_nodes(), 10), th.float32, name='test')
.. note::

Expand Down Expand Up @@ -183,7 +183,7 @@ node embeddings. Creating distributed embeddings is very similar to creating dis
arr = th.zeros(shape, dtype=dtype)
arr.uniform_(-1, 1)
return arr
emb = dgl.distributed.DistEmbedding(g.number_of_nodes(), 10, init_func=initializer)
emb = dgl.distributed.DistEmbedding(g.num_nodes(), 10, init_func=initializer)
Internally, distributed embeddings are built on top of distributed tensors,
and, thus, has very similar behaviors to distributed tensors. For example, when
Expand Down
2 changes: 1 addition & 1 deletion docs/source/guide/distributed-hetero.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ and store it as node data of :math:`T0`.
.. code:: python
g.nodes['T0'].data['feat1'] = dgl.distributed.DistTensor(
(g.number_of_nodes('T0'), 1), th.float32, 'feat1',
(g.num_nodes('T0'), 1), th.float32, 'feat1',
part_policy=g.get_node_partition_policy('T0'))
The partition policies used for creating distributed tensors and embeddings are
Expand Down
2 changes: 1 addition & 1 deletion docs/source/guide/minibatch-edge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ and removing them.

.. code:: python
n_edges = g.number_of_edges()
n_edges = g.num_edges()
sampler = dgl.dataloading.as_edge_prediction_sampler(
sampler, exclude='reverse_id', reverse_eids=torch.cat([
torch.arange(n_edges // 2, n_edges), torch.arange(0, n_edges // 2)]))
Expand Down
4 changes: 2 additions & 2 deletions docs/source/guide/minibatch-inference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ on how messages are aggregated and combined as well.
"""
# Compute representations layer by layer
for l, layer in enumerate([self.conv1, self.conv2]):
y = torch.zeros(g.number_of_nodes(),
y = torch.zeros(g.num_nodes(),
self.hidden_features
if l != self.n_layers - 1
else self.out_features)
sampler = dgl.dataloading.MultiLayerFullNeighborSampler(1)
dataloader = dgl.dataloading.NodeDataLoader(
g, torch.arange(g.number_of_nodes()), sampler,
g, torch.arange(g.num_nodes()), sampler,
batch_size=batch_size,
shuffle=True,
drop_last=False)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/guide/minibatch-nn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ MFGs.
:attr:`block.dstnodes <dgl.DGLGraph.dstnodes>` for features on output nodes,
if the original graph has multiple node types.
- Replace
:meth:`g.number_of_nodes <dgl.DGLGraph.number_of_nodes>` with either
:meth:`g.num_nodes <dgl.DGLGraph.num_nodes>` with either
:meth:`block.number_of_src_nodes <dgl.DGLGraph.number_of_src_nodes>` or
:meth:`block.number_of_dst_nodes <dgl.DGLGraph.number_of_dst_nodes>` for the number of
input nodes or output nodes respectively.
Expand Down
6 changes: 3 additions & 3 deletions docs/source/guide_cn/distributed-apis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ DGL分布式模块的初始化

.. code:: python
print(g.number_of_nodes())
print(g.num_nodes())
访问节点/边数据
^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -107,7 +107,7 @@ DGL为分布式张量提供了类似于单机普通张量的接口,以访问

.. code:: python
tensor = dgl.distributed.DistTensor((g.number_of_nodes(), 10), th.float32, name='test')
tensor = dgl.distributed.DistTensor((g.num_nodes(), 10), th.float32, name='test')
**Note**: :class:`~dgl.distributed.DistTensor` 的创建是一个同步操作。所有训练器都必须调用创建,
并且只有当所有训练器都调用它时,此创建过程才能成功。
Expand Down Expand Up @@ -147,7 +147,7 @@ DGL提供 :class:`~dgl.distributed.DistEmbedding` 以支持需要节点嵌入的
arr = th.zeros(shape, dtype=dtype)
arr.uniform_(-1, 1)
return arr
emb = dgl.distributed.DistEmbedding(g.number_of_nodes(), 10, init_func=initializer)
emb = dgl.distributed.DistEmbedding(g.num_nodes(), 10, init_func=initializer)
在内部,分布式嵌入建立在分布式张量之上,因此,其行为与分布式张量非常相似。
例如,创建嵌入时,DGL会将它们分片并存储在集群中的所有计算机上。(分布式嵌入)可以通过名称唯一标识。
Expand Down
4 changes: 2 additions & 2 deletions docs/source/guide_cn/minibatch-custom-sampler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ DGL确保块的输出节点将始终出现在输入节点中。如下代码所
src = src[mask]
dst = dst[mask]
# 返回一个与初始图有相同节点的边界
frontier = dgl.graph((src, dst), num_nodes=g.number_of_nodes())
frontier = dgl.graph((src, dst), num_nodes=g.num_nodes())
return frontier
def __len__(self):
Expand Down Expand Up @@ -376,7 +376,7 @@ DGL确保块的输出节点将始终出现在输入节点中。如下代码所
new_edges_masks = {}
# 遍历所有边的类型
for etype in sg.canonical_etypes:
edge_mask = torch.zeros(sg.number_of_edges(etype))
edge_mask = torch.zeros(sg.num_edges(etype))
edge_mask.bernoulli_(self.p)
new_edges_masks[etype] = edge_mask.bool()
Expand Down
2 changes: 1 addition & 1 deletion docs/source/guide_cn/minibatch-edge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

.. code:: python
n_edges = g.number_of_edges()
n_edges = g.num_edges()
dataloader = dgl.dataloading.EdgeDataLoader(
g, train_eid_dict, sampler,
Expand Down
4 changes: 2 additions & 2 deletions docs/source/guide_cn/minibatch-inference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
""" 用该模块进行离线推断 """
# 逐层计算表示
for l, layer in enumerate([self.conv1, self.conv2]):
y = torch.zeros(g.number_of_nodes(),
y = torch.zeros(g.num_nodes(),
self.hidden_features
if l != self.n_layers - 1
else self.out_features)
sampler = dgl.dataloading.MultiLayerFullNeighborSampler(1)
dataloader = dgl.dataloading.NodeDataLoader(
g, torch.arange(g.number_of_nodes()), sampler,
g, torch.arange(g.num_nodes()), sampler,
batch_size=batch_size,
shuffle=True,
drop_last=False)
Expand Down
4 changes: 2 additions & 2 deletions docs/source/guide_cn/minibatch-nn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
:attr:`block.srcnodes <dgl.DGLGraph.srcnodes>`;对于输出节点特征,将
:attr:`g.nodes <dgl.DGLGraph.nodes>` 替换为
:attr:`block.dstnodes <dgl.DGLGraph.dstnodes>`。
- 对于输入节点数量,将 :meth:`g.number_of_nodes <dgl.DGLGraph.number_of_nodes>` 替换为
- 对于输入节点数量,将 :meth:`g.num_nodes <dgl.DGLGraph.num_nodes>` 替换为
:meth:`block.number_of_src_nodes <dgl.DGLGraph.number_of_src_nodes>` ;
对于输出节点数量,将 :meth:`g.number_of_nodes <dgl.DGLGraph.number_of_nodes>` 替换为
对于输出节点数量,将 :meth:`g.num_nodes <dgl.DGLGraph.num_nodes>` 替换为
:meth:`block.number_of_dst_nodes <dgl.DGLGraph.number_of_dst_nodes>` 。

异构图上的模型定制
Expand Down
Loading

0 comments on commit 3c8ac09

Please sign in to comment.