-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andreas Blattmann
committed
Nov 28, 2023
1 parent
ed09971
commit e6f0e36
Showing
5 changed files
with
285 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,58 @@ | ||
STABILITY AI NON-COMMERCIAL RESEARCH COMMUNITY LICENSE AGREEMENT | ||
Dated: November 28, 2023 | ||
|
||
|
||
By using or distributing any portion or element of the Models, Software, Software Products or Derivative Works, you agree to be bound by this Agreement. | ||
|
||
|
||
"Agreement" means this Stable Non-Commercial Research Community License Agreement. | ||
|
||
|
||
“AUP” means the Stability AI Acceptable Use Policy available at https://stability.ai/use-policy, as may be updated from time to time. | ||
|
||
|
||
"Derivative Work(s)” means (a) any derivative work of the Software Products as recognized by U.S. copyright laws and (b) any modifications to a Model, and any other model created which is based on or derived from the Model or the Model’s output. For clarity, Derivative Works do not include the output of any Model. | ||
|
||
|
||
“Documentation” means any specifications, manuals, documentation, and other written information provided by Stability AI related to the Software. | ||
|
||
|
||
"Licensee" or "you" means you, or your employer or any other person or entity (if you are entering into this Agreement on such person or entity's behalf), of the age required under applicable laws, rules or regulations to provide legal consent and that has legal authority to bind your employer or such other person or entity if you are entering in this Agreement on their behalf. | ||
|
||
|
||
“Model(s)" means, collectively, Stability AI’s proprietary models and algorithms, including machine-learning models, trained model weights and other elements of the foregoing, made available under this Agreement. | ||
|
||
|
||
“Non-Commercial Uses” means exercising any of the rights granted herein for the purpose of research or non-commercial purposes. Non-Commercial Uses does not include any production use of the Software Products or any Derivative Works. | ||
|
||
|
||
"Stability AI" or "we" means Stability AI Ltd. and its affiliates. | ||
|
||
"Software" means Stability AI’s proprietary software made available under this Agreement. | ||
|
||
|
||
“Software Products” means the Models, Software and Documentation, individually or in any combination. | ||
|
||
|
||
|
||
1. License Rights and Redistribution. | ||
|
||
a. Subject to your compliance with this Agreement, the AUP (which is hereby incorporated herein by reference), and the Documentation, Stability AI grants you a non-exclusive, worldwide, non-transferable, non-sublicensable, revocable, royalty free and limited license under Stability AI’s intellectual property or other rights owned or controlled by Stability AI embodied in the Software Products to reproduce the Software Products and produce, reproduce, distribute, and create Derivative Works of the Software Products for Non-Commercial Uses only, respectively. | ||
|
||
b. You may not use the Software Products or Derivative Works to enable third parties to use the Software Products or Derivative Works as part of your hosted service or via your APIs, whether you are adding substantial additional functionality thereto or not. Merely distributing the Software Products or Derivative Works for download online without offering any related service (ex. by distributing the Models on HuggingFace) is not a violation of this subsection. If you wish to use the Software Products or any Derivative Works for commercial or production use or you wish to make the Software Products or any Derivative Works available to third parties via your hosted service or your APIs, contact Stability AI at https://stability.ai/contact. | ||
|
||
c. If you distribute or make the Software Products, or any Derivative Works thereof, available to a third party, the Software Products, Derivative Works, or any portion thereof, respectively, will remain subject to this Agreement and you must (i) provide a copy of this Agreement to such third party, and (ii) retain the following attribution notice within a "Notice" text file distributed as a part of such copies: "This Stability AI Model is licensed under the Stability AI Non-Commercial Research Community License, Copyright (c) Stability AI Ltd. All Rights Reserved.” If you create a Derivative Work of a Software Product, you may add your own attribution notices to the Notice file included with the Software Product, provided that you clearly indicate which attributions apply to the Software Product and you must state in the NOTICE file that you changed the Software Product and how it was modified. | ||
|
||
2. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE SOFTWARE PRODUCTS AND ANY OUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE SOFTWARE PRODUCTS, DERIVATIVE WORKS OR ANY OUTPUT OR RESULTS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE SOFTWARE PRODUCTS, DERIVATIVE WORKS AND ANY OUTPUT AND RESULTS. | ||
|
||
3. Limitation of Liability. IN NO EVENT WILL STABILITY AI OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF STABILITY AI OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING. | ||
|
||
4. Intellectual Property. | ||
|
||
a. No trademark licenses are granted under this Agreement, and in connection with the Software Products or Derivative Works, neither Stability AI nor Licensee may use any name or mark owned by or associated with the other or any of its affiliates, except as required for reasonable and customary use in describing and redistributing the Software Products or Derivative Works. | ||
|
||
b. Subject to Stability AI’s ownership of the Software Products and Derivative Works made by or for Stability AI, with respect to any Derivative Works that are made by you, as between you and Stability AI, you are and will be the owner of such Derivative Works | ||
|
||
c. If you institute litigation or other proceedings against Stability AI (including a cross-claim or counterclaim in a lawsuit) alleging that the Software Products, Derivative Works or associated outputs or results, or any portion of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable by you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold harmless Stability AI from and against any claim by any third party arising out of or related to your use or distribution of the Software Products or Derivative Works in violation of this Agreement. | ||
|
||
5. Term and Termination. The term of this Agreement will commence upon your acceptance of this Agreement or access to the Software Products and will continue in full force and effect until terminated in accordance with the terms and conditions herein. Stability AI may terminate this Agreement if you are in breach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete and cease use of any Software Products or Derivative Works. Sections 2-4 shall survive the termination of this Agreement. |
This file contains 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 |
---|---|---|
|
@@ -37,3 +37,4 @@ wandb>=0.15.6 | |
webdataset>=0.2.33 | ||
wheel>=0.41.0 | ||
xformers>=0.0.20 | ||
streamlit-keyup==0.2.0 |
This file contains 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,214 @@ | ||
from streamlit_helpers import * | ||
from st_keyup import st_keyup | ||
from sgm.modules.diffusionmodules.sampling import EulerAncestralSampler | ||
|
||
VERSION2SPECS = { | ||
"SDXL-Turbo": { | ||
"H": 512, | ||
"W": 512, | ||
"C": 4, | ||
"f": 8, | ||
"is_legacy": False, | ||
"config": "configs/inference/sd_xl_base.yaml", | ||
"ckpt": "checkpoints/sd_xl_turbo_1.0.safetensors", | ||
}, | ||
} | ||
|
||
|
||
class SubstepSampler(EulerAncestralSampler): | ||
def __init__(self, n_sample_steps=1, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.n_sample_steps = n_sample_steps | ||
self.steps_subset = [0, 100, 200, 300, 1000] | ||
|
||
def prepare_sampling_loop(self, x, cond, uc=None, num_steps=None): | ||
sigmas = self.discretization( | ||
self.num_steps if num_steps is None else num_steps, device=self.device | ||
) | ||
sigmas = sigmas[ | ||
self.steps_subset[: self.n_sample_steps] + self.steps_subset[-1:] | ||
] | ||
uc = cond | ||
x *= torch.sqrt(1.0 + sigmas[0] ** 2.0) | ||
num_sigmas = len(sigmas) | ||
s_in = x.new_ones([x.shape[0]]) | ||
return x, s_in, sigmas, num_sigmas, cond, uc | ||
|
||
|
||
def seeded_randn(shape, seed): | ||
randn = np.random.RandomState(seed).randn(*shape) | ||
randn = torch.from_numpy(randn).to(device="cuda", dtype=torch.float32) | ||
return randn | ||
|
||
|
||
class SeededNoise: | ||
def __init__(self, seed): | ||
self.seed = seed | ||
|
||
def __call__(self, x): | ||
self.seed = self.seed + 1 | ||
return seeded_randn(x.shape, self.seed) | ||
|
||
|
||
def init_embedder_options(keys, init_dict, prompt=None, negative_prompt=None): | ||
value_dict = {} | ||
for key in keys: | ||
if key == "txt": | ||
value_dict["prompt"] = prompt | ||
value_dict["negative_prompt"] = "" | ||
|
||
if key == "original_size_as_tuple": | ||
orig_width = init_dict["orig_width"] | ||
orig_height = init_dict["orig_height"] | ||
|
||
value_dict["orig_width"] = orig_width | ||
value_dict["orig_height"] = orig_height | ||
|
||
if key == "crop_coords_top_left": | ||
crop_coord_top = 0 | ||
crop_coord_left = 0 | ||
|
||
value_dict["crop_coords_top"] = crop_coord_top | ||
value_dict["crop_coords_left"] = crop_coord_left | ||
|
||
if key == "aesthetic_score": | ||
value_dict["aesthetic_score"] = 6.0 | ||
value_dict["negative_aesthetic_score"] = 2.5 | ||
|
||
if key == "target_size_as_tuple": | ||
value_dict["target_width"] = init_dict["target_width"] | ||
value_dict["target_height"] = init_dict["target_height"] | ||
|
||
return value_dict | ||
|
||
|
||
def sample( | ||
model, | ||
sampler, | ||
prompt="A lush garden with oversized flowers and vibrant colors, inhabited by miniature animals.", | ||
H=1024, | ||
W=1024, | ||
seed=0, | ||
filter=None, | ||
): | ||
F = 8 | ||
C = 4 | ||
shape = (1, C, H // F, W // F) | ||
|
||
value_dict = init_embedder_options( | ||
keys=get_unique_embedder_keys_from_conditioner(model.conditioner), | ||
init_dict={ | ||
"orig_width": W, | ||
"orig_height": H, | ||
"target_width": W, | ||
"target_height": H, | ||
}, | ||
prompt=prompt, | ||
) | ||
|
||
if seed is None: | ||
seed = torch.seed() | ||
precision_scope = autocast | ||
with torch.no_grad(): | ||
with precision_scope("cuda"): | ||
batch, batch_uc = get_batch( | ||
get_unique_embedder_keys_from_conditioner(model.conditioner), | ||
value_dict, | ||
[1], | ||
) | ||
c = model.conditioner(batch) | ||
uc = None | ||
randn = seeded_randn(shape, seed) | ||
|
||
def denoiser(input, sigma, c): | ||
return model.denoiser( | ||
model.model, | ||
input, | ||
sigma, | ||
c, | ||
) | ||
|
||
samples_z = sampler(denoiser, randn, cond=c, uc=uc) | ||
samples_x = model.decode_first_stage(samples_z) | ||
samples = torch.clamp((samples_x + 1.0) / 2.0, min=0.0, max=1.0) | ||
if filter is not None: | ||
samples = filter(samples) | ||
samples = ( | ||
(255 * samples) | ||
.to(dtype=torch.uint8) | ||
.permute(0, 2, 3, 1) | ||
.detach() | ||
.cpu() | ||
.numpy() | ||
) | ||
return samples | ||
|
||
|
||
def v_spacer(height) -> None: | ||
for _ in range(height): | ||
st.write("\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
st.title("Turbo") | ||
|
||
head_cols = st.columns([1, 1, 1]) | ||
with head_cols[0]: | ||
version = st.selectbox("Model Version", list(VERSION2SPECS.keys()), 0) | ||
version_dict = VERSION2SPECS[version] | ||
|
||
with head_cols[1]: | ||
v_spacer(2) | ||
if st.checkbox("Load Model"): | ||
mode = "txt2img" | ||
else: | ||
mode = "skip" | ||
|
||
if mode != "skip": | ||
state = init_st(version_dict, load_filter=True) | ||
if state["msg"]: | ||
st.info(state["msg"]) | ||
model = state["model"] | ||
load_model(model) | ||
|
||
# seed | ||
if "seed" not in st.session_state: | ||
st.session_state.seed = 0 | ||
|
||
def increment_counter(): | ||
st.session_state.seed += 1 | ||
|
||
def decrement_counter(): | ||
if st.session_state.seed > 0: | ||
st.session_state.seed -= 1 | ||
|
||
with head_cols[2]: | ||
n_steps = st.number_input(label="number of steps", min_value=1, max_value=4) | ||
|
||
sampler = SubstepSampler( | ||
n_sample_steps=1, | ||
num_steps=1000, | ||
eta=1.0, | ||
discretization_config=dict( | ||
target="sgm.modules.diffusionmodules.discretizer.LegacyDDPMDiscretization" | ||
), | ||
) | ||
sampler.n_sample_steps = n_steps | ||
default_prompt = "A cinematic shot of a baby racoon wearing an intricate italian priest robe." | ||
prompt = st_keyup("Enter a value", value=default_prompt, debounce=300, key="interactive_text") | ||
|
||
cols = st.columns([1, 5, 1]) | ||
if mode != "skip": | ||
with cols[0]: | ||
v_spacer(14) | ||
st.button("↩", on_click=decrement_counter) | ||
with cols[2]: | ||
v_spacer(14) | ||
st.button("↪", on_click=increment_counter) | ||
|
||
sampler.noise_sampler = SeededNoise(seed=st.session_state.seed) | ||
out = sample( | ||
model, sampler, H=512, W=512, seed=st.session_state.seed, prompt=prompt, filter=state.get("filter") | ||
) | ||
with cols[1]: | ||
st.image(out[0]) |