0


深入理解GPU内存分配:机器学习工程师的实用指南与实验

给定一个模型架构、数据类型、输入形状和优化器,你能否计算出前向传播和反向传播所需的GPU内存量?要回答这个问题,我们需要将流程分解为基本组件,并从底层理解内存需求。以下实验(可以在Google Colab上运行)将帮助你理解核心概念。

预留与分配

PyTorch预留了更多内存,但只分配所需的内存。这样做是为了在需要更多内存时能够快速分配,而不是进行昂贵的预留操作。我们只关心内存分配,而不关心预留。

  1. deftest_reservation_vs_allocation():
  2. print(f"Base memory reserved: {torch.cuda.memory_reserved(device_id)}")
  3. print(f"Base memory allocated: {torch.cuda.memory_allocated(device_id)}")
  4. # Allocate some memory
  5. x=torch.randn((1024,), dtype=torch.float32, device=device)
  6. print(f"Memory after allocation (reserved): {torch.cuda.memory_reserved(device_id)}")
  7. print(f"Memory after allocation (allocated): {torch.cuda.memory_allocated(device_id)}")
  8. # Cleanup
  9. delx
  10. print(f"Memory after cleanup (reserved): {torch.cuda.memory_reserved(device_id)}")
  11. print(f"Memory after cleanup (allocated): {torch.cuda.memory_allocated(device_id)}")
  12. torch.cuda.empty_cache()
  13. print(f"Memory after empty_cache (reserved): {torch.cuda.memory_reserved(device_id)}")
  14. print(f"Memory after empty_cache (allocated): {torch.cuda.memory_allocated(device_id)}")
  15. """
  16. Output:
  17. Base memory reserved: 0
  18. Base memory allocated: 0
  19. Memory after allocation (reserved): 2097152
  20. Memory after allocation (allocated): 4096
  21. Memory after cleanup (reserved): 2097152
  22. Memory after cleanup (allocated): 0
  23. Memory after empty_cache (reserved): 0
  24. Memory after empty_cache (allocated): 0
  25. """

当删除变量x或当x超出作用域时,x的内存被释放,但仍然为将来使用而预留。只有在调用

  1. torch.cuda.empty_cache()

时,才会释放预留的内存。

这里的

  1. torch.cuda.memory_allocated()

将返回PyTorch在此进程上分配的内存。如果有另一个进程正在使用一些GPU内存,将返回0。为了获取真实的GPU内存使用情况,可以使用以下函数。

  1. importsubprocess
  2. defget_gpu_memory_used(gpu_id):
  3. """
  4. Returns the amount of memory used on the specified GPU in bytes.
  5. Parameters:
  6. gpu_id (int): The ID of the GPU (e.g., 0 for "cuda:0", 1 for "cuda:1").
  7. Returns:
  8. int: The amount of memory used on the GPU in bytes.
  9. """
  10. try:
  11. # Run the nvidia-smi command to get memory usage
  12. result=subprocess.run(
  13. ["nvidia-smi", "--query-gpu=memory.used", "--format=csv,nounits,noheader", f"--id={gpu_id}"],
  14. stdout=subprocess.PIPE,
  15. text=True
  16. )
  17. # Get the used memory in MiB from the result
  18. used_memory_mib=int(result.stdout.strip())
  19. # Convert MiB to bytes (1 MiB = 1024 * 1024 bytes)
  20. used_memory_bytes=used_memory_mib*1024*1024
  21. returnused_memory_bytes
  22. exceptExceptionase:
  23. print(f"Error occurred: {e}")
  24. returnNone

数据类型

  1. float32

需要4字节的内存,

  1. bfloat16

需要2字节,我们可以绘制一些数据类型所需的内存图。

