ops.cpp 20.7 KB
Newer Older
M
Megvii Engine Team 已提交
1 2 3 4
/**
 * \file imperative/python/src/ops.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
M
Megvii Engine Team 已提交
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
#include "./ops.h"
13 14
#include "./helper.h"
#include "./tensor.h"
15

16
#include "megbrain/common.h"
17
#include "megbrain/imperative.h"
18
#include "megbrain/imperative/graph_builder.h"
19 20
#include "megbrain/imperative/ops/backward_graph.h"
#include "megbrain/imperative/ops/opr_attr.h"
21
#include "megbrain/imperative/ops/utility.h"
22
#include "megbrain/imperative/ops/autogen.h"
23
#include "megbrain/imperative/ops/rng.h"
24

25 26 27
#include <Python.h>
#include <unordered_map>

28
namespace py = pybind11;
29
using namespace mgb::imperative;
30

31 32 33 34 35 36 37 38 39 40
namespace {
auto normalize_enum(const std::string& in) {
    std::string ret;
    for (auto&& c : in) {
        ret += toupper(c);
    }
    return ret;
}
} // anonymous namespace

41 42 43 44 45 46 47 48 49 50 51 52
#define CATCH_ALL(RETVAL) \
    catch(py::error_already_set& e) { \
        e.restore(); \
        return RETVAL; \
    } catch(py::builtin_exception& e) { \
        e.set_error(); \
        return RETVAL; \
    } catch(std::exception& e) { \
        PyErr_SetString(PyExc_RuntimeError, e.what()); \
        return RETVAL; \
    } \

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
namespace {
#define PyOp(name) Py##name
#define PyOpType(name) PyOp(name)::py_type

#define PyOpDefBegin(name) \
struct PyOp(name) : PyOpDef { \
    using Ty = name; \
    Ty& inst() { return op->cast_final_safe<Ty>(); } \
    static PyTypeObject py_type;

#define PyOpDefEnd(name) \
}; \
PyTypeObject PyOpType(name);

#define RETURN_RICHCOMPARE(val1, val2, op)                               \
    do {                                                                    \
        switch (op) {                                                       \
        case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
        case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
        case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
        case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
        case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
        case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
        default:                                                            \
            Py_FatalError("Unreachable C code path reached");               \
        }                                                                   \
    } while (0)

81
template <typename T>
82 83 84 85 86 87 88 89 90
PyObject* py_new_generic(PyTypeObject* type, PyObject*, PyObject*) {
    PyObject* obj = type->tp_alloc(type, 0);
    T* self = reinterpret_cast<T*>(obj);
    if (self != NULL) {
        self->op = T::Ty::make();
    }
    return obj;
}

91 92 93 94 95 96 97 98 99 100 101 102 103
template<typename T, typename SNIFAE=void>
struct serialization {
    static T load(py::object obj) {
        return py::cast<T>(obj);
    }
    template<typename U,
        typename = std::enable_if_t<std::is_same_v<T, std::decay_t<U>>>>
    static py::object dump(U&& t) {
        return py::cast(std::forward<U>(t));
    }
};


104 105 106 107 108 109 110 111 112
template<typename T>
void py_dealloc_generic(PyObject* obj) {
    reinterpret_cast<T*>(obj)->op.reset();
    Py_TYPE(obj)->tp_free(obj);
}

template<typename T, typename U, U T::Ty::*attr>
PyObject* py_get_generic_impl(PyObject* obj, void* /* closure */) {
    auto& op = reinterpret_cast<T*>(obj)->inst();
113
    return py::cast(op.*attr).release().ptr();
114 115 116 117 118 119 120 121 122 123 124 125
}
#define py_get_generic(name, attr) \
    py_get_generic_impl<PyOp(name), decltype(std::declval<name>().attr), &name::attr>

template<typename T, typename U, U T::Ty::*attr>
int py_set_generic_impl(PyObject* obj, PyObject* value, void* /* closure */) {
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
        return -1;
    }
    auto& op = reinterpret_cast<T*>(obj)->inst();
    try {
126 127 128
        // TODO: remove this guard which is used for pybind11 implicit conversion
        py::detail::loader_life_support guard{};
        op.*attr = py::cast<U>(py::handle(value));
129 130
    } CATCH_ALL(-1)
    return 0;
