-
Notifications
You must be signed in to change notification settings - Fork 181
Add SmolLM2 support (1.7B, 360M, 135M) #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
be6b14f
Add SmolLM2 support (1.7B, 360M, 135M)
stikves bcb2619
Fix ruff formatting in iOS test
stikves b33431c
Fix Qwen2Config for transformers 5.x: set rope_parameters explicitly
stikves 176656b
Fix iOS test: same rope_parameters fix for transformers 5.x
stikves b1bab75
Merge branch 'main' into sukru/smollm2
stikves 81cd421
Use 6-bit palettization for SmolLM2-1.7B iOS
stikves 3844807
Use mixed 4/8-bit palettization for SmolLM2-1.7B iOS
stikves 142e8cf
Remove internal benchmark numbers from public files
stikves 5c2132a
Revert to 6-bit iOS preset, remove mixed 4/8-bit recipe
stikves File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # SmolLM2 | ||
|
|
||
| HuggingFace's SmolLM2 for on-device inference via Core AI. | ||
|
|
||
| ## Supported Models | ||
|
|
||
| | Model | Parameters | macOS | iOS | | ||
| | --------------------- | ---------- | ----- | --- | | ||
| | SmolLM2-1.7B-Instruct | 1.7B | Yes | Yes | | ||
| | SmolLM2-360M-Instruct | 360M | Yes | Yes | | ||
| | SmolLM2-135M-Instruct | 135M | Yes | Yes | | ||
|
|
||
| ## Setup to export models | ||
|
|
||
| If you haven't installed `uv`, install it by | ||
| ```bash | ||
| brew install uv | ||
| ``` | ||
|
|
||
| ## Export models | ||
|
|
||
| ```bash | ||
| # 1.7B (INT4 quantized with FP16 embedding) | ||
| uv run coreai.llm.export HuggingFaceTB/SmolLM2-1.7B-Instruct | ||
|
|
||
| # 360M (FP16) | ||
| uv run coreai.llm.export HuggingFaceTB/SmolLM2-360M-Instruct | ||
|
|
||
| # 135M (FP16) | ||
| uv run coreai.llm.export HuggingFaceTB/SmolLM2-135M-Instruct | ||
| ``` | ||
|
|
||
| **Options:** | ||
|
|
||
| ```bash | ||
| # Full precision | ||
| uv run coreai.llm.export HuggingFaceTB/SmolLM2-1.7B-Instruct --compression none | ||
|
|
||
| # iOS variant | ||
| uv run coreai.llm.export HuggingFaceTB/SmolLM2-1.7B-Instruct --platform iOS | ||
| ``` | ||
|
|
||
| ## Run a Core AI Language Model | ||
|
|
||
| ### In your iOS and macOS applications via Foundation Models | ||
|
|
||
| ```swift | ||
| import FoundationModels | ||
| import CoreAILanguageModels | ||
|
|
||
| let model = try await CoreAILanguageModel(resourcesAt: modelURL) | ||
|
|
||
| let session = LanguageModelSession(model: model) | ||
|
|
||
| let response = try await session.respond(to: "What is quantum computing?") | ||
|
|
||
| print(response) | ||
| ``` | ||
|
|
||
| ### On your Mac using built-in Command Line Tool | ||
|
|
||
| ```bash | ||
| swift run -c release llm-runner --model path/to/exported_model_folder --prompt "Hello" | ||
| ``` | ||
|
|
||
| ## Benchmark a Core AI Language Model | ||
|
|
||
| ```bash | ||
| swift run -c release llm-benchmark --model path/to/exported_model_folder | ||
| ``` | ||
|
|
||
| Defaults: 512 prompt tokens, 1024 generation tokens, 5 trials. Override with `-p`, `-g`, and `-n`. | ||
|
|
||
| ## Evaluation | ||
|
|
||
| Perplexity score on the [`WikiText-2`](https://huggingface.co/datasets/EleutherAI/wikitext_document_level) dataset computed using the [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness/blob/main/lm_eval/tasks/wikitext/README.md) with the Core AI PyTorch models. | ||
|
|
||
| | Model | Compression | BPW | Platform | Perplexity | | ||
| | ----- | ------------------------------------------ | ----- | -------- | ---------- | | ||
| | 1.7B | none (`float16`) | 16.00 | macOS | 12.27 | | ||
| | 1.7B | [INT4 with FP16 embedding][smollm2-yaml] | 4.56 | macOS | 14.17 | | ||
| | 1.7B | [6-bit palettized][smollm2-6bit-yaml] | 6.00 | iOS | 13.63 | | ||
| | 360M | none (`float16`) | 16.00 | macOS | 18.23 | | ||
| | 135M | none (`float16`) | 16.00 | macOS | 24.99 | | ||
|
|
||
| The iOS 1.7B uses 6-bit palettization (64 centroids). 4-bit palettization (16 centroids) | ||
| is too lossy for this model's weight distribution regardless of which layers are promoted. | ||
|
|
||
| [smollm2-yaml]: smollm2_4bit_embedding_excluded.yaml | ||
| [smollm2-6bit-yaml]: smollm2_1_7b_6bit.yaml |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # 6-bit palettization for SmolLM2-1.7B iOS. | ||
| # 64 centroids — middle ground between 4-bit (16) and 8-bit (256). | ||
| kmeans_palettization_config: | ||
| global_config: | ||
| op_state_spec: | ||
| weight: | ||
| n_bits: 6 | ||
| granularity: | ||
| type: per_grouped_channel | ||
| axis: 0 | ||
| group_size: 8 | ||
| module_type_configs: | ||
| torch.nn.modules.sparse.Embedding: null | ||
| coreai_models.primitives.ios.embedding.LoadEmbeddings: null | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # INT4 weight quantization with FP16 embedding/lm_head. | ||
| # SmolLM2 ties these weights — INT4 on lm_head degrades generation quality. | ||
| quantization_config: | ||
| execution_mode: eager | ||
| global_config: | ||
| op_state_spec: | ||
| weight: | ||
| dtype: int4 | ||
| qscheme: symmetric_with_clipping | ||
| granularity: | ||
| type: per_block | ||
| block_size: 32 | ||
| axis: 1 | ||
| op_input_spec: null | ||
| op_output_spec: null | ||
| module_type_configs: | ||
| coreai_models.primitives.macos.sdpa.SDPA: null | ||
| coreai_models.primitives.macos.rope.RoPE: null | ||
| coreai_models.primitives.macos.rms_norm.RMSNorm: null | ||
| coreai_models.primitives.macos.rms_norm.RMSNormPlusOne: null | ||
| torch.nn.modules.sparse.Embedding: null |
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can skip the second line comment