tensor.cpp 34.6 KB
Newer Older
1 2 3 4
/**
 * \file imperative/python/src/tensor.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8 9 10 11
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

12 13
#include "megbrain/dtype.h"
#include "megbrain/common.h"
14
#include "megbrain/imperative/ops/utility.h"
15
#include "megbrain/imperative/ops/backward_graph.h"
16

17 18
#include "./tensor.h"
#include "./grad.h"
19
#include "./trace.h"
20 21
#include "./common.h"
#include "./numpy_dtypes.h"
22
#include "./graph_rt.h"
23
#include "./helper.h"
24 25 26

#include <pybind11/numpy.h>
#include <pybind11/operators.h>
27
#include <range/v3/all.hpp>
28
#include <string>
29 30 31

#include <unordered_map>

32
namespace py = pybind11;
33
namespace views = ranges::views;
34 35 36

namespace mgb::imperative::python {

37
interpreter::Interpreter::Channel* interpreter_for_py;
38

39
PyObject *cpp_apply_with_tracing, *cpp_apply_const_with_tracing;
40
PyObject *cpp_apply_backward_varnode;
41

42 43 44
#define REGISTE_APPLY_FUNC(mode)            \
        void set_##mode(py::object pyf) {   \
            mode = pyf.ptr();               \
45 46 47 48 49 50 51 52
        }

REGISTE_APPLY_FUNC(cpp_apply_with_tracing)
REGISTE_APPLY_FUNC(cpp_apply_const_with_tracing)
REGISTE_APPLY_FUNC(cpp_apply_backward_varnode)

#undef REGISTE_APPLY_FUNC

53 54
Tensor::flags_t ApplyContext::global_disable = 0;
Tensor::flags_t ApplyContext::global_enable = 0;
55

56 57
void set_tracing() { ApplyContext::global_enable |= Tensor::Flags::TRACE; }
void unset_tracing() { ApplyContext::global_enable &= ~Tensor::Flags::TRACE; }
58 59 60

bool skip_tracing = false;

61 62 63 64
apply_result_t apply(ApplyContext& ctx) {
    // emulating scalar should be put to specific op's apply, e.g.,
    // elementwise, reduce, typecvt. Currently it's still handled at python
    // side. It could be move to C++ side if it has an impact on performance
65
    auto flags = ctx.flags & ~ApplyContext::global_disable;
66
    flags = flags | ApplyContext::global_enable;
67 68

    if (flags & Tensor::Flags::SCALAR) {
69 70 71
        // TODO: emulate scalar
    }

72
    if (flags & Tensor::Flags::GRAD) {
73 74 75
        return apply_grad(ctx);
    }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if (auto* op = ctx.op->try_cast_final<GenericPyOp>()) {
        py::tuple pyin(ctx.nargs);
        for (size_t i = 0; i < ctx.nargs; ++i) {
            pyin[i] = TensorWrapper::make(ctx.pytype, ctx.args[i]->shared_from_this());
        }
        auto f = py::getattr(op->obj, "_default_rule");
        auto pyout = py::reinterpret_steal<py::object>(PyObject_Call(f.ptr(), pyin.ptr(), nullptr));
        if (!pyout) throw py::error_already_set();
        if (auto* tw = TensorWrapper::try_cast(pyout.ptr())) {
            return {tw->m_tensor};
        }
        apply_result_t ret;
        ret.reserve(py::len(pyout));
        for (auto&& i : pyout) {
            auto* tw = TensorWrapper::try_cast(i.ptr());
            mgb_assert(tw);
            ret.push_back(tw->m_tensor);
        }
        return ret;
    }

97
    if (flags & Tensor::Flags::TRACE) {
98
        return apply_trace(ctx);
99 100 101 102 103 104
    } else {
        SmallVector<interpreter::Interpreter::Handle> handles(ctx.nargs);
        for (size_t i = 0; i < ctx.nargs; ++i) {
            handles[i] = ctx.args[i]->m_handle.get();
        }

105 106 107 108 109 110 111 112 113 114
        apply_result_t outputs;

        // fast copy without really applying
        if (ctx.op->same_type<FastpathCopy>()) {
            mgb_assert(ctx.nargs == 1);
            outputs.reserve(ctx.nargs);
            outputs.emplace_back(std::make_shared<Tensor>(ctx.args[0]->m_handle));
            return outputs;
        }

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        auto output_handles = interpreter_for_py->apply_op(ctx.op, handles);

        outputs.reserve(output_handles.size());
        for (auto h : output_handles) {
            outputs.emplace_back(std::make_shared<Tensor>(h));
        }
        return outputs;
    }

    mgb_assert(0);
}

PyObject* py_apply(PyObject* self, PyObject*const* args, size_t nargs/* , PyObject* kwnames */) {
    try {
        // if (kwnames && PyTuple_GET_SIZE(kwnames)) {
        //     PyErr_SetString(PyExc_TypeError, "keyword argument not allowed");
        //     return nullptr;
        // }
133 134 135 136
        if (nargs < 2) {
            PyErr_SetString(PyExc_TypeError,
                            "py_apply expects one Op and at least one tensor "
                            "as argument");
137 138
            return nullptr;
        }
139

140 141 142
        auto* op = args[0];

        PyTypeObject* pytype = args[1]->ob_type;
143 144 145 146 147 148

        // check if pytype is Parameter(and all other python Tensor's derived class),
        // if yes, using it's tp_base(python Tensor)
        if (TensorWrapper::wrap_t::type().same_pytype(pytype->tp_base->tp_base)) {
            pytype = pytype->tp_base;
        }
149 150 151 152 153 154 155 156 157
        ++args;
        --nargs;

        ApplyContext ctx;
        ctx.flags = 0;
        ctx.op = py::handle(op).cast<std::shared_ptr<OpDef>>();
        SmallVector<Tensor*, 64> tensors(nargs);
        ctx.args = &tensors[0];
        ctx.nargs = nargs;
158
        ctx.pytype = pytype;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

        if (py::isinstance<PySymbolVar>(py::handle(args[0]))){
            SmallVector<cg::VarNode*> vinputs(nargs);
            for (size_t i = 0; i < nargs; ++i) {
                    vinputs[i] = py::handle(args[i]).cast<PySymbolVar*>()->m_node;   
            }
            auto op = ctx.op.get();
            auto rst = OpDef::apply_on_var_node(*op, vinputs);
            auto ret = pybind11::tuple(rst.size());
            auto typeobj = py::handle(args[0]).get_type();
            for (size_t i = 0; i<rst.size(); ++i) {
                ret[i] = typeobj(pybind11::cast(rst[i], pybind11::return_value_policy::automatic));
            }
            return ret.release().ptr();
        }
174 175

        for (size_t i = 0; i < nargs; ++i) {
176
            if (TensorWrapper* tw = TensorWrapper::try_cast(args[i])) {
177 178 179
                auto* t = tensors[i] = tw->m_tensor.get();
                ctx.flags |= t->m_flags;
            } else {
180 181 182
                PyErr_SetString(PyExc_TypeError,
                    ssprintf("op %s expect type Tensor as inputs, got %s actually",
                        ctx.op->make_name().c_str(), Py_TYPE(args[i])->tp_name).c_str());
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
                return nullptr;
            }
        }

        auto outputs = apply(ctx);
        size_t nout = outputs.size();
        auto ret = py::tuple(nout);
        for (size_t i = 0; i < nout; ++i) {
            ret[i] = TensorWrapper::make(pytype, std::move(outputs[i]));
        }
        return ret.release().ptr();
    } catch (std::exception& e) {
        PyErr_SetString(PyExc_RuntimeError, e.what());
        return nullptr;
    }
}


