Skip to content

Commit

Permalink
NeuronModel API modification done
Browse files Browse the repository at this point in the history
  • Loading branch information
JingyaHuang committed Feb 13, 2025
1 parent 5a6e899 commit 20dd1c8
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 262 deletions.
2 changes: 1 addition & 1 deletion optimum/commands/export/neuronx.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def parse_args_neuronx(parser: "ArgumentParser"):
type=float,
help="Scaling factors for the IP-Adapters.",
)

# Static Input Shapes
input_group = parser.add_argument_group("Input shapes")
doc_input = "that the Neuronx-cc compiler exported model will be able to take as input."
Expand Down
6 changes: 3 additions & 3 deletions optimum/exporters/neuron/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
DIFFUSION_MODEL_VAE_ENCODER_NAME,
ENCODER_NAME,
NEURON_FILE_NAME,
LoRAAdapterArguments,
IPAdapterArguments,
ImageEncoderArguments,
InputShapesArguments,
IPAdapterArguments,
LoRAAdapterArguments,
is_neuron_available,
is_neuronx_available,
is_transformers_neuronx_available,
Expand Down Expand Up @@ -298,7 +298,7 @@ def infer_stable_diffusion_shapes_from_diffusers(
hidden_size=model.image_encoder.vision_model.embeddings.position_embedding.weight.shape[1],
projection_dim=getattr(model.image_encoder.config, "projection_dim", None),
)

# Format with `InputShapesArguments`
for sub_model_name in input_shapes.keys():
input_shapes[sub_model_name] = InputShapesArguments(**input_shapes[sub_model_name])
Expand Down
6 changes: 3 additions & 3 deletions optimum/exporters/neuron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from optimum.utils import logging

from ...exporters.base import ExportConfig
from ...neuron.utils import InputShapesArguments, ImageEncoderArguments, is_neuron_available
from ...neuron.utils import ImageEncoderArguments, InputShapesArguments, is_neuron_available


