forked from tdrussell/qlora-pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsloth_utils.py
61 lines (52 loc) · 2.21 KB
/
unsloth_utils.py
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
# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# I (tdrussell) made a few modifications.
import torch
class Unsloth_Offloaded_Gradient_Checkpointer(torch.autograd.Function):
"""
Saves VRAM by smartly offloading to RAM.
Tiny hit to performance, since we mask the movement via non blocking calls.
"""
@staticmethod
@torch.amp.custom_fwd(device_type='cuda')
def forward(ctx, forward_function, hidden_states, *args):
saved_hidden_states = hidden_states.to("cpu", non_blocking = True)
with torch.no_grad():
output = forward_function(hidden_states, *args)
ctx.save_for_backward(saved_hidden_states)
ctx.forward_function = forward_function
ctx.args = args
return output
pass
@staticmethod
@torch.amp.custom_bwd(device_type='cuda')
def backward(ctx, *grads):
(hidden_states,) = ctx.saved_tensors
hidden_states = hidden_states.to("cuda", non_blocking = True).detach()
hidden_states.requires_grad = True
with torch.enable_grad():
outputs = ctx.forward_function(hidden_states, *ctx.args)
output_tensors = []
grad_tensors = []
for out, grad in zip(outputs, grads):
if out.requires_grad:
output_tensors.append(out)
grad_tensors.append(grad)
torch.autograd.backward(output_tensors, grad_tensors)
return (None, hidden_states.grad,) + (None,)*len(ctx.args)
pass
pass
# hidden_states must be the first argument or this won't work
def unsloth_checkpoint(function, *args):
return Unsloth_Offloaded_Gradient_Checkpointer.apply(function, args[0], *args[1:])