!47914 [JIT Fallback] Not convert numpy array to tensor implicitly.

Merge pull request !47914 from 张清华/opt_jit_fallback
This commit is contained in:
i-robot 2023-01-16 10:46:04 +00:00 committed by Gitee
commit eccfc85cca
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 7 additions and 87 deletions

View File

@ -87,36 +87,6 @@ void PyExecuteCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
}
}
void ArrayToRawMemory(const py::array &array, const AddressPtr &address) {
MS_EXCEPTION_IF_NULL(address);
if (static_cast<unsigned int>(array.flags()) &
static_cast<unsigned int>(pybind11::detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_)) {
const py::buffer_info &buf_info = array.request();
const auto &res =
memcpy_s(address->addr, address->size, buf_info.ptr, LongToSize(buf_info.size * buf_info.itemsize));
if (res != EOK) {
MS_LOG(EXCEPTION) << "memcpy failed. res: " << res << ", address->size: " << address->size
<< ", size: " << LongToSize(buf_info.size * buf_info.itemsize);
}
} else {
// Transform numpy array to contiguous data.
Py_buffer pybuf;
if (PyObject_GetBuffer(array.ptr(), &pybuf, PyBUF_ANY_CONTIGUOUS) != 0) {
MS_LOG(EXCEPTION) << "Failed to get buffer from the input!";
}
auto buffer = std::make_unique<char[]>(LongToSize(pybuf.len));
if (PyBuffer_ToContiguous(buffer.get(), &pybuf, pybuf.len, 'C')) {
PyBuffer_Release(&pybuf);
MS_LOG(EXCEPTION) << "Can't copy numpy.ndarray to a contiguous buffer.";
}
PyBuffer_Release(&pybuf);
const auto &res = memcpy_s(address->addr, address->size, buffer.get(), LongToSize(pybuf.len));
if (res != EOK) {
MS_LOG(EXCEPTION) << "memcpy failed. res: " << res;
}
}
}
void PyExecuteCpuKernelMod::AttachPyOutputData(const py::object &py_res) {
const auto &py_output = std::make_shared<PyExecuteOutputData>();
py_output->obj = py_res;
@ -346,34 +316,10 @@ bool PyExecuteCpuKernelMod::Launch(const std::vector<AddressPtr> &inputs, const
auto params = py::tuple(2);
params[0] = global_dict;
params[1] = local_dict;
MS_LOG(DEBUG) << "py_script: " << py_script << ", params: " << params;
const auto &py_res = CallPythonScript(py_script, params);
// Check Python result.
if (py::isinstance<py::none>(py_res)) {
MS_LOG(EXCEPTION) << "Real output is None.";
} else if (py::isinstance<py::array>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::array, py_res: " << py_res;
ArrayToRawMemory(py_res.cast<py::array>(), outputs[0]);
} else if (py::isinstance<py::float_>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::float_, py_res: " << py_res;
} else if (py::isinstance<py::int_>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::int_, py_res: " << py_res;
} else if (py::isinstance<py::bool_>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::bool_, py_res: " << py_res;
} else if (py::isinstance<py::str>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::str, py_res: " << py_res;
} else if (py::isinstance<py::tuple>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::tuple, py_res: " << py_res;
} else if (py::isinstance<py::list>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::list, py_res: " << py_res;
} else if (py::isinstance<py::dict>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::dict, py_res: " << py_res;
} else if (py::isinstance<py::set>(py_res)) {
MS_LOG(DEBUG) << "Real output is py::set, py_res: " << py_res;
} else {
MS_LOG(DEBUG) << "Real output is function or other type, py_res: " << py_res;
}
AttachPyOutputData(py_res);
MS_LOG(DEBUG) << "Python script: " << py_script << ", params: " << params;
const auto &output = CallPythonScript(py_script, params);
MS_LOG(DEBUG) << "Python output type: " << py::str(output.get_type()) << ", output: " << output;
AttachPyOutputData(output);
return true;
}

View File

@ -80,34 +80,9 @@ class PyExecuteInitializer {
auto params = py::tuple(2);
params[0] = global_dict;
params[1] = local_dict;
MS_LOG(DEBUG) << "py_script: " << py_script << ", params: " << params;
const auto &py_res = parse::data_converter::CallPythonScript(py_script, params);
MS_LOG(DEBUG) << "py_res: " << py_res;
if (py::isinstance<py::none>(py_res)) {
MS_LOG(EXCEPTION) << "py_res is none";
} else if (py::isinstance<py::array>(py_res)) {
MS_LOG(DEBUG) << "is py::array, py_res: " << py_res;
const auto &res_tensor = tensor::TensorPy::MakeTensorOfNumpy(py_res);
MS_LOG(DEBUG) << "res_tensor: " << res_tensor->ToString();
} else if (py::isinstance<py::float_>(py_res)) {
MS_LOG(DEBUG) << "is py::float_, py_res: " << py_res;
} else if (py::isinstance<py::int_>(py_res)) {
MS_LOG(DEBUG) << "is py::int_, py_res: " << py_res;
} else if (py::isinstance<py::bool_>(py_res)) {
MS_LOG(DEBUG) << "is py::bool_, py_res: " << py_res;
} else if (py::isinstance<py::str>(py_res)) {
MS_LOG(DEBUG) << "is py::str, py_res: " << py_res;
} else if (py::isinstance<py::tuple>(py_res)) {
MS_LOG(DEBUG) << "is py::tuple, py_res: " << py_res;
} else if (py::isinstance<py::list>(py_res)) {
MS_LOG(DEBUG) << "is py::list, py_res: " << py_res;
} else if (py::isinstance<py::dict>(py_res)) {
MS_LOG(DEBUG) << "is py::dict, py_res: " << py_res;
} else if (py::isinstance<py::set>(py_res)) {
MS_LOG(DEBUG) << "is py::set, py_res: " << py_res;
} else {
MS_LOG(EXCEPTION) << "py_res is invalid, py_res: " << py_res;
}
MS_LOG(DEBUG) << "Python script: " << py_script << ", params: " << params;
const auto &output = parse::data_converter::CallPythonScript(py_script, params);
MS_LOG(DEBUG) << "Python output type: " << py::str(output.get_type()) << ", output: " << output;
}
};

View File

@ -102,7 +102,6 @@ def tensor_asnumpy():
return res
@pytest.mark.skip(reason="Not supported by now")
@pytest.mark.level1
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training