dgl.distributed

DGL分布式模块包含用于支持在机器集群上进行分布式图神经网络训练和推理的类和函数。

这包括几个子模块:

  • 分布式数据结构包括分布式图、分布式张量和分布式嵌入。

  • 分布式采样。

  • 在运行时分布式工作负载拆分。

  • 图分区。

初始化

initialize(ip_config[, max_queue_size, ...])

初始化DGL的分布式模块

分布式图

class dgl.distributed.DistGraph(graph_name, gpb=None, part_config=None)[source]

用于访问分布式图的类。

该类提供了DGLGraph API的一个子集,用于在分布式GNN训练和推理中访问分区图数据。因此,其主要用例是与分布式采样API一起工作,以生成小批量数据并在小批量数据上执行前向和后向计算。

该类可以在两种模式下运行:独立模式和分布式模式。

  • 当用户正常运行训练脚本时,DistGraph 将处于独立模式。 在此模式下,输入数据必须由 partition_graph() 构建,且只有一个分区。此模式 用于测试和调试目的。在此模式下,用户必须提供 part_config 以便 DistGraph 可以加载输入图。

  • 当用户使用分布式启动脚本运行训练脚本时,DistGraph将被设置为分布式模式。这用于实际的分布式训练。所有分区的数据都由DistGraph服务器加载,这些服务器由DGL的启动脚本创建。DistGraph与服务器连接以访问分区的图数据。

目前,DistGraph 服务器和客户端在分布式模式下运行在同一组机器上。DistGraph 使用共享内存来访问本地机器上的分区数据。这为分布式训练提供了最佳性能。

用户可能希望在独立的机器集上运行DistGraph服务器和客户端。 在这种情况下,用户可能希望通过在创建DistGraphServer时传递disable_shared_mem=False来禁用共享内存。当共享内存被禁用时, 用户必须传递一个分区书。

Parameters:
  • graph_name (str) – 图的名称。此名称必须与在dgl.distributed.partition.partition_graph()中用于分区图的名称相同。

  • gpb (GraphPartitionBook, 可选) – 分区书对象。通常,用户不需要提供分区书。 此参数仅在用户希望在不同的机器上运行服务器进程和训练器进程时才需要。

  • part_config (str, optional) – 由dgl.distributed.partition.partition_graph()生成的分区配置文件的路径。它用于独立模式。

示例

示例展示了在独立模式下创建DistGraph的过程。

>>> dgl.distributed.partition_graph(g, 'graph_name', 1, num_hops=1, part_method='metis',
...                                 out_path='output/')
>>> g = dgl.distributed.DistGraph('graph_name', part_config='output/graph_name.json')

该示例展示了在分布式模式下创建DistGraph的过程。

>>> g = dgl.distributed.DistGraph('graph-name')

下面的代码展示了使用DistGraph进行小批量训练。

>>> def sample(seeds):
...     seeds = th.LongTensor(np.asarray(seeds))
...     frontier = dgl.distributed.sample_neighbors(g, seeds, 10)
...     return dgl.to_block(frontier, seeds)
>>> dataloader = dgl.distributed.DistDataLoader(dataset=nodes, batch_size=1000,
...                                             collate_fn=sample, shuffle=True)
>>> for block in dataloader:
...     feat = g.ndata['features'][block.srcdata[dgl.NID]]
...     labels = g.ndata['labels'][block.dstdata[dgl.NID]]
...     pred = model(block, feat)

注意

DGL的分布式训练默认在同一组机器上运行服务器进程和训练器进程。如果用户需要在不同的机器组上运行它们,则需要手动设置服务器和训练器。该设置尚未完全测试。

barrier()[source]

所有客户端节点的屏障。

此API会阻塞当前进程,直到所有客户端都调用此API。 请谨慎使用此API。

property device

获取此图形的设备上下文。

示例

