Skip to content

layout: Add support for permutation layout in unpackOp - #3157

Open
VedantParanjape wants to merge 1 commit into
google:mainfrom
VedantParanjape:permutation
Open

layout: Add support for permutation layout in unpackOp#3157
VedantParanjape wants to merge 1 commit into
google:mainfrom
VedantParanjape:permutation

Conversation

@VedantParanjape

Copy link
Copy Markdown
Collaborator

Permutation layout is supported by assign_layout op. The inverse of this op is unpack op. It is required by client decryption handles to decrypt the data and store it according to the layout attribute. This patch adds support for permutation layout to work with unpackOp.

Permutation layout is supported by assign_layout op. The inverse of this
op is unpack op. It is required by client decryption handles to decrypt
the data and store it according to the layout attribute. This patch adds
support for permutation layout to work with unpackOp.
@VedantParanjape
VedantParanjape requested a review from j2kun July 1, 2026 23:45

@j2kun j2kun left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can merge this as-is, but I think the attribute as a whole needs to be changed to avoid the special cases and confusing aspects of this PR.

Value result = zeroCtxt.getResult();

int64_t ctBound = (tensorType.getRank() == 2) ? tensorType.getDimSize(0) : 1;
int64_t srcCtBound =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is a little strange here because only one of the source and destination are semantically "ct, slot," depending on the caller's context. Moreover, the non-ct-slot side is restricted (somewhat arbitrarily) to be dimension 2.

Maybe don't name the variables "ct, slot", and then document the function with a code comment for the dim-2 restriction and for the semantic ambiguity of which one is the data and which is ciphertext.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this, I can rework this when I address the overall issue with the design of layout attribute.

// Tests `tensor_ext.unpack` with a dense-permutation layout attribute

// -----------------------------------------------------------------------------
// Case 1: single dense permutation with a rank-1 data-semantic target.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the design of this explicit layout attribute is insufficient for what you're trying to use it for, and this PR is showing the strain.

The original permutation attribute seems incorrect in retrospect:

  // A list of tuples [a, b, c, d] representing an explicit map (ct, slot) ->
  // (ct, slot) defined by f(a, b) = (c, d).

In fact, it's not a mapping (ct, slot) -> (ct, slot), because one side is data semantic (and restricted to rank 2) and the other is ciphertext semantic.

And then here, one must further hack around it by having the permutation attribute use rank 2 indices for the data semantic side, even though it has semantic rank 1.

Probably what it should be is

  // A list of tuples [a, b, ..., c, d] representing an explicit map 
  // (d0, d1, ...) -> (ct, slot)
  // defined by f(a, b, ...) = (c, d)

With a verifier to assert that when this is attached to an op, the ranks agree (the data semantic tensor involved has rank denseEltsAttr.getType().getShape()[1] - 2 and numElements = denseEltsAttr.getType().getShape()[0]).

Then the layout attribute would naturally support any rank of data-semantic tensor.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that seems like a more generic implementation. But the subtle issue with f(a, b, ...) = (c, d) is that it cannot be used on result attributes. How about I implement a n-dim to n-dim mapping and then let downstream passes in the ciphertext semantic verify its correctness.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the subtle issue with f(a, b, ...) = (c, d) is that it cannot be used on result attributes.

I don't see why not...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the subtle issue with f(a, b, ...) = (c, d) is that it cannot be used on result attributes.

I don't see why not...

because for a result attribute, we are going from ciphertext to a n-dim array, hence we would need f(a, b) = (c, d, e, ...)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The layout attribute is defined to always map from the data-semantic domain to the ciphertext-semantic domain. See for example https://github.com/google/heir/blob/main/tests/Transforms/lower_unpack/lower_unpack.mlir

Then unpack is supposed to be smart enough to know that it needs to invert the mapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants