Skip to content

Commit f705006

Browse files
author
zhuyue
committed
Add debug function in InfiniCore tensor.
1 parent 53836c5 commit f705006

6 files changed

Lines changed: 795 additions & 3 deletions

File tree

include/infinicore/tensor.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ class TensorImpl : public std::enable_shared_from_this<TensorImpl> {
109109

110110
std::string info() const;
111111

112+
void debug(const std::string &filename) const;
113+
114+
void debug() const;
115+
112116
///
113117
/// Data Transfer APIs
114118
///

python/infinicore/tensor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ def permute(self, dims):
6969
def view(self, shape):
7070
return Tensor(self._underlying.view(shape))
7171

72+
def debug(self, filename=None):
73+
"""Print tensor data or save to file for debugging
74+
75+
Args:
76+
filename: Optional filename to save raw binary data. If None, prints to stdout.
77+
"""
78+
if filename is None:
79+
self._underlying.debug()
80+
else:
81+
self._underlying.debug(filename)
82+
7283

7384
def empty(size, *, dtype=None, device=None, pin_memory=False):
7485
return Tensor(

src/infinicore/pybind11/op/matmul.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ inline void bind_matmul(py::module &m) {
2323
R"doc(In-place matrix multiplication.)doc");
2424
}
2525

26-
} // namespace infinicore
26+
} // namespace infinicore::op

src/infinicore/pybind11/tensor.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ inline void bind(py::module &m) {
1616
.def_property_readonly("ndim", [](const Tensor &tensor) { return tensor->ndim(); })
1717
.def_property_readonly("dtype", [](const Tensor &tensor) { return tensor->dtype(); })
1818

19-
.def("data_ptr", [](const Tensor &tensor) { return tensor->data(); })
19+
.def("data_ptr", [](const Tensor &tensor) { return reinterpret_cast<uintptr_t>(tensor->data()); })
2020
.def("size", [](const Tensor &tensor, std::size_t dim) { return tensor->size(dim); })
2121
.def("stride", [](const Tensor &tensor, std::size_t dim) { return tensor->stride(dim); })
2222
.def("numel", [](const Tensor &tensor) { return tensor->numel(); })
2323

2424
.def("is_contiguous", [](const Tensor &tensor) { return tensor->is_contiguous(); })
2525
.def("is_pinned", [](const Tensor &tensor) { return tensor->is_pinned(); })
2626
.def("info", [](const Tensor &tensor) { return tensor->info(); })
27+
.def("debug", [](const Tensor &tensor) { return tensor->debug(); })
28+
.def("debug", [](const Tensor &tensor, const std::string &filename) { return tensor->debug(filename); })
2729

2830
.def("copy_", [](Tensor &tensor, const Tensor &other) { tensor->copy_from(other); })
2931
.def("to", [](const Tensor &tensor, const Device &device) { return tensor->to(device); })
@@ -49,7 +51,8 @@ inline void bind(py::module &m) {
4951
py::arg("device"),
5052
py::arg("pin_memory") = false);
5153

52-
m.def("from_blob", [](uintptr_t raw_ptr, Shape &shape, const DataType &dtype, const Device &device) { return Tensor{infinicore::Tensor::from_blob(reinterpret_cast<void *>(raw_ptr), shape, dtype, device)}; }, pybind11::arg("raw_ptr"), pybind11::arg("shape"), pybind11::arg("dtype"), pybind11::arg("device"));
54+
m.def(
55+
"from_blob", [](uintptr_t raw_ptr, Shape &shape, const DataType &dtype, const Device &device) { return Tensor{infinicore::Tensor::from_blob(reinterpret_cast<void *>(raw_ptr), shape, dtype, device)}; }, pybind11::arg("raw_ptr"), pybind11::arg("shape"), pybind11::arg("dtype"), pybind11::arg("device"));
5356
}
5457

5558
} // namespace infinicore::tensor

0 commit comments

Comments
 (0)