以下示例使用PyTorch后端。

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): ([0, 1, 1, 2], [0, 0, 2, 1])
... })
>>> print(g.device)
device(type='cpu')
>>> g = g.to('cuda:0')
>>> print(g.device)
device(type='cuda', index=0)
Return type:

设备上下文对象

property edata

返回所有边的数据视图。

Returns:

分布式图存储中的数据视图。

Return type:

EdgeDataView

edge_attr_schemes()[source]

返回边缘特征方案。

每个特征方案是一个命名的元组,用于存储边缘特征的形状和数据类型。

Returns:

边缘特征列的模式。

Return type:

dict 字符串到方案

示例

以下使用PyTorch后端。

>>> g.edge_attr_schemes()
{'h': Scheme(shape=(4,), dtype=torch.float32)}

另请参阅

node_attr_schemes

property edges

返回一个边的视图

property etypes

返回此图的边类型列表。

Return type:

liststr

示例

>>> g = DistGraph("test")
>>> g.etypes
['_E']
find_edges(edges, etype=None)[source]

给定一个边ID数组,返回源节点和目标节点ID数组 sds[i]d[i] 是边 eid[i] 的源节点和目标节点ID。

Parameters:
  • edges (Int Tensor) –

    每个元素都是一个ID。张量必须具有与图相同的设备类型

    和ID数据类型。

  • etype (str or (str, str, str), optional) –

    The type names of the edges. The allowed type name formats are:

    • (str, str, str) for source node type, edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns:

  • tensor – The source node ID array.

  • tensor – The destination node ID array.

get_edge_partition_policy(etype)[source]

获取边类型的分区策略。

在创建新的分布式张量时,我们需要提供一个分区策略,该策略指示如何在机器集群中分发分布式张量的数据。当我们在集群中加载分布式图时,我们已经为每种节点类型和每种边类型预定义了分区策略。通过提供边类型,我们可以引用为该边类型预定义的分区策略。

Parameters:

etype (str(str, str, str)) – 边的类型

Returns:

边缘类型的分区策略。

Return type:

PartitionPolicy

get_etype_id(etype)[source]

返回给定边类型的ID。

etype 也可以为 None。如果是这样,图中应该只有一种边类型。

Parameters:

etype (strtuplestr) – 边类型

Return type:

int

get_node_partition_policy(ntype)[source]

获取节点类型的分区策略。

在创建新的分布式张量时,我们需要提供一个分区策略,该策略指示如何在机器集群中分发分布式张量的数据。当我们在集群中加载分布式图时,我们已经为每种节点类型和每种边类型预定义了分区策略。通过提供节点类型,我们可以引用为该节点类型预定义的分区策略。

Parameters:

ntype (str) – 节点类型

Returns:

节点类型的分区策略。

Return type:

PartitionPolicy

get_ntype_id(ntype)[source]

返回给定节点类型的ID。

ntype 也可以是 None。如果是这样,图中应该只有一种节点类型。

Parameters:

ntype (str) – 节点类型

Return type:

int

get_partition_book()[source]

获取分区信息。

Returns:

存储所有图分区信息的对象。

Return type:

GraphPartitionBook

property idtype

图索引的数据类型

Returns:

th.int32/th.int64 或 tf.int32/tf.int64 等。

Return type:

后端数据类型对象

另请参阅

long, int

in_degrees(v='__ALL__')[source]

返回给定节点的入度。

它计算入度。 目前还不支持异构图。

Parameters:

v (node IDs) –

The node IDs. The allowed formats are:

  • int: A single node.

  • Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.

  • iterable[int]: Each element is a node ID.

If not given, return the in-degrees of all the nodes.

Returns:

The in-degree(s) of the node(s) in a Tensor. The i-th element is the in-degree of the i-th input node. If v is an int, return an int too.

Return type:

int or Tensor

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

查询所有节点。

>>> g.in_degrees()
tensor([0, 2, 1, 1])

查询节点1和2。

>>> g.in_degrees(torch.tensor([1, 2]))
tensor([2, 1])

另请参阅

out_degrees

property local_partition

返回客户端的本地分区

DistGraph 提供了分布式图的全局视图。在内部,如果它与服务器位于同一位置,它可能包含图的一个分区。当服务器和客户端在不同的机器集上运行时,这将返回 None。

Returns:

本地分区

Return type:

DGLGraph

property ndata

返回所有节点的数据视图。

Returns:

分布式图存储中的数据视图。

Return type:

节点数据视图

node_attr_schemes()[source]

返回节点特征方案。

每个特征方案是一个命名的元组,用于存储节点特征的形状和数据类型。

Returns:

节点特征列的模式。

Return type:

dict of str to schemes

示例

以下使用PyTorch后端。

>>> g.node_attr_schemes()
{'h': Scheme(shape=(4,), dtype=torch.float32)}

另请参阅

edge_attr_schemes

property nodes

返回一个节点视图

property ntypes

返回此图的节点类型列表。

Return type:

list of str

示例

>>> g = DistGraph("test")
>>> g.ntypes
['_U']
num_edges(etype=None)[source]

返回分布式图中的总边数。

Parameters:

etype (str or (str, str, str), optional) –

The type name of the edges. The allowed type name formats are:

  • (str, str, str) for source node type, edge type and destination node type.

  • or one str edge type name if the name can uniquely identify a triplet format in the graph.

If not provided, return the total number of edges regardless of the types in the graph.

Returns:

边的数量

Return type:

int

示例

>>> g = dgl.distributed.DistGraph('ogb-product')
>>> print(g.num_edges())
123718280
num_nodes(ntype=None)[source]

返回分布式图中节点的总数。

Parameters:

ntype (str, optional) – The node type name. If given, it returns the number of nodes of the type. If not given (default), it returns the total number of nodes of all types.

Returns:

节点数量

Return type:

int

示例

>>> g = dgl.distributed.DistGraph('ogb-product')
>>> print(g.num_nodes())
2449029
number_of_edges(etype=None)[source]

Alias of num_edges()

number_of_nodes(ntype=None)[source]

Alias of num_nodes()

out_degrees(u='__ALL__')[source]

返回给定节点的出度。

它计算出度。 目前还不支持异构图。

Parameters:

u (node IDs) –

The node IDs. The allowed formats are:

  • int: A single node.

  • Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.

  • iterable[int]: Each element is a node ID.

If not given, return the in-degrees of all the nodes.

Returns:

The out-degree(s) of the node(s) in a Tensor. The i-th element is the out-degree of the i-th input node. If v is an int, return an int too.

Return type:

int or Tensor

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

查询所有节点。

>>> g.out_degrees()
tensor([2, 2, 0, 0])

查询节点1和2。

>>> g.out_degrees(torch.tensor([1, 2]))
tensor([2, 0])

另请参阅

in_degrees

rank()[source]

当前 DistGraph 的排名。

这将返回一个唯一的数字,用于在所有客户端进程中识别DistGraph对象。

Returns:

当前DistGraph的排名。

Return type:

int

Distributed Tensor

class dgl.distributed.DistTensor(shape, dtype, name=None, init_func=None, part_policy=None, persistent=False, is_gdata=True, attach=True)[source]

分布式张量。

DistTensor 引用了一个分布式张量,该张量被分片并存储在一组机器集群中。 它具有与Pytorch Tensor相同的接口,用于访问其元数据(例如,形状和数据类型)。 为了访问分布式张量中的数据,它支持切片行和将数据写入行。 它不支持任何深度学习框架的操作符,例如加法和乘法。

