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

Reduce LM memory usage #60

Open
wants to merge 2 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
23 changes: 18 additions & 5 deletions ml_mdm/language_models/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (C) 2024 Apple Inc. All rights reserved.
import logging

from transformers import T5ForConditionalGeneration
from transformers import T5ForConditionalGeneration, BitsAndBytesConfig

import torch
import torch.nn as nn
Expand Down Expand Up @@ -42,11 +42,11 @@ def load(self):


class LanguageModel(nn.Module):
def __init__(self, args, model):
def __init__(self, args, model, device: torch.device = "cpu"):
super().__init__()
self.model = model
self.embed_dim = model.embed_dim
self.device = "cpu"
self.device = device
self.args = args

# use pre-computed text embeddings. delete the language model!
Expand All @@ -69,6 +69,7 @@ def forward(self, sample, tokenizer):
)
else:
sample_tokens = sample["tokens"]
sample_tokens = sample_tokens.to(self.device)

if args.categorical_conditioning:
lm_outputs = (
Expand Down Expand Up @@ -134,7 +135,19 @@ def create_lm(args, device: torch.device = "cuda"):
) # TODO (jack_carlson) this line is never reached
else:
tokenizer = create_tokenizer(args.vocab_file)
model = T5Encoder.from_pretrained(args.text_model)
model = LanguageModel(args, model).to(device)
if torch.cuda.is_available():
model = T5Encoder.from_pretrained(
args.text_model,
device_map="auto",
quantization_config=BitsAndBytesConfig(load_in_8bit=True)
)
model = LanguageModel(args, model, device="cuda") # .to() not supported for bitsandbytes `8-bit` quantized models
else:
model = T5Encoder.from_pretrained(
args.text_model,
device_map="cpu",
torch_dtype=torch.float16
)
model = LanguageModel(args, model, device = "cpu")
model.eval()
return tokenizer, model