TensorWrapper::TensorWrapper(PyObject* args, PyObject* kwargs) {
    if (kwargs && PyDict_Size(kwargs)) {
        throw py::type_error("keyword argument not allowed");
    }
    auto nargs = PyTuple_Size(args);
    auto tup = py::reinterpret_borrow<py::tuple>(args);
    if (nargs == 0) {
        throw py::type_error("too few arguments");
    }
210
    if (auto* t = try_cast(tup[0].ptr())) {
211 212 213 214 215
        if (nargs > 1) {
            throw py::type_error("expect 1 argument");
        }
        m_tensor = t->m_tensor;
    } else {
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        if (nargs == 1) {
            auto arg0 = PyTuple_GetItem(args, 0);
            // for lazy_eval_tensor
            if (strstr(arg0->ob_type->tp_name, "VarNode")) {
                if (PyObject_HasAttrString(arg0, "_node")) {
                    arg0 = PyObject_GetAttrString(arg0, "_node");
                }
                m_tensor = std::make_shared<Tensor>(py::handle(arg0).cast<cg::VarNode *>());
            } else {
                // for DeviceTensorND
                if (strstr(arg0->ob_type->tp_name, "DeviceTensorND")) {
                    auto dv = py::handle(arg0).cast<DeviceTensorND>();
                    interpreter::Interpreter::Handle handle = interpreter_for_py->put(dv);
                    m_tensor = std::make_shared<Tensor>(handle);
                } else {
                    throw py::type_error("single argument is not tensor, varnode or devicetensor");
                }
            }
234
        } else {
235
            py::detail::loader_life_support life_sup; // FIXME!!!required to cast DType
236 237
            if (nargs != 5 && nargs != 6) {
                throw py::type_error("expect 5 or 6 arguments");
238
            }
239 240 241 242
            auto data = tup[0].cast<py::array>();
            DType dtype = tup[1].cast<DType>();
            CompNode cn = tup[2].cast<CompNode>();
            bool is_const = tup[3].cast<bool>();
243
            bool no_cache = nargs == 6 ? tup[4].cast<bool>() : false;
244 245
            std::string name;
            if (tup[nargs - 1].ptr() != Py_None) name = tup[nargs - 1].cast<std::string>();
246 247

            // const op
248
            if (is_const && (ApplyContext::global_enable == Tensor::Flags::TRACE)) {
249
                auto py_ret = PyObject_Call(cpp_apply_const_with_tracing, tup.ptr(), nullptr);
250 251 252
                if (!py_ret) throw py::error_already_set();
                auto py_list = py::reinterpret_steal<py::list>(py_ret);
                if (auto* t = try_cast(py_list[0].ptr())) {
253 254 255 256 257 258
                    m_tensor = t->m_tensor;
                }
                return;
            }

            interpreter::Interpreter::Handle handle;
259
            {
260
                HostTensorND ret(cn);
261
                handle = interpreter_for_py->put(npy::np2tensor(data.ptr(), npy::Meth::copy_into(&ret), dtype), no_cache);
262 263 264
            }

            m_tensor = std::make_shared<Tensor>(handle);
265
            m_tensor->user_custom_name = name;
266

267 268 269
            if (data.ndim() == 0) {
                m_tensor->m_flags |= Tensor::Flags::SCALAR;
            }
270 271 272 273 274
        }
    }
}


