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

feat(text-to-image): integrate NSFW safety checker #76

Merged
merged 2 commits into from
May 15, 2024

Conversation

rickstaa
Copy link
Contributor

@rickstaa rickstaa commented May 5, 2024

This pull request integrates the CompVis/stable-diffusion-safety-checker into the text-to-image and image-to-image pipelines. By enabling the safety_check input variable, users can detect the generation of NSFW images. The feature is enabled by default on Orchestrators, but they can opt to perform the safety check on the CPU using the SAFETY_CHECKER_DEVICE environment variable to conserve VRAM, though this may slow down operations.

I have decided against implementing this feature in the image-to-video pipeline as checking each frame would introduce significant latency, and I believe responsibility for checking the input image should lie with the dApp. However, I can readily add a check on the input image if desired.

Performance Results

T2I pipeline

Benchmarking was conducted to assess how the new feature impacts inference speed and VRAM usage:

Commands:

docker run --gpus 0 -v /home/ricks/.lpData/models:/models livepeer/ai-runner:latest python bench.py --pipeline text-to-image --model_id stabilityai/sd-turbo --runs 10 --batch_size 3

and

docker run -e  SAFETY_CHECKER_DEVICE=<DEVICE> --gpus 0 -v /home/ricks/.lpData/models:/models livepeer/ai-runner:latest python bench.py --pipeline image-to-image --model_id stabilityai/sd-turbo --runs 10 --batch_size 3

Original Metrics:

pipeline load time: 1.664s
pipeline load max GPU memory allocated: 4.824GiB
pipeline load max GPU memory reserved: 4.904GiB
avg inference time: 0.635s
avg inference time per output: 0.212s
avg inference max GPU memory allocated: 7.651GiB
avg inference max GPU memory reserved: 9.521GiB

With Safety Checker (GPU, safety_check not set):

pipeline load time: 2.586s
pipeline load max GPU memory allocated: 5.966GiB
pipeline load max GPU memory reserved: 6.025GiB
avg inference time: 0.632s
avg inference time per output: 0.211s
avg inference max GPU memory allocated: 8.793GiB
avg inference max GPU memory reserved: 10.623GiB

With Safety Checker (GPU, safety_check set):

pipeline load time: 2.279s
pipeline load max GPU memory allocated: 5.966GiB
pipeline load max GPU memory reserved: 6.025GiB
avg inference time: 0.695s
avg inference time per output: 0.232s
avg inference max GPU memory allocated: 8.793GiB
avg inference max GPU memory reserved: 10.623GiB

With Safety Checker (CPU, safety_check not set):

pipeline load time: 2.245s
pipeline load max GPU memory allocated: 4.824GiB
pipeline load max GPU memory reserved: 4.904GiB
avg inference time: 0.640s
avg inference time per output: 0.213s
avg inference max GPU memory allocated: 7.651GiB
avg inference max GPU memory reserved: 9.521GiB

With Safety Checker (CPU, safety_check set):

pipeline load time: 2.203s
pipeline load max GPU memory allocated: 4.832GiB
pipeline load max GPU memory reserved: 4.895GiB
avg inference time: 1.233s
avg inference time per output: 0.411s
avg inference max GPU memory allocated: 7.659GiB
avg inference max GPU memory reserved: 9.512GiB

I2I pipeline

Benchmarking was conducted to assess how the new feature impacts inference speed and VRAM usage:

Commands:

docker run --gpus 0 -v /home/ricks/.lpData/models:/models livepeer/ai-runner:latest python bench.py --pipeline image-to-image --model_id stabilityai/sd-turbo --runs 10 --batch_size 3

and

docker run -e  SAFETY_CHECKER_DEVICE=<DEVICE> --gpus 0 -v /home/ricks/.lpData/models:/models livepeer/ai-runner:latest python bench.py --pipeline image-to-image --model_id stabilityai/sd-turbo --runs 10 --batch_size 3

Original Metrics:

pipeline load time: 1.672s
pipeline load max GPU memory allocated: 4.825GiB
pipeline load max GPU memory reserved: 4.900GiB
avg inference time: 4.179s
avg inference time per output: 1.393s
avg inference max GPU memory allocated: 16.092GiB
avg inference max GPU memory reserved: 20.023GiB

With Safety Checker (GPU, safety_check not set):

pipeline load time: 2.347s
pipeline load max GPU memory allocated: 5.966GiB
pipeline load max GPU memory reserved: 6.025GiB
avg inference time: 4.115s
avg inference time per output: 1.372s
avg inference max GPU memory allocated: 17.233GiB
avg inference max GPU memory reserved: 21.129GiB

