σ —ΐv]c@@sπdZddlmZddlZddlZddlZddlmZmZm Z ddlm Z ddlm Z dee e ƒfd „ƒYZ e d dƒe j_dd „Zdd „Zdd „Zd„Zdd„Zd„ZdS(s Context management API of mxnet.i(tabsolute_importNi(t classpropertytwith_metaclasst_MXClassPropertyMetaClass(t_LIB(t check_calltContextcB@sΩeZdZejƒZidd6dd6dd6dd6Zidd6dd6dd6dd6Zd d „Ze d „ƒZ d „Z d „Z d„Z d„Zd„Zd„Zed„ƒZejd„ƒZd„ZRS(s#Constructs a context. MXNet can run operations on CPU and different GPUs. A context describes the device type and ID on which computation should be carried on. One can use mx.cpu and mx.gpu for short. See also ---------- `How to run MXNet on multiple CPU/GPUs ` for more details. Parameters ---------- device_type : {'cpu', 'gpu'} or Context. String representing the device type. device_id : int (default=0) The device id of the device, needed for GPU. Note ---- Context can also be used as a way to change the default context. Examples -------- >>> # array on cpu >>> cpu_array = mx.nd.ones((2, 3)) >>> # switch default context to GPU(2) >>> with mx.Context(mx.gpu(2)): ... gpu_array = mx.nd.ones((2, 3)) >>> gpu_array.context gpu(2) One can also explicitly specify the context when creating an array. >>> gpu_array = mx.nd.ones((2, 3), mx.gpu(1)) >>> gpu_array.context gpu(1) tcpuitgpuit cpu_pinnedit cpu_sharediicC@sPt|tƒr*|j|_|j|_ntj||_||_d|_dS(N(t isinstanceRt device_typeidt device_idt devstr2typetNonet_old_ctx(tselft device_typeR ((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt__init__Js   cC@stj|jS(sReturns the device type of current context. Examples ------- >>> mx.context.current_context().device_type 'cpu' >>> mx.current_context().device_type 'cpu' Returns ------- device_type : str (Rt devtype2strR (R((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyRSscC@st|j|jfƒS(s3Compute hash value of context for dictionary lookup(thashR R (R((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt__hash__dscC@s1t|tƒo0|j|jko0|j|jkS(soCompares two contexts. Two contexts are equal if they have the same device type and device id. (R RR R (Rtother((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt__eq__hscC@sd|j|jfS(Ns%s(%d)(RR (R((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt__str__pscC@s |jƒS(N(R(R((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt__repr__sscC@sIttjdƒs*tddƒtj_ntjj|_|tj_|S(NtvalueRi(thasattrRt _default_ctxRR(R((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt __enter__vs  cC@s|jtj_dS(N(RRRR(RtptypeRttrace((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt__exit__}scC@sDtjdtƒttjdƒs:tddƒ|j_n|jjS(Ns™Context.default_ctx has been deprecated. Please use Context.current_context() instead. Please use test_utils.set_default_context to set a default contextRRi(twarningstwarntDeprecationWarningRRRR(tcls((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt default_ctxs  cC@s tjdtƒ||j_dS(Ns™Context.default_ctx has been deprecated. Please use Context.current_context() instead. Please use test_utils.set_default_context to set a default context(R"R#R$RR(R%tval((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyR&‹s cC@s>tj|jƒ}tj|jƒ}ttj||ƒƒdS(sζEmpties the memory cache for the current contexts device. MXNet utilizes a memory pool to avoid excessive allocations. Calling empty_cache will empty the memory pool of the contexts device. This will only free the memory of the unreferenced data. Examples ------- >>> ctx = mx.gpu(0) >>> arr = mx.nd.ones((200,200), ctx=ctx) >>> del arr >>> ctx.empty_cache() # forces release of memory allocated for arr N(tctypestc_intR R RRtMXStorageEmptyCache(Rtdev_typetdev_id((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyt empty_cache”s(t__name__t __module__t__doc__t threadingtlocalRRRRtpropertyRRRRRRR!RR&tsetterR-(((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyRs( ""         RcC@s td|ƒS(s°Returns a CPU context. This function is a short cut for ``Context('cpu', device_id)``. For most operations, when no context is specified, the default context is `cpu()`. Examples ---------- >>> with mx.cpu(): ... cpu_array = mx.nd.ones((2, 3)) >>> cpu_array.context cpu(0) >>> cpu_array = mx.nd.ones((2, 3), ctx=mx.cpu()) >>> cpu_array.context cpu(0) Parameters ---------- device_id : int, optional The device id of the device. `device_id` is not needed for CPU. This is included to make interface compatible with GPU. Returns ------- context : Context The corresponding CPU context. R(R(R ((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyRͺscC@s td|ƒS(sιReturns a CPU pinned memory context. Copying from CPU pinned memory to GPU is faster than from normal CPU memory. This function is a short cut for ``Context('cpu_pinned', device_id)``. Examples ---------- >>> with mx.cpu_pinned(): ... cpu_array = mx.nd.ones((2, 3)) >>> cpu_array.context cpu_pinned(0) >>> cpu_array = mx.nd.ones((2, 3), ctx=mx.cpu_pinned()) >>> cpu_array.context cpu_pinned(0) Parameters ---------- device_id : int, optional The device id of the device. `device_id` is not needed for CPU. This is included to make interface compatible with GPU. Returns ------- context : Context The corresponding CPU pinned memory context. R (R(R ((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyR ΘscC@s td|ƒS(sŽReturns a GPU context. This function is a short cut for Context('gpu', device_id). The K GPUs on a node are typically numbered as 0,...,K-1. Examples ---------- >>> cpu_array = mx.nd.ones((2, 3)) >>> cpu_array.context cpu(0) >>> with mx.gpu(1): ... gpu_array = mx.nd.ones((2, 3)) >>> gpu_array.context gpu(1) >>> gpu_array = mx.nd.ones((2, 3), ctx=mx.gpu(1)) >>> gpu_array.context gpu(1) Parameters ---------- device_id : int, optional The device id of the device, needed for GPU. Returns ------- context : Context The corresponding GPU context. R(R(R ((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyRζscC@s/tjƒ}ttjtj|ƒƒƒ|jS(s»Query CUDA for the number of GPUs present. Raises ------ Will raise an exception on any CUDA error. Returns ------- count : int The number of GPUs. (R(R)RRt MXGetGPUCounttbyrefR(tcount((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pytnum_gpuss cC@sbtjƒ}tjƒ}tj|ƒ}ttj|tj|ƒtj|ƒƒƒ|j|jfS(sCQuery CUDA for the free and total bytes of GPU global memory. Parameters ---------- device_id : int, optional The device id of the GPU device. Raises ------ Will raise an exception on any CUDA error. Returns ------- (free, total) : (int, int) The number of GPUs. (R(tc_uint64R)RRtMXGetGPUMemoryInformation64R6R(R tfreettotalR,((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pytgpu_memory_infos   +cC@s4ttjdƒs*tddƒtj_ntjjS(s6Returns the current context. By default, `mx.cpu()` is used for all the computations and it can be overridden by using `with mx.Context(x)` statement where x can be cpu(device_id) or gpu(device_id). Examples ------- >>> mx.current_context() cpu(0) >>> with mx.Context('gpu', 1): # Context changed in `with` block. ... mx.current_context() # Computation done here will be on gpu(1). ... gpu(1) >>> mx.current_context() # Back to default context. cpu(0) Returns ------- default_ctx : Context RRi(RRRR(((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pytcurrent_context/s(R0t __future__RR1R"R(tbaseRRRRRtobjectRRRRR RR8R=R>(((s./tmp/pip-install-Qvdv_2/mxnet/mxnet/context.pyts   Š