You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Convert contiguous select_copy to zero-copy view in ReplaceViewCopyWithViewPass (#19198)
Summary:
Extends the ReplaceViewCopyWithViewPass to convert `select_copy` ops to zero-copy `memory.select` views when the output is a contiguous sub-region of the base tensor. This is the same pattern used for `view_copy` -> `memory.view`, but for select operations.
The pass checks that the base is densely packed, non-constant, and static, and that the selected output forms a dense packing.
For static memory-planned subviews, the emitter elides the op entirely (no runtime instruction) by serializing tensor metadata with `mem_offset = base_offset + byte_delta`. For dynamic shapes, a new `executorch_prim::et_select` runtime op sets the output data pointer to `self.data_ptr + offset`.
Changes:
- `exir/memory.py`: Added `memory.select` function
- `exir/passes/replace_view_copy_with_view_pass.py`: Extended `_ViewSpec` with `byte_offset`, `stride`, `dim_order` params; added contiguity check using `stride_from_dim_order`; added select_copy handling in the pass
- Pipeline integration: memory planner, to_out_var skiplist, emitter, serialization
- `kernels/prim_ops/et_select.{h,cpp}`: C++ runtime op for dynamic select views
- Tests: 5 new Python tests + 1 C++ test
Authored with Claude.
Reviewed By: metascroy
Differential Revision: D102396195
f"_ViewSpec is incompatible with its base on creation. It has shape_dynamism={self.shape_dynamism}, but its base has shape_dynamism={base.shape_dynamism}."
161
-
)
162
-
self._guards.append(
163
-
_Guard(
164
-
"shape_dynamism_init",
165
-
lambdaview_spec: view_spec.shape_dynamism,
166
-
base.shape_dynamism,
167
-
)
168
-
)
169
-
self._guards.append(
170
-
_Guard(
171
-
"shape_dynamism_eq_base",
172
-
lambdaview_spec: view_spec.shape_dynamism
173
-
==view_spec._base.shape_dynamism,
174
-
True,
175
-
)
176
-
)
177
-
178
176
ifself.dtype!=base.dtype:
179
177
raiseException(
180
178
f"_ViewSpec is incompatible with its base on creation. It has dtype={self.dtype}, but its base has dtype={base.dtype}."
f"_ViewSpec is incompatible with its base on creation. It has shape_dynamism={self.shape_dynamism}, but its base has shape_dynamism={base.shape_dynamism}."
0 commit comments