Skip to content

Commit

Permalink
fix(server): fix content assignment (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxiao committed May 24, 2022
1 parent 926621b commit 0054b47
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
15 changes: 10 additions & 5 deletions server/clip_server/executors/clip_onnx.py
Expand Up @@ -5,8 +5,6 @@
from typing import Optional, Dict

import onnxruntime as ort
from jina import Executor, requests, DocumentArray

from clip_server.executors.helper import (
split_img_txt_da,
preproc_image,
Expand All @@ -15,6 +13,7 @@
)
from clip_server.model import clip
from clip_server.model.clip_onnx import CLIPOnnxModel
from jina import Executor, requests, DocumentArray


class CLIPEncoder(Executor):
Expand Down Expand Up @@ -103,11 +102,14 @@ async def encode(self, docs: 'DocumentArray', **kwargs):
):
minibatch.embeddings = self._model.encode_image(minibatch.tensors)
# recover original content
if _contents:
try:
_ = iter(_contents)
for _d, _ct in zip(minibatch, _contents):
_d.content = _ct
except TypeError:
pass

# for text
# for text
if _txt_da:
for minibatch, _contents in _txt_da.map_batch(
partial(preproc_text, return_np=True),
Expand All @@ -116,9 +118,12 @@ async def encode(self, docs: 'DocumentArray', **kwargs):
):
minibatch.embeddings = self._model.encode_text(minibatch.tensors)
# recover original content
if _contents:
try:
_ = iter(_contents)
for _d, _ct in zip(minibatch, _contents):
_d.content = _ct
except TypeError:
pass

# drop tensors
docs.tensors = None
Expand Down
17 changes: 11 additions & 6 deletions server/clip_server/executors/clip_tensorrt.py
Expand Up @@ -3,8 +3,6 @@
from typing import Dict

import numpy as np
from jina import Executor, requests, DocumentArray

from clip_server.executors.helper import (
split_img_txt_da,
preproc_image,
Expand All @@ -13,6 +11,7 @@
)
from clip_server.model import clip
from clip_server.model.clip_trt import CLIPTensorRTModel
from jina import Executor, requests, DocumentArray


class CLIPEncoder(Executor):
Expand Down Expand Up @@ -80,11 +79,14 @@ async def encode(self, docs: 'DocumentArray', **kwargs):
.astype(np.float32)
)
# recover original content
if _contents:
try:
_ = iter(_contents)
for _d, _ct in zip(minibatch, _contents):
_d.content = _ct
except TypeError:
pass

# for text
# for text
if _txt_da:
for minibatch, _contents in _txt_da.map_batch(
partial(preproc_text, device=self._device, return_np=False),
Expand All @@ -99,11 +101,14 @@ async def encode(self, docs: 'DocumentArray', **kwargs):
.astype(np.float32)
)
# recover original content
if _contents:
try:
_ = iter(_contents)
for _d, _ct in zip(minibatch, _contents):
_d.content = _ct
except TypeError:
pass

# drop tensors
# drop tensors
docs.tensors = None

return docs
13 changes: 9 additions & 4 deletions server/clip_server/executors/clip_torch.py
Expand Up @@ -6,15 +6,14 @@

import numpy as np
import torch
from jina import Executor, requests, DocumentArray

from clip_server.executors.helper import (
split_img_txt_da,
preproc_image,
preproc_text,
set_rank,
)
from clip_server.model import clip
from jina import Executor, requests, DocumentArray


class CLIPEncoder(Executor):
Expand Down Expand Up @@ -95,9 +94,12 @@ async def encode(self, docs: 'DocumentArray', **kwargs):
)

# recover original content
if _contents:
try:
_ = iter(_contents)
for _d, _ct in zip(minibatch, _contents):
_d.content = _ct
except TypeError:
pass

# for text
if _txt_da:
Expand All @@ -114,9 +116,12 @@ async def encode(self, docs: 'DocumentArray', **kwargs):
)

# recover original content
if _contents:
try:
_ = iter(_contents)
for _d, _ct in zip(minibatch, _contents):
_d.content = _ct
except TypeError:
pass

# drop tensors
docs.tensors = None
Expand Down

0 comments on commit 0054b47

Please sign in to comment.