目前,分布式张量被设计用于存储分布式图的节点数据和边数据。因此,它们的第一维必须是图中节点或边的数量。张量在第一维上根据节点或边的分区策略进行分片。当创建分布式张量时,如果未提供分区策略,则会根据第一维自动确定分区策略。如果第一维与节点类型的节点数量匹配,DistTensor 将使用该特定节点类型的分区策略;如果第一维与边类型的边数量匹配,DistTensor 将使用该特定边类型的分区策略。如果 DGL 无法自动确定分区策略(例如,多个节点类型或边类型具有相同数量的节点或边),用户必须显式提供分区策略。

分布式张量可以是有名称的或匿名的。 当分布式张量有名称时,如果persistent=True,张量可以是持久的。 通常,当DistTensor对象消失时,DGL会销毁系统中的分布式张量。 然而,即使DistTenor对象在训练器进程中消失,持久张量仍然存在于系统中。 持久张量的生命周期与DGL服务器相同。DGL不允许匿名张量是持久的。

当创建一个DistTensor对象时,它可能引用一个现有的分布式张量或创建一个新的张量。分布式张量通过传递给构造函数的名称来标识。如果名称存在,DistTensor将引用现有的张量。在这种情况下,形状和数据类型必须与现有张量匹配。如果名称不存在,将在kvstore中创建一个新的张量。

当创建一个分布式张量时,其值被初始化为零。用户可以定义一个初始化函数来控制值的初始化方式。初始化函数有两个输入参数:形状和数据类型,并返回一个张量。下面展示了一个初始化函数的示例:

def init_func(shape, dtype):
    return torch.ones(shape=shape, dtype=dtype)
Parameters:
  • shape (tuple) – 张量的形状。第一个维度必须是分布式图的节点数或边数。

  • dtype (dtype) – 张量的数据类型。数据类型必须是深度学习框架中的一种。

  • name (string, optional) – 嵌入的名称。该名称可以在系统中唯一标识嵌入,以便另一个DistTensor对象可以引用分布式张量。

  • init_func (callable, optional) – 用于初始化张量数据的函数。如果未提供初始化函数,嵌入值将初始化为零。

  • part_policy (PartitionPolicy, optional) – 张量行到集群中不同机器的分区策略。 目前,它仅支持节点分区策略或边分区策略。 系统会自动确定正确的分区策略。

  • persistent (bool) – 创建的张量是否在DistTensor对象销毁后仍然存在。

  • is_gdata (bool) – 创建的张量是否为ndata/edata。

  • attach (bool) – 是否将组ID附加到名称中以使其全局唯一。

示例

>>> init = lambda shape, dtype: th.ones(shape, dtype=dtype)
>>> arr = dgl.distributed.DistTensor((g.num_nodes(), 2), th.int32, init_func=init)
>>> print(arr[0:3])
tensor([[1, 1],
        [1, 1],
        [1, 1]], dtype=torch.int32)
>>> arr[0:3] = th.ones((3, 2), dtype=th.int32) * 2
>>> print(arr[0:3])
tensor([[2, 2],
        [2, 2],
        [2, 2]], dtype=torch.int32)

注意

创建DistTensor是一个同步操作。当一个训练器进程尝试创建一个DistTensor对象时,只有当所有训练器进程都执行相同的操作时,创建才会成功。

property dtype

返回分布式张量的数据类型。

Returns:

张量的数据类型。

Return type:

数据类型

property name

返回分布式张量的名称

Returns:

张量的名称。

Return type:

str

property part_policy

返回分区策略

Returns:

分布式张量的分区策略。

Return type:

PartitionPolicy

property shape

返回分布式张量的形状。

Returns:

分布式张量的形状。

Return type:

tuple

分布式节点嵌入

class dgl.distributed.DistEmbedding(num_embeddings, embedding_dim, name=None, init_func=None, part_policy=None)[source]

分布式节点嵌入。

DGL 提供了一个分布式嵌入来支持需要可学习嵌入的模型。 DGL 的分布式嵌入主要用于学习图模型的节点嵌入。 由于分布式嵌入是模型的一部分,它们通过小批量更新。 分布式嵌入必须由 DGL 的优化器更新,而不是 由深度学习框架(例如,Pytorch 和 MXNet)提供的优化器。

