Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]: context.tensors.load returns an object that is modifiable in-place #6820

Open
1 task done
dunkeroni opened this issue Sep 7, 2024 · 1 comment
Open
1 task done
Labels
bug Something isn't working

Comments

@dunkeroni
Copy link
Contributor

Is there an existing issue for this problem?

  • I have searched the existing issues

Operating system

Linux

GPU vendor

Nvidia (CUDA)

GPU model

No response

GPU VRAM

No response

Version number

4.2.9

Browser

Chrome

Python dependencies

No response

What happened

Modifying a loaded tensor and then saving the result in the context updates the original tensor as well.
Found while experimenting with noise. Noise Adjuster invocation is adding 2 to all values in the noise latent, and Analyze creates histogram plots.
image
Both Analyze results show the shift of 2 in this case, but one of them should show the original noise instead.

This is an issue of referencing the same object, not of reusing the same name:

[2024-09-07 15:22:09,044]::[InvokeAI]::INFO --> Loaded noise tensor with name Tensor_a8e64f3f-2288-4839-8e63-ca982eb79db6
[2024-09-07 15:22:09,067]::[InvokeAI]::INFO --> Saved noise tensor with name Tensor_a82a4b1b-f9fc-4957-b078-a870a4971164

What you expected to happen

Context should return a .clone() of the stored latent to avoid any direct access to the saved (cached) tensor.

How to reproduce the problem

Example for issue:

noise = context.tensors.load(self.noise.latents_name)
for i in range(noise.shape[1]):
    subnoise = noise[:, i, :, :]
    subnoise = subnoise + self.adjust
    noise[:, i, :, :] = subnoise
name = context.tensors.save(tensor=noise)
return NoiseOutput.build(latents_name=name, latents=noise, seed=self.noise.seed)

Additional context

No response

Discord username

dunkeroni

@dunkeroni dunkeroni added the bug Something isn't working label Sep 7, 2024
@psychedelicious
Copy link
Collaborator

I think this would be an issue for any entity we keep in memory across invocations - so, anything handled by these things:

Possible solutions:

  • Deep copy everything returned by the context API (as suggested)
  • Deep copy on read for each of the linked cache implementations
  • Theoretical transparent Readyonly<T> type added to the context API method return annotations, indicating to nodes that the values shouldn't be mutated without any runtime impact (I suspect this isn't possible in python without some juggling, it appears to have no concept of readonly/frozen)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants