diff --git a/.github/workflows/script/models/summary.py b/.github/workflows/script/models/summary.py index 9e5d132e310..da9ce307dee 100644 --- a/.github/workflows/script/models/summary.py +++ b/.github/workflows/script/models/summary.py @@ -47,7 +47,7 @@ def main(): def parse_tuning_log(line, url_dict): - """Parsing {Framework}-{Model}-tune.log to get tuning result""" + """Parsing {Framework}-{Model}-tune.log to get tuning result.""" result = line.split(";") OS, Platform, Framework, Version, Model, Strategy, Tune_time, Tuning_trials, URL, __ = result file_name = f"{Framework}-{Model}-tune.log" @@ -57,7 +57,7 @@ def parse_tuning_log(line, url_dict): def parse_summary_log(line, url_dict): - """Parse {Framework}-{Model}-tune.log to get benchmarking accuracy result""" + """Parse {Framework}-{Model}-tune.log to get benchmarking accuracy result.""" result = line.split(";") OS, Platform, Framework, Version, Precision, Model, Mode, Type, BS, Value, Url = result file_name = f"{Framework}-{Model}-tune.log" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2abf210ce9b..d0ad7b779a1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,11 +45,10 @@ repos: rev: v2.2.6 hooks: - id: codespell - args: - [-w, --ignore-words=.github/workflows/script/formatScan/nlp_dict.txt] + args: [-w, --ignore-words=.github/workflows/script/formatScan/nlp_dict.txt] exclude: | (?x)^( - .+.po|.+.ts|.+.js|.+.map|.+.js.map|.+.css.map| + .+.po|.+.map|.+.js.map|.+.css.map| .github/workflows/script/formatScan/nlp_dict.txt| docker/third_party_programs_docker.txt| intel_extension_for_transformers/transformers/llm/evaluation/hf_eval/datasets/cnn_validation.json| @@ -61,14 +60,12 @@ repos: )$ - repo: https://github.com/Lucas-C/pre-commit-hooks - rev: v1.5.4 + rev: v1.5.5 hooks: - id: insert-license files: | (?x)^( - intel_extension_for_transformers/.*(py|yaml|yml|sh)| - workflows/.*(py|yaml|yml|sh)| - tests/.*(py|yaml|yml|sh) + (intel_extension_for_transformers|workflows|tests)/.*(py|yaml|yml|sh) )$ args: [ @@ -77,64 +74,105 @@ repos: --detect-license-in-X-top-lines=40, --skip-license-insertion-comment=Copyright, ] -# - repo: https://github.com/asottile/yesqa -# rev: v1.5.0 -# hooks: -# - id: yesqa -# name: Unused noqa -# -# - repo: https://github.com/pycqa/isort -# rev: 5.13.2 -# hooks: -# - id: isort -# exclude: | -# (?x)^( -# examples/.+ -# )$ -# -# - repo: https://github.com/PyCQA/docformatter -# rev: v1.7.5 -# hooks: -# - id: docformatter -# args: [ -# --in-place, -# --wrap-summaries=0, # 0 means disable wrap -# --wrap-descriptions=0, # 0 means disable wrap -# --black, -# --style=google, -# ] -# exclude: | -# (?x)^( -# examples/.+ -# )$ -# -# - repo: https://github.com/psf/black.git -# rev: 23.12.1 -# hooks: -# - id: black -# files: (.*\.py)$ -# exclude: | -# (?x)^( -# examples/.+ -# )$ -# -# - repo: https://github.com/asottile/blacken-docs -# rev: 1.16.0 -# hooks: -# - id: blacken-docs -# args: [--line-length=120, --skip-errors] -# exclude: | -# (?x)^( -# examples/.+| -# docs/source-app -# )$ -# -# - repo: https://github.com/astral-sh/ruff-pre-commit -# rev: v0.1.9 -# hooks: -# - id: ruff -# args: [--fix, --exit-non-zero-on-fix, --no-cache] -# exclude: | -# (?x)^( -# examples/.+ -# )$ + - id: insert-license + files: | + (?x)^( + (intel_extension_for_transformers|workflows|tests)/.*(ts|js|cpp|hpp) + )$ + args: + [ + --license-filepath=.github/license_template.txt, + --use-current-year, + --detect-license-in-X-top-lines=40, + --skip-license-insertion-comment=Copyright, + --comment-style=//, + ] + - id: insert-license + files: | + (?x)^( + (intel_extension_for_transformers|workflows|tests)/.*(html|svelte) + )$ + args: + [ + --license-filepath=.github/license_template.txt, + --use-current-year, + --detect-license-in-X-top-lines=40, + --skip-license-insertion-comment=Copyright, + --comment-style=, + ] + + - repo: https://github.com/asottile/yesqa + rev: v1.5.0 + hooks: + - id: yesqa + name: Unused noqa + + # - repo: https://github.com/pycqa/isort + # rev: 5.13.2 + # hooks: + # - id: isort + # args: [--profile=black, --line-length=120] + # exclude: | + # (?x)^( + # examples/.+| + # .*\__init__.py + # )$ + + # - repo: https://github.com/pre-commit/mirrors-prettier + # rev: "v4.0.0-alpha.8" # Use the sha / tag you want to point at + # hooks: + # - id: prettier + # args: [--print-width=120] + # types_or: [yaml, html, css, scss, javascript, json, ts] + # additional_dependencies: + # - prettier@3.2.5 + + - repo: https://github.com/PyCQA/docformatter + rev: v1.7.5 + hooks: + - id: docformatter + args: [ + --in-place, + --wrap-summaries=0, # 0 means disable wrap + --wrap-descriptions=0, # 0 means disable wrap + --black, + --style=google, + ] + exclude: | + (?x)^( + examples/.+ + )$ + + # - repo: https://github.com/psf/black.git + # rev: 24.3.0 + # hooks: + # - id: black + # files: (.*\.py)$ + # args: [--line-length=120] + # exclude: | + # (?x)^( + # examples/.+ + # )$ + + # - repo: https://github.com/asottile/blacken-docs + # rev: 1.16.0 + # hooks: + # - id: blacken-docs + # args: [--line-length=120, --skip-errors] + # additional_dependencies: + # - black==24.3.0 + # exclude: | + # (?x)^( + # examples/.+| + # docs/source-app + # )$ + + # - repo: https://github.com/astral-sh/ruff-pre-commit + # rev: v0.3.5 + # hooks: + # - id: ruff + # args: [--fix, --exit-non-zero-on-fix, --no-cache] + # exclude: | + # (?x)^( + # examples/.+ + # )$ diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 00000000000..ce232f0468b --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,88 @@ +# Exclude a variety of commonly ignored directories. +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] + +# Same as Black. +line-length = 120 +indent-width = 4 + +# Assume Python 3.10 +target-version = "py310" + +[lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +select = ["E4", "E7", "E9", "F"] +ignore = [ + "E402", # Module level import not at top of file + "E501", # Line too long (121 > 120 characters) + "E721", # Do not compare types, use isinstance() + "E722", # Do not use bare except + "E731", # Do not assign a lambda expression, use a def + "E741", # Do not use variables named ‘l’, ‘O’, or ‘I’ + "F401", # {name} imported but unused + "F403", # from {name} import * used; unable to detect undefined names + "F405", # {name} may be undefined, or defined from star imports + "F841", # Local variable is assigned to but never used{name} +] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" + +# Enable auto-formatting of code examples in docstrings. Markdown, +# reStructuredText code/literal blocks and doctests are all supported. +# +# This is currently disabled by default, but it is planned for this +# to be opt-out in the future. +docstring-code-format = false + +# Set the line length limit used when formatting code snippets in +# docstrings. +# +# This only has an effect when the `docstring-code-format` setting is +# enabled. +docstring-code-line-length = "dynamic" diff --git a/docs/tutorials/pytorch/question-answering/benchmark.py b/docs/tutorials/pytorch/question-answering/benchmark.py index b98b4f5ef39..1f987a8ac9b 100644 --- a/docs/tutorials/pytorch/question-answering/benchmark.py +++ b/docs/tutorials/pytorch/question-answering/benchmark.py @@ -127,8 +127,7 @@ def postprocess_qa_predictions( prefix: Optional[str] = None, log_level: Optional[int] = logging.WARNING, ): - """ - Post-processes the predictions of a question-answering model to convert them to answers that are substrings of the + """Post-processes the predictions of a question-answering model to convert them to answers that are substrings of the original contexts. This is the base postprocessing functions for models that only return start and end logits. Args: diff --git a/docs/tutorials/pytorch/text-classification/models/bert/configuration_bert.py b/docs/tutorials/pytorch/text-classification/models/bert/configuration_bert.py index 6445117004a..d707799757e 100644 --- a/docs/tutorials/pytorch/text-classification/models/bert/configuration_bert.py +++ b/docs/tutorials/pytorch/text-classification/models/bert/configuration_bert.py @@ -13,8 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -""" BERT model configuration for SWEET""" +"""BERT model configuration for SWEET.""" from collections import OrderedDict from typing import Mapping diff --git a/docs/tutorials/pytorch/text-classification/models/bert/modeling_bert.py b/docs/tutorials/pytorch/text-classification/models/bert/modeling_bert.py index 1e8de683072..b416a8f2e69 100755 --- a/docs/tutorials/pytorch/text-classification/models/bert/modeling_bert.py +++ b/docs/tutorials/pytorch/text-classification/models/bert/modeling_bert.py @@ -730,10 +730,8 @@ def forward(self, sequence_output, pooled_output): class BertPreTrainedModel(PreTrainedModel): - """ - An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained - models. - """ + """An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained + models.""" config_class = BertConfig load_tf_weights = load_tf_weights_in_bert @@ -742,7 +740,7 @@ class BertPreTrainedModel(PreTrainedModel): _keys_to_ignore_on_load_missing = [r"position_ids"] def _init_weights(self, module): - """Initialize the weights""" + """Initialize the weights.""" if isinstance(module, nn.Linear): # Slightly different from the TF version which uses truncated_normal for initialization # cf https://github.com/pytorch/pytorch/pull/5617 @@ -764,8 +762,7 @@ def _set_gradient_checkpointing(self, module, value=False): @dataclass class BertForPreTrainingOutput(ModelOutput): - """ - Output type of [`BertForPreTraining`]. + """Output type of [`BertForPreTraining`]. Args: loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`): @@ -898,8 +895,9 @@ def set_input_embeddings(self, value): self.embeddings.word_embeddings = value def _prune_heads(self, heads_to_prune): - """ - Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base + """Prunes heads of the model. + + heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel """ for layer, heads in heads_to_prune.items(): @@ -931,8 +929,7 @@ def forward( start_idx: int = 0, exit_idx: int = -1, ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: - r""" - encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + r"""encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if the model is configured as a decoder. encoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1089,8 +1086,7 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], BertForPreTrainingOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + r"""Labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` @@ -1207,8 +1203,7 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: - r""" - encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + r"""encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if the model is configured as a decoder. encoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1344,11 +1339,11 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., - config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the - loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` + r"""Labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + + Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., + config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the + loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict @@ -1433,8 +1428,7 @@ def forward( return_dict: Optional[bool] = None, **kwargs, ) -> Union[Tuple[torch.Tensor], NextSentencePredictorOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + r"""Labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair (see `input_ids` docstring). Indices should be in `[0, 1]`: @@ -1597,11 +1591,11 @@ def forward( return_dict: Optional[bool] = None, exit_threshold=None, ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): - Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., - config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If - `config.num_labels > 1` a classification loss is computed (Cross-Entropy). + r"""Labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + + Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., + config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If + `config.num_labels > 1` a classification loss is computed (Cross-Entropy). """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict hidden_state = None @@ -1737,11 +1731,11 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): - Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., - num_choices-1]` where `num_choices` is the size of the second dimension of the input tensors. (See - `input_ids` above) + r"""Labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + + Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., + num_choices-1]` where `num_choices` is the size of the second dimension of the input tensors. (See + `input_ids` above) """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict num_choices = input_ids.shape[1] if input_ids is not None else inputs_embeds.shape[1] @@ -1837,9 +1831,9 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. + r"""Labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + + Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict @@ -1922,9 +1916,9 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: - r""" - start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): - Labels for position (index) of the start of the labelled span for computing the token classification loss. + r"""start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + + Labels for position (index) of the start of the labelled span for computing the token classification loss. Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence are not taken into account for computing the loss. end_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/docs/tutorials/pytorch/text-classification/models/deberta/configuration_deberta.py b/docs/tutorials/pytorch/text-classification/models/deberta/configuration_deberta.py index 6724ce44d98..7bd70867cfd 100644 --- a/docs/tutorials/pytorch/text-classification/models/deberta/configuration_deberta.py +++ b/docs/tutorials/pytorch/text-classification/models/deberta/configuration_deberta.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -""" DeBERTa model configuration for SWEET""" +"""DeBERTa model configuration for SWEET.""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union diff --git a/docs/tutorials/pytorch/text-classification/models/deberta/modeling_deberta.py b/docs/tutorials/pytorch/text-classification/models/deberta/modeling_deberta.py index 0e6ab8667fd..e6732519e39 100644 --- a/docs/tutorials/pytorch/text-classification/models/deberta/modeling_deberta.py +++ b/docs/tutorials/pytorch/text-classification/models/deberta/modeling_deberta.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -""" PyTorch DeBERTa model for SWEET.""" +"""PyTorch DeBERTa model for SWEET.""" import math from collections.abc import Sequence @@ -77,8 +77,7 @@ def output_dim(self): class XSoftmax(torch.autograd.Function): - """ - Masked Softmax which is optimized for saving memory + """Masked Softmax which is optimized for saving memory. Args: input (`torch.tensor`): The input tensor that will apply softmax. @@ -102,7 +101,8 @@ class XSoftmax(torch.autograd.Function): >>> dim = -1 >>> y = XSoftmax.apply(x, mask, dim) - ```""" + ``` + """ @staticmethod def forward(self, input, mask, dim): @@ -189,8 +189,7 @@ def backward(ctx, grad_output): class StableDropout(nn.Module): - """ - Optimized dropout module for stabilizing the training + """Optimized dropout module for stabilizing the training. Args: drop_prob (float): the dropout probabilities @@ -203,8 +202,7 @@ def __init__(self, drop_prob): self.context_stack = None def forward(self, x): - """ - Call the module + """Call the module. Args: x (`torch.tensor`): The input tensor to apply dropout @@ -373,7 +371,7 @@ def forward( class DebertaEncoder(nn.Module): - """Modified BertEncoder with relative position bias support""" + """Modified BertEncoder with relative position bias support.""" def __init__(self, config): super().__init__() @@ -486,8 +484,7 @@ def custom_forward(*inputs): def build_relative_position(query_size, key_size, device): - """ - Build relative position according to the query and key + """Build relative position according to the query and key. We assume the absolute position of query \\(P_q\\) is range from (0, query_size) and the absolute position of key \\(P_k\\) is range from (0, key_size), The relative positions from query to key is \\(R_{q \\rightarrow k} = P_q - @@ -499,7 +496,6 @@ def build_relative_position(query_size, key_size, device): Return: `torch.LongTensor`: A tensor with shape [1, query_size, key_size] - """ q_ids = torch.arange(query_size, dtype=torch.long, device=device) @@ -526,14 +522,12 @@ def pos_dynamic_expand(pos_index, p2c_att, key_layer): class DisentangledSelfAttention(nn.Module): - """ - Disentangled self-attention module + """Disentangled self-attention module. Parameters: config (`str`): A model config class instance with the configuration to build a new model. The schema is similar to *BertConfig*, for more details, please refer [`DebertaConfig`] - """ def __init__(self, config): @@ -585,8 +579,7 @@ def forward( relative_pos=None, rel_embeddings=None, ): - """ - Call the module + """Call the module. Args: hidden_states (`torch.FloatTensor`): @@ -611,8 +604,6 @@ def forward( rel_embeddings (`torch.FloatTensor`): The embedding of relative distances. It's a tensor of shape [\\(2 \\times \\text{max_relative_positions}\\), *hidden_size*]. - - """ if query_states is None: qp = self.in_proj(hidden_states) # .split(self.all_head_size, dim=-1) @@ -794,10 +785,8 @@ def forward(self, input_ids=None, token_type_ids=None, position_ids=None, mask=N class DebertaPreTrainedModel(PreTrainedModel): - """ - An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained - models. - """ + """An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained + models.""" config_class = DebertaConfig base_model_prefix = "deberta" @@ -906,8 +895,9 @@ def set_input_embeddings(self, new_embeddings): self.embeddings.word_embeddings = new_embeddings def _prune_heads(self, heads_to_prune): - """ - Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base + """Prunes heads of the model. + + heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel """ raise NotImplementedError("The prune function is not implemented in DeBERTa model.") @@ -1045,11 +1035,11 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple, MaskedLMOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., - config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the - loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` + r"""Labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + + Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., + config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the + loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict @@ -1242,11 +1232,11 @@ def forward( return_dict: Optional[bool] = None, exit_threshold=None, ) -> Union[Tuple, SequenceClassifierOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): - Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., - config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If - `config.num_labels > 1` a classification loss is computed (Cross-Entropy). + r"""Labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + + Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., + config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If + `config.num_labels > 1` a classification loss is computed (Cross-Entropy). """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict hidden_state = None @@ -1381,9 +1371,9 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple, TokenClassifierOutput]: - r""" - labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. + r"""Labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + + Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict @@ -1457,9 +1447,9 @@ def forward( output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple, QuestionAnsweringModelOutput]: - r""" - start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): - Labels for position (index) of the start of the labelled span for computing the token classification loss. + r"""start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): + + Labels for position (index) of the start of the labelled span for computing the token classification loss. Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence are not taken into account for computing the loss. end_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/docs/tutorials/pytorch/text-classification/utils/temperature_scaling.py b/docs/tutorials/pytorch/text-classification/utils/temperature_scaling.py index 1e967fc02f2..d1ecee57ac0 100644 --- a/docs/tutorials/pytorch/text-classification/utils/temperature_scaling.py +++ b/docs/tutorials/pytorch/text-classification/utils/temperature_scaling.py @@ -33,12 +33,12 @@ class ModelWithTemperature(nn.Module): - """ - A thin decorator, which wraps a model with temperature scaling + """A thin decorator, which wraps a model with temperature scaling model (nn.Module): - A classification neural network - NB: Output of the neural network should be the classification logits, - NOT the softmax (or log softmax)! + + A classification neural network + NB: Output of the neural network should be the classification logits, + NOT the softmax (or log softmax)! """ def __init__(self, model): @@ -51,17 +51,15 @@ def forward(self, input): return self.temperature_scale(logits) def temperature_scale(self, logits): - """ - Perform temperature scaling on logits - """ + """Perform temperature scaling on logits.""" # Expand temperature to match the size of logits temperature = self.temperature.unsqueeze(1).expand(logits.size(0), logits.size(1)) return logits / temperature def set_temperature(model, n_layers, data_iterator=None, cuda_device=0, is_sst=False): - """ - Tune the tempearature of the model (using the validation set). + """Tune the tempearature of the model (using the validation set). + We're going to set it to optimize NLL. valid_loader (DataLoader): validation set loader """ @@ -249,8 +247,7 @@ def optimize(self, temp_model: ModelWithTemperature, lr: float, class _ECELoss(nn.Module): - """ - Calculates the Expected Calibration Error of a model. + """Calculates the Expected Calibration Error of a model. (This isn't necessary for temperature scaling, just a cool metric). The input to this loss is the logits of a model, NOT the softmax scores. @@ -269,9 +266,7 @@ class _ECELoss(nn.Module): """ def __init__(self, n_bins=15): - """ - n_bins (int): number of confidence interval bins - """ + """n_bins (int): number of confidence interval bins.""" super(_ECELoss, self).__init__() bin_boundaries = torch.linspace(0, 1, n_bins + 1) self.bin_lowers = bin_boundaries[:-1] diff --git a/intel_extension_for_transformers/langchain/langchain_community/embeddings/__init__.py b/intel_extension_for_transformers/langchain/langchain_community/embeddings/__init__.py index 95fc2cacabb..b8183f5f55f 100644 --- a/intel_extension_for_transformers/langchain/langchain_community/embeddings/__init__.py +++ b/intel_extension_for_transformers/langchain/langchain_community/embeddings/__init__.py @@ -19,4 +19,4 @@ HuggingFaceEmbeddings, HuggingFaceBgeEmbeddings, HuggingFaceInstructEmbeddings -) +) diff --git a/intel_extension_for_transformers/langchain/langchain_community/embeddings/embeddings.py b/intel_extension_for_transformers/langchain/langchain_community/embeddings/embeddings.py index 41cd2ffbf81..e1dbc368224 100644 --- a/intel_extension_for_transformers/langchain/langchain_community/embeddings/embeddings.py +++ b/intel_extension_for_transformers/langchain/langchain_community/embeddings/embeddings.py @@ -64,7 +64,9 @@ class HuggingFaceEmbeddings(langchain_core.pydantic_v1.BaseModel, langchain_core """Model name to use.""" cache_folder: Optional[str] = None """Path to store models. - Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable.""" + + Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable. + """ model_kwargs: Dict[str, Any] = langchain_core.pydantic_v1.Field(default_factory=dict) """Keyword arguments to pass to the model.""" encode_kwargs: Dict[str, Any] = langchain_core.pydantic_v1.Field(default_factory=dict) @@ -150,7 +152,9 @@ class HuggingFaceBgeEmbeddings(langchain_core.pydantic_v1.BaseModel, langchain_c """Model name to use.""" cache_folder: Optional[str] = None """Path to store models. - Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable.""" + + Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable. + """ model_kwargs: Dict[str, Any] = langchain_core.pydantic_v1.Field(default_factory=dict) """Keyword arguments to pass to the model.""" encode_kwargs: Dict[str, Any] = langchain_core.pydantic_v1.Field(default_factory=dict) @@ -234,7 +238,9 @@ class HuggingFaceInstructEmbeddings(langchain_core.pydantic_v1.BaseModel, langch """Model name to use.""" cache_folder: Optional[str] = None """Path to store models. - Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable.""" + + Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable. + """ model_kwargs: Dict[str, Any] = langchain_core.pydantic_v1.Field(default_factory=dict) """Keyword arguments to pass to the model.""" encode_kwargs: Dict[str, Any] = langchain_core.pydantic_v1.Field(default_factory=dict) diff --git a/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_instructor_embedding.py b/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_instructor_embedding.py index 6e8a837127e..9bff81bcc70 100644 --- a/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_instructor_embedding.py +++ b/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_instructor_embedding.py @@ -37,7 +37,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _load_model(self, model_name_or_path, config, cache_dir, **model_args): - """Loads the transformer model""" + """Loads the transformer model.""" self.auto_model = OptimizedModel.from_pretrained(model_name_or_path, config=config, cache_dir=cache_dir, @@ -46,7 +46,7 @@ def _load_model(self, model_name_or_path, config, cache_dir, **model_args): setattr(self.auto_model, "config", config) def forward(self, features): - """Returns token_embeddings, cls_token""" + """Returns token_embeddings, cls_token.""" trans_features = {'input_ids': features['input_ids'], 'attention_mask': features['attention_mask']} if 'token_type_ids' in features: # pragma: no cover trans_features['token_type_ids'] = features['token_type_ids'] diff --git a/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_sentence_transformers.py b/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_sentence_transformers.py index 8e131353fa8..5cddcb8638e 100644 --- a/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_sentence_transformers.py +++ b/intel_extension_for_transformers/langchain/langchain_community/embeddings/optimized_sentence_transformers.py @@ -37,7 +37,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _load_model(self, model_name_or_path, config, cache_dir, **model_args): - """Loads the transformer model""" + """Loads the transformer model.""" self.auto_model = OptimizedModel.from_pretrained(model_name_or_path, config=config, cache_dir=cache_dir, @@ -46,7 +46,7 @@ def _load_model(self, model_name_or_path, config, cache_dir, **model_args): setattr(self.auto_model, "config", config) def forward(self, features): - """Returns token_embeddings, cls_token""" + """Returns token_embeddings, cls_token.""" trans_features = {"input_ids": features["input_ids"], "attention_mask": features["attention_mask"]} if "token_type_ids" in features: trans_features["token_type_ids"] = features["token_type_ids"] @@ -85,9 +85,7 @@ def _load_auto_model( cache_folder: Optional[str], revision: Optional[str] = None, trust_remote_code: bool = False): - """ - Creates a simple Transformer + Mean Pooling model and returns the modules - """ + """Creates a simple Transformer + Mean Pooling model and returns the modules.""" logger.warning("No sentence-transformers model found with name {}." \ "Creating a new one with MEAN pooling.".format(model_name_or_path)) transformer_model = OptimzedTransformer( @@ -109,9 +107,7 @@ def _load_sbert_model( cache_folder: Optional[str], revision: Optional[str] = None, trust_remote_code: bool = False): - """ - Loads a full sentence-transformers model - """ + """Loads a full sentence-transformers model.""" # Check if the config_sentence_transformers.json file exists (exists since v2 of the framework) config_sentence_transformers_json_path = sentence_transformers.util.load_file_path( model_name_or_path, diff --git a/intel_extension_for_transformers/langchain/langchain_community/retrievers/bge_reranker.py b/intel_extension_for_transformers/langchain/langchain_community/retrievers/bge_reranker.py index f4e0e55c5d0..57d873c2659 100644 --- a/intel_extension_for_transformers/langchain/langchain_community/retrievers/bge_reranker.py +++ b/intel_extension_for_transformers/langchain/langchain_community/retrievers/bge_reranker.py @@ -52,8 +52,7 @@ def compress_documents( query: str, callbacks: Optional[Callbacks] = None, ) -> Sequence[Document]: - """ - Compress documents using BAAI/bge-reranker models. + """Compress documents using BAAI/bge-reranker models. Args: documents: A sequence of documents to compress. diff --git a/intel_extension_for_transformers/langchain/langchain_community/retrievers/child_parent_retriever.py b/intel_extension_for_transformers/langchain/langchain_community/retrievers/child_parent_retriever.py index f491026d77f..9a482a75b41 100644 --- a/intel_extension_for_transformers/langchain/langchain_community/retrievers/child_parent_retriever.py +++ b/intel_extension_for_transformers/langchain/langchain_community/retrievers/child_parent_retriever.py @@ -14,8 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""The wrapper for Child-Parent retriever based on langchain""" +"""The wrapper for Child-Parent retriever based on langchain.""" from langchain_core.vectorstores import VectorStore from langchain_core.retrievers import BaseRetriever from langchain_core.pydantic_v1 import Field @@ -46,6 +45,7 @@ class ChildParentRetriever(BaseRetriever): def _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun) -> List[Document]: """Get documents relevant to a query. + Args: query: String to find relevant documents for run_manager: The callbacks handler to use diff --git a/intel_extension_for_transformers/langchain/langchain_community/vectorstores/chroma.py b/intel_extension_for_transformers/langchain/langchain_community/vectorstores/chroma.py index b84be514611..3efc37ddef8 100644 --- a/intel_extension_for_transformers/langchain/langchain_community/vectorstores/chroma.py +++ b/intel_extension_for_transformers/langchain/langchain_community/vectorstores/chroma.py @@ -14,8 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""The wrapper for Chroma retriever based on langchain""" +"""The wrapper for Chroma retriever based on langchain.""" from __future__ import annotations import base64 import logging, os diff --git "a/intel_extension_for_transformers/neural_chat/assets/docs/4th Generation Intel\302\256 Xeon\302\256 Scalable Processors Product Specifications.html" "b/intel_extension_for_transformers/neural_chat/assets/docs/4th Generation Intel\302\256 Xeon\302\256 Scalable Processors Product Specifications.html" index 8d93751d582..ed26bdf1b00 100644 --- "a/intel_extension_for_transformers/neural_chat/assets/docs/4th Generation Intel\302\256 Xeon\302\256 Scalable Processors Product Specifications.html" +++ "b/intel_extension_for_transformers/neural_chat/assets/docs/4th Generation Intel\302\256 Xeon\302\256 Scalable Processors Product Specifications.html" @@ -1,3 +1,19 @@ + + diff --git a/intel_extension_for_transformers/neural_chat/assets/docs/sample.html b/intel_extension_for_transformers/neural_chat/assets/docs/sample.html index b08c9a56294..a284c0cd9ce 100644 --- a/intel_extension_for_transformers/neural_chat/assets/docs/sample.html +++ b/intel_extension_for_transformers/neural_chat/assets/docs/sample.html @@ -1,3 +1,19 @@ + + diff --git a/intel_extension_for_transformers/neural_chat/cli/base_executor.py b/intel_extension_for_transformers/neural_chat/cli/base_executor.py index 21bd560cc94..c0c1d225138 100644 --- a/intel_extension_for_transformers/neural_chat/cli/base_executor.py +++ b/intel_extension_for_transformers/neural_chat/cli/base_executor.py @@ -23,17 +23,14 @@ __all__ = ['BaseCommandExecutor'] class BaseCommandExecutor(ABC): - """ - An abstract executor class of neuralchat server tasks. - """ + """An abstract executor class of neuralchat server tasks.""" def __init__(self): self.parser = argparse.ArgumentParser() @abstractmethod def execute(self, argv: List[str]) -> bool: - """ - Command line entry. This method can only be accessed by a command line such as `neuralchat textchat`. + """Command line entry. This method can only be accessed by a command line such as `neuralchat textchat`. Args: argv (List[str]): Arguments from command line. @@ -45,7 +42,5 @@ def execute(self, argv: List[str]) -> bool: @abstractmethod def __call__(self, *arg, **kwargs): - """ - Python API to call an executor. - """ + """Python API to call an executor.""" pass diff --git a/intel_extension_for_transformers/neural_chat/cli/cli_commands.py b/intel_extension_for_transformers/neural_chat/cli/cli_commands.py index 7e153b94783..f0841fc9e0f 100644 --- a/intel_extension_for_transformers/neural_chat/cli/cli_commands.py +++ b/intel_extension_for_transformers/neural_chat/cli/cli_commands.py @@ -84,8 +84,7 @@ def neuralchat_execute(): @cli_register(name='neuralchat') class BaseCommand: - """ - BaseCommand class serving as a foundation for other neuralchat commands. + """BaseCommand class serving as a foundation for other neuralchat commands. This class provides a common structure for neuralchat commands. It includes a default implementation of the execute method, which acts as a fallback and @@ -110,8 +109,7 @@ def execute(self, argv: List[str]) -> bool: @cli_register(name='neuralchat.help', description='Show help for neuralchat commands.') class HelpCommand: - """ - HelpCommand class for displaying help about available neuralchat commands. + """HelpCommand class for displaying help about available neuralchat commands. This class provides the functionality to display a list of available neuralchat commands and their descriptions. It helps users understand how to use different @@ -141,8 +139,7 @@ def execute(self, argv: List[str]) -> bool: class TextVoiceChatExecutor(BaseCommandExecutor): - """ - TextVoiceChatExecutor class for executing text-based or voice-based conversations with a chatbot. + """TextVoiceChatExecutor class for executing text-based or voice-based conversations with a chatbot. This class extends the BaseCommandExecutor class and provides functionality for interacting with a chatbot through the command line or the Python API. It initializes @@ -155,11 +152,9 @@ class TextVoiceChatExecutor(BaseCommandExecutor): Methods: execute(argv): Execute the chatbot using command-line arguments. __call__(prompt): Python API for calling the chatbot executor. - """ def __init__(self): - """ - Initializes the TextVoiceChatExecutor class. + """Initializes the TextVoiceChatExecutor class. This constructor sets up the necessary components for the chatbot executor. It creates a command-line argument parser, initializes the configuration, @@ -178,9 +173,7 @@ def __init__(self): '--device', type=str, default=None, help='Specify chat on which device.') def execute(self, argv: List[str]) -> bool: - """ - Command line entry point. - """ + """Command line entry point.""" parser_args = self.parser.parse_args(argv) prompt = parser_args.query @@ -210,9 +203,7 @@ def execute(self, argv: List[str]) -> bool: def __call__( self, prompt: str): - """ - Python API to call an executor. - """ + """Python API to call an executor.""" result = self.chatbot.chat(prompt) return result @@ -231,9 +222,7 @@ def __init__(self): '--max_steps', type=int, default=None, help='Specify max steps of finetuning.') def execute(self, argv: List[str]) -> bool: - """ - Command line entry. - """ + """Command line entry.""" parser_args = self.parser.parse_args(argv) base_model = parser_args.base_model device = parser_args.device @@ -259,9 +248,7 @@ def execute(self, argv: List[str]) -> bool: return False def __call__(self): - """ - Python API to call an executor. - """ + """Python API to call an executor.""" finetune_model(self.finetuneCfg) specific_commands = { diff --git a/intel_extension_for_transformers/neural_chat/config.py b/intel_extension_for_transformers/neural_chat/config.py index e955365894a..77c463f86e5 100644 --- a/intel_extension_for_transformers/neural_chat/config.py +++ b/intel_extension_for_transformers/neural_chat/config.py @@ -53,9 +53,7 @@ class RetrievalTypeOptions(Enum): @dataclass class ModelArguments: - """ - Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch. - """ + """Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.""" model_name_or_path: Optional[str] = field( default=None, @@ -121,9 +119,7 @@ class ModelArguments: @dataclass class DataArguments: - """ - Arguments pertaining to what data we are going to input our model for training and eval. - """ + """Arguments pertaining to what data we are going to input our model for training and eval.""" dataset_name: Optional[str] = field( default=None, @@ -272,9 +268,7 @@ def __post_init__(self): @dataclass class FinetuningArguments: - """ - Arguments of finetune we are going to apply on the model. - """ + """Arguments of finetune we are going to apply on the model.""" lora_rank: int = field( default=8, diff --git a/intel_extension_for_transformers/neural_chat/config_logging.py b/intel_extension_for_transformers/neural_chat/config_logging.py index fc37ee36d2c..e12379b1c9b 100644 --- a/intel_extension_for_transformers/neural_chat/config_logging.py +++ b/intel_extension_for_transformers/neural_chat/config_logging.py @@ -19,8 +19,7 @@ import logging def configure_logging(log_file="app.log", log_level=logging.INFO): - """ - Configure logging for the application. + """Configure logging for the application. Parameters: - log_file: str, optional, default: "app.log" diff --git a/intel_extension_for_transformers/neural_chat/examples/deployment/plugin/image2image/export_ir.py b/intel_extension_for_transformers/neural_chat/examples/deployment/plugin/image2image/export_ir.py index 35cf29d247d..50dc4a249c4 100644 --- a/intel_extension_for_transformers/neural_chat/examples/deployment/plugin/image2image/export_ir.py +++ b/intel_extension_for_transformers/neural_chat/examples/deployment/plugin/image2image/export_ir.py @@ -49,7 +49,6 @@ 'AttentionBlock_QKVPreReshape': False, 'AttentionBlock_AttentionMaskAddReshape': False, 'AttentionBlock_ConstantOfShapeWithMul': False, - 'Transformer2Dmodel_GetSampleBatch': False, 'Transformer2Dmodel_SampleSlice': False, 'Transformer2Dmodel_EncoderHiddenStatesReshape': False, @@ -111,7 +110,6 @@ 'AttentionBlock_QKVPreReshape': True, 'AttentionBlock_AttentionMaskAddReshape': True, 'AttentionBlock_ConstantOfShapeWithMul': True, - 'Transformer2Dmodel_GetSampleBatch': True, 'Transformer2Dmodel_SampleSlice': True, 'Transformer2Dmodel_EncoderHiddenStatesReshape': True, @@ -173,7 +171,6 @@ 'AttentionBlock_QKVPreReshape': True, 'AttentionBlock_AttentionMaskAddReshape': True, 'AttentionBlock_ConstantOfShapeWithMul': True, - 'Transformer2Dmodel_GetSampleBatch': True, 'Transformer2Dmodel_SampleSlice': True, 'Transformer2Dmodel_EncoderHiddenStatesReshape': True, @@ -243,7 +240,6 @@ 'AttentionBlock_QKVPreReshape': True, 'AttentionBlock_AttentionMaskAddReshape': True, 'AttentionBlock_ConstantOfShapeWithMul': True, - 'Transformer2Dmodel_GetSampleBatch': True, 'Transformer2Dmodel_SampleSlice': True, 'Transformer2Dmodel_EncoderHiddenStatesReshape': True, diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/dpo_pipeline/dpo_clm.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/dpo_pipeline/dpo_clm.py index 2e5cd352de2..e5bef629d40 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/dpo_pipeline/dpo_clm.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/dpo_pipeline/dpo_clm.py @@ -51,9 +51,7 @@ @dataclass class ModelArguments: - """ - Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch. - """ + """Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.""" model_name_or_path: Optional[str] = field( default=None, @@ -116,9 +114,7 @@ class ModelArguments: @dataclass class DataTrainingArguments: - """ - Arguments pertaining to what data we are going to input our model for training and eval. - """ + """Arguments pertaining to what data we are going to input our model for training and eval.""" dataset_name: Optional[str] = field( default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."} @@ -155,9 +151,7 @@ class DataTrainingArguments: @dataclass class FinetuningArguments: - """ - Arguments of finetune we are going to apply on the model. - """ + """Arguments of finetune we are going to apply on the model.""" lora_rank: int = field( default=8, diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/instruction/gaudi_spawn.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/instruction/gaudi_spawn.py index 9fd57f89886..37da2b4a7dd 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/instruction/gaudi_spawn.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/instruction/gaudi_spawn.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -""" -A simple launcher script for distributed training on HPUs. +"""A simple launcher script for distributed training on HPUs. Single node: :: @@ -43,8 +41,8 @@ def parse_args(): - """ - Helper function parsing the command line options. + """Helper function parsing the command line options. + @retval ArgumentParser """ parser = ArgumentParser( diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_eval_only.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_eval_only.py index b493ffa00bb..f5e46a0f574 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_eval_only.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_eval_only.py @@ -11,8 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""Parse and Evalate""" +"""Parse and Evalate.""" import os import json diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_parse_and_eval.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_parse_and_eval.py index f83a16fdbe3..9203cedc638 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_parse_and_eval.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/main_parse_and_eval.py @@ -11,8 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""Parse and Evalate""" +"""Parse and Evalate.""" import os import json import shlex diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/run_llava.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/run_llava.py index cb4080b7457..ab8c904ef82 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/run_llava.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/run_llava.py @@ -206,8 +206,7 @@ def run_model(args, samples, model, call_model_engine_fn=None, return out_samples def set_seed(seed_value): - """ - Set the seed for PyTorch (both CPU and CUDA), Python, and NumPy for reproducible results. + """Set the seed for PyTorch (both CPU and CUDA), Python, and NumPy for reproducible results. :param seed_value: An integer value to be used as the seed. """ diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/data_utils.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/data_utils.py index e2788a1fe28..a495b754381 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/data_utils.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/data_utils.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """Utils for data load, save, and process (e.g., prompt construction)""" import os @@ -70,10 +69,8 @@ def save_json(filename, ds): def get_multi_choice_info(options): - """ - Given the list of options for multiple choice question - Return the index2ans and all_choices - """ + """Given the list of options for multiple choice question + Return the index2ans and all_choices.""" start_chr = 'A' all_choices = [] @@ -120,8 +117,7 @@ def save_json(filename, ds): json.dump(ds, f, indent=4) def save_jsonl(filename, data): - """ - Save a dictionary of data to a JSON Lines file with the filename as key and caption as value. + """Save a dictionary of data to a JSON Lines file with the filename as key and caption as value. Args: filename (str): The path to the file where the data should be saved. diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/eval_utils.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/eval_utils.py index 24bdd73b210..ade3cf47231 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/eval_utils.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/eval/mmmu_eval/utils/eval_utils.py @@ -11,8 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""Response Parsing and Evaluation for various models""" +"""Response Parsing and Evaluation for various models.""" from typing import Dict import re @@ -22,8 +21,8 @@ # ----------- Process Multi-choice ------------- def parse_multi_choice_response(response, all_choices, index2ans): - """ - Parse the prediction from the generated response. + """Parse the prediction from the generated response. + Return the predicted index e.g., A, B, C, D. """ for char in [',', '.', '!', '?', ';', ':', "'"]: @@ -77,9 +76,7 @@ def parse_multi_choice_response(response, all_choices, index2ans): # ----------- Process Open ------------- def check_is_number(string): - """ - Check if the given string a number. - """ + """Check if the given string a number.""" try: float(string.replace(',', '')) return True @@ -88,9 +85,7 @@ def check_is_number(string): return False def normalize_str(string): - """ - Normalize the str to lower case and make them float numbers if possible. - """ + """Normalize the str to lower case and make them float numbers if possible.""" # check if characters in the string # if number, numerize it. @@ -112,9 +107,7 @@ def normalize_str(string): return [string] def extract_numbers(string): - """ - Exact all forms of numbers from a string with regex. - """ + """Exact all forms of numbers from a string with regex.""" # Pattern for numbers with commas pattern_commas = r'-?\b\d{1,3}(?:,\d{3})+\b' # Pattern for scientific notation @@ -134,8 +127,8 @@ def extract_numbers(string): return all_numbers def parse_open_response(response): - """ - Parse the prediction from the generated response. + """Parse the prediction from the generated response. + Return a list of predicted strings or numbers. """ # content = content.strip("\n").strip(".").strip(" ") @@ -187,9 +180,7 @@ def get_key_subresponses(response): # ----------- Evaluation ------------- def eval_multi_choice(gold_i, pred_i): - """ - Evaluate a multiple choice instance. - """ + """Evaluate a multiple choice instance.""" correct = False # only they are exactly the same, we consider it as correct if isinstance(gold_i, list): @@ -203,9 +194,7 @@ def eval_multi_choice(gold_i, pred_i): return correct def eval_open(gold_i, pred_i): - """ - Evaluate an open question instance - """ + """Evaluate an open question instance.""" correct = False if isinstance(gold_i, list): # use float to avoid trivial matches @@ -231,9 +220,7 @@ def eval_open(gold_i, pred_i): # ----------- Batch Evaluation ------------- def evaluate(samples): - """ - Batch evaluation for multiple choice and open questions. - """ + """Batch evaluation for multiple choice and open questions.""" pred_correct = 0 judge_dict = dict() for sample in samples: @@ -258,7 +245,7 @@ def evaluate(samples): # ----------- Calculate Accuracy ------------- def calculate_ins_level_acc(results: Dict): - """Calculate the instruction level accuracy for given Subject results""" + """Calculate the instruction level accuracy for given Subject results.""" acc = 0 ins_num = 0 for cat_results in results.values(): diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/gaudi_spawn.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/gaudi_spawn.py index 9fd57f89886..37da2b4a7dd 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/gaudi_spawn.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/gaudi_spawn.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -""" -A simple launcher script for distributed training on HPUs. +"""A simple launcher script for distributed training on HPUs. Single node: :: @@ -43,8 +41,8 @@ def parse_args(): - """ - Helper function parsing the command line options. + """Helper function parsing the command line options. + @retval ArgumentParser """ parser = ArgumentParser( diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_trainer.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_trainer.py index df3018efcc0..913e687da90 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_trainer.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_trainer.py @@ -52,9 +52,7 @@ def get_mm_adapter_state_maybe_zero_3(named_params, keys_to_match): def split_to_even_chunks(indices, lengths, num_chunks): - """ - Split a list of indices into `chunks` chunks of roughly equal lengths. - """ + """Split a list of indices into `chunks` chunks of roughly equal lengths.""" if len(indices) % num_chunks != 0: return [indices[i::num_chunks] for i in range(num_chunks)] @@ -114,10 +112,8 @@ def get_length_grouped_indices(lengths, batch_size, world_size, generator=None, class LengthGroupedSampler(Sampler): - r""" - Sampler that samples indices in a way that groups together features of the dataset of roughly the same length while - keeping a bit of randomness. - """ + r"""Sampler that samples indices in a way that groups together features of the dataset of roughly the same length while + keeping a bit of randomness.""" def __init__( self, @@ -167,8 +163,7 @@ def _get_train_sampler(self) -> Optional[torch.utils.data.Sampler]: return super()._get_train_sampler() def create_optimizer(self): - """ - Setup the optimizer. + """Setup the optimizer. We provide a reasonable default that works well. If you want to use something else, you can pass a tuple in the Trainer's init through `optimizers`, or subclass and override this method in a subclass. @@ -311,8 +306,7 @@ def _get_train_sampler(self) -> Optional[torch.utils.data.Sampler]: return super()._get_train_sampler() def create_optimizer(self): - """ - Setup the optimizer. + """Setup the optimizer. We provide a reasonable default that works well. If you want to use something else, you can pass a tuple in the Trainer's init through `optimizers`, or subclass and override this method in a subclass. diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_utils.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_utils.py index 599c9e6ce5f..8e885f7f6e7 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_utils.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/multi_modal/llava_utils.py @@ -40,8 +40,7 @@ def resize_and_pad_image(image, target_resolution): - """ - Resize and pad an image to a target resolution while maintaining aspect ratio. + """Resize and pad an image to a target resolution while maintaining aspect ratio. Args: image (PIL.Image.Image): The input image. @@ -75,8 +74,7 @@ def resize_and_pad_image(image, target_resolution): def divide_to_patches(image, patch_size): - """ - Divides an image into patches of a specified size. + """Divides an image into patches of a specified size. Args: image (PIL.Image.Image): The input image. @@ -97,8 +95,7 @@ def divide_to_patches(image, patch_size): def process_anyres_image(image, processor, grid_pinpoints): - """ - Process an image with variable resolutions. + """Process an image with variable resolutions. Args: image (PIL.Image.Image): The input image to be processed. @@ -288,7 +285,7 @@ def safe_save_model_for_hf_trainer(trainer: transformers.Trainer, for key, value in state_dict.items() } del state_dict - trainer._save(output_dir, state_dict=cpu_state_dict) # noqa + trainer._save(output_dir, state_dict=cpu_state_dict) def smart_tokenizer_and_embedding_resize( @@ -776,8 +773,9 @@ def preprocess( conversation_template, has_image: bool = False, ) -> Dict: - """ - Given a list of sources, each is a conversation list. This transform: + """Given a list of sources, each is a conversation list. + + This transform: 1. Add signal '### ' at the beginning each sentence, with end signal '\n'; 2. Concatenate conversations together; 3. Tokenize the concatenated conversation; diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/merge_peft_adapter.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/merge_peft_adapter.py index bb6d1fa1043..af7a5a780eb 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/merge_peft_adapter.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/merge_peft_adapter.py @@ -26,10 +26,8 @@ @dataclass class ScriptArguments: - """ - The input names representing the Adapter and Base model fine-tuned with PEFT, and the output name representing the - merged model. - """ + """The input names representing the Adapter and Base model fine-tuned with PEFT, and the output name representing the + merged model.""" adapter_model_name: Optional[str] = field(default=None, metadata={"help": "the adapter name"}) output_name: Optional[str] = field(default=None, metadata={"help": "the merged model name"}) diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/reward_modeling.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/reward_modeling.py index 43160543225..ebb5509e06b 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/reward_modeling.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/reward_modeling.py @@ -47,9 +47,7 @@ @dataclass class ModelArguments: - """ - Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch. - """ + """Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.""" model_name_or_path: Optional[str] = field( default=None, @@ -111,9 +109,7 @@ class ModelArguments: @dataclass class DataTrainingArguments: - """ - Arguments pertaining to what data we are going to input our model for training and eval. - """ + """Arguments pertaining to what data we are going to input our model for training and eval.""" dataset_name: Optional[str] = field( default="Intel/orca_dpo_pairs", @@ -145,9 +141,7 @@ class DataTrainingArguments: @dataclass class FinetuningArguments: - """ - Arguments of finetune we are going to apply on the model. - """ + """Arguments of finetune we are going to apply on the model.""" lora_rank: int = field( default=8, diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/rl_training.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/rl_training.py index 2104959ebf6..d0c90543890 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/rl_training.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/ppo_pipeline/rl_training.py @@ -57,8 +57,7 @@ def build_dataset( tokenizer, dataset_name, ): - """ - Build dataset for training. This builds the dataset from `load_dataset`, one should + """Build dataset for training. This builds the dataset from `load_dataset`, one should customize this function to train the model on its own dataset. Args: @@ -106,9 +105,7 @@ def collator(data): @dataclass class ScriptArguments: - """ - The name of the Casual LM model we wish to fine with PPO - """ + """The name of the Casual LM model we wish to fine with PPO.""" # NOTE: gpt2 models use Conv1D instead of Linear layers which are not yet supported in 8 bit mode # models like gpt-neo* models are more suitable. diff --git a/intel_extension_for_transformers/neural_chat/examples/finetuning/tts/inference.py b/intel_extension_for_transformers/neural_chat/examples/finetuning/tts/inference.py index 223a8fddc50..8bf1152f8de 100644 --- a/intel_extension_for_transformers/neural_chat/examples/finetuning/tts/inference.py +++ b/intel_extension_for_transformers/neural_chat/examples/finetuning/tts/inference.py @@ -100,7 +100,7 @@ def correct_abbreviation(text): return " ".join(results) def correct_number(text): - """Ignore the year or other exception right now""" + """Ignore the year or other exception right now.""" words = text.split() results = [] for idx, word in enumerate(words): diff --git a/intel_extension_for_transformers/neural_chat/models/base_model.py b/intel_extension_for_transformers/neural_chat/models/base_model.py index a603cd4f0c5..d36b0a2d2d2 100644 --- a/intel_extension_for_transformers/neural_chat/models/base_model.py +++ b/intel_extension_for_transformers/neural_chat/models/base_model.py @@ -59,14 +59,10 @@ def construct_parameters(query, model_name, device, assistant_model, config): return params class BaseModel(ABC): - """ - A base class for LLM. - """ + """A base class for LLM.""" def __init__(self, model_name, task="chat"): - """ - Initializes the BaseModel class. - """ + """Initializes the BaseModel class.""" self.model_name = model_name self.asr = None self.tts = None @@ -85,8 +81,7 @@ def __init__(self, model_name, task="chat"): self.get_conv_template(task) def match(self): - """ - Check if the provided model_name matches the current model. + """Check if the provided model_name matches the current model. Returns: bool: True if the model_name matches, False otherwise. @@ -94,8 +89,7 @@ def match(self): return True def load_model(self, kwargs: dict): - """ - Load the model using the provided arguments. + """Load the model using the provided arguments. Args: kwargs (dict): A dictionary containing the configuration parameters for model loading. @@ -142,8 +136,7 @@ def load_model(self, kwargs: dict): gguf_model_path=kwargs["gguf_model_path"]) def predict_stream(self, query, origin_query="", config=None): - """ - Predict using a streaming approach. + """Predict using a streaming approach. Args: query: The input query for prediction. @@ -268,8 +261,7 @@ def is_generator(obj): return response, link def predict(self, query, origin_query="", config=None): - """ - Predict using a non-streaming approach. + """Predict using a non-streaming approach. Args: query: The input query for prediction. @@ -386,8 +378,7 @@ def predict(self, query, origin_query="", config=None): return response def chat_stream(self, query, origin_query="", config=None): - """ - Chat using a streaming approach. + """Chat using a streaming approach. Args: query: The input query for prediction. @@ -397,8 +388,7 @@ def chat_stream(self, query, origin_query="", config=None): return self.predict_stream(query=query, origin_query=origin_query, config=config) def chat(self, query, origin_query="", config=None): - """ - Chat using a non-streaming approach. + """Chat using a non-streaming approach. Args: query: The input query for conversation. @@ -433,8 +423,7 @@ def face_animate(self, image_path, audio_path=None, text=None, voice=None) -> st return video_path def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Args: model_path (str): Path to the model. @@ -445,8 +434,7 @@ def get_default_conv_template(self) -> Conversation: return get_conv_template("zero_shot") def get_conv_template(self, model_path: str, task: str = "") -> Conversation: - """ - Get the conversation template for the given model path or given task. + """Get the conversation template for the given model path or given task. Args: model_path (str): Path to the model. @@ -487,8 +475,7 @@ def set_customized_system_prompts(self, system_prompts, model_path: str, task: s self.conv_template.conv.system_message = system_prompts def register_plugin_instance(self, plugin_name, instance): - """ - Register a plugin instance. + """Register a plugin instance. Args: instance: An instance of a plugin. diff --git a/intel_extension_for_transformers/neural_chat/models/chatglm_model.py b/intel_extension_for_transformers/neural_chat/models/chatglm_model.py index 11339b68c9d..a05b378edbb 100644 --- a/intel_extension_for_transformers/neural_chat/models/chatglm_model.py +++ b/intel_extension_for_transformers/neural_chat/models/chatglm_model.py @@ -28,8 +28,7 @@ class ChatGlmModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Args: model_path (str): Path to a model. @@ -40,8 +39,7 @@ def match(self): return "chatglm" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Args: model_path (str): Path to the model. diff --git a/intel_extension_for_transformers/neural_chat/models/decilm_model.py b/intel_extension_for_transformers/neural_chat/models/decilm_model.py index 118d951bca5..81729758c04 100644 --- a/intel_extension_for_transformers/neural_chat/models/decilm_model.py +++ b/intel_extension_for_transformers/neural_chat/models/decilm_model.py @@ -43,8 +43,7 @@ class DeciLMModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Returns: bool: True if the model_path matches, False otherwise. @@ -52,8 +51,7 @@ def match(self): return "decilm" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Returns: Conversation: A default conversation template. diff --git a/intel_extension_for_transformers/neural_chat/models/deepseek_coder_model.py b/intel_extension_for_transformers/neural_chat/models/deepseek_coder_model.py index 37544a5fc03..68e35c815f0 100644 --- a/intel_extension_for_transformers/neural_chat/models/deepseek_coder_model.py +++ b/intel_extension_for_transformers/neural_chat/models/deepseek_coder_model.py @@ -28,8 +28,7 @@ class DeepseekCoderModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Returns: bool: True if the model_path matches, False otherwise. @@ -37,8 +36,7 @@ def match(self): return "deepseek-coder" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Returns: Conversation: A default conversation template. diff --git a/intel_extension_for_transformers/neural_chat/models/llama_model.py b/intel_extension_for_transformers/neural_chat/models/llama_model.py index 86cb90e741b..da58e0f1d76 100644 --- a/intel_extension_for_transformers/neural_chat/models/llama_model.py +++ b/intel_extension_for_transformers/neural_chat/models/llama_model.py @@ -28,8 +28,7 @@ class LlamaModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Returns: bool: True if the model_path matches, False otherwise. @@ -37,8 +36,7 @@ def match(self): return "llama" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Returns: Conversation: A default conversation template. diff --git a/intel_extension_for_transformers/neural_chat/models/mistral_model.py b/intel_extension_for_transformers/neural_chat/models/mistral_model.py index b85f5987e4a..daceed79839 100644 --- a/intel_extension_for_transformers/neural_chat/models/mistral_model.py +++ b/intel_extension_for_transformers/neural_chat/models/mistral_model.py @@ -28,8 +28,7 @@ class MistralModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Returns: bool: True if the model_path matches, False otherwise. @@ -37,8 +36,7 @@ def match(self): return "mistral" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Returns: Conversation: A default conversation template. diff --git a/intel_extension_for_transformers/neural_chat/models/model_utils.py b/intel_extension_for_transformers/neural_chat/models/model_utils.py index 9e4d8a1b826..dbc44a2db37 100644 --- a/intel_extension_for_transformers/neural_chat/models/model_utils.py +++ b/intel_extension_for_transformers/neural_chat/models/model_utils.py @@ -88,9 +88,7 @@ def __call__( return False def get_repo_root(model_name_or_path, local_rank=-1, token=None): - """ - Downloads the specified model checkpoint and returns the repository where it was downloaded. - """ + """Downloads the specified model checkpoint and returns the repository where it was downloaded.""" if Path(model_name_or_path).is_dir(): # If it is a local model, no need to download anything return model_name_or_path @@ -130,9 +128,7 @@ def get_repo_root(model_name_or_path, local_rank=-1, token=None): def get_checkpoint_files(model_name_or_path, local_rank, token=None): - """ - Gets the list of files for the specified model checkpoint. - """ + """Gets the list of files for the specified model checkpoint.""" cached_repo_dir = get_repo_root(model_name_or_path, local_rank, token) # Extensions: .bin | .pt @@ -142,9 +138,7 @@ def get_checkpoint_files(model_name_or_path, local_rank, token=None): def write_checkpoints_json(model_name_or_path, local_rank, checkpoints_json, token=None): - """ - Dumps metadata into a JSON file for DeepSpeed-inference. - """ + """Dumps metadata into a JSON file for DeepSpeed-inference.""" checkpoint_files = get_checkpoint_files(model_name_or_path, local_rank, token) if local_rank == 0 and len(checkpoint_files) != 0: data = {"type": "ds_model", "checkpoints": checkpoint_files, "version": 1.0} @@ -154,9 +148,7 @@ def write_checkpoints_json(model_name_or_path, local_rank, checkpoints_json, tok def model_on_meta(config): - """ - Checks if load the model to meta. - """ + """Checks if load the model to meta.""" return config.model_type in ["bloom", "llama"] @@ -173,10 +165,8 @@ def get_optimized_model_name(config): def model_is_optimized(config): - """ - Checks if the given config belongs to a model in optimum/habana/transformers/models, which has a - new input token_idx. - """ + """Checks if the given config belongs to a model in optimum/habana/transformers/models, which has a + new input token_idx.""" return get_optimized_model_name(config) is not None @@ -419,8 +409,7 @@ def load_model( vllm_engine_params=None, gguf_model_path=None, ): - """ - Load the model and initialize the tokenizer. + """Load the model and initialize the tokenizer. Args: model_name (str): The name of the model. @@ -1003,8 +992,7 @@ def get_context_length(config): output_token_len = 0 def predict_stream(**params): - """ - Generates streaming text based on the given parameters and prompt. + """Generates streaming text based on the given parameters and prompt. Args: params (dict): A dictionary containing the parameters for text generation. @@ -1308,8 +1296,7 @@ def generate_output(): def predict(**params): - """ - Generates streaming text based on the given parameters and prompt. + """Generates streaming text based on the given parameters and prompt. Args: params (dict): A dictionary containing the parameters for text generation. diff --git a/intel_extension_for_transformers/neural_chat/models/mpt_model.py b/intel_extension_for_transformers/neural_chat/models/mpt_model.py index b3c4d96a3db..98971968a5c 100644 --- a/intel_extension_for_transformers/neural_chat/models/mpt_model.py +++ b/intel_extension_for_transformers/neural_chat/models/mpt_model.py @@ -28,8 +28,7 @@ class MptModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Returns: bool: True if the model_path matches, False otherwise. @@ -37,8 +36,7 @@ def match(self): return "mpt" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Returns: Conversation: A default conversation template. diff --git a/intel_extension_for_transformers/neural_chat/models/neuralchat_model.py b/intel_extension_for_transformers/neural_chat/models/neuralchat_model.py index 0836b8689cd..fd51b1e4584 100644 --- a/intel_extension_for_transformers/neural_chat/models/neuralchat_model.py +++ b/intel_extension_for_transformers/neural_chat/models/neuralchat_model.py @@ -28,8 +28,7 @@ class NeuralChatModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Returns: bool: True if the model_path matches, False otherwise. @@ -37,8 +36,7 @@ def match(self): return "neural-chat" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Returns: Conversation: A default conversation template. diff --git a/intel_extension_for_transformers/neural_chat/models/openai_model.py b/intel_extension_for_transformers/neural_chat/models/openai_model.py index b8b3300d5a4..90145b08b4a 100644 --- a/intel_extension_for_transformers/neural_chat/models/openai_model.py +++ b/intel_extension_for_transformers/neural_chat/models/openai_model.py @@ -33,9 +33,7 @@ logger = logging.getLogger(__name__) class OpenAIModel(BaseModel): - """ - Customized class to operate on OpenAI models in the pipeline. - """ + """Customized class to operate on OpenAI models in the pipeline.""" def __init__(self, model_name, task, openai_config): self.api_key = openai_config.api_key self.organization = openai_config.organization diff --git a/intel_extension_for_transformers/neural_chat/models/qwen_model.py b/intel_extension_for_transformers/neural_chat/models/qwen_model.py index bde81b5703f..7a9c6c7b781 100644 --- a/intel_extension_for_transformers/neural_chat/models/qwen_model.py +++ b/intel_extension_for_transformers/neural_chat/models/qwen_model.py @@ -50,8 +50,7 @@ def prepare_inputs_for_generation( class QwenModel(BaseModel): def match(self): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Returns: bool: True if the model_path matches, False otherwise. @@ -59,8 +58,7 @@ def match(self): return "qwen" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Args: model_path (str): Path to the model. diff --git a/intel_extension_for_transformers/neural_chat/models/solar_model.py b/intel_extension_for_transformers/neural_chat/models/solar_model.py index fa007c67ec0..13648beb194 100644 --- a/intel_extension_for_transformers/neural_chat/models/solar_model.py +++ b/intel_extension_for_transformers/neural_chat/models/solar_model.py @@ -42,8 +42,7 @@ class SolarModel(BaseModel): def match(self, model_path: str): - """ - Check if the provided model_path matches the current model. + """Check if the provided model_path matches the current model. Args: model_path (str): Path to a model. @@ -54,8 +53,7 @@ def match(self, model_path: str): return "solar-" in self.model_name.lower() and "instruct" in self.model_name.lower() def get_default_conv_template(self) -> Conversation: - """ - Get the default conversation template for the given model path. + """Get the default conversation template for the given model path. Args: model_path (str): Path to the model. diff --git a/intel_extension_for_transformers/neural_chat/pipeline/__init__.py b/intel_extension_for_transformers/neural_chat/pipeline/__init__.py index 7df490129b8..6c3d4271f82 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/__init__.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/__init__.py @@ -14,5 +14,4 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """Intel® Neural Chat: An open-source Python library supporting LLM chatbot.""" diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/asr.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/asr.py index b197ed22730..b5d9c04dd84 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/asr.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/asr.py @@ -66,7 +66,7 @@ def _convert_audio_type(self, audio_path): # pragma: no cover return f"{audio_file_name}.mp3" def audio2text(self, audio_path): - """Convert audio to text + """Convert audio to text. audio_path: the path to the input audio, e.g. ~/xxx.mp3 """ diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/attentions.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/attentions.py index 112e83542f8..b89cf5ec130 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/attentions.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/attentions.py @@ -416,6 +416,7 @@ def _absolute_position_to_relative_position(self, x): def _attention_bias_proximal(self, length): """Bias for self-attention to encourage attention to close positions. + Args: length: an integer scalar. Returns: diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/modules.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/modules.py index 4671a4c1a44..69d7d5ed336 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/modules.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/modules.py @@ -99,9 +99,7 @@ def forward(self, x, x_mask): class DDSConv(nn.Module): - """ - Dialted and Depth-Separable Convolution - """ + """Dialted and Depth-Separable Convolution.""" def __init__(self, channels, kernel_size, n_layers, p_dropout=0.0): super().__init__() diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/text/cleaner.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/text/cleaner.py index 0623957fff9..53c343fb263 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/text/cleaner.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/text/cleaner.py @@ -43,6 +43,7 @@ def text_to_sequence(text, language): def cleaned_text_to_sequence(cleaned_text, tones, language): """Converts a string of text to a sequence of IDs corresponding to the symbols in the text. + Args: text: string to convert to a sequence Returns: diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/tools/sentence.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/tools/sentence.py index 1519845ab11..4aabfb33577 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/tools/sentence.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/models/bert_vits2/tools/sentence.py @@ -22,7 +22,7 @@ def check_is_none(item) -> bool: - """none -> True, not none -> False""" + """None -> True, not none -> False.""" return ( item is None or (isinstance(item, str) and str(item).isspace()) diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/tts.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/tts.py index 039900cdc38..9fa764d30e5 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/tts.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/tts.py @@ -38,7 +38,7 @@ from intel_extension_for_transformers.neural_chat.utils.common import get_device_type class TextToSpeech(): - """Convert text to speech with a driven speaker embedding + """Convert text to speech with a driven speaker embedding. 1) Default voice (Original model + Proved good default speaker embedding from trained dataset) 2) Finetuned voice (Fine-tuned offline model of specific person's voice + corresponding embedding) diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/english_normalizer.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/english_normalizer.py index 772425142df..07d23181663 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/english_normalizer.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/english_normalizer.py @@ -77,7 +77,7 @@ def correct_abbreviation(self, text): return " ".join(results) def correct_number(self, text): - """Ignore the year or other exception right now""" + """Ignore the year or other exception right now.""" words = text.split() results = [] prepositions_year = ["in", "on"] diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/reduce_noise.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/reduce_noise.py index fd5aeac2b07..ee78f820395 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/reduce_noise.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/audio/utils/reduce_noise.py @@ -32,8 +32,7 @@ @torch.no_grad() def amp_to_db(x: torch.Tensor, eps=torch.finfo(torch.float64).eps, top_db=40) -> torch.Tensor: - """ - Convert the input tensor from amplitude to decibel scale. + """Convert the input tensor from amplitude to decibel scale. Arguments: x {[torch.Tensor]} -- [Input tensor.] @@ -53,8 +52,7 @@ def amp_to_db(x: torch.Tensor, eps=torch.finfo(torch.float64).eps, top_db=40) -> @torch.no_grad() def temperature_sigmoid(x: torch.Tensor, x0: float, temp_coeff: float) -> torch.Tensor: - """ - Apply a sigmoid function with temperature scaling. + """Apply a sigmoid function with temperature scaling. Arguments: x {[torch.Tensor]} -- [Input tensor.] @@ -69,8 +67,7 @@ def temperature_sigmoid(x: torch.Tensor, x0: float, temp_coeff: float) -> torch. @torch.no_grad() def linspace(start: Number, stop: Number, num: int = 50, endpoint: bool = True, **kwargs) -> torch.Tensor: - """ - Generate a linearly spaced 1-D tensor. + """Generate a linearly spaced 1-D tensor. Arguments: start {[Number]} -- [The starting value of the sequence.] @@ -95,8 +92,7 @@ def linspace(start: Number, stop: Number, num: int = 50, endpoint: bool = True, class TorchGate(torch.nn.Module): - """ - A PyTorch module that applies a spectral gate to an input signal. + """A PyTorch module that applies a spectral gate to an input signal. Arguments: sr {int} -- Sample rate of the input signal. @@ -162,8 +158,7 @@ def __init__( @torch.no_grad() def _generate_mask_smoothing_filter(self) -> Union[torch.Tensor, None]: - """ - A PyTorch module that applies a spectral gate to an input signal using the STFT. + """A PyTorch module that applies a spectral gate to an input signal using the STFT. Returns: smoothing_filter (torch.Tensor): a 2D tensor representing the smoothing filter, @@ -217,8 +212,7 @@ def _generate_mask_smoothing_filter(self) -> Union[torch.Tensor, None]: def _stationary_mask( self, X_db: torch.Tensor, xn: Optional[torch.Tensor] = None ) -> torch.Tensor: - """ - Computes a stationary binary mask to filter out noise in a log-magnitude spectrogram. + """Computes a stationary binary mask to filter out noise in a log-magnitude spectrogram. Arguments: X_db (torch.Tensor): 2D tensor of shape (frames, freq_bins) containing the log-magnitude spectrogram. @@ -256,8 +250,7 @@ def _stationary_mask( @torch.no_grad() def _nonstationary_mask(self, X_abs: torch.Tensor) -> torch.Tensor: - """ - Computes a non-stationary binary mask to filter out noise in a log-magnitude spectrogram. + """Computes a non-stationary binary mask to filter out noise in a log-magnitude spectrogram. Arguments: X_abs (torch.Tensor): 2D tensor of shape (frames, freq_bins) containing the magnitude spectrogram. @@ -290,8 +283,7 @@ def _nonstationary_mask(self, X_abs: torch.Tensor) -> torch.Tensor: def forward( self, x: torch.Tensor, xn: Optional[torch.Tensor] = None ) -> torch.Tensor: - """ - Apply the proposed algorithm to the input signal. + """Apply the proposed algorithm to the input signal. Arguments: x (torch.Tensor): The input audio signal, with shape (batch_size, signal_length). diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/image2image/instructpix2pix_pipeline.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/image2image/instructpix2pix_pipeline.py index 73e0dd95e09..fa25db5e459 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/image2image/instructpix2pix_pipeline.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/image2image/instructpix2pix_pipeline.py @@ -14,7 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""Pipeline Modificaiton based from the diffusers 0.12.1 StableDiffusionInstructPix2PixPipeline""" +"""Pipeline Modificaiton based from the diffusers 0.12.1 StableDiffusionInstructPix2PixPipeline.""" import inspect from typing import Callable, List, Optional, Union @@ -66,8 +66,7 @@ def preprocess(image): class StableDiffusionInstructPix2PixPipeline(DiffusionPipeline): - r""" - Pipeline for pixel-level image editing by following text instructions. Based on Stable Diffusion. + r"""Pipeline for pixel-level image editing by following text instructions. Based on Stable Diffusion. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.) @@ -157,8 +156,7 @@ def __call__( callback_steps: Optional[int] = 1, engine_graph: Optional[list] = [], ): - r""" - Function invoked when calling the pipeline for generation. + r"""Function invoked when calling the pipeline for generation. Args: prompt (`str` or `List[str]`, *optional*): @@ -403,8 +401,9 @@ def __call__( # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion. # StableDiffusionPipeline.enable_sequential_cpu_offload def enable_sequential_cpu_offload(self, gpu_id=0): - r""" - Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, unet, + r"""Offloads all models to CPU using accelerate, significantly reducing memory usage. + + When called, unet, text_encoder, vae and safety checker have their state dicts saved to CPU and then are moved to a `torch.device('meta') and loaded to GPU only when their specific submodule has its `forward` method called. """ @@ -425,8 +424,9 @@ def enable_sequential_cpu_offload(self, gpu_id=0): # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion. # StableDiffusionPipeline._execution_device def _execution_device(self): - r""" - Returns the device on which the pipeline's models will be executed. After calling + r"""Returns the device on which the pipeline's models will be executed. + + After calling `pipeline.enable_sequential_cpu_offload()` the execution device can only be inferred from Accelerate's module hooks. """ @@ -452,8 +452,7 @@ def _encode_prompt( prompt_embeds: Optional[torch.FloatTensor] = None, negative_prompt_embeds: Optional[torch.FloatTensor] = None, ): - r""" - Encodes the prompt into text encoder hidden states. + r"""Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `List[str]`, *optional*): diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/ner/ner.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/ner/ner.py index 83f1f944eed..1b15affebcb 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/ner/ner.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/ner/ner.py @@ -31,10 +31,8 @@ ) class NamedEntityRecognition(): - """ - NER class to inference with fp32 or bf16 llm models - Set bf16=True if you want to inference with bf16 model. - """ + """NER class to inference with fp32 or bf16 llm models + Set bf16=True if you want to inference with bf16 model.""" def __init__(self, spacy_model="en_core_web_lg") -> None: # initialize tokenizer and models diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/prompt/prompt_template.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/prompt/prompt_template.py index ca148e640fd..5c929f3ef93 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/prompt/prompt_template.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/prompt/prompt_template.py @@ -75,4 +75,4 @@ def polish_query_prompt(query): conv = PromptTemplate("polish") conv.append_message(conv.roles[0], query) conv.append_message(conv.roles[1], None) - return conv.get_prompt() + return conv.get_prompt() diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/detector/query_explainer.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/detector/query_explainer.py index e9f69721035..5d60d4d5544 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/detector/query_explainer.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/detector/query_explainer.py @@ -36,4 +36,4 @@ def polish_query(self, model_name, query): params["max_new_tokens"] = 512 params['do_sample'] = True new_query = predict(**params) - return new_query + return new_query diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/context_utils.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/context_utils.py index 91b8de54cb4..d5a206931f6 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/context_utils.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/context_utils.py @@ -189,7 +189,7 @@ def load_xlsx(input): return all_data def load_csv(input): - """ Load the csv file.""" + """Load the csv file.""" df = pd.read_csv(input) all_data = [] documents = [] diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/parser.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/parser.py index 3337470f430..22c059d6b40 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/parser.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/parser/parser.py @@ -31,17 +31,16 @@ class DocumentParser: def __init__(self, max_chuck_size=512, min_chuck_size=5, process=True): - """ - Wrapper for document parsing. - """ + """Wrapper for document parsing.""" self.max_chuck_size = max_chuck_size self.min_chuck_size = min_chuck_size self.process = process def load(self, input, **kwargs): - """ - The API for loading the file. Support single file, batch files, and urls parsing. + """The API for loading the file. + + Support single file, batch files, and urls parsing. """ if 'max_chuck_size' in kwargs: self.max_chuck_size=kwargs['max_chuck_size'] @@ -69,9 +68,7 @@ def load(self, input, **kwargs): def parse_document(self, input): - """ - Parse the uploaded file. - """ + """Parse the uploaded file.""" if input.endswith("pdf") or input.endswith("docx") or input.endswith("html") \ or input.endswith("txt") or input.endswith("md"): content = load_unstructured_data(input) @@ -89,9 +86,7 @@ def parse_document(self, input): return chuck def parse_html(self, input): - """ - Parse the uploaded file. - """ + """Parse the uploaded file.""" chucks = [] for link in input: if re.match(r'^https?:/{2}\w.+$', link): @@ -110,9 +105,7 @@ def parse_html(self, input): def batch_parse_document(self, input): - """ - Parse the uploaded batch files in the input folder. - """ + """Parse the uploaded batch files in the input folder.""" paragraphs = [] for dirpath, dirnames, filenames in os.walk(input): for filename in filenames: diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retrieval_agent.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retrieval_agent.py index acd8f37218d..ef17a3aaf1a 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retrieval_agent.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retrieval_agent.py @@ -57,8 +57,9 @@ def document_append_id(documents): class Agent_QA(): - """ - The agent for retrieval-based chatbot. Contains all parameters setting and file parsing. + """The agent for retrieval-based chatbot. + + Contains all parameters setting and file parsing. """ def __init__(self, vector_database="Chroma", @@ -210,8 +211,9 @@ def __init__(self, logging.info("The retriever is successfully built.") def reload_localdb(self, local_persist_dir, **kwargs): - """ - Reload the local existed knowledge base. Do not support inmemory database. + """Reload the local existed knowledge base. + + Do not support inmemory database. """ assert os.path.exists(local_persist_dir) and bool(os.listdir(local_persist_dir)), \ "Please check the local knowledge base was built!" @@ -232,9 +234,7 @@ def reload_localdb(self, local_persist_dir, **kwargs): def create(self, input_path, **kwargs): - """ - Create a new knowledge base based on the uploaded files. - """ + """Create a new knowledge base based on the uploaded files.""" data_collection = self.document_parser.load(input=input_path, **kwargs) langchain_documents = document_transfer(data_collection, self.min_chuck_size) diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retriever_adapter.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retriever_adapter.py index a5f30ec647c..3bb71e648b7 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retriever_adapter.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/retrieval/retriever_adapter.py @@ -14,8 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""The wrapper for Retriever based on langchain""" +"""The wrapper for Retriever based on langchain.""" from intel_extension_for_transformers.langchain_community.retrievers import ChildParentRetriever # pylint: disable=E0401, E0611 from langchain_core.vectorstores import VectorStoreRetriever from langchain_community.retrievers import BM25Retriever diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/security/safety_checker.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/security/safety_checker.py index 0c114c3a249..8df4318bd60 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/security/safety_checker.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/security/safety_checker.py @@ -130,7 +130,7 @@ def _checkSensitiveWord(self, txt, beginIndex): return tmpFlag, category def _get_sensitive_word(self, context): - """get the sensitive word.""" + """Get the sensitive word.""" sensitiveWordList = list() for i in range(len(context)): length = self._checkSensitiveWord(context, i)[0] diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/sadtalker.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/sadtalker.py index 0611d412312..227083a131d 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/sadtalker.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/sadtalker.py @@ -15,7 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import subprocess +import subprocess import sys import os import psutil diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/audio2pose_models/cvae.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/audio2pose_models/cvae.py index 179e130f8e4..392260cbe51 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/audio2pose_models/cvae.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/audio2pose_models/cvae.py @@ -63,11 +63,9 @@ def forward(self, batch): return self.decoder(batch) def test(self, batch): - """ - class_id = batch['class'] + """class_id = batch['class'] z = torch.randn([class_id.size(0), self.latent_size]).to(class_id.device) - batch['z'] = z - """ + batch['z'] = z.""" return self.decoder(batch) diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/bfm.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/bfm.py index 6bcaade8dbd..48fa5ea5d87 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/bfm.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/bfm.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""This script defines the parametric 3d face model for Deep3DFaceRecon_pytorch -""" +"""This script defines the parametric 3d face model for Deep3DFaceRecon_pytorch.""" import numpy as np import torch diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/networks.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/networks.py index 21b2e01bdcf..23bfd21040b 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/networks.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/models/networks.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""This script defines deep neural networks for Deep3DFaceRecon_pytorch -""" +"""This script defines deep neural networks for Deep3DFaceRecon_pytorch.""" import os from torch.optim import lr_scheduler @@ -48,7 +46,7 @@ def filter_state_dict(state_dict, remove_name="fc"): def get_scheduler(optimizer, opt): - """Return a learning rate scheduler + """Return a learning rate scheduler. Parameters: optimizer -- the optimizer of the network @@ -150,7 +148,7 @@ def forward(self, x): def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d: - """3x3 convolution with padding""" + """3x3 convolution with padding.""" return nn.Conv2d( in_planes, out_planes, @@ -164,7 +162,7 @@ def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, d def conv1x1(in_planes: int, out_planes: int, stride: int = 1, bias: bool = False) -> nn.Conv2d: - """1x1 convolution""" + """1x1 convolution.""" return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=bias) diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/__init__.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/__init__.py index 600a97ae5c0..1f2194d23e3 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/__init__.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/__init__.py @@ -14,6 +14,5 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """This package includes a miscellaneous collection of useful helper functions.""" from intel_extension_for_transformers.neural_chat.pipeline.plugins.video.face_animation.src.face3d.util import * diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/load_mats.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/load_mats.py index c1323216b2a..2c67e8f7794 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/load_mats.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/load_mats.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""This script is to load 3D face model for Deep3DFaceRecon_pytorch -""" +"""This script is to load 3D face model for Deep3DFaceRecon_pytorch.""" import numpy as np from scipy.io import loadmat, savemat diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/preprocess.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/preprocess.py index ace66ef4987..336fbef6972 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/preprocess.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/preprocess.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""This script contains the image preprocessing code for Deep3DFaceRecon_pytorch -""" +"""This script contains the image preprocessing code for Deep3DFaceRecon_pytorch.""" import numpy as np from PIL import Image diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/util.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/util.py index c6fff54da2e..0711357504a 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/util.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/face3d/util/util.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -"""This script contains basic utilities for Deep3DFaceRecon_pytorch -""" +"""This script contains basic utilities for Deep3DFaceRecon_pytorch.""" import numpy as np import os import argparse @@ -34,7 +32,7 @@ def str2bool(v): def mkdirs(paths): - """create empty directories if they don't exist + """Create empty directories if they don't exist. Parameters: paths (str list) -- a list of directory paths @@ -47,7 +45,7 @@ def mkdirs(paths): def mkdir(path): - """create a single empty directory if it didn't exist + """Create a single empty directory if it didn't exist. Parameters: path (str) -- a single directory path diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/dense_motion.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/dense_motion.py index 76e290f2738..17a3a838853 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/dense_motion.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/dense_motion.py @@ -26,9 +26,7 @@ class DenseMotionNetwork(nn.Module): - """ - Module that predicting a dense motion from sparse motion representation given by kp_source and kp_driving - """ + """Module that predicting a dense motion from sparse motion representation given by kp_source and kp_driving.""" def __init__( self, diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/discriminator.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/discriminator.py index 55ca1448082..0db5374d7e4 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/discriminator.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/discriminator.py @@ -20,9 +20,7 @@ class DownBlock2d(nn.Module): - """ - Simple block for processing video (encoder). - """ + """Simple block for processing video (encoder).""" def __init__(self, in_features, out_features, norm=False, kernel_size=4, pool=False, sn=False): super(DownBlock2d, self).__init__() @@ -50,9 +48,7 @@ def forward(self, x): class Discriminator(nn.Module): - """ - Discriminator similar to Pix2Pix - """ + """Discriminator similar to Pix2Pix.""" def __init__(self, num_channels=3, block_expansion=64, num_blocks=4, max_features=512, sn=False, **kwargs): super(Discriminator, self).__init__() @@ -89,9 +85,7 @@ def forward(self, x): class MultiScaleDiscriminator(nn.Module): - """ - Multi-scale (scale) discriminator - """ + """Multi-scale (scale) discriminator.""" def __init__(self, scales=(), **kwargs): super(MultiScaleDiscriminator, self).__init__() diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/generator.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/generator.py index 8c17b8ddb40..44bcc99312f 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/generator.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/generator.py @@ -25,9 +25,7 @@ class OcclusionAwareGenerator(nn.Module): - """ - Generator follows NVIDIA architecture. - """ + """Generator follows NVIDIA architecture.""" def __init__( self, diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/keypoint_detector.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/keypoint_detector.py index d538513ab26..463a236322b 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/keypoint_detector.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/keypoint_detector.py @@ -26,8 +26,9 @@ class KPDetector(nn.Module): - """ - Detecting canonical keypoints. Return keypoint position and jacobian near each keypoint. + """Detecting canonical keypoints. + + Return keypoint position and jacobian near each keypoint. """ def __init__( @@ -86,9 +87,7 @@ def __init__( self.down = AntiAliasInterpolation2d(image_channel, self.scale_factor) def gaussian2kp(self, heatmap): - """ - Extract the mean from a heatmap - """ + """Extract the mean from a heatmap.""" shape = heatmap.shape heatmap = heatmap.unsqueeze(-1) grid = make_coordinate_grid(shape[2:], heatmap.type()).unsqueeze_(0).unsqueeze_(0) @@ -128,9 +127,7 @@ def forward(self, x): class HEEstimator(nn.Module): - """ - Estimating head pose and expression. - """ + """Estimating head pose and expression.""" def __init__( self, block_expansion, feature_channel, num_kp, image_channel, max_features, num_bins=66, estimate_jacobian=True diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/util.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/util.py index e3fd08f5263..ba944f12b8f 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/util.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/modules/util.py @@ -30,9 +30,7 @@ def kp2gaussian(kp, spatial_size, kp_variance): - """ - Transform a keypoint into gaussian like representation - """ + """Transform a keypoint into gaussian like representation.""" mean = kp["value"] coordinate_grid = make_coordinate_grid(spatial_size, mean.type()) @@ -54,9 +52,7 @@ def kp2gaussian(kp, spatial_size, kp_variance): def make_coordinate_grid_2d(spatial_size, type): - """ - Create a meshgrid [-1,1] x [-1,1] of given spatial_size. - """ + """Create a meshgrid [-1,1] x [-1,1] of given spatial_size.""" h, w = spatial_size x = torch.arange(w).type(type) y = torch.arange(h).type(type) @@ -126,9 +122,7 @@ def forward(self, x): class ResBlock2d(nn.Module): - """ - Res block, preserve spatial resolution. - """ + """Res block, preserve spatial resolution.""" def __init__(self, in_features, kernel_size, padding): super(ResBlock2d, self).__init__() @@ -153,9 +147,7 @@ def forward(self, x): class ResBlock3d(nn.Module): - """ - Res block, preserve spatial resolution. - """ + """Res block, preserve spatial resolution.""" def __init__(self, in_features, kernel_size, padding): super(ResBlock3d, self).__init__() @@ -180,9 +172,7 @@ def forward(self, x): class UpBlock2d(nn.Module): - """ - Upsampling block for use in decoder. - """ + """Upsampling block for use in decoder.""" def __init__(self, in_features, out_features, kernel_size=3, padding=1, groups=1): super(UpBlock2d, self).__init__() @@ -201,9 +191,7 @@ def forward(self, x): class UpBlock3d(nn.Module): - """ - Upsampling block for use in decoder. - """ + """Upsampling block for use in decoder.""" def __init__(self, in_features, out_features, kernel_size=3, padding=1, groups=1): super(UpBlock3d, self).__init__() @@ -223,9 +211,7 @@ def forward(self, x): class DownBlock2d(nn.Module): - """ - Downsampling block for use in encoder. - """ + """Downsampling block for use in encoder.""" def __init__(self, in_features, out_features, kernel_size=3, padding=1, groups=1): super(DownBlock2d, self).__init__() @@ -244,16 +230,12 @@ def forward(self, x): class DownBlock3d(nn.Module): - """ - Downsampling block for use in encoder. - """ + """Downsampling block for use in encoder.""" def __init__(self, in_features, out_features, kernel_size=3, padding=1, groups=1): super(DownBlock3d, self).__init__() - """ - self.conv = nn.Conv3d(in_channels=in_features, out_channels=out_features, kernel_size=kernel_size, - padding=padding, groups=groups, stride=(1, 2, 2)) - """ + """self.conv = nn.Conv3d(in_channels=in_features, out_channels=out_features, kernel_size=kernel_size, + padding=padding, groups=groups, stride=(1, 2, 2))""" self.conv = nn.Conv3d( in_channels=in_features, out_channels=out_features, kernel_size=kernel_size, padding=padding, groups=groups ) @@ -269,9 +251,7 @@ def forward(self, x): class SameBlock2d(nn.Module): - """ - Simple block, preserve spatial resolution. - """ + """Simple block, preserve spatial resolution.""" def __init__(self, in_features, out_features, groups=1, kernel_size=3, padding=1, lrelu=False): super(SameBlock2d, self).__init__() @@ -292,9 +272,7 @@ def forward(self, x): class Encoder(nn.Module): - """ - Hourglass Encoder - """ + """Hourglass Encoder.""" def __init__(self, block_expansion, in_features, num_blocks=3, max_features=256): super(Encoder, self).__init__() @@ -319,9 +297,7 @@ def forward(self, x): class Decoder(nn.Module): - """ - Hourglass Decoder - """ + """Hourglass Decoder.""" def __init__(self, block_expansion, in_features, num_blocks=3, max_features=256): super(Decoder, self).__init__() @@ -355,9 +331,7 @@ def forward(self, x): class Hourglass(nn.Module): - """ - Hourglass architecture. - """ + """Hourglass architecture.""" def __init__(self, block_expansion, in_features, num_blocks=3, max_features=256): super(Hourglass, self).__init__() @@ -370,9 +344,7 @@ def forward(self, x): class KPHourglass(nn.Module): - """ - Hourglass architecture. - """ + """Hourglass architecture.""" def __init__(self, block_expansion, in_features, reshape_features, reshape_depth, num_blocks=3, max_features=256): super(KPHourglass, self).__init__() @@ -412,9 +384,7 @@ def forward(self, x): class AntiAliasInterpolation2d(nn.Module): - """ - Band-limited downsampling, for better preservation of the input signal. - """ + """Band-limited downsampling, for better preservation of the input signal.""" def __init__(self, channels, scale): super(AntiAliasInterpolation2d, self).__init__() diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/batchnorm.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/batchnorm.py index aaad60c9c93..9e33c4d474d 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/batchnorm.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/batchnorm.py @@ -29,12 +29,12 @@ def _sum_ft(tensor): - """sum over the first and last dimension""" + """Sum over the first and last dimension.""" return tensor.sum(dim=0).sum(dim=-1) def _unsqueeze_ft(tensor): - """add new dementions at the front and the tail""" + """Add new dementions at the front and the tail.""" return tensor.unsqueeze(0).unsqueeze(-1) @@ -125,8 +125,11 @@ def _data_parallel_master(self, intermediates): return outputs def _compute_mean_std(self, sum_, ssum, size): - """Compute the mean and standard-deviation with sum and square-sum. This method - also maintains the moving average on the master device.""" + """Compute the mean and standard-deviation with sum and square-sum. + + This method + also maintains the moving average on the master device. + """ assert size > 1, "BatchNorm computes unbiased standard-deviation, which requires size > 1." mean = sum_ / size sumvar = ssum - sum_ * mean @@ -203,7 +206,7 @@ def _check_input_dim(self, input): class SynchronizedBatchNorm2d(_SynchronizedBatchNorm): r"""Applies Batch Normalization over a 4d input that is seen as a mini-batch - of 3d inputs + of 3d inputs. .. math:: @@ -265,7 +268,7 @@ def _check_input_dim(self, input): class SynchronizedBatchNorm3d(_SynchronizedBatchNorm): r"""Applies Batch Normalization over a 5d input that is seen as a mini-batch - of 4d inputs + of 4d inputs. .. math:: diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/comm.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/comm.py index b0661100ecc..f8165498f58 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/comm.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/comm.py @@ -23,7 +23,10 @@ class FutureResult(object): - """A thread-safe future implementation. Used only as one-to-one pipe.""" + """A thread-safe future implementation. + + Used only as one-to-one pipe. + """ def __init__(self): self._result = None @@ -89,14 +92,12 @@ def __setstate__(self, state): self.__init__(state["master_callback"]) def register_slave(self, identifier): - """ - Register an slave device. + """Register an slave device. Args: identifier: an identifier, usually is the device id. Returns: a `SlavePipe` object which can be used to communicate with the master device. - """ if self._activated: assert self._queue.empty(), "Queue is not clean before next initialization." @@ -107,8 +108,7 @@ def register_slave(self, identifier): return SlavePipe(identifier, self._queue, future) def run_master(self, master_msg): - """ - Main entry for the master device in each forward pass. + """Main entry for the master device in each forward pass. The messages were first collected from each devices (including the master device), and then an callback will be invoked to compute the message to be sent back to each devices (including the master device). @@ -118,7 +118,6 @@ def run_master(self, master_msg): message when calling `master_callback`. For detailed usage, see `_SynchronizedBatchNorm` for an example. Returns: the message to be sent back to the master device. - """ self._activated = True diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/replicate.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/replicate.py index 071d1563fe7..b593687298a 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/replicate.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/facerender/sync_batchnorm/replicate.py @@ -27,8 +27,7 @@ class CallbackContext(object): def execute_replication_callbacks(modules): - """ - Execute an replication callback `__data_parallel_replicate__` on each module created by original replication. + """Execute an replication callback `__data_parallel_replicate__` on each module created by original replication. The callback will be invoked with arguments `__data_parallel_replicate__(ctx, copy_id)` @@ -50,8 +49,7 @@ def execute_replication_callbacks(modules): class DataParallelWithCallback(DataParallel): - """ - Data Parallel with a replication callback. + """Data Parallel with a replication callback. An replication callback `__data_parallel_replicate__` of each module will be invoked after being created by original `replicate` function. @@ -70,8 +68,7 @@ def replicate(self, module, device_ids): def patch_replication_callback(data_parallel): - """ - Monkey-patch an existing `DataParallel` object. Add the replication callback. + """Monkey-patch an existing `DataParallel` object. Add the replication callback. Useful when you have customized `DataParallel` implementation. Examples: diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/croper.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/croper.py index ddf01b02b21..64e5af36f2e 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/croper.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/croper.py @@ -34,9 +34,8 @@ def __init__(self, device="cuda"): self.predictor = KeypointExtractor(device) def get_landmark(self, img_np): - """get landmark with dlib - :return: np.array shape=(68, 2) - """ + """Get landmark with dlib + :return: np.array shape=(68, 2)""" with torch.no_grad(): dets = self.predictor.det_net.detect_faces(img_np, 0.97) diff --git a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/face_enhancer.py b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/face_enhancer.py index 983ba81c9c3..2cfd5c43beb 100644 --- a/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/face_enhancer.py +++ b/intel_extension_for_transformers/neural_chat/pipeline/plugins/video/face_animation/src/utils/face_enhancer.py @@ -177,8 +177,11 @@ def enhancer_generator_with_len(images, method="gfpgan", bg_upsampler="realesrga def enhancer_generator_no_len(images, method="gfpgan", bg_upsampler="realesrgan"): """Provide a generator function so that all of the enhanced images don't need - to be stored in memory at the same time. This can save tons of RAM compared to - the enhancer function.""" + to be stored in memory at the same time. + + This can save tons of RAM compared to + the enhancer function. + """ print("face enhancer....") if not isinstance(images, list) and os.path.isfile(images): # handle video to images diff --git a/intel_extension_for_transformers/neural_chat/server/base_executor.py b/intel_extension_for_transformers/neural_chat/server/base_executor.py index 813f6b81c53..6f189af4cdd 100644 --- a/intel_extension_for_transformers/neural_chat/server/base_executor.py +++ b/intel_extension_for_transformers/neural_chat/server/base_executor.py @@ -22,17 +22,14 @@ class BaseCommandExecutor(ABC): - """ - An abstract executor class of neuralchat server tasks. - """ + """An abstract executor class of neuralchat server tasks.""" def __init__(self): self.parser = argparse.ArgumentParser() @abstractmethod def execute(self, argv: List[str]) -> bool: - """ - Command line entry. This method can only be accessed by a command line such as `neuralchat textchat`. + """Command line entry. This method can only be accessed by a command line such as `neuralchat textchat`. Args: argv (List[str]): Arguments from command line. @@ -44,7 +41,5 @@ def execute(self, argv: List[str]) -> bool: @abstractmethod def __call__(self, *arg, **kwargs): - """ - Python API to call an executor. - """ + """Python API to call an executor.""" pass diff --git a/intel_extension_for_transformers/neural_chat/server/neuralchat_client.py b/intel_extension_for_transformers/neural_chat/server/neuralchat_client.py index 1e5c2468da6..b2a926e3f03 100644 --- a/intel_extension_for_transformers/neural_chat/server/neuralchat_client.py +++ b/intel_extension_for_transformers/neural_chat/server/neuralchat_client.py @@ -135,9 +135,7 @@ def __call__(self, repetition_penalty: float=1.1, max_new_tokens: int=128, stream: bool=False): - """ - Python API to call an executor. - """ + """Python API to call an executor.""" url = 'http://' + server_ip + ":" + str(port) + '/v1/chat/completions' request = { diff --git a/intel_extension_for_transformers/neural_chat/server/neuralchat_server.py b/intel_extension_for_transformers/neural_chat/server/neuralchat_server.py index 664f032554e..b6fc0572a7b 100644 --- a/intel_extension_for_transformers/neural_chat/server/neuralchat_server.py +++ b/intel_extension_for_transformers/neural_chat/server/neuralchat_server.py @@ -348,9 +348,7 @@ def execute(self, argv: List[str]) -> bool: def __call__(self, config_file: str="./conf/neuralchat.yaml", log_file: str="./log/neuralchat.log"): - """ - Python API to call an executor. - """ + """Python API to call an executor.""" config = get_config(config_file) if self.init(config): logging.basicConfig(filename=log_file, level=logging.INFO) diff --git a/intel_extension_for_transformers/neural_chat/server/restful/api.py b/intel_extension_for_transformers/neural_chat/server/restful/api.py index ba8c0b2c3b8..48f42aae42d 100644 --- a/intel_extension_for_transformers/neural_chat/server/restful/api.py +++ b/intel_extension_for_transformers/neural_chat/server/restful/api.py @@ -60,7 +60,7 @@ def setup_router( port=80, endpoint=None ): - """Setup router for FastAPI + """Setup router for FastAPI. Args: api_list (List): List of API names diff --git a/intel_extension_for_transformers/neural_chat/server/restful/openai_protocol.py b/intel_extension_for_transformers/neural_chat/server/restful/openai_protocol.py index d620e2b1fbe..3e9ee7dc75b 100644 --- a/intel_extension_for_transformers/neural_chat/server/restful/openai_protocol.py +++ b/intel_extension_for_transformers/neural_chat/server/restful/openai_protocol.py @@ -14,9 +14,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +"""Code source from FastChat's OpenAI protocol: -""" -Code source from FastChat's OpenAI protocol: https://github.com/lm-sys/FastChat/blob/main/fastchat/protocol/openai_api_protocol.py """ from enum import IntEnum diff --git a/intel_extension_for_transformers/neural_chat/server/restful/photoai_utils.py b/intel_extension_for_transformers/neural_chat/server/restful/photoai_utils.py index 5f603c9c18f..e6ccd2b6854 100644 --- a/intel_extension_for_transformers/neural_chat/server/restful/photoai_utils.py +++ b/intel_extension_for_transformers/neural_chat/server/restful/photoai_utils.py @@ -27,16 +27,12 @@ def latitude_and_longitude_convert_to_decimal_system(*arg): - """ - Convert latitude&longitude into decimal system - """ + """Convert latitude&longitude into decimal system.""" return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60) def find_GPS_image(pic_path): - """ - generate GPS and timestamp from image - """ + """Generate GPS and timestamp from image.""" GPS = {} date = '' with open(pic_path, 'rb') as f: diff --git a/intel_extension_for_transformers/neural_chat/server/restful/plugin_image2image_api.py b/intel_extension_for_transformers/neural_chat/server/restful/plugin_image2image_api.py index 69bd3692a78..36953dd7198 100644 --- a/intel_extension_for_transformers/neural_chat/server/restful/plugin_image2image_api.py +++ b/intel_extension_for_transformers/neural_chat/server/restful/plugin_image2image_api.py @@ -32,31 +32,31 @@ from ...plugins import plugins class Task(): - """ task class """ + """Task class.""" def __init__(self): self.cond = Condition() self.is_complete = False self.response = None def done(self): - """ function """ + """function.""" logger.info("task is done") self.cond.acquire() self.cond.notify() self.cond.release() def wait_for_done(self): - """ function """ + """function.""" self.cond.acquire() self.cond.wait_for(predicate=self.get_task_status, timeout=None) self.cond.release() def set_prompt(self, prompt): - """ function """ + """function.""" self.prompt = prompt def set_steps(self, num_inference_steps): - """ function """ + """function.""" self.num_inference_steps = num_inference_steps def set_scale(self, guidance_scale): @@ -72,33 +72,33 @@ def set_strength(self, strength): self.strength = strength def set_task_type(self, task_type): - """ set task type """ + """Set task type.""" self.task_type = task_type def set_start_time(self, time): - """ set_start_time for time-out """ + """set_start_time for time-out.""" self.start_time = time def get_task_status(self): return self.is_complete class TaskQueue(): - """ TaskQueue """ + """TaskQueue.""" def __init__(self): self.queue = queue.Queue() def push(self, task): - """ function """ + """function.""" self.queue.put(task) def pop(self): - """ function """ + """function.""" item = self.queue.get_nowait() self.queue.task_done() return item def batch_pop(self, batch_size): - """ function """ + """function.""" result = [] count = 0 while count < batch_size and not self.queue.empty(): @@ -108,11 +108,11 @@ def batch_pop(self, batch_size): return result def empty(self): - """ function """ + """function.""" return self.queue.empty() def join(self): - """ function """ + """function.""" self.queue.join() class Worker(threading.Thread): diff --git a/intel_extension_for_transformers/neural_chat/server/restful/request.py b/intel_extension_for_transformers/neural_chat/server/restful/request.py index d645a60a262..ba8030b1d92 100644 --- a/intel_extension_for_transformers/neural_chat/server/restful/request.py +++ b/intel_extension_for_transformers/neural_chat/server/restful/request.py @@ -64,11 +64,9 @@ class AskDocRequest(RequestBaseModel): class FeedbackRequest(RequestBaseModel): - """ - Request class for feedback api + """Request class for feedback api 'feedback_id': set to be auto_increment, no need to pass as argument - 'feedback': 0 for 'like', 1 for 'dislike' - """ + 'feedback': 0 for 'like', 1 for 'dislike'.""" # feedback_id: Optional[int] = None question: str answer: str diff --git a/intel_extension_for_transformers/neural_chat/server/restful/retrieval_api.py b/intel_extension_for_transformers/neural_chat/server/restful/retrieval_api.py index f8b92f7a6fb..866d559db50 100644 --- a/intel_extension_for_transformers/neural_chat/server/restful/retrieval_api.py +++ b/intel_extension_for_transformers/neural_chat/server/restful/retrieval_api.py @@ -122,8 +122,7 @@ def check_path(path: str): def remove_folder_with_ignore(folder_path: str, except_patterns=None): - """ - Remove the specific folder, and ignore some files/folders + """Remove the specific folder, and ignore some files/folders. :param folder_path: file path to delete :param except_patterns: files/folder name to ignore @@ -575,9 +574,9 @@ def data_generator(): @router.delete("/v1/askdoc/delete_all") async def delete_all_files(): - """ - Delete all files and knowledge bases of current user. - Re-create retriever using default plugin configs. + """Delete all files and knowledge bases of current user. + + Re-create retriever using default plugin configs. """ delete_path = RETRIEVAL_FILE_PATH if not os.path.exists(delete_path): @@ -597,12 +596,12 @@ async def delete_all_files(): @router.delete("/v1/askdoc/delete_file") async def delete_single_file(request: Request): - """ - Delete file according to `del_path` and `knowledge_base_id`. - `del_path`: - - specific file path(e.g. /path/to/file.txt) - - folder path(e.g. /path/to/folder) - - "all_files": delete all files of this knowledge base + """Delete file according to `del_path` and `knowledge_base_id`. + + `del_path`: + - specific file path(e.g. /path/to/file.txt) + - folder path(e.g. /path/to/folder) + - "all_files": delete all files of this knowledge base """ params = await request.json() del_path = params['del_path'] diff --git a/intel_extension_for_transformers/neural_chat/server/restful/textchat_api.py b/intel_extension_for_transformers/neural_chat/server/restful/textchat_api.py index 4ebac846c9d..5e2ff222e65 100644 --- a/intel_extension_for_transformers/neural_chat/server/restful/textchat_api.py +++ b/intel_extension_for_transformers/neural_chat/server/restful/textchat_api.py @@ -463,7 +463,10 @@ def stream_generator(): @router.post("/v1/models") async def show_available_models(): - """Show available models. Right now we only have one model.""" + """Show available models. + + Right now we only have one model. + """ model_cards = [ ModelCard(id=router.get_chatbot().model_name, root=router.get_chatbot().model_name, diff --git a/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/cuda/model.py b/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/cuda/model.py index 9ff7d88fdc7..2000d0d678d 100644 --- a/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/cuda/model.py +++ b/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/cuda/model.py @@ -27,7 +27,9 @@ from intel_extension_for_transformers.neural_chat import build_chatbot, PipelineConfig class TritonPythonModel: - """Your Python model must use the same class name. Every Python model + """Your Python model must use the same class name. + + Every Python model that is created must have "TritonPythonModel" as the class name. """ @@ -72,7 +74,7 @@ def execute(self, requests): Batching) used, `requests` may contain multiple requests. Every Python model, must create one pb_utils.InferenceResponse for every pb_utils.InferenceRequest in `requests`. If there is an error, you can - set the error argument when creating a pb_utils.InferenceResponse + set the error argument when creating a pb_utils.InferenceResponse. Parameters ---------- @@ -126,6 +128,7 @@ def execute(self, requests): def finalize(self): """`finalize` is called only once when the model is being unloaded. + Implementing `finalize` function is OPTIONAL. This function allows the model to perform any necessary clean ups before exit. """ diff --git a/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/model.py b/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/model.py index 50dffb2d4f4..baf5afa2e55 100644 --- a/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/model.py +++ b/intel_extension_for_transformers/neural_chat/serving/triton/text_generation/model.py @@ -27,7 +27,9 @@ from intel_extension_for_transformers.neural_chat import build_chatbot, PipelineConfig class TritonPythonModel: - """Your Python model must use the same class name. Every Python model + """Your Python model must use the same class name. + + Every Python model that is created must have "TritonPythonModel" as the class name. """ @@ -69,7 +71,7 @@ def execute(self, requests): Batching) used, `requests` may contain multiple requests. Every Python model, must create one pb_utils.InferenceResponse for every pb_utils.InferenceRequest in `requests`. If there is an error, you can - set the error argument when creating a pb_utils.InferenceResponse + set the error argument when creating a pb_utils.InferenceResponse. Parameters ---------- @@ -123,6 +125,7 @@ def execute(self, requests): def finalize(self): """`finalize` is called only once when the model is being unloaded. + Implementing `finalize` function is OPTIONAL. This function allows the model to perform any necessary clean ups before exit. """ diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_chatglm.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_chatglm.py index 42984bfa68f..7ac4eee742b 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_chatglm.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_chatglm.py @@ -44,4 +44,4 @@ def test_get_default_conv_template(self): self.assertIn('上海', str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_codellama.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_codellama.py index 85e436dc610..67fbb150192 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_codellama.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_codellama.py @@ -35,4 +35,4 @@ def test_code_gen(self): self.assertIn("Hello World", str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_gguf.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_gguf.py index a20fcd063e4..9edb0d4b31b 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_gguf.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_gguf.py @@ -42,4 +42,4 @@ def test_code_gen_with_gguf(self): self.assertIn('Intel Xeon Scalable Processors', str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_llama.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_llama.py index b008c351b88..3ed2ec78099 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_llama.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_llama.py @@ -42,4 +42,4 @@ def test_get_default_conv_template(self): self.assertIn('Intel Xeon Scalable Processors', str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mistral.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mistral.py index 3585810b504..2a26146c71e 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mistral.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mistral.py @@ -42,4 +42,4 @@ def test_get_default_conv_template(self): self.assertIn('Intel Xeon Scalable processors', str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mpt.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mpt.py index 7dd7e8ec2fc..1b5e8f66d04 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mpt.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_mpt.py @@ -41,4 +41,4 @@ def test_get_default_conv_template(self): self.assertIn('Intel Xeon Scalable processors', str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_neuralchat.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_neuralchat.py index 6b9f881954a..0e90daa1e4f 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_neuralchat.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_neuralchat.py @@ -78,4 +78,4 @@ def test_get_default_conv_template_v3_1(self): if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_phi2.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_phi2.py index c4a9ccb168d..ce484c81583 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_phi2.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_phi2.py @@ -34,4 +34,4 @@ def test_code_gen(self): self.assertIn("99 plus 22 equals 121", str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_qwen.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_qwen.py index dc342e4118e..28de698871a 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_qwen.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_qwen.py @@ -46,4 +46,4 @@ def test_get_default_conv_template(self): self.assertIn('上海', str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_starcoder.py b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_starcoder.py index 17785d7dd3b..03dad5a7fb2 100644 --- a/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_starcoder.py +++ b/intel_extension_for_transformers/neural_chat/tests/nightly/models/test_starcoder.py @@ -34,4 +34,4 @@ def test_code_gen(self): self.assertIn("Hello, world", str(result)) if __name__ == "__main__": - unittest.main() + unittest.main() diff --git a/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/evaluate.py b/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/evaluate.py index 2073c8b0294..d477b20130d 100644 --- a/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/evaluate.py +++ b/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/evaluate.py @@ -71,9 +71,7 @@ def load_list(file_jsonl_path, item): def evaluate(preds, labels, cutoffs=[1]): - """ - Evaluate MRR and Hit at cutoffs. - """ + """Evaluate MRR and Hit at cutoffs.""" metrics = {} # MRR diff --git a/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/finetune.py b/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/finetune.py index 7010ee7539c..43c14795972 100644 --- a/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/finetune.py +++ b/intel_extension_for_transformers/neural_chat/tools/embedding_finetune/finetune.py @@ -35,9 +35,7 @@ @dataclass class ModelArguments: - """ - Arguments pertaining to which model/config/tokenizer we are going to fine-tune from. - """ + """Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.""" model_name_or_path: str config_name: Optional[str] = None tokenizer_name: Optional[str] = None @@ -115,9 +113,9 @@ def __getitem__(self, item) -> Tuple[BatchEncoding, List[BatchEncoding]]: @dataclass class EmbedCollator(DataCollatorWithPadding): - """ - Wrapper that does conversion from List[Tuple[encode_qry, encode_psg]] to List[qry], List[psg] + """Wrapper that does conversion from List[Tuple[encode_qry, encode_psg]] to List[qry], List[psg] and pass batch separately to the actual collator. + Abstract out data detail for the model. """ query_max_len: int = 32 @@ -314,8 +312,7 @@ def _save(self, output_dir: Optional[str] = None, state_dict=None): normalized=self.args.normalized) def compute_loss(self, model, inputs, return_outputs=False): - """ - How the loss is computed by Trainer. By default, all models return the loss in the first element. + """How the loss is computed by Trainer. By default, all models return the loss in the first element. Subclass and override for custom behavior. """ diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/compute_u.py b/intel_extension_for_transformers/neural_chat/tools/rome/compute_u.py index 380e929011e..18aef74a18d 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/compute_u.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/compute_u.py @@ -34,8 +34,8 @@ def get_inv_cov( mom2_n_samples: str, mom2_dtype: str, ) -> torch.Tensor: - """ - Retrieves covariance statistics, then computes the algebraic inverse. + """Retrieves covariance statistics, then computes the algebraic inverse. + Caches result for future use. """ @@ -75,9 +75,7 @@ def compute_u( context_templates: List[str], batch_first: Optional[bool] = True ) -> torch.Tensor: - r""" - Computes the right vector used in constructing the rank-1 update matrix. - """ + r"""Computes the right vector used in constructing the rank-1 update matrix.""" print("Computing left vector (u)...") diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/compute_v.py b/intel_extension_for_transformers/neural_chat/tools/rome/compute_v.py index db35c69ee99..f5342f3a000 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/compute_v.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/compute_v.py @@ -35,8 +35,8 @@ def compute_v( context_templates: List[str], batch_first: Optional[bool] = True ) -> torch.Tensor: - r""" - Computes the value (right) vector for the rank-1 update. + r"""Computes the value (right) vector for the rank-1 update. + Runs a simple optimization procedure. """ @@ -196,9 +196,7 @@ def get_module_input_output_at_word( fact_token_strategy: str, batch_first: Optional[bool] = True ) -> Tuple[torch.Tensor]: - r""" - Retrieves detached representations for a word at the input and output of a particular layer module. - """ + r"""Retrieves detached representations for a word at the input and output of a particular layer module.""" word_repr_args = dict( model=model, @@ -235,9 +233,7 @@ def find_fact_lookup_idx( fact_token_strategy: str, verbose: Optional[bool] = True, ) -> int: - r""" - Computes hypothesized fact lookup index given a sentence and subject. - """ + r"""Computes hypothesized fact lookup index given a sentence and subject.""" ret = None if fact_token_strategy == "last": diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/examples/editor.py b/intel_extension_for_transformers/neural_chat/tools/rome/examples/editor.py index 021958f6a3e..125a7b635f8 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/examples/editor.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/examples/editor.py @@ -23,8 +23,7 @@ from intel_extension_for_transformers.neural_chat.tools.rome import ROMEHyperParams, apply_rome_to_model def print_head(x, pad=3): - r""" - Prints a string with # box for emphasis. + r"""Prints a string with # box for emphasis. Example: @@ -48,8 +47,7 @@ def print_head(x, pad=3): def test_rome( data: str, model: str, config: str, checkpointing: Optional[bool] = False, seed: Optional[int] = 99 ) -> None: - r""" - Edits a pre-trained model using model-editing algorithms. + r"""Edits a pre-trained model using model-editing algorithms. Args: data (`str`): diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/layer_stats.py b/intel_extension_for_transformers/neural_chat/tools/rome/layer_stats.py index 18143c827b1..94e591633f9 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/layer_stats.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/layer_stats.py @@ -53,9 +53,7 @@ def layer_stats( batch_tokens=None, progress=tqdm, ): - """ - Function to load or compute cached stats. - """ + """Function to load or compute cached stats.""" def get_ds(): raw_ds = load_dataset( diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/repr_tools.py b/intel_extension_for_transformers/neural_chat/tools/rome/repr_tools.py index 3c8ddb101cf..c192cb7cdfe 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/repr_tools.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/repr_tools.py @@ -14,10 +14,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +"""Contains utilities for extracting token representations and indices +from string templates. -""" -Contains utilities for extracting token representations and indices -from string templates. Used in computing the left and right vectors for ROME. +Used in computing the left and right vectors for ROME. """ import torch @@ -38,9 +38,10 @@ def get_reprs_at_word_tokens( track: Optional[Literal["in", "out", "both"]] = "in", batch_first: Optional[bool] = True ) -> torch.Tensor: - r""" - Retrieves the last token representation of `word` in `context_template` - when `word` is substituted into `context_template`. See `get_last_word_idx_in_template` + r"""Retrieves the last token representation of `word` in `context_template` + when `word` is substituted into `context_template`. + + See `get_last_word_idx_in_template` for more details. """ @@ -60,8 +61,7 @@ def get_reprs_at_word_tokens( def get_words_idxs_in_templates( tokenizer: PreTrainedTokenizer, context_templates: List[str], words: List[str], subtoken: str ) -> List[List[int]]: - r""" - Given list of template strings, each with *one* format specifier + r"""Given list of template strings, each with *one* format specifier (e.g. "{} plays basketball"), and words to be substituted into the template, computes the post-tokenization index of their last tokens. @@ -107,10 +107,8 @@ def get_reprs_at_idxs( track: Optional[Literal["in", "out", "both"]] = "in", batch_first: Optional[bool] = True ) -> torch.Tensor: - r""" - Runs input through model and returns averaged representations of the tokens - at each index in `idxs`. - """ + r"""Runs input through model and returns averaged representations of the tokens + at each index in `idxs`.""" if track == "both": to_return = {"in": [], "out": []} diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/rome_hparams.py b/intel_extension_for_transformers/neural_chat/tools/rome/rome_hparams.py index 6d2c0f36c20..a9ecd01aeee 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/rome_hparams.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/rome_hparams.py @@ -93,9 +93,7 @@ def from_name(cls, name: str): ln_f_module="model.norm" )) elif name == "llama-13b": - r""" - Supports LLaMA-13B, LLaMA-2-13B, Baichuan-13B... - """ + r"""Supports LLaMA-13B, LLaMA-2-13B, Baichuan-13B...""" data.update(dict( layers=[10], v_loss_layer=39, diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/rome_impl.py b/intel_extension_for_transformers/neural_chat/tools/rome/rome_impl.py index 3a5dd5d436f..2c8e842c558 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/rome_impl.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/rome_impl.py @@ -38,8 +38,7 @@ def apply_rome_to_model( copy: Optional[bool] = False, return_diff_weights: Optional[bool] = False ) -> Tuple[PreTrainedModel, Dict[str, torch.Tensor]]: - r""" - Edits a pre-trained model using model-editing algorithms. + r"""Edits a pre-trained model using model-editing algorithms. Args: model (`PreTrainedModel`): @@ -173,8 +172,8 @@ def execute_rome( def upd_matrix_match_shape(matrix: torch.Tensor, shape: torch.Size) -> torch.Tensor: - r""" - GPT-2 and GPT-J have transposed weight representations. + r"""GPT-2 and GPT-J have transposed weight representations. + Returns a matrix that matches the desired shape, else raises a ValueError """ diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/tok_dataset.py b/intel_extension_for_transformers/neural_chat/tools/rome/tok_dataset.py index 769727fdef2..9cfbce7983f 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/tok_dataset.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/tok_dataset.py @@ -21,9 +21,10 @@ class TokenizedDataset(Dataset): - """ - Converts a dataset of text samples into a dataset of token sequences, - as converted by a supplied tokenizer. The tokens come along with position + """Converts a dataset of text samples into a dataset of token sequences, + as converted by a supplied tokenizer. + + The tokens come along with position ids and attention masks, they can be supplied directly to the model. """ @@ -55,18 +56,17 @@ def __getitem__(self, i): def dict_to_(data, device): - """ - Moves a dictionary of tensors to the specified device. - """ + """Moves a dictionary of tensors to the specified device.""" for k in data: data[k] = data[k].to(device) return data def length_collation(token_size): - """ - Sorts a batch of sequences and breaks it up into subbatches - of same-sized sequences, padding as needed. Each batch + """Sorts a batch of sequences and breaks it up into subbatches + of same-sized sequences, padding as needed. + + Each batch has no more than token_size total tokens (or a single sequence, if the sequence happens to be larger). """ @@ -95,9 +95,7 @@ def collate_fn(items): def make_padded_batch(items): - """ - Pads sequences in a batch, so they are all the same length as the longest. - """ + """Pads sequences in a batch, so they are all the same length as the longest.""" max_len = max(len(d["input_ids"]) for d in items) if max_len == 0: return {k: torch.zeros((0, 0), dtype=torch.long) for k in items[0]} @@ -108,9 +106,7 @@ def make_padded_batch(items): def flatten_masked_batch(data, mask): - """ - Flattens feature data, ignoring items that are masked out of attention. - """ + """Flattens feature data, ignoring items that are masked out of attention.""" flat_data = data.view(-1, data.size(-1)) attended_tokens = mask.view(-1).nonzero()[:, 0] return flat_data[attended_tokens] diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/utils/nethook.py b/intel_extension_for_transformers/neural_chat/tools/rome/utils/nethook.py index 83525dae98d..c1d0cb9d10a 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/utils/nethook.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/utils/nethook.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -""" -Utilities for instrumenting a torch model. +"""Utilities for instrumenting a torch model. Trace will hook one layer at a time. TraceDict will hook multiple layers at once. @@ -76,10 +74,8 @@ def __init__( edit_output=None, stop=False, ): - r""" - Method to replace a forward method with a closure that - intercepts the call, and tracks the hook so that it can be reverted. - """ + r"""Method to replace a forward method with a closure that + intercepts the call, and tracks the hook so that it can be reverted.""" retainer = self self.layer = layer if layer is not None: @@ -214,9 +210,10 @@ class StopForward(Exception): def recursive_copy(x, clone=None, detach=None, retain_grad=None): - r""" - Copies a reference to a tensor, or an object that contains tensors, - optionally detaching and cloning the tensor(s). If retain_grad is + r"""Copies a reference to a tensor, or an object that contains tensors, + optionally detaching and cloning the tensor(s). + + If retain_grad is true, the original tensors are marked to have grads retained. """ if not clone and not detach and not retain_grad: @@ -249,8 +246,7 @@ def subsequence( single_layer=None, share_weights=False, ): - r""" - Creates a subsequence of a pytorch Sequential model, copying over + r"""Creates a subsequence of a pytorch Sequential model, copying over modules together with parameters for the subsequence. Only modules from first_layer to last_layer (inclusive) are included, or modules between after_layer and upto_layer (exclusive). @@ -284,9 +280,10 @@ def subsequence( def hierarchical_subsequence( sequential, first, last, after, upto, share_weights=False, depth=0 ): - r""" - Recursive helper for subsequence() to support descent into dotted - layer names. In this helper, first, last, after, and upto are + r"""Recursive helper for subsequence() to support descent into dotted + layer names. + + In this helper, first, last, after, and upto are arrays of names resulting from splitting on dots. Can only descend into nested Sequentials. """ @@ -355,10 +352,8 @@ def hierarchical_subsequence( def set_requires_grad(requires_grad, *models): - r""" - Sets requires_grad true or false for all parameters within the - models passed. - """ + r"""Sets requires_grad true or false for all parameters within the + models passed.""" for model in models: if isinstance(model, torch.nn.Module): for param in model.parameters(): @@ -370,9 +365,7 @@ def set_requires_grad(requires_grad, *models): def get_module(model, name): - r""" - Finds the named module within the given model. - """ + r"""Finds the named module within the given model.""" for n, m in model.named_modules(): if n == name: return m @@ -380,9 +373,7 @@ def get_module(model, name): def get_parameter(model, name): - r""" - Finds the named parameter within the given model. - """ + r"""Finds the named parameter within the given model.""" for n, p in model.named_parameters(): if n == name: return p @@ -390,9 +381,7 @@ def get_parameter(model, name): def replace_module(model, name, new_module): - r""" - Replaces the named module within the given model. - """ + r"""Replaces the named module within the given model.""" if "." in name: parent_name, attr_name = name.rsplit(".", 1) model = get_module(model, parent_name) @@ -401,10 +390,10 @@ def replace_module(model, name, new_module): def invoke_with_optional_args(fn, *args, **kwargs): - r""" - Invokes a function with only the arguments that it + r"""Invokes a function with only the arguments that it is written to accept, giving priority to arguments that match by-name, using the following rules. + (1) arguments with matching names are passed by name. (2) remaining non-name-matched args are passed by order. (3) extra caller arguments that the function cannot diff --git a/intel_extension_for_transformers/neural_chat/tools/rome/utils/runningstats.py b/intel_extension_for_transformers/neural_chat/tools/rome/utils/runningstats.py index 0877697983b..025f58fcc31 100644 --- a/intel_extension_for_transformers/neural_chat/tools/rome/utils/runningstats.py +++ b/intel_extension_for_transformers/neural_chat/tools/rome/utils/runningstats.py @@ -14,9 +14,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -""" -To use a runningstats object, +"""To use a runningstats object, 1. Create the the desired stat object, e.g., `m = Mean()` 2. Feed it batches via the add method, e.g., `m.add(batch)` @@ -69,8 +67,7 @@ def tally(stat, dataset, cache=None, quiet=False, **kwargs): - """ - To use tally, write code like the following. + """To use tally, write code like the following. stat = Mean() ds = MyDataset() @@ -142,10 +139,8 @@ def wrapped_loader(): class cache_load_enabled: - """ - When used as a context manager, cache_load_enabled(False) will prevent - tally from loading cached statsitics, forcing them to be recomputed. - """ + """When used as a context manager, cache_load_enabled(False) will prevent + tally from loading cached statsitics, forcing them to be recomputed.""" def __init__(self, enabled=True): self.prev = False @@ -162,73 +157,53 @@ def __exit__(self, exc_type, exc_value, traceback): class Stat: - """ - Abstract base class for a running pytorch statistic. - """ + """Abstract base class for a running pytorch statistic.""" def __init__(self, state): - """ - By convention, all Stat subclasses can be initialized by passing - state=; and then they will initialize by calling load_state_dict. - """ + """By convention, all Stat subclasses can be initialized by passing + state=; and then they will initialize by calling load_state_dict.""" self.load_state_dict(resolve_state_dict(state)) def add(self, x, *args, **kwargs): - """ - Observes a batch of samples to be incorporated into the statistic. + """Observes a batch of samples to be incorporated into the statistic. + Dimension 0 should be the batch dimension, and dimension 1 should be the feature dimension of the pytorch tensor x. """ pass def load_state_dict(self, d): - """ - Loads this Stat from a dictionary of numpy arrays as saved - by state_dict. - """ + """Loads this Stat from a dictionary of numpy arrays as saved + by state_dict.""" pass def state_dict(self): - """ - Saves this Stat as a dictionary of numpy arrays that can be - stored in an npz or reloaded later using load_state_dict. - """ + """Saves this Stat as a dictionary of numpy arrays that can be + stored in an npz or reloaded later using load_state_dict.""" return {} def save(self, filename): - """ - Saves this stat as an npz file containing the state_dict. - """ + """Saves this stat as an npz file containing the state_dict.""" save_cached_state(filename, self, {}) def load(self, filename): - """ - Loads this stat from an npz file containing a saved state_dict. - """ + """Loads this stat from an npz file containing a saved state_dict.""" self.load_state_dict(load_cached_state(filename, {}, quiet=True, throw=True)) def to_(self, device): - """ - Moves this Stat to the given device. - """ + """Moves this Stat to the given device.""" pass def cpu_(self): - """ - Moves this Stat to the cpu device. - """ + """Moves this Stat to the cpu device.""" self.to_("cpu") def cuda_(self): - """ - Moves this Stat to the default cuda device. - """ + """Moves this Stat to the default cuda device.""" self.to_("cuda") def _normalize_add_shape(self, x, attr="data_shape"): - """ - Flattens input data to 2d. - """ + """Flattens input data to 2d.""" if not torch.is_tensor(x): x = torch.tensor(x) if len(x.shape) < 1: @@ -242,9 +217,7 @@ def _normalize_add_shape(self, x, attr="data_shape"): return x.view(x.shape[0], int(numpy.prod(data_shape))) def _restore_result_shape(self, x, attr="data_shape"): - """ - Restores output data to input data shape. - """ + """Restores output data to input data shape.""" data_shape = getattr(self, attr, None) if data_shape is None: return x @@ -252,9 +225,7 @@ def _restore_result_shape(self, x, attr="data_shape"): class Mean(Stat): - """ - Running mean. - """ + """Running mean.""" def __init__(self, state=None): if state is not None: @@ -312,9 +283,7 @@ def state_dict(self): class NormMean(Mean): - """ - Running average of the norm of input vectors - """ + """Running average of the norm of input vectors.""" def __init__(self, state=None): super().__init__(state) @@ -324,8 +293,9 @@ def add(self, a): class Variance(Stat): - """ - Running computation of mean and variance. Use this when you just need + """Running computation of mean and variance. + + Use this when you just need basic stats without covariance. """ @@ -404,8 +374,7 @@ def state_dict(self): class Covariance(Stat): - """ - Running computation. Use this when the entire covariance matrix is needed, + """Running computation. Use this when the entire covariance matrix is needed, and when the whole covariance matrix fits in the GPU. Chan-style numerically stable update of mean and full covariance matrix. @@ -487,8 +456,9 @@ def load_state_dict(self, state): class SecondMoment(Stat): - """ - Running computation. Use this when the entire non-centered 2nd-moment + """Running computation. + + Use this when the entire non-centered 2nd-moment 'covariance-like' matrix is needed, and when the whole matrix fits in the GPU. """ @@ -532,8 +502,9 @@ def load_state_dict(self, state): class Bincount(Stat): - """ - Running bincount. The counted array should be an integer type with + """Running bincount. + + The counted array should be an integer type with non-negative integers. """ @@ -580,8 +551,7 @@ def load_state_dict(self, dic): class CrossCovariance(Stat): - """ - Covariance. Use this when an off-diagonal block of the covariance + """Covariance. Use this when an off-diagonal block of the covariance matrix is needed (e.g., when the whole covariance matrix does not fit in the GPU, this could use a quarter of the memory). @@ -676,8 +646,7 @@ def load_state_dict(self, state): def _float_from_bool(a): - """ - Since pytorch only supports matrix multiplication on float, + """Since pytorch only supports matrix multiplication on float, IoU computations are done using floating point types. This function binarizes the input (positive to True and @@ -693,9 +662,7 @@ def _float_from_bool(a): class IoU(Stat): - """ - Running computation of intersections and unions of all features. - """ + """Running computation of intersections and unions of all features.""" def __init__(self, state=None): if state is not None: @@ -741,9 +708,7 @@ def load_state_dict(self, state): class CrossIoU(Stat): - """ - Running computation of intersections and unions of two binary vectors. - """ + """Running computation of intersections and unions of two binary vectors.""" def __init__(self, state=None): if state is not None: @@ -805,8 +770,7 @@ def load_state_dict(self, state): class Quantile(Stat): - """ - Streaming randomized quantile computation for torch. + """Streaming randomized quantile computation for torch. Add any amount of data repeatedly via add(data). At any time, quantile estimates be read out using quantile(q). @@ -1143,11 +1107,9 @@ def readout(self, count=1001): return self.quantiles(torch.linspace(0.0, 1.0, count)) def normalize(self, data): - """ - Given input data as taken from the training distribution, + """Given input data as taken from the training distribution, normalizes every channel to reflect quantile values, - uniformly distributed, within [0, 1]. - """ + uniformly distributed, within [0, 1].""" assert self.count > 0 assert data.shape[0] == self.depth summary, weights = self._weighted_summary() @@ -1171,8 +1133,9 @@ def normalize(self, data): def sample_portion(vec, p=0.5): - """ - Subsamples a fraction (given by p) of the given batch. Used by + """Subsamples a fraction (given by p) of the given batch. + + Used by Quantile when the data gets very very large. """ bits = torch.bernoulli( @@ -1182,8 +1145,7 @@ def sample_portion(vec, p=0.5): class TopK: - """ - A class to keep a running tally of the the top k values (and indexes) + """A class to keep a running tally of the the top k values (and indexes) of any number of torch feature components. Will work on the GPU if the data is on the GPU. Tracks largest by default, but tracks smallest if largest=False is passed. @@ -1208,8 +1170,8 @@ def __init__(self, k=100, largest=True, state=None): self.largest = largest def add(self, data, index=None): - """ - Adds a batch of data to be considered for the running top k. + """Adds a batch of data to be considered for the running top k. + The zeroth dimension enumerates the observations. All other dimensions enumerate different features. """ @@ -1254,10 +1216,8 @@ def size(self): return self.count def topk(self, sorted=True, flat=False): - """ - Returns top k data items and indexes in each dimension, - with channels in the first dimension and k in the last dimension. - """ + """Returns top k data items and indexes in each dimension, + with channels in the first dimension and k in the last dimension.""" k = min(self.k, self.next) # bti are top indexes relative to buffer array. td, bti = self.top_data[:, : self.next].topk( @@ -1319,9 +1279,7 @@ def load_state_dict(self, state): class History(Stat): - """ - Accumulates the concatenation of all the added data. - """ + """Accumulates the concatenation of all the added data.""" def __init__(self, data=None, state=None): if state is not None: @@ -1409,18 +1367,14 @@ def to_(self, device): def push_key_prefix(prefix, d): - """ - Returns a dict with the same values as d, but where each key - adds the prefix, followed by a dot. - """ + """Returns a dict with the same values as d, but where each key + adds the prefix, followed by a dot.""" return {prefix + "." + k: v for k, v in d.items()} def pull_key_prefix(prefix, d): - """ - Returns a filtered dict of all the items of d that start with - the given key prefix, plus a dot, with that prefix removed. - """ + """Returns a filtered dict of all the items of d that start with + the given key prefix, plus a dot, with that prefix removed.""" pd = prefix + "." lpd = len(pd) return {k[lpd:]: v for k, v in d.items() if k.startswith(pd)} @@ -1440,9 +1394,7 @@ def pull_key_prefix(prefix, d): def is_null_numpy_value(v): - """ - True if v is a 64-bit float numpy scalar NaN matching null_numpy_value. - """ + """True if v is a 64-bit float numpy scalar NaN matching null_numpy_value.""" return ( isinstance(v, numpy.ndarray) and numpy.ndim(v) == 0 @@ -1453,8 +1405,8 @@ def is_null_numpy_value(v): def box_numpy_null(d): - """ - Replaces None with null_numpy_value, leaving non-None values unchanged. + """Replaces None with null_numpy_value, leaving non-None values unchanged. + Recursively descends into a dictionary replacing None values. """ try: @@ -1464,8 +1416,8 @@ def box_numpy_null(d): def unbox_numpy_null(d): - """ - Reverses box_numpy_null, replacing null_numpy_value with None. + """Reverses box_numpy_null, replacing null_numpy_value with None. + Recursively descends into a dictionary replacing None values. """ try: @@ -1475,18 +1427,14 @@ def unbox_numpy_null(d): def resolve_state_dict(s): - """ - Resolves a state, which can be a filename or a dict-like object. - """ + """Resolves a state, which can be a filename or a dict-like object.""" if isinstance(s, str): return unbox_numpy_null(numpy.load(s)) return s def load_cached_state(cachefile, args, quiet=False, throw=False): - """ - Resolves a state, which can be a filename or a dict-like object. - """ + """Resolves a state, which can be a filename or a dict-like object.""" if not global_load_cache_enabled or cachefile is None: return None try: @@ -1511,9 +1459,7 @@ def load_cached_state(cachefile, args, quiet=False, throw=False): def save_cached_state(cachefile, obj, args): - """ - Saves the state_dict of the given object in a dict or npz file. - """ + """Saves the state_dict of the given object in a dict or npz file.""" if cachefile is None: return dat = obj.state_dict() @@ -1531,6 +1477,7 @@ def save_cached_state(cachefile, obj, args): class FixedSubsetSampler(Sampler): """Represents a fixed sequence of data set indices. + Subsets can be created by specifying a subset of output indexes. """ @@ -1550,15 +1497,14 @@ def subset(self, new_subset): return FixedSubsetSampler(self.dereference(new_subset)) def dereference(self, indices): - """ - Translate output sample indices (small numbers indexing the sample) - to input sample indices (larger number indexing the original full set) - """ + """Translate output sample indices (small numbers indexing the sample) + to input sample indices (larger number indexing the original full set)""" return [self.samples[i] for i in indices] class FixedRandomSubsetSampler(FixedSubsetSampler): """Samples a fixed number of samples from the dataset, deterministically. + Arguments: data_source, sample_size, @@ -1573,9 +1519,7 @@ def __init__(self, data_source, start=None, end=None, seed=1): super(FixedRandomSubsetSampler, self).__init__(shuffled[start:end]) def class_subset(self, class_filter): - """ - Returns only the subset matching the given rule. - """ + """Returns only the subset matching the given rule.""" if isinstance(class_filter, int): def rule(d): diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/.eslintrc.cjs b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/.eslintrc.cjs index b5e265e2551..4fcef12f6cb 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/.eslintrc.cjs +++ b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/.eslintrc.cjs @@ -1,3 +1,17 @@ +// Copyright (c) 2024 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + module.exports = { root: true, parser: "@typescript-eslint/parser", diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/postcss.config.cjs b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/postcss.config.cjs index fe10e55a833..40ae96a449f 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/postcss.config.cjs +++ b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/postcss.config.cjs @@ -1,3 +1,17 @@ +// Copyright (c) 2024 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + const tailwindcss = require('tailwindcss'); const autoprefixer = require('autoprefixer'); diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.d.ts b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.d.ts index 75943652d59..2ebb889da7e 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.d.ts +++ b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.d.ts @@ -1,3 +1,17 @@ +// Copyright (c) 2024 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // See: https://kit.svelte.dev/docs/types#app // import { Result} from "neverthrow"; interface Window { diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.html b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.html index bc53a3b2b32..83b9c1611a3 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.html +++ b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/app.html @@ -1,3 +1,19 @@ + + diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/lib/assets/avatar/svelte/Delete.svelte b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/lib/assets/avatar/svelte/Delete.svelte index 074ddbb938e..300858a26d7 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/lib/assets/avatar/svelte/Delete.svelte +++ b/intel_extension_for_transformers/neural_chat/ui/customized/side_by_side/src/lib/assets/avatar/svelte/Delete.svelte @@ -1,3 +1,19 @@ + + diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/assets/chat/svelte/ArrowDown.svelte b/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/assets/chat/svelte/ArrowDown.svelte index c12603bec98..dcca69e22f9 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/assets/chat/svelte/ArrowDown.svelte +++ b/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/assets/chat/svelte/ArrowDown.svelte @@ -1,3 +1,19 @@ + +
diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/scrollbar/Scrollbar.svelte b/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/scrollbar/Scrollbar.svelte index 3e9df91ebf1..28957f4f4b5 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/scrollbar/Scrollbar.svelte +++ b/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/scrollbar/Scrollbar.svelte @@ -1,3 +1,19 @@ + + diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/talkbot/talking-knowledge-card.svelte b/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/talkbot/talking-knowledge-card.svelte index ed69383b4fc..70cf0851f4d 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/talkbot/talking-knowledge-card.svelte +++ b/intel_extension_for_transformers/neural_chat/ui/customized/talking_photo/src/lib/shared/components/talkbot/talking-knowledge-card.svelte @@ -1,3 +1,19 @@ + + diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/talkingbot/src/lib/assets/icons/Edit.svelte b/intel_extension_for_transformers/neural_chat/ui/customized/talkingbot/src/lib/assets/icons/Edit.svelte index 18e794e27f1..ce5d62caa3e 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/talkingbot/src/lib/assets/icons/Edit.svelte +++ b/intel_extension_for_transformers/neural_chat/ui/customized/talkingbot/src/lib/assets/icons/Edit.svelte @@ -1,3 +1,19 @@ + + + diff --git a/intel_extension_for_transformers/neural_chat/ui/customized/vision_demo/src/lib/assets/chat/svelte/Assistant.svelte b/intel_extension_for_transformers/neural_chat/ui/customized/vision_demo/src/lib/assets/chat/svelte/Assistant.svelte index 125dc62437f..6bf6298a98a 100644 --- a/intel_extension_for_transformers/neural_chat/ui/customized/vision_demo/src/lib/assets/chat/svelte/Assistant.svelte +++ b/intel_extension_for_transformers/neural_chat/ui/customized/vision_demo/src/lib/assets/chat/svelte/Assistant.svelte @@ -1,3 +1,19 @@ + + + diff --git a/workflows/chatbot/demo/advanced_frontend/src/routes/chat/[chatId]/+page.svelte b/workflows/chatbot/demo/advanced_frontend/src/routes/chat/[chatId]/+page.svelte index a676c3fde64..59de79c0e8c 100644 --- a/workflows/chatbot/demo/advanced_frontend/src/routes/chat/[chatId]/+page.svelte +++ b/workflows/chatbot/demo/advanced_frontend/src/routes/chat/[chatId]/+page.svelte @@ -1,3 +1,19 @@ + +