Skip to content
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

Offloading example #1299

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/pytorch/cpu_offloading/cpu_offload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import torch
import transformer_engine as te

from transformer_engine.pytorch.cpu_offload import get_cpu_offload_context

# Initialize a CPU offload context to enable activation offloading and set number of layers
# to be offloaded to 1
context, sync_func = get_cpu_offload_context(True, 1, True, False)


# Define a 2 Linear layer model
layer = []
for i in range(2):
layer.append(te.pytorch.Linear(1024, 1024, bias=False, device="cuda"))

# Create dummy inputs on GPU
input_state = torch.rand(1024, 1024).cuda()

# Wrap the forward prop under the context
with context:
hidden = layer[0](input_state)

# Use synchronize function to sync across layers
hidden = sync_func(hidden)

with context:
output = layer[1](hidden)

output = sync_func(output)

# Trigger backward
output.sum().backward()