131 132 133 134 135 136 137 138 139
}
#define py_set_generic(name, attr) \
    py_set_generic_impl<PyOp(name), decltype(std::declval<name>().attr), &name::attr>

struct PyOpDef {
    PyObject_HEAD
    std::shared_ptr<OpDef> op;
    static PyTypeObject py_type;
    static std::unordered_map<mgb::Typeinfo*, PyTypeObject*> ctype2pytype;
140
    static PyGetSetDef py_getsetters[];
141 142
    static Py_hash_t tp_hash(PyObject *obj);
    static PyObject* tp_richcompare(PyObject *self, PyObject *other, int op);
143 144 145 146 147 148 149
    static PyObject* py_repr(PyObject* self) {
        return py::cast(
                       reinterpret_cast<PyOpDef*>(self)->op->make_name())
                .release()
                .ptr();
    }

150 151 152 153
};
PyTypeObject PyOpType(OpDef);
std::unordered_map<mgb::Typeinfo*, PyTypeObject*> PyOp(OpDef)::ctype2pytype;

154
PyObject* py_get_scope(PyObject* obj, void* /* closure */) {
155 156
    return py::cast(
            reinterpret_cast<PyOp(OpDef)*>(obj)->op->scope()).release().ptr();
157 158 159 160 161 162 163 164 165
}

int py_set_scope(PyObject* obj, PyObject* value, void* /* closure */) {
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
        return -1;
    }
    try {
        reinterpret_cast<PyOp(OpDef)*>(obj)->op
166
            ->set_scope(py::cast<std::string>(py::handle(value)));
167 168 169 170 171 172 173 174 175
    } CATCH_ALL(-1)
    return 0;
}

PyGetSetDef PyOp(OpDef)::py_getsetters[] = {
    {const_cast<char*>("scope"), py_get_scope, py_set_scope, "scope", NULL},
    {NULL}
};

176 177 178 179 180 181 182 183 184 185 186 187 188 189
Py_hash_t PyOp(OpDef)::tp_hash(PyObject *obj) {
    return static_cast<Py_hash_t>(
        reinterpret_cast<PyOp(OpDef)*>(obj)->op->hash());
}

PyObject* PyOp(OpDef)::tp_richcompare(PyObject *self, PyObject *other, int op) {
    bool same = reinterpret_cast<PyOp(OpDef)*>(self)->op->is_same(
        *reinterpret_cast<PyOp(OpDef)*>(other)->op);
    if (op == Py_EQ || op == Py_NE) {
        RETURN_RICHCOMPARE(same, true, op);
    }
    Py_RETURN_NOTIMPLEMENTED;
}

190 191 192 193 194 195 196 197
template<typename T>
struct EnumTrait;

#define PyEnumHead \
    static_assert(std::is_enum_v<T>); \
    PyObject_HEAD \
    T value; \
    constexpr static const char *name = EnumTrait<T>::name; \
198
    static PyTypeObject* type; \
199 200 201 202
    static const char* members[]; \
    static std::unordered_map<std::string, T> mem2value; \
    static PyObject* pyobj_insts[];

203 204
template<typename T>
struct EnumWrapper {
205
    PyEnumHead
206
    std::string to_string() const {
207
        return members[static_cast<size_t>(value)];
208 209
    }
    static PyObject* py_repr(PyObject* self) {
210 211 212
        return py::cast(
            std::string(name) + "." + reinterpret_cast<EnumWrapper*>(self)->to_string())
                .release().ptr();
213
    }
214 215 216 217 218 219 220

