|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import torch |
| 16 | +from torch.nn import Module |
| 17 | + |
| 18 | + |
| 19 | +__all__ = [ |
| 20 | + "is_module_offloaded", |
| 21 | + "get_execution_device", |
| 22 | + "get_offloaded_device", |
| 23 | + "update_prefix_dict", |
| 24 | + "update_parameter_data", |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +def is_module_offloaded(module: Module) -> bool: |
| 29 | + """ |
| 30 | + :param module: layer to check |
| 31 | + :return: True if layer is offloaded from GPU, False otherwise |
| 32 | + """ |
| 33 | + return hasattr(module, "_hf_hook") and module._hf_hook.offload |
| 34 | + |
| 35 | + |
| 36 | +def get_execution_device(module: Module) -> torch.device: |
| 37 | + """ |
| 38 | + :param module: layer to check |
| 39 | + :return: device layer is loaded onto during forward pass |
| 40 | + """ |
| 41 | + if is_module_offloaded(module): |
| 42 | + return module._hf_hook.execution_device |
| 43 | + return next(module.parameters()).device |
| 44 | + |
| 45 | + |
| 46 | +def get_offloaded_device(module: Module) -> torch.device: |
| 47 | + """ |
| 48 | + :param module: layer to check |
| 49 | + :return: device layer is offloaded to onto after forward pass |
| 50 | + """ |
| 51 | + if is_module_offloaded(module): |
| 52 | + first_key = list(module._hf_hook.weights_map.keys())[0] |
| 53 | + prefix_dataset = module._hf_hook.weights_map.dataset |
| 54 | + return prefix_dataset[first_key].device |
| 55 | + return next(module.parameters()).device |
| 56 | + |
| 57 | + |
| 58 | +def update_prefix_dict(module: Module, key: str, data: torch.Tensor): |
| 59 | + """ |
| 60 | + Updates the offloaded state dict for a given module. Parameter named key is replaced |
| 61 | + by data. This is neccesary because parameter updates for offloaded modules do not |
| 62 | + persist automatically between loads. This function only affects the offloaded |
| 63 | + state dict and not the current state of the loaded module. |
| 64 | +
|
| 65 | + :param module: layer containing the parameter to update |
| 66 | + :param key: name of parameter to update |
| 67 | + :param data: tensor to update parameter with in the offloaded state dict |
| 68 | + """ |
| 69 | + if not is_module_offloaded(module): |
| 70 | + raise ValueError("Prefix dict is only applicable to offloaded modules") |
| 71 | + prefix_dict = module._hf_hook.weights_map |
| 72 | + prefix_dict.dataset[f"{prefix_dict.prefix}{key}"] = data |
| 73 | + |
| 74 | + |
| 75 | +def update_parameter_data( |
| 76 | + module: Module, new_param_data: torch.Tensor, param_name: str |
| 77 | +): |
| 78 | + """ |
| 79 | + Updates the paramter value named param_name for a given module. This function |
| 80 | + updates both the current loaded module state and the offloaded state dict if |
| 81 | + the module is offloaded. This is neccesary because parameter updates for offloaded |
| 82 | + modules do not persist automatically between loads. |
| 83 | +
|
| 84 | + :param module: layer containing the parameter to update |
| 85 | + :param new_param_data: tensor to update parameter with |
| 86 | + :param param_name: |
| 87 | + """ |
| 88 | + device = next(module.parameters()).device |
| 89 | + |
| 90 | + offloaded = False |
| 91 | + if is_module_offloaded(module): |
| 92 | + offload_device = get_offloaded_device(module) |
| 93 | + offloaded = True |
| 94 | + |
| 95 | + parameter = getattr(module, param_name, None) |
| 96 | + dtype = parameter.dtype |
| 97 | + parameter.data = new_param_data.to(device).to(dtype) |
| 98 | + |
| 99 | + if offloaded: |
| 100 | + prefix_dict = module._hf_hook.weights_map.dataset |
| 101 | + prefix = module._hf_hook.weights_map.prefix |
| 102 | + prefix_dict[f"{prefix}{param_name}"] = new_param_data.to(offload_device).to( |
| 103 | + dtype |
| 104 | + ) |
0 commit comments