| PT-007 | A restricted unpickler for torch.save checkpoints handles exactly the opcodes _weights_only_unpickler.py (04df708bcc03) handles, and rejects every other opcode. Object building: GLOBAL, NEWOBJ, REDUCE, BUILD. Containers: EMPTY_TUPLE, TUPLE, TUPLE1, TUPLE2, TUPLE3, EMPTY_LIST, APPEND, APPENDS, EMPTY_DICT, SETITEM, SETITEMS, EMPTY_SET, MARK. Constants: NONE, NEWFALSE, NEWTRUE, BININT, BININT1, BININT2, LONG1, BINFLOAT, BINUNICODE, SHORT_BINSTRING. Memo: BINGET, LONG_BINGET, BINPUT, LONG_BINPUT. Storage: BINPERSID (only persistent ids whose first item is 'storage'). Framing: PROTO, STOP. BUILD is needed for a normal state_dict: the OrderedDict carries _metadata through BUILD, and the reference applies BUILD to an OrderedDict as __dict__.update(state). LONG1 appears for any int >= 2^31, for example a storage numel (bytes, for UntypedStorage) of 2 GiB or more. BINFLOAT appears for float values outside tensors. | |
| PT-008 | Reader. A data/ record starts at local header offset + 30 + filename length + extra length, read from that record's local header. PyTorchStreamReader::getRecordOffset (inline_container.cc at f164555cf648) computes exactly that and does not look at the extra field's header id or contents. The 'FB' id (0x4246) is what torch's writer uses (detail::getPadding); any extra field, or none, loads. Alignment is a writer SHOULD (64, or the .storage_alignment record) for mmap speed. A reader must not assume it: an unaligned data/ record is valid and torch.load reads it, with mmap on and off. Checked with tools/check_pytorch_zip_alignment.py: Python zipfile archives with no extra field, and with 64-byte alignment through header id 0xCAFE, load correctly under the default settings. | |
| PT-010 | Reader. The optional last argument of _rebuild_tensor_v2 / _rebuild_tensor_v3 is the dict from torch._utils.get_tensor_metadata, which carries only the conj and neg bits. Tensor.__reduce_ex__ appends it only when it is not empty. {'conj': True} means the stored bytes are the un-conjugated values: negate every imaginary part. {'neg': True} means negate every element, as torch's neg does it: flip the sign bit of float16, bfloat16, float32, float64, and both parts of complex32/64/128 (0 becomes -0, a NaN's sign flips). For int8, int16, int32, int64, and uint8, negate in two's complement with wraparound: int8 -128 stays -128, uint8 1 becomes 255. torch.save accepts the bit on every dtype through the private torch._neg_view, and torch.load sets it again. For bool, uint16, uint32, uint64, and every float8 type, torch then cannot resolve it ("neg_cpu" not implemented), so the tensor is unusable in torch too. A reader refuses those, and may refuse the integer cases. A reader never ignores the bit. The only public way to get the bit is c.conj().imag on complex32, complex64, or complex128, which gives a float16, float32, or float64 view of a complex storage. torch.load applies both (weights_only=True accepts the dict). A reader that ignores them returns wrong values without an error. Plain float8, uint16/32/64, and other v3 dtypes carry no metadata in normal saves, so a writer of canonical data never needs the argument: it writes resolved values (resolve_conj, resolve_neg) and leaves the dict out. Checked by tools/check_rewrite_rules.py (torch 2.15.0). Test file: small-pt-fp8.pt (c64_conj, imag_neg; f8e4m3 and f8e5m2 without metadata). | |