-
Notifications
You must be signed in to change notification settings - Fork 200
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
Image generation: added TorchGenerator and rng_seed #1379
Image generation: added TorchGenerator and rng_seed #1379
Conversation
b110bb5
to
efeee56
Compare
2947845
to
66a506c
Compare
@@ -20,6 +20,10 @@ Users can change the sample code and play with the following generation paramete | |||
- Apply multiple different LoRA adapters and mix them with different blending coefficients | |||
- (Image to image and inpainting) Play with `strength` parameter to control how initial image is noised and reduce number of inference steps | |||
|
|||
> [!NOTE] | |||
> All samples use `openvino_genai.TorchGenerator` as an argument to `generate` call to align random numbers generation with Diffusers library where `torch.Generator` is used underhood. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> All samples use `openvino_genai.TorchGenerator` as an argument to `generate` call to align random numbers generation with Diffusers library where `torch.Generator` is used underhood. | |
> All Python samples use `openvino_genai.TorchGenerator` as an argument to `generate` call to align random numbers generation with Diffusers library where `torch.Generator` is used underhood. |
It reads "every existing genai sample" otherwise, including C++.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this README.md is in samples/python/image_generation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that we are pretty good at aligning C++ and Python. One may assume that C++ readme states the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how C++ samples can use torch.Generator ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before I red the changes my assumption was that you had copied full torch generator to genai. That would enable it for C++
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, please check
throw std::runtime_error("Expected a NumPy array with dtype float32"); | ||
} | ||
|
||
return ov::Tensor(ov::element::f32, shape, numpy_array.mutable_data()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you keep m_torch_tensor
as a member to keep this returned tensor valid. Calling randn_tensor()
again would override m_torch_tensor
and loose the previous data. You probably need that trick with an allocator here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, updated
m_float32 = m_torch.attr("float32"); | ||
} catch (const py::error_already_set& e) { | ||
if (e.matches(PyExc_ModuleNotFoundError)) { | ||
PyErr_WarnEx(PyExc_ImportWarning, "'torch' module is not installed. Random generation will fall back to 'CppStdGenerator'", 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Printing anything from a library isn't really welcome (at least for me). It's an application's (sample's in our case) responsibility to communicate with a user.
My proposal is the following. TorchGenerator
should throw if torch
is missing. Introduce a function in samples that picks an available generator and prints this warning.
I have another concern. Why do we need a C++ wrapper over python in the first place. Samples themselves could define a thin generator in python similar to this TorchGenerator
because there's no benefit in jumping between C++ and Python multiple times. That implementation would also serve as an example generator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proposal is the following. TorchGenerator should throw if torch is missing
Having slept on it, I agree that throwing exception is better idea.
c627c8a
to
72e5a39
Compare
72e5a39
to
b934d0e
Compare
b934d0e
to
e8f9928
Compare
e8f9928
to
afc22fe
Compare
TorchGenerator
which wrapstorch.Generator
. It throws an exception istorch
is not available.rng_seed
parameter toImageGenerationConfig
which has lower priority compared withgenerator
when they both are specified togenerate()
orImageGenerationConfig::update_generation_config