Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add token ips #1161

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions paddlemix/examples/llava/pretrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,10 @@ def main():
checkpoint = last_checkpoint
train_result = trainer.train(resume_from_checkpoint=checkpoint)
if training_args.benchmark:

def get_paddle_memory_info():
"""get_memory_info"""
divisor = 2**30
return (
paddle.device.cuda.memory_allocated() / divisor,
paddle.device.cuda.max_memory_allocated() / divisor,
paddle.device.cuda.memory_reserved() / divisor,
paddle.device.cuda.max_memory_reserved() / divisor,
)

memory_allocated, max_memory_allocated, memory_reserved, max_memory_reserved = get_paddle_memory_info()

logger.info(
f"memory_allocated:{memory_allocated}GB, max_memory_allocated: {max_memory_allocated}GB, memory_reserved:{memory_reserved}GB, max_memory_reserved: {max_memory_reserved}GB \n"
)
total_effective_samples = total_samples * training_args.num_train_epochs
effective_samples_per_second = total_effective_samples / train_result.metrics["train_runtime"]
mem_gpu = (
train_result.metrics["train_mem_gpu_peaked_delta"] + train_result.metrics["train_mem_gpu_alloc_delta"]
)
logger.info(f"ips: {effective_samples_per_second} ")
logger.info(f"train_mem_gpu_peaked: {int(mem_gpu/ (2**20))} MB")
logger.info(f"Effective_samples_per_second: {effective_samples_per_second} ")
logger.info(f"Train_runtime: {train_result.metrics['train_runtime']}")
logger.info("Benchmark done.")
else:
trainer.save_model(merge_tensor_parallel=training_args.tensor_parallel_degree > 1)
Expand Down
25 changes: 3 additions & 22 deletions paddlemix/examples/llava/supervised_finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,11 @@ def main():
checkpoint = last_checkpoint
train_result = trainer.train(resume_from_checkpoint=checkpoint)
if training_args.benchmark:

def get_paddle_memory_info():
"""get_memory_info"""
divisor = 2**30
return (
paddle.device.cuda.memory_allocated() / divisor,
paddle.device.cuda.max_memory_allocated() / divisor,
paddle.device.cuda.memory_reserved() / divisor,
paddle.device.cuda.max_memory_reserved() / divisor,
)

memory_allocated, max_memory_allocated, memory_reserved, max_memory_reserved = get_paddle_memory_info()

logger.info(
f"memory_allocated:{memory_allocated}GB, max_memory_allocated: {max_memory_allocated}GB, memory_reserved:{memory_reserved}GB, max_memory_reserved: {max_memory_reserved}GB \n"
)

total_effective_samples = total_samples * training_args.num_train_epochs
effective_samples_per_second = total_effective_samples / train_result.metrics["train_runtime"]
mem_gpu = (
train_result.metrics["train_mem_gpu_peaked_delta"] + train_result.metrics["train_mem_gpu_alloc_delta"]
)
logger.info(f"ips: {effective_samples_per_second} ")
logger.info(f"train_mem_gpu_peaked: {int(mem_gpu/ (2**20))} MB")

logger.info(f"Effective_samples_per_second: {effective_samples_per_second} ")
logger.info(f"Train_runtime: {train_result.metrics['train_runtime']}")
logger.info("Benchmark done.")
else:
trainer.save_model(merge_tensor_parallel=training_args.tensor_parallel_degree > 1)
Expand Down
10 changes: 10 additions & 0 deletions paddlemix/models/llava/language_model/llava_llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def forward(
input_ids, position_ids, attention_mask, past_key_values, labels, images, image_size
)

# 通过attention_mask计算有效token数量
if attention_mask is not None:
# 统计当前batch的有效token数(排除padding)
current_batch_tokens = attention_mask.sum().item() # shape: (batch_size, seq_len)
else:
# 如果没有padding,直接取inputs_embeds的batch_size*seq_length
current_batch_tokens = inputs_embeds.size(0) * inputs_embeds.size(1)
self.efficient_token_count = current_batch_tokens
self.input_shape = inputs_embeds.shape

return super().forward(
input_ids=input_ids,
attention_mask=attention_mask,
Expand Down
Loading