← HomOps Reference
HomReshape
Change how your data is laid out, without touching the encrypted values.
RhShape
RunsServer
Changes shapeYes: new layout
Changes scaleNo: unchanged
Levels spent0
KeysNone
What it does
HomReshape gives data a new shape (flattening, splitting, or merging axes) without changing its values. On the server, its backend applies the target shape to the ciphertext.It can appear anywhere in a pipeline, including the encrypted
hom section. It is not client-only; use client sections only when that placement suits the surrounding work.Typical uses:
Flatten an image for a linear layer:
(1, 28, 28) → (784,).Restore an output shape after decryption:
(10, 1) → (10,) in client_post.Bridge convolution feature maps into a matmul-ready vector:
(5, 8, 8) → (320,).Signature
HomReshape(dims)HomReshape has no set_data; it carries no weights, only the target shape.Parameters
ParameterTypeDefaultDescription
dimsshapenoneThe target shape. One dimension may be -1, which is inferred from the total number of elements.Rules that depend on the value
SettingRuleIf you break it
dimsThe total element count must be preserved: the product of dims must equal the product of the input shape (after any -1 is inferred).Compilation fails: the reshape is incompatible with the input size.dimsAt most one dimension may be -1.Compilation fails.dimsThe reshape may change which axis is the n axis, since the packing picks the axis that wastes the fewest slots. Worth knowing if a later op expects a particular n axis.Not an error, but see Concepts.Requirements
- Element count preserved. Input and output must hold the same number of elements.
- Any pipeline placement. It may run in
homon the server as well as in client sections.
Shape effect: yes
The data is re-viewed under
dims, and the n axis is re-inferred for the new layout.InputdimsOutput
(1, 28, 28)(784,)(784,) flatten an image
(784,)(28, 28)(28, 28) restore a grid
(5, 8, 8)(320,)(320,) feature maps to a vector
(10, 1)(10,)(10,) drop a trailing axis
(512, 1)(-1,)(512,) with -1 inferred
One input passes through the reshape:
in
(1, 28, 28)@scale Sclient_preHomReshape((784,))
out
(784,)@scale Sbefore encrypt: same values, new layout
Shape and the n axis are covered on Concepts.
Scale effect: no
A reshape is a layout view only. Scale is unchanged.
Level budget
Zero. Server-side reshape changes ciphertext layout without spending modulus-chain levels.
Keys
None. Server-side reshape needs no evaluation key.
Example
Flatten a
28×28 image before the first linear layer. This reshape may run in the encrypted hom section:pipeline.pyPYTHON
from lattica_build.base_classes.hom_pipeline import HomomorphicPipeline from lattica_build.operators.composite.sequential import SequentialHomOp from lattica_build.operators import HomLinear, HomSquare, HomReshape pipeline = HomomorphicPipeline( client_pre=[HomReshape((28 * 28,))], hom=SequentialHomOp( HomLinear((50, 28 * 28), bias=False, with_modswitch=False), HomSquare(with_modswitch=False), HomLinear((10, 50), bias=False, with_modswitch=False), ), input_shape=(1, 28, 28), )
A reshape in
client_post puts the decrypted result back into its expected shape:pipeline.pyPYTHON · CLIENT_POST
client_post=[HomReshape((10,))] # runs after decryption, on your machine
A reshape changes the shape but never the scale. Following one input through the pipeline above:
StageShapeScale
Pipeline input(1, 28, 28)S
After HomReshape(784,)S (unchanged)
After first HomLinear(50,)S
After HomSquare(50,)S²
See also
- HomUnsqueeze · HomSqueeze: insert or remove a size-1 axis on the server
- HomSlice: extract an index or range along one axis
- Repeat: fill the slots on the client, also client-only
- Concepts: the n axis and how layout maps to slots
Next
Back to
HomOps Reference
Browse all operators, or pick the next one from the sidebar.