forked from ROCm/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.h
85 lines (70 loc) · 2.08 KB
/
interpreter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once
#include <memory>
#include <vector>
#include "c10/util/Optional.h"
#include "torch/csrc/jit/ivalue.h"
#include "torch/csrc/WindowsTorchApiMacro.h"
namespace at {
class Tensor;
}
namespace c10 {
struct IValue;
}
namespace torch { namespace jit {
// The interpreter run Graphs with Tensor inputs and Tensor outputs
// a separate component in the autograd handles unwrapping and wrapping
// variable objects for use in the interpreter.
struct Node;
struct GraphExecutor;
struct CodeImpl;
struct InterpreterStateImpl;
struct Graph;
struct Node;
using Stack = std::vector<c10::IValue>;
struct TORCH_API Code {
Code()
: pImpl(nullptr) {}
explicit Code(const std::shared_ptr<Graph>& graph);
~Code();
const std::vector<GraphExecutor*>& grad_executors();
explicit operator bool() const {
return pImpl != nullptr;
}
private:
std::shared_ptr<CodeImpl> pImpl;
friend struct InterpreterStateImpl;
friend std::ostream & operator<<(std::ostream & out, const Code & code);
};
struct InterpreterState {
InterpreterState(const Code & code);
void run(Stack& stack);
c10::intrusive_ptr<Future> runAsync(Stack& stack);
c10::intrusive_ptr<Future> getFuture();
~InterpreterState();
private:
InterpreterState(c10::intrusive_ptr<c10::intrusive_ptr_target> pImpl);
// Ideally we should use c10::intrusive_ptr<InterpreterStateImpl> for pImpl;
// but intrusive_ptr requires full definition of InterpreterStateImpl,
// which we need to hide in the header.
c10::intrusive_ptr<c10::intrusive_ptr_target> pImpl;
friend struct InterpreterStateImpl;
};
// Created by wait()
struct Suspend : public std::exception {
virtual const char* what() const noexcept override {
return "Suspend";
}
explicit Suspend(c10::intrusive_ptr<Future> future_) : future(future_) {}
c10::intrusive_ptr<Future> future;
};
struct InterpreterContinuation {
InterpreterContinuation(InterpreterState state_, Stack stack_)
: state(std::move(state_)), stack(std::move(stack_)) {}
void operator()(void) {
state.runAsync(stack);
}
private:
InterpreterState state;
Stack stack;
};
}}