mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/37025 This allows us to reuse this framework in other places. Test Plan: buck test mode/dev-nosan caffe2/torch/fb/distributed/model_parallel/tests:test_dist_optim -- test_optimizer_hook Differential Revision: D20958327 fbshipit-source-id: 2a37dae3687fea8820427e174900111b58673194
35 lines
976 B
C++
35 lines
976 B
C++
#pragma once
|
|
|
|
#include <torch/csrc/autograd/function_hook.h>
|
|
|
|
namespace torch {
|
|
namespace autograd {
|
|
namespace utils {
|
|
|
|
// Turns lambda into a torch::autograd::FunctionPostHook.
|
|
class LambdaPostHook : public torch::autograd::FunctionPostHook {
|
|
using variable_list = std::vector<torch::autograd::Variable>;
|
|
|
|
public:
|
|
// The lambda function takes as arguments the outputs and inputs of the
|
|
// autograd function and can modify the outputs of the autograd function by
|
|
// returning a new output if needed.
|
|
/* implicit */ LambdaPostHook(
|
|
std::function<variable_list(const variable_list&, const variable_list&)>
|
|
fn)
|
|
: fn_(std::move(fn)) {}
|
|
|
|
variable_list operator()(
|
|
const variable_list& outputs,
|
|
const variable_list& inputs) override {
|
|
return fn_(outputs, inputs);
|
|
}
|
|
|
|
protected:
|
|
std::function<variable_list(const variable_list&, const variable_list&)> fn_;
|
|
};
|
|
|
|
} // namespace utils
|
|
} // namespace autograd
|
|
} // namespace torch
|