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

Ability to specify a GPU index #193

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $ nougat path/to/directory -o output_directory

```
usage: nougat [-h] [--batchsize BATCHSIZE] [--checkpoint CHECKPOINT] [--model MODEL] [--out OUT]
[--recompute] [--markdown] [--no-skipping] pdf [pdf ...]
[--recompute] [--markdown] [--no-skipping] [--device-index DEVICEINDEX] pdf [pdf ...]

positional arguments:
pdf PDF(s) to process.
Expand Down
2 changes: 1 addition & 1 deletion nougat/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def __init__(
]
else:
new_bart_state_dict[x] = bart_state_dict[x]
self.model.load_state_dict(new_bart_state_dict, strict=False)
self.model.load_state_dict(new_bart_state_dict)

def add_special_tokens(self, list_of_tokens: List[str]):
"""
Expand Down
23 changes: 11 additions & 12 deletions nougat/utils/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ def default_batch_size():
logging.warning("No GPU found. Conversion on CPU is very slow.")
return batch_size


def move_to_device(model, bf16: bool = True, cuda: bool = True):
try:
if torch.backends.mps.is_available():
return model.to("mps")
except AttributeError:
pass
if bf16:
model = model.to(torch.bfloat16)
if cuda and torch.cuda.is_available():
model = model.to("cuda")
return model
def move_to_device(model, bf16: bool = True, cuda: bool = True, device_index: int = 0):
try:
if torch.backends.mps.is_available():
return model.to("mps")
except AttributeError:
pass
if bf16:
model = model.to(torch.bfloat16)
if cuda and torch.cuda.is_available():
model = model.to(f"cuda:{device_index}")
return model
8 changes: 7 additions & 1 deletion predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def get_args():
type=str,
help="Provide page numbers like '1-4,7' for pages 1 through 4 and page 7. Only works for single PDF input.",
)
parser.add_argument(
"--device-index",
type=int,
default=0,
help="Index of the preferred GPU device.",
)
parser.add_argument("pdf", nargs="+", type=Path, help="PDF(s) to process.")
args = parser.parse_args()
if args.checkpoint is None or not args.checkpoint.exists():
Expand Down Expand Up @@ -125,7 +131,7 @@ def get_args():
def main():
args = get_args()
model = NougatModel.from_pretrained(args.checkpoint)
model = move_to_device(model, bf16=not args.full_precision, cuda=args.batchsize > 0)
model = move_to_device(model, bf16=not args.full_precision, cuda=args.batchsize > 0, device_index=args.device_index)
if args.batchsize <= 0:
# set batch size to 1. Need to check if there are benefits for CPU conversion for >1
args.batchsize = 1
Expand Down