torch.utils
Return a string containing the C++ stack trace of the current thread. | |
Return a string containing the C++ stack trace of the current thread. | |
set_moduleSet the module attribute on a python object for a given object for nicer printing |
This function swaps the content of the two Tensor objects. |
Automatically generate attributes and methods for the custom backend after rename privateuse1 backend. | |
Rename the privateuse1 backend device to make it more convenient to use as a device name within PyTorch APIs. |
Mark a function as an unserializable hook with this decorator.
This suppresses warnings that would otherwise arise if you attempt to serialize a tensor that has a hook.
Define time formatting.
Return (return-code, stdout, stderr). | |
Run command using run_lambda, returns the first regex match if it exists. | |
Run command using run_lambda; reads and returns entire output if rc is 0. | |
Run command using run_lambda and returns first line if output is not empty. |
Utilities for counting theoretical floating point operations.
FlopCounterMode is a context manager that intercepts PyTorch operators and
adds up their registered FLOP formulas. The result is a shape-based,
theoretical FLOP count for the operators that ran inside the context, not a
hardware performance measurement.
The counter is useful when comparing model graphs, activation checkpointing plans, or shape changes with a stable FLOP accounting rule. It should not be read as kernel instructions, wall-clock time, memory bandwidth, achieved FLOP/s, Tensor Core utilization, or the exact work done by a fused kernel.
Counts are produced by formulas in flop_registry. Operators without a
formula may be decomposed into registered operators; otherwise they add zero
FLOPs.
Formula inputs have tensor arguments replaced by their shapes by default.
Non-tensor arguments pass through unchanged. Use
register_flop_formula(..., get_raw=True) only when the formula needs the
original tensor arguments or metadata.
Forward and backward operations are counted only if they execute while the context manager is active.
The default formulas use dense, naive mathematical definitions. For example,
matrix multiplication counts 2 * m * n * k FLOPs.
Counts do not automatically adjust for dtype-specific throughput, Tensor Cores, sparsity, quantization, masking, skipped elements, memory movement, or data-dependent early exits. A formula must explicitly model those semantics. For example, the built-in attention formulas are upper bounds for causal or otherwise masked attention unless the formula explicitly models the mask.
Higher-order operators and torch.compile may expose decomposed or fused
work differently from eager execution. Custom operators and custom Triton
kernels need a formula or a decomposition if they should contribute FLOPs.
Module attribution is tracked through ModuleTracker. Totals are always
available under "Global"; submodule rows are best-effort attribution for
module calls observed during the context.
The workload still executes normally. Use this mode for a few representative iterations rather than long training runs if overhead or memory use matters.
import torch
from torch.utils.flop_counter import FlopCounterMode
model = torch.nn.Linear(16, 32)
x = torch.randn(4, 16)
with FlopCounterMode(display=False
Register custom FLOP formulas before constructing FlopCounterMode. The
mode snapshots the global registry during initialization.
from math import prod
import torch
from torch.utils.flop_counter import FlopCounterMode, register_flop_formula
@torch.library.custom_op("example::scale", mutates_args=())
def
Count theoretical FLOPs for operators that run inside the context. | |
Count flops for the baddbmm operation. | |
The Python Hipify script. ## # Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved. # 2017-2018 Advanced Micro Devices, Inc. and # Facebook Inc. All rights reserved. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE.
adds dim3() to the second and third arguments in the kernel launch | |
Return the list of arguments in the upcoming function parameter closure. | |
Finds the first balanced parentheses. | |
Generalization for finding a balancing closure group | |
Finds the first balanced bracket. | |
Static global kernels in HIP results in a compilation error. | |
Returns the new name of the hipified file | |
If the file makes kernel builtin calls and does not include the cuda_runtime.h header, then automatically add an #include to match the "magic" includes provided by NVCC. | |
Helper method to see if filename ends with certain extension | |
Executes the CUDA -> HIP conversion on the specified file. | |
Replace the CUDA style Kernel launches with the HIP style kernel launches. | |
Match 'extern __shared__ type foo[];' syntax and use HIP_DYNAMIC_SHARED() MACRO instead. | |
FIXME: Temporarily replace std:: invocations of math functions with non-std:: versions to prevent linker errors NOTE: This can lead to correctness issues when running tests, since the correct version of the math function (exp/expf) might not get called. | |
ArgumentParser doesn't support type=bool. |
Wrapper around a weak ref of a Tensor that handles the _fix_weakref() call required when unwrapping a Tensor weakref. |
Count flops for the bmm operation. |
Count flops for convolution. |
Count flops for convolution. |
Count flops for self-attention backward. |
Count flops for self-attention. |
Count flops for self-attention. |