Skip to content

Instantly share code, notes, and snippets.

@shinh
Last active August 26, 2019 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinh/2a52da164a0d46cc1274374d4ef1f0ba to your computer and use it in GitHub Desktop.
Save shinh/2a52da164a0d46cc1274374d4ef1f0ba to your computer and use it in GitHub Desktop.
# Usage:
#
# $ PYTHONPATH=. python3 testcases/elichika_tests/utils/collect_types.py
import importlib
import inspect
import os
import sys
import types
import chainer
import numpy
from chainer_compiler.elichika import testtools
def _run_model(model, args, subname=None, backprop=False):
if (isinstance(model, type) or
isinstance(model, types.FunctionType)):
model = model()
model(*args)
testtools.generate_testcase = _run_model
show_shape_info = False
class TypeInfo(object):
def __init__(self, value):
self.type = type(value)
self.dtype = None
self.shape = None
if isinstance(value, (numpy.ndarray, chainer.Variable)):
self.dtype = value.dtype
if show_shape_info:
self.shape = value.shape
def __str__(self):
r = self.type.__name__
if self.dtype is not None:
p = str(self.dtype)
if self.shape is not None:
p += ', {}'.format(self.shape)
r += '[{}]'.format(p)
return r
class TypeCollector(object):
def __init__(self):
self.wrapped_functions = {}
self.call_records = []
self.hook_funcs('chainer.functions', chainer.functions)
self.hook_funcs('numpy', numpy)
def hook_funcs(self, module_name, module):
for func_name in sorted(dir(module)):
func = getattr(module, func_name)
if isinstance(func, types.FunctionType):
wrap_func = self.make_wrap_func(func)
self.wrapped_functions[func] = (module_name, func_name)
setattr(module, func_name, wrap_func)
def make_wrap_func(self, func):
try:
sig = inspect.signature(func)
except ValueError:
sig = None
def wrap_func(*args, **kwargs):
ret = func(*args, **kwargs)
# Try canonicalization.
if sig is not None:
bound = sig.bind(*args, **kwargs)
bound.apply_defaults()
args = bound.args
kwargs = bound.kwargs
arg_types = [TypeInfo(a) for a in args]
kwarg_types = {k: TypeInfo(a) for k, a in kwargs.items()}
if isinstance(ret, (list, tuple)):
ret_types = [TypeInfo(a) for a in ret]
else:
ret_types = TypeInfo(ret)
self.call_records.append((func, arg_types, kwarg_types, ret_types))
return ret
return wrap_func
def dump_typeinfo(self):
typeinfo_by_func = {}
for func, arg_types, kwarg_types, ret_types in self.call_records:
arg_types = tuple(arg_types)
kwarg_types = tuple((k, kwarg_types[k])
for k in sorted(kwarg_types))
if isinstance(ret_types, list):
ret_types = tuple(ret_types)
if func not in typeinfo_by_func:
typeinfo_by_func[func] = set()
typeinfo_by_func[func].add((arg_types, kwarg_types, ret_types))
for func, (module_name, func_name) in self.wrapped_functions.items():
if func not in typeinfo_by_func:
continue
name = '{}.{}'.format(module_name, func_name)
sigs = set()
for arg_types, kwarg_types, ret_types in typeinfo_by_func[func]:
args_strs = []
for ti in arg_types:
args_strs.append(str(ti))
for k, ti in kwarg_types:
args_strs.append('{}: {}'.format(k, str(ti)))
args_str = ', '.join(args_strs)
if isinstance(ret_types, tuple):
ret_str = ', '.join(str(ti) for ti in ret_types)
else:
ret_str = str(ret_types)
sigs.add('{}({}) -> {}'.format(name, args_str, ret_str))
for sig in sorted(sigs):
print(sig)
def collect_test_filenames():
all_tests = []
for dirpath, _, filenames in os.walk('testcases/elichika_tests'):
for filename in filenames:
if 'canonicalizer' in dirpath:
continue
if dirpath.endswith('utils'):
continue
if filename.endswith('.py'):
all_tests.append(os.path.join(dirpath, filename))
return list(sorted(set(all_tests)))
def main():
type_collector = TypeCollector()
test_filenames = collect_test_filenames()
for filename in test_filenames:
module_name = os.path.splitext(filename)[0].replace('/', '.')
module = importlib.import_module(module_name)
if not hasattr(module, 'main'):
continue
print('Running', filename, '...')
module.main()
print('Type info:')
type_collector.dump_typeinfo()
if __name__ == '__main__':
main()
chainer.functions.absolute(ndarray[float32]) -> Variable[float32]
chainer.functions.accuracy(Variable[float32], Variable[int32], int) -> Variable[float32]
chainer.functions.arccos(ndarray[float32]) -> Variable[float32]
chainer.functions.arcsin(ndarray[float32]) -> Variable[float32]
chainer.functions.arctan(ndarray[float32]) -> Variable[float32]
chainer.functions.argmax(ndarray[float32], NoneType) -> Variable[int32]
chainer.functions.argmax(ndarray[float32], int) -> Variable[int32]
chainer.functions.argmin(ndarray[float32], NoneType) -> Variable[int32]
chainer.functions.argmin(ndarray[float32], int) -> Variable[int32]
chainer.functions.mean(Variable[float32], tuple, NoneType, bool) -> Variable[float32]
chainer.functions.mean(ndarray[float32], NoneType, NoneType, bool) -> Variable[float32]
chainer.functions.mean(ndarray[float32], int, NoneType, bool) -> Variable[float32]
chainer.functions.mean(ndarray[float32], tuple, NoneType, bool) -> Variable[float32]
chainer.functions.average_pooling_2d(Variable[float32], int, int, int) -> Variable[float32]
chainer.functions.average_pooling_2d(ndarray[float32], int, NoneType, int) -> Variable[float32]
chainer.functions.average_pooling_2d(ndarray[float32], int, int, int) -> Variable[float32]
chainer.functions.average_pooling_2d(ndarray[float32], tuple, tuple, tuple) -> Variable[float32]
chainer.functions.batch_normalization(Variable[float32], Parameter[float32], Parameter[float32], axis: NoneType, decay: float, eps: float, running_mean: ndarray[float32], running_var: ndarray[float32]) -> Variable[float32]
chainer.functions.batch_normalization(ndarray[float32], Parameter[float32], Parameter[float32], axis: NoneType, decay: float, eps: float, running_mean: ndarray[float32], running_var: ndarray[float32]) -> Variable[float32]
chainer.functions.broadcast_to(Variable[float32], tuple) -> Variable[float32]
chainer.functions.broadcast_to(ndarray[float32], tuple) -> Variable[float32]
chainer.functions.clip(ndarray[float32], float, float) -> Variable[float32]
chainer.functions.clipped_relu(ndarray[float32], float) -> Variable[float32]
chainer.functions.concat(list, int) -> Variable[float32]
chainer.functions.concat(list, int) -> Variable[int32]
chainer.functions.concat(tuple, int) -> Variable[float32]
chainer.functions.cos(ndarray[float32]) -> Variable[float32]
chainer.functions.cosh(ndarray[float32]) -> Variable[float32]
chainer.functions.dropout(Variable[float32], float) -> Variable[float32]
chainer.functions.dropout(ndarray[float32], float) -> Variable[float32]
chainer.functions.elu(ndarray[float32], float) -> Variable[float32]
chainer.functions.exp(ndarray[float32]) -> Variable[float32]
chainer.functions.expand_dims(Variable[float32], int) -> Variable[float32]
chainer.functions.expand_dims(ndarray[float32], int) -> Variable[float32]
chainer.functions.flatten(Variable[int32]) -> Variable[int32]
chainer.functions.hstack(list) -> Variable[float32]
chainer.functions.hstack(tuple) -> Variable[float32]
chainer.functions.leaky_relu(ndarray[float32], float) -> Variable[float32]
chainer.functions.local_response_normalization(Variable[float32], int, int, float, float) -> Variable[float32]
chainer.functions.local_response_normalization(ndarray[float32], int, int, float, float) -> Variable[float32]
chainer.functions.log(ndarray[float32]) -> Variable[float32]
chainer.functions.log_softmax(ndarray[float32], int) -> Variable[float32]
chainer.functions.matmul(Variable[float32], Variable[float32], bool, bool) -> Variable[float32]
chainer.functions.matmul(ndarray[float32], ndarray[float32], bool, bool) -> Variable[float32]
chainer.functions.max(ndarray[float32], NoneType, bool) -> Variable[float32]
chainer.functions.max(ndarray[float32], int, bool) -> Variable[float32]
chainer.functions.max_pooling_2d(Variable[float32], int, int, int, bool, bool) -> Variable[float32]
chainer.functions.max_pooling_2d(ndarray[float32], tuple, NoneType, int, bool, bool) -> Variable[float32]
chainer.functions.max_pooling_2d(ndarray[float32], tuple, tuple, int, bool, bool) -> Variable[float32]
chainer.functions.max_pooling_2d(ndarray[float32], tuple, tuple, tuple, bool, bool) -> Variable[float32]
chainer.functions.maximum(ndarray[float32], ndarray[float32]) -> Variable[float32]
chainer.functions.min(ndarray[float32], NoneType, bool) -> Variable[float32]
chainer.functions.min(ndarray[float32], int, bool) -> Variable[float32]
chainer.functions.minimum(ndarray[float32], ndarray[float32]) -> Variable[float32]
chainer.functions.pad_sequence(list, NoneType, int) -> Variable[float32]
chainer.functions.pad_sequence(list, NoneType, int) -> Variable[int32]
chainer.functions.pad_sequence(list, int, int) -> Variable[float32]
chainer.functions.pad_sequence(tuple, NoneType, int) -> Variable[float32]
chainer.functions.relu(Variable[float32]) -> Variable[float32]
chainer.functions.relu(ndarray[float32]) -> Variable[float32]
chainer.functions.reshape(Variable[float32], tuple) -> Variable[float32]
chainer.functions.reshape(Variable[int32], tuple) -> Variable[int32]
chainer.functions.reshape(ndarray[float32], tuple) -> Variable[float32]
chainer.functions.resize_images(ndarray[float32], tuple, str, bool) -> Variable[float32]
chainer.functions.roi_average_align_2d(ndarray[float32], ndarray[float32], ndarray[int32], int, float, int) -> Variable[float32]
chainer.functions.roi_average_pooling_2d(ndarray[float32], ndarray[float32], ndarray[int32], int, float) -> Variable[float32]
chainer.functions.roi_max_align_2d(ndarray[float32], ndarray[float32], ndarray[int32], int, float, int) -> Variable[float32]
chainer.functions.roi_max_pooling_2d(ndarray[float32], ndarray[float32], ndarray[int32], int, float) -> Variable[float32]
chainer.functions.selu(ndarray[float32], float, float) -> Variable[float32]
chainer.functions.separate(Variable[float32], int) -> Variable[float32], Variable[float32], Variable[float32]
chainer.functions.separate(Variable[float32], int) -> Variable[float32], Variable[float32], Variable[float32], Variable[float32]
chainer.functions.separate(Variable[float32], int) -> Variable[float32], Variable[float32], Variable[float32], Variable[float32], Variable[float32]
chainer.functions.separate(Variable[float32], int) -> Variable[float32], Variable[float32], Variable[float32], Variable[float32], Variable[float32], Variable[float32]
chainer.functions.separate(ndarray[float32], int) -> Variable[float32], Variable[float32]
chainer.functions.separate(ndarray[float32], int) -> Variable[float32], Variable[float32], Variable[float32]
chainer.functions.separate(ndarray[float32], int) -> Variable[float32], Variable[float32], Variable[float32], Variable[float32]
chainer.functions.sigmoid(Variable[float32]) -> Variable[float32]
chainer.functions.sigmoid(ndarray[float32]) -> Variable[float32]
chainer.functions.sign(ndarray[float32]) -> Variable[float32]
chainer.functions.sin(ndarray[float32]) -> Variable[float32]
chainer.functions.sinh(ndarray[float32]) -> Variable[float32]
chainer.functions.softmax(Variable[float32], int) -> Variable[float32]
chainer.functions.softmax(ndarray[float32], int) -> Variable[float32]
chainer.functions.softmax_cross_entropy(Variable[float32], Variable[int32], bool, bool, NoneType, int, str, bool) -> Variable[float32]
chainer.functions.softmax_cross_entropy(Variable[float32], ndarray[int32], bool, bool, NoneType, int, str, bool) -> Variable[float32]
chainer.functions.softmax_cross_entropy(Variable[float32], ndarray[int64], bool, bool, NoneType, int, str, bool) -> Variable[float32]
chainer.functions.softmax_cross_entropy(ndarray[float32], ndarray[int64], bool, bool, NoneType, int, str, bool) -> Variable[float32]
chainer.functions.split_axis(Variable[float32], ndarray[int64], int, bool) -> Variable[float32], Variable[float32], Variable[float32]
chainer.functions.split_axis(ndarray[float32], int, int, bool) -> Variable[float32], Variable[float32]
chainer.functions.split_axis(ndarray[float32], list, int, bool) -> Variable[float32], Variable[float32], Variable[float32], Variable[float32], Variable[float32], Variable[float32]
chainer.functions.squeeze(Variable[float32], int) -> Variable[float32]
chainer.functions.squeeze(ndarray[float32], NoneType) -> Variable[float32]
chainer.functions.squeeze(ndarray[float32], int) -> Variable[float32]
chainer.functions.squeeze(ndarray[float32], tuple) -> Variable[float32]
chainer.functions.stack(list, int) -> Variable[float32]
chainer.functions.stack(tuple, int) -> Variable[float32]
chainer.functions.sum(Variable[float32], NoneType, bool) -> Variable[float32]
chainer.functions.sum(Variable[float32], int, bool) -> Variable[float32]
chainer.functions.sum(Variable[float32], tuple, bool) -> Variable[float32]
chainer.functions.sum(ndarray[float32], NoneType, bool) -> Variable[float32]
chainer.functions.sum(ndarray[float32], int, bool) -> Variable[float32]
chainer.functions.sum(ndarray[float32], tuple, bool) -> Variable[float32]
chainer.functions.swapaxes(Variable[float32], int, int) -> Variable[float32]
chainer.functions.swapaxes(ndarray[float32], int, int) -> Variable[float32]
chainer.functions.tan(ndarray[float32]) -> Variable[float32]
chainer.functions.tanh(Variable[float32]) -> Variable[float32]
chainer.functions.tanh(ndarray[float32]) -> Variable[float32]
chainer.functions.unpooling_2d(ndarray[float32], int, NoneType, int, NoneType, bool) -> Variable[float32]
chainer.functions.unpooling_2d(ndarray[float32], tuple, NoneType, int, NoneType, bool) -> Variable[float32]
chainer.functions.vstack(list) -> Variable[float32]
chainer.functions.vstack(tuple) -> Variable[float32]
numpy.allclose(ndarray[float32], ndarray[float32], float, float, bool) -> bool
numpy.allclose(ndarray[int64], ndarray[int64], float, float, bool) -> bool
numpy.max(ndarray[float32], NoneType, NoneType, bool, _NoValueType, _NoValueType) -> float32
numpy.max(ndarray[float32], NoneType, NoneType, bool, _NoValueType, _NoValueType) -> ndarray[float32]
numpy.max(ndarray[float32], int, NoneType, _NoValueType, _NoValueType, _NoValueType) -> ndarray[float32]
numpy.max(ndarray[float32], tuple, NoneType, bool, _NoValueType, _NoValueType) -> ndarray[float32]
numpy.max(ndarray[int64], NoneType, NoneType, _NoValueType, _NoValueType, _NoValueType) -> int64
numpy.min(ndarray[float32], NoneType, NoneType, bool, _NoValueType, _NoValueType) -> float32
numpy.min(ndarray[float32], NoneType, NoneType, bool, _NoValueType, _NoValueType) -> ndarray[float32]
numpy.min(ndarray[float32], tuple, NoneType, bool, _NoValueType, _NoValueType) -> ndarray[float32]
numpy.min(ndarray[int64], NoneType, NoneType, _NoValueType, _NoValueType, _NoValueType) -> int64
numpy.argmax(ndarray[float32], NoneType, NoneType) -> int64
numpy.argmax(ndarray[float32], int, NoneType) -> ndarray[int64]
numpy.argmin(ndarray[float32], NoneType, NoneType) -> int64
numpy.argmin(ndarray[float32], int, NoneType) -> ndarray[int64]
numpy.argsort(list, int, NoneType, NoneType) -> ndarray[int64]
numpy.asanyarray(ndarray[float32], NoneType, NoneType) -> ndarray[float32]
numpy.asarray(DummyArray, NoneType, NoneType) -> ndarray[float32]
numpy.asarray(float, NoneType, NoneType) -> ndarray[float64]
numpy.asarray(float32, NoneType, NoneType) -> ndarray[float32]
numpy.asarray(float64, dtype, NoneType) -> ndarray[float32]
numpy.asarray(int, NoneType, NoneType) -> ndarray[int64]
numpy.asarray(int32, NoneType, NoneType) -> ndarray[int32]
numpy.asarray(ndarray[float32], NoneType, NoneType) -> ndarray[float32]
numpy.asarray(ndarray[int32], NoneType, NoneType) -> ndarray[int32]
numpy.asarray(tuple, NoneType, NoneType) -> ndarray[int64]
numpy.average(ndarray[float32], int, NoneType, bool) -> ndarray[float32]
numpy.broadcast_arrays(ndarray[int64], ndarray[int64], ndarray[float32], ndarray[float32]) -> ndarray[int64], ndarray[int64], ndarray[float32], ndarray[float32]
numpy.broadcast_to(ndarray[float32], tuple, bool) -> ndarray[float32]
numpy.broadcast_to(ndarray[int64], tuple, bool) -> ndarray[int64]
numpy.clip(ndarray[float32], float, float, NoneType) -> ndarray[float32]
numpy.concatenate(tuple, int) -> ndarray[float32]
numpy.concatenate(tuple, int) -> ndarray[int32]
numpy.cumsum(list, NoneType, NoneType, NoneType) -> ndarray[int64]
numpy.cumsum(ndarray[int64], NoneType, NoneType, NoneType) -> ndarray[int64]
numpy.dot(ndarray[float32], ndarray[float32], out: ndarray[float32]) -> ndarray[float32]
numpy.einsum(str, ndarray[float32], ndarray[float32], out: ndarray[float32]) -> ndarray[float32]
numpy.empty_like(ndarray[float32]) -> ndarray[float32]
numpy.empty_like(ndarray[int32]) -> ndarray[int32]
numpy.expand_dims(ndarray[float32], int) -> ndarray[float32]
numpy.expand_dims(ndarray[int64], int) -> ndarray[int64]
numpy.flip(ndarray[int64], int) -> ndarray[int64]
numpy.full(int, float, type, str) -> ndarray[float32]
numpy.full(tuple, float, dtype, str) -> ndarray[float32]
numpy.full(tuple, int, NoneType, str) -> ndarray[int64]
numpy.full(tuple, int, type, str) -> ndarray[float32]
numpy.full_like(ndarray[float32], float, NoneType, str, bool, NoneType) -> ndarray[float32]
numpy.hstack(tuple) -> ndarray[float32]
numpy.isscalar(Constant) -> bool
numpy.isscalar(HeNormal) -> bool
numpy.isscalar(LeCunNormal) -> bool
numpy.isscalar(Normal) -> bool
numpy.isscalar(Variable[float32]) -> bool
numpy.isscalar(float) -> bool
numpy.isscalar(float32) -> bool
numpy.isscalar(float64) -> bool
numpy.isscalar(int) -> bool
numpy.isscalar(ndarray[float32]) -> bool
numpy.issubdtype(dtype, type) -> bool
numpy.iterable(tuple) -> bool
numpy.linspace(int, int, int, bool, bool, type, int) -> ndarray[float64]
numpy.mean(list, NoneType, NoneType, NoneType, _NoValueType) -> float64
numpy.ones(int, type, str) -> ndarray[int64]
numpy.ones(tuple, type, str) -> ndarray[int32]
numpy.pad(ndarray[float32], tuple, str, constant_values: tuple) -> ndarray[float32]
numpy.prod(int, NoneType, NoneType, NoneType, _NoValueType, _NoValueType, _NoValueType) -> int64
numpy.prod(tuple, NoneType, NoneType, NoneType, _NoValueType, _NoValueType, _NoValueType) -> int64
numpy.rollaxis(ndarray[float32], int, int) -> ndarray[float32]
numpy.round_(ndarray[int64], int, NoneType) -> ndarray[int64]
numpy.vstack(tuple) -> ndarray[float32]
numpy.sort(ndarray[int64], int, NoneType, NoneType) -> ndarray[int64]
numpy.split(ndarray[float32], int, int) -> ndarray[float32], ndarray[float32]
numpy.split(ndarray[float32], int, int) -> ndarray[float32], ndarray[float32], ndarray[float32]
numpy.split(ndarray[float32], int, int) -> ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32]
numpy.split(ndarray[float32], int, int) -> ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32]
numpy.split(ndarray[float32], int, int) -> ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32]
numpy.split(ndarray[float32], list, int) -> ndarray[float32], ndarray[float32]
numpy.split(ndarray[float32], list, int) -> ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32], ndarray[float32]
numpy.split(ndarray[float32], ndarray[int64], int) -> ndarray[float32], ndarray[float32], ndarray[float32]
numpy.squeeze(ndarray[float32], NoneType) -> ndarray[float32]
numpy.squeeze(ndarray[float32], int) -> ndarray[float32]
numpy.squeeze(ndarray[float32], tuple) -> ndarray[float32]
numpy.stack(tuple, int, NoneType) -> ndarray[float32]
numpy.tensordot(ndarray[float32], ndarray[float32], tuple) -> ndarray[float32]
numpy.tile(ndarray[float32], tuple) -> ndarray[float32]
numpy.unravel_index(ndarray[int64], tuple) -> ndarray[int64], ndarray[int64]
numpy.where(ndarray[bool], int, ndarray[int64]) -> ndarray[int64]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment