Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- lib/python3.12/site-packages/torch/_refs/__pycache__/_conversions.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_refs/__pycache__/fft.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_refs/linalg/__init__.pyAdded+309
- lib/python3.12/site-packages/torch/_refs/linalg/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_refs/nn/__init__.pyAdded+4
- lib/python3.12/site-packages/torch/_refs/nn/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_refs/nn/functional/__init__.pyAdded+1280
- lib/python3.12/site-packages/torch/_refs/nn/functional/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_refs/special/__init__.pyAdded+236
- lib/python3.12/site-packages/torch/_refs/special/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_strobelight/__init__.pyAdded
- lib/python3.12/site-packages/torch/_strobelight/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_strobelight/__pycache__/cli_function_profiler.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_strobelight/__pycache__/compile_time_profiler.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/_strobelight/cli_function_profiler.pyAdded+311
- lib/python3.12/site-packages/torch/_strobelight/compile_time_profiler.pyAdded+195
- lib/python3.12/site-packages/torch/compiler/__init__.pyAdded+404
- lib/python3.12/site-packages/torch/compiler/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/compiler/__pycache__/config.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/compiler/config.pyAdded+67
- lib/python3.12/site-packages/torch/distributed/__init__.pyAdded+157
- lib/python3.12/site-packages/torch/distributed/_checkpointable.pyAdded+38
- lib/python3.12/site-packages/torch/distributed/_composable_state.pyAdded+44
- lib/python3.12/site-packages/torch/distributed/_functional_collectives.pyAdded+1191
- lib/python3.12/site-packages/torch/distributed/_functional_collectives_impl.pyAdded+117
- lib/python3.12/site-packages/torch/distributed/_shard/__init__.pyAdded+1
- lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/_utils.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/api.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/common_op_utils.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/metadata.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/op_registry_utils.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/sharder.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/_utils.pyAdded+32
- lib/python3.12/site-packages/torch/distributed/_shard/api.pyAdded+306
- lib/python3.12/site-packages/torch/distributed/_shard/checkpoint/__init__.pyAdded+19
- lib/python3.12/site-packages/torch/distributed/_shard/checkpoint/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/common_op_utils.pyAdded+65
- lib/python3.12/site-packages/torch/distributed/_shard/metadata.pyAdded+64
- lib/python3.12/site-packages/torch/distributed/_shard/op_registry_utils.pyAdded+41
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/__init__.pyAdded+52
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/__pycache__/api.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/api.pyAdded+101
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__init__.pyAdded+490
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/api.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/logger.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/logging_handlers.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/metadata.cpython-312.pycAdded
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309# mypy: allow-untyped-defsfrom functools import partialfrom typing import Optional, Tuple, Union
import torchimport torch._prims as primsimport torch._prims_common as utilsimport torch._refs as refsimport torch._refs.linalg as linalgfrom torch import Tensorfrom torch._prims_common import ( check_fp_or_complex, check_is_matrix, Dim, DimsType, ELEMENTWISE_TYPE_PROMOTION_KIND, IntLike, TensorLikeType,)from torch._prims_common.wrappers import ( _maybe_convert_to_dtype, elementwise_type_promotion_wrapper, out_wrapper,)
__all__ = [ "diagonal", "matrix_norm", "norm", "svd", "svdvals", "vector_norm", "vecdot", "cross",]
def _check_norm_dtype(dtype: Optional[torch.dtype], x_dtype: torch.dtype, fn_name: str): """ Checks related to the dtype kwarg in `linalg.*norm` functions """ if dtype is not None: torch._check( utils.is_float_dtype(dtype) or utils.is_complex_dtype(dtype), lambda: f"{fn_name}: dtype should be floating point or complex. Got {dtype}", ) torch._check( utils.is_complex_dtype(dtype) == utils.is_complex_dtype(x_dtype), lambda: "{fn_name}: dtype should be {d} for {d} inputs. Got {dtype}".format( fn_name=fn_name, d="complex" if utils.is_complex_dtype(x_dtype) else "real", dtype=dtype, ), ) torch._check( utils.get_higher_dtype(dtype, x_dtype) == dtype, lambda: f"{fn_name}: the dtype of the input ({x_dtype}) should be convertible " "without narrowing to the specified dtype ({dtype})", )
import operator
# Utilities should come BEFORE this importfrom torch._decomp import register_decompositionfrom torch._decomp.decompositions import pw_cast_for_opmath
@register_decomposition(torch._ops.ops.aten.linalg_cross)@out_wrapper()@pw_cast_for_opmathdef cross(a: Tensor, b: Tensor, dim: int = -1): torch._check( a.ndim == b.ndim, lambda: "linalg.cross: inputs must have the same number of dimensions.", ) torch._check( a.size(dim) == 3 and b.size(dim) == 3, lambda: f"linalg.cross: inputs dim {dim} must have length 3, got {a.size(dim)} and {b.size(dim)}", ) a, b = torch.broadcast_tensors(a, b) dim = utils.canonicalize_dim(a.ndim, dim) idx = torch.arange(3, device=a.device) return a.index_select(dim, (idx + 1) % 3) * b.index_select( dim, (idx + 2) % 3 ) - a.index_select(dim, (idx + 2) % 3) * b.index_select(dim, (idx + 1) % 3)
def diagonal( input: TensorLikeType, *, offset: int = 0, dim1: int = -2, dim2: int = -1,) -> TensorLikeType: return torch.diagonal(input, offset=offset, dim1=dim1, dim2=dim2)
@register_decomposition(torch._ops.ops.aten.linalg_vector_norm)@out_wrapper(exact_dtype=True)def vector_norm( x: TensorLikeType, ord: Union[float, int] = 2, dim: Optional[DimsType] = None, keepdim: bool = False, *, dtype: Optional[torch.dtype] = None,) -> Tensor: from torch.fx.experimental.symbolic_shapes import guard_size_oblivious
# Checks check_fp_or_complex(x.dtype, "linalg.vector_norm")
if isinstance(dim, Dim): dim = [dim] # type: ignore[assignment]
if guard_size_oblivious(x.numel() == 0) and (ord < 0.0 or ord == float("inf")): torch._check( dim is not None and len(dim) != 0, lambda: f"linalg.vector_norm cannot compute the {ord} norm on an empty tensor " "because the operation does not have an identity", ) shape = x.shape assert dim is not None # mypy does not seem to be able to see through check? for d in dim: torch._check( shape[d] != 0, lambda: f"linalg.vector_norm cannot compute the {ord} norm on the " f"dimension {d} because this dimension is empty and the " "operation does not have an identity", ) _check_norm_dtype(dtype, x.dtype, "linalg.vector_norm")
computation_dtype, result_dtype = utils.reduction_dtypes( x, utils.REDUCTION_OUTPUT_TYPE_KIND.COMPLEX_TO_FLOAT, dtype )
to_result_dtype = partial(_maybe_convert_to_dtype, dtype=result_dtype)
# Implementation if ord == 0.0: return torch.sum(torch.ne(x, 0.0), dim=dim, keepdim=keepdim, dtype=result_dtype) elif ord == float("inf"): return to_result_dtype(torch.amax(torch.abs(x), dim=dim, keepdim=keepdim)) # type: ignore[return-value,arg-type] elif ord == float("-inf"): return to_result_dtype(torch.amin(torch.abs(x), dim=dim, keepdim=keepdim)) # type: ignore[return-value,arg-type] else: # From here on the computation dtype is important as the reduction is non-trivial x = _maybe_convert_to_dtype(x, computation_dtype) # type: ignore[assignment] reduce_sum = partial(torch.sum, dim=dim, keepdim=keepdim)
is_ord_even = ord % 2 == 0 if isinstance(ord, IntLike) else ord % 2.0 == 0.0 if not (is_ord_even and utils.is_float_dtype(x.dtype)): x = torch.abs(x) return to_result_dtype(torch.pow(reduce_sum(torch.pow(x, ord)), 1.0 / ord)) # type: ignore[return-value]
def _backshift_permutation(dim0, dim1, ndim): # Auxiliary function for matrix_norm # Computes the permutation that moves the two given dimensions to the back ret = [i for i in range(ndim) if i != dim0 and i != dim1] ret.extend((dim0, dim1)) return ret
def _inverse_permutation(perm): # Given a permutation, returns its inverse. It's equivalent to argsort on an array return [i for i, j in sorted(enumerate(perm), key=operator.itemgetter(1))]
# CompositeImplicitAutograd@out_wrapper(exact_dtype=True)def matrix_norm( A: TensorLikeType, ord: Union[float, str] = "fro", dim: DimsType = (-2, -1), keepdim: bool = False, *, dtype: Optional[torch.dtype] = None,) -> TensorLikeType: # shape check_is_matrix(A, "linalg.matrix_norm") # dim dim = utils.canonicalize_dims(A.ndim, dim) if isinstance(dim, Dim): dim = (dim,) # type: ignore[assignment] torch._check( len(dim) == 2, lambda: "linalg.matrix_norm: dim must be a 2-tuple. Got {dim}" ) torch._check( dim[0] != dim[1], lambda: "linalg.matrix_norm: dims must be different. Got ({dim[0]}, {dim[1]})", ) # dtype arg _check_norm_dtype(dtype, A.dtype, "linalg.matrix_norm")
if isinstance(ord, str): # ord torch._check( ord in ("fro", "nuc"), lambda: "linalg.matrix_norm: Order {ord} not supported.", ) # dtype check_fp_or_complex( A.dtype, "linalg.matrix_norm", allow_low_precision_dtypes=ord != "nuc" )
if ord == "fro": return vector_norm(A, 2, dim, keepdim, dtype=dtype) else: # ord == "nuc" if dtype is not None: A = _maybe_convert_to_dtype(A, dtype) # type: ignore[assignment] perm = _backshift_permutation(dim[0], dim[1], A.ndim) result = torch.sum(svdvals(prims.transpose(A, perm)), -1, keepdim) if keepdim: inv_perm = _inverse_permutation(perm) result = prims.transpose(torch.unsqueeze(result, -1), inv_perm) return result else: # ord abs_ord = abs(ord) torch._check( abs_ord in (2, 1, float("inf")), lambda: "linalg.matrix_norm: Order {ord} not supported.", ) # dtype check_fp_or_complex( A.dtype, "linalg.matrix_norm", allow_low_precision_dtypes=ord != 2 )
max_min = partial(torch.amax if ord > 0.0 else torch.amin, keepdim=keepdim)
if abs_ord == 2.0: if dtype is not None: A = _maybe_convert_to_dtype(A, dtype) # type: ignore[assignment] perm = _backshift_permutation(dim[0], dim[1], A.ndim) result = max_min(svdvals(prims.transpose(A, perm)), dim=-1) if keepdim: inv_perm = _inverse_permutation(perm) result = prims.transpose(torch.unsqueeze(result, -1), inv_perm) return result else: # 1, -1, inf, -inf dim0, dim1 = dim if abs_ord == float("inf"): dim0, dim1 = dim1, dim0 if not keepdim and (dim0 < dim1): dim1 -= 1 return max_min( vector_norm(A, 1.0, dim=dim0, keepdim=keepdim, dtype=dtype), dim1 )
# CompositeImplicitAutograd@out_wrapper(exact_dtype=True)def norm( A: TensorLikeType, ord: Optional[Union[float, str]] = None, dim: Optional[DimsType] = None, keepdim: bool = False, *, dtype: Optional[torch.dtype] = None,) -> TensorLikeType: if dim is not None: if isinstance(dim, Dim): dim = (dim,) # type: ignore[assignment] torch._check( len(dim) in (1, 2), lambda: "linalg.norm: If dim is specified, it must be of length 1 or 2. Got {dim}", ) elif ord is not None: torch._check( A.ndim in (1, 2), lambda: "linalg.norm: If dim is not specified but ord is, the input must be 1D or 2D. Got {A.ndim}D", )
if ord is not None and ( (dim is not None and len(dim) == 2) or (dim is None and A.ndim == 2) ): if dim is None: dim = (0, 1) return matrix_norm(A, ord, dim, keepdim, dtype=dtype) else: if ord is None: ord = 2.0 return vector_norm(A, ord, dim, keepdim, dtype=dtype) # type: ignore[arg-type]
# CompositeImplicitAutograd@out_wrapper("U", "S", "Vh", exact_dtype=True)def svd(A: TensorLikeType, full_matrices: bool = True) -> Tuple[Tensor, Tensor, Tensor]: return prims.svd(A, full_matrices=full_matrices)
# CompositeImplicitAutograd@out_wrapper(exact_dtype=True)def svdvals(A: TensorLikeType) -> Tensor: return svd(A, full_matrices=False)[1]
# CompositeImplicitAutograd@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("x", "y"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def vecdot(x: Tensor, y: Tensor, dim: int = -1) -> Tensor: check_fp_or_complex(x.dtype, "linalg.vecdot") return (x.conj() * y).sum(dim=dim)1234from typing import List
__all__: List[str] = []12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280# mypy: allow-untyped-decorators# mypy: allow-untyped-defsimport mathfrom functools import wrapsfrom typing import Callable, Optional, Union
import torchimport torch._prims as primsimport torch._prims_common as utilsimport torch._refs as refsfrom torch._decomp import register_decompositionfrom torch._prims_common import ( ELEMENTWISE_TYPE_PROMOTION_KIND, NumberType, ShapeType, TensorLike, TensorLikeType,)from torch._prims_common.wrappers import ( elementwise_type_promotion_wrapper, elementwise_unary_scalar_wrapper, out_wrapper,)from torch._refs import _make_inplace
__all__ = [ "alpha_dropout", "celu", "celu_", "channel_shuffle", "dropout", "elu", "elu_", "gelu", "glu", "group_norm", "hardshrink", "hardtanh", "hinge_embedding_loss", "huber_loss", "l1_loss", "layer_norm", "leaky_relu", "log_softmax", "margin_ranking_loss", "mish", "mish_", "mse_loss", "nll_loss", "pairwise_distance", "pdist", "poisson_nll_loss", "prelu", "relu", "relu6", "selu", "selu_", "smooth_l1_loss", "softmax", "softmin", "softplus", "softshrink", "tanhshrink", "threshold", "threshold_", "triplet_margin_loss",]
Tensor = torch.Tensoraten = torch._ops.ops.atenDispatchKey = torch._C.DispatchKey # type: ignore[attr-defined]
def _dropout_helper( self: TensorLikeType, val: float,) -> TensorLikeType: """ Helper function for all dropout-type operators. During training, some of the elements of the input tensor are randomly masked.
Returns the masked tensor of the boolean values.
"""
return ( refs._uniform_helper( self.shape, low=0.0, high=1.0, dtype=torch.float32, device=self.device ) < val )
@register_decomposition(aten.alpha_dropout)def alpha_dropout( self: TensorLikeType, p: float = 0.5, training: bool = False, inplace: bool = False) -> TensorLikeType: if inplace: raise NotImplementedError
if not training: return self
torch._check( p <= 1 and p >= 0, lambda: f"dropout probability has to be between 0 and 1, but got, {p}", )
if p == 1: return torch.zeros_like(self)
if p == 0: return self
dropout_mask = _dropout_helper(self, 1 - p)
# From paper: Self-Normalizing Neural Networks (https://arxiv.org/pdf/1706.02515.pdf) # alpha = - SELU.alpha * SELU.scale, here # SELU.alpha = 1.6732632423543772848170429916717 and # SELU.scale = 1.0507009873554804934193349852946 alpha = -1.7580993408473766
a = 1.0 / math.sqrt((alpha * alpha * p + 1) * (1 - p)) b = torch.logical_not(dropout_mask) b = b * (alpha * a) + alpha * a * p dropout_mask = a * dropout_mask
return self * dropout_mask + b
def _inplace_wrapper(fn): """ Given a nn.functional non-linearity, implements its `inplace: bool` argument """
# nb. We use the name of the first argument used in the unary references @wraps(fn) def _fn(a, *args, inplace=False, **kwargs): if inplace: torch._check( "out" not in kwargs, lambda: "Cannot set inplace=True and pass out= at the same time", ) return fn(a, *args, inplace=False, out=a, **kwargs) else: return fn(a, *args, inplace=False, **kwargs)
return _fn
# celu is implemented specially because it has an alpha argument# celu is very similar to elu@register_decomposition(aten.celu)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def celu( a: TensorLikeType, alpha: Optional[NumberType] = None, inplace: bool = False) -> TensorLikeType: """ Reference implementation of torch.nn.functional.celu """
if inplace: raise NotImplementedError
rhs: TensorLikeType if alpha is not None: python_type = utils.dtype_to_type(a.dtype) if not utils.is_weakly_lesser_type(type(alpha), python_type): msg = f"alpha argument of type {type(alpha)} cannot be safely cast to type {python_type}!" raise ValueError(msg) rhs = alpha * torch.expm1(torch.true_divide(a, alpha)) # type: ignore[arg-type] else: rhs = torch.expm1(a)
return torch.where(a > 0, a, rhs)
@_inplace_wrapper@out_wrapper()def dropout( a: TensorLikeType, p: float = 0.5, training: bool = True, inplace: bool = False) -> TensorLikeType: if inplace: raise NotImplementedError
if not training: return a
torch._check( p <= 1 and p >= 0, lambda: f"dropout probability has to be between 0 and 1, but got, {p}", )
if p == 1: return torch.zeros_like(a)
if p == 0: return a
scale = 1 / (1 - p) dropout_mask = _dropout_helper(a, 1 - p)
return a * dropout_mask * scale
@register_decomposition(aten.elu)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def elu( a: TensorLikeType, alpha: NumberType = 1.0, scale: NumberType = 1.0, input_scale: NumberType = 1.0, inplace: bool = False,) -> TensorLikeType: """ Reference implementation of torch.nn.functional.elu """ if inplace: raise NotImplementedError
# nb. This should be factored out into a can_cast aux function python_type = utils.dtype_to_type(a.dtype) torch._check( utils.is_weakly_lesser_type(type(input_scale), python_type), lambda: f"input_scale argument of type {type(input_scale)} cannot be safely cast to type {python_type}!", ) torch._check( utils.is_weakly_lesser_type(type(scale), python_type), lambda: f"scale argument of type {type(scale)} cannot be safely cast to type {python_type}!", ) torch._check( utils.is_weakly_lesser_type(type(alpha), python_type), lambda: f"alpha argument of type {type(alpha)} cannot be safely cast to type {python_type}!", )
return torch.where(a > 0, scale * a, (alpha * scale) * torch.expm1(a * input_scale))
@register_decomposition(aten.relu)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def relu(a: TensorLikeType, inplace: bool = False) -> TensorLikeType: """ Reference implementation of torch.nn.functional.relu """
if inplace: raise NotImplementedError
return torch.where(torch.le(a, 0), 0, a)
@register_decomposition(aten.channel_shuffle)@out_wrapper()def channel_shuffle(input: TensorLikeType, groups: int) -> TensorLikeType: """ Reference implementation of :func:`torch.nn.functional.channel_shuffle`. """ from torch._meta_registrations import device_hint
torch._check( input.dim() > 2, lambda: f"channel_shuffle expects input with > 2 dims, but got input with sizes {list(input.size())}", ) c = input.shape[1] torch._check( groups > 0, lambda: f"Number of groups to divide channels in must be positive. Value of groups:{groups}", ) torch._check( (c % groups) == 0, lambda: f"Number of channels must be divisible by groups. Got {c} channels and {groups} groups.", ) n = input.shape[0] cg = c // groups dhw = input.shape[2:]
if input.numel() == 0 or ( device_hint(input) == "cuda" and (groups == 1 or groups == c) ): return input.view(input.shape)
return ( input.reshape(n, groups, cg, *dhw) .transpose(1, 2) .reshape(input.shape) .contiguous() )
def group_norm( input: Tensor, num_groups: int, weight: Optional[Tensor] = None, bias: Optional[Tensor] = None, eps: float = 1e-5,) -> Tensor: """ Reference implementation of :func:`torch.nn.functional.group_norm`. """ torch._check( input.ndim >= 2, lambda: f"Expected at least 2 dimensions for input tensor but received {input.ndim}", )
batch_size = input.shape[0] num_channels = input.shape[1] torch._check( num_channels % num_groups == 0, lambda: "Expected number of channels in input to be divisible by num_groups, " + f"but got input of shape {input.shape} and num_groups = {num_groups}", )
# input shape is (N, C, *), so we flatten all inner dimensions except (N, C) flattened_inner_size = 1 for dim_length in input.shape[2:]: flattened_inner_size *= dim_length
return torch.native_group_norm( input, weight, bias, batch_size, num_channels, flattened_inner_size, num_groups, eps, )[0]
def layer_norm( input: Tensor, normalized_shape: ShapeType, weight: Optional[Tensor] = None, bias: Optional[Tensor] = None, eps: float = 1e-5,) -> Tensor: """ Reference implementation of :func:`torch.nn.functional.layer_norm`. """ return torch.native_layer_norm(input, normalized_shape, weight, bias, eps)[0]
@register_decomposition(aten.leaky_relu)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def leaky_relu( a: TensorLikeType, negative_slope: float = 0.01, inplace: bool = False) -> TensorLikeType: """ Reference implementation of torch.nn.functional.leaky_relu """
if inplace: raise NotImplementedError
python_type = utils.dtype_to_type(a.dtype) if not utils.is_weakly_lesser_type(type(negative_slope), python_type): msg = f"negative_slope argument of type {type(negative_slope)} cannot be safely cast to type {python_type}!" raise ValueError(msg) return torch.where(torch.gt(a, 0), a, torch.mul(a, negative_slope))
@register_decomposition(aten.mish)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def mish(a: TensorLikeType, inplace: bool = False) -> TensorLikeType: """ Reference implementation of torch.nn.functional.mish """
if inplace: raise NotImplementedError return a * torch.tanh(torch.nn.functional.softplus(a))
@register_decomposition(aten.selu)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def selu(a: TensorLikeType, inplace: bool = False) -> TensorLikeType: """ Reference implementation of torch.nn.functional.selu """ if inplace: raise NotImplementedError
alpha = 1.6732632423543772848170429916717 scale = 1.0507009873554804934193349852946
rhs = alpha * torch.expm1(a)
return scale * torch.where(a > 0, a, rhs)
# Forwarding alias: the functional variant doesn't support the out kwarg# CompositeImplicitAutograd - don't register decompdef softmax( a: TensorLikeType, dim: Optional[int] = None, _stacklevel: int = 3, # for compat when using TorchRefsMode(strict=True) dtype: Optional[torch.dtype] = None,) -> TensorLikeType: # The error is for compat with regular PyTorch, which has this behavior # deprecated. For PrimTorch, it's fine to drop support for deprecated # behavior because it requires explicit opt in. This error is to inform # users how to update their calls. torch._check(dim is not None, lambda: "implicit dim not supported, use dim=X") return torch.softmax(a=a, dim=dim, dtype=dtype) # type: ignore[call-overload]
# CompositeImplicitAutograd - don't register decompdef softmin( a: TensorLikeType, dim: Optional[int] = None, _stacklevel: int = 3, # for compat when using TorchRefsMode(strict=True) dtype: Optional[torch.dtype] = None,) -> TensorLikeType: # The error is for compat with regular PyTorch, which has this behavior # deprecated. For PrimTorch, it's fine to drop support for deprecated # behavior because it requires explicit opt in. This error is to inform # users how to update their calls. torch._check(dim is not None, lambda: "implicit dim not supported, use dim=X") return torch.softmax(a=-a, dim=dim, dtype=dtype) # type: ignore[call-overload]
# softplus is implemented specially because it has beta and threshold arguments@register_decomposition(aten.softplus)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def softplus( a: TensorLikeType, beta: Optional[NumberType] = None, threshold: NumberType = 20, inplace: bool = False,) -> TensorLikeType: """ Reference implementation of torch.nn.functional.softplus """
if inplace: raise NotImplementedError
rhs: TensorLikeType if beta is not None: python_type = utils.dtype_to_type(a.dtype) if not utils.is_weakly_lesser_type(type(beta), python_type): msg = f"beta argument of type {type(beta)} cannot be safely cast to type {python_type}!" raise ValueError(msg) scaled_input = a * beta rhs = torch.true_divide(torch.log1p(torch.exp(scaled_input)), beta) # type: ignore[arg-type]
else: scaled_input = a rhs = torch.log1p(torch.exp(scaled_input))
return torch.where(scaled_input > threshold, a, rhs)
@aten.hardshrink.default.py_impl(DispatchKey.Autograd)@register_decomposition(aten.hardshrink)@out_wrapper()def hardshrink(a: TensorLikeType, lambd: float = 0.5): # Formula for reference, # hardshrink(x) = x if x > lambd # = x if x < -lambd # = 0 otherwise return torch.where(torch.abs(a) <= lambd, 0, a)
@aten.softshrink.default.py_impl(DispatchKey.Autograd)@register_decomposition(aten.softshrink)@out_wrapper()def softshrink(a: TensorLikeType, lambd: float = 0.5): # Formula for reference, # softshrink(x) = x - lambd if x > lambd # = x + lambd if x < -lambd # = 0 otherwise torch._check( lambd >= 0, lambda: f"lambda must be greater or equal to 0, but found to be {lambd}", ) # We implement this in one torch.where to generate better code in the backward # see https://github.com/pytorch/pytorch/pull/107052#discussion_r1293748211 # We multiply by 0 for dealing with nans return torch.where(torch.abs(a) > lambd, a - torch.sign(a) * lambd, a * 0)
# Lossesdef _reduction_int_to_str(reduction: int) -> str: from torch._decomp.decompositions import Reduction
if reduction == Reduction.NONE.value: return "none" elif reduction == Reduction.MEAN.value: return "mean" elif reduction == Reduction.SUM.value: return "sum" else: raise ValueError(f"{reduction} is not a valid value for reduction")
def _apply_loss_reduction(loss: TensorLikeType, reduction: str) -> TensorLikeType: if reduction == "sum": return torch.sum(loss) elif reduction == "mean": return torch.mean(loss) else: # reduction == "none" return loss
def _check_reduction_value(reduction: str): if reduction not in ("mean", "sum", "none"): raise ValueError(f"{reduction} is not a valid value for reduction")
# This helper function maps depreciated arguments, "size_average" and "reduce"# to their corresponding "reduction" string argumentdef _get_string_reduction_arg( *, size_average: Optional[bool], reduce: Optional[bool]) -> str: if size_average is None: size_average = True if reduce is None: reduce = True if size_average and reduce: ret = "mean" elif reduce: ret = "sum" else: ret = "none" return ret
# CompositeImplicitAutograd - don't register decomp@elementwise_type_promotion_wrapper( type_promoting_args=("input", "target"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.COMPLEX_TO_FLOAT,)def l1_loss( input: TensorLikeType, target: TensorLikeType, size_average: Optional[bool] = None, reduce: Optional[bool] = None, reduction: str = "mean",) -> TensorLikeType: """ Reference implementation of torch.nn.functional.l1_loss """ if size_average is not None or reduce is not None: # TODO: Raise exception instead of converting value. This is only for # primTorch since it can drop support for deprecated arguments. # msg = "size_average and reduce args are deprecated, please use reduction argument." reduction = _get_string_reduction_arg(size_average=size_average, reduce=reduce) _check_reduction_value(reduction) loss = torch.abs(input - target) return _apply_loss_reduction(loss, reduction)
@elementwise_type_promotion_wrapper( type_promoting_args=("input", "target"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.COMPLEX_TO_FLOAT,)def smooth_l1_loss( input: TensorLikeType, target: TensorLikeType, size_average: Optional[bool] = None, reduce: Optional[bool] = None, reduction: str = "mean", beta: float = 1.0,) -> TensorLikeType: """ Reference implementation of torch.nn.functional.smooth_l1_loss """ if size_average is not None or reduce is not None: # TODO: Raise exception instead of converting value. This is only for # primTorch since it can drop support for deprecated arguments. # msg = "size_average and reduce args are deprecated, please use reduction argument." reduction = _get_string_reduction_arg(size_average=size_average, reduce=reduce) _check_reduction_value(reduction)
if beta == 0.0: return torch.nn.functional.l1_loss( input, target, size_average=size_average, reduce=reduce, reduction=reduction ) else: loss = torch.abs(input - target) loss = torch.where(loss < beta, 0.5 * loss**2 / beta, loss - 0.5 * beta) return _apply_loss_reduction(loss, reduction)
# Forwarding alias: the functional variant doesn't support the out kwarg# CompositeImplicitAutograd - don't register decompdef log_softmax( a: TensorLikeType, dim: Optional[int] = None, _stacklevel: int = 3, # for compat when using TorchRefsMode(strict=True) dtype: Optional[torch.dtype] = None,) -> TensorLikeType: # The error is for compat with regular PyTorch, which has this behavior # deprecated. For PrimTorch, it's fine to drop support for deprecated # behavior because it requires explicit opt in. This error is to inform # users how to update their calls. torch._check(dim is not None, lambda: "implicit dim not supported, use dim=X") return torch.log_softmax(a=a, dim=dim, dtype=dtype) # type: ignore[call-overload]
@register_decomposition(aten.margin_ranking_loss)def margin_ranking_loss( input1: TensorLikeType, input2: TensorLikeType, target: TensorLikeType, margin: float = 0.0, reduction: str = "mean",) -> TensorLikeType: # loss_without_reduction = max(0, -target * (input1 - input2) + margin) if input1.ndim != input2.ndim or input1.ndim != target.ndim: raise RuntimeError( "margin_ranking_loss : All input tensors should have same dimension but got sizes: " f"input1: {input1.shape}, input2: {input2.shape}, target: {target.shape} " ) _check_reduction_value(reduction) loss = torch.clamp_min(-target * (input1 - input2) + margin, 0) return _apply_loss_reduction(loss, reduction)
@elementwise_type_promotion_wrapper( type_promoting_args=("input", "target"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.COMPLEX_TO_FLOAT,)def mse_loss( input: TensorLikeType, target: TensorLikeType, size_average: Optional[bool] = None, reduce: Optional[bool] = None, reduction: str = "mean",) -> TensorLikeType: if size_average is not None or reduce is not None: # TODO: Raise exception instead of converting value. This is only for # primTorch since it can drop support for deprecated arguments. # msg = "size_average and reduce args are deprecated, please use reduction argument." reduction = _get_string_reduction_arg(size_average=size_average, reduce=reduce) _check_reduction_value(reduction) loss = torch.pow(input - target, 2) return _apply_loss_reduction(loss, reduction)
@register_decomposition(aten.hinge_embedding_loss)def hinge_embedding_loss( input: TensorLikeType, target: TensorLikeType, margin: float = 1.0, reduction: str = "mean",) -> TensorLikeType: # loss_without_reduction = input if y == 1 # = max(0, margin - input) if y == -1 _check_reduction_value(reduction) margin_clamp = torch.clamp_min(margin - input, 0) output_margin = torch.where(target != 1, margin_clamp, 0) output_self = torch.where(target != -1, input, 0) loss = output_margin + output_self return _apply_loss_reduction(loss, reduction)
def _nll_loss_nd( input: TensorLikeType, target: TensorLikeType, weight: Optional[TensorLikeType], reduction: str, ignore_index: int,) -> TensorLikeType: torch._check( input.ndim > 0 and input.ndim <= 3, lambda: f"Expected input dimension to be either [1, 2, 3] but received {input.ndim}.", )
torch._check( (input.ndim == 1) or (input.shape[0] == target.shape[0]), lambda: f"Expected input batch size {input.shape[0]} to match target batch size {target.shape[0]}.", )
_check_reduction_value(reduction)
flat_target = torch.flatten(target) ignore_classes_mask = torch.eq(flat_target, ignore_index)
# TODO: Enable data-dependent checks with debug mode # TODO: This check does not work with FakeTensor inputs; See Issue #85834 # Explicit cast for class_check to bool; See Issue #78071 """ from torch._subclasses.fake_tensor import FakeTensor num_classes = input.shape[1] if input.ndim > 1 else input.shape[0] valid_classes_mask = torch.logical_and( (flat_target >= 0), (flat_target < num_classes) ) class_check = torch.all(torch.logical_or(ignore_classes_mask, valid_classes_mask)) torch._check( isinstance(target, FakeTensor) or bool(class_check.item()), lambda: "A target class is out-of-bounds and not the ignore index.", ) """
ignore_class_weight = torch.scalar_tensor(0, dtype=input.dtype, device=input.device) class_weight = ( torch.scalar_tensor(1, dtype=input.dtype, device=input.device) if weight is None else weight[flat_target] ) current_weight = torch.where( ignore_classes_mask, ignore_class_weight, class_weight, )
if input.ndim == 1: # implicit batch size = 1 # input (1 batch size, C classes) loss = -input[target] * current_weight elif input.ndim == 2: # input (N batch size, C classes) batch_size = input.shape[0] loss = -input[torch.arange(batch_size), target] * current_weight else: # 3D case (N batch size, C classe, K dimensions) # input (N batch size, C classes, K) batch_size = input.shape[0] extent = input.shape[2] numel = batch_size * extent indices = torch.arange(numel) bdx = indices // extent kdx = indices % extent loss = -input[bdx, flat_target, kdx] * current_weight loss = torch.reshape(loss, target.shape)
if reduction == "none": return loss elif reduction == "sum": return torch.sum(loss) else: # calculate weighted mean of the loss function return torch.sum(loss) / torch.sum(current_weight)
@register_decomposition(aten.nll_loss)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("input",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def nll_loss( input: TensorLikeType, target: TensorLikeType, weight: Optional[TensorLikeType] = None, size_average: Optional[bool] = None, ignore_index: int = -100, reduce: Optional[bool] = None, reduction: str = "mean",) -> TensorLikeType: """ Reference implementation of torch.nn.functional.nll_loss """ torch._check( input.ndim > 0, lambda: f"Expected input tensor to have 1 or more dimensions (got {input.ndim})", )
# TODO: raise exception instead of converting value # msg = "size_average and reduce args are deprecated, please use reduction argument." # Convert these options for consistency with the eager mode if size_average is not None or reduce is not None: reduction = _get_string_reduction_arg(size_average=size_average, reduce=reduce)
# The expected behavior when the target and input have zero elements: # reduction = 'none' --- tensor([]) # reduction = 'sum' --- tensor(0.) # reduction = 'mean' --- tensor(nan) # Mean reduction on empty tensors produces NaN. See the discussion in # https://github.com/pytorch/pytorch/pull/64572#issuecomment-926504162 if input.numel() == 0 and target.numel() == 0: if reduction == "none": return torch.zeros_like(target) elif reduction == "sum": return torch.empty_like(target) else: return torch.full_like(target, float("nan"))
# The _nll_loss_nd helper function handles the most common cases. # ndim == 1 (Single Example) # => Batch Size: 1, Input: (C), Target: () # ndim == 2 (k = 1) # => Batch Size: N, Input: (N, C), Target: (N) # ndim == 3 (k > 1) # => Batch Size: N, Input: (N, C, K), Target: (N, K) if input.ndim <= 3: return _nll_loss_nd(input, target, weight, reduction, ignore_index)
# For ndim > 3, we reshape the input and target to 3-D case. # Input (N batch-size, C classes, k-dimensions) # Target (N batch-size, k-dimensions) torch._check( input.ndim > 0 and target.ndim > 0 and target.shape[1:] == input.shape[2:], lambda: ( "Expected input and target to both have ndim > 0 and " "target.shape[1:] == input.shape[2:], but got " f"target.shape {target.shape} and input.shape {input.shape}" ), )
batch_size = input.shape[0] num_classes = input.shape[1] out_size = [batch_size] + list(target.shape[1:])
input = torch.reshape(input, [batch_size, num_classes, -1]) target = torch.reshape(target, [batch_size, -1]) if reduction != "none": return _nll_loss_nd(input, target, weight, reduction, ignore_index) else: result = _nll_loss_nd(input, target, weight, reduction, ignore_index) # reshape flattened inner-dim to original k-dimensions return torch.reshape(result, out_size)
# TODO: This ref supports int reduction and out kwarg to be compatible with ATen:# https://github.com/pytorch/pytorch/issues/83931# TODO: Could be rewritten to support complex:# https://github.com/pytorch/pytorch/pull/85041@register_decomposition(aten.huber_loss)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("input", "target"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def huber_loss( input: TensorLikeType, target: TensorLikeType, reduction: Union[str, int] = "mean", delta: float = 1.0,) -> TensorLikeType: """ Reference implementation of torch.nn.functional.huber_loss """ if type(reduction) is int: reduction = _reduction_int_to_str(reduction) _check_reduction_value(reduction) # type: ignore[arg-type] torch._check( delta > 0, lambda: "huber_loss does not support non-positive values for delta.", ) z = (input - target).abs() loss = torch.where(z < delta, 0.5 * z * z, delta * (z - 0.5 * delta)) return _apply_loss_reduction(loss, reduction) # type: ignore[arg-type]
# tanhshrink does not use _make_elementwise_unary_reference because it does not support out@elementwise_unary_scalar_wrapper@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def tanhshrink(a: TensorLikeType) -> TensorLikeType: """ Reference implementation of torch.nn.functional.tanhshrink """ if not isinstance(a, TensorLike): raise RuntimeError( "Expected a tensor input for an elementwise unary operation!" ) return a - torch.tanh(a)
@register_decomposition(aten.threshold)@_inplace_wrapper@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def threshold( a: TensorLikeType, threshold: NumberType, value: Union[bool, int, float], inplace: bool = False,) -> TensorLikeType: """ Reference implementation of torch.nn.functional.threshold """
if inplace: raise NotImplementedError
return torch.where(a <= threshold, value, a)
# CompositeImplicitAutograd - don't register decomp# No elementwise type promotion - core op doesn't explicitly type promotedef triplet_margin_loss( anchor: TensorLikeType, positive: TensorLikeType, negative: TensorLikeType, margin: float = 1.0, p: float = 2, eps: float = 1e-6, swap: bool = False, size_average: Optional[bool] = None, reduce: Optional[bool] = None, reduction: str = "mean",) -> TensorLikeType: if size_average is not None or reduce is not None: # TODO: Raise exception instead of converting value. This is only for # primTorch since it can drop support for deprecated arguments. # msg = "size_average and reduce args are deprecated, please use reduction argument." reduction = _get_string_reduction_arg(size_average=size_average, reduce=reduce)
if margin <= 0: raise ValueError(f"margin must be greater than 0, got {margin}")
# torch.nn.functional.triplet_margin_with_distance_loss has no ref defined # since it's a pure Python implementation. Use this helper instead. return _triplet_margin_with_distance_loss( anchor=anchor, positive=positive, negative=negative, distance_function=lambda x, y: torch.pairwise_distance(x, y, p, eps), margin=margin, swap=swap, reduction=reduction, )
# Pure Python impl - don't register decomp and don't add a ref. Defined as a# helper here since triplet_margin_loss can be nicely implemented with it.def _triplet_margin_with_distance_loss( anchor: TensorLikeType, positive: TensorLikeType, negative: TensorLikeType, *, distance_function: Optional[ Callable[[TensorLikeType, TensorLikeType], TensorLikeType] ] = None, margin: float = 1.0, swap: bool = False, reduction: str = "mean",) -> TensorLikeType: _check_reduction_value(reduction)
a_dim = anchor.ndim p_dim = positive.ndim n_dim = negative.ndim torch._check( a_dim == p_dim and p_dim == n_dim, lambda: ( f"The anchor, positive, and negative tensors are expected to have " f"the same number of dimensions, but got: anchor {a_dim}D, " f"positive {p_dim}D, and negative {n_dim}D inputs" ), )
if distance_function is None: distance_function = torch.pairwise_distance
dist_pos = distance_function(anchor, positive) dist_neg = distance_function(anchor, negative) # The distance swap is described in the paper "Learning shallow # convolutional feature descriptors with triplet losses" by V. Balntas, E. # Riba et al. If True, and if the positive example is closer to the # negative example than the anchor is, swaps the positive example and the # anchor in the loss computation. if swap: dist_swap = distance_function(positive, negative) dist_neg = torch.minimum(dist_neg, dist_swap) loss = torch.clamp_min(margin + dist_pos - dist_neg, 0) return _apply_loss_reduction(loss, reduction)
@register_decomposition(aten.hardtanh)@_inplace_wrapper@out_wrapper()@elementwise_unary_scalar_wrapper@elementwise_type_promotion_wrapper( type_promoting_args=("a"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def hardtanh( a: TensorLikeType, min_val: NumberType = -1, max_val: NumberType = 1, inplace: bool = False,) -> TensorLikeType: """ Reference implementation of torch.nn.functional.hardtanh """ if inplace: raise NotImplementedError if utils.is_boolean_dtype(a.dtype): raise RuntimeError("Bool inputs not supported for hardtanh")
# preserve legacy behavior of boundaries not causing type promotion if utils.is_integer_dtype(a.dtype): min_val = int(min_val) # type: ignore[arg-type] max_val = int(max_val) # type: ignore[arg-type] if not (a.dtype != torch.uint8 or (min_val >= 0 and max_val >= 0)): raise RuntimeError( "Cannot do hardtanh on an unsigned type with negative limits" )
if min_val > max_val: # type: ignore[operator] raise ValueError("min_val cannot be greater than max_val")
return torch.clamp(a, min_val, max_val) # type: ignore[arg-type]
@register_decomposition(aten.gelu)@out_wrapper()@elementwise_unary_scalar_wrapper@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def gelu(a: TensorLikeType, approximate: str = "none") -> TensorLikeType: """ Reference implementation of torch.nn.functional.gelu """ if not isinstance(a, TensorLike): raise RuntimeError( "Expected a tensor input for an elementwise unary operation!" ) M_SQRT2 = 1.41421356237309504880 M_SQRT1_2 = 0.70710678118654752440 M_2_SQRTPI = 1.12837916709551257390 if approximate == "tanh": kBeta = M_SQRT2 * M_2_SQRTPI * 0.5 kKappa = 0.044715 a_cube = a * a * a inner = kBeta * (a + kKappa * a_cube) return 0.5 * a * (1 + torch.tanh(inner)) elif approximate == "none": kAlpha = M_SQRT1_2 return a * 0.5 * (1 + torch.erf(a * kAlpha)) else: raise RuntimeError("approximate argument must be either none or tanh.")
# CompositeImplicitAutograd - don't register decomp@elementwise_type_promotion_wrapper( type_promoting_args=("input", "target"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def poisson_nll_loss( input: TensorLikeType, target: TensorLikeType, log_input: bool = True, full: bool = False, size_average: Optional[bool] = None, eps: float = 1e-8, reduce: Optional[bool] = None, reduction: str = "mean",) -> TensorLikeType: """ Reference implementation of torch.nn.functional.poisson_nll_loss """ if size_average is not None or reduce is not None: # TODO: Raise exception instead of converting value. This is only for # primTorch since it can drop support for deprecated arguments. # msg = "size_average and reduce args are deprecated, please use reduction argument." reduction = _get_string_reduction_arg(size_average=size_average, reduce=reduce) _check_reduction_value(reduction) if log_input: loss = torch.exp(input) - target * input else: loss = input - target * torch.log(input + eps)
if full: stirling_term = ( target * torch.log(target) - target + 0.5 * torch.log(2 * torch.pi * target) ) # avoid inplace add loss = loss + stirling_term.masked_fill(target <= 1, 0) return _apply_loss_reduction(loss, reduction)
@register_decomposition(aten.prelu)@elementwise_type_promotion_wrapper( type_promoting_args=("a", "weight"), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def prelu(a: TensorLikeType, weight: TensorLikeType) -> TensorLikeType: """ Reference implementation of torch.nn.functional.prelu """ torch._check( isinstance(a, TensorLike), lambda: f"prelu: Expected `a` to be tensor, but got: {type(a)}", ) torch._check( isinstance(weight, TensorLike), lambda: f"prelu: Expected `weight` to be tensor, but got: {type(weight)}", )
if weight.numel() != 1: torch._check(a.ndim > 0, lambda: "Not allow zero-dim input tensor.") channel_size = a.shape[1] if a.ndim >= 2 else 1 torch._check( weight.numel() == channel_size, lambda: f"Mismatch of parameter numbers and input channel size. Found parameter numbers =" f" {weight.numel()} and channel size = {channel_size}.", )
torch._check( weight.ndim == 0 or weight.ndim == 1, lambda: f"prelu: Expected `weight` to be a scalar or 1D tensor, but got: " f"ndim = {weight.ndim}", ) if a.ndim == 0: weight = weight[0] if weight.ndim == 1 else weight else: weight = prims.broadcast_in_dim( weight, a.shape, () if weight.ndim == 0 else (0 if a.ndim == 1 else 1,) )
return torch.where(a > 0, a, a * weight)
@register_decomposition(aten.relu6)@_inplace_wrapper@out_wrapper()def relu6(a: TensorLikeType, inplace: bool = False) -> TensorLikeType: """ Reference implementation of torch.nn.functional.relu6 """ if inplace: raise NotImplementedError
# See https://github.com/pytorch/pytorch/pull/81142#discussion_r918220126 # It may be better to use clamp here, but we use hardtanh to replicate # the behavior of the existing implementation return torch.nn.functional.hardtanh(a, 0, 6)
@register_decomposition(aten.glu)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def glu(a: TensorLikeType, dim: int = -1) -> TensorLikeType: dim = utils.canonicalize_dims(a.ndim, dim) torch._check( a.shape[dim] % 2 == 0, lambda: f"Halving dimension must be even, but dimension {dim} is size {a.shape[dim]}", ) b, c = torch.tensor_split(a, 2, dim)
return b * torch.sigmoid(c)
@register_decomposition(aten.pairwise_distance)@out_wrapper()def pairwise_distance( x1: TensorLikeType, x2: TensorLikeType, p: NumberType = 2.0, eps: NumberType = 1e-6, keepdim=False,) -> TensorLikeType: return torch.linalg.vector_norm(x1 - x2 + eps, ord=p, dim=-1, keepdim=keepdim)
@register_decomposition(aten.pdist)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,)def pdist(a: TensorLikeType, p: float = 2) -> TensorLikeType: torch._check(a.ndim == 2, lambda: f"pdist only supports 2D tensors, got: {a.ndim}D") torch._check(p >= 0, lambda: "pdist only supports non-negative p values") # For p == 2 we can use an efficient implementation, but other values of p # require creating a much bigger tensor for an intermediate step if p == 2: aTa = torch.mm(a, a.T) aTa_diag = torch.diag(aTa) t = torch.sqrt(torch.clamp(aTa_diag + aTa_diag.unsqueeze(-1) - 2 * aTa, min=0)) else: t = torch.linalg.vector_norm(a.unsqueeze(1) - a, ord=p, dim=2) i = torch.triu_indices(t.shape[0], t.shape[1], offset=1, device=a.device) return t.flatten().index_select(0, i[0] * t.shape[0] + i[1])
@register_decomposition(aten.pixel_shuffle)@out_wrapper()def pixel_shuffle(self: Tensor, upscale_factor: int): torch._check( self.dim() >= 3, lambda: f"pixel_shuffle expects input to have at least 3 dimensions, but got input with {self.dim} dimension(s)", ) batch = self.shape[:-3] C_out = self.shape[-3] // upscale_factor**2 HW_out = (self.shape[-2] * upscale_factor, self.shape[-1] * upscale_factor) n = len(batch) B_dims = range(n) C_dim, r1_dim, r2_dim, H_dim, W_dim = range(n, n + 5) return ( self.view( *batch, C_out, upscale_factor, upscale_factor, self.shape[-2], self.shape[-1], ) .permute(*B_dims, C_dim, H_dim, r1_dim, W_dim, r2_dim) .reshape(*batch, C_out, *HW_out) .clone(memory_format=utils.suggest_memory_format(self)) )
@register_decomposition(aten.pixel_unshuffle)@out_wrapper()def pixel_unshuffle(self: Tensor, downscale_factor: int): torch._check( self.dim() >= 3, lambda: f"pixel_unshuffle expects input to have at least 3 dimensions, but got input with {self.dim} dimension(s)", ) batch = self.shape[:-3] C_out = self.shape[-3] * downscale_factor**2 HW_out = (self.shape[-2] // downscale_factor, self.shape[-1] // downscale_factor) n = len(batch) B_dims = range(n) C_dim, H_dim, r1_dim, W_dim, r2_dim = range(n, n + 5) return ( self.view( *batch, self.shape[-3], HW_out[0], downscale_factor, HW_out[1], downscale_factor, ) .permute(*B_dims, C_dim, r1_dim, r2_dim, H_dim, W_dim) .reshape(*batch, C_out, *HW_out) .clone(memory_format=utils.suggest_memory_format(self)) )
# Needed as aten.{celu_,elu_...} exist (even if they don't have the in-place kwarg)celu_ = _make_inplace(celu)elu_ = _make_inplace(elu)mish_ = _make_inplace(mish)selu_ = _make_inplace(selu)threshold_ = _make_inplace(threshold)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236# mypy: allow-untyped-defsimport mathfrom typing import Optional, Union
import torchimport torch._prims as primsimport torch._prims_common as utilsimport torch._refs as refsfrom torch import Tensorfrom torch._decomp import register_decompositionfrom torch._prims_common import ( ELEMENTWISE_TYPE_PROMOTION_KIND, Number, NumberType, TensorLike, TensorLikeType,)from torch._prims_common.wrappers import elementwise_type_promotion_wrapper, out_wrapperfrom torch._refs import ( _make_alias, _make_elementwise_binary_reference, _make_elementwise_unary_reference,)
__all__ = [ "bessel_j0", "bessel_j1", "entr", "erfcx", "expit", "i0e", "i1", "i1e", "log_ndtr", "logit", "log_softmax", "multigammaln", "ndtr", "ndtri", "softmax", "spherical_bessel_j0", "xlog1py", "zeta",]aten = torch._ops.ops.aten
@_make_elementwise_unary_reference( ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def bessel_j0(a: TensorLikeType) -> TensorLikeType: return prims.bessel_j0(a)
@_make_elementwise_unary_reference( ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def bessel_j1(a: TensorLikeType) -> TensorLikeType: return prims.bessel_j1(a)
@register_decomposition(aten.special_entr)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def entr(a: TensorLikeType) -> TensorLikeType: return torch.where( torch.isnan(a), a, torch.where(a > 0, -a * torch.log(a), torch.where(a == 0, 0, -torch.inf)), )
@register_decomposition(aten.special_erfcx)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def erfcx(a: TensorLikeType) -> TensorLikeType: return prims.erfcx(a)
# alias for sigmoidexpit = _make_alias(torch.sigmoid, "expit")
@_make_elementwise_unary_reference( ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def i0e(a: TensorLikeType) -> TensorLikeType: return prims.bessel_i0e(a)
@_make_elementwise_unary_reference( ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def i1(a: TensorLikeType) -> TensorLikeType: return prims.bessel_i1(a)
@_make_elementwise_unary_reference( ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def i1e(a: TensorLikeType) -> TensorLikeType: return prims.bessel_i1e(a)
@register_decomposition(aten.special_log_ndtr)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def log_ndtr(a: TensorLikeType) -> TensorLikeType: # Note: M_SQRT1_2 is the value of 1 / sqrt(2) M_SQRT1_2 = 0.707106781186547524400844362104849039 t = a * M_SQRT1_2 return torch.where( a < 1.0, torch.log(torch.special.erfcx(-t) / 2) - t * t, torch.log1p(-torch.erfc(t) / 2), )
@register_decomposition(aten.logit)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("self",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def logit(self: TensorLikeType, eps: Optional[float] = None) -> TensorLikeType: if eps is None: eps = -1.0 lo = eps hi = 1 - eps self = torch.clamp(self, lo, hi) return torch.log(torch.true_divide(self, torch.sub(1, self)))
@register_decomposition(aten.special_xlog1py)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a", "b"), type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def xlog1py(a: Union[TensorLikeType, NumberType], b: Union[TensorLikeType, NumberType]): torch._check( isinstance(a, TensorLike) or isinstance(b, TensorLike), lambda: 'Expected either argument a or b to be a Tensor"', )
# Operations like eq and log do not handle scalar values, so we convert them to scalar_tensors. if isinstance(a, TensorLike) and isinstance(b, Number): b = refs.scalar_tensor(b, dtype=a.dtype, device=a.device) elif isinstance(b, TensorLike) and isinstance(a, Number): a = refs.scalar_tensor(a, dtype=b.dtype, device=b.device)
# mypy: expected "Tensor" assert isinstance(a, TensorLike) assert isinstance(b, TensorLike) rhs = torch.where(torch.eq(a, 0), 0, torch.mul(a, torch.log1p(b))) return torch.where(torch.isnan(b), float("nan"), rhs)
@register_decomposition(aten.mvlgamma)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def multigammaln(a: TensorLikeType, p: int) -> TensorLikeType: c = 0.25 * p * (p - 1) * math.log(math.pi) b = 0.5 * torch.arange(start=(1 - p), end=1, step=1, dtype=a.dtype, device=a.device) return torch.sum(torch.lgamma(a.unsqueeze(-1) + b), dim=-1) + c
@register_decomposition(aten.special_ndtr)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def ndtr(a: TensorLikeType) -> TensorLikeType: # Note: M_SQRT1_2 is the value of 1 / sqrt(2) M_SQRT1_2 = 0.707106781186547524400844362104849039 a_sqrt_2 = a * M_SQRT1_2 return (1 + torch.erf(a_sqrt_2)) * 0.5
@register_decomposition(aten.special_ndtri)@out_wrapper()@elementwise_type_promotion_wrapper( type_promoting_args=("a",), type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def ndtri(a: TensorLikeType) -> TensorLikeType: return prims.ndtri(a)
# Forwarding alias: the special variant doesn't support the out kwarg# CompositeImplicitAutograd - don't register decompdef log_softmax( a: TensorLikeType, dim: int, dtype: Optional[torch.dtype] = None,) -> TensorLikeType: return torch.log_softmax(a=a, dim=dim, dtype=dtype) # type: ignore[call-overload]
# Forwarding alias: the special variant doesn't support the out kwarg# CompositeImplicitAutograd - don't register decompdef softmax( a: TensorLikeType, dim: int, dtype: Optional[torch.dtype] = None,) -> TensorLikeType: return torch.softmax(a=a, dim=dim, dtype=dtype) # type: ignore[call-overload]
@_make_elementwise_unary_reference( ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def spherical_bessel_j0(a: TensorLikeType) -> TensorLikeType: return prims.spherical_bessel_j0(a)
# TODO: add docstring@_make_elementwise_binary_reference( type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,)def zeta(a: TensorLikeType, b: TensorLikeType) -> TensorLikeType: return prims.zeta(a, b)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311# mypy: disallow-untyped-defs
import functoolsimport loggingimport osimport reimport subprocessimport timefrom threading import Lockfrom timeit import default_timer as timerfrom typing import Any, List, Optional, Sequence
logger = logging.getLogger("strobelight_function_profiler")
console_handler = logging.StreamHandler()formatter = logging.Formatter( "%(name)s, line %(lineno)d, %(asctime)s, %(levelname)s: %(message)s")console_handler.setFormatter(formatter)
logger.addHandler(console_handler)logger.setLevel(logging.INFO)logger.propagate = False
class StrobelightCLIProfilerError(Exception): """ Raised when an error happens during strobelight profiling """
def _pid_namespace_link(pid: Optional[int] = None) -> str: """Returns the link to the process's namespace, example: pid:[4026531836]""" PID_NAMESPACE_PATH = "/proc/{}/ns/pid" pid = pid or os.getpid() return os.readlink(PID_NAMESPACE_PATH.format(pid))
def _pid_namespace(pid: Optional[int] = None) -> int: """Returns the process's namespace id""" pid = pid or os.getpid() link = _pid_namespace_link(pid) return int(link[link.find("[") + 1 : -1])
def _command_to_string(command: Sequence[str]) -> str: return " ".join(command)
class StrobelightCLIFunctionProfiler: """ Note: this is a Meta only tool.
StrobelightCLIFunctionProfiler can be used to profile a python function and generate a strobelight link with the results. It works on meta servers but does not requries an fbcode target. When stop_at_error is false(default), error during profiling does not prevent the work function from running.
Check function_profiler_example.py for an example. """
# This lock is used to make sure only one thread is running the profiler at any point. _lock = Lock()
def __init__( self, *, stop_at_error: bool = False, max_profile_duration_sec: int = 60 * 10, sample_each: float = 1e7, # sample each sample_each cycles. run_user_name: str = "pytorch-strobelight-ondemand", timeout_wait_for_running_sec: int = 60, timeout_wait_for_finished_sec: int = 60, recorded_env_variables: Optional[List[str]] = None, sample_tags: Optional[List[str]] = None, stack_max_len: int = 127, async_stack_max_len: int = 127, ): self.stop_at_error = stop_at_error self.max_profile_duration_sec = max_profile_duration_sec self.sample_each = sample_each self.run_user_name = run_user_name self.timeout_wait_for_running_sec = timeout_wait_for_running_sec self.timeout_wait_for_finished_sec = timeout_wait_for_finished_sec # Results of the most recent run. # Tracks the strobelight run id of the most recent run self.current_run_id: Optional[int] = None self.profile_result: Optional[List[str]] = None self.sample_tags = sample_tags
def _run_async(self) -> None: processId = os.getpid() namespace = _pid_namespace(processId) command = [ "strobeclient", "run", "--profiler", "pyperf", "--event", "cycles", "--async", "--sample-interval", f"{int(self.sample_each)}", "--duration-ms", f"{int(self.max_profile_duration_sec * 1000)}", "--pid", f"{namespace}:{processId}", ]
if self.sample_tags: command.append("--sample-tags") command.append(",".join(self.sample_tags))
logger.debug("running command: %s", _command_to_string(command)) result = subprocess.run(command, capture_output=True) output = result.stderr.decode("utf-8") logger.debug("output:\n{%s}", output)
if result.returncode != 0: raise StrobelightCLIProfilerError( f"failed to start strobelight profiling, error in run_async:{output}" )
if match := re.search(r"INFO Run Id: (-?\d+)", output): self.current_run_id = int(match.group(1)) return
raise StrobelightCLIProfilerError( f"failed to start strobelight profiling, unexpected result {output}" )
def _wait_for_running(self, counter: int = 0) -> None: if counter > 20: raise StrobelightCLIProfilerError( "wait_for_running called more than 20 times" )
command = ["strobeclient", "getRunStatus", "--run-id", f"{self.current_run_id}"] logger.debug("running command: %s", _command_to_string(command)) result = subprocess.run(command, capture_output=True) output = result.stderr.decode("utf-8") logger.debug("output:\n{%s}", output)
if result.returncode != 0: raise StrobelightCLIProfilerError( f"failed to start strobelight profiling, error in wait_for_running:{output}" )
if match := re.search("Profile run status: (.*)", output): current_status = match.group(1) if current_status == "RUNNING": return elif current_status == "PREPARING": time.sleep(10) self._wait_for_running(counter + 1) return else: raise StrobelightCLIProfilerError(f"unexpected {current_status} phase")
raise StrobelightCLIProfilerError(f"unexpected output\n: {output} ")
def _stop_run(self) -> None: command = ["strobeclient", "stopRun", "--run-id", str(self.current_run_id)] logger.debug("running command: %s", _command_to_string(command)) result = subprocess.run(command, capture_output=True) output = result.stderr.decode("utf-8") logger.debug("output:\n{%s}", output)
if result.returncode != 0: raise StrobelightCLIProfilerError( f"failed to stop strobelight profiling, return code is not 0 :{output}" )
if match := re.search("INFO ::1:(.*)", output): current_status = match.group(1) if current_status.__contains__("Success!"): return else: raise StrobelightCLIProfilerError( f"failed to stop strobelight profiling, got {current_status} result" )
raise StrobelightCLIProfilerError(f"unexpected output\n: {output} ")
def _get_results(self) -> None: command = ["strobeclient", "getRunStatus", "--run-id", str(self.current_run_id)] logger.debug("running command: %s", _command_to_string(command)) result = subprocess.run(command, capture_output=True) output = result.stderr.decode("utf-8") logger.debug("output:\n{%s}", output)
if result.returncode != 0: raise StrobelightCLIProfilerError( f"failed to extract profiling results, return code is not 0 : {output}" )
if match := re.search("INFO ::1:(.*)", output): current_status = match.group(1) if current_status.__contains__("Profile run status: PROCESSING"): time.sleep(10) self._get_results() return elif not current_status.__contains__("Profile run finished with SUCCESS"): raise StrobelightCLIProfilerError( f"failed to extract profiling results, unexpected response {output}" )
self.profile_result = [] for item in re.findall( r"(Total samples(.*)|GraphProfiler(.*)|Icicle view \(python stack\)(.*))", output, ): self.profile_result += item[0] logger.info(item[0])
def _stop_strobelight_no_throw( self, collect_results: bool, ) -> None: try: # call stop run self._stop_run() logger.info("strobelight profiling stopped")
logger.debug("collection stopped")
if not collect_results: return
self._get_results() except Exception as error: logger.warning("error during stop_strobelight", exc_info=True)
# Return true if strobelight started and is running. Never throw. def _start_strobelight(self) -> bool: strobelight_started = False try: self._run_async() strobelight_started = True logger.info("strobelight run id is: %s", self.current_run_id) self._wait_for_running() logger.info("strobelight profiling running") return True
except Exception as error: logger.warning("error during start_strobelight:", exc_info=True) if strobelight_started: self._stop_strobelight_no_throw(collect_results=False) return False
def profile(self, work_function: Any, *args: Any, **kwargs: Any) -> Any: self.current_run_id = None self.profile_result = None
if locked := StrobelightCLIFunctionProfiler._lock.acquire(False): if not locked: if self.stop_at_error: raise StrobelightCLIProfilerError("concurrent runs not supported")
logger.warning("concurrent runs not supported") return work_function(*args, **kwargs)
started = self._start_strobelight() if not started: if self.stop_at_error: StrobelightCLIFunctionProfiler._lock.release() raise StrobelightCLIProfilerError( "failed to start strobelight profiling" ) result = work_function(*args, **kwargs) StrobelightCLIFunctionProfiler._lock.release() return result
try: logger.debug("collection started") start = timer() result = work_function(*args, **kwargs) end = timer() total_time = end - start # Time in seconds, e.g. 5.38091952400282 logger.info("work function took %s seconds", total_time) self._stop_strobelight_no_throw(collect_results=True) StrobelightCLIFunctionProfiler._lock.release() return result except Exception as error: logger.warning("work function throw exception", exc_info=True) self._stop_strobelight_no_throw(collect_results=False) StrobelightCLIFunctionProfiler._lock.release() raise error
# A function decorator that wraps profile, if no profiler is provided one with# default args is created. A function can be annotated as:# @strobelight()# @strobelight(profiler = StrobelightFunctionProfiler(stop_at_error=True,..))# @strobelight(stop_at_error=True,...)def strobelight( profiler: Optional[StrobelightCLIFunctionProfiler] = None, **kwargs: Any) -> Any: if not profiler: profiler = StrobelightCLIFunctionProfiler(**kwargs)
def strobelight_inner(work_function: Any) -> Any: @functools.wraps(work_function) def wrapper_function(*args: Any, **kwargs: Any) -> Any: return profiler.profile(work_function, *args, **kwargs)
return wrapper_function
return strobelight_inner123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195# mypy: disallow-untyped-defs
import jsonimport loggingimport osimport subprocessfrom datetime import datetimefrom socket import gethostnamefrom typing import Any, Optional
from torch._strobelight.cli_function_profiler import StrobelightCLIFunctionProfiler
logger = logging.getLogger("strobelight_compile_time_profiler")
console_handler = logging.StreamHandler()formatter = logging.Formatter( "%(name)s, line %(lineno)d, %(asctime)s, %(levelname)s: %(message)s")console_handler.setFormatter(formatter)
logger.addHandler(console_handler)logger.setLevel(logging.INFO)logger.propagate = False
def get_fburl(url: str) -> str: short_url = url # Attempt to shorten the URL try: result = subprocess.run( ["fburl", url], capture_output=True, stdin=subprocess.DEVNULL ) if result.returncode == 0: short_url = result.stdout.decode("utf-8") except Exception as e: logger.warning("URL shortening failed: %s, using long URL", repr(e)) return short_url
def get_strobelight_url(identifier: str) -> str: scuba_json = { "aggregateList": [], "aggregation_field": "async_stack_complete", "b_constraints": [[]], "c_constraints": [[]], "cols": ["namespace_id", "namespace_process_id"], "compare": "none", "constraints": [ [{"column": "sample_tags", "op": "all", "value": [f'["{identifier}"]']}] ], "derivedCols": [], "end": "now", "enumCols": [], "filterMode": "DEFAULT", "hideEmptyColumns": "false", "ignoreGroupByInComparison": "false", "is_timeseries": "false", "mappedCols": [], "metric": "count", "modifiers": [], "order": "weight", "order_desc": "true", "param_dimensions": [ {"dim": "py_async_stack", "op": "edge", "param": "0", "anchor": "0"} ], "purposes": [], "return_remainder": "false", "samplingRatio": "1", "should_pivot": "false", "start": "-30 days", "timezone": "America/Los_Angeles", "top": 10000, } scuba_url_prefix = "https://www.internalfb.com/intern/scuba/query/?dataset=pyperf_experimental/on_demand&drillstate=" scuba_url_suff = "&view=GraphProfilerView&&normalized=1726332703&pool=uber" long_url = scuba_url_prefix + json.dumps(scuba_json) + scuba_url_suff return get_fburl(long_url)
class StrobelightCompileTimeProfiler: success_profile_count: int = 0 failed_profile_count: int = 0 ignored_profile_runs: int = 0 inside_profile_compile_time: bool = False enabled: bool = False # A unique identifier that is used as the run_user_name in the strobelight profile to # associate all compile time profiles together. identifier: Optional[str] = None
current_phase: Optional[str] = None
profiler: Optional[Any] = None
max_stack_length: int = int( os.environ.get("COMPILE_STROBELIGHT_MAX_STACK_LENGTH", 500) ) max_profile_time: int = int( os.environ.get("COMPILE_STROBELIGHT_MAX_PROFILE_TIME", 60 * 30) ) # Collect sample each x cycles. sample_each: int = int( float(os.environ.get("COMPILE_STROBELIGHT_SAMPLE_RATE", 1e7)) )
@classmethod def enable(cls, profiler_class: Any = StrobelightCLIFunctionProfiler) -> None: if cls.enabled: logger.info("compile time strobelight profiling already enabled") return
logger.info("compile time strobelight profiling enabled")
if profiler_class is StrobelightCLIFunctionProfiler: import shutil
if not shutil.which("strobeclient"): logger.info( "strobeclient not found, cant enable compile time strobelight profiling, seems" "like you are not on a FB machine." ) return
cls.enabled = True cls._cls_init() # profiler_class should have public API similar to that of StrobelightCLIFunctionProfiler. # we have pass different functionProfilerClass for meta-internal fbcode targets. # NB: the actual implementation in Meta is at # fbcode/caffe2/fb/strobelight/function_profiler.py cls.profiler = profiler_class( sample_each=cls.sample_each, max_profile_duration_sec=cls.max_profile_time, stack_max_len=cls.max_stack_length, async_stack_max_len=cls.max_stack_length, run_user_name="pt2-profiler/" + os.environ.get("USER", os.environ.get("USERNAME", "")), sample_tags={cls.identifier}, )
@classmethod def _cls_init(cls) -> None: cls.identifier = "{date}{pid}{hostname}".format( date=datetime.now().strftime("%Y-%m-%d-%H:%M:%S"), pid=os.getpid(), hostname=gethostname(), )
logger.info("Unique sample tag for this run is: %s", cls.identifier) logger.info( "URL to access the strobelight profile at the end of the run: %s", get_strobelight_url(cls.identifier), )
@classmethod def _log_stats(cls) -> None: logger.info( "%s strobelight success runs out of %s non-recursive compilation events.", cls.success_profile_count, cls.success_profile_count + cls.failed_profile_count, )
# TODO use threadlevel meta data to tags to record phases. @classmethod def profile_compile_time( cls, func: Any, phase_name: str, *args: Any, **kwargs: Any ) -> Any: if not cls.enabled: return func(*args, **kwargs)
if cls.profiler is None: logger.error("profiler is not set") return
if cls.inside_profile_compile_time: cls.ignored_profile_runs += 1 logger.info( "profile_compile_time is requested for phase: %s while already in running phase: %s, recursive call ignored", phase_name, cls.current_phase, ) return func(*args, **kwargs)
cls.inside_profile_compile_time = True cls.current_phase = phase_name
work_result = cls.profiler.profile(func, *args, **kwargs)
if cls.profiler.profile_result is not None: cls.success_profile_count += 1 else: cls.failed_profile_count += 1
cls._log_stats() cls.inside_profile_compile_time = False return work_result123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404# mypy: allow-untyped-defsfrom typing import Any, Callable, List, TypeVar
import torch
__all__ = [ "compile", "assume_constant_result", "reset", "allow_in_graph", "substitute_in_graph", "list_backends", "disable", "set_stance", "cudagraph_mark_step_begin", "wrap_numpy", "is_compiling", "is_dynamo_compiling",]
_F = TypeVar("_F", bound=Callable[..., Any])
def compile(*args, **kwargs): """ See :func:`torch.compile` for details on the arguments for this function. """ return torch.compile(*args, **kwargs)
def reset() -> None: """ This function clears all compilation caches and restores the system to its initial state. It is recommended to call this function, especially after using operations like `torch.compile(...)` to ensure a clean state before another unrelated compilation """ import torch._dynamo
torch._dynamo.reset()
def allow_in_graph(fn): """ Tells the compiler frontend (Dynamo) to skip symbolic introspection of the function and instead directly write it to the graph when encountered.
If you are using :func:`torch.compile` (with backend="inductor" (the default)), or :func:`torch.export.export`, and trying to black-box a Python function throughout all tracing, do not use this API. Instead, please create a custom operator (see `PyTorch Custom Operators Landing Page <https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html>`_)
.. warning::
If you're a typical torch.compile user (e.g. you're applying torch.compile to a model to make it run faster), you probably don't want to use this function. :func:`allow_in_graph` is a footgun because it skips the compiler frontend (Dynamo) that is responsible for doing safety checks (graph breaks, handling closures, etc). Incorrect usage will lead to difficult-to-debug silent incorrectness issues.
Given a Python function with no allow_in_graph decorator, regular execution of torch.compile traces through the function. :func:`allow_in_graph` changes it so that the frontend does not trace inside the function, but the compiler backend still traces through it. Compare this to custom operators, which treats a function as a black box throughout the torch.compile stack. The following table compares these mechanisms.
+------------------------+-----------------------+--------------------------------+ | Mechanism | Frontend (Dynamo) | Backend (AOTAutograd+Inductor) | +========================+=======================+================================+ | no decorator | trace inside | trace inside | +------------------------+-----------------------+--------------------------------+ | allow_in_graph | opaque callable | trace inside | +------------------------+-----------------------+--------------------------------+ | custom op | opaque callable | opaque callable | +------------------------+-----------------------+--------------------------------+
One common use case for :func:`allow_in_graph()` is as an escape hatch for the compiler frontend: if you know the function works w.r.t. to the downstream components of the compilation stack (AOTAutograd and Inductor) but there is a Dynamo bug that prevents it from symbolically introspecting the function properly (or if your code is in C/C++ and therefore cannot be introspected with Dynamo), then one can decorate said function with :func:`allow_in_graph` to bypass Dynamo.
We require that ``fn`` adhere to the following restrictions. Failure to adhere results in undefined behavior:
- The inputs to ``fn`` must be Proxy-able types in the FX graph. Valid types include: Tensor/int/bool/float/None/List[Tensor?]/List[int?]/List[float?] Tuple[Tensor?, ...]/Tuple[int?, ...]/Tuple[float?, ...]/torch.dtype/torch.device - The outputs to ``fn`` must be Proxy-able types in the FX graph (see previous bullet) - all Tensors used inside of ``fn`` must be passed directly as inputs to ``fn`` (as opposed to being captured variables).
Args: fn: A callable representing the function to be included in the graph. If ``fn`` is a list or tuple of callables it recursively applies :func:`allow_in_graph()` to each function and returns a new list or tuple containing the modified functions.
Example::
torch.compiler.allow_in_graph(my_custom_function)
@torch.compile(...) def fn(x): x = torch.add(x, 1) x = my_custom_function(x) x = torch.add(x, 1) return x
fn(...)
Will capture a single graph containing ``my_custom_function()``.
""" import torch._dynamo
return torch._dynamo.allow_in_graph(fn)
def substitute_in_graph( original_fn: _F, *, can_constant_fold_through: bool = False, skip_signature_check: bool = False,) -> Callable[[_F], _F]: """ Register a polyfill handler for a function, usually a C function from the C extension, to be used in place of the original function when inlining the original function in the graph.
.. note::
The polyfill handler is only used when inlining the original function. It is not used when the original function is called directly. In the eager mode, the decorated function calls the performant C function rather than the polyfill handler.
The polyfill handler is a function that will be called in place of the original function when inlining the original function. The polyfill handler should have the same signature and the same behavior as the original function.
Args: original_fn (callable): The original function, usually a C function, to register a polyfill handler for. can_constant_fold_through (bool, optional): Whether the polyfill handler can be constant folded through. That is, if the polyfill handler is a pure function and its arguments are constant, the result of the polyfill handler can be constant folded during the compilation. Defaults to ``False``. skip_signature_check (bool, optional): Whether to skip the signature check between the original function and the polyfill handler. Defaults to ``False``.
Returns: A decorator that registers the polyfill handler for the original function.
Example::
>>> import operator >>> operator.indexOf([1, 2, 3, 4, 5], 3) 2 >>> torch.compile(operator.indexOf, fullgraph=True)([1, 2, 3, 4, 5], 3) ... # xdoctest: +SKIP("Long tracebacks") Traceback (most recent call last): ... torch._dynamo.exc.Unsupported: ...
>>> @torch.compiler.substitute_in_graph(operator.indexOf) ... def indexOf(a, b, /): ... for i, item in enumerate(a): ... if item is b or item == b: ... return i ... raise ValueError("sequence.index(x): x not in sequence") >>> >>> torch.compile(operator.indexOf, fullgraph=True)([1, 2, 3, 4, 5], 3) 2 """ import torch._dynamo
return torch._dynamo.substitute_in_graph( original_fn, can_constant_fold_through=can_constant_fold_through, skip_signature_check=skip_signature_check, )
def list_backends(exclude_tags=("debug", "experimental")) -> List[str]: """ Return valid strings that can be passed to `torch.compile(..., backend="name")`.
Args: exclude_tags(optional): A tuple of strings representing tags to exclude. """ import torch._dynamo
return torch._dynamo.list_backends(exclude_tags)
def assume_constant_result(fn): """ This function is used to mark a function `fn` as having a constant result. This allows the compiler to optimize away your function Returns The same function `fn`
Args: fn: The function to be marked as having a constant result.
.. warning:: `assume_constant_result` can if invalid cause safety and soundness issues, :func:`torch.compile` will not attempt to validate whether the constant assumption is true or not
""" import torch._dynamo
return torch._dynamo.assume_constant_result(fn)
def disable(fn=None, recursive=True): """ This function provides a decorator to disable compilation on a function It also provides the option of recursively disabling called functions
Args: fn (optional): The function to disable recursive (optional): A boolean value indicating whether the disabling should be recursive. """ import torch._dynamo
return torch._dynamo.disable(fn, recursive)
def set_stance( stance: str = "default", *, skip_guard_eval_unsafe=False, force_backend=None): """ Set the current stance of the compiler. Can be used as a function, context manager, or decorator. Do not use this function inside a `torch.compile` region - an error will be raised otherwise.
.. code-block:: python
@torch.compile def foo(x): ...
@torch.compiler.set_stance("force_eager") def bar(): # will not be compiled foo(...)
bar()
with torch.compiler.set_stance("force_eager"): # will also not be compiled foo(...)
torch.compiler.set_stance("force_eager") # will also not be compiled foo(...) torch.compiler.set_stance("default")
# will be compiled foo(...)
Args: stance: The stance to set the compiler to. Valid values are:
- "default": The default stance, used for normal compilation. - "force_eager": Ignore all `torch.compile` directives. - "eager_on_recompile": Run code eagerly when a recompile is necessary. If there is cached compiled code valid for the input, it will still be used. - "fail_on_recompile": Raise an error when recompiling a function.
skip_guard_eval_unsafe: A flag to run only differentiating guards. CAUTION - This flag is unsafe and should only be used if your setup meets the following conditions.
torch.compile uses a guard system to support recompilations and choose which compiled artifact to run at runtime. These guards, though efficient, add some overhead, which may impact performance in scenarios where you need to optimize for minimal guard processing time. This API enables you to disable guard evaluation, assuming that you have warmed up the compiled model with a sufficient variety of inputs. This assumption means that, after the warmup phase, no further recompilations will be necessary. If this assumption fails, there is a risk of silently producing incorrect results (hence the term "unsafe" in the API name).
force_backend: If `stance` is "default", this argument can be used to force `torch.compile` to use a specific backend. Otherwise, an error is raised. """ import torch._dynamo
return torch._dynamo.set_stance( stance, skip_guard_eval_unsafe=skip_guard_eval_unsafe, force_backend=force_backend, )
# forbid in graphset_stance._dynamo_forbidden = True # type: ignore[attr-defined]
def cudagraph_mark_step_begin(): """ Indicates that a new iteration of inference or training is about to begin.
CUDA Graphs will free tensors of a prior iteration. A new iteration is started on each invocation of torch.compile, so long as there is not a pending backward that has not been called.
If that heuristic is wrong, such as in the following example, manually mark it with this api.
.. code-block:: python
@torch.compile(mode="reduce-overhead") def rand_foo(): return torch.rand([4], device="cuda")
for _ in range(5): torch.compiler.cudagraph_mark_step_begin() rand_foo() + rand_foo()
For more details, see `torch.compiler_cudagraph_trees <https://pytorch.org/docs/main/torch.compiler_cudagraph_trees.html>`__ """ from torch._inductor import cudagraph_trees
cudagraph_trees.mark_step_begin()
def wrap_numpy(fn): r"""Decorator that turns a function from ``np.ndarray``s to ``np.ndarray``s into a function from ``torch.Tensor``s to ``torch.Tensor``s.
It is designed to be used with :func:`torch.compile` with ``fullgraph=True``. It allows to compile a NumPy function as if it were a PyTorch function. This allows you to run NumPy code on CUDA or compute its gradients.
.. note::
This decorator does not work without :func:`torch.compile`.
Example::
>>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA) >>> # Compile a NumPy function as a Tensor -> Tensor function >>> @torch.compile(fullgraph=True) >>> @torch.compiler.wrap_numpy >>> def fn(a: np.ndarray): >>> return np.sum(a * a) >>> # Execute the NumPy function using Tensors on CUDA and compute the gradients >>> x = torch.arange(6, dtype=torch.float32, device="cuda", requires_grad=True) >>> out = fn(x) >>> out.backward() >>> print(x.grad) tensor([ 0., 2., 4., 6., 8., 10.], device='cuda:0') """ from torch._dynamo.external_utils import wrap_numpy as wrap
return wrap(fn)
_is_compiling_flag: bool = False
def is_compiling() -> bool: """ Indicates whether a graph is executed/traced as part of torch.compile() or torch.export().
Note that there are 2 other related flags that should deprecated eventually: * torch._dynamo.external_utils.is_compiling() * torch._utils.is_compiling()
Example::
>>> def forward(self, x): >>> if not torch.compiler.is_compiling(): >>> pass # ...logic that is not needed in a compiled/traced graph... >>> >>> # ...rest of the function... """ if torch.jit.is_scripting(): return False else: return _is_compiling_flag
def is_dynamo_compiling() -> bool: """ Indicates whether a graph is traced via TorchDynamo.
It's stricter than is_compiling() flag, as it would only be set to True when TorchDynamo is used.
Example::
>>> def forward(self, x): >>> if not torch.compiler.is_dynamo_compiling(): >>> pass # ...logic that is not needed in a TorchDynamo-traced graph... >>> >>> # ...rest of the function... """ return False12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667"""This is the top-level configuration module for the compiler, containingcross-cutting configuration options that affect all parts of the compilerstack.
You may also be interested in the per-component configuration modules, whichcontain configuration options that affect only a specific part of the compiler:
* :mod:`torch._dynamo.config`* :mod:`torch._inductor.config`* :mod:`torch._functorch.config`* :mod:`torch.fx.experimental.config`"""
import osimport sysfrom typing import Optional
__all__ = [ "job_id",]
# NB: Docblocks go UNDER variable definitions! Use spacing to make the# grouping clear.
# FB-internal note: you do NOT have to specify this explicitly specify this if# you run on MAST, we will automatically default this to# mast:MAST_JOB_NAME:MAST_JOB_VERSION.job_id: Optional[str] = os.environ.get("TORCH_COMPILE_JOB_ID", None)"""Semantically, this should be an identifier that uniquely identifies, e.g., atraining job. You might have multiple attempts of the same job, e.g., if it waspreempted or needed to be restarted, but each attempt should be runningsubstantially the same workload with the same distributed topology. You canset this by environment variable with :envvar:`TORCH_COMPILE_JOB_ID`.
Operationally, this controls the effect of profile-guided optimization relatedpersistent state. PGO state can affect how we perform compilation acrossmultiple invocations of PyTorch, e.g., the first time you run your program wemay compile twice as we discover what inputs are dynamic, and then PGO willsave this state so subsequent invocations only need to compile once, becausethey remember it is dynamic. This profile information, however, is sensitiveto what workload you are running, so we require you to tell us that two jobsare *related* (i.e., are the same workload) before we are willing to reusethis information. Notably, PGO does nothing (even if explicitly enabled)unless a valid ``job_id`` is available. In some situations, PyTorch canconfigured to automatically compute a ``job_id`` based on the environment itis running in.
Profiles are always collected on a per rank basis, so different ranks may havedifferent profiles. If you know your workload is truly SPMD, you can run with:data:`torch._dynamo.config.enable_compiler_collectives` to ensure nodes getconsistent profiles across all ranks."""
cache_key_tag: str = os.environ.get("TORCH_COMPILE_CACHE_KEY_TAG", "")"""Tag to be included in the cache key generation for all torch compile caching.A common use case for such a tag is to break caches."""
from torch.utils._config_module import install_config_module
install_config_module(sys.modules[__name__])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157# mypy: allow-untyped-defsimport loggingimport pdbimport sysimport tracebackimport typing
import torch
log = logging.getLogger(__name__)
def is_available() -> bool: """ Return ``True`` if the distributed package is available.
Otherwise, ``torch.distributed`` does not expose any other APIs. Currently, ``torch.distributed`` is available on Linux, MacOS and Windows. Set ``USE_DISTRIBUTED=1`` to enable it when building PyTorch from source. Currently, the default value is ``USE_DISTRIBUTED=1`` for Linux and Windows, ``USE_DISTRIBUTED=0`` for MacOS. """ return hasattr(torch._C, "_c10d_init")
if is_available() and not torch._C._c10d_init(): raise RuntimeError("Failed to initialize torch.distributed")
# Custom Runtime Errors thrown from the distributed packageDistError = torch._C._DistErrorDistBackendError = torch._C._DistBackendErrorDistNetworkError = torch._C._DistNetworkErrorDistStoreError = torch._C._DistStoreError
if is_available(): from torch._C._distributed_c10d import ( _broadcast_coalesced, _compute_bucket_assignment_by_size, _ControlCollectives, _DEFAULT_FIRST_BUCKET_BYTES, _make_nccl_premul_sum, _register_builtin_comm_hook, _register_comm_hook, _StoreCollectives, _test_python_store, _verify_params_across_processes, Backend as _Backend, BuiltinCommHookType, DebugLevel, FileStore, get_debug_level, GradBucket, Logger, PrefixStore, ProcessGroup as ProcessGroup, Reducer, set_debug_level, set_debug_level_from_env, Store, TCPStore, Work as _Work, )
class _DistributedPdb(pdb.Pdb): """ Supports using PDB from inside a multiprocessing child process.
Usage: _DistributedPdb().set_trace() """
def interaction(self, *args, **kwargs): _stdin = sys.stdin try: sys.stdin = open("/dev/stdin") pdb.Pdb.interaction(self, *args, **kwargs) finally: sys.stdin = _stdin
_breakpoint_cache: typing.Dict[int, typing.Any] = {}
def breakpoint(rank: int = 0, skip: int = 0): """ Set a breakpoint, but only on a single rank. All other ranks will wait for you to be done with the breakpoint before continuing.
Args: rank (int): Which rank to break on. Default: ``0`` skip (int): Skip the first ``skip`` calls to this breakpoint. Default: ``0``. """ if skip > 0: key = hash(str(traceback.format_exc())) counter = _breakpoint_cache.get(key, 0) + 1 _breakpoint_cache[key] = counter if counter <= skip: log.warning("Skip the breakpoint, counter=%d", counter) return
if get_rank() == rank: pdb = _DistributedPdb() pdb.message( "\n!!! ATTENTION !!!\n\n" f"Type 'up' to get to the frame that called dist.breakpoint(rank={rank})\n" ) pdb.set_trace() # If Meta/Python keys are in the TLS, we want to make sure that we ignore them # and hit the (default) CPU/CUDA implementation of barrier. meta_in_tls = torch._C._meta_in_tls_dispatch_include() guard = torch._C._DisableTorchDispatch() # type: ignore[attr-defined] torch._C._set_meta_in_tls_dispatch_include(False) try: barrier() finally: torch._C._set_meta_in_tls_dispatch_include(meta_in_tls) del guard
if sys.platform != "win32": from torch._C._distributed_c10d import HashStore
from .device_mesh import DeviceMesh, init_device_mesh
# Variables prefixed with underscore are not auto imported # See the comment in `distributed_c10d.py` above `_backend` on why we expose # this. from .distributed_c10d import * # noqa: F403 from .distributed_c10d import ( _all_gather_base, _coalescing_manager, _CoalescingManager, _create_process_group_wrapper, _get_process_group_name, _rank_not_in_group, _reduce_scatter_base, get_node_local_rank, ) from .remote_device import _remote_device from .rendezvous import ( _create_store_from_options, register_rendezvous_handler, rendezvous, )
set_debug_level_from_env()
else: # This stub is sufficient to get # python test/test_public_bindings.py -k test_correct_module_names # working even when USE_DISTRIBUTED=0. Feel free to add more # stubs as necessary. # We cannot define stubs directly because they confuse pyre
class _ProcessGroupStub: pass
sys.modules["torch.distributed"].ProcessGroup = _ProcessGroupStub # type: ignore[attr-defined]1234567891011121314151617181920212223242526272829303132333435363738# mypy: allow-untyped-defs# Copyright (c) Meta Platforms, Inc. and affiliatesfrom typing import Any, Protocol, runtime_checkable
import torch
@runtime_checkableclass _Checkpointable(Protocol): # noqa: PYI046 """ Interface for checkpointable objects. Implemented as a protocol, implicit subtyping is supported so subclasses do not need to inherit this explicitly. This is to allow arbitrary objects/tensor subclasses to hook into DCP seamlessly through implementing the interface. """
def __create_write_items__(self, fqn: str, object: Any): """ Return a list of WriteItems based on object's contents. """ raise NotImplementedError( "_Checkpointable._create_write_items is not implemented" )
def __create_chunk_list__(self): """ Return a list of `ChunkStorageMetadata` based on object's contents. """ raise NotImplementedError( "_Checkpointable._create_chunk_list is not implemented" )
def __get_tensor_shard__(self, index) -> torch.Tensor: """ Return a 'torch.Tensor' shard based on 'MetadataIndex'. """ raise NotImplementedError( "_Checkpointable._get_tensor_shard is not implemented" )1234567891011121314151617181920212223242526272829303132333435363738394041424344import weakreffrom typing import cast, Optional
import torch.nn as nn
class _State: pass
_module_state_mapping: weakref.WeakKeyDictionary[ nn.Module, weakref.ReferenceType[_State]] = weakref.WeakKeyDictionary()
def _insert_module_state(module: nn.Module, state: _State) -> None: global _module_state_mapping assert module not in _module_state_mapping, f"Inserting {module} more than once." _module_state_mapping[module] = weakref.ref(state)
def _get_module_state(module: nn.Module) -> Optional[_State]: """ Return the ``_State`` in ``model``.
Given a ``module``, this API finds out if the module is also a ``_State`` instance or if the module is managed by a composable API. If the module is also a ``_State``, ``module`` will be casted to ``_State` and returned. If it is managed by a composable API, the corresponding ``_State`` will be returned. """ global _module_state_mapping if isinstance(module, _State): return cast(_State, module) else: # https://github.com/pytorch/pytorch/issues/107054 if module in _module_state_mapping: state_ref = _module_state_mapping[module] state = state_ref() if state is None: raise AssertionError("State has already been garbage collected") return state else: return None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191# mypy: allow-untyped-defsimport contextlibimport sysimport warningsfrom typing import Any, cast, List, Optional, Tuple, Type, TYPE_CHECKING, Union
import torchimport torch.distributed as distimport torch.distributed.distributed_c10d as c10dfrom torch.distributed.device_mesh import DeviceMeshfrom torch.fx.experimental.proxy_tensor import get_proxy_mode
from . import _functional_collectives_impl as fun_col_impl
try: from torch.utils._cxx_pytree import tree_map_onlyexcept ImportError: from torch.utils._pytree import tree_map_only # type: ignore[no-redef]
if torch._running_with_deploy():
def is_torchdynamo_compiling(): """Can't import torchdynamo in torchdeploy builds currently.""" return False
else: try: from torch.compiler import is_dynamo_compiling as is_torchdynamo_compiling except Exception: warnings.warn( "Unable to import torchdynamo util `is_torchdynamo_compiling`, so won't support torchdynamo correctly" )
def is_torchdynamo_compiling(): return False
"""New traceable, functional collectives.RFC: https://github.com/pytorch/pytorch/issues/93173
compiler: trace these ops with plain-old-data schemas, then choose how to lower them. eager: execute these 'functional' ops which in eager return AsyncCollectiveTensor subclasses, automatically calling .wait() on underlying/hidden async 'work' obj only when fed to a downstream op.
Issues:* Where should these ops live? Couldn't `import torch` if putting these ops in existing torch.distributed files* Proper support for eager requires inplace ops. We should explore having it as an option for the API."""
"""Functional collectives are asynchronous only and we perform implicit stream synchronizationon behalf of the user.
We use AsyncCollectiveTensor to wrap the result tensor of a collective and it lets us witnessfirst usage of the tensor and insert cross stream sync at the right place.
The above are the easy bits, the hard one is how we match the Work object returned byc10d and the tensor AsyncCollectiveTensor wraps. We alloc the tensor inside the collectiveop implementation (see ``clone()`` call in ``_all_reduce``) and then it's handled by thedispatcher which might call other implementations that are allowed to change the returnedtensor - even return a tensor with a different shape (see ``torch.vmap``).
This means the caller of our ops receives a Tensor that is not guaranteed to be the sameallocated by our implementations and that makes pairing The AsyncTensor to the originaltensor a lot harder. This pairing is needed so we can lookup the Work object to use.
Originally, we tried WeakKeyDictionary to map from Tensor to Work, but because Tensor'sidentity is not stable across dispatch, the op caller would end up with a different Tensorinstance that would not match any in the dictionary.
With Tensor identity out of the question, we decided use the tensor data pointer, whichshould be stable across all the Tensor changes done during dispatch.
We have a dictionary of tensor::data_ptr -> Work that we insert right after we call into c10d.
We use this dictionary when AsyncCollectiveTensor is used to invoke Work::wait()
Finally, we setup a finalizer against the tensor wrapper to observe it getting collected so wecan clean up stale entries in the dictionary.
To eliminate the possibility of races we have a global version counter that is used by the finalizer.
As a wise man said once: Don't cross the streams (https://www.youtube.com/watch?v=wyKQe_i9yyo)
"""
"""Functional collectives can accept any of these types to describe the ranks participating in collectives.
The different types will be desugared to a canonical format"""RANK_TYPES = Union[ List[int], List[List[int]], dist.ProcessGroup, DeviceMesh, Tuple["dist.tensor.DeviceMesh", int], str,]
"""User facing APIs for functional collectives-------------------------------------------
These apis are called by user code and expected to work both in eager execution and compilation,but there are significant differences to how the two modes are implemented underneath.
Eager execution is 'optimized' using a tensor subclass that schedules the synchronization (via wait_tensor() op)just before the tensor is first used. Compiled tracing currently relies on the compiler to perform this optimization,and cannot yet correctly trace the AsyncTensor wrapper class. In the future, these paths may be unifiedif sufficient subclass support is added in dynamo.
Example: all_reduce is an entrypoint API, and other collectives follow a similar pattern.
Here's how it works under torch.compile/dynamo:all_reduce(...) |--> _expand_group(...) - desugars processgroup into canonical/traceable format |--> c10d_functional.all_reduce(...) - dynamo captures this op call, doesn't trace deeper |--> _maybe_wrap_tensor(...) - wait_tensor() op is immediately called, no AsyncTensor subclass needed
And under eager execution:all_reduce(...) |--> _expand_group(...) - same as above, but less critical for eager |--> c10d_functional.all_reduce(...) - dispatches to real kernel OR records op in trace |--> _maybe_wrap_tensor(...) - AsyncTensor wrapper applied to returned tensor, which issues wait_tensor() at the time of first use"""
def wait_tensor(tensor): """ Wait on a tensor returned by the collectives ops.
Waiting follows device semantics, which means blocking on CPU and synchronizing streams on CUDA. """ return torch.ops._c10d_functional.wait_tensor(tensor) # type: ignore[attr-defined]
def broadcast(self: torch.Tensor, src: int, group: RANK_TYPES, tag: str = ""): """ Broadcasts the tensor to all processes in the given process group.
Args: src (int): Source rank group (ProcessGroup or List[int]): The process group to work on. tag (str, optional): A unique identifier for the collective. Default: empty string """ group_name = _resolve_group_name(group, tag) tensor = torch.ops._c10d_functional.broadcast(self, src, group_name) return _maybe_wrap_tensor(tensor)
def all_reduce(self: torch.Tensor, reduceOp: str, group: RANK_TYPES, tag: str = ""): """ Reduces the tensor data across all machines in such a way that all get the final result.
The input tensor is left unmodified.
Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh
:: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover that information and perform collective algebraic optimization. Use other forms of input for that. """ group_name = _resolve_group_name(group, tag) tensor = torch.ops._c10d_functional.all_reduce(self, reduceOp.lower(), group_name) return _maybe_wrap_tensor(tensor)
def all_gather_tensor( self: torch.Tensor, gather_dim: int, group: RANK_TYPES, tag: str = "",): """ Gather tensor data across from all machines and concatenate over ``gather_dim``.
Note that it currently only supports gather_dim = 0.
The input tensor is left unmodified. Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh
:: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover that information and perform collective algebraic optimization. Use other forms of input for that. """ assert self.is_contiguous() group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name) tensor = torch.ops._c10d_functional.all_gather_into_tensor( self, group_size, group_name ) res = _maybe_wrap_tensor(tensor) # TODO this should be done inside AsyncCollectiveTensor to delay the wait() call if gather_dim != 0: # torch.cat access the data so we already need to wait here, first do wait # and then chunk + cat avoid us going through ACT dispatching logic again if isinstance(res, AsyncCollectiveTensor): res = res.wait() # type: ignore[attr-defined] res = torch.cat(torch.chunk(res, group_size, dim=0), dim=gather_dim) return res
def all_gather_tensor_autograd( self: torch.Tensor, gather_dim: int, group: RANK_TYPES, tag: str = "",): """ Gather tensor data across from all machines and concatenate over ``gather_dim``.
Note that it currently only supports gather_dim = 0.
This function is the same as all_gather_tensor but will propagate the backwards gradient across workers.
See all_gather_tensor for more details on usage. """ group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name)
tensor = torch.ops._c10d_functional_autograd.all_gather_into_tensor( self, group_size, group_name ) res = _FromTorchTensor.apply(tensor) # TODO this should be done inside AsyncCollectiveTensor to delay the wait() call if gather_dim != 0: # torch.cat access the data so we already need to wait here, first do wait # and then chunk + cat avoid us going through ACT dispatching logic again if isinstance(res, AsyncCollectiveTensor): res = res.wait() # type: ignore[attr-defined] res = torch.cat(torch.chunk(res, group_size, dim=0), dim=gather_dim) return res
def reduce_scatter_tensor( self: torch.Tensor, reduceOp: str, scatter_dim: int, group: RANK_TYPES, tag: str = "",): """ Reduces the tensor data across all machines in such a way that all get the final result, then scatter the results to corresponding ranks.
The input tensor is left unmodified. Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover that information and perform collective algebraic optimization. Use other forms of input for that. """ group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name)
assert ( self.size(scatter_dim) % group_size == 0 ), f"input dimension 0 ({self.size(0)} must be a multiple of group_size {group_size}" if scatter_dim != 0: tensor_list = torch.chunk(self, group_size, dim=scatter_dim) self = torch.cat(tensor_list)
tensor = torch.ops._c10d_functional.reduce_scatter_tensor( self, reduceOp.lower(), group_size, group_name, # type: ignore[possibly-undefined] ) res = _maybe_wrap_tensor(tensor) return res
def reduce_scatter_tensor_autograd( self: torch.Tensor, reduceOp: str, scatter_dim: int, group: RANK_TYPES, tag: str = "",): """ Reduces the tensor data across all machines in such a way that all get the final result, then scatter the results to corresponding ranks.
This function is the same as reduce_scatter_tensor but will propagate the backwards gradient across workers.
Currently only the "sum" reduceOp is supported.
See reduce_scatter_tensor for more details on usage. """
group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name)
assert ( self.size(scatter_dim) % group_size == 0 ), f"input dimension 0 ({self.size(0)} must be a multiple of group_size {group_size}" if scatter_dim != 0: tensor_list = torch.chunk(self, group_size, dim=scatter_dim) self = torch.cat(tensor_list)
tensor = torch.ops._c10d_functional_autograd.reduce_scatter_tensor( self, reduceOp.lower(), group_size, group_name, # type: ignore[possibly-undefined] ) res = _FromTorchTensor.apply(tensor) return res
def all_reduce_coalesced( self: List[torch.Tensor], reduceOp: str, group: RANK_TYPES, tag: str = "") -> List[torch.Tensor]: """ Reduces a list of tensors across all machines in such a way that all get the final result.
The all tensors in the input list are left unmodified.
Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh
:: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover that information and perform collective algebraic optimization. Use other forms of input for that. """ group_name = _resolve_group_name(group, tag) tensor_list = torch.ops._c10d_functional.all_reduce_coalesced( # type: ignore[attr-defined] self, reduceOp.lower(), group_name, ) return list(map(_maybe_wrap_tensor, tensor_list))
def all_gather_into_tensor_coalesced( self: List[torch.Tensor], group: RANK_TYPES, tag: str = "") -> List[torch.Tensor]: """ Gather a list of tensors across from all machines.
Note that it currently only supports gather_dim = 0.
The input tensor is left unmodified. Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh
:: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover that information and perform collective algebraic optimization. Use other forms of input for that. """ group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name) tensor_list = torch.ops._c10d_functional.all_gather_into_tensor_coalesced( # type: ignore[attr-defined] self, group_size, group_name, ) return list(map(_maybe_wrap_tensor, tensor_list))
def reduce_scatter_tensor_coalesced( inputs: List[torch.Tensor], reduceOp: str, scatter_dim: List[int], group: RANK_TYPES, tag: str = "",) -> List[torch.Tensor]: """ Reduces a list of tensors across all machines in such a way that all get the final result, then scatter the results to corresponding ranks.
The input tensors are left unmodified. Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh
:: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover that information and perform collective algebraic optimization. Use other forms of input for that. """ group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name)
assert len(scatter_dim) == len(inputs) for idx, (dim, tensor) in enumerate(zip(scatter_dim, inputs)): assert ( tensor.size(dim) % group_size == 0 ), f"input dimension {dim} ({tensor.size(dim)} must be a multiple of group_size {group_size} for tensor at index {idx}" if dim != 0: tensor_list = torch.chunk(tensor, group_size, dim=dim) inputs[idx] = torch.cat(tensor_list)
tensor_list = torch.ops._c10d_functional.reduce_scatter_tensor_coalesced( # type: ignore[attr-defined] inputs, reduceOp.lower(), group_size, group_name, # type: ignore[possibly-undefined] )
return list(map(_maybe_wrap_tensor, tensor_list))
# This is a bit unsafe: it checks if the first argument in the schema reports as a non-mutable alias.# Today, this maps 1:1 with "aten ops that are views".def _is_view_op(tgt): assert isinstance(tgt, torch._ops.OpOverload) schema = tgt._schema if len(schema.arguments) > 0: first_arg = schema.arguments[0] # check if op is a view return first_arg.alias_info is not None and not first_arg.alias_info.is_write
def all_to_all_single( self: torch.Tensor, output_split_sizes: Optional[List[int]], input_split_sizes: Optional[List[int]], group: RANK_TYPES, tag: str = "",) -> torch.Tensor: """ Each process splits input tensor and then scatters the split list to all processes in a group. Then concatenate the received tensors from all the processes in the group and return single output tensor.
Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh
:: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover that information and perform collective algebraic optimization. Use other forms of input for that. """ if output_split_sizes is not None: assert all( isinstance(size, (int, torch.SymInt)) for size in output_split_sizes ), output_split_sizes if input_split_sizes is not None: assert all( isinstance(size, (int, torch.SymInt)) for size in input_split_sizes ), input_split_sizes group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name) if output_split_sizes is None or input_split_sizes is None: assert output_split_sizes is None and input_split_sizes is None, ( "output_split_sizes and input_split_sizes must either be " "specified together or both set to None" ) output_split_sizes = [self.shape[0] // group_size] * group_size input_split_sizes = output_split_sizes tensor = torch.ops._c10d_functional.all_to_all_single( # type: ignore[attr-defined] self, output_split_sizes, input_split_sizes, group_name, ) return _maybe_wrap_tensor(tensor)
def all_to_all_single_autograd( self: torch.Tensor, output_split_sizes: Optional[List[int]], input_split_sizes: Optional[List[int]], group: RANK_TYPES, tag: str = "",) -> torch.Tensor: """ Same as all_to_all_single but supports autograd. """ if output_split_sizes is not None: assert all( isinstance(size, (int, torch.SymInt)) for size in output_split_sizes ), output_split_sizes if input_split_sizes is not None: assert all( isinstance(size, (int, torch.SymInt)) for size in input_split_sizes ), input_split_sizes
group_name = _resolve_group_name(group, tag) group_size = c10d._get_group_size_by_name(group_name) if output_split_sizes is None or input_split_sizes is None: assert output_split_sizes is None and input_split_sizes is None, ( "output_split_sizes and input_split_sizes must either be " "specified together or both set to None" ) output_split_sizes = [self.shape[0] // group_size] * group_size input_split_sizes = output_split_sizes tensor = torch.ops._c10d_functional_autograd.all_to_all_single( # type: ignore[attr-defined] self, output_split_sizes, input_split_sizes, group_name, ) return _FromTorchTensor.apply(tensor)
def permute_tensor( self: torch.Tensor, src_dst: List[int], group: RANK_TYPES, tag: str = "",) -> torch.Tensor: """ Permutes the elements of the tensor according to the given source/destination pairs. `src_dst` should be defined such that src_dst[m] == n means m sends to n.
Group can be one of: List[int]: ranks participating in the collective. List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD. ProcessGroup: Will perform a collective using the ranks and tag of the PG. DeviceMesh: Do a SPMD collective over all ranks of the mesh (DeviceMesh, int): Do a MPMD collective over one """ t, rankset, group_size = _expand_group(group, tag) local_pg = c10d._find_or_create_pg_by_ranks_and_tag(t, rankset, group_size)
output_split_sizes = [0] * group_size input_split_sizes = [0] * group_size for src, dst in enumerate(src_dst): if src == dist.get_rank(local_pg): input_split_sizes[dst] = self.numel() if dst == dist.get_rank(local_pg): output_split_sizes[src] = self.numel()
return all_to_all_single(self, output_split_sizes, input_split_sizes, group, tag)
class AsyncCollectiveTensor(torch.Tensor): r""" A Tensor wrapper subclass that is used to trigger a call to wait prior to first use of the underlying tensor. Use it inside functional collective pytorch wrappers like the following: def functional_collective(self, group, tag): tag, rankset, group_size = _expand_group(group, tag) tensor = torch.ops.c10d_functional.{collective}(self, tag, rankset, group_size) return _maybe_wrap_tensor(tensor) """ elem: torch.Tensor completed: bool
__slots__ = ["elem", "completed"]
@staticmethod def __new__(cls, elem: torch.Tensor): r = torch.Tensor._make_wrapper_subclass( # type: ignore[attr-defined] cls, elem.size(), strides=elem.stride(), storage_offset=elem.storage_offset(), dtype=elem.dtype, layout=elem.layout, device=elem.device, requires_grad=elem.requires_grad, ) r.elem = elem r.completed = False return r
def __tensor_flatten__(self): return ["elem"], None
def tolist(self): return self.trigger_wait().tolist()
@staticmethod def __tensor_unflatten__(inner_tensors, meta, outer_size, outer_stride): assert meta is None elem = inner_tensors["elem"] return AsyncCollectiveTensor(elem)
def __coerce_same_metadata_as_tangent__( self, expected_metadata: Any, expected_type: Optional[Type] = None ): if expected_type is not torch.Tensor: return None
return self.trigger_wait()
def __repr__(self) -> str: # type: ignore[override] return f"AsyncCollectiveTensor({self.trigger_wait()})"
def trigger_wait(self): if not self.completed: out = wait_tensor(self.elem) self.completed = True return out else: return self.elem
def wait(self) -> torch.Tensor: return wait_tensor(self.elem)
def _get_acs_underlying_tensor(self): """This method enables _functional_collectives_impl to test if a tensor is an ACS""" return self.elem
@classmethod def __torch_dispatch__(cls, func, types, args=(), kwargs=None): if func == torch.ops.aten.view.default: # Fast handle aten.view as a lot of view related op goes to aten.view # eventually, this avoids pytree slowdown res = func(args[0].elem, args[1]) wrapper_res = AsyncCollectiveTensor(res) return wrapper_res
is_view_op = _is_view_op(func)
def unwrap(e: AsyncCollectiveTensor): # wait_tensor is idepotent and will do stream sync only once if not is_view_op: return e.trigger_wait() return e.elem
def wrap(e: torch.Tensor): # wait_tensor is idepotent and will do stream sync only once assert not isinstance(e, AsyncCollectiveTensor) res = AsyncCollectiveTensor(e) return res
unwrapped_args = tree_map_only(AsyncCollectiveTensor, unwrap, args) unwrapped_kwargs = tree_map_only(AsyncCollectiveTensor, unwrap, kwargs)
# we don't wrap the result as it doesn't need to be waited on. out = func(*unwrapped_args, **unwrapped_kwargs)
# View ops dont require a sync, so we should re-wrap the outputs. if is_view_op: out = tree_map_only(torch.Tensor, wrap, out)
return out
def numpy(self): # type: ignore[override] return self.wait().numpy()
"""Utils and infrastructure for tracing support"""
def _expand_group(group: RANK_TYPES, tag: str = "") -> Tuple[str, List[int], int]: """ _expand_group desugars the different RANK_TYPES types into a canonical format that is traceable.
By having this be part of the explicit eager codepath, we avoid having to specialize behavior inside torchdynamo and can still interoperate with processgroup objects or other untraceable forms. """ # had to define this hack _inside_ expand_group to avoid # graph_break [('torch.* op returned non-Tensor int # caused by 'cast_*` functions being treated as 'torch.*' ops (iiuc) if TYPE_CHECKING:
def cast_listlistint(x): return cast(List[List[int]], x)
def cast_listint(x): return cast(List[int], x)
else: # fake cast op for use at runtime since dynamo doesn't support real cast # also, dynamo didn't like encountering 'typing' objects () # NotImplementedError: argument of type: <class 'typing._GenericAlias'> def cast_listlistint(x): return x
def cast_listint(x): return x
rankset: List[int] if isinstance(group, list): if isinstance(group[0], list): nested_list = cast_listlistint(group) rankset = [] group_size = -1 for rs in nested_list: rankset.extend(rs) if group_size != -1 and group_size != len(rs): raise ValueError( f"group sizes must be identical found {group_size} and {len(rs)}" ) group_size = len(rs) else: rankset = cast_listint(group) group_size = len(rankset) elif isinstance(group, dist.ProcessGroup): rankset = dist.get_process_group_ranks(group) group_size = len(rankset) tag = tag or c10d._get_group_tag(group) elif isinstance(group, DeviceMesh): assert ( group.ndim == 1 ), "Only 1D mesh is supported, pass in (DeviceMesh, int) together if mesh > 1D" # TODO: it should run collective in the whole mesh instead of dim 0 tag, rankset, _ = group._dim_group_infos[0] group_size = len(rankset) elif isinstance(group, tuple): if ( len(group) == 2 and isinstance(group[0], DeviceMesh) and isinstance(group[1], int) ): dmesh = group[0] dim = group[1] tag, rankset, _ = dmesh._dim_group_infos[dim] group_size = len(rankset) else: raise ValueError("Invalid tuple for group must be (DeviceMesh, int)") else: raise ValueError( "Invalid type for group, must be one of List, Processgroup, DeviceMesh or (DeviceMesh, int)." )
return (tag, rankset, group_size)
def _resolve_group_name(group: RANK_TYPES, tag: str = "") -> str: """ Given group in RANK_TYPES, return the group name. """ # `tag` will be deprecated. See details in: # https://github.com/pytorch/pytorch/issues/93173#issuecomment-1907095208 if isinstance(group, dist.ProcessGroup): return group.group_name elif isinstance(group, str): return group elif isinstance(group, DeviceMesh): assert ( group.ndim == 1 ), "Only 1D mesh is supported, pass in (DeviceMesh, int) together if mesh > 1D" return group._dim_group_infos[0][2] elif isinstance(group, tuple): if ( len(group) == 2 and isinstance(group[0], DeviceMesh) and isinstance(group[1], int) ): dmesh = group[0] dim = group[1] return dmesh._dim_group_infos[dim][2] else: raise ValueError("Invalid tuple for group must be (DeviceMesh, int)") elif isinstance(group, list): if not is_torchdynamo_compiling(): warnings.warn( "The combination of ranks + tag as process group " "identifier has been deprecated. Please switch to " "using ProcessGroup, DeviceMesh, or group name instead.", FutureWarning, stacklevel=3, ) return c10d._resolve_group_name_by_ranks_and_tag(cast(List[int], group), tag) else: raise ValueError(f"Unsupported group type: {type(group)}, {group}")
class _FromTorchTensor(torch.autograd.Function): """ _FromTorchTensor allows autograd to propagate from a normal Tensor to an AsyncCollectiveTensor. """
@staticmethod def forward( # type: ignore[override] ctx, # pyre-ignore[2]: Parameter must be annotated. input: torch.Tensor, ) -> torch.Tensor: return _maybe_wrap_tensor(input)
@staticmethod def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor: # type: ignore[override] return grad_output
def _are_we_tracing() -> bool: if is_torchdynamo_compiling(): return True # If fake mode is turned on, we are almost definitely compiling/tracing. if torch._C._get_dispatch_mode(torch._C._TorchDispatchModeKey.FAKE) is not None: return True return get_proxy_mode() is not None
def _maybe_wrap_tensor(self) -> torch.Tensor: if _are_we_tracing(): return wait_tensor(self) res = AsyncCollectiveTensor(self) return cast(torch.Tensor, res)
@contextlib.contextmanagerdef allow_inflight_collective_as_graph_input_ctx(value: bool = True): """ Context manager to temporarily set whether inflight collectives are allowed as torch.compile graph inputs. Common use case is when the collective is issued in eager (with `async_op=True`) but waited in compiled region: ``` def all_reduce_eager(x): y = x * x req = dist.all_reduce(y, op=dist.ReduceOp.SUM, async_op=True) return y
@torch.compile(fullgraph=True) def all_reduce_wait_compiled(y): torch.ops.c10d_functional.wait_tensor(y) return y * y
x = torch.ones(1280, 1280, device="cuda") + self.rank # the context manager ensures that `wait_tensor(y)` will wait on the correct work object with allow_inflight_collective_as_graph_input_ctx(): y = all_reduce_eager(x) z = all_reduce_wait_compiled(y) ``` With this context manager, when a collective is called, under the hood the work object of the collective will be registered in the work registry, and the wait_tensor() in compiled region called on the output tensor of the collective will wait on the correct work object. """ previous = torch._C._distributed_c10d._allow_inflight_collective_as_graph_input()
try: torch._C._distributed_c10d._set_allow_inflight_collective_as_graph_input(value) yield finally: torch._C._distributed_c10d._set_allow_inflight_collective_as_graph_input( previous )
def _all_gather_into_tensor_coalesced_meta(self, tag, rankset, group_size): def mk_out_tensor(shard): out_size = list(shard.size()) out_size[0] *= group_size out_tensor = shard.new_empty(out_size) return out_tensor
return [mk_out_tensor(t) for t in self]
# We now register meta kernels to deal with tracingdef _broadcast_meta(self, *args): return torch.empty_like(self)
def _all_reduce_meta(self, *args): return torch.empty_like(self)
def _wait_tensor_meta(self, *args): return torch.empty_like(self)
def _all_gather_into_tensor_meta(shard, tag, rankset, group_size): out_size = list(shard.size()) out_size[0] *= group_size return shard.new_empty(out_size)
def _reduce_scatter_tensor_meta(input, reduce_op, tag, rankset, group_size): out_size = list(input.size()) out_size[0] //= group_size return input.new_empty(out_size)
def _all_reduce_coalesced_meta(self, *args): return [torch.empty_like(t) for t in self]
def _all_reduce__meta(inp, *args): return inp
def _broadcast__meta(inp, *args): return inp
def _all_reduce_coalesced__meta(inputs, *args): return inputs
def _reduce_scatter_tensor_coalesced_meta(inputs, reduceOp, tag, rankset, group_size): def mk_out_tensor(input): out_size = list(input.size()) out_size[0] //= group_size out_tensor = input.new_empty(out_size) return out_tensor
return [mk_out_tensor(t) for t in inputs]
# NB: We often say all_to_all has dynamic output size, but this is not# technically true: instead, what typically happens is you manually# communicate the output_split_sizes ahead of time (which is dynamic),# but then you pass those sizes explicitly, and the all to all itself# isn't dynamic, it just follows the specified output splitsdef _all_to_all_single_meta( input, output_split_sizes, input_split_sizes, *args, **kwargs): if output_split_sizes is None: return input.new_empty(input.size()) else: for s in output_split_sizes: torch._check_is_size(s) out_size = list(input.size()) out_size[0] = sum(output_split_sizes) return input.new_empty(out_size)
def _all_gather_into_tensor_out_native_meta(input, group_size, group_name, *, out): shape = list(input.size()) shape[0] *= group_size return input.new_empty(shape)
def _all_gather_into_tensor_native_meta(input, group_size, group_name): shape = list(input.size()) shape[0] *= group_size return input.new_empty(shape)
def _all_gather_into_tensor_coalesced_native_meta(inputs, group_size, group_name): return [ _all_gather_into_tensor_native_meta(input, group_size, group_name) for input in inputs ]
def _reduce_scatter_tensor_native_meta(inp, reduce_op, group_size, group_name): shape = list(inp.size()) shape[0] //= group_size return inp.new_empty(shape)
def _reduce_scatter_tensor_coalesced_native_meta( inputs, reduce_op, group_size, group_name): return [ _reduce_scatter_tensor_native_meta(inp, reduce_op, group_size, group_name) for inp in inputs ]
if not torch._running_with_deploy(): # Library MUST be defined at module scope or it doesn't work # Creating a "DEF" Library always crashes torch::deploy so we create our # Library instances here guarded against running inside it lib_impl = torch.library.Library("_c10d_functional", "IMPL") lib_impl.impl("all_reduce", _all_reduce_meta, "Meta") lib_impl.impl("all_reduce_", _all_reduce__meta, "Meta") lib_impl.impl("all_reduce_coalesced", _all_reduce_coalesced_meta, "Meta") lib_impl.impl("all_reduce_coalesced_", _all_reduce_coalesced__meta, "Meta") lib_impl.impl("wait_tensor", _wait_tensor_meta, "Meta") lib_impl.impl( "all_gather_into_tensor_out", _all_gather_into_tensor_out_native_meta, "Meta" ) lib_impl.impl("all_gather_into_tensor", _all_gather_into_tensor_native_meta, "Meta") lib_impl.impl( "all_gather_into_tensor_coalesced", _all_gather_into_tensor_coalesced_native_meta, "Meta", ) lib_impl.impl("reduce_scatter_tensor", _reduce_scatter_tensor_native_meta, "Meta") lib_impl.impl( "reduce_scatter_tensor_coalesced", _reduce_scatter_tensor_coalesced_native_meta, "Meta", ) lib_impl.impl("all_to_all_single", _all_to_all_single_meta, "Meta") lib_impl.impl("broadcast", _broadcast_meta, "Meta") lib_impl.impl("broadcast_", _broadcast__meta, "Meta")
# mark these ops has side effect so that they won't be removed by DCE torch.fx.node.has_side_effect(torch.ops._c10d_functional.wait_tensor.default) torch.fx.node.has_side_effect(torch.ops._c10d_functional.wait_tensor)
# Register legacy ops for backward compatibility # TODO(yifu): remove these in functional collective beta release legacy_lib = torch.library.Library("c10d_functional", "DEF") legacy_lib_impl = torch.library.Library("c10d_functional", "IMPL") ops_defs = [ "broadcast(Tensor self, int src, str tag, int[] ranks, int group_size) -> Tensor", "all_reduce(Tensor self, str reduceOp, str tag, int[] ranks, int group_size) -> Tensor", "all_reduce_coalesced(Tensor[] self, str reduceOp, str tag, int[] ranks, int group_size) -> Tensor[]", "wait_tensor(Tensor self) -> Tensor", "all_gather_into_tensor(Tensor shard, str tag, int[] ranks, int group_size) -> Tensor", "all_gather_into_tensor_coalesced(Tensor[] input, str tag, int[] ranks, int group_size) -> Tensor[]", "reduce_scatter_tensor(Tensor input, str reduceOp, str tag, int[] ranks, int group_size) -> Tensor", "reduce_scatter_tensor_coalesced(Tensor[] inputs, str reduceOp, str tag, int[] ranks, int group_size) -> Tensor[]", "all_to_all_single(Tensor input, SymInt[]? output_split_sizes, SymInt[]? input_split_sizes, str tag, int[] ranks, int group_size) -> Tensor", # noqa: B950 ]
my_module = sys.modules[__name__] for op_def in ops_defs: op_name = op_def[0 : op_def.index("(")] backend_impl = getattr(fun_col_impl, f"_{op_name}") legacy_lib.define(op_def, tags=torch.Tag.pt2_compliant_tag) legacy_lib_impl.impl(op_name, backend_impl, "CompositeImplicitAutograd")
else: warnings.warn( "PyTorch Distributed functional collectives do not work with torch::deploy." )
"""Dynamo Remappings allow seamless translation from non-functional collectives of supportable form intofunctional collective calls followed by inplace copy ops, allowing them to be traced into a functional graph.
We implement this by writing a decomposition and teaching dynamo how to associate it to a corresponding op viathe mapping dict below.
These schemas intentionally match torch.distributed.distributed_c10d.* ops that we are trying to remap from"""
def all_gather_tensor_inplace( output_tensor: torch.Tensor, input_tensor: torch.Tensor, group, # TODO add a type, async_op: bool = False, tag: str = "", gather_dim: int = 0,): assert ( not async_op ), "Can't remap async version of inplace op to functional collective"
group = group or dist.group.WORLD assert group is not None
return output_tensor.copy_(all_gather_tensor(input_tensor, gather_dim, group, tag))
def reduce_scatter_tensor_inplace( output: torch.Tensor, input: torch.Tensor, op: str = "sum", # TODO type is actually c10d ReduceOp. is this ok? group=None, # TODO add a type async_op: bool = False, scatter_dim: int = 0, tag: str = "",): assert ( not async_op ), "Can't remap async version of inplace op to functional collective"
group = group or dist.group.WORLD assert group is not None
return output.copy_(reduce_scatter_tensor(input, op, scatter_dim, group, tag))
REDUCE_OP_TO_STR = { dist.ReduceOp.SUM: "sum", dist.ReduceOp.AVG: "avg", dist.ReduceOp.PRODUCT: "product", dist.ReduceOp.MIN: "min", dist.ReduceOp.MAX: "max", dist.ReduceOp.BAND: "band", dist.ReduceOp.BOR: "bor", dist.ReduceOp.BXOR: "bxor",}
def all_reduce_inplace( tensor: torch.Tensor, op: str = "sum", group=None, async_op: bool = False, tag: str = "",): assert ( not async_op ), "Can't remap async version of inplace op to functional collective"
group = group or dist.group.WORLD assert group is not None
return tensor.copy_(all_reduce(tensor, op, group, tag))
def all_to_all_inplace( output: torch.Tensor, input: torch.Tensor, output_split_sizes=None, input_split_sizes=None, group=None, async_op=False, tag: str = "",): assert ( not async_op ), "Can't remap async version of inplace op to functional collective"
group = group or dist.group.WORLD assert group is not None
return output.copy_( all_to_all_single( input, output_split_sizes, input_split_sizes, group, tag, ) )
def all_gather_inplace( tensor_list: List[torch.Tensor], tensor: torch.Tensor, group=None, async_op=False, tag: str = "",): assert ( not async_op ), "Can't remap async version of inplace op to functional collective" assert all( t.size(0) == tensor.size(0) for t in tensor_list ), "Remapping variable size all_gather is not yet supported"
group = group or dist.group.WORLD assert group is not None
output = all_gather_tensor(tensor, 0, group, tag)
# Use aten.slice instead of aten.split because the latter causes # tensor.shape(0) to be unnecessarily baked in when it's a SymInt. output_splits = [] offset = 0 for t in tensor_list: output_splits.append(output[offset : offset + t.size(0)]) offset += t.size(0) for dst, src in zip(tensor_list, output_splits): dst.copy_(src) return tensor_list
from torch.distributed.distributed_c10d import ( _all_gather_base as legacy_all_gather_base, _reduce_scatter_base as legacy_reduce_scatter_base, all_gather as legacy_all_gather, all_gather_into_tensor as legacy_allgather, all_reduce as legacy_allreduce, all_to_all_single as legacy_all_to_all_single, reduce_scatter_tensor as legacy_reducescatter,)
# This dict should contain sets of functions that dynamo is allowed to remap.# Functions in this set should accept the same args/kwargs 1:1 as their mapping.traceable_collective_remaps = { legacy_allgather: all_gather_tensor_inplace, legacy_reducescatter: reduce_scatter_tensor_inplace, legacy_allreduce: all_reduce_inplace, legacy_all_to_all_single: all_to_all_inplace, legacy_all_gather: all_gather_inplace, legacy_reduce_scatter_base: reduce_scatter_tensor_inplace, legacy_all_gather_base: all_gather_tensor_inplace,}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117# mypy: allow-untyped-defsfrom typing import List, Optional
import torchimport torch.distributed.distributed_c10d as c10d
"""This file contains the op impls for the legacy (c10d_functional) functional collectives.These impls simply call into the native (_c10d_functional) functional collectives."""
def _broadcast(input, src, tag, ranks, group_size): group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.broadcast( input, src, group_name, )
def _all_reduce(input, reduce_op, tag, ranks, group_size): group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.all_reduce( input, reduce_op, group_name, )
def _all_reduce_coalesced(inputs, reduce_op, tag, ranks, group_size): group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.all_reduce_coalesced( inputs, reduce_op, group_name, )
def _all_gather_into_tensor(input, tag, ranks, group_size): group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.all_gather_into_tensor( input, group_size, group_name, )
def _all_gather_into_tensor_coalesced(input, tag, ranks, group_size): group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.all_gather_into_tensor_coalesced( input, group_size, group_name, )
def _reduce_scatter_tensor( input: torch.Tensor, reduce_op: str, tag: str, ranks: List[int], group_size: int,): group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.reduce_scatter_tensor( input, reduce_op, group_size, group_name, )
def _reduce_scatter_tensor_coalesced( inputs: List[torch.Tensor], reduce_op: str, tag: str, ranks: List[int], group_size: int,): group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.reduce_scatter_tensor_coalesced( inputs, reduce_op, group_size, group_name, )
def _all_to_all_single( input: torch.Tensor, output_split_sizes: Optional[List[int]], input_split_sizes: Optional[List[int]], tag: str, ranks: List[int], group_size: int,): if output_split_sizes is None or input_split_sizes is None: assert output_split_sizes is None and input_split_sizes is None, ( "output_split_sizes and input_split_sizes must either be " "specified together or both set to None" ) output_split_sizes = [input.shape[0] // group_size] * group_size input_split_sizes = output_split_sizes
group_name = c10d._resolve_group_name_by_ranks_and_tag(ranks, tag) return torch.ops._c10d_functional.all_to_all_single( input, output_split_sizes, input_split_sizes, group_name, )
def _wait_tensor(tensor: torch.Tensor) -> torch.Tensor: return torch.ops._c10d_functional.wait_tensor(tensor)1from .api import _shard_tensor, load_with_process_group, shard_module, shard_parameter1234567891011121314151617181920212223242526272829303132from typing import Sequence
import torchfrom torch.distributed._shard.metadata import ShardMetadata
DEPRECATE_MSG = "Please use DTensor instead and we are deprecating ShardedTensor."
def narrow_tensor_by_index( tensor: torch.Tensor, offsets: Sequence[int], sizes: Sequence[int],) -> torch.Tensor: """ Narrow the tensor according to ``offsets`` and ``sizes``. """ narrowed_tensor = tensor for idx, (offset, size) in enumerate(zip(offsets, sizes)): if size < tensor.size(idx): # Reshape to get shard for this rank and we don't want autograd # recording here for the narrow op and 'local_shard' should be a # leaf variable in the autograd graph. narrowed_tensor = narrowed_tensor.narrow(idx, offset, size) return narrowed_tensor
def narrow_tensor(tensor: torch.Tensor, metadata: ShardMetadata) -> torch.Tensor: """ Narrow the tensor according to the metadata """ return narrow_tensor_by_index(tensor, metadata.shard_offsets, metadata.shard_sizes)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306# mypy: allow-untyped-defsfrom contextlib import contextmanagerfrom typing import Optional
import torchimport torch.distributed as distimport torch.nn as nnfrom torch.distributed import distributed_c10dfrom torch.distributed._shard.sharded_tensor import ShardedTensor
from .sharder import Sharderfrom .sharding_plan import ShardingPlanfrom .sharding_spec import ChunkShardingSpec, ShardingSpec
def _shard_tensor( tensor: torch.Tensor, sharding_spec: ShardingSpec, src_rank=0, process_group=None) -> ShardedTensor: """ Given a :class:`torch.Tensor`, it shards that tensor according to the provided ``sharding_spec``. ``src_rank`` denotes the source rank which would be used as the ground truth of the data which would be scattered as shards across the rest of the ranks.
Args: tensor (:class:`torch.Tensor`): Tensor needs to be sharded. sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor.
Keyword args: src_rank (int, optional): The source rank which is used as the ground truth of the data for the parameter that would be sharded and scattered across the rest of the ranks. Default: 0. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used.
Returns: A :class:`ShardedTensor` sharded from the given tensor.
.. warning:: Only :class:`torch.distributed._shard.sharding_spec.ChunkShardingSpec` is currently supported as the ``sharding_spec``. """ if not tensor.is_contiguous(): raise ValueError("input tensor is not a contiguous Tensor")
pg = ( process_group if process_group is not None else distributed_c10d._get_default_group() ) world_size = dist.get_world_size(pg) current_rank = dist.get_rank(pg)
# Validate src_rank and sharding_spec are same across all ranks. gathered_list = [None] * world_size dist.all_gather_object(gathered_list, (src_rank, sharding_spec), group=pg)
for idx, entry in enumerate(gathered_list): if src_rank != entry[0]: # type: ignore[index] raise ValueError( f"src_rank={src_rank} on rank: {current_rank} does not " # type: ignore[index] f"match with src_rank={entry[0]} on rank: {idx}" # type: ignore[index] ) if sharding_spec != entry[1]: # type: ignore[index] raise ValueError( f"sharding_spec={sharding_spec} on rank: {current_rank} does not " # type: ignore[index] f"match with sharding_spec={entry[1]} on rank: {idx}" # type: ignore[index] )
st = sharding_spec.shard(tensor, src_rank=src_rank, process_group=pg)
return st
def shard_parameter( module: torch.nn.Module, param_name: str, sharding_spec: ShardingSpec, src_rank=0, process_group=None,): """ Given a :class:`torch.nn.Module`, a ``param_name`` for a parameter in that module, it shards that parameter according to the provided ``sharding_spec``. ``src_rank`` denotes the source rank which would be used as the ground truth of the data which would be scattered as shards across the rest of the ranks.
This method replaces ``module.param_name`` with a :class:`torch.distributed._sharded_tensor.ShardedTensor`
Args: module (:class:`torch.nn.Module`): Module whose parameter needs to be sharded. param_name (str): Name of the parameter of ``module`` that needs to be sharded. sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor.
Keyword args: src_rank (int, optional): The source rank which is used as the ground truth of the data for the parameter that would be sharded and scattered across the rest of the ranks. Default: 0. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used.
.. warning:: Only :class:`torch.distributed._shard.sharding_spec.ChunkShardingSpec` is currently supported as the ``sharding_spec``. """ # Perform some validation first. if not hasattr(module, param_name): raise AttributeError(f"{module._get_name()} has no attribute `{param_name}`")
tensor = getattr(module, param_name) if not isinstance(tensor, torch.Tensor): raise ValueError( f"Expected {type(module).__name__}.{param_name} to be a Tensor, but found {type(tensor).__name__}" )
if not tensor.is_contiguous(): raise ValueError(f"param: {param_name} is not a contiguous Tensor")
st = _shard_tensor(tensor, sharding_spec, src_rank, process_group)
# Replace param with ShardedTensor. module.register_parameter(param_name, nn.Parameter(st))
# Tracks the current process group in the load context manager._CURRENT_PROCESS_GROUP: Optional[dist.ProcessGroup] = None
@contextmanagerdef load_with_process_group(process_group): """ Context manager to set the process group with which to load a ShardedTensor. """ global _CURRENT_PROCESS_GROUP if _CURRENT_PROCESS_GROUP is not None: raise RuntimeError( 'ProcessGroup already set by previous "load_with_process_group" ' "context manager" ) _CURRENT_PROCESS_GROUP = process_group try: yield process_group finally: _CURRENT_PROCESS_GROUP = None
def _get_current_process_group(): """ Retrieves the current process group set by ``load_with_process_group``. If not set, it just returns the default group. """ global _CURRENT_PROCESS_GROUP if _CURRENT_PROCESS_GROUP is None: return distributed_c10d._get_default_group() else: return _CURRENT_PROCESS_GROUP
def _reshard_output( module: torch.nn.Module, resharding_spec: ShardingSpec) -> torch.nn.Module: """ Hook a module with output resharding in the forward pass according to the given ``resharding_spec``.
Args: module (:class:`torch.nn.Module`): Module whose output needs to be resharded. resharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how the output of the module will be resharded.
Returns: A :class:`torch.nn.Module` object with reshard API hooked. """
def hook_func(_module, _input, output): if isinstance(output, ShardedTensor): return output.reshard(resharding_spec) return output
module.register_forward_hook(hook_func) return module
def _collect_local_shard(module: torch.nn.Module) -> torch.nn.Module: """ Hook a module with local shards collection in the forward pass.
This API is typically used to convert a sharded representation back to data parallel representation. In particular, it returns the local tensor for this Shard. If the size along the sharding dimension for the local tensor is 1, this dimension is removed from the final result. For example a [4, 16] ShardedTensor across 4 ranks is typically a local Tensor of size [16] across each rank and not [1, 16] across each rank.
Args: module (:class:`torch.nn.Module`): Module whose output is ShardedTensor and the local tensor value needs to be returned.
Returns: A :class:`torch.nn.Module` object with collection API hooked. """
def hook_func(_module, _input, output): if isinstance(output, ShardedTensor): local_tensor = output.local_tensor() # Squeeze the # of dimensions manually, only applicable to ChunkShardingSpec sharding_spec = output._sharding_spec if ( isinstance(sharding_spec, ChunkShardingSpec) and local_tensor.size(sharding_spec.dim) == 1 # type: ignore[attr-defined, arg-type] ): local_tensor = local_tensor.squeeze( output._sharding_spec.dim # type: ignore[attr-defined] ) return local_tensor
module.register_forward_hook(hook_func) return module
def shard_module(module: nn.Module, plan: ShardingPlan, src_rank=0, process_group=None): """ Shards a given module according to the provided sharding `plan`. This method first shards all the parameters according to the given sharding `plan`. Then if `output_plan` and `return_local_tensor` are specified in the sharding `plan`, it will tag the output of modules according `output_plan`, convert the module's output back to data parallel according to `return_local_tensor`.
Needs to be called on all ranks in an SPMD fashion.
Args: module (:class:`torch.nn.Module`): The module to apply sharding to plan (:class:`torch.distributed._shard.sharding_plan.ShardingPlan`): The ShardingPlan which specified param name to ShardingSpec to apply to each parameter.
Keyword args: src_rank (int, optional): The source rank which is used as the ground truth of the data for the module that would be sharded and scattered across the rest of the ranks. Default: 0. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. """ # record Sharder paths for sanity check on the plan to ensure items in the plan # does not conflict with the submodule tree that the Sharder is working with sharder_paths = [] for name, spec in plan.plan.items(): if isinstance(spec, Sharder): sharder_paths.append(name)
# shard the parameter according to the ShardingPlan for name, spec in plan.plan.items(): if isinstance(spec, ShardingSpec): # if found a sharding spec, try to shard the parameter module_path, _, param_name = name.rpartition(".")
for sharder_path in sharder_paths: if module_path.startswith(sharder_path): raise RuntimeError( f"ShardingPlan is in-valid, trying to shard a parameter: {name}," f" but there's already a Sharder entry for module {sharder_path}," f" parameter sharding should not conflict with the submodule tree" f" that a Sharder is working with!" )
mod = module.get_submodule(module_path) shard_parameter( mod, param_name, spec, src_rank=src_rank, process_group=process_group ) elif isinstance(spec, Sharder): parent_mod_path, _, _mod_name = name.rpartition(".") if name == "": raise KeyError("Module path must not be empty for custom sharder!") mod = module.get_submodule(name) parent_mod = module.get_submodule(parent_mod_path) sharded_mod = spec.shard(mod) # swap this submodule with the sharded module parent_mod.mod_name = sharded_mod else: raise TypeError( f"Only `ShardingSpec` and `Sharder` are supported to shard '{name}'" )
# reshard output if there's an entry in `reshard_output` for this module if plan.output_plan is not None: for module_path, output_spec in plan.output_plan.items(): if isinstance(output_spec, ShardingSpec): mod = module.get_submodule(module_path) _reshard_output(mod, output_spec) else: raise TypeError( f"Only `ShardingSpec` is supported as output_plan for '{module_path}'" ) # convert the output back to data parallel for the modules appears in # `return_local_tensor` of the plan, we will call `_collect_local_shard` # to collect the local tensor for output of modules if plan.return_local_tensor is not None: for module_path in plan.return_local_tensor: mod = module.get_submodule(module_path) _collect_local_shard(mod)12345678910111213141516171819# Keep old package for BC purposes, this file should be removed once# everything moves to the `torch.distributed.checkpoint` package.import sysimport warnings
import torchfrom torch.distributed.checkpoint import * # noqa: F403
with warnings.catch_warnings(): warnings.simplefilter("always") warnings.warn( "`torch.distributed._shard.checkpoint` will be deprecated, " "use `torch.distributed.checkpoint` instead", DeprecationWarning, stacklevel=2, )
sys.modules["torch.distributed._shard.checkpoint"] = torch.distributed.checkpoint1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465# mypy: allow-untyped-defsfrom typing import Optional
import torchfrom torch.utils import _pytree as pytree
def _basic_validation(op, args=(), kwargs=None): """ Common validation across all ops go in here. """ from torch.distributed._shard.sharded_tensor import ShardedTensor
if len(args) == 0 and (kwargs is None or len(kwargs) == 0): raise ValueError(f" No input for '{op.__name__}'!")
# Validate types has_distributed_tensor = False
def is_distributed_tensor(e): nonlocal has_distributed_tensor if isinstance(e, ShardedTensor): has_distributed_tensor = True
pytree.tree_map_(is_distributed_tensor, args) pytree.tree_map_(is_distributed_tensor, kwargs)
if not has_distributed_tensor: raise TypeError( f"torch function '{op.__name__}', with args: {args} and " f"kwargs: {kwargs} are called without any distributed tensor!" )
# Validate all distributed tensors use the same PG. cur_pg: Optional[torch.distributed.ProcessGroup] = None
def validate_pg(e): nonlocal cur_pg if isinstance(e, ShardedTensor): if cur_pg is not None and e._process_group is not cur_pg: raise RuntimeError( "All distributed tensors should use the " "same ProcessGroup if used together in an op." ) cur_pg = e._process_group
pytree.tree_map_(validate_pg, args) pytree.tree_map_(validate_pg, kwargs)
def _register_default_op(op, decorator): @decorator(op) def tensor_default_op(types, args=(), kwargs=None, pg=None): """ Handles ``__torch_function__`` dispatch for the default tensor ops that behave the same as ``torch.Tensor`` such as ``torch.Tensor.shape`` or ``torch.Tensor.dtype``. We simply lower to the real op call with DisableTorchFunctionSubclass context like ``torch.Tensor.__torch_function__`` to avoid recursions. """ if kwargs is None: kwargs = {}
with torch._C.DisableTorchFunctionSubclass(): return op(*args, **kwargs)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364# mypy: allow-untyped-defsfrom dataclasses import dataclassfrom functools import reducefrom typing import List, Optional, Union
from torch.distributed.remote_device import _remote_device
@dataclassclass ShardMetadata: """ Represents a shard of the overall Tensor including its offsets, lengths and device placement.
Args: shard_offsets(List[int]): Offsets in the original tensor indicating the start offsets for this shard. Should have the same rank as the original tensor. shard_sizes(List[int]): Integers indicating the size of each dimension for this shard. Should have the same rank as the original tensor. placement(:class:`torch.distributed._remote_device`): Specifies the placement of this shard. """
__slots__ = ["shard_offsets", "shard_sizes", "placement"]
shard_offsets: List[int] shard_sizes: List[int] placement: Optional[_remote_device]
def __init__( self, shard_offsets: List[int], shard_sizes: List[int], placement: Optional[Union[str, _remote_device]] = None, ): self.shard_offsets = shard_offsets self.shard_sizes = shard_sizes if isinstance(placement, str): self.placement = _remote_device(placement) else: self.placement = placement if len(self.shard_offsets) != len(self.shard_sizes): raise ValueError( f"shard_offsets and shard_sizes should have " f"the same number of elements, found {len(self.shard_offsets)} " f"and {self.shard_sizes} respectively" )
for i in range(len(self.shard_offsets)): if self.shard_offsets[i] < 0: raise ValueError("shard_offsets should be >=0") if self.shard_sizes[i] < 0: raise ValueError("shard_sizes should be >= 0")
def __hash__(self): def _hash_reduce(a, b): return (a << 8) + hash(b)
res = reduce(_hash_reduce, self.shard_offsets, 37) res = reduce(_hash_reduce, self.shard_sizes, res) res = _hash_reduce(res, self.placement) return res1234567891011121314151617181920212223242526272829303132333435363738394041# mypy: allow-untyped-defsimport functoolsfrom inspect import signature
from .common_op_utils import _basic_validation
"""Common utilities to register ops on ShardedTensorand PartialTensor."""
def _register_op(op, func, op_table): """ Performs basic validation and registers the provided op in the given op_table. """ if len(signature(func).parameters) != 4: raise TypeError( f"Custom sharded op function expects signature: " f"(types, args, kwargs, process_group), but received " f"signature: {signature(func)}" )
op_table[op] = func
def _decorator_func(wrapped_func, op, op_table): """ Decorator function to register the given ``op`` in the provided ``op_table`` """
@functools.wraps(wrapped_func) def wrapper(types, args, kwargs, process_group): _basic_validation(op, args, kwargs) return wrapped_func(types, args, kwargs, process_group)
_register_op(op, wrapper, op_table) return wrapper12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152from typing import Iterator, Tuple, Union
import torch.nn as nnfrom torch.distributed._shard.sharded_tensor import ShardedTensor
from .api import ShardedOptimizer
def named_params_with_sharded_tensor( module: nn.Module, prefix: str = "", recurse: bool = True,) -> Iterator[Tuple[str, Union[nn.Parameter, ShardedTensor]]]: r"""Returns an iterator over module parameters (together with the ShardedTensor parameters), yielding both the name of the parameter as well as the parameter itself. This is typically passed to a :class:torch.distributed._shard.sharded_optim.ShardedOptimizer
Args: prefix (str): prefix to prepend to all parameter names. recurse (bool): if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
Yields: (str, Union[Tensor, ShardedTensor]): Tuple containing the name and parameter (or ShardedTensor parameter)
Example::
>>> # xdoctest: +SKIP >>> model = torch.nn.Linear(*linear_size) >>> shard_parameter(model, "weight", spec) >>> for name, param in named_params_with_sharded_tensor(model): >>> if name in ['weight']: >>> print(param.size())
""" modules = module.named_modules(prefix=prefix) if recurse else [(prefix, module)]
memo = set() for mod_prefix, mod in modules: # find all sharded tensor params for name, val in vars(mod).items(): if isinstance(val, ShardedTensor) and val not in memo: memo.add(val) name = mod_prefix + ("." if mod_prefix else "") + name yield name, val
# find all nn.Parameters for name, val in module.named_parameters(): yield name, val123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101# mypy: allow-untyped-defsfrom typing import Any, Dict, List, Mapping, Union
import torch.optim as optimfrom torch import Tensorfrom torch.distributed._shard.sharded_tensor import ShardedTensor
class ShardedOptimizer(optim.Optimizer): def __init__( self, named_params: Mapping[str, Union[Tensor, ShardedTensor]], optimizer_class, *optimizer_args, **optimizer_kwargs, ): """ ShardedOptimizer collects all tensors and local shard tensors of ShardedTensor, then use these tensors as ``params`` for optimizers
Args: named_params (Dict[str, Union[Tensor, ShardedTensor]]) : a Dict of parameters, where key is the parameter key, value is either Tensor or ShardedTensor parameter. optimizer_class (torch.optim.Optimizer): the Optimizer to use locally, i.e. torch.optim.SGD, torch.optim.Adagrad, etc. *optimizer_args: the arguments to initialize the optimizer. **optimizer_kwargs: the key-word arguments to initialize the optimizer.
""" tensors: List[Tensor] = [] for value in named_params.values(): if isinstance(value, ShardedTensor): tensors.extend( local_shard.tensor for local_shard in value.local_shards() ) else: tensors.append(value)
self.named_params = named_params self._optim = optimizer_class(tensors, *optimizer_args, **optimizer_kwargs) self.param_groups = self._optim.param_groups self.state = self._optim.state
def zero_grad(self, set_to_none: bool = True): # type: ignore[override] r"""Resets the gradients of all optimized :class:`torch.Tensor` s.
Args: set_to_none (bool): instead of setting to zero, set the grads to None. This will in general have lower memory footprint, and can modestly improve performance. However, it changes certain behaviors. For example: 1. When the user tries to access a gradient and perform manual ops on it, a None attribute or a Tensor full of 0s will behave differently. 2. If the user requests ``zero_grad(set_to_none=True)`` followed by a backward pass, ``.grad``\ s are guaranteed to be None for params that did not receive a gradient. 3. ``torch.optim`` optimizers have a different behavior if the gradient is 0 or None (in one case it does the step with a gradient of 0 and in the other it skips the step altogether). """ self._optim.zero_grad(set_to_none)
def step(self, closure=None): r"""Performs a single optimization step (parameter update).
Args: closure (Callable): A closure that reevaluates the model and returns the loss. Optional for most optimizers.
.. note:: Unless otherwise specified, this function should not modify the ``.grad`` field of the parameters. """ self._optim.step(closure)
def state_dict(self) -> Dict[str, Any]: """ Returned state and param_groups will contain parameter keys instead of parameter indices like torch.optim.Optimizer. This allows for advanced functionality like optimizer re-sharding to be implemented. """ # TODO: implement state_dict raise NotImplementedError("ShardedOptimizer state_dict not implemented yet!")
def load_state_dict(self, state_dict: Mapping[str, Any]): r"""Loads the ShardedOptimizer state.
Args: state_dict (dict): ShardedOptimizer state. Should be an object returned from a call to :meth:`state_dict`. """ # TODO: implement load_state_dict raise NotImplementedError( "ShardedOptimizer load_state_dict not implemented yet!" )
def add_param_group(self, param_group: Any): r"""Add a new param group""" # TODO: implement add_param_group raise NotImplementedError( "ShardedOptimizer add_param_group not implemented yet!" )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490# mypy: allow-untyped-defsimport functoolsfrom typing import List, TYPE_CHECKING
import torchfrom torch.distributed._shard.op_registry_utils import _decorator_func
from .api import ( _CUSTOM_SHARDED_OPS, _SHARDED_OPS, Shard, ShardedTensor, ShardedTensorBase, ShardedTensorMetadata, TensorProperties,)from .metadata import ShardMetadata # noqa: F401
if TYPE_CHECKING: from torch.distributed._shard.sharding_spec import ShardingSpecelse: ShardingSpec = "ShardingSpec"
def empty( sharding_spec: ShardingSpec, *size, dtype=None, layout=torch.strided, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format, process_group=None, init_rrefs=False,) -> ShardedTensor: """ Returns a :class:`ShardedTensor` filled with uninitialized data. Needs to be called on all ranks in an SPMD fashion.
Args: sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor. size (int...): a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
Keyword args: dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor. Default: if ``None``, uses a global default (see :func:`torch.set_default_dtype`). layout (:class:`torch.layout`, optional): the desired layout of returned Tensor. Default: ``torch.strided``. requires_grad (bool, optional): If autograd should record operations on the returned tensor. Default: ``False``. pin_memory (bool, optional): If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: ``False``. memory_format (:class:`torch.memory_format`, optional): the desired memory format of returned Tensor. Default: ``torch.contiguous_format``. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. init_rrefs (bool, optional): Whether or not to initialize :class:`torch.distributed.rpc.RRef`s pointing to remote shards. Need to initialize the RPC Framework if specified as ``True``. Default: ``False``.
Returns: A :class:`ShardedTensor` object on each rank """ return ShardedTensor( sharding_spec, *size, dtype=dtype, layout=layout, requires_grad=requires_grad, pin_memory=pin_memory, memory_format=memory_format, process_group=process_group, init_rrefs=init_rrefs, )
def ones( sharding_spec: ShardingSpec, *size, dtype=None, layout=torch.strided, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format, process_group=None, init_rrefs=False,) -> ShardedTensor: """ Returns a :class:`ShardedTensor` with the scalar value 1. Needs to be called on all ranks in an SPMD fashion.
Args: sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor. size (int...): a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
Keyword args: dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor. Default: if ``None``, uses a global default (see :func:`torch.set_default_dtype`). layout (:class:`torch.layout`, optional): the desired layout of returned Tensor. Default: ``torch.strided``. requires_grad (bool, optional): If autograd should record operations on the returned tensor. Default: ``False``. pin_memory (bool, optional): If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: ``False``. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. init_rrefs (bool, optional): Whether or not to initialize :class:`torch.distributed.rpc.RRef`s pointing to remote shards. Need to initialize the RPC Framework if specified as ``True``. Default: ``False``.
Returns: A :class:`ShardedTensor` object on each rank """ return full( sharding_spec, size, fill_value=1, dtype=dtype, layout=layout, requires_grad=requires_grad, pin_memory=pin_memory, memory_format=memory_format, process_group=process_group, init_rrefs=init_rrefs, )
def zeros( sharding_spec: ShardingSpec, *size, dtype=None, layout=torch.strided, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format, process_group=None, init_rrefs=False,) -> ShardedTensor: """ Returns a :class:`ShardedTensor` filled with the scalar value 0. Needs to be called on all ranks in an SPMD fashion.
Args: sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor. size (int...): a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
Keyword args: dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor. Default: if ``None``, uses a global default (see :func:`torch.set_default_dtype`). layout (:class:`torch.layout`, optional): the desired layout of returned Tensor. Default: ``torch.strided``. requires_grad (bool, optional): If autograd should record operations on the returned tensor. Default: ``False``. pin_memory (bool, optional): If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: ``False``. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. init_rrefs (bool, optional): Whether or not to initialize :class:`torch.distributed.rpc.RRef`s pointing to remote shards. Need to initialize the RPC Framework if specified as ``True``. Default: ``False``.
Returns: A :class:`ShardedTensor` object on each rank """ return full( sharding_spec, size, fill_value=0, dtype=dtype, layout=layout, requires_grad=requires_grad, pin_memory=pin_memory, memory_format=memory_format, process_group=process_group, init_rrefs=init_rrefs, )
def full( sharding_spec: ShardingSpec, size, fill_value, *, dtype=None, layout=torch.strided, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format, process_group=None, init_rrefs=False,) -> ShardedTensor: """ Creates a :class:`ShardedTensor` filled with fill_value. The tensor's dtype is inferred from fill_value. If dtype is specified, it will override the inferred type from fill_value. Needs to be called on all ranks in an SPMD fashion. Args: sharding_spec (:class:`torch.distributed._sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor. size (int...): a list, tuple, or `torch.Size` of integers defining the shape of the output tensor. fill_value (Scalar) - the value to fill the output tensor with. Keyword args: dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor. Default: if ``None``, uses a global default (see :func:`torch.set_default_dtype`). layout (:class:`torch.layout`, optional): the desired layout of returned Tensor. Default: ``torch.strided``. requires_grad (bool, optional): If autograd should record operations on the returned tensor. Default: ``False``. pin_memory (bool, optional): If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: ``False``. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. init_rrefs (bool, optional): Whether or not to initialize :class:`torch.distributed.rpc.RRef`s pointing to remote shards. Need to initialize the RPC Framework if specified as ``True``. Default: ``False``. Returns: A :class:`ShardedTensor` object on each rank """ sharded_tensor = ShardedTensor( sharding_spec, *size, dtype=dtype, layout=layout, requires_grad=requires_grad, pin_memory=pin_memory, memory_format=memory_format, process_group=process_group, init_rrefs=init_rrefs, ) torch.nn.init.constant_(sharded_tensor, fill_value) # type: ignore[arg-type] return sharded_tensor
def rand( sharding_spec: ShardingSpec, *size, dtype=None, layout=torch.strided, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format, process_group=None, init_rrefs=False,) -> ShardedTensor: """ Creates a :class:`ShardedTensor` filled with random numbers from a uniform distribution on the interval :math:`[0, 1)`. The shape of the tensor is defined by the variable argument `size`. Needs to be called on all ranks in an SPMD fashion.
Args: sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor. size (int...): a list, tuple, or `torch.Size` of integers defining the shape of the output tensor.
Keyword args: dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor. Default: if ``None``, uses a global default (see :func:`torch.set_default_dtype`). layout (:class:`torch.layout`, optional): the desired layout of returned Tensor. Default: ``torch.strided``. requires_grad (bool, optional): If autograd should record operations on the returned tensor. Default: ``False``. pin_memory (bool, optional): If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: ``False``. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. init_rrefs (bool, optional): Whether or not to initialize :class:`torch.distributed.rpc.RRef`s pointing to remote shards. Need to initialize the RPC Framework if specified as ``True``. Default: ``False``.
Returns: A :class:`ShardedTensor` object on each rank """ sharded_tensor = ShardedTensor( sharding_spec, *size, dtype=dtype, layout=layout, requires_grad=requires_grad, pin_memory=pin_memory, memory_format=memory_format, process_group=process_group, init_rrefs=init_rrefs, ) torch.nn.init.uniform_(sharded_tensor, 0, 1) # type: ignore[arg-type] return sharded_tensor
def randn( sharding_spec: ShardingSpec, *size, dtype=None, layout=torch.strided, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format, process_group=None, init_rrefs=False,) -> ShardedTensor: """ Creates a :class:`ShardedTensor` filled with random numbers from a uniform distribution with mean `0` and variance `1` (also called standard normal distribution). The shape of the tensor is defined by the variable argument `size`. Needs to be called on all ranks in an SPMD fashion.
Args: sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The specification describing how to shard the Tensor. size (int...): a list, tuple, or `torch.Size` of integers defining the shape of the output tensor.
Keyword args: dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor. Default: if ``None``, uses a global default (see :func:`torch.set_default_dtype`). layout (:class:`torch.layout`, optional): the desired layout of returned Tensor. Default: ``torch.strided``. requires_grad (bool, optional): If autograd should record operations on the returned tensor. Default: ``False``. pin_memory (bool, optional): If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: ``False``. process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. init_rrefs (bool, optional): Whether or not to initialize :class:`torch.distributed.rpc.RRef`s pointing to remote shards. Need to initialize the RPC Framework if specified as ``True``. Default: ``False``.
Returns: A :class:`ShardedTensor` object on each rank """ sharded_tensor = ShardedTensor( sharding_spec, *size, dtype=dtype, layout=layout, requires_grad=requires_grad, pin_memory=pin_memory, memory_format=memory_format, process_group=process_group, init_rrefs=init_rrefs, ) torch.nn.init.normal_(sharded_tensor, 0, 1) # type: ignore[arg-type] return sharded_tensor
def init_from_local_shards( local_shards: List[Shard], *global_size, process_group=None, init_rrefs=False) -> ShardedTensor: """ Creates an :class:`ShardedTensor` from local shards and the global metadata. Needs to be called on all ranks in an SPMD fashion.
Args: local_shards (List[:class `torch.distributed._shard.sharded_tensor.Shard`]): A list of shards that represent the local shards on this rank. global_size (int...): a list, tuple, or `torch.Size` of integers defining the shape of the overall sharded tensor.
Keyword args: process_group (ProcessGroup, optional): The process group to work on. If None, the default process group will be used. init_rrefs (bool, optional): Whether or not to initialize :class:`torch.distributed.rpc.RRef`s pointing to remote shards. Need to initialize the RPC Framework if specified as ``True``. Default: ``False``.
Returns: A :class:`ShardedTensor` object handle on this rank
Examples: Suppose we want construct a sharded tensor on two ranks, global size = (10, 5), each shard have a (5, 5) local tensor, we can do it like below:
on rank 0: >>> # xdoctest: +SKIP("not distributed") >>> local_shard_metadata = ShardMetadata( >>> shard_offsets=[0, 0], >>> shard_lengths=[5, 5], >>> placement="rank:0/cuda:0" >>> ) >>> local_shards = [Shard(torch.randn(5, 5), local_shard_metadata)] >>> sharded_tensor = init_from_local_shards(local_shards, [10, 5])
on rank 1: >>> # xdoctest: +SKIP("not distributed") >>> local_shard_metadata = ShardMetadata( >>> shard_offsets=[5, 0], >>> shard_lengths=[5, 5], >>> placement="rank:1/cuda:1" >>> ) >>> local_shards = [Shard(torch.randn(5, 5), local_shard_metadata)] >>> sharded_tensor = init_from_local_shards(local_shards, [10, 5]) """ return ShardedTensor._init_from_local_shards( local_shards, *global_size, process_group=process_group, init_rrefs=init_rrefs )
def state_dict_hook(module, destination, prefix, local_metadata): """ Hook to add ShardedTensor to Module's ``state_dict``. Needs to be registered to the Module using :meth:`torch.nn.Module._register_state_dict_hook`. """ for submodule_name, submodule in module.named_modules(): for attr_name, attr in submodule.__dict__.items(): if isinstance(attr, ShardedTensor): mod_prefix = prefix + submodule_name key = mod_prefix + ("." if mod_prefix else "") + attr_name destination[key] = attr
def pre_load_state_dict_hook( module, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs,): """ Pre-load state dict hook to add ShardedTensor to the module. """ for submodule_name, submodule in module.named_modules(): for attr_name in submodule.__dict__.keys(): mod_prefix = prefix + submodule_name key = mod_prefix + ("." if mod_prefix else "") + attr_name if key in state_dict: if isinstance(state_dict[key], ShardedTensor): setattr(submodule, attr_name, state_dict[key])
def custom_sharded_op_impl(func): """ Provides a way for users to write their own custom sharded operator. This can be used to override existing ShardedTensor operators or write a new one not supported by ShardedTensor. If the operator in question is covered by ``__torch_function__`` dispatch and has a ShardedTensor as any of its parameters, the function provided will be invoked for that operator.
Example:: >>> # xdoctest: +SKIP >>> @custom_sharded_op_impl(torch.nn.functional.linear) >>> def my_custom_sharded_linear(types, args, kwargs, process_group): >>> ... >>> # xdoctest: +SKIP("Undefined variables") >>> input = torch.rand(10, 32) >>> weight = sharded_tensor.rand(32, 16) >>> bias = torch.rand(16) >>> # This will call 'my_custom_sharded_linear' >>> torch.nn.functional.linear(input, weight, bias)
The types, args and kwargs parameters are the same parameters that are passed to ``__torch_function__`` dispatch API (https://pytorch.org/docs/stable/notes/extending.html#extending-torch). There is an additional ``process_group`` parameter which is the process_group used for the ShardedTensor and can be used by implementations for communications within a sharded implementation.
Args: func(Callable): Torch function for which we want to provide a sharded implementation (ex: torch.nn.functional.linear) """ return functools.partial(_decorator_func, op=func, op_table=_CUSTOM_SHARDED_OPS)
def _sharded_op_impl(func): """ Decorator to register a default sharded op. """ return functools.partial(_decorator_func, op=func, op_table=_SHARDED_OPS)
# Import all builtin sharded opsfrom ._ops import * # noqa: F403lib/python3.12/site-packages/torch/_refs/__pycache__/_conversions.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_refs/__pycache__/fft.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_refs/linalg/__init__.pyAdded+309
lib/python3.12/site-packages/torch/_refs/linalg/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_refs/nn/__init__.pyAdded+4
lib/python3.12/site-packages/torch/_refs/nn/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_refs/nn/functional/__init__.pyAdded+1280
lib/python3.12/site-packages/torch/_refs/nn/functional/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_refs/special/__init__.pyAdded+236
lib/python3.12/site-packages/torch/_refs/special/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_strobelight/__init__.pyAdded
lib/python3.12/site-packages/torch/_strobelight/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_strobelight/__pycache__/cli_function_profiler.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_strobelight/__pycache__/compile_time_profiler.cpython-312.pycAdded
lib/python3.12/site-packages/torch/_strobelight/cli_function_profiler.pyAdded+311
lib/python3.12/site-packages/torch/_strobelight/compile_time_profiler.pyAdded+195
lib/python3.12/site-packages/torch/compiler/__init__.pyAdded+404
lib/python3.12/site-packages/torch/compiler/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/compiler/__pycache__/config.cpython-312.pycAdded
lib/python3.12/site-packages/torch/compiler/config.pyAdded+67
lib/python3.12/site-packages/torch/distributed/__init__.pyAdded+157
lib/python3.12/site-packages/torch/distributed/_checkpointable.pyAdded+38
lib/python3.12/site-packages/torch/distributed/_composable_state.pyAdded+44
lib/python3.12/site-packages/torch/distributed/_functional_collectives.pyAdded+1191
lib/python3.12/site-packages/torch/distributed/_functional_collectives_impl.pyAdded+117
lib/python3.12/site-packages/torch/distributed/_shard/__init__.pyAdded+1
lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/_utils.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/api.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/common_op_utils.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/metadata.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/op_registry_utils.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/__pycache__/sharder.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/_utils.pyAdded+32
lib/python3.12/site-packages/torch/distributed/_shard/api.pyAdded+306
lib/python3.12/site-packages/torch/distributed/_shard/checkpoint/__init__.pyAdded+19
lib/python3.12/site-packages/torch/distributed/_shard/checkpoint/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/common_op_utils.pyAdded+65
lib/python3.12/site-packages/torch/distributed/_shard/metadata.pyAdded+64
lib/python3.12/site-packages/torch/distributed/_shard/op_registry_utils.pyAdded+41
lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/__init__.pyAdded+52
lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/__pycache__/api.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/sharded_optim/api.pyAdded+101
lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__init__.pyAdded+490
lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/api.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/logger.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/logging_handlers.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_shard/sharded_tensor/__pycache__/metadata.cpython-312.pycAdded