Introduce weight util for shared methods
This commit is contained in:
parent
af41b08d90
commit
5ad5352481
@ -6,7 +6,7 @@ from mlx.utils import tree_flatten
|
|||||||
from mlx.utils import tree_unflatten
|
from mlx.utils import tree_unflatten
|
||||||
from safetensors import safe_open
|
from safetensors import safe_open
|
||||||
|
|
||||||
from flux_1.weights.weight_handler import WeightHandler
|
from flux_1.weights.weight_util import WeightUtil
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -86,8 +86,8 @@ class LoraUtil:
|
|||||||
def _lora_transformer(lora_file: Path) -> (dict, int):
|
def _lora_transformer(lora_file: Path) -> (dict, int):
|
||||||
quantization_level = safe_open(lora_file, framework="pt").metadata().get("quantization_level")
|
quantization_level = safe_open(lora_file, framework="pt").metadata().get("quantization_level")
|
||||||
weights = list(mx.load(str(lora_file)).items())
|
weights = list(mx.load(str(lora_file)).items())
|
||||||
weights = [WeightHandler.reshape_weights(k, v) for k, v in weights]
|
weights = [WeightUtil.reshape_weights(k, v) for k, v in weights]
|
||||||
weights = WeightHandler.flatten(weights)
|
weights = WeightUtil.flatten(weights)
|
||||||
unflatten = tree_unflatten(weights)
|
unflatten = tree_unflatten(weights)
|
||||||
for block in unflatten["transformer"]["transformer_blocks"]:
|
for block in unflatten["transformer"]["transformer_blocks"]:
|
||||||
block["ff"] = {
|
block["ff"] = {
|
||||||
|
|||||||
@ -5,8 +5,8 @@ from huggingface_hub import snapshot_download
|
|||||||
from mlx.utils import tree_unflatten
|
from mlx.utils import tree_unflatten
|
||||||
from safetensors import safe_open
|
from safetensors import safe_open
|
||||||
|
|
||||||
from flux_1.config.config import Config
|
|
||||||
from flux_1.weights.lora_util import LoraUtil
|
from flux_1.weights.lora_util import LoraUtil
|
||||||
|
from flux_1.weights.weight_util import WeightUtil
|
||||||
|
|
||||||
|
|
||||||
class WeightHandler:
|
class WeightHandler:
|
||||||
@ -117,22 +117,11 @@ class WeightHandler:
|
|||||||
return tree_unflatten(weights), quantization_level
|
return tree_unflatten(weights), quantization_level
|
||||||
|
|
||||||
# Huggingface weights needs to be reshaped
|
# Huggingface weights needs to be reshaped
|
||||||
weights = [WeightHandler.reshape_weights(k, v) for k, v in weights]
|
weights = [WeightUtil.reshape_weights(k, v) for k, v in weights]
|
||||||
weights = WeightHandler.flatten(weights)
|
weights = WeightUtil.flatten(weights)
|
||||||
unflatten = tree_unflatten(weights)
|
unflatten = tree_unflatten(weights)
|
||||||
return unflatten, quantization_level
|
return unflatten, quantization_level
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def flatten(params):
|
|
||||||
return [(k, v) for p in params for (k, v) in p]
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def reshape_weights(key, value):
|
|
||||||
if len(value.shape) == 4:
|
|
||||||
value = value.transpose(0, 2, 3, 1)
|
|
||||||
value = value.reshape(-1).reshape(value.shape).astype(Config.precision)
|
|
||||||
return [(key, value)]
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _download_or_get_cached_weights(repo_id: str) -> Path:
|
def _download_or_get_cached_weights(repo_id: str) -> Path:
|
||||||
return Path(
|
return Path(
|
||||||
|
|||||||
15
src/flux_1/weights/weight_util.py
Normal file
15
src/flux_1/weights/weight_util.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from flux_1.config.config import Config
|
||||||
|
|
||||||
|
|
||||||
|
class WeightUtil:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def flatten(params):
|
||||||
|
return [(k, v) for p in params for (k, v) in p]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def reshape_weights(key, value):
|
||||||
|
if len(value.shape) == 4:
|
||||||
|
value = value.transpose(0, 2, 3, 1)
|
||||||
|
value = value.reshape(-1).reshape(value.shape).astype(Config.precision)
|
||||||
|
return [(key, value)]
|
||||||
Loading…
Reference in New Issue
Block a user