tensor.h 4.2 KB
Newer Older
1
#pragma once
2
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
3 4 5

#include <variant>

6
#include <string>
7
#include <unordered_map>
M
Megvii Engine Team 已提交
8 9
#include "megbrain/imperative/interpreter.h"
#include "pybind11/pybind11.h"
10 11

#include "./pyext17.h"
12 13
#include "megbrain/imperative/dispatch.h"
#include "megbrain/imperative/utils/span.h"
14 15 16

namespace mgb::imperative::python {

M
Megvii Engine Team 已提交
17
template <typename T, typename B = pybind11::object>
18 19
struct ObjectPtr : B {
    using B::B;
M
Megvii Engine Team 已提交
20 21
    T& operator*() { return reinterpret_cast<T&>(*B::ptr()); }
    T* operator->() { return reinterpret_cast<T*>(B::ptr()); }
22 23
};

M
Megvii Engine Team 已提交
24
}  // namespace mgb::imperative::python
25 26 27

namespace mgb::imperative::python {

28
extern interpreter::Interpreter::Channel* interpreter_for_py;
29
extern PyTypeObject* py_tensor_type;
30
extern pybind11::handle py_device_type;
31 32
extern PyObject* cpp_use_symbolic_shape;
extern PyObject* cpp_astensor1d;
33

34
struct Tensor {
35
private:
36
    ValueRef m_data;
37
    std::string m_name;
M
Megvii Engine Team 已提交
38

39
public:
40 41
    using Handle = interpreter::Interpreter::Handle;

42
    inline explicit Tensor(ValueRef data) : m_data{data} {}
43

44 45
    ~Tensor() = default;

46
    inline Tensor copy() { return *this; }
47

48 49 50 51 52 53
    inline DType dtype() { return *data().dtype(); }
    inline CompNode comp_node() { return *data().device(); }
    inline std::optional<ValueShape> shape() {
        auto shape = data().shape();
        if (!shape) {
            return {};
54
        }
55
        return *shape;
56
    }
57 58 59 60 61
    inline HostValue::ref_t numpy() { return data().numpy(); }
    inline void reset(ValueRef value) {
        m_data = value;
        if (!m_name.empty()) {
            set_name(m_name);
62 63
        }
    }
64
    inline ValueRef data() const { return m_data.unwrap(); }
65 66 67 68 69 70 71
    bool is_scalar() { return data().is_scalar(); }
    inline std::string name() { return m_name; }
    inline void set_name(std::string name) {
        m_name = name;
        if (!name.empty()) {
            auto output = imperative::apply(RenameValue(name), m_data)[0];
            m_data = output;
72 73
        }
    }
74 75 76
};

struct TensorWrapper {
77
public:
78
    std::optional<Tensor> m_tensor;
79

80
    inline TensorWrapper(ValueRef value) { m_tensor.emplace(value); }
81 82 83 84 85 86 87 88
    TensorWrapper(PyObject* args, PyObject* kwargs);
    ~TensorWrapper() = default;

    static constexpr auto tp_name = pybind11::detail::_("Tensor");

    using wrap_t = pyext17::wrap<TensorWrapper>;
    friend wrap_t;

M
Megvii Engine Team 已提交
89 90 91
    inline static TensorWrapper* cast(PyObject* obj) {
        return reinterpret_cast<wrap_t*>(obj)->inst();
    }
92
    inline static TensorWrapper* try_cast(PyObject* obj) {
M
Megvii Engine Team 已提交
93 94
        if (!wrap_t::type().isinstance(obj))
            return nullptr;
95
        return cast(obj);
96
    }
M
Megvii Engine Team 已提交
97 98 99
    inline ObjectPtr<TensorWrapper, pybind11::handle> self() {
        return wrap_t::pycast(this);
    }
100 101 102 103 104 105 106 107 108

    template <typename... Args>
    static ObjectPtr<Tensor> make(Args&&... args) {
        auto* op = wrap_t::cnew(std::forward<Args>(args)...);
        return pybind11::reinterpret_steal<ObjectPtr<Tensor>>(op);
    }

    template <typename... Args>
    static ObjectPtr<Tensor> make(PyTypeObject* pytype, Args&&... args) {
M
Megvii Engine Team 已提交
109
        auto* op = wrap_t::cnew_with_type(pytype, std::forward<Args>(args)...);
110 111 112 113 114 115 116 117
        return pybind11::reinterpret_steal<ObjectPtr<Tensor>>(op);
    }

    PyObject* shape();
    PyObject* dtype();
    PyObject* device();
    PyObject* numpy();
    void reset(PyObject*);
118
    PyObject* detach();
119
    PyObject* isscalar();
120 121
    PyObject* _dev_tensor();
    void _drop();
122
    PyObject* varnode();
123
    PyObject* recording();
124
    PyObject* copied();
125
    PyObject* module_trace_info();
M
Megvii Engine Team 已提交
126
    void set_module_trace_info(PyObject*);
127 128 129
    void _set_name(PyObject*);
    PyObject* _detail();
    void _watch();
130 131
};

132 133 134 135
struct PySymbolVar {
    cg::VarNode* m_node = nullptr;
    bool is_scalar = false;
    PySymbolVar() = default;
M
Megvii Engine Team 已提交
136
    PySymbolVar(VarNode* m) : m_node(m) {}
137
};
138

M
Megvii Engine Team 已提交
139 140
PyObject* py_apply(
        PyObject* self, PyObject* const* args, size_t nargs /* , PyObject* kwnames */);
141

142 143
void init_tensor(pybind11::module);

M
Megvii Engine Team 已提交
144
extern PyObject* cpp_apply_module_trace;
145

M
Megvii Engine Team 已提交
146
}  // namespace mgb::imperative::python
147 148 149

namespace pybind11::detail {

M
Megvii Engine Team 已提交
150 151 152
template <>
struct type_caster<mgb::imperative::python::TensorWrapper>
        : mgb::imperative::python::TensorWrapper::wrap_t::caster {};
153

M
Megvii Engine Team 已提交
154
}  // namespace pybind11::detail