ó šÄďYc@sÉdZddlZddlZyddlZWn-ek r]defd„ƒYZeZnXddlZddlm Z de d„Z de d „Z d „Z d „Zd „Zdedd „ZdS(s"Parallelization utility optimizer.i˙˙˙˙Ntrequests_failed_to_importcBseZRS((t__name__t __module__(((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pyRsi(tndarrayicCsy|j|}||kr>tdt|jƒ||fƒ‚n|r|||dkr|tdt|jƒ|||fƒ‚n||}|dkrčgt|ƒD]@}||dkrÎ||||d|!n||||!^qŸ}n|r tj|d|d|ƒ}nigt|ƒD]V}||dkrStj|||||d|ƒntj|||||ƒ^q}|S(s°Splits an NDArray into `num_slice` slices along `batch_axis`. Usually used for data parallelism where each slices is sent to one device (i.e. GPU). Parameters ---------- data : NDArray A batch of data. num_slice : int Number of desired slices. batch_axis : int, default 0 The axis along which to slice. even_split : bool, default True Whether to force all slices to have the same number of elements. If `True`, an error will be raised when `num_slice` does not evenly divide `data.shape[batch_axis]`. Returns ------- list of NDArray Return value is a list even if `num_slice` is 1. sUToo many slices for data with shape %s. Arguments are num_slice=%d and batch_axis=%d.isŹdata with shape %s cannot be evenly split into %d slices along axis %d. Use a batch size that's multiple of %d or set even_split=False to allow uneven partitioning of data.it num_outputstaxis(tshapet ValueErrortstrtrangeRtsplitt slice_axis(tdatat num_slicet batch_axist even_splittsizetsteptitslices((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pyt split_data"s$  "  SfcCsžt|tjƒs.tj|d|dƒ}nt|ƒdkrT|j|dƒgSt|t|ƒ||ƒ}gt||ƒD]\}}|j|ƒ^qS(sSplits an NDArray into `len(ctx_list)` slices along `batch_axis` and loads each slice to one context in `ctx_list`. Parameters ---------- data : NDArray A batch of data. ctx_list : list of Context A list of Contexts. batch_axis : int, default 0 The axis along which to slice. even_split : bool, default True Whether to force all slices to have the same number of elements. Returns ------- list of NDArray Each corresponds to a context in `ctx_list`. tctxii(t isinstanceRtNDArraytarraytlent as_in_contextRtzip(R tctx_listRRRRR((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pytsplit_and_loadSs cCsĽt|ƒdkst‚d}x3|D]+}|jdƒ}|tj||ƒ7}q%Wtj|jƒƒ}||d}|dkrĄx|D]}||9}qŠWn|S(sRRescales NDArrays so that the sum of their 2-norm is smaller than `max_norm`. ii˙˙˙˙g:Œ0âŽyE>gđ?(i˙˙˙˙(RtAssertionErrortreshapeRtdottmathtsqrttasscalar(tarraystmax_normt total_normtarrtscale((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pytclip_global_normps   cCso|jdƒ}t|ƒdkr%|S|jdƒ}|gg|D]}|d|^qA}dj|ƒ}|S(sIndent string s iit (R Rtpoptjoin(ts_t numSpaceststfirsttline((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pyt_indent€s(cCsgtjƒ}t|dƒ:}x0trP|jdƒ}|s@Pn|j|ƒq!WWdQX|jƒ|kS(s?Check whether the sha1 hash of the file content matches the expected hash. Parameters ---------- filename : str Path to the file. sha1_hash : str Expected sha1 hash in hexadecimal digits. Returns ------- bool Whether the file content matches the expected hash. trbiN(thashlibtsha1topentTruetreadtupdatet hexdigest(tfilenamet sha1_hashR5tfR ((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pyt check_sha1Œs  c Csr|d kr"|jdƒd}n=tjj|ƒrYtjj||jdƒdƒ}n|}|sŽtjj|ƒ sŽ|rnt||ƒ rntjjtjj tjj |ƒƒƒ}tjj|ƒsÚtj |ƒnd||fGHt j |dtƒ}|jdkr td|ƒ‚nt|dƒ:}x0|jdd ƒD]}|rE|j|ƒqEqEWWd QXn|S( sCDownload an given URL Parameters ---------- url : str URL to download path : str, optional Destination path to store downloaded file. By default stores to the current directory with same name as in url. overwrite : bool, optional Whether to overwrite destination file if already exists. sha1_hash : str, optional Expected sha1 hash in hexadecimal digits. Will ignore existing file when hash is specified but doesn't match. Returns ------- str The file path of the downloaded file. t/i˙˙˙˙sDownloading %s from %s...tstreamiČsFailed downloading url %stwbt chunk_sizeiN(tNoneR tostpathtisdirR,texistsR>tdirnametabspatht expandusertmakedirstrequeststgetR7t status_codet RuntimeErrorR6t iter_contenttwrite( turlREt overwriteR<tfnameRHtrR=tchunk((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pytdownloadŚs$ %/*(t__doc__RDR4RLt ImportErrortobjectRR!tRR7RRR)R2R>RCtFalseRW(((s1build/bdist.linux-armv7l/egg/mxnet/gluon/utils.pyts     1