Skip to content

Commit f72193d

Browse files
author
zhuyue
committed
Add debug function in InfiniCore tensor.
1 parent 79dbccd commit f72193d

5 files changed

Lines changed: 792 additions & 1 deletion

File tree

include/infinicore/tensor.hpp

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

122122
std::string info() const;
123123

124+
void debug(const std::string &filename) const;
125+
126+
void debug() const;
127+
124128
///
125129
/// Data Transfer APIs
126130
///

python/infinicore/tensor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def permute(self, dims):
7575
def view(self, shape):
7676
return Tensor(self._underlying.view(shape))
7777

78+
def debug(self, filename=None):
79+
"""Print tensor data or save to file for debugging
80+
81+
Args:
82+
filename: Optional filename to save raw binary data. If None, prints to stdout.
83+
"""
84+
if filename is None:
85+
self._underlying.debug()
86+
else:
87+
self._underlying.debug(filename)
88+
7889

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

src/infinicore/pybind11/tensor.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ inline void bind(py::module &m) {
1717
.def_property_readonly("dtype", [](const Tensor &tensor) { return tensor->dtype(); })
1818
.def_property_readonly("device", [](const Tensor &tensor) { return tensor->device(); })
1919

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

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

2931
.def("copy_", [](Tensor &tensor, const Tensor &other) { tensor->copy_from(other); })
3032
.def("to", [](const Tensor &tensor, const Device &device) { return tensor->to(device); })

0 commit comments

Comments
 (0)