Spaces:
Runtime error
Runtime error
File size: 12,423 Bytes
e202b16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 147 148 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
#
# This source code is licensed under the BSD license found in the
# LICENSE file in the root directory of this source tree.
from contextlib import nullcontext
from copy import deepcopy
import pytest
import torch
from torch import nn
import xformers.ops
from xformers.checkpoint import (
_optimize_runtime_with_given_memory,
checkpoint,
get_optimal_checkpoint_policy,
list_operators,
selective_checkpoint_wrapper,
)
cuda_only = pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA")
_devices = ["cpu"]
cuda_cap = (0, 0)
if torch.cuda.is_available():
_devices.append("cuda")
cuda_cap = torch.cuda.get_device_capability(_devices[1])
def _relu_policy(ctx, func, *args, **kwargs):
return func == torch.ops.aten.relu.default
def _all_policy(ctx, func, *args, **kwargs):
return True
@pytest.mark.skipif(torch.__version__ < "2.2", reason="Only new PyTorch supported")
@pytest.mark.parametrize("policy_fn", [None, [], _relu_policy, _all_policy])
@pytest.mark.parametrize("input_requires_grad", [True, False])
@pytest.mark.parametrize("device", _devices)
@pytest.mark.parametrize("autocast", [True, False])
def test_checkpoint(policy_fn, input_requires_grad, device, autocast):
def build_module():
return nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10),
nn.ReLU(),
).to(device)
module = nn.ModuleList([build_module() for i in range(10)])
# Run model with and without checkpointing and verify gradients are
# equivalent, regardless of if inputs require grads or not.
module_copy = deepcopy(module)
inputs = torch.rand(32, 10, device=device)
inputs_copy = inputs.clone()
inputs.requires_grad_(input_requires_grad)
inputs_copy.requires_grad_(input_requires_grad)
out = inputs
out_copy = inputs_copy
with torch.autocast(device_type=device, enabled=autocast):
for i in range(10):
out = checkpoint(module[i], out, policy_fn=policy_fn)
out_copy = module_copy[i](out_copy)
assert torch.allclose(out, out_copy)
out.sum().backward()
out_copy.sum().backward()
for p, p_copy in zip(module.parameters(), module_copy.parameters()):
assert torch.allclose(p.grad, p_copy.grad)
@pytest.mark.skipif(torch.__version__ < "2.2", reason="Only new PyTorch supported")
@pytest.mark.parametrize("policy_fn", [None, [], _relu_policy, _all_policy])
@pytest.mark.parametrize("input_requires_grad", [True, False])
@pytest.mark.parametrize("grad_mode", [True, False])
def test_checkpoint_with_grad(policy_fn, input_requires_grad, grad_mode):
module = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10),
nn.ReLU(),
)
# Run model with and without checkpointing and verify gradients are
# equivalent, regardless of if inputs require grads or not.
module_copy = deepcopy(module)
inputs = torch.rand(32, 10)
inputs_copy = inputs.clone()
inputs.requires_grad_(input_requires_grad)
inputs_copy.requires_grad_(input_requires_grad)
out = inputs
out_copy = inputs_copy
with torch.set_grad_enabled(grad_mode):
for i in range(10):
out = checkpoint(module, out, policy_fn=policy_fn)
out_copy = module_copy(out_copy)
assert torch.allclose(out, out_copy)
@pytest.mark.skipif(torch.__version__ < "2.2", reason="Only new PyTorch supported")
@cuda_only
@pytest.mark.parametrize("policy_fn", [None, [], _relu_policy, _all_policy])
@pytest.mark.parametrize("input_requires_grad", [True, False])
@pytest.mark.parametrize("device", ["cuda"])
@pytest.mark.parametrize("autocast", [True, False])
@pytest.mark.parametrize(
"op",
[
xformers.ops.MemoryEfficientAttentionFlashAttentionOp,
(
xformers.ops.MemoryEfficientAttentionCutlassOp
if torch.version.cuda
else xformers.ops.MemoryEfficientAttentionCkOp
),
],
)
def test_checkpoint_attention(policy_fn, input_requires_grad, device, autocast, op):
if (
op[0].CUDA_MINIMUM_COMPUTE_CAPABILITY > cuda_cap
or op[1].CUDA_MINIMUM_COMPUTE_CAPABILITY > cuda_cap
):
pytest.skip("skipping operator not supported in this arch")
if (
op is xformers.ops.MemoryEfficientAttentionFlashAttentionOp
and torch.version.hip
):
pytest.skip("FlashAttentionOp is not supported on ROCM!")
if op is xformers.ops.MemoryEfficientAttentionCkOp:
pytest.skip("Gradience is currently not supported by ck-tiled!")
class Attn(nn.Module):
def forward(self, x):
out = xformers.ops.memory_efficient_attention(x, x, x, op=op)
return out + x
num_layers = 10
dtype = torch.float32 if autocast else torch.float16
modules = nn.Sequential(
*[
nn.Sequential(
nn.Linear(10, 64),
Attn(),
nn.ReLU(),
nn.Linear(64, 10),
nn.ReLU(),
)
.to(device)
.to(dtype)
for _ in range(num_layers)
]
)
# Run model with and without checkpointing and verify gradients are
# equivalent, regardless of if inputs require grads or not.
modules_copy = deepcopy(modules)
inputs = torch.rand(32, 128, 10, dtype=dtype, device=device)
inputs_copy = inputs.clone()
inputs.requires_grad_(input_requires_grad)
inputs_copy.requires_grad_(input_requires_grad)
out = inputs
out_copy = inputs_copy
with torch.autocast(device_type=device, enabled=autocast):
for i in range(num_layers):
out = checkpoint(modules[i], out, policy_fn=policy_fn)
out_copy = modules_copy[i](out_copy)
assert torch.allclose(out, out_copy)
out.sum().backward()
out_copy.sum().backward()
for p, p_copy in zip(modules.parameters(), modules_copy.parameters()):
assert torch.allclose(
p.grad, p_copy.grad
), f"{(p.grad - p_copy.grad).abs().max()}"
if input_requires_grad:
assert torch.allclose(inputs.grad, inputs_copy.grad)
def test_list_operators():
module = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10),
nn.ReLU(),
)
inputs = torch.rand(32, 10)
operators = list_operators(module, inputs)
operators_str = [str(x) for x in operators]
ref = [
"aten.t.default",
"aten.addmm.default",
"aten.relu.default",
"aten.detach.default",
"aten.t.default",
"aten.addmm.default",
"aten.relu.default",
"aten.detach.default",
]
assert operators_str == ref
@pytest.mark.parametrize(
"max_memory,optimal_soln",
[
(0, torch.tensor([1, 0, 0, 0, 0, 0, 0, 0], dtype=torch.float64)),
(100, torch.tensor([1, 0, 0, 0, 0, 1, 0, 1], dtype=torch.float64)),
(120, torch.tensor([1, 0, 0, 1, 0, 0, 0, 0], dtype=torch.float64)),
(200, torch.tensor([1, 0, 1, 0, 0, 1, 0, 1], dtype=torch.float64)),
(220, torch.tensor([1, 0, 0, 1, 0, 1, 0, 1], dtype=torch.float64)),
(320, torch.tensor([1, 0, 1, 1, 0, 1, 0, 1], dtype=torch.float64)),
(420, torch.tensor([1, 1, 1, 1, 0, 1, 0, 1], dtype=torch.float64)),
],
)
def test_optimize_runtime_with_given_memory(max_memory, optimal_soln):
data = [
("aten.copy_", 5, 0),
("aten.add", 5, 100),
("aten.div", 8, 100),
("aten.mm", 15, 120),
("aten.native_dropout", 15, 0),
("aten.linear", 9, 100),
("aten.t", 1, 0),
("aten.relu_", 5, 0),
]
inplace_ops = [(0, 0), (7, 5)]
view_like_ops = [6]
rand_ops = [4]
runtimes = torch.tensor([x[1] for x in data], dtype=torch.float64)
memory = torch.tensor([x[2] for x in data], dtype=torch.float64)
out = _optimize_runtime_with_given_memory(
memory,
runtimes,
max_memory,
view_like_ops,
inplace_ops,
rand_ops,
force_store_random=False,
)
torch.testing.assert_close(optimal_soln, out)
def _get_model_blocks(num_layers, dtype, device, inplace, random, first_inplace):
modules = []
class Add_(torch.nn.Module):
def forward(self, x):
return x.add_(1)
for _ in range(num_layers):
mods = [
nn.Linear(10, 10),
nn.CELU(inplace=inplace),
]
if first_inplace:
mods.insert(0, Add_())
if random:
mods.append(nn.Dropout())
mods.append(nn.Linear(10, 10))
if random:
mods.append(nn.Dropout())
mods.append(nn.CELU(inplace=inplace))
modules.append(nn.Sequential(*mods).to(device).to(dtype))
return modules
class _Model(torch.nn.Module):
def __init__(self, blocks, policy_fn):
super().__init__()
self.blocks = torch.nn.ModuleList(blocks)
self.policy_fn = policy_fn
def forward(self, x):
for b in self.blocks:
x = checkpoint(b, x, policy_fn=self.policy_fn)
return x
@pytest.mark.skipif(torch.__version__ < "2.2", reason="Only new PyTorch supported")
@cuda_only
@pytest.mark.parametrize("device", ["cuda"])
@pytest.mark.parametrize("memory_budget", [0, 0.03, 0.05, 0.1, 0.3, 0.5, 0.8, 1.0])
@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize("random", [True, False])
@pytest.mark.parametrize("first_inplace", [False])
def test_optimal_checkpoint_policy(
device, memory_budget, inplace, random, first_inplace
):
if first_inplace and inplace:
pytest.skip("This case is degenerate and doesn't work with vanilla PyTorch")
torch.manual_seed(42)
dtype = torch.float16
modules = _get_model_blocks(
3, dtype, device, inplace=inplace, random=random, first_inplace=first_inplace
)
inputs = torch.rand(32, 128, 10, dtype=dtype, device=device)
policy_fn = get_optimal_checkpoint_policy(
modules[0], inputs, memory_budget=memory_budget
)
model = _Model(modules, policy_fn)
model_ref = torch.nn.Sequential(*deepcopy(modules))
grad = torch.rand_like(inputs)
torch.manual_seed(42)
out = model(inputs.clone())
out.backward(grad)
torch.manual_seed(42)
out_ref = model_ref(inputs.clone())
out_ref.backward(grad)
torch.testing.assert_close(out, out_ref)
for p, p_ref in zip(model.parameters(), model_ref.parameters()):
torch.testing.assert_close(p.grad, p_ref.grad)
@pytest.mark.skipif(torch.__version__ < "2.2", reason="Only new PyTorch supported")
@pytest.mark.skipif(True, reason="TODO[fmassa]: Broken on nightly")
@cuda_only
@pytest.mark.parametrize("no_grad", [False, True])
@pytest.mark.parametrize("device", ["cuda"])
@pytest.mark.parametrize("memory_budget", [0, 0.1, 0.3, 1.0])
@pytest.mark.parametrize("inplace", [False])
@pytest.mark.parametrize("random", [False])
@torch._dynamo.config.patch( # type: ignore
"_experimental_support_context_fn_in_torch_utils_checkpoint", True
)
def test_selective_checkpoint_wrapper_compile(
device, no_grad, memory_budget, inplace, random
):
torch.manual_seed(42)
dtype = torch.float16
modules = _get_model_blocks(
3, dtype, device, inplace=inplace, random=random, first_inplace=False
)
inputs = torch.rand(32, 128, 10, dtype=dtype, device=device)
model = torch.nn.Sequential(
*[selective_checkpoint_wrapper(b, memory_budget=memory_budget) for b in modules]
)
model = torch.compile(model)
model_ref = torch.nn.Sequential(*deepcopy(modules))
grad = torch.rand_like(inputs)
context = torch.no_grad() if no_grad else nullcontext()
with context:
torch.manual_seed(42)
out = model(inputs.clone())
if not no_grad:
out.backward(grad)
torch.manual_seed(42)
out_ref = model_ref(inputs.clone())
if not no_grad:
out_ref.backward(grad)
atol = 3e-4
rtol = 1e-3
torch.testing.assert_close(out, out_ref, atol=atol, rtol=rtol)
if no_grad:
return
for p, p_ref in zip(model.parameters(), model_ref.parameters()):
atol = 4e-4
rtol = 2e-3
torch.testing.assert_close(p.grad, p_ref.grad, atol=atol, rtol=rtol)
|