diff --git a/imperative/python/megengine/core/tensor/megbrain_graph.py b/imperative/python/megengine/core/tensor/megbrain_graph.py index 8150c35b2bab9e247b7d49a71bfea426aa2c54d6..fbd29a96a54b39b8234f1acf3fc08a335961bc70 100644 --- a/imperative/python/megengine/core/tensor/megbrain_graph.py +++ b/imperative/python/megengine/core/tensor/megbrain_graph.py @@ -16,7 +16,6 @@ from typing import Dict, List, Union import numpy as np -from ...utils.comp_graph_tools import set_priority_to_id as _set_priority_to_id from .. import _imperative_rt from .._imperative_rt import GraphOptimizeOptions from .._imperative_rt.core2 import apply, set_cpp_apply_backward_varnode @@ -26,6 +25,19 @@ from ..ops.builtin import OpDef from .core import OpBase, TensorBase +def set_priority_to_id(dest_vars): + """ + For all oprs in the subgraph constructed by dest_vars, + sets its priority to id if its original priority is zero. + :param dest_vars: target vars representing the graph. + """ + dest_vec = [] + for i in dest_vars: + assert isinstance(i, _imperative_rt.VarNode) + dest_vec.append(i) + _imperative_rt.graph._set_priority_to_id(dest_vec) + + class Graph(_imperative_rt.ComputingGraph): def __init__(self): super().__init__() @@ -46,8 +58,8 @@ class Graph(_imperative_rt.ComputingGraph): cache[obj] = wrapper(obj) return cache[obj] - def set_priority_to_id(self, dest_vars): - _set_priority_to_id(_unwrap(dest_vars)) + def _set_priority_to_id(self, dest_vars): + set_priority_to_id(_unwrap(dest_vars)) def compile(self, *args): self._function = super().compile(_unwrap(args)) diff --git a/imperative/python/megengine/distributed/launcher.py b/imperative/python/megengine/distributed/launcher.py index f90683bdbbee817270e16519ceeb86c899569104..fad845decc60a46d9afe6f329735a74c9171d289 100644 --- a/imperative/python/megengine/distributed/launcher.py +++ b/imperative/python/megengine/distributed/launcher.py @@ -9,7 +9,7 @@ import functools import multiprocessing as mp -from ..core._imperative_rt import sync +from ..core._imperative_rt.core2 import sync from .group import group_barrier, init_process_group from .helper import get_device_count_by_fork from .server import Server diff --git a/imperative/python/megengine/jit/tracing.py b/imperative/python/megengine/jit/tracing.py index ad02403b4652aff82a963de5301c96ba2c8f61ff..7f714f545aa5f5967a4cc5c60f9c45599a5000cc 100644 --- a/imperative/python/megengine/jit/tracing.py +++ b/imperative/python/megengine/jit/tracing.py @@ -367,7 +367,7 @@ class trace: lazy_eval_graph.options.graph_opt_level = self._graph_opt_level else: lazy_eval_graph.options.graph_opt_level = 2 - lazy_eval_graph.set_priority_to_id([*lazy_eval_links, *readers]) + lazy_eval_graph._set_priority_to_id([*lazy_eval_links, *readers]) lazy_eval_graph.compile(*lazy_eval_links, *readers) lazy_eval_graph() for r, x in zip(readers, lazy_eval_tensors): @@ -618,7 +618,7 @@ class trace: graph.options.graph_opt_level = self._graph_opt_level else: graph.options.graph_opt_level = 2 - graph.set_priority_to_id([*readers, *in_out_links, *io_links]) + graph._set_priority_to_id([*readers, *in_out_links, *io_links]) graph.compile(*readers, *in_out_links, *io_links) def _reset_exec_env(self): diff --git a/imperative/python/megengine/utils/comp_graph_tools.py b/imperative/python/megengine/utils/comp_graph_tools.py index 7cff34003cddac6f21087e018d92225d88f1ee53..3b16a836e34a5e534f77ed380bac3c653ff78bd7 100644 --- a/imperative/python/megengine/utils/comp_graph_tools.py +++ b/imperative/python/megengine/utils/comp_graph_tools.py @@ -13,6 +13,7 @@ import numpy from ..core import _imperative_rt from ..core._imperative_rt import OperatorNode, VarNode from ..core.tensor import megbrain_graph as G +from ..core.tensor.megbrain_graph import set_priority_to_id from ..tensor import Tensor __all__ = [ @@ -271,19 +272,6 @@ def replace_oprs( return _imperative_rt.graph._replace_oprs(repl_src_vec, repl_dst_vec, dst_vec) -def set_priority_to_id(dest_vars): - """ - For all oprs in the subgraph constructed by dest_vars, - sets its priority to id if its original priority is zero. - :param dest_vars: target vars representing the graph. - """ - dest_vec = [] - for i in dest_vars: - assert isinstance(i, VarNode) - dest_vec.append(i) - _imperative_rt.graph._set_priority_to_id(dest_vec) - - def load_and_inference(file, inp_data_list: List[numpy.ndarray]) -> List[numpy.ndarray]: """ Loads a serialized computing graph and run inference with input data. diff --git a/imperative/python/src/grad.cpp b/imperative/python/src/grad.cpp index 46e39ebf2d12166bf00361f6262094dbb91041ee..8019137a618dcd613d79b352d325a473a7ee304d 100644 --- a/imperative/python/src/grad.cpp +++ b/imperative/python/src/grad.cpp @@ -32,7 +32,7 @@ struct GradSlotWeakPtr { size_t idx; }; -struct BackwardGraphCache : std::unordered_map>, CompNodeDepedentObject { +struct BackwardGraphCache : std::unordered_map>, CompNodeDepedentObject { std::shared_ptr on_comp_node_finalize() override { clear(); return {}; @@ -56,7 +56,7 @@ std::shared_ptr make_backward_graph( } mgb_assert(bool_ptr0 == reinterpret_cast(size_t_ptr) && bool_ptr == reinterpret_cast(buf + buf_size)); - size_t key = XXHash{}.update(buf, buf_size).digest(); + uint64_t key = XXHash{}.update(buf, buf_size).digest(); auto&& iter = backward_graph_cache.find(key); if (iter != backward_graph_cache.end()) { diff --git a/imperative/python/src/numpy_dtypes.cpp b/imperative/python/src/numpy_dtypes.cpp index 1c195a87d8292f28851b6b57dc54761a64c83d8b..f652cf1ee07c35c16b7eab53311d0fb05bcda0ba 100644 --- a/imperative/python/src/numpy_dtypes.cpp +++ b/imperative/python/src/numpy_dtypes.cpp @@ -32,7 +32,7 @@ inline bool _is_quantize(PyArray_Descr* dtype) { PyObject* _get_mgb_dtype(PyArray_Descr* dtype) { // Return value: New reference. if (!_is_quantize(dtype)) { - throw py::type_error("expact quantize dtype"); + throw py::type_error("expect quantize dtype"); } PyObject* ob = PyDict_GetItemString(dtype->metadata, "mgb_dtype"); if (!PyDict_CheckExact(ob)) { diff --git a/imperative/python/src/tensor.cpp b/imperative/python/src/tensor.cpp index 42583937cd0b2718baf4fe4ea1d1965614f0a72e..396ed644a507c15ea419453c1d90b7d55f152b35 100644 --- a/imperative/python/src/tensor.cpp +++ b/imperative/python/src/tensor.cpp @@ -143,8 +143,10 @@ PyObject* py_apply(PyObject* self, PyObject*const* args, size_t nargs/* , PyObje // PyErr_SetString(PyExc_TypeError, "keyword argument not allowed"); // return nullptr; // } - if (!nargs) { - PyErr_SetString(PyExc_TypeError, "expect Op"); + if (nargs < 2) { + PyErr_SetString(PyExc_TypeError, + "py_apply expects one Op and at least one tensor " + "as argument"); return nullptr; } @@ -227,7 +229,7 @@ TensorWrapper::TensorWrapper(PyObject* args, PyObject* kwargs) { } } } else { - py::detail::loader_life_support life_sup; // required to cast DType + py::detail::loader_life_support life_sup; // FIXME!!!required to cast DType auto data = tup[0].cast(); DType dtype = tup[1].cast(); CompNode cn = tup[2].cast(); @@ -298,7 +300,6 @@ PyObject* TensorWrapper::handle() { void TensorWrapper::set_handle(PyObject* dest) { auto py_dest = py::reinterpret_borrow(dest); SharedHandle real_dest = py_dest.cast(); - auto&& t = std::move(m_tensor->m_handle); m_tensor->m_handle = std::move(real_dest); } @@ -617,7 +618,7 @@ CompNode _get_device(PyObject*const* args, size_t nargs) { } } if (!valid) { - mgb_assert(0, "expact at least 1 device"); + mgb_assert(0, "expect at least 1 device"); } Py_DECREF(tuple); return cn; diff --git a/imperative/python/test/unit/core/test_autodiff.py b/imperative/python/test/unit/core/test_autodiff.py index 05d1526b47c1e5e2d06282722b0230021fdfd309..9c1efdf739f5198ce0608bca19aa3733abe8140b 100644 --- a/imperative/python/test/unit/core/test_autodiff.py +++ b/imperative/python/test/unit/core/test_autodiff.py @@ -88,6 +88,7 @@ def test_dist_grad(): worker() + def test_grad(): x_np = np.random.rand(10).astype("float32") x = as_tensor(x_np) diff --git a/imperative/python/test/unit/core/test_jit.py b/imperative/python/test/unit/core/test_jit.py deleted file mode 100644 index 088f56d6cc0364acaffc1563e985f67f78cc694d..0000000000000000000000000000000000000000 --- a/imperative/python/test/unit/core/test_jit.py +++ /dev/null @@ -1,27 +0,0 @@ -# -*- coding: utf-8 -*- -# MegEngine is Licensed under the Apache License, Version 2.0 (the "License") -# -# Copyright (c) 2014-2020 Megvii Inc. All rights reserved. -# -# 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. -import pytest - -# from megengine.core.interpreter.hints import function - - -@pytest.mark.skip(reason="under rewrite") -def test_1(): - @function - def f(x, p): - x = x + 1 - if p: - return x * x - return x * 2 - - x = Tensor(0) - - for _ in range(5): - assert f(x, 0).numpy() == 2 - assert f(x, 1).numpy() == 1 diff --git a/imperative/python/test/unit/quantization/test_fake_quant.py b/imperative/python/test/unit/quantization/test_fake_quant.py index bf84c93df7ad8a368fb093e18043f288034af8f5..be218e231a68aad31ceba677822dee1eb4797fd4 100644 --- a/imperative/python/test/unit/quantization/test_fake_quant.py +++ b/imperative/python/test/unit/quantization/test_fake_quant.py @@ -83,7 +83,7 @@ def test_TQT(): def _save_to(self, name="grad"): - def callback(tensor, grad): + def callback(grad): setattr(self, name, grad) return callback