Skip to content

Commit af108fc

Browse files
author
zhuyue
committed
Add debug.
1 parent 53836c5 commit af108fc

5 files changed

Lines changed: 777 additions & 1 deletion

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/tensor.hpp

Lines changed: 3 additions & 1 deletion
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); })

0 commit comments

Comments
 (0)