CPUCachedFeature
- class dgl.graphbolt.CPUCachedFeature(fallback_feature: Feature, cache: CPUFeatureCache, offset: int = 0)[源代码]
基类:
Feature
围绕后备特征的 CPU 缓存特征。使用 cpu_cached_feature 构造此类的实例。
- 参数:
- read(ids: Tensor | None = None)[源代码]
按索引读取特征。
- 参数:
ids (torch.Tensor, optional) – 特征的索引。如果指定,则仅读取特征的指定索引。如果为 None,则返回整个特征。
- 返回:
读取的特征。
- 返回类型:
torch.Tensor
- read_async(ids: Tensor)[源代码]
异步按索引读取特征。
- 参数:
ids (torch.Tensor) – 特征的索引。仅读取特征的指定索引。
- 返回:
返回的生成器对象在
read_async_num_stages(ids.device)
次调用时返回一个 future。可以通过调用返回的 future 对象的.wait()
来访问返回结果。多次调用.wait()
是未定义的行为。- 返回类型:
生成器对象。
示例
>>> import dgl.graphbolt as gb >>> feature = gb.Feature(...) >>> ids = torch.tensor([0, 2]) >>> for stage, future in enumerate(feature.read_async(ids)): ... pass >>> assert stage + 1 == feature.read_async_num_stages(ids.device) >>> result = future.wait() # result contains the read values.
- read_async_num_stages(ids_device: device)[源代码]
read_async 操作的阶段数。有关其用法,请参见 read_async 函数。当 read_async 与位于 ids_device 上的张量一起使用时,此函数需要返回 yield 操作的次数。
- 参数:
ids_device (torch.device) – 传递给 read_async 的 ids 参数的设备。
- 返回:
read_async 操作的阶段数。
- 返回类型:
- update(value: Tensor, ids: Tensor | None = None)[源代码]
更新特征。
- 参数:
value (torch.Tensor) – 特征的更新值。
ids (torch.Tensor, optional) – 要更新的特征索引。如果指定,则仅更新特征的指定索引。对于特征,ids[i] 行将更新为 value[i]。因此索引和值必须具有相同的长度。如果为 None,则将更新整个特征。
- property cache_size_in_bytes
返回缓存占用的字节大小。
- property miss_rate
返回自创建以来的缓存未命中率。