Config change for a pre-trained transformer model
I am trying to implement a classification head for the reformer transformer. The classification head works fine, but when I try to change one of the config parameters- config.axial_pos_shape i.e sequence length parameter for the model it throws an error;

size mismatch for reformer.embeddings.position_embeddings.weights.0: copying a param with shape torch.Size([512, 1, 64]) from checkpoint, the shape in current model is torch.Size([64, 1, 64]).
size mismatch for reformer.embeddings.position_embeddings.weights.1: copying a param with shape torch.Size([1, 1024, 192]) from checkpoint, the shape in current model is torch.Size([1, 128, 192]).

The config:
{
  &quot;architectures&quot;: [
    &quot;ReformerForSequenceClassification&quot;
  ],
  &quot;attention_head_size&quot;: 64,
  &quot;attention_probs_dropout_prob&quot;: 0.1,
  &quot;attn_layers&quot;: [
    &quot;local&quot;,
    &quot;lsh&quot;,
    &quot;local&quot;,
    &quot;lsh&quot;,
    &quot;local&quot;,
    &quot;lsh&quot;
  ],
  &quot;axial_norm_std&quot;: 1.0,
  &quot;axial_pos_embds&quot;: true,
  &quot;axial_pos_embds_dim&quot;: [
    64,
    192
  ],
  &quot;axial_pos_shape&quot;: [
    64,
    256
  ],
  &quot;chunk_size_feed_forward&quot;: 0,
  &quot;chunk_size_lm_head&quot;: 0,
  &quot;eos_token_id&quot;: 2,
  &quot;feed_forward_size&quot;: 512,
  &quot;hash_seed&quot;: null,
  &quot;hidden_act&quot;: &quot;relu&quot;,
  &quot;hidden_dropout_prob&quot;: 0.05,
  &quot;hidden_size&quot;: 256,
  &quot;initializer_range&quot;: 0.02,
  &quot;intermediate_size&quot;: 3072,
  &quot;is_decoder&quot;: true,
  &quot;layer_norm_eps&quot;: 1e-12,
  &quot;local_attention_probs_dropout_prob&quot;: 0.05,
  &quot;local_attn_chunk_length&quot;: 64,
  &quot;local_num_chunks_after&quot;: 0,
  &quot;local_num_chunks_before&quot;: 1,
  &quot;lsh_attention_probs_dropout_prob&quot;: 0.0,
  &quot;lsh_attn_chunk_length&quot;: 64,
  &quot;lsh_num_chunks_after&quot;: 0,
  &quot;lsh_num_chunks_before&quot;: 1,
  &quot;max_position_embeddings&quot;: 8192,
  &quot;model_type&quot;: &quot;reformer&quot;,
  &quot;num_attention_heads&quot;: 2,
  &quot;num_buckets&quot;: [
    64,
    128
  ],
  &quot;num_chunks_after&quot;: 0,
  &quot;num_chunks_before&quot;: 1,
  &quot;num_hashes&quot;: 1,
  &quot;num_hidden_layers&quot;: 6,
  &quot;output_past&quot;: true,
  &quot;pad_token_id&quot;: 0,
  &quot;task_specific_params&quot;: {
    &quot;text-generation&quot;: {
      &quot;do_sample&quot;: true,
      &quot;max_length&quot;: 100
    }
  },
  &quot;vocab_size&quot;: 320
}

Python Code:
config = ReformerConfig()
config.max_position_embeddings = 8192
config.axial_pos_shape=[64, 128]

#config = ReformerConfig.from_pretrained('./cnp/config.json', output_attention=True)

model = ReformerForSequenceClassification(config)
model.load_state_dict(torch.load(&quot;./cnp/pytorch_model.bin&quot;))


I run into the same issue, trying to halve the size of the 65536 (128*512) by default max sequence length used in Reformer pre-training.
As @cronoik mentioned, you must:

load pretrained Reformer
resize it to your need by dropping unnecessary weights
save this new model
load this new model to perform your desired tasks

Those unnecessary weights are the ones from the Position Embeddings layer. In Reformer model, the Axial Position Encodings strategy was used to learn the position embeddings (rather than having fixed ones like BERT). Axial Position Encodings stores position embeddings in a memory efficient manner, using two small tensors rather than a big one.
However, the idea of position embeddings remains exactly the same, which is obtaining different embeddings for each position.
That said, in theory (correct me if I am misunderstanding somewhere), removing the last position embeddings to match your custom max sequence length should not hurt the performance. You can refer to this post from HuggingFace to see a more detailed description of Axial Position Encodings and understand where to truncate your position embeddings tensor.
I have managed to resize and use Reformer with a custom max length of 32768 (128*256) with the following code:
# Load intial pretrained model
model = ReformerForSequenceClassification.from_pretrained('google/reformer-enwik8', num_labels=2)

# Reshape Axial Position Embeddings layer to match desired max seq length       
model.reformer.embeddings.position_embeddings.weights[1] = torch.nn.Parameter(model.reformer.embeddings.position_embeddings.weights[1][0][:256])

# Update the config file to match custom max seq length
model.config.axial_pos_shape = 128, 256
model.config.max_position_embeddings = 128*256 # 32768

# Save model with custom max length
output_model_path = &quot;path/to/model&quot;
model.save_pretrained(output_model_path)

