Sentence Similarity
sentence-transformers
Safetensors
baa-embedding-reranker
retrieval
embeddings
reranker
cross-encoder
rag
Instructions to use baa-ai/Merino-Nano with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use baa-ai/Merino-Nano with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("baa-ai/Merino-Nano") query = "Which planet is known as the Red Planet?" passages = [ "Venus is often called Earth's twin because of its similar size and proximity.", "Mars, known for its reddish appearance, is often referred to as the Red Planet.", "Jupiter, the largest planet in our solar system, has a prominent red spot.", "Saturn, famous for its rings, is sometimes mistaken for the Red Planet." ] scores = model.predict([(query, passage) for passage in passages]) print(scores) - Notebooks
- Google Colab
- Kaggle
Rebrand to baa.ai Merino-Nano (backbone-only attribution)
Browse files- LICENSE +24 -202
- LICENSE-minilm.txt +23 -0
- MODEL_CARD.md +61 -0
- NOTICE +4 -29
- README.md +19 -29
- config.json +12 -7
- embedder/README.md +0 -175
- modeling_baa_mini.py → modeling_baa.py +43 -19
LICENSE
CHANGED
|
@@ -1,202 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
exercising permissions granted by this License.
|
| 26 |
-
|
| 27 |
-
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
-
including but not limited to software source code, documentation
|
| 29 |
-
source, and configuration files.
|
| 30 |
-
|
| 31 |
-
"Object" form shall mean any form resulting from mechanical
|
| 32 |
-
transformation or translation of a Source form, including but
|
| 33 |
-
not limited to compiled object code, generated documentation,
|
| 34 |
-
and conversions to other media types.
|
| 35 |
-
|
| 36 |
-
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
-
Object form, made available under the License, as indicated by a
|
| 38 |
-
copyright notice that is included in or attached to the work
|
| 39 |
-
(an example is provided in the Appendix below).
|
| 40 |
-
|
| 41 |
-
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
-
form, that is based on (or derived from) the Work and for which the
|
| 43 |
-
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
-
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
-
of this License, Derivative Works shall not include works that remain
|
| 46 |
-
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
-
the Work and Derivative Works thereof.
|
| 48 |
-
|
| 49 |
-
"Contribution" shall mean any work of authorship, including
|
| 50 |
-
the original version of the Work and any modifications or additions
|
| 51 |
-
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
-
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
-
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
-
means any form of electronic, verbal, or written communication sent
|
| 56 |
-
to the Licensor or its representatives, including but not limited to
|
| 57 |
-
communication on electronic mailing lists, source code control systems,
|
| 58 |
-
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
-
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
-
excluding communication that is conspicuously marked or otherwise
|
| 61 |
-
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
-
|
| 63 |
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
-
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
-
subsequently incorporated within the Work.
|
| 66 |
-
|
| 67 |
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
-
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
-
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
-
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
-
Work and such Derivative Works in Source or Object form.
|
| 73 |
-
|
| 74 |
-
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
-
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
-
(except as stated in this section) patent license to make, have made,
|
| 78 |
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
-
where such license applies only to those patent claims licensable
|
| 80 |
-
by such Contributor that are necessarily infringed by their
|
| 81 |
-
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
-
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
-
institute patent litigation against any entity (including a
|
| 84 |
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
-
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
-
or contributory patent infringement, then any patent licenses
|
| 87 |
-
granted to You under this License for that Work shall terminate
|
| 88 |
-
as of the date such litigation is filed.
|
| 89 |
-
|
| 90 |
-
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
-
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
-
modifications, and in Source or Object form, provided that You
|
| 93 |
-
meet the following conditions:
|
| 94 |
-
|
| 95 |
-
(a) You must give any other recipients of the Work or
|
| 96 |
-
Derivative Works a copy of this License; and
|
| 97 |
-
|
| 98 |
-
(b) You must cause any modified files to carry prominent notices
|
| 99 |
-
stating that You changed the files; and
|
| 100 |
-
|
| 101 |
-
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
-
that You distribute, all copyright, patent, trademark, and
|
| 103 |
-
attribution notices from the Source form of the Work,
|
| 104 |
-
excluding those notices that do not pertain to any part of
|
| 105 |
-
the Derivative Works; and
|
| 106 |
-
|
| 107 |
-
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
-
distribution, then any Derivative Works that You distribute must
|
| 109 |
-
include a readable copy of the attribution notices contained
|
| 110 |
-
within such NOTICE file, excluding those notices that do not
|
| 111 |
-
pertain to any part of the Derivative Works, in at least one
|
| 112 |
-
of the following places: within a NOTICE text file distributed
|
| 113 |
-
as part of the Derivative Works; within the Source form or
|
| 114 |
-
documentation, if provided along with the Derivative Works; or,
|
| 115 |
-
within a display generated by the Derivative Works, if and
|
| 116 |
-
wherever such third-party notices normally appear. The contents
|
| 117 |
-
of the NOTICE file are for informational purposes only and
|
| 118 |
-
do not modify the License. You may add Your own attribution
|
| 119 |
-
notices within Derivative Works that You distribute, alongside
|
| 120 |
-
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
-
that such additional attribution notices cannot be construed
|
| 122 |
-
as modifying the License.
|
| 123 |
-
|
| 124 |
-
You may add Your own copyright statement to Your modifications and
|
| 125 |
-
may provide additional or different license terms and conditions
|
| 126 |
-
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
-
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
-
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
-
the conditions stated in this License.
|
| 130 |
-
|
| 131 |
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
-
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
-
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
-
this License, without any additional terms or conditions.
|
| 135 |
-
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
-
the terms of any separate license agreement you may have executed
|
| 137 |
-
with Licensor regarding such Contributions.
|
| 138 |
-
|
| 139 |
-
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
-
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
-
except as required for reasonable and customary use in describing the
|
| 142 |
-
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
-
|
| 144 |
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
-
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
-
implied, including, without limitation, any warranties or conditions
|
| 149 |
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
-
appropriateness of using or redistributing the Work and assume any
|
| 152 |
-
risks associated with Your exercise of permissions under this License.
|
| 153 |
-
|
| 154 |
-
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
-
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
-
unless required by applicable law (such as deliberate and grossly
|
| 157 |
-
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
-
liable to You for damages, including any direct, indirect, special,
|
| 159 |
-
incidental, or consequential damages of any character arising as a
|
| 160 |
-
result of this License or out of the use or inability to use the
|
| 161 |
-
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
-
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
-
other commercial damages or losses), even if such Contributor
|
| 164 |
-
has been advised of the possibility of such damages.
|
| 165 |
-
|
| 166 |
-
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
-
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
-
or other liability obligations and/or rights consistent with this
|
| 170 |
-
License. However, in accepting such obligations, You may act only
|
| 171 |
-
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
-
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
-
defend, and hold each Contributor harmless for any liability
|
| 174 |
-
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
-
of your accepting any such warranty or additional liability.
|
| 176 |
-
|
| 177 |
-
END OF TERMS AND CONDITIONS
|
| 178 |
-
|
| 179 |
-
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
-
|
| 181 |
-
To apply the Apache License to your work, attach the following
|
| 182 |
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
-
replaced with your own identifying information. (Don't include
|
| 184 |
-
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
-
comment syntax for the file format. We also recommend that a
|
| 186 |
-
file or class name and description of purpose be included on the
|
| 187 |
-
same "printed page" as the copyright notice for easier
|
| 188 |
-
identification within third-party archives.
|
| 189 |
-
|
| 190 |
-
Copyright [yyyy] [name of copyright owner]
|
| 191 |
-
|
| 192 |
-
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
-
you may not use this file except in compliance with the License.
|
| 194 |
-
You may obtain a copy of the License at
|
| 195 |
-
|
| 196 |
-
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
-
|
| 198 |
-
Unless required by applicable law or agreed to in writing, software
|
| 199 |
-
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
-
See the License for the specific language governing permissions and
|
| 202 |
-
limitations under the License.
|
|
|
|
| 1 |
+
Merino-Nano — Proprietary License
|
| 2 |
+
Copyright (c) 2026 BAA AI (Black Sheep AI). All rights reserved.
|
| 3 |
+
|
| 4 |
+
1. SCOPE. This license governs the "BAA Contributions" in this package: the
|
| 5 |
+
shared word-embedding architecture and configuration, the router / loader
|
| 6 |
+
code (modeling_baa.py), the model packaging, BAA AI's weight contributions,
|
| 7 |
+
the model card, and associated documentation.
|
| 8 |
+
|
| 9 |
+
2. GRANT. No right to use, reproduce, modify, distribute, sublicense, or create
|
| 10 |
+
derivative works of the BAA Contributions is granted except under a separate
|
| 11 |
+
written agreement with BAA AI (Black Sheep AI).
|
| 12 |
+
|
| 13 |
+
3. THIRD-PARTY COMPONENT. This package incorporates the MiniLM-L6-H384-uncased backbone,
|
| 14 |
+
provided under the MIT License — see LICENSE-minilm.txt. The MIT terms govern that
|
| 15 |
+
backbone component only; nothing in this license limits any rights you have
|
| 16 |
+
under the MIT License with respect to it.
|
| 17 |
+
|
| 18 |
+
4. NO WARRANTY. THE PACKAGE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 19 |
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO MERCHANTABILITY, FITNESS FOR
|
| 20 |
+
A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL BAA AI BE LIABLE
|
| 21 |
+
FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY ARISING FROM OR IN CONNECTION WITH
|
| 22 |
+
THE PACKAGE OR ITS USE.
|
| 23 |
+
|
| 24 |
+
Contact: BAA AI (Black Sheep AI) — baa.ai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LICENSE-minilm.txt
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Backbone component: MiniLM-L6-H384-uncased — MIT License
|
| 2 |
+
|
| 3 |
+
MIT License
|
| 4 |
+
|
| 5 |
+
Copyright (c) Microsoft Corporation.
|
| 6 |
+
|
| 7 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 8 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 9 |
+
in the Software without restriction, including without limitation the rights
|
| 10 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 11 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 12 |
+
furnished to do so, subject to the following conditions:
|
| 13 |
+
|
| 14 |
+
The above copyright notice and this permission notice shall be included in all
|
| 15 |
+
copies or substantial portions of the Software.
|
| 16 |
+
|
| 17 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 18 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 19 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 20 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 21 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 22 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 23 |
+
SOFTWARE.
|
MODEL_CARD.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: baa-proprietary
|
| 4 |
+
library_name: sentence-transformers
|
| 5 |
+
tags:
|
| 6 |
+
- retrieval
|
| 7 |
+
- embeddings
|
| 8 |
+
- reranker
|
| 9 |
+
- cross-encoder
|
| 10 |
+
- rag
|
| 11 |
+
- sentence-similarity
|
| 12 |
+
pipeline_tag: sentence-similarity
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# baa.ai · Merino-Nano
|
| 16 |
+
|
| 17 |
+
**One model that does both halves of RAG retrieval — bi-encoder embedding *and* cross-encoder reranking — over a single shared word-embedding table.** A 384-dimensional English model, ~34M parameters, by BAA AI (Black Sheep AI).
|
| 18 |
+
|
| 19 |
+
## Get the optimal model for *your* data
|
| 20 |
+
|
| 21 |
+
Merino-Nano is a strong, cost-efficient **default**. But the best embedder + reranker is **corpus-specific** — the ideal choice depends on your documents and your notion of relevance. **baa.ai offers exclusive tooling that identifies the optimal embedding and reranking models for your specific data**, so you ship the smallest models that maximize document recovery on your corpus. For a tailored recommendation, **reach out to baa.ai**.
|
| 22 |
+
|
| 23 |
+
## What it is
|
| 24 |
+
|
| 25 |
+
A two-role retrieval model over a **shared input word-embedding matrix** (stored once). The bi-encoder embedder and a compact cross-encoder reranker are built on the same `MiniLM-L6-H384-uncased` backbone, so their word-embedding table is stored a single time and injected into the reranker at load — a smaller download at **no measured quality loss**, with no retraining.
|
| 26 |
+
|
| 27 |
+
- **Embed role:** bi-encoder, 384-d, L2-normalized.
|
| 28 |
+
- **Rerank role:** cross-encoder, single relevance logit per (query, document) pair.
|
| 29 |
+
- **Router:** call `.embed(...)` or `.rerank(...)`.
|
| 30 |
+
|
| 31 |
+
## Usage
|
| 32 |
+
|
| 33 |
+
```python
|
| 34 |
+
from modeling_baa import BaaEmbeddingReranker # included in this repo
|
| 35 |
+
|
| 36 |
+
m = BaaEmbeddingReranker("baa-ai/Merino-Nano")
|
| 37 |
+
qv = m.embed(["how does a cross-encoder reranker work?"], is_query=True)[0]
|
| 38 |
+
dv = m.embed(["a cross-encoder scores a (query, document) pair jointly",
|
| 39 |
+
"bi-encoders embed query and document separately for fast retrieval"])
|
| 40 |
+
ranked = m.rerank("how does a cross-encoder reranker work?",
|
| 41 |
+
["a cross-encoder scores a (query, document) pair jointly",
|
| 42 |
+
"the mitochondria is the powerhouse of the cell"])
|
| 43 |
+
# -> [(doc, score), ...] sorted best-first
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
## Specs
|
| 47 |
+
|
| 48 |
+
| | |
|
| 49 |
+
|---|---|
|
| 50 |
+
| Embedding dim | 384 |
|
| 51 |
+
| Parameters | ~34M (embedder + reranker, shared word-embedding table) |
|
| 52 |
+
| Languages | English |
|
| 53 |
+
| Max sequence length | 512 |
|
| 54 |
+
| Hardware | CPU / edge / GPU |
|
| 55 |
+
|
| 56 |
+
## License & attribution
|
| 57 |
+
|
| 58 |
+
- **BAA Contributions** (shared-embedding architecture, router/loader code, packaging, weights, docs) are **proprietary to BAA AI (Black Sheep AI)** — see `LICENSE`.
|
| 59 |
+
- Incorporates the `MiniLM-L6-H384-uncased` backbone under the **MIT License** — see `LICENSE-minilm.txt`.
|
| 60 |
+
|
| 61 |
+
© 2026 BAA AI (Black Sheep AI) — baa.ai. Provided "as is" without warranty.
|
NOTICE
CHANGED
|
@@ -1,30 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
Copyright (c) 2026
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
licensed under the Apache License, Version 2.0:
|
| 7 |
-
|
| 8 |
-
1. sentence-transformers/all-MiniLM-L6-v2
|
| 9 |
-
Used as the bi-encoder embedder. Provides the canonical (shared)
|
| 10 |
-
word-embedding table for the combined model.
|
| 11 |
-
https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2
|
| 12 |
-
|
| 13 |
-
2. cross-encoder/ms-marco-MiniLM-L-6-v2
|
| 14 |
-
Used as the cross-encoder reranker. Its word-embedding table has been
|
| 15 |
-
removed on disk and is injected at load time from the shared table
|
| 16 |
-
above, reducing the combined footprint by ~26%.
|
| 17 |
-
https://huggingface.co/cross-encoder/ms-marco-MiniLM-L-6-v2
|
| 18 |
-
|
| 19 |
-
Both upstream models derive from the Microsoft MiniLM architecture
|
| 20 |
-
(microsoft/MiniLM-L6-H384-uncased).
|
| 21 |
-
|
| 22 |
-
Modifications by baa.ai:
|
| 23 |
-
- Unified the two models into a single artifact over one shared
|
| 24 |
-
word-embedding table (the reranker's word-embedding matrix is stored
|
| 25 |
-
once, in the embedder, and injected at load).
|
| 26 |
-
- Added a combined loader (modeling_baa_mini.py) exposing embed() and
|
| 27 |
-
rerank() over the shared backbone.
|
| 28 |
-
|
| 29 |
-
This NOTICE file is provided in accordance with Section 4(d) of the
|
| 30 |
-
Apache License, Version 2.0. See the LICENSE file for the full license text.
|
|
|
|
| 1 |
+
Merino-Nano
|
| 2 |
+
Copyright (c) 2026 BAA AI (Black Sheep AI). All rights reserved.
|
| 3 |
|
| 4 |
+
BAA Contributions: proprietary — see LICENSE.
|
| 5 |
+
Backbone: MiniLM-L6-H384-uncased — MIT License — see LICENSE-minilm.txt.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
license:
|
|
|
|
| 3 |
library_name: sentence-transformers
|
| 4 |
tags:
|
| 5 |
- retrieval
|
|
@@ -11,61 +12,50 @@ tags:
|
|
| 11 |
pipeline_tag: sentence-similarity
|
| 12 |
---
|
| 13 |
|
| 14 |
-
# baa.ai ·
|
| 15 |
|
| 16 |
-
**
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
- **
|
| 25 |
-
|
| 26 |
-
- **
|
|
|
|
|
|
|
| 27 |
|
| 28 |
## Usage
|
| 29 |
|
| 30 |
```python
|
| 31 |
-
from
|
| 32 |
-
|
| 33 |
-
m = BaaMiniEmbeddingReranker("baa-ai/Embedding-Reranker-Mini-v1")
|
| 34 |
|
| 35 |
-
|
| 36 |
qv = m.embed(["how does a cross-encoder reranker work?"], is_query=True)[0]
|
| 37 |
dv = m.embed(["a cross-encoder scores a (query, document) pair jointly",
|
| 38 |
"bi-encoders embed query and document separately for fast retrieval"])
|
| 39 |
-
|
| 40 |
-
# 2) rerank: score (query, doc) pairs and order them
|
| 41 |
ranked = m.rerank("how does a cross-encoder reranker work?",
|
| 42 |
["a cross-encoder scores a (query, document) pair jointly",
|
| 43 |
"the mitochondria is the powerhouse of the cell"])
|
| 44 |
# -> [(doc, score), ...] sorted best-first
|
| 45 |
```
|
| 46 |
|
| 47 |
-
Typical pipeline: embed the corpus once, retrieve the top-k by cosine for a query, then `rerank` those k and keep the top few for your LLM.
|
| 48 |
-
|
| 49 |
-
## Get the optimal models for *your* data
|
| 50 |
-
|
| 51 |
-
This model is a great **default**. But the best embedder and reranker are **corpus-specific** — the ideal choice depends on your documents and your notion of relevance.
|
| 52 |
-
|
| 53 |
-
**baa.ai offers exclusive tooling that identifies the optimal embedding and reranking models for your specific data**, so you ship the smallest models that maximize document recovery on your corpus. If you want that tailored recommendation for your use case, **reach out to baa.ai**.
|
| 54 |
-
|
| 55 |
## Specs
|
| 56 |
|
| 57 |
| | |
|
| 58 |
|---|---|
|
| 59 |
-
| Total parameters | ~34M (embedder + reranker, shared word-embedding table) |
|
| 60 |
| Embedding dim | 384 |
|
|
|
|
|
|
|
| 61 |
| Max sequence length | 512 |
|
| 62 |
-
| Footprint vs separate models | ~26% smaller on disk, no measured quality loss |
|
| 63 |
| Hardware | CPU / edge / GPU |
|
| 64 |
|
| 65 |
## License & attribution
|
| 66 |
|
| 67 |
-
|
| 68 |
-
- `
|
| 69 |
-
- `cross-encoder/ms-marco-MiniLM-L-6-v2` (reranker)
|
| 70 |
|
| 71 |
-
© baa.ai. Provided "as is" without warranty
|
|
|
|
| 1 |
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: baa-proprietary
|
| 4 |
library_name: sentence-transformers
|
| 5 |
tags:
|
| 6 |
- retrieval
|
|
|
|
| 12 |
pipeline_tag: sentence-similarity
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# baa.ai · Merino-Nano
|
| 16 |
|
| 17 |
+
**One model that does both halves of RAG retrieval — bi-encoder embedding *and* cross-encoder reranking — over a single shared word-embedding table.** A 384-dimensional English model, ~34M parameters, by BAA AI (Black Sheep AI).
|
| 18 |
|
| 19 |
+
## Get the optimal model for *your* data
|
| 20 |
|
| 21 |
+
Merino-Nano is a strong, cost-efficient **default**. But the best embedder + reranker is **corpus-specific** — the ideal choice depends on your documents and your notion of relevance. **baa.ai offers exclusive tooling that identifies the optimal embedding and reranking models for your specific data**, so you ship the smallest models that maximize document recovery on your corpus. For a tailored recommendation, **reach out to baa.ai**.
|
| 22 |
|
| 23 |
+
## What it is
|
| 24 |
|
| 25 |
+
A two-role retrieval model over a **shared input word-embedding matrix** (stored once). The bi-encoder embedder and a compact cross-encoder reranker are built on the same `MiniLM-L6-H384-uncased` backbone, so their word-embedding table is stored a single time and injected into the reranker at load — a smaller download at **no measured quality loss**, with no retraining.
|
| 26 |
+
|
| 27 |
+
- **Embed role:** bi-encoder, 384-d, L2-normalized.
|
| 28 |
+
- **Rerank role:** cross-encoder, single relevance logit per (query, document) pair.
|
| 29 |
+
- **Router:** call `.embed(...)` or `.rerank(...)`.
|
| 30 |
|
| 31 |
## Usage
|
| 32 |
|
| 33 |
```python
|
| 34 |
+
from modeling_baa import BaaEmbeddingReranker # included in this repo
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
m = BaaEmbeddingReranker("baa-ai/Merino-Nano")
|
| 37 |
qv = m.embed(["how does a cross-encoder reranker work?"], is_query=True)[0]
|
| 38 |
dv = m.embed(["a cross-encoder scores a (query, document) pair jointly",
|
| 39 |
"bi-encoders embed query and document separately for fast retrieval"])
|
|
|
|
|
|
|
| 40 |
ranked = m.rerank("how does a cross-encoder reranker work?",
|
| 41 |
["a cross-encoder scores a (query, document) pair jointly",
|
| 42 |
"the mitochondria is the powerhouse of the cell"])
|
| 43 |
# -> [(doc, score), ...] sorted best-first
|
| 44 |
```
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
## Specs
|
| 47 |
|
| 48 |
| | |
|
| 49 |
|---|---|
|
|
|
|
| 50 |
| Embedding dim | 384 |
|
| 51 |
+
| Parameters | ~34M (embedder + reranker, shared word-embedding table) |
|
| 52 |
+
| Languages | English |
|
| 53 |
| Max sequence length | 512 |
|
|
|
|
| 54 |
| Hardware | CPU / edge / GPU |
|
| 55 |
|
| 56 |
## License & attribution
|
| 57 |
|
| 58 |
+
- **BAA Contributions** (shared-embedding architecture, router/loader code, packaging, weights, docs) are **proprietary to BAA AI (Black Sheep AI)** — see `LICENSE`.
|
| 59 |
+
- Incorporates the `MiniLM-L6-H384-uncased` backbone under the **MIT License** — see `LICENSE-minilm.txt`.
|
|
|
|
| 60 |
|
| 61 |
+
© 2026 BAA AI (Black Sheep AI) — baa.ai. Provided "as is" without warranty.
|
config.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
{
|
| 2 |
-
"model_type": "baa-
|
| 3 |
-
"
|
| 4 |
-
"
|
| 5 |
-
"
|
|
|
|
| 6 |
"embedding_dim": 384,
|
|
|
|
|
|
|
| 7 |
"max_seq_length": 512,
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
-
|
|
|
|
|
|
|
|
|
| 1 |
{
|
| 2 |
+
"model_type": "baa-embedding-reranker",
|
| 3 |
+
"name": "Merino-Nano",
|
| 4 |
+
"version": "1",
|
| 5 |
+
"license": "Proprietary \u2014 BAA AI (Black Sheep AI); MiniLM-L6-H384-uncased backbone under MIT",
|
| 6 |
+
"architecture": "shared-word-embedding: one MiniLM-L6-H384-uncased word-embedding table shared across the embedder and reranker stacks",
|
| 7 |
"embedding_dim": 384,
|
| 8 |
+
"embed_query_prompt": "",
|
| 9 |
+
"embed_doc_prompt": "",
|
| 10 |
"max_seq_length": 512,
|
| 11 |
+
"params_millions": 34,
|
| 12 |
+
"backbone": "MiniLM-L6-H384-uncased (MIT)",
|
| 13 |
+
"trust_remote_code": false,
|
| 14 |
+
"loader": "modeling_baa.BaaEmbeddingReranker"
|
| 15 |
+
}
|
embedder/README.md
DELETED
|
@@ -1,175 +0,0 @@
|
|
| 1 |
-
---
|
| 2 |
-
base_model:
|
| 3 |
-
- nreimers/MiniLM-L6-H384-uncased
|
| 4 |
-
language: en
|
| 5 |
-
license: apache-2.0
|
| 6 |
-
library_name: sentence-transformers
|
| 7 |
-
tags:
|
| 8 |
-
- sentence-transformers
|
| 9 |
-
- feature-extraction
|
| 10 |
-
- sentence-similarity
|
| 11 |
-
- transformers
|
| 12 |
-
datasets:
|
| 13 |
-
- s2orc
|
| 14 |
-
- flax-sentence-embeddings/stackexchange_xml
|
| 15 |
-
- ms_marco
|
| 16 |
-
- gooaq
|
| 17 |
-
- yahoo_answers_topics
|
| 18 |
-
- code_search_net
|
| 19 |
-
- search_qa
|
| 20 |
-
- eli5
|
| 21 |
-
- snli
|
| 22 |
-
- multi_nli
|
| 23 |
-
- wikihow
|
| 24 |
-
- natural_questions
|
| 25 |
-
- trivia_qa
|
| 26 |
-
- embedding-data/sentence-compression
|
| 27 |
-
- embedding-data/flickr30k-captions
|
| 28 |
-
- embedding-data/altlex
|
| 29 |
-
- embedding-data/simple-wiki
|
| 30 |
-
- embedding-data/QQP
|
| 31 |
-
- embedding-data/SPECTER
|
| 32 |
-
- embedding-data/PAQ_pairs
|
| 33 |
-
- embedding-data/WikiAnswers
|
| 34 |
-
pipeline_tag: sentence-similarity
|
| 35 |
-
---
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# all-MiniLM-L6-v2
|
| 39 |
-
This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
| 40 |
-
|
| 41 |
-
## Usage (Sentence-Transformers)
|
| 42 |
-
Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
|
| 43 |
-
|
| 44 |
-
```
|
| 45 |
-
pip install -U sentence-transformers
|
| 46 |
-
```
|
| 47 |
-
|
| 48 |
-
Then you can use the model like this:
|
| 49 |
-
```python
|
| 50 |
-
from sentence_transformers import SentenceTransformer
|
| 51 |
-
sentences = ["This is an example sentence", "Each sentence is converted"]
|
| 52 |
-
|
| 53 |
-
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 54 |
-
embeddings = model.encode(sentences)
|
| 55 |
-
print(embeddings)
|
| 56 |
-
```
|
| 57 |
-
|
| 58 |
-
## Usage (HuggingFace Transformers)
|
| 59 |
-
Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
|
| 60 |
-
|
| 61 |
-
```python
|
| 62 |
-
from transformers import AutoTokenizer, AutoModel
|
| 63 |
-
import torch
|
| 64 |
-
import torch.nn.functional as F
|
| 65 |
-
|
| 66 |
-
#Mean Pooling - Take attention mask into account for correct averaging
|
| 67 |
-
def mean_pooling(model_output, attention_mask):
|
| 68 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
| 69 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 70 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Sentences we want sentence embeddings for
|
| 74 |
-
sentences = ['This is an example sentence', 'Each sentence is converted']
|
| 75 |
-
|
| 76 |
-
# Load model from HuggingFace Hub
|
| 77 |
-
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
| 78 |
-
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
| 79 |
-
|
| 80 |
-
# Tokenize sentences
|
| 81 |
-
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
| 82 |
-
|
| 83 |
-
# Compute token embeddings
|
| 84 |
-
with torch.no_grad():
|
| 85 |
-
model_output = model(**encoded_input)
|
| 86 |
-
|
| 87 |
-
# Perform pooling
|
| 88 |
-
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 89 |
-
|
| 90 |
-
# Normalize embeddings
|
| 91 |
-
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
| 92 |
-
|
| 93 |
-
print("Sentence embeddings:")
|
| 94 |
-
print(sentence_embeddings)
|
| 95 |
-
```
|
| 96 |
-
|
| 97 |
-
------
|
| 98 |
-
|
| 99 |
-
## Background
|
| 100 |
-
|
| 101 |
-
The project aims to train sentence embedding models on very large sentence level datasets using a self-supervised
|
| 102 |
-
contrastive learning objective. We used the pretrained [`nreimers/MiniLM-L6-H384-uncased`](https://huggingface.co/nreimers/MiniLM-L6-H384-uncased) model and fine-tuned in on a
|
| 103 |
-
1B sentence pairs dataset. We use a contrastive learning objective: given a sentence from the pair, the model should predict which out of a set of randomly sampled other sentences, was actually paired with it in our dataset.
|
| 104 |
-
|
| 105 |
-
We developed this model during the
|
| 106 |
-
[Community week using JAX/Flax for NLP & CV](https://discuss.huggingface.co/t/open-to-the-community-community-week-using-jax-flax-for-nlp-cv/7104),
|
| 107 |
-
organized by Hugging Face. We developed this model as part of the project:
|
| 108 |
-
[Train the Best Sentence Embedding Model Ever with 1B Training Pairs](https://discuss.huggingface.co/t/train-the-best-sentence-embedding-model-ever-with-1b-training-pairs/7354). We benefited from efficient hardware infrastructure to run the project: 7 TPUs v3-8, as well as intervention from Googles Flax, JAX, and Cloud team member about efficient deep learning frameworks.
|
| 109 |
-
|
| 110 |
-
## Intended uses
|
| 111 |
-
|
| 112 |
-
Our model is intended to be used as a sentence and short paragraph encoder. Given an input text, it outputs a vector which captures
|
| 113 |
-
the semantic information. The sentence vector may be used for information retrieval, clustering or sentence similarity tasks.
|
| 114 |
-
|
| 115 |
-
By default, input text longer than 256 word pieces is truncated.
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
## Training procedure
|
| 119 |
-
|
| 120 |
-
### Pre-training
|
| 121 |
-
|
| 122 |
-
We use the pretrained [`nreimers/MiniLM-L6-H384-uncased`](https://huggingface.co/nreimers/MiniLM-L6-H384-uncased) model. Please refer to the model card for more detailed information about the pre-training procedure.
|
| 123 |
-
|
| 124 |
-
### Fine-tuning
|
| 125 |
-
|
| 126 |
-
We fine-tune the model using a contrastive objective. Formally, we compute the cosine similarity from each possible sentence pairs from the batch.
|
| 127 |
-
We then apply the cross entropy loss by comparing with true pairs.
|
| 128 |
-
|
| 129 |
-
#### Hyper parameters
|
| 130 |
-
|
| 131 |
-
We trained our model on a TPU v3-8. We train the model during 100k steps using a batch size of 1024 (128 per TPU core).
|
| 132 |
-
We use a learning rate warm up of 500. The sequence length was limited to 128 tokens. We used the AdamW optimizer with
|
| 133 |
-
a 2e-5 learning rate. The full training script is accessible in this current repository: `train_script.py`.
|
| 134 |
-
|
| 135 |
-
#### Training data
|
| 136 |
-
|
| 137 |
-
We use the concatenation from multiple datasets to fine-tune our model. The total number of sentence pairs is above 1 billion sentences.
|
| 138 |
-
We sampled each dataset given a weighted probability which configuration is detailed in the `data_config.json` file.
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
| Dataset | Paper | Number of training tuples |
|
| 142 |
-
|--------------------------------------------------------|:----------------------------------------:|:--------------------------:|
|
| 143 |
-
| [Reddit comments (2015-2018)](https://github.com/PolyAI-LDN/conversational-datasets/tree/master/reddit) | [paper](https://arxiv.org/abs/1904.06472) | 726,484,430 |
|
| 144 |
-
| [S2ORC](https://github.com/allenai/s2orc) Citation pairs (Abstracts) | [paper](https://aclanthology.org/2020.acl-main.447/) | 116,288,806 |
|
| 145 |
-
| [WikiAnswers](https://github.com/afader/oqa#wikianswers-corpus) Duplicate question pairs | [paper](https://doi.org/10.1145/2623330.2623677) | 77,427,422 |
|
| 146 |
-
| [PAQ](https://github.com/facebookresearch/PAQ) (Question, Answer) pairs | [paper](https://arxiv.org/abs/2102.07033) | 64,371,441 |
|
| 147 |
-
| [S2ORC](https://github.com/allenai/s2orc) Citation pairs (Titles) | [paper](https://aclanthology.org/2020.acl-main.447/) | 52,603,982 |
|
| 148 |
-
| [S2ORC](https://github.com/allenai/s2orc) (Title, Abstract) | [paper](https://aclanthology.org/2020.acl-main.447/) | 41,769,185 |
|
| 149 |
-
| [Stack Exchange](https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_xml) (Title, Body) pairs | - | 25,316,456 |
|
| 150 |
-
| [Stack Exchange](https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_xml) (Title+Body, Answer) pairs | - | 21,396,559 |
|
| 151 |
-
| [Stack Exchange](https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_xml) (Title, Answer) pairs | - | 21,396,559 |
|
| 152 |
-
| [MS MARCO](https://microsoft.github.io/msmarco/) triplets | [paper](https://doi.org/10.1145/3404835.3462804) | 9,144,553 |
|
| 153 |
-
| [GOOAQ: Open Question Answering with Diverse Answer Types](https://github.com/allenai/gooaq) | [paper](https://arxiv.org/pdf/2104.08727.pdf) | 3,012,496 |
|
| 154 |
-
| [Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo-answers-dataset) (Title, Answer) | [paper](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02-Abstract.html) | 1,198,260 |
|
| 155 |
-
| [Code Search](https://huggingface.co/datasets/code_search_net) | - | 1,151,414 |
|
| 156 |
-
| [COCO](https://cocodataset.org/#home) Image captions | [paper](https://link.springer.com/chapter/10.1007%2F978-3-319-10602-1_48) | 828,395|
|
| 157 |
-
| [SPECTER](https://github.com/allenai/specter) citation triplets | [paper](https://doi.org/10.18653/v1/2020.acl-main.207) | 684,100 |
|
| 158 |
-
| [Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo-answers-dataset) (Question, Answer) | [paper](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02-Abstract.html) | 681,164 |
|
| 159 |
-
| [Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo-answers-dataset) (Title, Question) | [paper](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02-Abstract.html) | 659,896 |
|
| 160 |
-
| [SearchQA](https://huggingface.co/datasets/search_qa) | [paper](https://arxiv.org/abs/1704.05179) | 582,261 |
|
| 161 |
-
| [Eli5](https://huggingface.co/datasets/eli5) | [paper](https://doi.org/10.18653/v1/p19-1346) | 325,475 |
|
| 162 |
-
| [Flickr 30k](https://shannon.cs.illinois.edu/DenotationGraph/) | [paper](https://transacl.org/ojs/index.php/tacl/article/view/229/33) | 317,695 |
|
| 163 |
-
| [Stack Exchange](https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_xml) Duplicate questions (titles) | | 304,525 |
|
| 164 |
-
| AllNLI ([SNLI](https://nlp.stanford.edu/projects/snli/) and [MultiNLI](https://cims.nyu.edu/~sbowman/multinli/) | [paper SNLI](https://doi.org/10.18653/v1/d15-1075), [paper MultiNLI](https://doi.org/10.18653/v1/n18-1101) | 277,230 |
|
| 165 |
-
| [Stack Exchange](https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_xml) Duplicate questions (bodies) | | 250,519 |
|
| 166 |
-
| [Stack Exchange](https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_xml) Duplicate questions (titles+bodies) | | 250,460 |
|
| 167 |
-
| [Sentence Compression](https://github.com/google-research-datasets/sentence-compression) | [paper](https://www.aclweb.org/anthology/D13-1155/) | 180,000 |
|
| 168 |
-
| [Wikihow](https://github.com/pvl/wikihow_pairs_dataset) | [paper](https://arxiv.org/abs/1810.09305) | 128,542 |
|
| 169 |
-
| [Altlex](https://github.com/chridey/altlex/) | [paper](https://aclanthology.org/P16-1135.pdf) | 112,696 |
|
| 170 |
-
| [Quora Question Triplets](https://quoradata.quora.com/First-Quora-Dataset-Release-Question-Pairs) | - | 103,663 |
|
| 171 |
-
| [Simple Wikipedia](https://cs.pomona.edu/~dkauchak/simplification/) | [paper](https://www.aclweb.org/anthology/P11-2117/) | 102,225 |
|
| 172 |
-
| [Natural Questions (NQ)](https://ai.google.com/research/NaturalQuestions) | [paper](https://transacl.org/ojs/index.php/tacl/article/view/1455) | 100,231 |
|
| 173 |
-
| [SQuAD2.0](https://rajpurkar.github.io/SQuAD-explorer/) | [paper](https://aclanthology.org/P18-2124.pdf) | 87,599 |
|
| 174 |
-
| [TriviaQA](https://huggingface.co/datasets/trivia_qa) | - | 73,346 |
|
| 175 |
-
| **Total** | | **1,170,060,424** |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modeling_baa_mini.py → modeling_baa.py
RENAMED
|
@@ -1,40 +1,64 @@
|
|
| 1 |
-
"""baa
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
Usage:
|
| 6 |
-
from
|
| 7 |
-
m =
|
| 8 |
-
qv = m.embed(["what is a cross-encoder?"], is_query=True)
|
| 9 |
-
dv = m.embed(["a cross-encoder scores a (query, doc) pair jointly"
|
| 10 |
ranked = m.rerank("what is a cross-encoder?", ["doc A ...", "doc B ..."]) # [(doc, score), ...] desc
|
| 11 |
"""
|
| 12 |
-
import os, torch
|
| 13 |
from safetensors.torch import load_file
|
| 14 |
from sentence_transformers import SentenceTransformer
|
| 15 |
from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
|
| 16 |
|
| 17 |
|
| 18 |
-
class
|
| 19 |
def __init__(self, path=None, device=None):
|
| 20 |
path = path or os.path.dirname(os.path.abspath(__file__))
|
| 21 |
self.device = device or ("mps" if torch.backends.mps.is_available()
|
| 22 |
else ("cuda" if torch.cuda.is_available() else "cpu"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
emb_dir, rr_dir = os.path.join(path, "embedder"), os.path.join(path, "reranker")
|
|
|
|
| 24 |
# embedder = bi-encoder; holds the canonical shared word-embedding table
|
| 25 |
-
self.embedder = SentenceTransformer(emb_dir, device=self.device)
|
| 26 |
shared_wemb = self.embedder[0].auto_model.embeddings.word_embeddings.weight.data
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
self.reranker.load_state_dict(load_file(os.path.join(rr_dir, "model.safetensors")), strict=False)
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
self.reranker.to(self.device).eval()
|
| 33 |
-
self.rr_tok = AutoTokenizer.from_pretrained(rr_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def embed(self, texts, is_query=False, batch_size=32):
|
| 36 |
-
"""Return L2-normalized bi-encoder vectors.
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
batch_size=batch_size, show_progress_bar=False)
|
| 39 |
|
| 40 |
@torch.no_grad()
|
|
@@ -52,12 +76,12 @@ class BaaMiniEmbeddingReranker:
|
|
| 52 |
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
|
|
|
|
| 56 |
q = "How does a cross-encoder reranker work?"
|
| 57 |
docs = ["A cross-encoder jointly encodes the query and document to score relevance.",
|
| 58 |
"The mitochondria is the powerhouse of the cell.",
|
| 59 |
"Bi-encoders embed query and document separately for fast retrieval."]
|
| 60 |
-
import numpy as np
|
| 61 |
qv = m.embed([q], is_query=True)[0]; dv = m.embed(docs)
|
| 62 |
print("embed cos:", [round(float(np.dot(qv, d)), 3) for d in dv])
|
| 63 |
print("rerank :", [(round(s, 2), d[:45]) for d, s in m.rerank(q, docs)])
|
|
|
|
| 1 |
+
"""baa.ai unified Embedding+Reranker loader (generic).
|
| 2 |
+
|
| 3 |
+
A single artifact that does both halves of RAG retrieval — bi-encoder embedding AND cross-encoder reranking —
|
| 4 |
+
over ONE shared word-embedding table. The reranker's word-embedding matrix is stored only once (in the
|
| 5 |
+
embedder) and injected at load, so the packaged model is smaller than shipping the two components separately,
|
| 6 |
+
at no measured quality cost.
|
| 7 |
+
|
| 8 |
+
Works for BERT-based and XLM-RoBERTa-based stacks alike: the reranker's encoder submodule is resolved
|
| 9 |
+
generically via `reranker.base_model` (so `.bert` / `.roberta` are both handled). Optional per-model query/doc
|
| 10 |
+
prompts are read from config.json (e.g. some models use a "query: " prefix).
|
| 11 |
|
| 12 |
Usage:
|
| 13 |
+
from modeling_baa import BaaEmbeddingReranker
|
| 14 |
+
m = BaaEmbeddingReranker("path/to/model-dir")
|
| 15 |
+
qv = m.embed(["what is a cross-encoder?"], is_query=True) # normalized bi-encoder vectors
|
| 16 |
+
dv = m.embed(["a cross-encoder scores a (query, doc) pair jointly"])
|
| 17 |
ranked = m.rerank("what is a cross-encoder?", ["doc A ...", "doc B ..."]) # [(doc, score), ...] desc
|
| 18 |
"""
|
| 19 |
+
import os, json, torch
|
| 20 |
from safetensors.torch import load_file
|
| 21 |
from sentence_transformers import SentenceTransformer
|
| 22 |
from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
|
| 23 |
|
| 24 |
|
| 25 |
+
class BaaEmbeddingReranker:
|
| 26 |
def __init__(self, path=None, device=None):
|
| 27 |
path = path or os.path.dirname(os.path.abspath(__file__))
|
| 28 |
self.device = device or ("mps" if torch.backends.mps.is_available()
|
| 29 |
else ("cuda" if torch.cuda.is_available() else "cpu"))
|
| 30 |
+
cfg = {}
|
| 31 |
+
cfg_path = os.path.join(path, "config.json")
|
| 32 |
+
if os.path.exists(cfg_path):
|
| 33 |
+
cfg = json.load(open(cfg_path))
|
| 34 |
+
self.q_prompt = cfg.get("embed_query_prompt", "") or ""
|
| 35 |
+
self.d_prompt = cfg.get("embed_doc_prompt", "") or ""
|
| 36 |
+
trc = bool(cfg.get("trust_remote_code", False))
|
| 37 |
emb_dir, rr_dir = os.path.join(path, "embedder"), os.path.join(path, "reranker")
|
| 38 |
+
|
| 39 |
# embedder = bi-encoder; holds the canonical shared word-embedding table
|
| 40 |
+
self.embedder = SentenceTransformer(emb_dir, device=self.device, trust_remote_code=trc)
|
| 41 |
shared_wemb = self.embedder[0].auto_model.embeddings.word_embeddings.weight.data
|
| 42 |
+
|
| 43 |
+
# reranker = cross-encoder seq-classifier; word-embedding stripped on disk -> injected from shared table
|
| 44 |
+
rr_cfg = AutoConfig.from_pretrained(rr_dir, trust_remote_code=trc)
|
| 45 |
+
self.reranker = AutoModelForSequenceClassification.from_config(rr_cfg, trust_remote_code=trc)
|
| 46 |
self.reranker.load_state_dict(load_file(os.path.join(rr_dir, "model.safetensors")), strict=False)
|
| 47 |
+
# resolve the encoder submodule generically (.bert for BERT, .roberta for XLM-R, ...)
|
| 48 |
+
base = self.reranker.base_model
|
| 49 |
+
base.embeddings.word_embeddings.weight.data = shared_wemb.to(self.reranker.dtype).clone()
|
| 50 |
self.reranker.to(self.device).eval()
|
| 51 |
+
self.rr_tok = AutoTokenizer.from_pretrained(rr_dir, trust_remote_code=trc)
|
| 52 |
+
# Weights may be stored fp16 on disk (smaller artifact); CPU can't compute in half -> upcast to fp32.
|
| 53 |
+
if str(self.device) == "cpu":
|
| 54 |
+
self.embedder = self.embedder.to(torch.float32)
|
| 55 |
+
self.reranker = self.reranker.float()
|
| 56 |
|
| 57 |
def embed(self, texts, is_query=False, batch_size=32):
|
| 58 |
+
"""Return L2-normalized bi-encoder vectors. Applies the model's query/doc prompt if configured."""
|
| 59 |
+
prompt = self.q_prompt if is_query else self.d_prompt
|
| 60 |
+
texts = [prompt + t for t in texts] if prompt else list(texts)
|
| 61 |
+
return self.embedder.encode(texts, normalize_embeddings=True,
|
| 62 |
batch_size=batch_size, show_progress_bar=False)
|
| 63 |
|
| 64 |
@torch.no_grad()
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
import numpy as np
|
| 80 |
+
m = BaaEmbeddingReranker()
|
| 81 |
q = "How does a cross-encoder reranker work?"
|
| 82 |
docs = ["A cross-encoder jointly encodes the query and document to score relevance.",
|
| 83 |
"The mitochondria is the powerhouse of the cell.",
|
| 84 |
"Bi-encoders embed query and document separately for fast retrieval."]
|
|
|
|
| 85 |
qv = m.embed([q], is_query=True)[0]; dv = m.embed(docs)
|
| 86 |
print("embed cos:", [round(float(np.dot(qv, d)), 3) for d in dv])
|
| 87 |
print("rerank :", [(round(s, 2), d[:45]) for d, s in m.rerank(q, docs)])
|