实用工具
桶
基类:NamedTuple
用于存储桶边界和大小的容器。
属性
名称 | 类型 | 描述 |
---|---|---|
bucket_boundaries |
张量
|
包含所有桶边界的 1D 张量。 |
bucket_sizes |
张量
|
每个桶中的元素数量。 |
源代码位于 bionemo/size_aware_batching/utils.py
30 31 32 33 34 35 36 37 38 39 |
|
collect_cuda_peak_alloc(dataset, work, device, cleanup=None)
收集给定工作流程的 CUDA 峰值内存分配统计信息。
此函数迭代提供的数据集,将给定的特征函数应用于每个数据点,并记录此过程中的峰值 CUDA 内存分配。从数据点提取的特征与其相应的内存使用统计信息一起收集。
请注意,由于未初始化的数据(例如,内部 PyTorch 缓冲区),工作流程的最初几次迭代可能会导致较小的内存分配。因此,用户可能希望在分析结果时跳过这些初始数据点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
dataset
|
Iterable[Data]
|
包含输入数据的可迭代对象。 |
必需 |
work
|
Callable[[Data], Feature]
|
一个函数,它接受一个数据点并返回其对应的特征。这是主要计算发生和内存分配被跟踪的地方。 |
必需 |
device
|
device
|
目标 Torch CUDA 设备。 |
必需 |
cleanup
|
Optional[Callable[[], None]]
|
一个函数,在每次迭代后调用,以执行任何必要的清理。 |
无
|
返回值
类型 | 描述 |
---|---|
Tuple[List[Feature], List[int]]
|
一个元组,包含收集的特征及其对应的内存使用统计信息。 |
Raises
类型 | 描述 |
---|---|
ValueError
|
如果提供的设备不是 CUDA 设备。 |
示例
>>> import torch
>>> from bionemo.size_aware_batching.utils import collect_cuda_peak_alloc
>>> # prepare dataset, model and other components of a workflow
>>> # for which the user want to collect CUDA peak memory allocation statistics
>>> dataset, model, optimizer = ...
>>> # Set the target Torch CUDA device.
>>> device = torch.device("cuda:0")
>>> model = model.to(device)
>>> # Define a function that takes an element of the dataset as input and
>>> # do a training step
>>> def work(data):
... # example body of a training loop
... optimizer.zero_grad()
... output = model(data.to(device))
... loss = compute_loss(output)
... loss.backward()
... optimizer.step()
... # extract the feature for later to be modeled or analyzed
... return featurize(data)
>>> # can optionally use a cleanup function to release the references
>>> # hold during the work(). This cleanup function will be called
>>> # at the end of each step before garbage collection and memory allocations measurement
>>> def cleanup():
... model.zero_grad(set_to_none=True)
>>> # Collect features (i.e., model outputs) and memory usage statistics for the workflow.
>>> features, alloc_peaks = collect_cuda_peak_alloc(
... dataset=batches,
... work=work,
... device=device,
... cleanup=cleanup,
... )
>>> # use features and alloc_peaks as needed, e.g., fit a model
>>> # that can use these statistics to predict memory usage
>>> memory_model = ...
>>> memory_model.fit(features, alloc_peaks)
源代码位于 bionemo/size_aware_batching/utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
create_buckets(sizes, max_width, min_bucket_count)
为整数列表创建桶,预定义最大间隔宽度和最小桶计数。
它将返回一个命名元组,其中包含桶边界和实际桶大小。例如,torch.tensor([0, 5, 7]), torch.tensor([3,2]):指定 2 个桶:一个范围为 0<= sizes < 5,宽度=5,包含 3 个元素;另一个范围为 5 <= sizes < 7,宽度=2,包含 2 个元素。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
sizes
|
张量
|
一个 1D 整数张量。 |
必需 |
max_width
|
int
|
桶的最大宽度,应为正整数。 |
必需 |
min_bucket_count
|
int
|
桶的最小计数,应为正整数。如果桶的宽度达到 max_width,则桶大小可能小于 min_bucket_count。 |
必需 |
Raises
类型 | 描述 |
---|---|
ValueError
|
如果提供的 sizes 为空或不是整数。 |
ValueError
|
如果 max_width 不是正整数或 min_bucket_count 不是正整数。 |
返回值
类型 | 描述 |
---|---|
桶
|
一个命名元组,其中包含升序排列的桶边界和每个桶中的元素数量。 |
示例
>>> import torch
>>> from bionemo.size_aware_batching.utils import create_buckets
>>> sizes = torch.tensor([1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 22, 22, 22, 22])
>>> buckets = create_buckets(sizes, max_width=5, min_bucket_count=10)
>>> # 5 buckets: 1 <= sizes < 6, 6 <= sizes < 11, 11 <= sizes < 16, 16 <= sizes < 21, 21 <= sizes < 23
>>> print(buckets.bucket_boundaries)
tensor([ 1, 6, 11, 16, 21, 23])
>>> # each with 12, 0, 0, 0, 4 elements respectively.
>>> print(buckets.bucket_sizes)
tensor([12, 0, 0, 0, 4])
>>> sizes = torch.arange(20)
>>> # min_bucket_count is used to control bucket size
>>> buckets = create_buckets(sizes, max_width=10, min_bucket_count=5)
>>> print(buckets.bucket_boundaries)
tensor([ 0, 5, 10, 15, 20])
>>> print(buckets.bucket_sizes)
tensor([5, 5, 5, 5])
源代码位于 bionemo/size_aware_batching/utils.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|