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
#include "megbrain/imperative/dispatch.h"
13 14
#include "megbrain/imperative/transformations/scalar.h"
#include "megbrain/imperative/transformations/symbol.h"
15
#include "megbrain/imperative/utils/span.h"
16 17 18

namespace mgb::imperative::python {

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

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

namespace mgb::imperative::python {

30
extern interpreter::Interpreter::Channel* interpreter_for_py;
31
extern PyTypeObject* py_tensor_type;
32
extern PyTypeObject* py_varnode_type;
33
extern pybind11::handle py_device_type;
34 35
extern PyObject* cpp_use_symbolic_shape;
extern PyObject* cpp_astensor1d;
36

37
struct Tensor {
38
private:
39
    ValueRef m_data;
40
    std::string m_name;
M
Megvii Engine Team 已提交
41

42
public:
43 44
    using Handle = interpreter::Interpreter::Handle;

45
    inline explicit Tensor(ValueRef data) : m_data{data} {}
46

47 48
    ~Tensor() = default;

49
    inline Tensor copy() { return *this; }
50

51 52 53 54 55 56
    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 {};
57
        }
58
        return *shape;
59
    }
60 61 62 63 64
    inline HostValue::ref_t numpy() { return data().numpy(); }
    inline void reset(ValueRef value) {
        m_data = value;
        if (!m_name.empty()) {
            set_name(m_name);
65 66
        }
    }
67
    inline ValueRef data() const { return m_data.unwrap(); }
68 69 70 71 72 73 74
    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;
75 76
        }
    }
77 78 79
};

struct TensorWrapper {
80
public:
81
    std::optional<Tensor> m_tensor;
82

83
    inline TensorWrapper(ValueRef value) { m_tensor.emplace(value); }
84 85 86 87 88 89 90 91
    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 已提交
92 93 94
    inline static TensorWrapper* cast(PyObject* obj) {
        return reinterpret_cast<wrap_t*>(obj)->inst();
    }
95
    inline static TensorWrapper* try_cast(PyObject* obj) {
M
Megvii Engine Team 已提交
96 97
        if (!wrap_t::type().isinstance(obj))
            return nullptr;
98
        return cast(obj);
99
    }
M
Megvii Engine Team 已提交
100 101 102
    inline ObjectPtr<TensorWrapper, pybind11::handle> self() {
        return wrap_t::pycast(this);
    }
103 104 105 106 107 108 109 110 111

    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 已提交
112
        auto* op = wrap_t::cnew_with_type(pytype, std::forward<Args>(args)...);
113 114 115 116 117 118 119 120
        return pybind11::reinterpret_steal<ObjectPtr<Tensor>>(op);
    }

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

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

140 141
void init_tensor(pybind11::module);

M
Megvii Engine Team 已提交
142
extern PyObject* cpp_apply_module_trace;
143

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

namespace pybind11::detail {

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

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