Skip to content

Commit dfe1d73

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 55ab003 commit dfe1d73

15 files changed

Lines changed: 295 additions & 131 deletions

File tree

thunder/clang/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ def tensor_from_sequence(
353353
def diagonal(a: TensorLike, offset: int = 0, dim1: int = 0, dim2: int = 1) -> TensorLike:
354354
utils.check(
355355
a.ndim >= 2,
356-
lambda: f"diagonal() expected a tensor with at least two dimensions, but got a tensor with {a.ndims} dimensions",
356+
lambda: (
357+
f"diagonal() expected a tensor with at least two dimensions, but got a tensor with {a.ndims} dimensions"
358+
),
357359
)
358360

359361
diag_length = max(0, min(a.shape[dim1] + min(offset, 0), a.shape[dim2] - max(offset, 0)))

thunder/core/dtypes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,19 @@ def resolve_dtypes(args: Iterable) -> set[dtype]:
334334
for a in arg:
335335
baseutils.check(
336336
isinstance(a, dtype),
337-
lambda: f"Iterables passed to resolve_dtypes must only contain dtypes, but found an Iterable with {a}",
337+
lambda: (
338+
f"Iterables passed to resolve_dtypes must only contain dtypes, but found an Iterable with {a}"
339+
),
338340
exception_type=NotImplementedError,
339341
)
340342
if not a.is_weak:
341343
dtypes.add(a)
342344

343345
baseutils.check(
344346
arg in (dtype, exact, signedinteger, unsignedinteger, bool_, inexact, floating, complexfloating),
345-
lambda: f"Excepted arguments to resolve_dtypes to be dtypes, sets of dtypes, or a dtype (sub)class, but got {arg}",
347+
lambda: (
348+
f"Excepted arguments to resolve_dtypes to be dtypes, sets of dtypes, or a dtype (sub)class, but got {arg}"
349+
),
346350
exception_type=AssertionError,
347351
)
348352

thunder/core/prims.py

Lines changed: 100 additions & 42 deletions
Large diffs are not rendered by default.

thunder/core/transforms.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,10 @@ def _index_copy_grad(a: TensorProxy, /, index: TensorProxy, src: TensorProxy, di
917917
def _scatter_add_prim_grad(a: TensorProxy, /, index: TensorProxy, value: TensorProxy, dim: int) -> TensorProxy:
918918
utils.check(
919919
not value._requires_grad or value.shape == index.shape,
920-
lambda: "The gradient for the value Tensor is implemented only when value.shape == index.shape. "
921-
"value shape is {value.shape} while index shape is {index.shape}",
920+
lambda: (
921+
"The gradient for the value Tensor is implemented only when value.shape == index.shape. "
922+
"value shape is {value.shape} while index shape is {index.shape}"
923+
),
922924
)
923925

924926
fwd = prims.scatter_add(a, index, value, dim)
@@ -2985,8 +2987,10 @@ def put_grad(v: Variable, val: Any) -> None:
29852987
if len(symbol.args) != (orig_res_len := len(result)):
29862988
check(
29872989
orig_res_len <= len(symbol.args),
2988-
lambda: f"Backward for {symbol.sym.id} returned {orig_res_len} values, "
2989-
+ f"but expected at most {len(symbol.args)}",
2990+
lambda: (
2991+
f"Backward for {symbol.sym.id} returned {orig_res_len} values, "
2992+
+ f"but expected at most {len(symbol.args)}"
2993+
),
29902994
)
29912995
# Assuming that the non-differentiable arguments were dropped from
29922996
# the backward function, we are going to append None to the result
@@ -2998,8 +3002,10 @@ def put_grad(v: Variable, val: Any) -> None:
29983002
n_differentiable_args = sum(bool(_is_differentiable(arg)) for arg in symbol.args)
29993003
check(
30003004
n_differentiable_args <= orig_res_len,
3001-
lambda: f"Backward for {symbol.sym.id} returned {orig_res_len} value(s), "
3002-
+ f"but expected {n_differentiable_args}",
3005+
lambda: (
3006+
f"Backward for {symbol.sym.id} returned {orig_res_len} value(s), "
3007+
+ f"but expected {n_differentiable_args}"
3008+
),
30033009
)
30043010

30053011
result = tuple(next(iter_result) if _is_differentiable(arg) else None for arg in symbol.args)
@@ -3033,7 +3039,9 @@ def vjp_call(primals, cotangents, trace: Trace, **kwargs):
30333039
result, env = augmented_forward_pass(*primals, trace=trace, **kwargs)
30343040
check(
30353041
len(result) == len(cotangents) if isinstance(result, Sequence) else True,
3036-
lambda: f"Expected cotangents to be a sequence of length {len(result)}, got a sequence of length {len(cotangents)}",
3042+
lambda: (
3043+
f"Expected cotangents to be a sequence of length {len(result)}, got a sequence of length {len(cotangents)}"
3044+
),
30373045
)
30383046
return result, backward_pass(env, trace, cotangents)
30393047

thunder/core/vjp_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ def find_backward_output(forward_input):
141141
# same as the number of primal outputs of the augmented forward trace
142142
utils.check(
143143
len(utils.sequencify(bsym.output)) == len(utils.sequencify(augmented_forward_trace.output[0])),
144-
lambda: f"While generating forward and backward functions for {bsym.sym.name}, encountered an error.\n"
145-
"The number of outputs of the original forward function must be the same as the number of primal outputs of the augmented forward trace.\n"
146-
f"Number of outputs of the original forward function: {len(utils.sequencify(bsym.output))}\n"
147-
f"Number of primal outputs of the augmented forward trace: {len(utils.sequencify(augmented_forward_trace.output[0]))}\n"
148-
"Please check the forward function and the augmented forward trace to ensure that they have the same number of outputs.",
144+
lambda: (
145+
f"While generating forward and backward functions for {bsym.sym.name}, encountered an error.\n"
146+
"The number of outputs of the original forward function must be the same as the number of primal outputs of the augmented forward trace.\n"
147+
f"Number of outputs of the original forward function: {len(utils.sequencify(bsym.output))}\n"
148+
f"Number of primal outputs of the augmented forward trace: {len(utils.sequencify(augmented_forward_trace.output[0]))}\n"
149+
"Please check the forward function and the augmented forward trace to ensure that they have the same number of outputs."
150+
),
149151
)
150152

151153
# Check if any of the bound symbols in the backward trace are also in the

thunder/distributed/transforms/ddp.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def remove_grad_sync(backward_trace_with_grad_sync: TraceCtx) -> TraceCtx:
6767
bsym_of_preaveraging: BoundSymbol = producers[bsym_of_allreduce.flat_proxy_args[0]]
6868
utils.check(
6969
bsym_of_preaveraging.sym.id in {PrimIDs.DIV, "torch.true_divide"},
70-
lambda: f"expected to be either of {(PrimIDs.DIV, 'torch.true_divide')} but {bsym_of_preaveraging.sym.id=} for {synced_grad=}",
70+
lambda: (
71+
f"expected to be either of {(PrimIDs.DIV, 'torch.true_divide')} but {bsym_of_preaveraging.sym.id=} for {synced_grad=}"
72+
),
7173
)
7274
bsym_to_remove.extend([bsym_of_allreduce, bsym_of_wait, bsym_of_preaveraging])
7375
synced_to_unsynced[variableify(synced_grad)] = bsym_of_preaveraging.flat_proxy_args[0]
@@ -172,7 +174,9 @@ def __call__(self, bsym: BoundSymbol) -> None:
172174
if grads_of_bsym:
173175
utils.check(
174176
bsym.sym.id in {PrimIDs.DIV, "torch.true_divide"},
175-
lambda: f"This bsym's sym.id is expected to be {PrimIDs.DIV=} or 'torch.true_divide' but {bsym.sym.id=}",
177+
lambda: (
178+
f"This bsym's sym.id is expected to be {PrimIDs.DIV=} or 'torch.true_divide' but {bsym.sym.id=}"
179+
),
176180
)
177181
utils.check(len(grads_of_bsym) == 1, lambda: f"{len(grads_of_bsym)=} is expected to be 1")
178182
self.gradient_buckets.tell(grads_of_bsym[0], self.process_group)

thunder/distributed/transforms/fsdp.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ def stash_unsharded_grads_and_return_none_as_grads(
149149
prod_of_unsharded_grad = producers[preaverage_bsym.flat_proxy_args[0]]
150150
utils.check(
151151
preaverage_bsym.sym.id in {PrimIDs.DIV, "torch.true_divide"},
152-
lambda: f"expected to be either of {(PrimIDs.DIV, 'torch.true_divide')} but {preaverage_bsym.sym.id=} for {output_proxy=}",
152+
lambda: (
153+
f"expected to be either of {(PrimIDs.DIV, 'torch.true_divide')} but {preaverage_bsym.sym.id=} for {output_proxy=}"
154+
),
153155
)
154156
wait_bsym = consumers[reduce_scatter_bsym.flat_proxy_outs[0]][0]
155157

@@ -221,7 +223,9 @@ def __post_init__(self) -> None:
221223
case _:
222224
utils.check(
223225
False,
224-
lambda: f"Invalid {self.comm_to_bucket}, {(dist_prims.PrimIDs.ALL_GATHER, dist_prims.PrimIDs.REDUCE_SCATTER)} are supported",
226+
lambda: (
227+
f"Invalid {self.comm_to_bucket}, {(dist_prims.PrimIDs.ALL_GATHER, dist_prims.PrimIDs.REDUCE_SCATTER)} are supported"
228+
),
225229
)
226230

227231
@property
@@ -265,7 +269,9 @@ def maybe_swap_proxies_of_bsym_and_update_swap_map(bsym: BoundSymbol) -> bool:
265269
param = bsym.flat_proxy_args[0]
266270
utils.check(
267271
param in self.params,
268-
lambda: f"{variableify(param)} not found in param set: {(variableify(p) for p in self.original_params)}",
272+
lambda: (
273+
f"{variableify(param)} not found in param set: {(variableify(p) for p in self.original_params)}"
274+
),
269275
)
270276
if param not in self.param_to_bucket:
271277
# This path is highly likely to be backward reduce-scatter bucketing:
@@ -458,7 +464,9 @@ def __init__(
458464
hasattr(compile_data.fn, "process_group_for_ddp")
459465
and hasattr(compile_data.fn, "bucketing_strategy")
460466
and hasattr(compile_data.fn, "sharding_strategy"),
461-
lambda: f"Given module does not seem to have all the attributes of `process_group_for_ddp`, `bucketing_strategy`, and `sharding_strategy`, {hasattr(compile_data.fn, 'bucketing_strategy')=}, {hasattr(compile_data.fn, 'sharding_strategy')=}",
467+
lambda: (
468+
f"Given module does not seem to have all the attributes of `process_group_for_ddp`, `bucketing_strategy`, and `sharding_strategy`, {hasattr(compile_data.fn, 'bucketing_strategy')=}, {hasattr(compile_data.fn, 'sharding_strategy')=}"
469+
),
462470
)
463471
self.bucketing_strategy: FSDPBucketingStrategy = compile_data.fn.bucketing_strategy
464472
self.apply_bucketing = self.bucketing_strategy != FSDPBucketingStrategy.NONE
@@ -539,8 +547,10 @@ def apply_bucketing_to_forward_trace(self, fwd_trace: TraceCtx) -> TraceCtx:
539547

540548
collective_comm_bsyms: tuple[BoundSymbol, ...] = tuple(
541549
filter(
542-
lambda bsym: bsym.sym.id == dist_prims.PrimIDs.ALL_GATHER
543-
and any(arg in arg_to_index_in_flat_args for arg in bsym.flat_proxy_args),
550+
lambda bsym: (
551+
bsym.sym.id == dist_prims.PrimIDs.ALL_GATHER
552+
and any(arg in arg_to_index_in_flat_args for arg in bsym.flat_proxy_args)
553+
),
544554
fsdp_fwd_trace.bound_symbols,
545555
)
546556
)

thunder/distributed/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ def limit_in_flight_allgathers(
224224
pack_consumer = consumers.get(bsym.flat_proxy_outs[0], None)
225225
check(
226226
pack_consumer is not None and len(pack_consumer) in (1, 2),
227-
lambda: f"Pack's operand {bsym.flat_proxy_outs[0]} expected to be consumed by all-gather and del: {pack_consumer}",
227+
lambda: (
228+
f"Pack's operand {bsym.flat_proxy_outs[0]} expected to be consumed by all-gather and del: {pack_consumer}"
229+
),
228230
)
229231
# skip the pack operator corresponds to allgather
230232
if pack_consumer[0].sym.id != all_gather_prim_impl.id:
@@ -247,7 +249,9 @@ def limit_in_flight_allgathers(
247249
wait_consumer is not None
248250
and len(wait_consumer) in (1, 2)
249251
and wait_consumer[0].sym.id == unpack_for_fsdp_prim_impl.id,
250-
lambda: f"wait of {bsym.flat_proxy_outs[0]} expected to be consumed unpack and del: {wait_consumer}",
252+
lambda: (
253+
f"wait of {bsym.flat_proxy_outs[0]} expected to be consumed unpack and del: {wait_consumer}"
254+
),
251255
)
252256
unpack_bsyms.append(wait_consumer[0])
253257
bound_symbols.append(bsym)

thunder/dynamo/benchmark_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ def try_and_log_benchmark(compile_fn, filename):
528528
for m1, m2, name in zip(measure1, measure2, ("forward", "backward")):
529529
check(
530530
(m1 is None) == (m2 is None),
531-
lambda: f"{name} measurement for the two compilation methods should either both be None or both not None, but got {m1} and {m2}",
531+
lambda: (
532+
f"{name} measurement for the two compilation methods should either both be None or both not None, but got {m1} and {m2}"
533+
),
532534
)
533535
if m1 is None:
534536
continue

thunder/dynamo/compiler_graph_benchmark.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def test_func(benchmark):
7878
check(isinstance(executors, dict) and executors, lambda: "'executors' must be a non-empty dictionary.")
7979
check(
8080
not any("-" in k for k in executors.keys()),
81-
lambda: "Executor names cannot contain '-' as it conflicts with the 'benchmark-group-by' function. Please rename it using a different character.",
81+
lambda: (
82+
"Executor names cannot contain '-' as it conflicts with the 'benchmark-group-by' function. Please rename it using a different character."
83+
),
8284
)
8385
self.executors = executors
8486
self._get_debug_options(**debug_options)

0 commit comments

Comments
 (0)