-
Notifications
You must be signed in to change notification settings - Fork 61
Add diffusers utils #104
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
Add diffusers utils #104
Conversation
From your description: Why is that needed for SageMaker but cannot be used locally? That's a strange requirement for testing locally and does not give me confidence that everything here works correctly. |
It seems that when the container is deployed on SageMaker, the "body" is stringified and encoded. When starting the MMS server locally (no container), the body is just what you pass. Since we are not including the dockerfile here, we need to use MMS locally. |
Ok, but for whatever reason the "body" is a
>>> json_string = '{"🙃": 17}'
>>> json_bytes = json_string.encode("utf-8")
>>> dump = lambda v: print(f"{type(v)} => {repr(v)}")
>>> dump(json_string)
<class 'str'> => '{"🙃": 17}'
>>> dump(json_bytes)
<class 'bytes'> => b'{"\xf0\x9f\x99\x83": 17}'
>>> json.loads(json_string)
{'🙃': 17}
>>> json.loads(json_bytes)
{'🙃': 17} |
trust_remote_code=TRUST_REMOTE_CODE, | ||
model_kwargs={"device_map": "auto", "torch_dtype": torch_dtype}, | ||
) | ||
elif is_diffusers_available() and task == "text-to-image": |
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.
Would it makes sense to go to else
case if task == "text-to-image" but
is_diffusers_availablereturns
False`?
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.
no. There you need both.
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.
But that's what this if-elif-else chain will do. If is_diffusers_available()
is False and task
is "text-to-image", then this branch will fail and it will go to the else
clause.
So you're saying that doesn't make sense?
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.
You mean we should rather error? That when task-to-image
== True but diffusers not available?
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.
Yeah, probably. Right now, the code does something that doesn't make sense in that case, because it falls through to the else
clause.
I said that from the beginning. But yeah, at this point this back and forth is a bit ridiculous, so whatever. It's probably fine in practice. But still, this if-elif-else logic seems kind of wrong. I don't know how to be more clear.
# load pipeline | ||
hf_pipeline = pipeline(task=task, model=model_dir, device=device, **kwargs) | ||
if TRUST_REMOTE_CODE and os.environ.get("HF_MODEL_ID", None) is not None and device == 0: | ||
torch_dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16 |
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 if-elif-else
seems odd to me. Could the if
ever match when we want to use diffusers
?
Also, why is this extra logic in the if
case regarding bfloat16
and whatever else only used when TRUST_REMOTE_CODE
is True
, but then TRUST_REMOTE_CODE
is also passed in other cases?
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.
What is odd for you?
- branch is when you have
TRUST_REMOTE_CODE
which is for custom modelling available through the hub and it only work if there is aHF_MODEL_ID
and you have GPUs. -> was needed for mpt. - checks whether we have diffusers installed and if we have a diffusers task and to load image generation model.
- is default where we try to load the custom pipeline
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.
It's strange because there's special bfloat16 logic but only when TRUST_REMOTE_CODE is True. I mentioned that in my comment. And it also seemed strange to me because of what I said in my other comment below.
Thats what was implemented and what SageMaker is doing. I don't want to change something which is not wrong. It is just for testing. Or can you guarantee its not needed? |
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.
I'm approving because I don't want to hold things up. But the lack of bfloat16 logic in any branch but the first TRUST_REMOTE_CODE one, and the other issue I pointed out with the if-elif-else
clause, seem kind of wrong to me.
In practice, it may be fine. I'll leave that up to you.
trust_remote_code=TRUST_REMOTE_CODE, | ||
model_kwargs={"device_map": "auto", "torch_dtype": torch_dtype}, | ||
) | ||
elif is_diffusers_available() and task == "text-to-image": |
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.
Yeah, probably. Right now, the code does something that doesn't make sense in that case, because it falls through to the else
clause.
I said that from the beginning. But yeah, at this point this back and forth is a bit ridiculous, so whatever. It's probably fine in practice. But still, this if-elif-else logic seems kind of wrong. I don't know how to be more clear.
Let me double check and make sure the tests are green. |
What does this PR do?
This PR adds support for
diffusers
andtext-to-image
task for zero-code deployments. This will allow customers to deploy any diffusers model support by AutoPipeline including:This PR adds a new:
diffusers_utils.py
which handles the heavy lifting for the zero-code deployment with new unit and integration testsTest locally
MMS_CONFIG_FILE
text-to-image
Adjust
handler_service.py
and comment outif content_type in content_types.UTF8_TYPES:
thats needed for SageMaker but cannot be used locallySend request