275 276 277 278 279 280 281 282 283 284 285
#define REGISTE_TENSORWRAPPER_FUNC(type, member)                                    \
        PyObject* TensorWrapper::member() {                                         \
            return py::cast(m_tensor->m_trace_info.member).release().ptr();         \
        }                                                                           \
        void TensorWrapper::set_##member(PyObject* dest) {                          \
            auto py_dest = py::reinterpret_borrow<py::object>(dest);                \
            type real_dest = py_dest.cast<type>();                                  \
            m_tensor->m_trace_info.member = real_dest;                              \
        }

REGISTE_TENSORWRAPPER_FUNC(int64_t, mixin_handle)
286
REGISTE_TENSORWRAPPER_FUNC(bool, recording)
287 288 289 290

#undef REGISTE_TENSORWRAPPER_FUNC


291 292
#define REGISTE_TENSORWRAPPER_PYOBJECT_FUNC(member)                                 \
        PyObject* TensorWrapper::member() {                                         \
293 294 295 296 297
            if (m_tensor->m_trace_info.member) {                                    \
                return m_tensor->m_trace_info.member;                               \
            } else {                                                                \
                Py_RETURN_NONE;                                                     \
            }                                                                       \
298 299
        }                                                                           \
        void TensorWrapper::set_##member(PyObject* dest) {                          \
300 301 302 303 304 305 306
            if (dest == Py_None) {                                                  \
                Py_XDECREF(m_tensor->m_trace_info.member);                          \
                m_tensor->m_trace_info.member = nullptr;                            \
            } else {                                                                \
                Py_INCREF(dest);                                                    \
                m_tensor->m_trace_info.member = dest;                               \
            }                                                                       \
307 308 309 310 311 312 313 314
        }

REGISTE_TENSORWRAPPER_PYOBJECT_FUNC(compiled_info)
REGISTE_TENSORWRAPPER_PYOBJECT_FUNC(trace_mixin_info)

#undef REGISTE_TENSORWRAPPER_PYOBJECT_FUNC


315 316 317 318 319 320 321 322 323 324 325 326 327
#define SET_GET_NAME(member)                                     \
    PyObject* TensorWrapper::member() {                          \
        return py::cast(m_tensor->member).release().ptr();       \
    }                                                            \
    void TensorWrapper::set_##member(PyObject* dest) {           \
        auto py_dest = py::reinterpret_borrow<py::object>(dest); \
        m_tensor->member = py_dest.cast<std::string>();          \
    }
SET_GET_NAME(user_custom_name)
SET_GET_NAME(automatic_name)
#undef SET_GET_NAME


328 329 330 331 332 333 334 335 336 337 338 339
PyObject* TensorWrapper::handle() {
    return py::cast(m_tensor->m_handle).release().ptr();
}


void TensorWrapper::set_handle(PyObject* dest) {
    auto py_dest = py::reinterpret_borrow<py::object>(dest);
    SharedHandle real_dest = py_dest.cast<SharedHandle>();
    m_tensor->m_handle = std::move(real_dest);
}


340
PyObject* TensorWrapper::shape() {
341
    // if it's tracing compiled mode, get value from compiled_info 
342 343 344 345
    if (m_tensor->m_trace_info.compiled_info != nullptr) {
        if (m_tensor->m_flags & Tensor::Flags::SCALAR) {
            return PyTuple_New(0);
        }
346 347 348 349 350
        PyObject *shp = PyObject_GetAttrString(m_tensor->m_trace_info.compiled_info, "shape");
        if (shp == Py_None) {
            throw TraceReadError("shape of this tensor is not read in trace");
        }
        return shp;
351
    }
352 353

    // inside trace, if tensor shape is useful for other operations, set shape_read = true
354 355
    if (m_tensor->m_trace_info.recording && !skip_tracing) {
        PyObject_SetAttrString(m_tensor->m_trace_info.trace_mixin_info, "shape_read", py::cast(true).release().ptr());
356
    }
357

358 359 360
    if (m_tensor->m_flags & Tensor::Flags::SCALAR) {
        return PyTuple_New(0);
    }
361 362

    TensorShape shape;
363
    if (m_tensor->m_var) {      // get shape from m_var
364 365 366 367 368 369
        auto&& mgr = m_tensor->m_var->owner_graph()->static_infer_manager();
        auto *tshp = mgr.infer_shape_fallible(m_tensor->m_var);
        if (!tshp) {
            Py_RETURN_NONE;
        }
        shape = *tshp;
370 371 372 373
    } else {
        shape = m_tensor->shape();
    }

374 375 376 377 378 379 380 381 382 383 384 385
    if (!shape.ndim) {
        Py_RETURN_NONE;
    }
    py::tuple ret(shape.ndim);
    for (size_t i = 0; i < shape.ndim; ++i) {
        ret[i] = shape[i];
    }
    return ret.release().ptr();
}


