From ef1b453c359d6d564eb2e5ceb54e987638ad1193 Mon Sep 17 00:00:00 2001 From: Dave <69651599+D4ve-R@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:13:48 +0100 Subject: [PATCH 01/13] add stablelm model impl. --- src/models.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/models.js b/src/models.js index cf7807300..6e24b8022 100644 --- a/src/models.js +++ b/src/models.js @@ -5194,6 +5194,44 @@ export class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { ////////////////////////////////////////////////// +////////////////////////////////////////////////// +// StableLM models +export class StableLMPreTrainedModel extends PreTrainedModel { } + +/** + * The bare StableLM Model transformer outputting raw hidden-states without any specific head on top. + */ +export class StableLMModel extends StableLMPreTrainedModel { } + +/** + * StableLM Model with a `language modeling` head on top for Causal Language Modeling (with past). + */ +export class StableLMForCausalLM extends StableLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for causal language modeling. + */ + async _call(model_inputs) { + return new CausalLMOutputWithPast(await super._call(model_inputs)); + } +} + +/** + * StableLM Model with a sequence classification head on top (with Past). + */ +export class StableLMForSequenceClassification extends StableLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutputWithPast(await super._call(model_inputs)); + } +} + +////////////////////////////////////////////////// ////////////////////////////////////////////////// // AutoModels, used to simplify construction of PreTrainedModels @@ -5845,6 +5883,23 @@ export class SequenceClassifierOutput extends ModelOutput { } } +/** + * Base class for outputs of sentence classification models. + */ +export class SequenceClassifierOutputWithPast extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). + * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) + * that can be used (see `past_key_values` input) to speed up sequential decoding. + */ + constructor({ logits, past_key_values }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + } +} + /** * Base class for outputs of XVector models. */ From 01c8cf4112edc7eed6a73a6a24d658938ee58108 Mon Sep 17 00:00:00 2001 From: Dave <69651599+D4ve-R@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:20:43 +0100 Subject: [PATCH 02/13] add stablelm mapping --- src/models.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/models.js b/src/models.js index 6e24b8022..9b3644001 100644 --- a/src/models.js +++ b/src/models.js @@ -5418,6 +5418,7 @@ const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = new Map([ ['mbart', ['MBartForSequenceClassification', MBartForSequenceClassification]], ['mobilebert', ['MobileBertForSequenceClassification', MobileBertForSequenceClassification]], ['squeezebert', ['SqueezeBertForSequenceClassification', SqueezeBertForSequenceClassification]], + ['stablelm', ['StableLMForSequenceClassification', StableLMForSequenceClassification]], ]); const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = new Map([ @@ -5465,6 +5466,7 @@ const MODEL_WITH_LM_HEAD_MAPPING_NAMES = new Map([ ['mistral', ['MistralForCausalLM', MistralForCausalLM]], ['falcon', ['FalconForCausalLM', FalconForCausalLM]], ['trocr', ['TrOCRForCausalLM', TrOCRForCausalLM]], + ['stablelm', ['StableLMForCausalLM', StableLMForCausalLM]], ]); const MODEL_FOR_MASKED_LM_MAPPING_NAMES = new Map([ From 1c8a7b8aa9c43b39e022c82c5a9184a1525ef64d Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Fri, 1 Mar 2024 21:09:08 +0000 Subject: [PATCH 03/13] Update `StableLMPreTrainedModel` class config --- src/models.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/models.js b/src/models.js index 9b3644001..8f93ea482 100644 --- a/src/models.js +++ b/src/models.js @@ -5196,7 +5196,25 @@ export class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { ////////////////////////////////////////////////// // StableLM models -export class StableLMPreTrainedModel extends PreTrainedModel { } +export class StableLMPreTrainedModel extends PreTrainedModel { + /** + * Creates a new instance of the `StableLMPreTrainedModel` class. + * @param {Object} config The configuration of the model. + * @param {any} session The ONNX session containing the model weights. + * @param {GenerationConfig} generation_config The generation configuration. + */ + constructor(config, session, generation_config) { + super(config, session); + this.generation_config = generation_config; + + // config doesn't contain pad_token_id, so we assume it is the eos_token_id + this.config.pad_token_id = this.config.eos_token_id + + this.num_heads = this.config.num_attention_heads; + this.num_layers = this.config.num_hidden_layers; + this.dim_kv = this.config.hidden_size / this.num_heads; + } +} /** * The bare StableLM Model transformer outputting raw hidden-states without any specific head on top. From 303cd1acd935bb1dd95346223f5634b2dfa6164e Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Sat, 2 Mar 2024 12:35:26 +0000 Subject: [PATCH 04/13] Update stablelm conversion script quantization settings --- scripts/convert.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/convert.py b/scripts/convert.py index 5b8620471..e45f88c14 100644 --- a/scripts/convert.py +++ b/scripts/convert.py @@ -83,6 +83,10 @@ 'per_channel': False, 'reduce_range': False, }, + 'stablelm': { + 'per_channel': False, + 'reduce_range': False, + }, # Encoder-decoder models 'whisper': { From 35d14b6963417021e601a16c431440f8f48adb8a Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Sat, 2 Mar 2024 12:37:15 +0000 Subject: [PATCH 05/13] Add StableLm to list of supported models --- README.md | 1 + docs/snippets/6_supported-models.snippet | 1 + scripts/supported_models.py | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 4fcaf68f5..0f7f4f0c3 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,7 @@ You can refine your search by selecting the task you're interested in (e.g., [te 1. **[SigLIP](https://huggingface.co/docs/transformers/main/model_doc/siglip)** (from Google AI) released with the paper [Sigmoid Loss for Language Image Pre-Training](https://arxiv.org/abs/2303.15343) by Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov, Lucas Beyer. 1. **[SpeechT5](https://huggingface.co/docs/transformers/model_doc/speecht5)** (from Microsoft Research) released with the paper [SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing](https://arxiv.org/abs/2110.07205) by Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. 1. **[SqueezeBERT](https://huggingface.co/docs/transformers/model_doc/squeezebert)** (from Berkeley) released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://arxiv.org/abs/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer. +1. **[StableLm](https://huggingface.co/docs/transformers/model_doc/stablelm)** (from Stability AI) released with the paper [StableLM 3B 4E1T (Technical Report)](https://stability.wandb.io/stability-llm/stable-lm/reports/StableLM-3B-4E1T--VmlldzoyMjU4?accessToken=u3zujipenkx5g7rtcj9qojjgxpconyjktjkli2po09nffrffdhhchq045vp0wyfo) by Jonathan Tow, Marco Bellagente, Dakota Mahan, Carlos Riquelme Ruiz, Duy Phung, Maksym Zhuravinskyi, Nathan Cooper, Nikhil Pinnaparaju, Reshinth Adithyan, and James Baicoianu. 1. **[Swin Transformer](https://huggingface.co/docs/transformers/model_doc/swin)** (from Microsoft) released with the paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://arxiv.org/abs/2103.14030) by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo. 1. **[Swin2SR](https://huggingface.co/docs/transformers/model_doc/swin2sr)** (from University of Würzburg) released with the paper [Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration](https://arxiv.org/abs/2209.11345) by Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte. 1. **[T5](https://huggingface.co/docs/transformers/model_doc/t5)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. diff --git a/docs/snippets/6_supported-models.snippet b/docs/snippets/6_supported-models.snippet index ed2c1bdff..a21a2aeb1 100644 --- a/docs/snippets/6_supported-models.snippet +++ b/docs/snippets/6_supported-models.snippet @@ -70,6 +70,7 @@ 1. **[SigLIP](https://huggingface.co/docs/transformers/main/model_doc/siglip)** (from Google AI) released with the paper [Sigmoid Loss for Language Image Pre-Training](https://arxiv.org/abs/2303.15343) by Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov, Lucas Beyer. 1. **[SpeechT5](https://huggingface.co/docs/transformers/model_doc/speecht5)** (from Microsoft Research) released with the paper [SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing](https://arxiv.org/abs/2110.07205) by Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. 1. **[SqueezeBERT](https://huggingface.co/docs/transformers/model_doc/squeezebert)** (from Berkeley) released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://arxiv.org/abs/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer. +1. **[StableLm](https://huggingface.co/docs/transformers/model_doc/stablelm)** (from Stability AI) released with the paper [StableLM 3B 4E1T (Technical Report)](https://stability.wandb.io/stability-llm/stable-lm/reports/StableLM-3B-4E1T--VmlldzoyMjU4?accessToken=u3zujipenkx5g7rtcj9qojjgxpconyjktjkli2po09nffrffdhhchq045vp0wyfo) by Jonathan Tow, Marco Bellagente, Dakota Mahan, Carlos Riquelme Ruiz, Duy Phung, Maksym Zhuravinskyi, Nathan Cooper, Nikhil Pinnaparaju, Reshinth Adithyan, and James Baicoianu. 1. **[Swin Transformer](https://huggingface.co/docs/transformers/model_doc/swin)** (from Microsoft) released with the paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://arxiv.org/abs/2103.14030) by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo. 1. **[Swin2SR](https://huggingface.co/docs/transformers/model_doc/swin2sr)** (from University of Würzburg) released with the paper [Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration](https://arxiv.org/abs/2209.11345) by Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte. 1. **[T5](https://huggingface.co/docs/transformers/model_doc/t5)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. diff --git a/scripts/supported_models.py b/scripts/supported_models.py index 7d7a5c169..b987650c9 100644 --- a/scripts/supported_models.py +++ b/scripts/supported_models.py @@ -838,6 +838,14 @@ 'microsoft/speecht5_tts', ], }, + 'stablelm': { + # Text generation + 'text-generation': [ + 'hf-internal-testing/tiny-random-StableLmForCausalLM', + 'stabilityai/stablelm-2-1_6b', + 'stabilityai/stablelm-2-zephyr-1_6b', + ], + }, 'squeezebert': { # Feature extraction 'feature-extraction': [ From 02f516a758e94d68260d909ae465cf82439b9259 Mon Sep 17 00:00:00 2001 From: Dave <69651599+D4ve-R@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:00:57 +0100 Subject: [PATCH 06/13] Remove StableLMForSequenceClassification will be added in seperate PR Co-authored-by: Joshua Lochner --- src/models.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/models.js b/src/models.js index 8f93ea482..a773b04c5 100644 --- a/src/models.js +++ b/src/models.js @@ -5234,21 +5234,6 @@ export class StableLMForCausalLM extends StableLMPreTrainedModel { return new CausalLMOutputWithPast(await super._call(model_inputs)); } } - -/** - * StableLM Model with a sequence classification head on top (with Past). - */ -export class StableLMForSequenceClassification extends StableLMPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for sequence classification. - */ - async _call(model_inputs) { - return new SequenceClassifierOutputWithPast(await super._call(model_inputs)); - } -} - ////////////////////////////////////////////////// ////////////////////////////////////////////////// From 814e334fc46de770be178b9d5020408c9b883584 Mon Sep 17 00:00:00 2001 From: Dave <69651599+D4ve-R@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:01:22 +0100 Subject: [PATCH 07/13] Remove SequenceClassifierOutputWithPast will be added in seperate PR Co-authored-by: Joshua Lochner --- src/models.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/models.js b/src/models.js index a773b04c5..90fab1e48 100644 --- a/src/models.js +++ b/src/models.js @@ -5888,23 +5888,6 @@ export class SequenceClassifierOutput extends ModelOutput { } } -/** - * Base class for outputs of sentence classification models. - */ -export class SequenceClassifierOutputWithPast extends ModelOutput { - /** - * @param {Object} output The output of the model. - * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). - * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) - * that can be used (see `past_key_values` input) to speed up sequential decoding. - */ - constructor({ logits, past_key_values }) { - super(); - this.logits = logits; - this.past_key_values = past_key_values; - } -} - /** * Base class for outputs of XVector models. */ From 4837c477ab402c8a57b8f2fc964b4f1e48d16e75 Mon Sep 17 00:00:00 2001 From: Dave <69651599+D4ve-R@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:01:59 +0100 Subject: [PATCH 08/13] Remove StableLMForSequenceClassification mapping will be added in seperate pr Co-authored-by: Joshua Lochner --- src/models.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/models.js b/src/models.js index 90fab1e48..8fd4e01aa 100644 --- a/src/models.js +++ b/src/models.js @@ -5421,7 +5421,6 @@ const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = new Map([ ['mbart', ['MBartForSequenceClassification', MBartForSequenceClassification]], ['mobilebert', ['MobileBertForSequenceClassification', MobileBertForSequenceClassification]], ['squeezebert', ['SqueezeBertForSequenceClassification', SqueezeBertForSequenceClassification]], - ['stablelm', ['StableLMForSequenceClassification', StableLMForSequenceClassification]], ]); const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = new Map([ From b56aec27d76813048e3a0429d7f91dd3e12e0ad1 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Wed, 6 Mar 2024 17:37:41 +0200 Subject: [PATCH 09/13] Update src/models.js --- src/models.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models.js b/src/models.js index 36e5f23ae..d56a126a7 100644 --- a/src/models.js +++ b/src/models.js @@ -5317,7 +5317,7 @@ export class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { ////////////////////////////////////////////////// ////////////////////////////////////////////////// -// StableLM models +// StableLm models export class StableLMPreTrainedModel extends PreTrainedModel { /** * Creates a new instance of the `StableLMPreTrainedModel` class. From 295c317432770de61688f925575524dc52a61e90 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Wed, 6 Mar 2024 17:38:06 +0200 Subject: [PATCH 10/13] Naming nit --- src/models.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/models.js b/src/models.js index d56a126a7..d4a44525b 100644 --- a/src/models.js +++ b/src/models.js @@ -5344,9 +5344,9 @@ export class StableLMPreTrainedModel extends PreTrainedModel { export class StableLMModel extends StableLMPreTrainedModel { } /** - * StableLM Model with a `language modeling` head on top for Causal Language Modeling (with past). + * StableLm Model with a `language modeling` head on top for Causal Language Modeling (with past). */ -export class StableLMForCausalLM extends StableLMPreTrainedModel { +export class StableLmForCausalLM extends StableLmPreTrainedModel { /** * Calls the model on new inputs. * @param {Object} model_inputs The inputs to the model. @@ -5594,7 +5594,7 @@ const MODEL_WITH_LM_HEAD_MAPPING_NAMES = new Map([ ['starcoder2', ['Starcoder2ForCausalLM', Starcoder2ForCausalLM]], ['falcon', ['FalconForCausalLM', FalconForCausalLM]], ['trocr', ['TrOCRForCausalLM', TrOCRForCausalLM]], - ['stablelm', ['StableLMForCausalLM', StableLMForCausalLM]], + ['stablelm', ['StableLmForCausalLM', StableLmForCausalLM]], ]); const MODEL_FOR_MASKED_LM_MAPPING_NAMES = new Map([ From d02f0780fcd55cc0d94d733319b70813c7fbcdc1 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Wed, 6 Mar 2024 17:39:36 +0200 Subject: [PATCH 11/13] Naming nits --- src/models.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/models.js b/src/models.js index d4a44525b..f5a930e35 100644 --- a/src/models.js +++ b/src/models.js @@ -5318,9 +5318,9 @@ export class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { ////////////////////////////////////////////////// // StableLm models -export class StableLMPreTrainedModel extends PreTrainedModel { +export class StableLmPreTrainedModel extends PreTrainedModel { /** - * Creates a new instance of the `StableLMPreTrainedModel` class. + * Creates a new instance of the `StableLmPreTrainedModel` class. * @param {Object} config The configuration of the model. * @param {any} session The ONNX session containing the model weights. * @param {GenerationConfig} generation_config The generation configuration. @@ -5339,9 +5339,9 @@ export class StableLMPreTrainedModel extends PreTrainedModel { } /** - * The bare StableLM Model transformer outputting raw hidden-states without any specific head on top. + * The bare StableLm Model transformer outputting raw hidden-states without any specific head on top. */ -export class StableLMModel extends StableLMPreTrainedModel { } +export class StableLmModel extends StableLmPreTrainedModel { } /** * StableLm Model with a `language modeling` head on top for Causal Language Modeling (with past). From 0efe69e7c88230fee3fa88e7b4433d6268162043 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Thu, 7 Mar 2024 03:02:03 +0200 Subject: [PATCH 12/13] Update scripts/convert.py --- scripts/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/convert.py b/scripts/convert.py index 7e05a111b..3a2b223e8 100644 --- a/scripts/convert.py +++ b/scripts/convert.py @@ -84,7 +84,7 @@ 'reduce_range': False, }, 'stablelm': { - 'per_channel': False, + 'per_channel': False, 'reduce_range': False, }, 'starcoder2': { From 0c14c050814cadc7e95c8d470b8530f6d286b383 Mon Sep 17 00:00:00 2001 From: Joshua Lochner Date: Thu, 7 Mar 2024 03:03:53 +0200 Subject: [PATCH 13/13] Update src/models.js --- src/models.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/models.js b/src/models.js index f5a930e35..ef3d2806c 100644 --- a/src/models.js +++ b/src/models.js @@ -5346,16 +5346,7 @@ export class StableLmModel extends StableLmPreTrainedModel { } /** * StableLm Model with a `language modeling` head on top for Causal Language Modeling (with past). */ -export class StableLmForCausalLM extends StableLmPreTrainedModel { - /** - * Calls the model on new inputs. - * @param {Object} model_inputs The inputs to the model. - * @returns {Promise} An object containing the model's output logits for causal language modeling. - */ - async _call(model_inputs) { - return new CausalLMOutputWithPast(await super._call(model_inputs)); - } -} +export class StableLmForCausalLM extends StableLmPreTrainedModel { } ////////////////////////////////////////////////// //////////////////////////////////////////////////