Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace traversal_paths with access_paths #7

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions dpr_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def __init__(
base_tokenizer_model: Optional[str] = None,
title_tag_key: Optional[str] = None,
max_length: Optional[int] = None,
traversal_paths: str = "@r",
access_paths: str = '@r',
traversal_paths: Optional[str] = None,
batch_size: int = 32,
device: str = "cpu",
*args,
Expand All @@ -45,8 +46,9 @@ def __init__(
tag property. It is recommended to set this property for context encoders,
to match the model pre-training. It has no effect for question encoders.
:param max_length: Max length argument for the tokenizer
:param traversal_paths: Default traversal paths for encoding, used if the
traversal path is not passed as a parameter with the request.
:param access_paths: Default access paths for encoding, used if the
access path is not passed as a parameter with the request.
:param traversal_paths: Please use `access_paths`
:param batch_size: Default batch size for encoding, used if the
batch size is not passed as a parameter with the request.
:param device: The device (cpu or gpu) that the model should be on.
Expand Down Expand Up @@ -102,7 +104,16 @@ def __init__(

self.model = self.model.to(self.device).eval()

self.traversal_paths = traversal_paths
self.access_paths = access_paths
if traversal_paths is not None:
import warnings
warnings.warn(
f'`traversal_paths` is renamed to `access_paths` with the same usage, please use the latter instead. '
f'`traversal_paths` will be removed soon.',
DeprecationWarning,
)
self.access_paths = traversal_paths

self.batch_size = batch_size

@requests
Expand All @@ -117,13 +128,23 @@ def encode(
``text`` attribute.
:param parameters: dictionary to define the ``traversal_path`` and the
``batch_size``. For example,
``parameters={'traversal_paths': '@r', 'batch_size': 10}``
``parameters={'access_paths': '@r', 'batch_size': 10}``
"""

access_paths = parameters.get('traversal_paths', None)
if access_paths is not None:
import warnings
warnings.warn(
f'`traversal_paths` is renamed to `access_paths` with the same usage, please use the latter instead. '
f'`traversal_paths` will be removed soon.',
DeprecationWarning,
)
parameters['access_paths'] = access_paths

document_batches_generator = DocumentArray(
filter(
lambda x: bool(x.text),
docs[parameters.get("traversal_paths", self.traversal_paths)],
docs[parameters.get("access_paths", self.access_paths)],
)
).batch(batch_size=parameters.get("batch_size", self.batch_size))

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_encoding_gpu():


@pytest.mark.parametrize(
"traversal_paths, counts",
"access_paths, counts",
[
("@r", [["@r", 1], ["@c", 0], ["@cc", 0]]),
("@c", [["@r", 0], ["@c", 3], ["@cc", 0]]),
Expand All @@ -92,7 +92,7 @@ def test_encoding_gpu():
],
)
def test_traversal_path(
traversal_paths: List[str], counts: List, basic_encoder: DPRTextEncoder
access_paths: List[str], counts: List, basic_encoder: DPRTextEncoder
):
text = "blah"
docs = DocumentArray([Document(id="root1", text=text)])
Expand All @@ -106,7 +106,7 @@ def test_traversal_path(
Document(id="chunk112", text=text),
]

basic_encoder.encode(docs=docs, parameters={"traversal_paths": traversal_paths})
basic_encoder.encode(docs=docs, parameters={"access_paths": access_paths})
for path, count in counts:
embeddings = docs[path].embeddings

Expand Down