nn.Linear accepts a variable number of dimensions as explained in the docs 77:

Input: (N, *, H_in) where ∗ means any number of additional dimensions and H_in = in_features

So you should be able to pass the input in the shape [batch_size, *any_number_of_dimensions, in_features] to the linear layers.
By using this approach the linear layer will be applied to each input in the * dimension(s).