encode_pytorch_tensor
Engine.encode_pytorch_tensor(tensor, level)- Encodes a PyTorch tensor into a plaintext format for encryption.
The tensor's device can be either CPU or CUDA and must match that of the engine.
The message is encoded in segments of length
Engine.slot_count. If the message length is not divisible by the slot count, the remaining slots are padded with zeros during encoding.
- Input:
- tensor: torch.Tensor
- level: optional
- Specifies the maximum multiplication level for encoding. Lower levels reduce space usage but also reduce the maximum multiplication depth.
- Output:
- Plaintext
Examples
CPU
import torch
from desilofhe import Engine
engine = Engine()
tensor = torch.tensor([1, 2, 3, 4])
encoded = engine.encode_pytorch_tensor(tensor)
encoded_level_2 = engine.encode_pytorch_tensor(tensor, level=2)
GPU
import torch
from desilofhe import Engine
engine = Engine(mode="gpu")
tensor = torch.tensor([1, 2, 3, 4], device="cuda")
encoded = engine.encode_pytorch_tensor(tensor)
Typically, the encrypt function handles encoding automatically. Pre-encoding a message can improve efficiency when the same message is reused across multiple operations, as it avoids redundant encoding.