为了支持在具有许多节点的图上进行高效训练,嵌入支持稀疏更新。也就是说,只有参与小批量计算的嵌入才会被更新。请参考分布式优化器了解DGL中可用的优化器。

分布式嵌入以与dgl.distributed.DistTensor相同的方式分片并存储在一组机器中,不同之处在于分布式嵌入是可训练的。由于分布式嵌入以与分布式图的节点和边相同的方式分片,因此通常比深度学习框架提供的稀疏嵌入访问效率更高。

Parameters:
  • num_embeddings (int) – 嵌入的数量。目前,嵌入的数量必须与节点数量或边的数量相同。

  • embedding_dim (int) – The dimension size of embeddings.

  • name (str, optional) – 嵌入的名称。该名称可以在系统中唯一标识嵌入,以便另一个DistEmbedding对象可以引用相同的嵌入。

  • init_func (callable, optional) – The function to create the initial data. If the init function is not provided, the values of the embeddings are initialized to zero.

  • part_policy (PartitionPolicy, optional) – 分区策略,用于将嵌入分配到集群中的不同机器上。 目前,它仅支持节点分区策略或边分区策略。 系统会自动确定正确的分区策略。

示例

>>> def initializer(shape, dtype):
        arr = th.zeros(shape, dtype=dtype)
        arr.uniform_(-1, 1)
        return arr
>>> emb = dgl.distributed.DistEmbedding(g.num_nodes(), 10, init_func=initializer)
>>> optimizer = dgl.distributed.optim.SparseAdagrad([emb], lr=0.001)
>>> for blocks in dataloader:
...     feats = emb(nids)
...     loss = F.sum(feats + 1, 0)
...     loss.backward()
...     optimizer.step()

注意

当在正向计算中使用DistEmbedding对象时,用户必须随后调用 step()。否则, 将会出现一些内存泄漏。

分布式嵌入优化器

class dgl.distributed.optim.SparseAdagrad(params, lr, eps=1e-10)[source]

使用Adagrad算法的分布式节点嵌入优化器。

此优化器实现了用于优化dgl.distributed.DistEmbedding的分布式稀疏版本的Adagrad算法。稀疏意味着它只更新那些梯度有更新的嵌入,这些嵌入通常只占总嵌入的很小一部分。

Adagrad maintains a \(G_{t,i,j}\) for every parameter in the embeddings, where \(G_{t,i,j}=G_{t-1,i,j} + g_{t,i,j}^2\) and \(g_{t,i,j}\) is the gradient of the dimension \(j\) of embedding \(i\) at step \(t\).

注意:稀疏Adagrad优化器的支持是实验性的。

Parameters:
  • params (list[dgl.distributed.DistEmbedding]) – dgl.distributed.DistEmbedding 的列表。

  • lr (float) – The learning rate.

  • eps (float, Optional) – The term added to the denominator to improve numerical stability Default: 1e-10

load(f)

从每个等级的文件中加载优化器的本地状态。

注意:这需要在所有等级上调用。

Parameters:

f (Union[str, os.PathLike]) – 要加载的文件的路径。

另请参阅

save

save(f)

将本地state_dict保存到每个等级的磁盘上。

保存的字典包含两部分:

  • ‘params’: 优化器的超参数。

  • ‘emb_states’: partial optimizer states, each embedding contains 2 items:
    1. `ids`: 存储在此排名中的节点/边的全局ID。

    2. `states`: 与 `ids` 对应的状态数据。

注意:这需要在所有等级上调用。

Parameters:

f (Union[str, os.PathLike]) – 要保存文件的路径。

另请参阅

load

step()

阶跃函数。

在每批结束时调用step函数,将涉及小批量的嵌入梯度推送到DGL的服务器并更新嵌入。

