# Dynamic programming: been modified by an inplace operation

``````dp=torch.zeros(batch,height+1,width+1,self.hiden_size).cuda(device)
for b in range(batch):
for i in range(1,height+1):
for j in range(1,width+1):
dp[b,i,j]=self.compute(interaction[b,i-1,j-1],dp[b,i,j-1],dp[b,i-1,j],dp[b,i-1,j-1])
return dp[:,height,width]
``````

I can’t exactly understand what the definition of In-Place operation is, is it based on a variable? or on a element of a variable?

In my code above, there is a dp procedure in my computation graph, and during dp, I didn’t modify the pre-element value of the dp Tensor after the value had been computed, just modify current element value of the dp Tensor via some compution based on three pre-elements in dp Tensor, but I got a RuntimeError “one of the variables needed for gradient computation has been modified by an inplace operation”.And It can work when I used `.clone()` as follow.

``````dp=torch.zeros(batch,height+1,width+1,self.hiden_size).cuda(device)
for b in range(batch):
for i in range(1,height+1):
for j in range(1,width+1):
dp[b,i,j]=self.compute(interaction[b,i-1,j-1],dp[b,i,j-1].clone(),dp[b,i-1,j].clone(),dp[b,i-1,j-1].clone())
return dp[:,height,width]
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.7406468,"math_prob":0.9754521,"size":1122,"snap":"2022-05-2022-21","text_gpt3_token_len":311,"char_repetition_ratio":0.1350626,"word_repetition_ratio":0.15151516,"special_character_ratio":0.28342247,"punctuation_ratio":0.22653721,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9886296,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-21T08:32:53Z\",\"WARC-Record-ID\":\"<urn:uuid:7339444c-305d-4c2a-9da1-4dfe3fd52ebf>\",\"Content-Length\":\"14166\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a983ab13-11b9-4481-81b8-4e5986032801>\",\"WARC-Concurrent-To\":\"<urn:uuid:8504034c-5e9e-44db-82b3-7811003ad5c6>\",\"WARC-IP-Address\":\"159.203.145.104\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/dynamic-programming-been-modified-by-an-inplace-operation/38476\",\"WARC-Payload-Digest\":\"sha1:OQQMHQHMAZUB6VBHMTKKHXTHCF46WGQQ\",\"WARC-Block-Digest\":\"sha1:LCLAS6I26WE5HDLXSJIR6I75PDRCOLBB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662539049.32_warc_CC-MAIN-20220521080921-20220521110921-00429.warc.gz\"}"}