Commit 818a8c23 authored by Ajinkya Deogade's avatar Ajinkya Deogade Committed by Facebook GitHub Bot
Browse files

Move inplace_delegate from utils to modeling

Summary:
Pull Request resolved: https://github.com/facebookresearch/d2go/pull/553

Move `inplace_delegate` from `utils` to `modeling` to break the circular dependency.

Reviewed By: tglik, wat3rBro

Differential Revision: D45912068

fbshipit-source-id: f9f8b1be866ea4d793f4afcd019f16dec3d2f147
parent 1581776b
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
# misc.py # misc.py
# modules that are used in different places but are not a specific type (e.g., backbone) # modules that are used in different places but are not a specific type (e.g., backbone)
from typing import Any, Callable, Optional
import torch import torch
import torch.nn as nn import torch.nn as nn
...@@ -83,3 +85,31 @@ class AddCoordChannels(nn.Module): ...@@ -83,3 +85,31 @@ class AddCoordChannels(nn.Module):
out = torch.cat([out, rr], dim=1) out = torch.cat([out, rr], dim=1)
return out return out
def inplace_delegate(
self,
api_name: str,
sub_module_name: str,
setter_fn: Optional[Callable],
*args,
**kwargs,
) -> Any:
"""Helper function to delegate API calls to its submodule"""
sub_module = getattr(self, sub_module_name)
api_name = f"delegate_{api_name}"
if hasattr(sub_module, api_name):
func = getattr(sub_module, api_name)
orig_ret = func(*args, **kwargs)
if setter_fn is None:
# Assume the return of `func` will replace the submodule
setattr(self, sub_module_name, orig_ret)
return self
else:
return setter_fn(self, sub_module_name, orig_ret)
else:
raise RuntimeError(
f"It seems the {sub_module_name} doesn't implement {api_name},"
" quantization might fail."
)
...@@ -135,31 +135,3 @@ def _log_api_usage_on_main_process(identifier: str): ...@@ -135,31 +135,3 @@ def _log_api_usage_on_main_process(identifier: str):
""" """
if comm.is_main_process(): if comm.is_main_process():
_log_api_usage(identifier) _log_api_usage(identifier)
def inplace_delegate(
self,
api_name: str,
sub_module_name: str,
setter_fn: Optional[Callable],
*args,
**kwargs,
) -> Any:
"""Helper function to delegate API calls to its submodule"""
sub_module = getattr(self, sub_module_name)
api_name = f"delegate_{api_name}"
if hasattr(sub_module, api_name):
func = getattr(sub_module, api_name)
orig_ret = func(*args, **kwargs)
if setter_fn is None:
# Assume the return of `func` will replace the submodule
setattr(self, sub_module_name, orig_ret)
return self
else:
return setter_fn(self, sub_module_name, orig_ret)
else:
raise RuntimeError(
f"It seems the {sub_module_name} doesn't implement {api_name},"
" quantization might fail."
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment