Well, the tf_data grads are very logical since  loss = sum (tf_data) .No matter what your code looks like before, and since loss = tf_data[0,0,0,0] + tf_data[0,0,0,1] + ... + tf_data[1,2,1,1] the grad will be just a differentiation with respect to each element which means 1.
Now for data grads, this line tf_data[i] = t_data[i].expand_as(tf_data[i]) is basically repeating (by stacking) t_data[i] ( shape = ( 2, 2) in this case)  a number of times until it reaches tf_data[i] dimensions ( (3, 2, 2) in this case). means you are repeating 3 times.
So loss = sum(tf_data) = 3 * sum (t_data) = 3 * sum (data) means whatever your code looks like before ( without changing dimensions) you will get grads = 3.
3 * sum (t_data) = 3 * sum (data) because summing two tensors with same elements and different shapes, will result in the same result.