L i8X@ddlmZmZmZmZmZmZddlmZddl Z ddl Z ddl m Z m Z ddlmZedZdZGdd eZ d"dd d e j&j(d eeeeed feeeeeed fffdeeeeeeeeffdeeede j&j(f dZ d#d e j&j(d eeeed fdeeedeeeddf dZ d$d e j&j(d eeeeeed ffdeeedeeeeefddf dZ d%dededededeeee j:feeeff dZde j&j(deeeeeffdZde de dedefdZ!ddd Z"d!Z#y)&)AnyTypeVarOptional NamedTupleUnionCallable)SequenceN) TupleTypeListType)wrap_cpp_moduleTc8eZdZUdZeed<dZeed<dZeed<y) InflatableArgaHelper type for bundled inputs. 'value' is the compressed/deflated input that is stored in the model. Value must be of the same type as the argument to the function that it is a deflated input for. 'fmt' is a formatable code string that is executed to inflate the compressed data into the appropriate input. It can use 'value' as an input to the format str. It must result in a value of the same type as 'value'. 'fmt_fn' is a formatable function code string that is executed to inflate the compressed data into the appropriate input. It must result in a value of the same type as 'value'. The function name should be the formatable part of the string. Note: Only top level InflatableArgs can be inflated. i.e. you cannot place an inflatable arg inside of some other structure. You should instead create an inflatable arg such that the fmt code string returns the full structure of your input. value{}fmtfmt_fnN) __name__ __module__ __qualname____doc__r__annotations__rstrr`/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/utils/bundled_inputs.pyrrs!( JCOFCrr)_receive_inflate_exprmodelinputs.inforreturnct|tjjs t dt |\}}tj j|j||}t|}t|tr$t|ts|Jt|||||St|ts|Jt|||||S)a Create and return a copy of the specified model with inputs attached. The original model is not mutated or changed in any way. Models with bundled inputs can be invoked in a uniform manner by benchmarking and code coverage tools. If inputs is passed in as a list then the inputs will be bundled for 'forward'. If inputs is instead passed in as a map then all the methods specified in the map will have their corresponding inputs bundled. Info should match watchever type is chosen for the inputs. The returned model will support the following methods: `get_all_bundled_inputs_for_() -> List[Tuple[Any, ...]]` Returns a list of tuples suitable for passing to the model like `for inp in model.get_all_bundled_inputs_for_foo(): model.foo(*inp)` `get_bundled_inputs_functions_and_info() -> Dict[str, Dict[str: List[str]]]` Returns a dictionary mapping function names to a metadata dictionary. This nested dictionary maps preset strings like: 'get_inputs_function_name' -> the name of a function attribute in this model that can be run to get back a list of inputs corresponding to that function. 'info' -> the user provided extra information about the bundled inputs If forward has bundled inputs then these following functions will also be defined on the returned module: `get_all_bundled_inputs() -> List[Tuple[Any, ...]]` Returns a list of tuples suitable for passing to the model like `for inp in model.get_all_bundled_inputs(): model(*inp)` `get_num_bundled_inputs() -> int` Equivalent to `len(model.get_all_bundled_inputs())`, but slightly easier to call from C++. Inputs can be specified in one of two ways: - The model can define `_generate_bundled_inputs_for_`. If the user chooses this method inputs[] should map to None - The `inputs` argument to this function can be a dictionary mapping functions to a list of inputs, of the same form that will be returned by get_all_bundled_inputs_for_. Alternatively if only bundling inputs for forward the map can be omitted and a singular list of inputs can be provided instead. The type of the inputs is List[Tuple[Any, ...]]. The outer list corresponds with a list of inputs, the inner tuple is the list of args that together make up one input. For inputs of functions that take one arg, this will be a tuple of length one. The Any, ... is the actual data that makes up the args, e.g. a tensor. Info is an optional parameter that maps functions to a list of strings providing extra information about that function's bundled inputs. Alternatively if only bundling inputs for forward the map can be omitted and a singular list of information can be provided instead. This could be descriptions, expected outputs, etc. - Ex: info={model.forward : ['man eating icecream', 'an airplane', 'a dog']} This function will attempt to optimize arguments so that (e.g.) arguments like `torch.zeros(1000)` will be represented compactly. Only top-level arguments will be optimized. Tensors in lists or tuples will not. Only ScriptModule is supported.) isinstancetorchjit ScriptModule Exception*_get_bundled_inputs_attributes_and_methods_C(_hack_do_not_use_clone_module_with_class_cr dict0augment_many_model_functions_with_bundled_inputslist!augment_model_with_bundled_inputs)r r!r"rignored_methods ignored_attrsclone cloned_modules r bundle_inputsr7)sF eUYY33 49::%OPU%V"O] HH = =  E$E*M&$$%558Pegkl $%55)-AVX\] rct|tjjs t d|j }t |dsd|_t|||i||r||ind|y)aAdd bundled sample inputs to a model for the forward function. Models with bundled inputs can be invoked in a uniform manner by benchmarking and code coverage tools. Augmented models will support the following methods: `get_all_bundled_inputs() -> List[Tuple[Any, ...]]` Returns a list of tuples suitable for passing to the model like `for inp in model.get_all_bundled_inputs(): model(*inp)` `get_num_bundled_inputs() -> int` Equivalent to `len(model.get_all_bundled_inputs())`, but slightly easier to call from C++. `get_bundled_inputs_functions_and_info() -> Dict[str, Dict[str: List[str]]]` Returns a dictionary mapping function names to a metadata dictionary. This nested dictionary maps preset strings like: 'get_inputs_function_name' -> the name of a function attribute in this model that can be run to get back a list of inputs corresponding to that function. 'info' -> the user provided extra information about the bundled inputs Inputs can be specified in one of two ways: - The model can define `_generate_bundled_inputs_for_forward`. If the user chooses this method inputs should be None - `inputs` is a list of inputs of form List[Tuple[Any, ...]]. A list of tuples where the elements of each tuple are the args that make up one input. r%rforwardN)r!rr"skip_size_check) r&r'r(r)r*r9hasattrrr0)r r!rr"r:r9s rr2r2sgJ eUYY33 49:: G 7J '$4 &!3!%g 4' rc t|tjjs t d|s t dt |ds t |dr t dd}|j D]@\}}t |dr |j}n$t |dr |j}n t d |t|tstd |d |jjd d D cgc]} | j} } tt| } |j j#d|| gt |d|zr|t d|d|d|t%|dk(rt d|d|dg} g} t'|D]\}}t|t(s"t|t*std|d|dg}| j-dt'|D]y\}} t/|||}t1| d|d|d||\}}}|j-|| j-d|d |sV|j3t5j6|{| j-t)|| j-d!| j-dd"j9| }||j-|t;|d|| t5j6d#j=||$}|j3||j3t5j6d%j=|&|r||vrt?||nd'}|d(|d)|d*|d+z }|d,k(s|j3t5j6d-|j3t5j6d.C|j3t5j6d/|d0y cc} w)1a Add bundled sample inputs to a model for an arbitrary list of public functions. Models with bundled inputs can be invoked in a uniform manner by benchmarking and code coverage tools. Augmented models will support the following methods: `get_all_bundled_inputs_for_() -> List[Tuple[Any, ...]]` Returns a list of tuples suitable for passing to the model like `for inp in model.get_all_bundled_inputs_for_foo(): model.foo(*inp)` `get_bundled_inputs_functions_and_info() -> Dict[str, Dict[str: List[str]]]` Returns a dictionary mapping function names to a metadata dictionary. This nested dictionary maps preset strings like: 'get_inputs_function_name' -> the name of a function attribute in this model that can be run to get back a list of inputs corresponding to that function. 'info' -> the user provided extra information about the bundled inputs If forward has bundled inputs then these following functions are also defined: `get_all_bundled_inputs() -> List[Tuple[Any, ...]]` Returns a list of tuples suitable for passing to the model like `for inp in model.get_all_bundled_inputs(): model(*inp)` `get_num_bundled_inputs() -> int` Equivalent to `len(model.get_all_bundled_inputs())`, but slightly easier to call from C++. Inputs can be specified in one of two ways: - The model can define `_generate_bundled_inputs_for_`. If the user chooses this method inputs[] should map to None - The `inputs` argument to this function can be a dictionary mapping functions to a list of inputs, of the same form that will be returned by get_all_bundled_inputs_for_. The type of the inputs is List[Tuple[Any, ...]]. The outer list corresponds with a list of inputs, the inner tuple is the list of args that together make up one input. For inputs of functions that take one arg, this will be a tuple of length one. The Any, ... is the actual data that makes up the args, e.g. a tensor. Info is an optional parameter that maps functions to a list of strings providing extra information about that function's bundled inputs. This could be descriptions, expected outputs, etc. - Ex: info={model.forward : ['man eating icecream', 'an airplane', 'a dog']} This function will attempt to optimize arguments so that (e.g.) arguments like `torch.zeros(1000)` will be represented compactly. Only top-level arguments will be optimized. Tensors in lists or tuples will not. r%z-Please provide inputs for at least 1 functionget_all_bundled_inputs%get_bundled_inputs_functions_and_infozModels can only be augmented with bundled inputs once. This Model seems to have already been augmented with bundled inputs. Please start afresh with one that doesn't have bundled inputs.rrnamezcAt least one of your functions has no attribute name please ensure all have one. m.foo.name = "foo"NzError inputs for function z is not a Sequence_bundled_inputs_deflated__generate_bundled_inputs_for_zinputs[z0] is not None, but _generate_bundled_inputs_for_z is already definedrz inputs for z3 must be specified if _generate_bundled_inputs_for_z is not already definedz!Error bundled input for function z idx: z is not a Tuple or a List(z deflated[z][])r:z ,z), z def _generate_bundled_inputs_for_{name}(self): deflated = self._bundled_inputs_deflated_{name} return [ {expr} ] )exprr?z def get_all_bundled_inputs_for_{name}(self): all_inputs = self._generate_bundled_inputs_for_{name}() assert all_inputs is not None return all_inputs )r?z[]zP temp_dict : Dict[str,List[str]] = {} info: List[str] = zx temp_dict['info'] = info temp_dict['get_inputs_function_name'] = ['get_all_bundled_inputs_for_z'] all_inputs['z'] = temp_dict r9z def get_all_bundled_inputs(self): return self.get_all_bundled_inputs_for_forward() z def get_num_bundled_inputs(self): return len(self.get_all_bundled_inputs_for_forward()) z def get_bundled_inputs_functions_and_info(self): all_inputs : Dict[str, Dict[str,List[str]]] = {} z' return all_inputs ) r&r'r(r)r*r;itemsrr?r TypeErrorschema argumentstyper r r._register_attributelen enumeratetupler1append_get_inflate_helper_fn_name _inflate_exprdefinetextwrapdedentjoinsetattrformatrepr)r r!rr"r:.get_bundled_inputs_functions_and_info_templatefunction input_list function_nameargfunction_arg_typesdeflated_inputs_typedeflated_inputspartsinp_idxargs deflated_argsarg_idxinflate_helper_fn_namedeflatedinflaterhelper_definitionrG definition inputs_infos rr0r0sGp eUYY33 49:: GHHu./75Bi3j +  682 & d* 8Z ($--Mx( ( y{{  !*Z*J8GYZ[ [2://2K2KAB2OP3chhPP)1)UW !OE!*:!6 # !$.z$7M#;M?&QXPYYrs!#  S!$-dO ILGS-HRY[h-i**T 6?*m,RS`Q`a&( ; 6 I % LL*  LL* CdN LL& < <= "  sQs2Or_refrhr:c (t|tr|jrj|jdvr)t d|d|jd|jd|jj |}d|d|d}|j ||fS|j |jj |dfSt|tjrE|jjtks|r||dfS|jr*|jtkr|j|dfStjtj fD]}|j| s||j#d k(j%j'sI|j#d jj(|j|d |ddfcSt d|d |jjd ||dfS)N)rrz$Bundled input argument at position 'z' has both arg.fmt_fn => z and arg.fmt => zd. Please choose `arg.fmt` if the deflater is straightforward or `arg.fmt_fn` if you need a function.zself.rC)) memory_formatrz.contiguous(memory_format=z ' is a tensor with storage size z6. You probably don't want to bundle this as an input. )r&rrrr*rYrr'Tensor_typed_storagesizeMAX_RAW_TENSOR_SIZE is_contiguousnumelr5contiguous_format channels_lastflattenallitemexpand)r_rnrhr:rkrGrs rrSrSms #}% ::wwj(:3%@,,/JJ<8**-''3;;!$ 1 12H I 12!C5:D99d$55 599cggnnS147 7#u||$     $ $ &*= =T> !    399;2E#E99;T) )++U-@-@A HC  s 3 a@P9P8U8U8W8\8\8^7 a(..077D%9#a@$HH H 23%8**-*<*<*>*C*C*E)FGC D  C~r script_modulecg}g}t|dr3|jd|jd|jdt|dr|jd|j}|D]}|jd|z|jd|z|jd|zt|d|}t |}t||}t t |j jdz D]>}t |D].} t|| | } t|| s|j| 0@||fS) Nr=get_num_bundled_inputsrun_on_bundled_inputr>get_all_bundled_inputs_for_rBrAr@rg input_idxr^) r;rQr>getattrrNrangerJrKrR) r~methods attributesall_infor^bundled_inputs_fnnum_bundled_inputsfuncrgrhelper_fn_names rr+r+sYGJ}67/0/0-.}EF>? FFH% 7M NN8=H I NN:]J K   9MI J '-m_=! '**;*=&> =-8D T[[%:%:!;a!?@ 7!&'9!:7I%@ '"+&3&N }n=~67 7 70 Z  rrgrr^cd|d|d|S)N_inflate_helper_for__input__arg_rrs rrRrRs "- {%y QQrdtypecdtjd|j|}t|dS)z9Generate a tensor that will be inflated with torch.randn.r@rztorch.randn_like({})rr)r'zerosr}r)rrtstubs r bundle_randnrs- -5;;q & - -t 4D t)? @@rct|dS)z3Wrap a tensor to allow bundling regardless of size.rr)r)ts rbundle_large_tensorrs qd ++r)N)NNNF)NNF)F)$typingrrrrrrcollections.abcr rUr'torch._Cr r torch.jit._recursiver r rurr(r)rPr/r1rr7r2r0boolrrrSr+intrRrrrrrrsGF$ (0 CLJ:GKV 6: Vyy%%Vhxc3h894(S[\abegjbj\kSlJm@m;nnoVuT#YXtCy-@(AABCV (S 2 V  YY Vt7;59$( 3yy%%3%S/233 (S 23tCy! 3  3r6:48 tyy%%tXxsCx(ABBCt (S 2ttHd3i/01 t  tnLQ. ...1.DH. 5ELL !3 56.`%!eii>T>T%!Y^_cdg_hjnorjs_sYt%!PR RRR R#A ,r