Sentence Similarity
sentence-transformers
Safetensors
baa-embedding-reranker
retrieval
embeddings
reranker
cross-encoder
rag
Instructions to use baa-ai/Merino-Large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use baa-ai/Merino-Large with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("baa-ai/Merino-Large") 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-Large (backbone-only attribution)
Browse files- LICENSE +24 -202
- LICENSE-xlm-roberta-large.txt +23 -0
- MODEL_CARD.md +61 -0
- NOTICE +4 -26
- README.md +18 -21
- config.json +9 -17
- embedder/README.md +0 -300
- modeling_baa.py +5 -1
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-Large — 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 xlm-roberta-large backbone,
|
| 14 |
+
provided under the MIT License — see LICENSE-xlm-roberta-large.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-xlm-roberta-large.txt
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Backbone component: xlm-roberta-large — MIT License
|
| 2 |
+
|
| 3 |
+
MIT License
|
| 4 |
+
|
| 5 |
+
Copyright (c) Facebook, Inc. and its affiliates.
|
| 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-Large
|
| 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 1024-dimensional multilingual model, ~872M parameters, by BAA AI (Black Sheep AI).
|
| 18 |
+
|
| 19 |
+
## Get the optimal model for *your* data
|
| 20 |
+
|
| 21 |
+
Merino-Large 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 large cross-encoder reranker are built on the same `xlm-roberta-large` 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, 1024-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-Large")
|
| 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 | 1024 |
|
| 51 |
+
| Parameters | ~872M (embedder + reranker, shared word-embedding table) |
|
| 52 |
+
| Languages | multilingual |
|
| 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 `xlm-roberta-large` backbone under the **MIT License** — see `LICENSE-xlm-roberta-large.txt`.
|
| 60 |
+
|
| 61 |
+
© 2026 BAA AI (Black Sheep AI) — baa.ai. Provided "as is" without warranty.
|
NOTICE
CHANGED
|
@@ -1,27 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
Copyright (c) 2026
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
1. BAAI/bge-m3 (license: MIT)
|
| 8 |
-
Used as the bi-encoder embedder. Provides the canonical (shared)
|
| 9 |
-
word-embedding table for the combined model.
|
| 10 |
-
https://huggingface.co/BAAI/bge-m3
|
| 11 |
-
|
| 12 |
-
2. BAAI/bge-reranker-large (license: MIT)
|
| 13 |
-
Used as the cross-encoder reranker. Its word-embedding table has been
|
| 14 |
-
removed on disk and is injected at load time from the shared table
|
| 15 |
-
above, reducing the combined footprint.
|
| 16 |
-
https://huggingface.co/BAAI/bge-reranker-large
|
| 17 |
-
|
| 18 |
-
Both upstream models derive from the XLM-RoBERTa-large architecture.
|
| 19 |
-
|
| 20 |
-
Modifications by baa.ai:
|
| 21 |
-
- Unified the two models into a single artifact over one shared
|
| 22 |
-
word-embedding table (the reranker's word-embedding matrix is stored
|
| 23 |
-
once, in the embedder, and injected at load).
|
| 24 |
-
- Added a combined loader (modeling_baa.py) exposing embed() and rerank().
|
| 25 |
-
|
| 26 |
-
This NOTICE file is provided in accordance with Section 4(d) of the
|
| 27 |
-
Apache License, Version 2.0. See the LICENSE file for the full license text.
|
|
|
|
| 1 |
+
Merino-Large
|
| 2 |
+
Copyright (c) 2026 BAA AI (Black Sheep AI). All rights reserved.
|
| 3 |
|
| 4 |
+
BAA Contributions: proprietary — see LICENSE.
|
| 5 |
+
Backbone: xlm-roberta-large — MIT License — see LICENSE-xlm-roberta-large.txt.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
license:
|
|
|
|
| 3 |
library_name: sentence-transformers
|
| 4 |
tags:
|
| 5 |
- retrieval
|
|
@@ -11,26 +12,28 @@ 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 modeling_baa import BaaEmbeddingReranker # included in this repo
|
| 32 |
|
| 33 |
-
m = BaaEmbeddingReranker("baa-ai/
|
| 34 |
qv = m.embed(["how does a cross-encoder reranker work?"], is_query=True)[0]
|
| 35 |
dv = m.embed(["a cross-encoder scores a (query, document) pair jointly",
|
| 36 |
"bi-encoders embed query and document separately for fast retrieval"])
|
|
@@ -40,25 +43,19 @@ ranked = m.rerank("how does a cross-encoder reranker work?",
|
|
| 40 |
# -> [(doc, score), ...] sorted best-first
|
| 41 |
```
|
| 42 |
|
| 43 |
-
## Get the optimal models for *your* data
|
| 44 |
-
|
| 45 |
-
This model is a great **default**. But the best embedder and reranker are **corpus-specific**. **baa.ai offers exclusive tooling that identifies the optimal embedding and reranking models for your specific data** — if you want that tailored recommendation, **reach out to baa.ai**.
|
| 46 |
-
|
| 47 |
## Specs
|
| 48 |
|
| 49 |
| | |
|
| 50 |
|---|---|
|
| 51 |
| Embedding dim | 1024 |
|
| 52 |
-
|
|
|
|
|
| 53 |
| Max sequence length | 512 |
|
| 54 |
-
| Combined params | ~871.6M (vs ~1127.6M separate) |
|
| 55 |
-
| Footprint vs separate models | ~22.7% smaller on disk, no measured quality loss |
|
| 56 |
| Hardware | CPU / edge / GPU |
|
| 57 |
|
| 58 |
## License & attribution
|
| 59 |
|
| 60 |
-
|
| 61 |
-
- `
|
| 62 |
-
- `BAAI/bge-reranker-large` (reranker)
|
| 63 |
|
| 64 |
-
© 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-Large
|
| 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 1024-dimensional multilingual model, ~872M parameters, by BAA AI (Black Sheep AI).
|
| 18 |
|
| 19 |
+
## Get the optimal model for *your* data
|
| 20 |
|
| 21 |
+
Merino-Large 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 large cross-encoder reranker are built on the same `xlm-roberta-large` 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, 1024-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-Large")
|
| 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"])
|
|
|
|
| 43 |
# -> [(doc, score), ...] sorted best-first
|
| 44 |
```
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
## Specs
|
| 47 |
|
| 48 |
| | |
|
| 49 |
|---|---|
|
| 50 |
| Embedding dim | 1024 |
|
| 51 |
+
| Parameters | ~872M (embedder + reranker, shared word-embedding table) |
|
| 52 |
+
| Languages | multilingual |
|
| 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 `xlm-roberta-large` backbone under the **MIT License** — see `LICENSE-xlm-roberta-large.txt`.
|
|
|
|
| 60 |
|
| 61 |
+
© 2026 BAA AI (Black Sheep AI) — baa.ai. Provided "as is" without warranty.
|
config.json
CHANGED
|
@@ -1,23 +1,15 @@
|
|
| 1 |
{
|
| 2 |
-
"model_type": "baa-
|
| 3 |
-
"
|
| 4 |
-
"
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
"loader": "modeling_baa.BaaEmbeddingReranker",
|
| 9 |
"embed_query_prompt": "",
|
| 10 |
"embed_doc_prompt": "",
|
| 11 |
-
"embedding_dim": 1024,
|
| 12 |
-
"vocab_size": 250002,
|
| 13 |
"max_seq_length": 512,
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"disk_saving_pct": 22.7,
|
| 17 |
-
"license": "apache-2.0",
|
| 18 |
"trust_remote_code": false,
|
| 19 |
-
"
|
| 20 |
-
"embedder": "BAAI/bge-m3",
|
| 21 |
-
"reranker": "BAAI/bge-reranker-large"
|
| 22 |
-
}
|
| 23 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"model_type": "baa-embedding-reranker",
|
| 3 |
+
"name": "Merino-Large",
|
| 4 |
+
"version": "1",
|
| 5 |
+
"license": "Proprietary \u2014 BAA AI (Black Sheep AI); xlm-roberta-large backbone under MIT",
|
| 6 |
+
"architecture": "shared-word-embedding: one xlm-roberta-large word-embedding table shared across the embedder and reranker stacks",
|
| 7 |
+
"embedding_dim": 1024,
|
|
|
|
| 8 |
"embed_query_prompt": "",
|
| 9 |
"embed_doc_prompt": "",
|
|
|
|
|
|
|
| 10 |
"max_seq_length": 512,
|
| 11 |
+
"params_millions": 872,
|
| 12 |
+
"backbone": "xlm-roberta-large (MIT)",
|
|
|
|
|
|
|
| 13 |
"trust_remote_code": false,
|
| 14 |
+
"loader": "modeling_baa.BaaEmbeddingReranker"
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
embedder/README.md
DELETED
|
@@ -1,300 +0,0 @@
|
|
| 1 |
-
---
|
| 2 |
-
pipeline_tag: sentence-similarity
|
| 3 |
-
tags:
|
| 4 |
-
- sentence-transformers
|
| 5 |
-
- feature-extraction
|
| 6 |
-
- sentence-similarity
|
| 7 |
-
license: mit
|
| 8 |
-
---
|
| 9 |
-
|
| 10 |
-
For more details please refer to our github repo: https://github.com/FlagOpen/FlagEmbedding
|
| 11 |
-
|
| 12 |
-
# BGE-M3 ([paper](https://arxiv.org/pdf/2402.03216.pdf), [code](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/BGE_M3))
|
| 13 |
-
|
| 14 |
-
In this project, we introduce BGE-M3, which is distinguished for its versatility in Multi-Functionality, Multi-Linguality, and Multi-Granularity.
|
| 15 |
-
- Multi-Functionality: It can simultaneously perform the three common retrieval functionalities of embedding model: dense retrieval, multi-vector retrieval, and sparse retrieval.
|
| 16 |
-
- Multi-Linguality: It can support more than 100 working languages.
|
| 17 |
-
- Multi-Granularity: It is able to process inputs of different granularities, spanning from short sentences to long documents of up to 8192 tokens.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
**Some suggestions for retrieval pipeline in RAG**
|
| 22 |
-
|
| 23 |
-
We recommend to use the following pipeline: hybrid retrieval + re-ranking.
|
| 24 |
-
- Hybrid retrieval leverages the strengths of various methods, offering higher accuracy and stronger generalization capabilities.
|
| 25 |
-
A classic example: using both embedding retrieval and the BM25 algorithm.
|
| 26 |
-
Now, you can try to use BGE-M3, which supports both embedding and sparse retrieval.
|
| 27 |
-
This allows you to obtain token weights (similar to the BM25) without any additional cost when generate dense embeddings.
|
| 28 |
-
To use hybrid retrieval, you can refer to [Vespa](https://github.com/vespa-engine/pyvespa/blob/master/docs/sphinx/source/examples/mother-of-all-embedding-models-cloud.ipynb
|
| 29 |
-
) and [Milvus](https://github.com/milvus-io/pymilvus/blob/master/examples/hello_hybrid_sparse_dense.py).
|
| 30 |
-
|
| 31 |
-
- As cross-encoder models, re-ranker demonstrates higher accuracy than bi-encoder embedding model.
|
| 32 |
-
Utilizing the re-ranking model (e.g., [bge-reranker](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/reranker), [bge-reranker-v2](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/llm_reranker)) after retrieval can further filter the selected text.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
## News:
|
| 36 |
-
- 2024/7/1: **We update the MIRACL evaluation results of BGE-M3**. To reproduce the new results, you can refer to: [bge-m3_miracl_2cr](https://huggingface.co/datasets/hanhainebula/bge-m3_miracl_2cr). We have also updated our [paper](https://arxiv.org/pdf/2402.03216) on arXiv.
|
| 37 |
-
<details>
|
| 38 |
-
<summary> Details </summary>
|
| 39 |
-
|
| 40 |
-
The previous test results were lower because we mistakenly removed the passages that have the same id as the query from the search results. After correcting this mistake, the overall performance of BGE-M3 on MIRACL is higher than the previous results, but the experimental conclusion remains unchanged. The other results are not affected by this mistake. To reproduce the previous lower results, you need to add the `--remove-query` parameter when using `pyserini.search.faiss` or `pyserini.search.lucene` to search the passages.
|
| 41 |
-
|
| 42 |
-
</details>
|
| 43 |
-
- 2024/3/20: **Thanks Milvus team!** Now you can use hybrid retrieval of bge-m3 in Milvus: [pymilvus/examples
|
| 44 |
-
/hello_hybrid_sparse_dense.py](https://github.com/milvus-io/pymilvus/blob/master/examples/hello_hybrid_sparse_dense.py).
|
| 45 |
-
- 2024/3/8: **Thanks for the [experimental results](https://towardsdatascience.com/openai-vs-open-source-multilingual-embedding-models-e5ccb7c90f05) from @[Yannael](https://huggingface.co/Yannael). In this benchmark, BGE-M3 achieves top performance in both English and other languages, surpassing models such as OpenAI.**
|
| 46 |
-
- 2024/3/2: Release unified fine-tuning [example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/unified_finetune) and [data](https://huggingface.co/datasets/Shitao/bge-m3-data)
|
| 47 |
-
- 2024/2/6: We release the [MLDR](https://huggingface.co/datasets/Shitao/MLDR) (a long document retrieval dataset covering 13 languages) and [evaluation pipeline](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MLDR).
|
| 48 |
-
- 2024/2/1: **Thanks for the excellent tool from Vespa.** You can easily use multiple modes of BGE-M3 following this [notebook](https://github.com/vespa-engine/pyvespa/blob/master/docs/sphinx/source/examples/mother-of-all-embedding-models-cloud.ipynb)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
## Specs
|
| 52 |
-
|
| 53 |
-
- Model
|
| 54 |
-
|
| 55 |
-
| Model Name | Dimension | Sequence Length | Introduction |
|
| 56 |
-
|:----:|:---:|:---:|:---:|
|
| 57 |
-
| [BAAI/bge-m3](https://huggingface.co/BAAI/bge-m3) | 1024 | 8192 | multilingual; unified fine-tuning (dense, sparse, and colbert) from bge-m3-unsupervised|
|
| 58 |
-
| [BAAI/bge-m3-unsupervised](https://huggingface.co/BAAI/bge-m3-unsupervised) | 1024 | 8192 | multilingual; contrastive learning from bge-m3-retromae |
|
| 59 |
-
| [BAAI/bge-m3-retromae](https://huggingface.co/BAAI/bge-m3-retromae) | -- | 8192 | multilingual; extend the max_length of [xlm-roberta](https://huggingface.co/FacebookAI/xlm-roberta-large) to 8192 and further pretrained via [retromae](https://github.com/staoxiao/RetroMAE)|
|
| 60 |
-
| [BAAI/bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5) | 1024 | 512 | English model |
|
| 61 |
-
| [BAAI/bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 768 | 512 | English model |
|
| 62 |
-
| [BAAI/bge-small-en-v1.5](https://huggingface.co/BAAI/bge-small-en-v1.5) | 384 | 512 | English model |
|
| 63 |
-
|
| 64 |
-
- Data
|
| 65 |
-
|
| 66 |
-
| Dataset | Introduction |
|
| 67 |
-
|:----------------------------------------------------------:|:-------------------------------------------------:|
|
| 68 |
-
| [MLDR](https://huggingface.co/datasets/Shitao/MLDR) | Docuemtn Retrieval Dataset, covering 13 languages |
|
| 69 |
-
| [bge-m3-data](https://huggingface.co/datasets/Shitao/bge-m3-data) | Fine-tuning data used by bge-m3 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
## FAQ
|
| 74 |
-
|
| 75 |
-
**1. Introduction for different retrieval methods**
|
| 76 |
-
|
| 77 |
-
- Dense retrieval: map the text into a single embedding, e.g., [DPR](https://arxiv.org/abs/2004.04906), [BGE-v1.5](https://github.com/FlagOpen/FlagEmbedding)
|
| 78 |
-
- Sparse retrieval (lexical matching): a vector of size equal to the vocabulary, with the majority of positions set to zero, calculating a weight only for tokens present in the text. e.g., BM25, [unicoil](https://arxiv.org/pdf/2106.14807.pdf), and [splade](https://arxiv.org/abs/2107.05720)
|
| 79 |
-
- Multi-vector retrieval: use multiple vectors to represent a text, e.g., [ColBERT](https://arxiv.org/abs/2004.12832).
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
**2. How to use BGE-M3 in other projects?**
|
| 83 |
-
|
| 84 |
-
For embedding retrieval, you can employ the BGE-M3 model using the same approach as BGE.
|
| 85 |
-
The only difference is that the BGE-M3 model no longer requires adding instructions to the queries.
|
| 86 |
-
|
| 87 |
-
For hybrid retrieval, you can use [Vespa](https://github.com/vespa-engine/pyvespa/blob/master/docs/sphinx/source/examples/mother-of-all-embedding-models-cloud.ipynb
|
| 88 |
-
) and [Milvus](https://github.com/milvus-io/pymilvus/blob/master/examples/hello_hybrid_sparse_dense.py).
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
**3. How to fine-tune bge-M3 model?**
|
| 92 |
-
|
| 93 |
-
You can follow the common in this [example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/finetune)
|
| 94 |
-
to fine-tune the dense embedding.
|
| 95 |
-
|
| 96 |
-
If you want to fine-tune all embedding function of m3 (dense, sparse and colbert), you can refer to the [unified_fine-tuning example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/unified_finetune)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
## Usage
|
| 104 |
-
|
| 105 |
-
Install:
|
| 106 |
-
```
|
| 107 |
-
git clone https://github.com/FlagOpen/FlagEmbedding.git
|
| 108 |
-
cd FlagEmbedding
|
| 109 |
-
pip install -e .
|
| 110 |
-
```
|
| 111 |
-
or:
|
| 112 |
-
```
|
| 113 |
-
pip install -U FlagEmbedding
|
| 114 |
-
```
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
### Generate Embedding for text
|
| 119 |
-
|
| 120 |
-
- Dense Embedding
|
| 121 |
-
```python
|
| 122 |
-
from FlagEmbedding import BGEM3FlagModel
|
| 123 |
-
|
| 124 |
-
model = BGEM3FlagModel('BAAI/bge-m3',
|
| 125 |
-
use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
| 126 |
-
|
| 127 |
-
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
|
| 128 |
-
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
|
| 129 |
-
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
|
| 130 |
-
|
| 131 |
-
embeddings_1 = model.encode(sentences_1,
|
| 132 |
-
batch_size=12,
|
| 133 |
-
max_length=8192, # If you don't need such a long length, you can set a smaller value to speed up the encoding process.
|
| 134 |
-
)['dense_vecs']
|
| 135 |
-
embeddings_2 = model.encode(sentences_2)['dense_vecs']
|
| 136 |
-
similarity = embeddings_1 @ embeddings_2.T
|
| 137 |
-
print(similarity)
|
| 138 |
-
# [[0.6265, 0.3477], [0.3499, 0.678 ]]
|
| 139 |
-
```
|
| 140 |
-
You also can use sentence-transformers and huggingface transformers to generate dense embeddings.
|
| 141 |
-
Refer to [baai_general_embedding](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/baai_general_embedding#usage) for details.
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
- Sparse Embedding (Lexical Weight)
|
| 145 |
-
```python
|
| 146 |
-
from FlagEmbedding import BGEM3FlagModel
|
| 147 |
-
|
| 148 |
-
model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
| 149 |
-
|
| 150 |
-
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
|
| 151 |
-
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
|
| 152 |
-
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
|
| 153 |
-
|
| 154 |
-
output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
|
| 155 |
-
output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
|
| 156 |
-
|
| 157 |
-
# you can see the weight for each token:
|
| 158 |
-
print(model.convert_id_to_token(output_1['lexical_weights']))
|
| 159 |
-
# [{'What': 0.08356, 'is': 0.0814, 'B': 0.1296, 'GE': 0.252, 'M': 0.1702, '3': 0.2695, '?': 0.04092},
|
| 160 |
-
# {'De': 0.05005, 'fin': 0.1368, 'ation': 0.04498, 'of': 0.0633, 'BM': 0.2515, '25': 0.3335}]
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
# compute the scores via lexical mathcing
|
| 164 |
-
lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
|
| 165 |
-
print(lexical_scores)
|
| 166 |
-
# 0.19554901123046875
|
| 167 |
-
|
| 168 |
-
print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
|
| 169 |
-
# 0.0
|
| 170 |
-
```
|
| 171 |
-
|
| 172 |
-
- Multi-Vector (ColBERT)
|
| 173 |
-
```python
|
| 174 |
-
from FlagEmbedding import BGEM3FlagModel
|
| 175 |
-
|
| 176 |
-
model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
|
| 177 |
-
|
| 178 |
-
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
|
| 179 |
-
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
|
| 180 |
-
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
|
| 181 |
-
|
| 182 |
-
output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
|
| 183 |
-
output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
|
| 184 |
-
|
| 185 |
-
print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
|
| 186 |
-
print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
|
| 187 |
-
# 0.7797
|
| 188 |
-
# 0.4620
|
| 189 |
-
```
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
### Compute score for text pairs
|
| 193 |
-
Input a list of text pairs, you can get the scores computed by different methods.
|
| 194 |
-
```python
|
| 195 |
-
from FlagEmbedding import BGEM3FlagModel
|
| 196 |
-
|
| 197 |
-
model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
|
| 198 |
-
|
| 199 |
-
sentences_1 = ["What is BGE M3?", "Defination of BM25"]
|
| 200 |
-
sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
|
| 201 |
-
"BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
|
| 202 |
-
|
| 203 |
-
sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
|
| 204 |
-
|
| 205 |
-
print(model.compute_score(sentence_pairs,
|
| 206 |
-
max_passage_length=128, # a smaller max length leads to a lower latency
|
| 207 |
-
weights_for_different_modes=[0.4, 0.2, 0.4])) # weights_for_different_modes(w) is used to do weighted sum: w[0]*dense_score + w[1]*sparse_score + w[2]*colbert_score
|
| 208 |
-
|
| 209 |
-
# {
|
| 210 |
-
# 'colbert': [0.7796499729156494, 0.4621465802192688, 0.4523794651031494, 0.7898575067520142],
|
| 211 |
-
# 'sparse': [0.195556640625, 0.00879669189453125, 0.0, 0.1802978515625],
|
| 212 |
-
# 'dense': [0.6259765625, 0.347412109375, 0.349853515625, 0.67822265625],
|
| 213 |
-
# 'sparse+dense': [0.482503205537796, 0.23454029858112335, 0.2332356721162796, 0.5122477412223816],
|
| 214 |
-
# 'colbert+sparse+dense': [0.6013619303703308, 0.3255828022956848, 0.32089319825172424, 0.6232916116714478]
|
| 215 |
-
# }
|
| 216 |
-
```
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
## Evaluation
|
| 222 |
-
|
| 223 |
-
We provide the evaluation script for [MKQA](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MKQA) and [MLDR](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MLDR)
|
| 224 |
-
|
| 225 |
-
### Benchmarks from the open-source community
|
| 226 |
-

|
| 227 |
-
The BGE-M3 model emerged as the top performer on this benchmark (OAI is short for OpenAI).
|
| 228 |
-
For more details, please refer to the [article](https://towardsdatascience.com/openai-vs-open-source-multilingual-embedding-models-e5ccb7c90f05) and [Github Repo](https://github.com/Yannael/multilingual-embeddings)
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
### Our results
|
| 232 |
-
- Multilingual (Miracl dataset)
|
| 233 |
-
|
| 234 |
-

|
| 235 |
-
|
| 236 |
-
- Cross-lingual (MKQA dataset)
|
| 237 |
-
|
| 238 |
-

|
| 239 |
-
|
| 240 |
-
- Long Document Retrieval
|
| 241 |
-
- MLDR:
|
| 242 |
-

|
| 243 |
-
Please note that [MLDR](https://huggingface.co/datasets/Shitao/MLDR) is a document retrieval dataset we constructed via LLM,
|
| 244 |
-
covering 13 languages, including test set, validation set, and training set.
|
| 245 |
-
We utilized the training set from MLDR to enhance the model's long document retrieval capabilities.
|
| 246 |
-
Therefore, comparing baselines with `Dense w.o.long`(fine-tuning without long document dataset) is more equitable.
|
| 247 |
-
Additionally, this long document retrieval dataset will be open-sourced to address the current lack of open-source multilingual long text retrieval datasets.
|
| 248 |
-
We believe that this data will be helpful for the open-source community in training document retrieval models.
|
| 249 |
-
|
| 250 |
-
- NarritiveQA:
|
| 251 |
-

|
| 252 |
-
|
| 253 |
-
- Comparison with BM25
|
| 254 |
-
|
| 255 |
-
We utilized Pyserini to implement BM25, and the test results can be reproduced by this [script](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MLDR#bm25-baseline).
|
| 256 |
-
We tested BM25 using two different tokenizers:
|
| 257 |
-
one using Lucene Analyzer and the other using the same tokenizer as M3 (i.e., the tokenizer of xlm-roberta).
|
| 258 |
-
The results indicate that BM25 remains a competitive baseline,
|
| 259 |
-
especially in long document retrieval.
|
| 260 |
-
|
| 261 |
-

|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
## Training
|
| 266 |
-
- Self-knowledge Distillation: combining multiple outputs from different
|
| 267 |
-
retrieval modes as reward signal to enhance the performance of single mode(especially for sparse retrieval and multi-vec(colbert) retrival)
|
| 268 |
-
- Efficient Batching: Improve the efficiency when fine-tuning on long text.
|
| 269 |
-
The small-batch strategy is simple but effective, which also can used to fine-tune large embedding model.
|
| 270 |
-
- MCLS: A simple method to improve the performance on long text without fine-tuning.
|
| 271 |
-
If you have no enough resource to fine-tuning model with long text, the method is useful.
|
| 272 |
-
|
| 273 |
-
Refer to our [report](https://arxiv.org/pdf/2402.03216.pdf) for more details.
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
## Acknowledgement
|
| 281 |
-
|
| 282 |
-
Thanks to the authors of open-sourced datasets, including Miracl, MKQA, NarritiveQA, etc.
|
| 283 |
-
Thanks to the open-sourced libraries like [Tevatron](https://github.com/texttron/tevatron), [Pyserini](https://github.com/castorini/pyserini).
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
## Citation
|
| 288 |
-
|
| 289 |
-
If you find this repository useful, please consider giving a star :star: and citation
|
| 290 |
-
|
| 291 |
-
```
|
| 292 |
-
@misc{bge-m3,
|
| 293 |
-
title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
|
| 294 |
-
author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
|
| 295 |
-
year={2024},
|
| 296 |
-
eprint={2402.03216},
|
| 297 |
-
archivePrefix={arXiv},
|
| 298 |
-
primaryClass={cs.CL}
|
| 299 |
-
}
|
| 300 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modeling_baa.py
CHANGED
|
@@ -7,7 +7,7 @@ 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.
|
| 11 |
|
| 12 |
Usage:
|
| 13 |
from modeling_baa import BaaEmbeddingReranker
|
|
@@ -49,6 +49,10 @@ class BaaEmbeddingReranker:
|
|
| 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 |
|
| 53 |
def embed(self, texts, is_query=False, batch_size=32):
|
| 54 |
"""Return L2-normalized bi-encoder vectors. Applies the model's query/doc prompt if configured."""
|
|
|
|
| 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
|
|
|
|
| 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."""
|