(beta) Using TORCH_LOGS python API with torch.compile
Author: Michael Lazos
import logging
Author: Michael Lazos
import logging
This tutorial introduces the TORCH_LOGS environment variable, as well as the Python API, and
demonstrates how to apply it to observe the phases of torch.compile.
In this example, we’ll set up a simple Python function which performs an elementwise
add and observe the compilation process with TORCH_LOGS Python API.
import torch
# exit cleanly if we are on a device that doesn't support torch.compile
if torch.cuda.get_device_capability() < (7, 0):
print("Skipping because torch.compile is not supported on this device.")
else:
@torch.compile()
def fn(x, y):
z = x + y
return z + 2
inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda"))
# print separator and reset dynamo
# between each example
def separator(name):
print(f"==================={name}=========================")
torch._dynamo.reset()
separator("Dynamo Tracing")
# View dynamo tracing
# TORCH_LOGS="+dynamo"
torch._logging.set_logs(dynamo=logging.DEBUG)
fn(*inputs)
separator("Traced Graph")
# View traced graph
# TORCH_LOGS="graph"
torch._logging.set_logs(graph=True)
fn(*inputs)
separator("Fusion Decisions")
# View fusion decisions
# TORCH_LOGS="fusion"
torch._logging.set_logs(fusion=True)
fn(*inputs)
separator("Output Code")
# View output code generated by inductor
# TORCH_LOGS="output_code"
torch._logging.set_logs(output_code=True)
fn(*inputs)
separator("")
===================Dynamo Tracing=========================
I1106 19:55:07.074000 33309 torch/_dynamo/utils.py:2236] [0/0] ChromiumEventLogger initialized with id 4ae2f283-cfde-4e9d-9585-5854ef5b0524
V1106 19:55:07.076000 33309 torch/_dynamo/convert_frame.py:2108] [0/0] torchdynamo start compiling fn /__wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy stack elided frames
V1106 torch_dynamoconvert_framepy line module
V1106 torch_dynamoconvert_framepy sys
V1106 torch_dynamoconvert_framepy line main
V1106 torch_dynamoconvert_framepy argv
V1106 torch_dynamoconvert_framepy line make_main
V1106 torch_dynamoconvert_framepy make_modeargv
V1106 torch_dynamoconvert_framepy line run_make_mode
V1106 torch_dynamoconvert_framepy makeargs
V1106 torch_dynamoconvert_framepy line run_generic_build
V1106 torch_dynamoconvert_framepy args opts
V1106 torch_dynamoconvert_framepy line build_main
V1106 torch_dynamoconvert_framepy app argssourcedir argsconfdir argsoutputdir
V1106 torch_dynamoconvert_framepy line __init__
V1106 torch_dynamoconvert_framepy
V1106 torch_dynamoconvert_framepy line _init_builder
V1106 torch_dynamoconvert_framepy events
V1106 torch_dynamoconvert_framepy line emit
V1106 torch_dynamoconvert_framepy resultslistenerapp args
V1106 torch_dynamoconvert_framepy line generate_gallery_rst
V1106 torch_dynamoconvert_framepy
V1106 torch_dynamoconvert_framepy line generate_dir_rst
V1106 torch_dynamoconvert_framepy results
V1106 torch_dynamoconvert_framepy line genexpr
V1106 torch_dynamoconvert_framepy fname target_dir src_dir gallery_conf fname iterator
V1106 torch_dynamoconvert_framepy line wrapper
V1106 torch_dynamoconvert_framepy p
V1106 torch_dynamoconvert_framepy line start
V1106 torch_dynamoconvert_framepy _popen
V1106 torch_dynamoconvert_framepy line _Popen
V1106 torch_dynamoconvert_framepy _default_contextprocess_obj
V1106 torch_dynamoconvert_framepy line _Popen
V1106 torch_dynamoconvert_framepy process_obj
V1106 torch_dynamoconvert_framepy line __init__
V1106 torch_dynamoconvert_framepy process_obj
V1106 torch_dynamoconvert_framepy line _launch
V1106 torch_dynamoconvert_framepy code process_objparent_sentinelchild_r
V1106 torch_dynamoconvert_framepy line _bootstrap
V1106 torch_dynamoconvert_framepy
V1106 torch_dynamoconvert_framepy line run
V1106 torch_dynamoconvert_framepy _args _kwargs
V1106 torch_dynamoconvert_framepy line call_fn
V1106 torch_dynamoconvert_framepy result args kwargs
V1106 torch_dynamoconvert_framepy line generate_file_rst
V1106 torch_dynamoconvert_framepy output_blocks time_elapsed
V1106 torch_dynamoconvert_framepy line execute_script
V1106 torch_dynamoconvert_framepy
V1106 torch_dynamoconvert_framepy line execute_code_block
V1106 torch_dynamoconvert_framepy is_last_expr mem_max
V1106 torch_dynamoconvert_framepy line _exec_and_get_memory
V1106 torch_dynamoconvert_framepy mem_max _
V1106 torch_dynamoconvert_framepy line _sg_call_memory_noop
V1106 torch_dynamoconvert_framepy
V1106 torch_dynamoconvert_framepy line __call__
V1106 torch_dynamoconvert_framepy code fake_main__dict__
V1106 torch_dynamoconvert_framepy line module
V1106 torch_dynamoconvert_framepy inputs
V1106 torch_dynamoconvert_framepy
I1106 torch_dynamosymbolic_convertpy torchdynamo start tracing fn __wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy
I1106 torchfxexperimentalsymbolic_shapespy create_env
V1106 torch_dynamosymbolic_convertpy __trace_source TRACE starts_line __wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy fn
V1106 torch_dynamosymbolic_convertpy __trace_source
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE RESUME
V1106 torch_dynamosymbolic_convertpy __trace_source TRACE starts_line __wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy fn fn
V1106 torch_dynamosymbolic_convertpy __trace_source z x y
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE LOAD_FAST x
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE LOAD_FAST y unrealized
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE BINARY_OP unrealized unrealized
V1106 torch_dynamovariablesbuilderpy wrap_to_fake L dynamic_sizesSTATIC STATIC dynamic_stridesINFER_STRIDE INFER_STRIDE constraint_sizes constraint_strides specialize_on view_base_context shape_ids unbacked_bounds tensor_sourcelocal_name is_input dynamism is_derefed_cell_contents shape_env_to_source_to_symbol_cache excluded_sizes
V1106 torch_dynamooutput_graphpy create_graph_input L device size at debug_level before
V1106 torch_dynamovariablesbuilderpy wrap_to_fake L dynamic_sizesSTATIC STATIC dynamic_stridesINFER_STRIDE INFER_STRIDE constraint_sizes constraint_strides specialize_on view_base_context shape_ids unbacked_bounds tensor_sourcelocal_name is_input dynamism is_derefed_cell_contents shape_env_to_source_to_symbol_cache excluded_sizes
V1106 torch_dynamooutput_graphpy create_graph_input L device size at debug_level before
V1106 torch_dynamooutput_graphpy __trace_call TRACE FX call add __wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy fn fn
V1106 torch_dynamooutput_graphpy __trace_call z x y
V1106 torch_dynamooutput_graphpy __trace_call
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE STORE_FAST z
V1106 torch_dynamosymbolic_convertpy __trace_source TRACE starts_line __wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy fn fn
V1106 torch_dynamosymbolic_convertpy __trace_source z
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE LOAD_FAST z
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE LOAD_CONST
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE BINARY_OP
V1106 torch_dynamooutput_graphpy __trace_call TRACE FX call add_1 __wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy fn fn
V1106 torch_dynamooutput_graphpy __trace_call z
V1106 torch_dynamooutput_graphpy __trace_call
V1106 torch_dynamosymbolic_convertpy __trace_bytecode TRACE RETURN_VALUE
I1106 torch_dynamosymbolic_convertpy torchdynamo done tracing fn RETURN_VALUE
V1106 torch_dynamosymbolic_convertpy triggered compile
V1106 torch_dynamooutput_graphpy COMPILING GRAPH due to reason user_stack file __wtutorialstutorialspytorchtutorialsrecipes_sourcetorch_logspy line fn graph_break
V1106 torch_dynamooutput_graphpy __graph_code TRACED GRAPH
V1106 torch_dynamooutput_graphpy __graph_code __compiled_fn_1_eb096d6e_9ffd_4b27_bf7d_692f487dd43a
V1106 torch_dynamooutput_graphpy __graph_code usrlocallibpython3distpackagestorchfx_lazy_graph_modulepy torchnn
V1106 torch_dynamooutput_graphpy __graph_code
V1106 torch_dynamooutput_graphpy __graph_code l_x_
V1106 torch_dynamooutput_graphpy __graph_code l_y_
V1106 torch_dynamooutput_graphpy __graph_code
V1106 torch_dynamooutput_graphpy __graph_code
V1106 torch_dynamooutput_graphpy __graph_code z l_x_ l_y_ l_x_ l_y_
V1106 torch_dynamooutput_graphpy __graph_code
V1106 torch_dynamooutput_graphpy __graph_code
V1106 torch_dynamooutput_graphpy __graph_code add_1 z z
V1106 torch_dynamooutput_graphpy __graph_code add_1
V1106 torch_dynamooutput_graphpy __graph_code
V1106 torch_dynamooutput_graphpy __graph_code
I1106 torch_dynamooutput_graphpy calling compiler function inductor
I1106 torchfxexperimentalsymbolic_shapespy produce_guards
usrlocallibpython3distpackagestritonknobspy TRITON_DEFAULT_FP_FUSION unset defaulting to FP fusion ENABLED backward compatibility default will change to DISABLED TRITON_DEFAULT_FP_FUSION to keep fusion enabled to adopt the new default early silence this warning
FP_FUSION_DEFAULT_WARNING
I1106 torchfxexperimentalsymbolic_shapespy produce_guards
I1106 torch_dynamooutput_graphpy done compiler function inductor
I1106 torchfxexperimentalsymbolic_shapespy produce_guards
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy track_symint L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torchfxexperimentalsymbolic_shapespy guard L
V1106 torch_dynamoguardspy __guards GUARDS
V1106 torch_dynamoguardspy __guards
V1106 torch_dynamoguardspy __guards TREE_GUARD_MANAGER
V1106 torch_dynamoguardspy __guards
V1106 torch_dynamoguardspy __guards LAMBDA_GUARD torch_functorchaot_autogradutilstop_saved_tensors_hooks ids
V1106 torch_dynamoguardspy __guards GLOBAL_STATE against falsetruefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsetruetruefalse
V1106 torch_dynamoguardspy __guards TORCH_FUNCTION_MODE_STACK
V1106 torch_dynamoguardspy __guards DEFAULT_DEVICE utils_deviceCURRENT_DEVICE
V1106 torch_dynamoguardspy __guards sourceL accessed_bykey framelocals_idx tag_safe
V1106 torch_dynamoguardspy __guards TENSOR_MATCH L CUDA torchfloat32 device requires_grad size stride
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards NO_TENSOR_ALIASING L L
V1106 torch_dynamoguardspy __guards sourceL accessed_bykey framelocals_idx tag_safe
V1106 torch_dynamoguardspy __guards TENSOR_MATCH L CUDA torchfloat32 device requires_grad size stride
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards DIMENSION_DYNAMIC_MARKING_GUARD L
V1106 torch_dynamoguardspy __guards NO_TENSOR_ALIASING
V1106 torch_dynamoguardspy __guards
V1106 torch_dynamoguardspy __guards eval latency us
I1106 torch_dynamopgopy put_code_state no cache key skipping
I1106 torch_dynamoconvert_framepy run_gc_after_compile running gc
V1106 torch_dynamoconvert_framepy skipping store_user_object_weakrefs reason frame the skipfiles file usrlocallibpython3distpackagestorch_dynamograph_bytecode_inputspy
V1106 torch_dynamoconvert_framepy skipping _fn reason frame the skipfiles file usrlocallibpython3distpackagestorch_dynamoeval_framepy
I1106 torch_dynamo__init__py torch_dynamoreset
I1106 torch_dynamo__init__py torch_dynamoreset_code_caches
V1106 torch_inductorcodecachepy __output_code code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code ctypes c_void_p c_long c_int
V1106 torch_inductorcodecachepy __output_code torch
V1106 torch_inductorcodecachepy __output_code math
V1106 torch_inductorcodecachepy __output_code random
V1106 torch_inductorcodecachepy __output_code os
V1106 torch_inductorcodecachepy __output_code tempfile
V1106 torch_inductorcodecachepy __output_code math inf nan
V1106 torch_inductorcodecachepy __output_code cmath nanj
V1106 torch_inductorcodecachepy __output_code torch_inductorhooks run_intermediate_hooks
V1106 torch_inductorcodecachepy __output_code torch_inductorutils maybe_profile
V1106 torch_inductorcodecachepy __output_code torch_inductorcodegenmemory_planning _align align
V1106 torch_inductorcodecachepy __output_code torch device empty_strided
V1106 torch_inductorcodecachepy __output_code torch_inductorasync_compile
V1106 torch_inductorcodecachepy __output_code torch_inductorselect_algorithm extern_kernels
V1106 torch_inductorcodecachepy __output_code torch_C_dynamoguards copy_if_misaligned
V1106 torch_inductorcodecachepy __output_code triton
V1106 torch_inductorcodecachepy __output_code tritonlanguage tl
V1106 torch_inductorcodecachepy __output_code torch_inductorruntimetriton_heuristics start_graph end_graph
V1106 torch_inductorcodecachepy __output_code torch_C _cuda_getCurrentRawStream get_raw_stream
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code aten torchopsaten
V1106 torch_inductorcodecachepy __output_code inductor_ops torchopsinductor
V1106 torch_inductorcodecachepy __output_code _quantized torchops_quantized
V1106 torch_inductorcodecachepy __output_code assert_size_stride torch_C_dynamoguardsassert_size_stride
V1106 torch_inductorcodecachepy __output_code assert_size_stride_grouped torch_C_dynamoguardsassert_size_stride_grouped
V1106 torch_inductorcodecachepy __output_code assert_alignment torch_C_dynamoguardsassert_alignment
V1106 torch_inductorcodecachepy __output_code empty_strided_cpu torch_C_dynamoguards_empty_strided_cpu
V1106 torch_inductorcodecachepy __output_code empty_strided_cpu_pinned torch_C_dynamoguards_empty_strided_cpu_pinned
V1106 torch_inductorcodecachepy __output_code empty_strided_cuda torch_C_dynamoguards_empty_strided_cuda
V1106 torch_inductorcodecachepy __output_code empty_strided_xpu torch_C_dynamoguards_empty_strided_xpu
V1106 torch_inductorcodecachepy __output_code empty_strided_mtia torch_C_dynamoguards_empty_strided_mtia
V1106 torch_inductorcodecachepy __output_code reinterpret_tensor torch_C_dynamoguards_reinterpret_tensor
V1106 torch_inductorcodecachepy __output_code alloc_from_pool torchopsinductor_alloc_from_pool
V1106 torch_inductorcodecachepy __output_code async_compile
V1106 torch_inductorcodecachepy __output_code empty_strided_p2p torch_C_distributed_c10d_SymmetricMemoryempty_strided_p2p
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code triton_poi_fused_add_0 async_compile device_str
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code async_compile
V1106 torch_inductorcodecachepy __output_code async_compile
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code partitions
V1106 torch_inductorcodecachepy __output_code partitions partitions
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code fns
V1106 torch_inductorcodecachepy __output_code new_callables
V1106 torch_inductorcodecachepy __output_code fn c fns partitions
V1106 torch_inductorcodecachepy __output_code new_callablesc
V1106 torch_inductorcodecachepy __output_code partitions new_callables
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code args
V1106 torch_inductorcodecachepy __output_code arg0_1 arg1_1 args
V1106 torch_inductorcodecachepy __output_code args
V1106 torch_inductorcodecachepy __output_code arg0_1 arg1_1
V1106 torch_inductorcodecachepy __output_code torchcuda
V1106 torch_inductorcodecachepy __output_code torchcuda
V1106 torch_inductorcodecachepy __output_code arg0_1 arg0_1
V1106 torch_inductorcodecachepy __output_code arg1_1 arg1_1
V1106 torch_inductorcodecachepy __output_code buf0 torchfloat32
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code raw_stream0
V1106 torch_inductorcodecachepy __output_code triton_poi_fused_add_0arg0_1 arg1_1 buf0 streamraw_stream0
V1106 torch_inductorcodecachepy __output_code arg0_1
V1106 torch_inductorcodecachepy __output_code arg1_1
V1106 torch_inductorcodecachepy __output_code buf0
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code runner partitions
V1106 torch_inductorcodecachepy __output_code call runnercall
V1106 torch_inductorcodecachepy __output_code recursively_apply_fns runnerrecursively_apply_fns
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code torch_dynamotesting rand_strided
V1106 torch_inductorcodecachepy __output_code arg0_1 device dtypetorchfloat32
V1106 torch_inductorcodecachepy __output_code arg1_1 device dtypetorchfloat32
V1106 torch_inductorcodecachepy __output_code arg0_1 arg1_1
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code args times repeat
V1106 torch_inductorcodecachepy __output_code torch_inductorutils print_performance
V1106 torch_inductorcodecachepy __output_code fn args
V1106 torch_inductorcodecachepy __output_code fn timestimes repeatrepeat device
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code __name__
V1106 torch_inductorcodecachepy __output_code torch_inductorwrapper_benchmark compiled_module_main
V1106 torch_inductorcodecachepy __output_code args
V1106 torch_inductorcodecachepy __output_code times repeat args timestimes repeatrepeat
V1106 torch_inductorcodecachepy __output_code
V1106 torch_inductorcodecachepy __output_code code written to tmptorchinductor_ciuseruxcuxyavs7ioi4ujp4mhgtfmfkn44t2dzmmymfxxsi5puqwo5n3wsapy
In this tutorial we introduced the TORCH_LOGS environment variable and python API by experimenting with a small number of the available logging options. To view descriptions of all available options, run any python script which imports torch and set TORCH_LOGS to “help”.
Alternatively, you can view the torch._logging documentation to see descriptions of all available logging options.
For more information on torch.compile, see the torch.compile tutorial.
Total running time of the script: (0 minutes 2.716 seconds)