torch.PackShape
export type PackShape<
TensorShape extends Shape,
Count extends number,
Pattern extends string = '* ...',
> =
SplitSpace<Pattern> extends infer Tokens extends string[]
? CountBeforeStar<Tokens> extends infer Before extends number
? TakeFirst<TensorShape, Before> extends infer BeforeShape extends readonly number[]
? DropFirst<TensorShape, Before> extends infer AfterShape extends readonly number[]
? readonly [...BeforeShape, Count, ...AfterShape]
: readonly [Count, ...TensorShape]
: readonly [Count, ...TensorShape]
: readonly [Count, ...TensorShape]
: readonly [Count, ...TensorShape];TensorShapeextends ShapeCountextends numberPatternextends stringCompute pack output shape based on pattern. Pattern like '* h w' puts pack dim first, 'h * w' puts it in middle.
Examples
type S1 = PackShape<[4, 5], 3, '* h w'>;
// S1 = [3, 4, 5] - pack dimension first
type S2 = PackShape<[4, 5], 3, 'h * w'>;
// S2 = [4, 3, 5] - pack dimension in middle