PyObject* TensorWrapper::dtype() {
386 387 388
    if (m_tensor->m_var) {
        return py::cast(m_tensor->m_var->dtype()).release().ptr();
    }
389 390 391 392 393
    return py::cast(m_tensor->dtype()).release().ptr();
}


PyObject* TensorWrapper::device() {
394 395 396
    if (m_tensor->m_var) {
        return py::cast(m_tensor->m_var->comp_node()).release().ptr();
    }
397 398 399 400 401
    return py::cast(m_tensor->comp_node()).release().ptr();
}


PyObject* TensorWrapper::numpy() {
402 403
    if (m_tensor->m_trace_info.compiled_info != nullptr) {
        PyObject* np_val = PyObject_CallMethod(m_tensor->m_trace_info.compiled_info, "numpy", nullptr);
404
        if (!np_val) throw py::error_already_set();
405 406 407
        if (np_val == Py_None) {
            throw TraceReadError("value of this tensor is not read in trace");
        }
408
        if (m_tensor->m_flags & Tensor::Flags::SCALAR) {
409 410 411
            PyObject *np_scalar = PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(np_val));
            Py_DECREF(np_val);
            return np_scalar;
412 413 414
        }
        return np_val;
    }
415

416 417
    if (m_tensor->m_trace_info.recording && !skip_tracing) {
        PyObject_SetAttrString(m_tensor->m_trace_info.trace_mixin_info, "value_read", py::cast(true).release().ptr());
418
    }
419

420 421 422 423 424
    if (m_tensor->m_handle.get() == nullptr && m_tensor->m_var != nullptr) {
        auto&& mgr = m_tensor->m_var->owner_graph()->static_infer_manager();
        auto&& type = mgr.get_infer_type(m_tensor->m_var);
        using InferType = cg::static_infer::InferType;
        if (!(type.value & (InferType::CONST | InferType::RT_STATIC))) {
425
            PyErr_SetString(PyExc_ValueError, "tensor invalid");
426 427 428 429
            return nullptr;
        }
        auto* val = mgr.infer_value_fallible(m_tensor->m_var);
        if (!val) {
430
            PyErr_SetString(PyExc_ValueError, "tensor invalid");
431 432
            return nullptr;
        }
433 434 435 436 437
        auto np_val = py::cast(*val).attr("numpy")();
        if (m_tensor->m_flags & Tensor::Flags::SCALAR) {
            return PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(np_val.release().ptr()));
        }
        return np_val.release().ptr();
438
    }
439 440 441 442
    auto&& hv = [&]() {
        py::gil_scoped_release _;
        return interpreter_for_py->get_value(m_tensor->m_handle.get());
    }();
443
    auto arr = py::reinterpret_steal<py::array>(npy::ndarray_from_tensor(hv, npy::ShareType::TRY_SHARE));
444 445 446 447
    if (!arr) {
        PyErr_SetString(PyExc_ValueError, "tensor invalid");
        return nullptr;
    }
448

449 450 451 452 453 454 455
    if (m_tensor->m_flags & Tensor::Flags::SCALAR) {
        mgb_assert(PyArray_Check(arr.ptr()));
        return PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(arr.ptr()));
    }
    return arr.release().ptr();
}

456 457 458 459
PyObject* TensorWrapper::varnode() {
    if (m_tensor->m_var) {
        return py::cast(m_tensor->m_var).release().ptr();
    }
460
    Py_RETURN_NONE;
461 462
}

463
void TensorWrapper::reset(PyObject* tensor) {
464
    TensorWrapper* t = TensorWrapper::try_cast(tensor);
465 466 467
    if (!t) {
        throw py::type_error("expect Tensor");
    }
468 469
    std::string user_custom_name = m_tensor->user_custom_name;
    std::string automatic_name = m_tensor->automatic_name;
470
    m_tensor = t->m_tensor;
471 472
    m_tensor->user_custom_name = user_custom_name;
    m_tensor->automatic_name = automatic_name;
473 474
}

475 476 477 478
void TensorWrapper::reset_varnode() {
    m_tensor->m_var = nullptr;
}

