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: Thunks as an alternative to artifacts #120

Merged
merged 7 commits into from
Mar 21, 2024
Merged

Conversation

nicholasjng
Copy link
Collaborator

This branch contains Thunks as an alternative to Artifacts. Wiki -> https://en.wikipedia.org/wiki/Thunk.

This is basically the same as React's useMemo. Thunks are lazy-loadable similarly to artifacts, customizable as a class if state is required (with the generic nnbench.types.Thunk type), and usable as a drop-in replacement for a normal e.g. torch.Module value to be computed at benchmark runtime.

The strategy here is to bootstrap each value in the Thunk.__call__() method, relying on caching as much as possible to avoid recomputing values.

Another important addition is the BenchmarkRunner.typecheck flag, which can be turned off to skip typechecks and avoid shortcomings / bugs in our current typechecking logic.

Still to do:

  • Adding a cache for computed thunk memos.
  • Checking memory usage when models are loaded lazily instead of all at once.
  • Re-implement the NER example with thunks (almost done).
  • See if the UX/DX is better: clean code, runtimes, maintainability, ...

@nicholasjng nicholasjng added the refactor Makes existing code nicer and more useable. label Mar 19, 2024
@nicholasjng nicholasjng self-assigned this Mar 19, 2024
This is the better way of compressing parameters compared to directly in
the benchmark runner, which steals responsibility of the transform that
we just introduced.

Refactors `nnbench.io.transform->nnbench.transforms`, the latter being
its own submodule. This is useful to have when adding new builtin
transforms, so that they do not have to go into a single file.
Adds two conditional branches to disable typechecks in the _check()
method.

This is nice to have when prototyping new features and inputs to
benchmarks might not exactly be of the requested types.
Also stop the practice of binding partial parametrizations directly to
benchmarks. This has the effect that we can manipulate benchmark function
parameters if need be (for example by lazy-loading thunk parameters).

Changes the interface construction slightly to inject the partial
parametrization as defaults over the `inspect.Parameter` default values.
Slightly changes parameter construction and adds a dethunking step right
before the benchmark loop.

This means that the thunk values are accessed at the latest possible
time, which is just before benchmark execution.

Moves the context construction ahead of the empty collection check, so
that we give back a constructed context even in the case of no found
benchmarks.

Adds two C++-style thunk helpers, `is_thunk` for deciding if a value is
a thunk, and `is_thunk_type` to decide if a value type is a thunk type
annotation.

The whole thunk facility is designed to work both with the
`nnbench.types.Thunk` type as well as with general anonymous functions.
In the current setup, properly typed memos and callables pass the type
checker.

Factors out the types into their own submodule, to be refactored later
into their biggest constituents.
Showcases memo subclassing, parametrization, and trivializes the run()
command again.

As a downside, only per-class benchmarks and aggregates can be run in
a single run, not side-by-side (that would require `params` injection).
Partial parametrizations are not bound eagerly to the benchmark functions
anymore, which makes it simpler to inject memos and de-memoize variables
just in time for execution.

What is left is validation that a subsequent benchmark of models with
intermittent garbage collection actually reaps each model after the
benchmark is done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Makes existing code nicer and more useable.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant