Beispiel #1
0
 def decode(self, data):
     offset = Offset()
     self.src = unpack_int(data, offset)
     self.dst = unpack_int(data, offset)
     self.key = bytes(unpack_raw_data(data, offset))
     self.data = unpack_raw_data(data, offset)
     self.check()
     return self
Beispiel #2
0
 def deseralize_pubkeys(self, data: bytearray, offset: Offset = None):
     offset = offset or Offset()
     num_keys = unpack_int(data, offset)
     for _ in range(num_keys):
         id = unpack_int(data, offset)
         new_key = Key().deseralize_pubkey(data, offset)
         self[id] = new_key
     return self
Beispiel #3
0
 def deseralize_keys(self, data: bytearray, offset: Offset = None):
     offset = offset or Offset()
     num_keys = unpack_int(data, offset)
     for _ in range(num_keys):
         new_key = Key()
         new_key.deseralize_key(data, offset)
         self.append(new_key)
     return self
Beispiel #4
0
    def decompress(self, data: bytearray) -> [torch.Tensor]:
        result = []

        offset = Offset(0)
        num_layers = unpack_int(data, offset)

        if isinstance(self.compressors, _TensorCompressor):
            compressors = [self.compressors] * num_layers
        elif isinstance(self.compressors, list):
            assert (len(self.compressors) == num_layers)
            compressors = self.compressors

        for i in range(num_layers):
            result.append(compressors[i].decompress(data, offset))

        return result
Beispiel #5
0
    def decompress(self, data: bytearray) -> [torch.Tensor]:
        result = []
        result_types = []
        offset = Offset(0)

        # get number of tensors
        num_tensors = unpack_int(data, offset)

        # re-construct the tensor list
        for i in range(num_tensors):
            dtype = bytes(unpack_fixed_width_data(data, offset, 1))
            result_types.append(self.type_reverse_map[dtype])
            shape = unpack_tensor_shape(data, offset)
            result.append(torch.zeros(shape))

        # restore compressed data
        compressed_data = self.compressors.decompress(data, offset)
        torch.nn.utils.vector_to_parameters(compressed_data, result)

        # restore types
        for i in range(len(result)):
            result[i] = result[i].to(dtype=result_types[i])

        return result