L ieZ2ddlZddlZddlZddlmZddlmZddlmZddlZddl m Z  ddl ZGddZ Gdd eZeZd Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZd dZdZ dZ!dZ"dZ#dZ$dZ%dZ&y#e $rYgwxYw)!N) reduction)register_after_fork)Union)check_serializing_named_tensorcFeZdZdZddgZdZedZdZdZ dZ d Z y ) StorageWeakRefzA weak reference to a Storage. The cdata member is a Python number containing the integer representation of the Storage pointer. cdata_free_weak_refcl|j|_tjj|_yN) _weak_refr torchStorager )selfstorages f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/multiprocessing/reductions.py__init__zStorageWeakRef.__init__!s&&&( $mm::ct|j|}||_tjj|_|Sr )__new__r rrr )clsr instances r from_weakrefzStorageWeakRef.from_weakref's.;;s#"'--">">rcTtjj|jSr )rr_expiredr rs rexpiredzStorageWeakRef.expired.s}}%%djj11rc:|j|jyr )r r rs r__del__zStorageWeakRef.__del__1s DJJ'rc|jSr )r rs r__hash__zStorageWeakRef.__hash__4s zzrcdt|t|k(ry|j|jk(S)NT)idr )rothers r__eq__zStorageWeakRef.__eq__7s( d8r%y zzU[[((rN) __name__ __module__ __qualname____doc__ __slots__r classmethodrrrr!r%rrrrsA *+I;  2()rrc0eZdZdZddZdZdZdZdZy) SharedCachez:Dictionary from multiprocessing handles to StorageWeakRef.Ncfd|_|jt|tjy)N)limit _after_forkrr.rs rrzSharedCache.__init__@s)  D+"9"9:rc6tj|_yr ) threadingLocklockrs rr2zSharedCache._after_forkJsNN$ rcr|j5tj||cdddS#1swYyxYwr )r6dictget)rkeys rr9zSharedCache.getMs, YY '88D#& ' ' 's-6c|j5tj|||t||jkDr|j dddy#1swYyxYwr )r6r8 __setitem__lenr1free_dead_references)rr: storage_refs rr<zSharedCache.__setitem__QsL YY ,   T3 44y4::%))+ , , ,s AAAcd}t|jD]\}}|jr||=|dz } td|dz|_y)Nrr0)listitemsrmaxr1)rliver:r?s rr>z SharedCache.free_dead_referencesWsV $TZZ\ 2  C""$I    dQh' r)returnN) r&r'r(r)rr2r9r<r>r,rrr.r.=sD;%', (rr.cVtjjj||Sr )rcudaEventfrom_ipc_handle)devicehandles r rebuild_eventrNes ::   + +FF ;;rcJ|j}t|j|ffSr ) ipc_handlerNrL)eventrMs r reduce_eventrRis$    F ELL&1 22rc|\}}}}tjj||||}|tjjj k(r-tjjj ||}|S||_|S)N requires_grad)r_utils_rebuild_tensornn parameter ParameterrU)rrmetadatastorage_offsetsizestriderUts rrebuild_tensorr`nsx2:/ND&- $$WndFKA ehh  *** HH   ( (- ( H H( Hrc^tj|d}tj||d}tjj ||||} |tj j jk(r-tj j j| |} | S|| _| S)NmetarLT wrap_storagedtype _internalrT) rUntypedStorage TypedStoragerVrWrXrYrZrU) tensor_cls tensor_size tensor_stride tensor_offsetrfstorage_size_bytesrUuntyped_storage typed_storager_s rrebuild_meta_tensorrq{s**+=fMO&&$ETM  $$  AUXX''111 HH   ( (- ( H H( Hrc ||dk(r |d||d}not||| f}|Ktjj|j |||| | | | |}t |t || f<n|j| | |t|tjr|n |j}tjjtjj||d|||}|tjj j"k(r-tjj j#|| }|S| |_|S)NrT)rfrLrgrcrdrT)storage_from_cacherrI _lazy_init_new_shared_cudar shared_cache_release_ipc_counter isinstancerh_untyped_storagerVrWrrirXrYrZrU)rjrkrlrm storage_clsrfstorage_devicestorage_handlernstorage_offset_bytesrUref_counter_handleref_counter_offset event_handleevent_sync_requiredr_storager_s rrebuild_cuda_tensorrs^$!3q!8au^tT$ .*>?  ? JJ ! ! #!22"$""# GDRDL.*>? @  , ,"$6~ -  gu33 4   % %  $$ ""QU"V  AUXX''111 HH   ( (- ( H H( Hrc|jr|js tdt|tj j j|ddlm }|jrt||s t|S|jtjtjtj tj"tj$hvr t'|S|j)}|j*j,j.dk(r|j1\}}}}}}} } |j3} t5|t6|<t8t/||j;|j=| t/||j>|||||j||| | ffS|j*j,j.dk(rrt@t/||j;|j=|j3|j>|jCj;|jffS|j3|j;|j=|jf} tDt/||| ffS)NzCowardly refusing to serialize non-leaf tensor which requires_grad, since autograd does not support crossing process boundaries. If you just want to transfer the data, call detach() on the tensor before serializing (e.g., putting it on the queue).r) NestedTensorrIrb)#rUis_leaf RuntimeErrorrrutilshookswarn_if_has_hooks$torch.nested._internal.nested_tensorr is_nestedrxreduce_nested_tensorlayout sparse_coo sparse_csr sparse_bsr sparse_csc sparse_bscreduce_sparse_tensor_typed_storageryrLtype _share_cuda_r\rrvrr]r^rfrqror`) tensorrrrLrMrnr}r~rrrrmr[s r reduce_tensorrsD FNN B  #6* KK''/~B  6< @#F++ }}      $F++##%G&&++v5  "       --/ -g6 V V   W  "$$$""#   (  ! ! ( ( - - 7 V   %%' &&(--/$$       H T&\7H= >>rcZ||}||} ||} ||} tj|| | | Sr )r_nested_view_from_buffer_copy) rebuild_buffer_funcrebuild_buffer_argsrebuild_sizes_funcrebuild_sizes_argsrebuild_strides_funcrebuild_strides_argsrebuild_offsets_funcrebuild_offsets_argsbuffersizesstridesoffsetss rrebuild_nested_tensorrsG!"5 6F  2 3E"$89G"$89G  . .vugw OOrc t|j\}}t|j\}}t|j\}}t|j \}}t ||||||||ffSr )rvalues_nested_tensor_size_nested_tensor_strides_nested_tensor_storage_offsetsr) ntrrrrrrrrs rrrs/ !!#2..2? ))+2..         rcH||}||}tj||||S)N) is_coalesced)rsparse_coo_tensor)rebuild_indices_funcrebuild_indices_argsrebuild_values_funcrebuild_values_argsshaperindicesrs rrebuild_sparse_coo_tensorrs1#$89G "5 6F  " "7FE UUrcT||}||} ||} tj|| | ||S)N)r)rsparse_compressed_tensor) rebuild_compressed_indices_funcrebuild_compressed_indices_argsrebuild_plain_indices_funcrebuild_plain_indices_argsrrrrcompressed_indices plain_indicesrs r rebuild_sparse_compressed_tensorrsH9 (/0JKM "5 6F  ) )M65 rc  |jtjur^t|j \}}t|j \}}t |||||j|jffS|jtjtjhvr!|j}|j}nb|jtjtjhvr!|j}|j!}nt#|jt|\}}t|\} } t|j%\}}t&||| | |||j|jffSr )rrrr_indices_valuesrrrrr crow_indices col_indicesrr ccol_indices row_indicesNotImplementedErrorrr) sparserrrrrrrrrrs rrrsq }}(((5B6??CT5U223@AQ3R00 %$$## ##%     ==U--u/?/?@ @!'!4!4!6 "..0M ]]u//1A1AB B!'!4!4!6 "..0M%fmm4 4 , - + +AN B >"$>4A3Q00 ,//**##     rc\tj|}|j|jfSr )osfstatst_inost_dev)fdstats rfd_idr s$ 88Bt|j|jffSr )rryrfrs rreduce_typed_storagerFs !G$<$Cannot pickle CUDA storage; try pickling a CUDA tensor insteadrbz>Cannot pickle meta storage; try pickling a meta tensor instead file_systemr)ris_cudarrLr_share_filename_cpu_rrxrrirf_shared_increfr]r_share_fd_cpu_multiprocessingrDupFdrrrrv)rrr[ cache_keyrebuildrr]rs rreduce_storagerSs& L      & L    = 0//1QK * gu11 2 ( (H 1 &W '788))+D  & & , ,R 0"I :$,W5L d7m%0 11rcltjtjjt tj D]F}|jdk(rtj|t-tj|tHtjtjjttjD]}tj|ttjtjtddlm}tj|ty)Nrhr)rZ)rregisterrrIrJrR_storage_classesr&rrrrir_tensor_classesrTensortorch.nn.parameterrZ)r_rZs rinit_reductionsrts uzz''6  # #> ::) )   q. 1   q"< = > u}}113GH  " "-1m,-u||]3, y-0rr )'rrr4rmultiprocessing.utilrtypingrrtorch._namedtensor_internalsrmultiprocessing.resource_sharer ImportErrorrr8r.rvrNrRr`rqrrrrrrrrrsrrrrrrrrrr,rrrs %4 G + ")")J!($!(J} <3   @A Hq?hP"2 V(, ^&F $(Y N> T2B1A  sBBB