from transformers import BertTokenizer, BertForSequenceClassification, TrainingArguments, Trainer from datasets import load_from_disk import numpy as np from sklearn.metrics import accuracy_score def load_processed_data(): dataset = load_from_disk("../data/processed_imdb") return dataset['train'], dataset['test'] # 定义预处理函数 def preprocess_function(examples): return tokenizer( examples['text'], truncation=True, padding='max_length', max_length=256 ) # 定义评估指标 def compute_metrics(p): preds = np.argmax(p.predictions, axis=1) return {'accuracy': accuracy_score(p.label_ids, preds)} if __name__ == '__main__': train_data, test_data = load_processed_data() # 加载BERT的分词器 tokenizer = BertTokenizer.from_pretrained("C:/Users/Administrator/bert_cache") # 应用预处理 train_data = train_data.map(preprocess_function, batched=True) test_data = test_data.map(preprocess_function, batched=True) # 重命名标签列(适配HuggingFace格式) train_data = train_data.rename_column('label', 'labels') test_data = test_data.rename_column('label', 'labels') # 设置pytorch格式 train_data.set_format('torch', columns=['input_ids', 'attention_mask', 'labels']) test_data.set_format('torch', columns=['input_ids', 'attention_mask', 'labels']) # 加载预训练模型 model = BertForSequenceClassification.from_pretrained( "C:/Users/Administrator/bert_cache", num_labels=2 ) training_args = TrainingArguments( output_dir='./results', # 输出目录 num_train_epochs=3, # 训练轮次 per_device_train_batch_size=32, # 训练批次大小 per_device_eval_batch_size=128, # 评估批次大小 warmup_steps=500, # 学习率预热步数 weight_decay=0.01, # 权重衰减 logging_dir='./logs', # 日志目录 eval_strategy='epoch', # 每轮评估一次 save_strategy='epoch' ) # 创建Trainer trainer = Trainer( model=model, args=training_args, train_dataset=train_data, eval_dataset=test_data, # 实际应用时应使用验证集 compute_metrics=compute_metrics ) # 开始训练! trainer.train() # 保存模型 model.save_pretrained('./my_bert_model') tokenizer.save_pretrained('./my_bert_model') # 加载模型 # from_pretrained_model = BertForSequenceClassification.from_pretrained('./my_bert_model') 我这段代码我怎么判断他是否使用了GPU加速
时间: 2025-04-03 11:09:55 浏览: 41
<think>嗯,用户想知道如何判断他们的代码是否使用了GPU加速。首先,我需要回顾一下他们提供的代码。看起来他们使用的是Hugging Face的Transformers库和Trainer类进行训练。代码中没有明显设置设备的命令,比如`torch.cuda.is_available()`或者`model.to('cuda')`,所以可能依赖Trainer自动处理。
接下来,我要考虑Hugging Face的Trainer默认行为。通常,如果没有指定,Trainer会自动使用GPU如果可用。但用户的代码里没有明确设置,所以需要确认几个点:
1. **库的日志输出**:在训练开始的时候,Transformers通常会输出是否检测到CUDA,并正在使用GPU。用户可能需要在控制台查看这些信息,比如“Using CUDA”或者列出可用的GPU设备。
2. **PyTorch的设备设置**:虽然代码中没有显式调用,但PyTorch可能在底层自动使用GPU。用户可以通过在代码中添加检查语句,比如`import torch; print(torch.cuda.is_available())`和`print(torch.cuda.device_count())`,来确认CUDA是否可用以及GPU数量。
3. **训练速度**:如果训练速度很快,尤其是批次大小较大时,可能是在GPU上运行。但这种方法不太可靠,因为速度受多种因素影响。
4. **任务管理器或nvidia-smi**:用户可以在训练时打开任务管理器(Windows)或使用`nvidia-smi`命令(Linux)查看GPU使用情况。如果有显存占用和计算活动,说明正在使用GPU。
然后,我需要考虑用户可能的隐藏需求。他们可能刚接触深度学习,对GPU配置不熟悉,或者遇到训练速度慢的问题,想知道是否真的在使用GPU。因此,除了判断方法,可能还需要建议如何强制使用CPU或GPU,以及如何修改代码。
比如,在TrainingArguments中设置`no_cuda=True`可以强制使用CPU。或者,通过`device`参数指定,但Hugging Face的Trainer可能自动处理这些。另外,确保PyTorch正确安装了CUDA版本的库也是关键。
总结回答的结构:先说明检查日志输出,然后代码中添加CUDA检查,接着观察资源使用情况,最后提供修改建议。确保步骤清晰,帮助用户逐步验证。</think>要判断你的代码是否使用了GPU加速,可以通过以下几个步骤进行验证:
$\textcolor{green}{\textbf{1. 检查日志输出}}$
在代码运行时,控制台会输出类似以下信息:
```python
Using cuda device: NVIDIA GeForce RTX 3090 # 如果使用GPU会显示设备名称
```
或警告信息:
```python
CUDA is not available. Using CPU. # 如果未检测到GPU
```
$\textcolor{red}{注意:}$ 如果没有显式设置设备,HuggingFace Trainer会$\textcolor{blue}{自动优先使用GPU}$。
$\textcolor{green}{\textbf{2. 添加设备检测代码}}$
在`if __name__ == '__main__':`块开头添加:
```python
import torch
print("CUDA可用:", torch.cuda.is_available()) # 应该输出True
print("GPU数量:", torch.cuda.device_count()) # 应该≥1
print("当前设备:", torch.cuda.current_device())
print("设备名称:", torch.cuda.get_device_name(0))
```
$\textcolor{red}{预期结果:}$
```python
CUDA可用: True
GPU数量: 1
当前设备: 0
设备名称: NVIDIA GeForce RTX 3090
```
$\textcolor{green}{\textbf{3. 监控资源使用}}$(二选一)
- $\textcolor{blue}{\text{Windows}}$:任务管理器 → 性能 → GPU 查看使用率
- $\textcolor{blue}{\text{Linux}}$:终端运行 `watch -n 0.5 nvidia-smi` 实时监控
$\textcolor{green}{\textbf{4. 强制使用CPU验证}}$(可选)
修改`TrainingArguments`添加参数:
```python
training_args = TrainingArguments(
no_cuda=True, # ← 添加此参数强制使用CPU
output_dir='./results',
...
)
```
此时若GPU相关代码报错,则说明原本在使用GPU
$\textcolor{green}{\textbf{5. 显存占用验证}}$
正常使用GPU时会出现以下现象:
- 训练开始时会显示显存分配信息
- 使用`nvidia-smi`会看到对应进程显存占用
- 训练速度明显快于纯CPU(32批大小下文本分类约3-5秒/step)
$\textcolor{red}{\textbf{常见问题排查:}}$
1. 如果显示`CUDA可用: False`:
- 检查是否安装GPU版PyTorch:`print(torch.__version__)`应包含`cuXXX`
- 更新NVIDIA驱动
- 确认CUDA版本匹配(`nvcc --version`与`torch.version.cuda`)
2. 如果显存不足(OOM错误):
```python
training_args = TrainingArguments(
fp16=True, # 启用混合精度训练 ← 添加此参数
per_device_train_batch_size=16 # 减小批次大小
)
```
$\textcolor{blue}{\textbf{最终确认方法:}}$
在训练循环中添加回调函数:
```python
from transformers import TrainerCallback
class DeviceCheckCallback(TrainerCallback):
def on_train_begin(self, args, state, control, **kwargs):
print(f"Training device: {kwargs['model'].device}")
# 在Trainer中添加:
trainer = Trainer(
...,
callbacks=[DeviceCheckCallback()] # ← 添加回调
)
```
正常使用GPU时会输出:
```python
Training device: cuda:0
```
阅读全文