    static PyObject* py_dump(PyObject* self) {
        return py::cast(reinterpret_cast<EnumWrapper*>(self)->to_string())
                .release()
                .ptr();
    }

221 222
    static PyObject* tp_richcompare(PyObject *self, PyObject *other, int op) {
        if (op == Py_EQ || op == Py_NE) {
223 224 225 226 227 228
            T lhs, rhs;
            if (load(other, rhs) && load(self, lhs)) {
                RETURN_RICHCOMPARE(lhs, rhs, op);
            } else {
                RETURN_RICHCOMPARE(0, 1, op);
            }
229 230 231
        }
        Py_RETURN_NOTIMPLEMENTED;
    }
232 233
    static bool load(py::handle src, T& value) {
        PyObject* obj = src.ptr();
234
        if (PyObject_TypeCheck(obj, type)) {
235 236 237 238 239 240 241 242 243 244 245 246
            value = reinterpret_cast<EnumWrapper*>(obj)->value;
            return true;
        }
        if (py::isinstance<py::str>(src)) {
            auto&& iter = mem2value.find(
                normalize_enum(py::cast<std::string>(src)));
            if (iter != mem2value.end()) {
                value = iter->second;
                return true;
            } else {
                return false;
            }
247
        }
248
        return false;
249
    }
250 251 252 253 254
    static PyObject* cast(const T& value) {
        auto v = static_cast<std::underlying_type_t<T>>(value);
        mgb_assert(v <= EnumTrait<T>::max);
        PyObject* obj = pyobj_insts[v];
        Py_INCREF(obj);
255 256 257 258 259 260
        return obj;
    }
};

template<typename T>
struct BitCombinedEnumWrapper {
261
    PyEnumHead
262
    std::string to_string() const {
263 264
        uint32_t value_int = static_cast<uint32_t>(value);
        if (value_int == 0) {
265 266
            return "None";
        } else {
267
            std::string ret;
268 269
            bool first = true;
            for (uint32_t i = 0; i < 32; i++) {
270
                if (value_int >> i & 1) {
271 272 273 274 275
                    if (!first) {
                        ret += " + ";
                    } else {
                        first = false;
                    }
276
                    ret += (std::string(name) + "." + members[i]);
277 278 279 280 281
                }
            }
            return ret;
        }
    }
282 283 284 285 286 287 288 289 290 291 292 293
    static PyObject* py_new_combined_enum(PyTypeObject* type, PyObject* args, PyObject*) {
        if (!PyTuple_Size(args)) {
            PyObject* obj = type->tp_alloc(type, 0);
            reinterpret_cast<BitCombinedEnumWrapper*>(obj)->value = T();
            return obj;
        }
        else {
            PyObject* input;
            if (!PyArg_ParseTuple(args, "|O", &input)) {
                return nullptr;
            }
            T value;
294 295 296 297 298 299 300 301
            if (load(input, value)) {
                return cast(value);
            } else {
                PyErr_SetString(PyExc_RuntimeError,
                    mgb::ssprintf("Cannot convert type %s to type %s\n",
                        input->ob_type->tp_name, name).c_str());
                return nullptr;
            }
302 303 304
        }
    }
    static PyObject* py_repr(PyObject* self) {
305 306 307
        return py::cast(
                reinterpret_cast<BitCombinedEnumWrapper*>(self)->to_string())
                        .release().ptr();
308
    }
309 310 311 312 313 314 315 316 317 318 319 320 321

