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

[Feature] Support Op Input Info Logger #3091

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
6 changes: 6 additions & 0 deletions mmcv/utils/ext_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

import torch

from .op_input_info_logger import OpInputInfoLogger

if torch.__version__ != 'parrots':

def load_ext(name, funcs):
ext = importlib.import_module('mmcv.' + name)
for fun in funcs:
assert hasattr(ext, fun), f'{fun} miss in module {name}'
if os.getenv('MMCV_OPS_PRINT', '0') == '1':
if isinstance(getattr(ext, fun), OpInputInfoLogger):
continue
setattr(ext, fun, OpInputInfoLogger(getattr(ext, fun)))
return ext
else:
from parrots import extension
Expand Down
43 changes: 43 additions & 0 deletions mmcv/utils/op_input_info_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) OpenMMLab. All rights reserved.
import json
from collections import OrderedDict

import torch


class OpInputInfoLogger:

def __init__(self, op):
self.op = op
self.op_name = self.op.__name__
print(f'Wrap mmcv.ops.{self.op_name} with OpsInfoLogger')

def _get_input_info(self, *args, **kwargs):
input_info = OrderedDict()
for i, arg in enumerate(args):
input_info[f'arg_{i}'] = arg
for name, value in kwargs.items():
input_info[name] = value
return input_info

def _dump_input_info(self, input_info):
info = dict()
info[f'mmcv.ops.{self.op_name}'] = OrderedDict()
for name, param in input_info.items():
if isinstance(param, torch.Tensor):
info[f'mmcv.ops.{self.op_name}'][name] = {
'shape': str(param.shape),
'dtype': str(param.dtype),
}
else:
info[f'mmcv.ops.{self.op_name}'][name] = {
'value': str(param),
'type': str(type(param)),
}
with open('ops_input_info.jsonl', 'a') as f:
f.write(json.dumps(info) + '\n')

def __call__(self, *args, **kwargs):
input_info = self._get_input_info(*args, **kwargs)
self._dump_input_info(input_info)
return self.op(*args, **kwargs)
Loading