can you fix my code for me? if newblocksize > oldlength: newweights =…
- User
can you fix my code for me? if new_block_size > old_length: new_weights = torch.nn.Upsample(size=(1, new_block_size), mode='linear') print(new_weights) print(f'✅ Adjusting positional embeddings length to {new_block_size} from the loaded block_size {old_length}.\n') new_wpe = torch.nn.Embedding(new_block_size, self.model.transformer['wpe'].weight.shape[-1]) new_wpe.weight.data = new_weights
- Assistant
Sure, I can help you fix your code. There are a few issues that need to be addressed:
- Using
Upsample:torch.nn.Upsampleis deprecated. Instead, you can usetorch.nn.functional.interpolate. - Resizing positional embeddings: After interpolating the weights, you may need to set the resized weights to the new positional embeddings correctly.
- Using
- User
fix this please without F.interpolate n_embd = self.model.transformer['wpe'].weight.shape[-1] upsampler = torch.nn.Upsample(size=(new_block_size, n_embd), mode='linear') new_weights = upsampler(self.model.transformer['wpe'].weight.data) print(new_weights) print(f'✅ Adjusting positional embeddings length to {new_block_size} from the loaded block_size {old_length}.\n') new_wpe = torch.nn.Embedding(new_block_size, n_embd) new_wpe.weight.data = new_weights
- Assistant
Certainly! I'll revise the code to fix the existing issues while keeping the
torch.nn.Upsamplefor interpolation as per your request.Here's your corrected code:
python - User
Traceback (most recent call last): File "/home/developer6/kotoba-voice/fam/llm/train.py", line 315, in model.configure_model() File "/home/developer6/kotoba-voice/fam/llm/train.py", line 85, in configure_model self.first_stage_model_cls = Model( File "/home/developer6/kotoba-voice/fam/llm/sample.py", line 94, in init self._init_model() File "/home/developer6/kotoba-voice/fam/llm/sample.py", line 190, in _init_model new_weights = upsampler(old_weights.data.unsqueeze(0).unsqueeze(0)).squeeze(0).squeeze(0) File "/home/developer6/anaconda3/envs/py310/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/home/developer6/anaconda3/envs/py310/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "/home/developer6/anaconda3/envs/py310/lib/python3.10/site-packages/torch/nn/modules/upsampling.py", line 157, in forward return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners, File "/home/developer6/anaconda3/envs/py310/lib/python3.10/site-packages/torch/nn/functional.py", line 4080, in interpolate raise NotImplementedError("Got 4D input, but linear mode needs 3D input") NotImplementedError: Got 4D input, but linear mode needs 3D input
- Assistant
It appears that the issue lies with the input dimensions expected by the
linearmode in theUpsamplefunction. Thelinearmode forUpsampleindeed expects a 3D input (i.e., with batch, channels, and width), but you are providing a 4D input.One way to work around this issue is to use 1D interpolation explicitly since the positional embeddings are generally one-dimensional.
- User
this gave me final shape (prev_blcok_size, new_block_size) but it should be (new_block_size, nembd)
- Assistant
Thanks for pointing that out. The issue is due to how the dimensions are handled during interpolation. When using
torch.nn.functional.interpolatewithlinearmode, the default behavior is to interpret one dimension as the channel dimension and another as the spatial dimension, which is leading to the wrong shape.