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

Add interface class member to Benchmark class #20

Merged
merged 10 commits into from
Jan 24, 2024
20 changes: 18 additions & 2 deletions src/nnbench/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Useful type interfaces to override/subclass in benchmarking workflows."""
from __future__ import annotations

import inspect
import os
from dataclasses import dataclass, field
from typing import Any, Callable, Generic, TypedDict, TypeVar
from typing import Any, Callable, Dict, Generic, Tuple, TypedDict, TypeVar
maxmynter marked this conversation as resolved.
Show resolved Hide resolved

T = TypeVar("T")

Expand Down Expand Up @@ -93,9 +94,24 @@ class Benchmark:
tearDown: Callable[..., None] = field(repr=False, default=NoOp)
tags: tuple[str, ...] = field(repr=False, default=())

@dataclass(frozen=True)
class Interface:
fn: Callable[..., Any]
varnames: Tuple[str, ...] | None = field(default=None)
vartypes: Tuple[inspect.Parameter, ...] | None = field(default=None)
varitems: Tuple[Tuple[str, inspect.Parameter], ...] | None = field(default=None)
defaults: Dict[str, Any] | None = field(default=None)

def __post_init__(self) -> None:
sig = inspect.signature(self.fn)
super().__setattr__("varnames", tuple(sig.parameters.keys()))
super().__setattr__("vartypes", tuple(sig.parameters.values()))
super().__setattr__("varitems", tuple(sig.parameters.items()))
super().__setattr__("defaults", {n: p.default for n, p in sig.parameters.items()})
nicholasjng marked this conversation as resolved.
Show resolved Hide resolved

def __post_init__(self):
if not self.name:
name = self.fn.__name__

super().__setattr__("name", name)
# TODO: Parse interface using `inspect`, attach to the class
super().__setattr__("interface", self.Interface(self.fn))
32 changes: 32 additions & 0 deletions tests/test_benchmark_cls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import inspect

import nnbench


def test_interface_with_no_arguments():
def empty_function() -> None:
pass

interface = nnbench.Benchmark.Interface(empty_function)
assert interface.varnames == ()
assert interface.vartypes == ()
assert interface.varitems == ()
assert interface.defaults == {}


def test_interface_with_multiple_arguments():
def complex_function(a: int, b, c: str = "hello", d: float = 10.0) -> None: # type:ignore
pass

interface = nnbench.Benchmark.Interface(complex_function)
assert interface.varnames == ("a", "b", "c", "d")
assert tuple(param.annotation for param in interface.vartypes) == (
int,
inspect._empty,
str,
float,
)

varitems = [(param.name, param.annotation) for name, param in interface.varitems]
assert varitems == [("a", int), ("b", inspect._empty), ("c", str), ("d", float)]
maxmynter marked this conversation as resolved.
Show resolved Hide resolved
assert interface.defaults == {"a": inspect._empty, "b": inspect._empty, "c": "hello", "d": 10.0}
Loading