479 480 481
PyObject* TensorWrapper::detach() {
    PyObject* self = wrap_t::pycast(this);
    PyTypeObject* pytype = self->ob_type;
482 483 484 485 486 487 488

    std::shared_ptr<Tensor> new_tensor;
    if (m_tensor->m_handle.get()) {
        new_tensor = std::make_shared<Tensor>(m_tensor->m_handle);
    } else {
        new_tensor = std::make_shared<Tensor>(m_tensor->m_var);
    }
489
    new_tensor->m_trace_info = m_tensor->m_trace_info;
490 491

    new_tensor->m_flags = m_tensor->m_flags;
492 493 494 495
    auto ret = TensorWrapper::make(pytype, std::move(new_tensor));
    return ret.release().ptr();
}

496
PyObject* TensorWrapper::_dev_tensor(){
497 498
    if (m_tensor->m_trace_info.compiled_info != nullptr) {
        auto *dev_tensor = PyObject_CallMethod(m_tensor->m_trace_info.compiled_info, "_dev_tensor", nullptr);
499
        if (!dev_tensor) throw py::error_already_set();
500 501 502
        if (dev_tensor == Py_None) {
            throw TraceReadError("raw data of this tensor is not read in trace");
        }
503 504

        // set m_handle to make it a real tensor
505 506 507
        auto py_dev_tensor = py::reinterpret_borrow<py::object>(dev_tensor);
        auto sh = interpreter_for_py->put(py_dev_tensor.cast<DeviceTensorND>());
        m_tensor->m_handle = std::move(SharedHandle(sh));
508 509

        // compiled info is useless after m_handle is set
510 511
        Py_DECREF(m_tensor->m_trace_info.compiled_info);
        m_tensor->m_trace_info.compiled_info = nullptr;
512 513

        return dev_tensor;
514 515 516
    }
    if (m_tensor->m_trace_info.recording && !skip_tracing) {
        PyObject_SetAttrString(m_tensor->m_trace_info.trace_mixin_info, "data_read", py::cast(true).release().ptr());
517
    }
518 519 520 521
    auto dev_tensor = [&](){
        py::gil_scoped_release _;
        return interpreter_for_py->get_dev_tensor(m_tensor->m_handle.get());
    }();
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    return py::cast(dev_tensor).release().ptr();
}

void TensorWrapper::_swap_out() {
    interpreter_for_py->swap_out(m_tensor->m_handle.get());
}

void TensorWrapper::_swap_in() {
    interpreter_for_py->swap_in(m_tensor->m_handle.get());
}

void TensorWrapper::_drop() {
    interpreter_for_py->drop(m_tensor->m_handle.get());
}


538 539 540 541 542 543 544 545
PyObject* TensorWrapper::isscalar() {
    if(m_tensor->m_flags & Tensor::Flags::SCALAR) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}

546

547 548 549 550 551
void TensorWrapper::setscalar() {
    m_tensor->m_flags |= Tensor::Flags::SCALAR;
}


552 553 554 555 556
void TensorWrapper::unsetscalar() {
    m_tensor->m_flags &= ~Tensor::Flags::SCALAR;
}


557 558 559 560 561 562 563 564 565 566 567
struct TensorWeakRef {
    std::weak_ptr<Tensor> wptr;

    TensorWeakRef(const TensorWrapper& tw) : wptr(tw.m_tensor) {}

    py::object operator()() {
        if (auto p = wptr.lock()) {
            return TensorWrapper::make(p);
        }
        return py::none();
    }
568
    int _use_cnt() { return wptr.use_count(); }
569 570
};

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/* ============== convert inputs ============== */

// map numpy.dtype.kind to priority
inline uint8_t category_priority(char c) {
    switch (c) {
        case 'f': return 3; // floating-point
        case 'i': return 2; // signed integer
        case 'u': return 2; // unsigned integer
        case 'b': return 1; // boolean
        default: return 0;
    }
}

// Returns the maximum value of the priority of each type in the list `types`.
uint8_t max_priority(SmallVector<PyArray_Descr*> types) {
    if (types.size() == 0) {
        return 0;
    } else {
        uint8_t max_p = 0;
        for (auto&& desc: types) {
            max_p = std::max(max_p, category_priority(desc->kind));
        }
        return max_p;
    }
}

597
// Returns the data type with sufficient size to hold all types of
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
// category `cat` in the list `types`.
PyArray_Descr* promote_types(SmallVector<PyArray_Descr*> types, uint8_t cat) {
    // Return value: New reference
    SmallVector<PyArray_Descr*> used_types;
    for (auto&& desc: types) {
        auto&& v = category_priority(desc->kind);
        if (v == cat) {
            used_types.emplace_back(desc);
        }
    }
    mgb_assert(used_types.size() > 0, "size of used_types is 0");
    PyArray_Descr* res = used_types[0];
    Py_INCREF(res);

    for (size_t i = 1; i < used_types.size(); ++i) {
        PyArray_Descr* tmp = PyArray_PromoteTypes(used_types[i], res);
        Py_DECREF(res);
        res = tmp;
    }
    return res;
}

PyArray_Descr* scalar2dtype(PyObject* arg) {
    // Return value: New reference
    if (PyBool_Check(arg)) {
        auto&& descr = PyArray_DescrFromType(NPY_BOOL);
        return descr;
    }
    if (PyLong_CheckExact(arg)) {
        auto&& descr = PyArray_DescrFromType(NPY_INT32);
        return descr;
    }
    if (PyFloat_CheckExact(arg)) {
        auto&& descr = PyArray_DescrFromType(NPY_FLOAT32);
        return descr;
    }
    return nullptr;
}

