Skip to content

Commit a2b595f

Browse files
committed
处理空的tensor
1 parent df982d4 commit a2b595f

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

python/infinicore/_tensor_str.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _str(self):
114114
if self.device.type != "cpu":
115115
py_device_str = ", device='" + self.device.__str__() + "'"
116116
py_tensor_str += py_device_str
117-
py_tensor_str += py_dtype_str + ")"
117+
py_tensor_str += py_dtype_str + ")\n"
118118

119119
return py_tensor_str
120120

src/infinicore/io.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,11 @@ std::ostream &pretty_print(const Tensor &original_tensor,
589589

590590
namespace infinicore {
591591
std::ostream &operator<<(std::ostream &out, const Tensor &tensor) {
592-
592+
if (!tensor) {
593+
out << "tensor([])\n";
594+
return out;
595+
}
596+
593597
switch (tensor->dtype()) {
594598
case DataType::BYTE: // 1
595599
{

src/infinicore/pybind11/tensor.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ inline void bind(py::module &m) {
4545
std::ostringstream oss;
4646
oss << tensor;
4747
return oss.str();
48+
})
49+
.def("__bool__", [](const Tensor &tensor) {
50+
return bool(tensor);
4851
});
4952

5053
m.def("empty", &Tensor::empty,

0 commit comments

Comments
 (0)