
    
Ng8%                         d dl Z d dlmZ d dlZd dlmZmZmZ  G d de j                  Zedk    r e j	                     dS dS )    N)parameterized)
GraphProtoOperatorSetIdProtocheckerc            	           e Zd ZdeddfdZddZddZddZddZdd	Z	 e
j        d
i fdddifdddifddddfg          dededdfd            Zd Z e
j        g d          d             ZdS )TestBasicFunctionsgraphreturnNc                 :   |                      t          |j                  d           |                      |j        d         j        d           |                      |j        d         j        d           |                      |j        d         j        d           d S )N   r   MatMul   Add   Softmax)assertEquallennodeop_type)selfr	   s     Q/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/test/parser_test.pycheck_graphzTestBasicFunctions.check_graph   s    UZ!,,,A.999A.666A.	:::::    c                 r    d}t           j                            |          }|                     |           d S )Nz
           agraph (float[N, 128] X, float[128,10] W, float[10] B) => (float[N] C)
           {
              T = MatMul(X, W)
              S = Add(T, B)
              C = Softmax(S)
           }
           )onnxparserparse_graphr   )r   inputr	   s      r   test_parse_graphz#TestBasicFunctions.test_parse_graph   s:     ''..r   c                    d}t           j                            |          }|                     |j        d           |                     t          |j                  d           |                     |j                   d S )Na@  
           <
             ir_version: 7,
             opset_import: [ "" : 10, "com.microsoft": 1]
           >
           agraph (float[N, 128] X, float[128,10] W, float[10] B) => (float[N] C)
           {
              T = MatMul(X, W)
              S = Add(T, B)
              C = Softmax(S)
           }
              r   )	r   r   parse_modelr   
ir_versionr   opset_importr   r	   r   r   models      r   test_parse_modelz#TestBasicFunctions.test_parse_model   sv     ''..)1---U/00!444%%%%%r   c                 \    d|                      t          j        j        fd           d S )Nz
           agraph (float[N, 128] X, float[128,10] W, float[10] B) => (float[N] C)
           {
              T = MatMul[X, W]
              S = Add(T, B)
              C = Softmax(S)
           }
           c                  B    t           j                                       S N)r   r   r   r   s   r   <lambda>z;TestBasicFunctions.test_parse_graph_error.<locals>.<lambda>;       DK,C,CE,J,J r   assertRaisesr   r   
ParseErrorr   r   s    @r   test_parse_graph_errorz)TestBasicFunctions.test_parse_graph_error1   sB     	K"$J$J$J$J	
 	
 	
 	
 	
r   c                 \    d|                      t          j        j        fd           d S )NaA  
           <
             ir_version: 7,
             opset_import: [ "" : 10   "com.microsoft": 1]
           >
           agraph (float[N, 128] X, float[128,10] W, float[10] B) => (float[N] C)
           {
              T = MatMul(X, W)
              S = Add(T, B)
              C = Softmax(S)
           }
           c                  B    t           j                                       S r*   r   r   r"   r+   s   r   r,   z;TestBasicFunctions.test_parse_model_error.<locals>.<lambda>L   r-   r   r.   r1   s    @r   test_parse_model_errorz)TestBasicFunctions.test_parse_model_error>   sB     	K"$J$J$J$J	
 	
 	
 	
 	