PyArray_Descr* _dtype_promotion(PyObject*const* args, size_t nargs) {
    // Return value: New reference
    SmallVector<PyArray_Descr*> tensors;
    SmallVector<PyArray_Descr*> scalars;

    bool is_tuple = false;
643
    PyObject* tuple = nullptr;
644 645 646 647 648 649 650 651 652 653 654 655 656 657
    if (nargs == 1 && (PyTuple_Check(args[0]) || PyList_Check(args[0]))) {
        if (PyList_Check(args[0])) {
            tuple = PyList_AsTuple(args[0]);
        } else {
            tuple = args[0];
            Py_INCREF(tuple);
        }
        nargs = PyTuple_Size(tuple);
        is_tuple = true;
    }

    for (size_t i = 0; i < nargs; ++i) {
        PyObject* handle = is_tuple ? PyTuple_GetItem(tuple, i): args[i];
        if (handle == Py_None) continue;
658
        TensorWrapper* tw = TensorWrapper::try_cast(handle);
659 660 661 662 663 664 665 666 667 668 669
        if (tw) {
            mgb::DType type = tw->m_tensor->dtype();
            auto&& descr = npy::dtype_mgb2np_descr(type);
            Py_INCREF(descr.get());
            tensors.emplace_back(descr.get());
        }else{
            if (PyArray_Check(handle) || PyArray_CheckScalar(handle)) {
                auto&& descr = PyArray_DescrFromObject(handle, nullptr);
                tensors.emplace_back(descr);
                continue;
            }
670

671 672 673
            if (py::isinstance<PySymbolVar>(py::handle(handle))){
                auto var = py::handle(handle).cast<PySymbolVar*>();
                mgb::DType type = var->m_node->dtype();
674 675 676 677 678 679
                auto && descr = npy::dtype_mgb2np_descr(type);
                Py_INCREF(descr.get());
                tensors.emplace_back(descr.get());
                continue;
            }

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
            PyArray_Descr* descr = scalar2dtype(handle);
            if (descr) {
                scalars.emplace_back(descr);
                continue;
            }
        }
    }

    auto max_pri_scalars = max_priority(scalars);
    auto max_pri_tensors = max_priority(tensors);

    if (max_pri_scalars <= 0 && max_pri_tensors <= 0) {
        throw py::value_error("invalid input, no dtype avaliable");
    }
    PyArray_Descr* res;
    if (max_pri_scalars > max_pri_tensors) {
        res = promote_types(scalars, max_pri_scalars);
    }else{
        res = promote_types(tensors, max_pri_tensors);
    }
    for (auto *p: tensors) { Py_DECREF(p); }
    for (auto *p: scalars) { Py_DECREF(p); }
702
    Py_XDECREF(tuple);
703 704 705 706 707
    return res;
}

CompNode _get_device(PyObject*const* args, size_t nargs) {
    bool is_tuple = false;
708
    PyObject* tuple = nullptr;
709 710 711 712 713 714 715 716 717 718 719 720 721
    if (nargs == 1 && (PyTuple_Check(args[0]) || PyList_Check(args[0]))) {
        if (PyList_Check(args[0])) {
            tuple = PyList_AsTuple(args[0]);
        } else {
            tuple = args[0];
            Py_INCREF(tuple);
        }
        nargs = PyTuple_Size(tuple);
        is_tuple = true;
    }
    bool valid = false;
    CompNode cn;
    for (size_t i = 0; i < nargs; ++i) {
722
        PyObject* handle = is_tuple ? PyTuple_GetItem(tuple, i) : args[i];
723
        TensorWrapper* tw = TensorWrapper::try_cast(handle);
724

725 726
        bool is_symvar = py::isinstance<PySymbolVar>(py::handle(handle));
        if (tw || is_symvar) {
727
            if (!valid) {
728 729 730 731
                cn = tw ? tw->m_tensor->comp_node()
                        : py::handle(handle)
                                     .cast<PySymbolVar*>()
                                     ->m_node->comp_node();
732 733
                valid = true;
            } else {
734 735 736 737
                CompNode cn1 = tw ? tw->m_tensor->comp_node()
                                  : py::handle(handle)
                                               .cast<PySymbolVar*>()
                                               ->m_node->comp_node();
738 739
                if (cn1 != cn) {
                    throw py::value_error(ssprintf("ambiguous device: %s vs %s",
740 741
                                                   cn.to_string().c_str(),
                                                   cn1.to_string().c_str()));
742 743 744 745 746
                }
            }
        }
    }
    if (!valid) {
747
        return CompNode::load(get_default_device());
748
    }
749
    Py_XDECREF(tuple);
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    return cn;
}

