-
Notifications
You must be signed in to change notification settings - Fork 321
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
Equivalent of register_forward_hook
#218
Comments
register_forward_hook
I don't think there is any equivalent for this at the moment: the current api is mostly generated automatically from the declarations.yaml file and I haven't found anything related in it. |
So, I haven't used the C++ API before, so I'm not quite sure how to replicate that. Here's how I've been using it in Python: Let's say I have a DistilBert architecture:
Note: The architecture is cut to keep it brief Now, let's say I want to run my model using the library in a standard way, like this:
But at the same time, by running the above line, I want to save the output of The final goal is to store them in a list so that I can, for example for each layer, study each one of In PyTorch, you'd do it using hooks this way:
name_list = ['k_lin', 'q_lin']
NUM_LAYERS = 6
activations = {i : {name_ : [] for name_ in name_list} for i in range(NUM_LAYERS)}
def create_hook(layer_, name_):
def hook(model, input_, output_):
activations[layer_][name_].append(output_.detach())
return hook
# Access the q_lin layer and register a hook
for i in range(NUM_LAYERS):
model.transformer.layer[i].attention.q_lin.register_forward_hook(create_hook(i, 'q_lin'))
model.transformer.layer[i].attention.k_lin.register_forward_hook(create_hook(i, 'k_lin'))
model.encode('whatever') Hope this helps |
Just pinging the thread to see if there's any update on this |
@LaurentMazare It seems that there is a |
Hi,
I've been using hooks and especially register_forward_hook in PyTorch and wanted to be able to do the same in Rust.
Unless I missed something (I checked both
torch-sys
andtch-rs
for a similar function), is there any way to emulate a hook using your API?Thanks a lot
The text was updated successfully, but these errors were encountered: