ó šÄïYc@@sºdZddlmZddlZddlmZddlmZmZm Z m Z ddlm Z m Z m Z mZmZmZddlmZmZmZdd lmZmZmZmZmZmZmZdd lmZmZdd lm Z m!Z!m"Z"ee ƒZ#d e$fd „ƒYZ%de%fd„ƒYZ&de%fd„ƒYZ'de$fd„ƒYZ(de$fd„ƒYZ)de$fd„ƒYZ*e*ƒZ+d„Z,e,dƒe)ƒdS(snumpy interface for operators.i(tabsolute_importN(tLock(t CFUNCTYPEtPOINTERt Structuretpointer(tc_void_ptc_inttc_chartc_char_ptcasttc_booli(t_LIBt check_calltMXCallbackList(tc_arraytc_strtmx_uinttmx_floattctypes2numpy_sharedt NDArrayHandletpy_str(tsymboltcontext(tNDArrayt_DTYPE_NP_TO_MXt_DTYPE_MX_TO_NPtPythonOpcB@sheZdZgZed„Zd„Zd„Zd„Zd„Z d„Z d„Z d„Z d „Z RS( s­Base class for operators implemented in Python. Parameters ---------- need_top_grad : bool the default need_top_grad() function returns this value. cC@sd|_||_dS(N(tNonetinfo_tneed_top_grad_(tselft need_top_grad((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyt__init__.s cO@s|j||ŽS(N(t get_symbol(Rtargstkwargs((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyt__call__2scO@stdƒ‚dS(sCCreate a symbol from numpy operator. This should only be called once per instance if the operator contains internal states. Parameters ---------- args : list a list of input arguments (symbols). Returns ------- sym : mxnet.symbol.Symbol sMust override thisN(tNotImplementedError(RR#R$((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyR"5scC@s|d|d(dS(søForward interface. Override to create new operators. Parameters ---------- in_data, out_data: list input and output for forward. See document for corresponding arguments of Operator::Forward iN((Rtin_datatout_data((s.build/bdist.linux-armv7l/egg/mxnet/operator.pytforwardEs cC@sd|d(dS(sBackward interface. Can override when creating new operators. Parameters ---------- out_grad, in_data, out_data, in_grad : list input and output for backward. See document for corresponding arguments of Operator::Backward gð?iN((Rtout_gradR'R(tin_grad((s.build/bdist.linux-armv7l/egg/mxnet/operator.pytbackwardPs cC@s||dgfS(sInterface for ``infer_shape``. Can override when creating new operators. Parameters ---------- in_shape : list List of argument shapes in the same order as declared in list_arguments. Returns ------- in_shape : list List of argument shapes. Can be modified from in_shape. out_shape : list List of output shapes calculated from in_shape, in the same order as declared in list_arguments. i((Rtin_shape((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyt infer_shape\scC@sdgS(s±Interface for ``list_outputs``. Can override when creating new operators. Returns ------- outputs : list List of output blob names. toutput((R((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyt list_outputsoscC@sdgS(sîInterface for ``list_arguments``. Can override when creating new operators. Returns ------- in_shape : list list of argument shapes in the same order as declared in list_arguments. tdata((R((s.build/bdist.linux-armv7l/egg/mxnet/operator.pytlist_argumentsys cC@s|jS(sìWhether this operator needs out_grad for backward. Returns ------- need_top_grad : bool Whether this operator needs out_grad for backward. Should be set to False for loss layers. (R(R((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyR „s (t__name__t __module__t__doc__t _ref_holdertTrueR!R%R"R)R,R.R0R2R (((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyR$s     tNumpyOpcB@s#eZdZed„Zd„ZRS(s¤Base class for numpy operators. numpy operators allow parts of computation in symbolic graph to be writen in numpy. This feature is intended for quickly hacking out a solution for non performance critical parts. Please consider write a c++ implementation if it becomes a bottleneck. Note that if your operator contains internal states (like arrays), it cannot be used for multi-gpu training. cC@stt|ƒj|ƒdS(N(tsuperR8R!(RR ((s.build/bdist.linux-armv7l/egg/mxnet/operator.pyR!˜sc @s¤tdttttƒƒttƒtttƒƒttƒtƒ‰tdtttƒtttƒƒtƒ‰tdttttƒƒƒtƒ‰dtf‡‡‡fd†ƒY}‡fd†}‡fd†}‡fd†}‡fd†}‡fd†}|ˆ|ƒˆ|ƒˆ|ƒˆ|ƒˆ|ƒdddddƒ ˆ_ t t t ˆj ƒtƒj dƒ} tjjd | d ˆjƒ||Ž} tjjˆƒ| S( Nt NumpyOpInfoc @sneZdZdˆfdˆfdˆfdˆfdˆfdefdefdefd efd efg ZRS( s@Structure that holds Callback information. Passed to NumpyOpPropR)R,R.R0R2t p_forwardt p_backwardt p_infer_shapetp_list_outputstp_list_arguments(R3R4R5Rt_fields_((t fb_functypetinfer_functypet list_functype(s.build/bdist.linux-armv7l/egg/mxnet/operator.pyR:¡s         c @s«gtdƒD] }g^q }xgt|ƒD]Y}gt||ƒD]}|||^qC} t||| ƒ} |||j| ƒq,Wˆjd|dd|dƒdS(sC Callback for NumpyOp::ForwardiR'iR(iN(trangeRtappendR)( t num_tensort tensor_ptrst tensor_dimst tensor_shapest tensor_tagst_tittensorstjtshapetbuff(R(s.build/bdist.linux-armv7l/egg/mxnet/operator.pyt forward_entry¯s +c @s¿gtdƒD] }g^q }xgt|ƒD]Y}gt||ƒD]}|||^qC} t||| ƒ} |||j| ƒq,Wˆjd|dd|dd|dd|d ƒd S( s C Callback for NumpyOp::BackwardiR'iR(iR+iR*iN(RDRRER,( RFRGRHRIRJRKRLRMRNRORP(R(s.build/bdist.linux-armv7l/egg/mxnet/operator.pytbackward_entry¹s+c @s2tˆjƒƒ}tˆjƒƒ}|||ks:t‚gt|ƒD]1}gt||ƒD]}|||^q^^qG}ˆj|ƒ\} } t| ƒ|ks«t‚t| ƒ|ksÃt‚t| ƒt| ƒ} xRt||ƒD]@}ttt | |ƒt t ƒƒ||R?tp_declare_backward_dependency(R3R4R5RR@((t deps_functypeRARBRC(s.build/bdist.linux-armv7l/egg/mxnet/operator.pyRjs           c@sïyÇgtdƒD] }g^q}xƒt|ƒD]u}||dkrv|||jtt||tƒdtƒƒq/|||jtt||tƒdtƒƒq/Wˆjd|dd|dƒWn!tk rêdt j ƒGHtSXtS(s!C Callback for NDArrayOp::ForwardiitwritableR'iR(sError in NDArrayOp.forward: %s( RDRERR RR7tFalseR)t Exceptiont tracebackt format_exc(t num_ndarrayt ndarraiesttagsRKRLRM(R(s.build/bdist.linux-armv7l/egg/mxnet/operator.pyRQs$ $" c @syÛgtdƒD] }g^q}xƒt|ƒD]u}||dkrv|||jtt||tƒdtƒƒq/|||jtt||tƒdtƒƒq/Wˆjd|dd|dd|dd |d ƒWn!tk rþd t j ƒGHtSXtS( s"C Callback for NDArrayOp::BackwardiiRnR'iR(iR+R*isError in NDArrayOp.backward: %s( RDRERR RR7RoR,RpRqRr(RsRtRuRKRLRM(R(s.build/bdist.linux-armv7l/egg/mxnet/operator.pyRR's$ $ c @sZy2tˆjƒƒ}tˆjƒƒ}|||ks=t‚gt|ƒD]1}gt||ƒD]}|||^qa^qJ}ˆj|ƒ\} } t| ƒ|ks®t‚t| ƒ|ksÆt‚t| ƒt| ƒ} xRt||ƒD]@}ttt | |ƒt t ƒƒ||s( ".4 kdª.u  ÿ