Browse Source

fix(imperative): restrict using convert_inputs in py_apply

GitOrigin-RevId: b021aac8a6
tags/v1.9.0
Megvii Engine Team “wenjuan” 3 years ago
parent
commit
94960ecf42
2 changed files with 12 additions and 2 deletions
  1. +7
    -2
      imperative/python/src/tensor.cpp
  2. +5
    -0
      imperative/python/test/unit/functional/test_elemwise.py

+ 7
- 2
imperative/python/src/tensor.cpp View File

@@ -169,7 +169,7 @@ PyObject* py_apply(
}
HostTensorND ht(target_cn);
ht = npy::np2tensor(args[i], npy::Meth::copy_into(&ht), target_dtype);
if (PyArray_Check(args[i])) { // non scaler
if (PyArray_Check(args[i]) || PyList_Check(args[i])) { // non scaler
return imperative::apply(
CreateTensor(CreateTensor::Const, target_cn, ht.layout()),
HostStorage::make(ht.storage()))[0];
@@ -205,8 +205,13 @@ PyObject* py_apply(
for (size_t i = 0; i < nargs; ++i) {
if (TensorWrapper* tw = TensorWrapper::try_cast(args[i])) {
tensors[i] = tw->m_tensor->data();
} else {
} else if (
DTypePromoteCfg::convert_input_enabled &&
op->same_type<Elemwise>()) {
tensors[i] = convert_pyinput_to_tensor(i);
} else {
PyErr_SetString(PyExc_TypeError, "py_apply expects tensor as inputs");
return nullptr;
}
}



+ 5
- 0
imperative/python/test/unit/functional/test_elemwise.py View File

@@ -77,6 +77,11 @@ def test_div():
np.floor_divide(np.array([-5, -7], dtype=np.int32), 2),
)

np.testing.assert_allclose(
(tensor([[5, 4, 3], [4, 2, 6]]) // [1, 2, 1]).numpy(),
np.floor_divide(np.array([[5, 4, 3], [4, 2, 6]], dtype=np.int32), [1, 2, 1]),
)


def test_clamp():
"""Fix an issue when `lower` or `upper` is 0, it will be recognized as `False` and


Loading…
Cancel
Save