class dgl.distributed.optim.SparseAdam(params, lr, betas=(0.9, 0.999), eps=1e-08)[source]

使用Adam算法的分布式节点嵌入优化器。

此优化器实现了分布式稀疏版本的Adam算法,用于优化dgl.distributed.DistEmbedding。稀疏意味着它只更新那些梯度有更新的嵌入,这些嵌入通常只占总嵌入的很小一部分。

Adam maintains a \(Gm_{t,i,j}\) and Gp_{t,i,j} for every parameter in the embeddings, where \(Gm_{t,i,j}=beta1 * Gm_{t-1,i,j} + (1-beta1) * g_{t,i,j}\), \(Gp_{t,i,j}=beta2 * Gp_{t-1,i,j} + (1-beta2) * g_{t,i,j}^2\), \(g_{t,i,j} = lr * Gm_{t,i,j} / (1 - beta1^t) / \sqrt{Gp_{t,i,j} / (1 - beta2^t)}\) and \(g_{t,i,j}\) is the gradient of the dimension \(j\) of embedding \(i\) at step \(t\).

注意:稀疏Adam优化器的支持是实验性的。

Parameters:
  • params (list[dgl.distributed.DistEmbedding]) – The list of dgl.distributed.DistEmbedding.

  • lr (float) – The learning rate.

  • betas (tuple[float, float], Optional) – Coefficients used for computing running averages of gradient and its square. Default: (0.9, 0.999)

  • eps (float, Optional) – The term added to the denominator to improve numerical stability Default: 1e-8

load(f)

从每个等级的文件中加载优化器的本地状态。

注意:这需要在所有等级上调用。

Parameters:

f (Union[str, os.PathLike]) – The path of the file to load from.

另请参阅

save

save(f)

将本地state_dict保存到每个等级的磁盘上。

保存的字典包含两部分:

  • ‘params’: 优化器的超参数。

  • ‘emb_states’: partial optimizer states, each embedding contains 2 items:
    1. `ids`: 存储在此排名中的节点/边的全局ID。

    2. `states`: state data corrseponding to `ids`.

注意:这需要在所有等级上调用。

Parameters:

f (Union[str, os.PathLike]) – 要保存的文件路径。

另请参阅

load

step()

阶跃函数。

在每批结束时调用step函数,将小批量中涉及的嵌入梯度推送到DGL的服务器并更新嵌入。

分布式工作负载分割

node_split(nodes[, partition_book, ntype, ...])

分割节点并返回本地等级的子集。

edge_split(edges[, partition_book, etype, ...])

分割边并返回本地排名的子集。

分布式采样

分布式数据加载器

class dgl.distributed.DistDataLoader(dataset, batch_size, shuffle=False, collate_fn=None, drop_last=False, queue_size=None)[source]

DGL 定制的多进程数据加载器。

DistDataLoader 提供了一个与 Pytorch 的 DataLoader 类似的接口,用于生成小批量数据并进行多进程处理。它利用由 dgl.distributed.initialize() 创建的工作进程来并行化采样。