With Safety Checker (GPU, safety_check set):

pipeline load time: 2.404s
pipeline load max GPU memory allocated: 5.959GiB
pipeline load max GPU memory reserved: 6.045GiB
avg inference time: 4.220s
avg inference time per output: 1.407s
avg inference max GPU memory allocated: 17.225GiB
avg inference max GPU memory reserved: 21.148GiB

With Safety Checker (CPU, safety_check not set):

pipeline load time: 2.297s
pipeline load max GPU memory allocated: 4.833GiB
pipeline load max GPU memory reserved: 4.900GiB
avg inference time: 4.200s
avg inference time per output: 1.400s
avg inference max GPU memory allocated: 16.100GiB
avg inference max GPU memory reserved: 20.004GiB

With Safety Checker (CPU, safety_check set):

pipeline load time: 2.259s
pipeline load max GPU memory allocated: 4.833GiB
pipeline load max GPU memory reserved: 4.900GiB
avg inference time: 4.860s
avg inference time per output: 1.620s
avg inference max GPU memory allocated: 16.100GiB
avg inference max GPU memory reserved: 20.004GiB

Conclusion

From the results, we can conclude that the new feature:

  • Introduction of the new feature increases the pipeline startup time by approximately 0.5 seconds.
  • Enabling safety_check increases inference time by about 20ms on the GPU.
  • Enabling safety_check on the CPU increases inference time significantly, by about 200ms.
  • Increases VRAM usage with 1102 MiB when done on the GPU.

@rickstaa rickstaa marked this pull request as draft May 5, 2024 18:16
@rickstaa rickstaa force-pushed the add_nsfw_safety_checked_2 branch 3 times, most recently from cbb01b7 to 5f379e3 Compare May 6, 2024 10:20
@rickstaa rickstaa marked this pull request as ready for review May 6, 2024 10:59
@rickstaa
Copy link
Contributor Author

rickstaa commented May 6, 2024

@dob, @yondonfu, @eliteprox, @ad-astra-video, this feature was introduced following a request from CJ on Discord, addressing concerns about NSFW images being generated via their apps. I’d appreciate your insights on integrating this safety check into our pipelines. Additionally, I'm considering whether to include an input check in the text-to-video pipeline or if it should remain the responsibility of the dApp. What are your thoughts on this matter?

@dob
Copy link
Member

dob commented May 7, 2024 via email

This commit incorporates the CompVis/stable-diffusion-safety-checker
into the text-to-image and image-to-image pipelines. By enabling the
`safety_check` input variable, users get notified of the generation
of NSFW images.
rickstaa added a commit that referenced this pull request May 15, 2024
This commit enables the safety checker by default. For more information
about this decision see
#76 (comment).
This commit enables the safety checker by default. For more information
about this decision see
#76 (comment).
@rickstaa rickstaa merged commit 272ac74 into main May 15, 2024
1 check passed
@rickstaa rickstaa deleted the add_nsfw_safety_checked_2 branch May 15, 2024 09:41
rickstaa added a commit to livepeer/go-livepeer that referenced this pull request May 15, 2024
This commit enables the NSFW filter on the AI Subnet that has been
implement at the runner side in
livepeer/ai-worker#76.

BREAKING CHANGE: Depending on how dApps interact with the subnet this
could be a breaking change given that we return an extra `nsfw`
property.
rickstaa added a commit to livepeer/go-livepeer that referenced this pull request May 15, 2024
This commit enables the NSFW filter on the AI Subnet that has been
implement at the runner side in
livepeer/ai-worker#76.

BREAKING CHANGE: Depending on how dApps interact with the subnet this
could be a breaking change given that we return an extra `nsfw`
property.
rickstaa added a commit to livepeer/go-livepeer that referenced this pull request May 15, 2024
This commit enables the NSFW filter on the AI Subnet that has been
implement at the runner side in
livepeer/ai-worker#76.

BREAKING CHANGE: Depending on how dApps interact with the subnet this
could be a breaking change given that we return an extra `nsfw`
property.
rickstaa added a commit to livepeer/go-livepeer that referenced this pull request May 15, 2024
This commit enables the NSFW filter on the AI Subnet that has been
implement at the runner side in
livepeer/ai-worker#76.

BREAKING CHANGE: Depending on how dApps interact with the subnet this
could be a breaking change given that we return an extra `nsfw`
property.
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.

None yet

2 participants