I implemented a FunctionPreHook and use variable.add_hook to “register”, but at runtime the hook is not invoked at all. I checked the length of pre_hooks_ for Functions at runtime and they are all 0s.
This is where I checked length of pre_hooks_, inside engine.cpp
static variable_list call_pre_hooks(Function& fn, variable_list inputs) {
std::cout << fn.pre_hooks().size() << std::endl;
for (const auto& hook : fn.pre_hooks()) {
inputs = (*hook)(inputs);
}
return inputs;
}
I checked code under csrc/autograd but didn’t find where add_pre_hook() is called in cpp, or where hooks in variables are added to pre_hooks_ in Function.
Did I miss something? Thanks!
This is a minimal example, where “here” is expected to be printed out twice.
using namespace torch::autograd;
struct DummyHook : public FunctionPreHook {
DummyHook(Variable* x) : v(x) {};
variable_list operator()(const variable_list& grads) {
std::cout << "here" << std::endl;
return grads;
}
Variable* v;
};
int main(int argc, char** argv) {
torch::Tensor x = torch::tensor({1.0}, at::requires_grad()); // CPUDoubleType
Variable y = x * x;
y.add_hook(std::shared_ptr<DummyHook>(new DummyHook(&y)));
torch::Tensor z = 2 * y;
for (auto hook: y.hooks()) {
(*hook.get())({});
}
z.backward();
}