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

4
#include <string>
5
#include <unordered_map>
6 7 8
#include <variant>

#include "megbrain/imperative/dispatch.h"
M
Megvii Engine Team 已提交
9 10
#include "megbrain/imperative/interpreter.h"
#include "pybind11/pybind11.h"
11 12

#include "./pyext17.h"
13
#include "megbrain/imperative/dispatch.h"
14 15
#include "megbrain/imperative/transformations/scalar.h"
#include "megbrain/imperative/transformations/symbol.h"
16
#include "megbrain/imperative/utils/span.h"
17 18 19

namespace mgb::imperative::python {

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

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

namespace mgb::imperative::python {

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

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

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

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

48 49
    ~Tensor() = default;

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

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

struct TensorWrapper {
82
public:
83
    std::optional<Tensor> m_tensor;
84

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

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

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

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

143 144
void init_tensor(pybind11::module);

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

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

namespace pybind11::detail {

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

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