    static PyObject* py_dump(PyObject* self) {
        std::vector<std::string> result;
        auto value = reinterpret_cast<BitCombinedEnumWrapper*>(self)->value;
        uint32_t value_int = static_cast<uint32_t>(value);
        for (uint32_t i = 0; i < 32; i++) {
            if (value_int >> i & 1) {
                result.push_back(members[i]);
            }
        }
        return py::tuple(py::cast(result)).release().ptr();
    }

322 323 324 325 326 327 328 329
    static PyObject* py_or(PyObject* self, PyObject* other) {
        if(!(self->ob_type == other->ob_type)){
            return PyErr_Format(
                    PyExc_RuntimeError,
                    "Operand in or operator must be the same type.");
        }
        T lhs = reinterpret_cast<BitCombinedEnumWrapper*>(self)->value,
          rhs = reinterpret_cast<BitCombinedEnumWrapper*>(other)->value;
330
        return cast(lhs | rhs);
331 332 333 334 335 336 337 338 339
    }
    static PyObject* py_and(PyObject* self, PyObject* other) {
        if (!(self->ob_type == other->ob_type)) {
            return PyErr_Format(
                    PyExc_RuntimeError,
                    "Operand in and operator must be the same type.");
        }
        T lhs = reinterpret_cast<BitCombinedEnumWrapper*>(self)->value,
          rhs = reinterpret_cast<BitCombinedEnumWrapper*>(other)->value;
340
        return cast(lhs & rhs);
341 342 343
    }
    static PyObject* tp_richcompare(PyObject* self, PyObject* other, int op) {
        if (op == Py_EQ || op == Py_NE) {
344 345 346 347 348 349
            T lhs, rhs;
            if (load(other, rhs) && load(self, lhs)) {
                RETURN_RICHCOMPARE(lhs, rhs, op);
            } else {
                RETURN_RICHCOMPARE(0, 1, op);
            }
350 351 352
        }
        Py_RETURN_NOTIMPLEMENTED;
    }
353 354
    static bool load(py::handle src, T& value) {
        PyObject* obj = src.ptr();
355
        if (PyObject_TypeCheck(obj, type)) {
356 357 358 359 360 361 362 363 364 365 366 367 368
            value = reinterpret_cast<BitCombinedEnumWrapper*>(obj)->value;
            return true;
        }
        if (py::isinstance<py::str>(src)) {
            auto&& iter = mem2value.find(
                normalize_enum(py::cast<std::string>(src)));
            if (iter != mem2value.end()) {
                value = iter->second;
                return true;
            } else {
                return false;
            }
        }
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        if (py::isinstance<py::tuple>(src)) {
            auto params = py::cast<std::vector<std::string>>(src); 
            bool first = true;
            for (auto s : params){
                auto&& iter = mem2value.find(normalize_enum(s));
                if (iter != mem2value.end()) {
                    if (first) {
                        value = iter->second;
                        first = false;
                    } else {
                        value |= iter->second;
                    }
                } else {
                    return false;
                }
            }
            return true;
        }
387 388 389 390 391 392 393
        if (py::isinstance<py::int_>(obj)) {
            auto v = py::cast<std::underlying_type_t<T>>(src);
            if(v > EnumTrait<T>::max) {
                return false;
            }
            value = static_cast<T>(v);
            return true;
394
        }
395
        return false;
396
    }
397 398 399 400
    static PyObject* cast(const T& value) {
        auto v = static_cast<std::underlying_type_t<T>>(value);
        mgb_assert(v <= EnumTrait<T>::max);
        if ((!v) || (v & (v - 1))) {
401
            PyObject* obj = type->tp_alloc(type, 0);
402 403 404 405 406 407 408
            reinterpret_cast<BitCombinedEnumWrapper*>(obj)->value = value;
            return obj;
        } else {
            PyObject* obj = pyobj_insts[__builtin_ctz(v)];
            Py_INCREF(obj);
            return obj;
        }
409 410 411
    }
};

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
template<typename T>
struct serialization<T,
        std::enable_if_t<std::is_enum_v<std::decay_t<T>>>> {
    static T load(py::object obj) {
        auto caster = pybind11::detail::type_caster<T>();
        if (caster.load(obj, true)) {
            return caster;
        } else {
                PyErr_SetString(PyExc_RuntimeError,
                 "load faild \n");
                return caster;
        }
    }
    static py::object dump(T t) {
        return py::cast(t).attr("dump")();
    }
};


