3 ïN•b¶ã@svddlZddlZddlZdddgZGdd„deƒZejddgƒZejdd d dgƒZGd d„deƒZ Gd d„deƒZ dS) éNÚ FutureResultÚ SlavePipeÚ SyncMasterc@s(eZdZdZdd„Zdd„Zdd„ZdS) rzBA thread-safe future implementation. Used only as one-to-one pipe.cCs"d|_tjƒ|_tj|jƒ|_dS)N)Ú_resultÚ threadingÚLockÚ_lockÚ ConditionÚ_cond)Úself©r úG/home/ec2-user/SageMaker/lama/models/ade20k/segm_lib/nn/modules/comm.pyÚ__init__s zFutureResult.__init__c Cs8|j�(|jdkstdƒ‚||_|jjƒWdQRXdS)Nz#Previous result has't been fetched.)rrÚAssertionErrorr Únotify)r Úresultr r r ÚputszFutureResult.putc Cs6|j�&|jdkr|jjƒ|j}d|_|SQRXdS)N)rrr Úwait)r Úresr r r Úget s   zFutureResult.getN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrr r r r rsZMasterRegistryrÚ_SlavePipeBaseÚ identifierÚqueuec@seZdZdZdd„ZdS)rz$Pipe for master-slave communication.cCs,|jj|j|fƒ|jjƒ}|jjdƒ|S)NT)rrrrr)r ÚmsgÚretr r r Ú run_slave1s  zSlavePipe.run_slaveN)rrrrrr r r r r.sc@s4eZdZdZdd„Zdd„Zdd„Zedd „ƒZd S) ra An abstract `SyncMaster` object. - During the replication, as the data parallel will trigger an callback of each module, all slave devices should call `register(id)` and obtain an `SlavePipe` to communicate with the master. - During the forward pass, master device invokes `run_master`, all messages from slave devices will be collected, and passed to a registered callback. - After receiving the messages, the master device should gather the information and determine to message passed back to each slave devices. cCs$||_tjƒ|_tjƒ|_d|_dS)z‚ Args: master_callback: a callback to be invoked after having collected messages from slave devices. FN)Ú_master_callbackrÚQueueÚ_queueÚ collectionsÚ OrderedDictÚ _registryÚ _activated)r Zmaster_callbackr r r rCs  zSyncMaster.__init__cCsJ|jr(|jjƒstdƒ‚d|_|jjƒtƒ}t|ƒ|j|<t||j|ƒS)zÜ Register an slave device. Args: identifier: an identifier, usually is the device id. Returns: a `SlavePipe` object which can be used to communicate with the master device. z.Queue is not clean before next initialization.F) r&r"Úemptyrr%ÚclearrÚ_MasterRegistryr)r rÚfuturer r r Úregister_slaveNs  zSyncMaster.register_slavecCs¶d|_d|fg}x"t|jƒD]}|j|jjƒƒqW|j|ƒ}|dddksVtdƒ‚x,|D]$\}}|dkrnq\|j|j j |ƒq\Wx$t|jƒD]}|jjƒdks�t‚q�W|ddS)ad Main entry for the master device in each forward pass. The messages were first collected from each devices (including the master device), and then an callback will be invoked to compute the message to be sent back to each devices (including the master device). Args: master_msg: the message that the master want to send to itself. This will be placed as the first message when calling `master_callback`. For detailed usage, see `_SynchronizedBatchNorm` for an example. Returns: the message to be sent back to the master device. Trz.The first result should belongs to the master.é) r&ÚrangeÚ nr_slavesÚappendr"rr rr%rr)r Z master_msgÚ intermediatesÚiÚresultsrr r r Ú run_master`s  zSyncMaster.run_mastercCs t|jƒS)N)Úlenr%)r r r r r.�szSyncMaster.nr_slavesN) rrrrrr+r3Úpropertyr.r r r r r8s   !) rr#rÚ__all__ÚobjectrÚ namedtupler)rrrr r r r Ú s