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

Allow Torch half models #1577

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions mmengine/infer/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ def _init_model(
model.cfg = cfg
self._load_weights_to_model(model, checkpoint, cfg)
model.to(device)
if 'cuda' in device and checkpoint is not None and checkpoint.get(
'meta', {}).get('float16', False):
model.half()
model.eval()
return model

Expand Down
1 change: 1 addition & 0 deletions mmengine/runner/_flexible_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,7 @@ def save_checkpoint(
elif not isinstance(meta, dict):
raise TypeError(
f'meta should be a dict or None, but got {type(meta)}')
meta['float16'] = False

if by_epoch:
# self.epoch increments 1 after
Expand Down
1 change: 1 addition & 0 deletions mmengine/runner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,7 @@ def save_checkpoint(
elif not isinstance(meta, dict):
raise TypeError(
f'meta should be a dict or None, but got {type(meta)}')
meta['float16'] = False

if by_epoch:
# self.epoch increments 1 after
Expand Down
5 changes: 3 additions & 2 deletions mmengine/utils/package_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ def get_installed_path(package: str) -> str:
else:
raise e

possible_path = osp.join(pkg.location, package)
location = '' if pkg.location is None else pkg.location
possible_path = osp.join(location, package)
if osp.exists(possible_path):
return possible_path
else:
return osp.join(pkg.location, package2module(package))
return osp.join(location, package2module(package))


def package2module(package: str):
Expand Down