o
    s::j                     @  sF   d dl mZ d dlmZ d dlmZ d dlmZ G dd dej	Z
dS )    )annotations)Any)builder)_module_listc                      sJ   e Zd ZdZd fddZdd
dZdddZdddZdddZ  Z	S )
Sequentiala  A sequential container that calls children in order, mirroring ``torch.nn.Sequential``.

    Children are registered with string keys ``"0"``, ``"1"``, etc., just like
    ``ModuleList``. The ``forward`` method passes the output of each child as
    the input to the next.

    Example::

        class SiLU(Module):
            def forward(self, op, x):
                return op.Mul(x, op.Sigmoid(x))

        # Produces parameter names: "mod.0.weight", "mod.0.bias"
        # SiLU at index 0 has no parameters.
        mod = Sequential(SiLU(), Linear(4, 4))

        # Calling mod(op, x) is equivalent to:
        #   x = silu(op, x)
        #   x = linear(op, x)
    modules_module_list.ModulereturnNonec                   s   t  | d S )N)super__init__)selfr   	__class__ `/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnxscript/nn/_sequential.pyr   "   s   zSequential.__init__namestrc                 C  s0   t | d| | j D ]	\}}|| qdS )ay  Set this container's name. Children keep simple ``"0"``, ``"1"`` names.

        Unlike ``ModuleList._set_name`` which fully qualifies children (used
        when ModuleList is iterated externally), Sequential is called via
        ``__call__`` which already pushes its own name onto the builder stack.
        Children must keep simple keys to avoid double-prefixing.
        _nameN)object__setattr___modulesitems	_set_name)r   r   keychildr   r   r   r   %   s   zSequential._set_namer   modulec                 C  s4   |j du rt|d| || j|< t| || dS )a!  Register a child module under the given string key.

        Unlike ``ModuleList._register_child`` which qualifies the child name
        with the parent name, Sequential keeps children with simple index
        names because ``__call__`` already pushes the Sequential's own name.
        Nr   )r   r   r   r   )r   r   r   r   r   r   _register_child1   s   

zSequential._register_childop_builder.OpBuilderinputr   c                 C  s,   t | dkr
td| D ]}|||}q|S )zRun each child module sequentially, passing output to the next.

        Mirrors ``torch.nn.Sequential.forward``: each child receives exactly
        one positional argument (the output of the previous child).
        r   z4Cannot call forward on an empty Sequential container)lenRuntimeError)r   r   r    r   r   r   r   forward=   s
   zSequential.forwardc                 C  sT   dg}| j  D ]\}}t|dd}|d| d|  q|d d|S )NzSequential(
z
  z  (z): ))r   r   reprreplaceappendjoin)r   linesr   r   mod_reprr   r   r   __repr__I   s   

zSequential.__repr__)r   r   r	   r
   )r   r   r	   r
   )r   r   r   r   r	   r
   )r   r   r    r   r	   r   )r	   r   )
__name__
__module____qualname____doc__r   r   r   r#   r,   __classcell__r   r   r   r   r      s    


r   N)
__future__r   typingr   onnxscript._internalr   _builderonnxscript.nnr   
ModuleListr   r   r   r   r   <module>   s
   