forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Add flux1 pipeline #2
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
Draft
kkimmk
wants to merge
14
commits into
main
Choose a base branch
from
add-flux1-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
97b3f3a
feat: flux1 entrypoint
kkimmk 5ef581b
feat: flux1 interface
kkimmk d81e142
feat: flux1 pipeline
kkimmk 2b80ab7
[MAX] Add Conv2d and GroupNorm to module_v3
byungchul-sqzb ee0f9a4
[Pipelines] Add AutoencoderKL VAE decoder implementation for Flux.1
byungchul-sqzb 8cb2147
feat: add clip for flux1 pipeline support
kkimmk dc452f9
feat: add t5 for flux1 pipeline support
kkimmk 74b220a
feat: add flux1 model for flux1 pipeline support
kkimmk e5957d3
feat: add flux1 pipeline
kkimmk 120066a
Merge branch 'add/flux1-pipeline/models-clip' into add-flux1-pipeline
kkimmk 0aad0c2
Merge branch 'add/flux1-pipeline/models-t5' into add-flux1-pipeline
kkimmk d8b0ba3
Merge branch 'add/flux1-pipeline/models-flux1' into add-flux1-pipeline
kkimmk 3ba9328
Merge branch 'add/flux1-pipeline/models-vae' into add-flux1-pipeline
kkimmk c16451a
chore: add benchmark
kkimmk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # ===----------------------------------------------------------------------=== # | ||
| # Copyright (c) 2025, Modular Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
| # https://llvm.org/LICENSE.txt | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ===----------------------------------------------------------------------=== # | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
|
|
||
| from max.entrypoints.diffusion import DiffusionPipeline | ||
| from max.experimental.realization_context import set_seed | ||
| from max.pipelines import PipelineConfig | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--model-path", type=str, default="black-forest-labs/FLUX.1-dev" | ||
| ) | ||
| parser.add_argument("--seed", type=int, default=42) | ||
| args = parser.parse_args() | ||
|
|
||
| model_path = args.model_path | ||
| set_seed(args.seed) | ||
| pipeline_config = PipelineConfig(model_path=model_path) | ||
| pipe = DiffusionPipeline(pipeline_config) | ||
|
|
||
| prompt = "A cat holding a sign that says hello world" | ||
| print(f"Prompt: {prompt}") | ||
|
|
||
| result = pipe( | ||
| prompt=prompt, | ||
| height=1024, | ||
| width=1024, | ||
| num_inference_steps=50, | ||
| guidance_scale=3.5, | ||
| ) | ||
|
|
||
| images = result.images | ||
|
|
||
| output_path = Path("output.png") | ||
| output_path.parent.mkdir(parents=True, exist_ok=True) | ||
| images[0].save(output_path) | ||
| print(f"Image saved to: {output_path}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # ===----------------------------------------------------------------------=== # | ||
| # Copyright (c) 2025, Modular Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
| # https://llvm.org/LICENSE.txt | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ===----------------------------------------------------------------------=== # | ||
|
|
||
| """Extension for max.dtype to support additional attributes.""" | ||
|
|
||
| from numpy import finfo as np_finfo | ||
|
|
||
| from .dtype import DType | ||
|
|
||
|
|
||
| class finfo: | ||
| """A numerical properties of a floating point max.dtype.DType. | ||
|
|
||
| This class mimics torch.finfo behavior without torch dependency, | ||
| including support for bfloat16. | ||
|
|
||
| NOTE: Currently, it's applied through patching. | ||
| This extension is better to be implemented in dtype library itself. | ||
| """ | ||
|
|
||
| def __init__(self, dtype: DType): | ||
| """Initialize finfo for a given max.dtype.DType. | ||
|
|
||
| Args: | ||
| dtype: The data type to get limits for. | ||
| """ | ||
| if dtype == DType.bfloat16: | ||
| self.min = -3.38953e38 | ||
| self.max = 3.38953e38 | ||
| self.bits = 16 | ||
| self.eps = 0.0078125 | ||
| self.resolution = 0.01 | ||
| self.tiny = 1.17549e-38 | ||
| self.dtype = "bfloat16" | ||
| else: | ||
| np_finfo_obj = np_finfo(dtype.to_numpy()) | ||
| self.min = float(np_finfo_obj.min) | ||
| self.max = float(np_finfo_obj.max) | ||
| self.bits = np_finfo_obj.bits | ||
| self.eps = float(np_finfo_obj.eps) | ||
| self.resolution = float(np_finfo_obj.resolution) | ||
| self.tiny = float(np_finfo_obj.tiny) | ||
| self.dtype = str(np_finfo_obj.dtype) | ||
|
|
||
|
|
||
| DType.finfo = finfo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # ===----------------------------------------------------------------------=== # | ||
| # Copyright (c) 2025, Modular Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
| # https://llvm.org/LICENSE.txt | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ===----------------------------------------------------------------------=== # | ||
|
|
||
| from max.interfaces import ( | ||
| ImageGenerationInputs, | ||
| ImageGenerationOutput, | ||
| PipelineTask, | ||
| ) | ||
| from max.pipelines.lib import PIPELINE_REGISTRY, PipelineConfig | ||
|
|
||
|
|
||
| class DiffusionPipeline: | ||
| """Entrypoint for image-generation diffusion pipelines.""" | ||
|
|
||
| def __init__(self, pipeline_config: PipelineConfig) -> None: | ||
| # NOTE: Currently, this entrypoint is implemented minimally | ||
| # for offline image generation. | ||
| # It will be developed further to support serving as well. | ||
| self.pipeline_config = pipeline_config | ||
| _, model_factory = PIPELINE_REGISTRY.retrieve_factory( | ||
| pipeline_config, | ||
| task=PipelineTask.IMAGE_GENERATION, | ||
| ) | ||
| self.pipeline = model_factory() | ||
|
|
||
| def __call__( | ||
| self, | ||
| prompt: str, | ||
| negative_prompt: str | None = None, | ||
| true_cfg_scale: float = 1.0, | ||
| height: int = 1024, | ||
| width: int = 1024, | ||
| num_inference_steps: int = 50, | ||
| guidance_scale: float = 3.5, | ||
| num_images_per_prompt: int = 1, | ||
| ) -> ImageGenerationOutput: | ||
| """Generate images from a prompt with the configured pipeline.""" | ||
| # TODO: consider all possible diffusion tasks, | ||
| # e.g. T2I, I2I, T2V, I2V, V2V. | ||
| inputs = ImageGenerationInputs( | ||
| prompt=prompt, | ||
| negative_prompt=negative_prompt, | ||
| true_cfg_scale=true_cfg_scale, | ||
| height=height, | ||
| width=width, | ||
| num_inference_steps=num_inference_steps, | ||
| guidance_scale=guidance_scale, | ||
| num_images_per_prompt=num_images_per_prompt, | ||
| ) | ||
| pipeline_output: ImageGenerationOutput = self.pipeline.execute(inputs) | ||
| return pipeline_output |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Catching a generic
Exceptionis too broad and can hide unexpected errors. It's better to catch more specific exceptions that you expect to handle, such asjson.JSONDecodeErrorfor parsing issues andIOErrorfor file reading problems.