Changesets · Branches · Tags · Compare
| Changeset | b9a72751753b69a348693bf9b2cf1b21579f5c057faff4d18161e024dfbd7eb3 |
|---|---|
| Author | Yongsen Mao <sammaoys@gmail.com> · 2025-09-26 15:35 UTC |
| Committer | Yongsen Mao <sammaoys@gmail.com> · 2025-09-26 15:35 UTC |
| Parents | 45d26391e596 |
1 file changed
added spatiallm/tuner/initialize_weight.py +89 −0
| @@ -0,0 +1,89 @@ | ||
| 1 | +import argparse | |
| 2 | + | |
| 3 | +import torch | |
| 4 | +import spatiallm | |
| 5 | +from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer | |
| 6 | + | |
| 7 | +if __name__ == "__main__": | |
| 8 | + parser = argparse.ArgumentParser("Initialize SpatialLM weight") | |
| 9 | + parser.add_argument( | |
| 10 | + "--spatiallm_weight", | |
| 11 | + type=str, | |
| 12 | + default="manycore-research/SpatialLM-Qwen-0.5B", | |
| 13 | + help="Path to the SpatialLM weight", | |
| 14 | + ) | |
| 15 | + parser.add_argument( | |
| 16 | + "--llm_weight", | |
| 17 | + type=str, | |
| 18 | + default="Qwen/Qwen2.5-0.5B-Instruct", | |
| 19 | + help=""" | |
| 20 | + Path to the LLM weight | |
| 21 | + Qwen/Qwen2.5-0.5B-Instruct for SpatialLM Qwen variant | |
| 22 | + meta-llama/Llama-3.2-1B-Instruct for SpatialLM Llama variant | |
| 23 | + """, | |
| 24 | + ) | |
| 25 | + parser.add_argument( | |
| 26 | + "--encoder_weight", | |
| 27 | + type=str, | |
| 28 | + required=True, | |
| 29 | + help=""" | |
| 30 | + Path to the point cloud encoder weight file | |
| 31 | + sonata.pth for SpatialLM1.1 or scenescript_model_ase.ckpt for SpatialLM1.0 | |
| 32 | + """, | |
| 33 | + ) | |
| 34 | + parser.add_argument( | |
| 35 | + "--output_dir", type=str, required=True, help="Path to the output weight folder" | |
| 36 | + ) | |
| 37 | + args = parser.parse_args() | |
| 38 | + | |
| 39 | + print("Loading config...") | |
| 40 | + config = AutoConfig.from_pretrained(args.spatiallm_weight) | |
| 41 | + spatiallm_model = AutoModelForCausalLM.from_config(config) | |
| 42 | + | |
| 43 | + # load original LLM model weight | |
| 44 | + print("Loading LLM weight...") | |
| 45 | + llm_model = AutoModelForCausalLM.from_pretrained( | |
| 46 | + args.llm_weight, torch_dtype="auto" | |
| 47 | + ) | |
| 48 | + llm_state_dict = llm_model.state_dict() | |
| 49 | + spatiallm_model.load_state_dict(llm_state_dict, strict=False) | |
| 50 | + | |
| 51 | + print("Loading encoder weight...") | |
| 52 | + if config.point_backbone == "sonata": | |
| 53 | + encoder_state_dict = torch.load(args.encoder_weight, weights_only=False)[ | |
| 54 | + "state_dict" | |
| 55 | + ] | |
| 56 | + for name, param in spatiallm_model.point_backbone.named_parameters(): | |
| 57 | + if ( | |
| 58 | + name in encoder_state_dict | |
| 59 | + and param.shape != encoder_state_dict[name].shape | |
| 60 | + ): | |
| 61 | + new_param = encoder_state_dict[name][:, : param.shape[1]] | |
| 62 | + encoder_state_dict[name] = new_param | |
| 63 | + elif config.point_backbone == "scenescript": | |
| 64 | + ckpt_dict = torch.load(args.encoder_weight, weights_only=False) | |
| 65 | + encoder_state_dict = {} | |
| 66 | + for k, v in ckpt_dict["model_state_dict"].items(): | |
| 67 | + if k.startswith("encoder"): | |
| 68 | + encoder_state_dict[k.replace("encoder.", "")] = v | |
| 69 | + for name, param in spatiallm_model.point_backbone.named_parameters(): | |
| 70 | + if ( | |
| 71 | + name in encoder_state_dict | |
| 72 | + and param.shape != encoder_state_dict[name].shape | |
| 73 | + ): | |
| 74 | + # from [343, 3, 16] to [343, 6, 16] | |
| 75 | + # expand the parameter to the same shape | |
| 76 | + pretrained_param = encoder_state_dict[name] | |
| 77 | + missing_channels = param.shape[1] - pretrained_param.shape[1] | |
| 78 | + random_tensor = torch.randn( | |
| 79 | + param.shape[0], missing_channels, param.shape[2] | |
| 80 | + ) | |
| 81 | + new_param = torch.cat([pretrained_param, random_tensor], dim=1) | |
| 82 | + encoder_state_dict[name] = new_param | |
| 83 | + spatiallm_model.point_backbone.load_state_dict(encoder_state_dict, strict=False) | |
| 84 | + | |
| 85 | + # save the model | |
| 86 | + print("Saving SpatialLM weight...") | |
| 87 | + tokenizer = AutoTokenizer.from_pretrained(args.spatiallm_weight) | |
| 88 | + tokenizer.save_pretrained(args.output_dir) | |
| 89 | + spatiallm_model.save_pretrained(args.output_dir) |