Skip to content
Snippets Groups Projects
Commit 4c3b6ff3 authored by Christophe Guillon's avatar Christophe Guillon Committed by Olivier BICHLER
Browse files

fix : Tensor assertion with no implementation on toString()

Set toString() method to always return a valid string even
in the case where the Tensor has no implementaiton or when
the tensor is undefined (i.e. no specified size).
parent afcabd30
No related branches found
No related tags found
2 merge requests!279v0.4.0,!258fix : Tensor toString() and capacity() when implementation is undefined
Pipeline #59777 passed
......@@ -336,11 +336,7 @@ void init_Tensor(py::module& m){
.def("cpy_transpose", (void (Tensor::*)(const Tensor& src, const std::vector<DimSize_t>& transpose)) &Tensor::copyTranspose, py::arg("src"), py::arg("transpose"))
.def("__str__", [](Tensor& b) {
if (b.empty() && b.undefined()) {
return std::string("{}");
} else {
return b.toString();
}
return b.toString();
})
.def("__repr__", [](Tensor& b) {
return fmt::format("Tensor(dims = {}, dtype = {})", b.dims(), std::string(EnumStrings<DataType>::data[static_cast<int>(b.dataType())]));
......
......@@ -235,10 +235,11 @@ void Aidge::Tensor::resize(const std::vector<Aidge::DimSize_t>& dims,
}
std::string Aidge::Tensor::toString() const {
AIDGE_ASSERT(
mImpl && (undefined() || (dims() == std::vector<DimSize_t>({0})) ||
(mImpl->hostPtr() != nullptr)),
"tensor should have a valid host pointer");
if (!hasImpl() || undefined()) {
// Return no value on no implementation or undefined size
return std::string("{}");
}
// TODO: move lambda elsewhere?
auto ptrToString = [](DataType dt, void* ptr, std::size_t idx) {
......@@ -272,8 +273,10 @@ std::string Aidge::Tensor::toString() const {
};
if (dims().empty()) {
// The Tensor is defined with rank 0, hence scalar
return ptrToString(mDataType, mImpl->hostPtr(), 0);
}
std::string res;
std::size_t dim = 0;
std::size_t counter = 0;
......
......@@ -458,7 +458,7 @@ TEST_CASE("[core/data] Tensor(other)", "[Tensor][extract][zeros][print]") {
SECTION("Pretty printing for debug") {
Tensor x{};
// Empty Tensor
REQUIRE_THROWS(x.print());
REQUIRE_NOTHROW(x.print());
// scalar
x = Tensor(42);
REQUIRE_NOTHROW(x.print());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment