REAL PYTORCH · CPU TRAINING AND AUTOGRAD
Project conflicting task gradients without mutating their owners
Return a detached [tasks, parameters] matrix of PCGrad gradients. For each task, visit the other ORIGINAL task gradients in a seed-local shuffled order. If the current projected gradient has a negative dot product with an original peer, remove that component. Skip zero-norm peers. Do not mutate the input or global RNG. The caller chooses how to combine the projected rows; this Lab does not assert that a mean improves every task.
Run this Lab in Pythilo
Open Labs, choose its topic and tap Download entire topic, or Download entire category for all its Labs, including the required Python packages, then load the starter or worked answer and tap Trace. Progress, pause, resume and removal are in Downloads. Independent checks execute real CPU training and gradients.
No GPU, external model weights or API key is required. After downloading the topic and packages, this Lab runs offline.
PyTorch is unavailable in this browser runtime. The code and lessons are readable here; execution and checks run in the app.
Pythilo on the App Store →Read lesson: Project conflicting task gradients without mutating their owners →
Your task
Return a detached [tasks, parameters] matrix of PCGrad gradients. For each task, visit the other ORIGINAL task gradients in a seed-local shuffled order. If the current projected gradient has a negative dot product with an original peer, remove that component. Skip zero-norm peers. Do not mutate the input or global RNG. The caller chooses how to combine the projected rows; this Lab does not assert that a mean improves every task.
Starter
import random
import torch
def project_conflicts(gradients, seed=0):
# TODO: implement project_conflicts under the declared contract.
return NoneInspect the worked answer
import random
import torch
def project_conflicts(gradients, seed=0):
if gradients.ndim!=2 or gradients.shape[0]<2 or gradients.shape[1]==0 or not gradients.is_floating_point() or not torch.isfinite(gradients).all():
raise ValueError('Expected finite floating [tasks>=2, parameters>0] gradients.')
original=gradients.detach().clone()
projected=original.clone()
rng=random.Random(seed)
for task in range(len(original)):
order=[j for j in range(len(original)) if j!=task]
rng.shuffle(order)
for peer in order:
norm=original[peer].square().sum()
conflict=torch.dot(projected[task],original[peer])
if norm>0 and conflict<0:
projected[task]=projected[task]-conflict/norm*original[peer]
return projected