pytorch/caffe2/predictor/predictor.h
Hao Lu 13db5dbb81 Make predictor base class
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/16541

Reviewed By: ajtulloch

Differential Revision: D13858261

fbshipit-source-id: acbfdbea59bd20ab1cc7956ee0d8856d6faa8361
2019-02-01 20:59:19 -08:00

72 lines
1.8 KiB
C++

#pragma once
#include <unordered_set>
#include "caffe2/core/net.h"
#include "caffe2/core/tensor.h"
#include "caffe2/predictor/predictor_config.h"
namespace caffe2 {
class CAFFE2_API Predictor {
public:
using TensorList = std::vector<TensorCPU>;
using TensorMap = std::unordered_map<std::string, TensorCPU>;
Predictor(
const NetDef& init_net,
const NetDef& run_net,
Workspace* parent = nullptr,
bool run_init = true,
int optimization = 1);
Predictor(PredictorConfig config);
virtual ~Predictor() {}
// Executes `run_net` on the inputs.
// The first `inputs.size()` inputs from run_net::external_inputs
// are shared with the data in `inputs`.
// Precondition:
// inputs.size() <= run_net_.external_inputs.size()
// Postcondition:
// outputs->size() == run_net.external_inputs.size()
// NOTE: output is a part of thread local workspace
// and is only valid until the next predictor execution.
// Returns true on success
virtual bool operator()(const TensorList& inputs, TensorList* outputs);
// Similar to run, but consumes a map of name to tensor as input
bool operator()(const TensorMap& inputs, TensorList* outputs);
// Similar to the other run fns, except inputs and outputs are both maps of
// string name to tensor.
bool operator()(const TensorMap& inputs, TensorMap* outputs);
const NetDef& def() const {
return *config_.predict_net;
};
Workspace* ws() {
return config_.ws.get();
};
const std::vector<std::string>& input_names() const {
return config_.input_names;
}
const std::vector<std::string>& output_names() const {
return config_.output_names;
}
private:
bool run_map_workspace(const TensorMap& inputs);
protected:
PredictorConfig config_;
};
} // namespace caffe2