New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ONNX] Reduce exporter memory usage by removing intermediate values #101148
[ONNX] Reduce exporter memory usage by removing intermediate values #101148
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/101148
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 35b26fd: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
f6c2ba5
to
f3da754
Compare
@pytorchbot rebase |
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
This commit reduces the exporter memory usage by as much as 50%. During the shape inference step, the exporter caches the values of intermediate tensors. This can use as much memory as the model itself, or even more. For example, model weight tensors are often fed to a Transpose layer, and the output of that is the same size of the weights. This commit fixes the issue by removing the intermediate tensor values after they are used by all consumers.
Successfully rebased |
f3da754
to
35b26fd
Compare
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…ytorch#101148) This commit reduces the exporter memory usage by as much as 50%. During the shape inference step, the exporter caches the values of intermediate tensors in a `ConstantValueMap`. This can use as much memory as the model itself, or even more. For example, model weight tensors are often fed to a Transpose layer, and the output of that is the same size of the weights. This commit fixes the issue by removing the intermediate tensor values after they are used by all consumers. The cached values are only used for shape inference, so removing them after use should be safe. `ConstantValueMap` is cleared anyways once shape inference is complete for the entire graph. As an example, here is the model from issue pytorch#61263: ```python import torch import math # Size in GB tensor_size = 1 model_size = 8 layers_num = model_size // tensor_size kB = 1024 MB = kB * kB GB = MB * kB precision_size = 4 # bytes per float activation_size = math.floor(math.sqrt(tensor_size * GB / precision_size)) class Net(torch.nn.Module): def __init__(self): super(Net, self).__init__() for i in range(layers_num): name = "fc_%d" % i linear = torch.nn.Linear(activation_size, activation_size) setattr(self, name, linear) def forward(self, x): for i in range(layers_num): name = "fc_%d" % i linear = getattr(self, name) x = linear(x) return x model = Net().cuda() input = torch.zeros(activation_size, requires_grad=True).cuda() with torch.no_grad(): torch.onnx.export(model, (input, ), './model_large.onnx', do_constant_folding=False, opset_version=13) ``` It is just some large linear layers stacked together. Before this commit, my max GPU usage during export was about 16.7 GB, twice the model size. With this commit in combination with pytorch#101134, it was only about 9.5 GB. Together with pytorch#101134, fixes issue pytorch#61263 Pull Request resolved: pytorch#101148 Approved by: https://github.com/BowenBao
…ytorch#101148) This commit reduces the exporter memory usage by as much as 50%. During the shape inference step, the exporter caches the values of intermediate tensors in a `ConstantValueMap`. This can use as much memory as the model itself, or even more. For example, model weight tensors are often fed to a Transpose layer, and the output of that is the same size of the weights. This commit fixes the issue by removing the intermediate tensor values after they are used by all consumers. The cached values are only used for shape inference, so removing them after use should be safe. `ConstantValueMap` is cleared anyways once shape inference is complete for the entire graph. As an example, here is the model from issue pytorch#61263: ```python import torch import math # Size in GB tensor_size = 1 model_size = 8 layers_num = model_size // tensor_size kB = 1024 MB = kB * kB GB = MB * kB precision_size = 4 # bytes per float activation_size = math.floor(math.sqrt(tensor_size * GB / precision_size)) class Net(torch.nn.Module): def __init__(self): super(Net, self).__init__() for i in range(layers_num): name = "fc_%d" % i linear = torch.nn.Linear(activation_size, activation_size) setattr(self, name, linear) def forward(self, x): for i in range(layers_num): name = "fc_%d" % i linear = getattr(self, name) x = linear(x) return x model = Net().cuda() input = torch.zeros(activation_size, requires_grad=True).cuda() with torch.no_grad(): torch.onnx.export(model, (input, ), './model_large.onnx', do_constant_folding=False, opset_version=13) ``` It is just some large linear layers stacked together. Before this commit, my max GPU usage during export was about 16.7 GB, twice the model size. With this commit in combination with pytorch#101134, it was only about 9.5 GB. Together with pytorch#101134, fixes issue pytorch#61263 Pull Request resolved: pytorch#101148 Approved by: https://github.com/BowenBao
This commit reduces the exporter memory usage by as much as 50%. During the shape inference step, the exporter caches the values of intermediate tensors in a
ConstantValueMap
. This can use as much memory as the model itself, or even more. For example, model weight tensors are often fed to a Transpose layer, and the output of that is the same size of the weights. This commit fixes the issue by removing the intermediate tensor values after they are used by all consumers.The cached values are only used for shape inference, so removing them after use should be safe.
ConstantValueMap
is cleared anyways once shape inference is complete for the entire graph.As an example, here is the model from issue #61263:
It is just some large linear layers stacked together. Before this commit, my max GPU usage during export was about 16.7 GB, twice the model size. With this commit in combination with #101134, it was only about 9.5 GB.
Together with #101134, fixes issue #61263