Skip to content

Conversation

@sayakpaul
Copy link
Member

@sayakpaul sayakpaul commented Oct 23, 2023

From the model card of https://huggingface.co/hf-internal-testing/tiny-sdxl-custom-all, if we run

from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-sdxl-custom-all", trust_remote_code=True)

assert pipeline.config.unet == ('diffusers_modules.local.my_unet_model', 'MyUNetModel')
assert pipeline.config.scheduler == ('diffusers_modules.local.my_scheduler', 'MyScheduler')
assert pipeline.__class__.__name__ == "MyPipeline"

pipeline = pipeline.to("cuda")
images = pipeline("test", num_inference_steps=2, output_type="np")[0]

assert images.shape == (1, 64, 64, 3)

it will lead to:

Traceback (most recent call last):
  File "/home/sayak/test.py", line 2, in <module>
    pipeline = DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-sdxl-custom-all", trust_remote_code=True)
  File "/home/sayak/diffusers/src/diffusers/pipelines/pipeline_utils.py", line 1073, in from_pretrained
    cached_folder = cls.download(
  File "/home/sayak/diffusers/src/diffusers/pipelines/pipeline_utils.py", line 1842, in download
    pipeline_class = getattr(diffusers, cls_name, None)
TypeError: getattr(): attribute name must be string

To reproduce, you must clear the cache associated with hf-internal-testing/tiny-sdxl-custom-all.

This PR fixes that.

Additionally, this PR adds the functionality to serialize the module files with the pipeline making it easy for users to share a self-contained pipeline repository on the Hub that can be readily loaded with trust_remote_code=True without having to go through the steps outlined here: #5472 (comment). Here's a gist to test it all out: https://gist.github.com/sayakpaul/685d263df0bd845cbe8997ebd468a5c5.

More comments inline.

@sayakpaul sayakpaul changed the title Debug add custom pipelines [trust remote code] Additional features Oct 25, 2023
Comment on lines +173 to +186
# Additionally, save the implementation file too. It can happen for a pipeline, for a model, and
# for a scheduler.
if self.__class__.__name__ not in _all_available_pipeline_component_modules:
module_to_save = self.__class__.__module__
absolute_module_path = sys.modules[module_to_save].__file__
try:
with open(absolute_module_path, "r") as original_file:
content = original_file.read()
path_to_write = os.path.join(save_directory, f"{module_to_save}.py")
with open(path_to_write, "w") as new_file:
new_file.write(content)
logger.info(f"{module_to_save}.py saved in {save_directory}")
except Exception as e:
logger.error(e)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also parameterize this based on a function argument if that's preferred.

Base automatically changed from add_custom_remote_pipelines to main October 26, 2023 16:02
@sayakpaul
Copy link
Member Author

Closing in favor of #5556.

@sayakpaul sayakpaul closed this Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants