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

[Cherry pick] quantized fsdp model loading #2186

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion src/sparseml/transformers/finetune/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
)
from sparseml.transformers.finetune.model_args import ModelArguments
from sparseml.transformers.finetune.training_args import TrainingArguments
from sparseml.utils.fsdp.helpers import is_fsdp_model, unwrap_and_export_model
from sparseml.utils.fsdp.helpers import (
find_and_move_state_dicts_to_cpu,
is_fsdp_model,
unwrap_and_export_model,
)


_LOGGER: logging.Logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -175,6 +179,15 @@ def one_shot(self, stage: Optional[str] = None):
output_dir=self._output_dir,
tokenizer=self.tokenizer,
)
# only allow the main process move the state
# dicts to cpu
if self.trainer.accelerator.is_main_process:
# assuming quantization is the last step
# we no longer need the original model
# and can safely delete it to save memory
del self.trainer.model
find_and_move_state_dicts_to_cpu(self._output_dir)

else:
save_model_and_recipe(
model=self.trainer.model,
Expand Down
29 changes: 28 additions & 1 deletion src/sparseml/utils/fsdp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import operator
from pathlib import Path
from typing import Optional, Union


Expand All @@ -25,6 +27,7 @@
except ImportError:
FullyShardedDataParallel = None

import torch
from torch.nn import Module

from sparseml.core.model import ModifiableModel
Expand All @@ -39,8 +42,11 @@
"unwrap_and_export_model",
"save_pretrained_fsdp",
"get_fsdp_parent",
"find_and_move_state_dicts_to_cpu",
]

_LOGGER = logging.getLogger(__name__)


def is_fsdp_model(model: Module) -> bool:
"""
Expand Down Expand Up @@ -113,7 +119,28 @@ def unwrap_and_export_model(model, accelerator, output_dir, tokenizer):
)


def save_pretrained_fsdp(model, accelerator, output_dir):
def find_and_move_state_dicts_to_cpu(output_dir: str):
"""
Looks for state dicts in the output directory and overwrites them
with cpu state dicts.

this is needed for quantized models trained with FSDP as the state dict
contains device information, which can cause issues when loading the model
using transformers AutoModel.from_pretrained(...) if the device information
is not removed, assumes the state dicts are named pytorch_model*.bin
"""

for model_file in Path(output_dir).rglob("pytorch_model*.bin"):
loaded_dict = torch.load(model_file)
for key, value in loaded_dict.items():
if isinstance(value, torch.Tensor):
loaded_dict[key] = value.cpu()

torch.save(loaded_dict, model_file)
_LOGGER.info(f"Moved state dict {model_file} to cpu")


def save_pretrained_fsdp(model, accelerator, output_dir, save_safetensors: bool = True):
full_state_dict_config = FullStateDictConfig(offload_to_cpu=True, rank0_only=True)
"""
Gathers the full FSDP state dict of the model onto rank0 GPU, then uses it to save
Expand Down
2 changes: 1 addition & 1 deletion src/sparseml/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from datetime import date


version_base = "1.7.0"
version_base = "1.7.1"
is_release = False # change to True to set the generated version as a release version


Expand Down
Loading