1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file tvm/runtime/serializer.h
22 * \brief Serializer extension to support TVM data types
23 * Include this file to enable serialization of DLDataType, DLDevice
24 */
25#ifndef TVM_RUNTIME_SERIALIZER_H_
26#define TVM_RUNTIME_SERIALIZER_H_
27
28#include <dmlc/io.h>
29#include <dmlc/serializer.h>
30#include <tvm/runtime/c_runtime_api.h>
31#include <tvm/runtime/ndarray.h>
32
33namespace dmlc {
34namespace serializer {
35
36template <>
37struct Handler<DLDataType> {
38 inline static void Write(Stream* strm, const DLDataType& dtype) {
39 Handler<uint8_t>::Write(strm, dtype.code);
40 Handler<uint8_t>::Write(strm, dtype.bits);
41 Handler<uint16_t>::Write(strm, dtype.lanes);
42 }
43 inline static bool Read(Stream* strm, DLDataType* dtype) {
44 if (!Handler<uint8_t>::Read(strm, &(dtype->code))) return false;
45 if (!Handler<uint8_t>::Read(strm, &(dtype->bits))) return false;
46 if (!Handler<uint16_t>::Read(strm, &(dtype->lanes))) return false;
47 return true;
48 }
49};
50
51template <>
52struct Handler<DLDevice> {
53 inline static void Write(Stream* strm, const DLDevice& dev) {
54 int32_t device_type = static_cast<int32_t>(dev.device_type);
55 Handler<int32_t>::Write(strm, device_type);
56 Handler<int32_t>::Write(strm, dev.device_id);
57 }
58 inline static bool Read(Stream* strm, DLDevice* dev) {
59 int32_t device_type = 0;
60 if (!Handler<int32_t>::Read(strm, &(device_type))) return false;
61 dev->device_type = static_cast<DLDeviceType>(device_type);
62 if (!Handler<int32_t>::Read(strm, &(dev->device_id))) return false;
63 return true;
64 }
65};
66
67} // namespace serializer
68} // namespace dmlc
69#endif // TVM_RUNTIME_SERIALIZER_H_
70