tensor.h 4.5 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 void set_format(std::string format) {
        if (!format.empty()) {
            m_data = imperative::apply(SetFormat(format), m_data)[0];
        }
    }
67 68 69 70 71
    inline HostValue::ref_t numpy() { return data().numpy(); }
    inline void reset(ValueRef value) {
        m_data = value;
        if (!m_name.empty()) {
            set_name(m_name);
72 73
        }
    }
74
    inline ValueRef data() const { return m_data.unwrap(); }
75 76 77 78 79 80 81
    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;
82 83
        }
    }
84 85 86
};

struct TensorWrapper {
87
public:
88
    std::optional<Tensor> m_tensor;
89

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

    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 已提交
119
        auto* op = wrap_t::cnew_with_type(pytype, std::forward<Args>(args)...);
120 121 122 123 124 125
        return pybind11::reinterpret_steal<ObjectPtr<Tensor>>(op);
    }

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

M
Megvii Engine Team 已提交
146 147
PyObject* py_apply(
        PyObject* self, PyObject* const* args, size_t nargs /* , PyObject* kwnames */);
148

149 150
void init_tensor(pybind11::module);

M
Megvii Engine Team 已提交
151
extern PyObject* cpp_apply_module_trace;
152

M
Megvii Engine Team 已提交
153
}  // namespace mgb::imperative::python
154 155 156

namespace pybind11::detail {

M
Megvii Engine Team 已提交
157 158 159
template <>
struct type_caster<mgb::imperative::python::TensorWrapper>
        : mgb::imperative::python::TensorWrapper::wrap_t::caster {};
160

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