431
void _init_py_op_def(py::module m) {
432
    using py_op = PyOp(OpDef);
433 434 435 436 437 438 439 440 441
    auto& py_type = PyOpType(OpDef);
    py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
    py_type.tp_name = "megengine.core._imperative_rt.OpDef";
    py_type.tp_basicsize = sizeof(PyOp(OpDef));
    py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
    py_type.tp_doc = "OpDef";
    py_type.tp_base = &PyBaseObject_Type;
    py_type.tp_hash = PyOp(OpDef)::tp_hash;
    py_type.tp_richcompare = PyOp(OpDef)::tp_richcompare;
442
    py_type.tp_getset = py_op::py_getsetters;
443
    py_type.tp_repr = py_op::py_repr;
444 445 446 447 448
    mgb_assert(PyType_Ready(&py_type) >= 0);
    m.add_object("OpDef", reinterpret_cast<PyObject*>(&py_type));
}

/*********** begin of hand-write opdefs **************/
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
struct PyOpBase : PyOpDef {
    static PyTypeObject py_type;

    static PyObject* tp_new(PyTypeObject* type, PyObject*, PyObject*) {
        auto* obj = type->tp_alloc(type, 0);
        if (obj) {
            auto* self = reinterpret_cast<PyOpBase*>(obj);
            new(&self->op) decltype(self->op);
        }
        return obj;
    }
};
PyTypeObject PyOpBase::py_type;

void _init_py_op_base(py::module m) {
    using py_op = PyOpBase;
    auto& py_type = PyOpBase::py_type;
    py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
    py_type.tp_name = "megengine.core._imperative_rt.ops.PyOpBase";
    py_type.tp_basicsize = sizeof(py_op);
    py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
    py_type.tp_doc = "PyOpBase";
    py_type.tp_base = &PyOpType(OpDef);
    py_type.tp_dealloc = py_dealloc_generic<py_op>;
    py_type.tp_new = py_op::tp_new;
    mgb_assert(PyType_Ready(&py_type) >= 0);
    m.add_object("PyOpBase", reinterpret_cast<PyObject*>(&py_type));
}

478 479 480 481 482
/*********** end of hand-write opdefs **************/

// auto generated opdefs
#include "opdef.cpy.inl"

483
#undef CATCH_ALL
484 485 486 487 488 489 490 491 492 493
} // anonymous namespace

namespace PYBIND11_NAMESPACE {
namespace detail {
bool type_caster<OpDef>::load(handle src, bool convert) {
    PyObject* obj = src.ptr();
    if (!PyObject_TypeCheck(obj, &PyOpType(OpDef))) {
        return false;
    }
    value = reinterpret_cast<PyOp(OpDef)*>(obj)->op;
494 495 496 497
    if (!value) {
        // opdef only defined in Python
        value = std::make_shared<GenericPyOp>(reinterpret_borrow<object>(src));
    }
498 499 500
    return true;
}
handle type_caster<OpDef>::cast(const OpDef& op, return_value_policy, handle) {
501 502 503
    if (auto* pyop = op.try_cast_final<GenericPyOp>()) {
        return object(pyop->obj).release();
    }
504 505 506 507 508 509 510 511 512 513 514 515 516 517
    PyTypeObject* pytype;
    auto& c2p = PyOp(OpDef)::ctype2pytype;
    auto&& iter = c2p.find(op.dyn_typeinfo());
    if (iter != c2p.end()) { // FIXME: should always meet this condition
        pytype = iter->second;
    } else { // which means unregistered op type, jsut make it as an opaque op type
        // currently, only OprAttr goes into this branch
        pytype = &PyOpType(OpDef);
    }
    PyObject* obj = pytype->tp_alloc(pytype, 0);
    mgb_assert(PyObject_TypeCheck(obj, &PyOpType(OpDef)));
    reinterpret_cast<PyOp(OpDef)*>(obj)->op = const_cast<OpDef&>(op).shared_from_this();
    return py::handle(obj);
}
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

#define ENUM_CASTER_IMPL(T) \
bool type_caster<T>::load(handle src, bool) { \
    return EnumWrapper<T>::load(src, value); \
} \
handle type_caster<T>::cast(const T& value, return_value_policy, handle) { \
    return EnumWrapper<T>::cast(value); \
}
FOR_EACH_ENUM_PARAM(ENUM_CASTER_IMPL)

#define BIT_COMBINED_ENUM_CASTER_IMPL(T) \
bool type_caster<T>::load(handle src, bool) { \
    return BitCombinedEnumWrapper<T>::load(src, value); \
} \
handle type_caster<T>::cast(const T& value, return_value_policy, handle) { \
    return BitCombinedEnumWrapper<T>::cast(value); \
}
FOR_EACH_BIT_COMBINED_ENUM_PARAM(BIT_COMBINED_ENUM_CASTER_IMPL)

537 538 539
} // detail
} // PYBIND11_NAMESPACE