r   c                 p    d}t           j                            |          }t          j        |           d S )Nac  
            <
            ir_version: 9,
            opset_import: [ "" : 15, "custom_domain" : 1],
            producer_name: "FunctionProtoTest",
            producer_version: "1.0",
            model_version: 1,
            doc_string: "A test model for model local functions."
          >
         agraph (float[N] x) => (float[N] out)
         {
            out = custom_domain.Selu<alpha=2.0, gamma=3.0>(x)
         }
         <
         domain: "custom_domain",
         opset_import: [ "" : 15],
         doc_string: "Test function proto"
         >
           Selu
           <alpha: float=1.67326319217681884765625, gamma: float=1.05070102214813232421875>
           (X) => (C)
           {
               constant_alpha = Constant<value_float: float=@alpha>()
               constant_gamma = Constant<value_float: float=@gamma>()
               alpha_x = CastLike(constant_alpha, X)
               gamma_x = CastLike(constant_gamma, X)
               exp_x = Exp(X)
               alpha_x_exp_x = Mul(alpha_x, exp_x)
               alpha_x_exp_x_ = Sub(alpha_x_exp_x, alpha_x)
               neg = Mul(gamma_x, alpha_x_exp_x_)
               pos = Mul(gamma_x, X)
               _zero = Constant<value_float=0.0>()
               zero = CastLike(_zero, X)
               less_eq = LessOrEqual(X, zero)
               C = Where(less_eq, neg, pos)
           }
        )r   r   r"   r   check_modelr%   s      r   #test_parse_function_with_attributesz6TestBasicFunctions.test_parse_function_with_attributesO   s8    $L ''..E"""""r   zEagraph (float[N] x) => (float[N] out) { out = custom_domain.Selu(x) }zPagraph (float[N] x) => (float[N] out) { out = custom_domain.Selu<alpha=2.0>(x) }alphag       @zPagraph (float[N] x) => (float[N] out) { out = custom_domain.Selu<gamma=3.0>(x) }gammag      @z[agraph (float[N] x) => (float[N] out) { out = custom_domain.Selu<alpha=2.0, gamma=3.0>(x) })r:   r;   
graph_textexpected_attributec                   
 d
dd }
fd}d
 d d}t           j                            |          g}t           j                            |          }t	          dd	
          t	          dd
          g}t           j                            |||          }	t          j        |	            ||	            ||	j	        j
        d         |           d S )Ng   ?g   ?c                     |D ]Cfd| j         D             }t          |          dk    sJ |d         j        |         k    sJ Dd S )Nc                 *    g | ]}|j         k    |S  name).0attrkeys     r   
<listcomp>zzTestBasicFunctions.test_composite_parse_function_with_attributes.<locals>.expect_custom_node_attribute.<locals>.<listcomp>   s%    RRRtcAQAQdAQAQAQr   r   r   )	attributer   f)r   
attributes
match_attrrF   s      @r   expect_custom_node_attributezfTestBasicFunctions.test_composite_parse_function_with_attributes.<locals>.expect_custom_node_attribute   sl    ! : :RRRRt~RRR
:!++++!!}*S/99999: :r   c                 Z   t          | j        d         j                  dk    sJ d | j        d         j        D             }t          |          dk    r|d         j        k    sJ d | j        d         j        D             }t          |          dk    r|d         j        k    sJ d S )Nr   r   c                 (    g | ]}|j         d k    |S )r:   rB   rD   
attr_protos     r   rG   z}TestBasicFunctions.test_composite_parse_function_with_attributes.<locals>.expect_model_function_attribute.<locals>.<listcomp>   /          ?g-- ---r   r   c                 (    g | ]}|j         d k    |S )r;   rB   rO   s     r   rG   z}TestBasicFunctions.test_composite_parse_function_with_attributes.<locals>.expect_model_function_attribute.<locals>.<listcomp>   rQ   r   )r   	functionsattribute_protorI   )r&   attr_proto_alphaattr_proto_gammadefault_alphadefault_gammas      r   expect_model_function_attributeziTestBasicFunctions.test_composite_parse_function_with_attributes.<locals>.expect_model_function_attribute   s    uq)9::a????   "'/!"4"D     
 '((A--2B12E2G=2X2X2X2X   "'/!"4"D     
 '((A--2B12E2G=2X2X2X2X2X2Xr   z
         <
         domain: "custom_domain",
         opset_import: [ "" : 15],
         doc_string: "Test function proto"
         >
           Selu
           <alpha: float=z, gamma: float=a  >
           (X) => (C)
           {
               constant_alpha = Constant<value_float: float=@alpha>()
               constant_gamma = Constant<value_float: float=@gamma>()
               alpha_x = CastLike(constant_alpha, X)
               gamma_x = CastLike(constant_gamma, X)
               exp_x = Exp(X)
               alpha_x_exp_x = Mul(alpha_x, exp_x)
               alpha_x_exp_x_ = Sub(alpha_x_exp_x, alpha_x)
               neg = Mul(gamma_x, alpha_x_exp_x_)
               pos = Mul(gamma_x, X)
               _zero = Constant<value_float=0.0>()
               zero = CastLike(_zero, X)
               less_eq = LessOrEqual(X, zero)
               C = Where(less_eq, neg, pos)
           }
            )domainversioncustom_domainr   )rS   opset_importsr   )r   r   parse_functionr   r   helper
make_modelr   r8   r	   r   )r   r<   r=   rL   rY   function_textrS   r	   r_   r&   rW   rX   s             @@r   -test_composite_parse_function_with_attributesz@TestBasicFunctions.test_composite_parse_function_with_attributesy   s1   . 21	: 	: 	:	Y 	Y 	Y 	Y 	Y 	Y (  9F  4 [//>>?	''
33b"555oqAAA

 &&Ym ' 
 
 	E"""''...$$U[%5a%8:LMMMMMr   c                    t           j                            d          }|                     t	          |j                  ddg           |                     t	          |j                  ddg           |                     t          |j                  d           t           j	        
                    |d          }|                     |d           |                     |j        d           |                     |j        d	           d S )
Nz5out1, out2 = SomeDomain.SomeOp <attr1 = 1> (in1, in2)in1in2out1out2r   attr1
SomeDomainSomeOp)r   r   
parse_noder   listr   outputr   rH   ra   get_node_attr_valuer\   r   )r   r   attr_vals      r   test_parse_nodez"TestBasicFunctions.test_parse_node   s    {%%C
 
 	dj))E5>:::dk**VV,<===T^,,a000;224AA1%%%l333x00000r   ))not_a_good_floatT)inf1T)z-inf1T)nan0T)z-nan0T)naninfT)infF)z-infF)infinityF)z	-infinityF)nanF)z-NaNFc                    d| d|r*|                      t          j        j        fd           d S t          j                                      }|                     |j        d           |                     |j        d           |                     |j        d           |                     t          |j
        j                  d           |                     t          |j
        j        d         j                  d           |                     |j
        j        d         j        d         j        d	           |                     |j
        j        d         j        d         j        t          j        j                   |                     t#          |j
        j        d         j        d         j                  t#          t'          |                               d S )
Nz
        <
        ir_version: 8,
        opset_import: ["" : 18, "this" : 1],
        producer_name: "FunctionProtoTest",
        producer_version: "1.0"
        >
        _func () => ()
        {
        tmp = Constant <value_float = z>()
        }
        c                  B    t           j                                       S r*   r5   )
model_texts   r   r,   zDTestBasicFunctions.test_parse_various_float_values.<locals>.<lambda>   s    0G0G
0S0S r      FunctionProtoTestz1.0r   r   value_float)r/   r   r   r0   r"   r   r#   producer_nameproducer_versionr   r	   r   rH   rC   typeAttributeProtoFLOATstrrI   float)r   test_literalexpect_exceptionr&   r|   s       @r   test_parse_various_float_valuesz2TestBasicFunctions.test_parse_various_float_values   s   " (4  
  	&(S(S(S(S     K++J77EU-q111U02EFFFU3U;;;S!122A666S!1!!4!>??CCCU[-a0:1=BMRRR #-a05t7J7P   EK$Q'1!4677U<=P=P9Q9Q    r   )r
   N)__name__
__module____qualname__r   r   r   r'   r2   r6   r9   r   expandr   dictrd   rr   r   rA   r   r   r   r      s       ; ; ; ; ; ;
  
  
  
 & & & &$
 
 
 

 
 
 
"(# (# (# (#T ] X
 c#
 c#
 n,,	
 (BNBN37BN	BN BN BN) (BNH
1 
1 
1 ]	
 	
 	
   !    r   r   __main__)
unittestr   r   r   r   r   TestCaser   r   mainrA   r   r   <module>r      s     ' ' ' ' ' '  8 8 8 8 8 8 8 8 8 8    *   D zHMOOOOO r   