我目前正在尝试使用pytorch 2.0来提高我的项目的训练性能。我听说torch.compile可能会提升一些模型。
所以我的问题(目前)很简单;我应该如何使用torch. compiler与大型模型?
比如,我应该用这样的手电筒吗?
class BigModel(nn.Module):
def __init__(self, ...):
super(BigModel, self).__init__()
self.model = nn.Sequential(
SmallBlock(),
SmallBlock(),
SmallBlock(),
...
)
...
class SmallBlock(nn.Module):
def __init__(self, ...):
super(SmallBlock, self).__init__()
self.model = nn.Sequential(
...some small model...
)
model = BigModel()
model_opt = torch.compile(model)
,还是像这样?
class BigModel(nn.Module):
def __init__(self, ...):
super(BigModel, self).__init__()
self.model = nn.Sequential(
SmallBlock(),
SmallBlock(),
SmallBlock(),
...
)
...
class SmallBlock(nn.Module):
def __init__(self, ...):
super(SmallBlock, self).__init__()
self.model = nn.Sequential(
...some small model...
)
self.model = torch.compile(self.model)
model = BigModel()
model_opt = torch.compile(model)
作为总结,
1.是否应该编译每一层?或者torch.compile会自动执行吗?
1.有没有正确使用torch.compile的提示?
老实说,我都试过了,但没有区别。
而且,它并没有显着加快,我只是检查了我的模型的加速率只有大约5 ~ 10%。
2条答案
按热度按时间lnxxn5zx1#
PyTorch dev在这里,但你的问题有很多变量
1.您使用的是哪种硬件?在A100或A10 G GPU上,加速效果最显著
1.如果是,是否启用了Tensor核心?
1.编译发生在第一批,你的批大小是多少?如果很小,那么使用mode=reduce-overhead确实会使事情变得更快,因为它启用了CUDA图形,这有助于减少启动小内核的开销
1.你应该选择编译你实际运行的整个模型,实际上我们有一些实用程序来允许或禁止编译子图,你可以在这里查看https://pytorch.org/docs/master/_dynamo.html
scyqe7ek2#
torch.compile的默认模式似乎不起作用,但它有另一种模式,可以真正加速您的模型。“torch.compile(${yourmodel},mode=“reduce-overhead”)”