Concatenation and Slicing #
Concatenation, slicing, sequence concatenation, squeeze/unsqueeze, and channel layout transforms.
Concatenate a list of (n,d) tensors along the last axis, producing (n, headCount*d).
This is mainly used by attention blocks that split/merge heads.
PyTorch analogy: torch.cat(heads, dim=-1) after splitting heads, followed by a reshape.
Instances For
Concatenate two vectors by appending v2 after v1.
Instances For
Slicing / concatenation on the leading axis #
concat_dim0_spec is the "append on axis 0" primitive that powers many higher-level utilities
(sequence concatenation, channel skip connections, etc.).
For backprop and for "undoing" concatenations, it is convenient to have an explicit slice operation. We keep the API compact and index-safe:
slice_range0_spec start lenselectslenconsecutive entries starting atstartalong axis 0.concat_dim0_backward_specis the adjoint ofconcat_dim0_spec(splits a gradient tensor).
Slice len entries along axis 0, starting at start.
This is the simplest "range slice" one typically needs to express:
- taking the first
nchannels/tokens, - extracting the skip-connection half after a concat,
- implementing
take/dropwithout changing the inner shape.
The proof len + start ≤ n makes the slice total (no out-of-bounds behavior).
Instances For
Backward (adjoint) of concat_dim0_spec.
If y = concat_dim0_spec x1 x2, then in reverse-mode we split the upstream gradient δy into:
δx1= the firstnentries ofδy,δx2= the lastmentries ofδy.
Instances For
Backward (adjoint) of slice_range0_spec.
If y = slice_range0_spec start len x, then slice_range0_backward_spec start len δy re-inserts
the gradient into the original shape and fills everything outside the slice with zeros.
Instances For
Concatenate two sequences along time (axis 0), producing a longer sequence.
If seq1 : (seqLen1 x hidden) and seq2 : (seqLen2 x hidden), this returns
(seqLen1 + seqLen2) x hidden by appending seq2 after seq1.
Do not confuse this with Spec.concatSequenceSpec (defined in NN.Spec.Core.Sequence), which
concatenates along the feature dimension for same-length sequences.
Instances For
Concatenate two sequences along the feature dimension (inner axis).
Instances For
Same as expand_to_col_spec, specialized to vectors.
Instances For
Same as squeeze_col_spec, specialized to vectors.
Instances For
Unsqueeze (insert a singleton dim). Currently implemented as expand_to_col_spec.
Core uses singleton insertion mainly for column vectors, so this operation is specialized to that use case. General axis insertion can extend this definition.
Instances For
Turn a vector (n) into a batch of size 1: (1,n).
Instances For
Convert channel-first images (b,c,h,w) into channel-last (b,h,w,c).