图1:不同数据类型的内存分配

  1. deftest_dtype_memory_allocation():
  2. dtypes= [torch.float32, torch.float16, torch.bfloat16, torch.int32, torch.int64, torch.uint8, torch.int8, torch.uint16]
  3. memories= []
  4. fordtypeindtypes:
  5. base_memory=get_gpu_memory_used(device_id)
  6. x=torch.ones((1024,), dtype=dtype, device=device)
  7. memory_after_allocation=get_gpu_memory_used(device_id)
  8. memories.append((memory_after_allocation-base_memory) //1024)
  9. delx
  10. torch.cuda.empty_cache()
  11. fig=plt.figure(figsize=(7, 4))
  12. fig.set_tight_layout(True)
  13. plt.bar([str(d) fordindtypes], memories)
  14. plt.xlabel("Data type")
  15. plt.ylabel("Bytes per element")
  16. plt.title("Memory allocation for different data types")
  17. plt.xticks(rotation=45)
  18. plt.show()

内存块

内存以512字节的块分配。当创建一个张量时,它被分配到下一个可用的块中。对于形状为(800,)的float32张量,不是分配800 * 4 = 3200字节,而是分配3584(512 * 7)字节。

图2:不同张量大小的内存分配。

  1. deftest_memory_allocation_relationship():
  2. """
  3. For different sizes of tensors, check the memory allocated on GPU.
  4. """
  5. memories= []
  6. sizes=1050
  7. foriintqdm(range(sizes)):
  8. base_memory=get_gpu_memory_used(device_id)
  9. x=torch.randn((i,), dtype=torch.float32, device=device)
  10. memory_after_allocation=get_gpu_memory_used(device_id)
  11. memories.append(memory_after_allocation-base_memory)
  12. delx
  13. torch.cuda.empty_cache()
  14. plt.plot(memories)
  15. plt.xlabel("Size of float32 tensor")
  16. plt.ylabel("Memory allocated (bytes)")
  17. plt.title("Memory allocation for different tensor sizes")
  18. plt.show()

可训练参数(单个线性层前向传播)

接下来我们将看一个单一的线性层。进行前向传播,并计算所需的内存。

  1. deftest_single_linear_layer_forward_allocation():
  2. # Disable cublas
  3. # import os; os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":0:0"
  4. print(f"Base memory: {torch.cuda.memory_allocated(device_id)}")
  5. model=nn.Linear(256, 250, device=device, dtype=torch.float32)
  6. print(f"Memory after model allocation: {torch.cuda.memory_allocated(device_id)}")
  7. x=torch.randn((1, 256,), dtype=torch.float32, device=device)
  8. print(f"Memory after input allocation: {torch.cuda.memory_allocated(device_id)}")
  9. y=model(x)
  10. final_memory=torch.cuda.memory_allocated(device_id)
  11. print(f"Memory after forward pass: {final_memory}")
  12. # Memory calculations
  13. w_mem=len(model.weight.flatten()) *model.weight.dtype.itemsize
  14. # Get the higher multiple of 512
  15. w_mem_as_chunks= (w_mem+511) //512*512
  16. print(f"{model.weight.shape=}, {w_mem=}, {w_mem_as_chunks=}")
  17. b_mem=len(model.bias) *model.bias.dtype.itemsize
  18. b_mem_as_chunks= (b_mem+511) //512*512
  19. print(f"{model.bias.shape=}, {b_mem=}, {b_mem_as_chunks=}")
  20. x_mem= (len(x.flatten()) *x.dtype.itemsize+511) //512*512
  21. y_mem= (len(y.flatten()) *y.dtype.itemsize+511) //512*512
  22. print(f"{x_mem=}, {y_mem=}")
  23. total_memory_expected=w_mem_as_chunks+b_mem_as_chunks+x_mem+y_mem
  24. cublas_workspace_size=8519680
  25. memory_with_cublas=total_memory_expected+cublas_workspace_size
  26. print(f"{total_memory_expected=}, {memory_with_cublas=}")
  27. assertfinal_memory==memory_with_cublas
  28. delmodel, x, y
  29. torch.cuda.empty_cache()
  30. print(f"Memory after cleanup: {torch.cuda.memory_allocated(device_id)}")
  31. torch._C._cuda_clearCublasWorkspaces()
  32. print(f"Memory after clearing cublas workspace: {torch.cuda.memory_allocated(device_id)}")
  33. """
  34. Output:
  35. Base memory: 0
  36. Memory after model allocation: 257024
  37. Memory after input allocation: 258048
  38. Memory after forward pass: 8778752
  39. model.weight.shape=torch.Size([250, 256]), w_mem=256000, w_mem_as_chunks=256000
  40. model.bias.shape=torch.Size([250]), b_mem=1000, b_mem_as_chunks=1024
  41. x_mem=1024, y_mem=1024
  42. total_memory_expected=259072, memory_with_cublas=8778752
  43. Memory after cleanup: 8519680
  44. Memory after clearing cublas workspace: 0
  45. """
  1. model

有一个形状为(256, 250)的float32

  1. weight

矩阵,占用(256 * 250 * 4) = 256,000字节,这正好是内存块大小512的倍数(512 * 500 = 256,000)。但是

  1. bias

有250个float32需要占用(250 * 4) = 1000字节。而512的更高倍数是2,(512 * 2) = 1024字节。

  1. x

  1. y

是形状为(256,)的张量,所以它们各占用1024字节。总内存 =

  1. weight

+

  1. bias

+

  1. x

+

  1. y

当我们将所有内容加起来时,应该得到259,072字节(256,000 + 1024 + 1024 + 1024)。但是实际观察到的大小是8,778,752字节。这额外的8,519,680字节来自分配cuBLAS工作空间。

这是为快速矩阵乘法操作预留的内存空间。对于某些matmul操作,会分配一个新的8,519,680字节的块。这个大小可能会根据GPU和Python环境而变化。当调用

  1. torch.cuda.empty_cache()

时,cublas内存不会消失。它需要

  1. torch._C._cuda_clearCublasWorkspaces()

来实际清除它。也可以设置环境变量

  1. os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":0:0"

来禁用cublas工作空间。但这可能是一种以牺牲执行速度为代价来优化内存的方法,所以我们使用默认就好。

梯度(单个线性层反向传播)

使用相同的模型,运行

  1. loss.backward()

。为简单起见假设损失为

  1. loss = y.sum()

  1. deftest_single_linear_layer_backward_allocation():
  2. print(f"Base memory: {torch.cuda.memory_allocated(device_id)}")
  3. model=nn.Linear(256, 250, device=device, dtype=torch.float32)
  4. x=torch.randn((1, 256,), dtype=torch.float32, device=device)
  5. y=model(x)
  6. print(f"Memory after forward pass: {torch.cuda.memory_allocated(device_id)}")
  7. y.sum().backward()
  8. final_memory=torch.cuda.memory_allocated(device_id)
  9. print(f"Memory after backward pass: {final_memory}")
  10. # Memory calculations
  11. next_chunk=lambdan: (n+511) //512*512
  12. units=model.weight.dtype.itemsize # 4 bytes for float32
  13. mem=next_chunk(len(model.weight.flatten()) *units)
  14. mem+=next_chunk(len(model.bias) *units)
  15. print(f"Excepted model memory: {mem}")
  16. x_mem=next_chunk(len(x.flatten()) *units)
  17. y_mem=next_chunk(len(y.flatten()) *units)
  18. print(f"{x_mem=}, {y_mem=}")
  19. mem+=x_mem+y_mem
  20. # Gradient memory
  21. w_grad_mem=next_chunk(len(model.weight.grad.flatten()) *units)
  22. b_grad_mem=next_chunk(len(model.bias.grad.flatten()) *units)
  23. print(f"{model.weight.grad.shape=}, {w_grad_mem=}")
  24. print(f"{model.bias.grad.shape=}, {b_grad_mem=}")
  25. mem+=w_grad_mem+b_grad_mem
  26. mem+=2*8519680 # cublas_size doubled
  27. print(f"Total memory expected: {mem}")
  28. assertfinal_memory==mem
  29. delmodel, x, y
  30. torch.cuda.empty_cache()
  31. print(f"Memory after cleanup: {torch.cuda.memory_allocated(device_id)}")
  32. torch._C._cuda_clearCublasWorkspaces()
  33. print(f"Memory after clearing cublas workspace: {torch.cuda.memory_allocated(device_id)}")
  34. """
  35. Output:
  36. Base memory: 0
  37. Memory after forward pass: 8778752
  38. Memory after backward pass: 17555456
  39. Excepted model memory: 257024
  40. x_mem=1024, y_mem=1024
  41. model.weight.grad.shape=torch.Size([250, 256]), w_grad_mem=256000
  42. model.bias.grad.shape=torch.Size([250]), b_grad_mem=1024
  43. Total memory expected: 17555456
  44. Memory after cleanup: 17039360
  45. Memory after clearing cublas workspace: 0
  46. """

由于每个具有

  1. requires_grad=True

的模型参数都会有一个

  1. .grad

成员来存储底层张量的梯度,所以模型的大小会翻倍。

这次分配了2个cublas工作空间内存块,假设一个用于前向传播,一个用于反向传播。此时cublas何时确切地分配新块还不确定。

中间张量(多层前馈网络)

当模型在推理模式下运行时,没有自动求导图,不需要存储中间张量。所以内存量只是简单地将每一层的内存相加。

在需要跟踪计算图的训练模式下情况会有所不同。当有多个串行应用的操作时,比如在前馈网络或任何深度网络中,自动求导图需要记住这些操作的中间张量。存储需求取决于它们的偏导数操作的性质。这些中间张量在反向传播过程中从内存中清除。我们看一些例子:

  1. x

是输入,

  1. w

是需要梯度的参数(

  1. w.requires_grad = True

)。

  • x @ w不需要额外的存储。偏导数x已经存储。但是当x是某个输出,如x = u * w1时,x也需要被存储。
  • x + w也不需要存储,因为对w的偏导数是0。
  • (x * 2) @ w将需要存储操作数x * 2,因为它将用于找到梯度。
  • (((x + 2) @ w1) + 3) * w2是一个有趣的案例,模仿了2层。 - 对于关于w1的偏导数,我们需要存储x + 2 - 对于关于w2的偏导数,我们需要存储((x + 2) @ w1) + 3

让我们看看更深网络的实现:

  1. deftest_multi_layer_forward():
  2. print(f"Base memory: {torch.cuda.memory_allocated(device_id)}")
  3. inference_mode=False
  4. n_layers=1
  5. model=nn.Sequential(*[
  6. nn.Sequential(
  7. nn.Linear(200, 100),
  8. nn.ReLU(), # No trainable params
  9. nn.Linear(100, 200),
  10. nn.Sigmoid(), # No trainable params
  11. )
  12. for_inrange(n_layers)
  13. ]).to(device_id)
  14. batch_size=5
  15. x=torch.randn((batch_size, 200), device=device_id)
  16. withtorch.inference_mode(inference_mode):
  17. y=model(x)
  18. final_memory=torch.cuda.memory_allocated(device_id)
  19. print(f"Memory after forward pass: {final_memory}")
  20. # Computed memory
  21. next_chunk=lambdan: (n+511) //512*512
  22. mem=0
  23. unit=model[0][0].weight.dtype.itemsize
  24. forblockinmodel:
  25. forlayerinblock:
  26. ifisinstance(layer, nn.Linear):
  27. mem+=next_chunk(len(layer.weight.flatten()) *unit)
  28. mem+=next_chunk(len(layer.bias) *unit)
  29. ifnotinference_mode:
  30. # Gotta store the input
  31. mem+=next_chunk(layer.in_features*batch_size*unit)
  32. mem+=next_chunk(len(y.flatten()) *unit)
  33. mem+=8519680 # cublas_size
  34. ifinference_mode:
  35. mem+=next_chunk(len(y.flatten()) *unit)
  36. print(f"Total memory expected: {mem}")
  37. assertfinal_memory==mem

在像BatchNorm1d、LayerNorm、RMSNorm这样的归一化层中,在与参数

  1. w

相乘之前,有一个对输入

  1. x

的操作,如

  1. (x x.mean()) / (x.std() + 1e-6) * w

。操作数

  1. (x x.mean()) / (x.std() + 1e-6)

是需要存储的中间输出。并且可能还有其他状态,如running_mean、running_std或

  1. forward()

方法中的中间张量需要考虑。其中一些中间张量我们无法访问,所以我们无法确定发生了什么。当包含批量大小时,这变得更加复杂。

  1. deftest_layer_norm():
  2. print(f"Base memory: {torch.cuda.memory_allocated(device_id)}")
  3. x=torch.rand((10,), device=device_id)
  4. w=torch.rand((10,), requires_grad=True, device=device_id)
  5. # Layer Norm
  6. y= (x-x.mean()) / (x.std() +1e-6) *w
  7. final_memory=torch.cuda.memory_allocated(device_id)
  8. print(f"Memory after forward pass: {final_memory}")
  9. # Memory calculations
  10. next_chunk=lambdan: (n+511) //512*512
  11. mem=next_chunk(len(x.flatten()) *x.dtype.itemsize)
  12. mem+=next_chunk(len(w.flatten()) *w.dtype.itemsize)
  13. mem+=next_chunk(len(y.flatten()) *y.dtype.itemsize)
  14. mem+=next_chunk(len(x.flatten()) *x.dtype.itemsize) # intermediate
  15. print(f"Total memory expected: {mem}")
  16. assertfinal_memory==mem

反向传播非常相似,但有一些变化:

  • 模型大小因梯度存储而翻倍。
  • 所有中间张量在最后都被清除。
  • 分配了一个新的cublas工作空间。
  1. deftest_multi_layer_backward():
  2. print(f"Base memory: {torch.cuda.memory_allocated(device_id)}")
  3. n_layers=1
  4. model=nn.Sequential(*[
  5. nn.Sequential(
  6. nn.Linear(200, 100),
  7. nn.ReLU(), # No trainable params
  8. nn.Linear(100, 200),
  9. nn.Sigmoid(), # No trainable params
  10. )
  11. for_inrange(n_layers)
  12. ]).to(device_id)
  13. batch_size=5
  14. x=torch.randn((batch_size, 200), device=device_id)
  15. y=model(x)
  16. print(f"Memory after forward pass: {torch.cuda.memory_allocated(device_id)}")
  17. y.sum().backward()
  18. final_memory=torch.cuda.memory_allocated(device_id)
  19. print(f"Memory after backward pass: {final_memory}")
  20. # Computed memory
  21. next_chunk=lambdan: (n+511) //512*512
  22. mem=0
  23. unit=model[0][0].weight.dtype.itemsize
  24. forblockinmodel:
  25. forlayerinblock:
  26. ifisinstance(layer, nn.Linear):
  27. mem+=next_chunk(len(layer.weight.flatten()) *unit) *2 # Weights and gradients
  28. mem+=next_chunk(len(layer.bias) *unit) *2 # Biases and gradients
  29. # mem += next_chunk(layer.in_features * batch_size * unit) # Intermediate tensors are cleared
  30. mem+=next_chunk(len(y.flatten()) *unit)
  31. mem+=2*8519680 # cublas_size doubled
  32. mem+=next_chunk(len(y.flatten()) *unit)
  33. print(f"Total memory expected: {mem}")
  34. assertfinal_memory==mem

优化器(单个线性层反向传播)

我们观察一些优化步骤的内存分配。

  1. deftest_single_linear_layer_with_optimizer():
  2. # Disable cublas
  3. importos; os.environ["CUBLAS_WORKSPACE_CONFIG"] =":0:0"
  4. memory_timeline_real= []
  5. add=lambdae: memory_timeline_real.append({"event": e, "memory": torch.cuda.memory_allocated(device_id)})
  6. add("baseline")
  7. in_size=256
  8. out_size=250
  9. batch_size=100
  10. model=nn.Linear(in_size, out_size, device=device, dtype=torch.float32)
  11. add("model_allocation")
  12. optimizer=torch.optim.Adam(model.parameters(), lr=0.001)
  13. add("optimizer_init")
  14. x=torch.randn((batch_size, in_size,), dtype=torch.float32, device=device)
  15. add("input_allocation")
  16. defstep(n):
  17. optimizer.zero_grad()
  18. add(f"optim_zero_grad_{n}")
  19. y=model(x)
  20. add(f"forward_{n}")
  21. y.sum().backward()
  22. add(f"backward_{n}")
  23. optimizer.step()
  24. dely
  25. add(f"optim_step_{n}")
  26. foriinrange(4):
  27. step(i+1)
  28. # Bar chart with even name on x-axis and total_memory on y-axis
  29. fig=plt.figure(figsize=(15, 7))
  30. fig.set_tight_layout(True)
  31. plt.ylim((0, 1_300_000))
  32. plt.bar([event["event"] foreventinmemory_timeline_real], [event["memory"] foreventinmemory_timeline_real])
  33. plt.xlabel("Event")
  34. plt.ylabel("Total memory allocated (bytes)")
  35. plt.title(f"Memory allocation during training ({type(optimizer)})")
  36. plt.xticks(rotation=45)
  37. plt.show()

图3:使用SGD优化器在训练的各个阶段的内存分配

图4:使用Adam优化器在训练的各个阶段的内存分配

直到backward_1,我们看到内存分配如预期。当

  1. optimizer.step()

结束时,在这个特定的代码中删除了

  1. y

,所以该内存被释放。在底层优化器会获取额外的内存(等于可训练参数的大小)来更新它们,并在更新后释放该内存。这在图中没有显示。更详细的时间图可以在下图5中看到。

对于Adam对每个可训练参数都有一阶矩和二阶矩。所以它总是在内存中保留2倍的模型大小。这是这段代码中训练最耗费内存的部分。

图5:按毫秒计的内存分配时间图。

现在让我们尝试手动计算这些内存需求:

  1. # Memory calculations (continuing from previous code block)
  2. units=model.weight.dtype.itemsize
  3. memory_timeline= []
  4. all_keys= ["trainable_params", "input", "output", "gradient", "intermediate_tensors", "optimizer_state"]
  5. defupdate_memory(event: str, update: dict):
  6. prev_state=memory_timeline[-1] ifmemory_timelineelse {k: 0forkinall_keys}
  7. new_state= {k: prev_state.get(k, 0) +update.get(k, 0) forkinall_keys}
  8. new_state["event"] =event
  9. memory_timeline.append(new_state)
  10. next_chunk=lambdan: (n+511) //512*512
  11. update_memory("baseline", {})
  12. # Model memory
  13. model_mem=next_chunk(len(model.weight.flatten()) *units)
  14. model_mem+=next_chunk(len(model.bias) *units)
  15. update_memory("model_allocation", {"trainable_params": model_mem})
  16. update_memory("optimizer_init", {})
  17. # Input memory
  18. x_mem=next_chunk(len(x.flatten()) *units)
  19. update_memory("input_allocation", {"input": x_mem})
  20. update_memory("optim_zero_grad_1", {})
  21. # Forward
  22. y_mem=next_chunk(batch_size*out_size*units)
  23. # Add any intermediate tensors here.
  24. update_memory("forward_1", {"output": y_mem}) # , "intermediate_tensors": ...})
  25. # Backward
  26. grad_mem=next_chunk(len(model.weight.grad.flatten()) *units)
  27. grad_mem+=next_chunk(len(model.bias.grad.flatten()) *units)
  28. # Clear any intermediate tensors here.
  29. update_memory("backward_1", {"gradient": grad_mem}) # "intermediate_tensors": ...})
  30. # Optimizer memory
  31. ifisinstance(optimizer, torch.optim.SGD):
  32. # SGD has parameters in memory. They are cleared after each step.
  33. optimizer_mem=0
  34. elifisinstance(optimizer, torch.optim.Adam):
  35. # Adam has parameters and 2 momentum buffers. Parameters are cleared after each step.
  36. optimizer_mem=2*model_mem
  37. else:
  38. raise
  39. update_memory("optim_step_1", {"optimizer_state": optimizer_mem, "output": -y_mem})
  40. forstepinrange(2, 5):
  41. update_memory(f"optim_zero_grad_{step}", {"gradient": -grad_mem})
  42. update_memory(f"forward_{step}", {"output": y_mem})
  43. update_memory(f"backward_{step}", {"gradient": grad_mem})
  44. update_memory(f"optim_step_{step}", {"output": -y_mem})
  45. # Make totals
  46. foreventinmemory_timeline:
  47. event["total"] =sum([vforvinevent.values() ifisinstance(v, int)])
  48. # Plot memory timeline
  49. importpandasaspd
  50. df=pd.DataFrame(memory_timeline, columns=all_keys+ ["event"])
  51. df.set_index("event", inplace=True, drop=True)
  52. df.plot(kind='bar', stacked=True, figsize=(15, 7), ylim=(0, 1_300_000), xlabel="Event", ylabel="Total memory allocated (bytes)", title=f"Memory allocation expected ({type(optimizer)})")
  53. plt.tight_layout()
  54. plt.xticks(rotation=45)
  55. plt.show()
  56. # Compare the two timelines
  57. fori, (real, expected) inenumerate(zip(memory_timeline_real, memory_timeline)):
  58. assertreal["memory"] ==expected["total"], f"Memory mismatch at {real['event']}: {real['memory']} != {expected['total']}"

图6:使用SGD优化器在训练的不同阶段的内存使用分段

图7:使用Adam优化器在训练的不同阶段的内存使用分段

在手动计算内存分配后,我们的计算与观察结果相匹配。这次实际上可以看到内存分配到各种张量的分段。例如,Adam的状态占用了两倍的模型大小。梯度(红色)的不同变化。如果向继续测试,还可以尝试向这个模型添加更多层,添加中间张量并在适当的时候删除它们。这应该在这些条形图中创建另一个代表中间张量的分段。

总结

结合上面的每个概念我们可以回答主要问题:

  • 可训练参数:固定的模型大小
  • 内存块:它只以512字节的块出现
  • Cublas内存:前向传播一个块,反向传播一个块
  • 梯度:与模型大小相同
  • 中间张量:最麻烦的部分,取决于代码如何编写
  • 优化器:至少分配一倍的模型大小

最后一个问题就是,我们只处理了前馈层,那么CNN、Transformers、RNN等呢?首先CNN是类似前馈层的操作,所以我们可以根据他的计算规则进行计算,而Transformers、RNN都基础操作的组合,我们计算了一个前馈层可以根据他们的架构进行组合计算。我们已经掌握了计算前馈层内存需求的方法,所以我们可以自己解决这些问题!

作者:Akhilez

“深入理解GPU内存分配:机器学习工程师的实用指南与实验”的评论:

还没有评论