B e]@s ddlmZmZddlZddlmZddlZyddlZddlmZWn e k rddZddZYnXye Wne k re Z YnXye Wne k re Z YnXddZd d Zd d Zd dZddZd$ddZd%ddZddZd&ddZddZd'ddZd(d d!Zd"d#ZdS)))print_functionunicode_literalsN)Path)get_array_modulecCstS)N)numpy)_r4/tmp/pip-install-b8evvk6i/thinc/thinc/neural/util.pyr cCs$tdkr dSt|tjrdSdSdS)z&Check whether an array is a cupy arrayNFT)cupy isinstancendarray)arrrrr is_cupy_arrays  rcCst|tjrdSdSdS)z'Check whether an array is a numpy arrayTFN)r rr)rrrr is_numpy_array%s rcCs\ddlm}m}|dks*t|tr.|dkr.|S|dksHt|trL|dkrL|Std|dS)N)NumpyOpsCupyOps)rcpur)r Zgpuz'Invalid ops (or device) description: %s)opsrrr int ValueError)rrrrrr get_ops-s rcCs<ddlm}ddlm}|jdk r4||_||_dSdSdS)z?Use GPU if it's available. Returns True if so, False otherwise.r)Model)rNTF)_classes.modelrrrxpOps)rrrrr prefer_gpu8s   rcCs<ddlm}ddlm}|jdkr*td||_||_dS)Nr)r)rz;GPU is not accessible. Was the library installed correctly?T)rrrrrrr)rrrrr require_gpuEs   rccs`t|trt|}n|}t|}x8t|}tt|t|}t|dkrNPt|Vq$WdS)zlIterate over batches of items. `size` may be an iterator, so that batch-size can vary on each step. rN) r r itertoolsrepeatiternextlistislicelen)itemssizeZsize_Z batch_sizebatchrrr minibatchPs   r+cCsBx8|D]0}|dd|dd|d|dqW|dfS)z(Pad sentence sequences with EOL markers.rz-EOL-N)insertappend) sequencesdropsequencerrr mark_sentence_boundariesas     r2csddidfdd }|S)NrcsJt}x8t|D],\}}|kr2||<|d7}|||<qW|dfS)Nr)r' enumerate)idsr0Zn_vectoriid_)id_maprr begin_updatenszremap_ids..begin_update)r3r)rr9r)r8r remap_idsks r: same_kindcCsZt|tjr&t|tjr&||dd<n0t|rJtj|dd}t||n t||dS)NF)copy)r rrrr arrayZcopyto)dstsrcZcastingwhererrr copy_arrayzs rAcCs$t|tst|trt|S|SdS)N)r unicodestrr)pathrrr ensure_pathsrEcCstt|}|tkr|}tj|dd}|ss<