google-deepmind / dm-haiku

JAX-based neural network library
https://dm-haiku.readthedocs.io
Apache License 2.0
2.91k stars 231 forks source link

Add `__signature__` to `hk.ModuleMetaclass`. #619

Closed copybara-service[bot] closed 1 year ago

copybara-service[bot] commented 1 year ago

Add __signature__ to hk.ModuleMetaclass.

Previously, calling inspect.signature on a Haiku class would result in an inferred signature to be equivalent to def f(*args, **kwargs). This is suboptimal because (1) interactive systems like Colab can't provide useful help with the signature, and (2) systems like Fiddle can't eagerly enforce parameter names.

Although there are a few other ways to implement this functionality (such as by computing the signature and assigning it during __call__ or __new__), these are incompatible with @dataclasses.dataclass decorators, which compute a new __init__ function + signature after the class is defined. As a result, the @property approach works best. If this becomes a performance problem, it can be memoized, although systems arguably should be doing their own memoizing (e.g. Fiddle already does this).