-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
85 changed files
with
9,543 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,17 @@ vendorize_normal_map: | |
make af | ||
|
||
|
||
vendorize_refiners: | ||
export [email protected]:finegrain-ai/refiners.git PKG=refiners COMMIT=20c229903f53d05dc1c44659ec97603660ef964c && \ | ||
make download_repo REPO=$$REPO PKG=$$PKG COMMIT=$$COMMIT && \ | ||
mkdir -p ./imaginairy/vendored/$$PKG && \ | ||
rm -rf ./imaginairy/vendored/$$PKG/* && \ | ||
cp -R ./downloads/refiners/src/refiners/* ./imaginairy/vendored/$$PKG/ && \ | ||
cp ./downloads/refiners/LICENSE ./imaginairy/vendored/$$PKG/ && \ | ||
rm -rf ./imaginairy/vendored/$$PKG/training_utils && \ | ||
echo "vendored from $$REPO @ $$COMMIT" | tee ./imaginairy/vendored/$$PKG/readme.txt | ||
make af | ||
|
||
|
||
vendorize: ## vendorize a github repo. `make vendorize [email protected]:openai/CLIP.git PKG=clip` | ||
mkdir -p ./downloads | ||
|
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
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
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
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
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Lagon Technologies | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
File renamed without changes.
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,3 @@ | ||
from refiners.fluxion.utils import load_from_safetensors, manual_seed, norm, pad, save_to_safetensors | ||
|
||
__all__ = ["norm", "manual_seed", "save_to_safetensors", "load_from_safetensors", "pad"] |
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,3 @@ | ||
from refiners.fluxion.adapters.adapter import Adapter | ||
|
||
__all__ = ["Adapter"] |
101 changes: 101 additions & 0 deletions
101
imaginairy/vendored/refiners/fluxion/adapters/adapter.py
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,101 @@ | ||
import contextlib | ||
from typing import Any, Generic, Iterator, TypeVar | ||
|
||
import refiners.fluxion.layers as fl | ||
|
||
T = TypeVar("T", bound=fl.Module) | ||
TAdapter = TypeVar("TAdapter", bound="Adapter[Any]") # Self (see PEP 673) | ||
|
||
|
||
class Adapter(Generic[T]): | ||
# we store _target into a one element list to avoid pytorch thinking it is a submodule | ||
_target: "list[T]" | ||
|
||
def __init_subclass__(cls, **kwargs: Any) -> None: | ||
super().__init_subclass__(**kwargs) | ||
assert issubclass(cls, fl.Chain), f"Adapter {cls.__name__} must be a Chain" | ||
|
||
@property | ||
def target(self) -> T: | ||
return self._target[0] | ||
|
||
@contextlib.contextmanager | ||
def setup_adapter(self, target: T) -> Iterator[None]: | ||
assert isinstance(self, fl.Chain) | ||
assert (not hasattr(self, "_modules")) or ( | ||
len(self) == 0 | ||
), "Call the Chain constructor in the setup_adapter context." | ||
self._target = [target] | ||
|
||
if not isinstance(self.target, fl.ContextModule): | ||
yield | ||
return | ||
|
||
_old_can_refresh_parent = target._can_refresh_parent | ||
target._can_refresh_parent = False | ||
yield | ||
target._can_refresh_parent = _old_can_refresh_parent | ||
|
||
def inject(self: TAdapter, parent: fl.Chain | None = None) -> TAdapter: | ||
assert isinstance(self, fl.Chain) | ||
|
||
if (parent is None) and isinstance(self.target, fl.ContextModule): | ||
parent = self.target.parent | ||
if parent is not None: | ||
assert isinstance(parent, fl.Chain), f"{self.target} has invalid parent {parent}" | ||
|
||
target_parent = self.find_parent(self.target) | ||
|
||
if parent is None: | ||
if isinstance(self.target, fl.ContextModule): | ||
self.target._set_parent(target_parent) # type: ignore[reportPrivateUsage] | ||
return self | ||
|
||
# In general, `true_parent` is `parent`. We do this to support multiple adaptation, | ||
# i.e. initializing two adapters before injecting them. | ||
true_parent = parent.ensure_find_parent(self.target) | ||
true_parent.replace( | ||
old_module=self.target, | ||
new_module=self, | ||
old_module_parent=target_parent, | ||
) | ||
return self | ||
|
||
def eject(self) -> None: | ||
assert isinstance(self, fl.Chain) | ||
|
||
# In general, the "actual target" is the target. | ||
# Here we deal with the edge case where the target | ||
# is part of the replacement block and has been adapted by | ||
# another adapter after this one. For instance, this is the | ||
# case when stacking Controlnets. | ||
actual_target = lookup_top_adapter(self, self.target) | ||
|
||
if (parent := self.parent) is None: | ||
if isinstance(actual_target, fl.ContextModule): | ||
actual_target._set_parent(None) # type: ignore[reportPrivateUsage] | ||
else: | ||
parent.replace(old_module=self, new_module=actual_target) | ||
|
||
def _pre_structural_copy(self) -> None: | ||
if isinstance(self.target, fl.Chain): | ||
raise RuntimeError("Chain adapters typically cannot be copied, eject them first.") | ||
|
||
def _post_structural_copy(self: TAdapter, source: TAdapter) -> None: | ||
self._target = [source.target] | ||
|
||
|
||
def lookup_top_adapter(top: fl.Chain, target: fl.Module) -> fl.Module: | ||
"""Lookup and return last adapter in parents tree (or target if none).""" | ||
|
||
target_parent = top.find_parent(target) | ||
if (target_parent is None) or (target_parent == top): | ||
return target | ||
|
||
r, p = target, target_parent | ||
while p != top: | ||
if isinstance(p, Adapter): | ||
r = p | ||
assert p.parent, f"parent tree of {top} is broken" | ||
p = p.parent | ||
return r |
Oops, something went wrong.