ó R;]c@sdZdddddgZddlZddlZddlZyddlZWn-ek r~defd „ƒYZeZnXddl Z d d l m Z d e d „Zd e d„Zd„Zd„Zd„Zdedd„Zd„Zd„Zdd„ZdS(s"Parallelization utility optimizer.t split_datatsplit_and_loadtclip_global_normt check_sha1tdownloadi’’’’Ntrequests_failed_to_importcBseZRS((t__name__t __module__(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.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((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyR&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(Rtctx_listRRRRR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyRWs cCsāt|ƒdkst‚|dj}tjgd„|DƒD]!}tj||ƒj|ƒ^q<Œ}tj|ƒjƒ}t j |ƒs¦t j t dƒddƒn||d}|dkrŽx|D]}||9}qĒWn|S(sRRescales NDArrays so that the sum of their 2-norm is smaller than `max_norm`. icss|]}|jdƒVqdS(i’’’’N(i’’’’(treshape(t.0tarr((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pys zss;nan or inf is detected. Clipping results will be undefined.t stacklevelig:Œ0āŽyE>gš?(RtAssertionErrortcontextRtadd_ntdotRtsqrttasscalartnptisfinitetwarningstwarnt UserWarning(tarraystmax_normRtxt total_normtscaleR#((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyRts  8   cCso|jdƒ}t|ƒdkr%|S|jdƒ}|gg|D]}|d|^qA}dj|ƒ}|S(sIndent string s iit (RRtpoptjoin(ts_t numSpaceststfirsttline((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.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_hashR@tfR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyR’s  c Cs²|d kr"|jdƒd}nOtjj|ƒ}tjj|ƒrktjj||jdƒdƒ}n|}|s tjj|ƒ s |r®t||ƒ r®tjj tjj tjj|ƒƒƒ}tjj|ƒsģtj |ƒnd||fGHt j |dtƒ}|jdkr2td|ƒ‚nt|dƒ:}x0|jdd ƒD]}|rW|j|ƒqWqWWWd QX|r®t||ƒ r®td j|ƒƒ‚q®n|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_sizeiNs»File {} is downloaded but the content hash does not match. The repo may be outdated or download may be incomplete. If the "repo_url" is overridden, consider switching to the default repo.(tNoneRtostpatht expandusertisdirR7texistsRtdirnametabspathtmakedirstrequeststgetRBt status_codet RuntimeErrorRAt iter_contenttwriteR/tformat( turlROt overwriteRGtfnameRStrRHtchunk((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyR¬s, %/* cCs<d}tjjd|ƒ}|ddkr8|d}n|S(s;Return the base URL for Gluon dataset and model repository.s;https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/tMXNET_GLUON_REPOi’’’’RI(RNtenvironRW(t default_repotrepo_url((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyt _get_repo_urląs  cCsdjdtƒd|d|ƒS(s¶Return the URL for hosted file in Gluon repository. Parameters ---------- namespace : str Namespace of the file. filename : str Name of the file s {base_url}{namespace}/{filename}tbase_urlt namespaceRF(R\Rf(RhRF((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyt_get_repo_file_urlčs icCskt|ƒ|krAt||d |ƒdt|| d|ƒSdjg|D]}dt|ƒ^qNƒS(s'Print at most `limit` elements of list.is, ..., s, s'%s'(Rt_brief_print_listR7R (tlsttlimitR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyRjös(t__doc__t__all__RNR?R-RVt ImportErrortobjectRtnumpyR+tRRBRRRR=RRMtFalseRRfRiRj(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.2.1-py2.7.egg/mxnet/gluon/utils.pyts*        1  4