diff --git a/imperative/python/megengine/jit/tracing.py b/imperative/python/megengine/jit/tracing.py index a9c66c99..c63e3c01 100644 --- a/imperative/python/megengine/jit/tracing.py +++ b/imperative/python/megengine/jit/tracing.py @@ -1002,8 +1002,7 @@ class CompiledTensorProxy: else: # c++ will throw TraceReadError return None - if self._isscalar: - self.__value = self.__value.squeeze() + # c++ side will handle scalar case return self.__value def _dev_tensor(self): diff --git a/imperative/python/src/tensor.cpp b/imperative/python/src/tensor.cpp index 947cb271..bce96e15 100644 --- a/imperative/python/src/tensor.cpp +++ b/imperative/python/src/tensor.cpp @@ -393,7 +393,9 @@ PyObject* TensorWrapper::numpy() { throw TraceReadError("value of this tensor is not read in trace"); } if (m_tensor->m_flags & Tensor::Flags::SCALAR) { - np_val = PyArray_Squeeze(reinterpret_cast(np_val)); + PyObject *np_scalar = PyArray_Squeeze(reinterpret_cast(np_val)); + Py_DECREF(np_val); + return np_scalar; } return np_val; }