// Returns the dtype that would result from performing an arithmetic
// operation on the provided input tensors and scalars.
PyObject* dtype_promotion(PyObject* self, PyObject*const* args, size_t nargs) {
    if (!nargs) {
        PyErr_SetString(PyExc_TypeError, "empty input is not allowed");
        return nullptr;
    }
    try {
        PyArray_Descr* res = _dtype_promotion(args, nargs);
        return py::cast(npy::dtype_np2mgb_descr(res)).release().ptr();
    } catch (std::exception& e) {
        PyErr_SetString(PyExc_RuntimeError, e.what());
        return nullptr;
    }
}

PyObject* get_device(PyObject* self, PyObject*const* args, size_t nargs) {
    if (!nargs) {
        PyErr_SetString(PyExc_TypeError, "empty input is not allowed");
        return nullptr;
    }
    try {
        CompNode cn = _get_device(args, nargs);
        return py::cast(cn).release().ptr();
    } catch (std::exception& e) {
        PyErr_SetString(PyExc_RuntimeError, e.what());
        return nullptr;
    }
}
782

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
#ifdef METH_FASTCALL
#define MGE_PY_INTERFACE(NAME, FUNC) \
    { #NAME, (PyCFunction)FUNC, METH_FASTCALL, nullptr }
#else
#define WRAP_FUNC_PY35(FUNC)                                \
    PyObject* py35_##FUNC(PyObject* self, PyObject* args) { \
        auto* arr = &PyTuple_GET_ITEM(args, 0);             \
        auto size = PyTuple_GET_SIZE(args);                 \
        return FUNC(self, arr, size);                       \
    }