if TYPE_CHECKING:
Expand Down Expand Up @@ -192,8 +192,8 @@ def __init__(
"vae_scale_factor": input_shapes.vae_scale_factor,
"encoder_hidden_size": input_shapes.encoder_hidden_size,
"image_encoder_shapes": ImageEncoderArguments(
sequence_length=getattr(input_shapes.image_encoder_shapes, "sequence_length", None),
hidden_size=getattr(input_shapes.image_encoder_shapes, "hidden_size", None),
sequence_length=getattr(input_shapes.image_encoder_shapes, "sequence_length", None),
hidden_size=getattr(input_shapes.image_encoder_shapes, "hidden_size", None),
projection_dim=getattr(input_shapes.image_encoder_shapes, "projection_dim", None),
),
}
Expand Down
6 changes: 0 additions & 6 deletions optimum/exporters/neuron/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,6 @@ def export_models(
failed_models = []
total_compilation_time = 0
compile_configs = {}
models_and_neuron_configs.pop("text_encoder")
# models_and_neuron_configs.pop("text_encoder_2")
models_and_neuron_configs.pop("unet")
models_and_neuron_configs.pop("vae_encoder")
models_and_neuron_configs.pop("vae_decoder")
# models_and_neuron_configs.pop("image_encoder")
for i, model_name in enumerate(models_and_neuron_configs.keys()):
logger.info(f"***** Compiling {model_name} *****")
submodel, sub_neuron_config = models_and_neuron_configs[model_name]
Expand Down
2 changes: 1 addition & 1 deletion optimum/exporters/neuron/model_configs/traced_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def inputs(self) -> List[str]:
if self.image_encoder_output_hidden_states:
common_inputs += ["image_enc_hidden_states"]
else:
common_inputs += ["image_embeds"]
common_inputs += ["image_embeds"]

return common_inputs

Expand Down
8 changes: 4 additions & 4 deletions optimum/exporters/neuron/model_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ def forward(self, input_ids, attention_mask):

class CLIPVisionModelNeuronWrapper(torch.nn.Module):
def __init__(
self,
model,
self,
model,
input_names: List[str],
output_hidden_states: bool = True,
):
Expand All @@ -585,9 +585,9 @@ def forward(self, pixel_values):
vision_outputs = self.model.vision_model(pixel_values=pixel_values, output_hidden_states=self.output_hidden_states)
pooled_output = vision_outputs[1]
image_embeds = self.model.visual_projection(pooled_output)

outputs = (image_embeds, vision_outputs.last_hidden_state)

if self.output_hidden_states:
outputs += (vision_outputs.hidden_states, )
return outputs
Expand Down
8 changes: 4 additions & 4 deletions optimum/exporters/neuron/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
import os
from collections import OrderedDict
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union, Any
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union

import torch

from ...neuron.utils import (
LoRAAdapterArguments,
DECODER_NAME,
DIFFUSION_MODEL_CONTROLNET_NAME,
DIFFUSION_MODEL_TEXT_ENCODER_2_NAME,
Expand All @@ -33,6 +32,7 @@
DIFFUSION_MODEL_VAE_DECODER_NAME,
DIFFUSION_MODEL_VAE_ENCODER_NAME,
ENCODER_NAME,
LoRAAdapterArguments,
get_attention_scores_sd,
get_attention_scores_sdxl,
neuron_scaled_dot_product_attention,
Expand Down Expand Up @@ -235,7 +235,7 @@ def get_diffusion_models_for_export(
unet_neuron_config.with_ip_adapter = getattr(unet.config, "encoder_hid_dim_type", None) == "ip_image_proj"

models_for_export[DIFFUSION_MODEL_UNET_NAME] = (unet, unet_neuron_config)


# Diffusion Transformer
transformer = None
Expand Down Expand Up @@ -340,7 +340,7 @@ def get_diffusion_models_for_export(
input_shapes=image_encoder_input_shapes,
)
models_for_export["image_encoder"] = (image_encoder, image_encoder_neuron_config)
models_for_export[DIFFUSION_MODEL_UNET_NAME][1].image_encoder_output_hidden_states = output_hidden_states
models_for_export[DIFFUSION_MODEL_UNET_NAME][1].image_encoder_output_hidden_states = output_hidden_states

return models_for_export

Expand Down
40 changes: 13 additions & 27 deletions optimum/neuron/modeling_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import shutil
from abc import abstractmethod
from collections import OrderedDict
from dataclasses import asdict
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Tuple, Union
Expand Down Expand Up @@ -52,6 +53,7 @@
DIFFUSION_MODEL_VAE_ENCODER_NAME,
NEURON_FILE_NAME,
DiffusersPretrainedConfig,
NeuronArgumentParser,
check_if_weights_replacable,
is_neuronx_available,
replace_weights,
Expand Down Expand Up @@ -877,16 +879,8 @@ def _export(
dynamic_batch_size: bool = False,
output_hidden_states: bool = False,
data_parallel_mode: Optional[Literal["none", "unet", "transformer", "all"]] = None,
lora_model_ids: Optional[Union[str, List[str]]] = None,
lora_weight_names: Optional[Union[str, List[str]]] = None,
lora_adapter_names: Optional[Union[str, List[str]]] = None,
lora_scales: Optional[Union[float, List[float]]] = None,
controlnet_ids: Optional[Union[str, List[str]]] = None,
ip_adapter_ids: Optional[Union[str, List[str]]] = None,
ip_adapter_subfolders: Optional[Union[str, List[str]]] = None,
ip_adapter_weight_names: Optional[Union[str, List[str]]] = None,
ip_adapter_scales: Optional[Union[float, List[float]]] = None,
**kwargs_shapes,
**kwargs,
) -> "NeuronDiffusionPipelineBase":
"""
Args:
Expand Down Expand Up @@ -967,9 +961,13 @@ def _export(
The name of the weight file to load. If a list is passed, it should have the same length as `ip_adapter_subfolders`.
ip_adapter_scales (`Optional[Union[float, List[float]]]`, defaults to `None`):
Scaling factors for the IP-Adapters.
kwargs_shapes (`Dict[str, int]`):
Shapes to use during inference. This argument allows to override the default shapes used during the export.
"""
# Parse kwargs to their dataclass
parser = NeuronArgumentParser(**kwargs)
lora_args = parser.lora_args
ip_adapter_args = parser.ip_adapter_args
kwargs_shapes = asdict(parser.input_shapes)

if task is None:
if cls.task is not None:
task = cls.task
Expand Down Expand Up @@ -1024,17 +1022,11 @@ def _export(
local_files_only=local_files_only,
token=token,
submodels=submodels,
lora_args=lora_args,
ip_adapter_args=ip_adapter_args,
output_hidden_states=output_hidden_states,
lora_model_ids=lora_model_ids,
lora_weight_names=lora_weight_names,
lora_adapter_names=lora_adapter_names,
lora_scales=lora_scales,
torch_dtype=torch_dtype,
controlnet_ids=controlnet_ids,
ip_adapter_ids=ip_adapter_ids,
ip_adapter_subfolders=ip_adapter_subfolders,
ip_adapter_weight_names=ip_adapter_weight_names,
ip_adapter_scales=ip_adapter_scales,
**input_shapes_copy,
)

Expand Down Expand Up @@ -1090,6 +1082,8 @@ def _export(
model_name_or_path=model_id,
output=save_dir_path,
compiler_kwargs=compiler_kwargs,
lora_args=lora_args,
ip_adapter_args=ip_adapter_args,
torch_dtype=torch_dtype,
task=task,
dynamic_batch_size=dynamic_batch_size,
Expand All @@ -1107,15 +1101,7 @@ def _export(
do_validation=False,
submodels={"unet": unet_id},
output_hidden_states=output_hidden_states,
lora_model_ids=lora_model_ids,
lora_weight_names=lora_weight_names,
lora_adapter_names=lora_adapter_names,
lora_scales=lora_scales,
controlnet_ids=controlnet_ids,
ip_adapter_ids=ip_adapter_ids,
ip_adapter_subfolders=ip_adapter_subfolders,
ip_adapter_weight_names=ip_adapter_weight_names,
ip_adapter_scales=ip_adapter_scales,
library_name=cls.library_name,
**input_shapes,
)
Expand Down
2 changes: 1 addition & 1 deletion optimum/neuron/modeling_traced.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from ..exporters.tasks import TasksManager
from .modeling_base import NeuronModel
from .utils import (
InputShapesArguments,
NEURON_FILE_NAME,
InputShapesArguments,
check_if_weights_replacable,
is_neuron_available,
replace_weights,
Expand Down
10 changes: 6 additions & 4 deletions optimum/neuron/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"IPAdapterArguments",
"ImageEncoderArguments",
"InputShapesArguments",
"convert_neuronx_compiler_args_to_neuron",
"NeuronArgumentParser",
"convert_neuronx_compiler_args_to_neuron",
"store_compilation_config"
],
"constant": [
Expand Down Expand Up @@ -91,11 +92,12 @@

if TYPE_CHECKING:
from .argument_utils import (
LoRAAdapterArguments,
IPAdapterArguments,
ImageEncoderArguments,
InputShapesArguments,
convert_neuronx_compiler_args_to_neuron,
IPAdapterArguments,
LoRAAdapterArguments,
NeuronArgumentParser,
convert_neuronx_compiler_args_to_neuron,
store_compilation_config,
)
from .constant import (
Expand Down
42 changes: 40 additions & 2 deletions optimum/neuron/utils/argument_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Utilities related to CLI arguments."""

import os
from dataclasses import dataclass, is_dataclass, asdict
from dataclasses import asdict, dataclass, fields, is_dataclass
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union

from ...utils import logging
Expand All @@ -36,7 +36,7 @@ class LoRAAdapterArguments:
weight_names: Optional[Union[str, List[str]]] = None
adapter_names: Optional[Union[str, List[str]]] = None
scales: Optional[Union[float, List[float]]] = None

def __post_init__(self):
if isinstance(self.model_ids, str):
self.model_ids = [self.model_ids,]
Expand Down Expand Up @@ -86,6 +86,44 @@ class InputShapesArguments:
image_encoder_shapes: Optional[ImageEncoderArguments] = None


class DataclassParser:
def __init__(self, **kwargs):
for name, cls in self.__class__.__annotations__.items():
if is_dataclass(cls):
parsed_kwargs = {k: v for k, v in kwargs.items() if k in {f.name for f in fields(cls)}}
setattr(self, f"{name}", cls(**parsed_kwargs))


class NeuronArgumentParser(DataclassParser):
input_shapes: InputShapesArguments

def __init__(self, **kwargs):
super().__init__(**kwargs)
for name, value in kwargs.items():
if value is not None:
setattr(self, name, value)

@property
def lora_args(self):
_lora_args = LoRAAdapterArguments(
model_ids=getattr(self, "lora_model_ids", None),
weight_names=getattr(self, "lora_weight_names", None),
adapter_names=getattr(self, "lora_adapter_names", None),
scales=getattr(self, "lora_scales", None),
)
return _lora_args

@property
def ip_adapter_args(self):
_ip_adapter_args = IPAdapterArguments(
model_id=getattr(self, "ip_adapter_id", None),
subfolder=getattr(self, "ip_adapter_subfolder", None),
weight_name=getattr(self, "ip_adapter_weight_name", None),
scale=getattr(self, "ip_adapter_scale", None),
)
return _ip_adapter_args


def validate_arg(
args,
arg_name: str,
Expand Down
4 changes: 2 additions & 2 deletions optimum/neuron/utils/input_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
"""Dummy input generation classes."""

from typing import Optional, TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

import torch

Expand Down Expand Up @@ -195,7 +195,7 @@ def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int
if input_name == "image_enc_hidden_states":
shape = [self.batch_size, 1, self.image_encoder_shapes.sequence_length, self.image_encoder_shapes.hidden_size]
return self.random_float_tensor(shape, framework=framework, dtype=float_dtype)
elif input_name == "image_embeds":
elif input_name == "image_embeds":
shape = [self.batch_size, 1, self.image_encoder_shapes.projection_dim]
return self.random_float_tensor(shape, framework=framework, dtype=float_dtype)
elif input_name == "ip_adapter_masks":
Expand Down
Loading

0 comments on commit 20dd1c8

Please sign in to comment.