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

remove autocast for cpu #491

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions src/marqo/s2_inference/clip_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,13 @@ def encode_image(self, images: Union[str, ImageType, List[Union[str, ImageType]]

self.image_input_processed = torch.stack([self.preprocess(_img).to(self.device) for _img in image_input])

with torch.no_grad(), torch.autocast(device_type="cuda" if self.device.startswith("cuda") else "cpu"):
outputs = self.model.encode_image(self.image_input_processed).to(torch.float32)
with torch.no_grad():
if self.device.startswith("cuda"):
with torch.cuda.amp.autocast():
outputs = self.model.encode_image(self.image_input_processed).to(torch.float32)
else:
outputs = self.model.encode_image(self.image_input_processed).to(torch.float32)


if normalize:
_shape_before = outputs.shape
Expand All @@ -483,8 +488,12 @@ def encode_text(self, sentence: Union[str, List[str]], normalize=True) -> FloatT

text = self.tokenizer(sentence).to(self.device)

with torch.no_grad(), torch.autocast(device_type="cuda" if self.device.startswith("cuda") else "cpu"):
outputs = self.model.encode_text(text).to(torch.float32)
with torch.no_grad():
if self.device.startswith("cuda"):
with torch.cuda.amp.autocast():
outputs = self.model.encode_text(text).to(torch.float32)
else:
outputs = self.model.encode_text(text).to(torch.float32)

if normalize:
_shape_before = outputs.shape
Expand Down