[[老环境配置记录modelscope]]

modelscope :/usr/local/bin/python是默认的python路径

2025-09-02 17:35:55,329 - modelscope - WARNING - task token-classification input definition is missing 2025-09-02 17:35:55,509 - modelscope - WARNING - task token-classification output keys are missing

3/47 周赛1 # 2025天池极客挑战赛——用户购买行为预测 全国第三

adaseq感觉已经两年没有维护了,安全加载模型没有配置方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from adaseq.commands.train import build_trainer_from_partial_objects

work_dir = 'experiments/transformer_crf'
os.makedirs(work_dir, exist_ok=True)

trainer = build_trainer_from_partial_objects(
config,
work_dir=work_dir,
seed=42,
device='cuda:0'
)

trainer.train()
trainer.test()
1
2
3
4
Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint. 
(1) In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.
(2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
WeightsUnpickler error: Unsupported global: GLOBAL numpy.core.multiarray._reconstruct was not an allowed global by default. Please use `torch.serialization.add_safe_globals([_reconstruct])` or the `torch.serialization.safe_globals([_reconstruct])` context manager to allowlist this global if you trust this class/function.

echo ‘export PATH=”/mnt/workspace/miniconda3/bin:$PATH”‘ >> ~/.bashrc source ~/.bashrc
conda install ipykernel
python -m ipykernel install --user --name py311 --display-name "Python 3.11 (py311)"

解决方法 work!

1
2
!pip install torch==2.3.1 transformers==4.48.3 modelscope==1.29.1
!pip install "modelscope[nlp]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

mego backbone架构:

modelscope/models/multi_modal/mgeo/backbone.py
编码器768,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
MGeo(
(text_encoder): BertModel(
(embeddings): BertEmbeddings(
(word_embeddings): Embedding(21128, 768, padding_idx=0)
(position_embeddings): Embedding(512, 768)
(token_type_embeddings): Embedding(2, 768)
(LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
(dropout): Dropout(p=0.1, inplace=False)
)
(encoder): BertEncoder(
(layer): ModuleList(
(0-11): 12 x BertLayer(
(attention): BertAttention(
(self): BertSelfAttention(
(query): Linear(in_features=768, out_features=768, bias=True)
(key): Linear(in_features=768, out_features=768, bias=True)
(value): Linear(in_features=768, out_features=768, bias=True)
(dropout): Dropout(p=0.1, inplace=False)
)
(output): BertSelfOutput(
(dense): Linear(in_features=768, out_features=768, bias=True)
(LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
(dropout): Dropout(p=0.1, inplace=False)
)
)
(intermediate): BertIntermediate(
(dense): Linear(in_features=768, out_features=3072, bias=True)
(intermediate_act_fn): GELUActivation()
)
(output): BertOutput(
(dense): Linear(in_features=3072, out_features=768, bias=True)
(LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
(dropout): Dropout(p=0.1, inplace=False)
)
)
)
)
)
)

modelscope/models/multi_modal/mgeo/token_classification.py
MGeoForTokenClassification 类是一个用于执行词元分类任务的 PyTorch 模型,它继承自 MGeoPreTrainedModel 初始化backbone,并使用一个专门的、基于 Transformer 的主干网络 (MGeo) 来处理输入的文本。


核心组件与功能分析

__init__ 方法

这个构造函数用于构建模型的架构:

  • 它根据配置初始化标签数量 (self.num_labels)。
  • 它实例化了 MGeo 主干模型,该模型负责核心的文本编码工作。
  • 它添加了一个 dropout 层 (self.dropout) 来防止过拟合。
  • 它在 MGeo 主干网络的顶部添加了一个线性分类器头 (self.classifier)。这个分类器将主干网络的输出映射到所需的标签数量。

forward 方法

forward 方法定义了模型如何处理输入并生成输出。它接收多个参数,包括 input_idsattention_masklabels。其处理流程如下:

  1. 输入处理:该方法首先将输入(如 input_idsattention_mask)传递给 self.bert(即 MGeo 主干网络)以获得输入序列的编码表示。
  2. Dropout 与分类:它对主干网络输出的序列应用 dropout,然后将其输入线性分类器,计算出 logits(即每个词元的原始、非标准化的预测分数)。
  3. 损失计算:如果提供了 labels,模型会计算**交叉熵损失 (Cross-Entropy Loss)**。它巧妙地利用 attention_mask 来确保损失只针对非填充词元进行计算,这对于模型的准确训练至关重要。
  4. 输出:最后,该方法返回结果。如果 return_dictTrue,它会返回一个详细的 AttentionTokenClassificationModelOutput 对象,其中包含计算出的 losslogits 以及主干网络的其他输出。否则,它返回一个简单的元组。

800个词->40G

解决方法 NOT WORK

  1. make sure torch>=2.6.0
    1
    !pip install --upgrade torch==2.6.0 torchvision
  2. 1
    2
    3
    4
    5
    import torch.serialization
    # Add numpy.core.multiarray._reconstruct to safe globals
    from numpy.core.multiarray import _reconstruct,ndarray
    torch.serialization.add_safe_globals([_reconstruct])
    torch.serialization.add_safe_globals([ndarray])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# initialize a trainer
import os
from adaseq.commands.train import build_trainer_from_partial_objects

work_dir = 'experiments/transformer_crf'
os.makedirs(work_dir, exist_ok=True)

trainer = build_trainer_from_partial_objects(
config,
work_dir=work_dir,
seed=42,
device='cuda:0'
)

# do training
trainer.train()

# do testing
trainer.test()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
Model revision not specified, use revision: v1.0.0

ValueError: DefaultTrainer: SequenceLabelingModel: TransformerEmbedder: Due to a serious vulnerability issue in `torch.load`, even with `weights_only=True`, we now require users to upgrade torch to at least v2.6 in order to use the function. This version restriction does not apply when loading files with safetensors.
See the vulnerability report here https://nvd.nist.gov/vuln/detail/CVE-2025-32434

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File /usr/local/lib/python3.11/site-packages/modelscope/utils/registry.py:211, in build_from_cfg(cfg, registry, group_key, default_args)
210 else:
--> 211 return obj_cls(**args)
212 except Exception as e:
213 # Normal TypeError does not print class name.

File /usr/local/lib/python3.11/site-packages/adaseq/modules/embedders/transformer_embedder.py:84, in TransformerEmbedder.__init__(self, model_name_or_path, drop_special_tokens, sub_module, train_parameters, eval_mode, load_weights, scalar_mix, gradient_checkpointing, transformer_kwargs, sub_token_mode)
82 self.sub_token_mode = sub_token_mode
---> 84 self.transformer_model, self.from_hf = get_transformer(
85 model_name_or_path,
86 load_weights=load_weights,
87 **(transformer_kwargs or {}),
88 )
90 if self.from_hf:

File /usr/local/lib/python3.11/site-packages/adaseq/modules/embedders/transformer_embedder.py:363, in get_transformer(model_name_or_path, load_weights, source, **kwargs)
362 try:
--> 363 return get_ms_transformer(model_name_or_path, **kwargs), False
364 except HTTPError as e:

File /usr/local/lib/python3.11/site-packages/adaseq/modules/embedders/transformer_embedder.py:414, in get_ms_transformer(model_name_or_path, **kwargs)
413 try:
--> 414 transformer = MsModel.from_pretrained(model_name_or_path, task='backbone', **kwargs)
415 except KeyError:

File /usr/local/lib/python3.11/site-packages/modelscope/models/base/base_model.py:178, in Model.from_pretrained(cls, model_name_or_path, revision, cfg_dict, device, trust_remote_code, **kwargs)
177 if use_hf in {True, None}:
--> 178 model = try_to_load_hf_model(local_model_dir, task_name, use_hf,
179 **kwargs)
180 if model is not None:

File /usr/local/lib/python3.11/site-packages/modelscope/utils/automodel_utils.py:125, in try_to_load_hf_model(model_dir, task_name, use_hf, **kwargs)
123 if automodel_class is not None:
124 # use hf
--> 125 model = automodel_class.from_pretrained(model_dir, **kwargs)
126 return model

File /usr/local/lib/python3.11/site-packages/modelscope/utils/hf_util/patcher.py:281, in _patch_pretrained_class.<locals>.get_wrapped_class.<locals>.ClassWrapper.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
278 model_dir = get_model_dir(pretrained_model_name_or_path,
279 **kwargs)
--> 281 module_obj = module_class.from_pretrained(
282 model_dir, *model_args, **kwargs)
284 if module_class.__name__.startswith('AutoModel'):

File /usr/local/lib/python3.11/site-packages/transformers/models/auto/auto_factory.py:600, in _BaseAutoModelClass.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
599 config = config.get_text_config()
--> 600 return model_class.from_pretrained(
601 pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs
602 )
603 raise ValueError(
604 f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
605 f"Model type should be one of {', '.join(c.__name__ for c in cls._model_mapping)}."
606 )

File /usr/local/lib/python3.11/site-packages/transformers/modeling_utils.py:317, in restore_default_torch_dtype.<locals>._wrapper(*args, **kwargs)
316 try:
--> 317 return func(*args, **kwargs)
318 finally:

File /usr/local/lib/python3.11/site-packages/transformers/modeling_utils.py:5069, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, weights_only, *model_args, **kwargs)
5060 torch.set_default_dtype(dtype_orig)
5062 (
5063 model,
5064 missing_keys,
5065 unexpected_keys,
5066 mismatched_keys,
5067 offload_index,
5068 error_msgs,
-> 5069 ) = cls._load_pretrained_model(
5070 model,
5071 state_dict,
5072 checkpoint_files,
5073 pretrained_model_name_or_path,
5074 ignore_mismatched_sizes=ignore_mismatched_sizes,
5075 sharded_metadata=sharded_metadata,
5076 device_map=device_map,
5077 disk_offload_folder=offload_folder,
5078 offload_state_dict=offload_state_dict,
5079 dtype=torch_dtype,
5080 hf_quantizer=hf_quantizer,
5081 keep_in_fp32_regex=keep_in_fp32_regex,
5082 device_mesh=device_mesh,
5083 key_mapping=key_mapping,
5084 weights_only=weights_only,
5085 )
5086 # make sure token embedding weights are still tied if needed

File /usr/local/lib/python3.11/site-packages/transformers/modeling_utils.py:5335, in PreTrainedModel._load_pretrained_model(cls, model, state_dict, checkpoint_files, pretrained_model_name_or_path, ignore_mismatched_sizes, sharded_metadata, device_map, disk_offload_folder, offload_state_dict, dtype, hf_quantizer, keep_in_fp32_regex, device_mesh, key_mapping, weights_only)
5333 else:
5334 original_checkpoint_keys = list(
-> 5335 load_state_dict(checkpoint_files[0], map_location="meta", weights_only=weights_only).keys()
5336 )
5338 # Check if we are in a special state, i.e. loading from a state dict coming from a different architecture

File /usr/local/lib/python3.11/site-packages/transformers/modeling_utils.py:562, in load_state_dict(checkpoint_file, is_quantized, map_location, weights_only)
561 if weights_only:
--> 562 check_torch_load_is_safe()
563 try:

File /usr/local/lib/python3.11/site-packages/transformers/utils/import_utils.py:1622, in check_torch_load_is_safe()
1621 if not is_torch_greater_or_equal("2.6"):
-> 1622 raise ValueError(
1623 "Due to a serious vulnerability issue in `torch.load`, even with `weights_only=True`, we now require users "
1624 "to upgrade torch to at least v2.6 in order to use the function. This version restriction does not apply "
1625 "when loading files with safetensors."
1626 "\nSee the vulnerability report here [https://nvd.nist.gov/vuln/detail/CVE-2025-32434](https://nvd.nist.gov/vuln/detail/CVE-2025-32434)"
1627 )

ValueError: Due to a serious vulnerability issue in `torch.load`, even with `weights_only=True`, we now require users to upgrade torch to at least v2.6 in order to use the function. This version restriction does not apply when loading files with safetensors.
See the vulnerability report here [https://nvd.nist.gov/vuln/detail/CVE-2025-32434](https://nvd.nist.gov/vuln/detail/CVE-2025-32434)

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last)
File /usr/local/lib/python3.11/site-packages/modelscope/utils/registry.py:209, in build_from_cfg(cfg, registry, group_key, default_args)
208 if hasattr(obj_cls, '_instantiate'):
--> 209 return obj_cls._instantiate(**args)
210 else:

File /usr/local/lib/python3.11/site-packages/modelscope/models/base/base_model.py:85, in Model._instantiate(cls, **kwargs)
80 """ Define the instantiation method of a model,default method is by
81 calling the constructor. Note that in the case of no loading model
82 process in constructor of a task model, a load_model method is
83 added, and thus this method is overloaded
84 """
---> 85 return cls(**kwargs)

File /usr/local/lib/python3.11/site-packages/adaseq/models/base.py:40, in Model.__init_subclass__.<locals>.new_init(self, init, *args, **kwargs)
39 def new_init(self, init=cls.__init__, *args, **kwargs):
---> 40 init(self, *args, **kwargs)
41 self.post_init()

File /usr/local/lib/python3.11/site-packages/adaseq/models/sequence_labeling_model.py:64, in SequenceLabelingModel.__init__(self, id_to_label, embedder, encoder, dropout, word_dropout, use_crf, multiview, temperature, mv_loss_type, mv_interpolation, partial, chunk, **kwargs)
63 else:
---> 64 self.embedder = Embedder.from_config(embedder)
65 hidden_size = self.embedder.get_output_dim()

File /usr/local/lib/python3.11/site-packages/adaseq/modules/embedders/base.py:64, in Embedder.from_config(cls, cfg_dict_or_path, **kwargs)
63 if cfg['type'] is not None and cfg['type'] in EMBEDDERS.modules['default']:
---> 64 return build_embedder(cfg, default_args=kwargs)
65 else:

File /usr/local/lib/python3.11/site-packages/adaseq/modules/embedders/base.py:24, in build_embedder(cfg, default_args)
15 """Build embedder from config dict
16
17 Args:
(...) 22 embedder (:obj:`Embedder`): an embedder instance
23 """
---> 24 return build_from_cfg(cfg, EMBEDDERS, group_key='default', default_args=default_args)

File /usr/local/lib/python3.11/site-packages/modelscope/utils/registry.py:214, in build_from_cfg(cfg, registry, group_key, default_args)
212 except Exception as e:
213 # Normal TypeError does not print class name.
--> 214 raise type(e)(f'{obj_cls.__name__}: {e}') from e

ValueError: TransformerEmbedder: Due to a serious vulnerability issue in `torch.load`, even with `weights_only=True`, we now require users to upgrade torch to at least v2.6 in order to use the function. This version restriction does not apply when loading files with safetensors.
See the vulnerability report here [https://nvd.nist.gov/vuln/detail/CVE-2025-32434](https://nvd.nist.gov/vuln/detail/CVE-2025-32434)

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last)
File /usr/local/lib/python3.11/site-packages/modelscope/utils/registry.py:211, in build_from_cfg(cfg, registry, group_key, default_args)
210 else:
--> 211 return obj_cls(**args)
212 except Exception as e:
213 # Normal TypeError does not print class name.

File /usr/local/lib/python3.11/site-packages/adaseq/training/default_trainer.py:67, in DefaultTrainer.__init__(self, cfg_file, work_dir, dataset_manager, data_collator, preprocessor, seed, device, **kwargs)
56 def __init__(
57 self,
58 cfg_file: str,
(...) 65 **kwargs,
66 ) -> None:
---> 67 super().__init__(
68 model=None,
69 cfg_file=cfg_file,
70 cfg_modify_fn=None,
71 data_collator=data_collator,
72 train_dataset=dataset_manager.train,
73 eval_dataset=dataset_manager.valid,
74 preprocessor=preprocessor,
75 work_dir=work_dir,
76 seed=seed,
77 device=device,
78 **kwargs,
79 )
81 # Setup testset if there is one

File /usr/local/lib/python3.11/site-packages/modelscope/trainers/trainer.py:175, in EpochBasedTrainer.__init__(self, model, cfg_file, cfg_modify_fn, arg_parse_fn, data_collator, train_dataset, eval_dataset, preprocessor, optimizers, model_revision, seed, callbacks, samplers, efficient_tuners, **kwargs)
174 else:
--> 175 self.model = self.build_model()
177 if self._compile:
178 # Compile the model with torch 2.0

File /usr/local/lib/python3.11/site-packages/adaseq/training/default_trainer.py:102, in DefaultTrainer.build_model(self)
99 """
100 Override this func to build adaseq `Model`.
101 """
--> 102 return Model.from_config(self.cfg)

File /usr/local/lib/python3.11/site-packages/adaseq/models/base.py:118, in Model.from_config(cls, cfg_dict_or_path, **kwargs)
114 raise ValueError(
115 'Please pass a correct cfg dict, which should be a reachable file or a dict.'
116 )
--> 118 model = build_model(model_config, task_name=task, default_args=kwargs)
119 cfg['framework'] = 'pytorch'

File /usr/local/lib/python3.11/site-packages/modelscope/models/builder.py:35, in build_model(cfg, task_name, default_args)
34 try:
---> 35 model = build_from_cfg(
36 cfg, MODELS, group_key=task_name, default_args=default_args)
37 except KeyError as e:
38 # Handle subtask with a backbone model that hasn't been registered
39 # All the subtask with a parent task should have a task model, otherwise it is not a
40 # valid subtask

File /usr/local/lib/python3.11/site-packages/modelscope/utils/registry.py:214, in build_from_cfg(cfg, registry, group_key, default_args)
212 except Exception as e:
213 # Normal TypeError does not print class name.
--> 214 raise type(e)(f'{obj_cls.__name__}: {e}') from e

ValueError: SequenceLabelingModel: TransformerEmbedder: Due to a serious vulnerability issue in `torch.load`, even with `weights_only=True`, we now require users to upgrade torch to at least v2.6 in order to use the function. This version restriction does not apply when loading files with safetensors.
See the vulnerability report here [https://nvd.nist.gov/vuln/detail/CVE-2025-32434](https://nvd.nist.gov/vuln/detail/CVE-2025-32434)

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last)
Cell In[3], line 8
5 work_dir = 'experiments/transformer_crf'
6 os.makedirs(work_dir, exist_ok=True)
----> 8 trainer = build_trainer_from_partial_objects(
9 config,
10 work_dir=work_dir,
11 seed=42,
12 device='cuda:0'
13 )
15 # do training
16 trainer.train()

File /usr/local/lib/python3.11/site-packages/adaseq/commands/train.py:218, in build_trainer_from_partial_objects(config, work_dir, **kwargs)
215 collator_config = dict(type=collator_config)
216 data_collator = build_data_collator(preprocessor.tokenizer, collator_config)
--> 218 trainer = build_trainer(
219 config.safe_get('train.trainer', Trainers.default_trainer),
220 cfg_file=new_config_path,
221 work_dir=work_dir,
222 dataset_manager=dm,
223 data_collator=data_collator,
224 preprocessor=preprocessor,
225 **kwargs,
226 )
227 return trainer

File /usr/local/lib/python3.11/site-packages/adaseq/training/default_trainer.py:176, in build_trainer(name, **kwargs)
173 if 'WORLD_SIZE' in os.environ and int(os.environ['WORLD_SIZE']) > 1:
174 kwargs.update(launcher='pytorch', device='gpu')
--> 176 trainer = ms_build_trainer(name, kwargs)
177 return trainer

File /usr/local/lib/python3.11/site-packages/modelscope/trainers/builder.py:39, in build_trainer(name, default_args)
36 register_plugins_repo(configuration.safe_get('plugins'))
37 register_modelhub_repo(model_dir,
38 configuration.get('allow_remote', False))
---> 39 return build_from_cfg(cfg, TRAINERS, default_args=default_args)

File /usr/local/lib/python3.11/site-packages/modelscope/utils/registry.py:214, in build_from_cfg(cfg, registry, group_key, default_args)
211 return obj_cls(**args)
212 except Exception as e:
213 # Normal TypeError does not print class name.
--> 214 raise type(e)(f'{obj_cls.__name__}: {e}') from e

ValueError: DefaultTrainer: SequenceLabelingModel: TransformerEmbedder: Due to a serious vulnerability issue in `torch.load`, even with `weights_only=True`, we now require users to upgrade torch to at least v2.6 in order to use the function. This version restriction does not apply when loading files with safetensors.
See the vulnerability report here [https://nvd.nist.gov/vuln/detail/CVE-2025-32434](https://nvd.nist.gov/vuln/detail/CVE-2025-32434)