Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
serialization.hpp
Go to the documentation of this file.
1// Copyright 2025 Eric Hermosis
2//
3// This file is part of the Tannic Tensor Library.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17
18#ifndef SERIALIZATION_HPP
19#define SERIALIZATION_HPP
20
21#include <cstdint>
22#include "tensor.hpp"
23
24namespace tannic {
25
26constexpr uint32_t MAGIC = 0x43495245;
27
28#pragma pack(push, 1)
29struct Header {
30 uint32_t magic;
31 uint8_t version;
32 uint16_t checksum;
33 uint64_t nbytes;
34};
35#pragma pack(pop)
36
37template<class Object>
38struct Metadata;
39
40#pragma pack(push, 1)
41template<>
42struct Metadata<Tensor> {
43 uint8_t dcode;
44 size_t offset;
45 size_t nbytes;
46 uint8_t rank;
47};
48#pragma pack(pop)
49
50inline Header headerof(Tensor const& tensor) {
51 return Header{
52 .magic=MAGIC,
53 .version=1,
54 .checksum=0xABCD,
55 .nbytes=tensor.nbytes() + sizeof(Metadata<Tensor>) + tensor.rank() * sizeof(size_t)
56 };
57}
58
59inline Metadata<Tensor> metadataof(Tensor const& tensor) {
60 return Metadata<Tensor> {
61 .dcode = dcodeof(tensor.dtype()),
62 .offset = static_cast<size_t>(tensor.offset()),
63 .nbytes = tensor.nbytes(),
64 .rank = tensor.rank()
65 };
66}
67
68} // namespace TANNIC
69
70#endif // SERIALIZATION_HPP
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:99
rank_type rank() const
Returns the number of dimensions (rank) of the tensor.
Definition: tensor.hpp:179
std::size_t nbytes() const
Returns the total number of bytes occupied by the tensor's elements.
Definition: tensor.hpp:204
std::ptrdiff_t offset() const
Returns the offset of the tensor in the current buffer.
Definition: tensor.hpp:199
type dtype() const
Returns the tensor's data type.
Definition: tensor.hpp:174
Definition: buffer.hpp:41
constexpr uint8_t dcodeof(type type)
Returns the numeric code used for serialization of a data type.
Definition: types.hpp:182
Metadata< Tensor > metadataof(Tensor const &tensor)
Definition: serialization.hpp:59
Header headerof(Tensor const &tensor)
Definition: serialization.hpp:50
constexpr uint32_t MAGIC
Definition: serialization.hpp:26
Definition: serialization.hpp:29
uint64_t nbytes
Definition: serialization.hpp:33
uint32_t magic
Definition: serialization.hpp:30
uint8_t version
Definition: serialization.hpp:31
uint16_t checksum
Definition: serialization.hpp:32
uint8_t rank
Definition: serialization.hpp:46
size_t nbytes
Definition: serialization.hpp:45
size_t offset
Definition: serialization.hpp:44
uint8_t dcode
Definition: serialization.hpp:43
Definition: serialization.hpp:38