Write every tensor in the set to a file.
- Parameters
-
| filepath | Destination path. |
| tensors | Tensors with unique, non-empty names. |
- Returns
- True only if the complete file was written.
Implements MayaFlux::IO::TensorWriter.
Definition at line 139 of file NpyFormat.cpp.
140{
142 if (tensors.tensors.size() != 1) {
143 m_last_error =
"a .npy file holds exactly one tensor, received " + std::to_string(tensors.tensors.size());
144 return false;
145 }
146
148 if (!bytes) {
149 return false;
150 }
151
152 std::ofstream file(filepath, std::ios::binary | std::ios::trunc);
153 if (!file) {
155 return false;
156 }
157 file.write(reinterpret_cast<const char*>(bytes->data()), static_cast<std::streamsize>(bytes->size()));
158 file.close();
159 if (!file) {
160 m_last_error =
"failed while writing '" + filepath +
"'";
161 return false;
162 }
163 return true;
164}
std::optional< std::vector< std::byte > > encode_npy(const Kakshya::TensorData &tensor, std::string &why)
Encode a tensor as a complete .npy array in dense C order.
References MayaFlux::IO::Detail::encode_npy(), m_last_error, and MayaFlux::Kakshya::TensorSet::tensors.