/usr/local/lib64/python3.6/site-packages/caffe2/python/__pycache__
NameSizeModeActions
allcompare_test.cpython-36.pyc27100644editdlrm
attention.cpython-36.pyc49380644editdlrm
benchmark_generator.cpython-36.pyc39100644editdlrm
binarysize.cpython-36.pyc48530644editdlrm
brew.cpython-36.pyc38490644editdlrm
brew_test.cpython-36.pyc104520644editdlrm
build.cpython-36.pyc3400644editdlrm
cached_reader.cpython-36.pyc29540644editdlrm
caffe_translator.cpython-36.pyc241650644editdlrm
caffe_translator_test.cpython-36.pyc29290644editdlrm
checkpoint.cpython-36.pyc279730644editdlrm
checkpoint_test.cpython-36.pyc94620644editdlrm
cnn.cpython-36.pyc84350644editdlrm
context.cpython-36.pyc40080644editdlrm
context_test.cpython-36.pyc26650644editdlrm
control.cpython-36.pyc144660644editdlrm
control_ops_grad.cpython-36.pyc163650644editdlrm
control_ops_grad_test.cpython-36.pyc15020644editdlrm
control_ops_util.cpython-36.pyc83320644editdlrm
control_test.cpython-36.pyc118700644editdlrm
convert.cpython-36.pyc1480644editdlrm
convert_test.cpython-36.pyc5680644editdlrm
convnet_benchmarks.cpython-36.pyc129450644editdlrm
convnet_benchmarks_test.cpython-36.pyc10900644editdlrm
core.cpython-36.pyc944280644editdlrm
core_gradients_test.cpython-36.pyc248250644editdlrm
core_test.cpython-36.pyc339420644editdlrm
crf.cpython-36.pyc76220644editdlrm
crf_predict.cpython-36.pyc10440644editdlrm
crf_viterbi_test.cpython-36.pyc16770644editdlrm
dataio.cpython-36.pyc239780644editdlrm
dataio_test.cpython-36.pyc132670644editdlrm
dataset.cpython-36.pyc128790644editdlrm
data_parallel_model.cpython-36.pyc514160644editdlrm
data_parallel_model_test.cpython-36.pyc389230644editdlrm
data_workers.cpython-36.pyc131420644editdlrm
data_workers_test.cpython-36.pyc45220644editdlrm
db_file_reader.cpython-36.pyc53610644editdlrm
db_test.cpython-36.pyc13280644editdlrm
device_checker.cpython-36.pyc40140644editdlrm
dyndep.cpython-36.pyc15680644editdlrm
embedding_generation_benchmark.cpython-36.pyc42030644editdlrm
experiment_util.cpython-36.pyc32330644editdlrm
extension_loader.cpython-36.pyc5430644editdlrm
fakefp16_transform_lib.cpython-36.pyc5460644editdlrm
filler_test.cpython-36.pyc9250644editdlrm
functional.cpython-36.pyc35310644editdlrm
functional_test.cpython-36.pyc41320644editdlrm
fused_8bit_rowwise_conversion_ops_test.cpython-36.pyc36310644editdlrm
gradient_checker.cpython-36.pyc106220644editdlrm
gradient_check_test.cpython-36.pyc167880644editdlrm
gru_cell.cpython-36.pyc25960644editdlrm
hip_test_util.cpython-36.pyc6690644editdlrm
hsm_util.cpython-36.pyc18230644editdlrm
hypothesis_test.cpython-36.pyc843120644editdlrm
hypothesis_test_util.cpython-36.pyc200390644editdlrm
ideep_test_util.cpython-36.pyc10810644editdlrm
layers_test.cpython-36.pyc573720644editdlrm
layer_model_helper.cpython-36.pyc216410644editdlrm
layer_model_instantiator.cpython-36.pyc36650644editdlrm
layer_parameter_sharing_test.cpython-36.pyc56260644editdlrm
layer_test_util.cpython-36.pyc52730644editdlrm
lazy.cpython-36.pyc4240644editdlrm
lazy_dyndep.cpython-36.pyc24750644editdlrm
lazy_dyndep_test.cpython-36.pyc48070644editdlrm
lengths_reducer_fused_8bit_rowwise_ops_test.cpython-36.pyc43920644editdlrm
lengths_reducer_rowwise_8bit_ops_test.cpython-36.pyc36840644editdlrm
lstm_benchmark.cpython-36.pyc72820644editdlrm
memonger.cpython-36.pyc321700644editdlrm
memonger_test.cpython-36.pyc231000644editdlrm
mkl_test_util.cpython-36.pyc11910644editdlrm
model_device_test.cpython-36.pyc33760644editdlrm
model_helper.cpython-36.pyc186180644editdlrm
model_helper_test.cpython-36.pyc19910644editdlrm
modifier_context.cpython-36.pyc26500644editdlrm
muji.cpython-36.pyc55660644editdlrm
muji_test.cpython-36.pyc35670644editdlrm
net_builder.cpython-36.pyc267860644editdlrm
net_builder_test.cpython-36.pyc92550644editdlrm
net_drawer.cpython-36.pyc100560644editdlrm
net_printer.cpython-36.pyc143670644editdlrm
net_printer_test.cpython-36.pyc33230644editdlrm
nomnigraph.cpython-36.pyc51250644editdlrm
nomnigraph_test.cpython-36.pyc145910644editdlrm
nomnigraph_transformations.cpython-36.pyc24310644editdlrm
nomnigraph_transformations_test.cpython-36.pyc40730644editdlrm
normalizer.cpython-36.pyc19410644editdlrm
normalizer_context.cpython-36.pyc15530644editdlrm
normalizer_test.cpython-36.pyc8700644editdlrm
numa_benchmark.cpython-36.pyc18860644editdlrm
numa_test.cpython-36.pyc16260644editdlrm
observer_test.cpython-36.pyc41810644editdlrm
operator_fp_exceptions_test.cpython-36.pyc13050644editdlrm
optimizer.cpython-36.pyc456060644editdlrm
optimizer_context.cpython-36.pyc20080644editdlrm
optimizer_test.cpython-36.pyc247470644editdlrm
optimizer_test_util.cpython-36.pyc68080644editdlrm
parallelize_bmuf_distributed_test.cpython-36.pyc69900644editdlrm
parallel_workers.cpython-36.pyc91480644editdlrm
parallel_workers_test.cpython-36.pyc38240644editdlrm
pipeline.cpython-36.pyc129400644editdlrm
pipeline_test.cpython-36.pyc27150644editdlrm
predictor_constants.cpython-36.pyc3040644editdlrm
python_op_test.cpython-36.pyc105020644editdlrm
queue_util.cpython-36.pyc50550644editdlrm
record_queue.cpython-36.pyc42250644editdlrm
recurrent.cpython-36.pyc98930644editdlrm
regularizer.cpython-36.pyc186040644editdlrm
regularizer_context.cpython-36.pyc15620644editdlrm
regularizer_test.cpython-36.pyc88250644editdlrm
rnn_cell.cpython-36.pyc447850644editdlrm
schema.cpython-36.pyc417630644editdlrm
schema_test.cpython-36.pyc137870644editdlrm
scope.cpython-36.pyc26000644editdlrm
scope_test.cpython-36.pyc40680644editdlrm
session.cpython-36.pyc73460644editdlrm
session_test.cpython-36.pyc23530644editdlrm
sparse_to_dense_mask_test.cpython-36.pyc52980644editdlrm
sparse_to_dense_test.cpython-36.pyc30030644editdlrm
task.cpython-36.pyc222070644editdlrm
task_test.cpython-36.pyc11780644editdlrm
test_util.cpython-36.pyc37290644editdlrm
text_file_reader.cpython-36.pyc22390644editdlrm
timeout_guard.cpython-36.pyc31060644editdlrm
toy_regression_test.cpython-36.pyc23600644editdlrm
transformations.cpython-36.pyc18330644editdlrm
transformations_test.cpython-36.pyc96080644editdlrm
tt_core.cpython-36.pyc64680644editdlrm
tt_core_test.cpython-36.pyc18240644editdlrm
utils.cpython-36.pyc122450644editdlrm
utils_test.cpython-36.pyc14160644editdlrm
visualize.cpython-36.pyc60270644editdlrm
workspace.cpython-36.pyc226900644editdlrm
workspace_test.cpython-36.pyc290550644editdlrm
_import_c_extension.cpython-36.pyc15240644editdlrm
__init__.cpython-36.pyc26070644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/caffe2/python/__pycache__/memonger.cpython-36.pyc (32170B)
3 Eg@sddlZddlZddlZddlZddlmZmZddlm Z ddl Z ddl Z ddl m Z mZddljjZe jdZeje jejdddd gZd[d d Zd\ddZddZd]ddZddZddZddZddZ ddZ!ddZ"d d!Z#d"d#Z$d$d%Z%d&d'Z&d^d(d)Z'd*d+Z(d,d-Z)d.d/Z*d0d1Z+d_d2d3Z,d4d5Z-d`d6d7Z.dad8d9Z/d:d;Z0dd?Z2ejd@dAdBdCgZ3dDdEZ4dFdGZ5GdHdIdIe j6Z7dJdKZ8e&de7j9fdLdMZ:dNdOZ;dPdQZdWdXZ?dYdZZ@dS)bN) workspacecore) caffe2_pb2) viewitems viewvaluesZmemonger LiveRangedefinedusedsizeFc sfddfdd}tjddkr@jd r@d7tj|j}g} t|jj} xB|jjD]4} x.| j D]$} | d| j krx| | krx| j | qxWqlWt| d d} g} x(t |jD]\}} || r| j |qWt}xV|jjD]H} xBt | j t | j D]*} | s,|r| | kr|j| qWqWtj}tj|jd d |D| td d|Djd|d krtn||d krin|}tjdjtj|tj}|j|t|j|stdt|j|std|S)a$ Implements similar optimization as Torch's shareGradInput(): for the gradients that are passed between layers, share blobs between operators when possible. This yields significant memory savings with deep networks. Returns an optimized protobuf (assign to net._net) cs2t|}|jdo0|js*|jdo0|kS)NZ_grad_)strendswith startswith)bname) namescope param_gradsB/usr/local/lib64/python3.6/site-packages/caffe2/python/memonger.py is_grad_blob)sz&share_grad_blobs..is_grad_blobcs.x(t|jt|jD]}|rdSqWdS)NTF)listinputoutput)opr)rrr is_grad_op0sz$share_grad_blobs..is_grad_opz4NOTE: Executing memonger to optimize gradient memory/_wNcSsg|]}t|jdqS)zutf-8)r encode).0srrr Xsz$share_grad_blobs..css|]}t|jdVqdS)zutf-8N)r r)r r!rrr Zsz#share_grad_blobs..zutf-8z)Memonger memory optimization took {} secsz(Memonger graph is not equal to original.z+Inplace assignments differ in memonger net.)logwarnr copydeepcopyProtosetexternal_outputrrrappend enumerateraddtimeC'memonger_compute_blob_recycling_for_dagSerializeToStringrinfoformatrNetDefParseFromStringverify_graph_equalityAssertionErrorverify_inplace_blobs)netZlossesrrZdont_share_blobsZshare_activationsZ blob_shapesrnetprotoZ activationsr+rrZgrad_op_indicesidxZ shared_blobs start_time optim_stroptimr)rrrrshare_grad_blobssP     r@rcstj|j}t|jjt|jjfdd}t}t}t|jj}ddt|jjD}x|D]~} x6| j D],} || r||j | | |kr|t dj | q|Wx | j D]} || r|j | qW|jt| j }| j spt dqpWtj} tj|jdd|D|tdd |D|jd ti} tjd j tj| tj} | j| t|j| svt d t|j| st d | S)Ncs|ko|kS)Nr)r)external_inputr+rris_activation_blobrsz6optimize_inference_for_dag..is_activation_blobcSsg|] \}}|qSrr)r indexrrrrr"xsz.optimize_inference_for_dag..z{} not in external inputzCYou can only pass inference-only nets to optimize_inference_for_dagcSsg|]}t|jdqS)zutf-8)r r)r r!rrrr"scss|]}t|jdVqdS)zutf-8N)r r)r r!rrrr#sz-optimize_inference_for_dag..zutf-8z)Memonger memory optimization took {} secsz(Memonger graph is not equal to original.z+Inplace assignments differ in memonger net.)r'r(r)r*rAr+rrr-rr.r8r4runionZis_gradient_opr/r0r1r2rr%r3rr5r6r7r9)r: input_blobsrr;rBZactivation_blobsZseen_as_outputopsZ op_indicesrrr=r>r?r)rAr+roptimize_inference_for_dagmsL        rGcstddltjjdtjjdtjjdtjjdtjjdtjjdtjj dtjj dtjj dtjj di fddfdd }fd d |D}t jd d }d}d}d}t} x|D]} x| jD]} | jdks| jdkrx| jD]"} | | kr||| 8}| j| qWqxX| jD]N} | | kr|| }||7}||7}t||}| j| || j|7<qWqWqW|||fS)Nrrcs0fdd|jD}|jdd=|jj||S)Ncs$g|]}|jks|jdkr|qS)FreeAlias>rKrL) device_optiontype)r r) devicescoperrr"sz.split_net..)rextend)protorF)rOrr split_nets  z(estimate_memory_usage..split_netcsB|ks|kr$tjdj|dS|}|j|S)NzUnknown blob encountered: {}r)r%warningr4prod)blobsizeof)npshapessizeofstypesrr num_bytess  z(estimate_memory_usage..num_bytescsg|] }|qSrr)r rQ)rRrrr"sz)estimate_memory_usage..cSsdS)Nrrrrrrsz'estimate_memory_usage..rKrL)ZnumpyrZ TensorProtoZDOUBLEFLOATZFLOAT16ZINT32ZINT8ZUINT8ZUINT16ZINT16ZBOOLZINT64 collections defaultdictr*rrNrremovemaxr.)protosrXrZrOr[Z allocs_by_opsZcurrent_allocatedZ max_allocatedZtotal_allocatedZ allocatedrQrornbytesr)rOrWrXrYrRrZrestimate_memory_usagesF            rec CsJt}t}t}tj|}xv|jD]l}|jdkrD|j|jdq$x|jD]}|j|qLWx0|jD]&}||krf|dks||rf|j|qfWq$W|t|j}|j |}||}||}t |j} xft t dt |jD]N} |j| }x>|jD]4}||kr|j|| j| dtjd|g|gqWqW|jdd=|jj| |S)a Insert Free-ops after a blob has been used the last time, so that its memory can be reclaimed. Use this only with efficient caching memory managers (such as CUB, --caffe2_cuda_memory_pool=cub). Blobs used with Alias op won't be freed. @dont_free_blobs: is a set of blobs that should not be freed @selector_fun: optional lambda that return True if blob name can be released. Use for easy special filtering, like excluding blobs with "loss" in the name. Returns a new protobuffer. To use with a model, use: model.net._net = memonger.release_blobs_when_used(..) rLrNrJrK)r*r'r(rrNr.rrr+ intersectionrreversedrangelenr`insertrZCreateOperatorrP) r;Zdont_free_blobsZ selector_funrEZ can_releaseZ alias_blobsrinpZoutprFjrrrrelease_blobs_when_useds8          &  rmcCs2g}x(|D] }t|j|}|s |j|q W|S)z# Return nodes without predecessors )rZ predecessorsr,)gretcnZcur_predrrr_find_source_nodess  rqcCs2g}x(|D] }t|j|}|s |j|q W|S)z! Return nodes without successors )r successorsr,)rnrorpZcur_succrrr_find_target_nodes$s  rscCsjt|}t|dkstt|dkr(|Stj|}dd}||}|j|x|D]}|j||qRW|S)NrJcSs*d}x|D]}||kr |}q W|d7}|S)NrJr)rnrorprrr_next_available_idx5s  z8_add_single_target_ifneeded.._next_available_idx)rsrir8r'r(add_nodeadd_edge)rntargetsroruZtarget_node_idxrprrr_add_single_target_ifneeded.s    ryc stfddDsttfddd}g}|}xP|dk r|j|y||r`||dnd}Wq8tk r||}Yq8Xq8Wtt|S)z. Get the path from nx.bellman_ford()'s output c3s|]}|dkVqdS)rNr)r x) dist_listrrr#Isz_get_path..cs|S)Nr)rz)r{rrr\Ksz_get_path..)keyNr)allr8minr, TypeErrorrrg)Z pred_listr{targetrocurr)r{r _get_pathEs  rc Cstj|}x$|jD]\}}d|||d<qWi}x`|D]X}tj||dd\}}t||} | d|ksltt| d|| d kst| ||<q:W|S)zn Get the longest path for nodes in 'source_nodes' Find with bellman_ford() by setting weight = -1 rJweight)rrrtrt)r'r(edgesnxZ%bellman_ford_predecessor_and_distancerr8ri) rn source_nodesZnguvrorppreddistpathrrr_get_longest_paths\s    rcstfddDsttj}ddD}|j|xDD]<}x6t|dd|ddD]}|j|d|dq`Wq@Wdd }t||||fS) z Build a tree for given paths based on common elements. Last elements of all paths are the same, which is the root of the tree. c3s"|]}|dddkVqdS)rJrNrtrtr)r cp)pathsrrr#tsz_build_tree..cSsh|]}|D]}|q qSrr)r rzyrrr vsz_build_tree..rrJNrtrt)r}r8rDiGraphZadd_nodes_fromziprw_compute_tree_height)rrnZnode_setrZcerootr)rr _build_treeps     rcsfdd|dS)zR Compute the heights of the tree for all nodes Height of leaves are 0 csFtj|}d}|r4fdd|D}t|d}|j|d<|S)Nrcsg|] }|qSrr)r rz) _get_heightrrr"sz=_compute_tree_height.._get_height..rJheight)rrrranodes)rchildrenr child_heights)rrnrrrs z)_compute_tree_height.._get_heightNr)rnrr)rrnrrs rcs$fddfdd|S)z For each node, sort its child nodes based on the height of the nodes. Return the leaf nodes of the tree after sorting. csj|dS)Nr)r)r)rnrrrsz&_sort_tree_leaves.._get_heightcsptj|}|s|gSfdd|Dttt|fddd}g}x |D]}||}||7}qPW|S)Ncsg|] }|qSrr)r rz)rrrr"szA_sort_tree_leaves.._get_sorted_leaves..cs|S)Nr)rz)rrrr\sz?_sort_tree_leaves.._get_sorted_leaves..)r|)rrrsortedrhri)rrorderrocoZcr)r_get_sorted_leavesrn)rrrs z-_sort_tree_leaves.._get_sorted_leavesr)rnrr)rrrnr_sort_tree_leavess  rc st|}t|}t||}ttt|\}}t||}t|t|ksLtt j dkrdt j ||}nt|t |}xB|D]:} t j || } x(| D] } | |kr|j| j| qWqzWtfddtDt jjj|fddd}t|}t|t|jks t|S)a5 The graph 'g' may contain several source nodes (nodes without incoming edge), which could be in any order and still be a valid topological sorting result. We would like to arrange these source nodes so that the average live spans of the computed blobs are shorter. The idea is to sort the source nodes based on the length of their path to the target node so that the one with longer path is used first. This is done by: - Add a single target node if there are multiple target nodes in 'g'. - Find the longest path between each source and the target node. - Convert the longest paths to a tree with the target node being the root and source nodes being the leaves. - Sort the nodes of the tree based on the height of the tree. z2.0c3s"|]\}}|t|fVqdS)N)ri)r ir)dependency_orderrrr#sz:topological_sort_traversal_longest_path..cs|S)Nr)rz)sort_keyrrr\sz9topological_sort_traversal_longest_path..)r|)ryrqrrrrrrr8r __version__topological_sortr*Z descendantsr.r,dictr-Z algorithmsZdagZ lexicographical_topological_sortrir) rngtrZlpathstreerZsorted_sourcesroZ seen_nodesr!descdr)rrr'topological_sort_traversal_longest_paths,       rcCsttj|S)N)rrr)rnrrrtopological_sort_traversalsrc Cs6|stjdtjdd}xt|D]\}}xz|jD]p}||j}|dkrV|}n t||}||j|d||<|r||nd}| s|dk st ||j|d||<q:Wx~|j D]t}||j }|dkr|}n t ||}||j|d||<|r||nd}| s|dk st ||j|d||<qWq(W|S)Nz4Provide blob sizes to get more accurate assignments.cSstddddS)N)rr r )rrrrrr\sz compute_ranges..)r )r )r) r%rSr^r_r-rr ra_replacer8rrr~) linearized_ops blob_sizesblobsrrrUr Z blob_sizerrrrcompute_rangess0        rcCsF|d\}}||krdS|jdks6|jdks6|jdkr:dS|j|jkS)NrJFrt)rr )candidate_range assignment static_blobsrrange_rrr is_compatibles  rcCsJi}x@|D]8}t|dkrq |d\}}x|D]\}}|||<q.Wq W|S)NrJrt)ri) assignmentsblob_assignmentsrZ last_blobr rUrrrcompute_blob_assignmentss   rcCs.|sdStdd|D}|dkr&dn|}|S)NrcSsg|]}|djqS)rJ)r )r rzrrrr" sz!_get_max_size..)ra)rrorrr _get_max_size s rcCs"d}x|D]}|t|7}q W|S)Nr)r)rrorrrrget_memory_usages rc Cs|pg}dd|D}x|D]\}}||kr.qd}d}td}|jpFd} xDt|D]8\} } t|| grRd}tt| | } | |krR| }| }qRW|r||} | j||fq|j||fgqW|S)NcSsh|]}|D] }|dq qS)rr)r rzrrrrrsz-compute_assignments_greedy..FrinfT)floatr r-rabsrr,) ranges_sortedZinit_assignmentsrvisitedrrassignedbest_assignmentZmin_distZcandidate_sizer<rrrrrcompute_assignments_greedys*  rcCs|rtdd|DSdS)z' Return number of blobs in assignments cSsg|] }t|qSr)ri)r rzrrrr"6sz_get_count..r)sum)rrrr _get_count3srcCsFdd}dd}|sdg}|dd7<|rz|dddkrz|ddj|d djg}tjdj|d|d|d|pg}g}xt|D]\}}||||} | dkrtj|n tj|| } || d|d} || | |r|d n||} t| t| t | kst |j tj| qWt |t |ks:t |d } | S) aw Compute assignment for blobs in 'ranges_sorted' on top of 'init_assignment' using dynamic programming + recursion. ranges_sorted: blobs sorted by 'used' init_assignment: assignment to start with, blobs in 'ranges_sorted' should not be used in 'init_assignment' Using f(b, k, init) to represent the best assignment for blobs b[0:k] given initial assignment 'init', we have f(b, k, init) = f(b, j, init) + find_best(b[j:k], f(b, j, init)) where j is the index of the last best assignment that is independent of blob b[k - 1] (b[k - 1] is compatible with all assignments in f(b, j, init)), and find_best(b1, init1) gives the best assignment for blobs in 'b1' based on the initial assignment 'init1', and blobs b1[0:-1] should be incompatible with b1[-1]. f(b, len(b), []) gives the best assignment for blobs 'b'. For find_best(b, init), since b[0:-1] are not compatible with b[-1], we could reduce it to a smaller problem to find best assignment for b[0:-1] as find_best(b, init) = min { f(b[0:-1], len(b) - 1, init - x) + [x, b[-1]] for x in init, or f(b[0:-1], len(b) - 1, init) + [b[-1]] } where min{} gives the assignment with minimum memory usage. cSs@dd}|d}x*|dkr:||}|||r0|S|d8}qWdS)z Find closest position k of best_assignments that is independent of candidate_range that candiate_range is compatible with all assignments in best_assignments[k]. Return -1 if not found. cstfdd|DS)z> return true if compatible for all assignments in assignments csg|]}td|gqS)rJ)r)r rz)rrrr"_szccompute_assignments_dp.._get_compatible_prev..is_compatible_all..)r})rrr)rris_compatible_all]szOcompute_assignments_dp.._get_compatible_prev..is_compatible_allrJrrtr)rbest_assignmentsZcur_idxriiZcbarrr_get_compatible_prevWs   z4compute_assignments_dp.._get_compatible_prevc s|d tfdd|dd Ds*tt|}g}xt|D]td|gsZq@tj|}|jt|dkrfddt|D}t |dd ||}||g}|j|q@W|j|ggt |dd d }|S)a* Find the best assignment for blobs 'ranges' given an initialized assignment 'init_assignment'. Blobs in ranges[0:-1] should be incompatible with blob range[-1]. 'prev_best_assignment': best assignment for blobs in ranges[:-1] By assigning ranges[-1] to each assignment k in 'init_assignment' or in a new assignment, the problem becomes a smaller problem to find the best assignment for ranges[0:-1] given the initial assignment init_assigment[0:k, (k+1):-1]. rJc3s"|]}t|dgg VqdS)rJN)r)r rz) find_rangerrr#ysz=compute_assignments_dp.._find_best..rcsg|]\}}|kr|qSrr)r rrz)rrrr"sz>compute_assignments_dp.._find_best..NcSst|S)N)r)rzrrrr\sz._find_best..)r|rtrtrt) r}r8rirhrr'r(r,r-compute_assignments_dpr~) rangesinit_assignmentZprev_best_assignmentcounterszZbest_candidatescur_bestZ cur_best_tmpror)rrr _find_bestis$ "  z*compute_assignments_dp.._find_bestrrJiz$Finding assignments {} ({} -> {})...rtrtrt) rr r%r3r4r-r'r(rrir8r,)rrrrrrsrrZ cur_rangeZprev_idxZ prev_bestZ ranges_partrbestrrrr:s2' rcs8dd}dddkr ||fdd|D}|S)z Set LiveRange.defined = -1 if it is None Set LiveRange.used = max_live if it is None Set LiveRanee.size = 1 if it is None cSstdd|Dd}|S)Ncss"|]}|djr|djVqdS)rJN)r )r rzrrrr#sz._get_max_live..rJ)ra)rmax_liverrr _get_max_livesz)get_updated_ranges.._get_max_livecSsz|}|djdkr*|d|djddf}|djdkrP|d|dj|df}|djdkrv|d|dj|df}|S)NrJr)r)r )r rt)rrr r )rzrr Zcxrrr _update_rangesz)get_updated_ranges.._update_rangeNcsg|]}|dqS)rJr)r rz)rrrrr"sz&get_updated_ranges..r)rrrr)rrrget_updated_rangess  rcstt|ddd}t|}fdd|D}fdd|D}tjdjt|g}|tjkrnt |g}n$|tj krt |g}ndj|st |d d|D7}|S) a] algo: Method used to find assignments (AssignmentAlgorithm.GREEDY or AssignmentAlgorithm.DYNAMIC_PROGRAMMING). AssignmentAlgorithm.DYNAMIC_PROGRAMMING gives optimal solution at the cost of more computation. AssignmentAlgorithm.GREEDY may be better in the case 'blob_sizes' is not provided. cSs|djdk|djfS)NrJ)r )prrrr\sz%compute_assignments..)r|csg|]}|dkr|qS)rr)r rz)rrrr"sz'compute_assignments..csg|]}|dkr|qS)rr)r rz)rrrr"szTotal sharable blobs {}zInvalid algo name {}cSsg|] }|gqSrr)r rzrrrr"s) rrrr%r3r4riAssignmentAlgorithmDYNAMIC_PROGRAMMINGrGREEDYrr8)rralgoZranges_sharableZ ranges_staticrr)rrcompute_assignmentss     rcCsRxL|D]D}x>t|dd|ddD] \}}|dj|djks&tq&WqWdS)NrrJrt)rr rr8)rrrzrrrrverify_assignmentss $rcstj}x"t|D]\}}|j||dqWxt|D]t\}}xjt|D]^\}||krZqHtfdd|jDrHtjj|j}|j |||dtj |sHt qHWq6W|S)N)rc3s|]}|jkVqdS)N)r)r r)child_oprrr#sz-compute_interference_graph..)deps) rrr-rvanyrr*rrfrwZis_directed_acyclic_graphr8)rFrnrrZ parent_oprlrr)rrcompute_interference_graphsr Optimizationr:rrcsfdd}xr|jD]h}|jjdr0t||x$t|jD]\}}|||j|<q.canonical_nameZRecurrentNetwork)rrNr apply_recurrent_blob_assignmentsr-rr)r:rrrrZinput_rr)rrapply_assignmentss    rc Cstjdj|jdd|jD}xJ|D]B}t|j|x0t|jjD] \}}||krF|||jj|<qFWq(Wx\t |D]P\}}|t |j t |j krxt j} |d| _t|jd| _|jj| gqxWdS)Nz(Applying assignments to recurrent op: {}cSsg|]}|jjdr|qS)Zstep_net)rr )r arrrr"$sz4apply_recurrent_blob_assignments..z.renameascii)r%debugr4rNargrnr-rArrrrrZArgumentrr rr!rP) rrrZ step_argsZstep_argrZeinprUZrenamedrrrrr"s   rc@seZdZdZdZdS)rrrJN)__name__ __module__ __qualname__rrrrrrr3srcCs0tj}tj|jdd|D}|j||S)NcSsg|]}t|jdqS)zutf-8)r r)r r!rrrr"<sz+optimize_inference_fast..)rr5r0Zmemonger_optimize_inference_netr2r6)r:rr?r>rrroptimize_inference_fast8s  rc s|tjtj}||}fdd|D}jdd=jj|t||}t|||} t| } t| t | | dS)a_ ordering_function: topological_sort_traversal or topological_sort_traversal_longest_path. topological_sort_traversal_longest_path gives better results but needs a bit more computation. algo: Method used to find assignments (AssignmentAlgorithm.GREEDY or AssignmentAlgorithm.DYNAMIC_PROGRAMMING). AssignmentAlgorithm.DYNAMIC_PROGRAMMING gives optimal solution at the cost of more computation. AssignmentAlgorithm.GREEDY may be better in the case 'blob_sizes' is not provided. csg|]}j|qSr)r)r r)r:rrr"_sz)optimize_interference..N)r:rr) r'r(rrrPrrrrr) r:rZordering_functionrrrnZorderingrrrrr)r:roptimize_interferenceBs       rcCsLdd}x>t|j|jD],\}}|j|jkr0dS||||krdSqWdS)z Verifies that net_a and net_b have the same in-place blob assignments. Particularly, that memonger did not add an in-place assignment when that did not exist before. cSsFt|j}g}x2t|jD]$\}}||kr|j||j|gqW|S)N)rrr-rr,rC)routZinplacesrlrkrrr get_inplacesxs  z*verify_inplace_blobs..get_inplacesFT)rrrN)net_anet_brop_aop_brrrr9rs r9c Csdd}t|jt|jkr dSxBt|j|jD]0\}}|j|jks\|j|jks\|j|jkr0dSq0W||j}||j}||krd}xTt||D]F\}} || krtdj||j||j|tdj|| |d7}qW||kS)a Determines if the execution of two graphs are identical. That is, all inputs blobs are mapped to the same output blobs for each operator in their respective positions. This is meant to check the output of memonger with the original graph. It assumes that the nets have same external input and output. O(E) runtime + O(1) amortized cost to hash for python dict cSstdd|D}i}x\t|D]P\}}x.|jD]$}|j|}|dk r,||j|q,Wx|jD] }|||<q\WqW|S)NcSsg|]}gqSrr)r r rrrr"sz>verify_graph_equality..parent_list..)r-rgetr,r)rF parent_listZ edge_ownerrrrUZ parent_idrrrrs   z*verify_graph_equality..parent_listFrzDifference {} vs {} {}zParents: {} vs {}rJ)rirrrNrMZengineprintr4) rrrrrZ parent_list_aZ parent_list_brlrrrrrr7s&       r7 Statisticsbaseline_nbytesoptimized_nbytesc Cs>d}ytj|j}Wn$tk r8tjdj|YnX|S)NrzError when fetching blob {})rZ FetchBlobrd Exceptionr%rSr4)rUrrrr blob_nbytess rcs<dd|Dtt}tfdd|D}t||dS)NcSs$i|]}|D]\}}t||q qSr)r)r rrUr rrr sz&compute_statistics..c3s$|]}tfdd|DVqdS)c3s|]\}}|VqdS)Nr)r rUr ) blob_bytesrrr#sz/compute_statistics...N)ra)r r)rrrr#sz%compute_statistics..)rr)rrr)rrrr)rrcompute_statisticss   rcCsPi}xF|jD]<}x|jD]}t|||<qWx|jD]}t|||<q4Wq W|S)N)rrrr)r:rrrUrrrcollect_blob_sizess   r)NFN)r)N)N)N)N)N)AZnetworkxrr^r/r'Z caffe2.pythonrrZ caffe2.protorenumloggingZ future.utilsrrZ!caffe2.python._import_c_extensionpythonZ_import_c_extensionr0 getLoggerr%setLevelINFO namedtuplerr@rGrermrqrsryrrrrrrrrrrrrrrrrrrrrrrEnumrrrrr9r7rrrrrrrrsp     N 4D 5  )     x * -3