-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 utilities for hooking and chaining methods calls along tensor chains #3636
Conversation
Here are some examples of the kinds of code I'm trying to remove:
With |
Codecov Report
@@ Coverage Diff @@
## master #3636 +/- ##
==========================================
+ Coverage 94.68% 94.70% +0.01%
==========================================
Files 161 163 +2
Lines 17246 17294 +48
==========================================
+ Hits 16330 16378 +48
Misses 916 916
|
from functools import wraps | ||
|
||
|
||
def chain_call(obj, method_name, *args, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a nice convenience function - although I'm not sure that we ever truly go more than one child deep (or two children deep in special circumstances) within any call. I do assume that you have a use for it in mind though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we don’t usually reach very far into the chain yet, but having stared at the code for a while, it’s starting to look like it would get simpler/cleaner if we did. There are also a few ad-hoc implementations of this pattern floating around.
from syft.generic.abstract.hookable import hookable | ||
|
||
|
||
def test_chain_call(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! I get it... wow that's clever! Love that!
return results | ||
|
||
|
||
def hookable(hookable_method): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does overlap with ObjectConstructor a bit - but I'm ok with it given that ObjectConstructor doesn't exist yet - and there's a good chance they'll actually be compatible pieces of functionality with each other (aka ObjectConstructor creates the method names which this decorator detects)
Description
Adds utilities for inserting optional
_before_method()
/_after_method()
hooks on a method decorated with@hookable
, which allows custom tensor types to insert any functionality they individually require before or after a generic tensor method likesend
. This should help clean up the copiousif isinstance(self, TensorClass)
code inAbstractTensor
and beyond.In order to make sure that all custom functionality for types in the tensor chain gets called, this also creates a
chain_call()
function, which accepts a method name with args/kwargs, and calls that method (if it exists) on every tensor in the chain from outermost to innermost, returning the results in an ordered list.Type of Change
Please mark options that are relevant.
Checklist