o
    * i6                     @   s   d dl Zd dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZ dZdZdZd	Zd
Zd
ZdZG dd deZdddZdd Zedkrcd dlZedZedZede de d e  dS dS )    N)fleet)
DataLoaderDataset)ResNet)BottleneckBlockg?g?g-C6?      f   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )RandomDatasetc                 C   s
   || _ d S Nnum_samples)selfr    r   r/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/distributed/launch/plugins/test.py__init__#   s   
zRandomDataset.__init__c                 C   s8   t jg dd}t jdtd dd}||fS )N)r      r   Zfloat32r   r   )r   Zint64)nprandomZastyperandint	class_dim)r   idximagelabelr   r   r   __getitem__&   s   zRandomDataset.__getitem__c                 C   s   | j S r   r   )r   r   r   r   __len__+   s   zRandomDataset.__len__N)__name__
__module____qualname__r   r   r   r   r   r   r   r
   "   s    r
   c                 C   s    t jjttt jt| d}|S )N)Zlearning_rateZmomentumZweight_decay
parameters)paddle	optimizerZMomentumbase_lrmomentum_rateZregularizerZL2Decayl2_decay)parameter_listr!   r   r   r   optimizer_setting/   s   
r&   c                  C   s4  t jdd ttdtd} t|  d}t |}t | } t	t
t }t|tdddd}td ttD ]^}|   t| D ]R\}}|\}}d|_| |}	tjjj|	|d	}
tj|
d
}tjj|	|dd}tjj|	|dd}|  |  |   td| d| d|dd|dd|d
 q@q5td d S )NT)Zis_collective   )Znum_classes)r%      )
batch_sizeshuffleZ	drop_lastZnum_workerszDistributed training start...)inputr   )xr   )r+   r   k   z[Epoch z, batch z] loss: z.5fz, acc1: z, acc5: zDistributed training completed)r   initr   r   r   r&   r   Zdistributed_optimizerZdistributed_modelr
   	batch_numr)   r   printrangeepochtrain	enumerateZstop_gradientr    nnZ
functionalZcross_entropymeanZmetricZaccuracyZbackwardstepZclear_gradients)Zresnetr!   ZdatasetZtrain_loaderZeopZbatch_iddataimgr   outZlossZavg_lossZacc_top1Zacc_top5r   r   r   train_resnet9   s@   

&r<   __main__ZPADDLE_NNODESZPADDLE_LOCAL_SIZEz"Prepare distributed training with z nodes z cardsr   )numpyr   r    Zpaddle.distributedr   Z	paddle.ior   r   Zpaddle.vision.modelsr   Zpaddle.vision.models.resnetr   r"   r#   r$   r3   r0   r)   r   r
   r&   r<   r   osgetenvZnnodesZcnr1   r   r   r   r   <module>   s.   

*


