[FEAT] Refactor CUSTOM_ARCHITECTURES to provide custom model#1232
Closed
HarryTheWulf wants to merge 1 commit intohuggingface:mainfrom
Closed
[FEAT] Refactor CUSTOM_ARCHITECTURES to provide custom model#1232HarryTheWulf wants to merge 1 commit intohuggingface:mainfrom
HarryTheWulf wants to merge 1 commit intohuggingface:mainfrom
Conversation
Providing nothing will default to PreTrainedModel.
HarryTheWulf
commented
Mar 13, 2025
| * @param {T} task The task defining which pipeline will be returned. Currently accepted tasks are: | ||
| * - `"audio-classification"`: will return a `AudioClassificationPipeline`. | ||
| * - `"automatic-speech-recognition"`: will return a `AutomaticSpeechRecognitionPipeline`. | ||
| * - `"background-removal"`: will return a `BackgroundRemovalPipeline`. |
Contributor
Author
There was a problem hiding this comment.
I noticed that we forgot to add the new pipeline to the docblock.
HarryTheWulf
commented
Mar 13, 2025
Comment on lines
+7920
to
7936
| /** @type {CustomArchitectures} */ | ||
| const CUSTOM_ARCHITECTURES = new Map([ | ||
| ['modnet', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], | ||
| ['birefnet', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], | ||
| ['isnet', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], | ||
| ['ben', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], | ||
| ['modnet', [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, null]], | ||
| ['birefnet', [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, null]], | ||
| ['isnet', [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, null]], | ||
| ['ben', [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, null]], | ||
| ]); | ||
| for (const [name, mapping] of CUSTOM_ARCHITECTURES.entries()) { | ||
| mapping.set(name, ['PreTrainedModel', PreTrainedModel]) | ||
|
|
||
| for (let [name, [mapping, model]] of CUSTOM_ARCHITECTURES.entries()) { | ||
| if (!model) { | ||
| model = PreTrainedModel; | ||
| } | ||
| mapping.set(name, [model.constructor.name, model]); | ||
| MODEL_TYPE_MAPPING.set(name, MODEL_TYPES.EncoderOnly); | ||
| MODEL_CLASS_TO_NAME_MAPPING.set(PreTrainedModel, name); | ||
| MODEL_NAME_TO_CLASS_MAPPING.set(name, PreTrainedModel); | ||
| MODEL_CLASS_TO_NAME_MAPPING.set(model, name); | ||
| MODEL_NAME_TO_CLASS_MAPPING.set(name, model); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
This refactor allows us to specify a custom model for any given architecture.
Currently I haven't changed anything, so they will all default to PreTrainedModel.
Collaborator
|
We've significantly improve how architectures are registered in Transformers.js v4 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR will allow for a custom model to be provided for any of the custom architectures - providing
nullwill default toPreTrainedModel.This came about when I was creating a new model; I wanted to set it up as an Image Segmentation model.
Below shows how a new architecture can be provided with a specific model, rather than
PreTrainedModel.