Parameters:
  • dataset (a tensor) – 节点ID或边ID的张量。

  • batch_size (int) – 每批次加载的样本数量。

  • shuffle (bool, optional) – 设置为 True 以在每个时期重新洗牌数据(默认值:False)。

  • collate_fn (callable, optional) – 该函数通常用于对批次中节点的邻居或批次中边的端点节点进行采样。

  • drop_last (bool, optional) – 设置为 True 以丢弃最后一个不完整的批次,如果数据集大小不能被批次大小整除。如果 False 并且数据集的大小不能被批次大小整除,则最后一个批次将较小。(默认值:False

  • queue_size (int, optional) – 多进程队列的大小

示例

>>> g = dgl.distributed.DistGraph('graph-name')
>>> def sample(seeds):
...     seeds = th.LongTensor(np.asarray(seeds))
...     frontier = dgl.distributed.sample_neighbors(g, seeds, 10)
...     return dgl.to_block(frontier, seeds)
>>> dataloader = dgl.distributed.DistDataLoader(dataset=nodes, batch_size=1000,
                                                collate_fn=sample, shuffle=True)
>>> for block in dataloader:
...     feat = g.ndata['features'][block.srcdata[dgl.NID]]
...     labels = g.ndata['labels'][block.dstdata[dgl.NID]]
...     pred = model(block, feat)

注意

在使用多进程执行DGL的分布式采样时,用户必须使用此类而不是Pytorch的DataLoader,因为DGL的RPC要求所有进程在调用任何DGL的分布式API之前与服务器建立连接。因此,此数据加载器使用在dgl.distributed.initialize()中创建的工作进程。

注意

此数据加载器不保证迭代顺序。例如, 如果数据集 = [1, 2, 3, 4],批量大小 = 2 且 shuffle = False,则 [1, 2] 和 [3, 4] 的顺序不保证。

分布式图采样操作符

sample_neighbors(g, nodes, fanout[, ...])

从分布式图中给定节点的邻居中采样。

sample_etype_neighbors(g, nodes, fanout[, ...])

从分布式图中给定节点的邻居中采样。

find_edges(g, edge_ids)

给定一个边ID数组,从分布式图中返回源节点和目标节点ID数组 sd

in_subgraph(g, nodes)

返回由给定节点的入边诱导的子图。

分区

图分区手册

class dgl.distributed.GraphPartitionBook[source]

图分区书的基础类。

对于分布式训练,图被分割成多个部分并加载到多台机器中。分区书包含在集群中定位节点和边的所有必要信息。

分区手册包含各种分区信息,包括

  • 分区数量,

  • 节点或边所属的分区ID,

  • 分区所拥有的节点ID和边ID。

  • 分区中节点和边的本地ID。

目前,只有一个类实现了GraphPartitionBookRangePartitionBook。它基于一些小的元数据计算节点/边ID和分区ID之间的映射,因为节点/边已被重新标记,以使同一分区中的ID落在连续的ID范围内。

当图被分区时,会自动构建一个图分区书。 当加载图分区时,也会加载图分区书。 请参阅 partition_graph(), load_partition()load_partition_book() 了解更多详情。

property canonical_etypes

获取规范边类型的列表

Returns:

规范类型的列表

Return type:

list[(str, str, str)]

eid2localeid(eids, partid, etype)[source]

获取给定分区内的本地边ID。

Parameters:
  • eids (tensor) – 全局边ID

  • partid (int) – 分区ID

  • etype (str or (str, str, str)) – The edge type

Returns:

本地边缘ID

Return type:

张量

eid2partid(eids, etype)[source]

从全局边缘ID到分区ID

Parameters:
  • eids (tensor) – 全局边ID

  • etype (str or (str, str, str)) – The edge type

Returns:

分区ID

Return type:

张量

map_to_homo_eid(ids, etype)[source]

将类型化的边ID和类型ID映射到同质的边ID。

Parameters:
  • ids (tensor) – 类型化的边ID

  • etype (str or (str, str, str)) – The edge type

Returns:

同质边ID。

Return type:

张量

map_to_homo_nid(ids, ntype)[source]

将类型节点ID和类型ID映射到同质节点ID。

Parameters:
  • ids (tensor) – 按类型划分的节点ID

  • ntype (str) – 节点类型

Returns:

同质节点ID。

Return type:

张量

map_to_per_etype(ids)[source]

将同质边ID映射为类型化ID和边类型。

Parameters:

ids (tensor) – 同质边的ID。

Returns:

边的类型ID和按类型划分的边ID。

Return type:

(tensor, tensor)

map_to_per_ntype(ids)[source]

将同质节点ID映射为类型化ID和节点类型。

Parameters:

ids (tensor) – 同质节点ID。

Returns:

节点类型ID和按类型划分的节点ID。

Return type:

(tensor, tensor)

metadata()[source]

返回分区元数据。

元数据包括:

  • 机器ID。

  • 每个分区的节点和边的数量。

示例

>>> print(g.get_partition_book().metadata())
>>> [{'machine_id' : 0, 'num_nodes' : 3000, 'num_edges' : 5000},
...  {'machine_id' : 1, 'num_nodes' : 2000, 'num_edges' : 4888},
...  ...]
Returns:

每个分区的元数据。

Return type:

list[dict[str, any]]

nid2localnid(nids, partid, ntype)[source]

获取给定分区内的本地节点ID。

Parameters:
  • nids (tensor) – 全局节点ID

  • partid (int) – partition ID

  • ntype (str) – The node type

Returns:

本地节点ID

Return type:

张量

nid2partid(nids, ntype)[source]

从全局节点ID到分区ID

Parameters:
  • nids (tensor) – 全局节点ID

  • ntype (str) – The node type

Returns:

分区ID

Return type:

张量

num_partitions()[source]

返回分区的数量。

Returns:

分区数量

Return type:

int

property partid

获取当前分区ID

Returns:

当前机器的分区ID

Return type:

int

partid2eids(partid, etype)[source]

从分区ID到全局边ID

Parameters:
  • partid (int) – 分区ID

  • etype (str or (str, str, str)) – The edge type

Returns:

边ID

Return type:

张量

partid2nids(partid, ntype)[source]

从分区ID到全局节点ID

Parameters:
  • partid (int) – 分区ID

  • ntype (str) – The node type

Returns:

节点ID

Return type:

张量

shared_memory(graph_name)[source]

将分区书移动到共享内存。

Parameters:

graph_name (str) – 图名称。此名称将用于从共享内存中读取分区书在另一个进程中。

class dgl.distributed.PartitionPolicy(policy_str, partition_book)[source]

这定义了一个分布式张量或分布式嵌入的分区策略。

当DGL对张量进行分片并将它们存储在机器集群中时,它需要将张量的行映射到集群中的机器的分区策略。

虽然可以定义任意的分区策略,但DGL目前支持两种分区策略来将节点和边映射到机器上。要从图分区书中定义分区策略,用户需要指定策略名称(‘node’或‘edge’)。

Parameters:
  • policy_str (str) – 分区策略名称,例如,‘edge~_N:_E:_N’ 或 ‘node~_N’。

  • partition_book (GraphPartitionBook) – 一个图分区书

get_part_size()[source]

获取当前分区的数据大小。

Returns:

数据大小

Return type:

int

get_size()[source]

获取数据的完整大小。

Returns:

数据大小

Return type:

int

property part_id

获取分区ID

Returns:

分区ID

Return type:

int

property partition_book

获取分区书籍

Returns:

图分区书籍

Return type:

GraphPartitionBook

property policy_str

获取策略名称

Returns:

分区策略的名称。

Return type:

str

to_local(id_tensor)[source]

将全局ID映射到本地ID。

Parameters:

id_tensor (tensor) – 全局ID张量

Returns:

本地ID张量

Return type:

张量

to_partid(id_tensor)[source]

将全局ID映射到分区ID。

Parameters:

id_tensor (tensor) – 全局ID张量

Returns:

分区ID

Return type:

张量

拆分和加载分区

load_partition(part_config, part_id[, ...])

从数据路径加载分区的数据。

load_partition_feats(part_config, part_id[, ...])

从分区加载节点/边特征数据。

load_partition_book(part_config, part_id)

从分区配置文件中加载图分区书。

partition_graph(g, graph_name, num_parts, ...)

为分布式训练分割图并将分割存储在文件中。

dgl_partition_to_graphbolt(part_config, *[, ...])

将dgl的分区转换为GraphBolt的FusedCSCSamplingGraph。