o
    i                     @   s  d dl mZmZmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZmZ edd	d	e	d
fdee dee dee	 dede	eef f
ddZde	dededeeef fddZde	dededeeeegef f fddZde	dedefddZd	S )    )AnyCallableOptionalTupleType   )registry)Model)	MXNetShim)
ArgsKwargs)convert_recursiveis_mxnet_arrayis_xp_arraymxnet2xpxp2mxnetzMXNetWrapper.v1Nmxnetconvert_inputsconvert_outputsmodel_class
model_namereturnc                 C   s4   |du rt }|du rt}||t||dt| gdS )a)  Wrap a MXNet model, so that it has the same API as Thinc models.
    To optimize the model, you'll need to create a MXNet optimizer and call
    optimizer.step() after each batch.

    Your MXNet model's forward method can take arbitrary args and kwargs,
    but must return either a single tensor as output or a tuple. You may find the
    MXNet register_forward_hook helpful if you need to adapt the output.

    The convert functions are used to map inputs and outputs to and from your
    MXNet model. Each function should return the converted output, and a callback
    to use during the backward pass. So:

        Xmxnet, get_dX = convert_inputs(X)
        Ymxnet, mxnet_backprop = model.shims[0](Xmxnet, is_train)
        Y, get_dYmxnet = convert_outputs(Ymxnet)

    To allow maximum flexibility, the MXNetShim expects ArgsKwargs objects
    on the way into the forward and backward passes. The ArgsKwargs objects
    will be passed straight into the model in the forward pass, and straight
    into `mxnet.autograd.backward` during the backward pass.
    N)r   r   )attrsshims)convert_mxnet_default_inputsconvert_mxnet_default_outputsforwardr
   )mxnet_modelr   r   r   r    r   M/home/ubuntu/.local/lib/python3.10/site-packages/thinc/layers/mxnetwrapper.pyMXNetWrapper
   s   r   modelXis_trainc           	         sn   | j d }| j d }|| ||\} | jd ||\}|| ||f|\}dtdtf fdd}||fS )z}Return the output of the wrapped MXNet model for the given input,
    along with a callback to handle the backward pass.
    r   r   r   dYr   c                    s   | }|} |}|S Nr   )r#   dYmxnetdXmxnetdXget_dXget_dYmxnetmxnet_backpropr   r   backprop>   s   zforward.<locals>.backprop)r   r   r   )	r    r!   r"   r   r   XmxnetYmxnetYr,   r   r(   r   r   3   s   

r   c                    s    fdd}t t||}t|trdd }||fS t|tr+dd }tt |d|fS t|ttfr@dd }tt|i d|fS dd }t|fi d|fS )	Nc                    s   t |  dS )N)requires_grad)r   )xr"   r   r   <lambda>M   s    z.convert_mxnet_default_inputs.<locals>.<lambda>c                 S   s   t tt| S r$   )r   r   r   )r&   r   r   r   reverse_conversionQ   s   z8convert_mxnet_default_inputs.<locals>.reverse_conversionc                 S      t tt| }|jS r$   )r   r   r   kwargsr&   r'   r   r   r   r4   W      argsr6   c                 S   r5   r$   r   r   r   r:   r7   r   r   r   r4   ^   r8   c                 S   s   t tt| }|jd S )Nr   r;   r7   r   r   r   r4   e   s   
)r   r   
isinstancer   dicttuplelist)r    r!   r"   	xp2mxnet_	convertedr4   r   r2   r   r   J   s   

r   X_Ymxnetc                    s2   |\} t tt }dtdtf fdd}||fS )Nr#   r   c                    s    t tt| }t ffd|idS )N
head_gradsr9   )r   r   r   r   )r#   r%   r.   r   r   r4   p   s   z9convert_mxnet_default_outputs.<locals>.reverse_conversion)r   r   r   r   r   )r    rB   r"   r!   r/   r4   r   rD   r   r   l   s   r   )typingr   r   r   r   r   configr   r    r	   r   r
   typesr   utilr   r   r   r   r   layersstrr   boolr   r   r   r   r   r   r   <module>   sB    
"(
"