540
void init_ops(py::module m) {
541
    _init_py_op_def(m);
542
    _init_py_op_base(m);
543
    INIT_ALL_OP(m)
544

545 546 547 548
    m.def("new_rng_handle", &rng::new_handle);
    m.def("delete_rng_handle", [](size_t handle){
        // RNG op might execute after handle released due to async dispatch, so
        // we need sync before delete a handle to avoid memory leak or use-after-free
549 550 551
        if(python::interpreter_for_py->check_available()){
            python::interpreter_for_py->sync();
        }
552 553 554 555 556 557
        mgb::CompNode::sync_all();
        py_task_q.wait_all_task_finish();
        rng::delete_handle(handle);
    }, py::call_guard<py::gil_scoped_release>());
    m.def("set_global_rng_seed", &rng::set_global_rng_seed);
    m.def("get_global_rng_seed", &rng::get_global_rng_seed);
558
    m.def("get_rng_handle_compnode", &rng::get_rng_handle_compnode);
559 560 561 562

    struct PySubgraphBuilder {
        explicit PySubgraphBuilder(std::string name) : name{name}{}
        std::string name;
563 564 565
        std::shared_ptr<Subgraph> graph_storage = std::make_shared<Subgraph>();
        std::shared_ptr<UniqueKey> graph_key = std::make_shared<UniqueKey>();
        Subgraph& graph = *graph_storage;
566 567
        mgb::SmallVector<bool> output_grad_mask;
        Subgraph::var_t next_var = 1;
568 569 570 571

        std::shared_ptr<OpDef> build() const {
            return SubgraphOp::make(name, graph_storage, output_grad_mask, graph_key);
        }
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 597 598 599 600 601 602 603 604
    };

    py::class_<PySubgraphBuilder>(m, "SubgraphBuilder")
        .def(py::init<std::string>())
        .def("input", [](PySubgraphBuilder& self){
            auto var = self.next_var++;
            self.graph.inputs.push_back(var);
            return var;
        })
        .def("apply", [](PySubgraphBuilder& self, std::shared_ptr<OpDef> op, Subgraph::vars_t inputs, size_t nr_outputs){
            Subgraph::vars_t outputs;
            for (size_t i = 0; i < nr_outputs; ++i) {
                outputs.push_back(self.next_var++);
            }
            self.graph.exprs.push_back({op, inputs, outputs});
            return outputs;
        })
        .def("apply_const", [](PySubgraphBuilder& self, py::object value, mgb::DType dtype, mgb::CompNode cn){
            auto var = self.next_var++;
            mgb::HostTensorND hvalue(cn);
            npy::np2tensor(value.cast<py::array>().ptr(), npy::Meth::copy_into(&hvalue), dtype);
            self.graph.constants.push_back({var, Tensor::make(hvalue)});
            return var;
        })
        .def("outputs", [](PySubgraphBuilder& self, Subgraph::vars_t outputs){
            self.graph.outputs = outputs;
            self.output_grad_mask.resize(outputs.size(), true);
        })
        .def("outputs_has_grad", [](PySubgraphBuilder& self, mgb::SmallVector<bool> outputs_has_grad){
            mgb_assert(self.graph.outputs.size() == self.output_grad_mask.size());
            self.output_grad_mask = outputs_has_grad;
        })
        .def("get", [](PySubgraphBuilder& self){
605
            return (std::shared_ptr<OpDef>)self.build();
606 607
        })
        .def("compile", [](PySubgraphBuilder& self, int gopt_level){
608
            return (std::shared_ptr<OpDef>)CompiledOp::make(self.build(), gopt_level);
609
        });
610
}