Agora board builds (Model Farm, verified on real boards)
Browse files- LICENSE_MODEL_FARM.pdf +0 -0
- NOTICE.md +5 -0
- README.md +36 -0
- SHA256SUMS +9 -0
- targets/QCS8550/FP16/composite.json +54 -0
- targets/QCS8550/FP16/model.ctx.bin +3 -0
- targets/QCS8550/FP16/reference/app.py +63 -0
- targets/QCS8550/FP16/reference/model.py +238 -0
- targets/QCS8550/FP16/reference/source.json +4 -0
- targets/QCS8550/FP16/tokenizer/source.json +24 -0
- targets/QCS8550/FP16/tokenizer/tokenizer.json +0 -0
- targets/QCS8550/FP16/tokenizer/tokenizer_config.json +1 -0
- targets/QCS8550/FP16/tokenizer/vocab.txt +0 -0
- targets/QCS8550/model.json +7 -0
LICENSE_MODEL_FARM.pdf
ADDED
|
Binary file (71.1 kB). View file
|
|
|
NOTICE.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Provenance
|
| 2 |
+
|
| 3 |
+
Resources are copied without binary changes from the APLUX Model Farm build `Nomic-Embed-Text` that Agora verified on real boards (decoded sample check, then measured), pinned by sha256 in SHA256SUMS.
|
| 4 |
+
|
| 5 |
+
Agora arranges files under targets/<SoC>/<precision>/ and adds model.json. AidLite, AidGen, AidVoice and the Qualcomm QNN runtime are separate dependencies with their own terms.
|
README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: aplux-model-farm
|
| 4 |
+
license_link: https://huggingface.co/APLUX-Agora/Nomic-Embed-Text/blob/main/LICENSE_MODEL_FARM.pdf
|
| 5 |
+
tags: [Agora, QNN, AIoT, Qualcomm]
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
# Nomic-Embed-Text model resources
|
| 9 |
+
|
| 10 |
+
输入多段英文文本,输出 512 维归一化语义向量与相似度矩阵。
|
| 11 |
+
|
| 12 |
+
Board builds that [Agora](https://mega.aidlux.com) runs and measures on real Qualcomm boards. Try it online, without a board: https://mega.aidlux.com/models/nomic-embed-text
|
| 13 |
+
|
| 14 |
+
| SoC · board | Precisions | Measured on Agora (default precision) |
|
| 15 |
+
| --- | --- | --- |
|
| 16 |
+
| QCS8550 · 犀牛派 X1 (Rhino Pi X1) | FP16 | 23.5 ms |
|
| 17 |
+
|
| 18 |
+
Task: 其他. Inputs: text (text). Outputs: embedding (embedding).
|
| 19 |
+
|
| 20 |
+
## Download
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
export AGORA_WORKSPACE="$HOME/my-nomic-embed-text"
|
| 24 |
+
export AGORA_SOC='QCS8550'
|
| 25 |
+
hf download APLUX-Agora/Nomic-Embed-Text \
|
| 26 |
+
--include "targets/$AGORA_SOC/*" --local-dir "$AGORA_WORKSPACE/resources"
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
Each `targets/<SoC>/` holds `model.json` (default precision and variant paths) and one directory per precision. Context binaries are named `model.bin`; multi-file packages are kept as published by Model Farm.
|
| 30 |
+
|
| 31 |
+
Pre- and post-processing, the runner and an exported project: open the model on Agora and choose “部署指南”, or export any workflow that uses it.
|
| 32 |
+
|
| 33 |
+
## Licence and provenance
|
| 34 |
+
|
| 35 |
+
Source model licence: see the original model's terms.
|
| 36 |
+
Converted, deployable resources: APLUX Model Farm License (LICENSE_MODEL_FARM.pdf). See NOTICE.md and SHA256SUMS.
|
SHA256SUMS
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
d85a7dfd7e6d47c741fc4b5fe3d34ea0db586abc0f9f95cd9afcb1e5397eec53 targets/QCS8550/FP16/composite.json
|
| 2 |
+
337c944108113c75e6fe52bf81fc9c1ddccf4a0016846d52a6675ac3237aa7b0 targets/QCS8550/FP16/model.ctx.bin
|
| 3 |
+
c9070459017719972a463ce0b97661d4c31dfa42decab946b6159ef54ae5a4d0 targets/QCS8550/FP16/reference/app.py
|
| 4 |
+
3de04c66d95a155a03cc1ca78d9bc52c81e6ffc7f3cf2f1ec2f54b45c67b5418 targets/QCS8550/FP16/reference/model.py
|
| 5 |
+
d91b4f8b68b7b0ef5650ce1881a8c58d94cb630b936c1d0690689e3973396cb9 targets/QCS8550/FP16/reference/source.json
|
| 6 |
+
b7219bf56ec6526f4b7cf1d5fe56d5424f5fccf79f61a0e3732b450aefe19acd targets/QCS8550/FP16/tokenizer/source.json
|
| 7 |
+
ce64fce797c24f68df90b40a3f74f579b336a493db14bd583fd520ea0d8c9a98 targets/QCS8550/FP16/tokenizer/tokenizer.json
|
| 8 |
+
a025160ef0431f1a392f6f050c1310f4c5d9fb6f275932dbccba73c4d214bf10 targets/QCS8550/FP16/tokenizer/tokenizer_config.json
|
| 9 |
+
07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3 targets/QCS8550/FP16/tokenizer/vocab.txt
|
targets/QCS8550/FP16/composite.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"assets": {
|
| 3 |
+
"tokenizer": "tokenizer/tokenizer.json"
|
| 4 |
+
},
|
| 5 |
+
"components": {
|
| 6 |
+
"model": "model.ctx.bin"
|
| 7 |
+
},
|
| 8 |
+
"derivation": {
|
| 9 |
+
"files": {
|
| 10 |
+
"reference/app.py": "c9070459017719972a463ce0b97661d4c31dfa42decab946b6159ef54ae5a4d0",
|
| 11 |
+
"reference/model.py": "3de04c66d95a155a03cc1ca78d9bc52c81e6ffc7f3cf2f1ec2f54b45c67b5418",
|
| 12 |
+
"reference/source.json": "d91b4f8b68b7b0ef5650ce1881a8c58d94cb630b936c1d0690689e3973396cb9",
|
| 13 |
+
"tokenizer/source.json": "b7219bf56ec6526f4b7cf1d5fe56d5424f5fccf79f61a0e3732b450aefe19acd",
|
| 14 |
+
"tokenizer/tokenizer.json": "ce64fce797c24f68df90b40a3f74f579b336a493db14bd583fd520ea0d8c9a98",
|
| 15 |
+
"tokenizer/tokenizer_config.json": "a025160ef0431f1a392f6f050c1310f4c5d9fb6f275932dbccba73c4d214bf10",
|
| 16 |
+
"tokenizer/vocab.txt": "07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3"
|
| 17 |
+
},
|
| 18 |
+
"referenceSource": {
|
| 19 |
+
"commit": "2bc70e8c0f8758e16b406017d34bbfa5c56d1de4",
|
| 20 |
+
"repository": "https://github.com/qualcomm/ai-hub-models"
|
| 21 |
+
},
|
| 22 |
+
"sourcePackageSha256": "7cd609f465faa216b58755dc10d96b3f88861ab62f80657c1e77797fbf53951a",
|
| 23 |
+
"tokenizerSource": {
|
| 24 |
+
"files": [
|
| 25 |
+
{
|
| 26 |
+
"name": "tokenizer.json",
|
| 27 |
+
"sha256": "ce64fce797c24f68df90b40a3f74f579b336a493db14bd583fd520ea0d8c9a98",
|
| 28 |
+
"size": 466062,
|
| 29 |
+
"url": "https://huggingface.co/google-bert/bert-base-uncased/resolve/86b5e0934494bd15c9632b12f734a8a67f723594/tokenizer.json"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"name": "tokenizer_config.json",
|
| 33 |
+
"sha256": "a025160ef0431f1a392f6f050c1310f4c5d9fb6f275932dbccba73c4d214bf10",
|
| 34 |
+
"size": 48,
|
| 35 |
+
"url": "https://huggingface.co/google-bert/bert-base-uncased/resolve/86b5e0934494bd15c9632b12f734a8a67f723594/tokenizer_config.json"
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"name": "vocab.txt",
|
| 39 |
+
"sha256": "07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3",
|
| 40 |
+
"size": 231508,
|
| 41 |
+
"url": "https://huggingface.co/google-bert/bert-base-uncased/resolve/86b5e0934494bd15c9632b12f734a8a67f723594/vocab.txt"
|
| 42 |
+
}
|
| 43 |
+
],
|
| 44 |
+
"ref": "86b5e0934494bd15c9632b12f734a8a67f723594",
|
| 45 |
+
"source": "https://huggingface.co/google-bert/bert-base-uncased"
|
| 46 |
+
},
|
| 47 |
+
"weights": {
|
| 48 |
+
"sha256": "337c944108113c75e6fe52bf81fc9c1ddccf4a0016846d52a6675ac3237aa7b0",
|
| 49 |
+
"sourcePath": "models/QCS8550/FP16/nomic_embed_text.bin"
|
| 50 |
+
}
|
| 51 |
+
},
|
| 52 |
+
"kind": "nomic512",
|
| 53 |
+
"version": 1
|
| 54 |
+
}
|
targets/QCS8550/FP16/model.ctx.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:337c944108113c75e6fe52bf81fc9c1ddccf4a0016846d52a6675ac3237aa7b0
|
| 3 |
+
size 274795136
|
targets/QCS8550/FP16/reference/app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---------------------------------------------------------------------
|
| 2 |
+
# Copyright (c) 2024-2025 Qualcomm Technologies, Inc. and/or its subsidiaries.
|
| 3 |
+
# SPDX-License-Identifier: BSD-3-Clause
|
| 4 |
+
# ---------------------------------------------------------------------
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
from typing import Any, cast
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import AutoTokenizer
|
| 13 |
+
|
| 14 |
+
from qai_hub_models.protocols import ExecutableModelProtocol
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class NomicEmbedTextApp:
|
| 18 |
+
"""
|
| 19 |
+
This class consists of light-weight "app code" that is required to
|
| 20 |
+
perform end to end inference with NomicEmbedText.
|
| 21 |
+
|
| 22 |
+
The app uses 1 model:
|
| 23 |
+
* Nomic Embedding Text
|
| 24 |
+
|
| 25 |
+
For a given text input, the app will:
|
| 26 |
+
* tokenize the text
|
| 27 |
+
* Run inference
|
| 28 |
+
* Returns transformer embeddings
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
def __init__(
|
| 32 |
+
self,
|
| 33 |
+
model: ExecutableModelProtocol[torch.Tensor],
|
| 34 |
+
seq_len: int,
|
| 35 |
+
) -> None:
|
| 36 |
+
self.model = model
|
| 37 |
+
self.seq_len = seq_len
|
| 38 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 39 |
+
"bert-base-uncased", model_max_length=self.seq_len
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
def predict(self, *args: Any, **kwargs: Any) -> torch.Tensor:
|
| 43 |
+
# See predict_embeddings.
|
| 44 |
+
return self.predict_embeddings(*args, **kwargs)
|
| 45 |
+
|
| 46 |
+
def predict_embeddings(self, text: str) -> torch.Tensor:
|
| 47 |
+
"""
|
| 48 |
+
Predicts embeddings from text.
|
| 49 |
+
|
| 50 |
+
Parameters
|
| 51 |
+
----------
|
| 52 |
+
text
|
| 53 |
+
Text from which embeddings should be generated.
|
| 54 |
+
|
| 55 |
+
Returns
|
| 56 |
+
-------
|
| 57 |
+
token_embeddings : torch.Tensor
|
| 58 |
+
The generated transformer embeddings of shape [1, 512], dtype of fp32
|
| 59 |
+
"""
|
| 60 |
+
inputs = self.tokenizer(text, padding="max_length", return_tensors="pt")
|
| 61 |
+
input_ids = cast(torch.Tensor, inputs["input_ids"])
|
| 62 |
+
attention_mask = cast(torch.Tensor, inputs["attention_mask"])
|
| 63 |
+
return self.model(input_ids, attention_mask)
|
targets/QCS8550/FP16/reference/model.py
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---------------------------------------------------------------------
|
| 2 |
+
# Copyright (c) 2025 Qualcomm Technologies, Inc. and/or its subsidiaries.
|
| 3 |
+
# SPDX-License-Identifier: BSD-3-Clause
|
| 4 |
+
# ---------------------------------------------------------------------
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import importlib.util
|
| 10 |
+
import os
|
| 11 |
+
import shutil
|
| 12 |
+
import sys
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
from unittest import mock
|
| 15 |
+
|
| 16 |
+
import torch
|
| 17 |
+
import torch.nn.functional as F
|
| 18 |
+
from huggingface_hub import hf_hub_download
|
| 19 |
+
from torch import nn
|
| 20 |
+
from transformers import AutoModel
|
| 21 |
+
from transformers.utils.hub import HF_MODULES_CACHE
|
| 22 |
+
from typing_extensions import Self
|
| 23 |
+
|
| 24 |
+
from qai_hub_models.configs.model_metadata import ModelMetadata, TokenizerMetadata
|
| 25 |
+
from qai_hub_models.models.nomic_embed_text.dataset import (
|
| 26 |
+
AmazonCounterfactualClassificationDataset,
|
| 27 |
+
)
|
| 28 |
+
from qai_hub_models.models.nomic_embed_text.evaluator import (
|
| 29 |
+
NomicEmbedTextEvaluator,
|
| 30 |
+
)
|
| 31 |
+
from qai_hub_models.utils.asset_loaders import PathLike
|
| 32 |
+
from qai_hub_models.utils.base_dataset import BaseDataset
|
| 33 |
+
from qai_hub_models.utils.base_evaluator import BaseEvaluator
|
| 34 |
+
from qai_hub_models.utils.base_model import BaseModel
|
| 35 |
+
from qai_hub_models.utils.input_spec import InputSpec, IoType, OutputSpec, TensorSpec
|
| 36 |
+
|
| 37 |
+
MODEL_ID = __name__.split(".")[-2]
|
| 38 |
+
MODEL_ASSET_VERSION = 1
|
| 39 |
+
DEFAULT_MODEL_VERSION = "1.5"
|
| 40 |
+
MATRYOSHIKA_DIM = 512
|
| 41 |
+
|
| 42 |
+
BERT_BASE_UNCASED_REPO_ID = "google-bert/bert-base-uncased"
|
| 43 |
+
VOCAB_FILE_NAME = "vocab.txt"
|
| 44 |
+
TOKENIZER_JSON_FILE_NAME = "tokenizer.json"
|
| 45 |
+
TOKENIZER_CONFIG_FILE_NAME = "tokenizer_config.json"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Modify transformers so that it creates modules that can be traced.
|
| 49 |
+
# See https://github.com/huggingface/transformers/issues/35570 for details.
|
| 50 |
+
# TODO: Remove this when transfomers releases the upstreamed patch.
|
| 51 |
+
def _patched_transformers_get_class_in_module(
|
| 52 |
+
class_name: str,
|
| 53 |
+
module_path: PathLike,
|
| 54 |
+
force_reload: bool = False,
|
| 55 |
+
) -> type:
|
| 56 |
+
name = os.path.normpath(module_path).rstrip(".py")
|
| 57 |
+
# Everything in this function is copied from transformers except the following ".".join() statement.
|
| 58 |
+
name = ".".join(
|
| 59 |
+
[
|
| 60 |
+
f"_{x}" if x and x[0].isdigit() else x
|
| 61 |
+
for x in name.replace("-", "_").split(os.path.sep)
|
| 62 |
+
]
|
| 63 |
+
)
|
| 64 |
+
module_spec = importlib.util.spec_from_file_location(
|
| 65 |
+
name, location=Path(HF_MODULES_CACHE) / module_path
|
| 66 |
+
)
|
| 67 |
+
if not module_spec or not module_spec.loader:
|
| 68 |
+
raise ValueError(f"Module spec not found for path {name}")
|
| 69 |
+
|
| 70 |
+
module = sys.modules.get(name)
|
| 71 |
+
if force_reload or module is None:
|
| 72 |
+
module = importlib.util.module_from_spec(module_spec)
|
| 73 |
+
# insert it into sys.modules before any loading begins
|
| 74 |
+
sys.modules[name] = module
|
| 75 |
+
|
| 76 |
+
# reload in both cases
|
| 77 |
+
module_spec.loader.exec_module(module)
|
| 78 |
+
return getattr(module, class_name)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class NomicEmbedText(BaseModel):
|
| 82 |
+
def __init__(self, model: nn.Module, model_version: str, seq_length: int) -> None:
|
| 83 |
+
super().__init__(model=model)
|
| 84 |
+
self.seq_length = seq_length
|
| 85 |
+
self.model_version = model_version
|
| 86 |
+
assert model_version in [DEFAULT_MODEL_VERSION, "1"]
|
| 87 |
+
model.eval()
|
| 88 |
+
|
| 89 |
+
@classmethod
|
| 90 |
+
def from_pretrained(
|
| 91 |
+
cls, model_version: str = DEFAULT_MODEL_VERSION, sequence_length: int = 128
|
| 92 |
+
) -> Self:
|
| 93 |
+
"""
|
| 94 |
+
Create a Nomic Embedding BERT model.
|
| 95 |
+
|
| 96 |
+
Parameters
|
| 97 |
+
----------
|
| 98 |
+
model_version
|
| 99 |
+
Version of the Nomic model (1 or 1.5)
|
| 100 |
+
sequence_length
|
| 101 |
+
Model max sequence length
|
| 102 |
+
(When compiled for device, the sequence must be padded to this size.)
|
| 103 |
+
|
| 104 |
+
Returns
|
| 105 |
+
-------
|
| 106 |
+
model : Self
|
| 107 |
+
Instance of NomicEmbedText model.
|
| 108 |
+
"""
|
| 109 |
+
with mock.patch(
|
| 110 |
+
"transformers.dynamic_module_utils.get_class_in_module",
|
| 111 |
+
_patched_transformers_get_class_in_module,
|
| 112 |
+
):
|
| 113 |
+
return cls(
|
| 114 |
+
AutoModel.from_pretrained(
|
| 115 |
+
f"nomic-ai/nomic-embed-text-v{model_version}",
|
| 116 |
+
trust_remote_code=True,
|
| 117 |
+
rotary_scaling_factor=sequence_length // 2048 or 1,
|
| 118 |
+
),
|
| 119 |
+
model_version,
|
| 120 |
+
sequence_length,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
def forward(
|
| 124 |
+
self, input_ids: torch.Tensor, attention_mask: torch.Tensor
|
| 125 |
+
) -> torch.Tensor:
|
| 126 |
+
"""
|
| 127 |
+
Produce embeddings from the given tokenized inputs.
|
| 128 |
+
|
| 129 |
+
Parameters
|
| 130 |
+
----------
|
| 131 |
+
input_ids
|
| 132 |
+
Tokenized inputs of shape (1, sequence_length), dtype of int32
|
| 133 |
+
attention_mask
|
| 134 |
+
Attention mask of shape (1, sequence_length), dtype of int32
|
| 135 |
+
|
| 136 |
+
Where the default value of sequence_length is 128.
|
| 137 |
+
|
| 138 |
+
Returns
|
| 139 |
+
-------
|
| 140 |
+
token_embeddings : torch.Tensor
|
| 141 |
+
Transformer embeddings of shape [1, 512], dtype of fp32
|
| 142 |
+
"""
|
| 143 |
+
last_hidden_state = self.model(input_ids, attention_mask=attention_mask)[0]
|
| 144 |
+
token_embeddings = NomicEmbedText._mean_pooling(
|
| 145 |
+
last_hidden_state, attention_mask
|
| 146 |
+
)
|
| 147 |
+
if self.model_version == "1.5":
|
| 148 |
+
token_embeddings = F.layer_norm(
|
| 149 |
+
token_embeddings, normalized_shape=(token_embeddings.shape[1],)
|
| 150 |
+
)
|
| 151 |
+
token_embeddings = token_embeddings[:, :MATRYOSHIKA_DIM]
|
| 152 |
+
return F.normalize(token_embeddings, p=2, dim=1)
|
| 153 |
+
|
| 154 |
+
@staticmethod
|
| 155 |
+
def _mean_pooling(
|
| 156 |
+
token_embeddings: torch.Tensor, attention_mask: torch.Tensor
|
| 157 |
+
) -> torch.Tensor:
|
| 158 |
+
input_mask_expanded = (
|
| 159 |
+
attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 160 |
+
)
|
| 161 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(
|
| 162 |
+
input_mask_expanded.sum(1), min=1e-9
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
def get_input_spec(self, batch_size: int = 1) -> InputSpec:
|
| 166 |
+
return {
|
| 167 |
+
"input_tokens": TensorSpec(
|
| 168 |
+
shape=(batch_size, self.seq_length),
|
| 169 |
+
dtype="int32",
|
| 170 |
+
io_type=IoType.TENSOR,
|
| 171 |
+
),
|
| 172 |
+
"attention_masks": TensorSpec(
|
| 173 |
+
shape=(batch_size, self.seq_length),
|
| 174 |
+
dtype="int32",
|
| 175 |
+
io_type=IoType.TENSOR,
|
| 176 |
+
),
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
def get_output_spec(self) -> OutputSpec:
|
| 180 |
+
return {
|
| 181 |
+
"embeddings": TensorSpec(),
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
def get_evaluator(self) -> BaseEvaluator:
|
| 185 |
+
model = NomicEmbedText.from_pretrained(self.model_version, self.seq_length)
|
| 186 |
+
return NomicEmbedTextEvaluator(model, self.seq_length)
|
| 187 |
+
|
| 188 |
+
@classmethod
|
| 189 |
+
def get_eval_dataset_classes(cls) -> list[type[BaseDataset]]:
|
| 190 |
+
return [AmazonCounterfactualClassificationDataset]
|
| 191 |
+
|
| 192 |
+
def get_calibration_dataset_cls(self) -> type[BaseDataset]:
|
| 193 |
+
return AmazonCounterfactualClassificationDataset
|
| 194 |
+
|
| 195 |
+
def write_supplementary_files(
|
| 196 |
+
self,
|
| 197 |
+
output_dir: str | os.PathLike,
|
| 198 |
+
metadata: ModelMetadata,
|
| 199 |
+
) -> None:
|
| 200 |
+
"""
|
| 201 |
+
Download and bundle the bert-base-uncased tokenizer files alongside
|
| 202 |
+
the exported model so that downstream applications can tokenize inputs
|
| 203 |
+
without needing to locate them separately.
|
| 204 |
+
|
| 205 |
+
Files written:
|
| 206 |
+
- vocab.txt - WordPiece vocabulary for tokenizing inputs.
|
| 207 |
+
- tokenizer.json - HuggingFace fast-tokenizer configuration.
|
| 208 |
+
- tokenizer_config.json - HuggingFace tokenizer_config for bert-base-uncased.
|
| 209 |
+
|
| 210 |
+
All three files are published by Google under the Apache 2.0 license
|
| 211 |
+
(https://huggingface.co/bert-base-uncased/blob/main/LICENSE).
|
| 212 |
+
"""
|
| 213 |
+
for filename, description in [
|
| 214 |
+
(
|
| 215 |
+
VOCAB_FILE_NAME,
|
| 216 |
+
"WordPiece vocabulary file for tokenizing text inputs before inference.",
|
| 217 |
+
),
|
| 218 |
+
(
|
| 219 |
+
TOKENIZER_JSON_FILE_NAME,
|
| 220 |
+
"HuggingFace tokenizer configuration (fast tokenizer) for bert-base-uncased.",
|
| 221 |
+
),
|
| 222 |
+
(
|
| 223 |
+
TOKENIZER_CONFIG_FILE_NAME,
|
| 224 |
+
"HuggingFace tokenizer_config.json for bert-base-uncased.",
|
| 225 |
+
),
|
| 226 |
+
]:
|
| 227 |
+
cached = hf_hub_download(
|
| 228 |
+
repo_id=BERT_BASE_UNCASED_REPO_ID, filename=filename
|
| 229 |
+
)
|
| 230 |
+
shutil.copy(cached, Path(output_dir) / filename)
|
| 231 |
+
metadata.supplementary_files[filename] = description
|
| 232 |
+
|
| 233 |
+
metadata.tokenizer = TokenizerMetadata(
|
| 234 |
+
vocab_file=VOCAB_FILE_NAME,
|
| 235 |
+
type="wordpiece",
|
| 236 |
+
max_length=self.seq_length,
|
| 237 |
+
do_lower_case=True,
|
| 238 |
+
)
|
targets/QCS8550/FP16/reference/source.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"repository": "https://github.com/qualcomm/ai-hub-models",
|
| 3 |
+
"commit": "2bc70e8c0f8758e16b406017d34bbfa5c56d1de4"
|
| 4 |
+
}
|
targets/QCS8550/FP16/tokenizer/source.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"source": "https://huggingface.co/google-bert/bert-base-uncased",
|
| 3 |
+
"ref": "86b5e0934494bd15c9632b12f734a8a67f723594",
|
| 4 |
+
"files": [
|
| 5 |
+
{
|
| 6 |
+
"name": "tokenizer.json",
|
| 7 |
+
"url": "https://huggingface.co/google-bert/bert-base-uncased/resolve/86b5e0934494bd15c9632b12f734a8a67f723594/tokenizer.json",
|
| 8 |
+
"sha256": "ce64fce797c24f68df90b40a3f74f579b336a493db14bd583fd520ea0d8c9a98",
|
| 9 |
+
"size": 466062
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"name": "tokenizer_config.json",
|
| 13 |
+
"url": "https://huggingface.co/google-bert/bert-base-uncased/resolve/86b5e0934494bd15c9632b12f734a8a67f723594/tokenizer_config.json",
|
| 14 |
+
"sha256": "a025160ef0431f1a392f6f050c1310f4c5d9fb6f275932dbccba73c4d214bf10",
|
| 15 |
+
"size": 48
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"name": "vocab.txt",
|
| 19 |
+
"url": "https://huggingface.co/google-bert/bert-base-uncased/resolve/86b5e0934494bd15c9632b12f734a8a67f723594/vocab.txt",
|
| 20 |
+
"sha256": "07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3",
|
| 21 |
+
"size": 231508
|
| 22 |
+
}
|
| 23 |
+
]
|
| 24 |
+
}
|
targets/QCS8550/FP16/tokenizer/tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
targets/QCS8550/FP16/tokenizer/tokenizer_config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"do_lower_case": true, "model_max_length": 512}
|
targets/QCS8550/FP16/tokenizer/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
targets/QCS8550/model.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"soc": "QCS8550",
|
| 3 |
+
"default_precision": "FP16",
|
| 4 |
+
"variants": {
|
| 5 |
+
"FP16": "FP16/"
|
| 6 |
+
}
|
| 7 |
+
}
|