4 #include <ATen/core/ivalue.h> 5 #include <c10/util/ArrayRef.h> 6 #include <torch/csrc/utils/disallow_copy.h> 12 enum class OpCode : char {
30 SHORT_BINSTRING =
'U',
74 SHORT_BINUNICODE =
'\x8c',
81 STACK_GLOBAL =
'\x93',
86 enum PicklerClass : uint8_t { TENSOR = 0, INTLIST = 1 };
91 TH_DISALLOW_COPY_AND_ASSIGN(
Pickler);
94 Pickler(std::vector<at::Tensor>* tensor_table)
95 : tensor_table_(tensor_table) {}
97 const std::vector<char>& stack();
103 void pushBinGet(uint32_t memo_id);
104 void pushMemoizedString(
const IValue& ivalue);
105 void pushString(
const std::string&
string);
106 void pushTensor(
const IValue& ivalue);
107 void pushDouble(
const IValue& ivalue);
108 void pushMemoization(
const void* item);
109 void pushMemoization(
const IValue& ivalue);
110 void pushList(
const IValue& ivalue);
111 void pushIntList(
const IValue& ivalue);
112 void pushTuple(
const IValue& ivalue);
113 void pushDict(
const IValue& ivalue);
114 void pushClass(PicklerClass cls);
115 const void* getPointer(
const IValue& ivalue);
117 void pushUint8(uint8_t value);
118 void pushOpCode(OpCode value);
119 void pushUint32(uint32_t value);
120 void pushInt32(int32_t value);
123 std::vector<char> stack_;
127 std::unordered_map<const void*, uint32_t> memo_;
130 std::vector<at::Tensor>* tensor_table_;
134 uint32_t memo_id = 0;
144 const std::vector<at::Tensor>* tensor_table)
145 : bytes_(static_cast<const uint8_t*>(data)),
146 end_ptr_(bytes_ + size),
147 tensor_table_(tensor_table) {}
149 std::vector<IValue> parse_ivalue_list();
154 template <
typename T>
157 bytes_ +
sizeof(
T) <= end_ptr_,
158 "Unpickler overran buffer while reading a value");
160 std::memcpy(&item, bytes_,
sizeof(
T));
167 OpCode readInstruction();
168 std::string readString();
172 std::vector<IValue> stack_;
173 std::vector<IValue> memo_;
174 std::vector<size_t> marks_;
175 const uint8_t* bytes_;
176 const uint8_t* end_ptr_;
177 const std::vector<at::Tensor>* tensor_table_;