I’ve defined a new attribute named man in the grad_output with the backward function. I want to get this attribute when I call this function the second time,but I can`t get it. It is strange that when I use PDB I can get the man value.
code:
import torch
import torch.nn as nn
import pdb
import os
import random
import numpy as np
class LinearFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight):
print('input id: {}'.format(id(input)))
ctx.save_for_backward(input, weight)
output = input.mm(weight.t())
if not hasattr(input, 'man'):
output.man = 1
else:
output.man = input.man + 1
print('output.man :{}'.format(output.man))
return output
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_tensors
grad_input = grad_weight = grad_bias = None
if ctx.needs_input_grad[0]:
grad_input = grad_output.mm(weight)
if ctx.needs_input_grad[1]:
grad_weight = grad_output.t().mm(input)
# pdb.set_trace()
print(id(grad_output), id(grad_input))
if not hasattr(grad_output, 'man'):
setattr(grad_input, 'man', 1)
else:
grad_input.man = grad_output.man + 1
print('linear {} grad_input.man:{}'.format(id(grad_output), grad_input.man))
return grad_input, grad_weight, grad_bias
class Linear(nn.Module):
def __init__(self, input_features, output_features):
super(Linear, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.weight = nn.Parameter(torch.Tensor(output_features, input_features))
self.weight.data.uniform_(-0.1, 0.1)
def forward(self, input):
return LinearFunction.apply(input, self.weight)
def seed_torch(seed=1029):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
seed_torch()
x = torch.rand((2, 4), requires_grad=True)
y = Linear(input_features=4, output_features=3)(x)
y = Linear(input_features=3, output_features=3)(y)
z = torch.sum(y)
y.retain_grad()
z.backward()
output without pdb:
input id: 140034962031456
output.man :1
input id: 140034962034408
output.man :2
140034962080032 140034962080248
linear 140034962080032 grad_input.man:1
140034962080032 140034962080176
linear 140034962080032 grad_input.man:1
output with pdb:
input id: 139694370501472
output.man :1
input id: 139694370504424
output.man :2
> /data1/UAL/Codes/UAL2.6/tools/eval/test.py(32)backward()
-> print(id(grad_output), id(grad_input))
(Pdb) c
c
139694370545952 139694370546168
linear 139694370545952 grad_input.man:1
> /data1/UAL/Codes/UAL2.6/tools/eval/test.py(32)backward()
-> print(id(grad_output), id(grad_input))
(Pdb) c
c
139694370546168 139694370547608
linear 139694370546168 grad_input.man:2
What should I do to get the value of the attribute named man