WRAP_FUNC_PY35(py_apply);
WRAP_FUNC_PY35(dtype_promotion);
WRAP_FUNC_PY35(get_device);
#undef WRAP_FUNC_PY35
#define MGE_PY_INTERFACE(NAME, FUNC) \
    { #NAME, (PyCFunction)py35_##FUNC, METH_VARARGS, nullptr }
#endif

801

802
void init_tensor(py::module m) {
803 804 805
    imperative::Tensor::static_initialize();
    static auto sl_interpreter_for_py = interpreter::Interpreter::inst().create_channel();
    interpreter_for_py = sl_interpreter_for_py.get();
806 807 808 809 810 811 812

    auto* tensor_type = TensorWrapper::wrap_t::type()
        .def<&TensorWrapper::numpy>("numpy")
        .def_getset<&TensorWrapper::shape>("shape")
        .def_getset<&TensorWrapper::dtype>("dtype")
        .def_getset<&TensorWrapper::device>("device")
        .def<&TensorWrapper::reset>("_reset")
813 814 815
        .def<&TensorWrapper::isscalar>("_isscalar")
        .def<&TensorWrapper::setscalar>("_setscalar")
        .def<&TensorWrapper::unsetscalar>("_unsetscalar")
816
        .def<&TensorWrapper::detach>("detach")
817 818 819 820
        .def<&TensorWrapper::_dev_tensor>("_dev_tensor")
        .def<&TensorWrapper::_swap_out>("_swap_out")
        .def<&TensorWrapper::_swap_in>("_swap_in")
        .def<&TensorWrapper::_drop>("_drop")
821
        .def<&TensorWrapper::reset_varnode>("_reset_varnode")
822
        .def<&TensorWrapper::_use_cnt>("_use_cnt")
823
        .def_getset<&TensorWrapper::varnode>("_varnode")
824 825
        .def_getset<&TensorWrapper::mixin_handle, &TensorWrapper::set_mixin_handle>("_mixin_handle")
        .def_getset<&TensorWrapper::recording, &TensorWrapper::set_recording>("_recording")
826
        .def_getset<&TensorWrapper::handle, &TensorWrapper::set_handle>("_handle")
827 828
        .def_getset<&TensorWrapper::compiled_info, &TensorWrapper::set_compiled_info>("_compiled_info")
        .def_getset<&TensorWrapper::trace_mixin_info, &TensorWrapper::set_trace_mixin_info>("_trace_mixin_info")
829 830
        .def_getset<&TensorWrapper::user_custom_name, &TensorWrapper::set_user_custom_name>("c_name")
        .def_getset<&TensorWrapper::automatic_name, &TensorWrapper::set_automatic_name>("_name")
831 832 833 834 835 836
        .finalize();
    if (!tensor_type) throw py::error_already_set();
    py::setattr(m, "Tensor", tensor_type);

    py::class_<TensorWeakRef>(m, "TensorWeakRef")
        .def(py::init<const TensorWrapper&>())
837 838
        .def("__call__", &TensorWeakRef::operator())
        .def("_use_cnt", &TensorWeakRef::_use_cnt);
839

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    py::class_<PySymbolVar, std::shared_ptr<PySymbolVar>>(m, "SymbolVar")
            .def_property_readonly(
                    "dtype", [](PySymbolVar* v) { return v->m_node->dtype(); })
            .def_property("var", [](PySymbolVar* v) { return v->m_node; },
                          [](PySymbolVar* s, cg::VarNode* v) { s->m_node = v; })
            .def_property_readonly(
                    "device",
                    [](PySymbolVar* v) { return v->m_node->comp_node(); })
            .def_property_readonly(
                    "graph",
                    [](PySymbolVar* v) { return v->m_node->owner_graph(); })
            .def_property_readonly(
                    "shape",
                    [](PySymbolVar* v) -> const TensorShape* {
                        auto&& mgr = v->m_node->owner_graph()
                                             ->static_infer_manager();
                        return mgr.infer_shape_fallible(v->m_node);
                    })
            .def("_isscalar", [](PySymbolVar* v) { return v->is_scalar; })
            .def("_setscalar",
                 [](PySymbolVar* v) { return v->is_scalar = true; })
            .def(py::init([](cg::VarNode* node) {
                     return std::make_shared<PySymbolVar>(node);
                 }),
                 py::arg() = nullptr);

866
    static PyMethodDef method_defs[] = {
867 868 869 870
            MGE_PY_INTERFACE(apply, py_apply),
            MGE_PY_INTERFACE(dtype_promotion, dtype_promotion),
            MGE_PY_INTERFACE(get_device, get_device),
            {nullptr, nullptr, 0, nullptr}};
871 872 873 874 875 876 877
    for (auto&& def: method_defs) {
        if (def.ml_meth != nullptr) {
            auto* func = PyCFunction_NewEx(&def, nullptr, nullptr);
            if (!func) throw py::error_already_set();
            py::setattr(m, def.ml_name, func);
        }
    }
878

879 880 881 882 883
    static constexpr auto sync_py_task_q = []{
        py::gil_scoped_release _;
        py_task_q.wait_all_task_finish();
    };

884
    m.def("set_option",
885
          [](std::string name, size_t value){ interpreter_for_py->set_option(name, value); });
886 887
    m.def("get_option",
          [](std::string name){ return interpreter_for_py->get_option(name); });
888
    m.def("_set_swap_flag",
889
          [](bool flag) { interpreter_for_py->set_option("enable_swap", flag); });
890
    m.def("_set_drop_flag",
891
          [](bool flag) { interpreter_for_py->set_option("enable_drop", flag); });
892
    m.def("config_async_level",
893 894 895 896
          [](int level) {
              mgb_assert(level >= 0 and level <= 2, "async_level should be 0, 1 or 2");
              interpreter_for_py->set_option("async_level", level);
          });
897
    m.def("get_async_level",
898
          []() { return interpreter_for_py->get_option("async_level"); });
899
    m.def("set_buffer_length",
900 901 902 903 904 905 906 907 908 909 910 911
          [](int length) {
              mgb_assert(length >= 0 and length < 100, "buffer_length should be in [0, 100)");
              interpreter_for_py->set_option("buffer_length", length);
          });
    m.def("push_scope",
          [](std::string name) { interpreter_for_py->push_scope(name); });
    m.def("pop_scope",
          [](std::string name) { interpreter_for_py->pop_scope(name); });
    m.def("start_profile",
          [](std::unordered_map<std::string, int> option) { return interpreter_for_py->start_profile(option); });
    m.def("stop_profile",
          [](std::string basename, std::string format) { interpreter_for_py->stop_profile(basename, format); });
912 913 914
    m.def("sync",
          []() {
              interpreter_for_py->sync();
915 916
              sync_py_task_q();
          });
917 918 919 920
    m.def("full_sync",
          []() {
              interpreter_for_py->sync();
              CompNode::sync_all();
921 922 923 924 925 926 927
              sync_py_task_q();
          });
    m.def("close",
          []() {
              interpreter_for_py->close();
              sync_py_task_q();
          });
928

929 930
    py::handle grad_key_type = GradKeyWrapper::wrap_t::type()
        .def<&GradKeyWrapper::attach>("attach")
931 932
        .def<&GradKeyWrapper::is_attached_to>("is_attached_to")
        .def_getset<&GradKeyWrapper::get_name, &GradKeyWrapper::set_name>("name")
933 934 935
        .finalize();
    if (!grad_key_type) throw py::error_already_set();
    py::setattr(m, "GradKey", grad_key_type);
936 937
    m.def("backward", &GradKeyWrapper::backward);

938 939 940 941 942 943 944
    m.def("set_cpp_apply_with_tracing", &set_cpp_apply_with_tracing);
    m.def("set_cpp_apply_const_with_tracing", &set_cpp_apply_const_with_tracing);
    m.def("set_cpp_apply_backward_varnode", &set_cpp_apply_backward_varnode);

    m.attr("skip_tracing") = &skip_tracing;

    py::class_<SharedHandle>(m, "SharedHandle")
945 946 947 948 949 950 951 952
        .def(py::init<const SharedHandle&>())
        .def("__eq__", [](SharedHandle &thish, SharedHandle &thath) {
            return (thish.get() == thath.get());
        })
        .def("__hash__", [](SharedHandle &sh) {
            return reinterpret_cast<int64_t>(sh.get());
        })
        ;
953 954 955

    m.def("set_tracing", &set_tracing);
    m.def("unset_tracing", &unset_tracing);
956 957
}

958 959
#undef MGE_PY_INTERFACE

960
} // namespace mgb::imperative::python