o
    s::j                     @  sB   d dl mZ d dlmZ d dlZd dlmZ G dd dej	Z
dS )    )annotations)SequenceN)builderc                      sL   e Zd ZdZ			dd fddZedddZdddZdddZ  Z	S )	Parametera  A module parameter that is also an ``ir.Value``.

    Since ``Parameter`` subclasses ``ir.Value``, it can be passed directly
    to ONNX ops inside ``Module.forward()`` without any conversion.
    Calling :meth:`_realize` qualifies the name with the current module
    context and registers the parameter as a graph initializer.

    Args:
        shape: Shape of the parameter tensor.
        dtype: Data type of the parameter. If None and data is not provided, defaults to float32.
        name: Name for the parameter. If None, the attribute name from
            the parent Module is used.
        data: Optional initial tensor data. If provided, the initializer
            will carry this as its const_value.
    NshapeSequence[int]dtypeir.DataType | Nonename
str | Nonedatair.TensorProtocol | NonereturnNonec                   s|   |d ur!|d ur|j |krtd|j  d| d|d u r |j }n|d u r)tjj}t j|t|t||d d| _	d S )NzData type of provided data (z&) does not match the specified dtype (z).)r
   r   typeconst_valueF)
r   
ValueErrorirDataTypeFLOATsuper__init__Shape
TensorType	_realized)selfr   r   r
   r   	__class__ _/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnxscript/nn/_parameter.pyr      s"   
zParameter.__init__c                 C  s   | j dur	| j jS dS )z/Return the element data type of this parameter.N)r   r   r   r   r   r   r   7   s   zParameter.dtyper   _builder.GraphBuilderc                 C  sH   | j r| S | j}|std|j}|| }| _| |jj|< d| _ | S )u'  Qualify the name and register as a graph initializer.

        Uses the builder's *root* graph builder to qualify the name and
        register the initializer.  When the builder is a sub-builder (e.g.
        for a Scan body), this ensures the parameter is stored in the
        main graph — making it visible as an implicit input to the
        subgraph rather than incorrectly placed inside it.

        Uses direct assignment to ``graph.initializers[...]`` to skip the
        const_value check. Idempotent: subsequent calls are no-ops.
        zParameter._realize() called on a Parameter without a name. Ensure the Parameter is attached to a Module attribute or otherwise initialized with a name before realization.T)r   r
   r   root_qualify_initializer_namegraphinitializers)r   r   	self_namer"   r   r   r   _realize<   s   zParameter._realizestrc                 C  s"   dt | j d| j d| jdS )NzParameter(shape=z, dtype=z, name=))listr   r   r
   r    r   r   r   __repr__X   s   "zParameter.__repr__)NNN)
r   r   r   r	   r
   r   r   r   r   r   )r   r	   )r   r!   r   r   )r   r(   )
__name__
__module____qualname____doc__r   propertyr   r'   r+   __classcell__r   r   r   r   r      s    
r   )
__future__r   typingr   onnx_irr   onnxscript._internalr   _builderValuer   r   r   r   r   <module>   s
   